PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llui/llmenugl.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2505 lines | 1881 code | 345 blank | 279 comment | 373 complexity | d76650c162abcb8c82071b5f2aa201c4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmenugl.cpp
  3. * @brief LLMenuItemGL 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. //*****************************************************************************
  27. //
  28. // This file contains the opengl based menu implementation.
  29. //
  30. // NOTES: A menu label is split into 4 columns. The left column, the
  31. // label colum, the accelerator column, and the right column. The left
  32. // column is used for displaying boolean values for toggle and check
  33. // controls. The right column is used for submenus.
  34. //
  35. //*****************************************************************************
  36. //#include "llviewerprecompiledheaders.h"
  37. #include "linden_common.h"
  38. #include "llmenugl.h"
  39. #include "llgl.h"
  40. #include "llmath.h"
  41. #include "llrender.h"
  42. #include "llfocusmgr.h"
  43. #include "llcoord.h"
  44. #include "llwindow.h"
  45. #include "llcriticaldamp.h"
  46. #include "lluictrlfactory.h"
  47. #include "llbutton.h"
  48. #include "llfontgl.h"
  49. #include "llresmgr.h"
  50. #include "lltrans.h"
  51. #include "llui.h"
  52. #include "llstl.h"
  53. #include "v2math.h"
  54. #include <set>
  55. #include <boost/tokenizer.hpp>
  56. // static
  57. LLMenuHolderGL *LLMenuGL::sMenuContainer = NULL;
  58. S32 MENU_BAR_HEIGHT = 0;
  59. S32 MENU_BAR_WIDTH = 0;
  60. ///============================================================================
  61. /// Local function declarations, constants, enums, and typedefs
  62. ///============================================================================
  63. const S32 LABEL_BOTTOM_PAD_PIXELS = 2;
  64. const U32 LEFT_PAD_PIXELS = 3;
  65. const U32 LEFT_WIDTH_PIXELS = 15;
  66. const U32 LEFT_PLAIN_PIXELS = LEFT_PAD_PIXELS + LEFT_WIDTH_PIXELS;
  67. const U32 RIGHT_PAD_PIXELS = 2;
  68. const U32 RIGHT_WIDTH_PIXELS = 15;
  69. const U32 RIGHT_PLAIN_PIXELS = RIGHT_PAD_PIXELS + RIGHT_WIDTH_PIXELS;
  70. const U32 PLAIN_PAD_PIXELS = LEFT_PAD_PIXELS + LEFT_WIDTH_PIXELS + RIGHT_PAD_PIXELS + RIGHT_WIDTH_PIXELS;
  71. const U32 BRIEF_PAD_PIXELS = 2;
  72. const U32 SEPARATOR_HEIGHT_PIXELS = 8;
  73. const S32 TEAROFF_SEPARATOR_HEIGHT_PIXELS = 10;
  74. const S32 MENU_ITEM_PADDING = 4;
  75. const std::string SEPARATOR_NAME("separator");
  76. const std::string VERTICAL_SEPARATOR_LABEL( "|" );
  77. const std::string LLMenuGL::BOOLEAN_TRUE_PREFIX( "\xE2\x9C\x94" ); // U+2714 HEAVY CHECK MARK
  78. const std::string LLMenuGL::BRANCH_SUFFIX( "\xE2\x96\xB6" ); // U+25B6 BLACK RIGHT-POINTING TRIANGLE
  79. const std::string LLMenuGL::ARROW_UP ("^^^^^^^");
  80. const std::string LLMenuGL::ARROW_DOWN("vvvvvvv");
  81. const F32 MAX_MOUSE_SLOPE_SUB_MENU = 0.9f;
  82. const S32 PIE_GESTURE_ACTIVATE_DISTANCE = 10;
  83. BOOL LLMenuGL::sKeyboardMode = FALSE;
  84. LLHandle<LLView> LLMenuHolderGL::sItemLastSelectedHandle;
  85. LLFrameTimer LLMenuHolderGL::sItemActivationTimer;
  86. //LLColor4 LLMenuGL::sBackgroundColor( 0.8f, 0.8f, 0.0f, 1.0f );
  87. const S32 PIE_CENTER_SIZE = 20; // pixels, radius of center hole
  88. const F32 PIE_SCALE_FACTOR = 1.7f; // scale factor for pie menu when mouse is initially down
  89. const F32 PIE_SHRINK_TIME = 0.2f; // time of transition between unbounded and bounded display of pie menu
  90. const F32 ACTIVATE_HIGHLIGHT_TIME = 0.3f;
  91. static MenuRegistry::Register<LLMenuItemGL> register_menu_item("menu_item");
  92. static MenuRegistry::Register<LLMenuItemSeparatorGL> register_separator("menu_item_separator");
  93. static MenuRegistry::Register<LLMenuItemCallGL> register_menu_item_call("menu_item_call");
  94. static MenuRegistry::Register<LLMenuItemCheckGL> register_menu_item_check("menu_item_check");
  95. // Created programmatically but we need to specify custom colors in xml
  96. static MenuRegistry::Register<LLMenuItemTearOffGL> register_menu_item_tear_off("menu_item_tear_off");
  97. static MenuRegistry::Register<LLMenuGL> register_menu("menu");
  98. static LLDefaultChildRegistry::Register<LLMenuGL> register_menu_default("menu");
  99. ///============================================================================
  100. /// Class LLMenuItemGL
  101. ///============================================================================
  102. LLMenuItemGL::Params::Params()
  103. : shortcut("shortcut"),
  104. jump_key("jump_key", KEY_NONE),
  105. use_mac_ctrl("use_mac_ctrl", false),
  106. allow_key_repeat("allow_key_repeat", false),
  107. rect("rect"),
  108. left("left"),
  109. top("top"),
  110. right("right"),
  111. bottom("bottom"),
  112. width("width"),
  113. height("height"),
  114. bottom_delta("bottom_delta"),
  115. left_delta("left_delta"),
  116. enabled_color("enabled_color"),
  117. disabled_color("disabled_color"),
  118. highlight_bg_color("highlight_bg_color"),
  119. highlight_fg_color("highlight_fg_color")
  120. {
  121. changeDefault(mouse_opaque, true);
  122. }
  123. // Default constructor
  124. LLMenuItemGL::LLMenuItemGL(const LLMenuItemGL::Params& p)
  125. : LLUICtrl(p),
  126. mJumpKey(p.jump_key),
  127. mAllowKeyRepeat(p.allow_key_repeat),
  128. mHighlight( FALSE ),
  129. mGotHover( FALSE ),
  130. mBriefItem( FALSE ),
  131. mDrawTextDisabled( FALSE ),
  132. mFont(p.font),
  133. mAcceleratorKey(KEY_NONE),
  134. mAcceleratorMask(MASK_NONE),
  135. mLabel(p.label.isProvided() ? p.label() : p.name()),
  136. mEnabledColor(p.enabled_color()),
  137. mDisabledColor(p.disabled_color()),
  138. mHighlightBackground(p.highlight_bg_color()),
  139. mHighlightForeground(p.highlight_fg_color())
  140. {
  141. #ifdef LL_DARWIN
  142. // See if this Mac accelerator should really use the ctrl key and not get mapped to cmd
  143. BOOL useMacCtrl = p.use_mac_ctrl;
  144. #endif // LL_DARWIN
  145. std::string shortcut = p.shortcut;
  146. if (shortcut.find("control") != shortcut.npos)
  147. {
  148. #ifdef LL_DARWIN
  149. if ( useMacCtrl )
  150. {
  151. mAcceleratorMask |= MASK_MAC_CONTROL;
  152. }
  153. #endif // LL_DARWIN
  154. mAcceleratorMask |= MASK_CONTROL;
  155. }
  156. if (shortcut.find("alt") != shortcut.npos)
  157. {
  158. mAcceleratorMask |= MASK_ALT;
  159. }
  160. if (shortcut.find("shift") != shortcut.npos)
  161. {
  162. mAcceleratorMask |= MASK_SHIFT;
  163. }
  164. S32 pipe_pos = shortcut.rfind("|");
  165. std::string key_str = shortcut.substr(pipe_pos+1);
  166. LLKeyboard::keyFromString(key_str, &mAcceleratorKey);
  167. LL_DEBUGS("HotKeys") << "Process short cut key: shortcut: " << shortcut
  168. << ", key str: " << key_str
  169. << ", accelerator mask: " << mAcceleratorMask
  170. << ", accelerator key: " << mAcceleratorKey
  171. << LL_ENDL;
  172. }
  173. //virtual
  174. void LLMenuItemGL::setValue(const LLSD& value)
  175. {
  176. setLabel(value.asString());
  177. }
  178. //virtual
  179. LLSD LLMenuItemGL::getValue() const
  180. {
  181. return getLabel();
  182. }
  183. //virtual
  184. BOOL LLMenuItemGL::handleAcceleratorKey(KEY key, MASK mask)
  185. {
  186. if( getEnabled() && (!gKeyboard->getKeyRepeated(key) || mAllowKeyRepeat) && (key == mAcceleratorKey) && (mask == (mAcceleratorMask & MASK_NORMALKEYS)) )
  187. {
  188. onCommit();
  189. return TRUE;
  190. }
  191. return FALSE;
  192. }
  193. BOOL LLMenuItemGL::handleHover(S32 x, S32 y, MASK mask)
  194. {
  195. setHover(TRUE);
  196. getWindow()->setCursor(UI_CURSOR_ARROW);
  197. return TRUE;
  198. }
  199. //virtual
  200. BOOL LLMenuItemGL::handleRightMouseDown(S32 x, S32 y, MASK mask)
  201. {
  202. return LLUICtrl::handleRightMouseDown(x,y,mask);
  203. }
  204. //virtual
  205. BOOL LLMenuItemGL::handleRightMouseUp(S32 x, S32 y, MASK mask)
  206. {
  207. // If this event came from a right-click context menu spawn,
  208. // process as a left-click to allow menu items to be hit
  209. if (LLMenuHolderGL::sContextMenuSpawnPos.mX != S32_MAX
  210. || LLMenuHolderGL::sContextMenuSpawnPos.mY != S32_MAX)
  211. {
  212. BOOL handled = handleMouseUp(x, y, mask);
  213. return handled;
  214. }
  215. return LLUICtrl::handleRightMouseUp(x,y,mask);
  216. }
  217. // This function checks to see if the accelerator key is already in use;
  218. // if not, it will be added to the list
  219. BOOL LLMenuItemGL::addToAcceleratorList(std::list <LLKeyBinding*> *listp)
  220. {
  221. LLKeyBinding *accelerator = NULL;
  222. if (mAcceleratorKey != KEY_NONE)
  223. {
  224. std::list<LLKeyBinding*>::iterator list_it;
  225. for (list_it = listp->begin(); list_it != listp->end(); ++list_it)
  226. {
  227. accelerator = *list_it;
  228. if ((accelerator->mKey == mAcceleratorKey) && (accelerator->mMask == (mAcceleratorMask & MASK_NORMALKEYS)))
  229. {
  230. // *NOTE: get calling code to throw up warning or route
  231. // warning messages back to app-provided output
  232. // std::string warning;
  233. // warning.append("Duplicate key binding <");
  234. // appendAcceleratorString( warning );
  235. // warning.append("> for menu items:\n ");
  236. // warning.append(accelerator->mName);
  237. // warning.append("\n ");
  238. // warning.append(mLabel);
  239. // llwarns << warning << llendl;
  240. // LLAlertDialog::modalAlert(warning);
  241. return FALSE;
  242. }
  243. }
  244. if (!accelerator)
  245. {
  246. accelerator = new LLKeyBinding;
  247. if (accelerator)
  248. {
  249. accelerator->mKey = mAcceleratorKey;
  250. accelerator->mMask = (mAcceleratorMask & MASK_NORMALKEYS);
  251. // accelerator->mName = mLabel;
  252. }
  253. listp->push_back(accelerator);//addData(accelerator);
  254. }
  255. }
  256. return TRUE;
  257. }
  258. // This function appends the character string representation of
  259. // the current accelerator key and mask to the provided string.
  260. void LLMenuItemGL::appendAcceleratorString( std::string& st ) const
  261. {
  262. st = LLKeyboard::stringFromAccelerator( mAcceleratorMask, mAcceleratorKey );
  263. LL_DEBUGS("HotKeys") << "appendAcceleratorString: " << st << LL_ENDL;
  264. }
  265. void LLMenuItemGL::setJumpKey(KEY key)
  266. {
  267. mJumpKey = LLStringOps::toUpper((char)key);
  268. }
  269. // virtual
  270. U32 LLMenuItemGL::getNominalHeight( void ) const
  271. {
  272. return llround(mFont->getLineHeight()) + MENU_ITEM_PADDING;
  273. }
  274. //virtual
  275. void LLMenuItemGL::setBriefItem(BOOL brief)
  276. {
  277. mBriefItem = brief;
  278. }
  279. //virtual
  280. BOOL LLMenuItemGL::isBriefItem() const
  281. {
  282. return mBriefItem;
  283. }
  284. // Get the parent menu for this item
  285. LLMenuGL* LLMenuItemGL::getMenu() const
  286. {
  287. return (LLMenuGL*) getParent();
  288. }
  289. // getNominalWidth() - returns the normal width of this control in
  290. // pixels - this is used for calculating the widest item, as well as
  291. // for horizontal arrangement.
  292. U32 LLMenuItemGL::getNominalWidth( void ) const
  293. {
  294. U32 width;
  295. if (mBriefItem)
  296. {
  297. width = BRIEF_PAD_PIXELS;
  298. }
  299. else
  300. {
  301. width = PLAIN_PAD_PIXELS;
  302. }
  303. if( KEY_NONE != mAcceleratorKey )
  304. {
  305. width += getMenu()->getShortcutPad();
  306. std::string temp;
  307. appendAcceleratorString( temp );
  308. width += mFont->getWidth( temp );
  309. }
  310. width += mFont->getWidth( mLabel.getWString().c_str() );
  311. return width;
  312. }
  313. // called to rebuild the draw label
  314. void LLMenuItemGL::buildDrawLabel( void )
  315. {
  316. mDrawAccelLabel.clear();
  317. std::string st = mDrawAccelLabel.getString();
  318. appendAcceleratorString( st );
  319. mDrawAccelLabel = st;
  320. }
  321. void LLMenuItemGL::onCommit( void )
  322. {
  323. // Check torn-off status to allow left-arrow keyboard navigation back
  324. // to parent menu.
  325. // Also, don't hide if item triggered by keyboard shortcut (and hence
  326. // parent not visible).
  327. if (!getMenu()->getTornOff()
  328. && getMenu()->getVisible())
  329. {
  330. LLMenuGL::sMenuContainer->hideMenus();
  331. }
  332. LLUICtrl::onCommit();
  333. }
  334. // set the hover status (called by it's menu)
  335. void LLMenuItemGL::setHighlight( BOOL highlight )
  336. {
  337. if (highlight)
  338. {
  339. getMenu()->clearHoverItem();
  340. }
  341. if (mHighlight != highlight)
  342. {
  343. dirtyRect();
  344. }
  345. mHighlight = highlight;
  346. }
  347. BOOL LLMenuItemGL::handleKeyHere( KEY key, MASK mask )
  348. {
  349. if (getHighlight() &&
  350. getMenu()->isOpen())
  351. {
  352. if (key == KEY_UP)
  353. {
  354. // switch to keyboard navigation mode
  355. LLMenuGL::setKeyboardMode(TRUE);
  356. getMenu()->highlightPrevItem(this);
  357. return TRUE;
  358. }
  359. else if (key == KEY_DOWN)
  360. {
  361. // switch to keyboard navigation mode
  362. LLMenuGL::setKeyboardMode(TRUE);
  363. getMenu()->highlightNextItem(this);
  364. return TRUE;
  365. }
  366. else if (key == KEY_RETURN && mask == MASK_NONE)
  367. {
  368. // switch to keyboard navigation mode
  369. LLMenuGL::setKeyboardMode(TRUE);
  370. onCommit();
  371. return TRUE;
  372. }
  373. }
  374. return FALSE;
  375. }
  376. BOOL LLMenuItemGL::handleMouseUp( S32 x, S32 y, MASK mask)
  377. {
  378. // switch to mouse navigation mode
  379. LLMenuGL::setKeyboardMode(FALSE);
  380. onCommit();
  381. make_ui_sound("UISndClickRelease");
  382. return LLView::handleMouseUp(x, y, mask);
  383. }
  384. BOOL LLMenuItemGL::handleMouseDown( S32 x, S32 y, MASK mask)
  385. {
  386. // switch to mouse navigation mode
  387. LLMenuGL::setKeyboardMode(FALSE);
  388. setHighlight(TRUE);
  389. return LLView::handleMouseDown(x, y, mask);
  390. }
  391. BOOL LLMenuItemGL::handleScrollWheel( S32 x, S32 y, S32 clicks )
  392. {
  393. // If the menu is scrollable let it handle the wheel event.
  394. return !getMenu()->isScrollable();
  395. }
  396. void LLMenuItemGL::draw( void )
  397. {
  398. // *FIX: This can be optimized by using switches. Want to avoid
  399. // that until the functionality is finalized.
  400. // HACK: Brief items don't highlight. Pie menu takes care of it. JC
  401. // let disabled items be highlighted, just don't draw them as such
  402. if( getEnabled() && getHighlight() && !mBriefItem)
  403. {
  404. int debug_count = 0;
  405. if (dynamic_cast<LLMenuItemCallGL*>(this))
  406. debug_count++;
  407. gGL.color4fv( mHighlightBackground.get().mV );
  408. gl_rect_2d( 0, getRect().getHeight(), getRect().getWidth(), 0 );
  409. }
  410. LLColor4 color;
  411. if ( getEnabled() && getHighlight() )
  412. {
  413. color = mHighlightForeground.get();
  414. }
  415. else if( getEnabled() && !mDrawTextDisabled )
  416. {
  417. color = mEnabledColor.get();
  418. }
  419. else
  420. {
  421. color = mDisabledColor.get();
  422. }
  423. // Draw the text on top.
  424. if (mBriefItem)
  425. {
  426. mFont->render( mLabel, 0, BRIEF_PAD_PIXELS / 2, 0, color,
  427. LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL);
  428. }
  429. else
  430. {
  431. if( !mDrawBoolLabel.empty() )
  432. {
  433. mFont->render( mDrawBoolLabel.getWString(), 0, (F32)LEFT_PAD_PIXELS, ((F32)MENU_ITEM_PADDING / 2.f) + 1.f, color,
  434. LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL, LLFontGL::NO_SHADOW, S32_MAX, S32_MAX, NULL, FALSE );
  435. }
  436. mFont->render( mLabel.getWString(), 0, (F32)LEFT_PLAIN_PIXELS, ((F32)MENU_ITEM_PADDING / 2.f) + 1.f, color,
  437. LLFontGL::LEFT, LLFontGL::BOTTOM, LLFontGL::NORMAL, LLFontGL::NO_SHADOW, S32_MAX, S32_MAX, NULL, FALSE );
  438. if( !mDrawAccelLabel.empty() )
  439. {
  440. mFont->render( mDrawAccelLabel.getWString(), 0, (F32)getRect().mRight - (F32)RIGHT_PLAIN_PIXELS, ((F32)MENU_ITEM_PADDING / 2.f) + 1.f, color,
  441. LLFontGL::RIGHT, LLFontGL::BOTTOM, LLFontGL::NORMAL, LLFontGL::NO_SHADOW, S32_MAX, S32_MAX, NULL, FALSE );
  442. }
  443. if( !mDrawBranchLabel.empty() )
  444. {
  445. mFont->render( mDrawBranchLabel.getWString(), 0, (F32)getRect().mRight - (F32)RIGHT_PAD_PIXELS, ((F32)MENU_ITEM_PADDING / 2.f) + 1.f, color,
  446. LLFontGL::RIGHT, LLFontGL::BOTTOM, LLFontGL::NORMAL, LLFontGL::NO_SHADOW, S32_MAX, S32_MAX, NULL, FALSE );
  447. }
  448. }
  449. // underline "jump" key only when keyboard navigation has been initiated
  450. if (getMenu()->jumpKeysActive() && LLMenuGL::getKeyboardMode())
  451. {
  452. std::string upper_case_label = mLabel.getString();
  453. LLStringUtil::toUpper(upper_case_label);
  454. std::string::size_type offset = upper_case_label.find(mJumpKey);
  455. if (offset != std::string::npos)
  456. {
  457. S32 x_begin = LEFT_PLAIN_PIXELS + mFont->getWidth(mLabel, 0, offset);
  458. S32 x_end = LEFT_PLAIN_PIXELS + mFont->getWidth(mLabel, 0, offset + 1);
  459. gl_line_2d(x_begin, (MENU_ITEM_PADDING / 2) + 1, x_end, (MENU_ITEM_PADDING / 2) + 1);
  460. }
  461. }
  462. // clear got hover every frame
  463. setHover(FALSE);
  464. }
  465. BOOL LLMenuItemGL::setLabelArg( const std::string& key, const LLStringExplicit& text )
  466. {
  467. mLabel.setArg(key, text);
  468. return TRUE;
  469. }
  470. void LLMenuItemGL::handleVisibilityChange(BOOL new_visibility)
  471. {
  472. if (getMenu())
  473. {
  474. getMenu()->needsArrange();
  475. }
  476. LLView::handleVisibilityChange(new_visibility);
  477. }
  478. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  479. // Class LLMenuItemSeparatorGL
  480. //
  481. // This class represents a separator.
  482. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  483. LLMenuItemSeparatorGL::Params::Params()
  484. {
  485. }
  486. LLMenuItemSeparatorGL::LLMenuItemSeparatorGL(const LLMenuItemSeparatorGL::Params& p) :
  487. LLMenuItemGL( p )
  488. {
  489. }
  490. //virtual
  491. U32 LLMenuItemSeparatorGL::getNominalHeight( void ) const
  492. {
  493. return SEPARATOR_HEIGHT_PIXELS;
  494. }
  495. void LLMenuItemSeparatorGL::draw( void )
  496. {
  497. gGL.color4fv( mDisabledColor.get().mV );
  498. const S32 y = getRect().getHeight() / 2;
  499. const S32 PAD = 6;
  500. gl_line_2d( PAD, y, getRect().getWidth() - PAD, y );
  501. }
  502. BOOL LLMenuItemSeparatorGL::handleMouseDown(S32 x, S32 y, MASK mask)
  503. {
  504. LLMenuGL* parent_menu = getMenu();
  505. if (y > getRect().getHeight() / 2)
  506. {
  507. // the menu items are in the child list in bottom up order
  508. LLView* prev_menu_item = parent_menu->findNextSibling(this);
  509. return prev_menu_item ? prev_menu_item->handleMouseDown(x, prev_menu_item->getRect().getHeight(), mask) : FALSE;
  510. }
  511. else
  512. {
  513. LLView* next_menu_item = parent_menu->findPrevSibling(this);
  514. return next_menu_item ? next_menu_item->handleMouseDown(x, 0, mask) : FALSE;
  515. }
  516. }
  517. BOOL LLMenuItemSeparatorGL::handleMouseUp(S32 x, S32 y, MASK mask)
  518. {
  519. LLMenuGL* parent_menu = getMenu();
  520. if (y > getRect().getHeight() / 2)
  521. {
  522. LLView* prev_menu_item = parent_menu->findNextSibling(this);
  523. return prev_menu_item ? prev_menu_item->handleMouseUp(x, prev_menu_item->getRect().getHeight(), mask) : FALSE;
  524. }
  525. else
  526. {
  527. LLView* next_menu_item = parent_menu->findPrevSibling(this);
  528. return next_menu_item ? next_menu_item->handleMouseUp(x, 0, mask) : FALSE;
  529. }
  530. }
  531. BOOL LLMenuItemSeparatorGL::handleHover(S32 x, S32 y, MASK mask)
  532. {
  533. LLMenuGL* parent_menu = getMenu();
  534. if (y > getRect().getHeight() / 2)
  535. {
  536. parent_menu->highlightPrevItem(this, FALSE);
  537. return FALSE;
  538. }
  539. else
  540. {
  541. parent_menu->highlightNextItem(this, FALSE);
  542. return FALSE;
  543. }
  544. }
  545. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  546. // Class LLMenuItemVerticalSeparatorGL
  547. //
  548. // This class represents a vertical separator.
  549. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  550. class LLMenuItemVerticalSeparatorGL
  551. : public LLMenuItemSeparatorGL
  552. {
  553. public:
  554. LLMenuItemVerticalSeparatorGL( void );
  555. virtual BOOL handleMouseDown(S32 x, S32 y, MASK mask) { return FALSE; }
  556. };
  557. LLMenuItemVerticalSeparatorGL::LLMenuItemVerticalSeparatorGL( void )
  558. {
  559. setLabel( VERTICAL_SEPARATOR_LABEL );
  560. }
  561. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  562. // Class LLMenuItemTearOffGL
  563. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  564. LLMenuItemTearOffGL::LLMenuItemTearOffGL(const LLMenuItemTearOffGL::Params& p)
  565. : LLMenuItemGL(p)
  566. {
  567. }
  568. // Returns the first floater ancestor if there is one
  569. LLFloater* LLMenuItemTearOffGL::getParentFloater()
  570. {
  571. LLView* parent_view = getMenu();
  572. while (parent_view)
  573. {
  574. if (dynamic_cast<LLFloater*>(parent_view))
  575. {
  576. return dynamic_cast<LLFloater*>(parent_view);
  577. }
  578. bool parent_is_menu = dynamic_cast<LLMenuGL*>(parent_view) && !dynamic_cast<LLMenuBarGL*>(parent_view);
  579. if (parent_is_menu)
  580. {
  581. // use menu parent
  582. parent_view = dynamic_cast<LLMenuGL*>(parent_view)->getParentMenuItem();
  583. }
  584. else
  585. {
  586. // just use regular view parent
  587. parent_view = parent_view->getParent();
  588. }
  589. }
  590. return NULL;
  591. }
  592. void LLMenuItemTearOffGL::onCommit()
  593. {
  594. if (getMenu()->getTornOff())
  595. {
  596. LLTearOffMenu* torn_off_menu = (LLTearOffMenu*)(getMenu()->getParent());
  597. torn_off_menu->closeFloater();
  598. }
  599. else
  600. {
  601. // transfer keyboard focus and highlight to first real item in list
  602. if (getHighlight())
  603. {
  604. getMenu()->highlightNextItem(this);
  605. }
  606. getMenu()->needsArrange();
  607. LLFloater* parent_floater = getParentFloater();
  608. LLFloater* tear_off_menu = LLTearOffMenu::create(getMenu());
  609. if (tear_off_menu)
  610. {
  611. if (parent_floater)
  612. {
  613. parent_floater->addDependentFloater(tear_off_menu, FALSE);
  614. }
  615. // give focus to torn off menu because it will have
  616. // been taken away when parent menu closes
  617. tear_off_menu->setFocus(TRUE);
  618. }
  619. }
  620. LLMenuItemGL::onCommit();
  621. }
  622. void LLMenuItemTearOffGL::draw()
  623. {
  624. // disabled items can be highlighted, but shouldn't render as such
  625. if( getEnabled() && getHighlight() && !isBriefItem())
  626. {
  627. gGL.color4fv( mHighlightBackground.get().mV );
  628. gl_rect_2d( 0, getRect().getHeight(), getRect().getWidth(), 0 );
  629. }
  630. if (getEnabled())
  631. {
  632. gGL.color4fv( mEnabledColor.get().mV );
  633. }
  634. else
  635. {
  636. gGL.color4fv( mDisabledColor.get().mV );
  637. }
  638. const S32 y = getRect().getHeight() / 3;
  639. const S32 PAD = 6;
  640. gl_line_2d( PAD, y, getRect().getWidth() - PAD, y );
  641. gl_line_2d( PAD, y * 2, getRect().getWidth() - PAD, y * 2 );
  642. }
  643. U32 LLMenuItemTearOffGL::getNominalHeight( void ) const
  644. {
  645. return TEAROFF_SEPARATOR_HEIGHT_PIXELS;
  646. }
  647. ///============================================================================
  648. /// Class LLMenuItemCallGL
  649. ///============================================================================
  650. LLMenuItemCallGL::LLMenuItemCallGL(const LLMenuItemCallGL::Params& p)
  651. : LLMenuItemGL(p)
  652. {
  653. }
  654. void LLMenuItemCallGL::initFromParams(const Params& p)
  655. {
  656. if (p.on_visible.isProvided())
  657. {
  658. mVisibleSignal.connect(initEnableCallback(p.on_visible));
  659. }
  660. if (p.on_enable.isProvided())
  661. {
  662. setEnableCallback(initEnableCallback(p.on_enable));
  663. // Set the enabled control variable (for backwards compatability)
  664. if (p.on_enable.control_name.isProvided() && !p.on_enable.control_name().empty())
  665. {
  666. LLControlVariable* control = findControl(p.on_enable.control_name());
  667. if (control)
  668. {
  669. setEnabledControlVariable(control);
  670. }
  671. }
  672. }
  673. if (p.on_click.isProvided())
  674. {
  675. setCommitCallback(initCommitCallback(p.on_click));
  676. }
  677. LLUICtrl::initFromParams(p);
  678. }
  679. void LLMenuItemCallGL::onCommit( void )
  680. {
  681. // RN: menu item can be deleted in callback, so beware
  682. getMenu()->setItemLastSelected( this );
  683. LLMenuItemGL::onCommit();
  684. }
  685. void LLMenuItemCallGL::updateEnabled( void )
  686. {
  687. if (mEnableSignal.num_slots() > 0)
  688. {
  689. bool enabled = mEnableSignal(this, LLSD());
  690. if (mEnabledControlVariable)
  691. {
  692. if (!enabled)
  693. {
  694. // callback overrides control variable; this will call setEnabled()
  695. mEnabledControlVariable->set(false);
  696. }
  697. }
  698. else
  699. {
  700. setEnabled(enabled);
  701. }
  702. }
  703. }
  704. void LLMenuItemCallGL::updateVisible( void )
  705. {
  706. if (mVisibleSignal.num_slots() > 0)
  707. {
  708. bool visible = mVisibleSignal(this, LLSD());
  709. setVisible(visible);
  710. }
  711. }
  712. void LLMenuItemCallGL::buildDrawLabel( void )
  713. {
  714. updateEnabled();
  715. updateVisible();
  716. LLMenuItemGL::buildDrawLabel();
  717. }
  718. BOOL LLMenuItemCallGL::handleKeyHere( KEY key, MASK mask )
  719. {
  720. return LLMenuItemGL::handleKeyHere(key, mask);
  721. }
  722. BOOL LLMenuItemCallGL::handleAcceleratorKey( KEY key, MASK mask )
  723. {
  724. if( (!gKeyboard->getKeyRepeated(key) || getAllowKeyRepeat()) && (key == mAcceleratorKey) && (mask == (mAcceleratorMask & MASK_NORMALKEYS)) )
  725. {
  726. updateEnabled();
  727. if (getEnabled())
  728. {
  729. onCommit();
  730. return TRUE;
  731. }
  732. }
  733. return FALSE;
  734. }
  735. // handleRightMouseUp moved into base class LLMenuItemGL so clicks are
  736. // handled for all menu item types
  737. ///============================================================================
  738. /// Class LLMenuItemCheckGL
  739. ///============================================================================
  740. LLMenuItemCheckGL::LLMenuItemCheckGL (const LLMenuItemCheckGL::Params& p)
  741. : LLMenuItemCallGL(p)
  742. {
  743. }
  744. void LLMenuItemCheckGL::initFromParams(const Params& p)
  745. {
  746. if (p.on_check.isProvided())
  747. {
  748. setCheckCallback(initEnableCallback(p.on_check));
  749. // Set the control name (for backwards compatability)
  750. if (p.on_check.control_name.isProvided() && !p.on_check.control_name().empty())
  751. {
  752. setControlName(p.on_check.control_name());
  753. }
  754. }
  755. LLMenuItemCallGL::initFromParams(p);
  756. }
  757. void LLMenuItemCheckGL::onCommit( void )
  758. {
  759. LLMenuItemCallGL::onCommit();
  760. }
  761. //virtual
  762. void LLMenuItemCheckGL::setValue(const LLSD& value)
  763. {
  764. LLUICtrl::setValue(value);
  765. if(value.asBoolean())
  766. {
  767. mDrawBoolLabel = LLMenuGL::BOOLEAN_TRUE_PREFIX;
  768. }
  769. else
  770. {
  771. mDrawBoolLabel.clear();
  772. }
  773. }
  774. //virtual
  775. LLSD LLMenuItemCheckGL::getValue() const
  776. {
  777. // Get our boolean value from the view model.
  778. // If we don't override this method then the implementation from
  779. // LLMenuItemGL will return a string. (EXT-8501)
  780. return LLUICtrl::getValue();
  781. }
  782. // called to rebuild the draw label
  783. void LLMenuItemCheckGL::buildDrawLabel( void )
  784. {
  785. // Note: mCheckSignal() returns true if no callbacks are set
  786. bool checked = mCheckSignal(this, LLSD());
  787. if (mControlVariable)
  788. {
  789. if (!checked)
  790. setControlValue(false); // callback overrides control variable; this will call setValue()
  791. }
  792. else
  793. {
  794. setValue(checked);
  795. }
  796. if(getValue().asBoolean())
  797. {
  798. mDrawBoolLabel = LLMenuGL::BOOLEAN_TRUE_PREFIX;
  799. }
  800. else
  801. {
  802. mDrawBoolLabel.clear();
  803. }
  804. LLMenuItemCallGL::buildDrawLabel();
  805. }
  806. ///============================================================================
  807. /// Class LLMenuItemBranchGL
  808. ///============================================================================
  809. LLMenuItemBranchGL::LLMenuItemBranchGL(const LLMenuItemBranchGL::Params& p)
  810. : LLMenuItemGL(p)
  811. {
  812. LLMenuGL* branch = p.branch;
  813. if (branch)
  814. {
  815. mBranchHandle = branch->getHandle();
  816. branch->setVisible(FALSE);
  817. branch->setParentMenuItem(this);
  818. }
  819. }
  820. LLMenuItemBranchGL::~LLMenuItemBranchGL()
  821. {
  822. if (mBranchHandle.get())
  823. {
  824. mBranchHandle.get()->die();
  825. }
  826. }
  827. // virtual
  828. LLView* LLMenuItemBranchGL::getChildView(const std::string& name, BOOL recurse) const
  829. {
  830. LLMenuGL* branch = getBranch();
  831. if (branch)
  832. {
  833. if (branch->getName() == name)
  834. {
  835. return branch;
  836. }
  837. // Always recurse on branches
  838. return branch->getChildView(name, recurse);
  839. }
  840. return LLView::getChildView(name, recurse);
  841. }
  842. LLView* LLMenuItemBranchGL::findChildView(const std::string& name, BOOL recurse) const
  843. {
  844. LLMenuGL* branch = getBranch();
  845. if (branch)
  846. {
  847. if (branch->getName() == name)
  848. {
  849. return branch;
  850. }
  851. // Always recurse on branches
  852. return branch->findChildView(name, recurse);
  853. }
  854. return LLView::findChildView(name, recurse);
  855. }
  856. // virtual
  857. BOOL LLMenuItemBranchGL::handleMouseUp(S32 x, S32 y, MASK mask)
  858. {
  859. // switch to mouse navigation mode
  860. LLMenuGL::setKeyboardMode(FALSE);
  861. onCommit();
  862. make_ui_sound("UISndClickRelease");
  863. return TRUE;
  864. }
  865. BOOL LLMenuItemBranchGL::handleAcceleratorKey(KEY key, MASK mask)
  866. {
  867. return getBranch() && getBranch()->handleAcceleratorKey(key, mask);
  868. }
  869. // This function checks to see if the accelerator key is already in use;
  870. // if not, it will be added to the list
  871. BOOL LLMenuItemBranchGL::addToAcceleratorList(std::list<LLKeyBinding*> *listp)
  872. {
  873. LLMenuGL* branch = getBranch();
  874. if (!branch)
  875. return FALSE;
  876. U32 item_count = branch->getItemCount();
  877. LLMenuItemGL *item;
  878. while (item_count--)
  879. {
  880. if ((item = branch->getItem(item_count)))
  881. {
  882. return item->addToAcceleratorList(listp);
  883. }
  884. }
  885. return FALSE;
  886. }
  887. // called to rebuild the draw label
  888. void LLMenuItemBranchGL::buildDrawLabel( void )
  889. {
  890. mDrawAccelLabel.clear();
  891. std::string st = mDrawAccelLabel;
  892. appendAcceleratorString( st );
  893. mDrawAccelLabel = st;
  894. mDrawBranchLabel = LLMenuGL::BRANCH_SUFFIX;
  895. }
  896. void LLMenuItemBranchGL::onCommit( void )
  897. {
  898. openMenu();
  899. // keyboard navigation automatically propagates highlight to sub-menu
  900. // to facilitate fast menu control via jump keys
  901. if (LLMenuGL::getKeyboardMode() && getBranch()&& !getBranch()->getHighlightedItem())
  902. {
  903. getBranch()->highlightNextItem(NULL);
  904. }
  905. LLUICtrl::onCommit();
  906. }
  907. BOOL LLMenuItemBranchGL::handleKey(KEY key, MASK mask, BOOL called_from_parent)
  908. {
  909. BOOL handled = FALSE;
  910. if (getBranch() && called_from_parent)
  911. {
  912. handled = getBranch()->handleKey(key, mask, called_from_parent);
  913. }
  914. if (!handled)
  915. {
  916. handled = LLMenuItemGL::handleKey(key, mask, called_from_parent);
  917. }
  918. return handled;
  919. }
  920. BOOL LLMenuItemBranchGL::handleUnicodeChar(llwchar uni_char, BOOL called_from_parent)
  921. {
  922. BOOL handled = FALSE;
  923. if (getBranch() && called_from_parent)
  924. {
  925. handled = getBranch()->handleUnicodeChar(uni_char, TRUE);
  926. }
  927. if (!handled)
  928. {
  929. handled = LLMenuItemGL::handleUnicodeChar(uni_char, called_from_parent);
  930. }
  931. return handled;
  932. }
  933. void LLMenuItemBranchGL::setHighlight( BOOL highlight )
  934. {
  935. if (highlight == getHighlight())
  936. return;
  937. LLMenuGL* branch = getBranch();
  938. if (!branch)
  939. return;
  940. BOOL auto_open = getEnabled() && (!branch->getVisible() || branch->getTornOff());
  941. // torn off menus don't open sub menus on hover unless they have focus
  942. if (getMenu()->getTornOff() && !((LLFloater*)getMenu()->getParent())->hasFocus())
  943. {
  944. auto_open = FALSE;
  945. }
  946. // don't auto open torn off sub-menus (need to explicitly active menu item to give them focus)
  947. if (branch->getTornOff())
  948. {
  949. auto_open = FALSE;
  950. }
  951. LLMenuItemGL::setHighlight(highlight);
  952. if( highlight )
  953. {
  954. if(auto_open)
  955. {
  956. openMenu();
  957. }
  958. }
  959. else
  960. {
  961. if (branch->getTornOff())
  962. {
  963. ((LLFloater*)branch->getParent())->setFocus(FALSE);
  964. branch->clearHoverItem();
  965. }
  966. else
  967. {
  968. branch->setVisible( FALSE );
  969. }
  970. }
  971. }
  972. void LLMenuItemBranchGL::draw()
  973. {
  974. LLMenuItemGL::draw();
  975. if (getBranch() && getBranch()->getVisible() && !getBranch()->getTornOff())
  976. {
  977. setHighlight(TRUE);
  978. }
  979. }
  980. void LLMenuItemBranchGL::updateBranchParent(LLView* parentp)
  981. {
  982. if (getBranch() && getBranch()->getParent() == NULL)
  983. {
  984. // make the branch menu a sibling of my parent menu
  985. getBranch()->updateParent(parentp);
  986. }
  987. }
  988. void LLMenuItemBranchGL::handleVisibilityChange( BOOL new_visibility )
  989. {
  990. if (new_visibility == FALSE && getBranch() && !getBranch()->getTornOff())
  991. {
  992. getBranch()->setVisible(FALSE);
  993. }
  994. LLMenuItemGL::handleVisibilityChange(new_visibility);
  995. }
  996. BOOL LLMenuItemBranchGL::handleKeyHere( KEY key, MASK mask )
  997. {
  998. LLMenuGL* branch = getBranch();
  999. if (!branch)
  1000. return LLMenuItemGL::handleKeyHere(key, mask);
  1001. // an item is highlighted, my menu is open, and I have an active sub menu or we are in
  1002. // keyboard navigation mode
  1003. if (getHighlight()
  1004. && getMenu()->isOpen()
  1005. && (isActive() || LLMenuGL::getKeyboardMode()))
  1006. {
  1007. if (branch->getVisible() && key == KEY_LEFT)
  1008. {
  1009. // switch to keyboard navigation mode
  1010. LLMenuGL::setKeyboardMode(TRUE);
  1011. BOOL handled = branch->clearHoverItem();
  1012. if (branch->getTornOff())
  1013. {
  1014. ((LLFloater*)branch->getParent())->setFocus(FALSE);
  1015. }
  1016. if (handled && getMenu()->getTornOff())
  1017. {
  1018. ((LLFloater*)getMenu()->getParent())->setFocus(TRUE);
  1019. }
  1020. return handled;
  1021. }
  1022. if (key == KEY_RIGHT && !branch->getHighlightedItem())
  1023. {
  1024. // switch to keyboard navigation mode
  1025. LLMenuGL::setKeyboardMode(TRUE);
  1026. LLMenuItemGL* itemp = branch->highlightNextItem(NULL);
  1027. if (itemp)
  1028. {
  1029. return TRUE;
  1030. }
  1031. }
  1032. }
  1033. return LLMenuItemGL::handleKeyHere(key, mask);
  1034. }
  1035. //virtual
  1036. BOOL LLMenuItemBranchGL::isActive() const
  1037. {
  1038. return isOpen() && getBranch() && getBranch()->getHighlightedItem();
  1039. }
  1040. //virtual
  1041. BOOL LLMenuItemBranchGL::isOpen() const
  1042. {
  1043. return getBranch() && getBranch()->isOpen();
  1044. }
  1045. void LLMenuItemBranchGL::openMenu()
  1046. {
  1047. LLMenuGL* branch = getBranch();
  1048. if (!branch)
  1049. return;
  1050. if (branch->getTornOff())
  1051. {
  1052. gFloaterView->bringToFront((LLFloater*)branch->getParent());
  1053. // this might not be necessary, as torn off branches don't get focus and hence no highligth
  1054. branch->highlightNextItem(NULL);
  1055. }
  1056. else if( !branch->getVisible() )
  1057. {
  1058. // get valid rectangle for menus
  1059. const LLRect menu_region_rect = LLMenuGL::sMenuContainer->getMenuRect();
  1060. branch->arrange();
  1061. LLRect branch_rect = branch->getRect();
  1062. // calculate root-view relative position for branch menu
  1063. S32 left = getRect().mRight;
  1064. S32 top = getRect().mTop - getRect().mBottom;
  1065. localPointToOtherView(left, top, &left, &top, branch->getParent());
  1066. branch_rect.setLeftTopAndSize( left, top,
  1067. branch_rect.getWidth(), branch_rect.getHeight() );
  1068. if (branch->getCanTearOff())
  1069. {
  1070. branch_rect.translate(0, TEAROFF_SEPARATOR_HEIGHT_PIXELS);
  1071. }
  1072. branch->setRect( branch_rect );
  1073. // if branch extends outside of menu region change the direction it opens in
  1074. S32 x, y;
  1075. S32 delta_x = 0;
  1076. S32 delta_y = 0;
  1077. branch->localPointToOtherView( 0, 0, &x, &y, branch->getParent() );
  1078. if( y < menu_region_rect.mBottom )
  1079. {
  1080. // open upwards if menu extends past bottom
  1081. // adjust by the height of the menu item branch since it is a submenu
  1082. delta_y = branch_rect.getHeight() - getRect().getHeight();
  1083. }
  1084. if( x + branch_rect.getWidth() > menu_region_rect.mRight )
  1085. {
  1086. // move sub-menu over to left side
  1087. delta_x = llmax(-x, ( -(branch_rect.getWidth() + getRect().getWidth())));
  1088. }
  1089. branch->translate( delta_x, delta_y );
  1090. branch->setVisible( TRUE );
  1091. branch->getParent()->sendChildToFront(branch);
  1092. dirtyRect();
  1093. }
  1094. }
  1095. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1096. // Class LLMenuItemBranchDownGL
  1097. //
  1098. // The LLMenuItemBranchDownGL represents a menu item that has a
  1099. // sub-menu. This is used to make menu bar menus.
  1100. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1101. class LLMenuItemBranchDownGL : public LLMenuItemBranchGL
  1102. {
  1103. protected:
  1104. public:
  1105. LLMenuItemBranchDownGL( const Params& );
  1106. // returns the normal width of this control in pixels - this is
  1107. // used for calculating the widest item, as well as for horizontal
  1108. // arrangement.
  1109. virtual U32 getNominalWidth( void ) const;
  1110. // called to rebuild the draw label
  1111. virtual void buildDrawLabel( void );
  1112. // handles opening, positioning, and arranging the menu branch associated with this item
  1113. virtual void openMenu( void );
  1114. // set the hover status (called by it's menu) and if the object is
  1115. // active. This is used for behavior transfer.
  1116. virtual void setHighlight( BOOL highlight );
  1117. virtual BOOL isActive( void ) const;
  1118. // LLView functionality
  1119. virtual BOOL handleMouseDown( S32 x, S32 y, MASK mask );
  1120. virtual BOOL handleMouseUp( S32 x, S32 y, MASK mask );
  1121. virtual void draw( void );
  1122. virtual BOOL handleKeyHere(KEY key, MASK mask);
  1123. virtual BOOL handleAcceleratorKey(KEY key, MASK mask);
  1124. };
  1125. LLMenuItemBranchDownGL::LLMenuItemBranchDownGL( const Params& p) :
  1126. LLMenuItemBranchGL(p)
  1127. {
  1128. }
  1129. // returns the normal width of this control in pixels - this is used
  1130. // for calculating the widest item, as well as for horizontal
  1131. // arrangement.
  1132. U32 LLMenuItemBranchDownGL::getNominalWidth( void ) const
  1133. {
  1134. U32 width = LEFT_PAD_PIXELS + LEFT_WIDTH_PIXELS + RIGHT_PAD_PIXELS;
  1135. width += getFont()->getWidth( mLabel.getWString().c_str() );
  1136. return width;
  1137. }
  1138. // called to rebuild the draw label
  1139. void LLMenuItemBranchDownGL::buildDrawLabel( void )
  1140. {
  1141. mDrawAccelLabel.clear();
  1142. std::string st = mDrawAccelLabel;
  1143. appendAcceleratorString( st );
  1144. mDrawAccelLabel = st;
  1145. }
  1146. void LLMenuItemBranchDownGL::openMenu( void )
  1147. {
  1148. LLMenuGL* branch = getBranch();
  1149. if( branch->getVisible() && !branch->getTornOff() )
  1150. {
  1151. branch->setVisible( FALSE );
  1152. }
  1153. else
  1154. {
  1155. if (branch->getTornOff())
  1156. {
  1157. gFloaterView->bringToFront((LLFloater*)branch->getParent());
  1158. }
  1159. else
  1160. {
  1161. // We're showing the drop-down menu, so patch up its labels/rects
  1162. branch->arrange();
  1163. LLRect rect = branch->getRect();
  1164. S32 left = 0;
  1165. S32 top = getRect().mBottom;
  1166. localPointToOtherView(left, top, &left, &top, branch->getParent());
  1167. rect.setLeftTopAndSize( left, top,
  1168. rect.getWidth(), rect.getHeight() );
  1169. branch->setRect( rect );
  1170. S32 x = 0;
  1171. S32 y = 0;
  1172. branch->localPointToScreen( 0, 0, &x, &y );
  1173. S32 delta_x = 0;
  1174. LLCoordScreen window_size;
  1175. LLWindow* windowp = getWindow();
  1176. windowp->getSize(&window_size);
  1177. S32 window_width = window_size.mX;
  1178. if( x > window_width - rect.getWidth() )
  1179. {
  1180. delta_x = (window_width - rect.getWidth()) - x;
  1181. }
  1182. branch->translate( delta_x, 0 );
  1183. setHighlight(TRUE);
  1184. branch->setVisible( TRUE );
  1185. branch->getParent()->sendChildToFront(branch);
  1186. }
  1187. }
  1188. }
  1189. // set the hover status (called by it's menu)
  1190. void LLMenuItemBranchDownGL::setHighlight( BOOL highlight )
  1191. {
  1192. if (highlight == getHighlight())
  1193. return;
  1194. //NOTE: Purposely calling all the way to the base to bypass auto-open.
  1195. LLMenuItemGL::setHighlight(highlight);
  1196. LLMenuGL* branch = getBranch();
  1197. if (!branch)
  1198. return;
  1199. if( !highlight)
  1200. {
  1201. if (branch->getTornOff())
  1202. {
  1203. ((LLFloater*)branch->getParent())->setFocus(FALSE);
  1204. branch->clearHoverItem();
  1205. }
  1206. else
  1207. {
  1208. branch->setVisible( FALSE );
  1209. }
  1210. }
  1211. }
  1212. BOOL LLMenuItemBranchDownGL::isActive() const
  1213. {
  1214. // for top level menus, being open is sufficient to be considered
  1215. // active, because clicking on them with the mouse will open
  1216. // them, without moving keyboard focus to them
  1217. return isOpen();
  1218. }
  1219. BOOL LLMenuItemBranchDownGL::handleMouseDown( S32 x, S32 y, MASK mask )
  1220. {
  1221. // switch to mouse control mode
  1222. LLMenuGL::setKeyboardMode(FALSE);
  1223. onCommit();
  1224. make_ui_sound("UISndClick");
  1225. return TRUE;
  1226. }
  1227. BOOL LLMenuItemBranchDownGL::handleMouseUp( S32 x, S32 y, MASK mask )
  1228. {
  1229. return TRUE;
  1230. }
  1231. BOOL LLMenuItemBranchDownGL::handleAcceleratorKey(KEY key, MASK mask)
  1232. {
  1233. BOOL branch_visible = getBranch()->getVisible();
  1234. BOOL handled = getBranch()->handleAcceleratorKey(key, mask);
  1235. if (handled && !branch_visible && isInVisibleChain())
  1236. {
  1237. // flash this menu entry because we triggered an invisible menu item
  1238. LLMenuHolderGL::setActivatedItem(this);
  1239. }
  1240. return handled;
  1241. }
  1242. BOOL LLMenuItemBranchDownGL::handleKeyHere(KEY key, MASK mask)
  1243. {
  1244. BOOL menu_open = getBranch()->getVisible();
  1245. // don't do keyboard navigation of top-level menus unless in keyboard mode, or menu expanded
  1246. if (getHighlight() && getMenu()->isOpen() && (isActive() || LLMenuGL::getKeyboardMode()))
  1247. {
  1248. if (key == KEY_LEFT)
  1249. {
  1250. // switch to keyboard navigation mode
  1251. LLMenuGL::setKeyboardMode(TRUE);
  1252. LLMenuItemGL* itemp = getMenu()->highlightPrevItem(this);
  1253. // open new menu only if previous menu was open
  1254. if (itemp && itemp->getEnabled() && menu_open)
  1255. {
  1256. itemp->onCommit();
  1257. }
  1258. return TRUE;
  1259. }
  1260. else if (key == KEY_RIGHT)
  1261. {
  1262. // switch to keyboard navigation mode
  1263. LLMenuGL::setKeyboardMode(TRUE);
  1264. LLMenuItemGL* itemp = getMenu()->highlightNextItem(this);
  1265. // open new menu only if previous menu was open
  1266. if (itemp && itemp->getEnabled() && menu_open)
  1267. {
  1268. itemp->onCommit();
  1269. }
  1270. return TRUE;
  1271. }
  1272. else if (key == KEY_DOWN)
  1273. {
  1274. // switch to keyboard navigation mode
  1275. LLMenuGL::setKeyboardMode(TRUE);
  1276. if (!isActive())
  1277. {
  1278. onCommit();
  1279. }
  1280. getBranch()->highlightNextItem(NULL);
  1281. return TRUE;
  1282. }
  1283. else if (key == KEY_UP)
  1284. {
  1285. // switch to keyboard navigation mode
  1286. LLMenuGL::setKeyboardMode(TRUE);
  1287. if (!isActive())
  1288. {
  1289. onCommit();
  1290. }
  1291. getBranch()->highlightPrevItem(NULL);
  1292. return TRUE;
  1293. }
  1294. }
  1295. return FALSE;
  1296. }
  1297. void LLMenuItemBranchDownGL::draw( void )
  1298. {
  1299. //FIXME: try removing this
  1300. if (getBranch()->getVisible() && !getBranch()->getTornOff())
  1301. {
  1302. setHighlight(TRUE);
  1303. }
  1304. if( getHighlight() )
  1305. {
  1306. gGL.color4fv( mHighlightBackground.get().mV );
  1307. gl_rect_2d( 0, getRect().getHeight(), getRect().getWidth(), 0 );
  1308. }
  1309. LLColor4 color;
  1310. if (getHighlight())
  1311. {
  1312. color = mHighlightForeground.get();
  1313. }
  1314. else if( getEnabled() )
  1315. {
  1316. color = mEnabledColor.get();
  1317. }
  1318. else
  1319. {
  1320. color = mDisabledColor.get();
  1321. }
  1322. getFont()->render( mLabel.getWString(), 0, (F32)getRect().getWidth() / 2.f, (F32)LABEL_BOTTOM_PAD_PIXELS, color,
  1323. LLFontGL::HCENTER, LLFontGL::BOTTOM, LLFontGL::NORMAL);
  1324. // underline navigation key only when keyboard navigation has been initiated
  1325. if (getMenu()->jumpKeysActive() && LLMenuGL::getKeyboardMode())
  1326. {
  1327. std::string upper_case_label = mLabel.getString();
  1328. LLStringUtil::toUpper(upper_case_label);
  1329. std::string::size_type offset = upper_case_label.find(getJumpKey());
  1330. if (offset != std::string::npos)
  1331. {
  1332. S32 x_offset = llround((F32)getRect().getWidth() / 2.f - getFont()->getWidthF32(mLabel.getString(), 0, S32_MAX) / 2.f);
  1333. S32 x_begin = x_offset + getFont()->getWidth(mLabel, 0, offset);
  1334. S32 x_end = x_offset + getFont()->getWidth(mLabel, 0, offset + 1);
  1335. gl_line_2d(x_begin, LABEL_BOTTOM_PAD_PIXELS, x_end, LABEL_BOTTOM_PAD_PIXELS);
  1336. }
  1337. }
  1338. // reset every frame so that we only show highlight
  1339. // when we get hover events on that frame
  1340. setHover(FALSE);
  1341. }
  1342. class LLMenuScrollItem : public LLMenuItemCallGL
  1343. {
  1344. public:
  1345. enum EArrowType
  1346. {
  1347. ARROW_DOWN,
  1348. ARROW_UP
  1349. };
  1350. struct ArrowTypes : public LLInitParam::TypeValuesHelper<EArrowType, ArrowTypes>
  1351. {
  1352. static void declareValues()
  1353. {
  1354. declare("up", ARROW_UP);
  1355. declare("down", ARROW_DOWN);
  1356. }
  1357. };
  1358. struct Params : public LLInitParam::Block<Params, LLMenuItemCallGL::Params>
  1359. {
  1360. Optional<EArrowType, ArrowTypes> arrow_type;
  1361. Optional<CommitCallbackParam> scroll_callback;
  1362. };
  1363. protected:
  1364. LLMenuScrollItem(const Params&);
  1365. friend class LLUICtrlFactory;
  1366. public:
  1367. /*virtual*/ void draw();
  1368. /*virtual*/ void reshape(S32 width, S32 height, BOOL called_from_parent);
  1369. /*virtual*/ void setEnabled(BOOL enabled);
  1370. virtual void onCommit( void );
  1371. private:
  1372. LLButton* mArrowBtn;
  1373. };
  1374. LLMenuScrollItem::LLMenuScrollItem(const Params& p)
  1375. : LLMenuItemCallGL(p)
  1376. {
  1377. std::string icon;
  1378. if (p.arrow_type.isProvided() && p.arrow_type == ARROW_UP)
  1379. {
  1380. icon = "arrow_up.tga";
  1381. }
  1382. else
  1383. {
  1384. icon = "arrow_down.tga";
  1385. }
  1386. LLButton::Params bparams;
  1387. // Disabled the Return key handling by LLMenuScrollItem instead of
  1388. // passing the key press to the currently selected menu item. See STORM-385.
  1389. bparams.commit_on_return(false);
  1390. bparams.mouse_opaque(true);
  1391. bparams.scale_image(false);
  1392. bparams.click_callback(p.scroll_callback);
  1393. bparams.mouse_held_callback(p.scroll_callback);
  1394. bparams.follows.flags(FOLLOWS_ALL);
  1395. std::string background = "transparent.j2c";
  1396. bparams.image_unselected.name(background);
  1397. bparams.image_disabled.name(background);
  1398. bparams.image_selected.name(background);
  1399. bparams.image_hover_selected.name(background);
  1400. bparams.image_disabled_selected.name(background);
  1401. bparams.image_hover_unselected.name(background);
  1402. bparams.image_overlay.name(icon);
  1403. mArrowBtn = LLUICtrlFactory::create<LLButton>(bparams);
  1404. addChild(mArrowBtn);
  1405. }
  1406. /*virtual*/
  1407. void LLMenuScrollItem::draw()
  1408. {
  1409. LLUICtrl::draw();
  1410. }
  1411. /*virtual*/
  1412. void LLMenuScrollItem::reshape(S32 width, S32 height, BOOL called_from_parent)
  1413. {
  1414. mArrowBtn->reshape(width, height, called_from_parent);
  1415. LLView::reshape(width, height, called_from_parent);
  1416. }
  1417. /*virtual*/
  1418. void LLMenuScrollItem::setEnabled(BOOL enabled)
  1419. {
  1420. mArrowBtn->setEnabled(enabled);
  1421. LLView::setEnabled(enabled);
  1422. }
  1423. void LLMenuScrollItem::onCommit( void )
  1424. {
  1425. LLUICtrl::onCommit();
  1426. }
  1427. ///============================================================================
  1428. /// Class LLMenuGL
  1429. ///============================================================================
  1430. LLMenuGL::LLMenuGL(const LLMenuGL::Params& p)
  1431. : LLUICtrl(p),
  1432. mBackgroundColor( p.bg_color() ),
  1433. mBgVisible( p.bg_visible ),
  1434. mDropShadowed( p.drop_shadow ),
  1435. mHasSelection(false),
  1436. mHorizontalLayout( p.horizontal_layout ),
  1437. mScrollable(mHorizontalLayout ? FALSE : p.scrollable), // Scrolling is supported only for vertical layout
  1438. mMaxScrollableItems(p.max_scrollable_items),
  1439. mPreferredWidth(p.preferred_width),
  1440. mKeepFixedSize( p.keep_fixed_size ),
  1441. mLabel (p.label),
  1442. mLastMouseX(0),
  1443. mLastMouseY(0),
  1444. mMouseVelX(0),
  1445. mMouseVelY(0),
  1446. mTornOff(FALSE),
  1447. mTearOffItem(NULL),
  1448. mSpilloverBranch(NULL),
  1449. mFirstVisibleItem(NULL),
  1450. mArrowUpItem(NULL),
  1451. mArrowDownItem(NULL),
  1452. mSpilloverMenu(NULL),
  1453. mJumpKey(p.jump_key),
  1454. mCreateJumpKeys(p.create_jump_keys),
  1455. mNeedsArrange(FALSE),
  1456. mResetScrollPositionOnShow(true),
  1457. mShortcutPad(p.shortcut_pad)
  1458. {
  1459. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  1460. boost::char_separator<char> sep("_");
  1461. tokenizer tokens(p.label(), sep);
  1462. tokenizer::iterator token_iter;
  1463. S32 token_count = 0;
  1464. std::string new_menu_label;
  1465. for( token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
  1466. {
  1467. new_menu_label += (*token_iter);
  1468. if (token_count > 0)
  1469. {
  1470. setJumpKey((*token_iter).c_str()[0]);
  1471. }
  1472. ++token_count;
  1473. }
  1474. setLabel(new_menu_label);
  1475. mFadeTimer.stop();
  1476. }
  1477. void LLMenuGL::initFromParams(const LLMenuGL::Params& p)
  1478. {
  1479. LLUICtrl::initFromParams(p);
  1480. setCanTearOff(p.can_tear_off);
  1481. }
  1482. // Destroys the object
  1483. LLMenuGL::~LLMenuGL( void )
  1484. {
  1485. // delete the branch, as it might not be in view hierarchy
  1486. // leave the menu, because it is always in view hierarchy
  1487. delete mSpilloverBranch;
  1488. mJumpKeys.clear();
  1489. }
  1490. void LLMenuGL::setCanTearOff(BOOL tear_off)
  1491. {
  1492. if (tear_off && mTearOffItem == NULL)
  1493. {
  1494. LLMenuItemTearOffGL::Params p;
  1495. mTearOffItem = LLUICtrlFactory::create<LLMenuItemTearOffGL>(p);
  1496. addChild(mTearOffItem);
  1497. }
  1498. else if (!tear_off && mTearOffItem != NULL)
  1499. {
  1500. mItems.remove(mTearOffItem);
  1501. removeChild(mTearOffItem);
  1502. delete mTearOffItem;
  1503. mTearOffItem = NULL;
  1504. needsArrange();
  1505. }
  1506. }
  1507. bool LLMenuGL::addChild(LLView* view, S32 tab_group)
  1508. {
  1509. if (LLMenuGL* menup = dynamic_cast<LLMenuGL*>(view))
  1510. {
  1511. appendMenu(menup);
  1512. return true;
  1513. }
  1514. else if (LLMenuItemGL* itemp = dynamic_cast<LLMenuItemGL*>(view))
  1515. {
  1516. append(itemp);
  1517. return true;
  1518. }
  1519. return false;
  1520. }
  1521. void LLMenuGL::removeChild( LLView* ctrl)
  1522. {
  1523. // previously a dynamic_cast with if statement to check validity
  1524. // unfortunately removeChild is called by ~LLView, and at that point the
  1525. // object being deleted is no longer a LLMenuItemGL so a dynamic_cast will fail
  1526. LLMenuItemGL* itemp = static_cast<LLMenuItemGL*>(ctrl);
  1527. item_list_t::iterator found_it = std::find(mItems.begin(), mItems.end(), (itemp));
  1528. if (found_it != mItems.end())
  1529. {
  1530. mItems.erase(found_it);
  1531. }
  1532. return LLUICtrl::removeChild(ctrl);
  1533. }
  1534. BOOL LLMenuGL::postBuild()
  1535. {
  1536. createJumpKeys();
  1537. return LLUICtrl::postBuild();
  1538. }
  1539. // are we the childmost active menu and hence our jump keys should be enabled?
  1540. // or are we a free-standing torn-off menu (which uses jump keys too)
  1541. BOOL LLMenuGL::jumpKeysActive()
  1542. {
  1543. LLMenuItemGL* highlighted_item = getHighlightedItem();
  1544. BOOL active = getVisible() && getEnabled();
  1545. if (getTornOff())
  1546. {
  1547. // activation of jump keys on torn off menus controlled by keyboard focus
  1548. active = active && ((LLFloater*)getParent())->hasFocus();
  1549. }
  1550. else
  1551. {
  1552. // Are we the terminal active menu?
  1553. // Yes, if parent menu item deems us to be active (just being visible is sufficient for top-level menus)
  1554. // and we don't have a highlighted menu item pointing to an active sub-menu
  1555. active = active && (!getParentMenuItem() || getParentMenuItem()->isActive()) // I have a parent that is active...
  1556. && (!highlighted_item || !highlighted_item->isActive()); //... but no child that is active
  1557. }
  1558. return active;
  1559. }
  1560. BOOL LLMenuGL::isOpen()
  1561. {
  1562. if (getTornOff())
  1563. {
  1564. LLMenuItemGL* itemp = getHighlightedItem();
  1565. // if we have an open sub-menu, then we are considered part of
  1566. // the open menu chain even if we don't have focus
  1567. if (itemp && itemp->isOpen())
  1568. {
  1569. return TRUE;
  1570. }
  1571. // otherwise we are only active if we have keyboard focus
  1572. return ((LLFloater*)getParent())->hasFocus();
  1573. }
  1574. else
  1575. {
  1576. // normally, menus are hidden as soon as the user focuses
  1577. // on another menu, so just use the visibility criterion
  1578. return getVisible();
  1579. }
  1580. }
  1581. bool LLMenuGL::scrollItems(EScrollingDirection direction)
  1582. {
  1583. // Slowing down items scrolling when arrow button is held
  1584. if (mScrollItemsTimer.hasExpired() && NULL != mFirstVisibleItem)
  1585. {
  1586. mScrollItemsTimer.setTimerExpirySec(.033f);
  1587. }
  1588. else
  1589. {
  1590. return false;
  1591. }
  1592. switch (direction)
  1593. {
  1594. case SD_UP:
  1595. {
  1596. item_list_t::iterator cur_item_iter;
  1597. item_list_t::iterator prev_item_iter;
  1598. for (cur_item_iter = mItems.begin(), prev_item_iter = mItems.begin(); cur_item_iter != mItems.end(); cur_item_iter++)
  1599. {
  1600. if( (*cur_item_iter) == mFirstVisibleItem)
  1601. {
  1602. break;
  1603. }
  1604. if ((*cur_item_iter)->getVisible())
  1605. {
  1606. prev_item_iter = cur_item_iter;
  1607. }
  1608. }
  1609. if ((*prev_item_iter)->getVisible())
  1610. {
  1611. mFirstVisibleItem = *prev_item_iter;
  1612. }
  1613. break;
  1614. }
  1615. case SD_DOWN:
  1616. {
  1617. if (NULL == mFirstVisibleItem)
  1618. {
  1619. mFirstVisibleItem = *mItems.begin();
  1620. }
  1621. item_list_t::iterator cur_item_iter;
  1622. for (cur_item_iter = mItems.begin(); cur_item_iter != mItems.end(); cur_item_iter++)
  1623. {
  1624. if( (*cur_item_iter) == mFirstVisibleItem)
  1625. {
  1626. break;
  1627. }
  1628. }
  1629. item_list_t::iterator next_item_iter;
  1630. if (cur_item_iter != mItems.end())
  1631. {
  1632. for (next_item_iter = ++cur_item_iter; next_item_iter != mItems.end(); next_item_iter++)
  1633. {
  1634. if( (*next_item_iter)->getVisible())
  1635. {
  1636. break;
  1637. }
  1638. }
  1639. if (next_item_iter != mItems.end() &&
  1640. (*next_item_iter)->getVisible())
  1641. {
  1642. mFirstVisibleItem = *next_item_iter;
  1643. }
  1644. }
  1645. break;
  1646. }
  1647. case SD_BEGIN:
  1648. {
  1649. mFirstVisibleItem = *mItems.begin();
  1650. break;
  1651. }
  1652. case SD_END:
  1653. {
  1654. item_list_t::reverse_iterator first_visible_item_iter = mItems.rend();
  1655. // Need to scroll through number of actual existing items in menu.
  1656. // Otherwise viewer will hang for a time needed to scroll U32_MAX
  1657. // times in std::advance(). STORM-659.
  1658. size_t nitems = mItems.size();
  1659. U32 scrollable_items = nitems < mMaxScrollableItems ? nitems : mMaxScrollableItems;
  1660. // Advance by mMaxScrollableItems back from the end of the list
  1661. // to make the last item visible.
  1662. std::advance(first_visible_item_iter, scrollable_items);
  1663. mFirstVisibleItem = *first_visible_item_iter;
  1664. break;
  1665. }
  1666. default:
  1667. llwarns << "Unknown scrolling direction: " << direction << llendl;
  1668. }
  1669. mNeedsArrange = TRUE;
  1670. arrangeAndClear();
  1671. return true;
  1672. }
  1673. // rearrange the child rects so they fit the shape of the menu.
  1674. void LLMenuGL::arrange( void )
  1675. {
  1676. // calculate the height & width, and set our rect based on that
  1677. // information.
  1678. const LLRect& initial_rect = getRect();
  1679. U32 width = 0, height = MENU_ITEM_PADDING;
  1680. cleanupSpilloverBranch();
  1681. if( mItems.size() )
  1682. {
  1683. const LLRect menu_region_rect = LLMenuGL::sMenuContainer ? LLMenuGL::sMenuContainer->getMenuRect() : LLRect(0, S32_MAX, S32_MAX, 0);
  1684. // torn off menus are not constrained to the size of the screen
  1685. U32 max_width = getTornOff() ? U32_MAX : menu_region_rect.getWidth();
  1686. U32 max_height = U32_MAX;
  1687. if (!getTornOff())
  1688. {
  1689. max_height = getRect().mTop - menu_region_rect.mBottom;
  1690. if (menu_region_rect.mTop - getRect().mTop > (S32)max_height)
  1691. {
  1692. max_height = menu_region_rect.mTop - getRect().mTop;
  1693. }
  1694. }
  1695. // *FIX: create the item first and then ask for its dimensions?
  1696. S32 spillover_item_width = PLAIN_PAD_PIXELS + LLFontGL::getFontSansSerif()->getWidth( std::string("More") ); // *TODO: Translate
  1697. S32 spillover_item_height = llround(LLFontGL::getFontSansSerif()->getLineHeight()) + MENU_ITEM_PADDING;
  1698. // Scrolling support
  1699. item_list_t::iterator first_visible_item_iter;
  1700. item_list_t::iterator first_hidden_item_iter = mItems.end();
  1701. S32 height_before_first_visible_item = -1;
  1702. S32 visible_items_height = 0;
  1703. U32 scrollable_items_cnt = 0;
  1704. if (mHorizontalLayout)
  1705. {
  1706. item_list_t::iterator item_iter;
  1707. for (item_iter = mItems.begin(); item_iter != mItems.end(); ++item_iter)
  1708. {
  1709. // do first so LLMenuGLItemCall can call on_visible to determine if visible
  1710. (*item_iter)->buildDrawLabel();
  1711. if ((*item_iter)->getVisible())
  1712. {
  1713. if (!getTornOff()
  1714. && *item_iter != mSpilloverBranch
  1715. && width + (*item_iter)->getNominalWidth() > max_width - spillover_item_width)
  1716. {
  1717. // no room for any more items
  1718. createSpilloverBranch();
  1719. std::vector<LLMenuItemGL*> items_to_remove;
  1720. std::copy(item_iter, mItems.end(), std::back_inserter(items_to_remove));
  1721. std::vector<LLMenuItemGL*>::iterator spillover_iter;
  1722. for (spillover_iter= items_to_remove.begin(); spillover_iter != items_to_remove.end(); ++spillover_iter)
  1723. {
  1724. LLMenuItemGL* itemp = (*spillover_iter);
  1725. removeChild(itemp);
  1726. mSpilloverMenu->addChild(itemp);
  1727. }
  1728. addChild(mSpilloverBranch);
  1729. height = llmax(height, mSpilloverBranch->getNominalHeight());
  1730. width += mSpilloverBranch->getNominalWidth();
  1731. break;
  1732. }
  1733. else
  1734. {
  1735. // track our rect
  1736. height = llmax(height, (*item_iter)->getNominalHeight());
  1737. width += (*item_iter)->getNominalWidth();
  1738. }
  1739. }
  1740. }
  1741. }
  1742. else
  1743. {
  1744. item_list_t::iterator item_iter;
  1745. for (item_iter = mItems.begin(); item_iter != mItems.end(); ++item_iter)
  1746. {
  1747. // do first so LLMenuGLItemCall can call on_visible to determine if visible
  1748. (*item_iter)->buildDrawLabel();
  1749. if ((*item_iter)->getVisible())
  1750. {
  1751. if (!getTornOff()
  1752. && !mScrollable
  1753. && *item_iter != mSpilloverBranch
  1754. && height + (*item_iter)->getNominalHeight() > max_height - spillover_item_height)
  1755. {
  1756. // no room for any more items
  1757. createSpilloverBranch();
  1758. std::vector<LLMenuItemGL*> items_to_remove;
  1759. std::copy(item_iter, mItems.end(), std::back_inserter(items_to_remove));
  1760. std::vector<LLMenuItemGL*>::iterator spillover_iter;
  1761. for (spillover_iter= items_to_remove.begin(); spillover_iter != items_to_remove.end(); ++spillover_iter)
  1762. {
  1763. LLMenuItemGL* itemp = (*spillover_iter);
  1764. removeChild(itemp);
  1765. mSpilloverMenu->addChild(itemp);
  1766. }
  1767. addChild(mSpilloverBranch);
  1768. height += mSpilloverBranch->getNominalHeight();
  1769. width = llmax( width, mSpilloverBranch->getNominalWidth() );
  1770. break;
  1771. }
  1772. else
  1773. {
  1774. // track our rect
  1775. height += (*item_iter)->getNominalHeight();
  1776. width = llmax( width, (*item_iter)->getNominalWidth() );
  1777. }
  1778. if (mScrollable)
  1779. {
  1780. // Determining visible items boundaries
  1781. if (NULL == mFirstVisibleItem)
  1782. {
  1783. mFirstVisibleItem = *item_iter;
  1784. }
  1785. if (*item_iter == mFirstVisibleItem)
  1786. {
  1787. height_before_first_visible_item = height - (*item_iter)->getNominalHeight();
  1788. first_visible_item_iter = item_iter;
  1789. scrollable_items_cnt = 0;
  1790. }
  1791. if (-1 != height_before_first_visible_item && 0 == visible_items_height &&
  1792. (++scrollable_items_cnt > mMaxScrollableItems ||
  1793. height - height_before_first_visible_item > max_height - spillover_item_height * 2 ))
  1794. {
  1795. first_hidden_item_iter = item_iter;
  1796. visible_items_height = height - height_before_first_visible_item - (*item_iter)->getNominalHeight();
  1797. scrollable_items_cnt--;
  1798. }
  1799. }
  1800. }
  1801. }
  1802. if (mPreferredWidth < U32_MAX)
  1803. width = llmin(mPreferredWidth, max_width);
  1804. if (mScrollable)
  1805. {
  1806. S32 max_items_height = max_height - spillover_item_height * 2;
  1807. if (visible_items_height == 0)
  1808. visible_items_height = height - height_before_first_visible_item;
  1809. // Fix mFirstVisibleItem value, if it doesn't allow to display all items, that can fit
  1810. if (visible_items_height < max_items_height && scrollable_items_cnt < mMaxScrollableItems)
  1811. {
  1812. item_list_t::iterator tmp_iter(first_visible_item_iter);
  1813. while (visible_items_height < max_items_height &&
  1814. scrollable_items_cnt < mMaxScrollableItems &&
  1815. first_visible_item_iter != mItems.begin())
  1816. {
  1817. if ((*first_visible_item_iter)->getVisible())
  1818. {
  1819. // It keeps visible item, after first_visible_item_iter
  1820. tmp_iter = first_visible_item_iter;
  1821. }
  1822. first_visible_item_iter--;
  1823. if ((*first_visible_item_iter)->getVisible())
  1824. {
  1825. visible_items_height += (*first_visible_item_iter)->getNominalHeight();
  1826. height_before_first_visible_item -= (*first_visible_item_iter)->getNominalHeight();
  1827. scrollable_items_cnt++;
  1828. }
  1829. }
  1830. // Roll back one item, that doesn't fit
  1831. if (visible_items_height > max_items_height)
  1832. {
  1833. visible_items_height -= (*first_visible_item_iter)->getNominalHeight();
  1834. height_before_first_visible_item += (*first_visible_item_iter)->getNominalHeight();
  1835. scrollable_items_cnt--;
  1836. first_visible_item_iter = tmp_iter;
  1837. }
  1838. if (!(*first_visible_item_iter)->getVisible())
  1839. {
  1840. first_visible_item_iter = tmp_iter;
  1841. }
  1842. mFirstVisibleItem = *first_visible_item_iter;
  1843. }
  1844. }
  1845. }
  1846. S32 cur_height = (S32)llmin(max_height, height);
  1847. if (mScrollable &&
  1848. (height_before_first_visible_item > MENU_ITEM_PADDING ||
  1849. height_before_first_visible_item + visible_items_height < (S32)height))
  1850. {
  1851. // Reserving 2 extra slots for arrow items
  1852. cur_height = visible_items_height + spillover_item_height * 2;
  1853. }
  1854. setRect(LLRect(getRect().mLeft, getRect().mTop, getRect().mLeft + width, getRect().mTop - cur_height));
  1855. S32 cur_width = 0;
  1856. S32 offset = 0;
  1857. if (mScrollable)
  1858. {
  1859. // No space for all items, creating arrow items
  1860. if (height_before_first_visible_item > MENU_ITEM_PADDING ||
  1861. height_before_first_visible_item + visible_items_height < (S32)height)
  1862. {
  1863. if (NULL == mArrowUpItem)
  1864. {
  1865. LLMenuScrollItem::Params item_params;
  1866. item_params.name(ARROW_UP);
  1867. item_params.arrow_type(LLMenuScrollItem::ARROW_UP);
  1868. item_params.scroll_callback.function(boost::bind(&LLMenuGL::scrollItems, this, SD_UP));
  1869. mArrowUpItem = LLUICtrlFactory::create<LLMenuScrollItem>(item_params);
  1870. LLUICtrl::addChild(mArrowUpItem);
  1871. }
  1872. if (NULL == mArrowDownItem)
  1873. {
  1874. LLMenuScrollItem::Params item_params;
  1875. item_params.name(ARROW_DOWN);
  1876. item_params.arrow_type(LLMenuScrollItem::ARROW_DOWN);
  1877. item_params.scroll_callback.function(boost::bind(&LLMenuGL::scrollItems, this, SD_DOWN));
  1878. mArrowDownItem = LLUICtrlFactory::create<LLMenuScrollItem>(item_params);
  1879. LLUICtrl::addChild(mArrowDownItem);
  1880. }
  1881. LLRect rect;
  1882. mArrowUpItem->setRect(rect.setLeftTopAndSize( 0, cur_height, width, mArrowUpItem->getNominalHeight()));
  1883. mArrowUpItem->setVisible(TRUE);
  1884. mArrowUpItem->setEnabled(height_before_first_visible_item > MENU_ITEM_PADDING);
  1885. mArrowUpItem->reshape(width, mArrowUpItem->getNominalHeight());
  1886. mArrowDownItem->setRect(rect.setLeftTopAndSize( 0, mArrowDownItem->getNominalHeight(), width, mArrowDownItem->getNominalHeight()));
  1887. mArrowDownItem->setVisible(TRUE);
  1888. mArrowDownItem->setEnabled(height_before_first_visible_item + visible_items_height < (S32)height);
  1889. mArrowDownItem->reshape(width, mArrowDownItem->getNominalHeight());
  1890. cur_height -= mArrowUpItem->getNominalHeight();
  1891. offset = menu_region_rect.mRight; // This moves items behind visible area
  1892. }
  1893. else
  1894. {
  1895. if (NULL != mArrowUpItem)
  1896. {
  1897. mArrowUpItem->setVisible(FALSE);
  1898. }
  1899. if (NULL != mArrowDownItem)
  1900. {
  1901. mArrowDownItem->setVisible(FALSE);
  1902. }
  1903. }
  1904. }
  1905. item_list_t::iterator item_iter;
  1906. for (item_iter = mItems.begin(); item_iter != mItems.end(); ++item_iter)
  1907. {
  1908. if ((*item_iter)->getVisible())
  1909. {
  1910. if (mScrollable)
  1911. {
  1912. if (item_iter == first_visible_item_iter)
  1913. {
  1914. offset = 0;
  1915. }
  1916. else if (item_iter == first_hidden_item_iter)
  1917. {
  1918. offset = menu_region_rect.mRight; // This moves items behind visible area
  1919. }
  1920. }
  1921. // setup item rect to hold label
  1922. LLRect rect;
  1923. if (mHorizontalLayout)
  1924. {
  1925. rect.setLeftTopAndSize( cur_width, height, (*item_iter)->getNominalWidth(), height);
  1926. cur_width += (*item_iter)->getNominalWidth();
  1927. }
  1928. else
  1929. {
  1930. rect.setLeftTopAndSize( 0 + offset, cur_height, width, (*item_iter)->getNominalHeight());
  1931. if (offset == 0)
  1932. {
  1933. cur_height -= (*item_iter)->getNominalHeight();
  1934. }
  1935. }
  1936. (*item_iter)->setRect( rect );
  1937. }
  1938. }
  1939. }
  1940. if (mKeepFixedSize)
  1941. {
  1942. reshape(initial_rect.getWidth(), initial_rect.getHeight());
  1943. }
  1944. }
  1945. void LLMenuGL::arrangeAndClear( void )
  1946. {
  1947. if (mNeedsArrange)
  1948. {
  1949. arrange();
  1950. mNeedsArrange = FALSE;
  1951. }
  1952. }
  1953. void LLMenuGL::createSpilloverBranch()
  1954. {
  1955. if (!mSpilloverBranch)
  1956. {
  1957. // should be NULL but delete anyway
  1958. delete mSpilloverMenu;
  1959. // technically, you can't tear off spillover menus, but we're passing the handle
  1960. // along just to be safe
  1961. LLMenuGL::Params p;
  1962. std::string label = LLTrans::getString("More");
  1963. p.name("More");
  1964. p.label(label);
  1965. p.bg_color(mBackgroundColor);
  1966. p.bg_visible(true);
  1967. p.can_tear_off(false);
  1968. mSpilloverMenu = new LLMenuGL(p);
  1969. mSpilloverMenu->updateParent(LLMenuGL::sMenuContainer);
  1970. LLMenuItemBranchGL::Params branch_params;
  1971. branch_params.name = "More";
  1972. branch_params.label = label;
  1973. branch_params.branch = mSpilloverMenu;
  1974. branch_params.font.style = "italic";
  1975. mSpilloverBranch = LLUICtrlFactory::create<LLMenuItemBranchGL>(branch_params);
  1976. }
  1977. }
  1978. void LLMenuGL::cleanupSpilloverBranch()
  1979. {
  1980. if (mSpilloverBranch && mSpilloverBranch->getParent() == this)
  1981. {
  1982. // head-recursion to propagate items back up to root menu
  1983. mSpilloverMenu->cleanupSpilloverBranch();
  1984. // pop off spillover items
  1985. while (mSpilloverMenu->getItemCount())
  1986. {
  1987. LLMenuItemGL* itemp = mSpilloverMenu->getItem(0);
  1988. mSpilloverMenu->removeChild(itemp);
  1989. // put them at the end of our own list
  1990. addChild(itemp);
  1991. }
  1992. // Delete the branch, and since the branch will delete the menu,
  1993. // set the menu* to null.
  1994. delete mSpilloverBranch;
  1995. mSpilloverBranch = NULL;
  1996. mSpilloverMenu = NULL;
  1997. }
  1998. }
  1999. void LLMenuGL::createJumpKeys()
  2000. {
  2001. if (!mCreateJumpKeys) return;
  2002. mCreateJumpKeys = FALSE;
  2003. mJumpKeys.clear();
  2004. std::set<std::string> unique_words;
  2005. std::set<std::string> shared_words;
  2006. item_list_t::iterator item_it;
  2007. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  2008. boost::char_separator<char> sep(" ");
  2009. for(item_it = mItems.begin(); item_it != mItems.end(); ++item_it)
  2010. {
  2011. std::string uppercase_label = (*item_it)->getLabel();
  2012. LLStringUtil::toUpper(uppercase_label);
  2013. tokenizer tokens(uppercase_label, sep);
  2014. tokenizer::iterator token_iter;
  2015. for( token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
  2016. {
  2017. if (unique_words.find(*token_iter) != unique_words.end())
  2018. {
  2019. // this word exists in more than one menu instance
  2020. shared_words.insert(*token_iter);
  2021. }
  2022. else
  2023. {
  2024. // we have a new word, keep track of it
  2025. unique_words.insert(*token_iter);
  2026. }
  2027. }
  2028. }
  2029. // pre-assign specified jump keys
  2030. for(item_it = mItems.begin(); item_it != mItems.end(); ++item_it)
  2031. {
  2032. KEY jump_key = (*item_it)->getJumpKey();
  2033. if(jump_key != KEY_NONE)
  2034. {
  2035. if (mJumpKeys.find(jump_key) == mJumpKeys.end())
  2036. {
  2037. mJumpKeys.insert(std::pair<KEY, LLMenuItemGL*>(jump_key, (*item_it)));
  2038. }
  2039. else
  2040. {
  2041. // this key is already spoken for,
  2042. // so we need to reassign it below
  2043. (*item_it)->setJumpKey(KEY_NONE);
  2044. }
  2045. }
  2046. }
  2047. for(item_it = mItems.begin(); item_it != mItems.end(); ++item_it)
  2048. {
  2049. // skip over items that already have assigned jump keys
  2050. if ((*item_it)->getJumpKey() != KEY_NONE)
  2051. {
  2052. continue;
  2053. }
  2054. std::string uppercase_label = (*item_it)->getLabel();
  2055. LLStringUtil::toUpper(uppercase_label);
  2056. tokenizer tokens(uppercase_label, sep);
  2057. tokenizer::iterator token_iter;
  2058. BOOL found_key = FALSE;
  2059. for( token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
  2060. {
  2061. std::string uppercase_word = *token_iter;
  2062. // this word is not shared with other menu entries...
  2063. if (shared_words.find(*token_iter) == shared_words.end())
  2064. {
  2065. S32 i;
  2066. for(i = 0; i < (S32)uppercase_word.size(); i++)
  2067. {
  2068. char jump_key = uppercase_word[i];
  2069. if (LLStringOps::isDigit(jump_key) || (LLStringOps::isUpper(jump_key) &&
  2070. mJumpKeys.find(jump_key) == mJumpKeys.end()))
  2071. {
  2072. mJumpKeys.insert(std::pair<KEY, LLMenuItemGL*>(jump_key, (*item_it)));
  2073. (*item_it)->setJumpKey(jump_key);
  2074. found_key = TRUE;
  2075. break;
  2076. }
  2077. }
  2078. }
  2079. if (found_key)
  2080. {
  2081. break;
  2082. }
  2083. }
  2084. }
  2085. }
  2086. // remove all items on the menu
  2087. void LLMenuGL::empty( void )
  2088. {
  2089. cleanupSpilloverBranch();
  2090. mItems.clear();
  2091. mFirstVisibleItem = NULL;
  2092. mArrowUpItem = NULL;
  2093. mArrowDownItem = NULL;
  2094. deleteAllChildren();
  2095. }
  2096. // Adjust rectangle of the menu
  2097. void LLMenuGL::setLeftAndBottom(S32 left, S32 bottom)
  2098. {
  2099. setRect(LLRect(left, getRect().mTop, getRect().mRight, bottom));
  2100. needsArrange();
  2101. }
  2102. BOOL LLMenuGL::handleJumpKey(KEY key)
  2103. {
  2104. // must perform case-insensitive comparison, so just switch to uppercase input key
  2105. key = toupper(key);
  2106. navigation_key_map_t::iterator found_it = mJumpKeys.find(key);
  2107. if(found_it != mJumpKeys.end() && found_it->second->getEnabled())
  2108. {
  2109. // switch to keyboard navigation mode
  2110. LLMenuGL::setKeyboardMode(TRUE);
  2111. // force highlight to close old menus and open and sub-menus
  2112. found_it->second->setHighlight(TRUE);
  2113. found_it->second->onCommit();
  2114. }
  2115. // if we are navigating the menus, we need to eat the keystroke
  2116. // so rest of UI doesn't handle it
  2117. return TRUE;
  2118. }
  2119. // Add the menu item to this menu.
  2120. BOOL LLMenuGL::append( LLMenuItemGL* item )
  2121. {
  2122. if (!item) return FALSE;
  2123. mItems.push_back( item );
  2124. LLUICtrl::addChild(item);
  2125. needsArrange();
  2126. return TRUE;
  2127. }
  2128. // add a separator to this menu
  2129. BOOL LLMenuGL::addSeparator()
  2130. {
  2131. LLMenuItemGL* separator = new LLMenuItemSeparatorGL();
  2132. return addChild(separator);
  2133. }
  2134. // add a menu - this will create a cascading menu
  2135. BOOL LLMenuGL::appendMenu( LLMenuGL* menu )
  2136. {
  2137. if( menu == this )
  2138. {
  2139. llerrs << "** Attempt to attach menu to itself. This is certainly "
  2140. << "a logic error." << llendl;
  2141. }
  2142. BOOL success = TRUE;
  2143. LLMenuItemBranchGL::Params p;
  2144. p.name = menu->getName();
  2145. p.label = menu->getLabel();
  2146. p.branch = menu;
  2147. p.jump_key = menu->getJumpKey();
  2148. p.enabled_color=LLUIColorTable::instance().getColor("MenuItemEnabledColor");
  2149. p.disabled_color=LLUIColorTable::instance().getColor("MenuItemDisabledColor");
  2150. p.highlight_bg_color=LLUIColorTable::instance().getColor("MenuItemHighlightBgColor");
  2151. p.highlight_fg_color=LLUIColorTable::instance().getColor("MenuItemHighlightFgColor");
  2152. LLMenuItemBranchGL* branch = LLUICtrlFactory::create<LLMenuItemBranchGL>(p);
  2153. success &= append( branch );
  2154. // Inherit colors
  2155. menu->setBackgroundColor( mBackgroundColor );
  2156. menu->updateParent(LLMenuGL::sMenuContainer);
  2157. return success;
  2158. }
  2159. void LLMenuGL::setEnabledSubMenus(BOOL enable)
  2160. {