treeviewer.h

Go to the documentation of this file.
00001 #ifndef TREE_VIEWER_HEADER_INCLUDED_p
00002 #define TREE_VIEWER_HEADER_INCLUDED_p
00003 //
00004 // Copyright 2011, Lowell Boggs Jr.
00005 //
00006 // This file or directory, containing source code for a computer program,
00007 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00008 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00009 // source file without charge or obligation so long as you agree to
00010 // the following:
00011 //
00012 //  1.  You must indemnify Lowell Boggs against any and all financial
00013 //      obligations caused by its use, misuse, function, or malfunction.
00014 //      Further, you acknowledge that there is no warranty of any kind,
00015 //      whatsoever.
00016 //
00017 //  2.  You agree not to attempt to patent any portion of this original
00018 //      work -- though you may attempt to patent your own extensions to
00019 //      it if you so choose.
00020 //
00021 //  3.  You keep this copyright notice with the file and all copies
00022 //      of the file and do not change it anyway except language translation.
00023 //
00024 // You are responsible for enforcing your own compliance with these
00025 // conditions and may not use this source file if you cannot agree to the
00026 // above terms and conditions.
00027 
00028 #include <cxxtls/viewer.h>
00029 #include <cxxtls/simple_regex.h>
00030 #include <cxxtls/file.h>
00031 
00032 #include <list>
00033 #include <vector>
00034 #include <utility>  // to get std::pair
00035 
00036 //============================================================================================
00037 //============================================================================================
00038 //============================================================================================
00039 
00040 namespace cxxtls
00041 {
00042 
00043     //----------------------------------------------------------------------------------------
00044 
00045     struct TreeViewerNodeInfo
00050     {
00051        virtual ~TreeViewerNodeInfo()=0;
00052 
00053        virtual bool operator() (ViewerManager*vm, CursorWindow::viewport *vp, int command, FileName const &) = 0;
00064 
00065     };
00066 
00067     //----------------------------------------------------------------------------------------
00068 
00069     class TreeViewerNode
00087 
00088     {
00089     public:
00090         typedef  std::list<TreeViewerNode> ChildrenList;                  
00091         typedef  std::vector<std::pair<TreeViewerNode*, int> > FlatForm;  
00092 
00093     private:
00094 
00095         ChildrenList children_;  // can have TreeViewerContains in them
00096 
00097         bool opened_;            // Indicates that the user has requested to see the 
00098                                  // child members displayed.  True by default.
00099 
00100         std::string name_;       // The text of this tree viewer node.
00101 
00102         TreeViewerNodeInfo *info_; // user defined extension
00103 
00104         bool highlighted_;          // display this node underlined
00105 
00106     public:
00107 
00108         TreeViewerNode(std::string const &name);
00109         TreeViewerNode();
00110 
00111         virtual ~TreeViewerNode(); 
00112 
00113         void setName(std::string const &n); 
00114 
00115         void adoptInfo(TreeViewerNodeInfo *p); 
00116 
00117 
00118         TreeViewerNodeInfo *info(); 
00119 
00120 
00121         TreeViewerNode& addChild(std::string const &name);
00124 
00125         ChildrenList const &children() const; 
00126         ChildrenList       &children();       
00127 
00128         bool hasChildren() const;        
00129         bool opened() const;             
00130         std::string const &name() const; 
00131         virtual int height() const;      
00132 
00133 
00134         void close(bool childrenToo=true);  
00135         void open(bool childrenToo=true);   
00136 
00137         void flatten(FlatForm &output, int depth=0);
00142 
00143         void setHighlighted();  
00144 
00145         bool highlighted() const { return highlighted_; }  
00146     };
00147 
00148     //----------------------------------------------------------------------------------------
00149 
00150     class TreeViewer
00151     : public Viewer
00156     {
00157         bool           active_;   
00158         TreeViewerNode nodes_;    
00159 
00160         TreeViewerNode::FlatForm flattenedNodes_;  
00161 
00162         FileName rootDir_; 
00163 
00164 
00165 
00166     public:
00167         std::string application_name_;
00168     private:
00169 
00170         int topNode_;        // top item in the flattenedNodes_ list to be displayed.
00171         int displayNodes_;   // amount of flattenedNodes_ to be displayed in the viewport
00172         int cursorNode_;     // node under the cursor
00173 
00174         int treeStartRow_;  // Top row in viewport for display of tree info.
00175                             // The titles etc occur above this row.
00176         int displayRows_;   // rows where displaying of tree data can occur.
00177 
00178         bool keyKludge_;    // ^X has been pressed
00179 
00180         bool is_regex_;            // search type flag
00181         std::string search_text_;  // most recent search string
00182         SimpleRegex regexp_;       // most recent regex search
00183 
00184     public:
00185 
00186         TreeViewerNode &topNode();   
00187 
00188         void flatten() { nodes_.flatten(flattenedNodes_); }
00189 
00190         virtual ~TreeViewer();
00191 
00192         void setApplicationName(std::string const &name);
00193 
00194         static Viewer* app(ViewerManager*vm, std::string &fullname);
00200 
00201 
00202         TreeViewer(ViewerManager* vm); 
00203 
00204         virtual void operator() ( CursorWindow::viewport *     vp,
00205                                   int                          cmd
00206                                 );
00211         
00212         virtual bool handle_event( CursorWindow::input_event const * e,
00213                                    CursorWindow::viewport          * vp
00214                                  );
00239         
00240         virtual std::string const &application_name() const;
00246         
00247         virtual std::string description() const;
00253 
00254         virtual void help();
00256 
00257 
00258         void setRootDir(FileName const &dirname); 
00259 
00260 
00261         virtual void helpHeader(std::list<std::string> &helpText);  
00262 
00263     private:
00264         void acknowledgeViewport(CursorWindow::viewport * vp); // Set display height, etc
00265         void paintViewport(CursorWindow::viewport * vp);       // fill the viewport text
00266         void paintTitle(CursorWindow::viewport * vp);          // display window title
00267         void moveUp(CursorWindow::viewport * vp);              // cursor up
00268         void moveDown(CursorWindow::viewport * vp);            // cursor down
00269         void pageTop(CursorWindow::viewport * vp);             // move to top of list
00270         void pageBottom(CursorWindow::viewport * vp);          // move to bottom of list
00271         void pageDown(CursorWindow::viewport * vp);            // page down
00272         void pageUp(CursorWindow::viewport * vp);              // page up
00273         void findFirst(CursorWindow::viewport * vp);           // find string
00274         void findNext(CursorWindow::viewport * vp);            // find next instance of prev string
00275         bool findNextHelper(CursorWindow::viewport * vp, bool); // used by findfirst & next
00276         void make_line_visible(CursorWindow::viewport * vp);
00277         void displayCurrentLine(CursorWindow::viewport * vp, bool highlighted);
00278         void paintNodeText(CursorWindow::viewport * vp, int nodeNumber, bool isCursor=false);  
00279         void positionCursor(CursorWindow::viewport *vp); // for next user input
00280 
00281         void close(CursorWindow::viewport * vp);     // close the current item and redisplay the page
00282         void open(CursorWindow::viewport * vp);      // close the current item and redisplay the page
00283         void toggle(CursorWindow::viewport * vp);    // toggle open/close on current item
00284         void toggleAll(CursorWindow::viewport * vp); // toggle open/close on all items
00285 
00286         void findMyParent(CursorWindow::viewport * vp);     // search up for parent of curre item
00287         void findNextParent(CursorWindow::viewport * vp);   // search downfor next parent node, higher than current item
00288 
00289         
00290     };
00291 
00292 
00293 //----------------------------------------------------------------------------------------
00294 
00295 inline 
00296 TreeViewerNode::TreeViewerNode(std::string const &name)
00297 : opened_(true)
00298 , name_(name)
00299 , info_(0)
00300 , highlighted_(false)
00301 {
00302 }
00303 
00304 //----------------------------------------------------------------------------------------
00305 
00306 inline 
00307 TreeViewerNode::TreeViewerNode()
00308 : opened_(true)
00309 , info_(0)
00310 , highlighted_(false)
00311 {
00312 }
00313 
00314 //----------------------------------------------------------------------------------------
00315 
00316 inline 
00317 void
00318 TreeViewerNode::
00319 setName(std::string const &name)
00320 {
00321    name_ = name;
00322 }
00323 
00324 //----------------------------------------------------------------------------------------
00325 
00326 inline 
00327 std::string const &
00328 TreeViewerNode::
00329 name() const
00330 {
00331   return name_;
00332 }
00333 
00334 //----------------------------------------------------------------------------------------
00335 
00336 inline
00337 TreeViewerNode&
00338 TreeViewerNode::
00339 addChild(std::string const &name)
00340 {
00341    children_.push_back(name);
00342    return children_.back();
00343 }
00344 //----------------------------------------------------------------------------------------
00345 
00346 inline
00347 TreeViewerNode::ChildrenList const &
00348 TreeViewerNode::
00349 children() const 
00350 { 
00351     return children_; 
00352 }
00353 //----------------------------------------------------------------------------------------
00354 inline
00355 TreeViewerNode::ChildrenList &
00356 TreeViewerNode::
00357 children()
00358 { 
00359     return children_; 
00360 }
00361 
00362 //----------------------------------------------------------------------------------------
00363 
00364 inline
00365 bool 
00366 TreeViewerNode::
00367 hasChildren() const 
00368 { 
00369     return !children_.empty(); 
00370 }  
00371 
00372 //----------------------------------------------------------------------------------------
00373 
00374 inline
00375 bool 
00376 TreeViewerNode::
00377 opened() const 
00378 { 
00379     return opened_; 
00380 }  
00381 
00382 //----------------------------------------------------------------------------------------
00383 
00384 inline 
00385 TreeViewer::
00386 TreeViewer(ViewerManager* vm)
00387 : Viewer(vm)
00388 , active_(false)
00389 , application_name_("TreeViewer")
00390 , topNode_(0)
00391 , displayNodes_(0)
00392 , cursorNode_(0)
00393 , treeStartRow_(0)
00394 , displayRows_(0)
00395 , keyKludge_(false)
00396 , is_regex_(false)
00397 {
00398 }
00399 //----------------------------------------------------------------------------------------
00400 
00401 
00402 inline 
00403 TreeViewerNode &
00404 TreeViewer::
00405 topNode() 
00406 { 
00407    return nodes_; 
00408 
00409 } 
00410 
00411 //----------------------------------------------------------------------------------------
00412 
00413 
00414 inline 
00415 void
00416 TreeViewerNode::
00417 adoptInfo(TreeViewerNodeInfo *p) 
00418 { 
00419     delete info_;
00420 
00421     info_ = p;
00422 } 
00423 
00424 //----------------------------------------------------------------------------------------
00425 
00426 
00427 inline 
00428 TreeViewerNode::
00429 ~TreeViewerNode()
00430 { 
00431     delete info_;
00432 
00433 } 
00434 
00435 //----------------------------------------------------------------------------------------
00436 
00437 
00438 inline 
00439 TreeViewerNodeInfo *
00440 TreeViewerNode::
00441 info()
00442 { 
00443     return info_;
00444 } 
00445 
00446 //----------------------------------------------------------------------------------------
00447 
00448 inline
00449 void 
00450 TreeViewer::
00451 setApplicationName(std::string const &name)
00452 {
00453    application_name_ = name;
00454 }
00455 
00456 //----------------------------------------------------------------------------------------
00457 
00458 inline
00459 void 
00460 TreeViewer::
00461 setRootDir(FileName const &name)
00462 {
00463    rootDir_ = name;
00464 }
00465 
00466 //----------------------------------------------------------------------------------------
00467 inline
00468 void
00469 TreeViewerNode::
00470 setHighlighted()
00471 {
00472    highlighted_ = true;
00473 }
00474 
00475 
00476 
00477 //----------------------------------------------------------------------------------------
00478 
00479 } // namespace cxxtls
00480 #endif
00481 
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3