forked from tecknicaltom/HT-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtiobox.cc
269 lines (240 loc) · 6.17 KB
/
htiobox.cc
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
/*
* HT Editor
* htiobox.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "atom.h"
#include "htctrl.h"
#include "htdialog.h"
#include "hthist.h"
#include "htiobox.h"
#include "snprintf.h"
#include "tools.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
int imsgbox(Bounds *b, int buttonmask, const char *title, bool modal, statictext_align align, char *buf)
{
ht_dialog *dialog=new ht_dialog();
dialog->init(b, title, FS_KILLER | FS_TITLE | (modal ? 0 : FS_MOVE | FS_RESIZE));
Bounds c;
c.x=1;
c.y=0;
c.w=b->w-4;
c.h=b->h-4;
ht_statictext *text=new ht_statictext();
text->init(&c, buf, align);
text->growmode = MK_GM(GMH_FIT, GMV_FIT);
dialog->insert(text);
int buttons=0;
if (buttonmask & btmask_ok) buttons++; else
if (buttonmask & btmask_yes) buttons++;
if (buttonmask & btmask_no) buttons++; else
if (buttonmask & btmask_skip) buttons++;
if (buttonmask & btmask_cancel) buttons++;
if (buttonmask & btmask_all) buttons++;
if (buttonmask & btmask_none) buttons++;
int pos=0;
if (buttonmask & btmask_ok) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "O~K", button_ok);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
} else if (buttonmask & btmask_yes) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~Yes", button_yes);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
}
if (buttonmask & btmask_no) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~No", button_no);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
} else if (buttonmask & btmask_skip) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~Skip", button_skip);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
}
if (buttonmask & btmask_all) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~All", button_all);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
}
if (buttonmask & btmask_none) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~None", button_none);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
}
if (buttonmask & btmask_cancel) {
c.x=b->w/2-5*(buttons-2*pos);
c.y=b->h-4;
c.w=9;
c.h=2;
ht_button *button=new ht_button();
button->init(&c, "~Cancel", button_cancel);
button->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
dialog->insert(button);
pos++;
}
if (!buttons) {
Bounds x;
text->getbounds(&x);
x.h+=2;
text->setbounds(&x);
}
int r=dialog->run(modal);
dialog->done();
delete dialog;
return r;
}
int msgbox(int buttonmask, const char *title, bool modal, statictext_align align, const char *format, ...)
{
char buf[1024];
va_list arg;
va_start(arg, format);
ht_vsnprintf(buf, 1024, format, arg);
va_end(arg);
char *s=buf;
int ns=1;
while ((s=strchr(s, '\n'))) {
s++;
ns++;
}
int strl=strlen(buf);
Bounds b;
app->getbounds(&b);
b.w=55;
b.h=MAX(strl/(b.w-4), ns)+6;
b.x=(screen->w - b.w)/2;
b.y=(screen->h - b.h)/2;
return imsgbox(&b, buttonmask, title, modal, align, buf);
}
int msgboxrect(Bounds *b, int buttonmask, const char *title, bool modal, statictext_align align, const char *format, ...)
{
char buf[1024];
va_list arg;
va_start(arg, format);
ht_vsnprintf(buf, 1024, format, arg);
va_end(arg);
return imsgbox(b, buttonmask, title, modal, align, buf);
}
bool inputbox(const char *title, const char *label, char *result, int limit, uint32 histid)
{
Bounds b;
app->getbounds(&b);
b.x = (b.w - 60) / 2,
b.y = (b.h - 8) / 2;
b.w = 60;
b.h = 8;
return inputboxrect(&b, title, label, result, limit, histid);
}
bool inputboxrect(Bounds *b, const char *title, const char *label, char *result, int limit, uint32 histid)
{
ht_dialog *dialog=new ht_dialog();
dialog->init(b, title, FS_KILLER | FS_TITLE | FS_MOVE | FS_RESIZE);
ht_strinputfield *input;
Bounds b2;
b2.x = 3 + strlen(label);
b2.y = 1;
b2.w = b->w - 3 - b2.x;
b2.h = 1;
List *hist = NULL;
if (histid) hist = (List*)getAtomValue(histid);
input = new ht_strinputfield();
input->init(&b2, limit, hist);
ht_inputfield_data d;
d.text = (byte *)result;
d.textlen = strlen((char*)d.text);
input->databuf_set(&d, sizeof d);
dialog->insert(input);
if (label) {
b2.x = 1;
b2.y = 1;
b2.w = 3 + strlen(label) - b2.x;
b2.h = 1;
ht_label *lab = new ht_label();
lab->init(&b2, label, input);
dialog->insert(lab);
}
b2.x = b->w - 25;
b2.y = b->h - 5;
b2.w = 10;
b2.h = 2;
ht_button *bok = new ht_button();
bok->init(&b2, "O~k", button_ok);
dialog->insert(bok);
b2.x += 12;
ht_button *bcancel = new ht_button();
bcancel->init(&b2, "~Cancel", button_cancel);
dialog->insert(bcancel);
if (dialog->run(0)) {
int dsize = input->datasize();
ht_inputfield_data *data = ht_malloc(dsize);
ViewDataBuf vdb(input, data, dsize);
bin2str(result, data->text, data->textlen);
free(data);
if (hist) insert_history_entry(hist, result, 0);
dialog->done();
delete dialog;
return true;
}
dialog->done();
delete dialog;
return false;
}
void get_std_progress_indicator_metrics(Bounds *b)
{
app->getbounds(b);
b->w=b->w*2/3;
b->h=6;
b->x=(screen->w - b->w)/2;
b->y=(screen->h - b->h)/2;
}