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