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.
20 #include <gdkmm/cursor.h>
21 #include "gtkmm2ext/pane.h"
27 using namespace Gtkmm2ext;
39 set_has_window (false);
42 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
44 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
50 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
51 c->w->remove_destroy_notify_callback (&(*c));
57 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
59 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
68 Pane::set_drag_cursor (Gdk::Cursor c)
74 Pane::on_size_request (GtkRequisition* req)
76 GtkRequisition largest;
78 /* iterate over all children, get their size requests */
80 /* horizontal pane is as high as its tallest child, including the dividers.
81 * Its width is the sum of the children plus the dividers.
83 * vertical pane is as wide as its widest child, including the dividers.
84 * Its height is the sum of the children plus the dividers.
88 largest.width = (children.size() - 1) * divider_width;
91 largest.height = (children.size() - 1) * divider_width;
95 for (Children::iterator child = children.begin(); child != children.end(); ++child) {
98 child->w->size_request (r);
101 largest.height = max (largest.height, r.height);
102 largest.width += r.width;
104 largest.width = max (largest.width, r.width);
105 largest.height += r.height;
113 Pane::child_type_vfunc() const
115 /* We accept any number of any types of widgets */
116 return Gtk::Widget::get_type();
122 Divider* d = new Divider;
123 d->set_name (X_("Divider"));
124 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
125 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
126 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
127 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
128 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
129 d->set_parent (*this);
132 dividers.push_back (d);
136 Pane::handle_child_visibility ()
138 reallocate (get_allocation());
142 Pane::on_add (Widget* w)
144 children.push_back (Child (this, w, 0));
146 w->set_parent (*this);
147 /* Gtkmm 2.4 does not correctly arrange for ::on_remove() to be called
148 for custom containers that derive from Gtk::Container. So ... we need
149 to ensure that we hear about child destruction ourselves.
151 w->add_destroy_notify_callback (&children.back(), &Pane::notify_child_destroyed);
153 w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
154 w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
156 while (dividers.size() < (children.size() - 1)) {
162 Pane::notify_child_destroyed (void* data)
164 Child* child = reinterpret_cast<Child*> (data);
165 return child->pane->child_destroyed (child->w);
169 Pane::child_destroyed (Gtk::Widget* w)
171 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
181 Pane::on_remove (Widget* w)
183 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
185 w->remove_destroy_notify_callback (&(*c));
194 Pane::on_size_allocate (Gtk::Allocation& alloc)
196 std::cerr << "pane @ " << this << " reallocated as " << alloc.get_width() << " x " << alloc.get_height() << std::endl;
198 Container::on_size_allocate (alloc);
202 Pane::reallocate (Gtk::Allocation const & alloc)
205 int xpos = alloc.get_x();
206 int ypos = alloc.get_y();
209 if (children.empty()) {
213 if (children.size() == 1) {
214 /* only child gets the full allocation */
215 children.front().w->size_allocate (alloc);
220 remaining = alloc.get_width ();
222 remaining = alloc.get_height ();
225 Children::iterator child;
226 Children::iterator next;
227 Dividers::iterator div;
229 child = children.begin();
231 /* skip initial hidden children */
233 while (child != children.end()) {
234 if (child->w->is_visible()) {
240 for (div = dividers.begin(); child != children.end(); ) {
242 Gtk::Allocation child_alloc;
246 /* Move on to next *visible* child */
248 while (++next != children.end()) {
249 if (next->w->is_visible()) {
254 child_alloc.set_x (xpos);
255 child_alloc.set_y (ypos);
257 if (next == children.end()) {
258 /* last child gets all the remaining space */
261 /* child gets the fraction of the remaining space given by the divider that follows it */
262 fract = (*div)->fract;
266 child->w->size_request (cr);
269 child_alloc.set_width ((gint) floor (remaining * fract));
270 child_alloc.set_height (alloc.get_height());
271 remaining = max (0, (remaining - child_alloc.get_width()));
272 xpos += child_alloc.get_width();
274 child_alloc.set_width (alloc.get_width());
275 child_alloc.set_height ((gint) floor (remaining * fract));
276 remaining = max (0, (remaining - child_alloc.get_height()));
277 ypos += child_alloc.get_height ();
280 if (child->minsize) {
282 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
284 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
288 child->w->size_allocate (child_alloc);
290 if (next == children.end()) {
291 /* done, no more children, no need for a divider */
297 /* add a divider between children */
299 Gtk::Allocation divider_allocation;
301 divider_allocation.set_x (xpos);
302 divider_allocation.set_y (ypos);
305 divider_allocation.set_width (divider_width);
306 divider_allocation.set_height (alloc.get_height());
307 remaining = max (0, remaining - divider_width);
308 xpos += divider_width;
310 divider_allocation.set_width (alloc.get_width());
311 divider_allocation.set_height (divider_width);
312 remaining = max (0, remaining - divider_width);
313 ypos += divider_width;
316 (*div)->size_allocate (divider_allocation);
321 /* hide all remaining dividers */
323 while (div != dividers.end()) {
330 Pane::on_expose_event (GdkEventExpose* ev)
332 Children::iterator child;
333 Dividers::iterator div;
335 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child) {
337 if (child->w->is_visible()) {
338 propagate_expose (*(child->w), ev);
341 if (div != dividers.end()) {
342 if ((*div)->is_visible()) {
343 propagate_expose (**div, ev);
353 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
362 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
366 if (did_move && !children.empty()) {
367 children.front().w->queue_resize ();
374 Pane::set_check_divider_position (bool yn)
380 Pane::fract_is_ok (Dividers::size_type div, float fract)
388 if (get_allocation().get_width() == 1 && get_allocation().get_height() == 1) {
389 /* space not * allocated - * divider being set from startup code. Let it pass,
390 since our goal is mostly to catch drags to a position that will interfere with window
396 /* On Quartz, if the pane handle (divider) gets to
397 be adjacent to the window edge, you can no longer grab it:
398 any attempt to do so is interpreted by the Quartz window
399 manager ("Finder") as a resize drag on the window edge.
403 if (div == dividers.size() - 1) {
404 if (get_allocation().get_width() * (1.0 - fract) < (divider_width*2)) {
405 /* too close to right edge */
411 if (get_allocation().get_width() * fract < (divider_width*2)) {
412 /* too close to left edge */
417 if (div == dividers.size() - 1) {
418 if (get_allocation().get_height() * (1.0 - fract) < (divider_width*2)) {
419 /* too close to bottom */
425 if (get_allocation().get_width() * fract < (divider_width*2)) {
426 /* too close to top */
436 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
444 /* determine new position for handle */
449 d->translate_coordinates (*this, ev->x, ev->y, px, py);
451 Dividers::iterator prev = dividers.end();
452 Dividers::size_type div = 0;
454 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di, ++div) {
465 if (prev != dividers.end()) {
466 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
470 space_remaining = get_allocation().get_width() - prev_edge;
471 new_fract = (float) (px - prev_edge) / space_remaining;
473 if (prev != dividers.end()) {
474 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
478 space_remaining = get_allocation().get_height() - prev_edge;
479 new_fract = (float) (py - prev_edge) / space_remaining;
482 new_fract = min (1.0f, max (0.0f, new_fract));
484 if (!fract_is_ok (div, new_fract)) {
488 if (new_fract != d->fract) {
489 d->fract = new_fract;
490 reallocate (get_allocation ());
498 Pane::set_divider (Dividers::size_type div, float fract)
500 Dividers::iterator d = dividers.begin();
502 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
506 if (d == dividers.end()) {
507 /* caller is trying to set divider that does not exist
513 fract = max (0.0f, min (1.0f, fract));
515 if (!fract_is_ok (div, fract)) {
519 if (fract != (*d)->fract) {
521 /* our size hasn't changed, but our internal allocations have */
522 reallocate (get_allocation());
528 Pane::get_divider (Dividers::size_type div)
530 Dividers::iterator d = dividers.begin();
532 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
536 if (d == dividers.end()) {
537 /* caller is trying to set divider that does not exist
547 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
549 /* since the callback could modify the child list(s), make sure we keep
550 * the iterators safe;
553 for (Children::iterator c = children.begin(); c != children.end(); ) {
554 Children::iterator next = c;
556 callback (c->w->gobj(), callback_data);
560 if (include_internals) {
561 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
562 Dividers::iterator next = d;
564 callback (GTK_WIDGET((*d)->gobj()), callback_data);
570 Pane::Divider::Divider ()
574 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
582 Pane::Divider::on_expose_event (GdkEventExpose* ev)
584 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
585 get_style()->get_fg (get_state()));
587 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
588 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
589 draw_context->clip_preserve ();
590 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
591 draw_context->fill ();
597 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
599 d->get_window()->set_cursor (drag_cursor);
600 d->set_state (Gtk::STATE_SELECTED);
605 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
607 d->get_window()->set_cursor ();
608 d->set_state (Gtk::STATE_NORMAL);