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