xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
text_box_base.h
Go to the documentation of this file.
1 #pragma once
5 #include "control.h"
6 #include <xtd/bit_converter.h>
8 
10 namespace xtd {
12  namespace forms {
20  class text_box_base : public control {
21  public:
24  virtual bool accepts_tab() const {return accepts_tab_;}
28  virtual text_box_base& accepts_tab(bool value) {
29  if (accepts_tab_ != value) {
30  accepts_tab_ = value;
33  }
34  return *this;
35  }
36 
39  virtual forms::border_style border_style() const {return border_style_;}
44  if (border_style_ != border_style) {
45  border_style_ = border_style;
48  }
49  return *this;
50  }
51 
52 
53  forms::cursor default_cursor() const override {return forms::cursors::ibeam();}
54 
58  std::vector<xtd::ustring> lines() const {
59  return text().split({'\n'});
60  }
65  text_box_base& lines(const std::vector<xtd::ustring>& lines) {
66  text(xtd::ustring::join("\n", lines));
67  return *this;
68  }
69 
73  virtual bool multiline() const {return multiline_;}
78  virtual text_box_base& multiline(bool value) {
79  if (multiline_ != value) {
80  multiline_ = value;
83  }
84  return *this;
85  }
86 
90  virtual bool read_only() const {return read_only_;}
95  virtual text_box_base& read_only(bool value) {
96  if (read_only_ != value) {
97  read_only_ = value;
100  }
101  return *this;
102  }
103 
107  virtual size_t selection_length() const {return selection_length_;}
112  virtual text_box_base& selection_length(size_t value) {
113  select(selection_start_, value);
114  return *this;
115  }
116 
120  virtual size_t selection_start() const {return selection_start_;}
125  virtual text_box_base& selection_start(size_t value) {
126  select(value, selection_length_);
127  return *this;
128  }
129 
132  virtual bool word_wrap() const {return word_wrap_;}
136  virtual text_box_base& word_wrap(bool value) {
137  if (word_wrap_ != value) {
138  word_wrap_ = value;
139  recreate_handle();
140  }
141  return *this;
142  }
143 
146  virtual void append_text(const xtd::ustring& value) {
147  suspend_layout();
148  text(text() + value);
149  select(text().size(), 0);
150  resume_layout();
151  }
152 
155  void clear() {
156  text("");
157  }
158 
163  virtual void select(size_t start, size_t length) {
164  if (selection_start_ != start || length != selection_length_) {
165  if (start > text().size()) throw argument_out_of_range_exception("start greater than text size"_t, current_stack_frame_);
166  if (start + length > text().size()) throw argument_out_of_range_exception("start + length greater than text size"_t, current_stack_frame_);
167  selection_start_ = start;
168  selection_length_ = length;
169  }
170  }
171 
174  void select_all() {
175  select(0, text().size());
176  }
177 
181 
185 
189 
193 
194  protected:
196  text_box_base() = default;
197 
200  virtual void on_accepts_tab_changed(const event_args& e) {
201  if (can_raise_events()) accepts_tab_changed(*this, e);
202  }
203 
206  virtual void on_border_style_changed(const event_args& e) {
207  if (can_raise_events()) border_style_changed(*this, e);
208  }
209 
212  virtual void on_multiline_changed(const event_args& e) {
213  if (can_raise_events()) multiline_changed(*this, e);
214  }
215 
218  virtual void on_read_only_changed(const event_args& e) {
219  if (can_raise_events()) read_only_changed(*this, e);
220  }
221 
223  bool accepts_tab_ = false;
225  bool multiline_ = false;
226  bool read_only_ = false;
227  bool word_wrap_ = true;
228  mutable size_t selection_start_ = 0;
229  mutable size_t selection_length_ = 0;
231  };
232  }
233 }
virtual text_box_base & multiline(bool value)
Sets a value indicating whether this is a multiline text box control.
Definition: text_box_base.h:78
virtual bool accepts_tab() const
Gets a value indicating whether pressing the TAB key in a multiline text box control types a TAB char...
Definition: text_box_base.h:24
virtual void recreate_handle()
Forces the re-creation of the handle for the control.
Contains xtd::argument_out_of_range_exception exception.
event< text_box_base, event_handler > border_style_changed
Occurs when the value of the accepts_tab border_style has changed.
Definition: text_box_base.h:184
std::vector< xtd::ustring > lines() const
Gets the lines of text in a text box control.
Definition: text_box_base.h:58
virtual void on_accepts_tab_changed(const event_args &e)
Raises the text_box_base::accepts_tab_changed event.
Definition: text_box_base.h:200
virtual const drawing::size & size() const
Gets the height and width of the control.
Definition: control.h:648
event< text_box_base, event_handler > multiline_changed
Occurs when the value of the accepts_tab border_style has changed.
Definition: text_box_base.h:188
event< text_box_base, event_handler > read_only_changed
Occurs when the value of the read_only property has changed.
Definition: text_box_base.h:192
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
static const event_args empty
Provides a value to use with events that do not have event data.
Definition: event_args.h:31
virtual text_box_base & selection_start(size_t value)
Gets o the starting point of text selected in the text box.
Definition: text_box_base.h:125
virtual size_t selection_start() const
Gets o the starting point of text selected in the text box.
Definition: text_box_base.h:120
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
text_box_base & lines(const std::vector< xtd::ustring > &lines)
Sets the lines of text in a text box control.
Definition: text_box_base.h:65
virtual bool word_wrap() const
Indicates whether a multiline text box control automatically wraps words to the beginning of the next...
Definition: text_box_base.h:132
void clear()
Clears all text from the text box control.
Definition: text_box_base.h:155
Implements the basic functionality required by text controls.
Definition: text_box_base.h:20
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Represents the image used to paint the mouse pointer.
Definition: cursor.h:35
#define current_stack_frame_
Provides information about the current stack frame.
Definition: stack_frame.h:201
virtual text_box_base & read_only(bool value)
Sets a value indicating whether text in the text box is read-only.
Definition: text_box_base.h:95
virtual void select(size_t start, size_t length)
Selects a range of text in the text box.
Definition: text_box_base.h:163
virtual text_box_base & accepts_tab(bool value)
Sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB char...
Definition: text_box_base.h:28
virtual size_t selection_length() const
Gets the number of characters selected in the text box.
Definition: text_box_base.h:107
virtual text_box_base & border_style(forms::border_style border_style)
Sets the border type of the text box control.
Definition: text_box_base.h:43
forms::cursor default_cursor() const override
Gets the default cursor for the control.
Definition: text_box_base.h:53
virtual text_box_base & selection_length(size_t value)
Sets the number of characters selected in the text box.
Definition: text_box_base.h:112
Contains xtd::bit_converter class.
void select_all()
Selects all text in the text box.
Definition: text_box_base.h:174
bool can_raise_events() const override
Determines if events can be raised on the control.
Definition: control.h:332
Represents an event.
Definition: event.h:21
virtual text_box_base & word_wrap(bool value)
Indicates whether a multiline text box control automatically wraps words to the beginning of the next...
Definition: text_box_base.h:136
std::vector< ustring > split(const std::vector< value_type > &separators, size_t count, string_split_options options) const noexcept
Splits this string into a maximum number of substrings based on the characters in an array...
virtual bool multiline() const
Gets a value indicating whether this is a multiline text box control.
Definition: text_box_base.h:73
virtual void append_text(const xtd::ustring &value)
Appends text to the current text of a text box.
Definition: text_box_base.h:146
virtual forms::border_style border_style() const
Gets the border type of the text box control.
Definition: text_box_base.h:39
static cursor ibeam()
Gets the I-beam cursor, which is used to show where the text cursor appears when the mouse is clicked...
border_style
Specifies the border style for a control.
Definition: border_style.h:18
virtual bool read_only() const
Gets a value indicating whether text in the text box is read-only.
Definition: text_box_base.h:90
The exception that is thrown when one of the arguments provided to a method is out of range...
Definition: argument_out_of_range_exception.h:18
virtual void on_read_only_changed(const event_args &e)
Raises the text_box_base::read_only_changed event.
Definition: text_box_base.h:218
Defines the base class for controls, which are components with visual representation.
Definition: control.h:68
virtual void on_multiline_changed(const event_args &e)
Raises the text_box_base::text_box_base::multiline_changed event.
Definition: text_box_base.h:212
event< text_box_base, event_handler > accepts_tab_changed
Occurs when the value of the accepts_tab property has changed.
Definition: text_box_base.h:180
Contains xtd::forms::control control.
virtual void on_border_style_changed(const event_args &e)
Raises the text_box_base::border_style_changed event.
Definition: text_box_base.h:206
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
void resume_layout()
Resumes usual layout logic.
Definition: control.h:983
void suspend_layout()
Temporarily suspends the layout logic for the control.
Definition: control.h:1032
virtual const xtd::ustring & text() const
Gets the text associated with this control.
Definition: control.h:678
text_box_base()=default
initializes a new instance of the text_box_base class.
static ustring join(const ustring separator, const collection_t &values) noexcept
Concatenates a specified separator string between each element of a specified object array...
Definition: ustring.h:849