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
|
#include <stdlib.h>
#include <string.h>
#include "doomkeys.h"
#include "txt_window_action.h"
#include "txt_io.h"
#include "txt_main.h"
#include "txt_window.h"
static void TXT_WindowActionSizeCalc(TXT_UNCAST_ARG(action))
{
TXT_CAST_ARG(txt_window_action_t, action);
char buf[10];
TXT_GetKeyDescription(action->key, buf);
// Minimum width is the string length + two spaces for padding
action->widget.w = strlen(action->label) + strlen(buf) + 1;
action->widget.h = 1;
}
static void TXT_WindowActionDrawer(TXT_UNCAST_ARG(action), int selected)
{
TXT_CAST_ARG(txt_window_action_t, action);
int i;
char buf[10];
TXT_GetKeyDescription(action->key, buf);
TXT_FGColor(TXT_COLOR_BRIGHT_GREEN);
TXT_DrawString(buf);
TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
TXT_DrawString("=");
TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
TXT_DrawString(action->label);
}
static void TXT_WindowActionDestructor(TXT_UNCAST_ARG(action))
{
TXT_CAST_ARG(txt_window_action_t, action);
free(action->label);
}
static int TXT_WindowActionKeyPress(TXT_UNCAST_ARG(action), int key)
{
TXT_CAST_ARG(txt_window_action_t, action);
if (key == action->key)
{
TXT_EmitSignal(action, "pressed");
return 1;
}
return 0;
}
static void TXT_WindowActionMousePress(TXT_UNCAST_ARG(action),
int x, int y, int b)
{
TXT_CAST_ARG(txt_window_action_t, action);
// Simulate a press of the key
if (b == TXT_MOUSE_LEFT)
{
TXT_WindowActionKeyPress(action, action->key);
}
}
txt_widget_class_t txt_window_action_class =
{
TXT_WindowActionSizeCalc,
TXT_WindowActionDrawer,
TXT_WindowActionKeyPress,
TXT_WindowActionDestructor,
TXT_WindowActionMousePress,
};
txt_window_action_t *TXT_NewWindowAction(int key, char *label)
{
txt_window_action_t *action;
action = malloc(sizeof(txt_window_action_t));
TXT_InitWidget(action, &txt_window_action_class);
action->key = key;
action->label = strdup(label);
return action;
}
static void WindowCloseCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(window))
{
TXT_CAST_ARG(txt_window_t, window);
TXT_CloseWindow(window);
}
static void WindowAcceptCallback(TXT_UNCAST_ARG(widget), TXT_UNCAST_ARG(window))
{
TXT_CAST_ARG(txt_window_t, window);
TXT_WidgetKeyPress(window, KEY_ENTER);
}
txt_window_action_t *TXT_NewWindowEscapeAction(txt_window_t *window)
{
txt_window_action_t *action;
action = TXT_NewWindowAction(KEY_ESCAPE, "Abort");
TXT_SignalConnect(action, "pressed", WindowCloseCallback, window);
return action;
}
txt_window_action_t *TXT_NewWindowAcceptAction(txt_window_t *window)
{
txt_window_action_t *action;
action = TXT_NewWindowAction(KEY_ENTER, "Accept");
TXT_SignalConnect(action, "pressed", WindowAcceptCallback, window);
return action;
}
|