-
-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy pathkeyboard.h
351 lines (335 loc) · 7.74 KB
/
keyboard.h
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#include "../core/api.h"
#include "../core/resource.h"
#include "../core/word.h"
#include "../core/wnd.h"
#include "../core/display.h"
#include "../core/theme.h"
#include "../widgets/button.h"
#include <string.h>
//Changing key width/height will change the width/height of keyboard
#define KEY_WIDTH 65
#define KEY_HEIGHT 38
#define KEYBOARD_WIDTH ((KEY_WIDTH + 2) * 10)
#define KEYBOARD_HEIGHT ((KEY_HEIGHT + 2) * 4)
#define NUM_BOARD_WIDTH ((KEY_WIDTH + 2) * 4)
#define NUM_BOARD_HEIGHT ((KEY_HEIGHT + 2) * 4)
#define CAPS_WIDTH (KEY_WIDTH * 3 / 2)
#define DEL_WIDTH (KEY_WIDTH * 3 / 2 + 1)
#define ESC_WIDTH (KEY_WIDTH * 2 + 2)
#define SWITCH_WIDTH (KEY_WIDTH * 3 / 2 )
#define SPACE_WIDTH (KEY_WIDTH * 3 + 2 * 2)
#define DOT_WIDTH (KEY_WIDTH * 3 / 2 + 3)
#define ENTER_WIDTH (KEY_WIDTH * 2 + 2)
#define POS_X(c) ((KEY_WIDTH * c) + (c + 1) * 2)
#define POS_Y(r) ((KEY_HEIGHT * r) + (r + 1) * 2)
#define KEYBORAD_CLICK 0x5014
#define ON_KEYBORAD_UPDATE(func) \
{MSG_TYPE_WND, KEYBORAD_CLICK, 0, msgCallback(&func)},
typedef enum
{
STATUS_UPPERCASE,
STATUS_LOWERCASE
STATUS_NUMBER,
}KEYBOARD_STATUS;
typedef enum
{
STYLE_ALL_BOARD,
STYLE_NUM_BOARD
}KEYBOARD_STYLE;
typedef enum
{
CLICK_CHAR,
CLICK_ENTER,
CLICK_ESC
}CLICK_STATUS;
extern WND_TREE g_key_board_children[];
extern WND_TREE g_number_board_children[];
class c_keyboard: public c_wnd
{
public:
virtual char convert_char2num(char id)
{
char num = id;
switch (id)
{
case 'Q':
num = '1';
break;
case 'W':
num = '2';
break;
case 'E':
num = '3';
break;
case 'R':
num = '4';
break;
case 'T':
num = '5';
break;
case 'Y':
num = '6';
break;
case 'U':
num = '7';
break;
case 'I':
num = '8';
break;
case 'O':
num = '9';
break;
case 'P':
num = '0';
break;
default:
break;
}
return num;
}
virtual int connect(c_wnd *user, unsigned short resource_id, KEYBOARD_STYLE style)
{
c_rect user_rect;
user->get_wnd_rect(user_rect);
if (style == STYLE_ALL_BOARD)
{//Place keyboard at the bottom of user's parent window.
c_rect user_parent_rect;
user->get_parent()->get_wnd_rect(user_parent_rect);
return c_wnd::connect(user, resource_id, 0, (0 - user_rect.m_left), (user_parent_rect.height() - user_rect.m_top - KEYBOARD_HEIGHT - 1), KEYBOARD_WIDTH, KEYBOARD_HEIGHT, g_key_board_children);
}
else if (style == STYLE_NUM_BOARD)
{//Place keyboard below the user window.
return c_wnd::connect(user, resource_id, 0, 0, user_rect.height(), NUM_BOARD_WIDTH, NUM_BOARD_HEIGHT, g_number_board_children);
}
else
{
ASSERT(false);
}
return -1;
}
virtual void on_init_children()
{
c_wnd* child = m_top_child;
if (0 != child)
{
while (child)
{
((c_button*)child)->set_on_click(WND_CALLBACK(&c_keyboard::on_key_clicked));
child = child->get_next_sibling();
}
}
}
KEYBOARD_STATUS get_cap_status(){return m_cap_status;}
char* get_str() { return m_str; }
void set_on_click(WND_CALLBACK on_click) { this->on_click = on_click; }
protected:
virtual void pre_create_wnd()
{
m_attr = (WND_ATTRIBUTION)(ATTR_VISIBLE | ATTR_FOCUS);
m_cap_status = STATUS_UPPERCASE;
memset(m_str, 0, sizeof(m_str));
m_str_len = 0;
}
virtual void on_paint()
{
c_rect rect;
get_screen_rect(rect);
m_surface->fill_rect(rect, GL_RGB(0, 0, 0), m_z_order);
}
void on_key_clicked(int id, int param)
{
switch (id)
{
case 0x14:
on_caps_clicked(id, param);
break;
case '\n':
on_enter_clicked(id, param);
break;
case 0x1B:
on_esc_clicked(id, param);
break;
case 0x7F:
on_del_clicked(id, param);
break;
case 0x90:
on_num_switch_clicked(id, param);
break;
default:
on_char_clicked(id, param);
break;
}
}
void on_char_clicked(int id, int param)
{//id = char ascii code.
if (m_str_len >= sizeof(m_str))
{
return;
}
if ((id >= '0' && id <= '9') || id == ' ' || id == '.')
{
goto InputChar;
}
if (id >= 'A' && id <= 'Z')
{
if (STATUS_LOWERCASE == m_cap_status)
{
id += 0x20;
}
else if (STATUS_NUMBER == m_cap_status)
{
id = convert_char2num(id);
}
goto InputChar;
}
ASSERT(false);
InputChar:
m_str[m_str_len++] = id;
(m_parent->*(on_click))(m_id, CLICK_CHAR);
}
void on_del_clicked(int id, int param)
{
if (m_str_len <= 0)
{
return;
}
m_str[--m_str_len] = 0;
(m_parent->*(on_click))(m_id, CLICK_CHAR);
}
void on_caps_clicked(int id, int param)
{
m_cap_status = (m_cap_status == STATUS_LOWERCASE) ? STATUS_UPPERCASE : STATUS_LOWERCASE;
show_window();
}
void on_num_switch_clicked(int id, int param)
{
m_cap_status = (m_cap_status == STATUS_NUMBER) ? STATUS_UPPERCASE : STATUS_NUMBER;
show_window();
}
void on_enter_clicked(int id, int param)
{
memset(m_str, 0, sizeof(m_str));
(m_parent->*(on_click))(m_id, CLICK_ENTER);
}
void on_esc_clicked(int id, int param)
{
memset(m_str, 0, sizeof(m_str));
(m_parent->*(on_click))(m_id, CLICK_ESC);
}
private:
char m_str[32];
int m_str_len;
KEYBOARD_STATUS m_cap_status;
WND_CALLBACK on_click;
};
class c_keyboard_button : public c_button
{
protected:
virtual char convert_char2num(unsigned short id)
{
char num = id;
switch(id)
{
case 'Q':
num = '1';
break;
case 'W':
num = '2';
break;
case 'E':
num = '3';
break;
case 'R':
num = '4';
break;
case 'T':
num = '5';
break;
case 'Y':
num = '6';
break;
case 'U':
num = '7';
break;
case 'I':
num = '8';
break;
case 'O':
num = '9';
break;
case 'P':
num = '0';
break;
default:
break;
}
return num;
}
virtual void on_paint()
{
c_rect rect;
get_screen_rect(rect);
switch (m_status)
{
case STATUS_NORMAL:
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_NORMAL), m_z_order);
break;
case STATUS_FOCUSED:
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_FOCUS), m_z_order);
break;
case STATUS_PUSHED:
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_PUSHED), m_z_order);
m_surface->draw_rect(rect, c_theme::get_color(COLOR_WND_BORDER), 2, m_z_order);
break;
default:
ASSERT(false);
break;
}
if (m_id == 0x14)
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "Caps", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == 0x1B)
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "Esc", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == ' ')
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "Space", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == '\n')
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "Enter", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == '.')
{
return c_word::draw_string_in_rect(m_surface, m_z_order, ".", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == 0x7F)
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "Back", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
else if (m_id == 0x90)
{
return c_word::draw_string_in_rect(m_surface, m_z_order, "?123", rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
char letter[] = { 0, 0 };
if (m_id >= 'A' && m_id <= 'Z')
{
if (((c_keyboard*)m_parent)->get_cap_status() == STATUS_NUMBER)
{
letter[0] = convert_char2num(m_id);
}
else
{
letter[0] = (((c_keyboard*)m_parent)->get_cap_status() == STATUS_UPPERCASE) ? m_id : (m_id + 0x20);
}
}
else if (m_id >= '0' && m_id <= '9')
{
letter[0] = (char)m_id;
}
c_word::draw_string_in_rect(m_surface, m_z_order, letter, rect, m_font, m_font_color, GL_ARGB(0, 0, 0, 0), m_attr);
}
};