guiListBoxCtrl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. StringTableEntry itemText;
  51. bool isSelected;
  52. bool isActive;
  53. int ID;
  54. void* itemData;
  55. ColorF color;
  56. bool hasColor;
  57. static bool sIncreasing;
  58. // Compare Functions
  59. static bool compByID(const LBItem *a, const LBItem *b)
  60. {
  61. bool res = a->ID < b->ID;
  62. return (sIncreasing ? res : !res);
  63. }
  64. static bool compByText(const LBItem *a, const LBItem *b)
  65. {
  66. char buf[512];
  67. char bufB[512];
  68. dSprintf(buf, 512, "%s", a->itemText);
  69. dSprintf(bufB, 512, "%s", b->itemText);
  70. S32 res = dStricmp(buf, bufB);
  71. bool result = res <= 0;
  72. return (sIncreasing ? result : !result);
  73. }
  74. };
  75. VectorPtr<LBItem*> mItems;
  76. VectorPtr<LBItem*> mSelectedItems;
  77. bool mMultipleSelections;
  78. Point2I mItemSize;
  79. bool mFitParentWidth;
  80. LBItem* mLastClickItem;
  81. // Persistence
  82. static void initPersistFields();
  83. // Item Accessors
  84. S32 getItemCount();
  85. S32 getSelCount();
  86. S32 getSelectedItem();
  87. void getSelectedItems( Vector<S32> &Items );
  88. S32 getItemIndex( LBItem *item );
  89. StringTableEntry getItemText( S32 index );
  90. void setCurSel( S32 index );
  91. void setCurSelRange( S32 start, S32 stop );
  92. void setItemText( S32 index, StringTableEntry text );
  93. S32 addItem( StringTableEntry text, void *itemData = NULL );
  94. S32 addItemWithColor( StringTableEntry text, ColorF color = ColorF(-1, -1, -1), void *itemData = NULL);
  95. S32 addItemWithID(StringTableEntry text, S32 ID = 0, void *itemData = NULL);
  96. S32 insertItem( S32 index, StringTableEntry text, void *itemData = NULL );
  97. S32 insertItemWithColor( S32 index, StringTableEntry text, ColorF color = ColorF(-1, -1, -1), void *itemData = NULL);
  98. S32 findItemText( StringTableEntry text, bool caseSensitive = false );
  99. void setItemColor(S32 index, ColorF color);
  100. void clearItemColor(S32 index);
  101. void clearAllColors();
  102. ColorF getItemColor(S32 index);
  103. bool getItemHasColor(S32 index);
  104. void setItemID(S32 index, S32 ID);
  105. S32 getItemID(S32 index);
  106. S32 findItemID(S32 ID);
  107. void setItemActive(S32 index);
  108. void setItemInactive(S32 index);
  109. bool getItemActive(S32 index);
  110. void deleteItem( S32 index );
  111. void clearItems();
  112. void clearSelection();
  113. void removeSelection( LBItem *item, S32 index );
  114. void removeSelection( S32 index );
  115. virtual void addSelection( LBItem *item, S32 index );
  116. void addSelection( S32 index );
  117. inline void setMultipleSelection(bool allowMultipleSelect = true) { mMultipleSelections = allowMultipleSelect; };
  118. inline bool getMultipleSelection() { return mMultipleSelections; };
  119. // Sizing
  120. void updateSize();
  121. virtual void parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent);
  122. virtual bool onWake();
  123. virtual void addObject(SimObject *obj);
  124. // Rendering
  125. virtual void onRender( Point2I offset, const RectI &updateRect );
  126. virtual void onRenderItem( RectI &itemRect, LBItem *item );
  127. // Mouse Events
  128. virtual void onTouchDown( const GuiEvent &event );
  129. virtual void onTouchDragged(const GuiEvent &event);
  130. // Sorting
  131. virtual void sortByText(bool increasing = true);
  132. virtual void sortByID(bool increasing = true);
  133. protected:
  134. GuiControl *caller;
  135. };
  136. #endif