llcombobox.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @file llcombobox.h
  3. * @brief LLComboBox base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. // A control that displays the name of the chosen item, which when clicked
  33. // shows a scrolling box of choices.
  34. #ifndef LL_LLCOMBOBOX_H
  35. #define LL_LLCOMBOBOX_H
  36. #include "lluictrl.h"
  37. class LLButton;
  38. class LLLineEditor;
  39. class LLScrollListItem;
  40. class LLScrollListCtrl;
  41. class LLComboBox : public LLUICtrl
  42. {
  43. public:
  44. enum EOperation
  45. {
  46. OP_DELETE = 1,
  47. OP_SELECT,
  48. OP_DESELECT,
  49. };
  50. typedef enum e_preferred_position
  51. {
  52. ABOVE,
  53. BELOW
  54. } EPreferredPosition;
  55. LLComboBox(const std::string& name, const LLRect &rect,
  56. const std::string& label,
  57. void (*commit_callback)(LLUICtrl*, void*) = NULL,
  58. void* callback_userdata = NULL);
  59. ~LLComboBox() override;
  60. // LLView interface
  61. const std::string& getTag() const override;
  62. LLXMLNodePtr getXML(bool save_children = true) const override;
  63. static LLView* fromXML(LLXMLNodePtr node, LLView* parent,
  64. LLUICtrlFactory* factory);
  65. void draw() override;
  66. void onFocusLost() override;
  67. void onLostTop() override;
  68. void setEnabled(bool enabled) override;
  69. bool handleToolTip(S32 x, S32 y, std::string& msg, LLRect* r) override;
  70. bool handleKeyHere(KEY key, MASK mask) override;
  71. bool handleUnicodeCharHere(llwchar uni_char) override;
  72. // LLUICtrl interface
  73. void clear() override; // To select nothing
  74. void onCommit() override;
  75. LL_INLINE bool acceptsTextInput() const override { return mAllowTextEntry; }
  76. // Returns true if the user has modified this control.
  77. bool isDirty() const override;
  78. // Clears dirty state
  79. void resetDirty() override;
  80. void setFocus(bool b) override;
  81. // Allow prevalidation of text input field
  82. void setPrevalidate(bool (*func)(const LLWString&));
  83. // Selects item by underlying LLSD value, using LLSD::asString() matching.
  84. // For simple items, this is just the name of the label.
  85. void setValue(const LLSD& value) override;
  86. // Gets underlying LLSD value for currently selected items. For simple
  87. // items, this is just the label.
  88. LLSD getValue() const override;
  89. void setAllowTextEntry(bool allow, S32 max_chars = 50,
  90. bool make_tentative = true);
  91. void setTextEntry(const std::string& text);
  92. // Sets focus to the text input area instead of the list
  93. void setFocusText(bool b);
  94. // Returns true if the user has modified the text input area
  95. bool isTextDirty() const;
  96. // Resets the dirty flag on the input field
  97. void resetTextDirty();
  98. // Add item "name" to menu:
  99. LLScrollListItem* add(const std::string& name,
  100. EAddPosition pos = ADD_BOTTOM, bool enabled = true);
  101. LLScrollListItem* add(const std::string& name, const LLUUID& id,
  102. EAddPosition pos = ADD_BOTTOM, bool enabled = true);
  103. LLScrollListItem* add(const std::string& name, void* userdata,
  104. EAddPosition pos = ADD_BOTTOM, bool enabled = true);
  105. LLScrollListItem* add(const std::string& name, LLSD value,
  106. EAddPosition pos = ADD_BOTTOM, bool enabled = true);
  107. LLScrollListItem* addSeparator(EAddPosition pos = ADD_BOTTOM);
  108. // Removes item by index, return true if found and removed:
  109. bool remove(S32 index);
  110. LL_INLINE void removeall() { clearRows(); }
  111. bool itemExists(const std::string& name);
  112. // Sort the entries in the combobox by name:
  113. void sortByName(bool ascending = true);
  114. // Selects current item by name using selectItemByLabel. Returns false if
  115. // not found:
  116. bool setSimple(const std::string& name);
  117. // Gets name of current item. Returns an empty string if not found:
  118. const std::string getSimple() const;
  119. // Gets contents of column x of selected row:
  120. const std::string getSelectedItemLabel(S32 column = 0) const;
  121. // Sets the label, which doesn't have to exist in the label.
  122. // This is probably an UI abuse.
  123. void setLabel(const std::string& name);
  124. // Removes item "name", return true if found and removed:
  125. bool remove(const std::string& name);
  126. bool setCurrentByIndex(S32 index);
  127. S32 getCurrentIndex() const;
  128. virtual void updateLayout();
  129. S32 getItemCount() const;
  130. // Overwrites the default column (See LLScrollListCtrl for format)
  131. void addColumn(const LLSD& column, EAddPosition pos = ADD_BOTTOM);
  132. void clearColumns();
  133. void setColumnLabel(const std::string& col, const std::string& l);
  134. LLScrollListItem* addElement(const LLSD& value,
  135. EAddPosition pos = ADD_BOTTOM,
  136. void* userdata = NULL);
  137. LLScrollListItem* addSimpleElement(const std::string& value,
  138. EAddPosition pos = ADD_BOTTOM,
  139. const LLSD& id = LLSD());
  140. void clearRows();
  141. void sortByColumn(const std::string& name, bool ascending);
  142. LLScrollListItem* getItemByIndex(S32 index) const;
  143. LL_INLINE bool getCanSelect() const { return true; }
  144. LL_INLINE bool selectFirstItem() { return setCurrentByIndex(0); }
  145. LL_INLINE bool selectNthItem(S32 index) { return setCurrentByIndex(index); }
  146. bool selectItemRange(S32 first, S32 last);
  147. LL_INLINE S32 getFirstSelectedIndex() const { return getCurrentIndex(); }
  148. bool setCurrentByID(const LLUUID& id);
  149. LLUUID getCurrentID() const;
  150. bool setSelectedByValue(const LLSD& value, bool selected);
  151. LL_INLINE bool selectByValue(const LLSD value) { return setSelectedByValue(value, true); }
  152. LLSD getSelectedValue();
  153. bool isSelected(const LLSD& value) const;
  154. bool operateOnSelection(EOperation op);
  155. bool operateOnAll(EOperation op);
  156. void* getCurrentUserdata();
  157. LL_INLINE void setPrearrangeCallback(void (*cb)(LLUICtrl*, void*))
  158. {
  159. mPrearrangeCallback = cb;
  160. }
  161. LL_INLINE void setTextEntryCallback(void (*cb)(LLLineEditor*, void*))
  162. {
  163. mTextEntryCallback = cb;
  164. }
  165. void setButtonVisible(bool visible);
  166. void setSuppressTentative(bool suppress);
  167. void updateSelection();
  168. virtual void showList();
  169. virtual void hideList();
  170. protected:
  171. static void onButtonDown(void* userdata);
  172. static void onItemSelected(LLUICtrl* item, void* userdata);
  173. static void onTextEntry(LLLineEditor* line_editor, void* user_data);
  174. static void onTextCommit(LLUICtrl* caller, void* user_data);
  175. protected:
  176. LLButton* mButton;
  177. LLScrollListCtrl* mList;
  178. EPreferredPosition mListPosition;
  179. LLUIImagePtr mArrowImage;
  180. std::string mLabel;
  181. private:
  182. LLLineEditor* mTextEntry;
  183. void (*mPrearrangeCallback)(LLUICtrl*, void*);
  184. void (*mTextEntryCallback)(LLLineEditor*, void*);
  185. S32 mMaxChars;
  186. bool mTextEntryTentative;
  187. bool mSuppressTentative;
  188. bool mAllowTextEntry;
  189. };
  190. class LLFlyoutButton final : public LLComboBox
  191. {
  192. public:
  193. LLFlyoutButton(const std::string& name, const LLRect& rect,
  194. const std::string& label,
  195. void (*commit_callback)(LLUICtrl*, void*) = NULL,
  196. void* callback_userdata = NULL);
  197. void updateLayout() override;
  198. void draw() override;
  199. void setEnabled(bool enabled) override;
  200. LL_INLINE void setToggleState(bool b) { mToggleState = b; }
  201. // Allows to change the label of the action button:
  202. void setLabel(const std::string& label);
  203. const std::string& getTag() const override;
  204. LLXMLNodePtr getXML(bool save_children = true) const override;
  205. static LLView* fromXML(LLXMLNodePtr node, LLView* parent,
  206. LLUICtrlFactory* factory);
  207. protected:
  208. static void onActionButtonClick(void* userdata);
  209. static void onSelectAction(LLUICtrl* ctrl, void* userdata);
  210. protected:
  211. LLButton* mActionButton;
  212. LLUIImagePtr mActionButtonImage;
  213. LLUIImagePtr mExpanderButtonImage;
  214. LLUIImagePtr mActionButtonImageSelected;
  215. LLUIImagePtr mExpanderButtonImageSelected;
  216. LLUIImagePtr mActionButtonImageDisabled;
  217. LLUIImagePtr mExpanderButtonImageDisabled;
  218. bool mToggleState;
  219. };
  220. #endif