MySQL Workbench  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
treenodeview.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; version 2 of the
7  * License.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #ifndef _MFORMS_TREENODEVIEW_H_
20 #define _MFORMS_TREENODEVIEW_H_
21 
22 #include <mforms/view.h>
23 #include <boost/cstdint.hpp>
24 
29 namespace mforms {
30  class TreeNodeView;
31  class ContextMenu;
32 
35  {
46  };
47 
50  {
52  TreeNoColumns = 1 << 3,
53  TreeAllowReorderRows = 1 << 4,
59  TreeShowRowLines = 1 << 6,
60  TreeNoBorder = 1 << 7,
61  TreeSidebar = 1 << 8,
62  TreeNoHeader = 1 << 9,
64  TreeFlatList = 1 << 10,
65  TreeAltRowColors = 1 << 11,
66  TreeSizeSmall = 1 << 12,
67  TreeIndexOnTag = 1 << 13,
68 
69  TreeCanBeDragSource = 1 << 14,
70  };
71 
72 #ifndef SWIG
74  {
75  return (TreeOptions) ((int) a | (int) b);
76  }
77 #endif
78 
80  {
81  TreeSelectSingle, // 0 or 1 item selected
82  TreeSelectMultiple // 0+ items selection
83  };
84 
86 
87  // This struct represents the data comprising a node
88  // including a collection of child nodes.
90  {
91  public:
93  TreeNodeSkeleton(const std::string& caption, const std::string& icon, const std::string& tag);
94  std::string caption;
95  std::string icon;
96  std::string tag;
97  std::vector<TreeNodeSkeleton> children;
98  };
99 
100  // This struct represents a collection of nodes sharing tag, icon and structure
102  {
103  public:
105  TreeNodeCollectionSkeleton(const std::string& icon);
106  std::string icon;
107  std::vector<TreeNodeSkeleton> children;
108  std::vector<std::string> captions;
109  };
110 
112  protected:
114  public:
115  TreeNodeData() : _refcount(0) {}
116  virtual ~TreeNodeData() {}
117  void retain() { _refcount++; }
118  void release() { _refcount--; if (_refcount == 0) delete this; }
119  };
120 
121 
122  class TreeNode;
124  {
125  private:
127 
128  public:
129 #ifndef SWIG
130  TreeNodeRef(TreeNode *anode);
131  TreeNode *ptr() { return node; }
132 #endif
133  public:
134  TreeNodeRef() : node(0) {}
135  TreeNodeRef(const TreeNodeRef &other);
136  ~TreeNodeRef();
137 #ifndef SWIG
138  TreeNodeRef &operator= (const TreeNodeRef &other);
139 #endif
140  TreeNode *operator->() const;
141  TreeNode *operator->();
142 
143  operator bool () const { return node !=0; }
144  bool operator == (const TreeNodeRef &other) const;
145  bool operator != (const TreeNodeRef &other) const;
146 
147  bool is_valid();
148  };
149 
151  {
152  friend struct TreeNodeRef;
153 
154  protected:
155  virtual void release() = 0;
156  virtual void retain() = 0;
157  public:
158  virtual ~TreeNode() {};
159 
160  virtual bool equals(const TreeNode &other) = 0;
161 #ifndef SWIG
162  virtual bool is_valid() const = 0;
163 #endif
164  virtual void set_icon_path(int column, const std::string &icon) = 0;
165 
166  virtual void set_string(int column, const std::string &value) = 0;
167  virtual void set_int(int column, int value) = 0;
168  virtual void set_long(int column, boost::int64_t value) = 0;
169  virtual void set_bool(int column, bool value) = 0;
170  virtual void set_float(int column, double value) = 0;
171 
172  virtual void set_attributes(int column, const TreeNodeTextAttributes &attrs) = 0;
173 
174  virtual std::string get_string(int column) const = 0;
175  virtual int get_int(int column) const = 0;
176  virtual boost::int64_t get_long(int column) const = 0;
177  virtual bool get_bool(int column) const = 0;
178  virtual double get_float(int column) const = 0;
179 
180  virtual int count() const = 0;
181  virtual TreeNodeRef add_child() { return insert_child(-1); }
182  virtual TreeNodeRef insert_child(int index) = 0;
183  virtual void remove_from_parent() = 0;
184  virtual void remove_children(); // default impl provided, subclasses may override to provide faster impl
185  virtual TreeNodeRef get_child(int index) const = 0;
186  virtual TreeNodeRef get_parent() const = 0;
187  virtual TreeNodeRef find_child_with_tag(const std::string &tag); // this will search the subnodes sequentially
188 
189  virtual std::vector<mforms::TreeNodeRef> add_node_collection(const TreeNodeCollectionSkeleton &nodes, int position = -1) = 0;
190 
191  virtual void expand() = 0;
192  virtual void collapse() = 0;
193  virtual bool is_expanded() = 0;
194 
195  // Primitive check. Use TreeNodeView::can_expand() instead for potentially more sophisticated checks.
196  virtual bool can_expand() { return count() > 0; };
197 
198  virtual void set_tag(const std::string &tag) = 0;
199  virtual std::string get_tag() const = 0;
200 
201  virtual void set_data(TreeNodeData *data) = 0;
202  virtual TreeNodeData *get_data() const = 0;
203  };
204 
205 
206 #ifndef DOXYGEN_SHOULD_SKIP_THIS
207 #ifndef SWIG
208  struct TreeNodeViewImplPtrs
209  {
210  bool (*create)(TreeNodeView *self, TreeOptions options);
211 
212 #if defined(_WIN32)
213  int (*add_column)(TreeNodeView *self, TreeColumnType type, const std::string &name, int initial_width, bool editable);
214 #else
215  int (*add_column)(TreeNodeView *self, TreeColumnType type, const std::string &name, int initial_width, bool editable, bool attributed);
216 #endif
217  void (*end_columns)(TreeNodeView *self);
218 
219  void (*clear)(TreeNodeView *self);
220 
221  TreeNodeRef (*root_node)(TreeNodeView *self);
222 
223  void (*set_row_height)(TreeNodeView *self, int height);
224 
225  void (*set_allow_sorting)(TreeNodeView *self, bool);
226 
227  void (*freeze_refresh)(TreeNodeView *self, bool);
228 
229  void (*set_selection_mode)(TreeNodeView *self, TreeSelectionMode mode);
230  TreeSelectionMode (*get_selection_mode)(TreeNodeView *self);
231 
232  std::list<TreeNodeRef> (*get_selection)(TreeNodeView *self);
233  TreeNodeRef (*get_selected_node)(TreeNodeView *self);
234 
235  void (*clear_selection)(TreeNodeView *self);
236  void (*set_selected)(TreeNodeView *self, TreeNodeRef node, bool state);
237 
238  int (*row_for_node)(TreeNodeView *self, TreeNodeRef node);
239  TreeNodeRef (*node_at_row)(TreeNodeView *self, int row);
240  TreeNodeRef (*node_with_tag)(TreeNodeView *self, const std::string &tag);
241 
242  void (*set_column_visible)(TreeNodeView *self, int column, bool flag);
243  bool (*get_column_visible)(TreeNodeView *self, int column);
244 
245  void (*set_column_width)(TreeNodeView *self, int column, int width); // XXX linux
246  int (*get_column_width)(TreeNodeView *self, int column); // XXX linux
247  };
248 #endif
249 #endif
250 
251  class ContextMenu;
252 
259  {
260  public:
261  TreeNodeView(TreeOptions options);
262  ~TreeNodeView();
263 
271  int add_column(TreeColumnType type, const std::string &name, int initial_width, bool editable = false, bool attributed = false);
273  void end_columns();
274 
275  int get_column_count() const { return (int)_column_types.size(); }
276  TreeColumnType get_column_type(int column);
277 
279  void clear();
280  TreeNodeRef root_node();
281 
282  TreeNodeRef add_node();
283 
285  void set_selection_mode(TreeSelectionMode mode);
287  TreeSelectionMode get_selection_mode();
288 
290  std::list<TreeNodeRef> get_selection();
292  TreeNodeRef get_selected_node();
293  int get_selected_row();
294  void clear_selection();
295 
297  void select_node(TreeNodeRef node);
298  void set_node_selected(TreeNodeRef node, bool flag);
299 
300  int row_for_node(TreeNodeRef node);
301  TreeNodeRef node_at_row(int row);
302 
304  TreeNodeRef node_with_tag(const std::string &tag);
305 
307  void set_row_height(int height);
308 
318  void set_allow_sorting(bool flag);
319 
321  void freeze_refresh();
322 
324  void thaw_refresh();
325 
326  void set_column_visible(int column, bool flag);
327 
328  bool get_column_visible(int column);
329 
330 #ifndef SWIG
331 
335  boost::signals2::signal<void ()>* signal_changed() {return &_signal_changed;}
342  boost::signals2::signal<void (TreeNodeRef, int)>* signal_node_activated() {return &_signal_activated;}
343 
344  boost::signals2::signal<void (TreeNodeRef, bool)>* signal_expand_toggle() {return &_signal_expand_toggle;}
345 
346  boost::signals2::signal<void (int)>* signal_column_resized() {return &_signal_column_resized;}
347 
353  void set_cell_edit_handler(const boost::function<void (TreeNodeRef, int, std::string)> &handler);
354 #endif
355 
359  void set_context_menu(ContextMenu *menu);
360 
362  ContextMenu *get_context_menu() { return _context_menu; }
363 
365  int get_column_width(int column);
366 
368  void set_column_width(int column, int width);
369 
370  public:
371  // backwards compatibility with TreeView
372 
373  int count() { return root_node()->count(); }
374 
375 #ifndef DOXYGEN_SHOULD_SKIP_THIS
376 #ifndef SWIG
377  public:
378  // Following methods are for internal use.
379  virtual void changed();
380  virtual void node_activated(TreeNodeRef row, int column);
381  virtual bool can_expand(TreeNodeRef row);
382 
383  // to be called BEFORE a node is expanded or AFTER its collapsed
384  virtual void expand_toggle(TreeNodeRef row, bool expanded);
385 
386  // for use when sorting NumericWithUnitColumns in platform code
387  static double parse_string_with_unit(const char *s);
388 
389  // To be called when user edits a cell. If this returns true, the cell value should be
390  // updated by the caller, otherwise it should be left unchanged.
391  virtual bool cell_edited(TreeNodeRef row, int column, const std::string &value);
392 
393  // Drag/drop support. The treeview has a different handling as dragging is initiated by the platform
394  // and we call back to get data and formats (pull request). The general handling, in opposition,
395  // is using a push request, initiating the drag in the backend.
396 
397  // Return format, data and other details for a drag operation that is about to start.
398  // Called only if this tree can be a drag source at all (see create flags).
399  // If the result of this call is false then no native drag operation is started.
400  virtual bool get_drag_data(DragDetails &details, void **data, std::string &format);
401 
402  // Called only if get_drag_data returned true (and the tree can be a drag source).
403  // The given operation tells what happened.
404  virtual void drag_finished(DragOperation operation);
405 
406  virtual void column_resized(int column);
407 
408 #endif
409 #endif
410 
411  private:
412  TreeNodeViewImplPtrs *_treeview_impl;
413  boost::signals2::signal<void ()> _signal_changed;
414  boost::signals2::signal<void (TreeNodeRef, int)> _signal_activated;
415  boost::signals2::signal<void (TreeNodeRef, bool)> _signal_expand_toggle;
416  boost::function<void (TreeNodeRef, int, std::string)> _cell_edited;
417  boost::signals2::signal<void (int)> _signal_column_resized;
419  std::vector<TreeColumnType> _column_types;
423  };
424 }
425 
426 #endif /* _TREENODEVIEW_H_ */
TreeNodeViewImplPtrs * _treeview_impl
Definition: treenodeview.h:412
TreeSelectionMode
Definition: treenodeview.h:79
for backwards compatibility
Definition: treenodeview.h:63
Don't show a column header. Only meaningful if columns are used.
Definition: treenodeview.h:62
show row separator lines
Definition: treenodeview.h:59
LineMarkup operator|(LineMarkup a, LineMarkup b)
Definition: code_editor.h:73
int get_column_count() const
Definition: treenodeview.h:275
no child items expected
Definition: treenodeview.h:64
64bit numeric field, with text entry editor
Definition: treenodeview.h:39
show column separator lines
Definition: treenodeview.h:58
std::vector< std::string > captions
Definition: treenodeview.h:108
string type, representing numbers with a unit suffix (like KB, MB, ms etc) TODO windows, linux
Definition: treenodeview.h:44
TreeNode * ptr()
Definition: treenodeview.h:131
boolean field, with checkbox
Definition: treenodeview.h:40
icon field, value is the icon path
Definition: treenodeview.h:42
DragOperation
Definition: view.h:78
virtual TreeNodeRef add_child()
Definition: treenodeview.h:181
boost::signals2::signal< void(int)> * signal_column_resized()
Definition: treenodeview.h:346
ContextMenu * get_context_menu()
Returns the context menu object attached to the treeview.
Definition: treenodeview.h:362
virtual bool can_expand()
Definition: treenodeview.h:196
std::vector< TreeColumnType > _column_types
Definition: treenodeview.h:419
#define MFORMS_EXPORT
Definition: base.h:33
numeric field, with text entry editor
Definition: treenodeview.h:38
a double precision floating point number
Definition: treenodeview.h:45
TreeColumnType
Determines what type a column should have (mainly describing the column editor).
Definition: treenodeview.h:34
Switch off the border around the control. Default is to show the border.
Definition: treenodeview.h:60
std::vector< TreeNodeSkeleton > children
Definition: treenodeview.h:107
boost::signals2::signal< void()> _signal_changed
Definition: treenodeview.h:413
boost::signals2::signal< void(TreeNodeRef, int)> _signal_activated
Definition: treenodeview.h:414
Control to show nodes in multiple columns in the form of a tree.
Definition: treenodeview.h:258
Allows row reordering, sets TreeCanBeDragSource implicitely.
Definition: treenodeview.h:57
std::vector< TreeNodeSkeleton > children
Definition: treenodeview.h:97
checkbox field (same as CheckColumnType) which also accepts -1 as mixed state
Definition: treenodeview.h:41
ContextMenu * _context_menu
Definition: treenodeview.h:418
boost::function< void(TreeNodeRef, int, std::string)> _cell_edited
Definition: treenodeview.h:416
keep a node index on the tags (use with node_with_tag)
Definition: treenodeview.h:67
virtual ~TreeNode()
Definition: treenodeview.h:158
A menu that can be attached to other controls.
Definition: menubar.h:177
simple string, with text entry editor
Definition: treenodeview.h:36
boost::signals2::signal< void(TreeNodeRef, bool)> _signal_expand_toggle
Definition: treenodeview.h:415
On non-Windows platforms columns are always on, so switch them on on Windows too by default and use t...
Definition: treenodeview.h:52
boost::signals2::signal< void(TreeNodeRef, int)> * signal_node_activated()
Signal emitted when the user double-clicks a cell.
Definition: treenodeview.h:342
same as StringColumnType, but truncated at the left end if it doesn't fit
Definition: treenodeview.h:37
boost::signals2::signal< void()> * signal_changed()
Signal emitted when the selected row is changed.
Definition: treenodeview.h:335
TreeOptions
Options used to customize what to show in the tree.
Definition: treenodeview.h:49
boost::signals2::signal< void(int)> _signal_column_resized
Definition: treenodeview.h:417
allow the tree to be a drag source, data used depends on actual tree
Definition: treenodeview.h:69
boost::signals2::signal< void(TreeNodeRef, bool)> * signal_expand_toggle()
Definition: treenodeview.h:344
enable alternating row colors
Definition: treenodeview.h:65
sidebar style treeview
Definition: treenodeview.h:61
MySQL Workbench 6.1 documentation