tb_widgets_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // ATOMIC BEGIN
  105. /** Set the URL of the button */
  106. void SetURL(const char* url) { m_url = url; }
  107. const TBStr& GetURL() { return m_url; }
  108. // ATOMIC END
  109. protected:
  110. void UpdateTextFieldVisibility();
  111. class ButtonLayout : public TBLayout
  112. {
  113. virtual void OnChildAdded(TBWidget *child);
  114. virtual void OnChildRemove(TBWidget *child);
  115. };
  116. ButtonLayout m_layout;
  117. TBTextField m_textfield;
  118. bool m_auto_repeat_click;
  119. bool m_toggle_mode;
  120. // ATOMIC BEGIN
  121. TBStr m_url;
  122. // ATOMIC END
  123. };
  124. /** TBClickLabel has a text field in its internal layout by default. Pointer input on the
  125. text field will be redirected to another child widget (that you add) to it.
  126. Typically useful for creating check boxes, radio buttons with labels. */
  127. class TBClickLabel : public TBWidget
  128. {
  129. public:
  130. // For safe typecasting
  131. TBOBJECT_SUBCLASS(TBClickLabel, TBWidget);
  132. TBClickLabel();
  133. ~TBClickLabel();
  134. /** Set along which axis the content should layouted (If the label has more content than the text) */
  135. virtual void SetAxis(AXIS axis) { m_layout.SetAxis(axis); }
  136. virtual AXIS GetAxis() const { return m_layout.GetAxis(); }
  137. /** Set the text of the label. */
  138. virtual bool SetText(const char *text) { return m_textfield.SetText(text); }
  139. virtual bool GetText(TBStr &text) { return m_textfield.GetText(text); }
  140. using TBWidget::GetText; ///< Make all versions in base class available.
  141. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints) { return m_layout.GetPreferredSize(); }
  142. virtual TBWidget *GetContentRoot() { return &m_layout; }
  143. virtual bool OnEvent(const TBWidgetEvent &ev);
  144. protected:
  145. TBLayout m_layout;
  146. TBTextField m_textfield;
  147. };
  148. /** TBSkinImage is a widget showing a skin element, constrained in size to its skin.
  149. If you need to load and show images dynamically (i.e. not always loaded as the skin),
  150. you can use TBImageWidget. */
  151. class TBSkinImage : public TBWidget
  152. {
  153. public:
  154. // For safe typecasting
  155. TBOBJECT_SUBCLASS(TBSkinImage, TBWidget);
  156. TBSkinImage() {}
  157. TBSkinImage(const TBID &skin_bg) { SetSkinBg(skin_bg); }
  158. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  159. };
  160. /** TBSeparator is a widget only showing a skin.
  161. It is disabled by default. */
  162. class TBSeparator : public TBWidget
  163. {
  164. public:
  165. // For safe typecasting
  166. TBOBJECT_SUBCLASS(TBSeparator, TBWidget);
  167. TBSeparator();
  168. };
  169. /** TBProgressSpinner is a animation that is running while its value is 1.
  170. Typically used to indicate that the application is working. */
  171. class TBProgressSpinner : public TBWidget, protected TBMessageHandler
  172. {
  173. public:
  174. // For safe typecasting
  175. TBOBJECT_SUBCLASS(TBProgressSpinner, TBWidget);
  176. TBProgressSpinner();
  177. /** Return true if the animation is running. */
  178. bool IsRunning() { return m_value > 0; }
  179. /** Begin/End are used to start or stop the animation in a incremental way.
  180. If several tasks may activate the same spinner, calling Begin/End instead
  181. of using SetValue, so it will keep running as long as any source wants it to. */
  182. void Begin() { SetValue(GetValue() + 1); }
  183. void End() { SetValue(GetValue() - 1); }
  184. /** Setting the value to 1 will start the spinner.
  185. Setting it to 0 will stop it. */
  186. virtual void SetValue(int value);
  187. virtual int GetValue() { return m_value; }
  188. virtual void OnPaint(const PaintProps &paint_props);
  189. // == TBMessageHandler ==============================================================
  190. virtual void OnMessageReceived(TBMessage *msg);
  191. protected:
  192. int m_value;
  193. int m_frame;
  194. TBID m_skin_fg;
  195. };
  196. /** TBRadioCheckBox has shared functionality for TBCheckBox and TBRadioButton. */
  197. class TBRadioCheckBox : public TBWidget
  198. {
  199. public:
  200. // For safe typecasting
  201. TBOBJECT_SUBCLASS(TBRadioCheckBox, TBWidget);
  202. TBRadioCheckBox();
  203. virtual void SetValue(int value);
  204. virtual int GetValue() { return m_value; }
  205. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  206. virtual bool OnEvent(const TBWidgetEvent &ev);
  207. protected:
  208. void ToggleGroup(TBWidget *root, TBWidget *toggled);
  209. int m_value;
  210. };
  211. /** TBCheckBox is a box toggling a check mark on click.
  212. For a labeled checkbox, use a TBClickLabel containing a TBCheckBox. */
  213. class TBCheckBox : public TBRadioCheckBox
  214. {
  215. public:
  216. // For safe typecasting
  217. TBOBJECT_SUBCLASS(TBCheckBox, TBRadioCheckBox);
  218. TBCheckBox() { SetSkinBg(TBIDC("TBCheckBox"), WIDGET_INVOKE_INFO_NO_CALLBACKS); }
  219. };
  220. /** TBRadioButton is a button which unselects other radiobuttons of the same
  221. group number when clicked.
  222. For a labeled radio button, use a TBClickLabel containing a TBRadioButton. */
  223. class TBRadioButton : public TBRadioCheckBox
  224. {
  225. public:
  226. // For safe typecasting
  227. TBOBJECT_SUBCLASS(TBRadioButton, TBRadioCheckBox);
  228. TBRadioButton() { SetSkinBg(TBIDC("TBRadioButton"), WIDGET_INVOKE_INFO_NO_CALLBACKS); }
  229. };
  230. /** TBScrollBar is a scroll bar in the given axis. */
  231. class TBScrollBar : public TBWidget
  232. {
  233. public:
  234. // For safe typecasting
  235. TBOBJECT_SUBCLASS(TBScrollBar, TBWidget);
  236. TBScrollBar();
  237. ~TBScrollBar();
  238. /** Set along which axis the scrollbar should scroll */
  239. virtual void SetAxis(AXIS axis);
  240. virtual AXIS GetAxis() const { return m_axis; }
  241. /** Set the min, max limits for the scrollbar.
  242. The visible parameter is how much of the range that is visible.
  243. When this is called, the scrollbar might change value and invoke if the current value is
  244. outside of the new limits. */
  245. void SetLimits(double min, double max, double visible);
  246. /** Return true if the scrollbar has anywhere to go with the current limits. */
  247. bool CanScroll() const { return m_visible > 0; }
  248. /** Return true if the scrollbar can scroll in the positive direction with its current limits. */
  249. bool CanScrollPositive() const { return m_value < m_max; }
  250. /** Return true if the scrollbar can scroll in the negative direction with its current limits. */
  251. bool CanScrollNegative() const { return m_value > m_min; }
  252. double GetMinValue() const { return m_min; }
  253. double GetMaxValue() const { return m_max; }
  254. double GetVisible() const { return m_visible; }
  255. /** Same as SetValue, but with double precision. */
  256. virtual void SetValueDouble(double value);
  257. virtual double GetValueDouble() { return m_value; }
  258. virtual void SetValue(int value) { SetValueDouble(value); }
  259. virtual int GetValue() { return (int) GetValueDouble(); }
  260. virtual void OnInflate(const INFLATE_INFO &info);
  261. virtual bool OnEvent(const TBWidgetEvent &ev);
  262. virtual void OnResized(int old_w, int old_h);
  263. protected:
  264. TBWidget m_handle;
  265. AXIS m_axis;
  266. double m_value;
  267. double m_min, m_max, m_visible;
  268. double m_to_pixel_factor;
  269. void UpdateHandle();
  270. };
  271. /** TBSlider is a horizontal or vertical slider for a number within a range. */
  272. // FIX: Add a "track value" showing as a line within the track (to be used for buffering etc).
  273. // FIX: Also add a auto track that keeps it up to date with value (default).
  274. class TBSlider : public TBWidget
  275. {
  276. public:
  277. // For safe typecasting
  278. TBOBJECT_SUBCLASS(TBSlider, TBWidget);
  279. TBSlider();
  280. ~TBSlider();
  281. /** Set along which axis the scrollbar should scroll */
  282. virtual void SetAxis(AXIS axis);
  283. virtual AXIS GetAxis() const { return m_axis; }
  284. /** Set the min, max limits for the slider. */
  285. void SetLimits(double min, double max);
  286. double GetMinValue() const { return m_min; }
  287. double GetMaxValue() const { return m_max; }
  288. /** Get a small value (depending on the min and max limits) for stepping by f.ex. keyboard. */
  289. double GetSmallStep() const { return (m_max - m_min) / 100.0; }
  290. /** Same as SetValue, but with double precision. */
  291. virtual void SetValueDouble(double value);
  292. virtual double GetValueDouble() { return m_value; }
  293. virtual void SetValue(int value) { SetValueDouble(value); }
  294. virtual int GetValue() { return (int) GetValueDouble(); }
  295. virtual void OnInflate(const INFLATE_INFO &info);
  296. virtual bool OnEvent(const TBWidgetEvent &ev);
  297. virtual void OnResized(int old_w, int old_h);
  298. protected:
  299. TBWidget m_handle;
  300. AXIS m_axis;
  301. double m_value;
  302. double m_min, m_max;
  303. double m_to_pixel_factor;
  304. void UpdateHandle();
  305. };
  306. /** TBContainer is just a TBWidget with border and padding (using skin "TBContainer") */
  307. class TBContainer : public TBWidget
  308. {
  309. public:
  310. // For safe typecasting
  311. TBOBJECT_SUBCLASS(TBContainer, TBWidget);
  312. TBContainer();
  313. };
  314. /** TBMover is moving its parent widget when dragged. */
  315. class TBMover : public TBWidget
  316. {
  317. public:
  318. // For safe typecasting
  319. TBOBJECT_SUBCLASS(TBMover, TBWidget);
  320. TBMover();
  321. virtual bool OnEvent(const TBWidgetEvent &ev);
  322. };
  323. /** TBResizer is a lower right corner resize grip. It will resize its parent widget. */
  324. class TBResizer : public TBWidget
  325. {
  326. public:
  327. // For safe typecasting
  328. TBOBJECT_SUBCLASS(TBResizer, TBWidget);
  329. TBResizer();
  330. virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y);
  331. virtual bool OnEvent(const TBWidgetEvent &ev);
  332. };
  333. /** TBDimmer dim widgets in the background and block input. */
  334. class TBDimmer : public TBWidget
  335. {
  336. public:
  337. // For safe typecasting
  338. TBOBJECT_SUBCLASS(TBDimmer, TBWidget);
  339. TBDimmer();
  340. virtual void OnAdded();
  341. };
  342. }; // namespace tb
  343. #endif // TB_WIDGETS_COMMON_H