libyui-ncurses  2.55.0
NCTablePad.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
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: NCTablePad.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef NCTablePad_h
26 #define NCTablePad_h
27 
28 #include <iosfwd>
29 #include <vector>
30 #include <memory> // unique_ptr
31 
32 #include "NCTableItem.h"
33 #include "NCPad.h"
34 #include "NCstring.h"
35 
36 class NCTableLine;
37 class NCTableCol;
38 
39 
41 {
42 public:
43 
44  NCTableSortStrategyBase() : _column(0), _reverse(false) {}
45  NCTableSortStrategyBase(int column) : _column(column), _reverse(false) {}
46 
47  virtual ~NCTableSortStrategyBase() {}
48 
49  virtual void sort (
50  std::vector<NCTableLine *>::iterator itemsBegin,
51  std::vector<NCTableLine *>::iterator itemsEnd
52  ) = 0;
53 
54  int getColumn () const { return _column; }
55  void setColumn ( int column ) { _column = column; }
56 
57  bool isReverse () const { return _reverse; }
58  void setReverse ( bool reverse ) { _reverse = reverse; }
59 
60 private:
61 
62  int _column;
63  bool _reverse;
64 
65 };
66 
67 
68 
70 {
71 public:
72  virtual void sort ( std::vector<NCTableLine *>::iterator itemsBegin,
73  std::vector<NCTableLine *>::iterator itemsEnd ) override;
74 
75 private:
76  class Compare
77  {
78  public:
79  Compare ( int column, bool reverse )
80  : column(column), reverse(reverse)
81  {}
82 
83  bool operator() ( const NCTableLine * first,
84  const NCTableLine * second ) const;
85 
86  private:
87 
88  // if available returns the sort key otherwise the first line of the label
89  std::wstring smartSortKey( const NCTableLine * tableLine ) const;
90 
91  long long toNumber(const std::wstring& s, bool* ok) const;
92 
93  const int column;
94  const bool reverse;
95  };
96 
97 };
98 
99 
100 
101 class NCTableTag : public NCTableCol
102 {
103 private:
104 
105  YItem *yitem;
106  bool selected;
107  bool single_selection;
108 
109 public:
110 
111  NCTableTag( YItem *item, bool sel = false, bool single_sel = false )
112  : NCTableCol( NCstring( single_sel ? "( )" : "[ ]" ), SEPARATOR )
113  , yitem( item )
114  , selected( sel )
115  , single_selection( single_sel )
116  {
117  // store pointer to this tag in Yitem data
118  yitem->setData( this );
119  }
120 
121  virtual ~NCTableTag() {}
122 
123  virtual void SetLabel( const NClabel & ) { /*NOOP*/; }
124 
125  virtual void DrawAt( NCursesWindow & w, const wrect at,
126  NCTableStyle & tableStyle,
127  NCTableLine::STATE linestate,
128  unsigned colidx ) const
129  {
130  NCTableCol::DrawAt( w, at, tableStyle, linestate, colidx );
131 
132  if ( selected )
133  {
134  setBkgd( w, tableStyle, linestate, DATA );
135  w.addch( at.Pos.L, at.Pos.C + 1, 'x' );
136  }
137  }
138 
139  virtual void SetSelected( bool sel ) { selected = sel; }
140 
141  virtual bool Selected() const { return selected; }
142 
143  virtual bool SingleSelection() const { return single_selection; }
144 
145  YItem *origItem() const { return yitem; }
146 };
147 
148 
149 
150 class NCTablePad : public NCPad
151 {
152 
153  friend std::ostream & operator<<( std::ostream & str, const NCTablePad & obj );
154 
155  NCTablePad & operator=( const NCTablePad & );
156  NCTablePad( const NCTablePad & );
157 
158 private:
159 
160  NCursesPad Headpad;
161  bool dirtyHead;
162  bool dirtyFormat;
163 
164  NCTableStyle ItemStyle;
165  NCTableLine Headline;
166  std::vector<NCTableLine*> Items;
167  wpos citem;
168 
169  std::unique_ptr<NCTableSortStrategyBase> sortStrategy;
170 
171  void assertLine( unsigned idx );
172 
173 protected:
174 
175  void DirtyFormat() { dirty = dirtyFormat = true; }
176 
177  virtual wsze UpdateFormat();
178 
179  virtual int dirtyPad() { return setpos( CurPos() ); }
180 
181  virtual int setpos( const wpos & newpos );
182  virtual int DoRedraw();
183  virtual void updateScrollHint();
184 
185  virtual void directDraw( NCursesWindow & w, const wrect at, unsigned lineno );
186 
187 public:
188 
189  NCTablePad( int lines, int cols, const NCWidget & p );
190  virtual ~NCTablePad();
191 
192 public:
193 
194  virtual void wRecoded();
195 
196  virtual wpos CurPos() const;
197  virtual bool handleInput( wint_t key );
198 
199  bool setItemByKey( int key );
200 
201  wsze tableSize()
202  {
203  return dirtyFormat ? UpdateFormat()
204  : wsze( Lines(), ItemStyle.TableWidth() );
205  }
206 
207  void setOrder( int column, bool do_reverse = false );
208 
209  void sort();
210 
211 public:
212 
213  bool SetHeadline( const std::vector<NCstring> & head );
214 
215  virtual void SendHead()
216  {
217  SetHead( Headpad, srect.Pos.C );
218  dirtyHead = false;
219  }
220 
221  void SetSepChar( const chtype colSepchar )
222  {
223  ItemStyle.SetSepChar( colSepchar );
224  }
225 
226  void SetSepWidth( const unsigned sepwidth )
227  {
228  ItemStyle.SetSepWidth( sepwidth );
229  }
230 
231  void SetHotCol( int hcol )
232  {
233  ItemStyle.SetHotCol( hcol );
234  }
235 
236  unsigned Cols() const { return ItemStyle.Cols(); }
237 
238  unsigned Lines() const { return Items.size(); }
239 
240  unsigned HotCol()const { return ItemStyle.HotCol(); }
241 
242  void SetLines( unsigned idx );
243  void SetLines( std::vector<NCTableLine*> & nItems );
244  void ClearTable() { SetLines( 0 ); }
245 
246  void Append( NCTableLine * item ) { AddLine( Lines(), item ); }
247 
248  void Append( std::vector<NCTableCol*> & nItems, int index = -1 )
249  {
250  AddLine( Lines(), new NCTableLine( nItems, index ) );
251  }
252 
253  void AddLine( unsigned idx, NCTableLine * item );
254  void DelLine( unsigned idx );
255 
256  const NCTableLine * GetLine( unsigned idx ) const;
257  NCTableLine * ModifyLine( unsigned idx );
258 
259  void stripHotkeys();
260 
261  void setSortStrategy ( NCTableSortStrategyBase * newSortStrategy ) // dyn. allocated
262  {
263  if ( newSortStrategy != 0 )
264  sortStrategy.reset ( newSortStrategy );
265  }
266 };
267 
268 
269 #endif // NCTablePad_h
Definition: NCPad.h:94
virtual void directDraw(NCursesWindow &w, const wrect at, unsigned lineno)
Directly draw a table item at a specific location.
Definition: NCTablePad.cc:316
Definition: NCtext.h:82
C++ class for windows.
Definition: ncursesw.h:904
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1227
WINDOW * w
the curses WINDOW
Definition: ncursesw.h:946
static int lines()
Number of lines on terminal, not window.
Definition: ncursesw.h:1041
static int cols()
Number of cols on terminal, not window.
Definition: ncursesw.h:1046
Definition: position.h:110
Definition: position.h:155