guiTabBookCtrl.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. mIsFrameSetGenerated = false;
  64. mTabProfile = NULL;
  65. setField("profile", "GuiDefaultProfile");
  66. setField("TabProfile", "GuiTabProfile");
  67. }
  68. void GuiTabBookCtrl::initPersistFields()
  69. {
  70. Parent::initPersistFields();
  71. addField("TabPosition", TypeEnum, Offset(mTabPosition,GuiTabBookCtrl), 1, &gTabAlignEnums );
  72. addField("MinTabWidth", TypeS32, Offset(mMinTabWidth,GuiTabBookCtrl));
  73. addField("TabProfile", TypeGuiProfile, Offset(mTabProfile, GuiTabBookCtrl));
  74. }
  75. // Empty for now, will implement for handling design time context menu for manipulating pages
  76. ConsoleMethod( GuiTabBookCtrl, addPage, void, 2, 2, "() Empty")
  77. {
  78. object->addNewPage();
  79. }
  80. //ConsoleMethod( GuiTabBookCtrl, removePage, void, 2, 2, "()")
  81. //{
  82. //}
  83. bool GuiTabBookCtrl::onAdd()
  84. {
  85. Parent::onAdd();
  86. return true;
  87. }
  88. void GuiTabBookCtrl::onRemove()
  89. {
  90. Parent::onRemove();
  91. }
  92. void GuiTabBookCtrl::onChildRemoved( GuiControl* child )
  93. {
  94. for (S32 i = 0; i < mPages.size(); i++ )
  95. {
  96. GuiTabPageCtrl* tab = mPages[i].Page;
  97. if( tab == child )
  98. {
  99. if( tab == mActivePage )
  100. mActivePage = NULL;
  101. mPages.erase( i );
  102. break;
  103. }
  104. }
  105. if( mPages.empty() )
  106. mActivePage = NULL;
  107. else if (mActivePage == NULL )
  108. mActivePage = static_cast<GuiTabPageCtrl*>(mPages[0].Page);
  109. }
  110. void GuiTabBookCtrl::onChildAdded( GuiControl *child )
  111. {
  112. GuiTabPageCtrl *page = dynamic_cast<GuiTabPageCtrl*>(child);
  113. if( !page )
  114. {
  115. Con::warnf("GuiTabBookCtrl::onChildAdded - attempting to add NON GuiTabPageCtrl as child page");
  116. SimObject *simObj = reinterpret_cast<SimObject*>(child);
  117. removeObject( simObj );
  118. if( mActivePage )
  119. {
  120. mActivePage->addObject( simObj );
  121. }
  122. else
  123. {
  124. Con::warnf("GuiTabBookCtrl::onChildAdded - unable to find active page to reassign ownership of new child control to, placing on parent");
  125. GuiControl *rent = getParent();
  126. if( rent )
  127. rent->addObject( simObj );
  128. }
  129. return;
  130. }
  131. TabHeaderInfo newPage;
  132. newPage.Page = page;
  133. newPage.TabRow = -1;
  134. newPage.TabColumn = -1;
  135. mPages.push_back( newPage );
  136. // Calculate Page Information
  137. calculatePageTabs();
  138. child->resize( Point2I(0, 0), mPageRect.extent );
  139. }
  140. bool GuiTabBookCtrl::onWake()
  141. {
  142. if (! Parent::onWake())
  143. return false;
  144. mHasTexture = mProfile->constructBitmapArray();
  145. if( mHasTexture )
  146. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  147. //increment the tab profile
  148. if (mTabProfile != NULL)
  149. mTabProfile->incRefCount();
  150. return true;
  151. }
  152. void GuiTabBookCtrl::onSleep()
  153. {
  154. Parent::onSleep();
  155. //decrement the tab profile referrence
  156. if (mTabProfile != NULL)
  157. mTabProfile->decRefCount();
  158. }
  159. void GuiTabBookCtrl::setControlTabProfile(GuiControlProfile* prof)
  160. {
  161. AssertFatal(prof, "GuiTabBookCtrl::setControlTabProfile: invalid tab profile");
  162. if (prof == mTabProfile)
  163. return;
  164. if (mAwake)
  165. mTabProfile->decRefCount();
  166. mTabProfile = prof;
  167. if (mAwake)
  168. mTabProfile->incRefCount();
  169. calculatePageTabs();
  170. }
  171. void GuiTabBookCtrl::addNewPage()
  172. {
  173. char textbuf[1024];
  174. GuiTabPageCtrl * page = new GuiTabPageCtrl();
  175. page->setField("profile", "GuiTabPageProfile");
  176. dSprintf(textbuf, sizeof(textbuf), "TabBookPage%d_%d", getId(), page->getId());
  177. page->registerObject(textbuf);
  178. this->addObject( page );
  179. }
  180. void GuiTabBookCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  181. {
  182. Parent::resize( newPosition, newExtent );
  183. calculatePageTabs();
  184. // Resize Children
  185. SimSet::iterator i;
  186. for(i = begin(); i != end(); i++)
  187. {
  188. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  189. ctrl->resize( Point2I(0, 0), mPageRect.extent );
  190. }
  191. }
  192. void GuiTabBookCtrl::childResized(GuiControl *child)
  193. {
  194. if(mPageRect.extent != child->mBounds.extent || child->mBounds.point != Point2I::Zero)
  195. {
  196. child->resize( Point2I(0,0), mPageRect.extent );
  197. }
  198. }
  199. Point2I GuiTabBookCtrl::getTabLocalCoord(const Point2I &src)
  200. {
  201. //Get the border profiles
  202. GuiBorderProfile *leftProfile = mProfile->getLeftBorder();
  203. GuiBorderProfile *topProfile = mProfile->getTopBorder();
  204. S32 leftSize = (leftProfile) ? leftProfile->getMargin(NormalState) + leftProfile->getBorder(NormalState) + leftProfile->getPadding(NormalState) : 0;
  205. S32 topSize = (topProfile) ? topProfile->getMargin(NormalState) + topProfile->getBorder(NormalState) + topProfile->getPadding(NormalState) : 0;
  206. Point2I ret = Point2I(src.x - leftSize, src.y - topSize);
  207. ret.x -= mTabRect.point.x;
  208. ret.y -= mTabRect.point.y;
  209. return ret;
  210. }
  211. void GuiTabBookCtrl::onTouchDown(const GuiEvent &event)
  212. {
  213. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  214. if( mTabRect.pointInRect( localMouse ) )
  215. {
  216. mTabDownPosition = event.mousePoint;
  217. mDepressed = true;
  218. mouseLock();
  219. Point2I tabLocalMouse = getTabLocalCoord(localMouse);
  220. GuiTabPageCtrl *tab = findHitTab(tabLocalMouse);
  221. if( tab != NULL && tab->isActive() )
  222. selectPage( tab );
  223. }
  224. else
  225. {
  226. Parent::onTouchDown(event);
  227. }
  228. }
  229. void GuiTabBookCtrl::onTouchMove(const GuiEvent &event)
  230. {
  231. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  232. if( mTabRect.pointInRect( localMouse ) )
  233. {
  234. Point2I tabLocalMouse = getTabLocalCoord(localMouse);
  235. GuiTabPageCtrl *tab = findHitTab(tabLocalMouse);
  236. if( tab != NULL && mHoverTab != tab )
  237. mHoverTab = tab;
  238. else if ( !tab )
  239. mHoverTab = NULL;
  240. }
  241. else
  242. {
  243. mHoverTab = NULL;
  244. }
  245. Parent::onTouchMove( event );
  246. }
  247. void GuiTabBookCtrl::onTouchLeave( const GuiEvent &event )
  248. {
  249. mHoverTab = NULL;
  250. Parent::onTouchLeave(event);
  251. }
  252. void GuiTabBookCtrl::onTouchDragged(const GuiEvent& event)
  253. {
  254. if (mDepressed && mActivePage && mActivePage->size() > 0)
  255. {
  256. Point2I deltaMousePosition = event.mousePoint - mTabDownPosition;
  257. const S32 dragDist = 20;
  258. if (mAbs(deltaMousePosition.x) > dragDist || mAbs(deltaMousePosition.y) > dragDist)
  259. {
  260. //That's cool, but to transform the tab into window, we need a parent FrameSet and a grandchild that's a docked window.
  261. GuiFrameSetCtrl* frameSet = dynamic_cast<GuiFrameSetCtrl*>(getParent());
  262. GuiWindowCtrl* window = dynamic_cast<GuiWindowCtrl*>((*mActivePage)[0]);
  263. if (frameSet && window && window->mPageDocked)
  264. {
  265. //We have a winner!!!
  266. mDepressed = false;
  267. mouseUnlock();
  268. frameSet->undockWindowFromBook(window, this, mActivePage);
  269. return;
  270. }
  271. }
  272. }
  273. Parent::onTouchDragged(event);
  274. }
  275. void GuiTabBookCtrl::onTouchUp(const GuiEvent& event)
  276. {
  277. if (mDepressed)
  278. {
  279. mouseUnlock();
  280. }
  281. Parent::onTouchUp(event);
  282. }
  283. bool GuiTabBookCtrl::onMouseDownEditor(const GuiEvent &event, const Point2I& offset)
  284. {
  285. bool handled = false;
  286. Point2I localMouse = globalToLocalCoord( event.mousePoint );
  287. if( mTabRect.pointInRect( localMouse ) )
  288. {
  289. GuiTabPageCtrl *tab = findHitTab( localMouse );
  290. if( tab != NULL )
  291. {
  292. selectPage( tab );
  293. handled = true;
  294. }
  295. }
  296. // This shouldn't be called if it's not design time, but check just incase
  297. if ( GuiControl::smDesignTime )
  298. {
  299. // If we clicked in the editor and our addset is the tab book
  300. // ctrl, select the child ctrl so we can edit it's properties
  301. GuiEditCtrl* edit = GuiControl::smEditorHandle;
  302. if( edit && ( edit->getAddSet() == this ) && mActivePage != NULL )
  303. edit->select( mActivePage );
  304. }
  305. if (!handled)
  306. {
  307. return Parent::onMouseDownEditor(event, offset);
  308. }
  309. return true;
  310. }
  311. void GuiTabBookCtrl::onPreRender()
  312. {
  313. // sometimes we need to resize because of a changed persistent field
  314. // that's what this does
  315. solveDirty();
  316. }
  317. void GuiTabBookCtrl::onRender(Point2I offset, const RectI &updateRect)
  318. {
  319. Point2I totalOffset = offset + mTabRect.point;
  320. RectI ctrlRect = applyMargins(totalOffset, mTabRect.extent, NormalState, mProfile);
  321. if (!ctrlRect.isValidRect())
  322. {
  323. return;
  324. }
  325. renderUniversalRect(ctrlRect, mProfile, NormalState);
  326. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  327. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  328. if (contentRect.isValidRect())
  329. {
  330. renderTabs(contentRect.point);
  331. }
  332. if(mPageRect.isValidRect())
  333. {
  334. // Render Children
  335. renderChildControls(offset, RectI(offset + mPageRect.point, mPageRect.extent), updateRect);
  336. }
  337. }
  338. void GuiTabBookCtrl::renderTabs( const Point2I &offset )
  339. {
  340. // If the tab size is zero, don't render tabs,
  341. // and assume it's a tab-less tab-book - JDD
  342. if( mPages.empty())
  343. return;
  344. for( S32 i = 0; i < mPages.size(); i++ )
  345. {
  346. RectI tabBounds = mPages[i].TabRect;
  347. tabBounds.point += offset;
  348. GuiTabPageCtrl *tab = mPages[i].Page;
  349. if( tab != NULL )
  350. renderTab( tabBounds, tab );
  351. }
  352. }
  353. void GuiTabBookCtrl::renderTab( RectI tabRect, GuiTabPageCtrl *tab )
  354. {
  355. StringTableEntry text = tab->getText();
  356. GuiControlState currentState = GuiControlState::NormalState;
  357. if (mActivePage == tab)
  358. {
  359. currentState = SelectedState;
  360. }
  361. else if (mHoverTab == tab)
  362. {
  363. currentState = HighlightState;
  364. }
  365. RectI ctrlRect = applyMargins(tabRect.point, tabRect.extent, currentState, mTabProfile);
  366. if (!ctrlRect.isValidRect())
  367. {
  368. return;
  369. }
  370. renderUniversalRect(ctrlRect, mTabProfile, currentState);
  371. //Render Text
  372. dglSetBitmapModulation(getFontColor(mTabProfile, currentState));
  373. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mTabProfile);
  374. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mTabProfile);
  375. TextRotationOptions rot = tRotateNone;
  376. if (mTabPosition == AlignLeft)
  377. {
  378. rot = tRotateLeft;
  379. }
  380. else if(mTabPosition == AlignRight)
  381. {
  382. rot = tRotateRight;
  383. }
  384. renderText(contentRect.point, contentRect.extent, text, mTabProfile, rot);
  385. }
  386. // This is nothing but a clever hack to allow the tab page children
  387. // to cast this to a GuiControl* so that the file doesn't need to have circular
  388. // includes. generic method overriding for the win!
  389. void GuiTabBookCtrl::setUpdate()
  390. {
  391. Parent::setUpdate();
  392. setUpdateRegion(Point2I(0,0), mBounds.extent);
  393. calculatePageTabs();
  394. }
  395. void GuiTabBookCtrl::solveDirty()
  396. {
  397. bool dirty = false;
  398. GFont* font = mTabProfile->getFont(mFontSizeAdjust);
  399. if( mTabPosition != mLastTabPosition )
  400. {
  401. mLastTabPosition = mTabPosition;
  402. dirty = true;
  403. }
  404. else if( mTabProfile != NULL && font != NULL && font->getHeight() != mFontHeight )
  405. {
  406. dirty = true;
  407. }
  408. else if(mPages.size() > 0 && mTabProfile != NULL && font != NULL)
  409. {
  410. S32 tabWidth = calculatePageTabWidth(mPages[0].Page);
  411. tabWidth = getMax(tabWidth, mMinTabWidth);
  412. if(mTabWidth != tabWidth)
  413. {
  414. dirty = true;
  415. }
  416. }
  417. if( dirty )
  418. {
  419. resize( mBounds.point, mBounds.extent );
  420. }
  421. }
  422. S32 GuiTabBookCtrl::calculatePageTabWidth( GuiTabPageCtrl *page )
  423. {
  424. if( !page )
  425. return mTabWidth;
  426. StringTableEntry text = page->getText();
  427. if( !text || dStrlen(text) == 0 || !mTabProfile )
  428. return mTabWidth;
  429. S32 textLength = mTabProfile->getFont(mFontSizeAdjust)->getStrNWidth(text, dStrlen(text));
  430. Point2I innerExtent = Point2I(textLength, textLength);
  431. Point2I outerExtent = getOuterExtent(innerExtent, NormalState, mTabProfile);
  432. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  433. {
  434. return outerExtent.x;
  435. }
  436. else
  437. {
  438. return outerExtent.y;
  439. }
  440. }
  441. void GuiTabBookCtrl::calculatePageTabs()
  442. {
  443. // Short Circuit.
  444. //
  445. // If the tab size is zero, don't render tabs,
  446. // and assume it's a tab-less tab-book - JDD
  447. if( mPages.empty())
  448. return;
  449. S32 currRow = 0;
  450. S32 currColumn = 0;
  451. S32 currX = 0;
  452. S32 currY = 0;
  453. S32 tabHeight = 0;
  454. RectI innerRect = getInnerRect();
  455. mFontHeight = mTabProfile->getFont(mFontSizeAdjust)->getHeight();
  456. Point2I innerExtent = Point2I(mFontHeight, mFontHeight);
  457. Point2I fontBasedBounds = getOuterExtent(innerExtent, NormalState, mTabProfile);
  458. if (mTabPosition == AlignTop || mTabPosition == AlignBottom)
  459. {
  460. tabHeight = fontBasedBounds.y;
  461. }
  462. else
  463. {
  464. tabHeight = fontBasedBounds.x;
  465. }
  466. for( S32 i = 0; i < mPages.size(); i++ )
  467. {
  468. // Fetch Tab Width
  469. S32 tabWidth = calculatePageTabWidth( mPages[i].Page );
  470. tabWidth = getMax( tabWidth, mMinTabWidth );
  471. if (i == 0)
  472. {
  473. mTabWidth = tabWidth;
  474. }
  475. TabHeaderInfo &info = mPages[i];
  476. switch( mTabPosition )
  477. {
  478. case AlignTop:
  479. case AlignBottom:
  480. // If we're going to go outside our bounds
  481. // with this tab move it down a row
  482. if( currX + tabWidth > innerRect.extent.x )
  483. {
  484. // Calculate and Advance State.
  485. balanceRow( currRow, currX );
  486. info.TabRow = ++currRow;
  487. // Reset Necessaries
  488. info.TabColumn = currColumn = currX = 0;
  489. }
  490. else
  491. {
  492. info.TabRow = currRow;
  493. info.TabColumn = currColumn++;
  494. }
  495. // Calculate Tabs Bounding Rect
  496. info.TabRect.point.x = currX;
  497. info.TabRect.point.y = (info.TabRow * tabHeight);
  498. info.TabRect.extent.x = tabWidth;
  499. info.TabRect.extent.y = tabHeight;
  500. currX += tabWidth;
  501. break;
  502. case AlignLeft:
  503. case AlignRight:
  504. // If we're going to go outside our bounds
  505. // with this tab move it down a row
  506. if( currY + tabWidth > innerRect.extent.y )
  507. {
  508. // Balance Tab Column.
  509. balanceColumn( currColumn, currY );
  510. // Calculate and Advance State.
  511. info.TabColumn = ++currColumn;
  512. info.TabRow = currRow = currY = 0;
  513. }
  514. else
  515. {
  516. info.TabColumn = currColumn;
  517. info.TabRow = currRow++;
  518. }
  519. // Calculate Tabs Bounding Rect
  520. info.TabRect.point.x = (info.TabColumn * tabHeight);
  521. info.TabRect.point.y = currY;
  522. info.TabRect.extent.x = tabHeight;
  523. info.TabRect.extent.y = tabWidth;
  524. currY += tabWidth;
  525. break;
  526. };
  527. }
  528. currRow++;
  529. currColumn++;
  530. Point2I colExtent = Point2I(currColumn * tabHeight, currRow * tabHeight);
  531. Point2I outerExtent = getOuterExtent(colExtent, 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();
  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();
  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. }