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 ();
54 (*c)->w->remove_destroy_notify_callback (*c);
62 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
64 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
66 (*c)->minsize = minsize;
73 Pane::set_drag_cursor (Gdk::Cursor c)
79 Pane::on_size_request (GtkRequisition* req)
81 GtkRequisition largest;
83 /* iterate over all children, get their size requests */
85 /* horizontal pane is as high as its tallest child, including the dividers.
86 * Its width is the sum of the children plus the dividers.
88 * vertical pane is as wide as its widest child, including the dividers.
89 * Its height is the sum of the children plus the dividers.
93 largest.width = (children.size() - 1) * divider_width;
96 largest.height = (children.size() - 1) * divider_width;
100 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
103 if (!(*c)->w->is_visible ()) {
107 (*c)->w->size_request (r);
110 largest.height = max (largest.height, r.height);
112 largest.width += (*c)->minsize;
114 largest.width += r.width;
117 largest.width = max (largest.width, r.width);
119 largest.height += (*c)->minsize;
121 largest.height += r.height;
130 Pane::child_type_vfunc() const
132 /* We accept any number of any types of widgets */
133 return Gtk::Widget::get_type();
139 Divider* d = new Divider;
140 d->set_name (X_("Divider"));
141 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
142 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
143 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
144 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
145 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
146 d->set_parent (*this);
149 dividers.push_back (d);
153 Pane::handle_child_visibility ()
155 reallocate (get_allocation());
159 Pane::on_add (Widget* w)
161 children.push_back (new Child (this, w, 0));
162 Child* kid = children.back ();
164 w->set_parent (*this);
165 /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
166 for custom containers that derive from Gtk::Container. So ... we need
167 to ensure that we hear about child destruction ourselves.
169 w->add_destroy_notify_callback (kid, &Pane::notify_child_destroyed);
171 kid->show_con = w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
172 kid->hide_con = w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
174 while (dividers.size() < (children.size() - 1)) {
180 Pane::notify_child_destroyed (void* data)
182 Child* child = reinterpret_cast<Child*> (data);
183 return child->pane->child_destroyed (child->w);
187 Pane::child_destroyed (Gtk::Widget* w)
189 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
191 (*c)->show_con.disconnect ();
192 (*c)->hide_con.disconnect ();
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);
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 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 children.front()->w->size_allocate (alloc);
254 remaining = alloc.get_width ();
256 remaining = alloc.get_height ();
259 Children::iterator child;
260 Children::iterator next;
261 Dividers::iterator div;
263 child = children.begin();
265 /* skip initial hidden children */
267 while (child != children.end()) {
268 if ((*child)->w->is_visible()) {
274 for (div = dividers.begin(); child != children.end(); ) {
276 Gtk::Allocation child_alloc;
280 /* Move on to next *visible* child */
282 while (++next != children.end()) {
283 if ((*next)->w->is_visible()) {
288 child_alloc.set_x (xpos);
289 child_alloc.set_y (ypos);
291 if (next == children.end()) {
292 /* last child gets all the remaining space */
295 /* child gets the fraction of the remaining space given by the divider that follows it */
296 fract = (*div)->fract;
300 (*child)->w->size_request (cr);
303 child_alloc.set_width ((gint) floor (remaining * fract));
304 child_alloc.set_height (alloc.get_height());
305 remaining = max (0, (remaining - child_alloc.get_width()));
306 xpos += child_alloc.get_width();
308 child_alloc.set_width (alloc.get_width());
309 child_alloc.set_height ((gint) floor (remaining * fract));
310 remaining = max (0, (remaining - child_alloc.get_height()));
311 ypos += child_alloc.get_height ();
314 if ((*child)->minsize) {
316 child_alloc.set_width (max (child_alloc.get_width(), (*child)->minsize));
318 child_alloc.set_height (max (child_alloc.get_height(), (*child)->minsize));
322 (*child)->w->size_allocate (child_alloc);
324 if (next == children.end()) {
325 /* done, no more children, no need for a divider */
331 /* add a divider between children */
333 Gtk::Allocation divider_allocation;
335 divider_allocation.set_x (xpos);
336 divider_allocation.set_y (ypos);
339 divider_allocation.set_width (divider_width);
340 divider_allocation.set_height (alloc.get_height());
341 remaining = max (0, remaining - divider_width);
342 xpos += divider_width;
344 divider_allocation.set_width (alloc.get_width());
345 divider_allocation.set_height (divider_width);
346 remaining = max (0, remaining - divider_width);
347 ypos += divider_width;
350 (*div)->size_allocate (divider_allocation);
355 /* hide all remaining dividers */
357 while (div != dividers.end()) {
364 Pane::on_expose_event (GdkEventExpose* ev)
366 Children::iterator child;
367 Dividers::iterator div;
369 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
371 if ((*child)->w->is_visible()) {
372 propagate_expose (*((*child)->w), ev);
375 if (div != dividers.end()) {
376 if ((*div)->is_visible()) {
377 propagate_expose (**div, ev);
387 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
396 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
400 if (did_move && !children.empty()) {
401 children.front()->w->queue_resize ();
408 Pane::set_check_divider_position (bool yn)
414 Pane::constrain_fract (Dividers::size_type div, float fract)
416 if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
417 /* space not * allocated - * divider being set from startup code. Let it pass,
418 * since our goal is mostly to catch drags to a position that will interfere with window
424 const float size = horizontal ? get_allocation().get_width() : get_allocation().get_height();
426 // TODO: optimize: cache in Pane::on_size_request
427 Gtk::Requisition prev_req(children.at (div)->w->size_request ());
428 Gtk::Requisition next_req(children.at (div + 1)->w->size_request ());
429 float prev = divider_width + (horizontal ? prev_req.width : prev_req.height);
430 float next = divider_width + (horizontal ? next_req.width : next_req.height);
432 if (children.at (div)->minsize) {
433 prev = children.at (div)->minsize;
435 if (children.at (div + 1)->minsize) {
436 next = children.at (div + 1)->minsize;
439 if (size * fract < prev) {
442 if (size * (1.f - fract) < next) {
443 return 1.f - next / size;
452 /* On Quartz, if the pane handle (divider) gets to
453 be adjacent to the window edge, you can no longer grab it:
454 any attempt to do so is interpreted by the Quartz window
455 manager ("Finder") as a resize drag on the window edge.
460 if (div == dividers.size() - 1) {
461 if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
462 /* too close to right edge */
463 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_width();
468 if (get_allocation().get_width() * fract < (divider_width*2)) {
469 /* too close to left edge */
470 return (divider_width * 2.f) / (float)get_allocation().get_width();
474 if (div == dividers.size() - 1) {
475 if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
476 /* too close to bottom */
477 return 1.f - (divider_width * 2.f) / (float) get_allocation().get_height();
482 if (get_allocation().get_height() * fract < (divider_width*2)) {
483 /* too close to top */
484 return (divider_width * 2.f) / (float) get_allocation().get_height();
493 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
501 /* determine new position for handle */
506 d->translate_coordinates (*this, ev->x, ev->y, px, py);
508 Dividers::iterator prev = dividers.end();
509 Dividers::size_type div = 0;
511 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
522 if (prev != dividers.end()) {
523 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
527 space_remaining = get_allocation().get_width() - prev_edge;
528 new_fract = (float) (px - prev_edge) / space_remaining;
530 if (prev != dividers.end()) {
531 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
535 space_remaining = get_allocation().get_height() - prev_edge;
536 new_fract = (float) (py - prev_edge) / space_remaining;
539 new_fract = min (1.0f, max (0.0f, new_fract));
540 new_fract = constrain_fract (div, new_fract);
542 if (new_fract != d->fract) {
543 d->fract = new_fract;
544 reallocate (get_allocation ());
552 Pane::set_divider (Dividers::size_type div, float fract)
554 Dividers::iterator d = dividers.begin();
556 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
560 if (d == dividers.end()) {
561 /* caller is trying to set divider that does not exist
567 fract = max (0.0f, min (1.0f, fract));
568 fract = constrain_fract (div, fract);
570 if (fract != (*d)->fract) {
572 /* our size hasn't changed, but our internal allocations have */
573 reallocate (get_allocation());
579 Pane::get_divider (Dividers::size_type div)
581 Dividers::iterator d = dividers.begin();
583 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
587 if (d == dividers.end()) {
588 /* caller is trying to set divider that does not exist
598 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
600 /* since the callback could modify the child list(s), make sure we keep
601 * the iterators safe;
604 for (Children::iterator c = children.begin(); c != children.end(); ) {
605 Children::iterator next = c;
607 callback ((*c)->w->gobj(), callback_data);
611 if (include_internals) {
612 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
613 Dividers::iterator next = d;
615 callback (GTK_WIDGET((*d)->gobj()), callback_data);
621 Pane::Divider::Divider ()
625 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
633 Pane::Divider::on_expose_event (GdkEventExpose* ev)
635 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
636 get_style()->get_fg (get_state()));
638 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
639 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
640 draw_context->clip_preserve ();
641 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
642 draw_context->fill ();
648 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
650 d->get_window()->set_cursor (drag_cursor);
651 d->set_state (Gtk::STATE_SELECTED);
656 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
658 d->get_window()->set_cursor ();
659 d->set_state (Gtk::STATE_NORMAL);