2
0

guiTabBookCtrl.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 "gui/containers/guiTabBookCtrl.h"
  23. #include "console/engineAPI.h"
  24. #include "gui/core/guiCanvas.h"
  25. #include "gui/editor/guiEditCtrl.h"
  26. #include "gui/controls/guiPopUpCtrl.h"
  27. #include "gui/core/guiDefaultControlRender.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. IMPLEMENT_CONOBJECT( GuiTabBookCtrl );
  30. ConsoleDocClass( GuiTabBookCtrl,
  31. "@brief A container \n\n"
  32. "@tsexample\n"
  33. "// Create \n"
  34. "@endtsexample\n\n"
  35. "@note Only GuiTabPageCtrls must be added to GuiTabBookCtrls. If an object of a different "
  36. "class is added to the control, it will be reassigned to either the active page or the "
  37. "tab book's parent.\n\n"
  38. "@see GuiTabPageCtrl\n"
  39. "@ingroup GuiContainers"
  40. );
  41. ImplementEnumType( GuiTabPosition,
  42. "Where the control should put the tab headers for selecting individual pages.\n\n"
  43. "@ingroup GuiContainers" )
  44. { GuiTabBookCtrl::AlignTop, "Top", "Tab headers on top edge." },
  45. { GuiTabBookCtrl::AlignBottom,"Bottom", "Tab headers on bottom edge." }
  46. EndImplementEnumType;
  47. IMPLEMENT_CALLBACK( GuiTabBookCtrl, onTabSelected, void, ( const String& text, U32 index ), ( text, index ),
  48. "Called when a new tab page is selected.\n\n"
  49. "@param text Text of the page header for the tab that is being selected.\n"
  50. "@param index Index of the tab page being selected." );
  51. IMPLEMENT_CALLBACK( GuiTabBookCtrl, onTabRightClick, void, ( const String& text, U32 index ), ( text, index ),
  52. "Called when the user right-clicks on a tab page header.\n\n"
  53. "@param text Text of the page header for the tab that is being selected.\n"
  54. "@param index Index of the tab page being selected." );
  55. //-----------------------------------------------------------------------------
  56. GuiTabBookCtrl::GuiTabBookCtrl()
  57. {
  58. VECTOR_SET_ASSOCIATION( mPages );
  59. mTabHeight = 24;
  60. mTabPosition = AlignTop;
  61. mActivePage = NULL;
  62. mHoverTab = NULL;
  63. mHasTexture = false;
  64. mBitmapBounds = NULL;
  65. setExtent( 400, 300 );
  66. mPageRect = RectI(0,0,0,0);
  67. mTabRect = RectI(0,0,0,0);
  68. mFrontTabPadding = 0;
  69. mPages.reserve(12);
  70. mTabMargin = 7;
  71. mMinTabWidth = 64;
  72. mIsContainer = true;
  73. mSelectedPageNum = -1;
  74. mDefaultPageNum = -1;
  75. mAllowReorder = false;
  76. mDraggingTab = false;
  77. mDraggingTabRect = false;
  78. mIsFirstWake = true;
  79. }
  80. //-----------------------------------------------------------------------------
  81. void GuiTabBookCtrl::initPersistFields()
  82. {
  83. addGroup( "TabBook" );
  84. addField( "tabPosition", TYPEID< TabPosition >(), Offset( mTabPosition, GuiTabBookCtrl ),
  85. "Where to place the tab page headers." );
  86. addField( "tabMargin", TypeS32, Offset( mTabMargin, GuiTabBookCtrl ),
  87. "Spacing to put between individual tab page headers." );
  88. addField( "minTabWidth", TypeS32, Offset( mMinTabWidth, GuiTabBookCtrl ),
  89. "Minimum width allocated to a tab page header." );
  90. addField( "tabHeight", TypeS32, Offset( mTabHeight, GuiTabBookCtrl ),
  91. "Height of tab page headers." );
  92. addField( "allowReorder", TypeBool, Offset( mAllowReorder, GuiTabBookCtrl ),
  93. "Whether reordering tabs with the mouse is allowed." );
  94. addField( "defaultPage", TypeS32, Offset( mDefaultPageNum, GuiTabBookCtrl ),
  95. "Index of page to select on first onWake() call (-1 to disable)." );
  96. addProtectedField( "selectedPage", TypeS32, Offset( mSelectedPageNum, GuiTabBookCtrl ),
  97. &_setSelectedPage, &defaultProtectedGetFn,
  98. "Index of currently selected page." );
  99. addField( "frontTabPadding", TypeS32, Offset( mFrontTabPadding, GuiTabBookCtrl ),
  100. "X offset of first tab page header." );
  101. endGroup( "TabBook" );
  102. Parent::initPersistFields();
  103. }
  104. //-----------------------------------------------------------------------------
  105. void GuiTabBookCtrl::onChildRemoved( GuiControl* child )
  106. {
  107. for (S32 i = 0; i < mPages.size(); i++ )
  108. {
  109. GuiTabPageCtrl* tab = mPages[i].Page;
  110. if( tab == child )
  111. {
  112. mPages.erase( i );
  113. break;
  114. }
  115. }
  116. // Calculate Page Information
  117. calculatePageTabs();
  118. // Active Index.
  119. mSelectedPageNum = getMin( mSelectedPageNum, mPages.size() - 1 );
  120. if ( mSelectedPageNum != -1 )
  121. {
  122. // Select Page.
  123. selectPage( mSelectedPageNum );
  124. }
  125. }
  126. //-----------------------------------------------------------------------------
  127. void GuiTabBookCtrl::onChildAdded( GuiControl *child )
  128. {
  129. GuiTabPageCtrl *page = dynamic_cast<GuiTabPageCtrl*>(child);
  130. if( !page )
  131. {
  132. Con::warnf("GuiTabBookCtrl::onChildAdded - attempting to add NON GuiTabPageCtrl as child page");
  133. SimObject *simObj = reinterpret_cast<SimObject*>(child);
  134. removeObject( simObj );
  135. if( mActivePage )
  136. {
  137. mActivePage->addObject( simObj );
  138. }
  139. else
  140. {
  141. Con::warnf("GuiTabBookCtrl::onChildAdded - unable to find active page to reassign ownership of new child control to, placing on parent");
  142. GuiControl *rent = getParent();
  143. if( rent )
  144. rent->addObject( simObj );
  145. }
  146. return;
  147. }
  148. TabHeaderInfo newPage;
  149. newPage.Page = page;
  150. newPage.TabRow = -1;
  151. newPage.TabColumn = -1;
  152. mPages.push_back( newPage );
  153. // Calculate Page Information
  154. calculatePageTabs();
  155. if( page->getFitBook() )
  156. fitPage( page );
  157. // Select this Page
  158. selectPage( page );
  159. }
  160. //-----------------------------------------------------------------------------
  161. bool GuiTabBookCtrl::reOrder(SimObject* obj, SimObject* target)
  162. {
  163. if ( !Parent::reOrder(obj, target) )
  164. return false;
  165. // Store the Selected Page.
  166. GuiTabPageCtrl *selectedPage = NULL;
  167. if ( mSelectedPageNum != -1 )
  168. selectedPage = mPages[mSelectedPageNum].Page;
  169. // Determine the Target Page Index.
  170. S32 targetIndex = -1;
  171. for( S32 i = 0; i < mPages.size(); i++ )
  172. {
  173. const TabHeaderInfo &info = mPages[i];
  174. if ( info.Page == target )
  175. {
  176. targetIndex = i;
  177. break;
  178. }
  179. }
  180. if ( targetIndex == -1 )
  181. {
  182. return false;
  183. }
  184. for( S32 i = 0; i < mPages.size(); i++ )
  185. {
  186. const TabHeaderInfo &info = mPages[i];
  187. if ( info.Page == obj )
  188. {
  189. // Store Info.
  190. TabHeaderInfo objPage = info;
  191. // Remove.
  192. mPages.erase( i );
  193. // Insert.
  194. mPages.insert( targetIndex, objPage );
  195. break;
  196. }
  197. }
  198. // Update Tabs.
  199. calculatePageTabs();
  200. // Reselect Page.
  201. selectPage( selectedPage );
  202. return true;
  203. }
  204. //-----------------------------------------------------------------------------
  205. bool GuiTabBookCtrl::acceptsAsChild( SimObject* object ) const
  206. {
  207. // Only accept tab pages.
  208. return ( dynamic_cast< GuiTabPageCtrl* >( object ) != NULL );
  209. }
  210. //-----------------------------------------------------------------------------
  211. bool GuiTabBookCtrl::onWake()
  212. {
  213. if (! Parent::onWake())
  214. return false;
  215. mHasTexture = mProfile->constructBitmapArray() > 0;
  216. if( mHasTexture )
  217. {
  218. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  219. mTabHeight = mBitmapBounds[TabSelected].extent.y;
  220. }
  221. calculatePageTabs();
  222. if( mIsFirstWake )
  223. {
  224. // Awaken all pages, visible or not. We need to do this so
  225. // any pages that make use of a language table for their label
  226. // are correctly initialized.
  227. for ( U32 i = 0; i < mPages.size(); ++i)
  228. {
  229. if ( !mPages[i].Page->isAwake() )
  230. {
  231. mPages[i].Page->awaken();
  232. }
  233. }
  234. if( mDefaultPageNum >= 0 && mDefaultPageNum < mPages.size() )
  235. selectPage( mDefaultPageNum );
  236. mIsFirstWake = false;
  237. }
  238. return true;
  239. }
  240. //-----------------------------------------------------------------------------
  241. void GuiTabBookCtrl::addNewPage( const char* text )
  242. {
  243. GuiTabPageCtrl* page = new GuiTabPageCtrl();
  244. if( text )
  245. page->setText( text );
  246. page->registerObject();
  247. addObject( page );
  248. }
  249. //-----------------------------------------------------------------------------
  250. bool GuiTabBookCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  251. {
  252. bool result = Parent::resize( newPosition, newExtent );
  253. calculatePageTabs();
  254. return result;
  255. }
  256. //-----------------------------------------------------------------------------
  257. void GuiTabBookCtrl::childResized(GuiControl *child)
  258. {
  259. Parent::childResized( child );
  260. //child->resize( mPageRect.point, mPageRect.extent );
  261. }
  262. //-----------------------------------------------------------------------------
  263. void GuiTabBookCtrl::onMouseDown(const GuiEvent &event)
  264. {
  265. mDraggingTab = false;
  266. mDraggingTabRect = false;
  267. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  268. if( mTabRect.pointInRect( localMouse ) )
  269. {
  270. GuiTabPageCtrl *tab = findHitTab( localMouse );
  271. if( tab != NULL )
  272. {
  273. selectPage( tab );
  274. mDraggingTab = mAllowReorder;
  275. }
  276. else
  277. {
  278. mDraggingTabRect = true;
  279. }
  280. }
  281. }
  282. //-----------------------------------------------------------------------------
  283. void GuiTabBookCtrl::onMouseUp(const GuiEvent &event)
  284. {
  285. Parent::onMouseUp( event );
  286. mDraggingTab = false;
  287. mDraggingTabRect = false;
  288. }
  289. //-----------------------------------------------------------------------------
  290. void GuiTabBookCtrl::onMouseDragged(const GuiEvent &event)
  291. {
  292. Parent::onMouseDragged( event );
  293. if ( !mDraggingTab )
  294. return;
  295. GuiTabPageCtrl *selectedPage = NULL;
  296. if ( mSelectedPageNum != -1 )
  297. selectedPage = mPages[mSelectedPageNum].Page;
  298. if ( !selectedPage )
  299. return;
  300. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  301. if( mTabRect.pointInRect( localMouse ) )
  302. {
  303. GuiTabPageCtrl *tab = findHitTab( localMouse );
  304. if( tab != NULL && tab != selectedPage )
  305. {
  306. S32 targetIndex = -1;
  307. for( S32 i = 0; i < mPages.size(); i++ )
  308. {
  309. if( mPages[i].Page == tab )
  310. {
  311. targetIndex = i;
  312. break;
  313. }
  314. }
  315. if ( targetIndex > mSelectedPageNum )
  316. {
  317. reOrder( tab, selectedPage );
  318. }
  319. else
  320. {
  321. reOrder( selectedPage, tab );
  322. }
  323. }
  324. }
  325. }
  326. //-----------------------------------------------------------------------------
  327. void GuiTabBookCtrl::onMouseMove(const GuiEvent &event)
  328. {
  329. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  330. if( mTabRect.pointInRect( localMouse ) )
  331. {
  332. GuiTabPageCtrl *tab = findHitTab( localMouse );
  333. if( tab != NULL && mHoverTab != tab )
  334. mHoverTab = tab;
  335. else if ( !tab )
  336. mHoverTab = NULL;
  337. }
  338. Parent::onMouseMove( event );
  339. }
  340. //-----------------------------------------------------------------------------
  341. void GuiTabBookCtrl::onMouseLeave( const GuiEvent &event )
  342. {
  343. Parent::onMouseLeave( event );
  344. mHoverTab = NULL;
  345. }
  346. //-----------------------------------------------------------------------------
  347. bool GuiTabBookCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset)
  348. {
  349. bool handled = false;
  350. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  351. if( mTabRect.pointInRect( localMouse ) )
  352. {
  353. GuiTabPageCtrl *tab = findHitTab( localMouse );
  354. if( tab != NULL )
  355. {
  356. selectPage( tab );
  357. handled = true;
  358. }
  359. }
  360. #ifdef TORQUE_TOOLS
  361. // This shouldn't be called if it's not design time, but check just incase
  362. if ( GuiControl::smDesignTime )
  363. {
  364. // If we clicked in the editor and our addset is the tab book
  365. // ctrl, select the child ctrl so we can edit it's properties
  366. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  367. if( edit && ( edit->getAddSet() == this ) && mActivePage != NULL )
  368. edit->select( mActivePage );
  369. }
  370. #endif
  371. // Return whether we handled this or not.
  372. return handled;
  373. }
  374. //-----------------------------------------------------------------------------
  375. void GuiTabBookCtrl::onRightMouseUp( const GuiEvent& event )
  376. {
  377. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  378. if( mTabRect.pointInRect( localMouse ) )
  379. {
  380. GuiTabPageCtrl* tab = findHitTab( localMouse );
  381. if( tab )
  382. onTabRightClick_callback( tab->getText(), getPageNum( tab ) );
  383. }
  384. }
  385. //-----------------------------------------------------------------------------
  386. void GuiTabBookCtrl::onRender(Point2I offset, const RectI &updateRect)
  387. {
  388. RectI tabRect = mTabRect;
  389. tabRect.point += offset;
  390. RectI pageRect = mPageRect;
  391. pageRect.point += offset;
  392. // We're so nice we'll store the old modulation before we clear it for our rendering! :)
  393. ColorI oldModulation;
  394. GFX->getDrawUtil()->getBitmapModulation( &oldModulation );
  395. // Wipe it out
  396. GFX->getDrawUtil()->clearBitmapModulation();
  397. Parent::onRender(offset, updateRect);
  398. // Clip to tab area
  399. RectI savedClipRect = GFX->getClipRect();
  400. RectI clippedTabRect = tabRect;
  401. clippedTabRect.intersect( savedClipRect );
  402. GFX->setClipRect( clippedTabRect );
  403. // Render our tabs
  404. renderTabs( offset, tabRect );
  405. // Restore Rect.
  406. GFX->setClipRect( savedClipRect );
  407. // Restore old modulation
  408. GFX->getDrawUtil()->setBitmapModulation( oldModulation );
  409. }
  410. //-----------------------------------------------------------------------------
  411. void GuiTabBookCtrl::renderTabs( const Point2I &offset, const RectI &tabRect )
  412. {
  413. // If the tab size is zero, don't render tabs,
  414. // assuming it's a tab-less book
  415. if( mPages.empty() || mTabHeight <= 0 )
  416. return;
  417. for( S32 i = 0; i < mPages.size(); i++ )
  418. {
  419. const TabHeaderInfo &currentTabInfo = mPages[i];
  420. RectI tabBounds = mPages[i].TabRect;
  421. tabBounds.point += offset;
  422. GuiTabPageCtrl *tab = mPages[i].Page;
  423. if( tab != NULL )
  424. renderTab( tabBounds, tab );
  425. // If we're on the last tab, draw the nice end piece
  426. if( i + 1 == mPages.size() )
  427. {
  428. Point2I tabEndPoint = Point2I(currentTabInfo.TabRect.point.x + currentTabInfo.TabRect.extent.x + offset.x, currentTabInfo.TabRect.point.y + offset.y);
  429. Point2I tabEndExtent = Point2I((tabRect.point.x + tabRect.extent.x) - tabEndPoint.x, currentTabInfo.TabRect.extent.y);
  430. RectI tabEndRect = RectI(tabEndPoint,tabEndExtent);
  431. GFX->setClipRect( tabEndRect );
  432. // As it turns out the last tab can be outside the viewport in which
  433. // case trying to render causes a DX assert. Could be better if
  434. // setClipRect returned a bool.
  435. if ( GFX->getViewport().isValidRect() )
  436. renderFixedBitmapBordersFilled( tabEndRect, TabEnds + 1, mProfile );
  437. }
  438. }
  439. }
  440. //-----------------------------------------------------------------------------
  441. void GuiTabBookCtrl::renderTab(const RectI& tabRect, GuiTabPageCtrl *tab)
  442. {
  443. StringTableEntry text = tab->getText();
  444. ColorI oldColor;
  445. GFX->getDrawUtil()->getBitmapModulation( &oldColor );
  446. // Is this a skinned control?
  447. if( mHasTexture && mProfile->mBitmapArrayRects.size() >= 9 )
  448. {
  449. S32 indexMultiplier = 1;
  450. switch( mTabPosition )
  451. {
  452. case AlignTop:
  453. case AlignBottom:
  454. if ( mActivePage == tab )
  455. indexMultiplier += TabSelected;
  456. else if( mHoverTab == tab )
  457. indexMultiplier += TabHover;
  458. else
  459. indexMultiplier += TabNormal;
  460. break;
  461. }
  462. renderFixedBitmapBordersFilled( tabRect, indexMultiplier, mProfile );
  463. }
  464. else
  465. {
  466. // If this isn't a skinned control or the bitmap is simply missing, handle it WELL
  467. if ( mActivePage == tab )
  468. GFX->getDrawUtil()->drawRectFill(tabRect, mProfile->mFillColor);
  469. else if( mHoverTab == tab )
  470. GFX->getDrawUtil()->drawRectFill(tabRect, mProfile->mFillColorHL);
  471. else
  472. GFX->getDrawUtil()->drawRectFill(tabRect, mProfile->mFillColorNA);
  473. }
  474. GFX->getDrawUtil()->setBitmapModulation(mProfile->mFontColor);
  475. switch( mTabPosition )
  476. {
  477. case AlignTop:
  478. case AlignBottom:
  479. renderJustifiedText( tabRect.point, tabRect.extent, text);
  480. break;
  481. }
  482. GFX->getDrawUtil()->setBitmapModulation( oldColor);
  483. }
  484. //-----------------------------------------------------------------------------
  485. void GuiTabBookCtrl::setUpdate()
  486. {
  487. Parent::setUpdate();
  488. setUpdateRegion(Point2I(0,0), getExtent());
  489. calculatePageTabs();
  490. }
  491. //-----------------------------------------------------------------------------
  492. S32 GuiTabBookCtrl::calculatePageTabWidth( GuiTabPageCtrl *page )
  493. {
  494. if( !page )
  495. return mMinTabWidth;
  496. const char* text = page->getText();
  497. if( !text || dStrlen(text) == 0 || mProfile == NULL || mProfile->mFont == NULL )
  498. return mMinTabWidth;
  499. GFont *font = mProfile->mFont;
  500. return font->getStrNWidth( text, dStrlen(text) );
  501. }
  502. //-----------------------------------------------------------------------------
  503. const RectI GuiTabBookCtrl::getClientRect()
  504. {
  505. if( !mProfile || mProfile->mBitmapArrayRects.size() < NumBitmaps )
  506. return Parent::getClientRect();
  507. return mPageRect;
  508. }
  509. //-----------------------------------------------------------------------------
  510. void GuiTabBookCtrl::calculatePageTabs()
  511. {
  512. // Short Circuit.
  513. //
  514. // If the tab size is zero, don't render tabs,
  515. // assuming it's a tab-less book
  516. if( mPages.empty() || mTabHeight <= 0 )
  517. {
  518. mPageRect.point.x = 0;
  519. mPageRect.point.y = 0;
  520. mPageRect.extent.x = getWidth();
  521. mPageRect.extent.y = getHeight();
  522. return;
  523. }
  524. S32 currRow = 0;
  525. S32 currColumn = 0;
  526. S32 currX = mFrontTabPadding;
  527. S32 maxWidth = 0;
  528. for( S32 i = 0; i < mPages.size(); i++ )
  529. {
  530. // Fetch Tab Width
  531. S32 tabWidth = calculatePageTabWidth( mPages[i].Page ) + ( mTabMargin * 2 );
  532. tabWidth = getMax( tabWidth, mMinTabWidth );
  533. TabHeaderInfo &info = mPages[i];
  534. switch( mTabPosition )
  535. {
  536. case AlignTop:
  537. case AlignBottom:
  538. // If we're going to go outside our bounds
  539. // with this tab move it down a row
  540. if( currX + tabWidth > getWidth() )
  541. {
  542. // Calculate and Advance State.
  543. maxWidth = getMax( tabWidth, maxWidth );
  544. balanceRow( currRow, currX );
  545. info.TabRow = ++currRow;
  546. // Reset Necessaries
  547. info.TabColumn = currColumn = maxWidth = currX = 0;
  548. }
  549. else
  550. {
  551. info.TabRow = currRow;
  552. info.TabColumn = currColumn++;
  553. }
  554. // Calculate Tabs Bounding Rect
  555. info.TabRect.point.x = currX;
  556. info.TabRect.extent.x = tabWidth;
  557. info.TabRect.extent.y = mTabHeight;
  558. // Adjust Y Point based on alignment
  559. if( mTabPosition == AlignTop )
  560. info.TabRect.point.y = ( info.TabRow * mTabHeight );
  561. else
  562. info.TabRect.point.y = getHeight() - ( ( 1 + info.TabRow ) * mTabHeight );
  563. currX += tabWidth;
  564. break;
  565. };
  566. }
  567. currRow++;
  568. currColumn++;
  569. Point2I localPoint = getExtent();
  570. // Calculate
  571. switch( mTabPosition )
  572. {
  573. case AlignTop:
  574. localPoint.y -= getTop();
  575. mTabRect.point.x = 0;
  576. mTabRect.extent.x = localPoint.x;
  577. mTabRect.point.y = 0;
  578. mTabRect.extent.y = currRow * mTabHeight;
  579. mPageRect.point.x = 0;
  580. mPageRect.point.y = mTabRect.extent.y;
  581. mPageRect.extent.x = mTabRect.extent.x;
  582. mPageRect.extent.y = getHeight() - mTabRect.extent.y;
  583. break;
  584. case AlignBottom:
  585. mTabRect.point.x = 0;
  586. mTabRect.extent.x = localPoint.x;
  587. mTabRect.extent.y = currRow * mTabHeight;
  588. mTabRect.point.y = getHeight() - mTabRect.extent.y;
  589. mPageRect.point.x = 0;
  590. mPageRect.point.y = 0;
  591. mPageRect.extent.x = mTabRect.extent.x;
  592. mPageRect.extent.y = localPoint.y - mTabRect.extent.y;
  593. break;
  594. }
  595. }
  596. //-----------------------------------------------------------------------------
  597. void GuiTabBookCtrl::balanceRow( S32 row, S32 totalTabWidth )
  598. {
  599. // Short Circuit.
  600. //
  601. // If the tab size is zero, don't render tabs,
  602. // and assume it's a tab-less tab-book - JDD
  603. if( mPages.empty() || mTabHeight <= 0 )
  604. return;
  605. Vector<TabHeaderInfo*> rowTemp;
  606. rowTemp.clear();
  607. for( S32 i = 0; i < mPages.size(); i++ )
  608. {
  609. TabHeaderInfo &info = mPages[i];
  610. if(info.TabRow == row )
  611. rowTemp.push_back( &mPages[i] );
  612. }
  613. if( rowTemp.empty() )
  614. return;
  615. // Balance the tabs across the remaining space
  616. S32 spaceToDivide = getWidth() - totalTabWidth;
  617. S32 pointDelta = 0;
  618. for( S32 i = 0; i < rowTemp.size(); i++ )
  619. {
  620. TabHeaderInfo &info = *rowTemp[i];
  621. S32 extraSpace = (S32)spaceToDivide / ( rowTemp.size() );
  622. info.TabRect.extent.x += extraSpace;
  623. info.TabRect.point.x += pointDelta;
  624. pointDelta += extraSpace;
  625. }
  626. }
  627. //-----------------------------------------------------------------------------
  628. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( const GuiEvent &event )
  629. {
  630. return findHitTab( event.mousePoint );
  631. }
  632. //-----------------------------------------------------------------------------
  633. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( Point2I hitPoint )
  634. {
  635. // Short Circuit.
  636. //
  637. // If the tab size is zero, don't render tabs,
  638. // and assume it's a tab-less tab-book - JDD
  639. if( mPages.empty() || mTabHeight <= 0 )
  640. return NULL;
  641. for( S32 i = 0; i < mPages.size(); i++ )
  642. {
  643. if( mPages[i].TabRect.pointInRect( hitPoint ) )
  644. return mPages[i].Page;
  645. }
  646. return NULL;
  647. }
  648. //-----------------------------------------------------------------------------
  649. void GuiTabBookCtrl::selectPage( S32 index )
  650. {
  651. if( mPages.empty() || index < 0 )
  652. return;
  653. if( mPages.size() <= index )
  654. index = mPages.size() - 1;
  655. // Select the page
  656. selectPage( mPages[ index ].Page );
  657. }
  658. //-----------------------------------------------------------------------------
  659. void GuiTabBookCtrl::selectPage( GuiTabPageCtrl *page )
  660. {
  661. // Return if already selected.
  662. if( mSelectedPageNum >= 0 && mSelectedPageNum < mPages.size() && mPages[ mSelectedPageNum ].Page == page )
  663. return;
  664. mSelectedPageNum = -1;
  665. Vector<TabHeaderInfo>::iterator i = mPages.begin();
  666. for( S32 index = 0; i != mPages.end() ; i++, index++ )
  667. {
  668. GuiTabPageCtrl *tab = (*i).Page;
  669. if( page == tab )
  670. {
  671. mActivePage = tab;
  672. tab->setVisible( true );
  673. mSelectedPageNum = index;
  674. // Notify User
  675. onTabSelected_callback( tab->getText(), index );
  676. }
  677. else
  678. tab->setVisible( false );
  679. }
  680. setUpdateLayout( updateSelf );
  681. }
  682. //-----------------------------------------------------------------------------
  683. bool GuiTabBookCtrl::_setSelectedPage( void *object, const char *index, const char *data )
  684. {
  685. GuiTabBookCtrl* book = reinterpret_cast< GuiTabBookCtrl* >( object );
  686. book->selectPage( dAtoi( data ) );
  687. return false;
  688. }
  689. //-----------------------------------------------------------------------------
  690. bool GuiTabBookCtrl::onKeyDown(const GuiEvent &event)
  691. {
  692. // Tab = Next Page
  693. // Ctrl-Tab = Previous Page
  694. if( 0 && event.keyCode == KEY_TAB )
  695. {
  696. if( event.modifier & SI_PRIMARY_CTRL )
  697. selectPrevPage();
  698. else
  699. selectNextPage();
  700. return true;
  701. }
  702. return Parent::onKeyDown( event );
  703. }
  704. //-----------------------------------------------------------------------------
  705. void GuiTabBookCtrl::selectNextPage()
  706. {
  707. if( mPages.empty() )
  708. return;
  709. if( mActivePage == NULL )
  710. mActivePage = mPages[0].Page;
  711. S32 nI = 0;
  712. for( ; nI < mPages.size(); nI++ )
  713. {
  714. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  715. if( tab == mActivePage )
  716. {
  717. if( nI == ( mPages.size() - 1 ) )
  718. selectPage( 0 );
  719. else if ( nI + 1 <= ( mPages.size() - 1 ) )
  720. selectPage( nI + 1 );
  721. else
  722. selectPage( 0 );
  723. return;
  724. }
  725. }
  726. }
  727. //-----------------------------------------------------------------------------
  728. void GuiTabBookCtrl::selectPrevPage()
  729. {
  730. if( mPages.empty() )
  731. return;
  732. if( mActivePage == NULL )
  733. mActivePage = mPages[0].Page;
  734. S32 nI = 0;
  735. for( ; nI < mPages.size(); nI++ )
  736. {
  737. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  738. if( tab == mActivePage )
  739. {
  740. if( nI == 0 )
  741. selectPage( mPages.size() - 1 );
  742. else
  743. selectPage( nI - 1 );
  744. return;
  745. }
  746. }
  747. }
  748. //-----------------------------------------------------------------------------
  749. void GuiTabBookCtrl::fitPage( GuiTabPageCtrl* page )
  750. {
  751. page->resize( mPageRect.point, mPageRect.extent );
  752. }
  753. //-----------------------------------------------------------------------------
  754. S32 GuiTabBookCtrl::getPageNum( GuiTabPageCtrl* page ) const
  755. {
  756. const U32 numPages = mPages.size();
  757. for( U32 i = 0; i < numPages; ++ i )
  758. if( mPages[ i ].Page == page )
  759. return i;
  760. return -1;
  761. }
  762. //=============================================================================
  763. // API.
  764. //=============================================================================
  765. // MARK: ---- API ----
  766. //-----------------------------------------------------------------------------
  767. DefineEngineMethod( GuiTabBookCtrl, addPage, void, ( const char* title ), ( "" ),
  768. "Add a new tab page to the control.\n\n"
  769. "@param title Title text for the tab page header." )
  770. {
  771. object->addNewPage( title );
  772. }
  773. //-----------------------------------------------------------------------------
  774. DefineEngineMethod( GuiTabBookCtrl, selectPage, void, ( S32 index ),,
  775. "Set the selected tab page.\n\n"
  776. "@param index Index of the tab page." )
  777. {
  778. object->selectPage( index );
  779. }
  780. //-----------------------------------------------------------------------------
  781. DefineEngineMethod( GuiTabBookCtrl, getSelectedPage, S32, (),,
  782. "Get the index of the currently selected tab page.\n\n"
  783. "@return Index of the selected tab page or -1 if no tab page is selected." )
  784. {
  785. return object->getSelectedPageNum();
  786. }