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_drag_cursor (Gdk::Cursor c)
54 Pane::on_size_request (GtkRequisition* req)
56 GtkRequisition largest;
58 /* iterate over all children, get their size requests */
60 /* horizontal pane is as high as its tallest child, including the dividers.
61 * Its width is the sum of the children plus the dividers.
63 * vertical pane is as wide as its widest child, including the dividers.
64 * Its height is the sum of the children plus the dividers.
68 largest.width = (children.size() - 1) * divider_width;
71 largest.height = (children.size() - 1) * divider_width;
75 for (Children::iterator child = children.begin(); child != children.end(); ++child) {
78 (*child)->size_request (r);
81 largest.height = max (largest.height, r.height);
82 largest.width += r.width;
84 largest.width = max (largest.width, r.width);
85 largest.height += r.height;
93 Pane::child_type_vfunc() const
95 /* We accept any number of any types of widgets */
96 return Gtk::Widget::get_type();
102 Divider* d = new Divider;
103 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
104 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
105 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
106 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
107 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
108 d->set_parent (*this);
111 dividers.push_back (d);
115 Pane::on_add (Widget* w)
117 children.push_back (w);
119 w->set_parent (*this);
121 while (dividers.size() < (children.size() - 1)) {
127 Pane::on_remove (Widget* w)
134 Pane::on_size_allocate (Gtk::Allocation& alloc)
137 Container::on_size_allocate (alloc);
141 Pane::reallocate (Gtk::Allocation const & alloc)
144 int xpos = alloc.get_x();
145 int ypos = alloc.get_y();
148 if (children.empty()) {
152 if (children.size() == 1) {
153 /* only child gets the full allocation */
154 children.front()->size_allocate (alloc);
159 remaining = alloc.get_width ();
161 remaining = alloc.get_height ();
164 Children::iterator child;
165 Children::iterator next;
166 Dividers::iterator div;
168 for (child = children.begin(), div = dividers.begin(); child != children.end(); ) {
170 Gtk::Allocation child_alloc;
174 child_alloc.set_x (xpos);
175 child_alloc.set_y (ypos);
177 if (next == children.end()) {
178 /* last child gets all the remaining space */
181 /* child gets the fraction of the remaining space given by the divider that follows it */
182 fract = (*div)->fract;
186 (*child)->size_request (cr);
189 child_alloc.set_width ((gint) floor (remaining * fract));
190 child_alloc.set_height (alloc.get_height());
191 remaining = max (0, (remaining - child_alloc.get_width()));
192 xpos += child_alloc.get_width();
194 child_alloc.set_width (alloc.get_width());
195 child_alloc.set_height ((gint) floor (remaining * fract));
196 remaining = max (0, (remaining - child_alloc.get_height()));
197 ypos += child_alloc.get_height ();
200 (*child)->size_allocate (child_alloc);
203 if (child == children.end()) {
204 /* done, no more children, no need for a divider */
208 /* add a divider between children */
210 Gtk::Allocation divider_allocation;
212 divider_allocation.set_x (xpos);
213 divider_allocation.set_y (ypos);
216 divider_allocation.set_width (divider_width);
217 divider_allocation.set_height (alloc.get_height());
218 remaining = max (0, remaining - divider_width);
219 xpos += divider_width;
221 divider_allocation.set_width (alloc.get_width());
222 divider_allocation.set_height (divider_width);
223 remaining = max (0, remaining - divider_width);
224 ypos += divider_width;
227 (*div)->size_allocate (divider_allocation);
233 Pane::on_expose_event (GdkEventExpose* ev)
235 Children::iterator child;
236 Dividers::iterator div;
238 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child, ++div) {
240 propagate_expose (**child, ev);
242 if (div != dividers.end()) {
243 propagate_expose (**div, ev);
251 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
260 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
265 children.front()->queue_resize ();
273 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
281 /* determine new position for handle */
286 d->translate_coordinates (*this, ev->x, ev->y, px, py);
288 Dividers::iterator prev = dividers.end();
290 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
301 if (prev != dividers.end()) {
302 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
306 space_remaining = get_allocation().get_width() - prev_edge;
307 new_fract = (float) (px - prev_edge) / space_remaining;
309 if (prev != dividers.end()) {
310 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
314 space_remaining = get_allocation().get_height() - prev_edge;
315 new_fract = (float) (py - prev_edge) / space_remaining;
318 new_fract = min (1.0f, max (0.0f, new_fract));
320 if (new_fract != d->fract) {
321 d->fract = new_fract;
322 reallocate (get_allocation ());
330 Pane::set_divider (Dividers::size_type div, float fract)
334 Dividers::iterator d = dividers.begin();
338 if (d == dividers.end()) {
339 /* caller is trying to set divider that does not exist
346 if (fract != (*d)->fract) {
352 /* our size hasn't changed, but our internal allocations have */
353 reallocate (get_allocation());
359 Pane::get_divider (Dividers::size_type div)
361 Dividers::iterator d = dividers.begin();
365 if (d == dividers.end()) {
366 /* caller is trying to set divider that does not exist
377 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
379 /* since the callback could modify the child list(s), make sure we keep
380 * the iterators safe;
383 for (Children::iterator w = children.begin(); w != children.end(); ) {
384 Children::iterator next = w;
386 callback ((*w)->gobj(), callback_data);
390 if (include_internals) {
391 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
392 Dividers::iterator next = d;
394 callback (GTK_WIDGET((*d)->gobj()), callback_data);
400 Pane::Divider::Divider ()
404 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
412 Pane::Divider::on_expose_event (GdkEventExpose* ev)
414 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
415 get_style()->get_fg (get_state()));
417 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
418 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
419 draw_context->clip_preserve ();
420 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
421 draw_context->fill ();
427 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
429 d->get_window()->set_cursor (drag_cursor);
430 d->set_state (Gtk::STATE_SELECTED);
435 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
437 d->get_window()->set_cursor ();
438 d->set_state (Gtk::STATE_NORMAL);