llradiogroup.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file llradiogroup.h
  3. * @brief LLRadioGroup 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. #ifndef LL_LLRADIOGROUP_H
  33. #define LL_LLRADIOGROUP_H
  34. #include "llcheckboxctrl.h"
  35. #include "llpreprocessor.h"
  36. #include "lluictrl.h"
  37. // A checkbox control with use_radio_style == true.
  38. class LLRadioCtrl final : public LLCheckBoxCtrl
  39. {
  40. public:
  41. LLRadioCtrl(const std::string& name, const LLRect& rect,
  42. const std::string& label, const LLFontGL* font = NULL,
  43. void (*commit_callback)(LLUICtrl*, void*) = NULL,
  44. void* callback_userdata = NULL)
  45. : LLCheckBoxCtrl(name, rect, label, font, commit_callback, callback_userdata,
  46. false, RADIO_STYLE)
  47. {
  48. setTabStop(false);
  49. }
  50. // LLView interface
  51. const std::string& getTag() const override;
  52. LLXMLNodePtr getXML(bool save_children = true) const override;
  53. void setValue(const LLSD& value) override;
  54. };
  55. // An invisible view containing multiple mutually exclusive toggling buttons
  56. // (usually radio buttons). Automatically handles the mutex condition by
  57. // highlighting only one button at a time.
  58. class LLRadioGroup final : public LLUICtrl
  59. {
  60. protected:
  61. LOG_CLASS(LLRadioGroup);
  62. public:
  63. enum EOperation
  64. {
  65. OP_DELETE = 1,
  66. OP_SELECT,
  67. OP_DESELECT,
  68. };
  69. // Builds a radio group. The number (0...n - 1) of the currently selected
  70. // element will be stored in the named control. After the control is
  71. // changed the callback will be called.
  72. LLRadioGroup(const std::string& name, const LLRect& rect,
  73. const char* control_name, LLUICtrlCallback callback = NULL,
  74. void* userdata = NULL, bool border = true);
  75. // Another radio group constructor, but this one does not rely on needing a
  76. // control
  77. LLRadioGroup(const std::string& name, const LLRect& rect, S32 initial_idx,
  78. LLUICtrlCallback callback = NULL, void* userdata = NULL,
  79. bool border = true);
  80. bool handleKeyHere(KEY key, MASK mask) override;
  81. void setEnabled(bool enabled) override;
  82. const std::string& getTag() const override;
  83. LLXMLNodePtr getXML(bool save_children = true) const override;
  84. static LLView* fromXML(LLXMLNodePtr nodep, LLView* parentp,
  85. LLUICtrlFactory*);
  86. void setIndexEnabled(S32 index, bool enabled);
  87. // Returns the index value of the selected item
  88. LL_INLINE S32 getSelectedIndex() const { return mSelectedIndex; }
  89. // Sets the index value programatically
  90. bool setSelectedIndex(S32 index, bool from_event = false);
  91. // Accepts and retrieves strings of the radio group control names
  92. void setValue(const LLSD& value) override;
  93. LLSD getValue() const override;
  94. // Draws the group, but also fix the highlighting based on the control.
  95. void draw() override;
  96. // You must use this method to add buttons to a radio group. Do not use
  97. // addChild() since it would not set the callback method correctly.
  98. LLRadioCtrl* addRadioButton(const std::string& name,
  99. const std::string& label,
  100. const LLRect& rect, const LLFontGL* fontp);
  101. LL_INLINE LLRadioCtrl* getRadioButton(S32 index) { return mRadioButtons[index]; }
  102. LL_INLINE S32 getItemCount() const { return mRadioButtons.size(); }
  103. LL_INLINE bool getCanSelect()const { return true; }
  104. LL_INLINE bool selectFirstItem() { return setSelectedIndex(0); }
  105. LL_INLINE bool selectNthItem(S32 index) { return setSelectedIndex(index); }
  106. LL_INLINE bool selectItemRange(S32 first, S32) { return setSelectedIndex(first); }
  107. LL_INLINE S32 getFirstSelectedIndex() const { return getSelectedIndex(); }
  108. LL_INLINE bool setCurrentByID(const LLUUID& id) { return false; }
  109. LL_INLINE LLUUID getCurrentID() const { return LLUUID::null; }
  110. bool setSelectedByValue(const LLSD& value, bool selected);
  111. LL_INLINE LLSD getSelectedValue() { return getValue(); }
  112. bool isSelected(const LLSD& value) const;
  113. LL_INLINE bool operateOnSelection(EOperation) { return false; }
  114. LL_INLINE bool operateOnAll(EOperation) { return false; }
  115. private:
  116. void init(bool border);
  117. // Updates the control as needed. 'userdata' *must* be a pointer to a
  118. // child button.
  119. static void onClickButton(LLUICtrl* radiop, void* userdata);
  120. private:
  121. S32 mSelectedIndex;
  122. bool mHasBorder;
  123. typedef std::vector<LLRadioCtrl*> button_list_t;
  124. button_list_t mRadioButtons;
  125. };
  126. #endif