PageRenderTime 86ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/llmodaldialog.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 306 lines | 212 code | 40 blank | 54 comment | 27 complexity | c51aac01fcd2d6dc0a03c452fde5a80a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmodaldialog.cpp
  3. * @brief LLModalDialog base class
  4. *
  5. * $LicenseInfo:firstyear=2002&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 "llmodaldialog.h"
  28. #include "llfocusmgr.h"
  29. #include "v4color.h"
  30. #include "v2math.h"
  31. #include "llui.h"
  32. #include "llwindow.h"
  33. #include "llkeyboard.h"
  34. // static
  35. std::list<LLModalDialog*> LLModalDialog::sModalStack;
  36. LLModalDialog::LLModalDialog( const LLSD& key, BOOL modal )
  37. : LLFloater(key),
  38. mModal( modal )
  39. {
  40. if (modal)
  41. {
  42. setCanMinimize(FALSE);
  43. setCanClose(FALSE);
  44. }
  45. setVisible( FALSE );
  46. setBackgroundVisible(TRUE);
  47. setBackgroundOpaque(TRUE);
  48. centerOnScreen(); // default position
  49. mCloseSignal.connect(boost::bind(&LLModalDialog::stopModal, this));
  50. }
  51. LLModalDialog::~LLModalDialog()
  52. {
  53. // don't unlock focus unless we have it
  54. if (gFocusMgr.childHasKeyboardFocus(this))
  55. {
  56. gFocusMgr.unlockFocus();
  57. }
  58. std::list<LLModalDialog*>::iterator iter = std::find(sModalStack.begin(), sModalStack.end(), this);
  59. if (iter != sModalStack.end())
  60. {
  61. llerrs << "Attempt to delete dialog while still in sModalStack!" << llendl;
  62. }
  63. }
  64. // virtual
  65. BOOL LLModalDialog::postBuild()
  66. {
  67. return LLFloater::postBuild();
  68. }
  69. // virtual
  70. void LLModalDialog::openFloater(const LLSD& key)
  71. {
  72. // SJB: Hack! Make sure we don't ever host a modal dialog
  73. LLMultiFloater* thost = LLFloater::getFloaterHost();
  74. LLFloater::setFloaterHost(NULL);
  75. LLFloater::openFloater(key);
  76. LLFloater::setFloaterHost(thost);
  77. }
  78. void LLModalDialog::reshape(S32 width, S32 height, BOOL called_from_parent)
  79. {
  80. LLFloater::reshape(width, height, called_from_parent);
  81. centerOnScreen();
  82. }
  83. // virtual
  84. void LLModalDialog::onOpen(const LLSD& key)
  85. {
  86. if (mModal)
  87. {
  88. // If Modal, Hide the active modal dialog
  89. if (!sModalStack.empty())
  90. {
  91. LLModalDialog* front = sModalStack.front();
  92. front->setVisible(FALSE);
  93. }
  94. // This is a modal dialog. It sucks up all mouse and keyboard operations.
  95. gFocusMgr.setMouseCapture( this );
  96. LLUI::addPopup(this);
  97. setFocus(TRUE);
  98. sModalStack.push_front( this );
  99. }
  100. }
  101. void LLModalDialog::stopModal()
  102. {
  103. gFocusMgr.unlockFocus();
  104. gFocusMgr.releaseFocusIfNeeded( this );
  105. if (mModal)
  106. {
  107. std::list<LLModalDialog*>::iterator iter = std::find(sModalStack.begin(), sModalStack.end(), this);
  108. if (iter != sModalStack.end())
  109. {
  110. sModalStack.erase(iter);
  111. }
  112. else
  113. {
  114. llwarns << "LLModalDialog::stopModal not in list!" << llendl;
  115. }
  116. }
  117. if (!sModalStack.empty())
  118. {
  119. LLModalDialog* front = sModalStack.front();
  120. front->setVisible(TRUE);
  121. }
  122. }
  123. void LLModalDialog::setVisible( BOOL visible )
  124. {
  125. if (mModal)
  126. {
  127. if( visible )
  128. {
  129. // This is a modal dialog. It sucks up all mouse and keyboard operations.
  130. gFocusMgr.setMouseCapture( this );
  131. // The dialog view is a root view
  132. LLUI::addPopup(this);
  133. setFocus( TRUE );
  134. }
  135. else
  136. {
  137. gFocusMgr.releaseFocusIfNeeded( this );
  138. }
  139. }
  140. LLFloater::setVisible( visible );
  141. }
  142. BOOL LLModalDialog::handleMouseDown(S32 x, S32 y, MASK mask)
  143. {
  144. if (mModal)
  145. {
  146. if (!LLFloater::handleMouseDown(x, y, mask))
  147. {
  148. // Click was outside the panel
  149. make_ui_sound("UISndInvalidOp");
  150. }
  151. }
  152. else
  153. {
  154. LLFloater::handleMouseDown(x, y, mask);
  155. }
  156. return TRUE;
  157. }
  158. BOOL LLModalDialog::handleHover(S32 x, S32 y, MASK mask)
  159. {
  160. if( childrenHandleHover(x, y, mask) == NULL )
  161. {
  162. getWindow()->setCursor(UI_CURSOR_ARROW);
  163. lldebugst(LLERR_USER_INPUT) << "hover handled by " << getName() << llendl;
  164. }
  165. return TRUE;
  166. }
  167. BOOL LLModalDialog::handleMouseUp(S32 x, S32 y, MASK mask)
  168. {
  169. childrenHandleMouseUp(x, y, mask);
  170. return TRUE;
  171. }
  172. BOOL LLModalDialog::handleScrollWheel(S32 x, S32 y, S32 clicks)
  173. {
  174. childrenHandleScrollWheel(x, y, clicks);
  175. return TRUE;
  176. }
  177. BOOL LLModalDialog::handleDoubleClick(S32 x, S32 y, MASK mask)
  178. {
  179. if (!LLFloater::handleDoubleClick(x, y, mask))
  180. {
  181. // Click outside the panel
  182. make_ui_sound("UISndInvalidOp");
  183. }
  184. return TRUE;
  185. }
  186. BOOL LLModalDialog::handleRightMouseDown(S32 x, S32 y, MASK mask)
  187. {
  188. childrenHandleRightMouseDown(x, y, mask);
  189. return TRUE;
  190. }
  191. BOOL LLModalDialog::handleKeyHere(KEY key, MASK mask )
  192. {
  193. LLFloater::handleKeyHere(key, mask );
  194. if (mModal)
  195. {
  196. // Suck up all keystokes except CTRL-Q.
  197. BOOL is_quit = ('Q' == key) && (MASK_CONTROL == mask);
  198. return !is_quit;
  199. }
  200. else
  201. {
  202. // don't process escape key until message box has been on screen a minimal amount of time
  203. // to avoid accidentally destroying the message box when user is hitting escape at the time it appears
  204. BOOL enough_time_elapsed = mVisibleTime.getElapsedTimeF32() > 1.0f;
  205. if (enough_time_elapsed && key == KEY_ESCAPE)
  206. {
  207. closeFloater();
  208. return TRUE;
  209. }
  210. return FALSE;
  211. }
  212. }
  213. // virtual
  214. void LLModalDialog::draw()
  215. {
  216. static LLUIColor shadow_color = LLUIColorTable::instance().getColor("ColorDropShadow");
  217. static LLUICachedControl<S32> shadow_lines ("DropShadowFloater", 0);
  218. gl_drop_shadow( 0, getRect().getHeight(), getRect().getWidth(), 0,
  219. shadow_color, shadow_lines);
  220. LLFloater::draw();
  221. // Focus retrieval moved to LLFloaterView::refresh()
  222. }
  223. void LLModalDialog::centerOnScreen()
  224. {
  225. LLVector2 window_size = LLUI::getWindowSize();
  226. centerWithin(LLRect(0, 0, llround(window_size.mV[VX]), llround(window_size.mV[VY])));
  227. }
  228. // static
  229. void LLModalDialog::onAppFocusLost()
  230. {
  231. if( !sModalStack.empty() )
  232. {
  233. LLModalDialog* instance = LLModalDialog::sModalStack.front();
  234. if( gFocusMgr.childHasMouseCapture( instance ) )
  235. {
  236. gFocusMgr.setMouseCapture( NULL );
  237. }
  238. instance->setFocus(FALSE);
  239. }
  240. }
  241. // static
  242. void LLModalDialog::onAppFocusGained()
  243. {
  244. if( !sModalStack.empty() )
  245. {
  246. LLModalDialog* instance = LLModalDialog::sModalStack.front();
  247. // This is a modal dialog. It sucks up all mouse and keyboard operations.
  248. gFocusMgr.setMouseCapture( instance );
  249. instance->setFocus(TRUE);
  250. LLUI::addPopup(instance);
  251. instance->centerOnScreen();
  252. }
  253. }
  254. void LLModalDialog::shutdownModals()
  255. {
  256. // This method is only for use during app shutdown. ~LLModalDialog()
  257. // checks sModalStack, and if the dialog instance is still there, it
  258. // crumps with "Attempt to delete dialog while still in sModalStack!" But
  259. // at app shutdown, all bets are off. If the user asks to shut down the
  260. // app, we shouldn't have to care WHAT's open. Put differently, if a modal
  261. // dialog is so crucial that we can't let the user terminate until s/he
  262. // addresses it, we should reject a termination request. The current state
  263. // of affairs is that we accept it, but then produce an llerrs popup that
  264. // simply makes our software look unreliable.
  265. sModalStack.clear();
  266. }