guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gtextitem.hpp
1 //
2 // Text Gadgets: textfield and textarea
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_TextItem_hpp
9 #define Guit_TextItem_hpp
10 #include <gitem.hpp>
11 #include <gtext.hpp>
12 #include <gfont.hpp>
13 namespace guit {
14 
38 class GTextItem : public GItem {
39 protected:
40  GTextItem(GadgetType&, GString const& css_and_value);
41 
42 public:
43  ~GTextItem();
44  GTextItem(GString const& css_and_text = "");
45  GTextItem& clone(GClone const&) override;
46 
47  GTextItem* toTextItem() override {return this;}
48  using GItem::add;
49 
52  GTextItem& operator=(GString const& str) override;
53  GTextItem& operator=(float) override;
54  void setValue(GString const& str) override;
55  void add(GString const&) override;
57 
60  GString text() const override;
61  //GString const& value() const;
62  GString stringValue() const override;
64 
67  virtual int intValue() const;
68  virtual float floatValue() const;
70 
78  GText& operator()();
79  GProp* valueProp() override {return value_;}
81 
85  GTextAttributes* getAttributes() {return attrlist_;}
87 
91  int caretColumn();
92  void caretRowColumn(int& row, int& column);
94 
98  void setCaretColumn(int column);
99  void setCaretRowColumn(int row, int column);
101 
106  ssize_t caretIndex() const {return ctrl_.caret;}
107  void setCaretIndex(ssize_t index);
109 
114  ssize_t posToIndex(ssize_t charpos) const;
115  ssize_t indexToPos(ssize_t index) const;
117 
118  void setCaret(GPoint const& wpos);
119  ssize_t findCaretIndex(GPoint const& wpos);
120  void findCaretRowColumn(ssize_t index, int& line, int& column);
121  void moveCaret(GPoint const& wpos);
122  void moveCaretRight(ssize_t count, bool scroll_one);
123  void moveCaretLeft();
124  void moveCaretUp();
125  void moveCaretDown();
126  void insertText(GString const& text);
127  void deleteChar(bool before_caret);
128  void deleteEnd();
129  bool deleteSelection();
130  void copyOrCut(bool cut);
131  void paste();
132 
133  bool hasValue() const override {return true;}
134 
135  using GType = GadgetType_<GTextItem,GItem>;
136  static GType& Type();
137  GType& type() const override {return Type();}
138 
141  struct Ctrl {
142  ssize_t offset, caret, selbegin, selend;
143  } ctrl_{};
144  gptr<GText> value_;
145  GFontValue font_;
146  const class GMarginValue* margin_{};
147  GTextAttributes* attrlist_{};
148  void addProp(GProp&, bool add_to_proplist) override;
149  void pack(GRender* parent_render) override;
150  ssize_t findCaretIndexInRow(ssize_t begin, ssize_t end, float caretX);
151  bool keyCB(class GKeyEvent&);
152  void selectAllTextCB();
154 };
155 
157 GTextItem& TextItem(GString const& css_and_text = "");
158 
159 
160 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161 
162 class GTextField : public GTextItem {
163 public:
164  GTextField(GString const& css_and_text = "");
165  GTextField& clone(GClone const&) override;
166 
167  using GTextItem::operator=;
168 
169  using GType = GadgetType_<GTextField,GTextItem>;
170  static GType& Type();
171  GType& type() const override {return Type();}
172 };
173 
175 GTextField& TextField(GString const& css_and_text = "");
176 
177 
178 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179 
180 class GTextArea : public GTextItem {
181 public:
182  GTextArea(GString const& css_and_text = "");
183  GTextArea& clone(GClone const&) override;
184 
185  using GTextItem::operator=;
186 
187  using GType = GadgetType_<GTextArea,GTextItem>;
188  static GType& Type();
189  GType& type() const override {return Type();}
190 };
191 
193 GTextArea& TextArea(GString const& css_and_text = "");
194 
195 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196 
198 class GTextAttr {
199 public:
200  GTextAttr(size_t pos_in_text, size_t length);
201 
202  size_t pos_{}, len_{};
203  GPropList props_;
204  gptr<GObject> data_;
205 };
206 
207 
210 public:
211  GTextAttributes() {list_.clear();}
212  void add(GTextAttr&);
213  void clear();
214  GTextAttr* find(size_t pos) const;
215  GTextAttr* find(GObject* data) const;
216  void adjust(size_t pos, ssize_t delta);
217 
218  std::vector<GTextAttr*> list_;
219 };
220 
221 }
222 #endif