2 * Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4 * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <gdkmm/cursor.h>
24 #include "widgets/pane.h"
31 using namespace ArdourWidgets;
42 set_has_window (false);
45 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
47 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
53 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
54 (*c)->show_con.disconnect ();
55 (*c)->hide_con.disconnect ();
57 (*c)->w->remove_destroy_notify_callback ((*c).get());
65 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
67 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
69 (*c)->minsize = minsize;
76 Pane::set_drag_cursor (Gdk::Cursor c)
82 Pane::on_size_request (GtkRequisition* req)
84 GtkRequisition largest;
86 /* iterate over all children, get their size requests */
88 /* horizontal pane is as high as its tallest child, including the dividers.
89 * Its width is the sum of the children plus the dividers.
91 * vertical pane is as wide as its widest child, including the dividers.
92 * Its height is the sum of the children plus the dividers.
96 largest.width = (children.size() - 1) * divider_width;
99 largest.height = (children.size() - 1) * divider_width;
103 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
106 if (!(*c)->w->is_visible ()) {
110 (*c)->w->size_request (r);
113 largest.height = max (largest.height, r.height);
115 largest.width += (*c)->minsize;
117 largest.width += r.width;
120 largest.width = max (largest.width, r.width);
122 largest.height += (*c)->minsize;
124 largest.height += r.height;
133 Pane::child_type_vfunc() const
135 /* We accept any number of any types of widgets */
136 return Gtk::Widget::get_type();
142 Divider* d = new Divider;
143 d->set_name (X_("Divider"));
144 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
145 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
146 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
147 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
148 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
149 d->set_parent (*this);
152 dividers.push_back (d);
156 Pane::handle_child_visibility ()
158 reallocate (get_allocation());
162 Pane::on_add (Widget* w)
164 children.push_back (boost::shared_ptr<Child> (new Child (this, w, 0)));
165 Child* kid = children.back ().get();
167 w->set_parent (*this);
168 /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
169 for custom containers that derive from Gtk::Container. So ... we need
170 to ensure that we hear about child destruction ourselves.
172 w->add_destroy_notify_callback (kid, &Pane::notify_child_destroyed);
174 kid->show_con = w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
175 kid->hide_con = w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
177 while (dividers.size() < (children.size() - 1)) {
183 Pane::notify_child_destroyed (void* data)
185 Child* child = reinterpret_cast<Child*> (data);
186 return child->pane->child_destroyed (child->w);
190 Pane::child_destroyed (Gtk::Widget* w)
192 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
194 (*c)->show_con.disconnect ();
195 (*c)->hide_con.disconnect ();
196 (*c)->w = NULL; // mark invalid
205 Pane::on_remove (Widget* w)
207 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
209 (*c)->show_con.disconnect ();
210 (*c)->hide_con.disconnect ();
211 w->remove_destroy_notify_callback ((*c).get());
213 (*c)->w = NULL; // mark invalid
221 Pane::on_size_allocate (Gtk::Allocation& alloc)
224 Container::on_size_allocate (alloc);
226 /* minumum pane size constraints */
227 Dividers::size_type div = 0;
228 for (Dividers::const_iterator d = dividers.begin(); d != dividers.end(); ++d, ++div) {
229 // XXX skip dividers that were just hidden in reallocate()
230 Pane::set_divider (div, (*d)->fract);
232 // TODO this needs tweaking for panes with > 2 children
233 // if a child grows, re-check the ones before it.
234 assert (dividers.size () < 3);
238 Pane::reallocate (Gtk::Allocation const & alloc)
241 int xpos = alloc.get_x();
242 int ypos = alloc.get_y();
245 if (children.empty()) {
249 if (children.size() == 1) {
250 /* only child gets the full allocation */
251 if (children.front()->w->is_visible ()) {
252 children.front()->w->size_allocate (alloc);
258 remaining = alloc.get_width ();
260 remaining = alloc.get_height ();
263 Children::iterator child;
264 Children::iterator next;
265 Dividers::iterator div;
267 child = children.begin();
269 /* skip initial hidden children */
271 while (child != children.end()) {
272 if ((*child)->w->is_visible()) {
278 for (div = dividers.begin(); child != children.end(); ) {
280 Gtk::Allocation child_alloc;
284 /* Move on to next *visible* child */
286 while (++next != children.end()) {
287 if ((*next)->w->is_visible()) {
292 child_alloc.set_x (xpos);
293 child_alloc.set_y (ypos);
295 if (next == children.end()) {
296 /* last child gets all the remaining space */
299 /* child gets the fraction of the remaining space given by the divider that follows it */
300 fract = (*div)->fract;
304 (*child)->w->size_request (cr);
307 child_alloc.set_width ((gint) floor (remaining * fract));
308 child_alloc.set_height (alloc.get_height());
309 remaining = max (0, (remaining - child_alloc.get_width()));
310 xpos += child_alloc.get_width();
312 child_alloc.set_width (alloc.get_width());
313 child_alloc.set_height ((gint) floor (remaining * fract));
314 remaining = max (0, (remaining - child_alloc.get_height()));
315 ypos += child_alloc.get_height ();
318 if ((*child)->minsize) {
320 child_alloc.set_width (max (child_alloc.get_width(), (*child)->minsize));
322 child_alloc.set_height (max (child_alloc.get_height(), (*child)->minsize));
326 if ((*child)->w->is_visible ()) {
327 (*child)->w->size_allocate (child_alloc);
330 if (next == children.end()) {
331 /* done, no more children, no need for a divider */
337 /* add a divider between children */
339 Gtk::Allocation divider_allocation;
341 divider_allocation.set_x (xpos);
342 divider_allocation.set_y (ypos);
345 divider_allocation.set_width (divider_width);
346 divider_allocation.set_height (alloc.get_height());
347 remaining = max (0, remaining - divider_width);
348 xpos += divider_width;
350 divider_allocation.set_width (alloc.get_width());
351 divider_allocation.set_height (divider_width);
352 remaining = max (0, remaining - divider_width);
353 ypos += divider_width;
356 (*div)->size_allocate (divider_allocation);
361 /* hide all remaining dividers */
363 while (div != dividers.end()) {
370 Pane::on_expose_event (GdkEventExpose* ev)
372 Children::iterator child;
373 Dividers::iterator div;
375 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
377 if ((*child)->w->is_visible()) {
378 propagate_expose (*((*child)->w), ev);
381 if (div != dividers.end()) {
382 if ((*div)->is_visible()) {
383 propagate_expose (**div, ev);
393 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
402 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
406 if (did_move && !children.empty()) {
407 children.front()->w->queue_resize ();
414 Pane::set_check_divider_position (bool yn)
420 Pane::constrain_fract (Dividers::size_type div, float fract)
422 if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
423 /* space not * allocated - * divider being set from startup code. Let it pass,
424 * since our goal is mostly to catch drags to a position that will interfere with window
430 if (children.size () <= div + 1) { return fract; } // XXX remove once hidden divs are skipped
431 assert(children.size () > div + 1);
433 const float size = horizontal ? get_allocation().get_width() : get_allocation().get_height();
435 // TODO: optimize: cache in Pane::on_size_request
436 Gtk::Requisition prev_req(children.at (div)->w->size_request ());
437 Gtk::Requisition next_req(children.at (div + 1)->w->size_request ());
438 float prev = (horizontal ? prev_req.width : prev_req.height);
439 float next = (horizontal ? next_req.width : next_req.height);
441 if (children.at (div)->minsize) {
442 prev = children.at (div)->minsize;
444 if (children.at (div + 1)->minsize) {
445 next = children.at (div + 1)->minsize;
448 if (size * fract < prev) {
451 if (size * (1.f - fract) < next) {
452 return 1.f - next / size;
461 /* On Quartz, if the pane handle (divider) gets to
462 be adjacent to the window edge, you can no longer grab it:
463 any attempt to do so is interpreted by the Quartz window
464 manager ("Finder") as a resize drag on the window edge.
469 if (div == dividers.size() - 1) {
470 if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
471 /* too close to right edge */
472 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_width();
477 if (get_allocation().get_width() * fract < (divider_width*2)) {
478 /* too close to left edge */
479 return (divider_width * 2.f) / (float)get_allocation().get_width();
483 if (div == dividers.size() - 1) {
484 if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
485 /* too close to bottom */
486 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_height();
491 if (get_allocation().get_height() * fract < (divider_width*2)) {
492 /* too close to top */
493 return (divider_width * 2.f) / (float) get_allocation().get_height();
502 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
510 /* determine new position for handle */
515 d->translate_coordinates (*this, ev->x, ev->y, px, py);
517 Dividers::iterator prev = dividers.end();
518 Dividers::size_type div = 0;
520 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
531 if (prev != dividers.end()) {
532 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
536 space_remaining = get_allocation().get_width() - prev_edge;
537 new_fract = (float) (px - prev_edge) / space_remaining;
539 if (prev != dividers.end()) {
540 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
544 space_remaining = get_allocation().get_height() - prev_edge;
545 new_fract = (float) (py - prev_edge) / space_remaining;
548 new_fract = min (1.0f, max (0.0f, new_fract));
549 new_fract = constrain_fract (div, new_fract);
550 new_fract = min (1.0f, max (0.0f, new_fract));
552 if (new_fract != d->fract) {
553 d->fract = new_fract;
554 reallocate (get_allocation ());
562 Pane::set_divider (Dividers::size_type div, float fract)
564 Dividers::iterator d = dividers.begin();
566 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
570 if (d == dividers.end()) {
571 /* caller is trying to set divider that does not exist
577 fract = max (0.0f, min (1.0f, fract));
578 fract = constrain_fract (div, fract);
579 fract = max (0.0f, min (1.0f, fract));
581 if (fract != (*d)->fract) {
583 /* our size hasn't changed, but our internal allocations have */
584 reallocate (get_allocation());
590 Pane::get_divider (Dividers::size_type div)
592 Dividers::iterator d = dividers.begin();
594 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
598 if (d == dividers.end()) {
599 /* caller is trying to set divider that does not exist
609 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
611 /* since the callback could modify the child list(s), make sure we keep
612 * the iterators safe;
614 Children kids (children);
615 for (Children::const_iterator c = kids.begin(); c != kids.end(); ++c) {
617 callback ((*c)->w->gobj(), callback_data);
621 if (include_internals) {
622 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
623 Dividers::iterator next = d;
625 callback (GTK_WIDGET((*d)->gobj()), callback_data);
631 Pane::Divider::Divider ()
635 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
643 Pane::Divider::on_expose_event (GdkEventExpose* ev)
645 Gdk::Color c = (dragging ? get_style()->get_bg (Gtk::STATE_ACTIVE) :
646 get_style()->get_bg (get_state()));
648 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
649 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
650 draw_context->clip_preserve ();
651 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
652 draw_context->fill ();
658 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
660 d->get_window()->set_cursor (drag_cursor);
661 d->set_state (Gtk::STATE_ACTIVE);
666 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
668 d->get_window()->set_cursor ();
669 d->set_state (Gtk::STATE_NORMAL);