ALISTBOX.CPP 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #ifndef ASYSTEM_H
  5. #include "aSystem.h"
  6. #endif
  7. #ifndef AFONT_H
  8. #include "aFont.h"
  9. #endif
  10. #ifndef ALISTBOX_H
  11. #include "aListBox.h"
  12. #endif
  13. #include "soundSys.h"
  14. extern SoundSystem *sndSystem;
  15. #include "mclib.h"
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // aListBox methods
  18. ///////////////////////////////////////////////////////////////////////////////
  19. aListBox::aListBox()
  20. {
  21. itemCount = 0;
  22. itemSelected = -1;
  23. memset( items, 0, sizeof( aListItem*) * MAX_LIST_ITEMS );
  24. scrollBar = 0;
  25. skipAmount = 1;
  26. singleCheck = 1;
  27. topSkip = 0;
  28. clickSFX = LOG_SELECT;
  29. highlightSFX = LOG_DIGITALHIGHLIGHT;
  30. disabledSFX = LOG_WRONGBUTTON;
  31. }
  32. long aListBox::init(long xPos, long yPos,long w, long h )
  33. {
  34. long err;
  35. err = aObject::init(xPos,yPos,w,h);
  36. if (err)
  37. return err;
  38. //If you call the FitIni INIT and then this init afterwards, we leak one.
  39. if (scrollBar)
  40. {
  41. scrollBar->destroy();
  42. delete scrollBar;
  43. scrollBar = NULL;
  44. }
  45. scrollBar = new mcScrollBar;
  46. scrollBar->init( w - 17, 2, 15, h-4 );
  47. scrollBar->SetScrollMax( 0 );
  48. addChild( scrollBar );
  49. return (NO_ERR);
  50. }
  51. void aListBox::init( FitIniFile* file, const char* blockName )
  52. {
  53. long x, y, width, height;
  54. file->readIdLong( "XLocation", x );
  55. file->readIdLong( "YLocation", y );
  56. file->readIdLong( "Width", width );
  57. file->readIdLong( "Height", height );
  58. file->readIdLong( "HelpCaption", helpHeader );
  59. file->readIdLong( "HelpDesc", helpID );
  60. init( x, y, width, height );
  61. }
  62. void aListBox::destroy()
  63. {
  64. if (scrollBar)
  65. {
  66. scrollBar->destroy();
  67. delete scrollBar;
  68. scrollBar = NULL;
  69. }
  70. removeAllItems( true );
  71. aObject::destroy();
  72. }
  73. void aListBox::removeAllItems( bool bDelete )
  74. {
  75. if (items)
  76. {
  77. for ( int i = 0; i < itemCount; i++ )
  78. {
  79. if ( items[i] && bDelete )
  80. delete items[i];
  81. items[i] = 0;
  82. }
  83. }
  84. itemCount = 0;
  85. itemSelected = -1;
  86. if ( scrollBar )
  87. scrollBar->SetScroll( 0 );
  88. }
  89. void aListBox::update()
  90. {
  91. if ( showWindow )
  92. { /* if the listbox is invisible, then we probably don't want to update it either */
  93. if ( scrollBar )
  94. scrollBar->update();
  95. int prevCheck = GetCheckedItem();
  96. int newCheck = -1;
  97. int iSel = -1;
  98. int iHighlight = -1;
  99. long mouseX = userInput->getMouseX();
  100. long mouseY = userInput->getMouseY();
  101. bool bInside = pointInside( mouseX, mouseY );
  102. if ( bInside && helpID )
  103. {
  104. ::helpTextID = helpID;
  105. }
  106. for ( int i = 0; i < itemCount; i++ )
  107. {
  108. if ( bInside &&
  109. items[i]->pointInside(mouseX, mouseY) && items[i]->isShowing()
  110. && items[i]->getState() != aListItem::DISABLED )
  111. {
  112. if ( userInput->isLeftClick() )
  113. {
  114. iSel = i;
  115. if ( items[i]->getState() != aListItem::SELECTED )
  116. sndSystem->playDigitalSample( clickSFX );
  117. }
  118. else
  119. {
  120. iHighlight = i;
  121. }
  122. }
  123. if ( items[i]->globalBottom() > globalTop()
  124. && items[i]->globalTop() < globalBottom() )
  125. {
  126. items[i]->showGUIWindow( 1 );
  127. }
  128. else
  129. {
  130. items[i]->showGUIWindow( 0 );
  131. }
  132. }
  133. for ( i = 0; i < itemCount; i++ )
  134. {
  135. if ( iSel == i )
  136. {
  137. items[i]->setState( aListItem::SELECTED );
  138. itemSelected = i;
  139. }
  140. else if ( iHighlight == i && items[i]->getState() != aListItem::SELECTED )
  141. {
  142. if ( items[i]->getState() != aListItem::HIGHLITE )
  143. sndSystem->playDigitalSample( highlightSFX );
  144. items[i]->setState( aListItem::HIGHLITE );
  145. }
  146. else if ( (iSel != -1 || items[i]->getState() != aListItem::SELECTED)
  147. && items[i]->getState() != aListItem::DISABLED )
  148. items[i]->setState( aListItem::ENABLED );
  149. items[i]->update();
  150. }
  151. for ( i = 0; i < itemCount; i++ )
  152. {
  153. if ( items[i]->isChecked() && i != prevCheck )
  154. {
  155. newCheck = i;
  156. }
  157. }
  158. if ( singleCheck && newCheck != -1 && prevCheck != -1)
  159. {
  160. items[prevCheck]->setCheck( 0 );
  161. }
  162. }
  163. }
  164. int aListBox::handleMessage( unsigned long message, unsigned long who )
  165. {
  166. switch ( message )
  167. {
  168. case aMSG_SCROLLUP:
  169. scroll( -1 );
  170. break;
  171. case aMSG_SCROLLDOWN:
  172. scroll( 1 );
  173. break;
  174. case aMSG_PAGEUP:
  175. scroll( 10 );
  176. break;
  177. case aMSG_PAGEDOWN:
  178. scroll( -10 );
  179. break;
  180. case aMSG_SCROLLTO:
  181. {
  182. scroll( -scrollBar->GetScrollPos() );
  183. scroll( who );
  184. }
  185. break;
  186. }
  187. return 0;
  188. }
  189. void aListBox::resize(long w, long h)
  190. {
  191. aObject::resize(w, h);
  192. scrollBar->resize(scrollBar->width(), h - 4);
  193. }
  194. void aListBox::setScrollPos( int pos )
  195. {
  196. scroll( pos - scrollBar->GetScrollPos() );
  197. scrollBar->SetScroll( pos );
  198. }
  199. void aListBox::render()
  200. {
  201. if ( showWindow )
  202. {
  203. bool bTop =0;
  204. bool bBottom = 0;
  205. gos_SetRenderState( gos_State_Texture, 0 );
  206. gos_SetRenderState(gos_State_Filter, gos_FilterNone);
  207. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
  208. gos_SetRenderState( gos_State_ZCompare, 0 );
  209. gos_SetRenderState( gos_State_ZWrite, 0 );
  210. gos_DrawQuads( location, 4 );
  211. long topHeight = 0;
  212. long bottomHeight = 0;
  213. bool bItemOutOfRange = 0;
  214. for ( int i = 0; i < itemCount; i++ )
  215. {
  216. if ( items[i]->globalBottom() > globalTop()
  217. && items[i]->globalTop() < globalBottom() )
  218. {
  219. items[i]->render();
  220. if ( items[i]->globalTop() < globalTop() )
  221. {
  222. topHeight = items[i]->height();
  223. bTop = 1;
  224. bItemOutOfRange = true;
  225. }
  226. if ( items[i]->globalBottom() >= globalBottom() )
  227. {
  228. bBottom = 1;
  229. if ( !bottomHeight )
  230. bottomHeight = items[i]->height();
  231. bItemOutOfRange = true;
  232. }
  233. }
  234. else
  235. {
  236. bItemOutOfRange = true;
  237. }
  238. }
  239. // draw black box above this
  240. if ( bTop || bBottom )
  241. {
  242. GUI_RECT rect = { globalX(), globalY() - topHeight, globalX() + width(), globalY() };
  243. if ( bTop )
  244. drawRect( rect, 0xff000000 );
  245. rect.top = globalY() + height()+1;
  246. rect.bottom =globalY() + height() + bottomHeight+2;
  247. if ( bBottom )
  248. drawRect( rect, 0xff000000 );
  249. }
  250. if ( scrollBar )
  251. {
  252. if ( bItemOutOfRange )
  253. {
  254. scrollBar->Enable( 1 );
  255. }
  256. else
  257. scrollBar->Enable( 0 );
  258. scrollBar->render();
  259. }
  260. }
  261. }
  262. void aListBox::move( float offsetX, float offsetY )
  263. {
  264. aObject::move( offsetX, offsetY );
  265. for ( int i = 0; i < itemCount; i++ )
  266. {
  267. items[i]->move( offsetX, offsetY );
  268. }
  269. }
  270. long aListBox::AddItem(aListItem* add)
  271. {
  272. if (itemCount >= MAX_LIST_ITEMS)
  273. return TOO_MANY_ITEMS;
  274. float lastX = globalX() + add->globalX();
  275. float lastY = globalY() + topSkip;
  276. if ( itemCount )
  277. {
  278. lastY = items[itemCount-1]->globalY() + items[itemCount-1]->height() + skipAmount;
  279. }
  280. items[itemCount++] = add;
  281. add->moveTo( lastX, lastY );
  282. if ( add->globalRight() > globalRight() ) // can't have it hanging over the edge
  283. {
  284. float shrink = globalRight() - add->globalRight();
  285. add->resize( add->width() + shrink, add->height() );
  286. }
  287. if ( scrollBar )
  288. {
  289. int itemsTotalHeight = 0;
  290. if ( items )
  291. itemsTotalHeight = items[itemCount-1]->bottom() - items[0]->top() + skipAmount;
  292. if ( itemsTotalHeight > scrollBar->height() )
  293. scrollBar->SetScrollMax( itemsTotalHeight - scrollBar->height() );
  294. else
  295. {
  296. scrollBar->SetScroll( 0 );
  297. scrollBar->SetScrollMax( 0 );
  298. }
  299. }
  300. return itemCount - 1;
  301. }
  302. long aListBox::InsertItem(aListItem* itemString, long where)
  303. {
  304. if ( itemCount >= MAX_LIST_ITEMS )
  305. return -1;
  306. gosASSERT( itemString );
  307. if ( where >= itemCount )
  308. where = itemCount;
  309. for ( int i = itemCount - 1; i > where - 1; i-- )
  310. {
  311. items[i+1] = items[i];
  312. items[i+1]->move( 0, itemString->height() + skipAmount );
  313. }
  314. float lastX = x() + itemString->x();
  315. float lastY = y();
  316. items[where] = itemString;
  317. if ( itemCount > 0 )
  318. {
  319. if ( where - 1 > -1 )
  320. lastY = items[where-1]->bottom() + skipAmount;
  321. else
  322. lastY = items[where+1]->top() - skipAmount - itemString->height();
  323. }
  324. itemString->moveTo( lastX, lastY );
  325. itemString->showGUIWindow( true );
  326. if ( itemString->globalRight() > globalRight() ) // can't have it hanging over the edge
  327. {
  328. float shrink = globalRight() - itemString->globalRight();
  329. itemString->resize( itemString->width() + shrink, itemString->height() );
  330. }
  331. itemCount++;
  332. if ( scrollBar )
  333. {
  334. int itemsTotalHeight = 0;
  335. if ( items )
  336. itemsTotalHeight = items[itemCount-1]->bottom() - items[0]->top();
  337. if ( itemsTotalHeight > scrollBar->height() )
  338. scrollBar->SetScrollMax( itemsTotalHeight - scrollBar->height() );
  339. else
  340. scrollBar->SetScrollMax( 0 );
  341. }
  342. return where;
  343. }
  344. long aListBox::RemoveItem( aListItem* item, bool bDelete )
  345. {
  346. if ( !item )
  347. return -1;
  348. float height = item->height();
  349. bool bFound = false;
  350. for ( int i = 0; i < itemCount; i++ )
  351. {
  352. if ( items[i] == item )
  353. {
  354. bFound = true;
  355. if ( bDelete )
  356. delete items[i];
  357. if ( i < itemCount - 1 )
  358. {
  359. items[i] = items[i+1];
  360. items[i]->move( 0, -height - skipAmount );
  361. }
  362. else
  363. {
  364. items[i] = NULL;
  365. if ( itemSelected == i )
  366. itemSelected = -1;
  367. }
  368. }
  369. else if ( bFound )
  370. {
  371. if ( i < itemCount - 1 )
  372. {
  373. items[i] = items[i+1];
  374. items[i]->move( 0, -height - skipAmount );
  375. }
  376. else
  377. items[i] = NULL;
  378. }
  379. }
  380. if ( bFound )
  381. {
  382. itemCount --;
  383. if ( itemSelected != -1 )
  384. SelectItem( itemSelected );
  385. }
  386. else
  387. return -1;
  388. return 0;
  389. }
  390. long aListBox::SelectItem(long itemNumber)
  391. {
  392. if (itemNumber >= itemCount)
  393. return ITEM_OUT_OF_RANGE;
  394. for ( int i = 0; i < itemCount; i++ )
  395. {
  396. items[i]->deselect();
  397. }
  398. itemSelected = itemNumber;
  399. if ( itemSelected > -1 )
  400. {
  401. aListItem* pItem = GetItem( itemNumber );
  402. if ( pItem )
  403. {
  404. pItem->setState( aListItem::SELECTED );
  405. if ( !pItem->isShowing() || pItem->globalY() + pItem->height() > globalY() + height()
  406. || pItem->globalY() + pItem->height() < globalY() )
  407. {
  408. int newPos = (pItem->globalY() + pItem->height() - globalY()) - scrollBar->height() + scrollBar->GetScrollPos();
  409. scrollBar->SetScroll( newPos );// float itemY = pItem->globalY() + pItem->height();
  410. }
  411. }
  412. }
  413. return NO_ERR;
  414. }
  415. void aListBox::enableAllItems()
  416. {
  417. for ( int i = 0; i < itemCount; i++ )
  418. {
  419. if ( items[i]->getState() == aListItem::DISABLED )
  420. items[i]->setState( aListItem::ENABLED );
  421. }
  422. }
  423. aListItem* aListBox::GetItem(long itemNumber)
  424. {
  425. if (itemNumber >= itemCount)
  426. return NULL;
  427. return items[itemNumber];
  428. }
  429. void aListBox::scroll( int amount )
  430. {
  431. if ( !items || !itemCount )
  432. return;
  433. if ( amount < 0 && items[0]->globalY() > globalY() ) // no need to scroll
  434. return;
  435. else if ( amount > 0 && (items[itemCount-1]->globalY() + items[itemCount-1]->height()) < (globalY() + height() ) )
  436. return;
  437. for ( int i = 0; i < itemCount; i++ )
  438. {
  439. items[i]->move( 0, -amount );
  440. }
  441. }
  442. long aListBox::GetCheckedItem() const
  443. {
  444. int ret = -1;
  445. if ( items )
  446. {
  447. for ( int i = 0; i < itemCount; i++ )
  448. {
  449. if ( items[i] && items[i]->isChecked() )
  450. return i;
  451. }
  452. }
  453. return ret;
  454. }
  455. long aListBox::getScrollBarWidth()
  456. {
  457. if ( scrollBar )
  458. return scrollBar->width();
  459. return 0;
  460. }
  461. void aListBox::setOrange(bool bOrange)
  462. {
  463. scrollBar->setOrange();
  464. }
  465. bool aListBox::pointInScrollBar( long mouseX, long mouseY )
  466. {
  467. if ( scrollBar )
  468. {
  469. return scrollBar->pointInside( mouseX, mouseY );
  470. }
  471. return 0;
  472. }
  473. ///////////////////////////////////////////////////////////////////////////////
  474. // aDropList methods
  475. ///////////////////////////////////////////////////////////////////////////////
  476. aDropList::aDropList(void)
  477. : templateItem( 27333 )
  478. {
  479. listBoxMaxHeight = 0.0;
  480. selectionIndex = -1;
  481. rects = NULL;
  482. }
  483. aDropList& aDropList::operator=( const aDropList& src)
  484. {
  485. aObject::operator =( src );
  486. //Toss the CURRENT listBox
  487. listBox.destroy();
  488. //TOss the current rects. We are about to NEW over them!!
  489. if ( rects )
  490. {
  491. delete [] rects;
  492. rects = NULL;
  493. }
  494. listBox.removeAllItems( 0 );
  495. listBoxMaxHeight = src.listBoxMaxHeight;
  496. selectionIndex = -1;
  497. rectCount = src.rectCount;
  498. if ( rectCount )
  499. {
  500. rects = new aRect[rectCount];
  501. for ( int i = 0; i < rectCount; i++ )
  502. {
  503. addChild( &rects[i] );
  504. rects[i] = src.rects[i];
  505. }
  506. }
  507. addChild( &listBox );
  508. listBox.init( rects[2].globalX(), rects[2].globalY(), rects[2].width(), listBoxMaxHeight );
  509. listBox.setOrange(true);
  510. addChild( &expandButton );
  511. expandButton = src.expandButton;
  512. textLeft = src.textLeft;
  513. textTop = src.textTop;
  514. IsExpanded( 0 );
  515. return *this;
  516. }
  517. long aDropList::init( FitIniFile* file, const char* blockName )
  518. {
  519. file->seekBlock( blockName );
  520. long x, y, width, height;
  521. x = y = width = height = 0;
  522. file->readIdLong( "XLocation", x );
  523. file->readIdLong( "YLocation", y );
  524. file->readIdLong( "Width", width );
  525. file->readIdLong( "Height", height );
  526. expandButton.init(*file, "ExpandButton");
  527. file->seekBlock( "Rects" );
  528. file->readIdLong( "Rectcount", rectCount );
  529. gosASSERT( rectCount > 1 );
  530. char tmpBlockName[64];
  531. if ( rectCount )
  532. {
  533. rects = new aRect[rectCount];
  534. for ( int i = 0; i < rectCount; i++ )
  535. {
  536. sprintf( tmpBlockName, "Rect%ld", i );
  537. rects[i].init( file, tmpBlockName );
  538. }
  539. }
  540. listBoxMaxHeight = rects[2].height();
  541. //rects[2].resize(rects[2].width(), rects[1].height());
  542. listBox.init( rects[2].globalX(), rects[2].globalY(), rects[2].width(), listBoxMaxHeight );
  543. rects[2].showGUIWindow(false);
  544. listBox.showGUIWindow(false);
  545. if (width < rects[1].width()) {
  546. width = rects[1].width();
  547. }
  548. if (height < rects[1].height()) {
  549. height = rects[1].height();
  550. }
  551. aObject::init(0, 0, width, height);
  552. textLeft = 0;
  553. textTop = 0;
  554. if ( NO_ERR == file->seekBlock( "Text0" ) )
  555. {
  556. file->readIdLong( "XLocation", textLeft );
  557. file->readIdLong( "YLocation", textTop );
  558. }
  559. addChild( &rects[1] );
  560. addChild( &listBox );
  561. addChild( &rects[2] );
  562. addChild( &expandButton );
  563. addChild( &rects[0] );
  564. expandButton.setPressFX( LOG_VIDEOBUTTONS );
  565. expandButton.setHighlightFX( LOG_DIGITALHIGHLIGHT );
  566. expandButton.setDisabledFX( LOG_WRONGBUTTON );
  567. expandButton.setMessageOnRelease();
  568. move(x, y);
  569. selectionIndex = -1;
  570. templateItem.init( *file );
  571. return 0;
  572. }
  573. void aDropList::destroy()
  574. {
  575. removeAllChildren();
  576. expandButton.destroy();
  577. listBox.destroy(); // This deletes all the list items as well
  578. if ( rects )
  579. {
  580. delete [] rects;
  581. rects = NULL;
  582. }
  583. aObject::destroy();
  584. }
  585. void aDropList::specialDestroy()
  586. {
  587. //Do NOT destroy the listBox. Heidi's = operator just copies the pointer and base one is gone now.
  588. removeAllChildren();
  589. expandButton.destroy();
  590. if ( rects )
  591. {
  592. delete [] rects;
  593. rects = NULL;
  594. }
  595. aObject::destroy();
  596. }
  597. void aDropList::render()
  598. {
  599. if ( showWindow )
  600. {
  601. for ( int i = 0; i < pNumberOfChildren; i++ )
  602. {
  603. pChildren[i]->render();
  604. }
  605. if (0 <= selectionIndex)
  606. {
  607. aListItem *pListItem = ListBox().GetItem(selectionIndex);
  608. if ( pListItem )
  609. {
  610. float l, t, w, h;
  611. l = pListItem->globalLeft();
  612. t = pListItem->globalTop();
  613. w = pListItem->width();
  614. h = pListItem->height();
  615. float availableWidth = (expandButton.globalLeft() - 1) - (globalLeft() + 1);
  616. if ((0 < availableWidth) && (w > availableWidth))
  617. {
  618. pListItem->resize(availableWidth, h);
  619. }
  620. bool bShowing = pListItem->isShowing();
  621. pListItem->moveTo( globalX() + textLeft, globalY() + textTop );
  622. pListItem->showGUIWindow( true );
  623. pListItem->render();
  624. pListItem->moveTo(l, t);
  625. pListItem->showGUIWindow( bShowing );
  626. if ((0 < availableWidth) && (w > availableWidth))
  627. {
  628. pListItem->resize(w, h);
  629. }
  630. }
  631. }
  632. }
  633. }
  634. void aDropList::IsExpanded(bool isExpanded)
  635. {
  636. if (isExpanded)
  637. {
  638. listBox.showGUIWindow(true);
  639. listBox.SelectItem( -1 );
  640. rects[2].showGUIWindow(true);
  641. }
  642. else
  643. {
  644. listBox.showGUIWindow(false);
  645. rects[2].showGUIWindow(false);
  646. }
  647. }
  648. void aDropList::update()
  649. {
  650. if ( showWindow )
  651. {
  652. aObject::update();
  653. if (userInput->isLeftClick())
  654. {
  655. int cx = userInput->getMouseX();
  656. int cy = userInput->getMouseY();
  657. if (expandButton.pointInside(cx, cy)) {
  658. handleMessage(aMSG_BUTTONCLICKED, (unsigned long)(&expandButton));
  659. }
  660. // lose focus if appropriate
  661. if ( ListBox().isShowing() &&
  662. ( !pointInside( userInput->getMouseX(), userInput->getMouseY() )
  663. || ( ListBox().pointInside(userInput->getMouseX(), userInput->getMouseY() )
  664. && !ListBox().pointInScrollBar(userInput->getMouseX(), userInput->getMouseY() ) ) ) )
  665. {
  666. listBox.showGUIWindow(false);
  667. rects[2].showGUIWindow(false);
  668. }
  669. }
  670. if (0 <= ListBox().GetSelectedItem())
  671. {
  672. selectionIndex = ListBox().GetSelectedItem();
  673. if (listBox.isShowing())
  674. {
  675. IsExpanded(false);
  676. }
  677. ListBox().SelectItem(-1);
  678. }
  679. else if (listBox.isShowing())
  680. {
  681. if (userInput->getKeyDown( KEY_RETURN ) )
  682. {
  683. listBox.showGUIWindow(false);
  684. rects[2].showGUIWindow(false);
  685. }
  686. }
  687. }
  688. ListBox().setHelpID( helpID );
  689. }
  690. int aDropList::handleMessage( unsigned long message, unsigned long who )
  691. {
  692. {
  693. if ((unsigned long)(&expandButton) == who)
  694. {
  695. if (aMSG_BUTTONCLICKED == message)
  696. {
  697. if (listBox.isShowing())
  698. {
  699. IsExpanded(false);
  700. }
  701. else
  702. {
  703. IsExpanded(true);
  704. }
  705. return 1;
  706. }
  707. }
  708. }
  709. return 0;
  710. }
  711. bool aDropList::pointInside(long xPos, long yPos) const
  712. {
  713. bool retval = aObject::pointInside(xPos, yPos);
  714. if (listBox.isShowing())
  715. {
  716. retval = retval || listBox.pointInside(xPos, yPos);
  717. }
  718. return retval;
  719. }
  720. long aDropList::AddItem(aListItem* itemString)
  721. {
  722. long retval = ListBox().AddItem(itemString);
  723. float newHeight = (itemString->height() + ListBox().getSpaceBetweenItems()) * ListBox().GetItemCount()+4;
  724. if (newHeight > listBoxMaxHeight)
  725. {
  726. newHeight = listBoxMaxHeight;
  727. }
  728. ListBox().resize(ListBox().width(), newHeight);
  729. rects[2].resize(rects[2].width(), newHeight);
  730. return retval;
  731. }
  732. long aDropList::AddItem( const char* text, unsigned long color )
  733. {
  734. aAnimTextListItem* pItem = new aAnimTextListItem( 27333 );
  735. *pItem = templateItem;
  736. pItem->setText( text );
  737. pItem->setColor( color );
  738. return AddItem( pItem );
  739. }
  740. long aDropList::AddItem( unsigned long textID, unsigned long color )
  741. {
  742. aAnimTextListItem* pItem = new aAnimTextListItem( 27333 );
  743. *pItem = templateItem;
  744. pItem->setText( textID );
  745. pItem->setColor( color );
  746. return AddItem( pItem );
  747. }
  748. long aDropList::SelectItem(long item)
  749. {
  750. listBox.showGUIWindow(false);
  751. rects[2].showGUIWindow(false);
  752. selectionIndex = item;
  753. return listBox.SelectItem(item);
  754. }
  755. ///////////////////////////////////////////////////////////////////////////////
  756. // aComboBox methods
  757. ///////////////////////////////////////////////////////////////////////////////
  758. aComboBox::aComboBox(void) : templateItem( 27333 )
  759. {
  760. listBoxMaxHeight = 0.0;
  761. selectionIndex = -1;
  762. rects = NULL;
  763. }
  764. aComboBox& aComboBox::operator=(const aComboBox& src )
  765. {
  766. aObject::operator =( src );
  767. //Toss the CURRENT listBox
  768. listBox.destroy();
  769. addChild( &listBox );
  770. //TOss the current rects. We are about to NEW over them!!
  771. if ( rects )
  772. {
  773. delete [] rects;
  774. rects = NULL;
  775. }
  776. listBox = src.listBox;
  777. listBox.removeAllItems( 0 );
  778. listBoxMaxHeight = src.listBoxMaxHeight;
  779. selectionIndex = -1;
  780. rectCount = src.rectCount;
  781. if ( rectCount )
  782. {
  783. rects = new aRect[rectCount];
  784. for ( int i = 0; i < rectCount; i++ )
  785. {
  786. addChild( &rects[i] );
  787. rects[i] = src.rects[i];
  788. }
  789. }
  790. addChild( &expandButton );
  791. expandButton = src.expandButton;
  792. entry = src.entry;
  793. addChild( &entry );
  794. return *this;
  795. }
  796. long aComboBox::init( FitIniFile* file, const char* blockName )
  797. {
  798. file->seekBlock( blockName );
  799. long x, y, width, height;
  800. x = y = width = height = 0;
  801. file->readIdLong( "XLocation", x );
  802. file->readIdLong( "YLocation", y );
  803. file->readIdLong( "Width", width );
  804. file->readIdLong( "Height", height );
  805. file->seekBlock( "Rects" );
  806. file->readIdLong( "Rectcount", rectCount );
  807. gosASSERT( rectCount > 1 );
  808. {
  809. char blockName[64];
  810. if ( rectCount )
  811. {
  812. rects = new aRect[rectCount];
  813. for ( int i = 0; i < rectCount; i++ )
  814. {
  815. sprintf( blockName, "Rect%ld", i );
  816. rects[i].init( file, blockName );
  817. }
  818. }
  819. }
  820. // editbox rect = first rect
  821. // drop list outline = 2nd rect
  822. entry.init(file, "EditBox");
  823. expandButton.init(*file, "ExpandButton");
  824. expandButton.setPressFX( LOG_VIDEOBUTTONS );
  825. expandButton.setHighlightFX( LOG_DIGITALHIGHLIGHT );
  826. expandButton.setDisabledFX( LOG_WRONGBUTTON );
  827. listBoxMaxHeight = rects[2].height();
  828. //rects[2].resize(rects[2].width(), entry.height());
  829. listBox.init( rects[2].globalX(), rects[2].globalY(), rects[2].width(), listBoxMaxHeight );
  830. rects[2].showGUIWindow(false);
  831. listBox.showGUIWindow(false);
  832. if (width < entry.width()) {
  833. width = entry.width();
  834. }
  835. if (height < entry.height()) {
  836. height = entry.height();
  837. }
  838. aObject::init(0, 0, width, height);
  839. addChild( &listBox );
  840. addChild( &entry );
  841. addChild( &expandButton );
  842. for ( int i = 0; i < rectCount; i++ )
  843. addChild( &rects[i] );
  844. templateItem.init( *file );
  845. expandButton.setMessageOnRelease();
  846. move(x, y);
  847. return 0;
  848. }
  849. long aComboBox::SelectItem(long item)
  850. {
  851. listBox.showGUIWindow(false);
  852. rects[2].showGUIWindow(false);
  853. selectionIndex = item;
  854. return listBox.SelectItem(item);
  855. }
  856. void aComboBox::destroy()
  857. {
  858. removeAllChildren();
  859. entry.destroy();
  860. expandButton.destroy();
  861. listBox.destroy(); // This deletes all the list items as well
  862. aObject::destroy();
  863. delete [] rects;
  864. rects = NULL;
  865. }
  866. void aComboBox::render()
  867. {
  868. if ( showWindow )
  869. {
  870. for ( int i = 0; i < pNumberOfChildren; i++ )
  871. {
  872. pChildren[i]->render();
  873. }
  874. }
  875. }
  876. void aComboBox::update()
  877. {
  878. if ( showWindow )
  879. {
  880. aObject::update();
  881. if (userInput->isLeftClick())
  882. {
  883. int cx = userInput->getMouseX();
  884. int cy = userInput->getMouseY();
  885. if (expandButton.pointInside(cx, cy)) {
  886. handleMessage(aMSG_BUTTONCLICKED, (unsigned long)(&expandButton));
  887. }
  888. // lose focus if appropriate
  889. else if ( ListBox().isShowing() &&
  890. ( !pointInside( userInput->getMouseX(), userInput->getMouseY() )
  891. || ( ListBox().pointInside(userInput->getMouseX(), userInput->getMouseY() ) )
  892. && !ListBox().pointInScrollBar(userInput->getMouseX(), userInput->getMouseY()) ) )
  893. {
  894. listBox.showGUIWindow(false);
  895. rects[2].showGUIWindow(false);
  896. }
  897. }
  898. if (0 <= ListBox().GetSelectedItem())
  899. {
  900. selectionIndex = ListBox().GetSelectedItem();
  901. aListItem *pListItem = ListBox().GetItem(selectionIndex);
  902. aTextListItem *pTextListItem = dynamic_cast<aTextListItem *>(pListItem);
  903. if (0 != pTextListItem)
  904. {
  905. entry.setEntry(pTextListItem->getText());
  906. }
  907. if ( pListItem )
  908. {
  909. if (listBox.isShowing())
  910. {
  911. listBox.showGUIWindow(false);
  912. rects[2].showGUIWindow(false);
  913. }
  914. if ( getParent() )
  915. getParent()->handleMessage( aMSG_SELCHANGED, ID );
  916. }
  917. ListBox().SelectItem(-1);
  918. }
  919. else if (listBox.isShowing())
  920. {
  921. if (userInput->getKeyDown( KEY_RETURN ) )
  922. {
  923. listBox.showGUIWindow(false);
  924. rects[2].showGUIWindow(false);
  925. }
  926. }
  927. }
  928. ListBox().setHelpID( helpID );
  929. }
  930. int aComboBox::handleMessage( unsigned long message, unsigned long who )
  931. {
  932. {
  933. if ((unsigned long)(&expandButton) == who)
  934. {
  935. if (aMSG_BUTTONCLICKED == message)
  936. {
  937. if (listBox.isShowing())
  938. {
  939. listBox.showGUIWindow(false);
  940. rects[2].showGUIWindow(false);
  941. }
  942. else
  943. {
  944. listBox.showGUIWindow(true);
  945. rects[2].showGUIWindow(true);
  946. }
  947. return 1;
  948. }
  949. }
  950. }
  951. return 0;
  952. }
  953. bool aComboBox::pointInside(long xPos, long yPos) const
  954. {
  955. bool retval = aObject::pointInside(xPos, yPos);
  956. if (listBox.isShowing())
  957. {
  958. retval = retval || listBox.pointInside(xPos, yPos);
  959. }
  960. return retval;
  961. }
  962. long aComboBox::AddItem(aListItem* itemString)
  963. {
  964. float newHeight = (itemString->height() + (float)ListBox().getSpaceBetweenItems()) * ((float)ListBox().GetItemCount()+1)+4;
  965. if (newHeight > listBoxMaxHeight)
  966. {
  967. newHeight = listBoxMaxHeight;
  968. }
  969. ListBox().resize(ListBox().width(), newHeight);
  970. rects[2].resize(rects[2].width(), newHeight);
  971. long retval = ListBox().AddItem(itemString);
  972. return retval;
  973. }
  974. long aComboBox::AddItem( unsigned long textID, unsigned long color )
  975. {
  976. aAnimTextListItem* pItem = new aAnimTextListItem( 27333 );
  977. *pItem = templateItem;
  978. pItem->setText( textID );
  979. pItem->setColor( color );
  980. return AddItem( pItem );
  981. }
  982. long aComboBox::AddItem( const char* text, unsigned long color )
  983. {
  984. aAnimTextListItem* pItem = new aAnimTextListItem( 27333 );
  985. *pItem = templateItem;
  986. pItem->setText( text );
  987. pItem->setColor( color );
  988. return AddItem( pItem );
  989. }
  990. ///////////////////////////////////////////////////////////////////////////////
  991. // aTextListItem methods
  992. ///////////////////////////////////////////////////////////////////////////////
  993. aTextListItem::aTextListItem( const aFont& newFont )
  994. {
  995. font = newFont;
  996. unsigned long height;
  997. unsigned long width;
  998. font.getSize( width, height, "> ");
  999. aListItem::init( width, 0, Environment.screenWidth, ((float)height*1.25) );
  1000. state = ENABLED;
  1001. alignment = 0;
  1002. bForceToTop = 0;
  1003. }
  1004. aTextListItem::aTextListItem(long newFontResID)
  1005. {
  1006. font.init( newFontResID );
  1007. unsigned long height;
  1008. unsigned long width;
  1009. font.getSize( width, height, "> ");
  1010. aListItem::init( width, 0, Environment.screenWidth, ((float)height*1.25) );
  1011. state = ENABLED;
  1012. alignment = 0;
  1013. }
  1014. void aTextListItem::init(long newFontResID)
  1015. {
  1016. font.init( newFontResID );
  1017. unsigned long height;
  1018. unsigned long width;
  1019. font.getSize( width, height, "> ");
  1020. aListItem::init( width, 0, Environment.screenWidth, ((float)height*1.25) );
  1021. state = ENABLED;
  1022. alignment = 0;
  1023. }
  1024. aTextListItem::~aTextListItem()
  1025. {
  1026. }
  1027. void aTextListItem::setText( const char* newText )
  1028. {
  1029. text = newText;
  1030. }
  1031. const char* aTextListItem::getText() const
  1032. {
  1033. return text;
  1034. }
  1035. void aTextListItem::render()
  1036. {
  1037. if ( !isShowing() )
  1038. return;
  1039. float y = location[2].y - location[0].y;
  1040. float tmpHeight = font.height();
  1041. if ( y > tmpHeight && font.height( text, width() ) <= tmpHeight
  1042. && !bForceToTop )
  1043. {
  1044. y = (location[2].y + location[0].y)/2.f - tmpHeight/2.f;
  1045. }
  1046. else
  1047. y = location[0].y + tmpHeight/4.f;
  1048. font.render( text, location[0].x, y,
  1049. location[2].x - location[0].x, location[2].y - location[0].y,
  1050. location[0].argb, 0, alignment );
  1051. }
  1052. void aTextListItem::setText( long resID )
  1053. {
  1054. char tmp[4096];
  1055. tmp[0] = 0;
  1056. cLoadString( resID, tmp, 4095 );
  1057. text = tmp;
  1058. }
  1059. void aTextListItem::sizeToText()
  1060. {
  1061. // assume the width is right and we need to make the height different
  1062. unsigned long height = font.height( text, width() );
  1063. resize( width(), height );
  1064. }
  1065. void aTextListItem::init( FitIniFile& file, const char* blockName )
  1066. {
  1067. file.seekBlock( blockName );
  1068. long x, y, width, height;
  1069. file.readIdLong( "XLocation", x );
  1070. file.readIdLong( "YLocation", y );
  1071. file.readIdLong( "Width", width );
  1072. file.readIdLong( "Height", height );
  1073. aObject::init( x, y, width, height );
  1074. long color;
  1075. file.readIdLong( "Color", color );
  1076. setColor( color );
  1077. }
  1078. //////////////////////////////////////////////////////////////////
  1079. aAnimTextListItem::aAnimTextListItem( const aAnimTextListItem& src )
  1080. {
  1081. CopyData( src );
  1082. }
  1083. aAnimTextListItem& aAnimTextListItem::operator=( const aAnimTextListItem& src )
  1084. {
  1085. if ( &src != this )
  1086. {
  1087. CopyData( src );
  1088. }
  1089. return *this;
  1090. }
  1091. void aAnimTextListItem::init( FitIniFile& file, const char* blockName )
  1092. {
  1093. file.seekBlock( blockName );
  1094. long x, y, width, height;
  1095. file.readIdLong( "XLocation", x );
  1096. file.readIdLong( "YLocation", y );
  1097. file.readIdLong( "Width", width );
  1098. file.readIdLong( "Height", height );
  1099. aObject::init( x, y, width, height );
  1100. long color;
  1101. file.readIdLong( "Color", color );
  1102. setColor( color );
  1103. // I could read the font here
  1104. char animationSt[256];
  1105. if ( NO_ERR == file.readIdString( "Animation", animationSt, 255 ) )
  1106. animInfo.init( &file, animationSt );
  1107. }
  1108. void aAnimTextListItem::CopyData( const aAnimTextListItem& src )
  1109. {
  1110. animInfo = src.animInfo;
  1111. font = src.font;
  1112. text = src.text;
  1113. state = src.state;
  1114. aObject::operator=( src );
  1115. }
  1116. void aAnimTextListItem::render()
  1117. {
  1118. animInfo.setState( (aAnimGroup::STATE)state );
  1119. long color = animInfo.getCurrentColor( (aAnimGroup::STATE)state );
  1120. setColor( color );
  1121. aTextListItem::render();
  1122. }
  1123. void aAnimTextListItem::update()
  1124. {
  1125. animInfo.update();
  1126. aObject::update();
  1127. }
  1128. aLocalizedListItem::aLocalizedListItem() : aAnimTextListItem( 27663 )
  1129. {
  1130. }
  1131. long aLocalizedListItem::init( FitIniFile* file, const char* blockName )
  1132. {
  1133. file->seekBlock( blockName );
  1134. long x = 0;
  1135. long y = 0;
  1136. file->readIdLong( "XLocation", x );
  1137. file->readIdLong( "YLocation", y );
  1138. long fontResID = 0;
  1139. file->readIdLong( "Font", fontResID );
  1140. long textID = 0;
  1141. file->readIdLong( "TextID", textID );
  1142. aTextListItem::init(fontResID);
  1143. setText(textID);
  1144. long color = 0xff808080;
  1145. file->readIdLong( "Color", color );
  1146. char tmpStr[64];
  1147. strcpy(tmpStr, "");
  1148. file->readIdString( "Animation", tmpStr, 63 );
  1149. if (0 != strcmp("", tmpStr))
  1150. {
  1151. animInfo.init(file, tmpStr);
  1152. }
  1153. moveTo(x, y);
  1154. return 0;
  1155. }
  1156. void aLocalizedListItem::render()
  1157. {
  1158. long color = 0xffffffff;
  1159. if ( animInfo.getState() != getState() )
  1160. animInfo.setState( (aAnimGroup::STATE)(long)getState() );
  1161. color = animInfo.getCurrentColor( animInfo.getState() );
  1162. aTextListItem::setColor((unsigned long)color);
  1163. aTextListItem::render();
  1164. }