guiTabPageCtrl.cpp 6.2 KB

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