1 /* PDCurses */
2 
3 #include <curspriv.h>
4 
5 /*man-start**************************************************************
6 
7 attr
8 ----
9 
10 ### Synopsis
11 
12     int attroff(chtype attrs);
13     int wattroff(WINDOW *win, chtype attrs);
14     int attron(chtype attrs);
15     int wattron(WINDOW *win, chtype attrs);
16     int attrset(chtype attrs);
17     int wattrset(WINDOW *win, chtype attrs);
18     int standend(void);
19     int wstandend(WINDOW *win);
20     int standout(void);
21     int wstandout(WINDOW *win);
22 
23     int color_set(short color_pair, void *opts);
24     int wcolor_set(WINDOW *win, short color_pair, void *opts);
25 
26     int attr_get(attr_t *attrs, short *color_pair, void *opts);
27     int attr_off(attr_t attrs, void *opts);
28     int attr_on(attr_t attrs, void *opts);
29     int attr_set(attr_t attrs, short color_pair, void *opts);
30     int wattr_get(WINDOW *win, attr_t *attrs, short *color_pair,
31                   void *opts);
32     int wattr_off(WINDOW *win, attr_t attrs, void *opts);
33     int wattr_on(WINDOW *win, attr_t attrs, void *opts);
34     int wattr_set(WINDOW *win, attr_t attrs, short color_pair,
35                   void *opts);
36 
37     int chgat(int n, attr_t attr, short color, const void *opts);
38     int mvchgat(int y, int x, int n, attr_t attr, short color,
39                 const void *opts);
40     int mvwchgat(WINDOW *win, int y, int x, int n, attr_t attr,
41                  short color, const void *opts);
42     int wchgat(WINDOW *win, int n, attr_t attr, short color,
43                const void *opts);
44 
45     chtype getattrs(WINDOW *win);
46 
47     int underend(void);
48     int wunderend(WINDOW *win);
49     int underscore(void);
50     int wunderscore(WINDOW *win);
51 
52 ### Description
53 
54    These functions manipulate the current attributes and/or colors of
55    the named window. These attributes can be any combination of
56    A_STANDOUT, A_REVERSE, A_BOLD, A_DIM, A_BLINK, A_UNDERLINE. These
57    constants are defined in <curses.h> and can be combined with the
58    bitwise-OR operator (|).
59 
60    The current attributes of a window are applied to all chtypes that
61    are written into the window with waddch(). Attributes are a property
62    of the chtype, and move with the character through any scrolling or
63    insert/delete operations.
64 
65    wattrset() sets the current attributes of the given window to attrs.
66    attrset() is the stdscr version.
67 
68    wattroff() turns off the named attributes without affecting any other
69    attributes; wattron() turns them on.
70 
71    wcolor_set() sets the window color to the value of color_pair. opts
72    is unused.
73 
74    standout() is the same as attron(A_STANDOUT). standend() is the same
75    as attrset(A_NORMAL); that is, it turns off all attributes.
76 
77    The attr_* and wattr_* functions are intended for use with the WA_*
78    attributes. In PDCurses, these are the same as A_*, and there is no
79    difference in bevahior from the chtype-based functions. In all cases,
80    opts is unused.
81 
82    wattr_get() retrieves the attributes and color pair for the specified
83    window.
84 
85    wchgat() sets the color pair and attributes for the next n cells on
86    the current line of a given window, without changing the existing
87    text, or alterting the window's attributes. An n of -1 extends the
88    change to the edge of the window. The changes take effect
89    immediately. opts is unused.
90 
91    wunderscore() turns on the A_UNDERLINE attribute; wunderend() turns
92    it off. underscore() and underend() are the stdscr versions.
93 
94 ### Return Value
95 
96    All functions return OK on success and ERR on error.
97 
98 ### Portability
99                              X/Open  ncurses  NetBSD
100     attroff                     Y       Y       Y
101     wattroff                    Y       Y       Y
102     attron                      Y       Y       Y
103     wattron                     Y       Y       Y
104     attrset                     Y       Y       Y
105     wattrset                    Y       Y       Y
106     standend                    Y       Y       Y
107     wstandend                   Y       Y       Y
108     standout                    Y       Y       Y
109     wstandout                   Y       Y       Y
110     color_set                   Y       Y       Y
111     wcolor_set                  Y       Y       Y
112     attr_get                    Y       Y       Y
113     wattr_get                   Y       Y       Y
114     attr_on                     Y       Y       Y
115     wattr_on                    Y       Y       Y
116     attr_off                    Y       Y       Y
117     wattr_off                   Y       Y       Y
118     attr_set                    Y       Y       Y
119     wattr_set                   Y       Y       Y
120     chgat                       Y       Y       Y
121     wchgat                      Y       Y       Y
122     mvchgat                     Y       Y       Y
123     mvwchgat                    Y       Y       Y
124     getattrs                    -       Y       Y
125     underend                    -       -       Y
126     wunderend                   -       -       Y
127     underscore                  -       -       Y
128     wunderscore                 -       -       Y
129 
130 **man-end****************************************************************/
131 
wattroff(WINDOW * win,chtype attrs)132 int wattroff(WINDOW *win, chtype attrs)
133 {
134     PDC_LOG(("wattroff() - called\n"));
135 
136     if (!win)
137         return ERR;
138 
139     win->_attrs &= (~attrs & A_ATTRIBUTES);
140 
141     return OK;
142 }
143 
attroff(chtype attrs)144 int attroff(chtype attrs)
145 {
146     PDC_LOG(("attroff() - called\n"));
147 
148     return wattroff(stdscr, attrs);
149 }
150 
wattron(WINDOW * win,chtype attrs)151 int wattron(WINDOW *win, chtype attrs)
152 {
153     chtype newcolr, oldcolr, newattr, oldattr;
154 
155     PDC_LOG(("wattron() - called\n"));
156 
157     if (!win)
158         return ERR;
159 
160     if ((win->_attrs & A_COLOR) && (attrs & A_COLOR))
161     {
162         oldcolr = win->_attrs & A_COLOR;
163         oldattr = win->_attrs ^ oldcolr;
164         newcolr = attrs & A_COLOR;
165         newattr = (attrs & A_ATTRIBUTES) ^ newcolr;
166         newattr |= oldattr;
167         win->_attrs = newattr | newcolr;
168     }
169     else
170         win->_attrs |= (attrs & A_ATTRIBUTES);
171 
172     return OK;
173 }
174 
attron(chtype attrs)175 int attron(chtype attrs)
176 {
177     PDC_LOG(("attron() - called\n"));
178 
179     return wattron(stdscr, attrs);
180 }
181 
wattrset(WINDOW * win,chtype attrs)182 int wattrset(WINDOW *win, chtype attrs)
183 {
184     PDC_LOG(("wattrset() - called\n"));
185 
186     if (!win)
187         return ERR;
188 
189     win->_attrs = attrs & A_ATTRIBUTES;
190 
191     return OK;
192 }
193 
attrset(chtype attrs)194 int attrset(chtype attrs)
195 {
196     PDC_LOG(("attrset() - called\n"));
197 
198     return wattrset(stdscr, attrs);
199 }
200 
standend(void)201 int standend(void)
202 {
203     PDC_LOG(("standend() - called\n"));
204 
205     return wattrset(stdscr, A_NORMAL);
206 }
207 
standout(void)208 int standout(void)
209 {
210     PDC_LOG(("standout() - called\n"));
211 
212     return wattrset(stdscr, A_STANDOUT);
213 }
214 
wstandend(WINDOW * win)215 int wstandend(WINDOW *win)
216 {
217     PDC_LOG(("wstandend() - called\n"));
218 
219     return wattrset(win, A_NORMAL);
220 }
221 
wstandout(WINDOW * win)222 int wstandout(WINDOW *win)
223 {
224     PDC_LOG(("wstandout() - called\n"));
225 
226     return wattrset(win, A_STANDOUT);
227 }
228 
getattrs(WINDOW * win)229 chtype getattrs(WINDOW *win)
230 {
231     return win ? win->_attrs : 0;
232 }
233 
wcolor_set(WINDOW * win,short color_pair,void * opts)234 int wcolor_set(WINDOW *win, short color_pair, void *opts)
235 {
236     PDC_LOG(("wcolor_set() - called\n"));
237 
238     if (!win)
239         return ERR;
240 
241     win->_attrs = (win->_attrs & ~A_COLOR) | COLOR_PAIR(color_pair);
242 
243     return OK;
244 }
245 
color_set(short color_pair,void * opts)246 int color_set(short color_pair, void *opts)
247 {
248     PDC_LOG(("color_set() - called\n"));
249 
250     return wcolor_set(stdscr, color_pair, opts);
251 }
252 
wattr_get(WINDOW * win,attr_t * attrs,short * color_pair,void * opts)253 int wattr_get(WINDOW *win, attr_t *attrs, short *color_pair, void *opts)
254 {
255     PDC_LOG(("wattr_get() - called\n"));
256 
257     if (!win)
258         return ERR;
259 
260     if (attrs)
261         *attrs = win->_attrs & (A_ATTRIBUTES & ~A_COLOR);
262 
263     if (color_pair)
264         *color_pair = (short)PAIR_NUMBER(win->_attrs);
265 
266     return OK;
267 }
268 
attr_get(attr_t * attrs,short * color_pair,void * opts)269 int attr_get(attr_t *attrs, short *color_pair, void *opts)
270 {
271     PDC_LOG(("attr_get() - called\n"));
272 
273     return wattr_get(stdscr, attrs, color_pair, opts);
274 }
275 
wattr_off(WINDOW * win,attr_t attrs,void * opts)276 int wattr_off(WINDOW *win, attr_t attrs, void *opts)
277 {
278     PDC_LOG(("wattr_off() - called\n"));
279 
280     return wattroff(win, attrs);
281 }
282 
attr_off(attr_t attrs,void * opts)283 int attr_off(attr_t attrs, void *opts)
284 {
285     PDC_LOG(("attr_off() - called\n"));
286 
287     return wattroff(stdscr, attrs);
288 }
289 
wattr_on(WINDOW * win,attr_t attrs,void * opts)290 int wattr_on(WINDOW *win, attr_t attrs, void *opts)
291 {
292     PDC_LOG(("wattr_off() - called\n"));
293 
294     return wattron(win, attrs);
295 }
296 
attr_on(attr_t attrs,void * opts)297 int attr_on(attr_t attrs, void *opts)
298 {
299     PDC_LOG(("attr_on() - called\n"));
300 
301     return wattron(stdscr, attrs);
302 }
303 
wattr_set(WINDOW * win,attr_t attrs,short color_pair,void * opts)304 int wattr_set(WINDOW *win, attr_t attrs, short color_pair, void *opts)
305 {
306     PDC_LOG(("wattr_set() - called\n"));
307 
308     if (!win)
309         return ERR;
310 
311     win->_attrs = (attrs & (A_ATTRIBUTES & ~A_COLOR)) | COLOR_PAIR(color_pair);
312 
313     return OK;
314 }
315 
attr_set(attr_t attrs,short color_pair,void * opts)316 int attr_set(attr_t attrs, short color_pair, void *opts)
317 {
318     PDC_LOG(("attr_get() - called\n"));
319 
320     return wattr_set(stdscr, attrs, color_pair, opts);
321 }
322 
wchgat(WINDOW * win,int n,attr_t attr,short color,const void * opts)323 int wchgat(WINDOW *win, int n, attr_t attr, short color, const void *opts)
324 {
325     chtype *dest, newattr;
326     int startpos, endpos;
327 
328     PDC_LOG(("wchgat() - called\n"));
329 
330     if (!win)
331         return ERR;
332 
333     newattr = (attr & A_ATTRIBUTES) | COLOR_PAIR(color);
334 
335     startpos = win->_curx;
336     endpos = ((n < 0) ? win->_maxx : min(startpos + n, win->_maxx)) - 1;
337     dest = win->_y[win->_cury];
338 
339     for (n = startpos; n <= endpos; n++)
340         dest[n] = (dest[n] & A_CHARTEXT) | newattr;
341 
342     n = win->_cury;
343 
344     if (startpos < win->_firstch[n] || win->_firstch[n] == _NO_CHANGE)
345         win->_firstch[n] = startpos;
346 
347     if (endpos > win->_lastch[n])
348         win->_lastch[n] = endpos;
349 
350     PDC_sync(win);
351 
352     return OK;
353 }
354 
chgat(int n,attr_t attr,short color,const void * opts)355 int chgat(int n, attr_t attr, short color, const void *opts)
356 {
357     PDC_LOG(("chgat() - called\n"));
358 
359     return wchgat(stdscr, n, attr, color, opts);
360 }
361 
mvchgat(int y,int x,int n,attr_t attr,short color,const void * opts)362 int mvchgat(int y, int x, int n, attr_t attr, short color, const void *opts)
363 {
364     PDC_LOG(("mvchgat() - called\n"));
365 
366     if (move(y, x) == ERR)
367         return ERR;
368 
369     return wchgat(stdscr, n, attr, color, opts);
370 }
371 
mvwchgat(WINDOW * win,int y,int x,int n,attr_t attr,short color,const void * opts)372 int mvwchgat(WINDOW *win, int y, int x, int n, attr_t attr, short color,
373              const void *opts)
374 {
375     PDC_LOG(("mvwchgat() - called\n"));
376 
377     if (wmove(win, y, x) == ERR)
378         return ERR;
379 
380     return wchgat(win, n, attr, color, opts);
381 }
382 
underend(void)383 int underend(void)
384 {
385     PDC_LOG(("underend() - called\n"));
386 
387     return wattroff(stdscr, A_UNDERLINE);
388 }
389 
wunderend(WINDOW * win)390 int wunderend(WINDOW *win)
391 {
392     PDC_LOG(("wunderend() - called\n"));
393 
394     return wattroff(win, A_UNDERLINE);
395 }
396 
underscore(void)397 int underscore(void)
398 {
399     PDC_LOG(("underscore() - called\n"));
400 
401     return wattron(stdscr, A_UNDERLINE);
402 }
403 
wunderscore(WINDOW * win)404 int wunderscore(WINDOW *win)
405 {
406     PDC_LOG(("wunderscore() - called\n"));
407 
408     return wattron(win, A_UNDERLINE);
409 }
410