tb_editfield.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_EDITFIELD_H
  6. #define TB_EDITFIELD_H
  7. #include "tb_widgets_common.h"
  8. #include "tb_msg.h"
  9. #include "tb_style_edit.h"
  10. namespace tb {
  11. /** EDIT_TYPE - These types does not restrict input (may change in the future).
  12. They are just hints for virtual keyboard, so it can show special keys. */
  13. enum EDIT_TYPE {
  14. EDIT_TYPE_TEXT,
  15. EDIT_TYPE_SEARCH,
  16. EDIT_TYPE_PASSWORD,
  17. EDIT_TYPE_EMAIL,
  18. EDIT_TYPE_PHONE,
  19. EDIT_TYPE_URL,
  20. EDIT_TYPE_NUMBER
  21. };
  22. /** The default content factory for embedded content in TBEditField with styling enabled.
  23. Creates all that TBTextFragmentContentFactory creates by default,
  24. and any type of widget from a inline resource string.
  25. Syntax: <widget xxx> Where xxx is parsed by TBWidgetsReader.
  26. Example - Create a button with id "hello":
  27. <widget TBButton: text: "Hello world!" id: "hello">
  28. Example - Create a image from skin element "Icon48":
  29. <widget TBSkinImage: skin: "Icon48">
  30. */
  31. class TBEditFieldContentFactory : public TBTextFragmentContentFactory
  32. {
  33. public:
  34. class TBEditField *editfield;
  35. virtual int GetContent(const char *text);
  36. virtual TBTextFragmentContent *CreateFragmentContent(const char *text, int text_len);
  37. };
  38. /** TBEditFieldScrollRoot - Internal for TBEditField.
  39. Acts as a scrollable container for any widget created as embedded content. */
  40. class TBEditFieldScrollRoot : public TBWidget
  41. {
  42. private: // May only be used by TBEditField.
  43. friend class TBEditField;
  44. TBEditFieldScrollRoot() {}
  45. public:
  46. virtual void OnPaintChildren(const PaintProps &paint_props);
  47. virtual void GetChildTranslation(int &x, int &y) const;
  48. virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y);
  49. };
  50. /** TBEditField is a one line or multi line textfield that is editable or
  51. read-only. It can also be a passwordfield by calling
  52. SetEditType(EDIT_TYPE_PASSWORD).
  53. It may perform styling of text and contain custom embedded content,
  54. if enabled by SetStyling(true). Disabled by default.
  55. */
  56. class TBEditField : public TBWidget, private TBStyleEditListener, public TBMessageHandler
  57. {
  58. public:
  59. // For safe typecasting
  60. TBOBJECT_SUBCLASS(TBEditField, TBWidget);
  61. TBEditField();
  62. ~TBEditField();
  63. /** Get the visible rect (the GetPaddingRect() minus any scrollbars) */
  64. TBRect GetVisibleRect();
  65. /** Set if multiple lines should be allowed or not.
  66. Will also set wrapping (to true if multiline, and false if not). */
  67. void SetMultiline(bool multiline);
  68. bool GetMultiline() const { return m_style_edit.packed.multiline_on; }
  69. /** Set if styling should be enabled or not. Default is disabled. */
  70. void SetStyling(bool styling);
  71. bool GetStyling() const { return m_style_edit.packed.styling_on; }
  72. /** Set if read only mode should be enabled. Default is disabled.
  73. In read only mode, editing is disabled and caret is hidden.
  74. The user is still able to focus, select and copy text. */
  75. void SetReadOnly(bool readonly);
  76. bool GetReadOnly() const { return m_style_edit.packed.read_only; }
  77. /** Set to true if the text should wrap if multi line is enabled (See SetMultiline). */
  78. void SetWrapping(bool wrapping);
  79. bool GetWrapping() const { return m_style_edit.packed.wrapping; }
  80. /** Set to true if the preferred size of this editfield should adapt to the
  81. size of the content (disabled by default).
  82. If wrapping is enabled, the result is partly dependant on the virtual
  83. width (See SetVirtualWidth). */
  84. void SetAdaptToContentSize(bool adapt);
  85. bool GetAdaptToContentSize() const { return m_adapt_to_content_size; }
  86. /** The virtual width is only used if the size is adapting to content size
  87. (See SetAdaptToContentSize) and wrapping is enabled.
  88. The virtual width will be used to layout the text and see which resulting
  89. width and height it takes up. The width that is actually used depends on
  90. the content. It is also up to the the layouter to decide if the size
  91. should be respected or not. The default is 250. */
  92. void SetVirtualWidth(int virtual_width);
  93. int GetVirtualWidth() const { return m_virtual_width; }
  94. /** Get the TBStyleEdit object that contains more functions and settings. */
  95. TBStyleEdit *GetStyleEdit() { return &m_style_edit; }
  96. /** Set the edit type that is a hint for virtual keyboards about what the
  97. content should be. */
  98. void SetEditType(EDIT_TYPE type);
  99. EDIT_TYPE GetEditType() { return m_edit_type; }
  100. /** Support custom skin condition properties. Currently supported properties are:
  101. "edit-type", matching those of EDIT_TYPE.
  102. "multiline", matching 1 if multiline mode is enabled.
  103. "readonly", matching 1 if readonly mode is enabled. */
  104. virtual bool GetCustomSkinCondition(const TBSkinCondition::CONDITION_INFO &info);
  105. /** Set which alignment the text should have if the space
  106. given when painting is larger than the text.
  107. This changes the default for new blocks, as wel as the currently selected blocks or the block
  108. of the current caret position if nothing is selected. */
  109. void SetTextAlign(TB_TEXT_ALIGN align) { m_style_edit.SetAlign(align); }
  110. TB_TEXT_ALIGN GetTextAlign() { return m_style_edit.align; }
  111. void AppendText(const char *text, int32 len = TB_ALL_TO_TERMINATION, bool clear_undo_redo = false) { m_style_edit.AppendText(text, len, clear_undo_redo); }
  112. virtual bool SetText(const char *text) { return m_style_edit.SetText(text, TB_CARET_POS_BEGINNING); }
  113. virtual bool GetText(TBStr &text) { return m_style_edit.GetText(text); }
  114. using TBWidget::GetText; ///< Make all versions in base class available.
  115. using TBWidget::Invalidate; ///< Make Invalidate in base class available.
  116. /** Set the text and also specify if the caret should be positioned at the beginning
  117. or end of the text. */
  118. bool SetText(const char *text, TB_CARET_POS pos)
  119. { return m_style_edit.SetText(text, pos); }
  120. /** Set the text of the given length and also specify if the caret should be positioned
  121. at the beginning or end of the text. */
  122. bool SetText(const char *text, int text_len, TB_CARET_POS pos = TB_CARET_POS_BEGINNING)
  123. { return m_style_edit.SetText(text, text_len, pos); }
  124. /** Set the placeholder text. It will be visible only when the textfield is empty. */
  125. virtual bool SetPlaceholderText(const char *text) { return m_placeholder.SetText(text); }
  126. virtual bool GetPlaceholderText(TBStr &text) { return m_placeholder.GetText(text); }
  127. virtual void ScrollTo(int x, int y);
  128. virtual TBWidget::ScrollInfo GetScrollInfo();
  129. virtual TBWidget *GetScrollRoot() { return &m_root; }
  130. virtual bool OnEvent(const TBWidgetEvent &ev);
  131. virtual void OnPaint(const PaintProps &paint_props);
  132. virtual void OnPaintChildren(const PaintProps &paint_props);
  133. virtual void OnInflate(const INFLATE_INFO &info);
  134. virtual void OnAdded();
  135. virtual void OnFontChanged();
  136. virtual void OnFocusChanged(bool focused);
  137. virtual void OnResized(int old_w, int old_h);
  138. virtual TBWidget *GetContentRoot() { return &m_root; }
  139. virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints);
  140. void Reformat(bool update_fragments) { m_style_edit.Reformat(update_fragments); }
  141. virtual void OnMessageReceived(TBMessage *msg);
  142. private:
  143. TBScrollBar m_scrollbar_x;
  144. TBScrollBar m_scrollbar_y;
  145. TBWidgetString m_placeholder;
  146. EDIT_TYPE m_edit_type;
  147. TBEditFieldScrollRoot m_root;
  148. TBEditFieldContentFactory m_content_factory;
  149. TBStyleEdit m_style_edit;
  150. bool m_adapt_to_content_size;
  151. int m_virtual_width;
  152. void UpdateScrollbarVisibility(bool multiline);
  153. TBStr m_initial_edit_text;
  154. // == TBStyleEditListener =======================
  155. virtual void OnChange();
  156. virtual bool OnEnter();
  157. virtual void Invalidate(const TBRect &rect);
  158. virtual void DrawString(int32 x, int32 y, TBFontFace *font, const TBColor &color, const char *str, int32 len);
  159. virtual void DrawRect(const TBRect &rect, const TBColor &color);
  160. virtual void DrawRectFill(const TBRect &rect, const TBColor &color);
  161. virtual void DrawTextSelectionBg(const TBRect &rect);
  162. virtual void DrawContentSelectionFg(const TBRect &rect);
  163. virtual void DrawCaret(const TBRect &rect);
  164. virtual void Scroll(int32 dx, int32 dy);
  165. virtual void UpdateScrollbars();
  166. virtual void CaretBlinkStart();
  167. virtual void CaretBlinkStop();
  168. };
  169. }; // namespace tb
  170. #endif // TB_EDITFIELD_H