curses
[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         raw();
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 }
39
40
41 void tg_fgcolor(int col)
42 {
43         fgcol = curses_color(col);
44         init_pair(1, fgcol, bgcol);
45 }
46
47 void tg_bgcolor(int col)
48 {
49         bgcol = curses_color(col);
50         init_pair(1, fgcol, bgcol);
51 }
52
53 void tg_color(int col)
54 {
55         fgcol = curses_color(col & 0xf);
56         bgcol = curses_color((col >> 4) & 0xf);
57         init_pair(1, fgcol, bgcol);
58 }
59
60 void tg_bgchar(int c)
61 {
62         bgchar = c;
63 }
64
65
66 void tg_setcursor(int x, int y)
67 {
68         move(y, x);
69         cur_x = x;
70         cur_y = y;
71 }
72
73
74 void tg_text(int x, int y, const char *fmt, ...)
75 {
76         va_list ap;
77
78         va_start(ap, fmt);
79         tg_vtext(x, y, fmt, ap);
80         va_end(ap);
81 }
82
83 void tg_vtext(int x, int y, const char *fmt, va_list ap)
84 {
85         attron(COLOR_PAIR(1));
86         move(y, x);
87         vw_printw(stdscr, fmt, ap);
88         attroff(COLOR_PAIR(1));
89 }
90
91
92 void tg_rect(const char *label, int x, int y, int xsz, int ysz, unsigned int flags)
93 {
94         int i;
95
96         attron(COLOR_PAIR(1));
97
98         for(i=0; i<ysz; i++) {
99                 move(y + i, x);
100                 hline(bgchar, xsz);
101         }
102
103         if(flags & TGFX_FRAME) {
104                 move(y, x + 1);
105                 hline(ACS_HLINE, xsz - 2);
106                 move(y + ysz - 1, x + 1);
107                 hline(ACS_HLINE, xsz - 2);
108                 move(y + 1, x);
109                 vline(ACS_VLINE, ysz - 2);
110                 move(y + 1, x + xsz - 1);
111                 vline(ACS_VLINE, ysz - 2);
112
113                 mvaddch(y, x, ACS_ULCORNER);
114                 mvaddch(y, x + xsz - 1, ACS_URCORNER);
115                 mvaddch(y + ysz - 1, x, ACS_LLCORNER);
116                 mvaddch(y + ysz - 1, x + xsz - 1, ACS_LRCORNER);
117         }
118
119         if(label) {
120                 tg_text(x + 2, y, "%s", label);
121         }
122
123         attroff(COLOR_PAIR(1));
124 }
125
126 static int curses_color(int col)
127 {
128         switch(col) {
129         case TGFX_RED:
130                 return COLOR_RED;
131         case TGFX_BLUE:
132                 return COLOR_BLUE;
133         case TGFX_CYAN:
134                 return COLOR_CYAN;
135         case TGFX_YELLOW:
136                 return COLOR_YELLOW;
137         default:
138                 break;
139         }
140         return col;
141 }