guiTabPageCtrl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "console/engineAPI.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "gui/controls/guiTabPageCtrl.h"
  28. #include "gui/containers/guiTabBookCtrl.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. #include "gui/editor/guiEditCtrl.h"
  31. IMPLEMENT_CONOBJECT(GuiTabPageCtrl);
  32. ConsoleDocClass( GuiTabPageCtrl,
  33. "@brief A single page in a GuiTabBookCtrl.\n\n"
  34. "@tsexample\n\n"
  35. "new GuiTabPageCtrl()\n"
  36. "{\n"
  37. " fitBook = \"1\";\n"
  38. " //Properties not specific to this control have been omitted from this example.\n"
  39. "};\n"
  40. "@endtsexample\n\n"
  41. "@ingroup GuiContainers"
  42. );
  43. GuiTabPageCtrl::GuiTabPageCtrl(void)
  44. {
  45. setExtent(Point2I(100, 200));
  46. mFitBook = false;
  47. dStrcpy(mText,(UTF8*)"TabPage", MAX_STRING_LENGTH);
  48. mActive = true;
  49. mIsContainer = true;
  50. mTabIndex = -1;
  51. }
  52. void GuiTabPageCtrl::initPersistFields()
  53. {
  54. docsURL;
  55. addField( "fitBook", TypeBool, Offset( mFitBook, GuiTabPageCtrl ),
  56. "Determines whether to resize this page when it is added to the tab book. "
  57. "If true, the page will be resized according to the tab book extents and "
  58. "<i>tabPosition</i> property." );
  59. Parent::initPersistFields();
  60. }
  61. bool GuiTabPageCtrl::onWake()
  62. {
  63. if (! Parent::onWake())
  64. return false;
  65. return true;
  66. }
  67. void GuiTabPageCtrl::onSleep()
  68. {
  69. Parent::onSleep();
  70. }
  71. GuiControl* GuiTabPageCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  72. {
  73. return Parent::findHitControl(pt, initialLayer);
  74. }
  75. void GuiTabPageCtrl::onMouseDown(const GuiEvent &event)
  76. {
  77. setUpdate();
  78. Point2I localPoint = globalToLocalCoord( event.mousePoint );
  79. GuiControl *ctrl = findHitControl(localPoint);
  80. if (ctrl && ctrl != this)
  81. {
  82. ctrl->onMouseDown(event);
  83. }
  84. }
  85. bool GuiTabPageCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset )
  86. {
  87. #ifdef TORQUE_TOOLS
  88. // This shouldn't be called if it's not design time, but check just incase
  89. if ( GuiControl::smDesignTime )
  90. {
  91. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  92. if( edit )
  93. edit->select( this );
  94. }
  95. return Parent::onMouseDownEditor( event, offset );
  96. #else
  97. return false;
  98. #endif
  99. }
  100. GuiControl *GuiTabPageCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  101. {
  102. //set the global if this is the first call (directly from the canvas)
  103. if (firstCall)
  104. {
  105. GuiControl::smCurResponder = NULL;
  106. }
  107. //if the window does not already contain the first responder, return false
  108. //ie. Can't tab into or out of a window
  109. if (! controlIsChild(curResponder))
  110. {
  111. return NULL;
  112. }
  113. //loop through, checking each child to see if it is the one that follows the firstResponder
  114. GuiControl *tabCtrl = NULL;
  115. iterator i;
  116. for (i = begin(); i != end(); i++)
  117. {
  118. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  119. tabCtrl = ctrl->findNextTabable(curResponder, false);
  120. if (tabCtrl) break;
  121. }
  122. //to ensure the tab cycles within the current window...
  123. if (! tabCtrl)
  124. {
  125. tabCtrl = findFirstTabable();
  126. }
  127. mFirstResponder = tabCtrl;
  128. return tabCtrl;
  129. }
  130. GuiControl *GuiTabPageCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  131. {
  132. if (firstCall)
  133. {
  134. GuiControl::smPrevResponder = NULL;
  135. }
  136. //if the window does not already contain the first responder, return false
  137. //ie. Can't tab into or out of a window
  138. if (! controlIsChild(curResponder))
  139. {
  140. return NULL;
  141. }
  142. //loop through, checking each child to see if it is the one that follows the firstResponder
  143. GuiControl *tabCtrl = NULL;
  144. iterator i;
  145. for (i = begin(); i != end(); i++)
  146. {
  147. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  148. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  149. if (tabCtrl) break;
  150. }
  151. //to ensure the tab cycles within the current window...
  152. if (! tabCtrl)
  153. {
  154. tabCtrl = findLastTabable();
  155. }
  156. mFirstResponder = tabCtrl;
  157. return tabCtrl;
  158. }
  159. void GuiTabPageCtrl::setText(const char *txt)
  160. {
  161. Parent::setText( txt );
  162. GuiControl *parent = getParent();
  163. if( parent )
  164. parent->setUpdate();
  165. };
  166. void GuiTabPageCtrl::selectWindow(void)
  167. {
  168. //first make sure this window is the front most of its siblings
  169. GuiControl *parent = getParent();
  170. if (parent)
  171. {
  172. parent->pushObjectToBack(this);
  173. }
  174. //also set the first responder to be the one within this window
  175. setFirstResponder(mFirstResponder);
  176. }
  177. void GuiTabPageCtrl::onRender(Point2I offset,const RectI &updateRect)
  178. {
  179. // Call directly into GuiControl to skip the GuiTextCtrl parent render
  180. GuiControl::onRender( offset, updateRect );
  181. }
  182. void GuiTabPageCtrl::inspectPostApply()
  183. {
  184. Parent::inspectPostApply();
  185. if( mFitBook )
  186. {
  187. GuiTabBookCtrl* book = dynamic_cast< GuiTabBookCtrl* >( getParent() );
  188. if( book )
  189. book->fitPage( this );
  190. }
  191. }
  192. DefineEngineMethod( GuiTabPageCtrl, select, void, (),,
  193. "Select this page in its tab book." )
  194. {
  195. GuiTabBookCtrl* book = dynamic_cast< GuiTabBookCtrl* >( object->getParent() );
  196. if( !book )
  197. return;
  198. book->selectPage( object );
  199. }