UIMenubar.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_menu_window.h>
  23. #include <TurboBadger/tb_select.h>
  24. #include "UIMenubar.h"
  25. using namespace tb;
  26. namespace tb {
  27. // THIS MUST MATCH TBSimpleLayoutItemWidget in tb_select_item.cpp
  28. class TBSimpleLayoutItemWidget : public TBLayout, private TBWidgetListener
  29. {
  30. public:
  31. TBSimpleLayoutItemWidget(TBID image, TBSelectItemSource *source, const char *str);
  32. ~TBSimpleLayoutItemWidget();
  33. virtual bool OnEvent(const TBWidgetEvent &ev);
  34. private:
  35. TBSelectItemSource *m_source;
  36. TBTextField m_textfield;
  37. TBSkinImage m_image;
  38. TBSkinImage m_image_arrow;
  39. TBMenuWindow *m_menu; ///< Points to the submenu window if opened
  40. virtual void OnWidgetDelete(TBWidget *widget);
  41. void OpenSubMenu();
  42. void CloseSubMenu();
  43. };
  44. }
  45. namespace Atomic
  46. {
  47. class MenubarItem : public TBGenericStringItem
  48. {
  49. public:
  50. MenubarItem(const char *str, const TBID &id = TBID((uint32)0), const String& shortcut = String::EMPTY)
  51. : TBGenericStringItem(str, id), shortcut_(shortcut)
  52. {
  53. }
  54. MenubarItem(const char *str, TBSelectItemSource *sub_source) : TBGenericStringItem(str, sub_source) {}
  55. String shortcut_;
  56. };
  57. class MenubarItemSource : public TBSelectItemSourceList<MenubarItem>
  58. {
  59. public:
  60. virtual bool Filter(int index, const char *filter);
  61. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  62. };
  63. class MenubarItemWidget : public TBLayout
  64. {
  65. public:
  66. MenubarItemWidget(MenubarItem *item, MenubarItemSource *source, TBSelectItemViewer *source_viewer, int index);
  67. virtual bool OnEvent(const TBWidgetEvent &ev);
  68. private:
  69. MenubarItemSource *m_source;
  70. TBSelectItemViewer *m_source_viewer;
  71. int m_index;
  72. };
  73. // UI IMPLEMENTATION
  74. UIMenuItem::UIMenuItem(Context* context, const String& str, const String& id, const String& shortcut, const String& skinBg):
  75. UISelectItem(context, str, id),
  76. shortcut_(shortcut),
  77. skinBg_(skinBg)
  78. {
  79. }
  80. UIMenuItem::~UIMenuItem()
  81. {
  82. }
  83. tb::TBGenericStringItem* UIMenuItem::GetTBItem()
  84. {
  85. MenubarItem* item;
  86. if (!subSource_)
  87. {
  88. item = new MenubarItem(str_.CString(), id_, shortcut_);
  89. }
  90. else
  91. {
  92. item = new MenubarItem(str_.CString(), subSource_->GetTBItemSource());
  93. }
  94. if (skinBg_.Length())
  95. {
  96. item->SetSkinImage(TBIDC(skinBg_.CString()));
  97. }
  98. return item;
  99. }
  100. tb::TBSelectItemSource* UIMenuItemSource::GetTBItemSource()
  101. {
  102. // caller's responsibility to clean up
  103. MenubarItemSource* src = new MenubarItemSource();
  104. for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  105. {
  106. MenubarItem* tbitem = (MenubarItem*) (*itr)->GetTBItem();
  107. src->AddItem(tbitem);
  108. }
  109. return src;
  110. }
  111. UIMenuItemSource::UIMenuItemSource(Context* context) : UISelectItemSource(context)
  112. {
  113. }
  114. UIMenuItemSource::~UIMenuItemSource()
  115. {
  116. }
  117. // WIDGET IMPLEMENTATION
  118. MenubarItemWidget::MenubarItemWidget(MenubarItem *item, MenubarItemSource *source,
  119. TBSelectItemViewer *source_viewer, int index)
  120. : m_source(source)
  121. , m_source_viewer(source_viewer)
  122. , m_index(index)
  123. {
  124. SetSkinBg(TBIDC("TBSelectItem"));
  125. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  126. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  127. SetPaintOverflowFadeout(false);
  128. TBWidget* root = GetContentRoot();
  129. TBFontDescription fd;
  130. fd.SetID(TBIDC("Vera"));
  131. fd.SetSize(12);
  132. TBTextField* text = new TBTextField();
  133. text->SetIgnoreInput(true);
  134. text->SetText(item->str);
  135. text->SetFontDescription(fd);
  136. root->AddChild(text);
  137. if (item->shortcut_.Length())
  138. {
  139. TBWidget* spacer = new TBWidget();
  140. spacer->SetIgnoreInput(true);
  141. spacer->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  142. root->AddChild(spacer);
  143. TBTextField* shortcut = new TBTextField();
  144. shortcut->SetIgnoreInput(true);
  145. shortcut->SetText(item->shortcut_.CString());
  146. shortcut->SetFontDescription(fd);
  147. shortcut->SetGravity(WIDGET_GRAVITY_RIGHT);
  148. root->AddChild(shortcut);
  149. }
  150. }
  151. bool MenubarItemWidget::OnEvent(const TBWidgetEvent &ev)
  152. {
  153. if (m_source && ev.type == EVENT_TYPE_CLICK && ev.target == this)
  154. {
  155. return false;
  156. }
  157. return false;
  158. }
  159. bool MenubarItemSource::Filter(int index, const char *filter)
  160. {
  161. return true;
  162. }
  163. TBWidget *MenubarItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  164. {
  165. const char *string = GetItemString(index);
  166. TBSelectItemSource *sub_source = GetItemSubSource(index);
  167. TBID image = GetItemImage(index);
  168. if (sub_source || image)
  169. {
  170. if (TBSimpleLayoutItemWidget *itemwidget = new TBSimpleLayoutItemWidget(image, sub_source, string))
  171. {
  172. itemwidget->SetID(GetItem(index)->id);
  173. return itemwidget;
  174. }
  175. }
  176. else if (string && *string == '-')
  177. {
  178. if (TBSeparator *separator = new TBSeparator)
  179. {
  180. separator->SetGravity(WIDGET_GRAVITY_ALL);
  181. separator->SetSkinBg(TBIDC("AESeparator"));
  182. return separator;
  183. }
  184. }
  185. else if (TBLayout *layout = new MenubarItemWidget(GetItem(index), this, viewer, index))
  186. {
  187. layout->SetID(GetItem(index)->id);
  188. return layout;
  189. }
  190. return NULL;
  191. }
  192. }