cwidget 0.5.18
style.h
1// style.h -*-c++-*-
2//
3// Copyright (C) 2005 Daniel Burrows
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License as
7// published by the Free Software Foundation; either version 2 of
8// the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; see the file COPYING. If not, write to
17// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18// Boston, MA 02111-1307, USA.
19
20#ifndef STYLE_H
21#define STYLE_H
22
23#include <ncursesw/curses.h>
24
25#include <cwidget/curses++.h>
26
27#include <string>
28
30
31#include <cwidget/generic/util/eassert.h>
32
33namespace cwidget
34{
51 class style
52 {
54 short fg;
58 short bg;
59
61 attr_t set_attrs;
63 attr_t clear_attrs;
67 attr_t flip_attrs;
68
69 // Note: it is assumed that set_attrs and clear_attrs are pairwise
70 // disjoint.
71
72 public:
74 style():fg(-1), bg(-2), set_attrs(0), clear_attrs(0), flip_attrs(0)
75 {
76 }
77
81 void set_fg(short _fg) {if(_fg >= 0) fg=_fg;}
82
86 void set_bg(short _bg) {if(_bg >= -1) bg = _bg;}
87
89 void attrs_on(attr_t attrs)
90 {
91 set_attrs|=attrs;
92 clear_attrs&=~attrs;
93 flip_attrs&=~attrs;
94 }
95
97 void attrs_off(attr_t attrs)
98 {
99 clear_attrs|=attrs;
100 set_attrs&=~attrs;
101 flip_attrs&=~attrs;
102 }
103
105 void attrs_flip(attr_t attrs)
106 {
107 flip_attrs^=attrs;
108 }
109
113 void apply_style(const style &other)
114 {
115 set_fg(other.fg);
116 set_bg(other.bg);
117 attrs_on(other.set_attrs);
118 attrs_off(other.clear_attrs);
119 attrs_flip(other.flip_attrs);
120 }
121
125 style operator+(const style &other) const
126 {
127 style rval(*this);
128 rval+=other;
129 return rval;
130 }
131
133 style &operator+=(const style &other)
134 {
135 apply_style(other);
136 return *this;
137 }
138
139 bool operator==(const style &other) const
140 {
141 return fg == other.fg && bg == other.bg &&
142 set_attrs == other.set_attrs && clear_attrs == other.clear_attrs &&
143 flip_attrs == other.flip_attrs;
144 }
145
146 bool operator!=(const style &other) const
147 {
148 return fg != other.fg || bg != other.bg ||
149 set_attrs != other.set_attrs || clear_attrs != other.clear_attrs ||
150 flip_attrs != other.flip_attrs;
151 }
152
154 short get_fg() const {return fg<0?0:fg;}
156 short get_bg() const {return bg<0?0:bg;}
158 attr_t get_attrs() const
159 {
160 attr_t rval=0;
161 rval |= set_attrs;
162 rval &= ~clear_attrs;
163 rval ^= flip_attrs;
164 rval |= config::mix_color(0, fg, bg);
165 if(fg == bg)
166 rval |= A_INVIS;
167 return rval;
168 }
169
171 chtype apply_to(chtype ch) const
172 {
173 // Relies somewhat on the bitwise representation of attributes;
174 // the multicharacter-capable stuff needed for utf8 will make this
175 // go away (for better or for worse..)
176 return (ch & A_CHARTEXT) |
177 config::mix_color(ch, fg, bg) |
178 ((((ch & ~ (A_CHARTEXT | A_COLOR)) | set_attrs) & ~clear_attrs) ^ flip_attrs);
179 }
180
183 {
184 // Relies somewhat on the bitwise representation of attributes;
185 // the multicharacter-capable stuff needed for utf8 will make this
186 // go away (for better or for worse..)
187 return wchtype(ch.ch,
188 config::mix_color(ch.attrs, fg, bg) |
189 ((((ch.attrs & ~ A_COLOR) | set_attrs) & ~clear_attrs) ^ flip_attrs));
190 }
191 };
192
193 // To allow styles to be built functionally, the following
194 // 'constructors' are provided. The main idea here is to make
195 // default-setting more compact and less obscure.
196
200 inline style style_fg(short fg)
201 {
202 style rval;
203 rval.set_fg(fg);
204 return rval;
205 }
206
210 inline style style_bg(short bg)
211 {
212 style rval;
213 rval.set_bg(bg);
214 return rval;
215 }
216
219 inline style style_attrs_on(attr_t attrs)
220 {
221 style rval;
222 rval.attrs_on(attrs);
223 return rval;
224 }
225
227 inline style style_attrs_off(attr_t attrs)
228 {
229 style rval;
230 rval.attrs_off(attrs);
231 return rval;
232 }
233
235 inline style style_attrs_flip(attr_t attrs)
236 {
237 style rval;
238 rval.attrs_flip(attrs);
239 return rval;
240 }
241
246 const style &get_style(const std::string &name);
247
249 void set_style(const std::string &name, const style &style);
250}
251
252#endif // STYLE_H
A "style" is a setting to be applied to a display element (widget, text, etc).
Definition style.h:52
style operator+(const style &other) const
Definition style.h:125
void set_fg(short _fg)
Set the foreground color.
Definition style.h:81
short get_fg() const
Definition style.h:154
attr_t get_attrs() const
Definition style.h:158
short get_bg() const
Definition style.h:156
style & operator+=(const style &other)
Shorthand for apply_style.
Definition style.h:133
wchtype apply_to(wchtype ch) const
Definition style.h:182
void apply_style(const style &other)
Update this style by applying the settings in the other style.
Definition style.h:113
void set_bg(short _bg)
Set the background color.
Definition style.h:86
void attrs_on(attr_t attrs)
Set the given attribute(s).
Definition style.h:89
chtype apply_to(chtype ch) const
Definition style.h:171
void attrs_flip(attr_t attrs)
Flip the given attribute(s).
Definition style.h:105
void attrs_off(attr_t attrs)
Clear the given attribute(s).
Definition style.h:97
style()
Initialize an "empty" style.
Definition style.h:74
Routines to support independently changing foreground and background colors.
int mix_color(short color, short fg, short bg)
Definition colors.cc:101
The namespace containing everything defined by cwidget.
Definition columnify.cc:28
const style & get_style(const std::string &name)
Look up a style in the global registry.
Definition style.cc:30
style style_attrs_on(attr_t attrs)
Definition style.h:219
void set_style(const std::string &name, const style &style)
Place a style in the global registry.
Definition style.cc:41
style style_attrs_flip(attr_t attrs)
Definition style.h:235
style style_attrs_off(attr_t attrs)
Definition style.h:227
style style_bg(short bg)
Definition style.h:210
style style_fg(short fg)
Definition style.h:200
A structure that amalgamates a wchar_t together with attributes.
Definition curses++.h:49
attr_t attrs
The text attributes (including color) associated with this character.
Definition curses++.h:60
wchar_t ch
The character value associated with this string.
Definition curses++.h:55