guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gtext.hpp
1 //
2 // Text Prop.
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_Text_hpp
9 #define Guit_Text_hpp
10 #include <gvarprop.hpp>
11 namespace guit {
12 
14 class GText : public GVarProp_<GString> {
15 public:
16  using Super = GVarProp_<GString>;
17 
18  GText();
19  GText(GString const& value);
20  GText(int value);
21  GText(float value);
22  GText(GText const&);
23  GText* clone(bool copy_value) const override;
24 
25  GText* toText() override {return this;}
26  bool isString() const override {return true;}
27 
28  GString stringValue() const override {return value_;}
29  float floatValue() const override;
30 
32  auto operator~() {return GPropRef_<GText>(*this);}
33 
36 
37  GText& operator=(GString const& str) {set(str,false); return *this;}
38  GText& operator=(GText const& text) {set(text(),false); return *this;}
39  GText& operator=(GInt const& num);
40  GText& operator=(GFloat const& num);
41 
42  GText& operator+=(GText const& text) {append(text.value_); return *this;}
43  GText& operator+=(GString const& str) {append(str); return *this;}
44  GText& operator+=(GInt const& num);
45  GText& operator+=(GFloat const& num);
46 
47  using Super::set;
48  void set(GString const& str) override {set(str, false);}
49  void set(GProp const&) override;
50 
53  char operator[](size_t pos) const;
54 
55  bool setByteAt(int bytepos, char ch); // tenir compte d'unicode !!!!
56 
57  bool empty() const;
58  size_t size() const;
59 
60  int compare(GString const& str) const noexcept;
61  int compare(GText const& txt) const noexcept {return compare(txt.value_);}
62 
63  size_t find(GString const& str, size_t pos = 0) const noexcept;
64  size_t rfind(GString const& str, size_t pos = GString::npos) const noexcept;
65 
66  bool append(GString const& str);
67  bool append(GText const& txt) {return append(txt.value_);}
68 
69  bool insert(size_t pos, GString const& str);
70  bool insert(size_t pos, GString const& str, size_t subpos, size_t sublen);
71  bool insert(size_t pos, GText const& t) {return insert(pos, t.value_);}
72 
73  bool erase(size_t pos = 0, size_t len = GString::npos);
74  GString substr(size_t pos = 0, size_t len = GString::npos) const;
75 
76  using GVarProp::read;
77  void read(std::istream&) override;
78 
80  GDim textSize(GFont&, bool richtext, bool multilines,
81  GTextAttributes const* = nullptr) const;
82 
87  ssize_t posToIndex(ssize_t charpos, bool richtext) const;
88  ssize_t indexToPos(ssize_t bytepos, bool richtext) const;
90 
94  ssize_t nextIndex(ssize_t bytepos, bool richtext) const;
95  ssize_t previousIndex(ssize_t bytepos, bool richtext) const;
97 
98  using GType = GPropType_<GText,GProp>;
99  static GType& Type();
100  GType& type() const override {return Type();}
101 
102  bool doChange(GString const& v) override;
103 
104 protected:
105  void apply(Gadget*, GRender&, Specif) override;
106  bool onAdd(Gadget*) override;
107  void onRemove(Gadget*) override;
108  void updateGadget(Gadget&) override;
109  virtual bool edit(GString const& newval);
110 };
111 
112 GString operator+(GString const& str, GText const& text);
113 GString operator+(GText const& text, GString const& str);
114 GString operator+(GText const& text, GInt& num);
115 GString operator+(GText const& text, GFloat& num);
116 
119 GText& Text();
120 GText& Text(GString const& value);
122 
123 
124 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125 
128 class GRegText : public GText {
129 public:
130  using Super = GText;
131 
132  GRegText(GString const& value = "");
133  GRegText(GRegText const&);
134  GRegText* clone(bool copy_value) const override;
135 
136  using GText::operator<<=;
137  GRegText& operator<<=(GExpr&);
138 
139  GRegText& operator=(GString const& val);
140  GRegText& operator=(const GText&);
141 
142  void setFormat(GString const& regex);
143  void setTimeFormat();
144  void setDateFormat();
145  void setNameFormat();
146  void setPhoneFormat();
147 
149  static GType& Type();
150  GType& type() const override {return Type();}
151 
152  bool isValid(GString const& v) const override;
153 
154 protected:
155  GString pattern_;
156 };
157 
160 GRegText& RegText();
161 GRegText& RegText(GString const& value);
163 
164 
165 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166 
168 class GTip : public GText {
169 public:
170  GTip(GString const& value = "") : GText(value) {}
171  GTip* clone(bool copy_value) const override;
172 
174  static GType& Type();
175  GType& type() const override {return Type();}
176 
177 protected:
178  void apply(Gadget*, GRender&, Specif) override;
179  void updateGadget(Gadget&) override {}
180  bool onAdd(Gadget*) override;
181  void onRemove(Gadget*) override;
182  void addModes(GadgetModes&) override;
183  void removeModes(GadgetModes&) override;
184 };
185 
188 GTip& Tip();
189 GTip& Tip(GString const& value);
191 
192 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
193 
195 class GTextData : public GText {
196 public:
197  GTextData(GString const& value = "") : GText(value) {}
198  GTextData* clone(bool copy_value) const override;
199 
201  static GType& Type();
202  GType& type() const override {return Type();}
203 
204 protected:
205  void apply(Gadget*, GRender&, Specif) override {}
206  bool onAdd(Gadget*) override {return true;}
207  void onRemove(Gadget*) override {}
208 };
209 
212 GTextData& TextData();
213 GTextData& TextData(GString const& value);
215 
216 }
217 #endif