guiTabPageCtrl.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/dgl.h"
  25. #include "gui/guiCanvas.h"
  26. #include "gui/containers/guiTabPageCtrl.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "gui/editor/guiEditCtrl.h"
  29. IMPLEMENT_CONOBJECT(GuiTabPageCtrl);
  30. GuiTabPageCtrl::GuiTabPageCtrl(void)
  31. {
  32. mBounds.extent.set(100, 200);
  33. //dStrcpy(mText,(UTF8*)"TabPage");
  34. mActive = true;
  35. mIsContainer = true;
  36. }
  37. void GuiTabPageCtrl::initPersistFields()
  38. {
  39. Parent::initPersistFields();
  40. }
  41. bool GuiTabPageCtrl::onWake()
  42. {
  43. if (! Parent::onWake())
  44. return false;
  45. return true;
  46. }
  47. void GuiTabPageCtrl::onSleep()
  48. {
  49. Parent::onSleep();
  50. }
  51. GuiControl* GuiTabPageCtrl::findHitControl(const Point2I &pt, S32 initialLayer)
  52. {
  53. return Parent::findHitControl(pt, initialLayer);
  54. }
  55. void GuiTabPageCtrl::onTouchDown(const GuiEvent &event)
  56. {
  57. setUpdate();
  58. Point2I localPoint = globalToLocalCoord( event.mousePoint );
  59. GuiControl *ctrl = findHitControl(localPoint);
  60. if (ctrl && ctrl != this)
  61. {
  62. ctrl->onTouchDown(event);
  63. }
  64. }
  65. bool GuiTabPageCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset )
  66. {
  67. // This shouldn't be called if it's not design time, but check just incase
  68. if ( GuiControl::smDesignTime )
  69. {
  70. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  71. if( edit )
  72. edit->select( this );
  73. }
  74. return Parent::onMouseDownEditor( event, offset );
  75. }
  76. GuiControl *GuiTabPageCtrl::findNextTabable(GuiControl *curResponder, bool firstCall)
  77. {
  78. //set the global if this is the first call (directly from the canvas)
  79. if (firstCall)
  80. {
  81. GuiControl::smCurResponder = NULL;
  82. }
  83. //if the window does not already contain the first responder, return false
  84. //ie. Can't tab into or out of a window
  85. if (! ControlIsChild(curResponder))
  86. {
  87. return NULL;
  88. }
  89. //loop through, checking each child to see if it is the one that follows the firstResponder
  90. GuiControl *tabCtrl = NULL;
  91. iterator i;
  92. for (i = begin(); i != end(); i++)
  93. {
  94. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  95. tabCtrl = ctrl->findNextTabable(curResponder, false);
  96. if (tabCtrl) break;
  97. }
  98. //to ensure the tab cycles within the current window...
  99. if (! tabCtrl)
  100. {
  101. tabCtrl = findFirstTabable();
  102. }
  103. mFirstResponder = tabCtrl;
  104. return tabCtrl;
  105. }
  106. GuiControl *GuiTabPageCtrl::findPrevTabable(GuiControl *curResponder, bool firstCall)
  107. {
  108. if (firstCall)
  109. {
  110. GuiControl::smPrevResponder = NULL;
  111. }
  112. //if the window does not already contain the first responder, return false
  113. //ie. Can't tab into or out of a window
  114. if (! ControlIsChild(curResponder))
  115. {
  116. return NULL;
  117. }
  118. //loop through, checking each child to see if it is the one that follows the firstResponder
  119. GuiControl *tabCtrl = NULL;
  120. iterator i;
  121. for (i = begin(); i != end(); i++)
  122. {
  123. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  124. tabCtrl = ctrl->findPrevTabable(curResponder, false);
  125. if (tabCtrl) break;
  126. }
  127. //to ensure the tab cycles within the current window...
  128. if (! tabCtrl)
  129. {
  130. tabCtrl = findLastTabable();
  131. }
  132. mFirstResponder = tabCtrl;
  133. return tabCtrl;
  134. }
  135. void GuiTabPageCtrl::setText(const char *txt)
  136. {
  137. Parent::setText( txt );
  138. GuiControl *parent = getParent();
  139. if( parent )
  140. parent->setUpdate();
  141. };
  142. void GuiTabPageCtrl::selectWindow(void)
  143. {
  144. //first make sure this window is the front most of its siblings
  145. GuiControl *parent = getParent();
  146. if (parent)
  147. {
  148. parent->pushObjectToBack(this);
  149. }
  150. //also set the first responder to be the one within this window
  151. setFirstResponder(mFirstResponder);
  152. }
  153. void GuiTabPageCtrl::onRender(Point2I offset, const RectI &updateRect)
  154. {
  155. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  156. if (!ctrlRect.isValidRect())
  157. {
  158. return;
  159. }
  160. renderUniversalRect(ctrlRect, mProfile, NormalState);
  161. //Render Text
  162. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  163. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  164. if (contentRect.isValidRect())
  165. {
  166. //Render the childen
  167. renderChildControls(offset, contentRect, updateRect);
  168. }
  169. }