guiTabPageCtrl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. addField( "fitBook", TypeBool, Offset( mFitBook, GuiTabPageCtrl ),
  55. "Determines whether to resize this page when it is added to the tab book. "
  56. "If true, the page will be resized according to the tab book extents and "
  57. "<i>tabPosition</i> property." );
  58. Parent::initPersistFields();
  59. }
  60. bool GuiTabPageCtrl::onWake()
  61. {
  62. if (! Parent::onWake())
  63. return false;
  64. return true;
  65. }
  66. void GuiTabPageCtrl::onSleep()
  67. {
  68. Parent::onSleep();
  69. }
  70. GuiControl* GuiTabPageCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  71. {
  72. return Parent::findHitControl(pt, initialLayer);
  73. }
  74. void GuiTabPageCtrl::onMouseDown(const GuiEvent &event)
  75. {
  76. setUpdate();
  77. Point2I localPoint = globalToLocalCoord( event.mousePoint );
  78. GuiControl *ctrl = findHitControl(localPoint);
  79. if (ctrl && ctrl != this)
  80. {
  81. ctrl->onMouseDown(event);
  82. }
  83. }
  84. bool GuiTabPageCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset )
  85. {
  86. #ifdef TORQUE_TOOLS
  87. // This shouldn't be called if it's not design time, but check just incase
  88. if ( GuiControl::smDesignTime )
  89. {
  90. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  91. if( edit )
  92. edit->select( this );
  93. }
  94. return Parent::onMouseDownEditor( event, offset );
  95. #else
  96. return false;
  97. #endif
  98. }
  99. GuiControl *GuiTabPageCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  100. {
  101. //set the global if this is the first call (directly from the canvas)
  102. if (firstCall)
  103. {
  104. GuiControl::smCurResponder = NULL;
  105. }
  106. //if the window does not already contain the first responder, return false
  107. //ie. Can't tab into or out of a window
  108. if (! controlIsChild(curResponder))
  109. {
  110. return NULL;
  111. }
  112. //loop through, checking each child to see if it is the one that follows the firstResponder
  113. GuiControl *tabCtrl = NULL;
  114. iterator i;
  115. for (i = begin(); i != end(); i++)
  116. {
  117. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  118. tabCtrl = ctrl->findNextTabable(curResponder, false);
  119. if (tabCtrl) break;
  120. }
  121. //to ensure the tab cycles within the current window...
  122. if (! tabCtrl)
  123. {
  124. tabCtrl = findFirstTabable();
  125. }
  126. mFirstResponder = tabCtrl;
  127. return tabCtrl;
  128. }
  129. GuiControl *GuiTabPageCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  130. {
  131. if (firstCall)
  132. {
  133. GuiControl::smPrevResponder = NULL;
  134. }
  135. //if the window does not already contain the first responder, return false
  136. //ie. Can't tab into or out of a window
  137. if (! controlIsChild(curResponder))
  138. {
  139. return NULL;
  140. }
  141. //loop through, checking each child to see if it is the one that follows the firstResponder
  142. GuiControl *tabCtrl = NULL;
  143. iterator i;
  144. for (i = begin(); i != end(); i++)
  145. {
  146. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  147. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  148. if (tabCtrl) break;
  149. }
  150. //to ensure the tab cycles within the current window...
  151. if (! tabCtrl)
  152. {
  153. tabCtrl = findLastTabable();
  154. }
  155. mFirstResponder = tabCtrl;
  156. return tabCtrl;
  157. }
  158. void GuiTabPageCtrl::setText(const char *txt)
  159. {
  160. Parent::setText( txt );
  161. GuiControl *parent = getParent();
  162. if( parent )
  163. parent->setUpdate();
  164. };
  165. void GuiTabPageCtrl::selectWindow(void)
  166. {
  167. //first make sure this window is the front most of its siblings
  168. GuiControl *parent = getParent();
  169. if (parent)
  170. {
  171. parent->pushObjectToBack(this);
  172. }
  173. //also set the first responder to be the one within this window
  174. setFirstResponder(mFirstResponder);
  175. }
  176. void GuiTabPageCtrl::onRender(Point2I offset,const RectI &updateRect)
  177. {
  178. // Call directly into GuiControl to skip the GuiTextCtrl parent render
  179. GuiControl::onRender( offset, updateRect );
  180. }
  181. void GuiTabPageCtrl::inspectPostApply()
  182. {
  183. Parent::inspectPostApply();
  184. if( mFitBook )
  185. {
  186. GuiTabBookCtrl* book = dynamic_cast< GuiTabBookCtrl* >( getParent() );
  187. if( book )
  188. book->fitPage( this );
  189. }
  190. }
  191. DefineEngineMethod( GuiTabPageCtrl, select, void, (),,
  192. "Select this page in its tab book." )
  193. {
  194. GuiTabBookCtrl* book = dynamic_cast< GuiTabBookCtrl* >( object->getParent() );
  195. if( !book )
  196. return;
  197. book->selectPage( object );
  198. }