1 #include <cdk_int.h>
2
3 /*
4 * $Author: aleahmad $
5 * $Date: 2019/02/15 23:58:59 $
6 * $Revision: 1.63 $
7 */
8
9 /*
10 * This sets up a basic set of color pairs. These can be redefined
11 * if wanted...
12 */
initCDKColor(void)13 void initCDKColor (void)
14 {
15 #ifdef HAVE_START_COLOR
16 if (has_colors ())
17 {
18 int color[] =
19 {
20 COLOR_WHITE, COLOR_RED, COLOR_GREEN,
21 COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA,
22 COLOR_CYAN, COLOR_BLACK
23 };
24 int pair = 1;
25 int fg, bg;
26 int limit;
27
28 start_color ();
29
30 limit = (COLORS < MAX_COLORS) ? COLORS : MAX_COLORS;
31
32 /* Create the color pairs. */
33 for (fg = 0; fg < limit; fg++)
34 {
35 for (bg = 0; bg < limit; bg++)
36 {
37 init_pair ((short)pair++, (short)color[fg], (short)color[bg]);
38 }
39 }
40 }
41 #endif
42 }
43
44 /*
45 * This prints out a box around a window with attributes
46 */
boxWindow(WINDOW * window,chtype attr)47 void boxWindow (WINDOW *window, chtype attr)
48 {
49 /* *INDENT-EQLS* */
50 int tlx = 0;
51 int tly = 0;
52 int brx = getmaxx (window) - 1;
53 int bry = getmaxy (window) - 1;
54
55 /* Draw horizontal lines. */
56 (void)mvwhline (window, tly, 0, ACS_HLINE | attr, getmaxx (window));
57 (void)mvwhline (window, bry, 0, ACS_HLINE | attr, getmaxx (window));
58
59 /* Draw vertical lines. */
60 (void)mvwvline (window, 0, tlx, ACS_VLINE | attr, getmaxy (window));
61 (void)mvwvline (window, 0, brx, ACS_VLINE | attr, getmaxy (window));
62
63 /* Draw in the corners. */
64 (void)mvwaddch (window, tly, tlx, ACS_ULCORNER | attr);
65 (void)mvwaddch (window, tly, brx, ACS_URCORNER | attr);
66 (void)mvwaddch (window, bry, tlx, ACS_LLCORNER | attr);
67 (void)mvwaddch (window, bry, brx, ACS_LRCORNER | attr);
68 (void)wrefresh (window);
69 }
70
71 /*
72 * This draws a box with attributes and lets the user define
73 * each element of the box.
74 */
attrbox(WINDOW * win,chtype tlc,chtype trc,chtype blc,chtype brc,chtype horz,chtype vert,chtype attr)75 void attrbox (WINDOW *win,
76 chtype tlc,
77 chtype trc,
78 chtype blc,
79 chtype brc,
80 chtype horz,
81 chtype vert,
82 chtype attr)
83 {
84 /* *INDENT-EQLS* */
85 int x1 = 0;
86 int y1 = 0;
87 int y2 = getmaxy (win) - 1;
88 int x2 = getmaxx (win) - 1;
89 int count = 0;
90
91 /* Draw horizontal lines. */
92 if (horz != 0)
93 {
94 (void)mvwhline (win, y1, 0, horz | attr, getmaxx (win));
95 (void)mvwhline (win, y2, 0, horz | attr, getmaxx (win));
96 count++;
97 }
98
99 /* Draw vertical lines. */
100 if (vert != 0)
101 {
102 (void)mvwvline (win, 0, x1, vert | attr, getmaxy (win));
103 (void)mvwvline (win, 0, x2, vert | attr, getmaxy (win));
104 count++;
105 }
106
107 /* Draw in the corners. */
108 if (tlc != 0)
109 {
110 (void)mvwaddch (win, y1, x1, tlc | attr);
111 count++;
112 }
113 if (trc != 0)
114 {
115 (void)mvwaddch (win, y1, x2, trc | attr);
116 count++;
117 }
118 if (blc != 0)
119 {
120 (void)mvwaddch (win, y2, x1, blc | attr);
121 count++;
122 }
123 if (brc != 0)
124 {
125 (void)mvwaddch (win, y2, x2, brc | attr);
126 count++;
127 }
128 if (count != 0)
129 {
130 wrefresh (win);
131 }
132 }
133
134 /*
135 * Draw a box around the given window using the object's defined line-drawing
136 * characters.
137 */
drawObjBox(WINDOW * win,CDKOBJS * object)138 void drawObjBox (WINDOW *win, CDKOBJS *object)
139 {
140 attrbox (win,
141 object->ULChar,
142 object->URChar,
143 object->LLChar,
144 object->LRChar,
145 object->HZChar,
146 object->VTChar,
147 object->BXAttr);
148 }
149
150 /*
151 * This draws a line on the given window. (odd angle lines not working yet)
152 */
drawLine(WINDOW * window,int startx,int starty,int endx,int endy,chtype line)153 void drawLine (WINDOW *window, int startx, int starty, int endx, int endy, chtype line)
154 {
155 /* *INDENT-EQLS* */
156 int xdiff = endx - startx;
157 int ydiff = endy - starty;
158 int x = 0;
159 int y = 0;
160
161 /* Determine if we are drawing a horizontal or vertical line. */
162 if (ydiff == 0)
163 {
164 if (xdiff > 0)
165 (void)mvwhline (window, starty, startx, line, xdiff);
166 }
167 else if (xdiff == 0)
168 {
169 if (ydiff > 0)
170 (void)mvwvline (window, starty, startx, line, ydiff);
171 }
172 else
173 {
174 /* We need to determine the angle of the line. */
175 /* *INDENT-EQLS* */
176 int height = xdiff;
177 int width = ydiff;
178 int xratio = (height > width ? 1 : (width / height));
179 int yratio = (width > height ? (width / height) : 1);
180 int xadj = 0;
181 int yadj = 0;
182
183 /* Set the vars. */
184 x = startx;
185 y = starty;
186 while (x != endx && y != endy)
187 {
188 /* Add the char to the window. */
189 (void)mvwaddch (window, y, x, line);
190
191 /* Make the x and y adjustments. */
192 if (xadj != xratio)
193 {
194 x = (xdiff < 0 ? x - 1 : x + 1);
195 xadj++;
196 }
197 else
198 {
199 xadj = 0;
200 }
201 if (yadj != yratio)
202 {
203 y = (ydiff < 0 ? y - 1 : y + 1);
204 yadj++;
205 }
206 else
207 {
208 yadj = 0;
209 }
210 }
211 }
212 }
213
214 /*
215 * This draws a shadow around a window.
216 */
drawShadow(WINDOW * shadowWin)217 void drawShadow (WINDOW *shadowWin)
218 {
219 if (shadowWin != 0)
220 {
221 int x_hi = getmaxx (shadowWin) - 1;
222 int y_hi = getmaxy (shadowWin) - 1;
223
224 /* Draw the line on the bottom. */
225 (void)mvwhline (shadowWin, y_hi, 1, ACS_HLINE | A_DIM, x_hi);
226
227 /* Draw the line on the right. */
228 (void)mvwvline (shadowWin, 0, x_hi, ACS_VLINE | A_DIM, y_hi);
229
230 (void)mvwaddch (shadowWin, 0, x_hi, ACS_URCORNER | A_DIM);
231 (void)mvwaddch (shadowWin, y_hi, 0, ACS_LLCORNER | A_DIM);
232 (void)mvwaddch (shadowWin, y_hi, x_hi, ACS_LRCORNER | A_DIM);
233 wrefresh (shadowWin);
234 }
235 }
236
237 /*
238 * Write a string of blanks, using writeChar().
239 */
writeBlanks(WINDOW * window,int xpos,int ypos,int align,int start,int end)240 void writeBlanks (WINDOW *window, int xpos, int ypos, int align, int start, int end)
241 {
242 if (start < end)
243 {
244 unsigned want = (unsigned)(end - start) + 1000;
245 char *blanks = (char *)malloc (want);
246
247 if (blanks != 0)
248 {
249 cleanChar (blanks, (int)(want - 1), ' ');
250 writeChar (window, xpos, ypos, blanks, align, start, end);
251 freeChar (blanks);
252 }
253 }
254 }
255
CDKmvwaddch(WINDOW * window,int ypos,int xpos,chtype ch,chtype attr)256 static void CDKmvwaddch (WINDOW *window,
257 int ypos,
258 int xpos,
259 chtype ch,
260 chtype attr)
261 {
262 chtype color = (attr | ch) & (A_COLOR);
263 (void)mvwaddch (window, ypos, xpos, ((attr | ch) & (~A_COLOR)) | color);
264 }
265 /*
266 * This writes out a char * string with no attributes.
267 */
writeChar(WINDOW * window,int xpos,int ypos,char * string,int align,int start,int end)268 void writeChar (WINDOW *window,
269 int xpos,
270 int ypos,
271 char *string,
272 int align,
273 int start,
274 int end)
275 {
276 writeCharAttrib (window, xpos, ypos, string, A_NORMAL, align, start, end);
277 }
278
279 /*
280 * This writes out a char * string with attributes.
281 */
writeCharAttrib(WINDOW * window,int xpos,int ypos,char * string,chtype attr,int align,int start,int end)282 void writeCharAttrib (WINDOW *window,
283 int xpos,
284 int ypos,
285 char *string,
286 chtype attr,
287 int align,
288 int start,
289 int end)
290 {
291 int display = end - start;
292 int x;
293
294 if (align == HORIZONTAL)
295 {
296 /* Draw the message on a horizontal axis. */
297 display = MINIMUM (display, getmaxx (window) - 1);
298 for (x = 0; x < display; x++)
299 {
300 CDKmvwaddch (window,
301 ypos,
302 xpos + x,
303 CharOf (string[x + start]),
304 attr);
305 }
306 }
307 else
308 {
309 /* Draw the message on a vertical axis. */
310 display = MINIMUM (display, getmaxy (window) - 1);
311 for (x = 0; x < display; x++)
312 {
313 CDKmvwaddch (window,
314 ypos + x,
315 xpos,
316 CharOf (string[x + start]),
317 attr);
318 }
319 }
320 }
321
322 /*
323 * This writes out a chtype * string.
324 */
writeChtype(WINDOW * window,int xpos,int ypos,chtype * string,int align,int start,int end)325 void writeChtype (WINDOW *window,
326 int xpos,
327 int ypos,
328 chtype *string,
329 int align,
330 int start,
331 int end)
332 {
333 writeChtypeAttrib (window, xpos, ypos, string, A_NORMAL, align, start, end);
334 }
335
336 /*
337 * This writes out a chtype * string * with the given attributes added.
338 */
writeChtypeAttrib(WINDOW * window,int xpos,int ypos,chtype * string,chtype attr,int align,int start,int end)339 void writeChtypeAttrib (WINDOW *window,
340 int xpos,
341 int ypos,
342 chtype *string,
343 chtype attr,
344 int align,
345 int start,
346 int end)
347 {
348 /* *INDENT-EQLS* */
349 int diff = end - start;
350 int display = 0;
351 int x = 0;
352
353 if (align == HORIZONTAL)
354 {
355 /* Draw the message on a horizontal axis. */
356 display = MINIMUM (diff, getmaxx (window) - xpos);
357 for (x = 0; x < display; x++)
358 {
359 CDKmvwaddch (window, ypos, xpos + x, string[x + start], attr);
360 }
361 }
362 else
363 {
364 /* Draw the message on a vertical axis. */
365 display = MINIMUM (diff, getmaxy (window) - ypos);
366 for (x = 0; x < display; x++)
367 {
368 CDKmvwaddch (window, ypos + x, xpos, string[x + start], attr);
369 }
370 }
371 }
372