libyui-ncurses  2.55.0
NCTableItem.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: NCTableItem.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef NCTableItem_h
26 #define NCTableItem_h
27 
28 #include <iosfwd>
29 #include <vector>
30 
31 #include "position.h"
32 #include "NCWidget.h"
33 #include <yui/YTableItem.h>
34 
35 class NCTableStyle;
36 class NCTableCol;
37 
38 
40 {
41 
42  friend std::ostream & operator<<( std::ostream & str, const NCTableLine & obj );
43 
44  NCTableLine & operator=( const NCTableLine & );
45  NCTableLine( const NCTableLine & );
46 
47 public:
48 
49  enum STATE
50  {
51  S_NORMAL = 0x00,
52  S_ACTIVE = 0x01,
53  S_DISABELED = 0x10,
54  S_HIDDEN = 0x20,
55  S_HEADLINE = 0x40
56  };
57 
58 
59 private:
60 
61  std::vector<NCTableCol*> Items;
62 
63  void assertCol( unsigned idx );
64 
65  unsigned state;
66 
67  int index;
68 
69  YTableItem *yitem;
70 
71 
72 protected:
73 
74  mutable STATE vstate;
75 
76 
77  virtual void DrawItems( NCursesWindow & w, const wrect at,
78  NCTableStyle & tableStyle,
79  bool active ) const;
80 
81 public:
82 
83  NCTableLine( unsigned cols, int index = -1, const unsigned s = S_NORMAL );
84  NCTableLine( std::vector<NCTableCol*> & nItems, int index = -1, const unsigned s = S_NORMAL );
85  void setOrigItem( YTableItem *it );
86  YTableItem *origItem() const { return yitem; }
87 
88  virtual ~NCTableLine();
89 
90  unsigned Cols() const { return Items.size(); }
91 
92  void SetCols( unsigned idx );
93  void SetCols( std::vector<NCTableCol*> & nItems );
94  void ClearLine() { SetCols( 0 ); }
95 
96  std::vector<NCTableCol*> GetItems() const { return Items; }
97 
98  void Append( NCTableCol * item ) { AddCol( Cols(), item ); }
99 
100  void AddCol( unsigned idx, NCTableCol * item );
101  void DelCol( unsigned idx );
102 
103  NCTableCol * GetCol( unsigned idx );
104  const NCTableCol * GetCol( unsigned idx ) const
105  {
106  return const_cast<NCTableLine*>( this )->GetCol( idx );
107  }
108 
109  void SetState( const STATE s ) { state |= s; }
110 
111  void ClearState( const STATE s ) { state &= ~s; }
112 
113  bool isHidden() const { return ( state & S_HIDDEN ); }
114 
115  bool isDisabeled() const { return ( state & S_DISABELED ); }
116 
117  bool isSpecial() const { return ( state & ( S_HIDDEN | S_DISABELED ) ); }
118 
119  bool isActive() const { return ( state & S_ACTIVE ); }
120 
121  virtual bool isVisible() const { return !isHidden(); }
122 
123  virtual bool isEnabeled() const { return isVisible() && !isDisabeled(); }
124 
125  int getIndex() const { return index; }
126 
127 public:
128 
129  virtual int handleInput( wint_t key ) { return 0; }
130 
131  virtual int ChangeToVisible() { return 0; }
132 
133  virtual unsigned Hotspot( unsigned & at ) const { at = 0; return 0; }
134 
135  virtual void UpdateFormat( NCTableStyle & TableStyle );
136 
137  virtual void DrawAt( NCursesWindow & w, const wrect at,
138  NCTableStyle & tableStyle,
139  bool active ) const;
140 
141  void stripHotkeys();
142 };
143 
144 
145 
147 {
148 
149  friend std::ostream & operator<<( std::ostream & str, const NCTableCol & obj );
150 
151 public:
152 
153  enum STYLE
154  {
155  NONE = 0, // use current bg
156  PLAIN, // plain text
157  DATA, // data style
158  ACTIVEDATA, // data style if line active, else plain
159  HINT, // hint
160  SEPARATOR // separator
161  };
162 
163 private:
164 
165  NClabel label;
166  STYLE style;
167 
168 public:
169 
170  NCTableCol( const NCstring & l = "", STYLE st = ACTIVEDATA );
171  virtual ~NCTableCol();
172 
173  const NClabel & Label() const { return label; }
174 
175  virtual void SetLabel( const NClabel & l ) { label = l; }
176 
177  void stripHotkey() { label.stripHotkey(); }
178 
179 protected:
180 
181  chtype setBkgd( NCursesWindow & w,
182  NCTableStyle & tableStyle,
183  NCTableLine::STATE linestate,
184  STYLE colstyle ) const ;
185 
186 public:
187 
188  virtual wsze Size() const { return wsze( 1, label.width() ); }
189 
190  virtual void DrawAt( NCursesWindow & w, const wrect at,
191  NCTableStyle & tableStyle,
192  NCTableLine::STATE linestate,
193  unsigned colidx ) const;
194 
195  bool hasHotkey() const { return label.hasHotkey(); }
196 
197  unsigned char hotkey() const { return label.hotkey(); }
198 };
199 
200 
201 
202 class NCTableHead : public NCTableLine
203 {
204 
205 public:
206 
207  NCTableHead( unsigned cols ) : NCTableLine( cols ) {}
208 
209  NCTableHead( std::vector<NCTableCol*> & nItems ) : NCTableLine( nItems ) {}
210 
211  virtual ~NCTableHead() {}
212 
213 public:
214 
215  virtual void DrawAt( NCursesWindow & w, const wrect at,
216  NCTableStyle & tableStyle,
217  bool active ) const;
218 };
219 
220 
221 
223 {
224 
225  friend std::ostream & operator<<( std::ostream & str, const NCTableStyle & obj );
226 
227 private:
228 
229  NCTableHead headline;
230  std::vector<unsigned> colWidth;
231  std::vector<NC::ADJUST> colAdjust;
232 
233  const NCWidget & parw;
234 
235  unsigned colSepwidth;
236  chtype colSepchar;
237  unsigned hotCol;
238 
239 public:
240 
241  static const chtype currentBG = ( chtype ) - 1;
242 
243  NCTableStyle( const NCWidget & p );
244  ~NCTableStyle() {}
245 
246  bool SetStyleFrom( const std::vector<NCstring> & head );
247  void SetSepChar( const chtype sepchar ) { colSepchar = sepchar; }
248 
249  void SetSepWidth( const unsigned sepwidth ) { colSepwidth = sepwidth; }
250 
251  void SetHotCol( int hcol )
252  {
253  hotCol = ( hcol < 0 || Cols() <= ( unsigned )hcol ) ? -1 : hcol;
254  }
255 
256  void ResetToMinCols()
257  {
258  colWidth.clear();
259  AssertMinCols( headline.Cols() );
260  headline.UpdateFormat( *this );
261  }
262 
263  void AssertMinCols( unsigned num )
264  {
265  if ( colWidth.size() < num )
266  {
267  colWidth.resize( num, 0 );
268  colAdjust.resize( colWidth.size(), NC::LEFT );
269  }
270  }
271 
272  void MinColWidth( unsigned num, unsigned val )
273  {
274  AssertMinCols( num );
275 
276  if ( val > colWidth[num] )
277  colWidth[num] = val;
278  }
279 
280  NC::ADJUST ColAdjust( unsigned num ) const { return colAdjust[num]; }
281 
282  unsigned Cols() const { return colWidth.size(); }
283 
284  unsigned ColWidth( unsigned num ) const { return colWidth[num]; }
285 
286  unsigned ColSepwidth() const { return colSepwidth; }
287 
288  chtype ColSepchar() const { return colSepchar; }
289 
290  unsigned HotCol() const { return hotCol; }
291 
292  const NCstyle::StList & listStyle() const { return parw.listStyle(); }
293 
294  chtype getBG() const { return listStyle().item.plain; }
295 
296  chtype getBG( const NCTableLine::STATE lstate,
297  const NCTableCol::STYLE cstyle = NCTableCol::PLAIN ) const;
298 
299  chtype highlightBG( const NCTableLine::STATE lstate,
300  const NCTableCol::STYLE cstyle,
301  const NCTableCol::STYLE dstyle = NCTableCol::PLAIN ) const ;
302 
303  chtype hotBG( const NCTableLine::STATE lstate, unsigned colidx ) const
304  {
305  return ( colidx == hotCol ) ? getBG( lstate, NCTableCol::HINT ) : currentBG;
306  }
307 
308  const NCTableLine & Headline() const { return headline; }
309 
310  unsigned TableWidth() const
311  {
312  unsigned twidth = 0;
313 
314  for ( unsigned i = 0; i < Cols(); ++i )
315  twidth += colWidth[i];
316 
317  if ( Cols() > 1 )
318  twidth += colSepwidth * ( Cols() - 1 );
319 
320  return twidth;
321  }
322 };
323 
324 
325 #endif // NCTableItem_h
wsze
Definition: position.h:155
NCstring
Definition: NCstring.h:33
NCTableCol
Definition: NCTableItem.h:147
NCursesWindow
C++ class for windows.
Definition: ncursesw.h:904
NCTableHead
Definition: NCTableItem.h:203
NClabel
Definition: NCtext.h:82
NCWidget
Definition: NCWidget.h:46
NCTableLine
Definition: NCTableItem.h:40
NCTableStyle
Definition: NCTableItem.h:223
NCstyle::StList
Definition: NCstyle.h:367
wrect
Definition: position.h:198