explicitly expose dividers
[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 "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 (5)
34 {
35         using namespace Gdk;
36
37         set_name ("Pane");
38         set_has_window (false);
39
40         if (horizontal) {
41                 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
42         } else {
43                 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
44         }
45 }
46
47 void
48 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
49 {
50         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
51                 if (c->w == &w) {
52                         c->minsize = minsize;
53                         break;
54                 }
55         }
56 }
57
58 void
59 Pane::set_drag_cursor (Gdk::Cursor c)
60 {
61         drag_cursor = c;
62 }
63
64 void
65 Pane::on_size_request (GtkRequisition* req)
66 {
67         GtkRequisition largest;
68
69         /* iterate over all children, get their size requests */
70
71         /* horizontal pane is as high as its tallest child, including the dividers.
72          * Its width is the sum of the children plus the dividers.
73          *
74          * vertical pane is as wide as its widest child, including the dividers.
75          * Its height is the sum of the children plus the dividers.
76          */
77
78         if (horizontal) {
79                 largest.width = (children.size()  - 1) * divider_width;
80                 largest.height = 0;
81         } else {
82                 largest.height = (children.size() - 1) * divider_width;
83                 largest.width = 0;
84         }
85
86         for (Children::iterator child = children.begin(); child != children.end(); ++child) {
87                 GtkRequisition r;
88
89                 child->w->size_request (r);
90
91                 if (horizontal) {
92                         largest.height = max (largest.height, r.height);
93                         largest.width += r.width;
94                 } else {
95                         largest.width = max (largest.width, r.width);
96                         largest.height += r.height;
97                 }
98         }
99
100         *req = largest;
101 }
102
103 GType
104 Pane::child_type_vfunc() const
105 {
106         /* We accept any number of any types of widgets */
107         return Gtk::Widget::get_type();
108 }
109
110 void
111 Pane::add_divider ()
112 {
113         Divider* d = new Divider;
114         d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
115         d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
116         d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
117         d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
118         d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
119         d->set_parent (*this);
120         d->show ();
121         d->fract = 0.5;
122         dividers.push_back (d);
123 }
124
125 void
126 Pane::on_add (Widget* w)
127 {
128         children.push_back (Child (w, 0));
129
130         w->set_parent (*this);
131
132         while (dividers.size() < (children.size() - 1)) {
133                 add_divider ();
134         }
135 }
136
137 void
138 Pane::on_remove (Widget* w)
139 {
140         w->unparent ();
141
142         for (Children::iterator c = children.begin(); c != children.end(); ++c) {
143                 if (c->w == w) {
144                         children.erase (c);
145                         break;
146                 }
147         }
148 }
149
150 void
151 Pane::on_size_allocate (Gtk::Allocation& alloc)
152 {
153         reallocate (alloc);
154         Container::on_size_allocate (alloc);
155 }
156
157 void
158 Pane::reallocate (Gtk::Allocation const & alloc)
159 {
160         int remaining;
161         int xpos = alloc.get_x();
162         int ypos = alloc.get_y();
163         float fract;
164
165         if (children.empty()) {
166                 return;
167         }
168
169         if (children.size() == 1) {
170                 /* only child gets the full allocation */
171                 children.front().w->size_allocate (alloc);
172                 return;
173         }
174
175         if (horizontal) {
176                 remaining = alloc.get_width ();
177         } else {
178                 remaining = alloc.get_height ();
179         }
180
181         Children::iterator child;
182         Children::iterator next;
183         Dividers::iterator div;
184
185         for (child = children.begin(), div = dividers.begin(); child != children.end(); ) {
186
187                 Gtk::Allocation child_alloc;
188                 next = child;
189                 ++next;
190
191                 child_alloc.set_x (xpos);
192                 child_alloc.set_y (ypos);
193
194                 if (next == children.end()) {
195                         /* last child gets all the remaining space */
196                         fract = 1.0;
197                 } else {
198                         /* child gets the fraction of the remaining space given by the divider that follows it */
199                         fract = (*div)->fract;
200                 }
201
202                 Gtk::Requisition cr;
203                 child->w->size_request (cr);
204
205                 if (horizontal) {
206                         child_alloc.set_width ((gint) floor (remaining * fract));
207                         child_alloc.set_height (alloc.get_height());
208                         remaining = max (0, (remaining - child_alloc.get_width()));
209                         xpos += child_alloc.get_width();
210                 } else {
211                         child_alloc.set_width (alloc.get_width());
212                         child_alloc.set_height ((gint) floor (remaining * fract));
213                         remaining = max (0, (remaining - child_alloc.get_height()));
214                         ypos += child_alloc.get_height ();
215                 }
216
217                 if (child->minsize) {
218                         if (horizontal) {
219                                 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
220                         } else {
221                                 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
222                         }
223                 }
224
225                 child->w->size_allocate (child_alloc);
226                 ++child;
227
228                 if (child == children.end()) {
229                         /* done, no more children, no need for a divider */
230                         break;
231                 }
232
233                 /* add a divider between children */
234
235                 Gtk::Allocation divider_allocation;
236
237                 divider_allocation.set_x (xpos);
238                 divider_allocation.set_y (ypos);
239
240                 if (horizontal) {
241                         divider_allocation.set_width (divider_width);
242                         divider_allocation.set_height (alloc.get_height());
243                         remaining = max (0, remaining - divider_width);
244                         xpos += divider_width;
245                 } else {
246                         divider_allocation.set_width (alloc.get_width());
247                         divider_allocation.set_height (divider_width);
248                         remaining = max (0, remaining - divider_width);
249                         ypos += divider_width;
250                 }
251
252                 (*div)->size_allocate (divider_allocation);
253                 ++div;
254         }
255 }
256
257 bool
258 Pane::on_expose_event (GdkEventExpose* ev)
259 {
260         Children::iterator child;
261         Dividers::iterator div;
262
263         for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child, ++div) {
264
265                 if (child->w->is_visible()) {
266                         propagate_expose (*(child->w), ev);
267                 }
268
269                 if ((div != dividers.end()) && (*div)->is_visible()) {
270                         propagate_expose (**div, ev);
271                 }
272         }
273
274         return true;
275 }
276
277 bool
278 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
279 {
280         d->dragging = true;
281         d->queue_draw ();
282
283         return false;
284 }
285
286 bool
287 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
288 {
289         d->dragging = false;
290
291         if (did_move) {
292                 children.front().w->queue_resize ();
293                 did_move = false;
294         }
295
296         return false;
297 }
298
299 bool
300 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
301 {
302         did_move = true;
303
304         if (!d->dragging) {
305                 return true;
306         }
307
308         /* determine new position for handle */
309
310         float new_fract;
311         int px, py;
312
313         d->translate_coordinates (*this, ev->x, ev->y, px, py);
314
315         Dividers::iterator prev = dividers.end();
316
317         for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
318                 if (*di == d) {
319                         break;
320                 }
321                 prev = di;
322         }
323
324         int space_remaining;
325         int prev_edge;
326
327         if (horizontal) {
328                 if (prev != dividers.end()) {
329                         prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
330                 } else {
331                         prev_edge = 0;
332                 }
333                 space_remaining = get_allocation().get_width() - prev_edge;
334                 new_fract = (float) (px - prev_edge) / space_remaining;
335         } else {
336                 if (prev != dividers.end()) {
337                         prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
338                 } else {
339                         prev_edge = 0;
340                 }
341                 space_remaining = get_allocation().get_height() - prev_edge;
342                 new_fract = (float) (py - prev_edge) / space_remaining;
343         }
344
345         new_fract = min (1.0f, max (0.0f, new_fract));
346
347         if (new_fract != d->fract) {
348                 d->fract = new_fract;
349                 reallocate (get_allocation ());
350                 queue_draw ();
351         }
352
353         return true;
354 }
355
356 void
357 Pane::set_divider (Dividers::size_type div, float fract)
358 {
359         bool redraw = false;
360
361         Dividers::iterator d = dividers.begin();
362
363         while (div--) {
364                 ++d;
365                 if (d == dividers.end()) {
366                         /* caller is trying to set divider that does not exist
367                          * yet.
368                          */
369                         return;
370                 }
371         }
372
373         if (fract != (*d)->fract) {
374                 (*d)->fract = fract;
375                 redraw = true;
376         }
377
378         if (redraw) {
379                 /* our size hasn't changed, but our internal allocations have */
380                 reallocate (get_allocation());
381                 queue_draw ();
382         }
383 }
384
385 float
386 Pane::get_divider (Dividers::size_type div)
387 {
388         Dividers::iterator d = dividers.begin();
389
390         while (div--) {
391                 ++d;
392                 if (d == dividers.end()) {
393                         /* caller is trying to set divider that does not exist
394                          * yet.
395                          */
396                         return -1.0f;
397                 }
398         }
399
400         return (*d)->fract;
401 }
402
403 void
404 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
405 {
406         /* since the callback could modify the child list(s), make sure we keep
407          * the iterators safe;
408          */
409
410         for (Children::iterator c = children.begin(); c != children.end(); ) {
411                 Children::iterator next = c;
412                 ++next;
413                 callback (c->w->gobj(), callback_data);
414                 c = next;
415         }
416
417         if (include_internals) {
418                 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
419                         Dividers::iterator next = d;
420                         ++next;
421                         callback (GTK_WIDGET((*d)->gobj()), callback_data);
422                         d = next;
423                 }
424         }
425 }
426
427 Pane::Divider::Divider ()
428         : fract (0.0)
429         , dragging (false)
430 {
431         set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
432                                     Gdk::BUTTON_RELEASE|
433                                     Gdk::MOTION_NOTIFY|
434                                     Gdk::ENTER_NOTIFY|
435                                     Gdk::LEAVE_NOTIFY));
436 }
437
438 bool
439 Pane::Divider::on_expose_event (GdkEventExpose* ev)
440 {
441         Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
442                         get_style()->get_fg (get_state()));
443
444         Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
445         draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
446         draw_context->clip_preserve ();
447         draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
448         draw_context->fill ();
449
450         return true;
451 }
452
453 bool
454 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
455 {
456         d->get_window()->set_cursor (drag_cursor);
457         d->set_state (Gtk::STATE_SELECTED);
458         return true;
459 }
460
461 bool
462 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
463 {
464         d->get_window()->set_cursor ();
465         d->set_state (Gtk::STATE_NORMAL);
466         return true;
467 }