SDL Window Engine  20200905
swe_termgui.h
1 /***************************************************************************
2  * Copyright (C) 2017 by SWE team <sdl.window.engine@gmail.com> *
3  * *
4  * Part of the SWE: SDL Window Engine: *
5  * https://github.com/AndreyBarmaley/sdl-window-engine *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #ifndef _SWE_TERMWIN_GUI_
24 #define _SWE_TERMWIN_GUI_
25 
26 #ifndef SWE_DISABLE_TERMGUI
27 
28 #include <array>
29 #include <memory>
30 #include "swe_termwin.h"
31 
32 #ifdef __clang__
33 #pragma clang diagnostic ignored "-Woverloaded-virtual"
34 #endif
35 
37 namespace SWE
38 {
40  namespace TermGUI
41  {
44  {
45  public:
46  virtual ~ThemeColors() {}
47 
48  enum type_t { ColorBackground = 0, ColorBorderLine, ColorHeaderText, ColorBodyText,
49  ColorButtonBackground, ColorButtonBracket, ColorButtonFirstText, ColorButtonBodyText,
50  ColorFocusedButtonBackground, ColorFocusedButtonBracket, ColorFocusedButtonFirstText, ColorFocusedButtonBodyText,
51  ColorInputFieldBackground, ColorFocusedInputFieldBackground, ColorInputFieldText, ColorInputCursor,
52  ColorListSelectedBackground, ColorListSelectedText };
53 
54 #define ColorsCounts (TermGUI::ThemeColors::ColorListSelectedText + 1)
55 
56  static void setColor(const type_t & type, const ColorIndex & col);
57  static void setTheme(const LineType &, std::array<ColorIndex, ColorsCounts>);
58  static void resetTheme(void);
59 
60 protected:
61  virtual LineType lineType(void) const;
62  virtual ColorIndex colorBackground(void) const;
63  virtual ColorIndex colorText(void) const;
64  virtual ColorIndex colorLine(void) const;
65  };
66 
68  class LabelAction : public TermWindow, public ThemeColors
69  {
70  UnicodeString content;
71  int hotkey;
72 
73  protected:
75 
76  bool keyPressEvent(const KeySym &) override;
77  void mouseFocusEvent(void) override;
78  void mouseLeaveEvent(void) override;
79  bool mouseClickEvent(const ButtonsEvent &) override;
80 
82  int parseHotKey(const UnicodeString &) const;
84  void clickAction(void);
86  void renderLabel(void);
87 
88  ColorIndex colorBackground(void) const override;
89  ColorIndex colorText(void) const override;
90 
91  FBColors defaultColors(void) const override
92  {
93  return FBColors(colorText(), colorBackground());
94  }
95 
97  virtual TermSize labelSize(const UnicodeString &) const;
98 
99  virtual ColorIndex colorBackground(bool focused) const;
100  virtual ColorIndex colorText(bool focused) const;
101  virtual ColorIndex colorHotKey(bool focused) const;
102 
103  public:
104  LabelAction(const UnicodeString &, int action, TermWindow &);
105  LabelAction(const UnicodeString &, int action, const TermPos &, TermWindow &);
106 
107  void setLabel(const UnicodeString &);
108  const UnicodeString & label(void) const;
109  bool isLabel(const UnicodeString &) const;
110 
111  void setAction(int);
112  int action(void) const;
113  bool isAction(int) const;
114 
115  void setHotKey(int);
116  int hotKey(void) const;
117  bool isHotKey(int) const;
118 
119  void setHotKeyDisabled(bool);
120  bool isHotKeyDisabled(void) const;
121 
122  void setDisabled(bool);
123  bool isDisabled(void) const;
124 
125  void setSelected(bool);
126  bool isSelected(void) const;
127 
128  void renderWindow(void) override;
129  const char* className(void) const override { return "SWE::TermGUI::LabelAction"; }
130  };
131 
133  enum buttons_t { ButtonOk = 1 << 1, ButtonYes = 1 << 2, ButtonNo = 1 << 3, ButtonCancel = 1 << 4 };
134 
136  class TextButton : public LabelAction
137  {
138  protected:
139  TextButton(TermWindow* term) : LabelAction(term) {}
140 
141  virtual ColorIndex colorBracket(bool focused) const;
142 
144  TermSize labelSize(const UnicodeString &) const override;
145 
146  public:
147  TextButton(const buttons_t &, TermWindow &);
148  TextButton(const UnicodeString &, int action, TermWindow &);
149  TextButton(const UnicodeString &, int action, const TermPos &, TermWindow &);
150 
151  void setButton(const buttons_t &);
152 
153  void renderWindow(void) override;
154  const char* className(void) const override { return "SWE::TermGUI::TextButton"; }
155  };
156 
157  /* LabelActionPtr */
158  struct LabelActionPtr : std::unique_ptr<LabelAction>
159  {
161  {
162  reset(ptr);
163  }
164 
165  LabelActionPtr(const std::string & str, int action, const TermPos & pos, TermWindow & term)
166  {
167  reset(new LabelAction(str, action, pos, term));
168  }
169  };
170 
172  class LabelActionGroup : public std::list<LabelActionPtr>
173  {
174  public:
175  LabelActionGroup() {}
176 
177  LabelAction* addLabel(const std::string &, int action, const TermPos &, TermWindow &);
178  LabelAction* addLabel(LabelAction*);
179 
180  bool findLabel(const LabelAction*) const;
181  LabelAction* findLabel(const UnicodeString &) const;
182  LabelAction* findAction(int) const;
183  LabelAction* findHotKey(int) const;
184  LabelAction* findSelected(void) const;
185  LabelAction* findIndex(size_t) const;
186 
187  bool setSelected(const LabelAction*);
188  void resetSelected(const LabelAction* exclude = nullptr);
189  void nextSelected(void);
190  void prevSelected(void);
191  void firstSelected(void);
192  void lastSelected(void);
193  bool isFirstSelected(void) const;
194  bool isLastSelected(void) const;
195 
196  int index(LabelAction*) const;
197  size_t count(void) const { return size(); }
198  size_t rows(void) const;
199  size_t cols(void) const;
200  };
201 
204  {
205  public:
206  ButtonsGroup(int buttons, TermWindow &);
207  };
208 
210  class HeaderAreaBox : public TermWindow, public ThemeColors
211  {
212  UnicodeString header;
213 
214  protected:
215  HeaderAreaBox(const UnicodeString & us, TermWindow & term);
216  HeaderAreaBox(const UnicodeString & us, const FontRender & frs, Window* win);
217 
218  bool keyPressEvent(const KeySym &) override;
219  LineType lineType(void) const override;
220 
221  FBColors defaultColors(void) const override
222  {
223  return FBColors(colorText(), colorBackground());
224  }
225 
226  virtual ColorIndex colorHeader(void) const;
227 
228  public:
229  HeaderAreaBox(const UnicodeString &, const TermSize &, TermWindow &);
230 
231  void renderBox(void);
232  void renderWindow(void) override;
233  const char* className(void) const override { return "SWE::TermGUI::HeaderAreaBox"; }
234  };
235 
238  {
239  ButtonsGroup btnsGroup;
240 
241  void initAreaBox(void);
242 
243  protected:
244  ButtonsAreaBox(const UnicodeString & us, int btns, TermWindow & term)
245  : HeaderAreaBox(us, term), btnsGroup(btns, *this)
246  {
247  initAreaBox();
248  }
249 
250  ButtonsAreaBox(const UnicodeString & us, int btns, const FontRender & frs, Window* win)
251  : HeaderAreaBox(us, frs, win), btnsGroup(btns, *this)
252  {
253  initAreaBox();
254  }
255 
256  void signalReceive(int, const SignalMember*) override;
257  bool keyPressEvent(const KeySym &) override;
258 
259  void setButtonsPosition(void);
260  void setHotKeyDisabled(bool);
261  void setButtonsSubscribe(int);
262 
263  public:
264  ButtonsAreaBox(const UnicodeString & str, const TermSize & tsz, int btns, TermWindow & term)
265  : HeaderAreaBox(str, tsz, term), btnsGroup(btns, *this)
266  {
267  initAreaBox();
268  }
269 
270  ButtonsGroup & buttonsGroup(void);
271  const ButtonsGroup &buttonsGroup(void) const;
272 
273  const char* className(void) const override { return "SWE::TermGUI::ButtonsAreaBox"; }
274 
275  void renderButtons(void);
276  void renderWindow(void) override;
277  };
278 
280  class MessageBox : protected UnicodeList, public ButtonsAreaBox
281  {
282  protected:
283 
284  public:
285  MessageBox(const UnicodeString &, const UnicodeString &, int buttons, TermWindow &);
286  MessageBox(const UnicodeString &, const UnicodeString &, int buttons, const FontRender &, Window*);
287 
288  const char* className(void) const override { return "SWE::TermGUI::MessageBox"; }
289  void renderWindow(void) override;
290  };
291 
293  class InputBox : public ButtonsAreaBox
294  {
295  std::string sResult;
296 
297  protected:
298  bool keyPressEvent(const KeySym &) override;
299 
300  void setInputFocused(bool);
301  bool checkInputFocused(void) const;
302 
303  virtual ColorIndex colorFieldText(void) const;
304  virtual ColorIndex colorFieldCursor(void) const;
305  virtual ColorIndex colorFieldBackground(bool focused) const;
306 
307  public:
308  InputBox(const UnicodeString &, size_t cols, const std::string & def, TermWindow &);
309  InputBox(const UnicodeString &, size_t cols, const std::string & def, const FontRender &, Window*);
310 
311  const std::string & result(void) const
312  {
313  return sResult;
314  }
315 
316  const char* className(void) const override { return "SWE::TermGUI::InputBox"; }
317  void renderWindow(void) override;
318  };
319 
321  class ListBox : public ButtonsAreaBox
322  {
323  UnicodeList content;
324  std::string sResult;
325  int selected;
326  int skipped;
327 
328  virtual ColorIndex colorBackground(bool selected) const;
329  virtual ColorIndex colorText(bool selected) const;
330 
331  protected:
332  bool keyPressEvent(const KeySym &) override;
333  bool mouseClickEvent(const ButtonsEvent &) override;
334  bool scrollUpEvent(void) override;
335  bool scrollDownEvent(void) override;
336  void windowVisibleEvent(bool) override;
337 
338  bool scrollUpContent(void);
339  bool scrollDownContent(void);
340 
341  public:
342  ListBox(const UnicodeString &, const UnicodeList &, size_t rows, TermWindow &);
343  ListBox(const UnicodeString &, const UnicodeList &, size_t rows, const FontRender &, Window*);
344 
345  const std::string & result(void) const
346  {
347  return sResult;
348  }
349 
350  const char* className(void) const override { return "SWE::TermGUI::ListBox"; }
351  void renderWindow(void) override;
352  };
353 
354  } // namespace TermGUI
355 
356  class CommandConsole : public TermWindow
357  {
358  UnicodeList content;
359  std::string command;
360 
361  protected:
362  FBColors defaultColors(void) const override;
363  bool keyPressEvent(const KeySym &) override;
364  bool textInputEvent(const std::string &) override;
365 
366  virtual bool actionCommand(const std::string &);
367 
368  public:
369  CommandConsole(const Size &, const FontRender &, Window &);
370  CommandConsole(const TermSize &, TermWindow &);
371 
372  void renderWindow(void) override;
373 
374  const std::string & commandLine(void) const { return command; }
375  const UnicodeList & contentLines(void) const { return content; }
376 
377  void commandLineClear(void);
378  void contentLinesClear(void);
379 
380  void contentLinesAppend(const std::string &);
381  void contentLinesAppend(const StringList &);
382  };
383 
384 } // SWE
385 #endif // SWE_DISABLE_TERMGUI
386 #endif
класс объектов сцены DisplayScene, с возможностью отправки/получения сигналов
Definition: swe_events.h:56
класс двухмерной размерности в терминале
Definition: swe_termwin.h:72
класс пустого текстового окна с заголовком
Definition: swe_termgui.h:210
const FontRender * frs(void) const override
Definition: swe_termwin.cpp:1053
класс двухмерной позиции в терминале
Definition: swe_termwin.h:93
класс пары двух цветов (foreground, background)
Definition: swe_colors.h:136
buttons_t
набор системных меток для TextButton.
Definition: swe_termgui.h:133
пространство SWE.
Definition: swe_binarybuf.cpp:30
Definition: swe_termgui.h:158
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:273
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:233
event класс кнопки мыши (с состояниями press, release)
Definition: swe_events.h:159
Definition: swe_inputs_keys.h:245
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:154
Definition: swe_termgui.h:136
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:350
Definition: swe_termgui.h:356
void signalReceive(int, const SignalMember *) override
метод получатель, вызывается при signalEmit со стороны отправителя
Definition: swe_termgui.cpp:704
класс списка unicode строк
Definition: swe_cunicode.h:109
класс текстовой метки
Definition: swe_termgui.h:68
size_t cols(void) const
Definition: swe_termwin.cpp:360
базовый класс шрифта
Definition: swe_fontset.h:151
Theme colors base class.
Definition: swe_termgui.h:43
LineType
перечисление типа символьных линий
Definition: swe_termwin.h:33
класс индекса цвета
Definition: swe_colors.h:113
класс диалога информационного сообщения, с заголовком и группой кнопок
Definition: swe_termgui.h:280
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:288
класс списка строк
Definition: swe_cstring.h:36
класс диалога выбора из списка значений, с заголовком и группой кнопок
Definition: swe_termgui.h:321
Definition: swe_termgui.h:203
size_t rows(void) const
Definition: swe_termwin.cpp:365
класс двухмерной размерности
Definition: swe_rect.h:36
основной класс терминального окна
Definition: swe_termwin.h:757
класс пустого текстового окна с заголовком и группой кнопок
Definition: swe_termgui.h:237
класс диалога ввода строки текста, с заголовком и группой кнопок
Definition: swe_termgui.h:293
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:129
базовый класс графических объектов сцены DisplayScene.
Definition: swe_window.h:57
класс группы для LabelAction.
Definition: swe_termgui.h:172
класс unicode строки
Definition: swe_cunicode.h:41
const char * className(void) const override
идентификацинная метка класса
Definition: swe_termgui.h:316