cwidget  0.5.17
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 <list>
29 #include <cwidget/generic/util/eassert.h>
30 
31 namespace cwidget
32 {
33  namespace config
34  {
35  class keybindings;
36  }
37 
38  namespace widgets
39  {
40  // A predicate on treeitems:
42  {
43  public:
44  virtual bool operator()(const treeitem &item)=0;
45  virtual ~tree_search_func() {}
46  };
47 
49  {
50  std::wstring s;
51  public:
52  tree_search_string(const std::wstring &_s):s(_s) {}
53 
54  virtual bool operator()(const treeitem &item);
55  };
56 
57  class tree : public widget
58  {
59  treeitem *root;
60  treeiterator begin, end;
61 
62  treeiterator top;
63  treeiterator selected;
64  // The top item on the current page and the currently selected item.
65  // NOTE: it's implicitly assumed in many places in the code that the
66  // currently selected item is visible (ie, on the screen).
67 
68  bool hierarchical;
69  // If not true, display the tree as a series of "flat thingies".
70  // Must be seen to be described :)
71 
72  // This structure is used to easily retrace our steps in flat-mode.
73  // (it could probably be done without this, but this makes it MUCH simpler)
74  // Note that we don't even bother with an STL list here; it's
75  // just not worth it.
76  struct flat_frame
77  {
78  treeiterator begin, end, top, selected;
79 
80  flat_frame *next;
81  flat_frame(treeiterator _begin,
82  treeiterator _end,
83  treeiterator _top,
84  treeiterator _selected,
85  flat_frame *_next)
86  :begin(_begin), end(_end), top(_top), selected(_selected), next(_next) {}
87  };
88  flat_frame *prev_level;
89 
90  int line_of(treeiterator item);
91  bool item_visible(treeiterator item);
92 
93  void do_shown();
94  protected:
95  void sync_bounds();
96  // This is an awful hack; I've been thinking about an alternate design of
97  // the tree code for a while, and this just confirms it. Yuck! :)
98  // It'll be the first thing to be removed in the next version..
99  // -- well, it wasn't.
100 
101  virtual bool handle_key(const config::key &k);
102 
103  protected:
104  tree();
105  tree(treeitem *_root, bool showroot);
106 
107  public:
108  static util::ref_ptr<tree>
109  create()
110  {
111  util::ref_ptr<tree> rval(new tree);
112  rval->decref();
113  return rval;
114  }
115 
116  static util::ref_ptr<tree>
117  create(treeitem *root, bool showroot = false)
118  {
119  util::ref_ptr<tree> rval(new tree(root, showroot));
120  rval->decref();
121  return rval;
122  }
123 
124  void set_root(treeitem *_root, bool showroot=false);
125 
127  int width_request();
128 
133  int height_request(int w);
134 
135  bool get_cursorvisible();
136  point get_cursorloc();
137  virtual bool focus_me() {return true;}
138  virtual void paint(const style &st);
139  virtual void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
140 
150  void set_selection(treeiterator to, bool force_to_top = false);
151 
159  {
160  return selected;
161  }
162 
165  {
166  return begin;
167  }
168 
171  {
172  return end;
173  }
174 
175  virtual ~tree();
176 
177  void search_for(tree_search_func &matches);
178  void search_for(const std::wstring &s)
179  {
180  tree_search_string matches(s);
181  search_for(matches);
182  }
183 
184  void search_back_for(tree_search_func &matches);
185  void search_back_for(const std::wstring &s)
186  {
187  tree_search_string matches(s);
188  search_back_for(matches);
189  }
190 
191  void set_hierarchical(bool _hierarchical);
192  bool get_hierarchical() {return hierarchical;}
193 
195  void highlight_current();
196 
198  void unhighlight_current();
199 
206  sigc::signal1<void, treeitem *> selection_changed;
207 
208  // Execute the given command
209  void line_up();
210  void line_down();
211  void page_up();
212  void page_down();
213  void jump_to_begin();
214  void jump_to_end();
215  void level_line_up();
216  void level_line_down();
217 
218  static config::keybindings *bindings;
219  static void init_bindings();
220  // Sets up the bindings..
221  };
222 
224  }
225 }
226 
227 #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:164
Stores the keys bound to various functions.
Definition: keybindings.h:87
Definition: widget.h:89
Definition: ref_ptr.h:19
sigc::signal1< void, treeitem * > selection_changed
Emitted when the selection moves to a new item.
Definition: tree.h:206
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
treeiterator get_end()
Retrieve an iterator referencing the end of the tree.
Definition: tree.h:170
Definition: treeitem.h:101
The basic widget interface.
Definition: widget.h:107
Represents a keystroke as seen by curses.
Definition: keybindings.h:42
Definition: treeitem.h:208
Definition: tree.h:57
treeiterator get_selection() const
Retrieve a reference to the currently selected entry in the tree.
Definition: tree.h:158