UIWidget.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #pragma once
  2. #include <ThirdParty/TurboBadger/tb_widgets.h>
  3. #include <ThirdParty/TurboBadger/tb_widgets_common.h>
  4. #include "../Core/Object.h"
  5. #include "UIPreferredSize.h"
  6. #include "UIDragObject.h"
  7. namespace Atomic
  8. {
  9. /// Defines widget visibility, used with UIWidget::SetVisibility.
  10. enum UI_WIDGET_VISIBILITY
  11. {
  12. /// Visible (default)
  13. UI_WIDGET_VISIBILITY_VISIBLE = tb:: WIDGET_VISIBILITY_VISIBLE,
  14. /// Invisible, but layouted. Interaction disabled.
  15. UI_WIDGET_VISIBILITY_INVISIBLE = tb::WIDGET_VISIBILITY_INVISIBLE,
  16. /// Invisible and no layout. Interaction disabled.
  17. UI_WIDGET_VISIBILITY_GONE = tb::WIDGET_VISIBILITY_GONE
  18. };
  19. /// TBWidget gravity (may be combined).
  20. /// Gravity gives hints about positioning and sizing preferences.
  21. enum UI_GRAVITY {
  22. UI_GRAVITY_NONE = tb::WIDGET_GRAVITY_NONE,
  23. UI_GRAVITY_LEFT = tb::WIDGET_GRAVITY_LEFT,
  24. UI_GRAVITY_RIGHT = tb::WIDGET_GRAVITY_RIGHT,
  25. UI_GRAVITY_TOP = tb::WIDGET_GRAVITY_TOP,
  26. UI_GRAVITY_BOTTOM = tb::WIDGET_GRAVITY_BOTTOM,
  27. UI_GRAVITY_LEFT_RIGHT = tb::WIDGET_GRAVITY_LEFT_RIGHT,
  28. UI_GRAVITY_TOP_BOTTOM = tb::WIDGET_GRAVITY_TOP_BOTTOM,
  29. UI_GRAVITY_ALL = tb::WIDGET_GRAVITY_ALL,
  30. UI_GRAVITY_DEFAULT = tb::WIDGET_GRAVITY_DEFAULT
  31. };
  32. enum UI_EVENT_TYPE {
  33. /** Click event is what should be used to trig actions in almost all cases.
  34. It is invoked on a widget after POINTER_UP if the pointer is still inside
  35. its hit area. It can also be invoked by keyboard on some clickable widgets
  36. (see TBWidget::SetClickByKey).
  37. If panning of scrollable widgets start while the pointer is down, CLICK
  38. won't be invoked when releasing the pointer (since that should stop panning). */
  39. UI_EVENT_TYPE_CLICK = tb::EVENT_TYPE_CLICK,
  40. /** Long click event is sent when the pointer has been down for some time
  41. without moving much.
  42. It is invoked on a widget that has enabled it (TBWidget::SetWantLongClick
  43. If this event isn't handled, the widget will invoke a CONTEXT_MENU event.
  44. If any of those are handled, the CLICK event that would normally be
  45. invoked after the pending POINTER_UP will be suppressed. */
  46. UI_EVENT_TYPE_LONG_CLICK = tb::EVENT_TYPE_LONG_CLICK,
  47. UI_EVENT_TYPE_POINTER_DOWN = tb::EVENT_TYPE_POINTER_DOWN,
  48. UI_EVENT_TYPE_POINTER_UP = tb::EVENT_TYPE_POINTER_UP,
  49. UI_EVENT_TYPE_POINTER_MOVE = tb::EVENT_TYPE_POINTER_MOVE,
  50. UI_EVENT_TYPE_RIGHT_POINTER_DOWN = tb::EVENT_TYPE_RIGHT_POINTER_DOWN,
  51. UI_EVENT_TYPE_RIGHT_POINTER_UP = tb::EVENT_TYPE_RIGHT_POINTER_UP,
  52. UI_EVENT_TYPE_WHEEL = tb::EVENT_TYPE_WHEEL,
  53. /** Invoked after changing text in a TBTextField, changing selected item
  54. in a TBSelectList etc. Invoking this event trigs synchronization with
  55. connected TBWidgetValue and other widgets connected to it. */
  56. UI_EVENT_TYPE_CHANGED = tb::EVENT_TYPE_CHANGED,
  57. UI_EVENT_TYPE_KEY_DOWN = tb::EVENT_TYPE_KEY_DOWN,
  58. UI_EVENT_TYPE_KEY_UP = tb::EVENT_TYPE_KEY_UP,
  59. /** Invoked by the platform when a standard keyboard shortcut is pressed.
  60. It's called before InvokeKeyDown (EVENT_TYPE_KEY_DOWN) and if the event
  61. is handled (returns true), the KeyDown is canceled.
  62. The ref_id will be set to one of the following:
  63. "cut", "copy", "paste", "selectall", "undo", "redo", "new", "open", "save". */
  64. UI_EVENT_TYPE_SHORTCUT = tb::EVENT_TYPE_SHORTCUT,
  65. /** Invoked when a context menu should be opened at the event x and y coordinates.
  66. It may be invoked automatically for a widget on long click, if nothing handles
  67. the long click event. */
  68. UI_EVENT_TYPE_CONTEXT_MENU = tb::EVENT_TYPE_CONTEXT_MENU,
  69. /** Invoked by the platform when one or multiple files has been dropped on
  70. the widget. The event is guaranteed to be a TBWidgetEventFileDrop. */
  71. UI_EVENT_TYPE_FILE_DROP = tb::EVENT_TYPE_FILE_DROP,
  72. /** Invoked by the platform when a tab container's tab changed */
  73. UI_EVENT_TYPE_TAB_CHANGED = tb::EVENT_TYPE_TAB_CHANGED,
  74. /** Custom event. Not used internally. ref_id may be used for additional type info. */
  75. UI_EVENT_TYPE_CUSTOM = tb::EVENT_TYPE_CUSTOM
  76. };
  77. /** Defines widget z level relative to another widget, used with TBWidget::AddChildRelative. */
  78. enum UI_WIDGET_Z_REL {
  79. UI_WIDGET_Z_REL_BEFORE = tb::WIDGET_Z_REL_BEFORE, ///< Before the reference widget (visually behind reference).
  80. UI_WIDGET_Z_REL_AFTER = tb::WIDGET_Z_REL_AFTER ///< After the reference widget (visually above reference).
  81. };
  82. /// TB_TEXT_ALIGN specifies horizontal text alignment
  83. enum UI_TEXT_ALIGN
  84. {
  85. UI_TEXT_ALIGN_LEFT = tb::TB_TEXT_ALIGN_LEFT,
  86. UI_TEXT_ALIGN_RIGHT = tb::TB_TEXT_ALIGN_RIGHT,
  87. UI_TEXT_ALIGN_CENTER = tb::TB_TEXT_ALIGN_CENTER
  88. };
  89. class UIView;
  90. class UILayoutParams;
  91. class UIFontDescription;
  92. /// Wraps a TurboBadger widget in our Object model
  93. class UIWidget : public Object, public tb::TBWidgetDelegate
  94. {
  95. friend class UI;
  96. OBJECT(UIWidget)
  97. public:
  98. UIWidget(Context* context, bool createWidget = true);
  99. virtual ~UIWidget();
  100. bool Load(const String& filename);
  101. const String& GetId();
  102. UIWidget* GetParent();
  103. UIWidget* GetContentRoot();
  104. IntRect GetRect();
  105. UIPreferredSize* GetPreferredSize();
  106. String GetText();
  107. void SetRect(IntRect r);
  108. void SetSize(int width, int height);
  109. void SetPosition(int x, int y);
  110. void SetText(const String& text);
  111. void SetSkinBg(const String& id);
  112. void SetLayoutParams(UILayoutParams* params);
  113. void SetFontDescription(UIFontDescription* fd);
  114. void RemoveChild(UIWidget* child, bool cleanup = true);
  115. void DeleteAllChildren();
  116. // String ID
  117. virtual void SetId(const String& id);
  118. void Center();
  119. void SetGravity(UI_GRAVITY gravity);
  120. void SetValue(double value);
  121. double GetValue();
  122. void SetFocus();
  123. bool GetFocus();
  124. /// Set focus to first widget which accepts it
  125. void SetFocusRecursive();
  126. void OnFocusChanged(bool focused);
  127. void SetState(/*WIDGET_STATE*/ unsigned state, bool on);
  128. bool GetState(/*WIDGET_STATE*/ unsigned state);
  129. void SetVisibility(UI_WIDGET_VISIBILITY visibility);
  130. UI_WIDGET_VISIBILITY GetVisibility();
  131. void SetStateRaw(/*WIDGET_STATE*/ unsigned state);
  132. /*WIDGET_STATE*/ unsigned GetStateRaw();
  133. void Invalidate();
  134. void Die();
  135. void SetDragObject(UIDragObject* object) { dragObject_ = object; }
  136. UIDragObject* GetDragObject() { return dragObject_; }
  137. UIWidget* GetFirstChild();
  138. UIWidget* GetNext();
  139. bool IsAncestorOf(UIWidget* widget);
  140. void SetIsFocusable(bool value);
  141. // get this or child widget with id
  142. UIWidget* GetWidget(const String& id);
  143. UIView* GetView();
  144. virtual void AddChild(UIWidget* child);
  145. /// Add the child to this widget. See AddChild for adding a child to the top or bottom.
  146. /// This takes a relative Z and insert the child before or after the given reference widget.
  147. void AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference);
  148. tb::TBWidget* GetInternalWidget() { return widget_; }
  149. protected:
  150. void ConvertEvent(UIWidget* handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data);
  151. void SetWidget(tb::TBWidget* widget);
  152. virtual bool OnEvent(const tb::TBWidgetEvent &ev);
  153. virtual void OnDelete();
  154. String id_;
  155. tb::TBWidget* widget_;
  156. SharedPtr<UIPreferredSize> preferredSize_;
  157. SharedPtr<UIDragObject> dragObject_;
  158. };
  159. }