2 Copyright (C) 2009 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 <gtkmm/box.h>
24 /** Parent class for children of a DnDVBox */
28 virtual ~DnDVBoxChild () {}
30 /** @return The widget that is to be put into the DnDVBox */
31 virtual Gtk::Widget& widget () = 0;
33 /** @return An EventBox containing the widget that should be used for selection, dragging etc. */
34 virtual Gtk::EventBox& action_widget () = 0;
36 /** @return Text to use in the icon that is dragged */
37 virtual std::string drag_text () const = 0;
40 /** A VBox whose contents can be dragged and dropped */
42 class DnDVBox : public Gtk::EventBox
45 DnDVBox () : _active (0), _drag_icon (0), _expecting_unwanted_button_event (false), _drag_placeholder (0)
47 _targets.push_back (Gtk::TargetEntry ("processor"));
51 Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
52 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK |
53 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
56 signal_button_press_event().connect (bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
57 signal_button_release_event().connect (bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
58 signal_drag_motion().connect (mem_fun (*this, &DnDVBox::drag_motion));
59 signal_drag_leave().connect (mem_fun (*this, &DnDVBox::drag_leave));
61 drag_dest_set (_targets);
62 signal_drag_data_received().connect (mem_fun (*this, &DnDVBox::drag_data_received));
72 /** Add a child at the end of the widget. The DnDVBox will take responsibility for deleting the child */
73 void add_child (T* child)
75 child->action_widget().drag_source_set (_targets);
76 child->action_widget().signal_drag_begin().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_begin), child));
77 child->action_widget().signal_drag_data_get().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_data_get), child));
78 child->action_widget().signal_drag_end().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_end), child));
79 child->action_widget().signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), child));
80 child->action_widget().signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), child));
82 _internal_vbox.pack_start (child->widget(), false, false);
84 _children.push_back (child);
85 child->widget().show_all ();
88 /** @return Children, sorted into the order that they are currently being displayed in the widget */
89 std::list<T*> children ()
91 std::list<T*> sorted_children;
93 std::list<Gtk::Widget*> widget_children = _internal_vbox.get_children ();
94 for (std::list<Gtk::Widget*>::iterator i = widget_children.begin(); i != widget_children.end(); ++i) {
95 T* c = child_from_widget (*i);
98 sorted_children.push_back (c);
102 return sorted_children;
105 /** @return Selected children */
106 std::list<T*> selection () const {
110 /** Set the `active' child; this is simply a child which is set to have the Gtk
111 * STATE_ACTIVE for whatever purposes the client may have.
112 * @param c Child, or 0 for none.
114 void set_active (T* c) {
115 T* old_active = _active;
118 setup_child_state (old_active);
121 setup_child_state (_active);
126 * @return true if the child is selected, otherwise false.
128 bool selected (T* child) const {
129 return (find (_selection.begin(), _selection.end(), child) != _selection.end());
132 /** Clear all children from the widget */
137 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
138 _internal_vbox.remove ((*i)->widget());
149 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
150 add_to_selection (*i);
153 SelectionChanged (); /* EMIT SIGNAL */
160 SelectionChanged (); /* EMIT SIGNAL */
163 /** Look at a y coordinate and find the children below y, and the ones either side.
164 * @param y y position.
165 * @param before Filled in with the child before, or 0.
166 * @param at Filled in with the child under y, or 0.
167 * @param after Filled in with the child after, or 0.
168 * @return Fractional position in terms of child height, or -1 if not over a child.
170 double get_children_around_position (int y, T** before, T** at, T** after) const
172 if (_children.empty()) {
173 *before = *at = *after = 0;
179 typename std::list<T*>::const_iterator j = _children.begin ();
181 /* index of current child */
183 /* top of current child */
185 /* bottom of current child */
186 double bottom = (*j)->widget().get_allocation().get_height ();
188 while (y >= bottom && j != _children.end()) {
196 if (j != _children.end()) {
197 bottom += (*j)->widget().get_allocation().get_height ();
201 if (j == _children.end()) {
210 *after = j != _children.end() ? *j : 0;
212 return i + ((y - top) / (*at)->widget().get_allocation().get_height());
215 /** @param y y coordinate.
216 * @return Pair consisting of the child under y (or 0) and the (fractional) index of the child under y (or -1)
218 std::pair<T*, double> get_child_at_position (int y) const
223 std::pair<T*, double> r;
225 r.second = get_children_around_position (y, &before, &r.first, &after);
230 void set_spacing (int s) {
231 _internal_vbox.set_spacing (s);
234 /** Children have been reordered by a drag */
235 sigc::signal<void> Reordered;
237 /** A button has been pressed over the widget */
238 sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
240 /** A button has been release over the widget */
241 sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
243 /** A child has been dropped onto this DnDVBox from another one;
244 * Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
246 sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
247 sigc::signal<void> SelectionChanged;
250 void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
254 /* make up an icon for the drag */
255 _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP);
257 Gtk::Allocation a = child->widget().get_allocation ();
258 _drag_icon->set_size_request (a.get_width(), a.get_height());
260 _drag_icon->signal_expose_event().connect (sigc::mem_fun (*this, &DnDVBox::icon_expose));
261 _drag_icon->set_name (get_name ());
263 /* make the icon transparent if possible */
264 Glib::RefPtr<Gdk::Screen const> s = _drag_icon->get_screen ();
265 Glib::RefPtr<Gdk::Colormap const> c = s->get_rgba_colormap ();
267 _drag_icon->set_colormap (c);
274 _drag_icon->get_size (w, h);
275 _drag_icon->drag_set_as_icon (context, w / 2, h / 2);
280 /* Draw the drag icon; we go to these lengths so that we can have the icon
281 * transparent if the window system supports it.
283 bool icon_expose (GdkEventExpose *)
285 cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj());
288 cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.5);
290 cairo_set_source_rgba (cr, 0, 0, 0, 1);
293 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
295 cairo_set_source_rgba (cr, 1, 1, 1, 0.8);
297 cairo_text_extents_t ext;
298 cairo_text_extents (cr, _drag_child->drag_text().c_str(), &ext);
300 cairo_move_to (cr, 0, ext.height);
301 cairo_show_text (cr, _drag_child->drag_text().c_str ());
309 void drag_data_get (Glib::RefPtr<Gdk::DragContext> const &, Gtk::SelectionData & selection_data, guint, guint, T* child)
311 selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child));
314 void drag_data_received (
315 Glib::RefPtr<Gdk::DragContext> const & context, int x, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time
318 /* work out where it was dropped */
319 std::pair<T*, double> const drop = get_child_at_position (y);
321 if (_drag_source == this) {
323 /* dropped from ourselves onto ourselves */
325 T* child = *((T **) selection_data.get_data());
327 /* reorder child widgets accordingly */
328 if (drop.first == 0) {
329 _internal_vbox.reorder_child (child->widget(), -1);
331 _internal_vbox.reorder_child (child->widget(), int (drop.second));
336 /* drag started in another DnDVBox; raise a signal to say what happened */
338 std::list<T*> dropped = _drag_source->selection ();
339 DropFromAnotherBox (_drag_source, drop.first, context);
342 context->drag_finish (false, false, time);
345 void drag_end (Glib::RefPtr<Gdk::DragContext> const &, T *)
351 remove_drag_placeholder ();
353 Reordered (); /* EMIT SIGNAL */
356 void remove_drag_placeholder ()
358 if (_drag_placeholder) {
359 _internal_vbox.remove (*_drag_placeholder);
360 _drag_placeholder = 0;
364 bool drag_motion (Glib::RefPtr<Gdk::DragContext> const &, int x, int y, guint)
366 if (_children.empty ()) {
374 /* decide where we currently are */
375 double const c = get_children_around_position (y, &before, &at, &after);
377 /* whether we're in the top or bottom half of the child that we're over */
378 bool top_half = (c - int (c)) < 0.5;
380 if (top_half && (before == _drag_child || at == _drag_child)) {
381 /* dropping here would have no effect, so remove the visual cue */
382 remove_drag_placeholder ();
386 if (!top_half && (at == _drag_child || after == _drag_child)) {
387 /* dropping here would have no effect, so remove the visual cue */
388 remove_drag_placeholder ();
392 /* the index that the placeholder should be put at */
393 int const n = int (c + 0.5);
395 if (_drag_placeholder == 0) {
396 _drag_placeholder = manage (new Gtk::Label (""));
397 _internal_vbox.pack_start (*_drag_placeholder, false, false);
398 _drag_placeholder->show ();
402 _internal_vbox.reorder_child (*_drag_placeholder, -1);
404 _internal_vbox.reorder_child (*_drag_placeholder, n);
410 void drag_leave (Glib::RefPtr<Gdk::DragContext> const &, guint)
412 remove_drag_placeholder ();
415 bool button_press (GdkEventButton* ev, T* child)
417 if (_expecting_unwanted_button_event == true && child == 0) {
418 _expecting_unwanted_button_event = false;
423 _expecting_unwanted_button_event = true;
426 if (ev->button == 1 || ev->button == 3) {
428 if (!selected (child)) {
430 if ((ev->state & Gdk::SHIFT_MASK) && !_selection.empty()) {
432 /* Shift-click; select all between the clicked child and any existing selections */
434 bool selecting = false;
436 for (typename std::list<T*>::const_iterator i = _children.begin(); i != _children.end(); ++i) {
438 bool const was_selected = selected (*i);
440 if (selecting && !was_selected) {
441 add_to_selection (*i);
444 if (!selecting && !done) {
447 } else if (*i == child) {
449 add_to_selection (child);
451 } else if (selecting) {
452 if (was_selected || *i == child) {
461 if ((ev->state & Gdk::CONTROL_MASK) == 0) {
466 add_to_selection (child);
471 SelectionChanged (); /* EMIT SIGNAL */
474 /* XXX THIS NEEDS GENERALIZING FOR OS X */
475 if (ev->button == 1 && (ev->state & Gdk::CONTROL_MASK)) {
476 if (child && selected (child)) {
477 remove_from_selection (child);
478 SelectionChanged (); /* EMIT SIGNAL */
485 return ButtonPress (ev, child); /* EMIT SIGNAL */
488 bool button_release (GdkEventButton* ev, T* child)
490 if (_expecting_unwanted_button_event == true && child == 0) {
491 _expecting_unwanted_button_event = false;
496 _expecting_unwanted_button_event = true;
499 return ButtonRelease (ev, child); /* EMIT SIGNAL */
502 /** Setup a child's GTK state correctly */
503 void setup_child_state (T* c)
508 c->action_widget().set_state (Gtk::STATE_ACTIVE);
509 } else if (selected (c)) {
510 c->action_widget().set_state (Gtk::STATE_SELECTED);
512 c->action_widget().set_state (Gtk::STATE_NORMAL);
516 void clear_selection ()
518 std::list<T*> old_selection = _selection;
520 for (typename std::list<T*>::iterator i = old_selection.begin(); i != old_selection.end(); ++i) {
521 setup_child_state (*i);
525 void add_to_selection (T* child)
527 _selection.push_back (child);
528 setup_child_state (child);
531 void remove_from_selection (T* child)
533 typename std::list<T*>::iterator x = find (_selection.begin(), _selection.end(), child);
534 if (x != _selection.end()) {
535 _selection.erase (x);
536 setup_child_state (*x);
540 T* child_from_widget (Gtk::Widget const * w) const
542 typename std::list<T*>::const_iterator i = _children.begin();
543 while (i != _children.end() && &(*i)->widget() != w) {
547 if (i == _children.end()) {
554 Gtk::VBox _internal_vbox;
555 std::list<Gtk::TargetEntry> _targets;
556 std::list<T*> _children;
557 std::list<T*> _selection;
559 Gtk::Window* _drag_icon;
560 bool _expecting_unwanted_button_event;
561 /** A blank label used as a placeholder to indicate where a dragged item would
562 * go if it were dropped now.
564 Gtk::Label* _drag_placeholder;
565 /** Our child being dragged, or 0 */
567 /** true if we can have an alpha-blended drag icon */
570 static DnDVBox* _drag_source;
575 DnDVBox<T>* DnDVBox<T>::_drag_source = 0;