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