ListBox.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // ListBox.H
  21. //
  22. // History:
  23. // 01/13/97 JMI Started.
  24. //
  25. // 01/15/97 JMI Added overrides of base class's Save/LoadChildren() to
  26. // implement special cases for m_sbVert, Horz, &
  27. // m_frmContents.
  28. //
  29. // 01/21/97 JMI Added ReadMembers() and WriteMembers() overloads to read
  30. // and write members of this class.
  31. //
  32. // 01/21/97 JMI Changed m_frmContents (RFrame) to m_lcContents
  33. // (RListContents).
  34. //
  35. // 02/05/97 JMI Added m_typeEncapsulator, the type of GUI item that
  36. // AddString() adds.
  37. //
  38. // 02/05/97 JMI Now overrides OnLoseChild() to deselect the child, if
  39. // it is the currently selected item.
  40. //
  41. // 05/25/97 JMI Added GetFirst(), GetNext(), and GetPrev().
  42. //
  43. // 07/04/97 JMI Added IsListItem().
  44. //
  45. // 09/01/97 JMI Added MakeEncapsulator().
  46. //
  47. // 09/07/97 JMI Now EnsureVisible() accepts an optional position
  48. // preference so you can bias the location of your item.
  49. //
  50. // 09/22/97 JMI Also, added friend class CListBoxPropPage for GUI
  51. // editor.
  52. //
  53. //////////////////////////////////////////////////////////////////////////////
  54. //
  55. // See CPP for description.
  56. //
  57. //////////////////////////////////////////////////////////////////////////////
  58. #ifndef LISTBOX_H
  59. #define LISTBOX_H
  60. //////////////////////////////////////////////////////////////////////////////
  61. // Headers.
  62. //////////////////////////////////////////////////////////////////////////////
  63. #include "System.h"
  64. // If PATHS_IN_INCLUDES macro is defined, we can utilized relative
  65. // paths to a header file. In this case we generally go off of our
  66. // RSPiX root directory. System.h MUST be included before this macro
  67. // is evaluated. System.h is the header that, based on the current
  68. // platform (or more so in this case on the compiler), defines
  69. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  70. // instead.
  71. #ifdef PATHS_IN_INCLUDES
  72. #include "ORANGE/GUI/guiItem.h"
  73. #include "ORANGE/GUI/scrollbar.h"
  74. #include "ORANGE/GUI/ListContents.h"
  75. #else
  76. #include "GuiItem.h"
  77. #include "ScrollBar.h"
  78. #include "ListContents.h"
  79. #endif // PATHS_IN_INCLUDES
  80. //////////////////////////////////////////////////////////////////////////////
  81. // Macros.
  82. //////////////////////////////////////////////////////////////////////////////
  83. // Prop indicating this is an ecapsulator item that should be freed when
  84. // done.
  85. #define ENCAPSULATOR_PROP_KEY 8817473
  86. //////////////////////////////////////////////////////////////////////////////
  87. // Typedefs.
  88. //////////////////////////////////////////////////////////////////////////////
  89. //////////////////////////////////////////////////////////////////////////////
  90. class RListBox : public RGuiItem
  91. {
  92. public: // Construction/Destruction.
  93. // Default constructor.
  94. RListBox(void);
  95. // Destructor.
  96. ~RListBox(void);
  97. public: // Typedefs.
  98. ////////////////////////////////////////////////////////////////////////
  99. // Typedefs.
  100. ////////////////////////////////////////////////////////////////////////
  101. typedef enum // State defining visibility of listbox scrollbars.
  102. {
  103. Hidden, // The scrollbar will remain hidden.
  104. Shown, // The scrollbar will remain shown.
  105. ShownAsNeeded // The scrollbar will be shown when necessary and hidden
  106. // when not necessary.
  107. } ScrollBarVisibility;
  108. typedef enum
  109. {
  110. Top, // Toward the top of the listbox.
  111. Middle, // Toward the middle of the listbox.
  112. Bottom // Toward the bottom of the listbox.
  113. } Position;
  114. //////////////////////////////////////////////////////////////////////////////
  115. public: // Methods.
  116. ////////////////////////////////////////////////////////////////////////
  117. // Methods.
  118. ////////////////////////////////////////////////////////////////////////
  119. // Creates a displayable Gui. Call SetFont and SetText before calling
  120. // this as it calls Compose.
  121. virtual // If you override this, call this base if possible.
  122. short Create( // Returns 0 on success.
  123. short sX, // X position relative to "parent" item.
  124. short sY, // Y position relative to "parent" item.
  125. short sW, // Width.
  126. short sH, // Height.
  127. short sDepth); // Color depth.
  128. // Destroys dynamic display data.
  129. virtual // If you override this, call this base if possible.
  130. void Destroy(void) // Returns nothing.
  131. {
  132. // Call built-in children.
  133. m_sbVert.Destroy();
  134. m_sbHorz.Destroy();
  135. m_lcContents.Destroy();
  136. // Call base class.
  137. RGuiItem::Destroy();
  138. }
  139. // Compose item.
  140. virtual // If you override this, call this base if possible.
  141. void Compose( // Returns nothing.
  142. RImage* pim = NULL); // Dest image, uses m_im if NULL.
  143. // Add a string into the list box.
  144. // Note that adding a string at the end is by far the fastest.
  145. RGuiItem* AddString( // Returns new GUI item on success.
  146. char* pszString, // String to add.
  147. RGuiItem* pguiAfter = NULL); // Gui to add after or NULL to add at
  148. // end.
  149. // Insert a string into the list box.
  150. // Note that adding a string at the end is by far the fastest.
  151. RGuiItem* InsertString( // Returns new GUI item on success.
  152. char* pszString, // String to insert.
  153. RGuiItem* pguiBefore = NULL); // Gui to insert before or NULL to
  154. // insert at beginning.
  155. // Add an item into the list box.
  156. // Note that adding an item at the end is by far the fastest.
  157. RGuiItem* AddItem( // Returns new GUI item or pgui on
  158. // success. Depends on sEncapsulate.
  159. RGuiItem* pgui, // GUI item to insert.
  160. short sEncapsulate = FALSE, // If TRUE, this item will be
  161. // encapsulated in an RGuiItem that
  162. // will be returned on success.
  163. // If FALSE, this item will be a direct
  164. // child of the listbox and will be
  165. // returned on success.
  166. RGuiItem* pguiAfter = NULL); // Gui to add after or NULL to add at
  167. // end.
  168. // Note that adding an item at the end is by far the fastest.
  169. // Insert an item into the list box.
  170. RGuiItem* InsertItem( // Returns new GUI item or pgui on
  171. // success. Depends on sEncapsulate.
  172. RGuiItem* pgui, // GUI item to insert.
  173. short sEncapsulate = FALSE, // If TRUE, this item will be
  174. // encapsulated in an RGuiItem that
  175. // will be returned on success.
  176. // If FALSE, this item will be a direct
  177. // child of the listbox and will be
  178. // returned on success.
  179. RGuiItem* pguiBefore = NULL); // Gui to insert before or NULL to
  180. // insert at beginning.
  181. // Remove an item from the list box and destroy encapsulator, if an
  182. // encapsulator exists for that item.
  183. void RemoveItem( // Returns nothing.
  184. RGuiItem* pgui); // Item to remove. NOTE: Any
  185. // encapsulating GUI item will be
  186. // destroyed.
  187. // Remove all items and encapsulators in listbox.
  188. // Calls RemoveItem() for each item.
  189. void RemoveAll(void); // Returns nothing.
  190. // Ensure the specified item is within the client of the listbox.
  191. void EnsureVisible( // Returns nothing.
  192. RGuiItem* pgui, // Item to ensure visibility of.
  193. Position posPreference = Top); // In: Preferred position.
  194. // Set the selection to the specified GUI item.
  195. void SetSel( // Returns nothing.
  196. RGuiItem* pgui); // Item to select or NULL for none.
  197. // Set scroll visibility state for vertical scrollbar.
  198. void SetVScrollVisibility( // Returns nothing.
  199. ScrollBarVisibility sbv) // Visibility for vertical scrollbar
  200. // (See typedef for ScrollBarVisibility).
  201. {
  202. // Set new state.
  203. m_sbvVert = sbv;
  204. // Update visibility.
  205. UpdateScrollBarVisibility();
  206. }
  207. // Set scroll visibility state for horizontal scrollbar.
  208. void SetHScrollVisibility( // Returns nothing.
  209. ScrollBarVisibility sbv) // Visibility for horizontal scrollbar
  210. // (See typedef for ScrollBarVisibility).
  211. {
  212. // Set new state.
  213. m_sbvHorz = sbv;
  214. // Update visibility.
  215. UpdateScrollBarVisibility();
  216. }
  217. // Update scrollbar visibility based on current listbox contents and
  218. // their visibility.
  219. void UpdateScrollBarVisibility(void); // Returns nothing.
  220. // Resize encapsulators to fit the largest encapsulated item or string,
  221. // reposition all items to appear in the correct order, resize scrollable
  222. // area (m_guiContents) to fit all listbox items, and update scrollbar
  223. // ranges accordingly.
  224. void AdjustContents(void); // Returns nothing.
  225. // Update scrollbars' positions. This function is called by
  226. // AdjustContents() and EnsureVisible(), so there is no need to call it
  227. // after calling one of those functions.
  228. void UpdateScrollBars(void);
  229. // Called by ListContents() when it is losing a child item.
  230. virtual // Overridden here.
  231. void OnLoseChild( // Returns nothing.
  232. RGuiItem* pguiChild) // Child item we're about to lose.
  233. {
  234. // If this is the selected item . . .
  235. if (pguiChild == m_pguiSel)
  236. {
  237. // Unselect.
  238. SetSel(NULL);
  239. }
  240. }
  241. ////////////////////////////////////////////////////////////////////////
  242. // Querries.
  243. ////////////////////////////////////////////////////////////////////////
  244. // Get the currently selected GUI item.
  245. RGuiItem* GetSel(void) // Returns currently selected GUI item
  246. // or NULL, if none.
  247. {
  248. return m_pguiSel;
  249. }
  250. // Get the first child item.
  251. RGuiItem* GetFirst(void); // Returns the first child item or NULL.
  252. // Get the child item after the specified item.
  253. RGuiItem* GetNext( // Returns the next child item or NULL.
  254. RGuiItem* pgui); // In: Child item that precedes the next.
  255. // Get the child item before the specified item.
  256. RGuiItem* GetPrev( // Returns the previous child item or NULL.
  257. RGuiItem* pgui); // In: Child item that follows the prev.
  258. // Returns TRUE, if the specified item is a list item contained within
  259. // this listbox; FALSE otherwise.
  260. short IsListItem(
  261. RGuiItem* pguiListItem) // In: Item to check.
  262. {
  263. return m_lcContents.IsChild(pguiListItem);
  264. }
  265. //////////////////////////////////////////////////////////////////////////////
  266. public: // Static.
  267. // Determines if the specified item is an encapsulator item.
  268. static short IsEncapsulator( // Returns TRUE, if encapsulator item;
  269. // FALSE, otherwise.
  270. RGuiItem* pgui) // In: Item in question.
  271. {
  272. // Return encapsulation status.
  273. return (short)pgui->IsProp(ENCAPSULATOR_PROP_KEY);
  274. }
  275. // Makes the specified item an encapsulator item.
  276. static void MakeEncapsulator( // Returns nothing.
  277. RGuiItem* pgui) // In: Item to make an encapsulator item.
  278. {
  279. pgui->SetProp(ENCAPSULATOR_PROP_KEY, TRUE);
  280. }
  281. // Callback from child items that calls instantiated version.
  282. static void PressedCall( // Returns nothing.
  283. RGuiItem* pgui) // Gui item pressed.
  284. {
  285. // Call RListBox instantiated.
  286. ((RListBox*)(pgui->m_ulUserInstance))->SetSel(pgui);
  287. }
  288. // Callback from scrollbar telling us to update list position.
  289. static void ScrollCall( // Returns nothing.
  290. RScrollBar* psb) // Scroll bar.
  291. {
  292. RListBox* plb = (RListBox*)psb->m_ulUserInstance;
  293. ASSERT(plb != NULL);
  294. short sPos = psb->GetPos();
  295. if (psb->m_oOrientation == RScrollBar::Vertical)
  296. {
  297. // Move vertically.
  298. plb->m_lcContents.Move(
  299. plb->m_lcContents.m_sX,
  300. -sPos);
  301. }
  302. else
  303. {
  304. // Move horizontally.
  305. plb->m_lcContents.Move(
  306. -sPos,
  307. plb->m_lcContents.m_sY);
  308. }
  309. }
  310. //////////////////////////////////////////////////////////////////////////////
  311. public: // Querries.
  312. //////////////////////////////////////////////////////////////////////////////
  313. protected: // Internal functions.
  314. // Creates an item appropriate for the specified string and makes it
  315. // a child of this listbox.
  316. RGuiItem* CreateStringItem( // Returns new item on success; NULL,
  317. // otherwise.
  318. char* pszString); // Text for new item.
  319. // Creates an encapsulator object for the specified GUI.
  320. RGuiItem* CreateEncapsulator( // Returns new item on success; NULL,
  321. // otherwise.
  322. RGuiItem* pgui); // Item to encapsulate.
  323. // Place item at specified location in list of container's child items.
  324. // Under new pretenses, this should not fail.
  325. void AddAfter( // Returns nothing.
  326. RGuiItem* pgui, // Item to add.
  327. RGuiItem* pguiAfter); // Item to add after or NULL to add at
  328. // end.
  329. // Place item at specified location in list of container's child items.
  330. // Under new pretenses, this should not fail.
  331. void InsertBefore( // Returns nothing.
  332. RGuiItem* pgui, // Item to insert.
  333. RGuiItem* pguiBefore); // Item to insert before or NULL to insert
  334. // at beginning.
  335. // [Un]Select the specified item.
  336. void SelectItem( // Returns nothing.
  337. RGuiItem* pguiSel, // Item to [un]select.
  338. short sSelect); // If TRUE, item is selected; if FALSE,
  339. // item is unselected.
  340. // Save item's children to the specified file.
  341. virtual // Overridden here.
  342. short SaveChildren( // Returns 0 on success.
  343. RFile* pfile); // File to save to.
  344. // Load item's children from the specified file.
  345. virtual // Overridden here.
  346. short LoadChildren( // Returns 0 on success.
  347. RFile* pfile); // File to load from.
  348. // Read item's members from file.
  349. virtual // Overridden here.
  350. short ReadMembers( // Returns 0 on success.
  351. RFile* pfile, // File to read from.
  352. U32 u32Version); // File format version to use.
  353. // Write item's members to file.
  354. virtual // Overridden here.
  355. short WriteMembers( // Returns 0 on success.
  356. RFile* pfile); // File to write to.
  357. //////////////////////////////////////////////////////////////////////////////
  358. public: // Member variables.
  359. // The scrollable region that owns the list items.
  360. RListContents m_lcContents;
  361. // The vertical scrollbar.
  362. RScrollBar m_sbVert;
  363. // The horizontal scrollbar.
  364. RScrollBar m_sbHorz;
  365. // The currently selected item or NULL.
  366. RGuiItem* m_pguiSel;
  367. // Width of largest list item currently in listbox.
  368. short m_sLargestWidth;
  369. // Scrollbar visibility state for vertical scrollbar.
  370. ScrollBarVisibility m_sbvVert;
  371. // Scrollbar visibility state for horizontal scrollbar.
  372. ScrollBarVisibility m_sbvHorz;
  373. // Type of GUI item added via AddString.
  374. Type m_typeEncapsulator;
  375. protected: // Internal typedefs.
  376. protected: // Protected member variables.
  377. ///////////////////////////////////////////////////////////////////////////
  378. // Friends.
  379. ///////////////////////////////////////////////////////////////////////////
  380. friend class CListBoxPropPage;
  381. };
  382. #endif // LISTBOX_H
  383. //////////////////////////////////////////////////////////////////////////////
  384. // EOF
  385. //////////////////////////////////////////////////////////////////////////////