guiTabBookCtrl.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. else
  214. {
  215. Parent::onTouchDown(event);
  216. }
  217. }
  218. void GuiTabBookCtrl::onTouchMove(const GuiEvent &event)
  219. {
  220. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  221. if( mTabRect.pointInRect( localMouse ) )
  222. {
  223. Point2I tabLocalMouse = getTabLocalCoord(localMouse);
  224. GuiTabPageCtrl *tab = findHitTab(tabLocalMouse);
  225. if( tab != NULL && mHoverTab != tab )
  226. mHoverTab = tab;
  227. else if ( !tab )
  228. mHoverTab = NULL;
  229. }
  230. else
  231. {
  232. mHoverTab = NULL;
  233. }
  234. Parent::onTouchMove( event );
  235. }
  236. void GuiTabBookCtrl::onTouchLeave( const GuiEvent &event )
  237. {
  238. mHoverTab = NULL;
  239. }
  240. bool GuiTabBookCtrl::onMouseDownEditor(const GuiEvent &event, Point2I offset)
  241. {
  242. bool handled = false;
  243. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  244. if( mTabRect.pointInRect( localMouse ) )
  245. {
  246. GuiTabPageCtrl *tab = findHitTab( localMouse );
  247. if( tab != NULL )
  248. {
  249. selectPage( tab );
  250. handled = true;
  251. }
  252. }
  253. // This shouldn't be called if it's not design time, but check just incase
  254. if ( GuiControl::smDesignTime )
  255. {
  256. // If we clicked in the editor and our addset is the tab book
  257. // ctrl, select the child ctrl so we can edit it's properties
  258. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  259. if( edit && ( edit->getAddSet() == this ) && mActivePage != NULL )
  260. edit->select( mActivePage );
  261. }
  262. // Return whether we handled this or not.
  263. return handled;
  264. }
  265. void GuiTabBookCtrl::onPreRender()
  266. {
  267. // sometimes we need to resize because of a changed persistent field
  268. // that's what this does
  269. solveDirty();
  270. }
  271. void GuiTabBookCtrl::onRender(Point2I offset, const RectI &updateRect)
  272. {
  273. Point2I totalOffset = offset + mTabRect.point;
  274. RectI ctrlRect = applyMargins(totalOffset, mTabRect.extent, NormalState, mProfile);
  275. if (!ctrlRect.isValidRect())
  276. {
  277. return;
  278. }
  279. renderUniversalRect(ctrlRect, mProfile, NormalState);
  280. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  281. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  282. if (contentRect.isValidRect())
  283. {
  284. renderTabs(contentRect.point);
  285. }
  286. if(mPageRect.isValidRect())
  287. {
  288. // Render Children
  289. renderChildControls(offset, RectI(offset + mPageRect.point, mPageRect.extent), updateRect);
  290. }
  291. }
  292. void GuiTabBookCtrl::renderTabs( const Point2I &offset )
  293. {
  294. // If the tab size is zero, don't render tabs,
  295. // and assume it's a tab-less tab-book - JDD
  296. if( mPages.empty())
  297. return;
  298. for( S32 i = 0; i < mPages.size(); i++ )
  299. {
  300. RectI tabBounds = mPages[i].TabRect;
  301. tabBounds.point += offset;
  302. GuiTabPageCtrl *tab = mPages[i].Page;
  303. if( tab != NULL )
  304. renderTab( tabBounds, tab );
  305. }
  306. }
  307. void GuiTabBookCtrl::renderTab( RectI tabRect, GuiTabPageCtrl *tab )
  308. {
  309. StringTableEntry text = tab->getText();
  310. GuiControlState currentState = GuiControlState::NormalState;
  311. if (mActivePage == tab)
  312. {
  313. currentState = SelectedState;
  314. }
  315. else if (mHoverTab == tab)
  316. {
  317. currentState = HighlightState;
  318. }
  319. RectI ctrlRect = applyMargins(tabRect.point, tabRect.extent, currentState, mTabProfile);
  320. if (!ctrlRect.isValidRect())
  321. {
  322. return;
  323. }
  324. renderUniversalRect(ctrlRect, mTabProfile, currentState);
  325. //Render Text
  326. dglSetBitmapModulation(getFontColor(mTabProfile, currentState));
  327. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mTabProfile);
  328. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mTabProfile);
  329. TextRotationOptions rot = tRotateNone;
  330. if (mTabPosition == AlignLeft)
  331. {
  332. rot = tRotateLeft;
  333. }
  334. else if(mTabPosition == AlignRight)
  335. {
  336. rot = tRotateRight;
  337. }
  338. renderText(contentRect.point, contentRect.extent, text, mTabProfile, rot);
  339. }
  340. // This is nothing but a clever hack to allow the tab page children
  341. // to cast this to a GuiControl* so that the file doesn't need to have circular
  342. // includes. generic method overriding for the win!
  343. void GuiTabBookCtrl::setUpdate()
  344. {
  345. Parent::setUpdate();
  346. setUpdateRegion(Point2I(0,0), mBounds.extent);
  347. calculatePageTabs();
  348. }
  349. void GuiTabBookCtrl::solveDirty()
  350. {
  351. bool dirty = false;
  352. GFont* font = mTabProfile->getFont(mFontSizeAdjust);
  353. if( mTabPosition != mLastTabPosition )
  354. {
  355. mLastTabPosition = mTabPosition;
  356. dirty = true;
  357. }
  358. else if( mTabProfile != NULL && font != NULL && font->getHeight() != mFontHeight )
  359. {
  360. dirty = true;
  361. }
  362. else if(mPages.size() > 0 && mTabProfile != NULL && font != NULL)
  363. {
  364. S32 tabWidth = calculatePageTabWidth(mPages[0].Page);
  365. tabWidth = getMax(tabWidth, mMinTabWidth);
  366. if(mTabWidth != tabWidth)
  367. {
  368. dirty = true;
  369. }
  370. }
  371. if( dirty )
  372. {
  373. resize( mBounds.point, mBounds.extent );
  374. }
  375. }
  376. S32 GuiTabBookCtrl::calculatePageTabWidth( GuiTabPageCtrl *page )
  377. {
  378. if( !page )
  379. return mTabWidth;
  380. StringTableEntry text = page->getText();
  381. if( !text || dStrlen(text) == 0 || !mTabProfile )
  382. return mTabWidth;
  383. S32 textLength = mTabProfile->getFont(mFontSizeAdjust)->getStrNWidth(text, dStrlen(text));
  384. Point2I innerExtent = Point2I(textLength, textLength);
  385. Point2I outerExtent = getOuterExtent(innerExtent, NormalState, mTabProfile);
  386. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  387. {
  388. return outerExtent.x;
  389. }
  390. else
  391. {
  392. return outerExtent.y;
  393. }
  394. }
  395. void GuiTabBookCtrl::calculatePageTabs()
  396. {
  397. // Short Circuit.
  398. //
  399. // If the tab size is zero, don't render tabs,
  400. // and assume it's a tab-less tab-book - JDD
  401. if( mPages.empty())
  402. return;
  403. S32 currRow = 0;
  404. S32 currColumn = 0;
  405. S32 currX = 0;
  406. S32 currY = 0;
  407. S32 tabHeight = 0;
  408. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  409. mFontHeight = mTabProfile->getFont(mFontSizeAdjust)->getHeight();
  410. Point2I innerExtent = Point2I(mFontHeight, mFontHeight);
  411. Point2I fontBasedBounds = getOuterExtent(innerExtent, NormalState, mTabProfile);
  412. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  413. {
  414. tabHeight = fontBasedBounds.y;
  415. }
  416. else
  417. {
  418. tabHeight = fontBasedBounds.x;
  419. }
  420. for( S32 i = 0; i < mPages.size(); i++ )
  421. {
  422. // Fetch Tab Width
  423. S32 tabWidth = calculatePageTabWidth( mPages[i].Page );
  424. tabWidth = getMax( tabWidth, mMinTabWidth );
  425. if (i == 0)
  426. {
  427. mTabWidth = tabWidth;
  428. }
  429. TabHeaderInfo &info = mPages[i];
  430. switch( mTabPosition )
  431. {
  432. case AlignTop:
  433. case AlignBottom:
  434. // If we're going to go outside our bounds
  435. // with this tab move it down a row
  436. if( currX + tabWidth > innerRect.extent.x )
  437. {
  438. // Calculate and Advance State.
  439. balanceRow( currRow, currX );
  440. info.TabRow = ++currRow;
  441. // Reset Necessaries
  442. info.TabColumn = currColumn = currX = 0;
  443. }
  444. else
  445. {
  446. info.TabRow = currRow;
  447. info.TabColumn = currColumn++;
  448. }
  449. // Calculate Tabs Bounding Rect
  450. info.TabRect.point.x = currX;
  451. info.TabRect.point.y = (info.TabRow * tabHeight);
  452. info.TabRect.extent.x = tabWidth;
  453. info.TabRect.extent.y = tabHeight;
  454. currX += tabWidth;
  455. break;
  456. case AlignLeft:
  457. case AlignRight:
  458. // If we're going to go outside our bounds
  459. // with this tab move it down a row
  460. if( currY + tabWidth > innerRect.extent.y )
  461. {
  462. // Balance Tab Column.
  463. balanceColumn( currColumn, currY );
  464. // Calculate and Advance State.
  465. info.TabColumn = ++currColumn;
  466. info.TabRow = currRow = currY = 0;
  467. }
  468. else
  469. {
  470. info.TabColumn = currColumn;
  471. info.TabRow = currRow++;
  472. }
  473. // Calculate Tabs Bounding Rect
  474. info.TabRect.point.x = (info.TabColumn * tabHeight);
  475. info.TabRect.point.y = currY;
  476. info.TabRect.extent.x = tabHeight;
  477. info.TabRect.extent.y = tabWidth;
  478. currY += tabWidth;
  479. break;
  480. };
  481. }
  482. currRow++;
  483. currColumn++;
  484. Point2I colExtent = Point2I(currColumn * tabHeight, currRow * tabHeight);
  485. Point2I outerExtent = getOuterExtent(colExtent, NormalState, mProfile);
  486. // Calculate
  487. switch( mTabPosition )
  488. {
  489. case AlignTop:
  490. mTabRect.point.x = 0;
  491. mTabRect.point.y = 0;
  492. mTabRect.extent.x = mBounds.extent.x;
  493. mTabRect.extent.y = outerExtent.y;
  494. mPageRect.point.x = 0;
  495. mPageRect.point.y = mTabRect.extent.y;
  496. mPageRect.extent.x = mTabRect.extent.x;
  497. mPageRect.extent.y = mBounds.extent.y - mTabRect.extent.y;
  498. break;
  499. case AlignBottom:
  500. mTabRect.point.x = 0;
  501. mTabRect.point.y = mBounds.extent.y - mTabRect.extent.y;
  502. mTabRect.extent.x = mBounds.extent.x;
  503. mTabRect.extent.y = outerExtent.y;
  504. mPageRect.point.x = 0;
  505. mPageRect.point.y = 0;
  506. mPageRect.extent.x = mTabRect.extent.x;
  507. mPageRect.extent.y = mBounds.extent.y - mTabRect.extent.y;
  508. break;
  509. case AlignLeft:
  510. mTabRect.point.x = 0;
  511. mTabRect.point.y = 0;
  512. mTabRect.extent.x = outerExtent.x;
  513. mTabRect.extent.y = mBounds.extent.y;
  514. mPageRect.point.x = mTabRect.extent.x;
  515. mPageRect.point.y = 0;
  516. mPageRect.extent.x = mBounds.extent.x - mTabRect.extent.x;
  517. mPageRect.extent.y = mBounds.extent.y;
  518. break;
  519. case AlignRight:
  520. mTabRect.point.x = mBounds.extent.x - mTabRect.extent.x;
  521. mTabRect.point.y = 0;
  522. mTabRect.extent.x = outerExtent.x;
  523. mTabRect.extent.y = mBounds.extent.y;
  524. mPageRect.point.x = 0;
  525. mPageRect.point.y = 0;
  526. mPageRect.extent.x = mBounds.extent.x - mTabRect.extent.x;
  527. mPageRect.extent.y = mTabRect.extent.y;
  528. break;
  529. };
  530. }
  531. void GuiTabBookCtrl::balanceColumn( S32 column , S32 totalTabWidth )
  532. {
  533. // Short Circuit.
  534. //
  535. // If the tab size is zero, don't render tabs,
  536. // and assume it's a tab-less tab-book - JDD
  537. if( mPages.empty())
  538. return;
  539. Vector<TabHeaderInfo*> rowTemp;
  540. rowTemp.clear();
  541. for( S32 i = 0; i < mPages.size(); i++ )
  542. {
  543. TabHeaderInfo &info = mPages[i];
  544. if(info.TabColumn == column )
  545. rowTemp.push_back( &mPages[i] );
  546. }
  547. if( rowTemp.empty() )
  548. return;
  549. // Balance the tabs across the remaining space
  550. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  551. S32 spaceToDivide = innerRect.extent.y - totalTabWidth;
  552. S32 pointDelta = 0;
  553. for( S32 i = 0; i < rowTemp.size(); i++ )
  554. {
  555. TabHeaderInfo &info = *rowTemp[i];
  556. S32 extraSpace = (S32)( spaceToDivide / rowTemp.size() );
  557. info.TabRect.extent.y += extraSpace;
  558. info.TabRect.point.y += pointDelta;
  559. pointDelta += extraSpace;
  560. }
  561. }
  562. void GuiTabBookCtrl::balanceRow( S32 row, S32 totalTabWidth )
  563. {
  564. // Short Circuit.
  565. //
  566. // If the tab size is zero, don't render tabs,
  567. // and assume it's a tab-less tab-book - JDD
  568. if( mPages.empty())
  569. return;
  570. Vector<TabHeaderInfo*> rowTemp;
  571. rowTemp.clear();
  572. for( S32 i = 0; i < mPages.size(); i++ )
  573. {
  574. TabHeaderInfo &info = mPages[i];
  575. if(info.TabRow == row )
  576. rowTemp.push_back( &mPages[i] );
  577. }
  578. if( rowTemp.empty() )
  579. return;
  580. // Balance the tabs across the remaining space
  581. RectI innerRect = getInnerRect(mBounds.point, mBounds.extent, NormalState, mProfile);
  582. S32 spaceToDivide = innerRect.extent.x - totalTabWidth;
  583. S32 pointDelta = 0;
  584. for( S32 i = 0; i < rowTemp.size(); i++ )
  585. {
  586. TabHeaderInfo &info = *rowTemp[i];
  587. S32 extraSpace = (S32)spaceToDivide / ( rowTemp.size() );
  588. info.TabRect.extent.x += extraSpace;
  589. info.TabRect.point.x += pointDelta;
  590. pointDelta += extraSpace;
  591. }
  592. }
  593. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( const GuiEvent &event )
  594. {
  595. return findHitTab( event.mousePoint );
  596. }
  597. GuiTabPageCtrl *GuiTabBookCtrl::findHitTab( Point2I hitPoint )
  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())
  604. return NULL;
  605. for( S32 i = 0; i < mPages.size(); i++ )
  606. {
  607. if( mPages[i].TabRect.pointInRect( hitPoint ) )
  608. return mPages[i].Page;
  609. }
  610. return NULL;
  611. }
  612. U32 GuiTabBookCtrl::getSelectedPage()
  613. {
  614. U32 index = 0;
  615. for (U32 i = 0; i < mPages.size(); i++)
  616. {
  617. if (mActivePage == mPages[i].Page)
  618. {
  619. index = i;
  620. break;
  621. }
  622. }
  623. return index;
  624. }
  625. void GuiTabBookCtrl::selectPage( S32 index )
  626. {
  627. if( index < 0 || index >= mPages.size())
  628. return;
  629. // Select the page
  630. selectPage( mPages[ index ].Page );
  631. }
  632. void GuiTabBookCtrl::selectPage( GuiTabPageCtrl *page )
  633. {
  634. Vector<TabHeaderInfo>::iterator i = mPages.begin();
  635. for( ; i != mPages.end() ; i++ )
  636. {
  637. GuiTabPageCtrl *tab = reinterpret_cast<GuiTabPageCtrl*>((*i).Page);
  638. if( page == tab )
  639. {
  640. mActivePage = tab;
  641. tab->setVisible( true );
  642. // Notify User
  643. char *retBuffer = Con::getReturnBuffer( 512 );
  644. dStrcpy( retBuffer, tab->getText() );
  645. Con::executef( this, 2, "onTabSelected", retBuffer );
  646. }
  647. else
  648. tab->setVisible( false );
  649. }
  650. }
  651. void GuiTabBookCtrl::selectPage( const char* pageName )
  652. {
  653. Vector<TabHeaderInfo>::iterator i = mPages.begin();
  654. for( ; i != mPages.end() ; i++ )
  655. {
  656. GuiTabPageCtrl *tab = reinterpret_cast<GuiTabPageCtrl*>((*i).Page);
  657. if( dStricmp( pageName, tab->getText() ) == 0 )
  658. {
  659. mActivePage = tab;
  660. tab->setVisible( true );
  661. // Notify User
  662. char *retBuffer = Con::getReturnBuffer( 512 );
  663. dStrcpy( retBuffer, tab->getText() );
  664. Con::executef( this, 2, "onTabSelected", retBuffer );
  665. }
  666. else
  667. tab->setVisible( false );
  668. }
  669. }
  670. bool GuiTabBookCtrl::onKeyDown(const GuiEvent &event)
  671. {
  672. // Tab = Next Page
  673. // Ctrl-Tab = Previous Page
  674. if( 0 && event.keyCode == KEY_TAB )
  675. {
  676. if( event.modifier & SI_CTRL )
  677. selectPrevPage();
  678. else
  679. selectNextPage();
  680. return true;
  681. }
  682. return Parent::onKeyDown( event );
  683. }
  684. void GuiTabBookCtrl::selectNextPage()
  685. {
  686. if( mPages.empty() )
  687. return;
  688. if( mActivePage == NULL )
  689. mActivePage = mPages[0].Page;
  690. S32 nI = 0;
  691. for( ; nI < mPages.size(); nI++ )
  692. {
  693. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  694. if( tab == mActivePage )
  695. {
  696. if( nI == ( mPages.size() - 1 ) )
  697. selectPage( 0 );
  698. else if ( nI + 1 <= ( mPages.size() - 1 ) )
  699. selectPage( nI + 1 );
  700. else
  701. selectPage( 0 );
  702. // Notify User
  703. if( isMethod( "onTabSelected" ) )
  704. {
  705. char *retBuffer = Con::getReturnBuffer( 512 );
  706. dStrcpy( retBuffer, tab->getText() );
  707. Con::executef( this, 2, "onTabSelected", retBuffer );
  708. }
  709. return;
  710. }
  711. }
  712. }
  713. void GuiTabBookCtrl::selectPrevPage()
  714. {
  715. if( mPages.empty() )
  716. return;
  717. if( mActivePage == NULL )
  718. mActivePage = mPages[0].Page;
  719. S32 nI = 0;
  720. for( ; nI < mPages.size(); nI++ )
  721. {
  722. GuiTabPageCtrl *tab = mPages[ nI ].Page;
  723. if( tab == mActivePage )
  724. {
  725. if( nI == 0 )
  726. selectPage( mPages.size() - 1 );
  727. else
  728. selectPage( nI - 1 );
  729. // Notify User
  730. if( isMethod( "onTabSelected" ) )
  731. {
  732. char *retBuffer = Con::getReturnBuffer( 512 );
  733. dStrcpy( retBuffer, tab->getText() );
  734. Con::executef( this, 2, "onTabSelected", retBuffer );
  735. }
  736. return;
  737. }
  738. }
  739. }