guiTabBookCtrl.cc 25 KB

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