libyui-ncurses  2.47.4
 All Classes Functions Variables
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> // auto_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  NCTableSortStrategyBase( ) { _uiColumn = -1; }
44 
45  virtual ~NCTableSortStrategyBase() {}
46 
47  virtual void sort (
48  std::vector<NCTableLine *>::iterator itemsBegin,
49  std::vector<NCTableLine *>::iterator itemsEnd,
50  int uiColumn
51  ) = 0;
52  int getColumn () { return _uiColumn; }
53  void setColumn ( int column) { _uiColumn = column; }
54 
55 private:
56  int _uiColumn;
57 
58 };
59 
61 public:
62  virtual void sort (
63  std::vector<NCTableLine *>::iterator itemsBegin,
64  std::vector<NCTableLine *>::iterator itemsEnd,
65  int uiColumn
66  )
67  {
68  std::sort ( itemsBegin, itemsEnd, Compare(uiColumn) );
69  }
70 
71 private:
72  class Compare
73  {
74  public:
75  Compare ( int uiCol)
76  : _uiCol ( uiCol )
77  {}
78 
79  bool operator() ( NCTableLine * first,
80  NCTableLine * second
81  ) const
82  {
83  std::wstring w1 = first->GetCol( _uiCol )->Label().getText().begin()->str();
84  std::wstring w2 = second->GetCol( _uiCol )->Label().getText().begin()->str();
85  wchar_t *endptr1 = 0;
86  wchar_t *endptr2 = 0;
87 
88  long int number1 = std::wcstol( w1.c_str(), &endptr1, 10 );
89  long int number2 = std::wcstol( w2.c_str(), &endptr2, 10 );
90 
91  if ( *endptr1 == L'\0' && *endptr2 == L'\0' )
92  {
93  // both are numbers
94  return number1 < number2;
95  }
96  else
97  {
98  // compare strings using collating information
99  int result = std::wcscoll ( w1.c_str(), w2.c_str() );
100 
101  return result < 0;
102  }
103  }
104 
105  private:
106  int _uiCol;
107  };
108 
109 
110 };
111 
112 class NCTableTag : public NCTableCol
113 {
114 private:
115 
116  YItem *yitem;
117  bool selected;
118 
119 public:
120 
121  NCTableTag( YItem *item, const bool sel = false )
122  : NCTableCol( NCstring( "[ ]" ), SEPARATOR )
123  , yitem( item )
124  , selected( sel )
125  {
126  //store pointer to this tag in Yitem data
127  yitem->setData( this );
128  }
129 
130  virtual ~NCTableTag() {}
131 
132  virtual void SetLabel( const NCstring & ) { /*NOOP*/; }
133 
134  virtual void DrawAt( NCursesWindow & w, const wrect at,
135  NCTableStyle & tableStyle,
136  NCTableLine::STATE linestate,
137  unsigned colidx ) const
138  {
139  NCTableCol::DrawAt( w, at, tableStyle, linestate, colidx );
140 
141  if ( selected )
142  {
143  setBkgd( w, tableStyle, linestate, DATA );
144  w.addch( at.Pos.L, at.Pos.C + 1, 'x' );
145  }
146  }
147 
148  void SetSelected( const bool sel ) { selected = sel; }
149 
150  bool Selected() const { return selected; }
151 
152  YItem *origItem() { return yitem; }
153 };
154 
155 class NCTablePad : public NCPad
156 {
157 
158  friend std::ostream & operator<<( std::ostream & STREAM, const NCTablePad & OBJ );
159 
160  NCTablePad & operator=( const NCTablePad & );
161  NCTablePad( const NCTablePad & );
162 
163 private:
164 
165  NCursesPad Headpad;
166  bool dirtyHead;
167  bool dirtyFormat;
168 
169  NCTableStyle ItemStyle;
170  NCTableLine Headline;
171  std::vector<NCTableLine*> Items;
172  wpos citem;
173 
174  std::auto_ptr<NCTableSortStrategyBase> sortStrategy;
175 
176  void assertLine( unsigned idx );
177 
178 protected:
179 
180  void DirtyFormat() { dirty = dirtyFormat = true; }
181 
182  virtual wsze UpdateFormat();
183 
184  virtual int dirtyPad() { return setpos( CurPos() ); }
185 
186  virtual int setpos( const wpos & newpos );
187  virtual int DoRedraw();
188  virtual void updateScrollHint();
189 
190  virtual void directDraw( NCursesWindow & w, const wrect at, unsigned lineno );
191 
192 public:
193 
194  NCTablePad( int lines, int cols, const NCWidget & p );
195  virtual ~NCTablePad();
196 
197 public:
198 
199  virtual void wRecoded();
200 
201  virtual wpos CurPos() const;
202  virtual bool handleInput( wint_t key );
203 
204  bool setItemByKey( int key );
205 
206  wsze tableSize()
207  {
208  return dirtyFormat ? UpdateFormat()
209  : wsze( Lines(), ItemStyle.TableWidth() );
210  }
211 
212  void setOrder( int column, bool do_reverse = false );
213 
214 public:
215 
216  bool SetHeadline( const std::vector<NCstring> & head );
217 
218  virtual void SendHead()
219  {
220  SetHead( Headpad, srect.Pos.C );
221  dirtyHead = false;
222  }
223 
224  void SetSepChar( const chtype colSepchar )
225  {
226  ItemStyle.SetSepChar( colSepchar );
227  }
228 
229  void SetSepWidth( const unsigned sepwidth )
230  {
231  ItemStyle.SetSepWidth( sepwidth );
232  }
233 
234  void SetHotCol( const int hcol )
235  {
236  ItemStyle.SetHotCol( hcol );
237  }
238 
239  unsigned Cols() const { return ItemStyle.Cols(); }
240 
241  unsigned Lines() const { return Items.size(); }
242 
243  unsigned HotCol()const { return ItemStyle.HotCol(); }
244 
245  void SetLines( unsigned idx );
246  void SetLines( std::vector<NCTableLine*> & nItems );
247  void ClearTable() { SetLines( 0 ); }
248 
249  void Append( NCTableLine * item ) { AddLine( Lines(), item ); }
250 
251  void Append( std::vector<NCTableCol*> & nItems, int index = -1 )
252  {
253  AddLine( Lines(), new NCTableLine( nItems, index ) );
254  }
255 
256  void AddLine( unsigned idx, NCTableLine * item );
257  void DelLine( unsigned idx );
258 
259  const NCTableLine * GetLine( unsigned idx ) const;
260  NCTableLine * ModifyLine( unsigned idx );
261 
262  void stripHotkeys();
263 
264  void setSortStrategy ( NCTableSortStrategyBase * newSortStrategy ) // dyn. allocated
265  {
266  if ( newSortStrategy != 0 )
267  sortStrategy.reset ( newSortStrategy );
268  }
269 };
270 
271 
272 #endif // NCTablePad_h
C++ class for windows.
Definition: ncursesw.h:904
static int lines()
Number of lines on terminal, not window.
Definition: ncursesw.h:1042
Definition: NCPad.h:93
static int cols()
Number of cols on terminal, not window.
Definition: ncursesw.h:1047
Definition: position.h:109
virtual void directDraw(NCursesWindow &w, const wrect at, unsigned lineno)
Directly draw a table item at a specific location.
Definition: NCTablePad.cc:243
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1228
Definition: position.h:154
WINDOW * w
the curses WINDOW
Definition: ncursesw.h:947