remove debug output
[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         reallocate (alloc);
197         Container::on_size_allocate (alloc);
198 }
199
200 void
201 Pane::reallocate (Gtk::Allocation const & alloc)
202 {
203         int remaining;
204         int xpos = alloc.get_x();
205         int ypos = alloc.get_y();
206         float fract;
207
208         if (children.empty()) {
209                 return;
210         }
211
212         if (children.size() == 1) {
213                 /* only child gets the full allocation */
214                 children.front().w->size_allocate (alloc);
215                 return;
216         }
217
218         if (horizontal) {
219                 remaining = alloc.get_width ();
220         } else {
221                 remaining = alloc.get_height ();
222         }
223
224         Children::iterator child;
225         Children::iterator next;
226         Dividers::iterator div;
227
228         child = children.begin();
229
230         /* skip initial hidden children */
231
232         while (child != children.end()) {
233                 if (child->w->is_visible()) {
234                         break;
235                 }
236                 ++child;
237         }
238
239         for (div = dividers.begin(); child != children.end(); ) {
240
241                 Gtk::Allocation child_alloc;
242
243                 next = child;
244
245                 /* Move on to next *visible* child */
246
247                 while (++next != children.end()) {
248                         if (next->w->is_visible()) {
249                                 break;
250                         }
251                 }
252
253                 child_alloc.set_x (xpos);
254                 child_alloc.set_y (ypos);
255
256                 if (next == children.end()) {
257                         /* last child gets all the remaining space */
258                         fract = 1.0;
259                 } else {
260                         /* child gets the fraction of the remaining space given by the divider that follows it */
261                         fract = (*div)->fract;
262                 }
263
264                 Gtk::Requisition cr;
265                 child->w->size_request (cr);
266
267                 if (horizontal) {
268                         child_alloc.set_width ((gint) floor (remaining * fract));
269                         child_alloc.set_height (alloc.get_height());
270                         remaining = max (0, (remaining - child_alloc.get_width()));
271                         xpos += child_alloc.get_width();
272                 } else {
273                         child_alloc.set_width (alloc.get_width());
274                         child_alloc.set_height ((gint) floor (remaining * fract));
275                         remaining = max (0, (remaining - child_alloc.get_height()));
276                         ypos += child_alloc.get_height ();
277                 }
278
279                 if (child->minsize) {
280                         if (horizontal) {
281                                 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
282                         } else {
283                                 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
284                         }
285                 }
286
287                 child->w->size_allocate (child_alloc);
288
289                 if (next == children.end()) {
290                         /* done, no more children, no need for a divider */
291                         break;
292                 }
293
294                 child = next;
295
296                 /* add a divider between children */
297
298                 Gtk::Allocation divider_allocation;
299
300                 divider_allocation.set_x (xpos);
301                 divider_allocation.set_y (ypos);
302
303                 if (horizontal) {
304                         divider_allocation.set_width (divider_width);
305                         divider_allocation.set_height (alloc.get_height());
306                         remaining = max (0, remaining - divider_width);
307                         xpos += divider_width;
308                 } else {
309                         divider_allocation.set_width (alloc.get_width());
310                         divider_allocation.set_height (divider_width);
311                         remaining = max (0, remaining - divider_width);
312                         ypos += divider_width;
313                 }
314
315                 (*div)->size_allocate (divider_allocation);
316                 (*div)->show ();
317                 ++div;
318         }
319
320         /* hide all remaining dividers */
321
322         while (div != dividers.end()) {
323                 (*div)->hide ();
324                 ++div;
325         }
326 }
327
328 bool
329 Pane::on_expose_event (GdkEventExpose* ev)
330 {
331         Children::iterator child;
332         Dividers::iterator div;
333
334         for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
335
336                 if (child->w->is_visible()) {
337                         propagate_expose (*(child->w), ev);
338                 }
339
340                 if (div != dividers.end()) {
341                         if ((*div)->is_visible()) {
342                                 propagate_expose (**div, ev);
343                         }
344                         ++div;
345                 }
346         }
347
348         return true;
349 }
350
351 bool
352 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
353 {
354         d->dragging = true;
355         d->queue_draw ();
356
357         return false;
358 }
359
360 bool
361 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
362 {
363         d->dragging = false;
364
365         if (did_move && !children.empty()) {
366                 children.front().w->queue_resize ();
367                 did_move = false;
368         }
369
370         return false;
371 }
372 void
373 Pane::set_check_divider_position (bool yn)
374 {
375         check_fract = yn;
376 }
377
378 bool
379 Pane::fract_is_ok (Dividers::size_type div, float fract)
380 {
381 #ifdef __APPLE__
382         if (!check_fract) {
383                 return true;
384         }
385
386
387         if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
388                 /* space not * allocated - * divider being set from startup code. Let it pass,
389                    since our goal is mostly to catch drags to a position that will interfere with window
390                    resizing.
391                 */
392                 return true;
393         }
394
395         /* On Quartz, if the pane handle (divider) gets to
396            be adjacent to the window edge, you can no longer grab it:
397            any attempt to do so is interpreted by the Quartz window
398            manager ("Finder") as a resize drag on the window edge.
399         */
400
401         if (horizontal) {
402                 if (div == dividers.size() - 1) {
403                         if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
404                                 /* too close to right edge */
405                                 return false;
406                         }
407                 }
408
409                 if (div == 0) {
410                         if (get_allocation().get_width() * fract < (divider_width*2)) {
411                                 /* too close to left edge */
412                                 return false;
413                         }
414                 }
415         } else {
416                 if (div == dividers.size() - 1) {
417                         if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
418                                 /* too close to bottom */
419                                 return false;
420                         }
421                 }
422
423                 if (div == 0) {
424                         if (get_allocation().get_width() * fract < (divider_width*2)) {
425                                 /* too close to top */
426                                 return false;
427                         }
428                 }
429         }
430 #endif
431         return true;
432 }
433
434 bool
435 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
436 {
437         did_move = true;
438
439         if (!d->dragging) {
440                 return true;
441         }
442
443         /* determine new position for handle */
444
445         float new_fract;
446         int px, py;
447
448         d->translate_coordinates (*this, ev->x, ev->y, px, py);
449
450         Dividers::iterator prev = dividers.end();
451         Dividers::size_type div = 0;
452
453         for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
454                 if (*di == d) {
455                         break;
456                 }
457                 prev = di;
458         }
459
460         int space_remaining;
461         int prev_edge;
462
463         if (horizontal) {
464                 if (prev != dividers.end()) {
465                         prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
466                 } else {
467                         prev_edge = 0;
468                 }
469                 space_remaining = get_allocation().get_width() - prev_edge;
470                 new_fract = (float) (px - prev_edge) / space_remaining;
471         } else {
472                 if (prev != dividers.end()) {
473                         prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
474                 } else {
475                         prev_edge = 0;
476                 }
477                 space_remaining = get_allocation().get_height() - prev_edge;
478                 new_fract = (float) (py - prev_edge) / space_remaining;
479         }
480
481         new_fract = min (1.0f, max (0.0f, new_fract));
482
483         if (!fract_is_ok (div, new_fract)) {
484                 return true;
485         }
486
487         if (new_fract != d->fract) {
488                 d->fract = new_fract;
489                 reallocate (get_allocation ());
490                 queue_draw ();
491         }
492
493         return true;
494 }
495
496 void
497 Pane::set_divider (Dividers::size_type div, float fract)
498 {
499         Dividers::iterator d = dividers.begin();
500
501         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
502                 /* relax */
503         }
504
505         if (d == dividers.end()) {
506                 /* caller is trying to set divider that does not exist
507                  * yet.
508                  */
509                 return;
510         }
511
512         fract = max (0.0f, min (1.0f, fract));
513
514         if (!fract_is_ok (div, fract)) {
515                 return;
516         }
517
518         if (fract != (*d)->fract) {
519                 (*d)->fract = fract;
520                 /* our size hasn't changed, but our internal allocations have */
521                 reallocate (get_allocation());
522                 queue_draw ();
523         }
524 }
525
526 float
527 Pane::get_divider (Dividers::size_type div)
528 {
529         Dividers::iterator d = dividers.begin();
530
531         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
532                 /* relax */
533         }
534
535         if (d == dividers.end()) {
536                 /* caller is trying to set divider that does not exist
537                  * yet.
538                  */
539                 return -1.0f;
540         }
541
542         return (*d)->fract;
543 }
544
545 void
546 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
547 {
548         /* since the callback could modify the child list(s), make sure we keep
549          * the iterators safe;
550          */
551
552         for (Children::iterator c = children.begin(); c != children.end(); ) {
553                 Children::iterator next = c;
554                 ++next;
555                 callback (c->w->gobj(), callback_data);
556                 c = next;
557         }
558
559         if (include_internals) {
560                 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
561                         Dividers::iterator next = d;
562                         ++next;
563                         callback (GTK_WIDGET((*d)->gobj()), callback_data);
564                         d = next;
565                 }
566         }
567 }
568
569 Pane::Divider::Divider ()
570         : fract (0.0)
571         , dragging (false)
572 {
573         set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
574                                     Gdk::BUTTON_RELEASE|
575                                     Gdk::MOTION_NOTIFY|
576                                     Gdk::ENTER_NOTIFY|
577                                     Gdk::LEAVE_NOTIFY));
578 }
579
580 bool
581 Pane::Divider::on_expose_event (GdkEventExpose* ev)
582 {
583         Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
584                         get_style()->get_fg (get_state()));
585
586         Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
587         draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
588         draw_context->clip_preserve ();
589         draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
590         draw_context->fill ();
591
592         return true;
593 }
594
595 bool
596 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
597 {
598         d->get_window()->set_cursor (drag_cursor);
599         d->set_state (Gtk::STATE_SELECTED);
600         return true;
601 }
602
603 bool
604 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
605 {
606         d->get_window()->set_cursor ();
607         d->set_state (Gtk::STATE_NORMAL);
608         d->queue_draw ();
609         return true;
610 }