1 /*
2  * Copyright (c)2004 Cat's Eye Technologies.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
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
14  *   distribution.
15  *
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.
19  *
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.
32  */
33 
34 /*
35  * curses_bar.c
36  * $Id: curses_bar.c,v 1.9 2005/08/26 22:44:37 cpressey Exp $
37  */
38 
39 #include <ncurses.h>
40 #include <panel.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #ifdef SYSTEM_AURA
46 #include <aura/mem.h>
47 #else
48 #include "mem.h"
49 #endif
50 
51 #include "curses_util.h"
52 #include "curses_bar.h"
53 
54 struct curses_bar *
curses_bar_new(unsigned int x,unsigned int y,unsigned int width,unsigned int height,int colors,int flags)55 curses_bar_new(unsigned int x, unsigned int y,
56 		unsigned int width, unsigned int height,
57 		int colors, int flags)
58 {
59 	struct curses_bar *b;
60 
61 	AURA_MALLOC(b, curses_bar);
62 
63 	if (flags & CURSES_BAR_WIDEN)
64 		width = xmax;
65 	if (flags & CURSES_BAR_BOTTOM)
66 		y = ymax - 1;
67 
68 	b->x = x;
69 	b->y = y;
70 	b->width = width;
71 	b->height = height;
72 	b->colors = colors;
73 
74 	if ((b->win = newwin(height, width, y, x)) == NULL) {
75 		AURA_FREE(b, curses_bar);
76 		return(NULL);
77 	}
78 
79 	curses_colors_set(b->win, colors);
80 	curses_window_blank(b->win);
81 
82 	if ((b->pan = new_panel(b->win)) == NULL) {
83 		delwin(b->win);
84 		AURA_FREE(b, curses_bar);
85 		return(NULL);
86 	}
87 
88 	return(b);
89 }
90 
91 void
curses_bar_free(struct curses_bar * b)92 curses_bar_free(struct curses_bar *b)
93 {
94 	if (b != NULL) {
95 		if (b->pan != NULL) {
96 			del_panel(b->pan);
97 			if (b->win != NULL) {
98 				delwin(b->win);
99 			}
100 		}
101 		AURA_FREE(b, curses_bar);
102 	}
103 
104 	update_panels();
105 	doupdate();
106 }
107 
108 void
curses_bar_set_text(struct curses_bar * b,const char * fmt,...)109 curses_bar_set_text(struct curses_bar *b, const char *fmt, ...)
110 {
111 	int spaces;
112 	char *text;
113 	va_list va;
114 
115 	va_start(va, fmt);
116 	if (vasprintf(&text, fmt, va) == -1)
117 		return;
118 	va_end(va);
119 	text[b->width] = '\0';
120 
121 	curses_colors_set(b->win, b->colors);
122 	mvwaddstr(b->win, 0, 0, text);
123 	spaces = b->width - strlen(text);
124 	whline(b->win, ' ', spaces);
125 	free(text);
126 }
127