c807aed9c3c879b4f39bbba33770dc780be13de0
[oftp] / src / unix / tgfx.c
1 #include <curses.h>
2 #include "tgfx.h"
3
4 static int fgcol, bgcol;
5 static int bgchar;
6 static int cur_x, cur_y;
7
8 static int curses_color(int col);
9
10 void tg_init(void)
11 {
12         initscr();
13         cbreak();
14         keypad(stdscr, TRUE);
15         noecho();
16         start_color();
17
18         fgcol = curses_color(TGFX_WHITE);
19         bgcol = curses_color(TGFX_BLACK);
20         bgchar = ' ';
21
22         tg_color(fgcol | (bgcol << 4));
23 }
24
25 void tg_cleanup(void)
26 {
27         endwin();
28 }
29
30 void tg_redraw(void)
31 {
32         move(cur_y, cur_x);
33         refresh();
34 }
35
36 void tg_clear(void)
37 {
38         clear();
39 }
40
41
42 void tg_fgcolor(int col)
43 {
44         fgcol = curses_color(col);
45         init_pair(1, fgcol, bgcol);
46 }
47
48 void tg_bgcolor(int col)
49 {
50         bgcol = curses_color(col);
51         init_pair(1, fgcol, bgcol);
52 }
53
54 void tg_color(int col)
55 {
56         fgcol = curses_color(col & 0xf);
57         bgcol = curses_color((col >> 4) & 0xf);
58         init_pair(1, fgcol, bgcol);
59 }
60
61 void tg_bgchar(int c)
62 {
63         bgchar = c;
64 }
65
66
67 void tg_setcursor(int x, int y)
68 {
69         move(y, x);
70         cur_x = x;
71         cur_y = y;
72 }
73
74
75 void tg_text(int x, int y, const char *fmt, ...)
76 {
77         va_list ap;
78
79         va_start(ap, fmt);
80         tg_vtext(x, y, fmt, ap);
81         va_end(ap);
82 }
83
84 void tg_vtext(int x, int y, const char *fmt, va_list ap)
85 {
86         attron(COLOR_PAIR(1));
87         move(y, x);
88         vw_printw(stdscr, fmt, ap);
89         attroff(COLOR_PAIR(1));
90 }
91
92
93 void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags)
94 {
95         int i;
96
97         attron(COLOR_PAIR(1));
98
99         for(i=0; i<ysz; i++) {
100                 move(y + i, x);
101                 hline(bgchar, xsz);
102         }
103
104         if(flags & TGFX_FRAME) {
105                 move(y, x + 1);
106                 hline(ACS_HLINE, xsz - 2);
107                 move(y + ysz - 1, x + 1);
108                 hline(ACS_HLINE, xsz - 2);
109                 move(y + 1, x);
110                 vline(ACS_VLINE, ysz - 2);
111                 move(y + 1, x + xsz - 1);
112                 vline(ACS_VLINE, ysz - 2);
113
114                 mvaddch(y, x, ACS_ULCORNER);
115                 mvaddch(y, x + xsz - 1, ACS_URCORNER);
116                 mvaddch(y + ysz - 1, x, ACS_LLCORNER);
117                 mvaddch(y + ysz - 1, x + xsz - 1, ACS_LRCORNER);
118         }
119
120         if(label) {
121                 tg_text(x + 2, y, "%s", label);
122         }
123
124         attroff(COLOR_PAIR(1));
125 }
126
127 static int curses_color(int col)
128 {
129         switch(col) {
130         case TGFX_RED:
131                 return COLOR_RED;
132         case TGFX_BLUE:
133                 return COLOR_BLUE;
134         case TGFX_CYAN:
135                 return COLOR_CYAN;
136         case TGFX_YELLOW:
137                 return COLOR_YELLOW;
138         default:
139                 break;
140         }
141         return col;
142 }