tb_atomic_widgets.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. //
  6. // Copyright (c) 2016-2017, THUNDERBEAST GAMES LLC All rights reserved
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #ifndef TB_ATOMIC_WIDGETS_H
  27. #define TB_ATOMIC_WIDGETS_H
  28. #include "tb_widgets.h"
  29. #include "tb_layout.h"
  30. #include "tb_msg.h"
  31. #include "tb_widgets_listener.h"
  32. #include "tb_widgets_common.h"
  33. #include "tb_window.h"
  34. #include "tb_scroll_container.h"
  35. #include "tb_select_item.h"
  36. #include "tb_menu_window.h"
  37. #define UIPROMPTMESSAGEID 1
  38. #define UIPROMPTEDITID 2
  39. #define UIFINDEREDITPATHID 1
  40. #define UIFINDERUPBUTTONID 2
  41. #define UIFINDERBOOKBUTTONID 3
  42. #define UIFINDERFOLDERBUTTONID 4
  43. #define UIFINDERBOOKLISTID 5
  44. #define UIFINDERFILELISTID 6
  45. #define UIFINDEREDITFILEID 7
  46. #define UIFINDEROKBUTTONID 8
  47. #define UIFINDERCANCELBUTTONID 9
  48. namespace tb {
  49. // fruxo recommends : Subclass TBWidget and override OnPaint. From
  50. // there you can paint solid colors using g_tb_skin->PaintRectFill.
  51. class TBColorWidget : public TBWidget
  52. {
  53. public:
  54. TBOBJECT_SUBCLASS(TBColorWidget, TBWidget); // For safe typecasting
  55. TBColorWidget();
  56. void SetColor ( const char * );
  57. void SetColor (float r, float g, float b, float a);
  58. void SetAlpha ( float );
  59. const TBColor& GetColor() const { return color_; }
  60. float GetAlpha() const { return alpha_; }
  61. virtual void OnInflate(const INFLATE_INFO &info);
  62. virtual void OnPaint(const PaintProps &paint_props);
  63. private:
  64. TBColor color_;
  65. float alpha_;
  66. };
  67. class TBColorWheel : public TBWidget
  68. {
  69. public:
  70. TBOBJECT_SUBCLASS(TBColorWheel, TBWidget); // For safe typecasting
  71. TBColorWheel();
  72. virtual void OnInflate(const INFLATE_INFO &info);
  73. virtual void OnPaint(const PaintProps &paint_props);
  74. virtual bool OnEvent(const TBWidgetEvent &ev);
  75. float GetHue() const { return hue_; }
  76. float GetSaturation() const { return saturation_; }
  77. void SetHueSaturation ( float hue, float saturation );
  78. void SetMarkerX ( int );
  79. void SetMarkerY( int );
  80. void SetMarkerColor ( const char * );
  81. private:
  82. void CalcHueSaturation ( int, int ); // maths.
  83. int markerx_;
  84. int markery_; // where we clicked, put a box there
  85. TBColor markercolor_; // what color box, default = black
  86. float hue_; // varies with the angle
  87. float saturation_; // varies with the radius.
  88. };
  89. class TBEventBlocker : public TBWidgetListener
  90. {
  91. public:
  92. TBEventBlocker() : TBWidgetListener() { }
  93. /** Called when a event is about to be invoked on a widget. This make it possible
  94. to intercept events before they are handled, and block it (by returning true --like here ).
  95. Note, if returning true, other global listeners will still also be notified. */
  96. virtual bool OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev) { return true; }
  97. };
  98. /// TBBarGraph a simple bargraph, plots 0.0 to 100.0 in the selected color
  99. class TBBarGraph : public TBWidget
  100. {
  101. public:
  102. TBOBJECT_SUBCLASS(TBBarGraph, TBWidget); // For safe typecasting
  103. TBBarGraph();
  104. void SetColor ( const char * );
  105. void SetColor (float r, float g, float b, float a);
  106. const TBColor& GetColor() const { return color_; }
  107. virtual void OnInflate(const INFLATE_INFO &info);
  108. virtual void OnPaint(const PaintProps &paint_props);
  109. virtual void SetValueDouble(double value);
  110. virtual double GetValueDouble() { return m_value; }
  111. virtual void SetValue(int value) { SetValueDouble(value); }
  112. virtual int GetValue() { return (int) GetValueDouble(); }
  113. virtual void SetAxis(AXIS axis);
  114. virtual AXIS GetAxis() const { return m_axis; }
  115. virtual void SetMargin(unsigned margin) { m_margin = margin; }
  116. virtual unsigned GetMargin() const { return m_margin; }
  117. private:
  118. TBColor color_;
  119. double m_value;
  120. AXIS m_axis;
  121. unsigned m_margin;
  122. };
  123. /** TBPromptWindow is a window for requesting a string. */
  124. class TBPromptWindow : public TBWindow, private TBWidgetListener
  125. {
  126. public:
  127. // For safe typecasting
  128. TBOBJECT_SUBCLASS(TBPromptWindow, TBWindow);
  129. TBPromptWindow(TBWidget *target, TBID id);
  130. virtual ~TBPromptWindow();
  131. bool Show(const char *title, const char *message,
  132. const char *preset = nullptr, int dimmer = 0,
  133. int width = 0, int height = 0);
  134. virtual TBWidget *GetEventDestination() { return m_target.Get(); }
  135. virtual bool OnEvent(const TBWidgetEvent &ev);
  136. virtual void OnDie();
  137. private:
  138. // TBWidgetListener
  139. virtual void OnWidgetDelete(TBWidget *widget);
  140. virtual bool OnWidgetDying(TBWidget *widget);
  141. TBWidgetSafePointer m_dimmer;
  142. TBWidgetSafePointer m_target;
  143. };
  144. // file, path finder
  145. class TBFinderWindow : public TBWindow, private TBWidgetListener
  146. {
  147. public:
  148. // For safe typecasting
  149. TBOBJECT_SUBCLASS(TBFinderWindow, TBWindow);
  150. TBFinderWindow(TBWidget *target, TBID id);
  151. virtual ~TBFinderWindow();
  152. bool Show(const char *title,
  153. const char *preset = nullptr, int dimmer = 0,
  154. int width = 0, int height = 0);
  155. virtual TBWidget *GetEventDestination() { return m_target.Get(); }
  156. virtual bool OnEvent(const TBWidgetEvent &ev);
  157. virtual void OnDie();
  158. private:
  159. // TBWidgetListener
  160. virtual void OnWidgetDelete(TBWidget *widget);
  161. virtual bool OnWidgetDying(TBWidget *widget);
  162. TBWidget *FindParentList( TBWidget *widget); // utility for dealing with menus.
  163. TBWidgetSafePointer m_dimmer;
  164. TBWidgetSafePointer m_target;
  165. TBWidget *rightMenuParent; // information for context menus
  166. TBWidget *rightMenuChild;
  167. };
  168. /** TBSelectDropdown shows a button that opens a popup with a TBSelectList with items
  169. provided by a TBSelectItemSource. */
  170. class TBPulldownMenu : public TBButton, public TBSelectItemViewer
  171. {
  172. public:
  173. // For safe typecasting
  174. TBOBJECT_SUBCLASS(TBPulldownMenu, TBButton);
  175. TBPulldownMenu();
  176. ~TBPulldownMenu();
  177. /** Get the default item source for this widget. This source can be used to add
  178. items of type TBGenericStringItem to this widget. */
  179. TBGenericStringItemSource *GetDefaultSource() { return &m_default_source; }
  180. /** Save the selected item. Transfers value from menu to button parent */
  181. virtual void SetValue(int value);
  182. virtual int GetValue() { return m_value; }
  183. /** Save the selected item TBID. Transfers value from menu to button parent */
  184. void SetValueID(TBID value) { m_valueid = value; }
  185. TBID GetValueID() { return m_valueid; }
  186. /** Get the ID of the selected item, or 0 if there is no item selected. */
  187. TBID GetSelectedItemID();
  188. /** Open the window if the model has items. */
  189. void OpenWindow();
  190. /** Close the window if it is open. */
  191. void CloseWindow();
  192. /** Return the menu window if it's open, or nullptr. */
  193. TBMenuWindow *GetMenuIfOpen() const;
  194. virtual void OnInflate(const INFLATE_INFO &info);
  195. virtual bool OnEvent(const TBWidgetEvent &ev);
  196. // TBSelectItemViewer
  197. virtual void OnSourceChanged();
  198. virtual void OnItemChanged(int index) {}
  199. virtual void OnItemAdded(int index) {}
  200. virtual void OnItemRemoved(int index) {}
  201. virtual void OnAllItemsRemoved() {}
  202. virtual bool OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev) { return false; }
  203. protected:
  204. void InvokeModifiedEvent(TBID entryid);
  205. TBGenericStringItemSource m_default_source;
  206. TBWidgetSafePointer m_window_pointer; ///< Points to the dropdown window if opened
  207. int m_value; /// which list item has been selected
  208. TBID m_valueid; /// the TBID of the list item that was selected
  209. };
  210. /** TBDockWindow is a window for undocking (and redocking) content */
  211. class TBDockWindow : public TBWindow
  212. {
  213. public:
  214. // For safe typecasting
  215. TBOBJECT_SUBCLASS(TBDockWindow, TBWindow);
  216. TBDockWindow( TBStr title, TBWidget *contentptr, int minwidth = 800, int minheight = 400 );
  217. virtual ~TBDockWindow();
  218. void SetDockOrigin( TBID dockid );
  219. TBWidget *GetDockContent();
  220. bool HasDockContent();
  221. void Dock( TBWidget *target );
  222. void Show(TBWidget *host, int xpos = 50, int ypos = 50 );
  223. virtual bool OnEvent(const TBWidgetEvent &ev);
  224. virtual void OnResized(int old_w, int old_h);
  225. /// Get the widget that should receive the events this widget invoke.
  226. /// By default the parent, when undocked, the origin widget if specified.
  227. virtual TBWidget *GetEventDestination();
  228. protected:
  229. void Redock();
  230. TBWidget m_redock_button; // return content to origin
  231. TBID m_dockid; /// the TBID of the origin container
  232. };
  233. /// == MultiList Support ==========================================
  234. /** MultiItem adds extra info to a string item. */
  235. class MultiItem : public TBGenericStringItem
  236. {
  237. public:
  238. MultiItem(const TBID &colid, TBStr widgettype0, TBStr str0, int width0, int colheight=0 )
  239. : TBGenericStringItem(str0, colid),
  240. colStr_(TBValue::TYPE_ARRAY),
  241. colWidget_(TBValue::TYPE_ARRAY),
  242. colWidth_(TBValue::TYPE_ARRAY),
  243. colHeight_(colheight)
  244. {
  245. colStr_.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
  246. colWidget_.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
  247. colWidth_.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
  248. AddColumn ( widgettype0, str0, width0);
  249. }
  250. void AddColumn ( TBStr string, TBStr widgettype, int width );
  251. const char* GetColumnStr( int col );
  252. const char* GetColumnWidget( int col );
  253. int GetColumnWidth( int col );
  254. int GetColumnHeight();
  255. int GetNumColumns();
  256. private:
  257. TBValue colStr_;
  258. TBValue colWidget_;
  259. TBValue colWidth_;
  260. int colHeight_;
  261. };
  262. /** MultiItemSource provides items of type MultiItem and makes sure
  263. the viewer is populated with the customized widget for each item. */
  264. class MultiItemSource : public TBSelectItemSourceList<MultiItem>
  265. {
  266. public:
  267. virtual bool Filter(int index, const char *filter);
  268. virtual TBWidget *CreateItemWidget(int index, TBSelectItemViewer *viewer);
  269. };
  270. /** MultiItemWidget is the widget representing a MultiItem.
  271. On changes to the item, it calls InvokeItemChanged on the source, so that all
  272. viewers of the source are updated to reflect the change. */
  273. class MultiItemWidget : public TBLayout
  274. {
  275. public:
  276. MultiItemWidget(MultiItem *item, MultiItemSource *source, TBSelectItemViewer *source_viewer, int index);
  277. virtual bool OnEvent(const TBWidgetEvent &ev);
  278. private:
  279. MultiItemSource *m_source;
  280. TBSelectItemViewer *m_source_viewer;
  281. int m_index;
  282. };
  283. }; // namespace tb
  284. #endif // TB_ATOMIC_WIDGETS_H