guiTabBookCtrl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #ifndef _GUI_TABBOOKCTRL_H_
  23. #define _GUI_TABBOOKCTRL_H_
  24. #ifndef _GUICONTAINER_H_
  25. #include "gui/containers/guiContainer.h"
  26. #endif
  27. #ifndef _GUITABPAGECTRL_H_
  28. #include "gui/controls/guiTabPageCtrl.h"
  29. #endif
  30. /// Tab Book Control for creation of tabbed dialogs
  31. ///
  32. /// @see GUI for an overview of the Torque GUI system.
  33. ///
  34. /// @section GuiTabBookCtrl_Intro Introduction
  35. ///
  36. /// GuiTabBookCtrl is a container class that holds children of type GuiTabPageCtrl
  37. ///
  38. /// GuiTabBookCtrl creates an easy to work with system for creating tabbed dialogs
  39. /// allowing for creation of dialogs that store a lot of information in a small area
  40. /// by separating the information into pages which are changeable by clicking their
  41. /// page title on top or bottom of the control
  42. ///
  43. /// tabs may be aligned to be on top or bottom of the book and are changeable while
  44. /// the GUI editor is open for quick switching between pages allowing multi-page dialogs
  45. /// to be edited quickly and easily.
  46. ///
  47. /// The control may only contain children of type GuiTabPageCtrl.
  48. /// If a control is added to the Book that is not of type GuiTabPageCtrl, it will be
  49. /// removed and relocated to the currently active page of the control.
  50. /// If there is no active page in the book, the child control will be relocated to the
  51. /// parent of the book.
  52. ///
  53. /// @ref GUI has an overview of the GUI system.
  54. ///
  55. /// @nosubgrouping
  56. class GuiTabBookCtrl : public GuiContainer
  57. {
  58. public:
  59. typedef GuiContainer Parent;
  60. enum TabPosition
  61. {
  62. AlignTop, ///< Align the tabs on the top of the tab book control
  63. AlignBottom ///< Align the tabs on the bottom of the tab book control
  64. };
  65. struct TabHeaderInfo
  66. {
  67. GuiTabPageCtrl *Page;
  68. S32 TextWidth;
  69. S32 TabRow;
  70. S32 TabColumn;
  71. RectI TabRect;
  72. };
  73. private:
  74. static bool _setSelectedPage( void *object, const char *index, const char *data );
  75. protected:
  76. RectI mPageRect; ///< Rectangle of the tab page portion of the control
  77. RectI mTabRect; ///< Rectangle of the tab portion of the control
  78. Vector<TabHeaderInfo> mPages; ///< Vector of pages contained by the control
  79. GuiTabPageCtrl* mActivePage; ///< Pointer to the active (selected) tab page child control
  80. GuiTabPageCtrl* mHoverTab; ///< Pointer to the tab page that currently has the mouse positioned ontop of its tab
  81. S32 mMinTabWidth; ///< Minimum Width a tab will display as.
  82. TabPosition mTabPosition; ///< Current tab position (see alignment)
  83. S32 mTabHeight; ///< Current tab height
  84. S32 mTabMargin; ///< Margin left/right of tab text in tab
  85. S32 mSelectedPageNum; ///< Current selected tab position
  86. S32 mDefaultPageNum; ///< Page to select on first wake.
  87. bool mAllowReorder; ///< Allow the user to reorder tabs by dragging them
  88. S32 mFrontTabPadding; ///< Padding to the Left of the first Tab
  89. bool mDraggingTab;
  90. bool mDraggingTabRect;
  91. bool mIsFirstWake;
  92. enum
  93. {
  94. TabSelected = 0, ///< Index of selected tab texture
  95. TabNormal, ///< Index of normal tab texture
  96. TabHover, ///< Index of hover tab texture
  97. TabEnds, ///< Index of end lines for horizontal tabs
  98. NumBitmaps ///< Number of bitmaps in this array
  99. };
  100. bool mHasTexture; ///< Indicates whether we have a texture to render the tabs with
  101. RectI *mBitmapBounds;///< Array of rectangles identifying textures for tab book
  102. /// @name Callbacks
  103. /// @{
  104. DECLARE_CALLBACK( void, onTabSelected, ( const String& text, U32 index ) );
  105. DECLARE_CALLBACK( void, onTabRightClick, ( const String& text, U32 index ) );
  106. /// @}
  107. public:
  108. GuiTabBookCtrl();
  109. DECLARE_CONOBJECT(GuiTabBookCtrl);
  110. DECLARE_DESCRIPTION( "A control that allows to select from a set of tabbed pages." );
  111. static void initPersistFields();
  112. /// @name Control Events
  113. /// @{
  114. bool onWake();
  115. void onRender( Point2I offset, const RectI &updateRect );
  116. /// @}
  117. /// @name Child events
  118. /// @{
  119. void onChildRemoved( GuiControl* child );
  120. void onChildAdded( GuiControl *child );
  121. bool reOrder(SimObject* obj, SimObject* target);
  122. bool acceptsAsChild( SimObject* object ) const;
  123. /// @}
  124. /// @name Rendering methods
  125. /// @{
  126. /// Tab rendering routine, iterates through all tabs rendering one at a time
  127. /// @param offset the render offset to accomodate global coords
  128. /// @param tabRect the Rectangle in which these tabs are to be rendered
  129. void renderTabs( const Point2I &offset, const RectI &tabRect );
  130. /// Tab rendering subroutine, renders one tab with specified options
  131. /// @param tabRect the rectangle to render the tab into
  132. /// @param tab pointer to the tab page control for which to render the tab
  133. void renderTab(const RectI& tabRect, GuiTabPageCtrl* tab);
  134. /// @}
  135. /// @name Page Management
  136. /// @{
  137. /// Create a new tab page child in the book
  138. ///
  139. /// Pages created are not titled and appear with no text on their tab when created.
  140. /// This may change in the future.
  141. ///
  142. /// @param text Tab page header text.
  143. void addNewPage( const char* text = NULL );
  144. /// Select a tab page based on an index
  145. /// @param index The index in the list that specifies which page to select
  146. void selectPage( S32 index );
  147. /// Select a tab page by a pointer to that page
  148. /// @param page A pointer to the GuiTabPageCtrl to select. This page must be a child of the tab book.
  149. void selectPage( GuiTabPageCtrl *page );
  150. /// Get the current selected tab number
  151. S32 getSelectedPageNum(){ return mSelectedPageNum; };
  152. /// Select the Next page in the tab book
  153. void selectNextPage();
  154. /// Select the Previous page in the tab book
  155. void selectPrevPage();
  156. /// Make the page fill the entire page space.
  157. void fitPage( GuiTabPageCtrl* page );
  158. /// Return the index for the given page. -1 if not a page in this book.
  159. S32 getPageNum( GuiTabPageCtrl* page ) const;
  160. /// @}
  161. /// @name Internal Utility Functions
  162. /// @{
  163. /// Update ourselves by hooking common GuiControl functionality.
  164. void setUpdate();
  165. /// Balance a top/bottom tab row
  166. void balanceRow( S32 row, S32 totalTabWidth );
  167. /// Balance a left/right tab column
  168. void balanceColumn( S32 row, S32 totalTabWidth );
  169. /// Calculate the tab width of a page, given it's caption
  170. S32 calculatePageTabWidth( GuiTabPageCtrl *page );
  171. /// Calculate Page Header Information
  172. void calculatePageTabs();
  173. /// Get client area of tab book
  174. virtual const RectI getClientRect();
  175. /// Find the tab that was hit by the current event, if any
  176. /// @param event The GuiEvent that caused this function call
  177. GuiTabPageCtrl *findHitTab( const GuiEvent &event );
  178. /// Find the tab that was hit, based on a point
  179. /// @param hitPoint A Point2I that specifies where to search for a tab hit
  180. GuiTabPageCtrl *findHitTab( Point2I hitPoint );
  181. /// @}
  182. /// @name Sizing
  183. /// @{
  184. /// Rezize our control
  185. /// This method is overridden so that we may handle resizing of our child tab
  186. /// pages when we are resized.
  187. /// This ensures we keep our sizing in sync when we are moved or sized.
  188. ///
  189. /// @param newPosition The new position of the control
  190. /// @param newExtent The new extent of the control
  191. bool resize(const Point2I &newPosition, const Point2I &newExtent);
  192. /// Called when a child page is resized
  193. /// This method is overridden so that we may handle resizing of our child tab
  194. /// pages when one of them is resized.
  195. /// This ensures we keep our sizing in sync when we our children are sized or moved.
  196. ///
  197. /// @param child A pointer to the child control that has been resized
  198. void childResized(GuiControl *child);
  199. /// @}
  200. virtual bool onKeyDown(const GuiEvent &event);
  201. /// @name Mouse Events
  202. /// @{
  203. virtual void onMouseDown( const GuiEvent &event );
  204. virtual void onMouseUp( const GuiEvent &event );
  205. virtual void onMouseDragged( const GuiEvent &event );
  206. virtual void onMouseMove( const GuiEvent &event );
  207. virtual void onMouseLeave( const GuiEvent &event );
  208. virtual bool onMouseDownEditor( const GuiEvent &event, Point2I offset );
  209. virtual void onRightMouseUp( const GuiEvent& event );
  210. /// @}
  211. };
  212. typedef GuiTabBookCtrl::TabPosition GuiTabPosition;
  213. DefineEnumType( GuiTabPosition );
  214. #endif //_GUI_TABBOOKCTRL_H_