guiListBoxCtrl.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GUI_LISTBOXCTRL_H_
  23. #define _GUI_LISTBOXCTRL_H_
  24. #ifndef _CONSOLETYPES_H_
  25. #include "console/consoleTypes.h"
  26. #endif
  27. #ifndef _GUICONTROL_H_
  28. #include "gui/guiControl.h"
  29. #endif
  30. #ifndef _DGL_H_
  31. #include "graphics/dgl.h"
  32. #endif
  33. #ifndef _H_GUIDEFAULTCONTROLRENDER_
  34. #include "gui/guiDefaultControlRender.h"
  35. #endif
  36. #ifndef _GUISCROLLCTRL_H_
  37. #include "gui/containers/guiScrollCtrl.h"
  38. #endif
  39. class GuiListBoxCtrl : public GuiControl
  40. {
  41. private:
  42. typedef GuiControl Parent;
  43. public:
  44. GuiListBoxCtrl();
  45. ~GuiListBoxCtrl();
  46. DECLARE_CONOBJECT(GuiListBoxCtrl);
  47. class LBItem
  48. {
  49. public:
  50. LBItem() : itemText(StringTable->EmptyString), isSelected(0), isActive(1), ID(0), itemData(nullptr), color(ColorF()), hasColor(0) { }
  51. virtual ~LBItem() { }
  52. StringTableEntry itemText;
  53. bool isSelected;
  54. bool isActive;
  55. S32 ID;
  56. void* itemData;
  57. ColorF color;
  58. bool hasColor;
  59. static bool sIncreasing;
  60. // Compare Functions
  61. static bool compByID(const LBItem *a, const LBItem *b)
  62. {
  63. bool res = a->ID < b->ID;
  64. return (sIncreasing ? res : !res);
  65. }
  66. static bool compByText(const LBItem *a, const LBItem *b)
  67. {
  68. char buf[512];
  69. char bufB[512];
  70. dSprintf(buf, 512, "%s", a->itemText);
  71. dSprintf(bufB, 512, "%s", b->itemText);
  72. S32 res = dStricmp(buf, bufB);
  73. bool result = res <= 0;
  74. return (sIncreasing ? result : !result);
  75. }
  76. };
  77. VectorPtr<LBItem*> mItems;
  78. VectorPtr<LBItem*> mSelectedItems;
  79. bool mMultipleSelections;
  80. Point2I mItemSize;
  81. bool mFitParentWidth;
  82. LBItem* mLastClickItem;
  83. // Persistence
  84. static void initPersistFields();
  85. // Item Accessors
  86. S32 getItemCount();
  87. S32 getSelCount();
  88. S32 getSelectedItem();
  89. void getSelectedItems( Vector<S32> &Items );
  90. S32 getItemIndex( LBItem *item );
  91. StringTableEntry getItemText( S32 index );
  92. void setSelectionInternal(StringTableEntry text);
  93. virtual void setCurSel( S32 index );
  94. void setCurSelRange( S32 start, S32 stop );
  95. void setItemText( S32 index, StringTableEntry text );
  96. S32 addItem( StringTableEntry text, void *itemData = NULL );
  97. S32 addItemWithColor( StringTableEntry text, ColorF color = ColorF(-1, -1, -1), void *itemData = NULL);
  98. S32 addItemWithID(StringTableEntry text, S32 ID = 0, void *itemData = NULL);
  99. S32 insertItem( S32 index, StringTableEntry text, void *itemData = NULL );
  100. virtual LBItem* createItem();
  101. S32 insertItemWithColor( S32 index, StringTableEntry text, ColorF color = ColorF(-1, -1, -1), void *itemData = NULL);
  102. S32 findItemText( StringTableEntry text, bool caseSensitive = false );
  103. void setItemColor(S32 index, ColorF color);
  104. void clearItemColor(S32 index);
  105. void clearAllColors();
  106. ColorF getItemColor(S32 index);
  107. bool getItemHasColor(S32 index);
  108. void setItemID(S32 index, S32 ID);
  109. S32 getItemID(S32 index);
  110. S32 findItemID(S32 ID);
  111. void setItemActive(S32 index);
  112. void setItemInactive(S32 index);
  113. bool getItemActive(S32 index);
  114. void deleteItem( S32 index );
  115. void clearItems();
  116. void clearSelection();
  117. void removeSelection( LBItem *item, S32 index );
  118. void removeSelection( S32 index );
  119. virtual void addSelection( LBItem *item, S32 index );
  120. void addSelection( S32 index );
  121. inline void setMultipleSelection(bool allowMultipleSelect = true) { mMultipleSelections = allowMultipleSelect; };
  122. inline bool getMultipleSelection() { return mMultipleSelections; };
  123. // Sizing
  124. void updateSize();
  125. virtual void parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent);
  126. virtual bool onWake();
  127. virtual void addObject(SimObject *obj);
  128. // Rendering
  129. virtual void onRender( Point2I offset, const RectI &updateRect );
  130. virtual void onRenderItem( RectI &itemRect, LBItem *item );
  131. virtual void ScrollToIndex(const S32 targetIndex);
  132. // Mouse Events
  133. virtual void onTouchDown( const GuiEvent &event );
  134. virtual void onTouchDragged(const GuiEvent &event);
  135. virtual bool onKeyDown(const GuiEvent &event);
  136. virtual S32 getHitIndex(const GuiEvent& event);
  137. virtual void handleItemClick(LBItem* hitItem, S32 hitIndex, const GuiEvent& event);
  138. virtual void handleItemClick_SingleSelection(LBItem* hitItem, S32 hitIndex, const GuiEvent& event);
  139. virtual bool handleItemClick_MultiSelection(LBItem* hitItem, S32 hitIndex, const GuiEvent& event);
  140. virtual void handleItemClick_ClickCallbacks(LBItem* hitItem, S32 hitIndex, const GuiEvent& event);
  141. // Sorting
  142. virtual void sortByText(bool increasing = true);
  143. virtual void sortByID(bool increasing = true);
  144. protected:
  145. GuiControl *caller;
  146. };
  147. #endif