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);
49 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
55 Pane::set_child_minsize (Gtk::Widget const& w, int32_t minsize)
57 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
66 Pane::set_drag_cursor (Gdk::Cursor c)
72 Pane::on_size_request (GtkRequisition* req)
74 GtkRequisition largest;
76 /* iterate over all children, get their size requests */
78 /* horizontal pane is as high as its tallest child, including the dividers.
79 * Its width is the sum of the children plus the dividers.
81 * vertical pane is as wide as its widest child, including the dividers.
82 * Its height is the sum of the children plus the dividers.
86 largest.width = (children.size() - 1) * divider_width;
89 largest.height = (children.size() - 1) * divider_width;
93 for (Children::iterator child = children.begin(); child != children.end(); ++child) {
96 child->w->size_request (r);
99 largest.height = max (largest.height, r.height);
100 largest.width += r.width;
102 largest.width = max (largest.width, r.width);
103 largest.height += r.height;
111 Pane::child_type_vfunc() const
113 /* We accept any number of any types of widgets */
114 return Gtk::Widget::get_type();
120 Divider* d = new Divider;
121 d->set_name (X_("Divider"));
122 d->signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_press_event), d), false);
123 d->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_release_event), d), false);
124 d->signal_motion_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_motion_event), d), false);
125 d->signal_enter_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_enter_event), d), false);
126 d->signal_leave_notify_event().connect (sigc::bind (sigc::mem_fun (*this, &Pane::handle_leave_event), d), false);
127 d->set_parent (*this);
130 dividers.push_back (d);
134 Pane::handle_child_visibility ()
136 reallocate (get_allocation());
140 Pane::on_add (Widget* w)
142 children.push_back (Child (w, 0));
144 w->set_parent (*this);
146 w->signal_show().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
147 w->signal_hide().connect (sigc::mem_fun (*this, &Pane::handle_child_visibility));
149 while (dividers.size() < (children.size() - 1)) {
155 Pane::on_remove (Widget* w)
157 for (Children::iterator c = children.begin(); c != children.end(); ++c) {
167 Pane::on_size_allocate (Gtk::Allocation& alloc)
170 Container::on_size_allocate (alloc);
174 Pane::reallocate (Gtk::Allocation const & alloc)
177 int xpos = alloc.get_x();
178 int ypos = alloc.get_y();
181 if (children.empty()) {
185 if (children.size() == 1) {
186 /* only child gets the full allocation */
187 children.front().w->size_allocate (alloc);
192 remaining = alloc.get_width ();
194 remaining = alloc.get_height ();
197 Children::iterator child;
198 Children::iterator next;
199 Dividers::iterator div;
201 child = children.begin();
203 /* skip initial hidden children */
205 while (child != children.end()) {
206 if (child->w->is_visible()) {
212 for (div = dividers.begin(); child != children.end(); ) {
214 Gtk::Allocation child_alloc;
218 /* Move on to next *visible* child */
220 while (++next != children.end()) {
221 if (next->w->is_visible()) {
226 child_alloc.set_x (xpos);
227 child_alloc.set_y (ypos);
229 if (next == children.end()) {
230 /* last child gets all the remaining space */
233 /* child gets the fraction of the remaining space given by the divider that follows it */
234 fract = (*div)->fract;
238 child->w->size_request (cr);
241 child_alloc.set_width ((gint) floor (remaining * fract));
242 child_alloc.set_height (alloc.get_height());
243 remaining = max (0, (remaining - child_alloc.get_width()));
244 xpos += child_alloc.get_width();
246 child_alloc.set_width (alloc.get_width());
247 child_alloc.set_height ((gint) floor (remaining * fract));
248 remaining = max (0, (remaining - child_alloc.get_height()));
249 ypos += child_alloc.get_height ();
252 if (child->minsize) {
254 child_alloc.set_width (max (child_alloc.get_width(), child->minsize));
256 child_alloc.set_height (max (child_alloc.get_height(), child->minsize));
260 child->w->size_allocate (child_alloc);
262 if (next == children.end()) {
263 /* done, no more children, no need for a divider */
269 /* add a divider between children */
271 Gtk::Allocation divider_allocation;
273 divider_allocation.set_x (xpos);
274 divider_allocation.set_y (ypos);
277 divider_allocation.set_width (divider_width);
278 divider_allocation.set_height (alloc.get_height());
279 remaining = max (0, remaining - divider_width);
280 xpos += divider_width;
282 divider_allocation.set_width (alloc.get_width());
283 divider_allocation.set_height (divider_width);
284 remaining = max (0, remaining - divider_width);
285 ypos += divider_width;
288 (*div)->size_allocate (divider_allocation);
293 /* hide all remaining dividers */
295 while (div != dividers.end()) {
302 Pane::on_expose_event (GdkEventExpose* ev)
304 Children::iterator child;
305 Dividers::iterator div;
307 for (child = children.begin(), div = dividers.begin(); child != children.end(); ++child, ++div) {
309 if (child->w->is_visible()) {
310 propagate_expose (*(child->w), ev);
313 if ((div != dividers.end()) && (*div)->is_visible()) {
314 propagate_expose (**div, ev);
322 Pane::handle_press_event (GdkEventButton* ev, Divider* d)
331 Pane::handle_release_event (GdkEventButton* ev, Divider* d)
336 children.front().w->queue_resize ();
344 Pane::handle_motion_event (GdkEventMotion* ev, Divider* d)
352 /* determine new position for handle */
357 d->translate_coordinates (*this, ev->x, ev->y, px, py);
359 Dividers::iterator prev = dividers.end();
361 for (Dividers::iterator di = dividers.begin(); di != dividers.end(); ++di) {
372 if (prev != dividers.end()) {
373 prev_edge = (*prev)->get_allocation().get_x() + (*prev)->get_allocation().get_width();
377 space_remaining = get_allocation().get_width() - prev_edge;
378 new_fract = (float) (px - prev_edge) / space_remaining;
380 if (prev != dividers.end()) {
381 prev_edge = (*prev)->get_allocation().get_y() + (*prev)->get_allocation().get_height();
385 space_remaining = get_allocation().get_height() - prev_edge;
386 new_fract = (float) (py - prev_edge) / space_remaining;
389 new_fract = min (1.0f, max (0.0f, new_fract));
391 if (new_fract != d->fract) {
392 d->fract = new_fract;
393 reallocate (get_allocation ());
401 Pane::set_divider (Dividers::size_type div, float fract)
403 Dividers::iterator d = dividers.begin();
405 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
409 if (d == dividers.end()) {
410 /* caller is trying to set divider that does not exist
416 fract = max (0.0f, min (1.0f, fract));
418 if (fract != (*d)->fract) {
420 /* our size hasn't changed, but our internal allocations have */
421 reallocate (get_allocation());
427 Pane::get_divider (Dividers::size_type div)
429 Dividers::iterator d = dividers.begin();
431 for (d = dividers.begin(); d != dividers.end() && div != 0; ++d, --div) {
435 if (d == dividers.end()) {
436 /* caller is trying to set divider that does not exist
446 Pane::forall_vfunc (gboolean include_internals, GtkCallback callback, gpointer callback_data)
448 /* since the callback could modify the child list(s), make sure we keep
449 * the iterators safe;
452 for (Children::iterator c = children.begin(); c != children.end(); ) {
453 Children::iterator next = c;
455 callback (c->w->gobj(), callback_data);
459 if (include_internals) {
460 for (Dividers::iterator d = dividers.begin(); d != dividers.end(); ) {
461 Dividers::iterator next = d;
463 callback (GTK_WIDGET((*d)->gobj()), callback_data);
469 Pane::Divider::Divider ()
473 set_events (Gdk::EventMask (Gdk::BUTTON_PRESS|
481 Pane::Divider::on_expose_event (GdkEventExpose* ev)
483 Gdk::Color c = (dragging ? get_style()->get_fg (Gtk::STATE_ACTIVE) :
484 get_style()->get_fg (get_state()));
486 Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
487 draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
488 draw_context->clip_preserve ();
489 draw_context->set_source_rgba (c.get_red_p(), c.get_green_p(), c.get_blue_p(), 1.0);
490 draw_context->fill ();
496 Pane::handle_enter_event (GdkEventCrossing*, Divider* d)
498 d->get_window()->set_cursor (drag_cursor);
499 d->set_state (Gtk::STATE_SELECTED);
504 Pane::handle_leave_event (GdkEventCrossing*, Divider* d)
506 d->get_window()->set_cursor ();
507 d->set_state (Gtk::STATE_NORMAL);