Planeshift
pawstree.h
Go to the documentation of this file.
1 /*
2  * pawstree.h - Author: Ondrej Hurt
3  *
4  * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PAWS_TREE_HEADER
21 #define PAWS_TREE_HEADER
22 
23 #include <csutil/parray.h>
24 #include <iutil/document.h>
25 #include "pawswidget.h"
26 #include "pawstextbox.h"
27 #include "pawsmanager.h"
28 #include "pawscrollbar.h"
29 
34 #define TREE_MOUSE_SCROLL_AMOUNT 20
35 
74 class pawsTreeNode;
75 
77 {
78 public:
79  virtual void NodeChanged()=0;
80  // this is called by tree nodes when they change
81  virtual void NewNode(pawsTreeNode* node)=0;
82  // OWNERSHIP of 'node' goes to callee
83  virtual void RemoveNode(pawsTreeNode* node)=0;
84  // OWNERSHIP of 'node' goes to caller
85  virtual ~pawsITreeStruct() {};
86 };
87 
89 // pawsTreeNode
91 
93 {
94  csString name,value;
95 };
96 
97 
98 // pawsTreeNode summarizes everything that is common to all types of tree
99 // control nodes
100 
101 class pawsTreeNode : public pawsWidget
102 {
103 public:
104  pawsTreeNode();
105  pawsTreeNode(const pawsTreeNode &origin);
106  virtual ~pawsTreeNode();
107 
108  // from pawsWidget:
109  virtual bool Load(iDocumentNode* node);
110 
111 
112  // Sets the tree that the node belongs to
113  void SetTree(pawsITreeStruct* tree);
114 
115  // Set first children. ONLY CALLED BY PAWSTREE'S COPY CONSTRUCTOR!!!
116  void SetFirstChild(pawsTreeNode* child);
117 
118  // Attributes - you can set and retrieve named string values for each node.
119  csString GetAttr(const csString &name);
120  void SetAttr(const csString &name, const csString &value);
121 
122  void SetParent(pawsTreeNode* parent);
123  void SetPrevSibling(pawsTreeNode* node);
124  void SetNextSibling(pawsTreeNode* node);
125 
126  pawsTreeNode* GetParent();
127  pawsTreeNode* GetFirstChild();
128  pawsTreeNode* FindLastChild();
129  pawsTreeNode* GetPrevSibling();
130  pawsTreeNode* GetNextSibling();
131  pawsTreeNode* FindLastSibling();
132 
133  pawsTreeNode* FindNodeAbove();
134  // Finds node that is positioned right above our node
135  pawsTreeNode* FindNodeBelow();
136  // Finds node that is positioned right below our node
137  pawsTreeNode* FindLowestSubtreeNode();
138  // Finds lowest node in subtree where our node is root
139 
140  virtual void InsertChild(pawsTreeNode* node, pawsTreeNode* nextSibling = NULL);
141  // inserts 'node' before 'nextSibling' (NULL=append)
142  // OWNERSHIP of 'node' goes to pawsTreeNode
143  virtual void MoveChild(pawsTreeNode* node, pawsTreeNode* nextSibling = NULL);
144  // moves 'node' before 'nextSibling' (NULL=append)
145  virtual void RemoveChild(pawsTreeNode* node);
146  // removes 'node' from children (without destructing it)
147  // OWNERSHIP of 'node' goes to CALLER
148  virtual void DeleteChild(pawsTreeNode* node);
149  // deletes 'node' from children and destructs it (so the pointer is no longer valid)
150  // children of 'node' are deleted too
151  virtual void Clear();
152  // DeleteChild() for all children
153 
154  pawsTreeNode* FindChildByName(const csString &name, bool indirectToo);
155  // Finds child nu its name
156  pawsTreeNode* FindNodeByPath(const csString &path);
157  // Finds node using slash-separated list of names of the node and its parents.
158  // e.g. "options/controls/keyboard"
159 
160  virtual void SetCollapsable(bool collapsable);
161  // sets if node can be collapsed
162  virtual bool IsCollapsable()
163  {
164  return collapsable;
165  }
166  virtual void Expand();
167  virtual void ExpandAll();
168  virtual void Collapse();
169  virtual void CollapseAll();
170  virtual bool IsCollapsed();
171  virtual bool BuriedInRuins();
172  //determines if this node is hidden because of collapsed parents (including indirect parents)
173 
174 
175 // virtual pawsTreeNode * Copy() = 0;
176 
177  int GetRowNum();
178  // Calculates position of our node in tree (positions are starting at 0)
179 
180 protected:
181  void SetChildrenVisibAfterCollapseChange(bool expanded);
182  // calls either Show() or Hide() method for all children
183 
184  pawsITreeStruct* tree; //tree our node belongs to
185  pawsTreeNode* parent; //NULL in root node
187  pawsTreeNode* prevSibling, * nextSibling; //NULL in first/last sibling
188 
189  csArray<TreeNodeAttribute> attrList; //attributes associated with node
190  bool collapsable; //can node by collapsed ?
191  bool collapsed; //is node currently collapsed ?
192 };
193 
194 
196 // pawsTreeStruct
198 
199 
200 
201 
202 // pawsTreeStruct maintains tree structure
203 // it is NOT able not draw itself on screen etc.
204 
206 {
207 public:
208  pawsTreeStruct();
209  virtual ~pawsTreeStruct();
210 
211  // from pawsWidget:
212  virtual bool Load(iDocumentNode* node);
213 
214  // from pawsITreeStruct:
215  virtual void NodeChanged();
216  virtual void NewNode(pawsTreeNode* node)=0;
217  virtual void RemoveNode(pawsTreeNode* node)=0;
218 
219  pawsTreeNode* GetRoot();
220  virtual void SetRoot(pawsTreeNode* root);
221  //OWNERSHIP of 'root' goes to pawsTreeStruct
222 
223  // Manipulation with nodes using pointers:
224  virtual void InsertChild(pawsTreeNode* parent, pawsTreeNode* node, pawsTreeNode* nextSibling = NULL);
225  virtual void MoveChild(pawsTreeNode* node, pawsTreeNode* nextSibling = NULL);
226  virtual void RemoveChild(pawsTreeNode* node);
227  virtual void DeleteChild(pawsTreeNode* node);
228  virtual void Clear();
229 
230  // Manipulation with nodes using their names:
231  virtual void InsertChild(const csString &parent, pawsTreeNode* node, const csString &nextSibling);
232  virtual void InsertChild(const csString &parent, pawsTreeNode* node);
233  virtual void MoveChild(const csString &name, const csString &nextSibling);
234  virtual void DeleteChild(const csString &name);
235 
236 
237  pawsTreeNode* FindNodeByName(const csString &name);
238  // searches for first node of name 'name' in whole tree, NULL means not found
239 
240  pawsTreeNode* FindNodeAt(pawsTreeNode* parent, int x, int y);
241  // returns the node that the [x, y] coordinates point to
242  // NULL means no such node
244  {
245  return version;
246  }
247 
248 protected:
250  int version; // This number is increased every time the NodeChanged() method is called.
251  // Its purpose is to detect changes made in tree.
252 };
253 
254 
256 // pawsTree
258 
259 class pawsITreeLayout;
260 class pawsITreeDecorator;
261 
262 
263 // pawsTree is customisable tree widget
264 // You will probably want to use pawsSimpleTree instead because it's easier.
265 
266 class pawsTree : public pawsWidget, public pawsTreeStruct
267 {
268 public:
269  pawsTree();
270  pawsTree(const pawsTree &origin);
271  virtual ~pawsTree();
272 
273  // from pawsWidget:
274  virtual bool Setup(iDocumentNode* node);
275  virtual bool PostSetup();
276 
277  virtual void Draw();
278  virtual bool OnMouseDown(int button, int modifiers, int x, int y);
279  virtual bool OnKeyDown(utf32_char keyCode, utf32_char keyChar, int modifiers);
280  virtual bool OnScroll(int scrollDirection, pawsScrollBar* widget);
281  virtual bool LoadChildren(iDocumentNode* node);
282 
283  // from pawsITreeStruct:
284  virtual void NewNode(pawsTreeNode* node);
285  virtual void RemoveNode(pawsTreeNode* node);
286 
287  // from pawsTreeStruct:
288  virtual void SetRoot(pawsTreeNode* root);
289 
290 
291  void SetTreeLayout(pawsITreeLayout* layout);
292  // OWNERSHIP of 'layout' goes to pawsTree
293  void SetTreeDecorator(pawsITreeDecorator* decor);
294  // decor==NULL disables decorations
295  // OWNERSHIP of 'decor' goes to pawsTree
296 
297  void SetScrollBars(bool horiz, bool vert);
298  // sets visibility of scrollbars
299 
300  // Multiselect is not implemented directly, use pawsICheckTreeNode tree nodes instead
301  virtual void Select(pawsTreeNode*);
302  virtual void Deselect();
303  pawsTreeNode* GetSelected();
304 
305  void SetNotify(pawsWidget* _notificationTarget);
306  // sets widget that is target of OnSelected() events
307 
308 protected:
309  void SetScrollBarMax();
310  void cloneTreeNodes(const pawsTree &origin);
311 
314 
315  pawsTreeNode* selected; // selected node
316  pawsWidget* notificationTarget; // target of tree events
317  pawsScrollBar* horizScrollBar, * vertScrollBar;
318 };
319 
320 
322 // tree layout
324 
325 // pawsITreeLayout classes calculate and set position of tree nodes
327 {
328 public:
329  virtual void SetLayout() = 0;
330  // Sets position of 'node' and all nodes beneath
331  virtual void SetHorizScroll(int horizScroll) = 0;
332  virtual void SetVertScroll(int vertScroll) = 0;
333  virtual void GetTreeSize(int &width, int &height) = 0;
334  virtual ~pawsITreeLayout() {};
335 };
336 
338 {
339 public:
340  pawsStdTreeLayout(pawsTree* tree, int rowSpacing, int levelSpacing);
341  virtual ~pawsStdTreeLayout();
342  // rowSpacing = space between rows
343  // levelSpacing = difference in X coordinate of two levels
344 
345  // from pawsITreeLayout:
346  virtual void SetLayout();
347  virtual void SetHorizScroll(int horizScroll);
348  virtual void SetVertScroll(int vertScroll);
349  virtual void GetTreeSize(int &width, int &height);
350 
351 protected:
352  void SetSubtreeLayout(pawsTreeNode* subtreeRoot, int x, int y, int &maxX, int &maxY);
353  // Moves 'subtreeRoot' to [x , y] and all children accordingly
354  // 'maxX' and 'maxY' are the greates coordinates where the subtree reaches on screen
355 
357  int rowSpacing, levelSpacing;
358  int horizScroll, vertScroll;
359 
360  int width, height; // size of tree calculated by object during last SetLayout()
361 
362  int lastVersion; // value of pawsTreeStruct::version at the time the SetLayout() method was last called
363 };
364 
365 
367 // tree decorator
369 
370 // pawsITreeDecorator classes are used to paint all graphics, except graphics of nodes
371 // e.g. lines connecting nodes.
372 // It also receives user input related to graphics painted by itself.
373 
375 {
376 public:
377  virtual ~pawsITreeDecorator() {};
378  virtual void Decorate() = 0;
379  virtual bool OnMouseDown(int button, int modifiers, int x, int y) = 0;
380 };
381 
383 {
384 public:
385  pawsStdTreeDecorator(pawsTree* tree, iGraphics2D* g2d, int selectedColor, int lineColor, int collSpacing);
386  virtual ~pawsStdTreeDecorator();
387 
388  // from pawsITreeDecorator;
389  virtual void Decorate();
390  virtual bool OnMouseDown(int button, int modifiers, int x, int y);
391 
392 protected:
393  void DecorateSubtree(pawsTreeNode* node);
394  // paints graphics around nodes of subtree with root 'subtreeRoot'
395  pawsTreeNode* FindCollapsingNodeInSubtree(pawsTreeNode* subtreeRoot, int x, int y);
396  // finds collapse/expand sign that is being pointed to by [x, y] in subtree with root 'subtreeRoot'
397  void GetCollapseSignFrame(pawsTreeNode* node, csRect &rect);
398  // calculates position of collapse/expand sign of 'node' on screen
399 
401  iGraphics2D* g2d;
402  int selectedColor; // background color of selected node
403  int lineColor; // color of lines
404  int collSpacing; // distance between node and expand/collapse sign
405 
406  csRef<iPawsImage> collImage;
407  csRef<iPawsImage> expandImage;
408 };
409 
410 
411 
413 // miscellaneous tree node types
415 
416 // class pawsWidgetTreeNode
418 {
419 public:
421  pawsWidgetTreeNode(const pawsWidgetTreeNode &origin);
422  //from pawsWidget:
423  virtual bool Load(iDocumentNode* node);
424 
425  void SetWidget(pawsWidget* widget);
426  // Assigns the widget that makes main body of the node.
427  // OWNERSHIP of 'widget' goes to pawsWidgetTreeNode
428 protected:
430 };
431 
432 // iCheckTreeNode is common interface for tree nodes that have some kind of checkbox inside
433 // (something that is represented by boolean value)
434 
436 {
437 public:
438  virtual bool GetCheck() = 0;
439  virtual void SetCheck(bool ch) = 0;
440  virtual ~pawsICheckTreeNode() {};
441 };
442 
443 // pawsCheckTreeNode is standard implementation of iCheckTreeNode
444 // The node is made of a checkbox and arbitrary additional widget following the checkbox
445 
447 {
448 public:
450  pawsCheckTreeNode(const pawsCheckTreeNode &origin);
451  virtual ~pawsCheckTreeNode() {};
452 
453  //from pawsICheckTreeNode:
454  virtual bool GetCheck();
455  virtual void SetCheck(bool ch);
456 
457 
458  virtual void SetWidget(pawsWidget* widget);
459  // set widget that follows the checkbox
460  // OWNERSHIP of widget goes to pawsCheckTreeNode
461 
462 protected:
463 // pawsCheckbox * checkbox;
465 };
466 
467 
468 
470 {
471 public:
472  pawsSeqTreeNode_widget(pawsWidget* w, int width);
474  int width;
475 };
476 
477 // pawsSeqTreeNode node is sequence of arbitrary widgets that are drawn from left to right.
478 
480 {
481 public:
483  {
484  factory = "pawsSeqTreeNode";
485  }
486  pawsSeqTreeNode(const pawsSeqTreeNode &origin);
487  //from pawsWidget:
488  virtual bool Load(iDocumentNode* node);
489 
490  virtual void AddSeqWidget(pawsWidget* widget, int width);
491  // adds 'widget' and reserves 'width' space for it (this does not have to be equal to widget width)
492  // OWNERSHIP of widget goes to pawsSeqTreeNode
493  virtual void AddSeqWidget(pawsWidget* widget);
494  // calls AddWidget() with width of widget
495  // OWNERSHIP of widget goes to pawsSeqTreeNode
496  virtual pawsWidget* GetSeqWidget(int index);
497 
498  virtual void Draw();
499 
500 protected:
501  csList<pawsSeqTreeNode_widget> widgets;
502 };
503 
505 // pawsSimpleTree
507 
508 // pawsSimpleTreeNode is tree node which can comprise of checkbox, image and text label.
509 // (checkbox is drawn first, image second, label third)
510 // All parts are optional.
511 
512 
514 
516 {
517 public:
519  pawsSimpleTreeNode(const pawsSimpleTreeNode &origin);
520  virtual ~pawsSimpleTreeNode();
521 
522  // from pawsWidget:
523  virtual bool Load(iDocumentNode* node);
524 
525 
526  virtual void Set(int mode, bool checked, const csString &imageName, const csString &label);
527  // This method sets content of the node.
528  // 'mode' is something like "showCheckBox | showLabel"
529  // Node must already be in a tree before you can call this method.
530 
531 protected:
534 
535 };
536 
537 
538 
539 // class pawsSimpleTree is tree with nodes of type pawsSimpleTreeNode
540 // Its purpose is to provide simple tree control that will fit for most situations
541 
542 class pawsSimpleTree : public pawsTree
543 {
544 public:
545  pawsSimpleTree();
546  pawsSimpleTree(const pawsSimpleTree &origin);
547  // from pawsWidget:
548  virtual bool Setup(iDocumentNode* node);
549 
550  // Those methods set the default color used by nodes of the tree:
551  int GetDefaultColor();
552  void SetDefaultColor(int);
553 
554  virtual void InsertChildL(const csString &parent, const csString &name, const csString &label, const csString &nextSibling);
555  // creates and inserts node that contains label
556  virtual void InsertChildI(const csString &parent, const csString &name, const csString &image, const csString &nextSibling);
557  // creates and inserts node that contains image
558  virtual void InsertChildIL(const csString &parent, const csString &name, const csString &image, const csString &label, const csString &nextSibling);
559  // creates and inserts node that contains label and image
560 protected:
562 };
563 
564 
566 // class factories
568 
569 
576 
579 #endif
580 
pawsWidget * widget
Definition: pawstree.h:473
pawsWidget * notificationTarget
Definition: pawstree.h:316
pawsScrollBar * vertScrollBar
Definition: pawstree.h:317
The main base widget that all other widgets should inherit from.
Definition: pawswidget.h:116
virtual ~pawsITreeLayout()
Definition: pawstree.h:334
pawsITreeDecorator * decor
Definition: pawstree.h:313
pawsTreeNode * firstChild
Definition: pawstree.h:186
virtual bool IsCollapsable()
Definition: pawstree.h:162
pawsWidget * widget
Definition: pawstree.h:464
pawsITreeLayout * layout
Definition: pawstree.h:312
csArray< TreeNodeAttribute > attrList
Definition: pawstree.h:189
CREATE_PAWS_FACTORY(pawsTree)
pawsTreeNode * prevSibling
Definition: pawstree.h:187
csList< pawsSeqTreeNode_widget > widgets
Definition: pawstree.h:501
pawsTree * tree
Definition: pawstree.h:356
virtual ~pawsITreeStruct()
Definition: pawstree.h:85
virtual void RemoveNode(pawsTreeNode *node)=0
pawsWidget * image
Definition: pawstree.h:533
pawsWidget * widget
Definition: pawstree.h:429
bool collapsable
Definition: pawstree.h:190
A simple scroll bar widget.
Definition: pawscrollbar.h:64
virtual ~pawsCheckTreeNode()
Definition: pawstree.h:451
csString value
Definition: pawstree.h:94
pawsTreeNode * root
Definition: pawstree.h:249
virtual void NewNode(pawsTreeNode *node)=0
virtual ~pawsICheckTreeNode()
Definition: pawstree.h:440
virtual void NodeChanged()=0
csRef< iPawsImage > expandImage
Definition: pawstree.h:407
int defaultColor
Definition: pawstree.h:561
pawsTextBox * textBox
Definition: pawstree.h:532
pawsITreeStruct * tree
Definition: pawstree.h:184
bool collapsed
Definition: pawstree.h:191
virtual ~pawsITreeDecorator()
Definition: pawstree.h:377
pawsTree * tree
Definition: pawstree.h:400
int GetVersion()
Definition: pawstree.h:243
A basic text box widget.
Definition: pawstextbox.h:42
csRef< iPawsImage > collImage
Definition: pawstree.h:406
pawsTreeNode * parent
Definition: pawstree.h:185
pawsTreeNode * selected
Definition: pawstree.h:315
iGraphics2D * g2d
Definition: pawstree.h:401