button.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // ImageButtonFacePane
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class ImageButtonFacePane : public ButtonFacePane {
  8. private:
  9. TRef<Surface> m_psurface;
  10. DWORD m_dwFaces;
  11. int m_countFaces;
  12. int m_xmin;
  13. int m_xmax;
  14. int m_xsize;
  15. int m_ysize;
  16. bool m_bFocus;
  17. bool m_bEnabled;
  18. bool m_bChecked;
  19. bool m_bChecked2;
  20. bool m_bDown;
  21. bool m_bInside;
  22. public:
  23. ImageButtonFacePane(Surface* psurface, DWORD dwFaces, int xmin, int xmax) :
  24. m_psurface(psurface),
  25. m_dwFaces(dwFaces),
  26. m_bFocus(false),
  27. m_bEnabled(true),
  28. m_bChecked(false),
  29. m_bChecked2(false),
  30. m_bDown(false),
  31. m_bInside(false),
  32. m_xmin(xmin),
  33. m_xmax(xmax)
  34. {
  35. m_countFaces = CountBits(m_dwFaces);
  36. const WinPoint& size = m_psurface->GetSize();
  37. if (m_xmax == 0) {
  38. m_xmin = 0;
  39. m_xmax = size.X();
  40. }
  41. m_ysize = size.Y() / m_countFaces;
  42. InternalSetSize(WinPoint(m_xmax - m_xmin, m_ysize));
  43. //
  44. // Make sure the bitmap is an integer multiple on m_countFaces
  45. //
  46. ZAssert(m_countFaces * m_ysize == size.Y());
  47. }
  48. void SetFocus(bool bFocus)
  49. {
  50. if (m_bFocus != bFocus) {
  51. m_bFocus = bFocus;
  52. NeedPaint();
  53. }
  54. }
  55. void SetEnabled(bool bEnabled)
  56. {
  57. if (m_bEnabled != bEnabled) {
  58. m_bEnabled = bEnabled;
  59. NeedPaint();
  60. }
  61. }
  62. void SetChecked(bool bChecked)
  63. {
  64. if (m_bChecked != bChecked) {
  65. m_bChecked = bChecked;
  66. NeedPaint();
  67. }
  68. }
  69. void SetChecked2(bool bChecked)
  70. {
  71. if (m_bChecked2 != bChecked) {
  72. m_bChecked2 = bChecked;
  73. NeedPaint();
  74. }
  75. }
  76. void SetInside(bool bInside)
  77. {
  78. if (m_bInside != bInside) {
  79. m_bInside = bInside;
  80. NeedPaint();
  81. }
  82. }
  83. void SetDown(bool bDown)
  84. {
  85. if (m_bDown != bDown) {
  86. m_bDown = bDown;
  87. NeedPaint();
  88. }
  89. }
  90. int GetFaceIndex(DWORD face)
  91. {
  92. int index = 0;
  93. DWORD faces = m_dwFaces;
  94. while ((face & 1) == 0) {
  95. if (faces & 1) {
  96. index++;
  97. }
  98. faces = faces >> 1;
  99. face = face >> 1;
  100. }
  101. if ((faces & 1) == 0) {
  102. return -1;
  103. }
  104. return index;
  105. }
  106. void BltFace(Surface* psurface, DWORD face)
  107. {
  108. int index = GetFaceIndex(face);
  109. if (index != -1) {
  110. psurface->BitBlt(
  111. WinPoint(0, 0),
  112. m_psurface,
  113. WinRect(
  114. m_xmin,
  115. index * m_ysize,
  116. m_xmax,
  117. (index + 1) * m_ysize
  118. )
  119. );
  120. }
  121. }
  122. DWORD ChooseFace(DWORD dw, DWORD dwFallBack)
  123. {
  124. if (m_dwFaces & dw) {
  125. return dw;
  126. } else {
  127. return dwFallBack;
  128. }
  129. }
  130. void Paint(Surface* psurface)
  131. {
  132. //
  133. // Figure out which face to draw
  134. //
  135. DWORD face = 0;
  136. if (m_bEnabled) {
  137. if (m_bDown) {
  138. if (m_bChecked2) {
  139. face = ChooseFace(ButtonFaceChecked2Down, ButtonFaceUp);
  140. } else if (m_bChecked) {
  141. face = ChooseFace(ButtonFaceCheckedDown, ButtonFaceUp);
  142. } else {
  143. face = ChooseFace(ButtonFaceDown, ButtonFaceUp);
  144. }
  145. } else if (m_bInside) {
  146. if (m_bChecked2) {
  147. face =
  148. ChooseFace(
  149. ButtonFaceChecked2Inside,
  150. ChooseFace(
  151. ButtonFaceInside,
  152. ChooseFace(
  153. ButtonFaceChecked2Up,
  154. ButtonFaceUp
  155. )
  156. )
  157. );
  158. } else if (m_bChecked) {
  159. face =
  160. ChooseFace(
  161. ButtonFaceCheckedInside,
  162. ChooseFace(
  163. ButtonFaceInside,
  164. ChooseFace(
  165. ButtonFaceCheckedUp,
  166. ButtonFaceUp
  167. )
  168. )
  169. );
  170. } else {
  171. face = ChooseFace(ButtonFaceInside, ButtonFaceUp);
  172. }
  173. } else {
  174. if (m_bChecked2) {
  175. face = ChooseFace(ButtonFaceChecked2Up, ButtonFaceDown);
  176. } else if (m_bChecked) {
  177. face = ChooseFace(ButtonFaceCheckedUp, ButtonFaceDown);
  178. } else {
  179. face = ButtonFaceUp;
  180. }
  181. }
  182. } else {
  183. if (m_bChecked2) {
  184. face = ChooseFace(ButtonFaceChecked2Disabled, ButtonFaceDisabled);
  185. } else if (m_bChecked) {
  186. face = ChooseFace(ButtonFaceCheckedDisabled, ButtonFaceDisabled);
  187. } else {
  188. face = ButtonFaceDisabled;
  189. }
  190. }
  191. //
  192. // Draw the face
  193. //
  194. BltFace(psurface, face);
  195. //
  196. // Overlay the focus bitmap on top of the button
  197. //
  198. if (m_bFocus && (m_dwFaces & ButtonFaceFocus)) {
  199. BltFace(psurface, ButtonFaceFocus);
  200. }
  201. }
  202. };
  203. TRef<ButtonFacePane> CreateButtonFacePane(Surface* psurface, DWORD dwFaces, int xmin, int xmax)
  204. {
  205. return new ImageButtonFacePane(psurface, dwFaces, xmin, xmax);
  206. }
  207. //////////////////////////////////////////////////////////////////////////////
  208. //
  209. //
  210. //
  211. //////////////////////////////////////////////////////////////////////////////
  212. class PaneButtonFacePane : public ButtonFacePane {
  213. private:
  214. TRef<Pane> m_ppaneUp;
  215. TRef<Pane> m_ppaneDown;
  216. bool m_bDown;
  217. bool m_bChecked;
  218. public:
  219. PaneButtonFacePane(Pane* ppaneUp, Pane* ppaneDown) :
  220. m_ppaneUp(ppaneUp),
  221. m_ppaneDown(ppaneDown),
  222. m_bDown(false),
  223. m_bChecked(false)
  224. {
  225. InsertAtBottom(m_ppaneUp);
  226. if (m_ppaneDown) {
  227. InsertAtBottom(m_ppaneDown);
  228. m_ppaneDown->SetHidden(true);
  229. }
  230. }
  231. void SetDown(bool bDown)
  232. {
  233. if (bDown != m_bDown) {
  234. m_bDown = bDown;
  235. NeedLayout();
  236. NeedPaint();
  237. }
  238. }
  239. virtual void SetChecked(bool bChecked)
  240. {
  241. if (bChecked != m_bChecked) {
  242. m_bChecked = bChecked;
  243. NeedLayout();
  244. NeedPaint();
  245. }
  246. }
  247. void UpdateLayout()
  248. {
  249. bool bFaceDown;
  250. if (m_bChecked) {
  251. bFaceDown = !m_bDown;
  252. } else {
  253. bFaceDown = m_bDown;
  254. }
  255. if (bFaceDown && m_ppaneDown) {
  256. InternalSetHidden(m_ppaneUp, true);
  257. InternalSetHidden(m_ppaneDown, false);
  258. InternalSetExpand(m_ppaneDown, GetExpand());
  259. m_ppaneDown->UpdateLayout();
  260. InternalSetOffset(m_ppaneDown, WinPoint(0, 0));
  261. InternalSetSize(m_ppaneDown->GetSize());
  262. } else {
  263. InternalSetHidden(m_ppaneUp, false);
  264. if (m_ppaneDown) {
  265. InternalSetHidden(m_ppaneDown, true);
  266. }
  267. InternalSetExpand(m_ppaneUp, GetExpand());
  268. m_ppaneUp->UpdateLayout();
  269. InternalSetOffset(m_ppaneUp, WinPoint(0, 0));
  270. InternalSetSize(m_ppaneUp->GetSize());
  271. }
  272. }
  273. };
  274. TRef<ButtonFacePane> CreateButtonFacePane(Pane* ppaneUp, Pane* ppaneDown)
  275. {
  276. return new PaneButtonFacePane(ppaneUp, ppaneDown);
  277. }
  278. //////////////////////////////////////////////////////////////////////////////
  279. //
  280. // ButtonUIPane
  281. //
  282. //////////////////////////////////////////////////////////////////////////////
  283. class ButtonUIPane :
  284. public ButtonPane,
  285. public IEventSink
  286. {
  287. TRef<ButtonFacePane> m_pfacePane;
  288. TRef<EventSourceImpl> m_peventSource;
  289. TRef<EventSourceImpl> m_peventSourceDoubleClick;
  290. TRef<EventSourceImpl> m_peventMouseEnterSource;
  291. TRef<EventSourceImpl> m_peventMouseLeaveSource;
  292. TRef<EventSourceImpl> m_peventMouseEnterWhileEnabledSource;
  293. TRef<IInputProvider> m_pprovider;
  294. TRef<IEventSink> m_peventSinkDelegate;
  295. bool m_bToggle;
  296. bool m_bFocus;
  297. bool m_bEnabled;
  298. bool m_bChecked;
  299. bool m_bChecked2;
  300. bool m_bDown;
  301. bool m_bInside;
  302. bool m_bFirstEvent;
  303. bool m_bDownTrigger;
  304. float m_repeatDelay;
  305. float m_repeatRate;
  306. public:
  307. ButtonUIPane(
  308. ButtonFacePane* ppane,
  309. float repeatRate,
  310. float repeatDelay,
  311. bool bToggle
  312. ) :
  313. m_pfacePane(ppane),
  314. m_peventSource(new EventSourceImpl()),
  315. m_peventSourceDoubleClick(new EventSourceImpl()),
  316. m_peventMouseEnterSource(new EventSourceImpl()),
  317. m_peventMouseLeaveSource(new EventSourceImpl()),
  318. m_peventMouseEnterWhileEnabledSource(new EventSourceImpl()),
  319. m_repeatDelay(repeatDelay),
  320. m_repeatRate(repeatRate),
  321. m_bToggle(bToggle),
  322. m_bFocus(false),
  323. m_bEnabled(true),
  324. m_bChecked(false),
  325. m_bChecked2(false),
  326. m_bDown(false),
  327. m_bInside(false),
  328. m_bDownTrigger(false)
  329. {
  330. InsertAtBottom(m_pfacePane);
  331. m_peventSinkDelegate = IEventSink::CreateDelegate(this);
  332. }
  333. ~ButtonUIPane()
  334. {
  335. if (m_pprovider)
  336. {
  337. m_pprovider->GetTimer()->RemoveSink(m_peventSinkDelegate);
  338. m_pprovider = NULL;
  339. }
  340. }
  341. void SetDown(bool bDown)
  342. {
  343. if (m_bDown != bDown) {
  344. m_bDown = bDown;
  345. m_pfacePane->SetDown(m_bDown);
  346. }
  347. }
  348. void SetInside(bool bInside)
  349. {
  350. if (m_bInside != bInside) {
  351. m_bInside = bInside;
  352. m_pfacePane->SetInside(m_bInside);
  353. }
  354. }
  355. void SetFocus(bool bFocus)
  356. {
  357. if (m_bFocus != bFocus) {
  358. m_bFocus = bFocus;
  359. m_pfacePane->SetFocus(m_bFocus);
  360. }
  361. }
  362. //////////////////////////////////////////////////////////////////////////////
  363. //
  364. // ButtonPane Methods
  365. //
  366. //////////////////////////////////////////////////////////////////////////////
  367. void SetDownTrigger(bool bDownTrigger)
  368. {
  369. m_bDownTrigger = bDownTrigger;
  370. }
  371. void SetEnabled(bool bEnabled)
  372. {
  373. if (m_bEnabled != bEnabled) {
  374. m_bEnabled = bEnabled;
  375. m_pfacePane->SetEnabled(m_bEnabled);
  376. if (bEnabled && m_bInside)
  377. m_peventMouseEnterWhileEnabledSource->Trigger();
  378. }
  379. }
  380. void SetChecked(bool bChecked)
  381. {
  382. if (m_bChecked != bChecked) {
  383. m_bChecked = bChecked;
  384. m_pfacePane->SetChecked(m_bChecked);
  385. }
  386. }
  387. void SetChecked2(bool bChecked)
  388. {
  389. if (m_bChecked2 != bChecked) {
  390. m_bChecked2 = bChecked;
  391. m_pfacePane->SetChecked2(m_bChecked2);
  392. }
  393. }
  394. void SetRepeat(float repeatRate, float repeatDelay)
  395. {
  396. if (repeatRate == 0 && m_pprovider != NULL) {
  397. m_pprovider->GetTimer()->RemoveSink(m_peventSinkDelegate);
  398. m_pprovider = NULL;
  399. }
  400. m_repeatRate = repeatRate;
  401. m_repeatDelay = repeatDelay;
  402. }
  403. bool GetChecked()
  404. {
  405. return m_bChecked;
  406. }
  407. bool GetChecked2()
  408. {
  409. return m_bChecked2;
  410. }
  411. bool GetEnabled()
  412. {
  413. return m_bEnabled;
  414. }
  415. IEventSource* GetEventSource()
  416. {
  417. return m_peventSource;
  418. }
  419. IEventSource* GetDoubleClickEventSource()
  420. {
  421. return m_peventSourceDoubleClick;
  422. }
  423. IEventSource* GetMouseEnterEventSource()
  424. {
  425. return m_peventMouseEnterSource;
  426. }
  427. IEventSource* GetMouseLeaveEventSource()
  428. {
  429. return m_peventMouseLeaveSource;
  430. }
  431. IEventSource* GetMouseEnterWhileEnabledEventSource()
  432. {
  433. return m_peventMouseEnterWhileEnabledSource;
  434. }
  435. //////////////////////////////////////////////////////////////////////////////
  436. //
  437. // Pane Methods
  438. //
  439. //////////////////////////////////////////////////////////////////////////////
  440. void UpdateLayout()
  441. {
  442. DefaultUpdateLayout();
  443. }
  444. //////////////////////////////////////////////////////////////////////////////
  445. //
  446. // IMouseInput Methods
  447. //
  448. //////////////////////////////////////////////////////////////////////////////
  449. void RemoveCapture()
  450. {
  451. if (m_pprovider) {
  452. m_pprovider->GetTimer()->RemoveSink(m_peventSinkDelegate);
  453. m_pprovider = NULL;
  454. }
  455. }
  456. void MouseEnter(IInputProvider* pprovider, const Point& point)
  457. {
  458. SetInside(true);
  459. m_peventMouseEnterSource->Trigger();
  460. if (m_bEnabled)
  461. m_peventMouseEnterWhileEnabledSource->Trigger();
  462. }
  463. void MouseLeave(IInputProvider* pprovider)
  464. {
  465. SetInside(false);
  466. m_peventMouseLeaveSource->Trigger();
  467. }
  468. void MouseMove(IInputProvider* pprovider, const Point& point, bool bCaptured, bool bInside)
  469. {
  470. SetInside(bInside);
  471. if (bCaptured) {
  472. SetDown(bInside);
  473. }
  474. }
  475. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  476. {
  477. if (button == 0) {
  478. if (bDown) {
  479. if (m_pprovider != NULL) {
  480. assert(false); // we should not have a timer set before the button is pressed
  481. m_pprovider->GetTimer()->RemoveSink(m_peventSinkDelegate);
  482. }
  483. if (pprovider->IsDoubleClick()) {
  484. m_peventSourceDoubleClick->Trigger();
  485. } else if (m_bDownTrigger) {
  486. m_peventSource->Trigger();
  487. } else {
  488. SetDown(true);
  489. if (m_repeatRate != 0 && m_bEnabled) {
  490. m_peventSource->Trigger();
  491. if (m_repeatDelay != 0) {
  492. m_bFirstEvent = true;
  493. m_pprovider = pprovider;
  494. pprovider->GetTimer()->AddSink(m_peventSinkDelegate, m_repeatDelay);
  495. } else {
  496. m_bFirstEvent = false;
  497. m_pprovider = pprovider;
  498. pprovider->GetTimer()->AddSink(m_peventSinkDelegate, m_repeatRate);
  499. }
  500. }
  501. return MouseResultCapture();
  502. }
  503. } else {
  504. if (bCaptured) {
  505. bool bWasDown = m_bDown;
  506. SetDown(false);
  507. if (m_repeatRate == 0) {
  508. if (bWasDown && m_bEnabled) {
  509. if (m_bToggle) {
  510. SetChecked(!m_bChecked);
  511. }
  512. m_peventSource->Trigger();
  513. }
  514. } else {
  515. ZAssert(m_pprovider == NULL || pprovider == m_pprovider);
  516. if (m_pprovider) {
  517. m_pprovider->GetTimer()->RemoveSink(m_peventSinkDelegate);
  518. m_pprovider = NULL;
  519. }
  520. }
  521. return MouseResultRelease();
  522. }
  523. }
  524. }
  525. return MouseResult();
  526. }
  527. //////////////////////////////////////////////////////////////////////////////
  528. //
  529. // IEventSink Methods
  530. //
  531. //////////////////////////////////////////////////////////////////////////////
  532. bool OnEvent(IEventSource* pevent)
  533. {
  534. if (m_bDown) {
  535. m_peventSource->Trigger();
  536. }
  537. if (m_bFirstEvent) {
  538. m_bFirstEvent = false;
  539. m_pprovider->GetTimer()->AddSink(m_peventSinkDelegate, m_repeatRate);
  540. return false;
  541. }
  542. return true;
  543. }
  544. };
  545. //////////////////////////////////////////////////////////////////////////////
  546. //
  547. // ButtonPane Contructors
  548. //
  549. //////////////////////////////////////////////////////////////////////////////
  550. TRef<ButtonPane> CreateButton(
  551. ButtonFacePane* ppane,
  552. bool bToggle,
  553. float repeatRate,
  554. float repeatDelay
  555. ) {
  556. return new ButtonUIPane(ppane, repeatRate, repeatDelay, bToggle);
  557. }
  558. TRef<ButtonPane> CreateButton(
  559. Pane* ppaneUp,
  560. Pane* ppaneDown,
  561. bool bToggle,
  562. float repeatRate,
  563. float repeatDelay
  564. ) {
  565. return
  566. CreateButton(
  567. CreateButtonFacePane(ppaneUp, ppaneDown),
  568. bToggle,
  569. repeatRate,
  570. repeatDelay
  571. );
  572. }
  573. TRef<ButtonPane> CreateButton(int size)
  574. {
  575. return
  576. CreateButton(
  577. new EdgePane(new Pane(NULL, WinPoint(size, size)), true),
  578. new EdgePane(new Pane(NULL, WinPoint(size, size)), false)
  579. );
  580. }
  581. //////////////////////////////////////////////////////////////////////////////
  582. //
  583. // ButtonBar
  584. //
  585. //////////////////////////////////////////////////////////////////////////////
  586. class ButtonBarPaneImpl :
  587. public ButtonBarPane,
  588. public IEventSink
  589. {
  590. private:
  591. TRef<IntegerEventSourceImpl> m_peventSource;
  592. TRef<IntegerEventSourceImpl> m_peventMouseEnterWhileEnabledSource;
  593. TRef<IEventSink> m_peventSink;
  594. TMap<TRef<IEventSource>, int> m_mapEventSources;
  595. TMap<TRef<IEventSource>, int> m_mapMouseEnterWhileEnabledSources;
  596. TMap<int, TRef<ButtonPane> > m_mapButtonPanes;
  597. TRef<Pane> m_pRowPane;
  598. int m_nCmdSelected;
  599. bool m_bActAsTabs;
  600. public:
  601. ButtonBarPaneImpl(bool bActAsTabs, bool bUseColumn) :
  602. m_nCmdSelected(-1),
  603. m_bActAsTabs(bActAsTabs)
  604. {
  605. m_peventSource = new IntegerEventSourceImpl();
  606. m_peventMouseEnterWhileEnabledSource = new IntegerEventSourceImpl();
  607. m_peventSink = IEventSink::CreateDelegate(this);
  608. if (!bUseColumn)
  609. m_pRowPane = new RowPane();
  610. else
  611. m_pRowPane = new ColumnPane();
  612. InsertAtBottom(m_pRowPane);
  613. }
  614. ~ButtonBarPaneImpl()
  615. {
  616. {
  617. TMap<TRef<IEventSource>, int>::Iterator iter(m_mapMouseEnterWhileEnabledSources);
  618. while (!iter.End()) {
  619. iter.Key()->RemoveSink(m_peventSink);
  620. iter.Next();
  621. }
  622. }
  623. {
  624. TMap<TRef<IEventSource>, int>::Iterator iter(m_mapEventSources);
  625. while (!iter.End()) {
  626. iter.Key()->RemoveSink(m_peventSink);
  627. iter.Next();
  628. }
  629. }
  630. }
  631. bool OnEvent(IEventSource* pevent)
  632. {
  633. int nCmd;
  634. if (m_mapMouseEnterWhileEnabledSources.Find(pevent, nCmd))
  635. {
  636. m_peventMouseEnterWhileEnabledSource->Trigger(nCmd);
  637. }
  638. else
  639. {
  640. ZVerify(m_mapEventSources.Find(pevent, nCmd));
  641. if (m_bActAsTabs) {
  642. TRef<ButtonPane> pButtonPane;
  643. if (m_nCmdSelected != -1) {
  644. ZVerify(m_mapButtonPanes.Find(m_nCmdSelected, pButtonPane));
  645. pButtonPane->SetChecked(false);
  646. }
  647. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  648. pButtonPane->SetChecked(true);
  649. m_nCmdSelected = nCmd;
  650. }
  651. m_peventSource->Trigger(nCmd);
  652. }
  653. return true;
  654. }
  655. IIntegerEventSource* GetEventSource()
  656. {
  657. return m_peventSource;
  658. }
  659. IIntegerEventSource* GetMouseEnterWhileEnabledEventSource()
  660. {
  661. return m_peventMouseEnterWhileEnabledSource;
  662. }
  663. void RemoveAll()
  664. {
  665. {
  666. TMap<TRef<IEventSource>, int>::Iterator iter(m_mapMouseEnterWhileEnabledSources);
  667. while (!iter.End()) {
  668. iter.Key()->RemoveSink(m_peventSink);
  669. iter.Next();
  670. }
  671. m_mapMouseEnterWhileEnabledSources.SetEmpty();
  672. }
  673. {
  674. TMap<TRef<IEventSource>, int>::Iterator iter(m_mapEventSources);
  675. while (!iter.End()) {
  676. iter.Key()->RemoveSink(m_peventSink);
  677. iter.Next();
  678. }
  679. m_mapEventSources.SetEmpty();
  680. }
  681. m_pRowPane->RemoveAllChildren();
  682. }
  683. void InsertButton(ButtonPane* pPane, int nCmd)
  684. {
  685. ZAssert(nCmd != -1);
  686. TRef<IEventSource> pEventSource = pPane->GetEventSource();
  687. m_mapEventSources.Set(pEventSource, nCmd);
  688. pEventSource->AddSink(m_peventSink);
  689. TRef<IEventSource> pEnterEventSource = pPane->GetMouseEnterWhileEnabledEventSource();
  690. m_mapMouseEnterWhileEnabledSources.Set(pEnterEventSource, nCmd);
  691. pEnterEventSource->AddSink(m_peventSink);
  692. m_mapButtonPanes.Set(nCmd, pPane);
  693. m_pRowPane->InsertAtBottom(pPane);
  694. if (m_bActAsTabs && m_mapButtonPanes.Count() == 1) {
  695. m_nCmdSelected = nCmd;
  696. pPane->SetChecked(true);
  697. pPane->SetChecked2(false);
  698. }
  699. }
  700. void SetHidden(int nCmd, bool bHidden)
  701. {
  702. TRef<ButtonPane> pButtonPane;
  703. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  704. pButtonPane->SetHidden(bHidden);
  705. }
  706. void SetEnabled(int nCmd, bool bEnabled)
  707. {
  708. TRef<ButtonPane> pButtonPane;
  709. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  710. pButtonPane->SetEnabled(bEnabled);
  711. }
  712. void SetChecked(int nCmd, bool bChecked)
  713. {
  714. TRef<ButtonPane> pButtonPane;
  715. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  716. pButtonPane->SetChecked(bChecked);
  717. }
  718. void SetChecked2(int nCmd, bool bChecked)
  719. {
  720. TRef<ButtonPane> pButtonPane;
  721. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  722. pButtonPane->SetChecked2(bChecked);
  723. }
  724. bool GetEnabled(int nCmd)
  725. {
  726. TRef<ButtonPane> pButtonPane;
  727. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  728. return pButtonPane->GetEnabled();
  729. }
  730. bool GetChecked(int nCmd)
  731. {
  732. TRef<ButtonPane> pButtonPane;
  733. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  734. return pButtonPane->GetChecked();
  735. }
  736. bool GetChecked2(int nCmd)
  737. {
  738. TRef<ButtonPane> pButtonPane;
  739. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  740. return pButtonPane->GetChecked2();
  741. }
  742. void FlashButton(int nCmd)
  743. {
  744. // NYI
  745. }
  746. int GetSelection()
  747. {
  748. return m_nCmdSelected;
  749. }
  750. void SetSelection(int nCmd)
  751. {
  752. if (m_bActAsTabs) {
  753. TRef<ButtonPane> pButtonPane;
  754. if (m_nCmdSelected != -1) {
  755. ZVerify(m_mapButtonPanes.Find(m_nCmdSelected, pButtonPane));
  756. pButtonPane->SetChecked(false);
  757. pButtonPane->SetChecked2(false);
  758. }
  759. if (nCmd != -1) {
  760. ZVerify(m_mapButtonPanes.Find(nCmd, pButtonPane));
  761. pButtonPane->SetChecked(true);
  762. m_nCmdSelected = nCmd;
  763. }
  764. m_nCmdSelected = nCmd;
  765. }
  766. }
  767. void UpdateLayout()
  768. {
  769. DefaultUpdateLayout();
  770. }
  771. };
  772. //////////////////////////////////////////////////////////////////////////////
  773. //
  774. // ButtonBar
  775. //
  776. //////////////////////////////////////////////////////////////////////////////
  777. TRef<ButtonBarPane> CreateButtonBarPane(bool bActAsTabs, bool bUseColumn)
  778. {
  779. return new ButtonBarPaneImpl(bActAsTabs, bUseColumn);
  780. }
  781. TRef<ButtonBarPane> CreateButtonBarPane(Surface* psurface, DWORD dwFaces, TVector<int>& vecColumns, bool bActAsTabs)
  782. {
  783. TRef<ButtonBarPane> pbuttonBar = new ButtonBarPaneImpl(bActAsTabs, false);
  784. //
  785. // Create all the buttons
  786. //
  787. int count = vecColumns.GetCount();
  788. int xprev = 0;
  789. for (int index = 0; index < count; index++) {
  790. int x = vecColumns[index];
  791. pbuttonBar->InsertButton(
  792. CreateButton(CreateButtonFacePane(psurface, dwFaces, xprev, x)),
  793. index
  794. );
  795. xprev = x;
  796. }
  797. return pbuttonBar;
  798. }