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