Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXTreeList.h

00001 /********************************************************************************
00002 *                                                                               *
00003 *                         T r e e   L i s t   W i d g e t                       *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2002 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXTreeList.h,v 1.55 2002/09/30 13:06:56 fox Exp $                        *
00023 ********************************************************************************/
00024 #ifndef FXTREELIST_H
00025 #define FXTREELIST_H
00026 
00027 #ifndef FXSCROLLAREA_H
00028 #include "FXScrollArea.h"
00029 #endif
00030 
00031 namespace FX {
00032 
00033 
00034 struct FXTimer;
00035 class FXIcon;
00036 class FXFont;
00037 class FXTreeList;
00038 class FXDirList;
00039 
00040 
00041 /// Tree list styles
00042 enum {
00043   TREELIST_EXTENDEDSELECT = 0,            /// Extended selection mode allows for drag-selection of ranges of items
00044   TREELIST_SINGLESELECT   = 0x00100000,   /// Single selection mode allows up to one item to be selected
00045   TREELIST_BROWSESELECT   = 0x00200000,   /// Browse selection mode enforces one single item to be selected at all times
00046   TREELIST_MULTIPLESELECT = 0x00300000,   /// Multiple selection mode is used for selection of individual items
00047   TREELIST_AUTOSELECT     = 0x00400000,   /// Automatically select under cursor
00048   TREELIST_SHOWS_LINES    = 0x00800000,   /// Lines shown
00049   TREELIST_SHOWS_BOXES    = 0x01000000,   /// Boxes to expand shown
00050   TREELIST_ROOT_BOXES     = 0x02000000,   /// Display root boxes also
00051   TREELIST_NORMAL         = TREELIST_EXTENDEDSELECT
00052   };
00053 
00054 
00055 /// Tree list Item
00056 class FXAPI FXTreeItem : public FXObject {
00057   FXDECLARE(FXTreeItem)
00058   friend class FXTreeList;
00059   friend class FXDirList;
00060 protected:
00061   FXTreeItem *prev;
00062   FXTreeItem *next;
00063   FXTreeItem *parent;
00064   FXTreeItem *first;
00065   FXTreeItem *last;
00066   FXString    label;
00067   FXIcon     *openIcon;
00068   FXIcon     *closedIcon;
00069   void       *data;
00070   FXuint      state;
00071   FXint       x,y;
00072 protected:
00073   FXTreeItem():prev(NULL),next(NULL),parent(NULL),first(NULL),last(NULL),openIcon(NULL),closedIcon(NULL),data(NULL),state(0),x(0),y(0){}
00074   virtual void draw(const FXTreeList* list,FXDC& dc,FXint x,FXint y,FXint w,FXint h) const;
00075   virtual FXint hitItem(const FXTreeList* list,FXint x,FXint y) const;
00076 protected:
00077   enum{
00078     SELECTED        = 1,
00079     FOCUS           = 2,
00080     DISABLED        = 4,
00081     OPENED          = 8,
00082     EXPANDED        = 16,
00083     HASITEMS        = 32,
00084     DRAGGABLE       = 64,
00085     OPENICONOWNED   = 128,
00086     CLOSEDICONOWNED = 256
00087     };
00088 public:
00089 
00090   /// Constructor
00091   FXTreeItem(const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL):prev(NULL),next(NULL),parent(NULL),first(NULL),last(NULL),label(text),openIcon(oi),closedIcon(ci),data(ptr),state(0),x(0),y(0){}
00092 
00093   /// Get parent item
00094   FXTreeItem* getParent() const { return parent; }
00095 
00096   /// Get next sibling item
00097   FXTreeItem* getNext() const { return next; }
00098 
00099   /// Get previous sibling item
00100   FXTreeItem* getPrev() const { return prev; }
00101 
00102   /// Get first child item
00103   FXTreeItem* getFirst() const { return first; }
00104 
00105   /// Get las child item
00106   FXTreeItem* getLast() const { return last; }
00107 
00108   /// Get item below this one in list
00109   FXTreeItem* getBelow() const;
00110 
00111   /// Get item above this one in list
00112   FXTreeItem* getAbove() const;
00113 
00114   /// Get number of children of item
00115   FXint getNumChildren() const;
00116 
00117   /// Change item label
00118   virtual void setText(const FXString& txt){ label=txt; }
00119 
00120   /// Get item label
00121   const FXString& getText() const { return label; }
00122 
00123   /// Change open icon
00124   virtual void setOpenIcon(FXIcon* icn){ openIcon=icn; }
00125 
00126   /// Get open icon
00127   FXIcon* getOpenIcon() const { return openIcon; }
00128 
00129   /// Change closed icon
00130   virtual void setClosedIcon(FXIcon* icn){ closedIcon=icn; }
00131 
00132   /// Get closed icon
00133   FXIcon* getClosedIcon() const { return closedIcon; }
00134 
00135   /// Change item user data
00136   void setData(void* ptr){ data=ptr; }
00137 
00138   /// Get item user data
00139   void* getData() const { return data; }
00140 
00141   /// Make item draw as focused
00142   virtual void setFocus(FXbool focus);
00143 
00144   /// Return true if item has focus
00145   FXbool hasFocus() const { return (state&FOCUS)!=0; }
00146 
00147   /// Select item
00148   virtual void setSelected(FXbool selected);
00149 
00150   /// Return true if this item is selected
00151   FXbool isSelected() const { return (state&SELECTED)!=0; }
00152 
00153   /// Make item show as open
00154   virtual void setOpened(FXbool opened);
00155 
00156   /// Return true if this item is open
00157   FXbool isOpened() const { return (state&OPENED)!=0; }
00158 
00159   /// Expand or collapse item
00160   virtual void setExpanded(FXbool expanded);
00161 
00162   /// Return true if this item is expanded into sub items
00163   FXbool isExpanded() const { return (state&EXPANDED)!=0; }
00164 
00165   /// Enable or disable item
00166   virtual void setEnabled(FXbool enabled);
00167 
00168   /// Return true if this item is enabled
00169   FXbool isEnabled() const { return (state&DISABLED)==0; }
00170 
00171   /// Make item draggable
00172   virtual void setDraggable(FXbool draggable);
00173 
00174   /// Return true if this item is draggable
00175   FXbool isDraggable() const { return (state&DRAGGABLE)!=0; }
00176 
00177   /// Make open and or icon owned by the item
00178   void setIconOwned(FXuint owned=(OPENICONOWNED|CLOSEDICONOWNED));
00179 
00180   /// Return open icon and closed icon ownership status
00181   FXuint isIconOwned() const { return (state&(OPENICONOWNED|CLOSEDICONOWNED)); }
00182 
00183   /// Return width of item as drawin in list
00184   virtual FXint getWidth(const FXTreeList* list) const;
00185 
00186   /// Return height of item as drawn in list
00187   virtual FXint getHeight(const FXTreeList* list) const;
00188 
00189   /// Create server-side resources
00190   virtual void create();
00191 
00192   /// Detach server-side resources
00193   virtual void detach();
00194 
00195   /// Destroy server-side resources
00196   virtual void destroy();
00197 
00198   /// Save to stream
00199   virtual void save(FXStream& store) const;
00200 
00201   /// Load from stream
00202   virtual void load(FXStream& store);
00203 
00204   /// Destroy item and free icons if owned
00205   virtual ~FXTreeItem();
00206   };
00207 
00208 
00209 
00210 /// Tree item collate function
00211 typedef FXint (*FXTreeListSortFunc)(const FXTreeItem*,const FXTreeItem*);
00212 
00213 
00214 
00215 /// Tree list Widget
00216 class FXAPI FXTreeList : public FXScrollArea {
00217   FXDECLARE(FXTreeList)
00218 protected:
00219   FXTreeItem        *firstitem;         // First root item
00220   FXTreeItem        *lastitem;          // Last root item
00221   FXTreeItem        *anchoritem;        // Selection anchor item
00222   FXTreeItem        *currentitem;       // Current item
00223   FXTreeItem        *extentitem;        // Selection extent
00224   FXTreeItem        *cursoritem;        // Item under cursor
00225   FXFont            *font;              // Font
00226   FXTreeListSortFunc sortfunc;          // Item sort function
00227   FXColor            textColor;         // Text color
00228   FXColor            selbackColor;      // Selected background color
00229   FXColor            seltextColor;      // Selected text color
00230   FXColor            lineColor;         // Line color
00231   FXint              treeWidth;         // Tree width
00232   FXint              treeHeight;        // Tree height
00233   FXint              visible;           // Number of visible items
00234   FXint              indent;            // Parent to child indentation
00235   FXint              grabx;             // Grab point x
00236   FXint              graby;             // Grab point y
00237   FXString           lookup;            // Lookup string
00238   FXTimer           *timer;             // Tip timer
00239   FXTimer           *lookuptimer;       // Lookup timer
00240   FXString           help;              // Help string
00241   FXbool             state;             // State of item
00242 protected:
00243   FXTreeList();
00244   virtual void layout();
00245   virtual FXTreeItem* createItem(const FXString& text,FXIcon* oi,FXIcon* ci,void* ptr);
00246   void sort(FXTreeItem*& f1,FXTreeItem*& t1,FXTreeItem*& f2,FXTreeItem*& t2,int n);
00247   void recompute();
00248 private:
00249   FXTreeList(const FXTreeList&);
00250   FXTreeList& operator=(const FXTreeList&);
00251 public:
00252   long onPaint(FXObject*,FXSelector,void*);
00253   long onEnter(FXObject*,FXSelector,void*);
00254   long onLeave(FXObject*,FXSelector,void*);
00255   long onUngrabbed(FXObject*,FXSelector,void*);
00256   long onMotion(FXObject*,FXSelector,void*);
00257   long onKeyPress(FXObject*,FXSelector,void*);
00258   long onKeyRelease(FXObject*,FXSelector,void*);
00259   long onLeftBtnPress(FXObject*,FXSelector,void*);
00260   long onLeftBtnRelease(FXObject*,FXSelector,void*);
00261   long onRightBtnPress(FXObject*,FXSelector,void*);
00262   long onRightBtnRelease(FXObject*,FXSelector,void*);
00263   long onQueryTip(FXObject*,FXSelector,void*);
00264   long onQueryHelp(FXObject*,FXSelector,void*);
00265   long onTipTimer(FXObject*,FXSelector,void*);
00266   long onFocusIn(FXObject*,FXSelector,void*);
00267   long onFocusOut(FXObject*,FXSelector,void*);
00268   long onAutoScroll(FXObject*,FXSelector,void*);
00269   long onClicked(FXObject*,FXSelector,void*);
00270   long onDoubleClicked(FXObject*,FXSelector,void*);
00271   long onTripleClicked(FXObject*,FXSelector,void*);
00272   long onCommand(FXObject*,FXSelector,void*);
00273   long onSelected(FXObject*,FXSelector,void*);
00274   long onDeselected(FXObject*,FXSelector,void*);
00275   long onOpened(FXObject*,FXSelector,void*);
00276   long onClosed(FXObject*,FXSelector,void*);
00277   long onExpanded(FXObject*,FXSelector,void*);
00278   long onCollapsed(FXObject*,FXSelector,void*);
00279   long onLookupTimer(FXObject*,FXSelector,void*);
00280 public:
00281   static FXint ascending(const FXTreeItem* a,const FXTreeItem* b);
00282   static FXint descending(const FXTreeItem* a,const FXTreeItem* b);
00283 public:
00284   enum {
00285     ID_TIPTIMER=FXScrollArea::ID_LAST,
00286     ID_LOOKUPTIMER,
00287     ID_LAST
00288     };
00289 public:
00290 
00291   /// Construct a tree list with nvis visible items; the tree list is initially empty
00292   FXTreeList(FXComposite *p,FXint nvis,FXObject* tgt=NULL,FXSelector sel=0,FXuint opts=TREELIST_NORMAL,FXint x=0,FXint y=0,FXint w=0,FXint h=0);
00293 
00294   /// Create server-side resources
00295   virtual void create();
00296 
00297   /// Detach server-side resources
00298   virtual void detach();
00299 
00300   /// Return default width
00301   virtual FXint getDefaultWidth();
00302 
00303   /// Return default height
00304   virtual FXint getDefaultHeight();
00305 
00306   /// Compute and return content width
00307   virtual FXint getContentWidth();
00308 
00309   /// Return content height
00310   virtual FXint getContentHeight();
00311 
00312   /// Recalculate layout
00313   virtual void recalc();
00314 
00315   /// Tree list can receive focus
00316   virtual FXbool canFocus() const;
00317 
00318   /// Move the focus to this window
00319   virtual void setFocus();
00320 
00321   /// Remove the focus from this window
00322   virtual void killFocus();
00323 
00324   /// Return number of items
00325   FXint getNumItems() const;
00326 
00327   /// Return number of visible items
00328   FXint getNumVisible() const { return visible; }
00329 
00330   /// Change number of visible items
00331   void setNumVisible(FXint nvis);
00332 
00333   /// REturn first root item
00334   FXTreeItem* getFirstItem() const { return firstitem; }
00335 
00336   /// Return last root item
00337   FXTreeItem* getLastItem() const { return lastitem; }
00338 
00339   /// Prepend new [possibly subclassed] item as first child of p
00340   FXTreeItem* addItemFirst(FXTreeItem* p,FXTreeItem* item,FXbool notify=FALSE);
00341 
00342   /// Prepend new item with given text and optional icon, and user-data pointer as first child of p
00343   FXTreeItem* addItemFirst(FXTreeItem* p,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE);
00344 
00345   /// Append new [possibly subclassed] item as last child of p
00346   FXTreeItem* addItemLast(FXTreeItem* p,FXTreeItem* item,FXbool notify=FALSE);
00347 
00348   /// Append new item with given text and optional icon, and user-data pointer as last child of p
00349   FXTreeItem* addItemLast(FXTreeItem* p,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE);
00350 
00351   /// Append new [possibly subclassed] item after to other item
00352   FXTreeItem* addItemAfter(FXTreeItem* other,FXTreeItem* item,FXbool notify=FALSE);
00353 
00354   /// Append new item with given text and optional icon, and user-data pointer after to other item
00355   FXTreeItem* addItemAfter(FXTreeItem* other,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE);
00356 
00357   /// Prepend new [possibly subclassed] item prior to other item
00358   FXTreeItem* addItemBefore(FXTreeItem* other,FXTreeItem* item,FXbool notify=FALSE);
00359 
00360   /// Prepend new item with given text and optional icon, and user-data pointer prior to other item
00361   FXTreeItem* addItemBefore(FXTreeItem* other,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE);
00362 
00363   /// Remove item
00364   void removeItem(FXTreeItem* item,FXbool notify=FALSE);
00365 
00366   /// Remove items in range [fm, to] inclusively
00367   void removeItems(FXTreeItem* fm,FXTreeItem* to,FXbool notify=FALSE);
00368 
00369   /// Remove all items from list
00370   void clearItems(FXbool notify=FALSE);
00371 
00372   /// Return item width
00373   FXint getItemWidth(const FXTreeItem* item) const;
00374 
00375   /// Return item height
00376   FXint getItemHeight(const FXTreeItem* item) const;
00377 
00378   /// Get item at x,y, if any
00379   virtual FXTreeItem* getItemAt(FXint x,FXint y) const;
00380 
00381   /**
00382   * Search items for item by name, starting from start item; the
00383   * flags argument controls the search direction, and case sensitivity.
00384   */
00385   FXTreeItem* findItem(const FXString& text,FXTreeItem* start=NULL,FXuint flags=SEARCH_FORWARD|SEARCH_WRAP) const;
00386 
00387   /// Scroll to make item visible
00388   void makeItemVisible(FXTreeItem* item);
00389 
00390   /// Change item's text
00391   void setItemText(FXTreeItem* item,const FXString& text);
00392 
00393   /// Return item's text
00394   FXString getItemText(const FXTreeItem* item) const;
00395 
00396   /// Change item's open icon
00397   void setItemOpenIcon(FXTreeItem* item,FXIcon* icon);
00398 
00399   /// Return item's open icon
00400   FXIcon* getItemOpenIcon(const FXTreeItem* item) const;
00401 
00402   /// Chance item's closed icon
00403   void setItemClosedIcon(FXTreeItem* item,FXIcon* icon);
00404 
00405   /// Return item's closed icon
00406   FXIcon* getItemClosedIcon(const FXTreeItem* item) const;
00407 
00408   /// Change item user-data pointer
00409   void setItemData(FXTreeItem* item,void* ptr) const;
00410 
00411   /// Return item user-data pointer
00412   void* getItemData(const FXTreeItem* item) const;
00413 
00414   /// Return TRUE if item is selected
00415   FXbool isItemSelected(const FXTreeItem* item) const;
00416 
00417   /// Return TRUE if item is current
00418   FXbool isItemCurrent(const FXTreeItem* item) const;
00419 
00420   /// Return TRUE if item is visible
00421   FXbool isItemVisible(const FXTreeItem* item) const;
00422 
00423   /// Return TRUE if item opened
00424   FXbool isItemOpened(const FXTreeItem* item) const;
00425 
00426   /// Return TRUE if item expanded
00427   FXbool isItemExpanded(const FXTreeItem* item) const;
00428 
00429   /// Return TRUE if item is a leaf-item, i.e. has no children
00430   FXbool isItemLeaf(const FXTreeItem* item) const;
00431 
00432   /// Return TRUE if item is enabled
00433   FXbool isItemEnabled(const FXTreeItem* item) const;
00434 
00435   /// Return item hit code: 0 outside, 1 icon, 2 text, 3 box
00436   FXint hitItem(const FXTreeItem* item,FXint x,FXint y) const;
00437 
00438   /// Repaint item
00439   void updateItem(FXTreeItem* item) const;
00440 
00441   /// Enable item
00442   FXbool enableItem(FXTreeItem* item);
00443 
00444   /// Disable item
00445   FXbool disableItem(FXTreeItem* item);
00446 
00447   /// Select item
00448   FXbool selectItem(FXTreeItem* item,FXbool notify=FALSE);
00449 
00450   /// Deselect item
00451   FXbool deselectItem(FXTreeItem* item,FXbool notify=FALSE);
00452 
00453   /// Toggle item selection
00454   FXbool toggleItem(FXTreeItem* item,FXbool notify=FALSE);
00455 
00456   /// Open item
00457   FXbool openItem(FXTreeItem* item,FXbool notify=FALSE);
00458 
00459   /// Close item
00460   FXbool closeItem(FXTreeItem* item,FXbool notify=FALSE);
00461 
00462   /// Collapse tree
00463   FXbool collapseTree(FXTreeItem* tree,FXbool notify=FALSE);
00464 
00465   /// Expand tree
00466   FXbool expandTree(FXTreeItem* tree,FXbool notify=FALSE);
00467 
00468   /// Reparent item under parent p
00469   void reparentItem(FXTreeItem* item,FXTreeItem* p);
00470 
00471   /// Change current item
00472   void setCurrentItem(FXTreeItem* item,FXbool notify=FALSE);
00473 
00474   /// Return current item, if any
00475   FXTreeItem* getCurrentItem() const { return currentitem; }
00476 
00477   /// Change anchor item
00478   void setAnchorItem(FXTreeItem* item);
00479 
00480   /// Return anchor item, if any
00481   FXTreeItem* getAnchorItem() const { return anchoritem; }
00482 
00483   /// Return item under cursor, if any
00484   FXTreeItem* getCursorItem() const { return cursoritem; }
00485 
00486   /// Extend selection from anchor item to item
00487   FXbool extendSelection(FXTreeItem* item,FXbool notify=FALSE);
00488 
00489   /// Deselect all items
00490   FXbool killSelection(FXbool notify=FALSE);
00491 
00492   /// Sort root items
00493   void sortItems();
00494 
00495   /// Sort children of item
00496   void sortChildItems(FXTreeItem* item);
00497 
00498   /// Change text font
00499   void setFont(FXFont* fnt);
00500 
00501   /// Return text font
00502   FXFont* getFont() const { return font; }
00503 
00504   /// Change parent-child indent amount
00505   void setIndent(FXint in);
00506 
00507   /// Return parent-child indent amount
00508   FXint getIndent() const { return indent; }
00509 
00510   /// Return normal text color
00511   FXColor getTextColor() const { return textColor; }
00512 
00513   /// Change normal text color
00514   void setTextColor(FXColor clr);
00515 
00516   /// Return selected text background
00517   FXColor getSelBackColor() const { return selbackColor; }
00518 
00519   /// Change selected text background
00520   void setSelBackColor(FXColor clr);
00521 
00522   /// Return selected text color
00523   FXColor getSelTextColor() const { return seltextColor; }
00524 
00525   /// Change selected text color
00526   void setSelTextColor(FXColor clr);
00527 
00528   /// Return line color
00529   FXColor getLineColor() const { return lineColor; }
00530 
00531   /// Change line color
00532   void setLineColor(FXColor clr);
00533 
00534   /// Return sort function
00535   FXTreeListSortFunc getSortFunc() const { return sortfunc; }
00536 
00537   /// Change sort function
00538   void setSortFunc(FXTreeListSortFunc func){ sortfunc=func; }
00539 
00540   /// Return list style
00541   FXuint getListStyle() const;
00542 
00543   /// Change list style
00544   void setListStyle(FXuint style);
00545 
00546   /// Set the status line help text for this list
00547   void setHelpText(const FXString& text);
00548 
00549   /// Get the status line help text for this list
00550   const FXString& getHelpText() const { return help; }
00551 
00552   /// Save object to a stream
00553   virtual void save(FXStream& store) const;
00554 
00555   /// Load object from a stream
00556   virtual void load(FXStream& store);
00557 
00558   /// Destructor
00559   virtual ~FXTreeList();
00560   };
00561 
00562 }
00563 
00564 #endif