libyui-ncurses  2.57.2
NCTablePadBase.h
1 /*
2  Copyright (C) 2020 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: NCTablePadBase.h
20 
21  Authors: Michael Andres <ma@suse.de>
22  Stefan Hundhammer <shundhammer@suse.de>
23 
24 /-*/
25 
26 #ifndef NCTablePadBase_h
27 #define NCTablePadBase_h
28 
29 #include <vector>
30 #include "NCPad.h"
31 #include "NCTableItem.h"
32 
33 class NCTableCol;
34 
35 
36 /**
37  * An NCPad for an NCTable or an NCTree.
38  *
39  * This is an abstract base class for NCTablePad and NCTreePad.
40  *
41  * See also
42  * https://github.com/libyui/libyui-ncurses/blob/master/doc/nctable-and-nctree.md
43  *
44  * Notice that this class is heavily used in other selection widgets like
45  *
46  * - NCFileSelection
47  * - NCItemSelector
48  * - NCMultiSelectionBox
49  * - NCSelectionBox
50  *
51  * And the table classes from libyui-ncurses-pkg:
52  * - NCPkgTable
53  * - NCPkgLocaleTable
54  * - NCPkgRepoTable
55  *
56  * So if you change anything here, make sure to test all those classes.
57  **/
58 class NCTablePadBase : public NCPad
59 {
60  friend std::ostream & operator<<( std::ostream & str, const NCTablePadBase & obj );
61 
62 protected:
63  /**
64  * Constructor. This is protected because this is an abstract base class,
65  * not meant to be instantiated on this level.
66  **/
67  NCTablePadBase( int lines, int cols, const NCWidget & p );
68 
69 public:
70 
71  virtual ~NCTablePadBase();
72 
73  /**
74  * Clear all content.
75  **/
76  void ClearTable();
77 
78  virtual void wRecoded();
79 
80  /// CurPos().L is the index of the selected item
81  virtual wpos CurPos() const;
82 
83  wsze tableSize();
84 
85  /**
86  * Return the number of lines that are currently visible.
87  * This is updated in UpdateFormat().
88  **/
89  unsigned visibleLines() const { return _visibleItems.size(); }
90 
91  bool SetHeadline( const std::vector<NCstring> & head );
92 
93  virtual void SendHead()
94  {
95  SetHead( _headpad, srect.Pos.C );
96  _dirtyHead = false;
97  }
98 
99  void SetSepChar( const chtype colSepchar )
100  {
101  _itemStyle.SetSepChar( colSepchar );
102  }
103 
104  void SetSepWidth( const unsigned sepwidth )
105  {
106  _itemStyle.SetSepWidth( sepwidth );
107  }
108 
109  void SetHotCol( int hcol )
110  {
111  _itemStyle.SetHotCol( hcol );
112  }
113 
114  /**
115  * Return the number of table columns (logical, not screen)
116  */
117  unsigned Cols() const { return _itemStyle.Cols(); }
118 
119  /**
120  * Return the number of table lines (logical, not screen)
121  **/
122  unsigned Lines() const { return _items.size(); }
123 
124  bool empty() const { return _items.empty(); }
125 
126  unsigned HotCol() const { return _itemStyle.HotCol(); }
127 
128  /**
129  * Expand or shrink to have exactly *count* logical lines
130  **/
131  void SetLines( unsigned count );
132 
133  void SetLines( std::vector<NCTableLine*> & newItems );
134 
135  /**
136  * Add *item* at position *idx*, expanding if needed
137  * @param item we take ownership
138  *
139  * @deprecated Used only by Append; undefined behaviour if used after
140  * sorting
141  **/
142  void AddLine( unsigned idx, NCTableLine * item );
143 
144  /**
145  * Add one item to the end of _items.
146  **/
147  void Append( NCTableLine * item ) { AddLine( Lines(), item ); }
148 
149  /**
150  * Create a new item from 'cells' and add it to the end of _items.
151  **/
152  void Append( std::vector<NCTableCol*> & cells, int index )
153  { AddLine( Lines(), new NCTableLine( cells, index ) ); }
154 
155  /**
156  * Return the line at *idx* for read-only operations.
157  **/
158  const NCTableLine * GetLine( unsigned idx ) const;
159 
160  /**
161  * Return line at *idx* for read-write operations and mark it as modified.
162  **/
163  NCTableLine * ModifyLine( unsigned idx );
164 
165  /**
166  * Find the item with index 'idx' in the items and return its position.
167  * Return -1 if not found.
168  **/
169  int findIndex( unsigned idx ) const;
170 
171  /**
172  * Return the current line (the line at the cursor position) or 0 if there
173  * is none.
174  **/
175  NCTableLine * GetCurrentLine() const;
176 
177  /**
178  * Handle a keyboard input event. Return 'true' if the event is now
179  * handled, 'false' if it should be propagated to the parent widget.
180  *
181  * Most of the keys are now handled in the individual items' handlers
182  * (NCTreeLine, NCTableLine). This method is mostly here as a stub for
183  * future extensions.
184  *
185  * Reimplemented from NCPad.
186  **/
187  virtual bool handleInput( wint_t key );
188 
189 
190 private:
191 
192  // Disable unwanted assignment operator and copy constructor
193 
194  NCTablePadBase & operator=( const NCTablePadBase & );
195  NCTablePadBase( const NCTablePadBase & );
196 
197  /**
198  * Return the line with index 'idx' or 0 if not found.
199  **/
200  NCTableLine * getLineWithIndex( unsigned idx ) const;
201 
202 
203 protected:
204 
205  virtual wsze UpdateFormat();
206 
207  /**
208  * Call the current item's handleInput() method and return 'true' if the
209  * event is now handled, 'false' if not.
210  **/
211  virtual bool currentItemHandleInput( wint_t key );
212 
213  /**
214  * Update the internal _visibleItems vector with the items that are
215  * currently visible: Clear the old contents of the vector, iterate over
216  * all lines and check which ones are currently visible.
217  *
218  * This does NOT do a screen update of the visible items!
219  **/
220  void updateVisibleItems();
221 
222  void setFormatDirty() { dirty = _dirtyFormat = true; }
223 
224  virtual int dirtyPad() { return setpos( CurPos() ); }
225 
226  /**
227  * Redraw the pad.
228  *
229  * Reimplemented from NCPad.
230  **/
231  virtual int DoRedraw();
232 
233  /**
234  * Prepare a redraw: Update the format if needed, set the background, clear
235  * the old content.
236  **/
237  virtual void prepareRedraw();
238 
239  /**
240  * Redraw the (visible) content lines one by one.
241  **/
242  virtual void drawContentLines();
243 
244  /**
245  * Redraw the table header.
246  **/
247  virtual void drawHeader();
248 
249  /**
250  * Base function for scrolling: Move the cursor position to 'newPos' and
251  * redraw the old and the new current item with suitable attributes: The
252  * new current item is highlighted, the old one is not.
253  *
254  * Reimplemented from NCPad.
255  **/
256  virtual int setpos( const wpos & newPos );
257 
258  /**
259  * Return the current line number (the cursor position).
260  **/
261  int currentLineNo() const { return _citem.L; }
262 
263  /**
264  * Set the current line number (the cursor position).
265  **/
266  void setCurrentLineNo( int newVal ) { _citem.L = newVal; }
267 
268  /**
269  * Return the current column number (the cursor position).
270  **/
271  int currentColNo() const { return _citem.C; }
272 
273  /**
274  * Set the current column number (the cursor position).
275  **/
276  void setCurrentColNo( int newVal ) { _citem.C = newVal; }
277 
278  /**
279  * Ensure that a line with the specified index exists.
280  * Enlarge or shrink if necessary.
281  **/
282  void assertLine( unsigned index );
283 
284 
285  //
286  // Data members
287  //
288 
289  std::vector<NCTableLine*> _items; ///< (owned)
290  std::vector<NCTableLine*> _visibleItems; ///< not owned
291  NCursesPad _headpad;
292  bool _dirtyHead;
293  bool _dirtyFormat; ///< does table format (size) need recalculating?
294  NCTableStyle _itemStyle;
295  wpos _citem; ///< current/cursor position
296 };
297 
298 
299 #endif // NCTablePadBase_h
NCursesWindow::lines
static int lines()
Number of lines on terminal, not window.
Definition: ncursesw.h:1044
wsze
Screen dimension (screen size) in the order height, width: (H, W)
Definition: position.h:154
NCTablePadBase::ClearTable
void ClearTable()
Clear all content.
Definition: NCTablePadBase.cc:51
NCTableCol
One cell in an NCTableLine with a label and a cell-specific style.
Definition: NCTableItem.h:422
NCTablePadBase::findIndex
int findIndex(unsigned idx) const
Find the item with index 'idx' in the items and return its position.
Definition: NCTablePadBase.cc:100
NCTablePadBase::GetCurrentLine
NCTableLine * GetCurrentLine() const
Return the current line (the line at the cursor position) or 0 if there is none.
Definition: NCTablePadBase.cc:444
NCTablePadBase::SetLines
void SetLines(unsigned count)
Expand or shrink to have exactly count logical lines.
Definition: NCTablePadBase.cc:112
NCTablePadBase::updateVisibleItems
void updateVisibleItems()
Update the internal _visibleItems vector with the items that are currently visible: Clear the old con...
Definition: NCTablePadBase.cc:227
NCTablePadBase::currentColNo
int currentColNo() const
Return the current column number (the cursor position).
Definition: NCTablePadBase.h:271
NCTablePadBase::Lines
unsigned Lines() const
Return the number of table lines (logical, not screen)
Definition: NCTablePadBase.h:122
NCTablePadBase::Append
void Append(NCTableLine *item)
Add one item to the end of _items.
Definition: NCTablePadBase.h:147
NCursesWindow::cols
static int cols()
Number of cols on terminal, not window.
Definition: ncursesw.h:1049
NCTablePadBase::CurPos
virtual wpos CurPos() const
CurPos().L is the index of the selected item.
Definition: NCTablePadBase.cc:188
NCTablePadBase::prepareRedraw
virtual void prepareRedraw()
Prepare a redraw: Update the format if needed, set the background, clear the old content.
Definition: NCTablePadBase.cc:261
NCWidget
Definition: NCWidget.h:46
NCTablePadBase::drawHeader
virtual void drawHeader()
Redraw the table header.
Definition: NCTablePadBase.cc:285
NCTablePadBase::_dirtyFormat
bool _dirtyFormat
does table format (size) need recalculating?
Definition: NCTablePadBase.h:293
NCTablePadBase::ModifyLine
NCTableLine * ModifyLine(unsigned idx)
Return line at idx for read-write operations and mark it as modified.
Definition: NCTablePadBase.cc:93
NCTablePadBase::visibleLines
unsigned visibleLines() const
Return the number of lines that are currently visible.
Definition: NCTablePadBase.h:89
NCTablePadBase::setpos
virtual int setpos(const wpos &newPos)
Base function for scrolling: Move the cursor position to 'newPos' and redraw the old and the new curr...
Definition: NCTablePadBase.cc:301
NCTablePadBase::assertLine
void assertLine(unsigned index)
Ensure that a line with the specified index exists.
Definition: NCTablePadBase.cc:164
NCTablePadBase::GetLine
const NCTableLine * GetLine(unsigned idx) const
Return the line at idx for read-only operations.
Definition: NCTablePadBase.cc:87
NCTablePadBase::currentLineNo
int currentLineNo() const
Return the current line number (the cursor position).
Definition: NCTablePadBase.h:261
NCTablePadBase::_citem
wpos _citem
current/cursor position
Definition: NCTablePadBase.h:295
NCTablePadBase::setCurrentColNo
void setCurrentColNo(int newVal)
Set the current column number (the cursor position).
Definition: NCTablePadBase.h:276
NCTablePadBase::_visibleItems
std::vector< NCTableLine * > _visibleItems
not owned
Definition: NCTablePadBase.h:290
NCPad
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
Definition: NCPad.h:113
NCTablePadBase::drawContentLines
virtual void drawContentLines()
Redraw the (visible) content lines one by one.
Definition: NCTablePadBase.cc:271
wpos
Screen position pair in the order line, column: (L, C)
Definition: position.h:110
NCTablePadBase::currentItemHandleInput
virtual bool currentItemHandleInput(wint_t key)
Call the current item's handleInput() method and return 'true' if the event is now handled,...
Definition: NCTablePadBase.cc:424
NCTablePadBase::handleInput
virtual bool handleInput(wint_t key)
Handle a keyboard input event.
Definition: NCTablePadBase.cc:386
NCTablePadBase::_items
std::vector< NCTableLine * > _items
(owned)
Definition: NCTablePadBase.h:289
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
NCTableStyle::SetSepWidth
void SetSepWidth(const unsigned sepWidth)
total width of space between adjacent columns, including the separator character
Definition: NCTableItem.h:544
NCTableStyle
Styling for a NCTable: column widths, alignment and colors.
Definition: NCTableItem.h:523
NCursesWindow::count
static long count
count of all active windows
Definition: ncursesw.h:941
NCTablePadBase::DoRedraw
virtual int DoRedraw()
Redraw the pad.
Definition: NCTablePadBase.cc:239
NCTablePadBase::NCTablePadBase
NCTablePadBase(int lines, int cols, const NCWidget &p)
Constructor.
Definition: NCTablePadBase.cc:33
NCTablePadBase
An NCPad for an NCTable or an NCTree.
Definition: NCTablePadBase.h:59
NCTablePadBase::Cols
unsigned Cols() const
Return the number of table columns (logical, not screen)
Definition: NCTablePadBase.h:117
NCursesPad
Definition: ncursesw.h:1832
NCTablePadBase::setCurrentLineNo
void setCurrentLineNo(int newVal)
Set the current line number (the cursor position).
Definition: NCTablePadBase.h:266
NCTablePadBase::Append
void Append(std::vector< NCTableCol * > &cells, int index)
Create a new item from 'cells' and add it to the end of _items.
Definition: NCTablePadBase.h:152
NCPad::srect
wrect srect
Source rectangle: the visible part of this pad.
Definition: NCPad.h:138
NCTablePadBase::AddLine
void AddLine(unsigned idx, NCTableLine *item)
Add item at position idx, expanding if needed.
Definition: NCTablePadBase.cc:154