e6b067522ec48bc36cdef7cb27ab020250f04895
[ardour.git] / libs / gtkmm2ext / pane.cc
1 /*
2     Copyright (C) 2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <gdkmm/cursor.h>
21 #include "gtkmm2ext/pane.h"
22
23 #include "pbd/i18n.h"
24
25 using namespace PBD;
26 using namespace Gtk;
27 using namespace Gtkmm2ext;
28 using namespace std;
29
30 Pane::Pane (bool h)
31         : horizontal (h)
32         , did_move (false)
33         , divider_width (2)
34         , check_fract (false)
35 {
36         using namespace Gdk;
37
38         set_name ("Pane");
39         set_has_window (false);
40
41         if (horizontal) {
42                 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
43         } else {
44                 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
45         }
46 }
47
48 Pane::~Pane ()
49 {
50         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
51                 c->w->remove_destroy_notify_callback (&(*c));
52                 c->w->unparent ();
53         }
54 }
55
56 void
57 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
58 {
59         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
60                 if (c->w == &w) {
61                         c->minsize = minsize;
62                         break;
63                 }
64         }
65 }
66
67 void
68 Pane::set_drag_cursor (Gdk::Cursor c)
69 {
70         drag_cursor = c;
71 }
72
73 void
74 Pane::on_size_request (GtkRequisition* req)
75 {
76         GtkRequisition largest;
77
78         /* iterate over all children, get their size requests */
79
80         /* horizontal pane is as high as its tallest child, including the dividers.
81          * Its width is the sum of the children plus the dividers.
82          *
83          * vertical pane is as wide as its widest child, including the dividers.
84          * Its height is the sum of the children plus the dividers.
85          */
86
87         if (horizontal) {
88                 largest.width = (children.size()  - 1) * divider_width;
89                 largest.height = 0;
90         } else {
91                 largest.height = (children.size() - 1) * divider_width;
92                 largest.width = 0;
93         }
94
95         for (Children::iterator child = children.begin(); child != children.end(); ++child) {
96                 GtkRequisition r;
97
98                 child->w->size_request (r);
99
100                 if (horizontal) {
101                         largest.height = max (largest.height, r.height);
102                         largest.width += r.width;
103                 } else {
104                         largest.width = max (largest.width, r.width);
105                         largest.height += r.height;
106                 }
107         }
108
109         *req = largest;
110 }
111
112 GType
113 Pane::child_type_vfunc() const
114 {
115         /* We accept any number of any types of widgets */
116         return Gtk::Widget::get_type();
117 }
118
119 void
120 Pane::add_divider ()
121 {
122         Divider* d = new Divider;
123         d->set_name (X_("Divider"));
124         d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
125         d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
126         d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
127         d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
128         d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
129         d->set_parent (*this);
130         d->show ();
131         d->fract = 0.5;
132         dividers.push_back (d);
133 }
134
135 void
136 Pane::handle_child_visibility ()
137 {
138         reallocate (get_allocation());
139 }
140
141 void
142 Pane::on_add (Widget* w)
143 {
144         children.push_back (Child (this, w, 0));
145
146         w->set_parent (*this);
147         /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
148            for custom containers that derive from Gtk::Container. So ... we need
149            to ensure that we hear about child destruction ourselves.
150         */
151         w->add_destroy_notify_callback (&children.back(), &Pane::notify_child_destroyed);
152
153         w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
154         w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
155
156         while (dividers.size() < (children.size() - 1)) {
157                 add_divider ();
158         }
159 }
160
161 void*
162 Pane::notify_child_destroyed (void* data)
163 {
164         Child* child = reinterpret_cast<Child*> (data);
165         return child->pane->child_destroyed (child->w);
166 }
167
168 void*
169 Pane::child_destroyed (Gtk::Widget* w)
170 {
171         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
172                 if (c->w == w) {
173                         children.erase (c);
174                         break;
175                 }
176         }
177         return 0;
178 }
179
180 void
181 Pane::on_remove (Widget* w)
182 {
183         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
184                 if (c->w == w) {
185                         w->remove_destroy_notify_callback (&(*c));
186                         w->unparent ();
187                         children.erase (c);
188                         break;
189                 }
190         }
191 }
192
193 void
194 Pane::on_size_allocate (Gtk::Allocation& alloc)
195 {
196         std::cerr << "pane @ " << this << " reallocated as " << alloc.get_width() << " x " << alloc.get_height() << std::endl;
197         reallocate (alloc);
198         Container::on_size_allocate (alloc);
199 }
200
201 void
202 Pane::reallocate (Gtk::Allocation const & alloc)
203 {
204         int remaining;
205         int xpos = alloc.get_x();
206         int ypos = alloc.get_y();
207         float fract;
208
209         if (children.empty()) {
210                 return;
211         }
212
213         if (children.size() == 1) {
214                 /* only child gets the full allocation */
215                 children.front().w->size_allocate (alloc);
216                 return;
217         }
218
219         if (horizontal) {
220                 remaining = alloc.get_width ();
221         } else {
222                 remaining = alloc.get_height ();
223         }
224
225         Children::iterator child;
226         Children::iterator next;
227         Dividers::iterator div;
228
229         child = children.begin();
230
231         /* skip initial hidden children */
232
233         while (child != children.end()) {
234                 if (child->w->is_visible()) {
235                         break;
236                 }
237                 ++child;
238         }
239
240         for (div = dividers.begin(); child != children.end(); ) {
241
242                 Gtk::Allocation child_alloc;
243
244                 next = child;
245
246                 /* Move on to next *visible* child */
247
248                 while (++next != children.end()) {
249                         if (next->w->is_visible()) {
250                                 break;
251                         }
252                 }
253
254                 child_alloc.set_x (xpos);
255                 child_alloc.set_y (ypos);
256
257                 if (next == children.end()) {
258                         /* last child gets all the remaining space */
259                         fract = 1.0;
260                 } else {
261                         /* child gets the fraction of the remaining space given by the divider that follows it */
262                         fract = (*div)->fract;
263                 }
264
265                 Gtk::Requisition cr;
266                 child->w->size_request (cr);
267
268                 if (horizontal) {
269                         child_alloc.set_width ((gint) floor (remaining * fract));
270                         child_alloc.set_height (alloc.get_height());
271                         remaining = max (0, (remaining - child_alloc.get_width()));
272                         xpos += child_alloc.get_width();
273                 } else {
274                         child_alloc.set_width (alloc.get_width());
275                         child_alloc.set_height ((gint) floor (remaining * fract));
276                         remaining = max (0, (remaining - child_alloc.get_height()));
277                         ypos += child_alloc.get_height ();
278                 }
279
280                 if (child->minsize) {
281                         if (horizontal) {
282                                 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
283                         } else {
284                                 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
285                         }
286                 }
287
288                 child->w->size_allocate (child_alloc);
289
290                 if (next == children.end()) {
291                         /* done, no more children, no need for a divider */
292                         break;
293                 }
294
295                 child = next;
296
297                 /* add a divider between children */
298
299                 Gtk::Allocation divider_allocation;
300
301                 divider_allocation.set_x (xpos);
302                 divider_allocation.set_y (ypos);
303
304                 if (horizontal) {
305                         divider_allocation.set_width (divider_width);
306                         divider_allocation.set_height (alloc.get_height());
307                         remaining = max (0, remaining - divider_width);
308                         xpos += divider_width;
309                 } else {
310                         divider_allocation.set_width (alloc.get_width());
311                         divider_allocation.set_height (divider_width);
312                         remaining = max (0, remaining - divider_width);
313                         ypos += divider_width;
314                 }
315
316                 (*div)->size_allocate (divider_allocation);
317                 (*div)->show ();
318                 ++div;
319         }
320
321         /* hide all remaining dividers */
322
323         while (div != dividers.end()) {
324                 (*div)->hide ();
325                 ++div;
326         }
327 }
328
329 bool
330 Pane::on_expose_event (GdkEventExpose* ev)
331 {
332         Children::iterator child;
333         Dividers::iterator div;
334
335         for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
336
337                 if (child->w->is_visible()) {
338                         propagate_expose (*(child->w), ev);
339                 }
340
341                 if (div != dividers.end()) {
342                         if ((*div)->is_visible()) {
343                                 propagate_expose (**div, ev);
344                         }
345                         ++div;
346                 }
347         }
348
349         return true;
350 }
351
352 bool
353 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
354 {
355         d->dragging = true;
356         d->queue_draw ();
357
358         return false;
359 }
360
361 bool
362 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
363 {
364         d->dragging = false;
365
366         if (did_move && !children.empty()) {
367                 children.front().w->queue_resize ();
368                 did_move = false;
369         }
370
371         return false;
372 }
373 void
374 Pane::set_check_divider_position (bool yn)
375 {
376         check_fract = yn;
377 }
378
379 bool
380 Pane::fract_is_ok (Dividers::size_type div, float fract)
381 {
382 #ifdef __APPLE__
383         if (!check_fract) {
384                 return true;
385         }
386
387
388         if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
389                 /* space not * allocated - * divider being set from startup code. Let it pass,
390                    since our goal is mostly to catch drags to a position that will interfere with window
391                    resizing.
392                 */
393                 return true;
394         }
395
396         /* On Quartz, if the pane handle (divider) gets to
397            be adjacent to the window edge, you can no longer grab it:
398            any attempt to do so is interpreted by the Quartz window
399            manager ("Finder") as a resize drag on the window edge.
400         */
401
402         if (horizontal) {
403                 if (div == dividers.size() - 1) {
404                         if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
405                                 /* too close to right edge */
406                                 return false;
407                         }
408                 }
409
410                 if (div == 0) {
411                         if (get_allocation().get_width() * fract < (divider_width*2)) {
412                                 /* too close to left edge */
413                                 return false;
414                         }
415                 }
416         } else {
417                 if (div == dividers.size() - 1) {
418                         if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
419                                 /* too close to bottom */
420                                 return false;
421                         }
422                 }
423
424                 if (div == 0) {
425                         if (get_allocation().get_width() * fract < (divider_width*2)) {
426                                 /* too close to top */
427                                 return false;
428                         }
429                 }
430         }
431 #endif
432         return true;
433 }
434
435 bool
436 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
437 {
438         did_move = true;
439
440         if (!d->dragging) {
441                 return true;
442         }
443
444         /* determine new position for handle */
445
446         float new_fract;
447         int px, py;
448
449         d->translate_coordinates (*this, ev->x, ev->y, px, py);
450
451         Dividers::iterator prev = dividers.end();
452         Dividers::size_type div = 0;
453
454         for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
455                 if (*di == d) {
456                         break;
457                 }
458                 prev = di;
459         }
460
461         int space_remaining;
462         int prev_edge;
463
464         if (horizontal) {
465                 if (prev != dividers.end()) {
466                         prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
467                 } else {
468                         prev_edge = 0;
469                 }
470                 space_remaining = get_allocation().get_width() - prev_edge;
471                 new_fract = (float) (px - prev_edge) / space_remaining;
472         } else {
473                 if (prev != dividers.end()) {
474                         prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
475                 } else {
476                         prev_edge = 0;
477                 }
478                 space_remaining = get_allocation().get_height() - prev_edge;
479                 new_fract = (float) (py - prev_edge) / space_remaining;
480         }
481
482         new_fract = min (1.0f, max (0.0f, new_fract));
483
484         if (!fract_is_ok (div, new_fract)) {
485                 return true;
486         }
487
488         if (new_fract != d->fract) {
489                 d->fract = new_fract;
490                 reallocate (get_allocation ());
491                 queue_draw ();
492         }
493
494         return true;
495 }
496
497 void
498 Pane::set_divider (Dividers::size_type div, float fract)
499 {
500         Dividers::iterator d = dividers.begin();
501
502         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
503                 /* relax */
504         }
505
506         if (d == dividers.end()) {
507                 /* caller is trying to set divider that does not exist
508                  * yet.
509                  */
510                 return;
511         }
512
513         fract = max (0.0f, min (1.0f, fract));
514
515         if (!fract_is_ok (div, fract)) {
516                 return;
517         }
518
519         if (fract != (*d)->fract) {
520                 (*d)->fract = fract;
521                 /* our size hasn't changed, but our internal allocations have */
522                 reallocate (get_allocation());
523                 queue_draw ();
524         }
525 }
526
527 float
528 Pane::get_divider (Dividers::size_type div)
529 {
530         Dividers::iterator d = dividers.begin();
531
532         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
533                 /* relax */
534         }
535
536         if (d == dividers.end()) {
537                 /* caller is trying to set divider that does not exist
538                  * yet.
539                  */
540                 return -1.0f;
541         }
542
543         return (*d)->fract;
544 }
545
546 void
547 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
548 {
549         /* since the callback could modify the child list(s), make sure we keep
550          * the iterators safe;
551          */
552
553         for (Children::iterator c = children.begin(); c != children.end(); ) {
554                 Children::iterator next = c;
555                 ++next;
556                 callback (c->w->gobj(), callback_data);
557                 c = next;
558         }
559
560         if (include_internals) {
561                 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
562                         Dividers::iterator next = d;
563                         ++next;
564                         callback (GTK_WIDGET((*d)->gobj()), callback_data);
565                         d = next;
566                 }
567         }
568 }
569
570 Pane::Divider::Divider ()
571         : fract (0.0)
572         , dragging (false)
573 {
574         set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
575                                     Gdk::BUTTON_RELEASE|
576                                     Gdk::MOTION_NOTIFY|
577                                     Gdk::ENTER_NOTIFY|
578                                     Gdk::LEAVE_NOTIFY));
579 }
580
581 bool
582 Pane::Divider::on_expose_event (GdkEventExpose* ev)
583 {
584         Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
585                         get_style()->get_fg (get_state()));
586
587         Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
588         draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
589         draw_context->clip_preserve ();
590         draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
591         draw_context->fill ();
592
593         return true;
594 }
595
596 bool
597 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
598 {
599         d->get_window()->set_cursor (drag_cursor);
600         d->set_state (Gtk::STATE_SELECTED);
601         return true;
602 }
603
604 bool
605 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
606 {
607         d->get_window()->set_cursor ();
608         d->set_state (Gtk::STATE_NORMAL);
609         d->queue_draw ();
610         return true;
611 }