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;
39 /** Set the child's visual state */
40 virtual void set_visual_state (Gtk::StateType) = 0;
43 /** A VBox whose contents can be dragged and dropped */
45 class DnDVBox : public Gtk::EventBox
48 DnDVBox () : _active (0), _drag_icon (0), _expecting_unwanted_button_event (false), _drag_placeholder (0)
50 _targets.push_back (Gtk::TargetEntry ("processor"));
54 Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
55 Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK |
56 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
59 signal_button_press_event().connect (bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
60 signal_button_release_event().connect (bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
61 signal_drag_motion().connect (mem_fun (*this, &DnDVBox::drag_motion));
62 signal_drag_leave().connect (mem_fun (*this, &DnDVBox::drag_leave));
64 _internal_vbox.show ();
66 drag_dest_set (_targets);
67 signal_drag_data_received().connect (mem_fun (*this, &DnDVBox::drag_data_received));
77 /** Add a child at the end of the widget. The DnDVBox will take responsibility for deleting the child */
78 void add_child (T* child)
80 child->action_widget().drag_source_set (_targets);
81 child->action_widget().signal_drag_begin().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_begin), child));
82 child->action_widget().signal_drag_data_get().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_data_get), child));
83 child->action_widget().signal_drag_end().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_end), child));
84 child->action_widget().signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), child));
85 child->action_widget().signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), child));
87 _internal_vbox.pack_start (child->widget(), false, false);
89 _children.push_back (child);
90 child->widget().show ();
93 /** @return Children, sorted into the order that they are currently being displayed in the widget */
94 std::list<T*> children ()
96 std::list<T*> sorted_children;
98 std::list<Gtk::Widget*> widget_children = _internal_vbox.get_children ();
99 for (std::list<Gtk::Widget*>::iterator i = widget_children.begin(); i != widget_children.end(); ++i) {
100 T* c = child_from_widget (*i);
103 sorted_children.push_back (c);
107 return sorted_children;
110 /** @return Selected children */
111 std::list<T*> selection () const {
115 /** Set the `active' child; this is simply a child which is set to have the Gtk
116 * STATE_ACTIVE for whatever purposes the client may have.
117 * @param c Child, or 0 for none.
119 void set_active (T* c) {
120 T* old_active = _active;
123 setup_child_state (old_active);
126 setup_child_state (_active);
131 * @return true if the child is selected, otherwise false.
133 bool selected (T* child) const {
134 return (find (_selection.begin(), _selection.end(), child) != _selection.end());
137 /** Clear all children from the widget */
142 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
143 _internal_vbox.remove ((*i)->widget());
154 for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
155 add_to_selection (*i);
158 SelectionChanged (); /* EMIT SIGNAL */
165 SelectionChanged (); /* EMIT SIGNAL */
168 /** Look at a y coordinate and find the children below y, and the ones either side.
169 * @param y y position.
170 * @param before Filled in with the child before, or 0.
171 * @param at Filled in with the child under y, or 0.
172 * @param after Filled in with the child after, or 0.
173 * @return Fractional position in terms of child height, or -1 if not over a child.
175 double get_children_around_position (int y, T** before, T** at, T** after) const
177 if (_children.empty()) {
178 *before = *at = *after = 0;
184 typename std::list<T*>::const_iterator j = _children.begin ();
186 /* index of current child */
188 /* top of current child */
190 /* bottom of current child */
191 double bottom = (*j)->widget().get_allocation().get_height ();
193 while (y >= bottom && j != _children.end()) {
201 if (j != _children.end()) {
202 bottom += (*j)->widget().get_allocation().get_height ();
206 if (j == _children.end()) {
215 *after = j != _children.end() ? *j : 0;
217 return i + ((y - top) / (*at)->widget().get_allocation().get_height());
220 /** @param y y coordinate.
221 * @return Pair consisting of the child under y (or 0) and the (fractional) index of the child under y (or -1)
223 std::pair<T*, double> get_child_at_position (int y) const
228 std::pair<T*, double> r;
230 r.second = get_children_around_position (y, &before, &r.first, &after);
235 void set_spacing (int s) {
236 _internal_vbox.set_spacing (s);
239 /** Children have been reordered by a drag */
240 sigc::signal<void> Reordered;
242 /** A button has been pressed over the widget */
243 sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
245 /** A button has been release over the widget */
246 sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
248 /** A child has been dropped onto this DnDVBox from another one;
249 * Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
251 sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
252 sigc::signal<void> SelectionChanged;
255 void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
259 /* make up an icon for the drag */
260 _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP);
262 Gtk::Allocation a = child->widget().get_allocation ();
263 _drag_icon->set_size_request (a.get_width(), a.get_height());
265 _drag_icon->signal_expose_event().connect (sigc::mem_fun (*this, &DnDVBox::icon_expose));
266 _drag_icon->set_name (get_name ());
268 /* make the icon transparent if possible */
269 Glib::RefPtr<Gdk::Screen const> s = _drag_icon->get_screen ();
270 Glib::RefPtr<Gdk::Colormap const> c = s->get_rgba_colormap ();
272 _drag_icon->set_colormap (c);
279 _drag_icon->get_size (w, h);
280 _drag_icon->drag_set_as_icon (context, w / 2, h / 2);
285 /* Draw the drag icon */
286 bool icon_expose (GdkEventExpose* ev)
288 /* Just grab the child's widget and use that */
291 _drag_icon->get_size (w, h);
293 cairo_t* cr = gdk_cairo_create (_drag_icon->get_window()->gobj ());
295 Glib::RefPtr<Gdk::Pixmap> p = _drag_child->widget().get_snapshot();
296 gdk_cairo_set_source_pixmap (cr, p->gobj(), 0, 0);
297 cairo_rectangle (cr, 0, 0, w, h);
304 void drag_data_get (Glib::RefPtr<Gdk::DragContext> const &, Gtk::SelectionData & selection_data, guint, guint, T* child)
306 selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child));
309 void drag_data_received (
310 Glib::RefPtr<Gdk::DragContext> const & context, int x, int y, Gtk::SelectionData const & selection_data, guint /*info*/, guint time
313 /* work out where it was dropped */
314 std::pair<T*, double> const drop = get_child_at_position (y);
318 if (_drag_source == this) {
320 /* dropped from ourselves onto ourselves */
322 T* child = *((T **) selection_data.get_data());
324 if (drop.first == 0) {
325 _internal_vbox.reorder_child (child->widget(), -1);
328 /* where in the list this child should be dropped */
329 int target = drop.second + 0.5;
331 /* find out whether the child was `picked up' from before the drop position */
333 typename std::list<T*>::const_iterator i = _children.begin ();
334 while (i != _children.end() && *i != child && n < target) {
339 /* if so, adjust the drop position to account for this */
344 _internal_vbox.reorder_child (child->widget(), target);
349 /* drag started in another DnDVBox; raise a signal to say what happened */
351 std::list<T*> dropped = _drag_source->selection ();
352 DropFromAnotherBox (_drag_source, drop.first, context);
355 context->drag_finish (false, false, time);
358 void drag_end (Glib::RefPtr<Gdk::DragContext> const &, T *)
364 remove_drag_placeholder ();
366 Reordered (); /* EMIT SIGNAL */
369 void remove_drag_placeholder ()
371 if (_drag_placeholder) {
372 _internal_vbox.remove (*_drag_placeholder);
373 _drag_placeholder = 0;
377 bool drag_motion (Glib::RefPtr<Gdk::DragContext> const &, int x, int y, guint)
379 if (_children.empty ()) {
387 /* decide where we currently are */
388 double const c = get_children_around_position (y, &before, &at, &after);
390 /* whether we're in the top or bottom half of the child that we're over */
391 bool top_half = (c - int (c)) < 0.5;
393 if (top_half && (before == _drag_child || at == _drag_child)) {
394 /* dropping here would have no effect, so remove the visual cue */
395 remove_drag_placeholder ();
399 if (!top_half && (at == _drag_child || after == _drag_child)) {
400 /* dropping here would have no effect, so remove the visual cue */
401 remove_drag_placeholder ();
405 /* the index that the placeholder should be put at */
406 int const n = int (c + 0.5);
408 if (_drag_placeholder == 0) {
409 _drag_placeholder = manage (new Gtk::Label (""));
410 _internal_vbox.pack_start (*_drag_placeholder, false, false);
411 _drag_placeholder->show ();
415 _internal_vbox.reorder_child (*_drag_placeholder, -1);
417 _internal_vbox.reorder_child (*_drag_placeholder, n);
423 void drag_leave (Glib::RefPtr<Gdk::DragContext> const &, guint)
425 remove_drag_placeholder ();
428 bool button_press (GdkEventButton* ev, T* child)
430 if (_expecting_unwanted_button_event == true && child == 0) {
431 _expecting_unwanted_button_event = false;
436 _expecting_unwanted_button_event = true;
439 if (ev->button == 1 || ev->button == 3) {
441 if (!selected (child)) {
443 if ((ev->state & Gdk::SHIFT_MASK) && !_selection.empty()) {
445 /* Shift-click; select all between the clicked child and any existing selections */
447 bool selecting = false;
449 for (typename std::list<T*>::const_iterator i = _children.begin(); i != _children.end(); ++i) {
451 bool const was_selected = selected (*i);
453 if (selecting && !was_selected) {
454 add_to_selection (*i);
457 if (!selecting && !done) {
460 } else if (*i == child) {
462 add_to_selection (child);
464 } else if (selecting) {
465 if (was_selected || *i == child) {
474 if ((ev->state & Gdk::CONTROL_MASK) == 0) {
479 add_to_selection (child);
484 SelectionChanged (); /* EMIT SIGNAL */
487 /* XXX THIS NEEDS GENERALIZING FOR OS X */
488 if (ev->button == 1 && (ev->state & Gdk::CONTROL_MASK)) {
489 if (child && selected (child)) {
490 remove_from_selection (child);
491 SelectionChanged (); /* EMIT SIGNAL */
498 return ButtonPress (ev, child); /* EMIT SIGNAL */
501 bool button_release (GdkEventButton* ev, T* child)
503 if (_expecting_unwanted_button_event == true && child == 0) {
504 _expecting_unwanted_button_event = false;
509 _expecting_unwanted_button_event = true;
512 return ButtonRelease (ev, child); /* EMIT SIGNAL */
515 /** Setup a child's GTK state correctly */
516 void setup_child_state (T* c)
521 c->set_visual_state (Gtk::STATE_ACTIVE);
522 } else if (selected (c)) {
523 c->set_visual_state (Gtk::STATE_SELECTED);
525 c->set_visual_state (Gtk::STATE_NORMAL);
529 void clear_selection ()
531 std::list<T*> old_selection = _selection;
533 for (typename std::list<T*>::iterator i = old_selection.begin(); i != old_selection.end(); ++i) {
534 setup_child_state (*i);
538 void add_to_selection (T* child)
540 _selection.push_back (child);
541 setup_child_state (child);
544 void remove_from_selection (T* child)
546 typename std::list<T*>::iterator x = find (_selection.begin(), _selection.end(), child);
547 if (x != _selection.end()) {
548 _selection.erase (x);
549 setup_child_state (*x);
553 T* child_from_widget (Gtk::Widget const * w) const
555 typename std::list<T*>::const_iterator i = _children.begin();
556 while (i != _children.end() && &(*i)->widget() != w) {
560 if (i == _children.end()) {
567 Gtk::VBox _internal_vbox;
568 std::list<Gtk::TargetEntry> _targets;
569 std::list<T*> _children;
570 std::list<T*> _selection;
572 Gtk::Window* _drag_icon;
573 bool _expecting_unwanted_button_event;
574 /** A blank label used as a placeholder to indicate where a dragged item would
575 * go if it were dropped now.
577 Gtk::Label* _drag_placeholder;
578 /** Our child being dragged, or 0 */
580 /** true if we can have an alpha-blended drag icon */
583 static DnDVBox* _drag_source;
588 DnDVBox<T>* DnDVBox<T>::_drag_source = 0;