ScummVM API documentation
list.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GUI_WIDGETS_LIST_H
23 #define GUI_WIDGETS_LIST_H
24 
25 #include "gui/widgets/editable.h"
26 #include "common/str.h"
27 
28 #include "gui/ThemeEngine.h"
29 
30 namespace GUI {
31 
32 class ScrollBarWidget;
33 
34 enum NumberingMode {
35  kListNumberingOff = -1,
36  kListNumberingZero = 0,
37  kListNumberingOne = 1
38 };
39 
41 enum {
42  kListItemSingleClickedCmd = 'LIsc',
43  kListItemDoubleClickedCmd = 'LIdc',
44  kListItemActivatedCmd = 'LIac',
45  kListItemRemovalRequestCmd = 'LIrm',
46  kListItemEditModeStartedCmd = 'LIes',
47  kListSelectionChangedCmd = 'Lsch'
48 };
49 
50 /* ListWidget */
51 class ListWidget : public EditableWidget {
52 public:
53  typedef bool (*FilterMatcher)(void *arg, int idx, const Common::U32String &item, const Common::U32String &token);
54 
55  struct ListData {
56  Common::U32String orig;
57  Common::U32String clean;
58 
59  ListData(const Common::U32String &o, const Common::U32String &c) { orig = o; clean = c; }
60  };
61 
63 
64 protected:
66  Common::U32StringArray _cleanedList;
67  ListDataArray _dataList;
68  Common::Array<int> _listIndex;
69  bool _editable;
70  bool _editMode;
71  NumberingMode _numberingMode;
72  int _currentPos;
73  int _entriesPerPage;
74  int _selectedItem;
75  Common::Array<bool> _selectedItems;
78  int _currentKeyDown;
79 
80  Common::String _quickSelectStr;
81  uint32 _quickSelectTime;
82 
83  int _hlLeftPadding;
84  int _hlRightPadding;
85  int _leftPadding;
86  int _rightPadding;
87  int _topPadding;
88  int _bottomPadding;
89  int _itemSpacing;
90  int _scrollBarWidth;
91 
92  Common::U32String _filter;
93  bool _quickSelect;
94  bool _dictionarySelect;
95 
96  uint32 _cmd;
97 
98  ThemeEngine::FontColor _editColor;
99 
100  int _lastRead;
101 
102  FilterMatcher _filterMatcher;
103  void *_filterMatcherArg;
104 public:
105  ListWidget(Dialog *boss, const Common::String &name, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
106  ListWidget(Dialog *boss, int x, int y, int w, int h, bool scale, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
107  ListWidget(Dialog *boss, int x, int y, int w, int h, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
108 
109  bool containsWidget(Widget *) const override;
110  Widget *findWidget(int x, int y) override;
111 
112  void setList(const Common::U32StringArray &list);
113  const Common::U32StringArray &getList() const { return _cleanedList; }
114 
115  void append(const Common::String &s);
116 
117  void setSelected(int item);
118  int getSelected() const { return (_filter.empty() || _selectedItem == -1) ? _selectedItem : _listIndex[_selectedItem]; }
119 
120  const Common::U32String getSelectedString() const { return stripGUIformatting(_list[_selectedItem]); }
121 
123  int getVisualPos(int dataIndex) const;
124 
126  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
127  bool isItemSelected(int item) const;
128  void markSelectedItem(int item, bool state);
129  void clearSelection();
130  void selectItemRange(int from, int to);
131  int _lastSelectionStartItem;
132  void setNumberingMode(NumberingMode numberingMode) { _numberingMode = numberingMode; }
133 
134  void scrollTo(int item);
135  void scrollToEnd();
136  int getCurrentScrollPos() const { return _currentPos; }
137  bool isItemVisible(int item) const { return _currentPos <= item && item < _currentPos + _entriesPerPage; }
138 
139  void enableQuickSelect(bool enable) { _quickSelect = enable; }
140  Common::String getQuickSelectString() const { return _quickSelectStr; }
141 
142  void enableDictionarySelect(bool enable) { _dictionarySelect = enable; }
143 
144  bool isEditable() const { return _editable; }
145  void setEditable(bool editable) { _editable = editable; }
146  void setEditColor(ThemeEngine::FontColor color) { _editColor = color; }
147  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
148 
149  // Multi-selection methods
150  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
151  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
152 
153  // Made startEditMode/endEditMode for SaveLoadChooser
154  void startEditMode() override;
155  void endEditMode() override;
156 
157  void setFilter(const Common::U32String &filter, bool redraw = true);
158 
159  void handleTickle() override;
160  void handleMouseDown(int x, int y, int button, int clickCount) override;
161  void handleMouseUp(int x, int y, int button, int clickCount) override;
162  void handleMouseWheel(int x, int y, int direction) override;
163  void handleMouseMoved(int x, int y, int button) override;
164  void handleMouseLeft(int button) override;
165  bool handleKeyDown(Common::KeyState state) override;
166  bool handleKeyUp(Common::KeyState state) override;
167  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
168 
169  void reflowLayout() override;
170 
171  bool wantsFocus() override { return true; }
172 
173  static Common::U32String getThemeColor(byte r, byte g, byte b);
174  static Common::U32String getThemeColor(ThemeEngine::FontColor color);
175  static ThemeEngine::FontColor getThemeColor(const Common::U32String &color);
176  static Common::U32String stripGUIformatting(const Common::U32String &str);
177  static Common::U32String escapeString(const Common::U32String &str);
178 
179 protected:
180  void drawWidget() override;
181 
183  int findItem(int x, int y) const;
184  void scrollBarRecalc();
185 
186  void abortEditMode() override;
187 
188  Common::Rect getEditRect() const override;
189  int getCaretOffset() const override;
190 
191  void copyListData(const Common::U32StringArray &list);
192 
193  void receivedFocusWidget() override;
194  void lostFocusWidget() override;
195  void checkBounds();
196  void scrollToCurrent();
197 
199  int findDataIndex(int dataIndex) const;
200 
202  virtual bool isItemSelectable(int item) const { return true; }
203 
204  virtual ThemeEngine::WidgetStateInfo getItemState(int item) const { return _state; }
205 
206  void drawFormattedText(const Common::Rect &r, const Common::U32String &str, ThemeEngine::WidgetStateInfo state = ThemeEngine::kStateEnabled,
208  ThemeEngine::TextInversionState inverted = ThemeEngine::kTextInversionNone, int deltax = 0, bool useEllipsis = true,
210 };
211 
212 } // End of namespace GUI
213 
214 #endif
Center the text.
Definition: font.h:52
ScrollBarWidget * _scrollBar
Flag for multi-selection.
Definition: list.h:77
Definition: str.h:59
Indicates that the text should not be drawn inverted.
Definition: ThemeEngine.h:260
FontColor
Font color selector.
Definition: ThemeEngine.h:288
TextAlign
Definition: font.h:48
Common::Rect getEditRect() const override
Use color from formatting.
Definition: ThemeEngine.h:289
Definition: list.h:55
Definition: rect.h:524
Definition: scrollbar.h:34
Definition: printman.h:30
virtual bool isItemSelectable(int item) const
Check if an item at a given position is selectable.
Definition: list.h:202
TextInversionState
Text inversion state of the text to be draw.
Definition: ThemeEngine.h:259
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: ustr.h:57
Indicates that the widget is enabled.
Definition: ThemeEngine.h:251
int findDataIndex(int dataIndex) const
Find the visual position of a data item.
Definition: list.h:51
void setNumberingMode(NumberingMode numberingMode)
Used for Shift+Click range selection.
Definition: list.h:132
int getVisualPos(int dataIndex) const
Get visual position (index in filtered list) from real data index.
Definition: dialog.h:49
Definition: editable.h:41
State
State of the widget to be drawn.
Definition: ThemeEngine.h:249
bool _multiSelectEnabled
Multiple selected items (bool array)
Definition: list.h:76
Definition: widget.h:101
Definition: keyboard.h:294
const Common::Array< bool > & getSelectedItems() const
Multi-selection support.
Definition: list.h:126
Definition: object.h:40
int findItem(int x, int y) const
Finds the item at position (x,y). Returns -1 if there is no item there.