PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llmenubutton.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 213 lines | 142 code | 39 blank | 32 comment | 26 complexity | a86fbb8787b8c2e23c3a7b3cdfbc1dd4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llbutton.cpp
  3. * @brief LLButton base class
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llmenubutton.h"
  28. // Linden library includes
  29. #include "lltoggleablemenu.h"
  30. #include "llstring.h"
  31. #include "v4color.h"
  32. static LLDefaultChildRegistry::Register<LLMenuButton> r("menu_button");
  33. void LLMenuButton::MenuPositions::declareValues()
  34. {
  35. declare("topleft", MP_TOP_LEFT);
  36. declare("topright", MP_TOP_RIGHT);
  37. declare("bottomleft", MP_BOTTOM_LEFT);
  38. }
  39. LLMenuButton::Params::Params()
  40. : menu_filename("menu_filename"),
  41. position("position", MP_BOTTOM_LEFT)
  42. {
  43. }
  44. LLMenuButton::LLMenuButton(const LLMenuButton::Params& p)
  45. : LLButton(p),
  46. mIsMenuShown(false),
  47. mMenuPosition(p.position)
  48. {
  49. std::string menu_filename = p.menu_filename;
  50. if (!menu_filename.empty())
  51. {
  52. LLToggleableMenu* menu = LLUICtrlFactory::getInstance()->createFromFile<LLToggleableMenu>(menu_filename, LLMenuGL::sMenuContainer, LLMenuHolderGL::child_registry_t::instance());
  53. if (!menu)
  54. {
  55. llwarns << "Error loading menu_button menu" << llendl;
  56. return;
  57. }
  58. menu->setVisibilityChangeCallback(boost::bind(&LLMenuButton::onMenuVisibilityChange, this, _2));
  59. mMenuHandle = menu->getHandle();
  60. updateMenuOrigin();
  61. }
  62. }
  63. boost::signals2::connection LLMenuButton::setMouseDownCallback( const mouse_signal_t::slot_type& cb )
  64. {
  65. return LLUICtrl::setMouseDownCallback(cb);
  66. }
  67. void LLMenuButton::hideMenu()
  68. {
  69. if(mMenuHandle.isDead()) return;
  70. LLToggleableMenu* menu = dynamic_cast<LLToggleableMenu*>(mMenuHandle.get());
  71. if (menu)
  72. {
  73. menu->setVisible(FALSE);
  74. }
  75. }
  76. LLToggleableMenu* LLMenuButton::getMenu()
  77. {
  78. return dynamic_cast<LLToggleableMenu*>(mMenuHandle.get());
  79. }
  80. void LLMenuButton::setMenu(LLToggleableMenu* menu, EMenuPosition position /*MP_TOP_LEFT*/)
  81. {
  82. if (!menu) return;
  83. mMenuHandle = menu->getHandle();
  84. mMenuPosition = position;
  85. menu->setVisibilityChangeCallback(boost::bind(&LLMenuButton::onMenuVisibilityChange, this, _2));
  86. }
  87. BOOL LLMenuButton::handleKeyHere(KEY key, MASK mask )
  88. {
  89. if (mMenuHandle.isDead()) return FALSE;
  90. if( KEY_RETURN == key && mask == MASK_NONE && !gKeyboard->getKeyRepeated(key))
  91. {
  92. // *HACK: We emit the mouse down signal to fire the callback bound to the
  93. // menu emerging event before actually displaying the menu. See STORM-263.
  94. LLUICtrl::handleMouseDown(-1, -1, MASK_NONE);
  95. toggleMenu();
  96. return TRUE;
  97. }
  98. LLToggleableMenu* menu = dynamic_cast<LLToggleableMenu*>(mMenuHandle.get());
  99. if (menu && menu->getVisible() && key == KEY_ESCAPE && mask == MASK_NONE)
  100. {
  101. menu->setVisible(FALSE);
  102. return TRUE;
  103. }
  104. return FALSE;
  105. }
  106. BOOL LLMenuButton::handleMouseDown(S32 x, S32 y, MASK mask)
  107. {
  108. LLButton::handleMouseDown(x, y, mask);
  109. toggleMenu();
  110. return TRUE;
  111. }
  112. void LLMenuButton::toggleMenu()
  113. {
  114. if(mMenuHandle.isDead()) return;
  115. LLToggleableMenu* menu = dynamic_cast<LLToggleableMenu*>(mMenuHandle.get());
  116. if (!menu) return;
  117. // Store the button rectangle to toggle menu visibility if a mouse event
  118. // occurred inside or outside the button rect.
  119. menu->setButtonRect(this);
  120. if (!menu->toggleVisibility() && mIsMenuShown)
  121. {
  122. setForcePressedState(false);
  123. mIsMenuShown = false;
  124. }
  125. else
  126. {
  127. menu->buildDrawLabels();
  128. menu->arrangeAndClear();
  129. menu->updateParent(LLMenuGL::sMenuContainer);
  130. updateMenuOrigin();
  131. LLMenuGL::showPopup(getParent(), menu, mX, mY);
  132. setForcePressedState(true);
  133. mIsMenuShown = true;
  134. }
  135. }
  136. void LLMenuButton::updateMenuOrigin()
  137. {
  138. if (mMenuHandle.isDead()) return;
  139. LLRect rect = getRect();
  140. switch (mMenuPosition)
  141. {
  142. case MP_TOP_LEFT:
  143. {
  144. mX = rect.mLeft;
  145. mY = rect.mTop + mMenuHandle.get()->getRect().getHeight();
  146. break;
  147. }
  148. case MP_TOP_RIGHT:
  149. {
  150. const LLRect& menu_rect = mMenuHandle.get()->getRect();
  151. mX = rect.mRight - menu_rect.getWidth();
  152. mY = rect.mTop + menu_rect.getHeight();
  153. break;
  154. }
  155. case MP_BOTTOM_LEFT:
  156. {
  157. mX = rect.mLeft;
  158. mY = rect.mBottom;
  159. break;
  160. }
  161. }
  162. }
  163. void LLMenuButton::onMenuVisibilityChange(const LLSD& param)
  164. {
  165. bool new_visibility = param["visibility"].asBoolean();
  166. bool is_closed_by_button_click = param["closed_by_button_click"].asBoolean();
  167. // Reset the button "pressed" state only if the menu is shown by this particular
  168. // menu button (not any other control) and is not being closed by a click on the button.
  169. if (!new_visibility && !is_closed_by_button_click && mIsMenuShown)
  170. {
  171. setForcePressedState(false);
  172. mIsMenuShown = false;
  173. }
  174. }