-
Notifications
You must be signed in to change notification settings - Fork 15
/
msgbox.c
211 lines (196 loc) · 5.91 KB
/
msgbox.c
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
/* ------------------ msgbox.c ------------------ */
#include "dflat.h"
extern DBOX MsgBox;
extern DBOX InputBoxDB;
WINDOW CancelWnd;
static int ReturnValue;
int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
GetClass(wnd) = MESSAGEBOX;
InitWindowColors(wnd);
ClearAttribute(wnd, CONTROLBOX);
break;
case KEYBOARD:
if (p1 == '\r' || p1 == ESC)
ReturnValue = (int)p1;
break;
default:
break;
}
return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
}
int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
GetClass(wnd) = MESSAGEBOX;
InitWindowColors(wnd);
ClearAttribute(wnd, CONTROLBOX);
break;
case KEYBOARD: {
int c = tolower((int)p1);
if (c == 'y')
SendMessage(wnd, COMMAND, ID_OK, 0);
else if (c == 'n')
SendMessage(wnd, COMMAND, ID_CANCEL, 0);
break;
}
default:
break;
}
return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
}
int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
GetClass(wnd) = ERRORBOX;
InitWindowColors(wnd);
break;
case KEYBOARD:
if (p1 == '\r' || p1 == ESC)
ReturnValue = (int)p1;
break;
default:
break;
}
return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
}
int CancelBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
switch (msg) {
case CREATE_WINDOW:
CancelWnd = wnd;
SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
break;
case COMMAND:
if ((int) p1 == ID_CANCEL && (int) p2 == 0)
SendMessage(GetParent(wnd), msg, p1, p2);
return TRUE;
case CLOSE_WINDOW:
CancelWnd = NULL;
SendMessage(wnd, RELEASE_MOUSE, 0, 0);
SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
p1 = TRUE;
break;
default:
break;
}
return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
}
void CloseCancelBox(void)
{
if (CancelWnd != NULL)
SendMessage(CancelWnd, CLOSE_WINDOW, 0, 0);
}
static char *InputText;
static int TextLength;
int InputBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
int rtn;
switch (msg) {
case CREATE_WINDOW:
rtn = DefaultWndProc(wnd, msg, p1, p2);
SendMessage(ControlWindow(&InputBoxDB,ID_INPUTTEXT),
SETTEXTLENGTH, TextLength, 0);
SendMessage(ControlWindow(&InputBoxDB,ID_INPUTTEXT),
ADDTEXT, (PARAM) InputText, 0);
return rtn;
case COMMAND:
if ((int) p1 == ID_OK && (int) p2 == 0)
GetItemText(wnd, ID_INPUTTEXT,
InputText, TextLength);
break;
default:
break;
}
return DefaultWndProc(wnd, msg, p1, p2);
}
BOOL InputBox(WINDOW wnd,char *ttl,char *msg,char *text,int len,int wd)
{
int ln = wd ? wd : len;
ln = min(SCREENWIDTH-8, ln);
InputText = text;
TextLength = len;
InputBoxDB.dwnd.title = ttl;
InputBoxDB.dwnd.w = 4 + max(20, max(ln, max(strlen(ttl), strlen(msg))));
InputBoxDB.ctl[1].dwnd.x = (InputBoxDB.dwnd.w-2-ln)/2;
InputBoxDB.ctl[0].dwnd.w = strlen(msg);
InputBoxDB.ctl[0].itext = msg;
InputBoxDB.ctl[1].itext = NULL;
InputBoxDB.ctl[1].dwnd.w = ln;
InputBoxDB.ctl[2].dwnd.x = (InputBoxDB.dwnd.w - 20) / 2;
InputBoxDB.ctl[3].dwnd.x = InputBoxDB.ctl[2].dwnd.x + 10;
InputBoxDB.ctl[2].isetting = ON;
InputBoxDB.ctl[3].isetting = ON;
return DialogBox(wnd, &InputBoxDB, TRUE, InputBoxProc);
}
BOOL GenericMessage(WINDOW wnd,char *ttl,char *msg,int buttonct,
int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
char *b1, char *b2, int c1, int c2, int isModal)
{
BOOL rtn;
MsgBox.dwnd.title = ttl;
MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
MsgBox.ctl[0].dwnd.w = max(max(MsgWidth(msg),
buttonct*8 + buttonct + 2), strlen(ttl)+2);
MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
if (buttonct == 1)
MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
else {
MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
MsgBox.ctl[2].Class = BUTTON;
}
MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
MsgBox.ctl[0].itext = msg;
MsgBox.ctl[1].itext = b1;
MsgBox.ctl[2].itext = b2;
MsgBox.ctl[1].command = c1;
MsgBox.ctl[2].command = c2;
MsgBox.ctl[1].isetting = ON;
MsgBox.ctl[2].isetting = ON;
rtn = DialogBox(wnd, &MsgBox, isModal, wndproc);
MsgBox.ctl[2].Class = 0;
return rtn;
}
WINDOW MomentaryMessage(char *msg)
{
WINDOW wnd = CreateWindow(
TEXTBOX,
NULL,
-1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
NULL,NULL,NULL,
HASBORDER | SHADOW | SAVESELF);
SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
if (cfg.mono == 0) {
WindowClientColor(wnd, WHITE, GREEN);
WindowFrameColor(wnd, WHITE, GREEN);
}
SendMessage(wnd, SHOW_WINDOW, 0, 0);
return wnd;
}
int MsgHeight(char *msg)
{
int h = 1;
while ((msg = strchr(msg, '\n')) != NULL) {
h++;
msg++;
}
return min(h, SCREENHEIGHT-10);
}
int MsgWidth(char *msg)
{
int w = 0;
char *cp = msg;
while ((cp = strchr(msg, '\n')) != NULL) {
w = max(w, (int) (cp-msg));
msg = cp+1;
}
return min(max(strlen(msg),w), SCREENWIDTH-10);
}