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_H_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::on_add (Widget* w)
128 children.push_back (Child (w, 0));
130 w->set_parent (*this);
132 while (dividers.size() < (children.size() - 1)) {
138 Pane::on_remove (Widget* w)
142 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
151 Pane::on_size_allocate (Gtk::Allocation& alloc)
154 Container::on_size_allocate (alloc);
158 Pane::reallocate (Gtk::Allocation const & alloc)
161 int xpos = alloc.get_x();
162 int ypos = alloc.get_y();
165 if (children.empty()) {
169 if (children.size() == 1) {
170 /* only child gets the full allocation */
171 children.front().w->size_allocate (alloc);
176 remaining = alloc.get_width ();
178 remaining = alloc.get_height ();
181 Children::iterator child;
182 Children::iterator next;
183 Dividers::iterator div;
185 for (child = children.begin(), div = dividers.begin(); child != children.end(); ) {
187 Gtk::Allocation child_alloc;
191 child_alloc.set_x (xpos);
192 child_alloc.set_y (ypos);
194 if (next == children.end()) {
195 /* last child gets all the remaining space */
198 /* child gets the fraction of the remaining space given by the divider that follows it */
199 fract = (*div)->fract;
203 child->w->size_request (cr);
206 child_alloc.set_width ((gint) floor (remaining * fract));
207 child_alloc.set_height (alloc.get_height());
208 remaining = max (0, (remaining - child_alloc.get_width()));
209 xpos += child_alloc.get_width();
211 child_alloc.set_width (alloc.get_width());
212 child_alloc.set_height ((gint) floor (remaining * fract));
213 remaining = max (0, (remaining - child_alloc.get_height()));
214 ypos += child_alloc.get_height ();
217 if (child->minsize) {
219 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
221 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
225 child->w->size_allocate (child_alloc);
228 if (child == children.end()) {
229 /* done, no more children, no need for a divider */
233 /* add a divider between children */
235 Gtk::Allocation divider_allocation;
237 divider_allocation.set_x (xpos);
238 divider_allocation.set_y (ypos);
241 divider_allocation.set_width (divider_width);
242 divider_allocation.set_height (alloc.get_height());
243 remaining = max (0, remaining - divider_width);
244 xpos += divider_width;
246 divider_allocation.set_width (alloc.get_width());
247 divider_allocation.set_height (divider_width);
248 remaining = max (0, remaining - divider_width);
249 ypos += divider_width;
252 (*div)->size_allocate (divider_allocation);
258 Pane::on_expose_event (GdkEventExpose* ev)
260 Children::iterator child;
261 Dividers::iterator div;
263 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child, ++div) {
265 if (child->w->is_visible()) {
266 propagate_expose (*(child->w), ev);
269 if ((div != dividers.end()) && (*div)->is_visible()) {
270 propagate_expose (**div, ev);
278 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
287 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
292 children.front().w->queue_resize ();
300 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
308 /* determine new position for handle */
313 d->translate_coordinates (*this, ev->x, ev->y, px, py);
315 Dividers::iterator prev = dividers.end();
317 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
328 if (prev != dividers.end()) {
329 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
333 space_remaining = get_allocation().get_width() - prev_edge;
334 new_fract = (float) (px - prev_edge) / space_remaining;
336 if (prev != dividers.end()) {
337 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
341 space_remaining = get_allocation().get_height() - prev_edge;
342 new_fract = (float) (py - prev_edge) / space_remaining;
345 new_fract = min (1.0f, max (0.0f, new_fract));
347 if (new_fract != d->fract) {
348 d->fract = new_fract;
349 reallocate (get_allocation ());
357 Pane::set_divider (Dividers::size_type div, float fract)
361 Dividers::iterator d = dividers.begin();
365 if (d == dividers.end()) {
366 /* caller is trying to set divider that does not exist
373 if (fract != (*d)->fract) {
379 /* our size hasn't changed, but our internal allocations have */
380 reallocate (get_allocation());
386 Pane::get_divider (Dividers::size_type div)
388 Dividers::iterator d = dividers.begin();
392 if (d == dividers.end()) {
393 /* caller is trying to set divider that does not exist
404 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
406 /* since the callback could modify the child list(s), make sure we keep
407 * the iterators safe;
410 for (Children::iterator c = children.begin(); c != children.end(); ) {
411 Children::iterator next = c;
413 callback (c->w->gobj(), callback_data);
417 if (include_internals) {
418 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
419 Dividers::iterator next = d;
421 callback (GTK_WIDGET((*d)->gobj()), callback_data);
427 Pane::Divider::Divider ()
431 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
439 Pane::Divider::on_expose_event (GdkEventExpose* ev)
441 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
442 get_style()->get_fg (get_state()));
444 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
445 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
446 draw_context->clip_preserve ();
447 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
448 draw_context->fill ();
454 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
456 d->get_window()->set_cursor (drag_cursor);
457 d->set_state (Gtk::STATE_SELECTED);
462 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
464 d->get_window()->set_cursor ();
465 d->set_state (Gtk::STATE_NORMAL);