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 _isMouseDown;
85  bool _isDragging;
86  int _dragStartY;
87  int _dragLastY;
88 
89  Common::String _quickSelectStr;
90  uint32 _quickSelectTime;
91 
92  int _hlLeftPadding;
93  int _hlRightPadding;
94  int _leftPadding;
95  int _rightPadding;
96  int _topPadding;
97  int _bottomPadding;
98  int _itemSpacing;
99  int _scrollBarWidth;
100 
101  Common::U32String _filter;
102  bool _quickSelect;
103  bool _dictionarySelect;
104 
105  uint32 _cmd;
106 
107  ThemeEngine::FontColor _editColor;
108 
109  int _lastRead;
110 
111  FilterMatcher _filterMatcher;
112  void *_filterMatcherArg;
113 
114  static const int kDragThreshold = 5;
115 
116 public:
117  ListWidget(Dialog *boss, const Common::String &name, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
118  ListWidget(Dialog *boss, int x, int y, int w, int h, bool scale, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
119  ListWidget(Dialog *boss, int x, int y, int w, int h, const Common::U32String &tooltip = Common::U32String(), uint32 cmd = 0);
120 
121  bool containsWidget(Widget *) const override;
122  Widget *findWidget(int x, int y) override;
123 
124  void setList(const Common::U32StringArray &list);
125  const Common::U32StringArray &getList() const { return _cleanedList; }
126 
127  void append(const Common::String &s);
128 
129  void setSelected(int item);
130  int getSelected() const { return (_filter.empty() || _selectedItem == -1) ? _selectedItem : _listIndex[_selectedItem]; }
131 
132  const Common::U32String getSelectedString() const { return stripGUIformatting(_list[_selectedItem]); }
133 
135  int getVisualPos(int dataIndex) const;
136 
138  const Common::Array<bool> &getSelectedItems() const { return _selectedItems; }
139  bool isItemSelected(int item) const;
140  void markSelectedItem(int item, bool state);
141  void clearSelection();
142  void selectItemRange(int from, int to);
143  int _lastSelectionStartItem;
144  void setNumberingMode(NumberingMode numberingMode) { _numberingMode = numberingMode; }
145 
146  void scrollTo(int item);
147  void scrollToEnd();
148  int getCurrentScrollPos() const { return _currentPos; }
149  bool isItemVisible(int item) const { return _currentPos <= item && item < _currentPos + _entriesPerPage; }
150 
151  void enableQuickSelect(bool enable) { _quickSelect = enable; }
152  Common::String getQuickSelectString() const { return _quickSelectStr; }
153 
154  void enableDictionarySelect(bool enable) { _dictionarySelect = enable; }
155 
156  bool isEditable() const { return _editable; }
157  void setEditable(bool editable) { _editable = editable; }
158  void setEditColor(ThemeEngine::FontColor color) { _editColor = color; }
159  void setFilterMatcher(FilterMatcher matcher, void *arg) { _filterMatcher = matcher; _filterMatcherArg = arg; }
160 
161  // Multi-selection methods
162  void setMultiSelectEnabled(bool enabled) { _multiSelectEnabled = enabled; }
163  bool isMultiSelectEnabled() const { return _multiSelectEnabled; }
164 
165  // Made startEditMode/endEditMode for SaveLoadChooser
166  void startEditMode() override;
167  void endEditMode() override;
168 
169  void setFilter(const Common::U32String &filter, bool redraw = true);
170 
171  void handleTickle() override;
172  void applyScrollPos();
173  void handleMouseDown(int x, int y, int button, int clickCount) override;
174  void handleMouseUp(int x, int y, int button, int clickCount) override;
175  void handleMouseWheel(int x, int y, int direction) override;
176  void handleMouseMoved(int x, int y, int button) override;
177  void handleMouseLeft(int button) override;
178  bool handleKeyDown(Common::KeyState state) override;
179  bool handleKeyUp(Common::KeyState state) override;
180  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
181 
182  void reflowLayout() override;
183 
184  bool wantsFocus() override { return true; }
185 
186  static Common::U32String getThemeColor(byte r, byte g, byte b);
187  static Common::U32String getThemeColor(ThemeEngine::FontColor color);
188  static ThemeEngine::FontColor getThemeColor(const Common::U32String &color);
189  static Common::U32String stripGUIformatting(const Common::U32String &str);
190  static Common::U32String escapeString(const Common::U32String &str);
191 
192 protected:
193  void drawWidget() override;
194 
196  int findItem(int x, int y) const;
197  void scrollBarRecalc();
198 
199  void abortEditMode() override;
200 
201  Common::Rect getEditRect() const override;
202  int getCaretOffset() const override;
203 
204  void copyListData(const Common::U32StringArray &list);
205 
206  void receivedFocusWidget() override;
207  void lostFocusWidget() override;
208  void checkBounds();
209  void scrollToCurrent();
210 
212  int findDataIndex(int dataIndex) const;
213 
215  virtual bool isItemSelectable(int item) const { return true; }
216 
217  // Searches for the next selectable item in the given direction (1 for down, -1 for up) starting from 'item' and returns its index.
218  // Returns -1 if no selectable item is found.
219  int findSelectableItem(int item, int direction) const;
220 
221  virtual ThemeEngine::WidgetStateInfo getItemState(int item) const { return _state; }
222 
223  void drawFormattedText(const Common::Rect &r, const Common::U32String &str, ThemeEngine::WidgetStateInfo state = ThemeEngine::kStateEnabled,
225  ThemeEngine::TextInversionState inverted = ThemeEngine::kTextInversionNone, int deltax = 0, bool useEllipsis = true,
227 };
228 
229 } // End of namespace GUI
230 
231 #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: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:215
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:144
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:138
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.