tree.h

00001 // tree.h  (this is -*-c++-*-)
00002 //
00003 //  Copyright 1999-2001, 2004-2007 Daniel Burrows
00004 //
00005 //  This program is free software; you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; see the file COPYING.  If not, write to
00017 //  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018 //  Boston, MA 02111-1307, USA.
00019 //
00020 //  A simple tree widget.
00021 
00022 #ifndef TREE_H
00023 #define TREE_H
00024 
00025 #include "treeitem.h"
00026 #include "widget.h"
00027 
00028 #include <list>
00029 #include <cwidget/generic/util/eassert.h>
00030 
00031 namespace cwidget
00032 {
00033   namespace config
00034   {
00035     class keybindings;
00036   }
00037 
00038   namespace widgets
00039   {
00040     // A predicate on treeitems:
00041     class tree_search_func
00042     {
00043     public:
00044       virtual bool operator()(const treeitem &item)=0;
00045       virtual ~tree_search_func() {}
00046     };
00047 
00048     class tree_search_string:public tree_search_func
00049     {
00050       std::wstring s;
00051     public:
00052       tree_search_string(const std::wstring &_s):s(_s) {}
00053 
00054       virtual bool operator()(const treeitem &item);
00055     };
00056 
00057     class tree : public widget
00058     {
00059       treeitem *root;
00060       treeiterator begin, end;
00061 
00062       treeiterator top;
00063       treeiterator selected;
00064       // The top item on the current page and the currently selected item.
00065       // NOTE: it's implicitly assumed in many places in the code that the 
00066       // currently selected item is visible (ie, on the screen).
00067 
00068       bool hierarchical;
00069       // If not true, display the tree as a series of "flat thingies".
00070       // Must be seen to be described :)
00071 
00072       // This structure is used to easily retrace our steps in flat-mode.
00073       // (it could probably be done without this, but this makes it MUCH simpler)
00074       // Note that we don't even bother with an STL list here; it's
00075       // just not worth it.
00076       struct flat_frame
00077       {
00078         treeiterator begin, end, top, selected;
00079 
00080         flat_frame *next;
00081         flat_frame(treeiterator _begin,
00082                    treeiterator _end,
00083                    treeiterator _top,
00084                    treeiterator _selected,
00085                    flat_frame *_next)
00086           :begin(_begin), end(_end), top(_top), selected(_selected), next(_next) {}
00087       };
00088       flat_frame *prev_level;
00089 
00090       int line_of(treeiterator item);
00091       bool item_visible(treeiterator item);
00092 
00093       void do_shown();
00094     protected:
00095       void sync_bounds();
00096       // This is an awful hack; I've been thinking about an alternate design of
00097       // the tree code for a while, and this just confirms it.  Yuck! :)
00098       //  It'll be the first thing to be removed in the next version..
00099       //  -- well, it wasn't.
00100 
00101       virtual bool handle_key(const config::key &k);
00102 
00103     protected:
00104       tree();
00105       tree(treeitem *_root, bool showroot);
00106 
00107     public:
00108       static util::ref_ptr<tree>
00109       create()
00110       {
00111         util::ref_ptr<tree> rval(new tree);
00112         rval->decref();
00113         return rval;
00114       }
00115 
00116       static util::ref_ptr<tree>
00117       create(treeitem *root, bool showroot = false)
00118       {
00119         util::ref_ptr<tree> rval(new tree(root, showroot));
00120         rval->decref();
00121         return rval;
00122       }
00123 
00124       void set_root(treeitem *_root, bool showroot=false);
00125 
00127       int width_request();
00128 
00133       int height_request(int w);
00134 
00135       bool get_cursorvisible();
00136       point get_cursorloc();
00137       virtual bool focus_me() {return true;}
00138       virtual void paint(const style &st);
00139       virtual void dispatch_mouse(short id, int x, int y, int z, mmask_t bstate);
00140 
00150       void set_selection(treeiterator to, bool force_to_top = false);
00151 
00158       treeiterator get_selection() const
00159       {
00160         return selected;
00161       }
00162 
00164       treeiterator get_begin()
00165       {
00166         return begin;
00167       }
00168 
00170       treeiterator get_end()
00171       {
00172         return end;
00173       }
00174 
00175       virtual ~tree();
00176 
00177       void search_for(tree_search_func &matches);
00178       void search_for(const std::wstring &s)
00179       {
00180         tree_search_string matches(s);
00181         search_for(matches);
00182       }
00183 
00184       void search_back_for(tree_search_func &matches);
00185       void search_back_for(const std::wstring &s)
00186       {
00187         tree_search_string matches(s);
00188         search_back_for(matches);
00189       }
00190 
00191       void set_hierarchical(bool _hierarchical);
00192       bool get_hierarchical() {return hierarchical;}
00193 
00195       void highlight_current();
00196 
00198       void unhighlight_current();
00199 
00206       sigc::signal1<void, treeitem *> selection_changed;
00207 
00208       // Execute the given command
00209       void line_up();
00210       void line_down();
00211       void page_up();
00212       void page_down();
00213       void jump_to_begin();
00214       void jump_to_end();
00215       void level_line_up();
00216       void level_line_down();
00217 
00218       static config::keybindings *bindings;
00219       static void init_bindings();
00220       // Sets up the bindings..
00221     };
00222 
00223     typedef util::ref_ptr<tree> tree_ref;
00224   }
00225 }
00226 
00227 #endif

Generated on Wed Jan 28 07:23:52 2009 for cwidget by  doxygen 1.5.6