libyui-ncurses
Loading...
Searching...
No Matches
NCTableItem.h
1/*
2 Copyright (C) 2000-2012 Novell, Inc
3 Copyright (C) 2020 SUSE LLC
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) version 3.0 of the License. This library
8 is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 License for more details. You should have received a copy of the GNU
12 Lesser General Public License along with this library; if not, write
13 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14 Floor, Boston, MA 02110-1301 USA
15*/
16
17
18/*-/
19
20 File: NCTableItem.h
21
22 Authors: Michael Andres <ma@suse.de>
23 Stefan Hundhammer <shundhammer@suse.de>
24
25/-*/
26
27#ifndef NCTableItem_h
28#define NCTableItem_h
29
30#include <iosfwd>
31#include <vector>
32
33#include "position.h"
34#include "NCWidget.h"
35#include <yui/YTableItem.h>
36
37class NCTableCol;
38class NCTableStyle;
39class NCTableTag;
40
41
68{
69public:
70
71 enum STATE
72 {
73 S_NORMAL = 0x00,
74 S_ACTIVE = 0x01,
75 S_DISABLED = 0x10,
76 S_HIDDEN = 0x20,
77 S_HEADLINE = 0x40
78 };
79
80
92 NCTableLine( std::vector<NCTableCol*> & cells,
93 int index = -1,
94 bool nested = false,
95 unsigned state = S_NORMAL );
96
97 NCTableLine( NCTableLine * parentLine,
98 YItem * yitem,
99 std::vector<NCTableCol*> & cells,
100 int index = -1,
101 bool nested = false,
102 unsigned state = S_NORMAL );
103
107 NCTableLine( unsigned colCount,
108 int index = -1,
109 bool nested = false,
110 unsigned state = S_NORMAL );
111
112 NCTableLine( NCTableLine * parentLine,
113 YItem * yitem,
114 unsigned colCount,
115 int index = -1,
116 bool nested = false,
117 unsigned state = S_NORMAL );
118
122 virtual ~NCTableLine();
123
127 YTableItem * origItem() const { return dynamic_cast<YTableItem *>( _yitem ); }
128
132 void setOrigItem( YTableItem *yitem );
133
137 int index() const { return _index; }
138
142 unsigned Cols() const { return _cells.size(); }
143
147 void SetCols( unsigned idx );
148
152 void SetCols( std::vector<NCTableCol*> & newCells );
153
157 void ClearLine() { SetCols( 0 ); }
158
163 std::vector<NCTableCol*> GetItems() const { return _cells; }
164
168 void Append( NCTableCol * cell ) { AddCol( Cols(), cell ); }
169
170 void AddCol( unsigned idx, NCTableCol * item );
171 void DelCol( unsigned idx );
172
180 NCTableCol * GetCol( unsigned idx );
181
188 const NCTableCol * GetCol( unsigned idx ) const
189 {
190 return const_cast<NCTableLine*>( this )->GetCol( idx );
191 }
192
193 void SetState ( const STATE s ) { _state |= s; }
194 void ClearState( const STATE s ) { _state &= ~s; }
195
196 bool isHidden() const { return ( _state & S_HIDDEN ); }
197 bool isDisabled() const { return ( _state & S_DISABLED ); }
198 bool isSpecial() const { return ( _state & ( S_HIDDEN | S_DISABLED ) ); }
199 bool isActive() const { return ( _state & S_ACTIVE ); }
200
201 virtual bool isVisible() const;
202
203 virtual bool isEnabled() const { return isVisible() && !isDisabled(); }
204
210 virtual bool isNested() const { return _nested; }
211
215 virtual void setNested( bool val ) { _nested = val; }
216
220 void openBranch();
221
225 void closeBranch();
226
231
236 virtual bool handleInput( wint_t key );
237
248 virtual bool ChangeToVisible() { return false; }
249
250 virtual unsigned Hotspot( unsigned & at ) const { at = 0; return 0; }
251
255 virtual void UpdateFormat( NCTableStyle & tableStyle );
256
261 virtual void updatePrefix();
262
263
265 virtual void DrawAt( NCursesWindow & w,
266 const wrect at,
267 NCTableStyle & tableStyle,
268 bool active ) const;
269
270 void stripHotkeys();
271
272 //
273 // Tree operations
274 //
275
276 virtual NCTableLine * parent() const { return _parent; }
277 virtual NCTableLine * firstChild() const { return _firstChild; }
278 virtual NCTableLine * nextSibling() const { return _nextSibling; }
279
280 void setParent ( NCTableLine * newVal ) { _parent = newVal; }
281 void setFirstChild ( NCTableLine * newVal ) { _firstChild = newVal; }
282 void setNextSibling( NCTableLine * newVal ) { _nextSibling = newVal; }
283
287 int treeLevel() const { return _treeLevel; }
288
292 void setTreeLevel( int newVal ) { _treeLevel = newVal; }
293
297 int prefixLen() const { return _nested ? treeLevel() + 3 : 0; }
298
302 NCTableTag * tagCell() const;
303
308 std::string indentationStr() const;
309
310protected:
311
316 void treeInit( NCTableLine * parentLine, YItem * yitem );
317
322
326 void addToTree( NCTableLine * parent );
327
332 bool isOpen( YItem * yitem ) const;
333
337 YItem * yitem() const { return _yitem; }
338
342 void setYItem( YItem * yitem );
343
344 virtual void DrawItems( NCursesWindow & w,
345 const wrect at,
346 NCTableStyle & tableStyle,
347 bool active ) const;
348
349 void assertCol( unsigned idx );
350
357 const std::string & prefixPlaceholder() const { return _prefixPlaceholder; }
358
363 void drawPrefix( NCursesWindow & w,
364 const wrect at,
365 NCTableStyle & tableStyle ) const;
366
367private:
368
369 friend std::ostream & operator<<( std::ostream & str, const NCTableLine & obj );
370
371 // Disable unwanted assignment operator and copy constructor
372
373 NCTableLine & operator=( const NCTableLine & );
374 NCTableLine( const NCTableLine & );
375
376
377 //
378 // Data members
379 //
380
381protected:
382
383 std::vector<NCTableCol*> _cells;
384
385 unsigned _state;
386 int _index;
387 YItem * _yitem;
388 bool _nested;
389
390 // Tree-related
391
392 int _treeLevel;
393 NCTableLine * _parent;
394 NCTableLine * _nextSibling;
395 NCTableLine * _firstChild;
396
397 // This should have been an argument for DrawItems.
398 //
399 // It needs to be mutable because some methods that change it promise to be
400 // const, but they break that promise with this variable.
401 mutable STATE _vstate;
402
403 // Tree hierarchy line graphics for this line.
404 //
405 // chtype is a very basic NCurses type to store one character with its
406 // attributes (bg/fg color).
407 chtype * _prefix;
408 std::string _prefixPlaceholder;
409};
410
411
422{
423
424 friend std::ostream & operator<<( std::ostream & str, const NCTableCol & obj );
425
426public:
427
428 enum STYLE
429 {
430 NONE = 0, // use current bg
431 PLAIN, // plain text
432 DATA, // data style
433 ACTIVEDATA, // data style if line active, else plain
434 HINT, // hint
435 SEPARATOR // separator
436 };
437
438
439 NCTableCol( const NCstring & label = "", STYLE st = ACTIVEDATA );
440
441 virtual ~NCTableCol();
442
443 const NClabel & Label() const { return _label; }
444 virtual void SetLabel( const NClabel & newVal ) { _label = newVal; }
445 virtual void SetLabel( const std::string & newVal ) { _label = NCstring( newVal ); }
446
451 const NClabel & prefix() const { return _prefix; }
452
453 virtual void setPrefix( const NClabel & newVal ) { _prefix = newVal; }
454 virtual void setPrefix( const std::string & newVal ) { _prefix = NCstring( newVal); }
455 int prefixWidth() const { return _prefix.width(); }
456
461 wrect prefixAdjusted( const wrect origRect ) const;
462
463 virtual wsze Size() const { return wsze( 1, _prefix.width() + _label.width() ); }
464
465 virtual void DrawAt( NCursesWindow & w,
466 const wrect at,
467 NCTableStyle & tableStyle,
468 NCTableLine::STATE linestate,
469 unsigned colidx ) const;
470
471 void stripHotkey() { _label.stripHotkey(); }
472
473 bool hasHotkey() const { return _label.hasHotkey(); }
474 unsigned char hotkey() const { return _label.hotkey(); }
475
476protected:
477
478 chtype setBkgd( NCursesWindow & w,
479 NCTableStyle & tableStyle,
480 NCTableLine::STATE linestate,
481 STYLE colstyle ) const ;
482
483private:
484
485 NClabel _prefix;
486 NClabel _label;
487 STYLE _style;
488};
489
490
495{
496
497public:
498
499 NCTableHead( unsigned cols )
500 : NCTableLine( cols )
501 {}
502
503 NCTableHead( std::vector<NCTableCol*> & headCells )
504 : NCTableLine( headCells )
505 {}
506
507 virtual ~NCTableHead()
508 {}
509
514 virtual void DrawAt( NCursesWindow & w,
515 const wrect at,
516 NCTableStyle & tableStyle,
517 bool active ) const;
518};
519
520
523{
524
525 friend std::ostream & operator<<( std::ostream & str, const NCTableStyle & obj );
526
527public:
528
529 static const chtype currentBG = (chtype) - 1;
530
531 NCTableStyle( const NCWidget & parentWidget );
532 ~NCTableStyle() {}
533
539 bool SetStyleFrom( const std::vector<NCstring> & head );
540
541 void SetSepChar( const chtype sepChar ) { _colSepChar = sepChar; }
542
544 void SetSepWidth( const unsigned sepWidth ) { _colSepWidth = sepWidth; }
545
546 void SetHotCol( int hcol )
547 {
548 _hotCol = ( hcol < 0 || Cols() <= (unsigned) hcol ) ? -1 : hcol;
549 }
550
553 {
554 _colWidth.clear();
555 AssertMinCols( _headline.Cols() );
556 _headline.UpdateFormat( *this );
557 }
558
560 void AssertMinCols( unsigned num )
561 {
562 if ( _colWidth.size() < num )
563 {
564 _colWidth.resize( num, 0 );
565 _colAdjust.resize( _colWidth.size(), NC::LEFT );
566 }
567 }
568
572 void MinColWidth( unsigned num, unsigned val )
573 {
574 AssertMinCols( num );
575
576 if ( val > _colWidth[num] )
577 _colWidth[ num ] = val;
578 }
579
580 NC::ADJUST ColAdjust( unsigned num ) const { return _colAdjust[num]; }
581
582 unsigned Cols() const { return _colWidth.size(); }
583
584 unsigned ColWidth( unsigned num ) const { return _colWidth[num]; }
585
586 unsigned ColSepWidth() const { return _colSepWidth; }
587
588 chtype ColSepChar() const { return _colSepChar; }
589
590 unsigned HotCol() const { return _hotCol; }
591
592 const NCstyle::StList & listStyle() const { return _parentWidget.listStyle(); }
593
594 chtype getBG() const { return listStyle().item.plain; }
595
596 chtype getBG( const NCTableLine::STATE lstate,
597 const NCTableCol::STYLE cstyle = NCTableCol::PLAIN ) const;
598
599 chtype highlightBG( const NCTableLine::STATE lstate,
600 const NCTableCol::STYLE cstyle,
601 const NCTableCol::STYLE dstyle = NCTableCol::PLAIN ) const ;
602
603 chtype hotBG( const NCTableLine::STATE lstate, unsigned colidx ) const
604 {
605 return ( colidx == _hotCol ) ?
606 getBG( lstate, NCTableCol::HINT ) : currentBG;
607 }
608
609 const NCTableLine & Headline() const { return _headline; }
610
612 unsigned TableWidth() const
613 {
614 unsigned twidth = 0;
615
616 for ( unsigned i = 0; i < Cols(); ++i )
617 twidth += _colWidth[i];
618
619 if ( Cols() > 1 )
620 twidth += _colSepWidth * ( Cols() - 1 );
621
622 return twidth;
623 }
624
625
626private:
627
628 const NCWidget & _parentWidget;
629 NCTableHead _headline;
630 std::vector<unsigned> _colWidth;
631 std::vector<NC::ADJUST> _colAdjust;
632
633
635 unsigned _colSepWidth;
636
637 chtype _colSepChar;
638 unsigned _hotCol;
639};
640
641
646class NCTableTag : public NCTableCol
647{
648public:
649
658 NCTableTag( YItem *item, bool sel = false, bool singleSel = false )
659 : NCTableCol( NCstring( singleSel ? "( )" : "[ ]" ), SEPARATOR )
660 , _yitem( item )
661 , _selected( sel )
662 , _singleSelection( singleSel )
663 {
664 // store pointer to this tag in Yitem data
665 _yitem->setData( this );
666 }
667
668 virtual ~NCTableTag() {}
669
670 virtual void SetLabel( const NClabel & ) { /*NOOP*/; }
671
672 virtual void DrawAt( NCursesWindow & w,
673 const wrect at,
674 NCTableStyle & tableStyle,
675 NCTableLine::STATE linestate,
676 unsigned colidx ) const
677 {
678 // Use parent DrawAt to draw the static part: "[ ]"
679 NCTableCol::DrawAt( w, at, tableStyle, linestate, colidx );
680
681 if ( _selected )
682 {
683 // Draw the "x" inside the "[ ]" with different attributes
684
685 setBkgd( w, tableStyle, linestate, DATA );
686 wrect drawRect = prefixAdjusted( at );
687 w.addch( drawRect.Pos.L, drawRect.Pos.C + 1, 'x' );
688 }
689 }
690
691 virtual bool Selected() const { return _selected; }
692
693 virtual void SetSelected( bool sel ) { _selected = sel; }
694
695 virtual bool SingleSelection() const { return _singleSelection; }
696
697 YItem *origItem() const { return _yitem; }
698
699private:
700
701 YItem * _yitem;
702 bool _selected;
703 bool _singleSelection;
704};
705
706
707#endif // NCTableItem_h
Definition NCTableItem.h:422
const NClabel & prefix() const
Definition NCTableItem.h:451
wrect prefixAdjusted(const wrect origRect) const
Definition NCTableItem.cc:640
Definition NCTableItem.h:495
virtual void DrawAt(NCursesWindow &w, const wrect at, NCTableStyle &tableStyle, bool active) const
Definition NCTableItem.cc:686
Definition NCTableItem.h:68
void drawPrefix(NCursesWindow &w, const wrect at, NCTableStyle &tableStyle) const
Definition NCTableItem.cc:441
void treeInit(NCTableLine *parentLine, YItem *yitem)
Definition NCTableItem.cc:138
NCTableTag * tagCell() const
Definition NCTableItem.cc:574
void setYItem(YItem *yitem)
Definition NCTableItem.cc:225
int _index
unique index to identify this line
Definition NCTableItem.h:386
void Append(NCTableCol *cell)
Definition NCTableItem.h:168
void SetCols(unsigned idx)
Definition NCTableItem.cc:241
virtual bool ChangeToVisible()
Definition NCTableItem.h:248
bool isOpen(YItem *yitem) const
Definition NCTableItem.cc:202
int index() const
Definition NCTableItem.h:137
std::vector< NCTableCol * > GetItems() const
Definition NCTableItem.h:163
YItem * yitem() const
Definition NCTableItem.h:337
virtual void setNested(bool val)
Definition NCTableItem.h:215
void closeBranch()
Definition NCTableItem.cc:535
YItem * _yitem
not owned
Definition NCTableItem.h:387
virtual bool handleInput(wint_t key)
Definition NCTableItem.cc:472
void addToTree(NCTableLine *parent)
Definition NCTableItem.cc:179
void setOrigItem(YTableItem *yitem)
Definition NCTableItem.cc:219
void openBranch()
Definition NCTableItem.cc:515
void setTreeLevel(int newVal)
Definition NCTableItem.h:292
void toggleOpenClosedState()
Definition NCTableItem.cc:555
YTableItem * origItem() const
Definition NCTableItem.h:127
int treeLevel() const
Definition NCTableItem.h:287
const NCTableCol * GetCol(unsigned idx) const
Definition NCTableItem.h:188
virtual ~NCTableLine()
Definition NCTableItem.cc:131
virtual void updatePrefix()
Definition NCTableItem.cc:407
unsigned Cols() const
Definition NCTableItem.h:142
NCTableCol * GetCol(unsigned idx)
Definition NCTableItem.cc:293
std::vector< NCTableCol * > _cells
owned
Definition NCTableItem.h:383
virtual void DrawAt(NCursesWindow &w, const wrect at, NCTableStyle &tableStyle, bool active) const
Definition NCTableItem.cc:319
virtual bool isNested() const
Definition NCTableItem.h:210
bool _nested
using nested (tree-like) items?
Definition NCTableItem.h:388
virtual void UpdateFormat(NCTableStyle &tableStyle)
Definition NCTableItem.cc:302
void initPrefixPlaceholder()
Definition NCTableItem.cc:161
unsigned _state
Or'ed STATE flags.
Definition NCTableItem.h:385
int prefixLen() const
Definition NCTableItem.h:297
NCTableLine(std::vector< NCTableCol * > &cells, int index=-1, bool nested=false, unsigned state=S_NORMAL)
Definition NCTableItem.cc:40
void ClearLine()
Definition NCTableItem.h:157
std::string indentationStr() const
Definition NCTableItem.cc:173
const std::string & prefixPlaceholder() const
Definition NCTableItem.h:357
Styling for a NCTable: column widths, alignment and colors.
Definition NCTableItem.h:523
void SetSepWidth(const unsigned sepWidth)
total width of space between adjacent columns, including the separator character
Definition NCTableItem.h:544
void ResetToMinCols()
Forget sizing based on table content, resize according to headline only.
Definition NCTableItem.h:552
void AssertMinCols(unsigned num)
Ensure we know width and alignment for at least num columns.
Definition NCTableItem.h:560
bool SetStyleFrom(const std::vector< NCstring > &head)
Definition NCTableItem.cc:721
unsigned TableWidth() const
Add up the widths of columns with the separators.
Definition NCTableItem.h:612
void MinColWidth(unsigned num, unsigned val)
Definition NCTableItem.h:572
Definition NCTableItem.h:647
NCTableTag(YItem *item, bool sel=false, bool singleSel=false)
Definition NCTableItem.h:658
Definition NCWidget.h:46
Multi-line string, with optional hotkey, drawable.
Definition NCtext.h:82
Definition NCstring.h:36
C++ class for windows.
Definition ncursesw.h:907
int addch(const char ch)
Definition ncursesw.h:1230
A rectangle is defined by its position and size: wpos Pos, wsze Sze.
Definition position.h:194
Screen dimension (screen size) in the order height, width: (H, W)
Definition position.h:154
ADJUST
Alignment aka justification: top/bottom, left/right, center.
Definition NCtypes.h:35
Definition NCstyle.h:367