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