libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GtkCRNSelectionBox.h
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, INSA-Lyon, ENS-Lyon
2  *
3  * This file is part of libgtkcrnmm.
4  *
5  * libgtkcrnmm is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libgtkcrnmm is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libgtkcrnmm. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: GtkCRNSelectionBox.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef GtkCRNSelectionBox_HEADER
23 #define GtkCRNSelectionBox_HEADER
24 
25 #include <libgtkcrnmm_config.h>
26 #include <gtkmm.h>
27 #include <vector>
28 #include <CRNMath/CRNMath.h>
29 #include <memory>
30 #include <CRNStringUTF8.h>
31 #include <CRNString.h>
32 #include <set>
33 
34 namespace GtkCRN
35 {
39  class SelectionBox: public Gtk::EventBox
40  {
41  public:
45  virtual ~SelectionBox() override;
46 
48  // Gtk::Box methods
50 
52  bool get_homogeneous() const { return box->get_homogeneous(); }
54  void set_homogeneous(bool homogeneous = true) { box->set_homogeneous(homogeneous); }
56  int get_spacing() const { return box->get_spacing(); }
58  void set_spacing(int spacing) { box->set_spacing(spacing); }
59 
61  void pack_start(Gtk::Widget& child, Gtk::PackOptions options = Gtk::PACK_EXPAND_WIDGET, guint padding = 0);
63  void pack_start(Gtk::Widget& child, bool expand, bool fill, guint padding = 0);
65  void pack_end(Gtk::Widget& child, Gtk::PackOptions options = Gtk::PACK_EXPAND_WIDGET, guint padding = 0);
67  void pack_end(Gtk::Widget& child, bool expand, bool fill, guint padding = 0);
68 
70  void clear();
71 
73  // Gtk::ScrolledWindow methods
75 
77  void set_policy(Gtk::PolicyType hscrollbar_policy, Gtk::PolicyType vscrollbar_policy) { sw.set_policy(hscrollbar_policy, vscrollbar_policy); }
78 
80  // Specific methods
82 
84  crn::Orientation get_orientation() const { return orientation; }
85 
87  void set_can_reorder(bool reorder = true) { can_reorder = reorder; }
89  bool get_can_reorder() const { return can_reorder; }
90 
92  std::vector<Gtk::Widget*> get_selection() const;
94  Gtk::Widget* get_last_selected() const;
95 
97  std::vector<Gtk::Widget*> get_content() const;
99  size_t get_nb_children() const { return content.size(); }
100 
102  void set_selection(size_t index);
104  void set_selected(size_t index, bool selected = true, bool silent = false);
106  bool is_selected(size_t index);
107 
109  void select_all();
111  void deselect_all();
113  void select_odd();
115  void select_even();
117  void invert_selection();
118 
120  void select_first();
122  void select_previous();
124  void select_next();
126  void select_last();
127 
129  sigc::signal<void, std::vector<size_t>, std::vector<size_t>> signal_moved() { return moved; }
130 
132  sigc::signal<void, int, crn::StringUTF8> signal_droppedin() { return droppedin; }
134  sigc::signal<void, Gtk::Widget*, const std::vector<Gtk::Widget*>> signal_selection_changed() { return selection_changed; }
135 
136  private:
137  crn::Orientation orientation;
138  bool can_reorder;
139  Gtk::Box *box;
140  Gtk::ScrolledWindow sw;
142  class Element;
144  class DropZone: public Gtk::DrawingArea
145  {
146  public:
147  DropZone(const sigc::slot7<void, Element*, const Glib::RefPtr<Gdk::DragContext>&, int, int, const Gtk::SelectionData&, guint, guint> &dropfun, SelectionBox *sb, Element *el);
148  private:
149  bool drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
150  void drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time);
151 #ifdef CRN_USING_GTKMM3
152  bool expose(const Cairo::RefPtr<Cairo::Context>&);
153 #else /* CRN_USING_GTKMM3 */
154  bool expose(GdkEventExpose *ev);
155 #endif /* CRN_USING_GTKMM3 */
156  bool in;
157 #ifdef CRN_USING_GTKMM3
158  Gdk::RGBA bg;
159 #endif /* CRN_USING_GTKMM3 */
160  };
161  DropZone first_drop_zone;
162  DropZone last_drop_zone;
164  static const crn::String reorderKey;
165  static const int reorderId;
166  static const int dropId;
169  class Element: public Gtk::Frame
170  {
171  public:
172  Element(Gtk::Widget &w, SelectionBox *sb);
173 
174  void Select() { tb.set_active(true); }
175  void Deselect() { tb.set_active(false); }
176 
177  Gtk::Widget& GetWidget() { return widget; }
178  const Gtk::Widget& GetWidget() const { return widget; }
179 
180  private:
181  void toggled(SelectionBox *const sb);
182  void drag_begin(const Glib::RefPtr<Gdk::DragContext>&) { Select(); }
183  void drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& seldata, guint info, guint time) { seldata.set_text(reorderKey.CStr()); }
184 
185  std::shared_ptr<Gtk::Box> box;
186  Gtk::Widget& widget;
187  Gtk::ToggleButton tb;
188  DropZone dz;
189 
190  };
191  friend class Element;
192 
193  std::vector<std::shared_ptr<Element>> content;
194  std::set<std::shared_ptr<Element>> selection;
195  std::weak_ptr<Element> last_selected;
198  bool keyevents(GdkEventKey *ev);
200  bool cursor_cross(GdkEventCrossing *ev);
201  bool shift_key;
202  bool control_key;
203  bool selecting;
206  void drop(Element *dropon, const Glib::RefPtr<Gdk::DragContext>& context, int, int, const Gtk::SelectionData& selection_data, guint, guint time);
208 #ifdef CRN_USING_GTKMM3
209  void dodrop(Element *dropon, const Gtk::SelectionData *selection_data, int info);
210 #else /* CRN_USING_GTKMM3 */
211  void dodrop(Element *dropon, const GtkSelectionData *selection_data, int info);
212 #endif /* CRN_USING_GTKMM3 */
213 
215  bool drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time);
217  bool autoscroll();
218 
219  Gtk::Widget *drag_item;
220  int vscrolldiv, hscrolldiv;
221  sigc::signal<void, std::vector<size_t>, std::vector<size_t>> moved;
222  sigc::signal<void, int, crn::StringUTF8> droppedin;
223  sigc::signal<void, Gtk::Widget*, const std::vector<Gtk::Widget*>> selection_changed;
224  };
225 }
226 
227 #endif
228 
229 
void set_selection(size_t index)
Sets the selection (one element)
void set_selected(size_t index, bool selected=true, bool silent=false)
Adds or remove an element from the selection.
void select_next()
If selection size is 0, the select the first element, if selection size is 1, then select the next it...
void set_policy(Gtk::PolicyType hscrollbar_policy, Gtk::PolicyType vscrollbar_policy)
Sets the scrolling policy.
Orientation
An enumeration of orientations.
Definition: CRNMath.h:152
int get_spacing() const
Gets the value set by set_spacing().
void select_even()
Selects odd elements (2nd, 4th…)
Gtk::Widget * get_last_selected() const
Returns the last widget that was selected.
sigc::signal< void, int, crn::StringUTF8 > signal_droppedin()
Signals when something was dropped from another application. Connect to void on_droppedin(int positio...
virtual ~SelectionBox() override
Destructor.
A UTF32 character string class.
Definition: CRNString.h:61
A Gtk::Box-like widget with multiple selection, reordering and drag'n drop features.
void select_odd()
Selects even elements (1st, 3rd…)
void pack_end(Gtk::Widget &child, Gtk::PackOptions options=Gtk::PACK_EXPAND_WIDGET, guint padding=0)
Right/bottom side insert a widget to a box.
void select_first()
If selection size is <= 1, then select the first item, else move last_selected to the first selected ...
void pack_start(Gtk::Widget &child, Gtk::PackOptions options=Gtk::PACK_EXPAND_WIDGET, guint padding=0)
Left/top side insert a widget to a box.
sigc::signal< void, std::vector< size_t >, std::vector< size_t > > signal_moved()
Signals when a widget was moved. Connect to void on_moved(moved_to, moved_from).
void clear()
Erase all elements.
void invert_selection()
Inverts the view selection.
std::vector< Gtk::Widget * > get_selection() const
Returns the list of selected widgets.
SelectionBox(crn::Orientation ori)
Constructor.
sigc::signal< void, Gtk::Widget *, const std::vector< Gtk::Widget * > > signal_selection_changed()
Signal when the selection has changed. Connect to void on_selection_changed(Gtk::Widget last_selected...
size_t get_nb_children() const
Returns the number of widgets inside the box.
bool is_selected(size_t index)
Is an element selected?
void select_last()
If selection size is <= 1, then select the last item, else move last_selected to the last selected el...
bool get_can_reorder() const
Returns if the elements can be reordered by the user.
void set_can_reorder(bool reorder=true)
Sets if the elements can be reordered by the user.
void select_previous()
If selection size is 0, the select the first element, if selection size is 1, then select the previou...
void select_all()
Selects all elements.
std::vector< Gtk::Widget * > get_content() const
Returns the list of widgets inside the box.
void set_spacing(int spacing)
Sets the Gtk::Box:spacing property of box, which is the number of pixels to place between children of...
void deselect_all()
Deselects all elements.
void set_homogeneous(bool homogeneous=true)
Sets the Gtk::Box:homogeneous property of box, controlling whether or not all children of box are giv...
bool get_homogeneous() const
Return value: true if the box is homogeneous.
crn::Orientation get_orientation() const
Returns the orientation of the box.