libyui-ncurses  2.57.2
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  virtual void activateItem( YItem * item );
168 
169  /**
170  * Notification that some shortcut was changed.
171  *
172  * Reimplemented from YSelectionWidget.
173  **/
174  virtual void shortcutChanged();
175 
176  /**
177  * Whether any item has the given hot-key .
178  * Reimplemented from NCWidget.
179  **/
180  virtual bool HasHotkey( int key ) ;
181 
182 protected:
183 
184  /**
185  * Create a widget for the given item.
186  **/
187  void createItemWidget( YItem * item );
188 
189  /**
190  * Create a tag cell for an item. This is the cell with the "[x]" or "(x)"
191  * selector. It also stores the item pointer so the item can later be
192  * referenced by this tag.
193  *
194  * Derived classes are required to implement this.
195  **/
196  virtual NCTableTag * createTagCell( YItem * item ) = 0;
197 
198  /**
199  * Cycle the status of the current item through its possible values.
200  * For a plain ItemSelector, this means true -> false -> true.
201  *
202  * Derived classes are required to implement this.
203  **/
204  virtual void cycleCurrentItemStatus() = 0;
205 
206  /**
207  * Return 'true' if a status change (by user interaction) from status
208  * 'fromStatus' to status 'toStatus' is allowed, 'false' if not.
209  **/
210  virtual bool statusChangeAllowed( int fromStatus, int toStatus )
211  { return false; }
212 
213  /**
214  * Notification that a status value was just changed in the input handler
215  * and the 'notify' flag is set. The returned event is used as the return
216  * value of the input handler (unless it has event type 'none' which is
217  * also returned by the default constructor of NCursesEvent), i.e. it is
218  * sent to the application.
219  *
220  * Derived classes are required to implement this.
221  **/
222  virtual NCursesEvent valueChangedNotify( YItem * item ) = 0;
223 
224  /**
225  * Return the desription text for an item. The result may contain newlines.
226  **/
227  std::string description( YItem * item ) const;
228 
229  /**
230  * Return the description text for an item as multiple lines.
231  **/
232  std::vector<std::string> descriptionLines( YItem * item ) const;
233 
234  /**
235  * If the cursor is not on the first line of an item (the line with the
236  * "[x]" selector), scroll down to the next line that is the first line of
237  * an item.
238  **/
239  YItem * scrollDownToNextItem();
240 
241  /**
242  * If the cursor is not on the first line of an item (the line with the
243  * "[x]" selector), scroll up to the next line that is the first line of
244  * an item.
245  **/
246  YItem * scrollUpToPreviousItem();
247 
248  /**
249  * Return the preferred size for this widget.
250  **/
251  virtual wsze preferredSize();
252 
253  /**
254  * Return the tag cell (the cell with the "[x]" or "(x)" selector) for the
255  * item with the specified index.
256  **/
257  virtual NCTableTag * tagCell( int index ) const;
258 
259  /**
260  * Return the line number that contains the first line of 'item'
261  * or -1 if not found.
262  **/
263  int findItemLine( YItem * item ) const;
264 
265  /**
266  * Create the pad for this widget.
267  **/
268  virtual NCPad * CreatePad();
269 
270  /**
271  * Return the pad for this widget; overloaded to narrow the type.
272  */
273  virtual NCTablePad * myPad() const
274  { return dynamic_cast<NCTablePad*>( NCPadWidget::myPad() ); }
275 
276  virtual void wRecoded() { NCPadWidget::wRecoded(); }
277 
278 private:
279 
280  // Disable assignment operator and copy constructor
281 
282  NCItemSelectorBase & operator=( const NCItemSelectorBase & );
284 
285  YItem* findItemWithHotkey( int key ) const;
286 
287 protected:
288 
289  // Data members
290 
291  wsze _prefSize;
292  bool _prefSizeDirty;
293  int _selectorWidth;
294  int _hotKey;
295 
296 
297 }; // class NCItemSelectorBase
298 
299 
300 
302 {
303 public:
304  /**
305  * Constructor.
306  **/
307  NCItemSelector( YWidget * parent, bool enforceSingleSelection );
308 
309  /**
310  * Destructor.
311  **/
312  virtual ~NCItemSelector();
313 
314  virtual const char * location() const { return "NCItemSelector"; }
315 
316 protected:
317 
318  /**
319  * Create a tag cell for an item. This is the cell with the "[x]" or "(x)"
320  * selector. It also stores the item pointer so the item can later be
321  * referenced by this tag.
322  **/
323  virtual NCTableTag * createTagCell( YItem * item );
324 
325  /**
326  * Notification that a status value was just changed in the input handler
327  * and the 'notify' flag is set.
328  **/
329  virtual NCursesEvent valueChangedNotify( YItem * item );
330 
331  /**
332  * Cycle the status of the current item through its possible values.
333  * For a plain ItemSelector, this means true -> false -> true.
334  **/
335  virtual void cycleCurrentItemStatus();
336 
337  /**
338  * Return 'true' if a status change (by user interaction) from status
339  * 'fromStatus' to status 'toStatus' is allowed, 'false' if not.
340  **/
341  virtual bool statusChangeAllowed( int fromStatus, int toStatus );
342 
343  /**
344  * Deselect all items except the specified one. This is used for single
345  * selection.
346  **/
347  void deselectAllItemsExcept( YItem * exceptItem );
348 
349 
350 private:
351 
352  // Disable assignment operator and copy constructor
353 
354  NCItemSelector & operator=( const NCItemSelector & );
355  NCItemSelector( const NCItemSelector & );
356 
357 }; // class NCItemSelector
358 
359 
360 #endif // NCItemSelector_h
wsze
Screen dimension (screen size) in the order height, width: (H, W)
Definition: position.h:154
NCItemSelectorBase::activateItem
virtual void activateItem(YItem *item)
Activate selected item.
Definition: NCItemSelector.cc:543
NCItemSelectorBase::tagCell
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...
Definition: NCItemSelector.cc:255
NCItemSelectorBase::HasHotkey
virtual bool HasHotkey(int key)
Whether any item has the given hot-key .
Definition: NCItemSelector.cc:572
NCItemSelectorBase::myPad
virtual NCTablePad * myPad() const
Return the pad for this widget; overloaded to narrow the type.
Definition: NCItemSelector.h:273
NCItemSelectorBase::CreatePad
virtual NCPad * CreatePad()
Create the pad for this widget.
Definition: NCItemSelector.cc:82
NCItemSelectorBase::currentItem
virtual YItem * currentItem() const
Return the current item, i.e.
Definition: NCItemSelector.cc:171
NCItemSelectorBase::deselectAllItems
virtual void deselectAllItems()
Deselect all items.
Definition: NCItemSelector.cc:335
NCItemSelectorBase::createItemWidget
void createItemWidget(YItem *item)
Create a widget for the given item.
Definition: NCItemSelector.cc:201
NCItemSelectorBase::preferredHeight
virtual int preferredHeight()
Return the preferred height for this widget.
Definition: NCItemSelector.cc:101
NCItemSelector::cycleCurrentItemStatus
virtual void cycleCurrentItemStatus()
Cycle the status of the current item through its possible values.
Definition: NCItemSelector.cc:622
NCItemSelectorBase::descriptionLines
std::vector< std::string > descriptionLines(YItem *item) const
Return the description text for an item as multiple lines.
Definition: NCItemSelector.cc:297
NCItemSelectorBase::setEnabled
virtual void setEnabled(bool do_bv)
Enable or disable this widget.
Definition: NCItemSelector.cc:157
NCItemSelector::createTagCell
virtual NCTableTag * createTagCell(YItem *item)
Create a tag cell for an item.
Definition: NCItemSelector.cc:601
NCTablePad
An NCPad for an NCTable.
Definition: NCTablePad.h:62
NCTablePadBase::Lines
unsigned Lines() const
Return the number of table lines (logical, not screen)
Definition: NCTablePadBase.h:122
NCItemSelector
Definition: NCItemSelector.h:302
NCTablePadBase::CurPos
virtual wpos CurPos() const
CurPos().L is the index of the selected item.
Definition: NCTablePadBase.cc:188
NCItemSelectorBase::statusChangeAllowed
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
Definition: NCItemSelector.h:210
NCItemSelectorBase::NCItemSelectorBase
NCItemSelectorBase(YWidget *parent, bool enforceSingleSelection)
Standard constructor.
Definition: NCItemSelector.cc:39
NCItemSelectorBase::selectItem
virtual void selectItem(YItem *item, bool selected)
Select or deselect an item.
Definition: NCItemSelector.cc:319
NCItemSelectorBase::cycleCurrentItemStatus
virtual void cycleCurrentItemStatus()=0
Cycle the status of the current item through its possible values.
NCTablePadBase::GetLine
const NCTableLine * GetLine(unsigned idx) const
Return the line at idx for read-only operations.
Definition: NCTablePadBase.cc:87
NCItemSelector::~NCItemSelector
virtual ~NCItemSelector()
Destructor.
Definition: NCItemSelector.cc:594
NCItemSelectorBase::setSize
virtual void setSize(int newWidth, int newHeight)
Set the size of this widget.
Definition: NCItemSelector.cc:142
NCItemSelectorBase::setVisibleItems
virtual void setVisibleItems(int newVal)
Set the number of visible items for this widget.
Definition: NCItemSelector.cc:164
NCItemSelectorBase::addItem
virtual void addItem(YItem *item)
Add an item to this widget.
Definition: NCItemSelector.cc:191
NCPad
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
Definition: NCPad.h:113
NCItemSelector::statusChangeAllowed
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
Definition: NCItemSelector.cc:641
NCItemSelector::valueChangedNotify
virtual NCursesEvent valueChangedNotify(YItem *item)
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
Definition: NCItemSelector.cc:611
NCItemSelectorBase
Definition: NCItemSelector.h:38
NCItemSelectorBase::shortcutChanged
virtual void shortcutChanged()
Notification that some shortcut was changed.
Definition: NCItemSelector.cc:556
NCItemSelectorBase::scrollDownToNextItem
YItem * scrollDownToNextItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
Definition: NCItemSelector.cc:352
NCItemSelectorBase::scrollUpToPreviousItem
YItem * scrollUpToPreviousItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
Definition: NCItemSelector.cc:375
NCItemSelectorBase::~NCItemSelectorBase
virtual ~NCItemSelectorBase()
Destructor.
Definition: NCItemSelector.cc:76
NCItemSelector::NCItemSelector
NCItemSelector(YWidget *parent, bool enforceSingleSelection)
Constructor.
Definition: NCItemSelector.cc:587
NCPadWidget::myPad
virtual NCPad * myPad() const
Return the current pad.
Definition: NCPadWidget.h:64
NCItemSelectorBase::getLine
const NCTableLine * getLine(int lineNo)
Return the text line with the specified line number.
Definition: NCItemSelector.h:155
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
NCItemSelectorBase::description
std::string description(YItem *item) const
Return the desription text for an item.
Definition: NCItemSelector.cc:280
NCItemSelectorBase::valueChangedNotify
virtual NCursesEvent valueChangedNotify(YItem *item)=0
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
NCItemSelectorBase::wHandleInput
virtual NCursesEvent wHandleInput(wint_t key)
Handle keyboard input.
Definition: NCItemSelector.cc:397
NCItemSelectorBase::createTagCell
virtual NCTableTag * createTagCell(YItem *item)=0
Create a tag cell for an item.
NCItemSelector::deselectAllItemsExcept
void deselectAllItemsExcept(YItem *exceptItem)
Deselect all items except the specified one.
Definition: NCItemSelector.cc:656
NCItemSelectorBase::linesCount
int linesCount() const
Return the number of lines in this widget.
Definition: NCItemSelector.h:120
NCItemSelectorBase::setCurrentItem
virtual void setCurrentItem(YItem *item)
Set the current item, i.e.
Definition: NCItemSelector.cc:182
NCItemSelectorBase::preferredSize
virtual wsze preferredSize()
Return the preferred size for this widget.
Definition: NCItemSelector.cc:107
NCItemSelectorBase::deleteAllItems
virtual void deleteAllItems()
Delete all items.
Definition: NCItemSelector.cc:311
NCursesEvent
Definition: NCurses.h:73
NCItemSelectorBase::currentLine
int currentLine() const
Return number of the current line, i.e.
Definition: NCItemSelector.h:126
NCPadWidget
Base class for widgets with scrollable contents.
Definition: NCPadWidget.h:40
NCItemSelectorBase::findItemLine
int findItemLine(YItem *item) const
Return the line number that contains the first line of 'item' or -1 if not found.
Definition: NCItemSelector.cc:266
NCItemSelectorBase::preferredWidth
virtual int preferredWidth()
Return the preferred width for this widget.
Definition: NCItemSelector.cc:95
NCTableTag
A column (one cell) used as a selection marker: [ ]/[x] or ( )/(x).
Definition: NCTableItem.h:647
NCItemSelectorBase::setKeyboardFocus
virtual bool setKeyboardFocus()
Set the keyboard focus to this widget.
Definition: NCItemSelector.cc:148