1 /* Public Domain Curses */
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 ### Description
48 
49    These functions manipulate the current attributes and/or colors
50    of the named window.  These attributes can be any combination
51    of A_STANDOUT, A_REVERSE, A_BOLD, A_DIM, A_BLINK, A_UNDERLINE.
52 
53    These constants are defined in <curses.h> and can be combined
54    with the bitwise-OR operator (|).
55 
56    The current attributes of a window are applied to all chtypes
57    that are written into the window with waddch(). Attributes are
58    a property of the chtype, and move with the character through
59    any scrolling or insert/delete operations.
60 
61    attrset() sets the current attributes of the given window to
62    attrs. attroff() turns off the named attributes without
63    affecting any other attributes; attron() turns them on.
64    color_set() sets the window color to the value of color_pair.
65 
66    standout() is the same as attron(A_STANDOUT). standend() is the
67    same as attrset(A_NORMAL); that is, it turns off all attributes.
68 
69 ### Return Value
70 
71    All functions return OK on success and ERR on error.
72 
73 ### Portability
74                              X/Open    BSD    SYS V
75     attroff                     Y       Y       Y
76     wattroff                    Y       Y       Y
77     attron                      Y       Y       Y
78     wattron                     Y       Y       Y
79     attrset                     Y       Y       Y
80     wattrset                    Y       Y       Y
81     standend                    Y       Y       Y
82     wstandend                   Y       Y       Y
83     standout                    Y       Y       Y
84     wstandout                   Y       Y       Y
85     color_set                   Y
86     wcolor_set                  Y
87     attr_get                    Y
88     wattr_get                   Y
89     attr_on                     Y
90     wattr_on                    Y
91     attr_off                    Y
92     wattr_off                   Y
93     attr_set                    Y
94     wattr_set                   Y
95     chgat                       Y
96     wchgat                      Y
97     mvchgat                     Y
98     mvwchgat                    Y
99     getattrs                    -
100 
101 **man-end****************************************************************/
102 
wattroff(WINDOW * win,chtype attrs)103 int wattroff(WINDOW *win, chtype attrs)
104 {
105     PDC_LOG(("wattroff() - called\n"));
106 
107     if (!win)
108         return ERR;
109 
110     win->_attrs &= (~attrs & A_ATTRIBUTES);
111 
112     return OK;
113 }
114 
attroff(chtype attrs)115 int attroff(chtype attrs)
116 {
117     PDC_LOG(("attroff() - called\n"));
118 
119     return wattroff(stdscr, attrs);
120 }
121 
wattron(WINDOW * win,chtype attrs)122 int wattron(WINDOW *win, chtype attrs)
123 {
124     chtype newcolr, oldcolr, newattr, oldattr;
125 
126     PDC_LOG(("wattron() - called\n"));
127 
128     if (!win)
129         return ERR;
130 
131     if ((win->_attrs & A_COLOR) && (attrs & A_COLOR))
132     {
133         oldcolr = win->_attrs & A_COLOR;
134         oldattr = win->_attrs ^ oldcolr;
135         newcolr = attrs & A_COLOR;
136         newattr = (attrs & A_ATTRIBUTES) ^ newcolr;
137         newattr |= oldattr;
138         win->_attrs = newattr | newcolr;
139     }
140     else
141         win->_attrs |= (attrs & A_ATTRIBUTES);
142 
143     return OK;
144 }
145 
attron(chtype attrs)146 int attron(chtype attrs)
147 {
148     PDC_LOG(("attron() - called\n"));
149 
150     return wattron(stdscr, attrs);
151 }
152 
wattrset(WINDOW * win,chtype attrs)153 int wattrset(WINDOW *win, chtype attrs)
154 {
155     PDC_LOG(("wattrset() - called\n"));
156 
157     if (!win)
158         return ERR;
159 
160     win->_attrs = attrs & A_ATTRIBUTES;
161 
162     return OK;
163 }
164 
attrset(chtype attrs)165 int attrset(chtype attrs)
166 {
167     PDC_LOG(("attrset() - called\n"));
168 
169     return wattrset(stdscr, attrs);
170 }
171 
standend(void)172 int standend(void)
173 {
174     PDC_LOG(("standend() - called\n"));
175 
176     return wattrset(stdscr, A_NORMAL);
177 }
178 
standout(void)179 int standout(void)
180 {
181     PDC_LOG(("standout() - called\n"));
182 
183     return wattrset(stdscr, A_STANDOUT);
184 }
185 
wstandend(WINDOW * win)186 int wstandend(WINDOW *win)
187 {
188     PDC_LOG(("wstandend() - called\n"));
189 
190     return wattrset(win, A_NORMAL);
191 }
192 
wstandout(WINDOW * win)193 int wstandout(WINDOW *win)
194 {
195     PDC_LOG(("wstandout() - called\n"));
196 
197     return wattrset(win, A_STANDOUT);
198 }
199 
getattrs(WINDOW * win)200 chtype getattrs(WINDOW *win)
201 {
202     return win ? win->_attrs : 0;
203 }
204 
wcolor_set(WINDOW * win,short color_pair,void * opts)205 int wcolor_set(WINDOW *win, short color_pair, void *opts)
206 {
207     PDC_LOG(("wcolor_set() - called\n"));
208 
209     if (!win)
210         return ERR;
211 
212     win->_attrs = (win->_attrs & ~A_COLOR) | COLOR_PAIR(color_pair);
213 
214     return OK;
215 }
216 
color_set(short color_pair,void * opts)217 int color_set(short color_pair, void *opts)
218 {
219     PDC_LOG(("color_set() - called\n"));
220 
221     return wcolor_set(stdscr, color_pair, opts);
222 }
223 
wattr_get(WINDOW * win,attr_t * attrs,short * color_pair,void * opts)224 int wattr_get(WINDOW *win, attr_t *attrs, short *color_pair, void *opts)
225 {
226     PDC_LOG(("wattr_get() - called\n"));
227 
228     if (!win)
229         return ERR;
230 
231     if (attrs)
232         *attrs = win->_attrs & (A_ATTRIBUTES & ~A_COLOR);
233 
234     if (color_pair)
235         *color_pair = (short)PAIR_NUMBER(win->_attrs);
236 
237     return OK;
238 }
239 
attr_get(attr_t * attrs,short * color_pair,void * opts)240 int attr_get(attr_t *attrs, short *color_pair, void *opts)
241 {
242     PDC_LOG(("attr_get() - called\n"));
243 
244     return wattr_get(stdscr, attrs, color_pair, opts);
245 }
246 
wattr_off(WINDOW * win,attr_t attrs,void * opts)247 int wattr_off(WINDOW *win, attr_t attrs, void *opts)
248 {
249     PDC_LOG(("wattr_off() - called\n"));
250 
251     return wattroff(win, attrs);
252 }
253 
attr_off(attr_t attrs,void * opts)254 int attr_off(attr_t attrs, void *opts)
255 {
256     PDC_LOG(("attr_off() - called\n"));
257 
258     return wattroff(stdscr, attrs);
259 }
260 
wattr_on(WINDOW * win,attr_t attrs,void * opts)261 int wattr_on(WINDOW *win, attr_t attrs, void *opts)
262 {
263     PDC_LOG(("wattr_off() - called\n"));
264 
265     return wattron(win, attrs);
266 }
267 
attr_on(attr_t attrs,void * opts)268 int attr_on(attr_t attrs, void *opts)
269 {
270     PDC_LOG(("attr_on() - called\n"));
271 
272     return wattron(stdscr, attrs);
273 }
274 
wattr_set(WINDOW * win,attr_t attrs,short color_pair,void * opts)275 int wattr_set(WINDOW *win, attr_t attrs, short color_pair, void *opts)
276 {
277     PDC_LOG(("wattr_set() - called\n"));
278 
279     if (!win)
280         return ERR;
281 
282     win->_attrs = (attrs & (A_ATTRIBUTES & ~A_COLOR)) | COLOR_PAIR(color_pair);
283 
284     return OK;
285 }
286 
attr_set(attr_t attrs,short color_pair,void * opts)287 int attr_set(attr_t attrs, short color_pair, void *opts)
288 {
289     PDC_LOG(("attr_get() - called\n"));
290 
291     return wattr_set(stdscr, attrs, color_pair, opts);
292 }
293 
wchgat(WINDOW * win,int n,attr_t attr,short color,const void * opts)294 int wchgat(WINDOW *win, int n, attr_t attr, short color, const void *opts)
295 {
296     chtype *dest, newattr;
297     int startpos, endpos;
298 
299     PDC_LOG(("wchgat() - called\n"));
300 
301     if (!win)
302         return ERR;
303 
304     newattr = (attr & A_ATTRIBUTES) | COLOR_PAIR(color);
305 
306     startpos = win->_curx;
307     endpos = ((n < 0) ? win->_maxx : min(startpos + n, win->_maxx)) - 1;
308     dest = win->_y[win->_cury];
309 
310     for (n = startpos; n <= endpos; n++)
311         dest[n] = (dest[n] & A_CHARTEXT) | newattr;
312 
313     n = win->_cury;
314 
315     if (startpos < win->_firstch[n] || win->_firstch[n] == _NO_CHANGE)
316         win->_firstch[n] = startpos;
317 
318     if (endpos > win->_lastch[n])
319         win->_lastch[n] = endpos;
320 
321     PDC_sync(win);
322 
323     return OK;
324 }
325 
chgat(int n,attr_t attr,short color,const void * opts)326 int chgat(int n, attr_t attr, short color, const void *opts)
327 {
328     PDC_LOG(("chgat() - called\n"));
329 
330     return wchgat(stdscr, n, attr, color, opts);
331 }
332 
mvchgat(int y,int x,int n,attr_t attr,short color,const void * opts)333 int mvchgat(int y, int x, int n, attr_t attr, short color, const void *opts)
334 {
335     PDC_LOG(("mvchgat() - called\n"));
336 
337     if (move(y, x) == ERR)
338         return ERR;
339 
340     return wchgat(stdscr, n, attr, color, opts);
341 }
342 
mvwchgat(WINDOW * win,int y,int x,int n,attr_t attr,short color,const void * opts)343 int mvwchgat(WINDOW *win, int y, int x, int n, attr_t attr, short color,
344              const void *opts)
345 {
346     PDC_LOG(("mvwchgat() - called\n"));
347 
348     if (wmove(win, y, x) == ERR)
349         return ERR;
350 
351     return wchgat(win, n, attr, color, opts);
352 }
353