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;
38 set_has_window (false);
41 drag_cursor = Cursor (SB_H_DOUBLE_ARROW);
43 drag_cursor = Cursor (SB_V_DOUBLE_ARROW);
48 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
50 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
59 Pane::set_drag_cursor (Gdk::Cursor c)
65 Pane::on_size_request (GtkRequisition* req)
67 GtkRequisition largest;
69 /* iterate over all children, get their size requests */
71 /* horizontal pane is as high as its tallest child, including the dividers.
72 * Its width is the sum of the children plus the dividers.
74 * vertical pane is as wide as its widest child, including the dividers.
75 * Its height is the sum of the children plus the dividers.
79 largest.width = (children.size() - 1) * divider_width;
82 largest.height = (children.size() - 1) * divider_width;
86 for (Children::iterator child = children.begin(); child != children.end(); ++child) {
89 child->w->size_request (r);
92 largest.height = max (largest.height, r.height);
93 largest.width += r.width;
95 largest.width = max (largest.width, r.width);
96 largest.height += r.height;
104 Pane::child_type_vfunc() const
106 /* We accept any number of any types of widgets */
107 return Gtk::Widget::get_type();
113 Divider* d = new Divider;
114 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
115 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
116 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
117 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
118 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
119 d->set_parent (*this);
122 dividers.push_back (d);
126 Pane::handle_child_visibility ()
128 reallocate (get_allocation());
132 Pane::on_add (Widget* w)
134 children.push_back (Child (w, 0));
136 w->set_parent (*this);
138 w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
139 w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
141 while (dividers.size() < (children.size() - 1)) {
147 Pane::on_remove (Widget* w)
149 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
160 Pane::on_size_allocate (Gtk::Allocation& alloc)
163 Container::on_size_allocate (alloc);
167 Pane::reallocate (Gtk::Allocation const & alloc)
170 int xpos = alloc.get_x();
171 int ypos = alloc.get_y();
174 if (children.empty()) {
178 if (children.size() == 1) {
179 /* only child gets the full allocation */
180 children.front().w->size_allocate (alloc);
185 remaining = alloc.get_width ();
187 remaining = alloc.get_height ();
190 Children::iterator child;
191 Children::iterator next;
192 Dividers::iterator div;
194 child = children.begin();
196 /* skip initial hidden children */
198 while (child != children.end()) {
199 if (child->w->is_visible()) {
205 for (div = dividers.begin(); child != children.end(); ) {
207 Gtk::Allocation child_alloc;
211 /* Move on to next *visible* child */
213 while (++next != children.end()) {
214 if (next->w->is_visible()) {
219 child_alloc.set_x (xpos);
220 child_alloc.set_y (ypos);
222 if (next == children.end()) {
223 /* last child gets all the remaining space */
226 /* child gets the fraction of the remaining space given by the divider that follows it */
227 fract = (*div)->fract;
231 child->w->size_request (cr);
234 child_alloc.set_width ((gint) floor (remaining * fract));
235 child_alloc.set_height (alloc.get_height());
236 remaining = max (0, (remaining - child_alloc.get_width()));
237 xpos += child_alloc.get_width();
239 child_alloc.set_width (alloc.get_width());
240 child_alloc.set_height ((gint) floor (remaining * fract));
241 remaining = max (0, (remaining - child_alloc.get_height()));
242 ypos += child_alloc.get_height ();
245 if (child->minsize) {
247 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
249 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
253 child->w->size_allocate (child_alloc);
255 if (next == children.end()) {
256 /* done, no more children, no need for a divider */
262 /* add a divider between children */
264 Gtk::Allocation divider_allocation;
266 divider_allocation.set_x (xpos);
267 divider_allocation.set_y (ypos);
270 divider_allocation.set_width (divider_width);
271 divider_allocation.set_height (alloc.get_height());
272 remaining = max (0, remaining - divider_width);
273 xpos += divider_width;
275 divider_allocation.set_width (alloc.get_width());
276 divider_allocation.set_height (divider_width);
277 remaining = max (0, remaining - divider_width);
278 ypos += divider_width;
281 (*div)->size_allocate (divider_allocation);
286 /* hide all remaining dividers */
288 while (div != dividers.end()) {
295 Pane::on_expose_event (GdkEventExpose* ev)
297 Children::iterator child;
298 Dividers::iterator div;
300 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child, ++div) {
302 if (child->w->is_visible()) {
303 propagate_expose (*(child->w), ev);
306 if ((div != dividers.end()) && (*div)->is_visible()) {
307 propagate_expose (**div, ev);
315 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
324 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
329 children.front().w->queue_resize ();
337 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
345 /* determine new position for handle */
350 d->translate_coordinates (*this, ev->x, ev->y, px, py);
352 Dividers::iterator prev = dividers.end();
354 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
365 if (prev != dividers.end()) {
366 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
370 space_remaining = get_allocation().get_width() - prev_edge;
371 new_fract = (float) (px - prev_edge) / space_remaining;
373 if (prev != dividers.end()) {
374 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
378 space_remaining = get_allocation().get_height() - prev_edge;
379 new_fract = (float) (py - prev_edge) / space_remaining;
382 new_fract = min (1.0f, max (0.0f, new_fract));
384 if (new_fract != d->fract) {
385 d->fract = new_fract;
386 reallocate (get_allocation ());
394 Pane::set_divider (Dividers::size_type div, float fract)
398 Dividers::iterator d = dividers.begin();
402 if (d == dividers.end()) {
403 /* caller is trying to set divider that does not exist
410 if (fract != (*d)->fract) {
416 /* our size hasn't changed, but our internal allocations have */
417 reallocate (get_allocation());
423 Pane::get_divider (Dividers::size_type div)
425 Dividers::iterator d = dividers.begin();
429 if (d == dividers.end()) {
430 /* caller is trying to set divider that does not exist
441 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
443 /* since the callback could modify the child list(s), make sure we keep
444 * the iterators safe;
447 for (Children::iterator c = children.begin(); c != children.end(); ) {
448 Children::iterator next = c;
450 callback (c->w->gobj(), callback_data);
454 if (include_internals) {
455 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
456 Dividers::iterator next = d;
458 callback (GTK_WIDGET((*d)->gobj()), callback_data);
464 Pane::Divider::Divider ()
468 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
476 Pane::Divider::on_expose_event (GdkEventExpose* ev)
478 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
479 get_style()->get_fg (get_state()));
481 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
482 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
483 draw_context->clip_preserve ();
484 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
485 draw_context->fill ();
491 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
493 d->get_window()->set_cursor (drag_cursor);
494 d->set_state (Gtk::STATE_SELECTED);
499 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
501 d->get_window()->set_cursor ();
502 d->set_state (Gtk::STATE_NORMAL);