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,2004 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.78 2004/03/15 16:05:28 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 class FXIcon; 00035 class FXFont; 00036 class FXTreeList; 00037 class FXDirList; 00038 00039 00040 /// Tree list styles 00041 enum { 00042 TREELIST_EXTENDEDSELECT = 0, /// Extended selection mode allows for drag-selection of ranges of items 00043 TREELIST_SINGLESELECT = 0x00100000, /// Single selection mode allows up to one item to be selected 00044 TREELIST_BROWSESELECT = 0x00200000, /// Browse selection mode enforces one single item to be selected at all times 00045 TREELIST_MULTIPLESELECT = 0x00300000, /// Multiple selection mode is used for selection of individual items 00046 TREELIST_AUTOSELECT = 0x00400000, /// Automatically select under cursor 00047 TREELIST_SHOWS_LINES = 0x00800000, /// Lines shown 00048 TREELIST_SHOWS_BOXES = 0x01000000, /// Boxes to expand shown 00049 TREELIST_ROOT_BOXES = 0x02000000, /// Display root boxes also 00050 TREELIST_NORMAL = TREELIST_EXTENDEDSELECT 00051 }; 00052 00053 00054 /// Tree list Item 00055 class FXAPI FXTreeItem : public FXObject { 00056 FXDECLARE(FXTreeItem) 00057 friend class FXTreeList; 00058 friend class FXDirList; 00059 protected: 00060 FXTreeItem *parent; 00061 FXTreeItem *prev; 00062 FXTreeItem *next; 00063 FXTreeItem *first; 00064 FXTreeItem *last; 00065 FXString label; 00066 FXIcon *openIcon; 00067 FXIcon *closedIcon; 00068 void *data; 00069 FXuint state; 00070 FXint x,y; 00071 protected: 00072 FXTreeItem():parent(NULL),prev(NULL),next(NULL),first(NULL),last(NULL),openIcon(NULL),closedIcon(NULL),data(NULL),state(0),x(0),y(0){} 00073 virtual void draw(const FXTreeList* list,FXDC& dc,FXint x,FXint y,FXint w,FXint h) const; 00074 virtual FXint hitItem(const FXTreeList* list,FXint x,FXint y) const; 00075 protected: 00076 enum{ 00077 SELECTED = 1, 00078 FOCUS = 2, 00079 DISABLED = 4, 00080 OPENED = 8, 00081 EXPANDED = 16, 00082 HASITEMS = 32, 00083 DRAGGABLE = 64, 00084 OPENICONOWNED = 128, 00085 CLOSEDICONOWNED = 256 00086 }; 00087 public: 00088 00089 /// Constructor 00090 FXTreeItem(const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL):parent(NULL),prev(NULL),next(NULL),first(NULL),last(NULL),label(text),openIcon(oi),closedIcon(ci),data(ptr),state(0),x(0),y(0){} 00091 00092 /// Get parent item 00093 FXTreeItem* getParent() const { return parent; } 00094 00095 /// Get next sibling item 00096 FXTreeItem* getNext() const { return next; } 00097 00098 /// Get previous sibling item 00099 FXTreeItem* getPrev() const { return prev; } 00100 00101 /// Get first child item 00102 FXTreeItem* getFirst() const { return first; } 00103 00104 /// Get las child item 00105 FXTreeItem* getLast() const { return last; } 00106 00107 /// Get item below this one in list 00108 FXTreeItem* getBelow() const; 00109 00110 /// Get item above this one in list 00111 FXTreeItem* getAbove() const; 00112 00113 /// Get number of children of item 00114 FXint getNumChildren() const; 00115 00116 /// Change item label 00117 virtual void setText(const FXString& txt){ label=txt; } 00118 00119 /// Get item label 00120 const FXString& getText() const { return label; } 00121 00122 /// Change open icon 00123 virtual void setOpenIcon(FXIcon* icn){ openIcon=icn; } 00124 00125 /// Get open icon 00126 FXIcon* getOpenIcon() const { return openIcon; } 00127 00128 /// Change closed icon 00129 virtual void setClosedIcon(FXIcon* icn){ closedIcon=icn; } 00130 00131 /// Get closed icon 00132 FXIcon* getClosedIcon() const { return closedIcon; } 00133 00134 /// Change item user data 00135 void setData(void* ptr){ data=ptr; } 00136 00137 /// Get item user data 00138 void* getData() const { return data; } 00139 00140 /// Make item draw as focused 00141 virtual void setFocus(FXbool focus); 00142 00143 /// Return true if item has focus 00144 FXbool hasFocus() const { return (state&FOCUS)!=0; } 00145 00146 /// Select item 00147 virtual void setSelected(FXbool selected); 00148 00149 /// Return true if this item is selected 00150 FXbool isSelected() const { return (state&SELECTED)!=0; } 00151 00152 /// Make item show as open 00153 virtual void setOpened(FXbool opened); 00154 00155 /// Return true if this item is open 00156 FXbool isOpened() const { return (state&OPENED)!=0; } 00157 00158 /// Expand or collapse item 00159 virtual void setExpanded(FXbool expanded); 00160 00161 /// Return true if this item is expanded into sub items 00162 FXbool isExpanded() const { return (state&EXPANDED)!=0; } 00163 00164 /// Enable or disable item 00165 virtual void setEnabled(FXbool enabled); 00166 00167 /// Return true if this item is enabled 00168 FXbool isEnabled() const { return (state&DISABLED)==0; } 00169 00170 /// Make item draggable 00171 virtual void setDraggable(FXbool draggable); 00172 00173 /// Return true if this item is draggable 00174 FXbool isDraggable() const { return (state&DRAGGABLE)!=0; } 00175 00176 /// Make open and or icon owned by the item 00177 void setIconOwned(FXuint owned=(OPENICONOWNED|CLOSEDICONOWNED)); 00178 00179 /// Return open icon and closed icon ownership status 00180 FXuint isIconOwned() const { return (state&(OPENICONOWNED|CLOSEDICONOWNED)); } 00181 00182 /// Return TRUE if subitems, real or imagined 00183 FXbool hasItems() const { return (state&HASITEMS)!=0; } 00184 00185 /// Change has items flag 00186 void setHasItems(FXbool flag); 00187 00188 /// Return true if descendent of parent item 00189 FXbool isChildOf(const FXTreeItem* item) const; 00190 00191 /// Return true if ancestor of child item 00192 FXbool isParentOf(const FXTreeItem* item) const; 00193 00194 /// Return width of item as drawn in list 00195 virtual FXint getWidth(const FXTreeList* list) const; 00196 00197 /// Return height of item as drawn in list 00198 virtual FXint getHeight(const FXTreeList* list) const; 00199 00200 /// Create server-side resources 00201 virtual void create(); 00202 00203 /// Detach server-side resources 00204 virtual void detach(); 00205 00206 /// Destroy server-side resources 00207 virtual void destroy(); 00208 00209 /// Save to stream 00210 virtual void save(FXStream& store) const; 00211 00212 /// Load from stream 00213 virtual void load(FXStream& store); 00214 00215 /// Destroy item and free icons if owned 00216 virtual ~FXTreeItem(); 00217 }; 00218 00219 00220 00221 /// Tree item collate function 00222 typedef FXint (*FXTreeListSortFunc)(const FXTreeItem*,const FXTreeItem*); 00223 00224 00225 00226 /** 00227 * A Tree List Widget organizes items in a hierarchical, tree-like fashion. 00228 * Subtrees can be collapsed or expanded by double-clicking on an item 00229 * or by clicking on the optional plus button in front of the item. 00230 * Each item may have a text and optional open-icon as well as a closed-icon. 00231 * The items may be connected by optional lines to show the hierarchical 00232 * relationship. 00233 * When an item's selected state changes, the treelist emits a SEL_SELECTED 00234 * or SEL_DESELECTED message. If an item is opened or closed, a message 00235 * of type SEL_OPENED or SEL_CLOSED is sent. When the subtree under an 00236 * item is expanded, a SEL_EXPANDED or SEL_COLLAPSED message is issued. 00237 * A change of the current item is signified by the SEL_CHANGED message. 00238 * In addition, the tree list sends SEL_COMMAND messages when the user 00239 * clicks on an item, and SEL_CLICKED, SEL_DOUBLECLICKED, and SEL_TRIPLECLICKED 00240 * when the user clicks once, twice, or thrice, respectively. 00241 * When items are added or removed, the tree list sends messages of the 00242 * type SEL_INSERTED or SEL_DELETED. 00243 * In each of these cases, a pointer to the item, if any, is passed in the 00244 * 3rd argument of the message. 00245 */ 00246 class FXAPI FXTreeList : public FXScrollArea { 00247 FXDECLARE(FXTreeList) 00248 protected: 00249 FXTreeItem *firstitem; // First root item 00250 FXTreeItem *lastitem; // Last root item 00251 FXTreeItem *anchoritem; // Selection anchor item 00252 FXTreeItem *currentitem; // Current item 00253 FXTreeItem *extentitem; // Selection extent 00254 FXTreeItem *cursoritem; // Item under cursor 00255 FXFont *font; // Font 00256 FXTreeListSortFunc sortfunc; // Item sort function 00257 FXColor textColor; // Text color 00258 FXColor selbackColor; // Selected background color 00259 FXColor seltextColor; // Selected text color 00260 FXColor lineColor; // Line color 00261 FXint treeWidth; // Tree width 00262 FXint treeHeight; // Tree height 00263 FXint visible; // Number of visible items 00264 FXint indent; // Parent to child indentation 00265 FXint grabx; // Grab point x 00266 FXint graby; // Grab point y 00267 FXString lookup; // Lookup string 00268 FXString help; // Help string 00269 FXbool state; // State of item 00270 protected: 00271 FXTreeList(); 00272 virtual FXTreeItem* createItem(const FXString& text,FXIcon* oi,FXIcon* ci,void* ptr); 00273 void sort(FXTreeItem*& f1,FXTreeItem*& t1,FXTreeItem*& f2,FXTreeItem*& t2,int n); 00274 void recompute(); 00275 private: 00276 FXTreeList(const FXTreeList&); 00277 FXTreeList& operator=(const FXTreeList&); 00278 public: 00279 long onPaint(FXObject*,FXSelector,void*); 00280 long onEnter(FXObject*,FXSelector,void*); 00281 long onLeave(FXObject*,FXSelector,void*); 00282 long onUngrabbed(FXObject*,FXSelector,void*); 00283 long onMotion(FXObject*,FXSelector,void*); 00284 long onKeyPress(FXObject*,FXSelector,void*); 00285 long onKeyRelease(FXObject*,FXSelector,void*); 00286 long onLeftBtnPress(FXObject*,FXSelector,void*); 00287 long onLeftBtnRelease(FXObject*,FXSelector,void*); 00288 long onRightBtnPress(FXObject*,FXSelector,void*); 00289 long onRightBtnRelease(FXObject*,FXSelector,void*); 00290 long onQueryTip(FXObject*,FXSelector,void*); 00291 long onQueryHelp(FXObject*,FXSelector,void*); 00292 long onTipTimer(FXObject*,FXSelector,void*); 00293 long onFocusIn(FXObject*,FXSelector,void*); 00294 long onFocusOut(FXObject*,FXSelector,void*); 00295 long onAutoScroll(FXObject*,FXSelector,void*); 00296 long onClicked(FXObject*,FXSelector,void*); 00297 long onDoubleClicked(FXObject*,FXSelector,void*); 00298 long onTripleClicked(FXObject*,FXSelector,void*); 00299 long onCommand(FXObject*,FXSelector,void*); 00300 long onLookupTimer(FXObject*,FXSelector,void*); 00301 public: 00302 static FXint ascending(const FXTreeItem*,const FXTreeItem*); 00303 static FXint descending(const FXTreeItem*,const FXTreeItem*); 00304 static FXint ascendingCase(const FXTreeItem*,const FXTreeItem*); 00305 static FXint descendingCase(const FXTreeItem*,const FXTreeItem*); 00306 public: 00307 enum { 00308 ID_LOOKUPTIMER=FXScrollArea::ID_LAST, 00309 ID_LAST 00310 }; 00311 public: 00312 00313 /// Construct a new, initially empty tree list 00314 FXTreeList(FXComposite *p,FXObject* tgt=NULL,FXSelector sel=0,FXuint opts=TREELIST_NORMAL,FXint x=0,FXint y=0,FXint w=0,FXint h=0); 00315 00316 /// Create server-side resources 00317 virtual void create(); 00318 00319 /// Detach server-side resources 00320 virtual void detach(); 00321 00322 /// Perform layout 00323 virtual void layout(); 00324 00325 /// Return default width 00326 virtual FXint getDefaultWidth(); 00327 00328 /// Return default height 00329 virtual FXint getDefaultHeight(); 00330 00331 /// Compute and return content width 00332 virtual FXint getContentWidth(); 00333 00334 /// Return content height 00335 virtual FXint getContentHeight(); 00336 00337 /// Recalculate layout 00338 virtual void recalc(); 00339 00340 /// Tree list can receive focus 00341 virtual FXbool canFocus() const; 00342 00343 /// Move the focus to this window 00344 virtual void setFocus(); 00345 00346 /// Remove the focus from this window 00347 virtual void killFocus(); 00348 00349 /// Return number of items 00350 FXint getNumItems() const; 00351 00352 /// Return number of visible items 00353 FXint getNumVisible() const { return visible; } 00354 00355 /// Change number of visible items 00356 void setNumVisible(FXint nvis); 00357 00358 /// Return first root item 00359 FXTreeItem* getFirstItem() const { return firstitem; } 00360 00361 /// Return last root item 00362 FXTreeItem* getLastItem() const { return lastitem; } 00363 00364 /// Prepend new [possibly subclassed] item as first child of p 00365 FXTreeItem* addItemFirst(FXTreeItem* p,FXTreeItem* item,FXbool notify=FALSE); 00366 00367 /// Prepend new item with given text and optional icon, and user-data pointer as first child of p 00368 FXTreeItem* addItemFirst(FXTreeItem* p,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE); 00369 00370 /// Append new [possibly subclassed] item as last child of p 00371 FXTreeItem* addItemLast(FXTreeItem* p,FXTreeItem* item,FXbool notify=FALSE); 00372 00373 /// Append new item with given text and optional icon, and user-data pointer as last child of p 00374 FXTreeItem* addItemLast(FXTreeItem* p,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE); 00375 00376 /// Append new [possibly subclassed] item after to other item 00377 FXTreeItem* addItemAfter(FXTreeItem* other,FXTreeItem* item,FXbool notify=FALSE); 00378 00379 /// Append new item with given text and optional icon, and user-data pointer after to other item 00380 FXTreeItem* addItemAfter(FXTreeItem* other,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE); 00381 00382 /// Prepend new [possibly subclassed] item prior to other item 00383 FXTreeItem* addItemBefore(FXTreeItem* other,FXTreeItem* item,FXbool notify=FALSE); 00384 00385 /// Prepend new item with given text and optional icon, and user-data pointer prior to other item 00386 FXTreeItem* addItemBefore(FXTreeItem* other,const FXString& text,FXIcon* oi=NULL,FXIcon* ci=NULL,void* ptr=NULL,FXbool notify=FALSE); 00387 00388 /// Reparent item under parent p 00389 void reparentItem(FXTreeItem* item,FXTreeItem* p); 00390 00391 // Move item before other 00392 FXTreeItem* moveItemBefore(FXTreeItem* other,FXTreeItem* item); 00393 00394 // Move item after other 00395 FXTreeItem* moveItemAfter(FXTreeItem* other,FXTreeItem* item); 00396 00397 /// Remove item 00398 void removeItem(FXTreeItem* item,FXbool notify=FALSE); 00399 00400 /// Remove items in range [fm, to] inclusively 00401 void removeItems(FXTreeItem* fm,FXTreeItem* to,FXbool notify=FALSE); 00402 00403 /// Remove all items from list 00404 void clearItems(FXbool notify=FALSE); 00405 00406 /// Return item width 00407 FXint getItemWidth(const FXTreeItem* item) const { return item->getWidth(this); } 00408 00409 /// Return item height 00410 FXint getItemHeight(const FXTreeItem* item) const { return item->getHeight(this); } 00411 00412 /// Get item at x,y, if any 00413 FXTreeItem* getItemAt(FXint x,FXint y) const; 00414 00415 /** 00416 * Search items for item by name, starting from start item; the 00417 * flags argument controls the search direction, and case sensitivity. 00418 */ 00419 FXTreeItem* findItem(const FXString& text,FXTreeItem* start=NULL,FXuint flags=SEARCH_FORWARD|SEARCH_WRAP) const; 00420 00421 /// Scroll to make item visible 00422 void makeItemVisible(FXTreeItem* item); 00423 00424 /// Change item's text 00425 void setItemText(FXTreeItem* item,const FXString& text); 00426 00427 /// Return item's text 00428 FXString getItemText(const FXTreeItem* item) const; 00429 00430 /// Change item's open icon 00431 void setItemOpenIcon(FXTreeItem* item,FXIcon* icon); 00432 00433 /// Return item's open icon 00434 FXIcon* getItemOpenIcon(const FXTreeItem* item) const; 00435 00436 /// Chance item's closed icon 00437 void setItemClosedIcon(FXTreeItem* item,FXIcon* icon); 00438 00439 /// Return item's closed icon 00440 FXIcon* getItemClosedIcon(const FXTreeItem* item) const; 00441 00442 /// Change item user-data pointer 00443 void setItemData(FXTreeItem* item,void* ptr) const; 00444 00445 /// Return item user-data pointer 00446 void* getItemData(const FXTreeItem* item) const; 00447 00448 /// Return TRUE if item is selected 00449 FXbool isItemSelected(const FXTreeItem* item) const; 00450 00451 /// Return TRUE if item is current 00452 FXbool isItemCurrent(const FXTreeItem* item) const; 00453 00454 /// Return TRUE if item is visible 00455 FXbool isItemVisible(const FXTreeItem* item) const; 00456 00457 /// Return TRUE if item opened 00458 FXbool isItemOpened(const FXTreeItem* item) const; 00459 00460 /// Return TRUE if item expanded 00461 FXbool isItemExpanded(const FXTreeItem* item) const; 00462 00463 /// Return TRUE if item is a leaf-item, i.e. has no children 00464 FXbool isItemLeaf(const FXTreeItem* item) const; 00465 00466 /// Return TRUE if item is enabled 00467 FXbool isItemEnabled(const FXTreeItem* item) const; 00468 00469 /// Return item hit code: 0 outside, 1 icon, 2 text, 3 box 00470 FXint hitItem(const FXTreeItem* item,FXint x,FXint y) const; 00471 00472 /// Repaint item 00473 void updateItem(FXTreeItem* item) const; 00474 00475 /// Enable item 00476 FXbool enableItem(FXTreeItem* item); 00477 00478 /// Disable item 00479 FXbool disableItem(FXTreeItem* item); 00480 00481 /// Select item 00482 virtual FXbool selectItem(FXTreeItem* item,FXbool notify=FALSE); 00483 00484 /// Deselect item 00485 virtual FXbool deselectItem(FXTreeItem* item,FXbool notify=FALSE); 00486 00487 /// Toggle item selection 00488 virtual FXbool toggleItem(FXTreeItem* item,FXbool notify=FALSE); 00489 00490 /// Extend selection from anchor item to item 00491 virtual FXbool extendSelection(FXTreeItem* item,FXbool notify=FALSE); 00492 00493 /// Deselect all items 00494 virtual FXbool killSelection(FXbool notify=FALSE); 00495 00496 /// Open item 00497 virtual FXbool openItem(FXTreeItem* item,FXbool notify=FALSE); 00498 00499 /// Close item 00500 virtual FXbool closeItem(FXTreeItem* item,FXbool notify=FALSE); 00501 00502 /// Collapse tree 00503 virtual FXbool collapseTree(FXTreeItem* tree,FXbool notify=FALSE); 00504 00505 /// Expand tree 00506 virtual FXbool expandTree(FXTreeItem* tree,FXbool notify=FALSE); 00507 00508 /// Change current item 00509 virtual void setCurrentItem(FXTreeItem* item,FXbool notify=FALSE); 00510 00511 /// Return current item, if any 00512 FXTreeItem* getCurrentItem() const { return currentitem; } 00513 00514 /// Change anchor item 00515 void setAnchorItem(FXTreeItem* item); 00516 00517 /// Return anchor item, if any 00518 FXTreeItem* getAnchorItem() const { return anchoritem; } 00519 00520 /// Return item under cursor, if any 00521 FXTreeItem* getCursorItem() const { return cursoritem; } 00522 00523 /// Sort all items recursively 00524 void sortItems(); 00525 00526 /// Sort root items 00527 void sortRootItems(); 00528 00529 /// Sort children of item 00530 void sortChildItems(FXTreeItem* item); 00531 00532 /// Return sort function 00533 FXTreeListSortFunc getSortFunc() const { return sortfunc; } 00534 00535 /// Change sort function 00536 void setSortFunc(FXTreeListSortFunc func){ sortfunc=func; } 00537 00538 /// Change text font 00539 void setFont(FXFont* fnt); 00540 00541 /// Return text font 00542 FXFont* getFont() const { return font; } 00543 00544 /// Change parent-child indent amount 00545 void setIndent(FXint in); 00546 00547 /// Return parent-child indent amount 00548 FXint getIndent() const { return indent; } 00549 00550 /// Return normal text color 00551 FXColor getTextColor() const { return textColor; } 00552 00553 /// Change normal text color 00554 void setTextColor(FXColor clr); 00555 00556 /// Return selected text background 00557 FXColor getSelBackColor() const { return selbackColor; } 00558 00559 /// Change selected text background 00560 void setSelBackColor(FXColor clr); 00561 00562 /// Return selected text color 00563 FXColor getSelTextColor() const { return seltextColor; } 00564 00565 /// Change selected text color 00566 void setSelTextColor(FXColor clr); 00567 00568 /// Return line color 00569 FXColor getLineColor() const { return lineColor; } 00570 00571 /// Change line color 00572 void setLineColor(FXColor clr); 00573 00574 /// Return list style 00575 FXuint getListStyle() const; 00576 00577 /// Change list style 00578 void setListStyle(FXuint style); 00579 00580 /// Set the status line help text for this list 00581 void setHelpText(const FXString& text); 00582 00583 /// Get the status line help text for this list 00584 FXString getHelpText() const { return help; } 00585 00586 /// Save object to a stream 00587 virtual void save(FXStream& store) const; 00588 00589 /// Load object from a stream 00590 virtual void load(FXStream& store); 00591 00592 /// Destructor 00593 virtual ~FXTreeList(); 00594 }; 00595 00596 } 00597 00598 #endif

Copyright © 1997-2004 Jeroen van der Zijp