cwidget  0.5.18
tree.h
1 // tree.h (this is -*-c++-*-)
2 //
3 // Copyright 1999-2001, 2004-2007 Daniel Burrows
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
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 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; see the file COPYING. If not, write to
17 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
19 //
20 // A simple tree widget.
21 
22 #ifndef TREE_H
23 #define TREE_H
24 
25 #include "treeitem.h"
26 #include "widget.h"
27 
28 #include <cwidget/generic/util/eassert.h>
29 
30 namespace cwidget
31 {
32  namespace config
33  {
34  class keybindings;
35  }
36 
37  namespace widgets
38  {
39  // A predicate on treeitems:
41  {
42  public:
43  virtual bool operator()(const treeitem &item)=0;
44  virtual ~tree_search_func() {}
45  };
46 
48  {
49  std::wstring s;
50  public:
51  tree_search_string(const std::wstring &_s):s(_s) {}
52 
53  virtual bool operator()(const treeitem &item);
54  };
55 
56  class tree : public widget
57  {
58  treeitem *root;
59  treeiterator begin, end;
60 
61  treeiterator top;
62  treeiterator selected;
63  // The top item on the current page and the currently selected item.
64  // NOTE: it's implicitly assumed in many places in the code that the
65  // currently selected item is visible (ie, on the screen).
66 
67  bool hierarchical;
68  // If not true, display the tree as a series of "flat thingies".
69  // Must be seen to be described :)
70 
71  // This structure is used to easily retrace our steps in flat-mode.
72  // (it could probably be done without this, but this makes it MUCH simpler)
73  // Note that we don't even bother with an STL list here; it's
74  // just not worth it.
75  struct flat_frame
76  {
77  treeiterator begin, end, top, selected;
78 
79  flat_frame *next;
80  flat_frame(treeiterator _begin,
81  treeiterator _end,
82  treeiterator _top,
83  treeiterator _selected,
84  flat_frame *_next)
85  :begin(_begin), end(_end), top(_top), selected(_selected), next(_next) {}
86  };
87  flat_frame *prev_level;
88 
89  int line_of(treeiterator item);
90  bool item_visible(treeiterator item);
91 
92  void do_shown();
93  protected:
94  void sync_bounds();
95  // This is an awful hack; I've been thinking about an alternate design of
96  // the tree code for a while, and this just confirms it. Yuck! :)
97  // It'll be the first thing to be removed in the next version..
98  // -- well, it wasn't.
99 
100  virtual bool handle_key(const config::key &k);
101 
102  protected:
103  tree();
104  tree(treeitem *_root, bool showroot);
105 
106  public:
107  static util::ref_ptr<tree>
108  create()
109  {
110  util::ref_ptr<tree> rval(new tree);
111  rval->decref();
112  return rval;
113  }
114 
115  static util::ref_ptr<tree>
116  create(treeitem *root, bool showroot = false)
117  {
118  util::ref_ptr<tree> rval(new tree(root, showroot));
119  rval->decref();
120  return rval;
121  }
122 
123  void set_root(treeitem *_root, bool showroot=false);
124 
126  int width_request();
127 
132  int height_request(int w);
133 
134  bool get_cursorvisible();
135  point get_cursorloc();
136  virtual bool focus_me() {return true;}
137  virtual void paint(const style &st);
138  virtual void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
139 
149  void set_selection(treeiterator to, bool force_to_top = false);
150 
158  {
159  return selected;
160  }
161 
164  {
165  return begin;
166  }
167 
170  {
171  return end;
172  }
173 
174  virtual ~tree();
175 
176  void search_for(tree_search_func &matches);
177  void search_for(const std::wstring &s)
178  {
179  tree_search_string matches(s);
180  search_for(matches);
181  }
182 
183  void search_back_for(tree_search_func &matches);
184  void search_back_for(const std::wstring &s)
185  {
186  tree_search_string matches(s);
187  search_back_for(matches);
188  }
189 
190  void set_hierarchical(bool _hierarchical);
191  bool get_hierarchical() {return hierarchical;}
192 
194  void highlight_current();
195 
197  void unhighlight_current();
198 
205  sigc::signal1<void, treeitem *> selection_changed;
206 
207  // Execute the given command
208  void line_up();
209  void line_down();
210  void page_up();
211  void page_down();
212  void jump_to_begin();
213  void jump_to_end();
214  void level_line_up();
215  void level_line_down();
216 
217  static config::keybindings *bindings;
218  static void init_bindings();
219  // Sets up the bindings..
220  };
221 
223  }
224 }
225 
226 #endif
A "style" is a setting to be applied to a display element (widget, text, etc).
Definition: style.h:51
treeiterator get_begin()
Retrieve an iterator referencing the start of the tree.
Definition: tree.h:163
Stores the keys bound to various functions.
Definition: keybindings.h:87
Definition: widget.h:88
Definition: ref_ptr.h:19
sigc::signal1< void, treeitem * > selection_changed
Emitted when the selection moves to a new item.
Definition: tree.h:205
The namespace containing everything defined by cwidget.
Definition: columnify.cc:27
treeiterator get_end()
Retrieve an iterator referencing the end of the tree.
Definition: tree.h:169
Definition: treeitem.h:101
The basic widget interface.
Definition: widget.h:106
Represents a keystroke as seen by curses.
Definition: keybindings.h:42
Definition: treeitem.h:208
Definition: tree.h:56
treeiterator get_selection() const
Retrieve a reference to the currently selected entry in the tree.
Definition: tree.h:157