The R Project SVN R

Rev

Rev 23715 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23715 Rev 25313
1
/*
1
/*
2
 * GraphApp - Cross-Platform Graphics Programming Library.
2
 * GraphApp - Cross-Platform Graphics Programming Library.
3
 *
3
 *
4
 * File: dialogs.c -- standard file dialogs and a few others.
4
 * File: dialogs.c -- standard file dialogs and a few others.
5
 * Platform: Windows  Version: 2.42  Date: 1998/07/07
5
 * Platform: Windows  Version: 2.42  Date: 1998/07/07
6
 *
6
 *
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
7
 * Version: 1.00  Changes: Original version by Lachlan Patrick.
8
 * Version: 1.50  Changes: Uses new rectangles, callbacks.
8
 * Version: 1.50  Changes: Uses new rectangles, callbacks.
9
 * Version: 2.20  Changes: Handle null strings correctly now.
9
 * Version: 2.20  Changes: Handle null strings correctly now.
10
 * Version: 2.40  Changes: Now uses YES, NO, CANCEL symbols.
10
 * Version: 2.40  Changes: Now uses YES, NO, CANCEL symbols.
11
 * Version: 2.42  Changes: Askstring fixed by Laurent Piguet.
11
 * Version: 2.42  Changes: Askstring fixed by Laurent Piguet.
12
 */
12
 */
13
 
13
 
14
/* Copyright (C) 1993-1998 Lachlan Patrick
14
/* Copyright (C) 1993-1998 Lachlan Patrick
15
 
15
 
16
   This file is part of GraphApp, a cross-platform C graphics library.
16
   This file is part of GraphApp, a cross-platform C graphics library.
17
 
17
 
18
   GraphApp is free software; you can redistribute it and/or modify it
18
   GraphApp is free software; you can redistribute it and/or modify it
19
   under the terms of the GNU Library General Public License.
19
   under the terms of the GNU Library General Public License.
20
   GraphApp is distributed in the hope that it will be useful, but
20
   GraphApp is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY.
21
   WITHOUT ANY WARRANTY.
22
 
22
 
23
   See the file COPYLIB.TXT for details.
23
   See the file COPYLIB.TXT for details.
24
*/
24
*/
25
 
25
 
26
#include "internal.h"
26
#include "internal.h"
27
 
27
 
28
#define BUFSIZE _MAX_PATH
28
#define BUFSIZE _MAX_PATH
29
static char strbuf[BUFSIZE];
29
static char strbuf[BUFSIZE];
30
 
30
 
31
static char *filter[] = {
31
static char *filter[] = {
32
	"All Files (*.*)",	"*.*",
32
	"All Files (*.*)",	"*.*",
33
	"Text Files (*.TXT)",	"*.txt",
33
	"Text Files (*.TXT)",	"*.txt",
34
	"HTML Files (*.HTM)",	"*.htm",
34
	"HTML Files (*.HTM)",	"*.htm",
35
	"PNG Files (*.PNG)",	"*.png",
35
	"PNG Files (*.PNG)",	"*.png",
36
	"JPEG Files (*.JPG)",	"*.jpg",
36
	"JPEG Files (*.JPG)",	"*.jpg",
37
	"BMP Files (*.BMP)",	"*.bmp",
37
	"BMP Files (*.BMP)",	"*.bmp",
38
	""
38
	""
39
};
39
};
40
 
40
 
41
static char *userfilter;
41
static char *userfilter;
42
void setuserfilter(char *uf) {
42
void setuserfilter(char *uf) {
43
   userfilter=uf;
43
   userfilter=uf;
44
}
44
}
45
 
45
 
46
/*
46
/*
47
 *  Error reporting dialog.
47
 *  Error reporting dialog.
48
 */
48
 */
49
void apperror(char *errstr)
49
void apperror(char *errstr)
50
{
50
{
51
	if (! errstr)
51
	if (! errstr)
52
		errstr = "Unspecified error";
52
		errstr = "Unspecified error";
53
	MessageBox(0, errstr, "Graphics Library Error",
53
	MessageBox(0, errstr, "Graphics Library Error",
54
		MB_TASKMODAL | MB_ICONSTOP | MB_OK);
54
		MB_TASKMODAL | MB_ICONSTOP | MB_OK);
55
	exitapp();
55
	exitapp();
56
}
56
}
57
 
57
 
58
void askok(char *info)
58
void askok(char *info)
59
{
59
{
60
	if (! info)
60
	if (! info)
61
		info = "";
61
		info = "";
62
	MessageBox(0, info, "Information",
62
	MessageBox(0, info, "Information",
63
		MB_TASKMODAL | MB_ICONINFORMATION | MB_OK);
63
		MB_TASKMODAL | MB_ICONINFORMATION | MB_OK);
64
}
64
}
65
 
65
 
66
int askokcancel(char *question)
66
int askokcancel(char *question)
67
{
67
{
68
	int result;
68
	int result;
69
 
69
 
70
	if (! question)
70
	if (! question)
71
		question = "";
71
		question = "";
72
	result = MessageBox(0, question, "Question",
72
	result = MessageBox(0, question, "Question",
73
		MB_TASKMODAL | MB_ICONQUESTION | MB_OKCANCEL);
73
		MB_TASKMODAL | MB_ICONQUESTION | MB_OKCANCEL);
74
 
74
 
75
	switch (result) {
75
	switch (result) {
76
		case IDOK: result = YES; break;
76
		case IDOK: result = YES; break;
77
		case IDCANCEL:
77
		case IDCANCEL:
78
		default: result = CANCEL; break;
78
		default: result = CANCEL; break;
79
	}
79
	}
80
	return result;
80
	return result;
81
}
81
}
82
 
82
 
83
int askyesno(char *question)
83
int askyesno(char *question)
84
{
84
{
85
	int result;
85
	int result;
86
 
86
 
87
	if (! question)
87
	if (! question)
88
		question = "";
88
		question = "";
89
	result = MessageBox(0, question, "Question",
89
	result = MessageBox(0, question, "Question",
90
		MB_TASKMODAL | MB_ICONQUESTION | MB_YESNO);
90
		MB_TASKMODAL | MB_ICONQUESTION | MB_YESNO);
91
 
91
 
92
	switch (result) {
92
	switch (result) {
93
		case IDYES: result = YES; break;
93
		case IDYES: result = YES; break;
94
		case IDNO:  result = NO; break;
94
		case IDNO:  result = NO; break;
95
		default: result = CANCEL; break;
95
		default: result = CANCEL; break;
96
	}
96
	}
97
	return result;
97
	return result;
98
}
98
}
99
 
99
 
100
int askyesnocancel(char *question)
100
int askyesnocancel(char *question)
101
{
101
{
102
	int result;
102
	int result;
103
 
103
 
104
	if (! question)
104
	if (! question)
105
		question = "";
105
		question = "";
106
	result = MessageBox(0, question, "Question",
106
	result = MessageBox(0, question, "Question",
107
		MB_TASKMODAL | MB_ICONQUESTION | MB_YESNOCANCEL);
107
		MB_TASKMODAL | MB_ICONQUESTION | MB_YESNOCANCEL);
108
 
108
 
109
	switch (result) {
109
	switch (result) {
110
		case IDYES: result = YES; break;
110
		case IDYES: result = YES; break;
111
		case IDNO:  result = NO; break;
111
		case IDNO:  result = NO; break;
112
		case IDCANCEL:
112
		case IDCANCEL:
113
		default: result = CANCEL; break;
113
		default: result = CANCEL; break;
114
	}
114
	}
115
	return result;
115
	return result;
116
}
116
}
117
 
117
 
118
static char cod[MAX_PATH]=""; /*current open directory*/
118
static char cod[MAX_PATH]=""; /*current open directory*/
119
 
119
 
120
void askchangedir()
120
void askchangedir()
121
{
121
{
122
    char *s, msg[MAX_PATH + 40];
122
    char *s, msg[MAX_PATH + 40];
123
 
123
 
124
/* if cod has never been used, set it to current directory */
124
/* if cod has never been used, set it to current directory */
125
    if (!cod[0]) GetCurrentDirectory(MAX_PATH, cod);
125
    if (!cod[0]) GetCurrentDirectory(MAX_PATH, cod);
126
    s = askcdstring(" Change working directory to:", cod);
126
    s = askcdstring(" Change working directory to:", cod);
127
    if (s && (SetCurrentDirectory(s) == FALSE)) {
127
    if (s && (SetCurrentDirectory(s) == FALSE)) {
-
 
128
	snprintf(msg, MAX_PATH + 40,
128
	sprintf(msg, "Unable to set '%s' as working directory", s);
129
		 "Unable to set '%s' as working directory", s);
129
	askok(msg);
130
	askok(msg);
130
    }
131
    }
131
    /* in every case reset cod (to new directory if all went ok
132
    /* in every case reset cod (to new directory if all went ok
132
       or to old since user may have edited it */
133
       or to old since user may have edited it */
133
    GetCurrentDirectory(MAX_PATH, cod);
134
    GetCurrentDirectory(MAX_PATH, cod);
134
}
135
}
135
 
136
 
136
char *askfilename(char *title, char *default_name)
137
char *askfilename(char *title, char *default_name)
137
{
138
{
138
	if (*askfilenames(title, default_name, 0, userfilter?userfilter:filter[0], 0,
139
	if (*askfilenames(title, default_name, 0, userfilter?userfilter:filter[0], 0,
139
				          strbuf, BUFSIZE)) return strbuf;
140
				          strbuf, BUFSIZE)) return strbuf;
140
	else return NULL;
141
	else return NULL;
141
}
142
}
142
 
143
 
143
char *askfilenames(char *title, char *default_name, int multi,
144
char *askfilenames(char *title, char *default_name, int multi,
144
			       char *filters, int filterindex, char *strbuf, int bufsize)
145
			       char *filters, int filterindex, char *strbuf, int bufsize)
145
{
146
{
146
	int i;
147
	int i;
147
	OPENFILENAME ofn;
148
	OPENFILENAME ofn;
148
        char cwd[MAX_PATH]="";
149
        char cwd[MAX_PATH]="";
149
	if (! default_name)
150
	if (! default_name)
150
		default_name = "";
151
		default_name = "";
151
	strcpy(strbuf, default_name);
152
	strcpy(strbuf, default_name);
152
        GetCurrentDirectory(MAX_PATH,cwd);
153
        GetCurrentDirectory(MAX_PATH,cwd);
153
        if (!strcmp(cod,"")) strcpy(cod,cwd);
154
        if (!strcmp(cod,"")) strcpy(cod,cwd);
154
 
155
 
155
	ofn.lStructSize     = sizeof(OPENFILENAME);
156
	ofn.lStructSize     = sizeof(OPENFILENAME);
156
	ofn.hwndOwner       = current_window ?
157
	ofn.hwndOwner       = current_window ?
157
				current_window->handle : 0;
158
				current_window->handle : 0;
158
	ofn.hInstance       = 0;
159
	ofn.hInstance       = 0;
159
    ofn.lpstrFilter     = filters;
160
    ofn.lpstrFilter     = filters;
160
	ofn.lpstrCustomFilter = NULL;
161
	ofn.lpstrCustomFilter = NULL;
161
	ofn.nMaxCustFilter  = 0;
162
	ofn.nMaxCustFilter  = 0;
162
	ofn.nFilterIndex    = filterindex;
163
	ofn.nFilterIndex    = filterindex;
163
	ofn.lpstrFile       = strbuf;
164
	ofn.lpstrFile       = strbuf;
164
	ofn.nMaxFile        = bufsize;
165
	ofn.nMaxFile        = bufsize;
165
	ofn.lpstrFileTitle  = NULL;
166
	ofn.lpstrFileTitle  = NULL;
166
	ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
167
	ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
167
        ofn.lpstrInitialDir = cod;
168
        ofn.lpstrInitialDir = cod;
168
	ofn.lpstrTitle      = title;
169
	ofn.lpstrTitle      = title;
169
	ofn.Flags           = OFN_CREATEPROMPT | OFN_HIDEREADONLY | OFN_EXPLORER;
170
	ofn.Flags           = OFN_CREATEPROMPT | OFN_HIDEREADONLY | OFN_EXPLORER;
170
	if (multi) ofn.Flags |= OFN_ALLOWMULTISELECT;
171
	if (multi) ofn.Flags |= OFN_ALLOWMULTISELECT;
171
	ofn.nFileOffset     = 0;
172
	ofn.nFileOffset     = 0;
172
	ofn.nFileExtension  = 0;
173
	ofn.nFileExtension  = 0;
173
	ofn.lpstrDefExt     = "*";
174
	ofn.lpstrDefExt     = "*";
174
	ofn.lCustData       = 0L;
175
	ofn.lCustData       = 0L;
175
	ofn.lpfnHook        = NULL;
176
	ofn.lpfnHook        = NULL;
176
	ofn.lpTemplateName  = NULL;
177
	ofn.lpTemplateName  = NULL;
177
 
178
 
178
	if (GetOpenFileName(&ofn) == 0) {
179
	if (GetOpenFileName(&ofn) == 0) {
179
		GetCurrentDirectory(MAX_PATH,cod);
180
		GetCurrentDirectory(MAX_PATH,cod);
180
		SetCurrentDirectory(cwd);
181
		SetCurrentDirectory(cwd);
181
		strbuf[0] = 0;
182
		strbuf[0] = 0;
182
		strbuf[1] = 0;
183
		strbuf[1] = 0;
183
		return strbuf;
184
		return strbuf;
184
	} else {
185
	} else {
185
		GetCurrentDirectory(MAX_PATH,cod);
186
		GetCurrentDirectory(MAX_PATH,cod);
186
		SetCurrentDirectory(cwd);
187
		SetCurrentDirectory(cwd);
187
		for (i=0; i<10; i++)
188
		for (i=0; i<10; i++)
188
			if (peekevent()) doevent();
189
			if (peekevent()) doevent();
189
		return strbuf;
190
		return strbuf;
190
	}
191
	}
191
}
192
}
192
 
193
 
193
int countFilenames(char *list)
194
int countFilenames(char *list)
194
{
195
{
195
	char *temp;
196
	char *temp;
196
	int count;
197
	int count;
197
	count = 0;
198
	count = 0;
198
	for (temp = list; *temp; temp += strlen(temp)+1) count++;
199
	for (temp = list; *temp; temp += strlen(temp)+1) count++;
199
	return count;
200
	return count;
200
}
201
}
201
 
202
 
202
char *askfilesave(char *title, char *default_name)
203
char *askfilesave(char *title, char *default_name)
203
{
204
{
204
    return askfilesavewithdir(title, default_name, NULL);
205
    return askfilesavewithdir(title, default_name, NULL);
205
}
206
}
206
 
207
 
207
char *askfilesavewithdir(char *title, char *default_name, char *dir)
208
char *askfilesavewithdir(char *title, char *default_name, char *dir)
208
{
209
{
209
	int i;
210
	int i;
210
	OPENFILENAME ofn;
211
	OPENFILENAME ofn;
211
        char cwd[MAX_PATH];
212
        char cwd[MAX_PATH];
212
 
213
 
213
	if (! default_name)
214
	if (! default_name)
214
		default_name = "";
215
		default_name = "";
215
	strcpy(strbuf, default_name);
216
	strcpy(strbuf, default_name);
216
 
217
 
217
	ofn.lStructSize     = sizeof(OPENFILENAME);
218
	ofn.lStructSize     = sizeof(OPENFILENAME);
218
	ofn.hwndOwner       = current_window ?
219
	ofn.hwndOwner       = current_window ?
219
				current_window->handle : 0;
220
				current_window->handle : 0;
220
	ofn.hInstance       = 0;
221
	ofn.hInstance       = 0;
221
        ofn.lpstrFilter     = userfilter?userfilter:filter[0];
222
        ofn.lpstrFilter     = userfilter?userfilter:filter[0];
222
	ofn.lpstrCustomFilter = NULL;
223
	ofn.lpstrCustomFilter = NULL;
223
	ofn.nMaxCustFilter  = 0;
224
	ofn.nMaxCustFilter  = 0;
224
	ofn.nFilterIndex    = 0;
225
	ofn.nFilterIndex    = 0;
225
	ofn.lpstrFile       = strbuf;
226
	ofn.lpstrFile       = strbuf;
226
	ofn.nMaxFile        = BUFSIZE;
227
	ofn.nMaxFile        = BUFSIZE;
227
	ofn.lpstrFileTitle  = NULL;
228
	ofn.lpstrFileTitle  = NULL;
228
	ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
229
	ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
229
	if(dir && strlen(dir) > 0)
230
	if(dir && strlen(dir) > 0)
230
	    ofn.lpstrInitialDir = dir;
231
	    ofn.lpstrInitialDir = dir;
231
	else {
232
	else {
232
	    if (GetCurrentDirectory(MAX_PATH,cwd))
233
	    if (GetCurrentDirectory(MAX_PATH,cwd))
233
		ofn.lpstrInitialDir = cwd;
234
		ofn.lpstrInitialDir = cwd;
234
	    else
235
	    else
235
		ofn.lpstrInitialDir = NULL;
236
		ofn.lpstrInitialDir = NULL;
236
	}
237
	}
237
	ofn.lpstrTitle      = title;
238
	ofn.lpstrTitle      = title;
238
	ofn.Flags           = OFN_OVERWRITEPROMPT |
239
	ofn.Flags           = OFN_OVERWRITEPROMPT |
239
                              OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
240
                              OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
240
	ofn.nFileOffset     = 0;
241
	ofn.nFileOffset     = 0;
241
	ofn.nFileExtension  = 0;
242
	ofn.nFileExtension  = 0;
242
	ofn.lpstrDefExt     = "*";
243
	ofn.lpstrDefExt     = "*";
243
	ofn.lCustData       = 0L;
244
	ofn.lCustData       = 0L;
244
	ofn.lpfnHook        = NULL;
245
	ofn.lpfnHook        = NULL;
245
	ofn.lpTemplateName  = NULL;
246
	ofn.lpTemplateName  = NULL;
246
 
247
 
247
	if (GetSaveFileName(&ofn) == 0)
248
	if (GetSaveFileName(&ofn) == 0)
248
		return NULL;
249
		return NULL;
249
	else {
250
	else {
250
		for (i=0; i<10; i++)
251
		for (i=0; i<10; i++)
251
			if (peekevent()) doevent();
252
			if (peekevent()) doevent();
252
		return strbuf;
253
		return strbuf;
253
	}
254
	}
254
}
255
}
255
 
256
 
256
/*
257
/*
257
 *  Input a string using a dialog box.
258
 *  Input a string using a dialog box.
258
 */
259
 */
259
 
260
 
260
/*
261
/*
261
 *  Dialog information structure:
262
 *  Dialog information structure:
262
 */
263
 */
263
	#define NOT_CHOSEN_YET -2
264
	#define NOT_CHOSEN_YET -2
264
 
265
 
265
	typedef struct dialog_data_class {
266
	typedef struct dialog_data_class {
266
		int 	hit;
267
		int 	hit;
267
		char *	result;
268
		char *	result;
268
		label	question;
269
		label	question;
269
		field	text, pass;
270
		field	text, pass;
270
		button	yes, no, cancel;
271
		button	yes, no, cancel;
271
	} dialog_data;
272
	} dialog_data;
272
 
273
 
273
	#define data(w) ((dialog_data *) (getdata(w)))
274
	#define data(w) ((dialog_data *) (getdata(w)))
274
 
275
 
275
/*
276
/*
276
 *  Some strings to use:
277
 *  Some strings to use:
277
 */
278
 */
278
	static char * OKAY_STRING	= "OK";
279
	static char * OKAY_STRING	= "OK";
279
	static char * CANCEL_STRING	= "Cancel";
280
	static char * CANCEL_STRING	= "Cancel";
280
	static char * BROWSE_STRING	= "Browse";
281
	static char * BROWSE_STRING	= "Browse";
281
 
282
 
282
	static char * QUESTION_TITLE	= "Question";
283
	static char * QUESTION_TITLE	= "Question";
283
	static char * PASSWORD_TITLE	= "Password Entry";
284
	static char * PASSWORD_TITLE	= "Password Entry";
284
	static char * FINDDIR_TITLE	= "Change directory";
285
	static char * FINDDIR_TITLE	= "Change directory";
285
 
286
 
286
static void add_data(window w)
287
static void add_data(window w)
287
{
288
{
288
	dialog_data *d;
289
	dialog_data *d;
289
 
290
 
290
	d = create (dialog_data);
291
	d = create (dialog_data);
291
	if (! d)
292
	if (! d)
292
		return;
293
		return;
293
	d->hit = NOT_CHOSEN_YET;
294
	d->hit = NOT_CHOSEN_YET;
294
 
295
 
295
	setdata(w, d);
296
	setdata(w, d);
296
}
297
}
297
 
298
 
298
static char * get_dialog_string(window w)
299
static char * get_dialog_string(window w)
299
{
300
{
300
	dialog_data *d = data(w);
301
	dialog_data *d = data(w);
301
 
302
 
302
	if (d->hit < YES) /* cancelled */
303
	if (d->hit < YES) /* cancelled */
303
		return NULL;
304
		return NULL;
304
	del_string(d->result);
305
	del_string(d->result);
305
	if (d->text)	/* question dialog */
306
	if (d->text)	/* question dialog */
306
		d->result = new_string(gettext(d->text));
307
		d->result = new_string(gettext(d->text));
307
 
308
 
308
	return d->result;
309
	return d->result;
309
}
310
}
310
 
311
 
311
static void hit_button(control c)
312
static void hit_button(control c)
312
{
313
{
313
	window w = parentwindow(c);
314
	window w = parentwindow(c);
314
	dialog_data *d = data(w);
315
	dialog_data *d = data(w);
315
	int value = getvalue(c);
316
	int value = getvalue(c);
316
 
317
 
317
	d->hit = value;
318
	d->hit = value;
318
	hide(w);
319
	hide(w);
319
}
320
}
320
 
321
 
321
#ifndef OLD
322
#ifndef OLD
322
extern void selectfolder(char *); /* from ../shext.c */
323
extern void selectfolder(char *); /* from ../shext.c */
323
 
324
 
324
static void browse_button(control c)
325
static void browse_button(control c)
325
{
326
{
326
    window w = parentwindow(c);
327
    window w = parentwindow(c);
327
    dialog_data *d = data(w);
328
    dialog_data *d = data(w);
328
    char strbuf[MAX_PATH];
329
    char strbuf[MAX_PATH];
329
    strcpy(strbuf, gettext(d->text));
330
    strcpy(strbuf, gettext(d->text));
330
    selectfolder(strbuf);
331
    selectfolder(strbuf);
331
    if(strlen(strbuf)) settext(d->text, strbuf);
332
    if(strlen(strbuf)) settext(d->text, strbuf);
332
}
333
}
333
#else
334
#else
334
static void browse_button(control c)
335
static void browse_button(control c)
335
{
336
{
336
    window w = parentwindow(c);
337
    window w = parentwindow(c);
337
    dialog_data *d = data(w);
338
    dialog_data *d = data(w);
338
 
339
 
339
    OPENFILENAME ofn;
340
    OPENFILENAME ofn;
340
    char strbuf[_MAX_PATH]="anything", *p;
341
    char strbuf[_MAX_PATH]="anything", *p;
341
 
342
 
342
    ofn.lStructSize     = sizeof(OPENFILENAME);
343
    ofn.lStructSize     = sizeof(OPENFILENAME);
343
    ofn.hwndOwner       = 0;
344
    ofn.hwndOwner       = 0;
344
    ofn.hInstance       = 0;
345
    ofn.hInstance       = 0;
345
    ofn.lpstrFilter     = "All files (*.*)\0*.*\0\0";
346
    ofn.lpstrFilter     = "All files (*.*)\0*.*\0\0";
346
    ofn.lpstrCustomFilter = NULL;
347
    ofn.lpstrCustomFilter = NULL;
347
    ofn.nMaxCustFilter  = 0;
348
    ofn.nMaxCustFilter  = 0;
348
    ofn.nFilterIndex    = 0;
349
    ofn.nFilterIndex    = 0;
349
    ofn.lpstrFile       = strbuf;
350
    ofn.lpstrFile       = strbuf;
350
    ofn.nMaxFile        = _MAX_PATH;
351
    ofn.nMaxFile        = _MAX_PATH;
351
    ofn.lpstrFileTitle  = NULL;
352
    ofn.lpstrFileTitle  = NULL;
352
    ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
353
    ofn.nMaxFileTitle   = _MAX_FNAME + _MAX_EXT;
353
    ofn.lpstrInitialDir = gettext(d->text);
354
    ofn.lpstrInitialDir = gettext(d->text);
354
    ofn.lpstrTitle      = "Select working directory";
355
    ofn.lpstrTitle      = "Select working directory";
355
    ofn.Flags           = OFN_HIDEREADONLY;
356
    ofn.Flags           = OFN_HIDEREADONLY;
356
    ofn.nFileOffset     = 0;
357
    ofn.nFileOffset     = 0;
357
    ofn.nFileExtension  = 0;
358
    ofn.nFileExtension  = 0;
358
    ofn.lpstrDefExt     = "";
359
    ofn.lpstrDefExt     = "";
359
    ofn.lCustData       = 0L;
360
    ofn.lCustData       = 0L;
360
    ofn.lpfnHook        = NULL;
361
    ofn.lpfnHook        = NULL;
361
    ofn.lpTemplateName  = NULL;
362
    ofn.lpTemplateName  = NULL;
362
 
363
 
363
    if(GetSaveFileName(&ofn) && strlen(strbuf)) {
364
    if(GetSaveFileName(&ofn) && strlen(strbuf)) {
364
	 p = strrchr(strbuf,'\\'); if(p) *p ='\0';
365
	 p = strrchr(strbuf,'\\'); if(p) *p ='\0';
365
	 settext(d->text, strbuf);
366
	 settext(d->text, strbuf);
366
    }
367
    }
367
}
368
}
368
#endif
369
#endif
369
 
370
 
370
static void hit_key(window w, int key)
371
static void hit_key(window w, int key)
371
{
372
{
372
	button btn;
373
	button btn;
373
	char *name = NULL;
374
	char *name = NULL;
374
 
375
 
375
	w = parentwindow(w);
376
	w = parentwindow(w);
376
 
377
 
377
	if (data(w) == NULL)
378
	if (data(w) == NULL)
378
		return;
379
		return;
379
 
380
 
380
	if ((btn = data(w)->yes) != NULL) {
381
	if ((btn = data(w)->yes) != NULL) {
381
		name = getname(btn);
382
		name = getname(btn);
382
		if ((key == '\n') || (tolower(name[0]) == tolower(key)))
383
		if ((key == '\n') || (tolower(name[0]) == tolower(key)))
383
		{
384
		{
384
			flashcontrol(btn);
385
			flashcontrol(btn);
385
			activatecontrol(btn);
386
			activatecontrol(btn);
386
			return;
387
			return;
387
		}
388
		}
388
	}
389
	}
389
 
390
 
390
	if ((btn = data(w)->cancel) != NULL) {
391
	if ((btn = data(w)->cancel) != NULL) {
391
		name = getname(btn);
392
		name = getname(btn);
392
		if ((key == ESC) || (tolower(name[0]) == tolower(key)))
393
		if ((key == ESC) || (tolower(name[0]) == tolower(key)))
393
		{
394
		{
394
			flashcontrol(btn);
395
			flashcontrol(btn);
395
			activatecontrol(btn);
396
			activatecontrol(btn);
396
			return;
397
			return;
397
		}
398
		}
398
	}
399
	}
399
 
400
 
400
	if ((btn = data(w)->no) != NULL) {
401
	if ((btn = data(w)->no) != NULL) {
401
		name = getname(btn);
402
		name = getname(btn);
402
		if ((key == ESC) || (tolower(name[0]) == tolower(key)))
403
		if ((key == ESC) || (tolower(name[0]) == tolower(key)))
403
		{
404
		{
404
			flashcontrol(btn);
405
			flashcontrol(btn);
405
			activatecontrol(btn);
406
			activatecontrol(btn);
406
			return;
407
			return;
407
		}
408
		}
408
	}
409
	}
409
 
410
 
410
}
411
}
411
 
412
 
412
/*
413
/*
413
 *  Handle the events from a message dialog, hide the window afterwards.
414
 *  Handle the events from a message dialog, hide the window afterwards.
414
 */
415
 */
415
static int handle_message_dialog(window w)
416
static int handle_message_dialog(window w)
416
{
417
{
417
	window old;
418
	window old;
418
	dialog_data *d = data(w);
419
	dialog_data *d = data(w);
419
 
420
 
420
	old = currentdrawing();
421
	old = currentdrawing();
421
	d->hit = NOT_CHOSEN_YET;
422
	d->hit = NOT_CHOSEN_YET;
422
 
423
 
423
	show(w);
424
	show(w);
424
	while (d->hit == NOT_CHOSEN_YET)
425
	while (d->hit == NOT_CHOSEN_YET)
425
		doevent();
426
		doevent();
426
	hide(w);
427
	hide(w);
427
 
428
 
428
	if (old) drawto(old);
429
	if (old) drawto(old);
429
 
430
 
430
	return d->hit;
431
	return d->hit;
431
}
432
}
432
 
433
 
433
static window init_askstr_dialog(char *title, char *question,
434
static window init_askstr_dialog(char *title, char *question,
434
				 char *default_str)
435
				 char *default_str)
435
{
436
{
436
	window win;
437
	window win;
437
	dialog_data *d;
438
	dialog_data *d;
438
	int tw, bw, h, middle;
439
	int tw, bw, h, middle;
439
 
440
 
440
	if (! question)
441
	if (! question)
441
		question= "";
442
		question= "";
442
	if (! default_str)
443
	if (! default_str)
443
		default_str = "";
444
		default_str = "";
444
 
445
 
445
	tw = strwidth(SystemFont, CANCEL_STRING) * 8;
446
	tw = strwidth(SystemFont, CANCEL_STRING) * 8;
446
	h = getheight(SystemFont);
447
	h = getheight(SystemFont);
447
 
448
 
448
	if (tw < 150) tw = 150;
449
	if (tw < 150) tw = 150;
449
 
450
 
450
	win = newwindow(title, rect(0,0,tw+30,h*9+12),
451
	win = newwindow(title, rect(0,0,tw+30,h*9+12),
451
			Titlebar | Centered | Modal);
452
			Titlebar | Centered | Modal);
452
        setbackground(win, dialog_bg());
453
        setbackground(win, dialog_bg());
453
	add_data(win);
454
	add_data(win);
454
	d = data(win);
455
	d = data(win);
455
	d->question = newlabel(question, rect(10,h,tw+4,h*2+2),
456
	d->question = newlabel(question, rect(10,h,tw+4,h*2+2),
456
			AlignLeft);
457
			AlignLeft);
457
	if (title == FINDDIR_TITLE) {
458
	if (title == FINDDIR_TITLE) {
458
	    bw = strwidth(SystemFont, BROWSE_STRING) * 3/2;
459
	    bw = strwidth(SystemFont, BROWSE_STRING) * 3/2;
459
	    d->text = newfield(default_str, rect(10,h*4,tw+4-bw,h*3/2));
460
	    d->text = newfield(default_str, rect(10,h*4,tw+4-bw,h*3/2));
460
	    newbutton(BROWSE_STRING, rect(20+tw-bw, h*4-2, bw, h+10),
461
	    newbutton(BROWSE_STRING, rect(20+tw-bw, h*4-2, bw, h+10),
461
		      browse_button);
462
		      browse_button);
462
	}
463
	}
463
	else if (title == PASSWORD_TITLE)
464
	else if (title == PASSWORD_TITLE)
464
		d->text = newpassword(default_str, rect(10,h*4,tw+4,h*3/2));
465
		d->text = newpassword(default_str, rect(10,h*4,tw+4,h*3/2));
465
	else
466
	else
466
		d->text = newfield(default_str, rect(10,h*4,tw+4,h*3/2));
467
		d->text = newfield(default_str, rect(10,h*4,tw+4,h*3/2));
467
 
468
 
468
	middle = (tw+30)/2;
469
	middle = (tw+30)/2;
469
	bw = strwidth(SystemFont, CANCEL_STRING) * 3/2;
470
	bw = strwidth(SystemFont, CANCEL_STRING) * 3/2;
470
 
471
 
471
	d->yes = newbutton(OKAY_STRING,
472
	d->yes = newbutton(OKAY_STRING,
472
			rect(middle-bw-10, h*7, bw, h+10), hit_button);
473
			rect(middle-bw-10, h*7, bw, h+10), hit_button);
473
	setvalue(d->yes, YES);
474
	setvalue(d->yes, YES);
474
 
475
 
475
	d->cancel = newbutton(CANCEL_STRING,
476
	d->cancel = newbutton(CANCEL_STRING,
476
			rect(middle+10, h*7, bw, h+10), hit_button);
477
			rect(middle+10, h*7, bw, h+10), hit_button);
477
	setvalue(d->cancel, CANCEL);
478
	setvalue(d->cancel, CANCEL);
478
 
479
 
479
	setkeydown(win, hit_key);
480
	setkeydown(win, hit_key);
480
 
481
 
481
	return win;
482
	return win;
482
}
483
}
483
 
484
 
484
char *askstring(char *question, char *default_str)
485
char *askstring(char *question, char *default_str)
485
{
486
{
486
	static window win = NULL;
487
	static window win = NULL;
487
	window prev = current_window;
488
	window prev = current_window;
488
 
489
 
489
	if (! win)
490
	if (! win)
490
		win = init_askstr_dialog(QUESTION_TITLE, question, default_str);
491
		win = init_askstr_dialog(QUESTION_TITLE, question, default_str);
491
	else {
492
	else {
492
		settext(data(win)->question, question);
493
		settext(data(win)->question, question);
493
		settext(data(win)->text, default_str);
494
		settext(data(win)->text, default_str);
494
	}
495
	}
495
	handle_message_dialog(win);
496
	handle_message_dialog(win);
496
	current_window = prev;
497
	current_window = prev;
497
	return get_dialog_string(win);
498
	return get_dialog_string(win);
498
}
499
}
499
 
500
 
500
char *askcdstring(char *question, char *default_str)
501
char *askcdstring(char *question, char *default_str)
501
{
502
{
502
	static window win = NULL;
503
	static window win = NULL;
503
	window prev = current_window;
504
	window prev = current_window;
504
 
505
 
505
	if (! win)
506
	if (! win)
506
		win = init_askstr_dialog(FINDDIR_TITLE, question, default_str);
507
		win = init_askstr_dialog(FINDDIR_TITLE, question, default_str);
507
	else {
508
	else {
508
		settext(data(win)->question, question);
509
		settext(data(win)->question, question);
509
		settext(data(win)->text, default_str);
510
		settext(data(win)->text, default_str);
510
	}
511
	}
511
	handle_message_dialog(win);
512
	handle_message_dialog(win);
512
	current_window = prev;
513
	current_window = prev;
513
	return get_dialog_string(win);
514
	return get_dialog_string(win);
514
}
515
}
515
 
516
 
516
char *askpassword(char *question, char *default_str)
517
char *askpassword(char *question, char *default_str)
517
{
518
{
518
	static window win = NULL;
519
	static window win = NULL;
519
	window prev = current_window;
520
	window prev = current_window;
520
 
521
 
521
	if (! win)
522
	if (! win)
522
		win = init_askstr_dialog(PASSWORD_TITLE, question, default_str);
523
		win = init_askstr_dialog(PASSWORD_TITLE, question, default_str);
523
	else {
524
	else {
524
		settext(data(win)->question, question);
525
		settext(data(win)->question, question);
525
		settext(data(win)->text, default_str);
526
		settext(data(win)->text, default_str);
526
	}
527
	}
527
	handle_message_dialog(win);
528
	handle_message_dialog(win);
528
	current_window = prev;
529
	current_window = prev;
529
	return get_dialog_string(win);
530
	return get_dialog_string(win);
530
}
531
}
531
 
532
 
532
char *askUserPass(char *title)
533
char *askUserPass(char *title)
533
{
534
{
534
    static window win = NULL;
535
    static window win = NULL;
535
    dialog_data *d;
536
    dialog_data *d;
536
    window prev = current_window;
537
    window prev = current_window;
537
 
538
 
538
    if (! win) {
539
    if (! win) {
539
	int tw, bw, h, middle;
540
	int tw, bw, h, middle;
540
 
541
 
541
	tw = strwidth(SystemFont, CANCEL_STRING) * 8;
542
	tw = strwidth(SystemFont, CANCEL_STRING) * 8;
542
	h = getheight(SystemFont);
543
	h = getheight(SystemFont);
543
	if (tw < 150) tw = 150;
544
	if (tw < 150) tw = 150;
544
	win = newwindow(title, rect(0, 0, tw+30, h*9+12),
545
	win = newwindow(title, rect(0, 0, tw+30, h*9+12),
545
			Titlebar | Centered | Modal);
546
			Titlebar | Centered | Modal);
546
        setbackground(win, dialog_bg());
547
        setbackground(win, dialog_bg());
547
	add_data(win);
548
	add_data(win);
548
	d = data(win);
549
	d = data(win);
549
	d->question = newlabel("User", rect(10, h, tw+4, h*2+2), AlignLeft);
550
	d->question = newlabel("User", rect(10, h, tw+4, h*2+2), AlignLeft);
550
	bw = strwidth(SystemFont, "Password");
551
	bw = strwidth(SystemFont, "Password");
551
	d->text = newfield("", rect(20+bw, h, tw-6-bw, h*3/2));
552
	d->text = newfield("", rect(20+bw, h, tw-6-bw, h*3/2));
552
	newlabel("Password", rect(10, h*4, tw+4, h*2+2), AlignLeft);
553
	newlabel("Password", rect(10, h*4, tw+4, h*2+2), AlignLeft);
553
	d->pass = newpassword("", rect(20+bw, h*4, tw-6-bw, h*3/2));
554
	d->pass = newpassword("", rect(20+bw, h*4, tw-6-bw, h*3/2));
554
	middle = (tw+30)/2;
555
	middle = (tw+30)/2;
555
	bw = strwidth(SystemFont, CANCEL_STRING) * 3/2;
556
	bw = strwidth(SystemFont, CANCEL_STRING) * 3/2;
556
 
557
 
557
	d->yes = newbutton(OKAY_STRING,
558
	d->yes = newbutton(OKAY_STRING,
558
			rect(middle-bw-10, h*7, bw, h+10), hit_button);
559
			rect(middle-bw-10, h*7, bw, h+10), hit_button);
559
	setvalue(d->yes, YES);
560
	setvalue(d->yes, YES);
560
 
561
 
561
	d->cancel = newbutton(CANCEL_STRING,
562
	d->cancel = newbutton(CANCEL_STRING,
562
			rect(middle+10, h*7, bw, h+10), hit_button);
563
			rect(middle+10, h*7, bw, h+10), hit_button);
563
	setvalue(d->cancel, CANCEL);
564
	setvalue(d->cancel, CANCEL);
564
 
565
 
565
	setkeydown(win, hit_key);
566
	setkeydown(win, hit_key);
566
    } else {
567
    } else {
567
	d = data(win);
568
	d = data(win);
568
	settext(d->text, "");
569
	settext(d->text, "");
569
	settext(d->pass, "");
570
	settext(d->pass, "");
570
    }
571
    }
571
    handle_message_dialog(win);
572
    handle_message_dialog(win);
572
    current_window = prev;
573
    current_window = prev;
573
    {
574
    {
574
	char *user, *pass;
575
	char *user, *pass;
575
	static char buf[1000];
576
	static char buf[1000];
576
	if (d->hit < YES) /* cancelled */ return "";
577
	if (d->hit < YES) /* cancelled */ return "";
577
	if (d->text) user = new_string(gettext(d->text));
578
	if (d->text) user = new_string(gettext(d->text));
578
	else return "";
579
	else return "";
579
	if (d->pass) pass = new_string(gettext(d->pass));
580
	if (d->pass) pass = new_string(gettext(d->pass));
580
	else return "";
581
	else return "";
581
	sprintf(buf, "%s:%s", user, pass);
582
	snprintf(buf, 1000, "%s:%s", user, pass);
582
	return buf;
583
	return buf;
583
    }
584
    }
584
    return ""; /* -Wall */
585
    return ""; /* -Wall */
585
}
586
}