fix thinko in Pane expose event handler.
[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) {
308
309                 if (child->w->is_visible()) {
310                         propagate_expose (*(child->w), ev);
311                 }
312
313                 if (div != dividers.end()) {
314                         if ((*div)->is_visible()) {
315                                 propagate_expose (**div, ev);
316                         }
317                         ++div;
318                 }
319         }
320
321         return true;
322 }
323
324 bool
325 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
326 {
327         d->dragging = true;
328         d->queue_draw ();
329
330         return false;
331 }
332
333 bool
334 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
335 {
336         d->dragging = false;
337
338         if (did_move) {
339                 children.front().w->queue_resize ();
340                 did_move = false;
341         }
342
343         return false;
344 }
345
346 bool
347 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
348 {
349         did_move = true;
350
351         if (!d->dragging) {
352                 return true;
353         }
354
355         /* determine new position for handle */
356
357         float new_fract;
358         int px, py;
359
360         d->translate_coordinates (*this, ev->x, ev->y, px, py);
361
362         Dividers::iterator prev = dividers.end();
363
364         for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
365                 if (*di == d) {
366                         break;
367                 }
368                 prev = di;
369         }
370
371         int space_remaining;
372         int prev_edge;
373
374         if (horizontal) {
375                 if (prev != dividers.end()) {
376                         prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
377                 } else {
378                         prev_edge = 0;
379                 }
380                 space_remaining = get_allocation().get_width() - prev_edge;
381                 new_fract = (float) (px - prev_edge) / space_remaining;
382         } else {
383                 if (prev != dividers.end()) {
384                         prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
385                 } else {
386                         prev_edge = 0;
387                 }
388                 space_remaining = get_allocation().get_height() - prev_edge;
389                 new_fract = (float) (py - prev_edge) / space_remaining;
390         }
391
392         new_fract = min (1.0f, max (0.0f, new_fract));
393
394         if (new_fract != d->fract) {
395                 d->fract = new_fract;
396                 reallocate (get_allocation ());
397                 queue_draw ();
398         }
399
400         return true;
401 }
402
403 void
404 Pane::set_divider (Dividers::size_type div, float fract)
405 {
406         Dividers::iterator d = dividers.begin();
407
408         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
409                 /* relax */
410         }
411
412         if (d == dividers.end()) {
413                 /* caller is trying to set divider that does not exist
414                  * yet.
415                  */
416                 return;
417         }
418
419         fract = max (0.0f, min (1.0f, fract));
420
421         if (fract != (*d)->fract) {
422                 (*d)->fract = fract;
423                 /* our size hasn't changed, but our internal allocations have */
424                 reallocate (get_allocation());
425                 queue_draw ();
426         }
427 }
428
429 float
430 Pane::get_divider (Dividers::size_type div)
431 {
432         Dividers::iterator d = dividers.begin();
433
434         for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
435                 /* relax */
436         }
437
438         if (d == dividers.end()) {
439                 /* caller is trying to set divider that does not exist
440                  * yet.
441                  */
442                 return -1.0f;
443         }
444
445         return (*d)->fract;
446 }
447
448 void
449 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
450 {
451         /* since the callback could modify the child list(s), make sure we keep
452          * the iterators safe;
453          */
454
455         for (Children::iterator c = children.begin(); c != children.end(); ) {
456                 Children::iterator next = c;
457                 ++next;
458                 callback (c->w->gobj(), callback_data);
459                 c = next;
460         }
461
462         if (include_internals) {
463                 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
464                         Dividers::iterator next = d;
465                         ++next;
466                         callback (GTK_WIDGET((*d)->gobj()), callback_data);
467                         d = next;
468                 }
469         }
470 }
471
472 Pane::Divider::Divider ()
473         : fract (0.0)
474         , dragging (false)
475 {
476         set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
477                                     Gdk::BUTTON_RELEASE|
478                                     Gdk::MOTION_NOTIFY|
479                                     Gdk::ENTER_NOTIFY|
480                                     Gdk::LEAVE_NOTIFY));
481 }
482
483 bool
484 Pane::Divider::on_expose_event (GdkEventExpose* ev)
485 {
486         Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
487                         get_style()->get_fg (get_state()));
488
489         Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
490         draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
491         draw_context->clip_preserve ();
492         draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
493         draw_context->fill ();
494
495         return true;
496 }
497
498 bool
499 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
500 {
501         d->get_window()->set_cursor (drag_cursor);
502         d->set_state (Gtk::STATE_SELECTED);
503         return true;
504 }
505
506 bool
507 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
508 {
509         d->get_window()->set_cursor ();
510         d->set_state (Gtk::STATE_NORMAL);
511         d->queue_draw ();
512         return true;
513 }