libyui-ncurses
 
Loading...
Searching...
No Matches
NCstyle.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: NCstyle.h
20
21 Author: Michael Andres <ma@suse.de>
22
23/-*/
24
25#ifndef NCstyle_h
26#define NCstyle_h
27
28#include <ncursesw/ncurses.h>
29
30#include <iosfwd>
31#include <string>
32#include <vector>
33
34#include "NCtypes.h"
35
36
38{
39 //
40 // available colors and color pairs
41 //
42 static int _colors;
43 static int _pairs;
44
45 //if we have color support, return number of available colors
46 //(at most 8 though)
47 //will be initialized by init_color() function
48 inline static int colors() { return _colors ? _colors : ::COLORS; }
49
50 // do the same with color pairs
51 inline static int color_pairs() { return _pairs ? _pairs : ::COLOR_PAIRS; }
52
53 //
54 //
55 // color pair to chtype
56 //
57 inline static chtype color_pair( short fg, short bg ) { return colors() ? COLOR_PAIR( bg * colors() + COLOR_WHITE - fg ) : A_NORMAL; }
58
59 inline static chtype color_pair( int i ) { return colors() ? COLOR_PAIR( i ) : A_NORMAL; }
60
61 inline static short fg_color_pair( int i ) { return colors() ? ( COLOR_WHITE - ( i % colors() ) ) : -1; }
62
63 inline static short bg_color_pair( int i ) { return colors() ? ( i / colors() ) : -1; }
64
65 //
66 // chtype to color pair
67 //
68 inline static int color_pair_of( chtype ch ) { return PAIR_NUMBER( ch ); }
69
70 inline static short fg_color_of( chtype ch ) { return fg_color_pair( color_pair_of( ch ) ); }
71
72 inline static short bg_color_of( chtype ch ) { return bg_color_pair( color_pair_of( ch ) ); }
73
74 //
75 // chtype manipualtion
76 //
77 static const chtype style_mask = A_ATTRIBUTES & ~A_COLOR & ~A_ALTCHARSET;
78 static const chtype color_mask = A_COLOR;
79 static const chtype char_mask = A_CHARTEXT | A_ALTCHARSET;
80 //
81 inline static chtype getStyle( chtype a ) { return a & style_mask; }
82
83 inline static chtype getColor( chtype a ) { return a & color_mask; }
84
85 inline static chtype getChar( chtype a ) { return a & char_mask; }
86
87 inline static chtype getNonChar( chtype a ) { return a & ~char_mask; }
88
89 inline static void setStyle( chtype & a, chtype ch ) { a = ( a & ~style_mask ) | ( ch & style_mask ); }
90
91 inline static void setColor( chtype & a, chtype ch ) { if ( colors() ) a = ( a & ~color_mask ) | ( ch & color_mask ); }
92
93 inline static void setChar( chtype & a, chtype ch ) { a = ( a & ~char_mask ) | ( ch & char_mask ); }
94
95 inline static void addStyle( chtype & a, chtype ch ) { a = a | ( ch & style_mask ); }
96
97 inline static void delStyle( chtype & a, chtype ch ) { a = a & ~( ch & style_mask ); }
98
99 inline static void toggleStyle( chtype & a, chtype ch ) { setStyle( a, ( a & ~ch ) | (( a ^ ch ) & ch ) ); }
100
101 inline static void addAlt( chtype & a ) { a |= A_ALTCHARSET; }
102
103 inline static void delAlt( chtype & a ) { a &= ~A_ALTCHARSET; }
104
105 //
106 inline static short getFg( chtype a ) { return fg_color_of( a ); }
107
108 inline static short getBg( chtype a ) { return bg_color_of( a ); }
109
110 inline static void setFg( chtype & a, short c ) { if ( colors() ) setColor( a, color_pair(( colors() + c ) % colors(), getBg( a ) ) ); }
111
112 inline static void setBg( chtype & a, short c ) { if ( colors() ) setColor( a, color_pair( getFg( a ), ( colors() + c ) % colors() ) ); }
113
114 // debug aid
115
117 inline static std::string colorAsString( short i )
118 {
119 switch ( i )
120 {
121 #define OUTS(X) case COLOR_##X: return #X
122 OUTS( BLACK );
123 OUTS( RED );
124 OUTS( GREEN );
125 OUTS( YELLOW );
126 OUTS( BLUE );
127 OUTS( MAGENTA );
128 OUTS( CYAN );
129 OUTS( WHITE );
130 #undef OUTS
131 }
132 return "COLOR?";
133 }
134
136 inline static std::string color_pairAsString( int i )
137 {
138 return std::string( "[" ) + colorAsString( fg_color_pair( i ) )
139 + "," + colorAsString( bg_color_pair( i ) ) + "]";
140 }
141
142private:
143
144 friend class NCurses;
145
146 static void init_colors()
147 {
148
149 //get number of available colors (property of the terminal)
150 //the same with color pairs
151
152 _colors = ::COLORS;
153 _pairs = ::COLOR_PAIRS;
154
155 //if we have more than 8 colors available, use only 8 anyway
156 //in order to preserve the same color palette even for
157 //e.g. 256color terminal
158
159 if ( _colors > COLOR_WHITE + 1 )
160 //_colors = 8 at all times
161 _colors = COLOR_WHITE + 1;
162
163 if ( _pairs > _colors * _colors )
164 //_pairs == 64 at all times
165 _pairs = _colors * _colors;
166
167 for ( short i = 1; i < color_pairs(); ++i )
168 ::init_pair( i, fg_color_pair( i ), bg_color_pair( i ) );
169 }
170};
171
172
173
174class NCattrset
175{
176
177private:
178
179 std::vector<chtype> attr;
180
181public:
182
183 NCattrset( unsigned num ) : attr( num, A_NORMAL ) {}
184
185 virtual ~NCattrset() {}
186
187public:
188
189 const chtype & operator[]( unsigned a ) const { return attr[a]; }
190
191 chtype getAttr( unsigned a ) const { return attr[a]; }
192
193 chtype getStyle( unsigned a ) const { return NCattribute::getStyle( attr[a] ); }
194
195 chtype getColor( unsigned a ) const { return NCattribute::getColor( attr[a] ); }
196
197 chtype getChar( unsigned a ) const { return NCattribute::getChar( attr[a] ); }
198
199 chtype getNonChar( unsigned a ) const { return NCattribute::getNonChar( attr[a] ); }
200
201 void setAttr( unsigned a, chtype ch ) { attr[a] = ch; }
202
203 void setStyle( unsigned a, chtype ch ) { NCattribute::setStyle( attr[a], ch ); }
204
205 void setColor( unsigned a, chtype ch ) { NCattribute::setColor( attr[a], ch ); }
206
207 void setChar( unsigned a, chtype ch ) { NCattribute::setChar( attr[a], ch ); }
208
209 void addStyle( unsigned a, chtype ch ) { NCattribute::addStyle( attr[a], ch ); }
210
211 void delStyle( unsigned a, chtype ch ) { NCattribute::delStyle( attr[a], ch ); }
212
213 void toggleStyle( unsigned a, chtype ch ) { NCattribute::toggleStyle( attr[a], ch ); }
214
215 void addAlt( unsigned a ) { NCattribute::addAlt( attr[a] ); }
216
217 void delAlt( unsigned a ) { NCattribute::delAlt( attr[a] ); }
218
219public:
220
221 short getFg( unsigned a ) const { return NCattribute::getFg( attr[a] ); }
222
223 short getBg( unsigned a ) const { return NCattribute::getBg( attr[a] ); }
224
225 void setFg( unsigned a, short c ) { NCattribute::setFg( attr[a], c ); }
226
227 void setBg( unsigned a, short c ) { NCattribute::setBg( attr[a], c ); }
228};
229
230
231
232class NCstyle
233{
234
235 friend class NCStyleDef;
236
237public:
238
239 enum STglobal
240 {
241 AppTitle,
242 AppText,
243 // last entry
244 MaxSTglobal
245 };
246
247 enum STlocal
248 {
249 DialogBorder,
250 DialogTitle,
251 DialogActiveBorder,
252 DialogActiveTitle,
253 //
254 DialogText,
255 DialogHeadline,
256 //
257 DialogDisabled,
258 //
259 DialogPlain,
260 DialogLabel,
261 DialogData,
262 DialogHint,
263 DialogScrl,
264 DialogActivePlain,
265 DialogActiveLabel,
266 DialogActiveData,
267 DialogActiveHint,
268 DialogActiveScrl,
269 //
270 DialogFramePlain,
271 DialogFrameLabel,
272 DialogFrameData,
273 DialogFrameHint,
274 DialogFrameScrl,
275 DialogActiveFramePlain,
276 DialogActiveFrameLabel,
277 DialogActiveFrameData,
278 DialogActiveFrameHint,
279 DialogActiveFrameScrl,
280 //
281 ListTitle,
282 ListPlain,
283 ListLabel,
284 ListData,
285 ListHint,
286 ListSelPlain,
287 ListSelLabel,
288 ListSelData,
289 ListSelHint,
290 //
291 ListActiveTitle,
292 ListActivePlain,
293 ListActiveLabel,
294 ListActiveData,
295 ListActiveHint,
296 ListActiveSelPlain,
297 ListActiveSelLabel,
298 ListActiveSelData,
299 ListActiveSelHint,
300 //
301 RichTextPlain,
302 RichTextTitle,
303 RichTextLink,
304 RichTextArmedlink,
305 RichTextActiveArmedlink,
306 RichTextVisitedLink,
307 RichTextB,
308 RichTextI,
309 RichTextT,
310 RichTextBI,
311 RichTextBT,
312 RichTextIT,
313 RichTextBIT,
314 //
315 ProgbarCh,
316 ProgbarBgch,
317 //
318 TextCursor,
319 // last entry
320 MaxSTlocal
321 };
322
323public:
324
325 struct StBase
326 {
327 const chtype & title;
328 const chtype & text;
329 StBase( const chtype & ti, const chtype & te )
330 : title( ti ), text( te )
331 {}
332 };
333
334 struct STChar
335 {
336 const chtype & chattr;
337 chtype getChar() const { return NCattribute::getChar( chattr ); }
338
339 chtype getNonChar() const { return NCattribute::getNonChar( chattr ); }
340
341 STChar( const chtype & cha )
342 : chattr( cha )
343 {}
344 };
345
346 struct StItem
347 {
348 const chtype & plain;
349 const chtype & label;
350 const chtype & data;
351 const chtype & hint;
352 StItem( const chtype & p, const chtype & l, const chtype & d, const chtype & h )
353 : plain( p ), label( l ), data( d ), hint( h )
354 {}
355 };
356
357 struct StWidget : public StItem
358 {
359 const chtype & scrl;
360 StWidget( const chtype & p, const chtype & l, const chtype & d, const chtype & h,
361 const chtype & s )
362 : StItem( p, l, d, h ), scrl( s )
363 {}
364 };
365
366 struct StList
367 {
368 const chtype & title;
369 const StItem item;
370 const StItem selected;
371 StList( const chtype & t, const StItem & i, const StItem & s )
372 : title( t ), item( i ), selected( s )
373 {}
374
375 const StItem & getItem( bool sel ) const { return sel ? selected : item; }
376 };
377
378 struct StProgbar
379 {
380 const STChar bar;
381 const STChar nonbar;
382 StProgbar( const chtype & b, const chtype & nb )
383 : bar( b ), nonbar( nb )
384 {}
385 };
386
387 struct StRichtext
388 {
389 const chtype & plain;
390 const chtype & title;
391 const chtype & link;
392 const chtype & armedlink;
393 const chtype & activearmedlink;
394 const chtype & visitedlink;
395 const chtype & B;
396 const chtype & I;
397 const chtype & T;
398 const chtype & BI;
399 const chtype & BT;
400 const chtype & IT;
401 const chtype & BIT;
402
403 StRichtext( const chtype & p, const chtype & tit,
404 const chtype & l, const chtype & al, const chtype & aal, const chtype & vl,
405 const chtype & b, const chtype & i, const chtype & t,
406 const chtype & bi, const chtype & bt, const chtype & it,
407 const chtype & bit )
408 : plain( p ), title( tit ),
409 link( l ), armedlink( al ), activearmedlink( aal ), visitedlink( vl ),
410 B( b ), I( i ), T( t ),
411 BI( bi ), BT( bt ), IT( it ),
412 BIT( bit )
413 {}
414
415 const chtype & getArmed( NC::WState s ) const
416 {
417 return ( s == NC::WSactive ) ? activearmedlink : armedlink;
418 }
419 };
420
421 struct StDialog
422 {
423 StBase border;
424 StBase activeBorder;
425 StBase dumb;
426 StWidget disabled;
427 StWidget normal;
428 StWidget active;
429 StWidget frame;
430 StWidget activeFrame;
431 StList list;
432 StList activeList;
433 StList disabledList;
434 StProgbar progbar;
435 StRichtext richtext;
436 const chtype & cursor;
437 //
438 StDialog( const StBase & b, const StBase & ab, const StBase & d, const StWidget & dis,
439 const StWidget & n, const StWidget & a,
440 const StWidget & f, const StWidget & af,
441 const StList & l, const StList & al, const StList & dl,
442 const StProgbar & pbar,
443 const StRichtext & rtext,
444 const chtype & curs )
445 : border( b ), activeBorder( ab ), dumb( d ), disabled( dis ),
446 normal( n ), active( a ),
447 frame( f ), activeFrame( af ),
448 list( l ), activeList( al ), disabledList( dl ),
449 progbar( pbar ),
450 richtext( rtext ),
451 cursor( curs )
452 {}
453
454public:
455
456 const StBase & getDlgBorder( bool active ) const { return active ? activeBorder : border; }
457
458 const StBase & getDumb() const { return dumb; }
459
460 const StWidget & getWidget( NC::WState s, bool nonactive = false ) const
461 {
462 switch ( s )
463 {
464
465 case NC::WSdisabled:
466 return disabled;
467
468 case NC::WSactive:
469 return nonactive ? normal : active;
470
471 case NC::WSnormal:
472
473 case NC::WSdumb:
474 break;
475 }
476
477 return normal;
478 }
479
480 const StWidget & getFrame( NC::WState s ) const
481 {
482 switch ( s )
483 {
484
485 case NC::WSdisabled:
486 return disabled;
487
488 case NC::WSactive:
489 return activeFrame;
490
491 case NC::WSnormal:
492
493 case NC::WSdumb:
494 break;
495 }
496
497 return frame;
498 }
499
500 const StList & getList( NC::WState s ) const
501 {
502 switch ( s )
503 {
504
505 case NC::WSdisabled:
506 return disabledList;
507
508 case NC::WSactive:
509 return activeList;
510
511 case NC::WSnormal:
512
513 case NC::WSdumb:
514 break;
515 }
516
517 return list;
518 }
519 };
520
521public:
522
523
524 class Style : private NCattrset, public StDialog
525 {
526
527 friend class NCstyle;
528
529 Style & operator=( const Style & ); // no assignment
530
531 private:
532
533 static unsigned sanitycheck();
534 static NCattrset attrGlobal;
535
536 public:
537
538 NCattrset & getAttrGlobal() { return attrGlobal; }
539
540 NCattrset & getAttrLocal() { return *this; }
541
542 private:
543
544 StDialog initDialog(); // use this to initialize StDialog
545
546 public:
547
548 Style();
549 Style( const Style & rhs );
550 virtual ~Style();
551
552 public:
553
554 const chtype & attr( STglobal a ) const { return attrGlobal[a]; }
555
556 const chtype & attr( STlocal a ) const { return NCattrset::operator[]( a ); }
557
558 const chtype & operator()( STglobal a ) const { return attr( a ); }
559
560 const chtype & operator()( STlocal a ) const { return attr( a ); }
561 };
562
563public:
564
565 enum StyleSet
566 {
567 DefaultStyle,
568 InfoStyle,
569 WarnStyle,
570 PopupStyle,
571 // last entry
572 MaxStyleSet
573 };
574
575private:
576
577 std::string styleName;
578 std::string term;
579 std::vector<Style> styleSet;
580
581 StyleSet fakestyle_e;
582 void fakestyle( StyleSet f );
583 Style & getStyle( StyleSet a ) { return styleSet[a]; }
584
585public:
586
587 NCstyle( std::string term_t );
588 ~NCstyle();
589
590 const chtype & operator()( STglobal a ) const { return Style::attrGlobal[a]; }
591
592 const Style & operator[]( StyleSet a ) const
593 {
594 if ( fakestyle_e != MaxStyleSet )
595 return styleSet[fakestyle_e];
596
597 return styleSet[a];
598 }
599
600 void changeSyle();
601 void nextStyle();
602
603 static std::string dumpName( StyleSet a );
604 static std::string dumpName( STglobal a );
605 static std::string dumpName( STlocal a );
606};
607
608
609#endif // NCstyle_h
Definition NCstyle.h:525
Definition NCurses.h:144
Definition NCstyle.h:38
static std::string color_pairAsString(int i)
Definition NCstyle.h:136
static std::string colorAsString(short i)
Definition NCstyle.h:117
Definition NCstyle.h:335
Definition NCstyle.h:326
Definition NCstyle.h:347
Definition NCstyle.h:367
Definition NCstyle.h:379
Definition NCstyle.h:388
Definition NCstyle.h:358