combopane.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. //
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class StringComboFacePane : public ComboFacePane {
  8. private:
  9. TRef<StringPane> m_pstringPane;
  10. TRef<BorderPane> m_pborder;
  11. public:
  12. StringComboFacePane(
  13. const WinPoint& size,
  14. IEngineFont* pfont,
  15. const Color& color,
  16. bool bBackgroundColor
  17. ) {
  18. InternalSetSize(size);
  19. m_pstringPane =
  20. new StringPane(
  21. ZString(),
  22. pfont,
  23. size
  24. );
  25. m_pstringPane->SetTextColor(color);
  26. if (bBackgroundColor) {
  27. InsertAtBottom(
  28. m_pborder = new BorderPane(
  29. 0,
  30. Color(0.25, 0.25, 0.25),
  31. m_pstringPane
  32. )
  33. );
  34. } else {
  35. InsertAtBottom(m_pstringPane);
  36. }
  37. }
  38. void SetString(const ZString& str)
  39. {
  40. m_pstringPane->SetString(str);
  41. }
  42. void SetInside(bool bInside)
  43. {
  44. }
  45. void SetColor(const Color& color)
  46. {
  47. if (m_pborder) {
  48. m_pborder->SetColor(color);
  49. }
  50. }
  51. };
  52. TRef<ComboFacePane> CreateStringComboFacePane(
  53. const WinPoint& size,
  54. IEngineFont* pfont,
  55. const Color& color,
  56. bool bBackgroundColor
  57. ) {
  58. return new StringComboFacePane(size, pfont, color, bBackgroundColor);
  59. }
  60. //////////////////////////////////////////////////////////////////////////////
  61. //
  62. //
  63. //
  64. //////////////////////////////////////////////////////////////////////////////
  65. class ImageComboFacePane : public ComboFacePane {
  66. private:
  67. TRef<ButtonFacePane> m_pfacePane;
  68. public:
  69. ImageComboFacePane(Image* pimage)
  70. {
  71. m_pfacePane =
  72. CreateButtonFacePane(
  73. pimage->GetSurface(),
  74. ButtonFaceUp | ButtonFaceInside
  75. );
  76. InsertAtBottom(m_pfacePane);
  77. }
  78. void SetString(const ZString& str)
  79. {
  80. }
  81. void SetColor(const Color& color)
  82. {
  83. }
  84. void SetInside(bool bInside)
  85. {
  86. m_pfacePane->SetInside(bInside);
  87. }
  88. void UpdateLayout()
  89. {
  90. DefaultUpdateLayout();
  91. }
  92. };
  93. TRef<ComboFacePane> CreateImageComboFacePane(Image* pimage)
  94. {
  95. return new ImageComboFacePane(pimage);
  96. }
  97. //////////////////////////////////////////////////////////////////////////////
  98. //
  99. //
  100. //
  101. //////////////////////////////////////////////////////////////////////////////
  102. class ComboPaneImpl :
  103. public ComboPane,
  104. public IMenuCommandSink
  105. {
  106. private:
  107. TRef<IntegerEventSourceImpl> m_peventSource;
  108. TRef<EventSourceImpl> m_peventMouseEnterWhileEnabledSource;
  109. TRef<EventSourceImpl> m_peventMenuSelectSource;
  110. TRef<IMenu> m_pmenu;
  111. TRef<Modeler> m_pmodeler;
  112. TRef<IEngineFont> m_pfont;
  113. TRef<ComboFacePane> m_pface;
  114. TRef<IPopupContainer> m_ppopupContainer;
  115. bool m_bFirstItem;
  116. TVector<ZString> m_vstrItems;
  117. int m_idSelection;
  118. bool m_bEnabled;
  119. bool m_bInside;
  120. public:
  121. ComboPaneImpl(
  122. Modeler* pmodeler,
  123. IPopupContainer* ppopupContainer,
  124. IEngineFont* pfont,
  125. const WinPoint& size,
  126. ComboFacePane* pface
  127. ) :
  128. m_peventMouseEnterWhileEnabledSource(new EventSourceImpl()),
  129. m_peventMenuSelectSource(new EventSourceImpl()),
  130. m_ppopupContainer(ppopupContainer),
  131. m_pface(pface),
  132. m_bFirstItem(true),
  133. m_idSelection(0),
  134. m_bInside(false),
  135. m_bEnabled(true),
  136. m_pmodeler(pmodeler),
  137. m_pfont(pfont)
  138. {
  139. InsertAtBottom(
  140. new JustifyPane(
  141. JustifyPane::Left,
  142. size,
  143. pface
  144. )
  145. );
  146. m_peventSource = new IntegerEventSourceImpl();
  147. m_pmenu =
  148. CreateMenu(
  149. pmodeler,
  150. pfont,
  151. IMenuCommandSink::CreateDelegate(this)
  152. );
  153. }
  154. ~ComboPaneImpl()
  155. {
  156. m_pmenu->SetMenuCommandSink(NULL);
  157. }
  158. //////////////////////////////////////////////////////////////////////////////
  159. //
  160. // IEventSink
  161. //
  162. //////////////////////////////////////////////////////////////////////////////
  163. bool OnEvent(IEventSource* peventSource)
  164. {
  165. return true;
  166. }
  167. //////////////////////////////////////////////////////////////////////////////
  168. //
  169. // IMenuCommandSink
  170. //
  171. //////////////////////////////////////////////////////////////////////////////
  172. void OnMenuCommand(IMenuItem* pitem)
  173. {
  174. m_pface->SetString(pitem->GetString());
  175. m_pface->SetColor(pitem->GetColor());
  176. m_idSelection = pitem->GetID();
  177. m_peventMenuSelectSource->Trigger();
  178. m_peventSource->Trigger(pitem->GetID());
  179. m_ppopupContainer->ClosePopup(m_pmenu);
  180. }
  181. //////////////////////////////////////////////////////////////////////////////
  182. //
  183. // ComboPane methods
  184. //
  185. //////////////////////////////////////////////////////////////////////////////
  186. void ClearContents()
  187. {
  188. m_bFirstItem = true;
  189. m_idSelection = 0;
  190. m_vstrItems.SetEmpty();
  191. m_pmenu =
  192. CreateMenu(
  193. m_pmodeler,
  194. m_pfont,
  195. IMenuCommandSink::CreateDelegate(this)
  196. );
  197. }
  198. TRef<IMenuItem> AddItem(const ZString& str, int id)
  199. {
  200. TRef<IMenuItem> pitem = m_pmenu->AddMenuItem(id, str);
  201. if (m_bFirstItem) {
  202. m_bFirstItem = false;
  203. m_idSelection = id;
  204. m_pface->SetString(pitem->GetString());
  205. m_pface->SetColor(pitem->GetColor());
  206. }
  207. m_vstrItems.PushEnd(str);
  208. return pitem;
  209. }
  210. TRef<IMenuItem> AddItem(const ZString& str, int id, const Color& color)
  211. {
  212. bool bFirstItem = m_bFirstItem;
  213. TRef<IMenuItem> pitem = m_pmenu->AddMenuItem(id, str);
  214. if (bFirstItem) {
  215. m_pface->SetColor(color);
  216. }
  217. return pitem;
  218. }
  219. void SetSelection(int id)
  220. {
  221. m_idSelection = id;
  222. IMenuItem* pitem = m_pmenu->FindMenuItem(id);
  223. if (pitem) {
  224. m_pface->SetString(pitem->GetString());
  225. m_pface->SetColor(pitem->GetColor());
  226. m_peventSource->Trigger(id);
  227. } else {
  228. m_pface->SetString("");
  229. m_peventSource->Trigger(id);
  230. }
  231. }
  232. const ZString * GetSelectionString()
  233. {
  234. return &m_vstrItems[m_idSelection];
  235. }
  236. int GetSelection()
  237. {
  238. return m_idSelection;
  239. }
  240. IIntegerEventSource* GetEventSource()
  241. {
  242. return m_peventSource;
  243. }
  244. void SetEnabled(bool bEnabled)
  245. {
  246. if (m_bEnabled != bEnabled)
  247. {
  248. m_bEnabled = bEnabled;
  249. if (bEnabled && m_bInside)
  250. m_peventMouseEnterWhileEnabledSource->Trigger();
  251. }
  252. }
  253. IEventSource* GetMouseEnterWhileEnabledEventSource()
  254. {
  255. return m_peventMouseEnterWhileEnabledSource;
  256. }
  257. IEventSource* GetMenuSelectEventSource()
  258. {
  259. return m_peventMenuSelectSource;
  260. }
  261. //////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Pane methods
  264. //
  265. //////////////////////////////////////////////////////////////////////////////
  266. void UpdateLayout()
  267. {
  268. DefaultUpdateLayout();
  269. }
  270. //////////////////////////////////////////////////////////////////////////////
  271. //
  272. // IMouseInput methods
  273. //
  274. //////////////////////////////////////////////////////////////////////////////
  275. void MouseEnter(IInputProvider* pprovider, const Point& point)
  276. {
  277. m_bInside = true;
  278. m_pface->SetInside(true);
  279. if (m_bEnabled)
  280. m_peventMouseEnterWhileEnabledSource->Trigger();
  281. }
  282. void MouseLeave(IInputProvider* pprovider)
  283. {
  284. m_bInside = false;
  285. m_pface->SetInside(false);
  286. }
  287. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  288. {
  289. bool bInside =
  290. !IsHidden()
  291. && point.X() >= 0
  292. && point.X() < (float)XSize()
  293. && point.Y() >= 0
  294. && point.Y() < (float)YSize();
  295. return bInside ? MouseResultHit() : MouseResult();
  296. }
  297. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  298. {
  299. if (button == 0 && m_bEnabled) {
  300. if (bDown) {
  301. Point point =
  302. TransformLocalToImage(
  303. WinPoint(
  304. 0,
  305. m_pface->GetSize().Y()
  306. )
  307. );
  308. m_ppopupContainer->OpenPopup(m_pmenu, Rect(point,point), true, true);
  309. }
  310. }
  311. return MouseResult();
  312. }
  313. };
  314. TRef<ComboPane> CreateComboPane(
  315. Modeler* pmodeler,
  316. IPopupContainer* ppopupContainer,
  317. IEngineFont* pfont,
  318. const WinPoint& size,
  319. ComboFacePane* pface
  320. ) {
  321. return new ComboPaneImpl(pmodeler, ppopupContainer, pfont, size, pface);
  322. }