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