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