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 class FluidScroller;
34 
35 enum NumberingMode {
36  kListNumberingOff = -1,
37  kListNumberingZero = 0,
38  kListNumberingOne = 1
39 };
40 
42 enum {
43  kListItemSingleClickedCmd = 'LIsc',
44  kListItemDoubleClickedCmd = 'LIdc',
45  kListItemActivatedCmd = 'LIac',
46  kListItemRemovalRequestCmd = 'LIrm',
47  kListItemEditModeStartedCmd = 'LIes',
48  kListSelectionChangedCmd = 'Lsch'
49 };
50 
51 /* ListWidget */
52 class ListWidget : public EditableWidget {
53 public:
54  typedef bool (*FilterMatcher)(void *arg, int idx, const Common::U32String &item, const Common::U32String &token);
55 
56  struct ListData {
57  Common::U32String orig;
58  Common::U32String clean;
59 
60  ListData(const Common::U32String &o, const Common::U32String &c) { orig = o; clean = c; }
61  };
62 
64  ~ListWidget() override;
65 
66 protected:
68  Common::U32StringArray _cleanedList;
69  ListDataArray _dataList;
70  Common::Array<int> _listIndex;
71  bool _editable;
72  bool _editMode;
73  NumberingMode _numberingMode;
74  int _currentPos;
75  int _entriesPerPage;
76  int _selectedItem;
77  Common::Array<bool> _selectedItems;
80  int _currentKeyDown;
81 
82  float _scrollPos;
83  FluidScroller *_fluidScroller;
84  bool _wasAnimating;
85  bool _isMouseDown;
86  bool _isDragging;
87  int _dragStartY;
88  int _dragLastY;
89 
90  Common::String _quickSelectStr;
91  uint32 _quickSelectTime;
92 
93  int _hlLeftPadding;
94  int _hlRightPadding;
95  int _leftPadding;
96  int _rightPadding;
97  int _topPadding;
98  int _bottomPadding;
99  int _itemSpacing;
100  int _scrollBarWidth;
101 
102  Common::U32String _filter;
103  bool _quickSelect;
104  bool _dictionarySelect;
105 
106  uint32 _cmd;
107 
108  ThemeEngine::FontColor _editColor;
109 
110  int _lastRead;
111 
112  FilterMatcher _filterMatcher;
113  void *_filterMatcherArg;
114 
115  static const int kDragThreshold = 5;
116 
117 public:
118  ListWidget(Dialog *boss, const Common::String &name, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
119  ListWidget(Dialog *boss, int x, int y, int w, int h, bool scale, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
120  ListWidget(Dialog *boss, int x, int y, int w, int h, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
121 
122  bool containsWidget(Widget *) const override;
123  Widget *findWidget(int x, int y) override;
124 
125  void setList(const Common::U32StringArray &list);
126  const Common::U32StringArray &getList() const { return _cleanedList; }
127 
128  void append(const Common::String &s);
129 
130  void setSelected(int item);
131  int getSelected() const { return (_filter.empty() || _selectedItem == -1) ? _selectedItem : _listIndex[_selectedItem]; }
132 
133  const Common::U32String getSelectedString() const { return stripGUIformatting(_list[_selectedItem]); }
134 
136  int getVisualPos(int dataIndex) const;
137 
139  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
140  bool isItemSelected(int item) const;
141  void markSelectedItem(int item, bool state);
142  void clearSelection();
143  void selectItemRange(int from, int to);
144  int _lastSelectionStartItem;
145  void setNumberingMode(NumberingMode numberingMode) { _numberingMode = numberingMode; }
146 
147  void scrollTo(int item);
148  void scrollToEnd();
149  int getCurrentScrollPos() const { return _currentPos; }
150  bool isItemVisible(int item) const { return _currentPos <= item && item < _currentPos + _entriesPerPage; }
151 
152  void enableQuickSelect(bool enable) { _quickSelect = enable; }
153  Common::String getQuickSelectString() const { return _quickSelectStr; }
154 
155  void enableDictionarySelect(bool enable) { _dictionarySelect = enable; }
156 
157  bool isEditable() const { return _editable; }
158  void setEditable(bool editable) { _editable = editable; }
159  void setEditColor(ThemeEngine::FontColor color) { _editColor = color; }
160  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
161 
162  // Multi-selection methods
163  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
164  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
165 
166  // Made startEditMode/endEditMode for SaveLoadChooser
167  void startEditMode() override;
168  void endEditMode() override;
169 
170  void setFilter(const Common::U32String &filter, bool redraw = true);
171 
172  void handleTickle() override;
173  void applyScrollPos();
174  void handleMouseDown(int x, int y, int button, int clickCount) override;
175  void handleMouseUp(int x, int y, int button, int clickCount) override;
176  void handleMouseWheel(int x, int y, int direction) override;
177  void handleMouseMoved(int x, int y, int button) override;
178  void handleMouseLeft(int button) override;
179  bool handleKeyDown(Common::KeyState state) override;
180  bool handleKeyUp(Common::KeyState state) override;
181  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
182 
183  void reflowLayout() override;
184 
185  bool wantsFocus() override { return true; }
186 
187  static Common::U32String getThemeColor(byte r, byte g, byte b);
188  static Common::U32String getThemeColor(ThemeEngine::FontColor color);
189  static ThemeEngine::FontColor getThemeColor(const Common::U32String &color);
190  static Common::U32String stripGUIformatting(const Common::U32String &str);
191  static Common::U32String escapeString(const Common::U32String &str);
192 
193 protected:
194  void drawWidget() override;
195 
197  int findItem(int x, int y) const;
198  void scrollBarRecalc();
199 
200  void abortEditMode() override;
201 
202  Common::Rect getEditRect() const override;
203  int getCaretOffset() const override;
204 
205  void copyListData(const Common::U32StringArray &list);
206 
207  void receivedFocusWidget() override;
208  void lostFocusWidget() override;
209  void checkBounds();
210  void scrollToCurrent();
211 
213  int findDataIndex(int dataIndex) const;
214 
216  virtual bool isItemSelectable(int item) const { return true; }
217 
218  // Searches for the next selectable item in the given direction (1 for down, -1 for up) starting from 'item' and returns its index.
219  // Returns -1 if no selectable item is found.
220  int findSelectableItem(int item, int direction) const;
221 
222  virtual ThemeEngine::WidgetStateInfo getItemState(int item) const { return _state; }
223 
224  void drawFormattedText(const Common::Rect &r, const Common::U32String &str, ThemeEngine::WidgetStateInfo state = ThemeEngine::kStateEnabled,
226  ThemeEngine::TextInversionState inverted = ThemeEngine::kTextInversionNone, int deltax = 0, bool useEllipsis = true,
228 };
229 
230 } // End of namespace GUI
231 
232 #endif
Center the text.
Definition: font.h:52
ScrollBarWidget * _scrollBar
Flag for multi-selection.
Definition: list.h:79
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:56
Definition: FluidScroll.h:34
Definition: rect.h:536
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:216
TextInversionState
Text inversion state of the text to be draw.
Definition: ThemeEngine.h:259
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:52
void setNumberingMode(NumberingMode numberingMode)
Used for Shift+Click range selection.
Definition: list.h:145
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:78
Definition: widget.h:101
Definition: keyboard.h:294
const Common::Array< bool > & getSelectedItems() const
Multi-selection support.
Definition: list.h:139
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.