gcsx_treeview.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* GCSx
  2. ** TREEVIEW.H
  3. **
  4. ** Treeview window, for browsing worlds, libraries, etc.
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_TREEVIEW_H_
  24. #define __GCSx_TREEVIEW_H_
  25. // Treeview window
  26. class TreeView : public Window {
  27. protected:
  28. int open;
  29. // We use 'parent' for TreeView above us, if there is one
  30. // But this will be true
  31. int parentIsTree;
  32. // (or our frame window, so check type of parent)
  33. FrameWindow* myFrame;
  34. class WidgetScroll* myScroll;
  35. // Set to hide our top-most item; typically used on top level if appropriate
  36. int hideItem;
  37. // Starts with a top-most item
  38. std::string item;
  39. std::string sort; // item in lowercase, for sorting
  40. int itemWidth;
  41. int isCaseSensitive;
  42. int iconClosed;
  43. int iconOpen;
  44. // Can contain a list of items below it (which can be empty)
  45. int subitems;
  46. int expanded;
  47. int currentSelection; // -1 means no selection, 0 means us, 1+ means a subitem (cascades)
  48. std::vector<TreeView*> subtree;
  49. // ...and/or an action (function) to perform for events
  50. // Should expect LV_* commands or any standard command from a menu
  51. // Won't get dblclicks if has subitems
  52. // if check is true, we're just checking if command is supported return CommandSupport
  53. // otherwise return value is true/false whether command was used
  54. void* ptr;
  55. int code;
  56. int (*action)(void* ptr, int code, int command, int check);
  57. // Size and positioning of icons for each item (0 = +/-, 1 = graphical)
  58. static int iconX[2];
  59. static int iconY[2];
  60. static int iconWidth[2];
  61. static int iconHeight[2];
  62. static int iconIndent[2];
  63. // Size of a single item
  64. static int itemHeight;
  65. static int itemYOffset;
  66. // Icons
  67. static const std::string wtButtonDown;
  68. static const std::string wtButtonRight;
  69. static const std::string resourceError;
  70. static long int iconsLoaded;
  71. static SDL_Surface* iconSurface;
  72. enum {
  73. // Size of treeview icons (not the +/- kind)
  74. GUI_TREEVIEW_ICON_WIDTH = 16,
  75. GUI_TREEVIEW_ICON_HEIGHT = 16,
  76. // L/R padding for both icons
  77. GUI_TREEVIEW_ICON_PADDING = 2,
  78. // These only apply when used as a widget!
  79. // This is the number of X characters to fit in a standard width
  80. GUI_TREEVIEW_DEFAULTWIDTH = 30,
  81. // Default number of lines to show
  82. GUI_TREEVIEW_DEFAULTLINES = 20,
  83. };
  84. // Child items alert their parent when they expand/contract/add/remove items/rename
  85. public:
  86. void childResized(int fromW, int fromH, int toW, int toH, Window* child);
  87. protected:
  88. void childRenamed(TreeView* child);
  89. // Refreshes positions of all children without dirty anything (if we need to scrollToView)
  90. void positionChildren(int rescroll);
  91. // Selects first matching subitem of ours, recursively, as expanded
  92. // Returns true if found
  93. int selectFind(int letter);
  94. // Selects last subitem of ours, recursively, as expanded
  95. void selectLast();
  96. // A subitem is telling us they are selected
  97. void selectChild(TreeView* child);
  98. // We are now unselected, including any subitems
  99. // Always called by parent, only propogates downward
  100. void unselectMe();
  101. // Find position of child (0-based)
  102. int findChild(const TreeView* child) const;
  103. // Propogates upward through parents to upper frame window
  104. void scrollToView(int sX, int sY, int sWidth, int sHeight);
  105. public:
  106. // Set hidden for top level item ONLY- if on anything else, it impacts keyboard support
  107. // Case sensitive affects sorting and find() by string
  108. TreeView(const std::string& name, void* tPtr = NULL, int tCode = 0, int (*tAction)(void* ptr, int code, int command, int check) = NULL, int hideTop = 0, int caseSensitive = 0);
  109. virtual ~TreeView();
  110. // If you change name, it will reorder itself within it's parent
  111. void changeName(const std::string& name);
  112. void setIcon(int tIconClosed, int tIconOpen = -1);
  113. int getItemHeight() const { return itemHeight; }
  114. int event(int hasFocus, const SDL_Event* event);
  115. void display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset);
  116. WindowType windowType() const;
  117. void resolutionChange(int fromW, int fromH, int fromBpp, int toW, int toH, int toBpp);
  118. CommandSupport supportsCommand(int cmdCode) const;
  119. // By default, treeviews do NOT want to be deleted, they are static views you can
  120. // close then reopen later as-is
  121. int wantsToBeDeleted() const;
  122. // Insert an item "under" us; owns pointer; sorts automatically
  123. void insert(TreeView* toInsert, int makeCurrent = 1);
  124. // Find a node (subnode only- won't find self)
  125. TreeView* find(void* fPtr);
  126. TreeView* findRecursive(void* fPtr);
  127. TreeView* find(const std::string& fItem);
  128. TreeView* findRecursive(int fCode, int (*fAction)(void* ptr, int code, int command, int check) = NULL);
  129. TreeView* findParent();
  130. TreeView* findSelected();
  131. int getCode() const { return code; }
  132. // Find and remove an item (and DELETE it, normally)
  133. void remove(TreeView* toRemove, int deleteIt = 1);
  134. void removeAll(); // Deletes all
  135. // Do we have subitems? Expand or collapse
  136. // Derived versions can create their subtrees on the fly
  137. virtual int hasSubItems() const; // Only checked if subtree is NOT expanded
  138. virtual void expand(int state = 1);
  139. // Select this item
  140. void selectMe();
  141. // Select first subitem but only if expanded
  142. void selectFirst();
  143. // Creates a FrameWindow, adds self to desktop
  144. void runWindowed();
  145. // Adds self to dialog using a WScroll
  146. void addTo(class Dialog* dialog, int showWidth = -1, int showLines = -1, int cId = 0);
  147. // Are we open?
  148. int isOpen() const { return open; }
  149. };
  150. #endif