This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GUI_termwin.cpp
266 lines (233 loc) · 4.85 KB
/
GUI_termwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
GUILIB: An example GUI framework library for use with SDL
*/
/* A simple dumb terminal scrollable widget */
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "GUI_termwin.h"
#include "GUI_loadimage.h"
#define KEYREPEAT_TIME 100 /* Repeat every 100 ms */
GUI_TermWin:: GUI_TermWin(int x, int y, int w, int h, SDL_Surface *Font,
void (*KeyProc)(SDL_Keycode key, Uint16 unicode), int scrollback)
: GUI_Scrollable(NULL, x, y, w, h)
{
/* The font surface should be a 16x16 character pixmap */
if ( Font == NULL ) {
font = GUI_DefaultFont();
} else {
font = Font;
}
charh = font->h/16;
charw = font->w/16;
rows = h/(charh-1); /* Bottom row in font is not displayed */
cols = w/charw;
if ( scrollback == 0 ) {
scrollback = rows;
}
total_rows = scrollback;
/* Allocate and clear the character buffer */
vscreen = new Uint8[total_rows*cols];
Clear();
/* Set the user-defined keyboard handler */
keyproc = KeyProc;
}
GUI_TermWin:: ~GUI_TermWin()
{
delete[] vscreen;
}
void
GUI_TermWin:: Display(void)
{
int row, i, j;
Uint8 ch;
SDL_Rect src;
SDL_Rect dst;
row = first_row+scroll_row;
if ( row < 0 ) {
row = total_rows+row;
}
src.w = charw;
src.h = charh-1;
dst.w = charw;
dst.h = charh-1;
for ( i=0; i<rows; ++i ) {
for ( j=0; j<cols; ++j ) {
ch = vscreen[row*cols+j];
src.x = (ch%16)*charw;
src.y = (ch/16)*charh;
dst.x = area.x+j*charw;
dst.y = area.y+i*(charh-1);
SDL_BlitSurface(font, &src, screen, &dst);
}
row = (row+1)%total_rows;
}
changed = 0;
}
int
GUI_TermWin:: Scroll(int amount)
{
if ( amount ) {
scroll_row += amount;
if ( scroll_row < scroll_min ) {
scroll_row = scroll_min;
} else
if ( scroll_row > scroll_max ) {
scroll_row = scroll_max;
}
changed = 1;
}
return(scroll_row);
}
void
GUI_TermWin:: Range(int &first, int &last)
{
first = scroll_min; last = scroll_max;
}
GUI_status
GUI_TermWin:: TextInput(const char *text)
{
GUI_status status;
status = GUI_PASS;
if ( keyproc ) {
Uint16 *text16 = SDL_iconv_utf8_ucs2(text);
if (text16) {
for (int i = 0; text16[i]; ++i) {
keyproc(0, text16[i]);
}
SDL_free(text16);
}
status = GUI_YUM;
}
return(status);
}
GUI_status
GUI_TermWin:: KeyDown(SDL_Keysym key)
{
GUI_status status;
status = GUI_PASS;
if ( keyproc ) {
keyproc(key.sym, 0);
status = GUI_YUM;
}
return(status);
}
GUI_status
GUI_TermWin:: KeyUp(SDL_Keysym key)
{
return(GUI_PASS);
}
void
GUI_TermWin:: NewLine(void)
{
if ( cur_row == (rows-1) ) {
int newrow;
first_row = (first_row+1)%total_rows;
newrow = (first_row+rows-1)%total_rows;
memset(&vscreen[newrow*cols], ' ', cols);
} else {
cur_row += 1;
}
cur_col = 0;
}
void
GUI_TermWin:: AddText(const char *text, int len)
{
int row;
while ( len-- ) {
switch (*text) {
case '\b': {
/* Backspace */
if ( cur_col > 0 ) {
--cur_col;
row = (first_row+cur_row)%total_rows;
vscreen[row*cols+cur_col] = ' ';
}
}
break;
case '\r':
/* Skip '\n' in "\r\n" sequence */
if ( (len > 0) && (*(text+1) == '\n') ) {
--len; ++text;
}
/* Fall through to newline */
case '\n': {
NewLine();
}
break;
default: { /* Put characters on screen */
if ( cur_col == cols ) {
NewLine();
}
row = (first_row+cur_row)%total_rows;
vscreen[row*cols+cur_col] = *text;
cur_col += 1;
}
break;
}
++text;
}
/* Reset any scrolling, and mark us for update on idle */
scroll_row = 0;
changed = 1;
}
void
GUI_TermWin:: AddText(const char *fmt, ...)
{
char text[1024]; /* Caution! Buffer overflow!! */
va_list ap;
va_start(ap, fmt);
vsprintf(text, fmt, ap);
AddText(text, strlen(text));
va_end(ap);
}
void
GUI_TermWin:: Clear(void)
{
/* Clear the virtual screen */
memset(vscreen, ' ', total_rows*cols);
/* Set the first row in buffer, display row in buffer, and
current cursor offset.
*/
first_row = total_rows-rows; /* Actual address */
scroll_min = -(total_rows-rows); /* Virtual address */
scroll_row = 0; /* Virtual address */
scroll_max = 0; /* Virtual address */
cur_row = 0; /* Virtual address */
cur_col = 0; /* Virtual address */
/* Mark the display for update */
changed = 1;
}
int
GUI_TermWin:: Changed(void)
{
return(changed);
}
GUI_status
GUI_TermWin:: Idle(void)
{
GUI_status status;
status = GUI_PASS;
/* Check to see if display contents have changed */
if ( changed ) {
status = GUI_REDRAW;
changed = 0;
}
return(status);
}
/* change FG/BG colors and transparency */
void GUI_TermWin::SetColoring(Uint8 fr,Uint8 fg,Uint8 fb, int bg_opaque,
Uint8 br, Uint8 bg, Uint8 bb)
{
SDL_Color colors[3]={{br,bg,bb,0},{fr,fg,fb,0}};
if (bg_opaque)
{
SDL_SetPaletteColors(font->format->palette,colors,0,2);
SDL_SetColorKey(font,SDL_FALSE,0);
}
else
{
SDL_SetPaletteColors(font->format->palette,&colors[1],1,1);
SDL_SetColorKey(font,SDL_TRUE,0);
}
}