libyui-ncurses  2.55.0
NCItemSelector.h
1 /*
2  Copyright (C) 2019 SUSE LLC
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCItemSelector.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef NCItemSelector_h
26 #define NCItemSelector_h
27 
28 #include <iosfwd>
29 #include <string>
30 #include <vector>
31 
32 #include <yui/YItemSelector.h>
33 #include "NCPadWidget.h"
34 #include "NCTablePad.h"
35 
36 
37 class NCItemSelectorBase : public YItemSelector, public NCPadWidget
38 {
39  friend std::ostream & operator<<( std::ostream & str, const NCItemSelectorBase & obj );
40 
41 protected:
42 
43  /**
44  * Standard constructor.
45  **/
46  NCItemSelectorBase( YWidget * parent, bool enforceSingleSelection );
47 
48  /**
49  * Constructor for custom item status values.
50  **/
51  NCItemSelectorBase( YWidget * parent,
52  const YItemCustomStatusVector & customStates );
53 
54 public:
55 
56  /**
57  * Destructor.
58  **/
59  virtual ~NCItemSelectorBase();
60 
61  /**
62  * Handle keyboard input.
63  **/
64  virtual NCursesEvent wHandleInput( wint_t key );
65 
66  /**
67  * Return the preferred width for this widget.
68  * Reimplemented from YWidget.
69  **/
70  virtual int preferredWidth();
71 
72  /**
73  * Return the preferred height for this widget.
74  * Reimplemented from YWidget.
75  **/
76  virtual int preferredHeight();
77 
78  /**
79  * Set the size of this widget.
80  * Reimplemented from YWidget.
81  **/
82  virtual void setSize( int newWidth, int newHeight );
83 
84  /**
85  * Return the current item, i.e. the item that currently has the keyboard
86  * focus. Not to be confused with the selected item.
87  **/
88  virtual YItem * currentItem() const;
89 
90  /**
91  * Set the current item, i.e. the item that currently has the keyboard
92  * focus.
93  **/
94  virtual void setCurrentItem( YItem * item );
95 
96  /**
97  * Enable or disable this widget.
98  * Reimplemented from YWidget.
99  **/
100  virtual void setEnabled( bool do_bv );
101 
102  /**
103  * Set the keyboard focus to this widget.
104  * Reimplemented from YWidget.
105  **/
106  virtual bool setKeyboardFocus();
107 
108  /**
109  * Set the number of visible items for this widget.
110  * Reimplemented from YItemSelector.
111  **/
112  virtual void setVisibleItems( int newVal );
113 
114  /**
115  * Return the number of lines in this widget. This is different from the
116  * number of items because each item always has one line for the item
117  * label, optionally multiple lines for the description, and optionally a
118  * separator line between it and the next item.
119  **/
120  int linesCount() const { return (int) myPad()->Lines(); }
121 
122  /**
123  * Return number of the current line, i.e. the line that has the keyboard
124  * focus.
125  **/
126  int currentLine() const { return myPad()->CurPos().L; }
127 
128  /**
129  * Add an item to this widget.
130  * Reimplemented from YSelectionWidget.
131  **/
132  virtual void addItem( YItem * item );
133 
134  /**
135  * Delete all items.
136  * Reimplemented from YSelectionWidget.
137  **/
138  virtual void deleteAllItems();
139 
140  /**
141  * Select or deselect an item.
142  * Reimplemented from YSelectionWidget.
143  **/
144  virtual void selectItem( YItem * item, bool selected );
145 
146  /**
147  * Deselect all items.
148  **/
149  virtual void deselectAllItems();
150 
151  /**
152  * Return the text line with the specified line number. Notice that this is
153  * different from the item index (see getNumLines()).
154  **/
155  const NCTableLine * getLine( int lineNo ) { return myPad()->GetLine( lineNo ); }
156 
157 
158  virtual void startMultipleChanges() { startMultidraw(); }
159 
160  virtual void doneMultipleChanges() { stopMultidraw(); }
161 
162  virtual const char * location() const { return "NCItemSelectorBase"; }
163 
164  /**
165  * Activate selected item. Can be used in tests to simulate user input.
166  *
167  * Derived classes are required to implement this.
168  **/
169  virtual void activateItem( YItem * item );
170 
171 protected:
172 
173  /**
174  * Create a tag cell for an item. This is the cell with the "[x]" or "(x)"
175  * selector. It also stores the item pointer so the item can later be
176  * referenced by this tag.
177  *
178  * Derived classes are required to implement this.
179  **/
180  virtual NCTableTag * createTagCell( YItem * item ) = 0;
181 
182  /**
183  * Cycle the status of the current item through its possible values.
184  * For a plain ItemSelector, this means true -> false -> true.
185  *
186  * Derived classes are required to implement this.
187  **/
188  virtual void cycleCurrentItemStatus() = 0;
189 
190  /**
191  * Return 'true' if a status change (by user interaction) from status
192  * 'fromStatus' to status 'toStatus' is allowed, 'false' if not.
193  **/
194  virtual bool statusChangeAllowed( int fromStatus, int toStatus )
195  { return false; }
196 
197  /**
198  * Notification that a status value was just changed in the input handler
199  * and the 'notify' flag is set. The returned event is used as the return
200  * value of the input handler (unless it has event type 'none' which is
201  * also returned by the default constructor of NCursesEvent), i.e. it is
202  * sent to the application.
203  *
204  * Derived classes are required to implement this.
205  **/
206  virtual NCursesEvent valueChangedNotify( YItem * item ) = 0;
207 
208  /**
209  * Return the desription text for an item. The result may contain newlines.
210  **/
211  std::string description( YItem * item ) const;
212 
213  /**
214  * Return the description text for an item as multiple lines.
215  **/
216  std::vector<std::string> descriptionLines( YItem * item ) const;
217 
218  /**
219  * If the cursor is not on the first line of an item (the line with the
220  * "[x]" selector), scroll down to the next line that is the first line of
221  * an item.
222  **/
223  YItem * scrollDownToNextItem();
224 
225  /**
226  * If the cursor is not on the first line of an item (the line with the
227  * "[x]" selector), scroll up to the next line that is the first line of
228  * an item.
229  **/
230  YItem * scrollUpToPreviousItem();
231 
232  /**
233  * Return the preferred size for this widget.
234  **/
235  virtual wsze preferredSize();
236 
237  /**
238  * Return the tag cell (the cell with the "[x]" or "(x)" selector) for the
239  * item with the specified index.
240  **/
241  virtual NCTableTag * tagCell( int index ) const;
242 
243  /**
244  * Return the line number that contains the first line of 'item'
245  * or -1 if not found.
246  **/
247  int findItemLine( YItem * item ) const;
248 
249  /**
250  * Create the pad for this widget.
251  **/
252  virtual NCPad * CreatePad();
253 
254  /**
255  * Return the pad for this widget; overloaded to narrow the type.
256  */
257  virtual NCTablePad * myPad() const
258  { return dynamic_cast<NCTablePad*>( NCPadWidget::myPad() ); }
259 
260  virtual void wRecoded() { NCPadWidget::wRecoded(); }
261 
262 private:
263 
264  // Disable assignement operator and copy constructor
265 
266  NCItemSelectorBase & operator=( const NCItemSelectorBase & );
268 
269 
270 protected:
271 
272  // Data members
273 
274  wsze _prefSize;
275  bool _prefSizeDirty;
276  int _selectorWidth;
277 
278 }; // class NCItemSelectorBase
279 
280 
281 
283 {
284 public:
285  /**
286  * Constructor.
287  **/
288  NCItemSelector( YWidget * parent, bool enforceSingleSelection );
289 
290  /**
291  * Destructor.
292  **/
293  virtual ~NCItemSelector();
294 
295  virtual const char * location() const { return "NCItemSelector"; }
296 
297 protected:
298 
299  /**
300  * Create a tag cell for an item. This is the cell with the "[x]" or "(x)"
301  * selector. It also stores the item pointer so the item can later be
302  * referenced by this tag.
303  **/
304  virtual NCTableTag * createTagCell( YItem * item );
305 
306  /**
307  * Notification that a status value was just changed in the input handler
308  * and the 'notify' flag is set.
309  **/
310  virtual NCursesEvent valueChangedNotify( YItem * item );
311 
312  /**
313  * Cycle the status of the current item through its possible values.
314  * For a plain ItemSelector, this means true -> false -> true.
315  **/
316  virtual void cycleCurrentItemStatus();
317 
318  /**
319  * Return 'true' if a status change (by user interaction) from status
320  * 'fromStatus' to status 'toStatus' is allowed, 'false' if not.
321  **/
322  virtual bool statusChangeAllowed( int fromStatus, int toStatus );
323 
324  /**
325  * Deselect all items except the specified one. This is used for single
326  * selection.
327  **/
328  void deselectAllItemsExcept( YItem * exceptItem );
329 
330 
331 private:
332 
333  // Disable assignement operator and copy constructor
334 
335  NCItemSelector & operator=( const NCItemSelector & );
336  NCItemSelector( const NCItemSelector & );
337 
338 
339 }; // class NCItemSelector
340 
341 
342 #endif // NCItemSelector_h
virtual wsze preferredSize()
Return the preferred size for this widget.
virtual ~NCItemSelectorBase()
Destructor.
virtual int preferredHeight()
Return the preferred height for this widget.
virtual void deleteAllItems()
Delete all items.
virtual NCTableTag * tagCell(int index) const
Return the tag cell (the cell with the "[x]" or "(x)" selector) for the item with the specified index...
virtual void addItem(YItem *item)
Add an item to this widget.
virtual NCTableTag * createTagCell(YItem *item)=0
Create a tag cell for an item.
virtual void selectItem(YItem *item, bool selected)
Select or deselect an item.
int findItemLine(YItem *item) const
Return the line number that contains the first line of 'item' or -1 if not found.
const NCTableLine * getLine(int lineNo)
Return the text line with the specified line number.
virtual bool setKeyboardFocus()
Set the keyboard focus to this widget.
int linesCount() const
Return the number of lines in this widget.
virtual void setVisibleItems(int newVal)
Set the number of visible items for this widget.
int currentLine() const
Return number of the current line, i.e.
virtual int preferredWidth()
Return the preferred width for this widget.
YItem * scrollUpToPreviousItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
virtual void setSize(int newWidth, int newHeight)
Set the size of this widget.
virtual void cycleCurrentItemStatus()=0
Cycle the status of the current item through its possible values.
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
NCItemSelectorBase(YWidget *parent, bool enforceSingleSelection)
Standard constructor.
virtual YItem * currentItem() const
Return the current item, i.e.
virtual NCTablePad * myPad() const
Return the pad for this widget; overloaded to narrow the type.
std::vector< std::string > descriptionLines(YItem *item) const
Return the description text for an item as multiple lines.
virtual void activateItem(YItem *item)
Activate selected item.
virtual void setCurrentItem(YItem *item)
Set the current item, i.e.
virtual void deselectAllItems()
Deselect all items.
YItem * scrollDownToNextItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
std::string description(YItem *item) const
Return the desription text for an item.
virtual NCursesEvent valueChangedNotify(YItem *item)=0
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
virtual NCPad * CreatePad()
Create the pad for this widget.
virtual NCursesEvent wHandleInput(wint_t key)
Handle keyboard input.
virtual void setEnabled(bool do_bv)
Enable or disable this widget.
NCItemSelector(YWidget *parent, bool enforceSingleSelection)
Constructor.
virtual ~NCItemSelector()
Destructor.
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
virtual NCTableTag * createTagCell(YItem *item)
Create a tag cell for an item.
virtual NCursesEvent valueChangedNotify(YItem *item)
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
virtual void cycleCurrentItemStatus()
Cycle the status of the current item through its possible values.
void deselectAllItemsExcept(YItem *exceptItem)
Deselect all items except the specified one.
virtual NCPad * myPad() const
Return the current pad.
Definition: NCPadWidget.h:62
Definition: NCPad.h:94
Definition: position.h:155