guiTabBookCtrl.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "graphics/dgl.h"
  25. #include "sim/simBase.h"
  26. #include "gui/guiCanvas.h"
  27. #include "gui/containers/guiTabBookCtrl.h"
  28. #include "platform/event.h"
  29. #include "io/fileStream.h"
  30. #include "gui/containers/guiScrollCtrl.h"
  31. #include "gui/editor/guiEditCtrl.h"
  32. #include "gui/guiDefaultControlRender.h"
  33. #include "guiTabBookCtrl_ScriptBinding.h"
  34. // So we can set tab alignment via gui editor
  35. static EnumTable::Enums tabAlignEnums[] =
  36. {
  37. { GuiTabBookCtrl::AlignTop, "Top" },
  38. { GuiTabBookCtrl::AlignLeft, "Left" },
  39. { GuiTabBookCtrl::AlignBottom,"Bottom" },
  40. { GuiTabBookCtrl::AlignRight, "Right" }
  41. };
  42. static EnumTable gTabAlignEnums(4,&tabAlignEnums[0]);
  43. IMPLEMENT_CONOBJECT(GuiTabBookCtrl);
  44. GuiTabBookCtrl::GuiTabBookCtrl()
  45. {
  46. VECTOR_SET_ASSOCIATION(mPages);
  47. mFontHeight = 0;
  48. mTabPosition = GuiTabBookCtrl::AlignTop;
  49. mLastTabPosition = mTabPosition;
  50. mActivePage = NULL;
  51. mHoverTab = NULL;
  52. mHasTexture = false;
  53. mBitmapBounds = NULL;
  54. mBounds.extent.set( 400, 300 );
  55. mPageRect = RectI(0,0,0,0);
  56. mTabRect = RectI(0,0,0,0);
  57. mPages.reserve(12);
  58. mMinTabWidth = 64;
  59. mTabWidth = 64;
  60. mIsContainer = true;
  61. mTabProfile = NULL;
  62. setField("TabProfile", "GuiTabProfile");
  63. }
  64. void GuiTabBookCtrl::initPersistFields()
  65. {
  66. Parent::initPersistFields();
  67. addField("TabPosition", TypeEnum, Offset(mTabPosition,GuiTabBookCtrl), 1, &gTabAlignEnums );
  68. addField("MinTabWidth", TypeS32, Offset(mMinTabWidth,GuiTabBookCtrl));
  69. addField("TabProfile", TypeGuiProfile, Offset(mTabProfile, GuiTabBookCtrl));
  70. }
  71. // Empty for now, will implement for handling design time context menu for manipulating pages
  72. ConsoleMethod( GuiTabBookCtrl, addPage, void, 2, 2, "() Empty")
  73. {
  74. object->addNewPage();
  75. }
  76. //ConsoleMethod( GuiTabBookCtrl, removePage, void, 2, 2, "()")
  77. //{
  78. //}
  79. bool GuiTabBookCtrl::onAdd()
  80. {
  81. Parent::onAdd();
  82. return true;
  83. }
  84. void GuiTabBookCtrl::onRemove()
  85. {
  86. Parent::onRemove();
  87. }
  88. void GuiTabBookCtrl::onChildRemoved( GuiControl* child )
  89. {
  90. for (S32 i = 0; i < mPages.size(); i++ )
  91. {
  92. GuiTabPageCtrl* tab = mPages[i].Page;
  93. if( tab == child )
  94. {
  95. if( tab == mActivePage )
  96. mActivePage = NULL;
  97. mPages.erase( i );
  98. break;
  99. }
  100. }
  101. if( mPages.empty() )
  102. mActivePage = NULL;
  103. else if (mActivePage == NULL )
  104. mActivePage = static_cast<GuiTabPageCtrl*>(mPages[0].Page);
  105. }
  106. void GuiTabBookCtrl::onChildAdded( GuiControl *child )
  107. {
  108. GuiTabPageCtrl *page = dynamic_cast<GuiTabPageCtrl*>(child);
  109. if( !page )
  110. {
  111. Con::warnf("GuiTabBookCtrl::onChildAdded - attempting to add NON GuiTabPageCtrl as child page");
  112. SimObject *simObj = reinterpret_cast<SimObject*>(child);
  113. removeObject( simObj );
  114. if( mActivePage )
  115. {
  116. mActivePage->addObject( simObj );
  117. }
  118. else
  119. {
  120. Con::warnf("GuiTabBookCtrl::onChildAdded - unable to find active page to reassign ownership of new child control to, placing on parent");
  121. GuiControl *rent = getParent();
  122. if( rent )
  123. rent->addObject( simObj );
  124. }
  125. return;
  126. }
  127. TabHeaderInfo newPage;
  128. newPage.Page = page;
  129. newPage.TabRow = -1;
  130. newPage.TabColumn = -1;
  131. mPages.push_back( newPage );
  132. // Calculate Page Information
  133. calculatePageTabs();
  134. child->resize( Point2I(0, 0), mPageRect.extent );
  135. }
  136. bool GuiTabBookCtrl::onWake()
  137. {
  138. if (! Parent::onWake())
  139. return false;
  140. mHasTexture = mProfile->constructBitmapArray();
  141. if( mHasTexture )
  142. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  143. //increment the tab profile
  144. mTabProfile->incRefCount();
  145. return true;
  146. }
  147. void GuiTabBookCtrl::onSleep()
  148. {
  149. Parent::onSleep();
  150. //decrement the tab profile referrence
  151. if (mTabProfile != NULL)
  152. mTabProfile->decRefCount();
  153. }
  154. void GuiTabBookCtrl::setControlTabProfile(GuiControlProfile* prof)
  155. {
  156. AssertFatal(prof, "GuiTabBookCtrl::setControlTabProfile: invalid tab profile");
  157. if (prof == mTabProfile)
  158. return;
  159. if (mAwake)
  160. mTabProfile->decRefCount();
  161. mTabProfile = prof;
  162. if (mAwake)
  163. mTabProfile->incRefCount();
  164. calculatePageTabs();
  165. }
  166. void GuiTabBookCtrl::addNewPage()
  167. {
  168. char textbuf[1024];
  169. GuiTabPageCtrl * page = new GuiTabPageCtrl();
  170. page->setField("profile", "GuiTabPageProfile");
  171. dSprintf(textbuf, sizeof(textbuf), "TabBookPage%d_%d", getId(), page->getId());
  172. page->registerObject(textbuf);
  173. this->addObject( page );
  174. }
  175. void GuiTabBookCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  176. {
  177. Parent::resize( newPosition, newExtent );
  178. calculatePageTabs();
  179. // Resize Children
  180. SimSet::iterator i;
  181. for(i = begin(); i != end(); i++)
  182. {
  183. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  184. ctrl->resize( Point2I(0, 0), mPageRect.extent );
  185. }
  186. }
  187. void GuiTabBookCtrl::childResized(GuiControl *child)
  188. {
  189. child->resize( Point2I(0,0), mPageRect.extent );
  190. }
  191. Point2I GuiTabBookCtrl::getTabLocalCoord(const Point2I &src)
  192. {
  193. //Get the border profiles
  194. GuiBorderProfile *leftProfile = mProfile->getLeftBorder();
  195. GuiBorderProfile *topProfile = mProfile->getTopBorder();
  196. S32 leftSize = (leftProfile) ? leftProfile->getMargin(NormalState) + leftProfile->getBorder(NormalState) + leftProfile->getPadding(NormalState) : 0;
  197. S32 topSize = (topProfile) ? topProfile->getMargin(NormalState) + topProfile->getBorder(NormalState) + topProfile->getPadding(NormalState) : 0;
  198. Point2I ret = Point2I(src.x - leftSize, src.y - topSize);
  199. ret.x -= mTabRect.point.x;
  200. ret.y -= mTabRect.point.y;
  201. return ret;
  202. }
  203. void GuiTabBookCtrl::onTouchDown(const GuiEvent &event)
  204. {
  205. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  206. if( mTabRect.pointInRect( localMouse ) )
  207. {
  208. Point2I tabLocalMouse = getTabLocalCoord(localMouse);
  209. GuiTabPageCtrl *tab = findHitTab(tabLocalMouse);
  210. if( tab != NULL && tab->isActive() )
  211. selectPage( tab );
  212. }
  213. }
  214. void GuiTabBookCtrl::onTouchMove(const GuiEvent &event)
  215. {
  216. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  217. if( mTabRect.pointInRect( localMouse ) )
  218. {
  219. Point2I tabLocalMouse = getTabLocalCoord(localMouse);
  220. GuiTabPageCtrl *tab = findHitTab(tabLocalMouse);
  221. if( tab != NULL && mHoverTab != tab )
  222. mHoverTab = tab;
  223. else if ( !tab )
  224. mHoverTab = NULL;
  225. }
  226. else
  227. {
  228. mHoverTab = NULL;
  229. }
  230. Parent::onTouchMove( event );
  231. }
  232. void GuiTabBookCtrl::onTouchLeave( const GuiEvent &event )
  233. {
  234. mHoverTab = NULL;
  235. }
  236. bool GuiTabBookCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset)
  237. {
  238. bool handled = false;
  239. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  240. if( mTabRect.pointInRect( localMouse ) )
  241. {
  242. GuiTabPageCtrl *tab = findHitTab( localMouse );
  243. if( tab != NULL )
  244. {
  245. selectPage( tab );
  246. handled = true;
  247. }
  248. }
  249. // This shouldn't be called if it's not design time, but check just incase
  250. if ( GuiControl::smDesignTime )
  251. {
  252. // If we clicked in the editor and our addset is the tab book
  253. // ctrl, select the child ctrl so we can edit it's properties
  254. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  255. if( edit && ( edit->getAddSet() == this ) && mActivePage != NULL )
  256. edit->select( mActivePage );
  257. }
  258. // Return whether we handled this or not.
  259. return handled;
  260. }
  261. void GuiTabBookCtrl::onPreRender()
  262. {
  263. // sometimes we need to resize because of a changed persistent field
  264. // that's what this does
  265. solveDirty();
  266. }
  267. void GuiTabBookCtrl::onRender(Point2I offset, const RectI &updateRect)
  268. {
  269. RectI ctrlRect = applyMargins(offset + mTabRect.point, mTabRect.extent, NormalState, mProfile);
  270. if (!ctrlRect.isValidRect())
  271. {
  272. return;
  273. }
  274. renderUniversalRect(ctrlRect, mProfile, NormalState);
  275. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  276. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  277. if (contentRect.isValidRect())
  278. {
  279. renderTabs(contentRect.point);
  280. }
  281. if(mPageRect.isValidRect())
  282. {
  283. // Render Children
  284. renderChildControls(offset, RectI(offset + mPageRect.point, mPageRect.extent), updateRect);
  285. }
  286. }
  287. void GuiTabBookCtrl::renderTabs( const Point2I &offset )
  288. {
  289. // If the tab size is zero, don't render tabs,
  290. // and assume it's a tab-less tab-book - JDD
  291. if( mPages.empty())
  292. return;
  293. for( S32 i = 0; i < mPages.size(); i++ )
  294. {
  295. RectI tabBounds = mPages[i].TabRect;
  296. tabBounds.point += offset;
  297. GuiTabPageCtrl *tab = mPages[i].Page;
  298. if( tab != NULL )
  299. renderTab( tabBounds, tab );
  300. }
  301. }
  302. void GuiTabBookCtrl::renderTab( RectI tabRect, GuiTabPageCtrl *tab )
  303. {
  304. StringTableEntry text = tab->getText();
  305. GuiControlState currentState = GuiControlState::NormalState;
  306. if (mActivePage == tab)
  307. {
  308. currentState = SelectedState;
  309. }
  310. else if (mHoverTab == tab)
  311. {
  312. currentState = HighlightState;
  313. }
  314. RectI ctrlRect = applyMargins(tabRect.point, tabRect.extent, currentState, mTabProfile);
  315. if (!ctrlRect.isValidRect())
  316. {
  317. return;
  318. }
  319. renderUniversalRect(ctrlRect, mTabProfile, currentState);
  320. //Render Text
  321. dglSetBitmapModulation(mTabProfile->getFontColor(currentState));
  322. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mTabProfile);
  323. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mTabProfile);
  324. TextRotationOptions rot = tRotateNone;
  325. if (mTabPosition == AlignLeft)
  326. {
  327. rot = tRotateLeft;
  328. }
  329. else if(mTabPosition == AlignRight)
  330. {
  331. rot = tRotateRight;
  332. }
  333. renderText(contentRect.point, contentRect.extent, text, mTabProfile, rot);
  334. /*
  335. // Is this a skinned control?
  336. if( mHasTexture && mProfile->mBitmapArrayRects.size() >= 9 )
  337. {
  338. S32 indexMultiplier = 1;
  339. switch( mTabPosition )
  340. {
  341. case AlignTop:
  342. case AlignBottom:
  343. if ( mActivePage == tab )
  344. indexMultiplier += TabSelected;
  345. else if( mHoverTab == tab )
  346. indexMultiplier += TabHover;
  347. else
  348. indexMultiplier += TabNormal;
  349. //dglDrawBitmapStretchSR(mProfile->mTextureHandle,tabRect,stretchRect, ( mTabPosition == AlignBottom ) ? GFlip_Y : 0 );
  350. break;
  351. case AlignLeft:
  352. case AlignRight:
  353. if ( mActivePage == tab )
  354. indexMultiplier += TabSelectedVertical;
  355. else if( mHoverTab == tab )
  356. indexMultiplier += TabHoverVertical;
  357. else
  358. indexMultiplier += TabNormalVertical;
  359. //dglDrawBitmapStretchSR(mProfile->mTextureHandle,tabRect,stretchRect, ( mTabPosition == AlignRight ) ? GFlip_X : 0 );
  360. break;
  361. }
  362. renderFixedBitmapBordersFilled( tabRect, indexMultiplier, mProfile );
  363. }
  364. else
  365. {
  366. // If this isn't a skinned control or the bitmap is simply missing, handle it WELL
  367. if ( mActivePage == tab )
  368. dglDrawRectFill(tabRect, mProfile->mFillColor);
  369. else if( mHoverTab == tab )
  370. dglDrawRectFill(tabRect, mProfile->mFillColorHL);
  371. else
  372. dglDrawRectFill(tabRect, mProfile->mFillColorNA);
  373. }
  374. dglSetBitmapModulation(mProfile->mFontColor);
  375. switch( mTabPosition )
  376. {
  377. case AlignTop:
  378. case AlignBottom:
  379. renderJustifiedTextRot( tabRect.point, tabRect.extent, text, 0);
  380. break;
  381. case AlignLeft:
  382. renderJustifiedTextRot( tabRect.point, tabRect.extent, text, -90 );
  383. break;
  384. case AlignRight:
  385. renderJustifiedTextRot( tabRect.point, tabRect.extent, text, -90 );
  386. break;
  387. }
  388. */
  389. }
  390. // This is nothing but a clever hack to allow the tab page children
  391. // to cast this to a GuiControl* so that the file doesn't need to have circular
  392. // includes. generic method overriding for the win!
  393. void GuiTabBookCtrl::setUpdate()
  394. {
  395. Parent::setUpdate();
  396. setUpdateRegion(Point2I(0,0), mBounds.extent);
  397. calculatePageTabs();
  398. }
  399. void GuiTabBookCtrl::solveDirty()
  400. {
  401. bool dirty = false;
  402. if( mTabPosition != mLastTabPosition )
  403. {
  404. mLastTabPosition = mTabPosition;
  405. dirty = true;
  406. }
  407. else if( mTabProfile != NULL && mTabProfile->mFont != NULL && mTabProfile->mFont->getHeight() != mFontHeight )
  408. {
  409. dirty = true;
  410. }
  411. else if(mPages.size() > 0 && mTabProfile != NULL && mTabProfile->mFont != NULL)
  412. {
  413. S32 tabWidth = calculatePageTabWidth(mPages[0].Page);
  414. tabWidth = getMax(tabWidth, mMinTabWidth);
  415. if(mTabWidth != tabWidth)
  416. {
  417. dirty = true;
  418. }
  419. }
  420. if( dirty )
  421. {
  422. resize( mBounds.point, mBounds.extent );
  423. }
  424. }
  425. S32 GuiTabBookCtrl::calculatePageTabWidth( GuiTabPageCtrl *page )
  426. {
  427. if( !page )
  428. return mTabWidth;
  429. StringTableEntry text = page->getText();
  430. if( !text || dStrlen(text) == 0 || !mTabProfile || !mTabProfile->mFont || mTabProfile->mFont == '\0' )
  431. return mTabWidth;
  432. S32 textLength = mTabProfile->mFont->getStrNWidth(text, dStrlen(text));
  433. Point2I outerExtent = getOuterExtent(Point2I(textLength, textLength), NormalState, mTabProfile);
  434. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  435. {
  436. return outerExtent.x;
  437. }
  438. else
  439. {
  440. return outerExtent.y;
  441. }
  442. }
  443. void GuiTabBookCtrl::calculatePageTabs()
  444. {
  445. // Short Circuit.
  446. //
  447. // If the tab size is zero, don't render tabs,
  448. // and assume it's a tab-less tab-book - JDD
  449. if( mPages.empty())
  450. return;
  451. S32 currRow = 0;
  452. S32 currColumn = 0;
  453. S32 currX = 0;
  454. S32 currY = 0;
  455. S32 tabHeight = 0;
  456. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  457. Point2I fontBasedBounds = getOuterExtent(Point2I(mTabProfile->mFont->getHeight(), mTabProfile->mFont->getHeight()), NormalState, mTabProfile);
  458. mFontHeight = mTabProfile->mFont->getHeight();
  459. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  460. {
  461. tabHeight = fontBasedBounds.y;
  462. }
  463. else
  464. {
  465. tabHeight = fontBasedBounds.x;
  466. }
  467. for( S32 i = 0; i < mPages.size(); i++ )
  468. {
  469. // Fetch Tab Width
  470. S32 tabWidth = calculatePageTabWidth( mPages[i].Page );
  471. tabWidth = getMax( tabWidth, mMinTabWidth );
  472. if (i == 0)
  473. {
  474. mTabWidth = tabWidth;
  475. }
  476. TabHeaderInfo &info = mPages[i];
  477. switch( mTabPosition )
  478. {
  479. case AlignTop:
  480. case AlignBottom:
  481. // If we're going to go outside our bounds
  482. // with this tab move it down a row
  483. if( currX + tabWidth > innerRect.extent.x )
  484. {
  485. // Calculate and Advance State.
  486. balanceRow( currRow, currX );
  487. info.TabRow = ++currRow;
  488. // Reset Necessaries
  489. info.TabColumn = currColumn = currX = 0;
  490. }
  491. else
  492. {
  493. info.TabRow = currRow;
  494. info.TabColumn = currColumn++;
  495. }
  496. // Calculate Tabs Bounding Rect
  497. info.TabRect.point.x = currX;
  498. info.TabRect.point.y = (info.TabRow * tabHeight);
  499. info.TabRect.extent.x = tabWidth;
  500. info.TabRect.extent.y = tabHeight;
  501. currX += tabWidth;
  502. break;
  503. case AlignLeft:
  504. case AlignRight:
  505. // If we're going to go outside our bounds
  506. // with this tab move it down a row
  507. if( currY + tabWidth > innerRect.extent.y )
  508. {
  509. // Balance Tab Column.
  510. balanceColumn( currColumn, currY );
  511. // Calculate and Advance State.
  512. info.TabColumn = ++currColumn;
  513. info.TabRow = currRow = currY = 0;
  514. }
  515. else
  516. {
  517. info.TabColumn = currColumn;
  518. info.TabRow = currRow++;
  519. }
  520. // Calculate Tabs Bounding Rect
  521. info.TabRect.point.x = (info.TabColumn * tabHeight);
  522. info.TabRect.point.y = currY;
  523. info.TabRect.extent.x = tabHeight;
  524. info.TabRect.extent.y = tabWidth;
  525. currY += tabWidth;
  526. break;
  527. };
  528. }
  529. currRow++;
  530. currColumn++;
  531. Point2I outerExtent = getOuterExtent(Point2I(currColumn * tabHeight, currRow * tabHeight), NormalState, mProfile);
  532. // Calculate
  533. switch( mTabPosition )
  534. {
  535. case AlignTop:
  536. mTabRect.point.x = 0;
  537. mTabRect.point.y = 0;
  538. mTabRect.extent.x = mBounds.extent.x;
  539. mTabRect.extent.y = outerExtent.y;
  540. mPageRect.point.x = 0;
  541. mPageRect.point.y = mTabRect.extent.y;
  542. mPageRect.extent.x = mTabRect.extent.x;
  543. mPageRect.extent.y = mBounds.extent.y - mTabRect.extent.y;
  544. break;
  545. case AlignBottom:
  546. mTabRect.point.x = 0;
  547. mTabRect.point.y = mBounds.extent.y - mTabRect.extent.y;
  548. mTabRect.extent.x = mBounds.extent.x;
  549. mTabRect.extent.y = outerExtent.y;
  550. mPageRect.point.x = 0;
  551. mPageRect.point.y = 0;
  552. mPageRect.extent.x = mTabRect.extent.x;
  553. mPageRect.extent.y = mBounds.extent.y - mTabRect.extent.y;
  554. break;
  555. case AlignLeft:
  556. mTabRect.point.x = 0;
  557. mTabRect.point.y = 0;
  558. mTabRect.extent.x = outerExtent.x;
  559. mTabRect.extent.y = mBounds.extent.y;
  560. mPageRect.point.x = mTabRect.extent.x;
  561. mPageRect.point.y = 0;
  562. mPageRect.extent.x = mBounds.extent.x - mTabRect.extent.x;
  563. mPageRect.extent.y = mBounds.extent.y;
  564. break;
  565. case AlignRight:
  566. mTabRect.point.x = mBounds.extent.x - mTabRect.extent.x;
  567. mTabRect.point.y = 0;
  568. mTabRect.extent.x = outerExtent.x;
  569. mTabRect.extent.y = mBounds.extent.y;
  570. mPageRect.point.x = 0;
  571. mPageRect.point.y = 0;
  572. mPageRect.extent.x = mBounds.extent.x - mTabRect.extent.x;
  573. mPageRect.extent.y = mTabRect.extent.y;
  574. break;
  575. };
  576. }
  577. void GuiTabBookCtrl::balanceColumn( S32 column , S32 totalTabWidth )
  578. {
  579. // Short Circuit.
  580. //
  581. // If the tab size is zero, don't render tabs,
  582. // and assume it's a tab-less tab-book - JDD
  583. if( mPages.empty())
  584. return;
  585. Vector<TabHeaderInfo*> rowTemp;
  586. rowTemp.clear();
  587. for( S32 i = 0; i < mPages.size(); i++ )
  588. {
  589. TabHeaderInfo &info = mPages[i];
  590. if(info.TabColumn == column )
  591. rowTemp.push_back( &mPages[i] );
  592. }
  593. if( rowTemp.empty() )
  594. return;
  595. // Balance the tabs across the remaining space
  596. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  597. S32 spaceToDivide = innerRect.extent.y - totalTabWidth;
  598. S32 pointDelta = 0;
  599. for( S32 i = 0; i < rowTemp.size(); i++ )
  600. {
  601. TabHeaderInfo &info = *rowTemp[i];
  602. S32 extraSpace = (S32)( spaceToDivide / rowTemp.size() );
  603. info.TabRect.extent.y += extraSpace;
  604. info.TabRect.point.y += pointDelta;
  605. pointDelta += extraSpace;
  606. }
  607. }
  608. void GuiTabBookCtrl::balanceRow( S32 row, S32 totalTabWidth )
  609. {
  610. // Short Circuit.
  611. //
  612. // If the tab size is zero, don't render tabs,
  613. // and assume it's a tab-less tab-book - JDD
  614. if( mPages.empty())
  615. return;
  616. Vector<TabHeaderInfo*> rowTemp;
  617. rowTemp.clear();
  618. for( S32 i = 0; i < mPages.size(); i++ )
  619. {
  620. TabHeaderInfo &info = mPages[i];
  621. if(info.TabRow == row )
  622. rowTemp.push_back( &mPages[i] );
  623. }
  624. if( rowTemp.empty() )
  625. return;
  626. // Balance the tabs across the remaining space
  627. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  628. S32 spaceToDivide = innerRect.extent.x - totalTabWidth;
  629. S32 pointDelta = 0;
  630. for( S32 i = 0; i < rowTemp.size(); i++ )
  631. {
  632. TabHeaderInfo &info = *rowTemp[i];
  633. S32 extraSpace = (S32)spaceToDivide / ( rowTemp.size() );
  634. info.TabRect.extent.x += extraSpace;
  635. info.TabRect.point.x += pointDelta;
  636. pointDelta += extraSpace;
  637. }
  638. }
  639. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( const GuiEvent &event )
  640. {
  641. return findHitTab( event.mousePoint );
  642. }
  643. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( Point2I hitPoint )
  644. {
  645. // Short Circuit.
  646. //
  647. // If the tab size is zero, don't render tabs,
  648. // and assume it's a tab-less tab-book - JDD
  649. if( mPages.empty())
  650. return NULL;
  651. for( S32 i = 0; i < mPages.size(); i++ )
  652. {
  653. if( mPages[i].TabRect.pointInRect( hitPoint ) )
  654. return mPages[i].Page;
  655. }
  656. return NULL;
  657. }
  658. U32 GuiTabBookCtrl::getSelectedPage()
  659. {
  660. U32 index = 0;
  661. for (U32 i = 0; i < mPages.size(); i++)
  662. {
  663. if (mActivePage == mPages[i].Page)
  664. {
  665. index = i;
  666. break;
  667. }
  668. }
  669. return index;
  670. }
  671. void GuiTabBookCtrl::selectPage( S32 index )
  672. {
  673. if( index < 0 || index >= mPages.size())
  674. return;
  675. // Select the page
  676. selectPage( mPages[ index ].Page );
  677. }
  678. void GuiTabBookCtrl::selectPage( GuiTabPageCtrl *page )
  679. {
  680. Vector<TabHeaderInfo>::iterator i = mPages.begin();
  681. for( ; i != mPages.end() ; i++ )
  682. {
  683. GuiTabPageCtrl *tab = reinterpret_cast<GuiTabPageCtrl*>((*i).Page);
  684. if( page == tab )
  685. {
  686. mActivePage = tab;
  687. tab->setVisible( true );
  688. // Notify User
  689. char *retBuffer = Con::getReturnBuffer( 512 );
  690. dStrcpy( retBuffer, tab->getText() );
  691. Con::executef( this, 2, "onTabSelected", retBuffer );
  692. }
  693. else
  694. tab->setVisible( false );
  695. }
  696. }
  697. void GuiTabBookCtrl::selectPage( const char* pageName )
  698. {
  699. Vector<TabHeaderInfo>::iterator i = mPages.begin();
  700. for( ; i != mPages.end() ; i++ )
  701. {
  702. GuiTabPageCtrl *tab = reinterpret_cast<GuiTabPageCtrl*>((*i).Page);
  703. if( dStricmp( pageName, tab->getText() ) == 0 )
  704. {
  705. mActivePage = tab;
  706. tab->setVisible( true );
  707. // Notify User
  708. char *retBuffer = Con::getReturnBuffer( 512 );
  709. dStrcpy( retBuffer, tab->getText() );
  710. Con::executef( this, 2, "onTabSelected", retBuffer );
  711. }
  712. else
  713. tab->setVisible( false );
  714. }
  715. }
  716. bool GuiTabBookCtrl::onKeyDown(const GuiEvent &event)
  717. {
  718. // Tab = Next Page
  719. // Ctrl-Tab = Previous Page
  720. if( 0 && event.keyCode == KEY_TAB )
  721. {
  722. if( event.modifier & SI_CTRL )
  723. selectPrevPage();
  724. else
  725. selectNextPage();
  726. return true;
  727. }
  728. return Parent::onKeyDown( event );
  729. }
  730. void GuiTabBookCtrl::selectNextPage()
  731. {
  732. if( mPages.empty() )
  733. return;
  734. if( mActivePage == NULL )
  735. mActivePage = mPages[0].Page;
  736. S32 nI = 0;
  737. for( ; nI < mPages.size(); nI++ )
  738. {
  739. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  740. if( tab == mActivePage )
  741. {
  742. if( nI == ( mPages.size() - 1 ) )
  743. selectPage( 0 );
  744. else if ( nI + 1 <= ( mPages.size() - 1 ) )
  745. selectPage( nI + 1 );
  746. else
  747. selectPage( 0 );
  748. // Notify User
  749. if( isMethod( "onTabSelected" ) )
  750. {
  751. char *retBuffer = Con::getReturnBuffer( 512 );
  752. dStrcpy( retBuffer, tab->getText() );
  753. Con::executef( this, 2, "onTabSelected", retBuffer );
  754. }
  755. return;
  756. }
  757. }
  758. }
  759. void GuiTabBookCtrl::selectPrevPage()
  760. {
  761. if( mPages.empty() )
  762. return;
  763. if( mActivePage == NULL )
  764. mActivePage = mPages[0].Page;
  765. S32 nI = 0;
  766. for( ; nI < mPages.size(); nI++ )
  767. {
  768. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  769. if( tab == mActivePage )
  770. {
  771. if( nI == 0 )
  772. selectPage( mPages.size() - 1 );
  773. else
  774. selectPage( nI - 1 );
  775. // Notify User
  776. if( isMethod( "onTabSelected" ) )
  777. {
  778. char *retBuffer = Con::getReturnBuffer( 512 );
  779. dStrcpy( retBuffer, tab->getText() );
  780. Con::executef( this, 2, "onTabSelected", retBuffer );
  781. }
  782. return;
  783. }
  784. }
  785. }