libyui-ncurses  2.57.2
NCTree.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  Copyright (C) 2020 SUSE LLC
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 
18 /*-/
19 
20  File: NCTree.h
21 
22  Authors: Michael Andres <ma@suse.de>
23  Stefan Hundhammer <shundhammer@suse.de>
24 
25 /-*/
26 
27 #ifndef NCTree_h
28 #define NCTree_h
29 
30 #include <iosfwd>
31 
32 #include <yui/YTree.h>
33 #include "NCPadWidget.h"
34 #include "NCTreePad.h"
35 #include "NCTablePad.h"
36 
37 class NCTreeLine;
38 
39 /**
40  * A tree selection widget with one-column tree items and optionally multy
41  * selection.
42  *
43  * See also
44  * https://github.com/libyui/libyui-ncurses/blob/master/doc/nctable-and-nctree.md
45  **/
46 class NCTree : public YTree, public NCPadWidget
47 {
48  friend std::ostream & operator<<( std::ostream & str, const NCTree & obj );
49 
50 public:
51 
52  NCTree( YWidget * parent,
53  const std::string & label,
54  bool multiselection = false,
55  bool recursiveselection = false );
56 
57  virtual ~NCTree();
58 
59  /**
60  * Recursively build the tree in this widget according to the items.
61  *
62  * Implemented from YTree.
63  **/
64  virtual void rebuildTree();
65 
66  /**
67  * Return a pointer to the current item (the item under the cursor).
68  **/
69  virtual YTreeItem * getCurrentItem() const;
70 
71  /**
72  * Get the current item. This is an alias for getCurrentItem.
73  *
74  * Implemented from YSelectionWidget.
75  **/
76  virtual YTreeItem * currentItem();
77 
78  virtual void deselectAllItems();
79 
80  /**
81  * Select or deselect an item.
82  *
83  * Implemented from YSelectionWidget.
84  **/
85  virtual void selectItem( YItem *item, bool selected );
86 
87  /**
88  * Select the item with the specified index.
89  **/
90  virtual void selectItem( int index );
91 
92  /**
93  * Delete all items and clear the TreePad.
94  *
95  * Implemented from YSelectionWidget.
96  **/
97  virtual void deleteAllItems();
98 
99  /**
100  * libyui geometry management:
101  * Return the preferred width for this widget.
102  *
103  * Implemented from YWidget.
104  **/
105  virtual int preferredWidth();
106 
107  /**
108  * libyui geometry management:
109  * Return the preferred height for this widget.
110  *
111  * Implemented from YWidget.
112  **/
113  virtual int preferredHeight();
114 
115  /**
116  * libyui geometry management:
117  * Apply the width and height assigned from the parent layout widget.
118  *
119  * Implemented from YWidget.
120  **/
121  virtual void setSize( int newWidth, int newHeight );
122 
123  /**
124  * Set the label (the caption) above the tree.
125  *
126  * Implemented from YTree.
127  **/
128  virtual void setLabel( const std::string & nlabel );
129 
130  /**
131  * Enable or disable this widget.
132  *
133  * Implemented from YWidget.
134  **/
135  virtual void setEnabled( bool do_bv );
136 
137  /**
138  * Set the keyboard focus to this widget.
139  *
140  * Implemented from YWidget.
141  **/
142  virtual bool setKeyboardFocus()
143  {
144  if ( !grabFocus() )
145  return YWidget::setKeyboardFocus();
146 
147  return true;
148  }
149 
150  /**
151  * Activate the item selected in the tree.
152  * This can be used in tests to simulate user input.
153  *
154  * Implemented from YSelectionWidget.
155  **/
156  virtual void activate();
157 
158  /**
159  * Keyboard input handler.
160  *
161  * Implemented from NCWidget.
162  **/
163  virtual NCursesEvent wHandleInput( wint_t key );
164 
165 
166 protected:
167 
168  /**
169  * Code location for logging.
170  *
171  * Implemented from NCWidget.
172  **/
173  virtual const char * location() const { return "NCTree"; }
174 
175  /**
176  * Create an empty pad.
177  **/
178  virtual NCPad * CreatePad();
179 
180  /**
181  * Return the TreePad that belongs to this widget.
182  *
183  * Overloaded from NCPadWidget to narrow the type to the actual one used in
184  * this widget.
185  **/
186  virtual NCTreePad * myPad() const
187  { return dynamic_cast<NCTreePad*>( NCPadWidget::myPad() ); }
188 
189  /**
190  * Fill the TreePad with lines (using CreateTreeLines to create them)
191  **/
192  virtual void DrawPad();
193 
194 
195  /**
196  * Return a const pointer to the tree line at the specified index for
197  * read-only operations.
198  **/
199  const NCTreeLine * getTreeLine( unsigned idx ) const;
200 
201  /**
202  * Return a non-const pointer to the tree line at the specified index for
203  * read-write operations. This also marks that line as "dirty", i.e. it
204  * needs to be updated on the screen.
205  **/
206  NCTreeLine * modifyTreeLine( unsigned idx );
207 
208  /**
209  * Optimization for NCurses from libyui:
210  * Notification that multiple changes are about to come.
211  *
212  * Implemented from YWidget.
213  **/
214  virtual void startMultipleChanges() { startMultidraw(); }
215 
216  /**
217  * Optimization for NCurses from libyui:
218  * Notification that multiple changes are now finished.
219  *
220  * Implemented from YWidget.
221  **/
222  virtual void doneMultipleChanges() { stopMultidraw(); }
223 
224  /**
225  * Create TreeLines and append them to the TreePad.
226  * If 'item' has any children, this is called recursively for them.
227  **/
228  void CreateTreeLines( NCTreeLine * parentLine,
229  NCTreePad * pad,
230  YItem * item );
231 
232 private:
233 
234  // Disable unwanted assignment operator and copy constructor
235 
236  NCTree & operator=( const NCTree & );
237  NCTree( const NCTree & );
238 
239 
240  //
241  // Data members
242  //
243 
244  bool _multiSelect;
245  int _nextItemIndex; // Only used in CreateTreeLines()
246 };
247 
248 
249 /**
250  * One line in a tree widdget.
251  *
252  * This is just a very thin wrapper around NCTableLine which provides most of
253  * the functionality.
254  *
255  * Notice that on the libyui level, the inheritance hierarchy is
256  * YTableItem < YTreeItem < YItem
257  * whereas on the libyui-ncurses level, it is
258  * NCTreeLine < NCTableLine
259  * i.e. just the other way round.
260  **/
261 class NCTreeLine : public NCTableLine
262 {
263 public:
264 
265  NCTreeLine( NCTreeLine * parentLine,
266  YTreeItem * origItem,
267  bool multiSelection );
268 
269  virtual ~NCTreeLine();
270 
271 public:
272 
273  /**
274  * Return the corresponding YTreeItem.
275  **/
276  YTreeItem * YItem() const { return dynamic_cast<YTreeItem *>( _yitem ); }
277 
278  /**
279  * Change a line that may have been invisible until now to be visible.
280  *
281  * This also makes the parent lines (and its parent line until the
282  * toplevel) visible as well as all sibling lines of this line.
283  *
284  * Return 'true' if there was a status change, i.e. if it was invisible
285  * before, 'false' otherwise.
286  *
287  * Reimplemented from NCTableLine.
288  **/
289  virtual bool ChangeToVisible();
290 
291  virtual unsigned Hotspot( unsigned & at ) const;
292 
293  /**
294  * Handle keyboard input. Return 'true' if the key event is handled,
295  * 'false' to propagate it up to the pad.
296  **/
297  virtual bool handleInput( wint_t key );
298 
299 
300  //
301  // Some covariants of the tree operations returning this derived class
302  //
303 
304  virtual NCTreeLine * parent() const { return dynamic_cast<NCTreeLine *>( _parent ); }
305  virtual NCTreeLine * firstChild() const { return dynamic_cast<NCTreeLine *>( _firstChild ); }
306  virtual NCTreeLine * nextSibling() const { return dynamic_cast<NCTreeLine *>( _nextSibling ); }
307 
308 
309 private:
310 
311  //
312  // Data members
313  //
314 
315  bool _multiSelect;
316 };
317 
318 
319 #endif // NCTree_h
NCTree
A tree selection widget with one-column tree items and optionally multy selection.
Definition: NCTree.h:47
NCTree::getTreeLine
const NCTreeLine * getTreeLine(unsigned idx) const
Return a const pointer to the tree line at the specified index for read-only operations.
Definition: NCTree.cc:66
NCTreeLine::handleInput
virtual bool handleInput(wint_t key)
Handle keyboard input.
Definition: NCTree.cc:484
NCTreePad
An NCPad for an NCTree.
Definition: NCTreePad.h:50
NCTreeLine::ChangeToVisible
virtual bool ChangeToVisible()
Change a line that may have been invisible until now to be visible.
Definition: NCTree.cc:443
NCTree::setLabel
virtual void setLabel(const std::string &nlabel)
Set the label (the caption) above the tree.
Definition: NCTree.cc:216
NCTree::startMultipleChanges
virtual void startMultipleChanges()
Optimization for NCurses from libyui: Notification that multiple changes are about to come.
Definition: NCTree.h:214
NCTree::deleteAllItems
virtual void deleteAllItems()
Delete all items and clear the TreePad.
Definition: NCTree.cc:405
NCTree::location
virtual const char * location() const
Code location for logging.
Definition: NCTree.h:173
NCTreeLine::YItem
YTreeItem * YItem() const
Return the corresponding YTreeItem.
Definition: NCTree.h:276
NCTree::myPad
virtual NCTreePad * myPad() const
Return the TreePad that belongs to this widget.
Definition: NCTree.h:186
NCPad
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
Definition: NCPad.h:113
NCTree::selectItem
virtual void selectItem(YItem *item, bool selected)
Select or deselect an item.
Definition: NCTree.cc:146
NCTreeLine
One line in a tree widdget.
Definition: NCTree.h:262
NCTree::DrawPad
virtual void DrawPad()
Fill the TreePad with lines (using CreateTreeLines to create them)
Definition: NCTree.cc:297
NCTree::rebuildTree
virtual void rebuildTree()
Recursively build the tree in this widget according to the items.
Definition: NCTree.cc:223
NCTableLine::origItem
YTableItem * origItem() const
Return the YItem this line corresponds to.
Definition: NCTableItem.h:127
NCTree::preferredWidth
virtual int preferredWidth()
libyui geometry management: Return the preferred width for this widget.
Definition: NCTree.cc:85
NCTree::currentItem
virtual YTreeItem * currentItem()
Get the current item.
Definition: NCTree.cc:291
NCTree::setSize
virtual void setSize(int newWidth, int newHeight)
libyui geometry management: Apply the width and height assigned from the parent layout widget.
Definition: NCTree.cc:106
NCTree::doneMultipleChanges
virtual void doneMultipleChanges()
Optimization for NCurses from libyui: Notification that multiple changes are now finished.
Definition: NCTree.h:222
NCTree::activate
virtual void activate()
Activate the item selected in the tree.
Definition: NCTree.cc:395
NCPadWidget::myPad
virtual NCPad * myPad() const
Return the current pad.
Definition: NCPadWidget.h:64
NCTree::getCurrentItem
virtual YTreeItem * getCurrentItem() const
Return a pointer to the current item (the item under the cursor).
Definition: NCTree.cc:112
NCTree::CreateTreeLines
void CreateTreeLines(NCTreeLine *parentLine, NCTreePad *pad, YItem *item)
Create TreeLines and append them to the TreePad.
Definition: NCTree.cc:240
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
NCTree::setKeyboardFocus
virtual bool setKeyboardFocus()
Set the keyboard focus to this widget.
Definition: NCTree.h:142
NCTree::setEnabled
virtual void setEnabled(bool do_bv)
Enable or disable this widget.
Definition: NCTree.cc:99
NCTableLine::_yitem
YItem * _yitem
not owned
Definition: NCTableItem.h:387
NCTree::wHandleInput
virtual NCursesEvent wHandleInput(wint_t key)
Keyboard input handler.
Definition: NCTree.cc:321
NCTree::CreatePad
virtual NCPad * CreatePad()
Create an empty pad.
Definition: NCTree.cc:230
NCursesEvent
Definition: NCurses.h:73
NCTree::modifyTreeLine
NCTreeLine * modifyTreeLine(unsigned idx)
Return a non-const pointer to the tree line at the specified index for read-write operations.
Definition: NCTree.cc:76
NCPadWidget
Base class for widgets with scrollable contents.
Definition: NCPadWidget.h:40
NCTree::preferredHeight
virtual int preferredHeight()
libyui geometry management: Return the preferred height for this widget.
Definition: NCTree.cc:92