1
0

treewnd.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #ifndef _TREEWND_H
  2. #define _TREEWND_H
  3. // BU: lots of changes
  4. // - all items must be deletable, and will be deleted on destructor
  5. // - root items list not allocated w/ new
  6. // - items set sorting within their PtrListSorted instead of manually calling it
  7. // - setting an item to auto-sort does *not* make subitems autosort too
  8. #include <api/wnd/wndclass/scbkgwnd.h>
  9. #include <bfc/ptrlist.h>
  10. #include <api/wnd/wndclass/editwnd.h>
  11. #include <bfc/common.h>
  12. #include <tataki/color/skinclr.h>
  13. #define TREEWND_PARENT ScrlBkgWnd
  14. #define STATUS_EXPANDED 0
  15. #define STATUS_COLLAPSED 1
  16. #define HITTEST_BEFORE 0
  17. #define HITTEST_IN 1
  18. #define HITTEST_AFTER 2
  19. #define LINK_RIGHT 1
  20. #define LINK_TOP 2
  21. #define LINK_BOTTOM 4
  22. #define TAB_NO FALSE
  23. #define TAB_YES TRUE
  24. #define TAB_AUTO 2
  25. #define WM_SETITEMDEFERRED WM_USER+6546
  26. #define DC_SETITEM 10
  27. #define DC_DELITEM 20
  28. #define DC_EXPAND 30
  29. #define DC_COLLAPSE 40
  30. // Forward references
  31. class TreeItemList;
  32. class TreeItem;
  33. class TreeWnd;
  34. class FontSize;
  35. // classes & structs
  36. class CompareTreeItem {
  37. public:
  38. static int compareItem(TreeItem *p1, TreeItem *p2);
  39. };
  40. class TreeItemList : public PtrListQuickSorted<TreeItem, CompareTreeItem> { };
  41. class TreeItem
  42. {
  43. friend class TreeWnd;
  44. public:
  45. TreeItem(const wchar_t *label=NULL);
  46. virtual ~TreeItem();
  47. virtual SkinBitmap *getIcon();
  48. virtual void setIcon(SkinBitmap *newicon);
  49. virtual void onTreeAdd() {}
  50. virtual void onTreeRemove() {}
  51. virtual void onChildItemRemove(TreeItem *item) {}
  52. // override this to keep from being selected
  53. virtual int isHitTestable() { return 1; }
  54. virtual void onSelect() {}
  55. virtual void onDeselect() {}
  56. virtual int onLeftDoubleClick() { return 0; }
  57. virtual int onRightDoubleClick() { return 0; }
  58. virtual int onContextMenu(int x, int y);
  59. virtual int onChar(UINT key) { return 0; } // return 1 if you eat the key
  60. // these are called after the expand/collapse happens
  61. virtual void onExpand() {}
  62. virtual void onCollapse() {}
  63. virtual int onBeginLabelEdit();
  64. virtual int onEndLabelEdit(const wchar_t *newlabel);
  65. virtual void setLabel(const wchar_t *label);
  66. virtual const wchar_t *getLabel();
  67. void setTip(const wchar_t *tip);
  68. const wchar_t *getTip();
  69. // override to draw by yourself. Return the width of what you've drawn
  70. virtual int customDraw(Canvas *canvas, const POINT &pt, int defaultTxtHeight, int indentation, const RECT &clientRect, const Wasabi::FontInfo *fontInfo);
  71. // return 0 to refuse being dragged
  72. // else return 1 and install the droptype and dropitem
  73. // also, write suggested title into suggestedTitle if any
  74. virtual int onBeginDrag(wchar_t *suggestedTitle) { return 0; }
  75. virtual int dragOver(ifc_window *sourceWnd) { return 0; }
  76. virtual int dragLeave(ifc_window *sourceWnd) { return 0; }
  77. virtual int dragDrop(ifc_window *sourceWnd) { return 0; }
  78. virtual int dragComplete(int success) { return 0; }
  79. void ensureVisible();
  80. TreeItem *getNthChild(int nth); // enumerates children (zero based)
  81. TreeItem *getChild();
  82. TreeItem *getChildSibling(TreeItem *item);
  83. TreeItem *getSibling();
  84. TreeItem *getParent();
  85. void editLabel();
  86. int getNumChildren();
  87. bool hasSubItems();
  88. void setSorted(int issorted);
  89. void setChildTab(int haschildtab);
  90. bool isSorted();
  91. bool isCollapsed();
  92. bool isExpanded();
  93. void invalidate();
  94. bool isSelected();
  95. bool isHilited();
  96. void setHilited(bool ishilited);
  97. int collapse();
  98. int expand();
  99. int getCurRect(RECT *r);
  100. void setCurrent(bool tf);
  101. TreeWnd *getTree() const;
  102. protected:
  103. bool isHilitedDrop();
  104. void setHilitedDrop(bool ishilitedDrop);
  105. void linkTo(TreeItem *linkto);
  106. // void childDeleted(TreeItem *child);
  107. void setTree(TreeWnd *newtree);
  108. void addSubItem(TreeItem *item);
  109. void setCurRect(int x1, int y1, int x2, int y2, int z);
  110. int getIndent();
  111. bool needTab();
  112. void sortItems(); // sorts the children of this item
  113. void setEdition(bool isedited);
  114. bool getEdition();
  115. private:
  116. void setSelected(bool isselected, bool expandCollapse=false, bool editifselected=false);
  117. // this really calls delete on the subitems
  118. void deleteSubitems();
  119. int removeSubitem(TreeItem *item);
  120. int getItemWidth(int txtHeight, int indentation);
  121. StringW label;
  122. class TreeItem *parent;
  123. TreeItemList subitems; // children
  124. RECT curRect;
  125. int childTab;
  126. TreeWnd *tree;
  127. int expandStatus;
  128. SkinBitmap *icon;
  129. int _z;
  130. StringW tooltip; // if empty, falls back to livetip
  131. bool selected:1;
  132. bool hilitedDrop:1;
  133. bool hilited:1;
  134. bool being_edited:1;
  135. };
  136. /**
  137. @short Tree-like view with leaf items.
  138. @ver 1.0
  139. @author Nullsoft
  140. @see TreeItem
  141. */
  142. class TreeWnd : public TREEWND_PARENT {
  143. friend class TreeItem;
  144. public:
  145. /**
  146. Sets up the default values for the TreeWnd. These defaults are
  147. auto collapse enabled and sets the TreeWnd bitmaps to the default Wasabi
  148. values.
  149. */
  150. TreeWnd();
  151. /**
  152. Deletes all the root items (including subitems).
  153. */
  154. virtual ~TreeWnd();
  155. /**
  156. Event is triggered when the button is about to be initialized.
  157. Override this event to implement your own behavior.
  158. @ret 1
  159. */
  160. virtual int onInit();
  161. /**
  162. Paints the bitmap on canvas according
  163. to current options (centering, tiling, stretching, title).
  164. @ret 0 for failure, 1 for success
  165. @param canvas The canvas on which to paint.
  166. */
  167. virtual int onPaint(Canvas *canvas);
  168. /**
  169. Notify a child window via a generic message system.
  170. @see addChild()
  171. @ret
  172. @param child A pointer to the child window which will receive the notify.
  173. @param msg The message you want to send to the child.
  174. @param p1 A user parameter.
  175. @param p2 A user parameter.
  176. */
  177. virtual int childNotify(ifc_window *child, int msg, intptr_t param1=0, intptr_t param2=0);
  178. /**
  179. Event triggered when the left mouse
  180. button is pressed over the TreeWnd.
  181. Override this to implement your own behavior.
  182. Default behavior is to stop editing a TreeItem label
  183. (if editing was occuring). Also will cause a collapse
  184. or expansion of the subitems if an item was previously
  185. selected.
  186. @ret 1, If you handle the event.
  187. @param x The X coordinate of the mouse.
  188. @param y The Y coordinate of the mouse.
  189. */
  190. virtual int onLeftButtonDown(int x, int y);
  191. /**
  192. Event is triggered when the left mouse button
  193. is released from a previously pressed state.
  194. Override this to implement your own behavior.
  195. @ret 1, If you handle the event.
  196. @param x The X coordinate of the mouse.
  197. @param y The Y coordinate of the mouse.
  198. */
  199. virtual int onLeftButtonUp(int x, int y);
  200. /**
  201. Event is triggered when the right mouse button
  202. is released from a previously pressed state.
  203. Override this to implement your own behavior.
  204. @ret 1, If you handle the event.
  205. @param x The X coordinate of the mouse.
  206. @param y The Y coordinate of the mouse.
  207. */
  208. virtual int onRightButtonUp(int x, int y);
  209. /**
  210. Event is triggered when the mouse is moved
  211. over the TreeWnd.
  212. Override this to implement your own behavior.
  213. Default is to handle drops (drag and drop).
  214. @ret 1, If you handle the event.
  215. @param x The X coordinate of the mouse.
  216. @param y The Y coordinate of the mouse.
  217. */
  218. virtual int onMouseMove(int x, int y);
  219. /**
  220. Do we want the context command menu to pop-up
  221. on right clicks?
  222. Default is no.
  223. @see ContextCmdI
  224. @ret 0, AutoContextMenu off; 1, AutoContextMenu on;
  225. */
  226. virtual int wantAutoContextMenu() { return 0; }
  227. /**
  228. Event is triggered when the left mouse button
  229. is double clicked and the cursor is over the
  230. TreeWnd.
  231. Default is to check if the doubleclick
  232. happened over an item, if it did, it calls
  233. the item's handler of this event.
  234. @ret 1, if you handle the event.
  235. @param x The X coordinate of the mouse.
  236. @param y The Y coordinate of the mouse.
  237. */
  238. virtual int onLeftButtonDblClk(int x, int y);
  239. /**
  240. Event is triggered when the right mouse button
  241. is double clicked and the cursor is over the
  242. TreeWnd.
  243. Default is to check if the doubleclick
  244. happened over an item, if it did, it calls
  245. the item's handler of this event.
  246. @ret 1, If you handle the event.
  247. @param x The X coordinate of the mouse.
  248. @param y The y coordinate of the mouse.
  249. */
  250. virtual int onRightButtonDblClk(int x, int y);
  251. /**
  252. Event is triggered when the mouse wheel
  253. is rolled up.
  254. Override this to implement your own behavior.
  255. Default is to scroll vertically as required.
  256. When the wheel is clicked and rolled, the
  257. TreeWnd is scrolled horizontally.
  258. @ret 1, If you handle the event.
  259. @param clicked The pushed state of the mouse wheel.
  260. @param lines The number of lines to scroll (or columns if horizontally scrolling).
  261. */
  262. virtual int onMouseWheelUp(int clicked, int lines);
  263. /**
  264. Event is triggered when the mouse wheel
  265. is rolled down.
  266. Override this to implement your own behavior.
  267. Default is to scroll vertically as required.
  268. When the wheel is clicked and rolled, the
  269. TreeWnd is scrolled horizontally.
  270. @ret 1, If you handle the event.
  271. @param clicked The pushed state of the mouse wheel.
  272. @param lines The number of lines to scroll (or columns if horizontally scrolling).
  273. */
  274. virtual int onMouseWheelDown(int clicked, int lines);
  275. /**
  276. */
  277. virtual void timerCallback(int c);
  278. /**
  279. Event is triggered when the right click occurs over
  280. the TreeWnd, but not on a TreeItem.
  281. Override this to implement your own behavior.
  282. @ret 1, If you handle the event.
  283. @param x The X coordinate of the mouse.
  284. @param y The Y coordinate of the mouse.
  285. */
  286. virtual int onContextMenu(int x, int y);
  287. // override and return 1 to abort calling context menu on item
  288. virtual int onPreItemContextMenu(TreeItem *item, int x, int y) { return 0; }
  289. // override to catch when item context menu complete
  290. virtual void onPostItemContextMenu(TreeItem *item, int x, int y, int retval) { }
  291. /**
  292. Event is triggered when a scheduled deferred callback
  293. occurs.
  294. Override this to implement your own behavior.
  295. @ret 1, If you handle this event; 0, If you do not handle this event;
  296. @param param1 Generic user paramater 1.
  297. @param param2 Generic user paramater 2.
  298. */
  299. virtual int onDeferredCallback(intptr_t param1, intptr_t param2);
  300. /**
  301. Event is triggered when a key is pressed
  302. and the TreeWnd has focus.
  303. Override this to implement your own behavior.
  304. @ret 1, If you handle the event.
  305. @param c The key that was pressed.
  306. */
  307. virtual int onChar(unsigned int c);
  308. /**
  309. Event is triggered when a key is pressed
  310. and the TreeWnd has focus.
  311. This method handles extended keys.
  312. @ret 1, If you handle the event.
  313. */
  314. virtual int onKeyDown(int keycode);
  315. /**
  316. */
  317. virtual void jumpToNext(wchar_t c);
  318. /**
  319. Verifies if the item received is in the
  320. viewable area of the TreeWnd. If not, it
  321. will make it visible by scrolling to the
  322. appropriate position.
  323. @param item A pointer to the item to verify.
  324. */
  325. void ensureItemVisible(TreeItem *item);
  326. // don't need to override this: just calls thru to the treeitem
  327. virtual int onBeginDrag(TreeItem *treeitem);
  328. virtual int dragEnter(ifc_window *sourceWnd);
  329. virtual int dragOver(int x, int y, ifc_window *sourceWnd);
  330. virtual int dragLeave(ifc_window *sourceWnd);
  331. virtual int dragDrop(ifc_window *sourceWnd, int x, int y);
  332. virtual int dragComplete(int success);
  333. int wantFocus() { return 1; }
  334. // override this if you want to control the item sort order
  335. virtual int compareItem(TreeItem *p1, TreeItem *p2);
  336. protected:
  337. // these will be called if the pointer is not over a treeitem
  338. virtual int defaultDragOver(int x, int y, ifc_window *sourceWnd) { return 0; }
  339. virtual int defaultDragDrop(ifc_window *sourceWnd, int x, int y) { return 0; }
  340. // called with item that received a drop
  341. virtual void onItemRecvDrop(TreeItem *item) {}
  342. virtual void onLabelChange(TreeItem *item) {}
  343. virtual void onItemSelected(TreeItem *item) {}
  344. virtual void onItemDeselected(TreeItem *item) {}
  345. virtual int onGetFocus();
  346. virtual int onKillFocus();
  347. public:
  348. virtual int getContentsWidth();
  349. virtual int getContentsHeight();
  350. void setRedraw(bool r);
  351. TreeItem *addTreeItem(TreeItem *item, TreeItem *par=NULL, int sorted=TRUE, int haschildtab=FALSE);
  352. // just removes a TreeItem from the tree, doesn't delete it... this is for
  353. // ~TreeItem to call only
  354. int removeTreeItem(TreeItem *item);
  355. void moveTreeItem(TreeItem *item, TreeItem *newparent);
  356. void deleteAllItems();
  357. int expandItem(TreeItem *item);
  358. void expandItemDeferred(TreeItem *item);
  359. int collapseItem(TreeItem *item);
  360. void collapseItemDeferred(TreeItem *item);
  361. void selectItem(TreeItem *item); // selects.
  362. void selectItemDeferred(TreeItem *item);// selects. posted.
  363. void delItemDeferred(TreeItem *item);
  364. void hiliteItem(TreeItem *item);
  365. void unhiliteItem(TreeItem *item);
  366. void setHilitedColor(const wchar_t *colorname);
  367. ARGB32 getHilitedColor();
  368. TreeItem *getCurItem();
  369. TreeItem *hitTest(POINT pos);
  370. TreeItem *hitTest(int x, int y);
  371. void editItemLabel(TreeItem *item);
  372. void cancelEditLabel(int destroyit=0);
  373. void setAutoEdit(int ae);
  374. int getAutoEdit();
  375. // use a NULL item to search all items. returns first item found
  376. TreeItem *getByLabel(TreeItem *item, const wchar_t *name);
  377. int getItemRect(TreeItem *item, RECT *r);
  378. int ownerDraw();
  379. int getNumRootItems();
  380. TreeItem *enumRootItem(int which);
  381. void setSorted(bool dosort);
  382. bool getSorted();
  383. void sortTreeItems();
  384. TreeItem *getSibling(TreeItem *item);
  385. TreeItem *getItemFromPoint(POINT *pt);
  386. void setAutoCollapse(bool doautocollapse);
  387. virtual int setFontSize(int newsize);
  388. int getFontSize();
  389. int getNumVisibleChildItems(TreeItem *c);
  390. int getNumVisibleItems();
  391. TreeItem *enumVisibleItems(int n);
  392. TreeItem *enumVisibleChildItems(TreeItem *c, int n);
  393. int findItem(TreeItem *i); // reverse
  394. int findChildItem(TreeItem *c, TreeItem *i, int *n);
  395. TreeItem *enumAllItems(int n); // unsorted
  396. void onSelectItem(TreeItem *i);
  397. void onDeselectItem(TreeItem *i);
  398. protected:
  399. void hiliteDropItem(TreeItem *item);
  400. void unhiliteDropItem(TreeItem *item);
  401. void invalidateMetrics();
  402. private:
  403. TreeItemList items; // root-level stuff
  404. PtrList<TreeItem> all_items; // unsorted
  405. TreeItem *curSelected;
  406. BltCanvas *dCanvas;
  407. void drawItems(Canvas *c, const Wasabi::FontInfo *fontInfo);
  408. void setCurItem(TreeItem *item, bool expandCollapse=true, bool editifselected=false);
  409. void countSubItems(PtrList<TreeItem> &drawlist, TreeItemList *list, int indent, int *c, int *m, int z);
  410. void getMetrics(int *numItemsShow, int *maxWidth);
  411. void ensureMetricsValid();
  412. int getLinkLine(TreeItem *item, int level);
  413. void endEditLabel(const wchar_t *newlabel);
  414. void editUpdate();
  415. int jumpToNextSubItems(TreeItemList *list, wchar_t c);
  416. int itemHeight;
  417. AutoSkinBitmap tabClosed, tabOpen;
  418. AutoSkinBitmap linkTopRight, linkTopBottom, linkTopRightBottom;
  419. AutoSkinBitmap linkTabTopRight, linkTabTopBottom, linkTabTopRightBottom;
  420. TreeItem *firstItemVisible;
  421. TreeItem *lastItemVisible;
  422. TreeItem *mousedown_item, *prevbdownitem;
  423. POINT mousedown_anchor;
  424. bool mousedown_dragdone;
  425. TreeItem *hitItem, // the dest item
  426. *draggedItem; // the source item
  427. int inHitTest;
  428. bool metrics_ok;
  429. int maxWidth;
  430. int maxHeight;
  431. StringW defaultTip;
  432. const wchar_t *getLiveTip();
  433. void setLiveTip(const wchar_t *tip);
  434. TreeItem *tipitem;
  435. bool redraw;
  436. PtrList<TreeItem> drawList;
  437. TreeItem *edited;
  438. EditWnd *editwnd;
  439. wchar_t editbuffer[256];
  440. int deleteItems;
  441. bool firstFound;
  442. TreeItem *currentItem;
  443. StringW hilitedColorName;
  444. SkinColor hilitedColor;
  445. int autoedit;
  446. int autocollapse;
  447. int textsize;
  448. StringW oldtip;
  449. StringW accValue;
  450. };
  451. template<class T> class TreeItemParam : public TreeItem {
  452. public:
  453. TreeItemParam(T _param, const wchar_t *label=NULL) : TreeItem(label) { param = _param; }
  454. T getParam() { return param; }
  455. operator T() { return getParam(); }
  456. private:
  457. T param;
  458. };
  459. #endif