SDL Window Engine  20200905
swe_wingui_list.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_GUI_LIST_
24 #define _SWE_GUI_LIST_
25 
26 #include <vector>
27 
28 #include "swe_wingui.h"
29 
31 namespace SWE
32 {
33  // ListWidget emit signals: Signal::ListWidgetChanged, Signal::ListWidgetScrolled
34  class ListWidget;
35 
37  {
38  friend class ListWidget;
39  bool isVisibleNotHidden(void) const;
40 
41  protected:
42  bool mousePressEvent(const ButtonEvent &) override final;
43  bool mouseClickEvent(const ButtonsEvent &) override final;
44  void mouseFocusEvent(void) override final;
45  void mouseLeaveEvent(void) override final;
46 
47  public:
50 
51  bool isSelected(void) const;
52  ListWidget* listWidget(void) const;
53  void setSelected(bool);
54 
55  virtual bool isValid(void) const { return true; }
56  virtual bool operator< (const ListWidgetItem & v) const { return this < &v; }
57  virtual void swap(ListWidgetItem*) {}
58  const char* className(void) const override { return "SWE::ListWidgetItem"; }
59  };
60 
61  class ListWidget : public Window
62  {
63  std::vector<ListWidgetItem*>
64  listItems;
65  int skipItems;
66  ListWidgetItem* curItem;
67 
68  protected:
69  bool scrollUpEvent(void) override;
70  bool scrollDownEvent(void) override;
71  void signalReceive(int, const SignalMember*) override;
72  bool userEvent(int, void*) override;
73 
74  virtual int itemSpacer(void) const { return 0; }
75 
76  public:
77  ListWidget(bool vertical, Window*);
78  ListWidget(const Size &, bool vertical, Window*);
79  ListWidget(const Point &, const Size &, bool vertical, Window*);
80  virtual ~ListWidget();
81 
82  void addItem(ListWidgetItem*);
83 
84  int count(void) const;
85  void clear(void);
86  ListWidgetItem* currentItem(void) const;
87  int currentRow(void) const;
88 
89  void insertItem(int row, ListWidgetItem*);
90 
91  ListWidgetItem* item(int row) const;
92  ListWidgetItem* itemAt(const Point &) const;
93 
94  const std::vector<ListWidgetItem*> &
95  items(void) const;
96 
97  void removeItemWidget(ListWidgetItem*);
98  int row(const ListWidgetItem*) const;
99  int rowTop(void) const;
100 
101  void scrollToItem(const ListWidgetItem*);
102  void setCurrentItem(ListWidgetItem*);
103  void setCurrentRow(int row);
104  void sortItems(void);
105 
106  ListWidgetItem* takeItem(int row);
107  virtual int visibleItems(void) const;
108  virtual int scrollItems(void) const;
109  virtual Rect listArea(void) const;
110 
111  virtual void currentItemChanged(ListWidgetItem* current, ListWidgetItem* previous) {}
112  virtual void itemEntered(ListWidgetItem*) {}
113  virtual void itemPressed(ListWidgetItem*, int) {}
114  virtual void itemClicked(ListWidgetItem*, int) {}
115  virtual void itemDoubleClicked(ListWidgetItem*) {}
116 
117  virtual void renderWindow(void) override;
118  virtual void renderVisibleItem(ListWidgetItem*, int visibleIndex, int visibleItems);
119 
120  bool isAreaPoint(const Point &) const override;
121  bool isVerticalOrientation(void) const;
122  bool scrollUp(int rows = 1);
123  bool scrollDown(int rows = 1);
124 
125  const char* className(void) const override { return "SWE::ListWidget"; }
126 #ifdef SWE_WITH_JSON
127  JsonObject toJson(void) const override
128  {
129  JsonObject res = Window::toJson();
130  res.addInteger("size", listItems.size());
131  res.addInteger("skipItems", skipItems);
132  JsonArray items;
133  for(auto it = listItems.begin(); it != listItems.end(); ++it)
134  items.addObject((*it)->toJson());
135  res.addString("curItem", String::pointer(curItem));
136  res.addArray("items", items);
137  return res;
138  }
139 #endif
140  };
141 
142  class TextArea;
143 
145  {
146  TexturePos content;
147 
148  public:
149  TextAreaItem(const TexturePos &, TextArea &);
150  void renderWindow(void) override;
151 
152  const char* className(void) const override { return "SWE::TextAreaItem"; }
153 #ifdef SWE_WITH_JSON
154  JsonObject toJson(void) const override
155  {
156  JsonObject res = ListWidgetItem::toJson();
157  res.addObject("content", content.toJson());
158  return res;
159  }
160 #endif
161  };
162 
163  class TextArea : public ListWidget
164  {
165  public:
166  TextArea(Window* win) : ListWidget(true, win) {}
167  TextArea(const Size & sz, Window* win) : ListWidget(sz, true, win) {}
168  TextArea(const Point & pos, const Size & sz, Window* win) : ListWidget(pos, sz, true, win) {}
169 
170  TextArea & appendString(const FontRender &, const UnicodeString &, const Color &, int halign = AlignLeft, bool wrap = false);
171  TextArea & appendString(const FontRender &, const UCString &, int halign = AlignLeft, bool wrap = false);
172  TextArea & appendString(const Texture &, const Point &);
173  TextArea & appendSpacer(const FontRender &);
174 
175  const char* className(void) const override { return "SWE::TextArea"; }
176  };
177 
178 } // SWE
179 #endif
Definition: swe_surface.h:154
класс объектов сцены DisplayScene, с возможностью отправки/получения сигналов
Definition: swe_events.h:56
const char * className(void) const override
идентификацинная метка класса
Definition: swe_wingui_list.h:152
const char * className(void) const override
идентификацинная метка класса
Definition: swe_wingui_list.h:58
пространство SWE.
Definition: swe_binarybuf.cpp:30
класс прямоугольника
Definition: swe_rect.h:144
bool userEvent(int, void *) override
метод получатель, вызывается при получении сообщения, отправленного через pushEventAction.
Definition: swe_wingui_list.cpp:370
event класс кнопки мыши (с состояниями press, release)
Definition: swe_events.h:159
Definition: swe_wingui.h:31
класс точки с двумя координатами
Definition: swe_rect.h:72
класс цветной unicode строки
Definition: swe_cunicode_color.h:67
класс цвета
Definition: swe_colors.h:65
базовый класс шрифта
Definition: swe_fontset.h:151
const char * className(void) const override
идентификацинная метка класса
Definition: swe_wingui_list.h:125
по левому краю
Definition: swe_fontset.h:39
Definition: swe_wingui_list.h:163
Definition: swe_surface.h:210
const char * className(void) const override
идентификацинная метка класса
Definition: swe_wingui_list.h:175
event класс кнопки мыши
Definition: swe_events.h:145
класс двухмерной размерности
Definition: swe_rect.h:36
Definition: swe_wingui_list.h:61
void signalReceive(int, const SignalMember *) override
метод получатель, вызывается при signalEmit со стороны отправителя
Definition: swe_wingui_list.cpp:397
Definition: swe_wingui_list.h:144
базовый класс графических объектов сцены DisplayScene.
Definition: swe_window.h:57
Definition: swe_wingui_list.h:36
класс unicode строки
Definition: swe_cunicode.h:41