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>
22 #include "gtkmm2ext/pane.h"
28 using namespace Gtkmm2ext;
40 set_has_window (false);
43 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
45 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
51 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
52 (*c)->show_con.disconnect ();
53 (*c)->hide_con.disconnect ();
55 (*c)->w->remove_destroy_notify_callback ((*c).get());
63 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
65 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
67 (*c)->minsize = minsize;
74 Pane::set_drag_cursor (Gdk::Cursor c)
80 Pane::on_size_request (GtkRequisition* req)
82 GtkRequisition largest;
84 /* iterate over all children, get their size requests */
86 /* horizontal pane is as high as its tallest child, including the dividers.
87 * Its width is the sum of the children plus the dividers.
89 * vertical pane is as wide as its widest child, including the dividers.
90 * Its height is the sum of the children plus the dividers.
94 largest.width = (children.size() - 1) * divider_width;
97 largest.height = (children.size() - 1) * divider_width;
101 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
104 if (!(*c)->w->is_visible ()) {
108 (*c)->w->size_request (r);
111 largest.height = max (largest.height, r.height);
113 largest.width += (*c)->minsize;
115 largest.width += r.width;
118 largest.width = max (largest.width, r.width);
120 largest.height += (*c)->minsize;
122 largest.height += r.height;
131 Pane::child_type_vfunc() const
133 /* We accept any number of any types of widgets */
134 return Gtk::Widget::get_type();
140 Divider* d = new Divider;
141 d->set_name (X_("Divider"));
142 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
143 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
144 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
145 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
146 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
147 d->set_parent (*this);
150 dividers.push_back (d);
154 Pane::handle_child_visibility ()
156 reallocate (get_allocation());
160 Pane::on_add (Widget* w)
162 children.push_back (boost::shared_ptr<Child> (new Child (this, w, 0)));
163 Child* kid = children.back ().get();
165 w->set_parent (*this);
166 /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
167 for custom containers that derive from Gtk::Container. So ... we need
168 to ensure that we hear about child destruction ourselves.
170 w->add_destroy_notify_callback (kid, &Pane::notify_child_destroyed);
172 kid->show_con = w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
173 kid->hide_con = w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
175 while (dividers.size() < (children.size() - 1)) {
181 Pane::notify_child_destroyed (void* data)
183 Child* child = reinterpret_cast<Child*> (data);
184 return child->pane->child_destroyed (child->w);
188 Pane::child_destroyed (Gtk::Widget* w)
190 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
192 (*c)->show_con.disconnect ();
193 (*c)->hide_con.disconnect ();
194 (*c)->w = NULL; // mark invalid
203 Pane::on_remove (Widget* w)
205 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
207 (*c)->show_con.disconnect ();
208 (*c)->hide_con.disconnect ();
209 w->remove_destroy_notify_callback ((*c).get());
211 (*c)->w = NULL; // mark invalid
219 Pane::on_size_allocate (Gtk::Allocation& alloc)
222 Container::on_size_allocate (alloc);
224 /* minumum pane size constraints */
225 Dividers::size_type div = 0;
226 for (Dividers::const_iterator d = dividers.begin(); d != dividers.end(); ++d, ++div) {
227 // XXX skip dividers that were just hidden in reallocate()
228 Pane::set_divider (div, (*d)->fract);
230 // TODO this needs tweaking for panes with > 2 children
231 // if a child grows, re-check the ones before it.
232 assert (dividers.size () < 3);
236 Pane::reallocate (Gtk::Allocation const & alloc)
239 int xpos = alloc.get_x();
240 int ypos = alloc.get_y();
243 if (children.empty()) {
247 if (children.size() == 1) {
248 /* only child gets the full allocation */
249 if (children.front()->w->is_visible ()) {
250 children.front()->w->size_allocate (alloc);
256 remaining = alloc.get_width ();
258 remaining = alloc.get_height ();
261 Children::iterator child;
262 Children::iterator next;
263 Dividers::iterator div;
265 child = children.begin();
267 /* skip initial hidden children */
269 while (child != children.end()) {
270 if ((*child)->w->is_visible()) {
276 for (div = dividers.begin(); child != children.end(); ) {
278 Gtk::Allocation child_alloc;
282 /* Move on to next *visible* child */
284 while (++next != children.end()) {
285 if ((*next)->w->is_visible()) {
290 child_alloc.set_x (xpos);
291 child_alloc.set_y (ypos);
293 if (next == children.end()) {
294 /* last child gets all the remaining space */
297 /* child gets the fraction of the remaining space given by the divider that follows it */
298 fract = (*div)->fract;
302 (*child)->w->size_request (cr);
305 child_alloc.set_width ((gint) floor (remaining * fract));
306 child_alloc.set_height (alloc.get_height());
307 remaining = max (0, (remaining - child_alloc.get_width()));
308 xpos += child_alloc.get_width();
310 child_alloc.set_width (alloc.get_width());
311 child_alloc.set_height ((gint) floor (remaining * fract));
312 remaining = max (0, (remaining - child_alloc.get_height()));
313 ypos += child_alloc.get_height ();
316 if ((*child)->minsize) {
318 child_alloc.set_width (max (child_alloc.get_width(), (*child)->minsize));
320 child_alloc.set_height (max (child_alloc.get_height(), (*child)->minsize));
324 if ((*child)->w->is_visible ()) {
325 (*child)->w->size_allocate (child_alloc);
328 if (next == children.end()) {
329 /* done, no more children, no need for a divider */
335 /* add a divider between children */
337 Gtk::Allocation divider_allocation;
339 divider_allocation.set_x (xpos);
340 divider_allocation.set_y (ypos);
343 divider_allocation.set_width (divider_width);
344 divider_allocation.set_height (alloc.get_height());
345 remaining = max (0, remaining - divider_width);
346 xpos += divider_width;
348 divider_allocation.set_width (alloc.get_width());
349 divider_allocation.set_height (divider_width);
350 remaining = max (0, remaining - divider_width);
351 ypos += divider_width;
354 (*div)->size_allocate (divider_allocation);
359 /* hide all remaining dividers */
361 while (div != dividers.end()) {
368 Pane::on_expose_event (GdkEventExpose* ev)
370 Children::iterator child;
371 Dividers::iterator div;
373 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
375 if ((*child)->w->is_visible()) {
376 propagate_expose (*((*child)->w), ev);
379 if (div != dividers.end()) {
380 if ((*div)->is_visible()) {
381 propagate_expose (**div, ev);
391 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
400 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
404 if (did_move && !children.empty()) {
405 children.front()->w->queue_resize ();
412 Pane::set_check_divider_position (bool yn)
418 Pane::constrain_fract (Dividers::size_type div, float fract)
420 if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
421 /* space not * allocated - * divider being set from startup code. Let it pass,
422 * since our goal is mostly to catch drags to a position that will interfere with window
428 if (children.size () <= div + 1) { return fract; } // XXX remove once hidden divs are skipped
429 assert(children.size () > div + 1);
431 const float size = horizontal ? get_allocation().get_width() : get_allocation().get_height();
433 // TODO: optimize: cache in Pane::on_size_request
434 Gtk::Requisition prev_req(children.at (div)->w->size_request ());
435 Gtk::Requisition next_req(children.at (div + 1)->w->size_request ());
436 float prev = (horizontal ? prev_req.width : prev_req.height);
437 float next = (horizontal ? next_req.width : next_req.height);
439 if (children.at (div)->minsize) {
440 prev = children.at (div)->minsize;
442 if (children.at (div + 1)->minsize) {
443 next = children.at (div + 1)->minsize;
446 if (size * fract < prev) {
449 if (size * (1.f - fract) < next) {
450 return 1.f - next / size;
459 /* On Quartz, if the pane handle (divider) gets to
460 be adjacent to the window edge, you can no longer grab it:
461 any attempt to do so is interpreted by the Quartz window
462 manager ("Finder") as a resize drag on the window edge.
467 if (div == dividers.size() - 1) {
468 if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
469 /* too close to right edge */
470 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_width();
475 if (get_allocation().get_width() * fract < (divider_width*2)) {
476 /* too close to left edge */
477 return (divider_width * 2.f) / (float)get_allocation().get_width();
481 if (div == dividers.size() - 1) {
482 if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
483 /* too close to bottom */
484 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_height();
489 if (get_allocation().get_height() * fract < (divider_width*2)) {
490 /* too close to top */
491 return (divider_width * 2.f) / (float) get_allocation().get_height();
500 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
508 /* determine new position for handle */
513 d->translate_coordinates (*this, ev->x, ev->y, px, py);
515 Dividers::iterator prev = dividers.end();
516 Dividers::size_type div = 0;
518 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
529 if (prev != dividers.end()) {
530 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
534 space_remaining = get_allocation().get_width() - prev_edge;
535 new_fract = (float) (px - prev_edge) / space_remaining;
537 if (prev != dividers.end()) {
538 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
542 space_remaining = get_allocation().get_height() - prev_edge;
543 new_fract = (float) (py - prev_edge) / space_remaining;
546 new_fract = min (1.0f, max (0.0f, new_fract));
547 new_fract = constrain_fract (div, new_fract);
548 new_fract = min (1.0f, max (0.0f, new_fract));
550 if (new_fract != d->fract) {
551 d->fract = new_fract;
552 reallocate (get_allocation ());
560 Pane::set_divider (Dividers::size_type div, float fract)
562 Dividers::iterator d = dividers.begin();
564 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
568 if (d == dividers.end()) {
569 /* caller is trying to set divider that does not exist
575 fract = max (0.0f, min (1.0f, fract));
576 fract = constrain_fract (div, fract);
577 fract = max (0.0f, min (1.0f, fract));
579 if (fract != (*d)->fract) {
581 /* our size hasn't changed, but our internal allocations have */
582 reallocate (get_allocation());
588 Pane::get_divider (Dividers::size_type div)
590 Dividers::iterator d = dividers.begin();
592 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
596 if (d == dividers.end()) {
597 /* caller is trying to set divider that does not exist
607 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
609 /* since the callback could modify the child list(s), make sure we keep
610 * the iterators safe;
612 Children kids (children);
613 for (Children::const_iterator c = kids.begin(); c != kids.end(); ++c) {
615 callback ((*c)->w->gobj(), callback_data);
619 if (include_internals) {
620 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
621 Dividers::iterator next = d;
623 callback (GTK_WIDGET((*d)->gobj()), callback_data);
629 Pane::Divider::Divider ()
633 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
641 Pane::Divider::on_expose_event (GdkEventExpose* ev)
643 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
644 get_style()->get_fg (get_state()));
646 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
647 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
648 draw_context->clip_preserve ();
649 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
650 draw_context->fill ();
656 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
658 d->get_window()->set_cursor (drag_cursor);
659 d->set_state (Gtk::STATE_SELECTED);
664 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
666 d->get_window()->set_cursor ();
667 d->set_state (Gtk::STATE_NORMAL);