libyui-ncurses  2.44.1
 All Classes Functions Variables
NCTableItem.cc
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.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCTableItem.h"
28 #include "stringutil.h"
29 #include "stdutil.h"
30 
31 using stdutil::form;
32 
33 
34 NCTableCol::NCTableCol( const NCstring & l, const STYLE & st )
35  : label( l )
36  , style( st )
37 {
38 }
39 
40 
41 NCTableCol::~NCTableCol()
42 {
43 }
44 
45 
46 chtype NCTableCol::setBkgd( NCursesWindow & w,
47  NCTableStyle & tableStyle,
48  NCTableLine::STATE linestate,
49  STYLE colstyle ) const
50 {
51  chtype bkgdstyle = tableStyle.getBG( linestate, colstyle );
52 
53  if ( bkgdstyle != NCTableStyle::currentBG )
54  w.bkgdset( bkgdstyle );
55  else
56  bkgdstyle = w.getbkgd();
57 
58  return bkgdstyle;
59 }
60 
61 
62 void NCTableCol::DrawAt( NCursesWindow & w, const wrect at,
63  NCTableStyle & tableStyle,
64  NCTableLine::STATE linestate,
65  unsigned colidx ) const
66 {
67  chtype bg = setBkgd( w, tableStyle, linestate, style );
68  chtype hbg = tableStyle.hotBG( linestate, colidx );
69 
70  if ( hbg == NCTableStyle::currentBG )
71  hbg = bg;
72 
73  label.drawAt( w, bg, hbg, at, tableStyle.ColAdjust( colidx ) );
74 }
75 
76 
77 std::ostream & operator<<( std::ostream & STREAM, const NCTableCol & OBJ )
78 {
79  return STREAM << OBJ.label;
80 }
81 
82 
83 
84 
85 
86 NCTableLine::NCTableLine( unsigned cols, int idx, const unsigned s )
87  : Items( cols, ( NCTableCol* )0 )
88  , state( s )
89  , index( idx )
90  , vstate( S_HIDDEN )
91 {
92 }
93 
94 
95 NCTableLine::NCTableLine( std::vector<NCTableCol*> & nItems, int idx, const unsigned s )
96  : Items( nItems )
97  , state( s )
98  , index( idx )
99  , vstate( S_HIDDEN )
100 {
101 }
102 
103 
104 void NCTableLine::setOrigItem( YTableItem *it )
105 {
106  yitem = it;
107  yitem->setData( this ) ;
108 }
109 
110 
111 NCTableLine::~NCTableLine()
112 {
113  ClearLine();
114 }
115 
116 
117 void NCTableLine::assertCol( unsigned idx )
118 {
119  if ( idx >= Cols() )
120  SetCols( idx + 1 );
121 }
122 
123 
124 void NCTableLine::SetCols( unsigned idx )
125 {
126  if ( idx == Cols() )
127  return;
128 
129  if ( idx < Cols() )
130  {
131  for ( unsigned i = idx; i < Cols(); ++i )
132  {
133  delete Items[i];
134  }
135  }
136 
137  Items.resize( idx, 0 );
138 }
139 
140 
141 void NCTableLine::stripHotkeys()
142 {
143  for ( unsigned i = 0; i < Cols(); ++i )
144  {
145  if ( Items[i] )
146  Items[i]->stripHotkey();
147  }
148 }
149 
150 
151 
152 void NCTableLine::SetCols( std::vector<NCTableCol*> & nItems )
153 {
154  SetCols( 0 );
155  Items = nItems;
156 }
157 
158 
159 void NCTableLine::AddCol( unsigned idx, NCTableCol * item )
160 {
161  assertCol( idx );
162  delete Items[idx];
163  Items[idx] = item;
164 }
165 
166 
167 void NCTableLine::DelCol( unsigned idx )
168 {
169  if ( idx < Cols() )
170  {
171  delete Items[idx];
172  Items[idx] = 0;
173  }
174 }
175 
176 
177 NCTableCol * NCTableLine::GetCol( unsigned idx )
178 {
179  if ( idx < Cols() )
180  return Items[idx];
181 
182  return 0;
183 }
184 
185 
186 void NCTableLine::UpdateFormat( NCTableStyle & tableStyle )
187 {
188  tableStyle.AssertMinCols( Cols() );
189 
190  for ( unsigned c = 0; c < Cols(); ++c )
191  {
192  if ( !Items[c] )
193  continue;
194 
195  tableStyle.MinColWidth( c, Items[c]->Size().W );
196  }
197 }
198 
199 
200 void NCTableLine::DrawAt( NCursesWindow & w, const wrect at,
201  NCTableStyle & tableStyle,
202  bool active ) const
203 {
204  vstate = S_HIDDEN;
205 
206  if ( isVisible() )
207  {
208  if ( isDisabeled() )
209  vstate = S_DISABELED;
210  else
211  vstate = active ? S_ACTIVE : S_NORMAL;
212  }
213 
214  w.bkgdset( tableStyle.getBG( vstate ) );
215 
216  for ( int l = 0; l < at.Sze.H; ++l )
217  {
218  w.move( at.Pos.L + l, at.Pos.C );
219  w.clrtoeol();
220  }
221 
222  DrawItems( w, at, tableStyle, active );
223 }
224 
225 
226 void NCTableLine::DrawItems( NCursesWindow & w, const wrect at,
227  NCTableStyle & tableStyle,
228  bool active ) const
229 {
230  if ( !( at.Sze > wsze( 0 ) ) )
231  return;
232 
233  wrect lRect( at );
234 
235  unsigned destWidth;
236 
237  for ( unsigned c = 0; c < Cols(); ++c )
238  {
239 
240  if ( c && tableStyle.ColSepwidth() )
241  {
242  // draw centered
243  destWidth = tableStyle.ColSepwidth() / 2;
244 
245  if ( destWidth < ( unsigned )lRect.Sze.W )
246  {
247  w.bkgdset( tableStyle.getBG( vstate, NCTableCol::SEPARATOR ) );
248  w.vline( lRect.Pos.L, lRect.Pos.C + destWidth,
249  lRect.Sze.H, tableStyle.ColSepchar() );
250  // skip over
251  destWidth = tableStyle.ColSepwidth();
252 
253  if (( unsigned )lRect.Sze.W <= destWidth )
254  break;
255 
256  lRect.Pos.C += destWidth;
257 
258  lRect.Sze.W -= destWidth;
259  }
260  }
261 
262  destWidth = tableStyle.ColWidth( c );
263 
264  wrect cRect( lRect );
265  // adjust remaining linespace
266  lRect.Pos.C += destWidth;
267  lRect.Sze.W -= destWidth;
268  // adjust destinated width
269 
270  if ( lRect.Sze.W < 0 )
271  cRect.Sze.W = destWidth + lRect.Sze.W;
272  else
273  cRect.Sze.W = destWidth;
274 
275  // draw item
276  if ( Items[c] )
277  {
278  Items[c]->DrawAt( w, cRect, tableStyle, vstate, c );
279  }
280  }
281 }
282 
283 
284 std::ostream & operator<<( std::ostream & STREAM, const NCTableLine & OBJ )
285 {
286  STREAM << "Line: cols " << OBJ.Cols() << std::endl;
287 
288  for ( unsigned idx = 0; idx < OBJ.Cols(); ++idx )
289  {
290  STREAM << " " << idx << " ";
291  const NCTableCol * ci = OBJ.GetCol( idx );
292 
293  if ( ci )
294  STREAM << *ci;
295  else
296  STREAM << "NO_ITEM";
297 
298  STREAM << std::endl;
299  }
300 
301  return STREAM;
302 }
303 
304 
305 
306 
307 
308 
309 void NCTableHead::DrawAt( NCursesWindow & w, const wrect at,
310  NCTableStyle & tableStyle,
311  bool active ) const
312 {
313  vstate = S_HEADLINE;
314 
315  w.bkgdset( tableStyle.getBG( vstate ) );
316 
317  for ( int l = 0; l < at.Sze.H; ++l )
318  {
319  w.move( at.Pos.L + l, at.Pos.C );
320  w.clrtoeol();
321  }
322 
323  DrawItems( w, at, tableStyle, active );
324 }
325 
326 
327 
328 
329 
330 
331 NCTableStyle::NCTableStyle( const NCWidget & p )
332  : headline( 0 )
333  , colWidth( 0 )
334  , colAdjust( 0 )
335  , parw( p )
336  , colSepwidth( 1 )
337  , colSepchar( ACS_VLINE )
338  , hotCol(( unsigned ) - 1 )
339 {
340 }
341 
342 
343 bool NCTableStyle::SetStyleFrom( const std::vector<NCstring> & head )
344 {
345  unsigned ncols = head.size();
346 
347  headline.ClearLine();
348  headline.SetCols( ncols );
349 
350  colWidth.clear();
351  colAdjust.clear();
352  AssertMinCols( ncols );
353 
354  bool hasContent = false;
355 
356  for ( unsigned i = 0; i < head.size(); ++i )
357  {
358  const std::wstring & entry( head[i].str() );
359  bool strip = false;
360 
361  if ( entry.length() )
362  {
363  switch ( entry[0] )
364  {
365  case 'R':
366  strip = true;
367  colAdjust[i] = NC::RIGHT;
368  break;
369 
370  case 'C':
371  strip = true;
372  colAdjust[i] = NC::CENTER;
373  break;
374 
375  case 'L':
376  strip = true;
377  colAdjust[i] = NC::LEFT;
378  break;
379 
380  default:
381  yuiWarning() << "No style char [LRC] at beginning of '" << entry << "'" << std::endl;
382  break;
383  }
384  }
385 
386  NCstring coltxt = strip ? entry.substr( 1 ) : entry;
387  headline.AddCol( i, new NCTableCol( coltxt ) );
388 
389  if ( ! hasContent && coltxt.str().length() )
390  hasContent = true;
391  }
392 
393  return hasContent;
394 }
395 
396 
397 chtype NCTableStyle::highlightBG( const NCTableLine::STATE lstate,
398  const NCTableCol::STYLE cstyle,
399  const NCTableCol::STYLE dstyle ) const
400 {
401  return getBG( lstate, cstyle );
402  // unused:
403 
404  if ( lstate == NCTableLine::S_ACTIVE
405  &&
406  parw.GetState() == NC::WSactive )
407  return getBG( lstate, cstyle );
408 
409  return getBG( lstate, dstyle );
410 }
411 
412 
413 chtype NCTableStyle::getBG( const NCTableLine::STATE lstate,
414  const NCTableCol::STYLE cstyle ) const
415 {
416  switch ( lstate )
417  {
418  case NCTableLine::S_NORMAL:
419 
420  switch ( cstyle )
421  {
422  case NCTableCol::PLAIN:
423  return listStyle().item.plain;
424 
425  case NCTableCol::DATA:
426  return listStyle().item.data;
427 
428  case NCTableCol::ACTIVEDATA:
429  return listStyle().item.plain;
430 
431  case NCTableCol::HINT:
432  return listStyle().item.hint;
433 
434  case NCTableCol::SEPARATOR:
435  return listStyle().item.plain;
436 
437  case NCTableCol::NONE:
438  return currentBG;
439  }
440  break;
441 
442 
443  case NCTableLine::S_ACTIVE:
444 
445  switch ( cstyle )
446  {
447  case NCTableCol::PLAIN:
448  return listStyle().selected.plain;
449 
450  case NCTableCol::DATA:
451  return listStyle().selected.data;
452 
453  case NCTableCol::ACTIVEDATA:
454  return listStyle().selected.data;
455 
456  case NCTableCol::HINT:
457  return listStyle().selected.hint;
458 
459  case NCTableCol::SEPARATOR:
460  return listStyle().selected.plain;
461 
462  case NCTableCol::NONE:
463  return currentBG;
464  }
465  break;
466 
467  case NCTableLine::S_DISABELED:
468 
469  switch ( cstyle )
470  {
471  case NCTableCol::PLAIN:
472  return parw.wStyle().disabledList.item.plain;
473 
474  case NCTableCol::DATA:
475  return parw.wStyle().disabledList.item.data;
476 
477  case NCTableCol::ACTIVEDATA:
478  return parw.wStyle().disabledList.item.plain;
479 
480  case NCTableCol::HINT:
481  return parw.wStyle().disabledList.item.hint;
482 
483  case NCTableCol::SEPARATOR:
484  return listStyle().item.plain;
485 
486  case NCTableCol::NONE:
487  return currentBG;
488  }
489  break;
490 
491 
492  case NCTableLine::S_HEADLINE:
493  return listStyle().title;
494  break;
495 
496  case NCTableLine::S_HIDDEN:
497  return currentBG;
498  break;
499  }
500 
501  return currentBG;
502 }
503 
504 
505 std::ostream & operator<<( std::ostream & STREAM, const NCTableStyle & OBJ )
506 {
507  STREAM << form( "cols %d, sep %d (%lx)\n",
508  OBJ.Cols(), OBJ.ColSepwidth(), (unsigned long)OBJ.ColSepchar() );
509 
510  for ( unsigned i = 0; i < OBJ.Cols(); ++i )
511  {
512  STREAM << form( "%2d %d(%3d) ", i, OBJ.ColAdjust( i ), OBJ.ColWidth( i ) );
513 
514  if ( OBJ.Headline().GetCol( i ) )
515  STREAM << OBJ.Headline().GetCol( i )->Label();
516 
517  STREAM << std::endl;
518  }
519 
520  return STREAM;
521 }
522 
C++ class for windows.
Definition: ncursesw.h:904
int vline(int len, chtype ch=0)
Definition: ncursesw.h:1499
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
int clrtoeol()
Definition: ncursesw.h:1538
chtype getbkgd() const
Definition: ncursesw.h:1438
int move(int y, int x)
Definition: ncursesw.h:1155
Definition: position.h:154