2 Copyright (C) 2016 Paul Davis
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.
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.
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.
21 #include <gdkmm/cursor.h>
23 #include "widgets/pane.h"
30 using namespace ArdourWidgets;
41 set_has_window (false);
44 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
46 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
52 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
53 (*c)->show_con.disconnect ();
54 (*c)->hide_con.disconnect ();
56 (*c)->w->remove_destroy_notify_callback ((*c).get());
64 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
66 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
68 (*c)->minsize = minsize;
75 Pane::set_drag_cursor (Gdk::Cursor c)
81 Pane::on_size_request (GtkRequisition* req)
83 GtkRequisition largest;
85 /* iterate over all children, get their size requests */
87 /* horizontal pane is as high as its tallest child, including the dividers.
88 * Its width is the sum of the children plus the dividers.
90 * vertical pane is as wide as its widest child, including the dividers.
91 * Its height is the sum of the children plus the dividers.
95 largest.width = (children.size() - 1) * divider_width;
98 largest.height = (children.size() - 1) * divider_width;
102 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
105 if (!(*c)->w->is_visible ()) {
109 (*c)->w->size_request (r);
112 largest.height = max (largest.height, r.height);
114 largest.width += (*c)->minsize;
116 largest.width += r.width;
119 largest.width = max (largest.width, r.width);
121 largest.height += (*c)->minsize;
123 largest.height += r.height;
132 Pane::child_type_vfunc() const
134 /* We accept any number of any types of widgets */
135 return Gtk::Widget::get_type();
141 Divider* d = new Divider;
142 d->set_name (X_("Divider"));
143 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
144 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
145 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
146 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
147 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
148 d->set_parent (*this);
151 dividers.push_back (d);
155 Pane::handle_child_visibility ()
157 reallocate (get_allocation());
161 Pane::on_add (Widget* w)
163 children.push_back (boost::shared_ptr<Child> (new Child (this, w, 0)));
164 Child* kid = children.back ().get();
166 w->set_parent (*this);
167 /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
168 for custom containers that derive from Gtk::Container. So ... we need
169 to ensure that we hear about child destruction ourselves.
171 w->add_destroy_notify_callback (kid, &Pane::notify_child_destroyed);
173 kid->show_con = w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
174 kid->hide_con = w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
176 while (dividers.size() < (children.size() - 1)) {
182 Pane::notify_child_destroyed (void* data)
184 Child* child = reinterpret_cast<Child*> (data);
185 return child->pane->child_destroyed (child->w);
189 Pane::child_destroyed (Gtk::Widget* w)
191 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
193 (*c)->show_con.disconnect ();
194 (*c)->hide_con.disconnect ();
195 (*c)->w = NULL; // mark invalid
204 Pane::on_remove (Widget* w)
206 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
208 (*c)->show_con.disconnect ();
209 (*c)->hide_con.disconnect ();
210 w->remove_destroy_notify_callback ((*c).get());
212 (*c)->w = NULL; // mark invalid
220 Pane::on_size_allocate (Gtk::Allocation& alloc)
223 Container::on_size_allocate (alloc);
225 /* minumum pane size constraints */
226 Dividers::size_type div = 0;
227 for (Dividers::const_iterator d = dividers.begin(); d != dividers.end(); ++d, ++div) {
228 // XXX skip dividers that were just hidden in reallocate()
229 Pane::set_divider (div, (*d)->fract);
231 // TODO this needs tweaking for panes with > 2 children
232 // if a child grows, re-check the ones before it.
233 assert (dividers.size () < 3);
237 Pane::reallocate (Gtk::Allocation const & alloc)
240 int xpos = alloc.get_x();
241 int ypos = alloc.get_y();
244 if (children.empty()) {
248 if (children.size() == 1) {
249 /* only child gets the full allocation */
250 if (children.front()->w->is_visible ()) {
251 children.front()->w->size_allocate (alloc);
257 remaining = alloc.get_width ();
259 remaining = alloc.get_height ();
262 Children::iterator child;
263 Children::iterator next;
264 Dividers::iterator div;
266 child = children.begin();
268 /* skip initial hidden children */
270 while (child != children.end()) {
271 if ((*child)->w->is_visible()) {
277 for (div = dividers.begin(); child != children.end(); ) {
279 Gtk::Allocation child_alloc;
283 /* Move on to next *visible* child */
285 while (++next != children.end()) {
286 if ((*next)->w->is_visible()) {
291 child_alloc.set_x (xpos);
292 child_alloc.set_y (ypos);
294 if (next == children.end()) {
295 /* last child gets all the remaining space */
298 /* child gets the fraction of the remaining space given by the divider that follows it */
299 fract = (*div)->fract;
303 (*child)->w->size_request (cr);
306 child_alloc.set_width ((gint) floor (remaining * fract));
307 child_alloc.set_height (alloc.get_height());
308 remaining = max (0, (remaining - child_alloc.get_width()));
309 xpos += child_alloc.get_width();
311 child_alloc.set_width (alloc.get_width());
312 child_alloc.set_height ((gint) floor (remaining * fract));
313 remaining = max (0, (remaining - child_alloc.get_height()));
314 ypos += child_alloc.get_height ();
317 if ((*child)->minsize) {
319 child_alloc.set_width (max (child_alloc.get_width(), (*child)->minsize));
321 child_alloc.set_height (max (child_alloc.get_height(), (*child)->minsize));
325 if ((*child)->w->is_visible ()) {
326 (*child)->w->size_allocate (child_alloc);
329 if (next == children.end()) {
330 /* done, no more children, no need for a divider */
336 /* add a divider between children */
338 Gtk::Allocation divider_allocation;
340 divider_allocation.set_x (xpos);
341 divider_allocation.set_y (ypos);
344 divider_allocation.set_width (divider_width);
345 divider_allocation.set_height (alloc.get_height());
346 remaining = max (0, remaining - divider_width);
347 xpos += divider_width;
349 divider_allocation.set_width (alloc.get_width());
350 divider_allocation.set_height (divider_width);
351 remaining = max (0, remaining - divider_width);
352 ypos += divider_width;
355 (*div)->size_allocate (divider_allocation);
360 /* hide all remaining dividers */
362 while (div != dividers.end()) {
369 Pane::on_expose_event (GdkEventExpose* ev)
371 Children::iterator child;
372 Dividers::iterator div;
374 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
376 if ((*child)->w->is_visible()) {
377 propagate_expose (*((*child)->w), ev);
380 if (div != dividers.end()) {
381 if ((*div)->is_visible()) {
382 propagate_expose (**div, ev);
392 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
401 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
405 if (did_move && !children.empty()) {
406 children.front()->w->queue_resize ();
413 Pane::set_check_divider_position (bool yn)
419 Pane::constrain_fract (Dividers::size_type div, float fract)
421 if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
422 /* space not * allocated - * divider being set from startup code. Let it pass,
423 * since our goal is mostly to catch drags to a position that will interfere with window
429 if (children.size () <= div + 1) { return fract; } // XXX remove once hidden divs are skipped
430 assert(children.size () > div + 1);
432 const float size = horizontal ? get_allocation().get_width() : get_allocation().get_height();
434 // TODO: optimize: cache in Pane::on_size_request
435 Gtk::Requisition prev_req(children.at (div)->w->size_request ());
436 Gtk::Requisition next_req(children.at (div + 1)->w->size_request ());
437 float prev = (horizontal ? prev_req.width : prev_req.height);
438 float next = (horizontal ? next_req.width : next_req.height);
440 if (children.at (div)->minsize) {
441 prev = children.at (div)->minsize;
443 if (children.at (div + 1)->minsize) {
444 next = children.at (div + 1)->minsize;
447 if (size * fract < prev) {
450 if (size * (1.f - fract) < next) {
451 return 1.f - next / size;
460 /* On Quartz, if the pane handle (divider) gets to
461 be adjacent to the window edge, you can no longer grab it:
462 any attempt to do so is interpreted by the Quartz window
463 manager ("Finder") as a resize drag on the window edge.
468 if (div == dividers.size() - 1) {
469 if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
470 /* too close to right edge */
471 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_width();
476 if (get_allocation().get_width() * fract < (divider_width*2)) {
477 /* too close to left edge */
478 return (divider_width * 2.f) / (float)get_allocation().get_width();
482 if (div == dividers.size() - 1) {
483 if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
484 /* too close to bottom */
485 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_height();
490 if (get_allocation().get_height() * fract < (divider_width*2)) {
491 /* too close to top */
492 return (divider_width * 2.f) / (float) get_allocation().get_height();
501 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
509 /* determine new position for handle */
514 d->translate_coordinates (*this, ev->x, ev->y, px, py);
516 Dividers::iterator prev = dividers.end();
517 Dividers::size_type div = 0;
519 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
530 if (prev != dividers.end()) {
531 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
535 space_remaining = get_allocation().get_width() - prev_edge;
536 new_fract = (float) (px - prev_edge) / space_remaining;
538 if (prev != dividers.end()) {
539 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
543 space_remaining = get_allocation().get_height() - prev_edge;
544 new_fract = (float) (py - prev_edge) / space_remaining;
547 new_fract = min (1.0f, max (0.0f, new_fract));
548 new_fract = constrain_fract (div, new_fract);
549 new_fract = min (1.0f, max (0.0f, new_fract));
551 if (new_fract != d->fract) {
552 d->fract = new_fract;
553 reallocate (get_allocation ());
561 Pane::set_divider (Dividers::size_type div, float fract)
563 Dividers::iterator d = dividers.begin();
565 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
569 if (d == dividers.end()) {
570 /* caller is trying to set divider that does not exist
576 fract = max (0.0f, min (1.0f, fract));
577 fract = constrain_fract (div, fract);
578 fract = max (0.0f, min (1.0f, fract));
580 if (fract != (*d)->fract) {
582 /* our size hasn't changed, but our internal allocations have */
583 reallocate (get_allocation());
589 Pane::get_divider (Dividers::size_type div)
591 Dividers::iterator d = dividers.begin();
593 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
597 if (d == dividers.end()) {
598 /* caller is trying to set divider that does not exist
608 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
610 /* since the callback could modify the child list(s), make sure we keep
611 * the iterators safe;
613 Children kids (children);
614 for (Children::const_iterator c = kids.begin(); c != kids.end(); ++c) {
616 callback ((*c)->w->gobj(), callback_data);
620 if (include_internals) {
621 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
622 Dividers::iterator next = d;
624 callback (GTK_WIDGET((*d)->gobj()), callback_data);
630 Pane::Divider::Divider ()
634 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
642 Pane::Divider::on_expose_event (GdkEventExpose* ev)
644 Gdk::Color c = (dragging ? get_style()->get_bg (Gtk::STATE_ACTIVE) :
645 get_style()->get_bg (get_state()));
647 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
648 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
649 draw_context->clip_preserve ();
650 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
651 draw_context->fill ();
657 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
659 d->get_window()->set_cursor (drag_cursor);
660 d->set_state (Gtk::STATE_ACTIVE);
665 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
667 d->get_window()->set_cursor ();
668 d->set_state (Gtk::STATE_NORMAL);