2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of Cat's Eye Technologies nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * $Id: curses_util.c,v 1.7 2005/02/08 07:49:03 cpressey Exp $
46 #include "curses_util.h"
48 unsigned int ymax, xmax;
50 int allocated_colors = 0;
57 struct curses_attr colors_tab[CURSES_COLORS_MAX];
71 * If there is an established color pair with the given fg and bg
72 * colors, return it. Else allocate a new pair with these colors
76 curses_colors_find(int fg, int bg)
82 pair_no <= allocated_colors && pair_no < COLOR_PAIRS;
84 pair_content(pair_no, &fge, &bge);
85 if (fg == fge && bg == bge)
90 * No pair was found, allocate a new one.
92 if (allocated_colors < (COLOR_PAIRS-1)) {
94 init_pair(allocated_colors, fg, bg);
95 return(allocated_colors);
99 * No space to allocate a new one, return error.
105 curses_colors_cfg(int role, int fg, int bg, int bold)
109 pair_no = curses_colors_find(fg, bg);
111 colors_tab[role].pair_no = pair_no;
112 colors_tab[role].bold = bold;
114 colors_tab[role].pair_no = 0;
115 colors_tab[role].bold = bold;
120 curses_colors_init(int force_monochrome)
122 if (!force_monochrome) {
130 * By default, make it look kinda like the default libdialog.
132 curses_colors_cfg(CURSES_COLORS_NORMAL, COLOR_BLACK, COLOR_GREY, 0);
133 curses_colors_cfg(CURSES_COLORS_BACKDROP, COLOR_WHITE, COLOR_BLUE, 0);
134 curses_colors_cfg(CURSES_COLORS_MENUBAR, COLOR_BLACK, COLOR_GREY, 0);
135 curses_colors_cfg(CURSES_COLORS_STATUSBAR, COLOR_BLACK, COLOR_GREY, 0);
136 curses_colors_cfg(CURSES_COLORS_BORDER, COLOR_WHITE, COLOR_GREY, 1);
137 curses_colors_cfg(CURSES_COLORS_FORMTITLE, COLOR_YELLOW, COLOR_GREY, 1);
138 curses_colors_cfg(CURSES_COLORS_LABEL, COLOR_BLACK, COLOR_GREY, 0);
139 curses_colors_cfg(CURSES_COLORS_CONTROL, COLOR_BLACK, COLOR_GREY, 0);
140 curses_colors_cfg(CURSES_COLORS_TEXT, COLOR_BLACK, COLOR_GREY, 0);
141 curses_colors_cfg(CURSES_COLORS_FOCUS, COLOR_WHITE, COLOR_BLUE, 1);
142 curses_colors_cfg(CURSES_COLORS_SCROLLAREA,COLOR_GREY, COLOR_BLACK, 0);
143 curses_colors_cfg(CURSES_COLORS_SCROLLBAR, COLOR_WHITE, COLOR_BLUE, 1);
144 curses_colors_cfg(CURSES_COLORS_ACCEL, COLOR_WHITE, COLOR_GREY, 1);
145 curses_colors_cfg(CURSES_COLORS_ACCELFOCUS,COLOR_YELLOW, COLOR_BLUE, 1);
149 curses_colors_set(WINDOW *w, int a)
152 wattrset(w, COLOR_PAIR(colors_tab[a].pair_no));
153 if (colors_tab[a].bold)
160 curses_window_blank(WINDOW *w)
164 for (i = 0; i <= ymax; i++) {
166 whline(w, ' ', xmax);
173 curses_frame_draw(int x, int y, int width, int height)
177 mvaddch(y, x, ACS_ULCORNER);
178 hline(ACS_HLINE, width - 2);
179 mvaddch(y, x + width - 1, ACS_URCORNER);
181 mvaddch(y + height - 1, x, ACS_LLCORNER);
182 hline(ACS_HLINE, width - 2);
183 mvaddch(y + height - 1, x + width - 1, ACS_LRCORNER);
186 vline(ACS_VLINE, height - 2);
188 move(y + 1, x + width - 1);
189 vline(ACS_VLINE, height - 2);
191 for (i = y + 1; i < y + height - 1; i++) {
193 hline(' ', width - 2);
198 curses_load_backdrop(WINDOW *w, const char *filename)
207 curses_colors_set(w, CURSES_COLORS_BACKDROP);
208 curses_window_blank(w);
210 if ((f = fopen(filename, "r")) != NULL) {
211 while (fgets(line, 79, f) != NULL) {
214 if (line[strlen(line) - 1] == '\n')
215 line[strlen(line) - 1] = '\0';
216 mvwaddnstr(w, row++, 0, line, mx);
223 curses_debug_str(const char *s)
228 sprintf(b, "[%77s]", s);
234 curses_debug_int(int i)
239 sprintf(b, "[%06d]", i);
245 curses_debug_key(int i)
250 sprintf(b, "[%06d] %s", i, keyname(i));
256 curses_debug_float(float f)
261 sprintf(b, "[%09.3f]", f);
269 * text: The text to word-wrap, as one long string. Spaces will be
270 * compressed, but end-of-line characters will be honoured.
271 * line: A buffer (must be allocated by the caller) to hold a single
272 * line extracted from text.
273 * width: The maximum width of a line.
274 * spos: Pointer to the source position in text. Should be initially
275 * set to zero, and retained between calls to this function.
276 * Returns: A boolean indicating whether the end of text was reached.
277 * Typically this function should be called repeatedly until
281 extract_wrapped_line(const char *text, char *line, int width, int *spos)
284 int saved_spos, saved_dpos;
288 * Skip over whitespace. If we find a newline or the
289 * end of the text, return a blank line. Leave *spos
290 * at the position of the 1st non-whitespace character.
292 while (isspace(text[*spos]) && text[*spos] != '\0') {
293 if (text[*spos] == '\n') {
302 * Save start position and destination position.
308 * Read a word from *spos onward.
310 while (!isspace(text[*spos]) &&
311 text[*spos] != '\0' &&
313 line[dpos++] = text[(*spos)++];
316 if (text[*spos] == '\0') {
318 * End of string - return this word as the last.
322 } else if (dpos >= width) {
324 * Last word is too long to fit on this line.
326 if (dpos - saved_dpos >= width) {
328 * In fact, it's too long to fit on any line!
331 line[width - 1] = '\0';
332 *spos = saved_spos + (dpos - saved_dpos);
336 * Save it for the next pass.
339 line[saved_dpos - 1] = '\0';