tb_widgets_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #ifndef TB_WIDGETS_COMMON_H
  6. #define TB_WIDGETS_COMMON_H
  7. #include "tb_widgets.h"
  8. #include "tb_layout.h"
  9. #include "tb_msg.h"
  10. namespace tb {
  11. /** TB_TEXT_ALIGN specifies horizontal text alignment */
  12. enum TB_TEXT_ALIGN {
  13. TB_TEXT_ALIGN_LEFT, ///< Aligned left
  14. TB_TEXT_ALIGN_RIGHT, ///< Aligned right
  15. TB_TEXT_ALIGN_CENTER ///< Aligned center
  16. };
  17. /** TBWidgetString holds a string that can be painted as one line with the set alignment. */
  18. class TBWidgetString
  19. {
  20. public:
  21. TBWidgetString();
  22. void Paint(TBWidget *widget, const TBRect &rect, const TBColor &color);
  23. int GetWidth(TBWidget *widget);
  24. int GetHeight(TBWidget *widget);
  25. bool SetText(const char *text) { return m_text.Set(text); }
  26. bool GetText(TBStr &text) const { return text.Set(m_text); }
  27. bool IsEmpty() const { return m_text.IsEmpty(); }
  28. /** Set which alignment the text should have if the space
  29. given when painting is larger than the text. */
  30. void SetTextAlign(TB_TEXT_ALIGN align) { m_text_align = align; }
  31. TB_TEXT_ALIGN GetTextAlign() { return m_text_align; }
  32. public:
  33. TBStr m_text;
  34. TB_TEXT_ALIGN m_text_align;
  35. };
  36. /** TBTextField is a one line text field that is not editable. */
  37. class TBTextField : public TBWidget
  38. {
  39. public:
  40. // For safe typecasting
  41. TBOBJECT_SUBCLASS(TBTextField, TBWidget);
  42. TBTextField();
  43. /** Set the text of the text field. */
  44. virtual bool SetText(const char *text);
  45. virtual bool GetText(TBStr &text) { return m_text.GetText(text); }
  46. using TBWidget::GetText; ///< Make all versions in base class available.
  47. bool IsEmpty() const { return m_text.IsEmpty(); }
  48. /** Set which alignment the text should have if the space
  49. given when painting is larger than the text. */
  50. void SetTextAlign(TB_TEXT_ALIGN align) { m_text.SetTextAlign(align); }
  51. TB_TEXT_ALIGN GetTextAlign() { return m_text.GetTextAlign(); }
  52. /** Set if this text field should be allowed to squeeze below its
  53. preferred size. If squeezable it may shrink to width 0. */
  54. void SetSqueezable(bool squeezable);
  55. bool GetSqueezable() { return m_squeezable; }
  56. virtual void OnInflate(const INFLATE_INFO &info);
  57. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints);
  58. virtual void OnFontChanged();
  59. virtual void OnPaint(const PaintProps &paint_props);
  60. protected:
  61. TBWidgetString m_text;
  62. int m_cached_text_width; ///< Cached width of m_text
  63. bool m_squeezable;
  64. };
  65. /** TBButton is a regular button widget.
  66. Has a text field in its internal layout by default. Other widgets can be added
  67. under GetContentRoot(). */
  68. class TBButton : public TBWidget, protected TBMessageHandler
  69. {
  70. public:
  71. // For safe typecasting
  72. TBOBJECT_SUBCLASS(TBButton, TBWidget);
  73. TBButton();
  74. ~TBButton();
  75. /** Set along which axis the content should layouted (If the button has more content than the text) */
  76. virtual void SetAxis(AXIS axis) { m_layout.SetAxis(axis); }
  77. virtual AXIS GetAxis() const { return m_layout.GetAxis(); }
  78. /** Set if the text field should be allowed to squeeze below its
  79. preferred size. If squeezable it may shrink to width 0. */
  80. void SetSqueezable(bool squeezable) { m_textfield.SetSqueezable(squeezable); }
  81. bool GetSqueezable() { return m_textfield.GetSqueezable(); }
  82. /** Set to true if the button should fire repeatedly while pressed. */
  83. void SetAutoRepeat(bool auto_repeat_click) { m_auto_repeat_click = auto_repeat_click; }
  84. bool GetAutoRepeat() { return m_auto_repeat_click; }
  85. /** Set to true if the button should toggle on and off, instead of just fire
  86. click events. When it's on, it will have value 1 pressed state. */
  87. void SetToggleMode(bool toggle_mode_on) { m_toggle_mode = toggle_mode_on; }
  88. bool GetToggleMode() const { return m_toggle_mode; }
  89. /** Set the text of the button. */
  90. virtual bool SetText(const char *text);
  91. virtual bool GetText(TBStr &text) { return m_textfield.GetText(text); }
  92. using TBWidget::GetText; ///< Make all versions in base class available.
  93. virtual void SetValue(int value) { SetState(WIDGET_STATE_PRESSED, value ? true : false); }
  94. virtual int GetValue() { return GetState(WIDGET_STATE_PRESSED); }
  95. virtual void OnInflate(const INFLATE_INFO &info);
  96. virtual void OnCaptureChanged(bool captured);
  97. virtual void OnSkinChanged();
  98. virtual bool OnEvent(const TBWidgetEvent &ev);
  99. virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y);
  100. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints) { return m_layout.GetPreferredSize(); }
  101. virtual TBWidget *GetContentRoot() { return &m_layout; }
  102. // == TBMessageHandler ==============================================================
  103. virtual void OnMessageReceived(TBMessage *msg);
  104. protected:
  105. void UpdateTextFieldVisibility();
  106. class ButtonLayout : public TBLayout
  107. {
  108. virtual void OnChildAdded(TBWidget *child);
  109. virtual void OnChildRemove(TBWidget *child);
  110. };
  111. ButtonLayout m_layout;
  112. TBTextField m_textfield;
  113. bool m_auto_repeat_click;
  114. bool m_toggle_mode;
  115. };
  116. /** TBClickLabel has a text field in its internal layout by default. Pointer input on the
  117. text field will be redirected to another child widget (that you add) to it.
  118. Typically useful for creating check boxes, radio buttons with labels. */
  119. class TBClickLabel : public TBWidget
  120. {
  121. public:
  122. // For safe typecasting
  123. TBOBJECT_SUBCLASS(TBClickLabel, TBWidget);
  124. TBClickLabel();
  125. ~TBClickLabel();
  126. /** Set along which axis the content should layouted (If the label has more content than the text) */
  127. virtual void SetAxis(AXIS axis) { m_layout.SetAxis(axis); }
  128. virtual AXIS GetAxis() const { return m_layout.GetAxis(); }
  129. /** Set the text of the label. */
  130. virtual bool SetText(const char *text) { return m_textfield.SetText(text); }
  131. virtual bool GetText(TBStr &text) { return m_textfield.GetText(text); }
  132. using TBWidget::GetText; ///< Make all versions in base class available.
  133. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints) { return m_layout.GetPreferredSize(); }
  134. virtual TBWidget *GetContentRoot() { return &m_layout; }
  135. virtual bool OnEvent(const TBWidgetEvent &ev);
  136. protected:
  137. TBLayout m_layout;
  138. TBTextField m_textfield;
  139. };
  140. /** TBSkinImage is a widget showing a skin element, constrained in size to its skin.
  141. If you need to load and show images dynamically (i.e. not always loaded as the skin),
  142. you can use TBImageWidget. */
  143. class TBSkinImage : public TBWidget
  144. {
  145. public:
  146. // For safe typecasting
  147. TBOBJECT_SUBCLASS(TBSkinImage, TBWidget);
  148. TBSkinImage() {}
  149. TBSkinImage(const TBID &skin_bg) { SetSkinBg(skin_bg); }
  150. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  151. };
  152. /** TBSeparator is a widget only showing a skin.
  153. It is disabled by default. */
  154. class TBSeparator : public TBWidget
  155. {
  156. public:
  157. // For safe typecasting
  158. TBOBJECT_SUBCLASS(TBSeparator, TBWidget);
  159. TBSeparator();
  160. };
  161. /** TBProgressSpinner is a animation that is running while its value is 1.
  162. Typically used to indicate that the application is working. */
  163. class TBProgressSpinner : public TBWidget, protected TBMessageHandler
  164. {
  165. public:
  166. // For safe typecasting
  167. TBOBJECT_SUBCLASS(TBProgressSpinner, TBWidget);
  168. TBProgressSpinner();
  169. /** Return true if the animation is running. */
  170. bool IsRunning() { return m_value > 0; }
  171. /** Begin/End are used to start or stop the animation in a incremental way.
  172. If several tasks may activate the same spinner, calling Begin/End instead
  173. of using SetValue, so it will keep running as long as any source wants it to. */
  174. void Begin() { SetValue(GetValue() + 1); }
  175. void End() { SetValue(GetValue() - 1); }
  176. /** Setting the value to 1 will start the spinner.
  177. Setting it to 0 will stop it. */
  178. virtual void SetValue(int value);
  179. virtual int GetValue() { return m_value; }
  180. virtual void OnPaint(const PaintProps &paint_props);
  181. // == TBMessageHandler ==============================================================
  182. virtual void OnMessageReceived(TBMessage *msg);
  183. protected:
  184. int m_value;
  185. int m_frame;
  186. TBID m_skin_fg;
  187. };
  188. /** TBRadioCheckBox has shared functionality for TBCheckBox and TBRadioButton. */
  189. class TBRadioCheckBox : public TBWidget
  190. {
  191. public:
  192. // For safe typecasting
  193. TBOBJECT_SUBCLASS(TBRadioCheckBox, TBWidget);
  194. TBRadioCheckBox();
  195. virtual void SetValue(int value);
  196. virtual int GetValue() { return m_value; }
  197. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  198. virtual bool OnEvent(const TBWidgetEvent &ev);
  199. protected:
  200. void ToggleGroup(TBWidget *root, TBWidget *toggled);
  201. int m_value;
  202. };
  203. /** TBCheckBox is a box toggling a check mark on click.
  204. For a labeled checkbox, use a TBClickLabel containing a TBCheckBox. */
  205. class TBCheckBox : public TBRadioCheckBox
  206. {
  207. public:
  208. // For safe typecasting
  209. TBOBJECT_SUBCLASS(TBCheckBox, TBRadioCheckBox);
  210. TBCheckBox() { SetSkinBg(TBIDC("TBCheckBox"), WIDGET_INVOKE_INFO_NO_CALLBACKS); }
  211. };
  212. /** TBRadioButton is a button which unselects other radiobuttons of the same
  213. group number when clicked.
  214. For a labeled radio button, use a TBClickLabel containing a TBRadioButton. */
  215. class TBRadioButton : public TBRadioCheckBox
  216. {
  217. public:
  218. // For safe typecasting
  219. TBOBJECT_SUBCLASS(TBRadioButton, TBRadioCheckBox);
  220. TBRadioButton() { SetSkinBg(TBIDC("TBRadioButton"), WIDGET_INVOKE_INFO_NO_CALLBACKS); }
  221. };
  222. /** TBScrollBar is a scroll bar in the given axis. */
  223. class TBScrollBar : public TBWidget
  224. {
  225. public:
  226. // For safe typecasting
  227. TBOBJECT_SUBCLASS(TBScrollBar, TBWidget);
  228. TBScrollBar();
  229. ~TBScrollBar();
  230. /** Set along which axis the scrollbar should scroll */
  231. virtual void SetAxis(AXIS axis);
  232. virtual AXIS GetAxis() const { return m_axis; }
  233. /** Set the min, max limits for the scrollbar.
  234. The visible parameter is how much of the range that is visible.
  235. When this is called, the scrollbar might change value and invoke if the current value is
  236. outside of the new limits. */
  237. void SetLimits(double min, double max, double visible);
  238. /** Return true if the scrollbar has anywhere to go with the current limits. */
  239. bool CanScroll() const { return m_visible > 0; }
  240. /** Return true if the scrollbar can scroll in the positive direction with its current limits. */
  241. bool CanScrollPositive() const { return m_value < m_max; }
  242. /** Return true if the scrollbar can scroll in the negative direction with its current limits. */
  243. bool CanScrollNegative() const { return m_value > m_min; }
  244. double GetMinValue() const { return m_min; }
  245. double GetMaxValue() const { return m_max; }
  246. double GetVisible() const { return m_visible; }
  247. /** Same as SetValue, but with double precision. */
  248. virtual void SetValueDouble(double value);
  249. virtual double GetValueDouble() { return m_value; }
  250. virtual void SetValue(int value) { SetValueDouble(value); }
  251. virtual int GetValue() { return (int) GetValueDouble(); }
  252. virtual void OnInflate(const INFLATE_INFO &info);
  253. virtual bool OnEvent(const TBWidgetEvent &ev);
  254. virtual void OnResized(int old_w, int old_h);
  255. protected:
  256. TBWidget m_handle;
  257. AXIS m_axis;
  258. double m_value;
  259. double m_min, m_max, m_visible;
  260. double m_to_pixel_factor;
  261. void UpdateHandle();
  262. };
  263. /** TBSlider is a horizontal or vertical slider for a number within a range. */
  264. // FIX: Add a "track value" showing as a line within the track (to be used for buffering etc).
  265. // FIX: Also add a auto track that keeps it up to date with value (default).
  266. class TBSlider : public TBWidget
  267. {
  268. public:
  269. // For safe typecasting
  270. TBOBJECT_SUBCLASS(TBSlider, TBWidget);
  271. TBSlider();
  272. ~TBSlider();
  273. /** Set along which axis the scrollbar should scroll */
  274. virtual void SetAxis(AXIS axis);
  275. virtual AXIS GetAxis() const { return m_axis; }
  276. /** Set the min, max limits for the slider. */
  277. void SetLimits(double min, double max);
  278. double GetMinValue() const { return m_min; }
  279. double GetMaxValue() const { return m_max; }
  280. /** Get a small value (depending on the min and max limits) for stepping by f.ex. keyboard. */
  281. double GetSmallStep() const { return (m_max - m_min) / 100.0; }
  282. /** Same as SetValue, but with double precision. */
  283. virtual void SetValueDouble(double value);
  284. virtual double GetValueDouble() { return m_value; }
  285. virtual void SetValue(int value) { SetValueDouble(value); }
  286. virtual int GetValue() { return (int) GetValueDouble(); }
  287. virtual void OnInflate(const INFLATE_INFO &info);
  288. virtual bool OnEvent(const TBWidgetEvent &ev);
  289. virtual void OnResized(int old_w, int old_h);
  290. protected:
  291. TBWidget m_handle;
  292. AXIS m_axis;
  293. double m_value;
  294. double m_min, m_max;
  295. double m_to_pixel_factor;
  296. void UpdateHandle();
  297. };
  298. /** TBContainer is just a TBWidget with border and padding (using skin "TBContainer") */
  299. class TBContainer : public TBWidget
  300. {
  301. public:
  302. // For safe typecasting
  303. TBOBJECT_SUBCLASS(TBContainer, TBWidget);
  304. TBContainer();
  305. };
  306. /** TBMover is moving its parent widget when dragged. */
  307. class TBMover : public TBWidget
  308. {
  309. public:
  310. // For safe typecasting
  311. TBOBJECT_SUBCLASS(TBMover, TBWidget);
  312. TBMover();
  313. virtual bool OnEvent(const TBWidgetEvent &ev);
  314. };
  315. /** TBResizer is a lower right corner resize grip. It will resize its parent widget. */
  316. class TBResizer : public TBWidget
  317. {
  318. public:
  319. // For safe typecasting
  320. TBOBJECT_SUBCLASS(TBResizer, TBWidget);
  321. TBResizer();
  322. virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y);
  323. virtual bool OnEvent(const TBWidgetEvent &ev);
  324. };
  325. /** TBDimmer dim widgets in the background and block input. */
  326. class TBDimmer : public TBWidget
  327. {
  328. public:
  329. // For safe typecasting
  330. TBOBJECT_SUBCLASS(TBDimmer, TBWidget);
  331. TBDimmer();
  332. virtual void OnAdded();
  333. };
  334. }; // namespace tb
  335. #endif // TB_WIDGETS_COMMON_H