428
|
1 /* Implements elisp-programmable dialog boxes -- MS Windows interface.
|
|
2 Copyright (C) 1998 Kirill M. Katsnelson <kkm@kis.ru>
|
442
|
3 Copyright (C) 2000 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Author:
|
|
25 Initially written by kkm, May 1998
|
|
26 */
|
|
27
|
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
|
31 #include "buffer.h"
|
|
32 #include "console-msw.h"
|
|
33 #include "frame.h"
|
|
34 #include "gui.h"
|
|
35 #include "opaque.h"
|
|
36
|
442
|
37 #include <cderr.h>
|
|
38 #include <commdlg.h>
|
|
39
|
|
40 Lisp_Object Qdialog_box_error;
|
|
41
|
|
42 static Lisp_Object Q_initial_directory;
|
|
43 static Lisp_Object Q_initial_filename;
|
|
44 static Lisp_Object Q_filter_list;
|
|
45 static Lisp_Object Q_allow_multi_select;
|
|
46 static Lisp_Object Q_create_prompt_on_nonexistent;
|
|
47 static Lisp_Object Q_overwrite_prompt;
|
|
48 static Lisp_Object Q_file_must_exist;
|
|
49 static Lisp_Object Q_no_network_button;
|
|
50 static Lisp_Object Q_no_read_only_return;
|
|
51
|
428
|
52 /* List containing all dialog data structures of currently popped up
|
442
|
53 dialogs. */
|
428
|
54 static Lisp_Object Vdialog_data_list;
|
|
55
|
442
|
56 /* List of popup frames wanting keyboard traversal handled */
|
|
57 static Lisp_Object Vpopup_frame_list;
|
|
58
|
|
59 Lisp_Object Vdefault_file_dialog_filter_alist;
|
|
60
|
428
|
61 /* DLUs per character metrics */
|
|
62 #define X_DLU_PER_CHAR 4
|
|
63 #define Y_DLU_PER_CHAR 8
|
|
64
|
|
65 /*
|
|
66 Button metrics
|
|
67 --------------
|
|
68 All buttons have height of 15 DLU. The minimum width for a button is 32 DLU,
|
|
69 but it can be expanded to accommodate its text, so the width is calculated as
|
|
70 8 DLU per button plus 4 DLU per character.
|
|
71 max (32, 6 * text_length). The factor of six is rather empirical, but it
|
|
72 works better than 8 which comes from the definition of a DLU. Buttons are
|
|
73 spaced with 6 DLU gap. Minimum distance from the button to the left or right
|
|
74 dialog edges is 6 DLU, and the distance between the dialog bottom edge and
|
|
75 buttons is 7 DLU.
|
|
76 */
|
|
77
|
|
78 #define X_MIN_BUTTON 32
|
|
79 #define X_BUTTON_MARGIN 8
|
|
80 #define Y_BUTTON 15
|
|
81 #define X_BUTTON_SPACING 6
|
|
82 #define X_BUTTON_FROM_EDGE 6
|
|
83 #define Y_BUTTON_FROM_EDGE 7
|
|
84
|
|
85 /*
|
|
86 Text field metrics
|
|
87 ------------------
|
|
88 Text distance from left and right edges is the same as for buttons, and the
|
|
89 top margin is 11 DLU. The static control has height of 2 DLU per control
|
|
90 plus 8 DLU per each line of text. Distance between the bottom edge of the
|
|
91 control and the button row is 15 DLU. Minimum width of the static control
|
|
92 is 100 DLU, thus giving minimum dialog weight of 112 DLU. Maximum width is
|
|
93 300 DLU, and, if the text is wider than that, the text is wrapped on the
|
|
94 next line. Each character in the text is considered 4 DLU wide.
|
|
95 */
|
|
96
|
|
97 #define X_MIN_TEXT 100
|
|
98 #define X_AVE_TEXT 200
|
|
99 #define X_MAX_TEXT 300
|
|
100 #define X_TEXT_FROM_EDGE X_BUTTON_FROM_EDGE
|
|
101 #define Y_TEXT_FROM_EDGE 11
|
|
102 #define Y_TEXT_MARGIN 2
|
|
103 #define Y_TEXT_FROM_BUTTON 15
|
|
104
|
|
105 #define X_MIN_TEXT_CHAR (X_MIN_TEXT / X_DLU_PER_CHAR)
|
|
106 #define X_AVE_TEXT_CHAR (X_AVE_TEXT / X_DLU_PER_CHAR)
|
|
107 #define X_MAX_TEXT_CHAR (X_MAX_TEXT / X_DLU_PER_CHAR)
|
|
108
|
|
109 /*
|
|
110 Layout algorithm
|
|
111 ----------------
|
|
112 First we calculate the minimum width of the button row, excluding "from
|
|
113 edge" distances. Note that the static control text can be narrower than
|
|
114 X_AVE_TEXT only if both text and button row are narrower than that (so,
|
|
115 even if text *can* be wrapped into 2 rows narrower than ave width, it is not
|
|
116 done). Let WBR denote the width of the button row.
|
|
117
|
|
118 Next, the width of the static field is determined.
|
|
119 First, if all lines of text fit into max (WBR, X_MAX_TEXT), the width of the
|
|
120 control is the same as the width of the longest line.
|
|
121 Second, if all lines of text are narrower than X_MIN_TEXT, then width of
|
|
122 the control is set to X_MIN_TEXT.
|
|
123 Otherwise, width is set to max(WBR, X_AVE_TEXT). In this case, line wrapping will
|
|
124 happen.
|
|
125
|
|
126 If width of the text control is larger than that of the button row, then the
|
|
127 latter is centered across the dialog, by giving it extra edge
|
|
128 margins. Otherwise, minimal margins are given to the button row.
|
|
129 */
|
|
130
|
|
131 #define ID_ITEM_BIAS 32
|
|
132
|
442
|
133 void
|
|
134 mswindows_register_popup_frame (Lisp_Object frame)
|
|
135 {
|
|
136 Vpopup_frame_list = Fcons (frame, Vpopup_frame_list);
|
|
137 }
|
|
138
|
|
139 void
|
|
140 mswindows_unregister_popup_frame (Lisp_Object frame)
|
|
141 {
|
|
142 Vpopup_frame_list = delq_no_quit (frame, Vpopup_frame_list);
|
|
143 }
|
|
144
|
|
145 /* Dispatch message to any dialog boxes. Return non-zero if dispatched. */
|
|
146 int
|
|
147 mswindows_is_dialog_msg (MSG *msg)
|
|
148 {
|
|
149 LIST_LOOP_2 (data, Vdialog_data_list)
|
|
150 {
|
|
151 if (IsDialogMessage (XMSWINDOWS_DIALOG_ID (data)->hwnd, msg))
|
|
152 return 1;
|
|
153 }
|
|
154
|
|
155 {
|
|
156 LIST_LOOP_2 (popup, Vpopup_frame_list)
|
|
157 {
|
|
158 HWND hwnd = FRAME_MSWINDOWS_HANDLE (XFRAME (popup));
|
444
|
159 /* This is a windows feature that allows dialog type
|
|
160 processing to be applied to standard windows containing
|
|
161 controls. */
|
442
|
162 if (IsDialogMessage (hwnd, msg))
|
|
163 return 1;
|
|
164 }
|
|
165 }
|
|
166 return 0;
|
|
167 }
|
|
168
|
|
169 static Lisp_Object
|
|
170 mark_mswindows_dialog_id (Lisp_Object obj)
|
|
171 {
|
|
172 struct mswindows_dialog_id *data = XMSWINDOWS_DIALOG_ID (obj);
|
|
173 mark_object (data->frame);
|
|
174 return data->callbacks;
|
|
175 }
|
|
176
|
|
177 DEFINE_LRECORD_IMPLEMENTATION ("mswindows-dialog-id", mswindows_dialog_id,
|
617
|
178 mark_mswindows_dialog_id,
|
|
179 internal_object_printer, 0, 0, 0, 0,
|
442
|
180 struct mswindows_dialog_id);
|
|
181
|
428
|
182 /* Dialog procedure */
|
|
183 static BOOL CALLBACK
|
|
184 dialog_proc (HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
|
|
185 {
|
|
186 switch (msg)
|
|
187 {
|
|
188 case WM_INITDIALOG:
|
|
189 SetWindowLong (hwnd, DWL_USER, l_param);
|
|
190 break;
|
|
191
|
|
192 case WM_DESTROY:
|
|
193 {
|
|
194 Lisp_Object data;
|
|
195 VOID_TO_LISP (data, GetWindowLong (hwnd, DWL_USER));
|
|
196 Vdialog_data_list = delq_no_quit (data, Vdialog_data_list);
|
|
197 }
|
|
198 break;
|
|
199
|
|
200 case WM_COMMAND:
|
|
201 {
|
|
202 Lisp_Object fn, arg, data;
|
442
|
203 struct mswindows_dialog_id *did;
|
|
204
|
428
|
205 VOID_TO_LISP (data, GetWindowLong (hwnd, DWL_USER));
|
442
|
206 did = XMSWINDOWS_DIALOG_ID (data);
|
|
207 if (w_param != IDCANCEL) /* user pressed escape */
|
|
208 {
|
|
209 assert (w_param >= ID_ITEM_BIAS
|
647
|
210 && (EMACS_INT) w_param
|
442
|
211 < XVECTOR_LENGTH (did->callbacks) + ID_ITEM_BIAS);
|
|
212
|
|
213 get_gui_callback (XVECTOR_DATA (did->callbacks)
|
|
214 [w_param - ID_ITEM_BIAS],
|
|
215 &fn, &arg);
|
|
216 mswindows_enqueue_misc_user_event (did->frame, fn, arg);
|
|
217 }
|
|
218 else
|
|
219 mswindows_enqueue_misc_user_event (did->frame, Qrun_hooks,
|
|
220 Qmenu_no_selection_hook);
|
|
221 /* #### need to error-protect! will do so when i merge in
|
|
222 my working ws */
|
|
223 va_run_hook_with_args (Qdelete_dialog_box_hook, 1, data);
|
428
|
224
|
|
225 DestroyWindow (hwnd);
|
|
226 }
|
|
227 break;
|
|
228
|
|
229 default:
|
|
230 return FALSE;
|
|
231 }
|
|
232 return TRUE;
|
|
233 }
|
|
234
|
|
235 /* Helper function which converts the supplied string STRING into Unicode and
|
|
236 pushes it at the end of DYNARR */
|
|
237 static void
|
|
238 push_lisp_string_as_unicode (unsigned_char_dynarr* dynarr, Lisp_Object string)
|
|
239 {
|
|
240 Extbyte *mbcs_string;
|
|
241 Charcount length = XSTRING_CHAR_LENGTH (string);
|
|
242 LPWSTR uni_string;
|
|
243
|
440
|
244 TO_EXTERNAL_FORMAT (LISP_STRING, string,
|
|
245 C_STRING_ALLOCA, mbcs_string,
|
|
246 Qnative);
|
428
|
247 uni_string = alloca_array (WCHAR, length + 1);
|
|
248 length = MultiByteToWideChar (CP_ACP, 0, mbcs_string, -1,
|
|
249 uni_string, sizeof(WCHAR) * (length + 1));
|
|
250 Dynarr_add_many (dynarr, uni_string, sizeof(WCHAR) * length);
|
|
251 }
|
|
252
|
442
|
253 /* Helper function which converts the supplied string STRING into Unicode and
|
|
254 pushes it at the end of DYNARR */
|
|
255 static void
|
665
|
256 push_intbyte_string_as_unicode (unsigned_char_dynarr* dynarr, Intbyte *string,
|
442
|
257 Bytecount len)
|
|
258 {
|
|
259 Extbyte *mbcs_string;
|
|
260 Charcount length = bytecount_to_charcount (string, len);
|
|
261 LPWSTR uni_string;
|
|
262
|
|
263 TO_EXTERNAL_FORMAT (C_STRING, string,
|
|
264 C_STRING_ALLOCA, mbcs_string,
|
|
265 Qnative);
|
|
266 uni_string = alloca_array (WCHAR, length + 1);
|
|
267 length = MultiByteToWideChar (CP_ACP, 0, mbcs_string, -1,
|
|
268 uni_string, sizeof(WCHAR) * (length + 1));
|
|
269 Dynarr_add_many (dynarr, uni_string, sizeof(WCHAR) * length);
|
|
270 }
|
|
271
|
428
|
272 /* Given button TEXT, return button width in DLU */
|
647
|
273 static int
|
428
|
274 button_width (Lisp_Object text)
|
|
275 {
|
647
|
276 int width = X_DLU_PER_CHAR * XSTRING_CHAR_LENGTH (text);
|
428
|
277 return max (X_MIN_BUTTON, width);
|
|
278 }
|
|
279
|
|
280 /* Unwind protection routine frees a dynarr opaqued into arg */
|
|
281 static Lisp_Object
|
|
282 free_dynarr_opaque_ptr (Lisp_Object arg)
|
|
283 {
|
|
284 Dynarr_free (get_opaque_ptr (arg));
|
|
285 return arg;
|
|
286 }
|
|
287
|
707
|
288 /* Unwind protection decrements dialog count */
|
|
289 static Lisp_Object
|
|
290 dialog_popped_down (Lisp_Object arg)
|
|
291 {
|
|
292 popup_up_p--;
|
|
293 }
|
|
294
|
428
|
295
|
647
|
296 #define ALIGN_TEMPLATE \
|
|
297 { \
|
|
298 int slippage = Dynarr_length (template_) & 3; \
|
|
299 if (slippage) \
|
|
300 Dynarr_add_many (template_, &zeroes, slippage); \
|
428
|
301 }
|
|
302
|
442
|
303 static struct
|
|
304 {
|
647
|
305 DWORD errmess;
|
442
|
306 char *errname;
|
|
307 } common_dialog_errors[] =
|
|
308 {
|
|
309 { CDERR_DIALOGFAILURE, "CDERR_DIALOGFAILURE" },
|
|
310 { CDERR_FINDRESFAILURE, "CDERR_FINDRESFAILURE" },
|
|
311 { CDERR_INITIALIZATION, "CDERR_INITIALIZATION" },
|
|
312 { CDERR_LOADRESFAILURE, "CDERR_LOADRESFAILURE" },
|
|
313 { CDERR_LOADSTRFAILURE, "CDERR_LOADSTRFAILURE" },
|
|
314 { CDERR_LOCKRESFAILURE, "CDERR_LOCKRESFAILURE" },
|
|
315 { CDERR_MEMALLOCFAILURE, "CDERR_MEMALLOCFAILURE" },
|
|
316 { CDERR_MEMLOCKFAILURE, "CDERR_MEMLOCKFAILURE" },
|
|
317 { CDERR_NOHINSTANCE, "CDERR_NOHINSTANCE" },
|
|
318 { CDERR_NOHOOK, "CDERR_NOHOOK" },
|
|
319 { CDERR_NOTEMPLATE, "CDERR_NOTEMPLATE" },
|
|
320 { CDERR_REGISTERMSGFAIL, "CDERR_REGISTERMSGFAIL" },
|
|
321 { CDERR_STRUCTSIZE, "CDERR_STRUCTSIZE" },
|
|
322 { PDERR_CREATEICFAILURE, "PDERR_CREATEICFAILURE" },
|
|
323 { PDERR_DEFAULTDIFFERENT, "PDERR_DEFAULTDIFFERENT" },
|
|
324 { PDERR_DNDMMISMATCH, "PDERR_DNDMMISMATCH" },
|
|
325 { PDERR_GETDEVMODEFAIL, "PDERR_GETDEVMODEFAIL" },
|
|
326 { PDERR_INITFAILURE, "PDERR_INITFAILURE" },
|
|
327 { PDERR_LOADDRVFAILURE, "PDERR_LOADDRVFAILURE" },
|
|
328 { PDERR_NODEFAULTPRN, "PDERR_NODEFAULTPRN" },
|
|
329 { PDERR_NODEVICES, "PDERR_NODEVICES" },
|
|
330 { PDERR_PARSEFAILURE, "PDERR_PARSEFAILURE" },
|
|
331 { PDERR_PRINTERNOTFOUND, "PDERR_PRINTERNOTFOUND" },
|
|
332 { PDERR_RETDEFFAILURE, "PDERR_RETDEFFAILURE" },
|
|
333 { PDERR_SETUPFAILURE, "PDERR_SETUPFAILURE" },
|
|
334 { CFERR_MAXLESSTHANMIN, "CFERR_MAXLESSTHANMIN" },
|
|
335 { CFERR_NOFONTS, "CFERR_NOFONTS" },
|
|
336 { FNERR_BUFFERTOOSMALL, "FNERR_BUFFERTOOSMALL" },
|
|
337 { FNERR_INVALIDFILENAME, "FNERR_INVALIDFILENAME" },
|
|
338 { FNERR_SUBCLASSFAILURE, "FNERR_SUBCLASSFAILURE" },
|
|
339 { FRERR_BUFFERLENGTHZERO, "FRERR_BUFFERLENGTHZERO" },
|
|
340 };
|
|
341
|
673
|
342 struct param_data {
|
|
343 char* fname;
|
|
344 char* unknown_fname;
|
|
345 int validate;
|
|
346 };
|
|
347
|
|
348 static int
|
|
349 CALLBACK handle_directory_proc (HWND hwnd, UINT msg,
|
|
350 LPARAM lParam, LPARAM lpData)
|
|
351 {
|
|
352 TCHAR szDir[MAX_PATH];
|
|
353 struct param_data* pd = (struct param_data*)lpData;
|
|
354
|
|
355 switch(msg) {
|
|
356 case BFFM_INITIALIZED:
|
|
357 // WParam is TRUE since you are passing a path.
|
|
358 // It would be FALSE if you were passing a pidl.
|
|
359 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)pd->fname);
|
|
360 break;
|
|
361
|
|
362 case BFFM_SELCHANGED:
|
|
363 // Set the status window to the currently selected path.
|
|
364 if (SHGetPathFromIDList((LPITEMIDLIST) lParam, szDir)) {
|
|
365 SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir);
|
|
366 }
|
|
367 break;
|
|
368
|
|
369 case BFFM_VALIDATEFAILED:
|
|
370 if (pd->validate)
|
|
371 return TRUE;
|
|
372 else
|
|
373 pd->unknown_fname = xstrdup((char*)lParam);
|
|
374 break;
|
|
375
|
|
376 default:
|
|
377 break;
|
|
378 }
|
|
379 return 0;
|
|
380 }
|
|
381
|
|
382 static Lisp_Object
|
|
383 handle_directory_dialog_box (struct frame *f, Lisp_Object keys)
|
|
384 {
|
|
385 Lisp_Object ret = Qnil;
|
|
386 BROWSEINFO bi;
|
|
387 LPITEMIDLIST pidl;
|
|
388 LPMALLOC pMalloc;
|
|
389 struct param_data pd;
|
|
390
|
|
391 xzero(pd);
|
|
392 xzero(bi);
|
|
393
|
|
394 bi.lParam = (LPARAM)&pd;
|
|
395 bi.hwndOwner = FRAME_MSWINDOWS_HANDLE (f);
|
|
396 bi.pszDisplayName = 0;
|
|
397 bi.pidlRoot = 0;
|
|
398 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_EDITBOX;
|
|
399 bi.lpfn = handle_directory_proc;
|
|
400
|
|
401 LOCAL_FILE_FORMAT_TO_TSTR (Fexpand_file_name (build_string (""), Qnil),
|
|
402 (char*)pd.fname);
|
|
403
|
|
404 {
|
|
405 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
|
|
406 {
|
|
407 if (EQ (key, Q_title))
|
|
408 {
|
|
409 CHECK_STRING (value);
|
|
410 LISP_STRING_TO_EXTERNAL (value, bi.lpszTitle, Qmswindows_tstr);
|
|
411 }
|
|
412 else if (EQ (key, Q_initial_directory))
|
|
413 LOCAL_FILE_FORMAT_TO_TSTR (Fexpand_file_name (value, Qnil),
|
|
414 pd.fname);
|
|
415 else if (EQ (key, Q_initial_filename))
|
|
416 ; /* do nothing */
|
|
417 else if (EQ (key, Q_file_must_exist))
|
|
418 {
|
|
419 if (!NILP (value)) {
|
|
420 pd.validate = TRUE;
|
|
421 bi.ulFlags |= BIF_VALIDATE;
|
|
422 }
|
|
423 else
|
|
424 bi.ulFlags &= ~BIF_VALIDATE;
|
|
425 }
|
|
426 else
|
|
427 invalid_constant ("Unrecognized directory-dialog keyword", key);
|
|
428 }
|
|
429 }
|
|
430
|
|
431 if (SHGetMalloc(&pMalloc) == NOERROR)
|
|
432 {
|
|
433 pidl = SHBrowseForFolder(&bi);
|
|
434 if (pidl) {
|
|
435 TCHAR* szDir = alloca (MAX_PATH);
|
|
436
|
|
437 if (SHGetPathFromIDList(pidl, szDir)) {
|
|
438 ret = tstr_to_local_file_format (szDir);
|
|
439 }
|
|
440
|
|
441 pMalloc->lpVtbl->Free(pMalloc, pidl);
|
|
442 pMalloc->lpVtbl->Release(pMalloc);
|
|
443 return ret;
|
707
|
444 }
|
673
|
445 else if (pd.unknown_fname != 0) {
|
|
446 ret = tstr_to_local_file_format (pd.unknown_fname);
|
|
447 xfree(pd.unknown_fname);
|
|
448 }
|
707
|
449 else while (1)
|
|
450 signal_quit ();
|
673
|
451 }
|
|
452 else
|
|
453 signal_error (Qdialog_box_error,
|
|
454 "Unable to create folder browser",
|
|
455 make_int (0));
|
|
456 return ret;
|
|
457 }
|
|
458
|
442
|
459 static Lisp_Object
|
|
460 handle_file_dialog_box (struct frame *f, Lisp_Object keys)
|
|
461 {
|
|
462 OPENFILENAME ofn;
|
673
|
463
|
442
|
464 char fnbuf[8000];
|
|
465
|
|
466 xzero (ofn);
|
|
467 ofn.lStructSize = sizeof (ofn);
|
673
|
468 ofn.Flags = OFN_EXPLORER;
|
442
|
469 ofn.hwndOwner = FRAME_MSWINDOWS_HANDLE (f);
|
|
470 ofn.lpstrFile = fnbuf;
|
|
471 ofn.nMaxFile = sizeof (fnbuf) / XETCHAR_SIZE;
|
|
472 xetcscpy (fnbuf, XETEXT (""));
|
|
473
|
|
474 LOCAL_FILE_FORMAT_TO_TSTR (Fexpand_file_name (build_string (""), Qnil),
|
|
475 ofn.lpstrInitialDir);
|
|
476
|
|
477 {
|
|
478 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
|
|
479 {
|
|
480 if (EQ (key, Q_initial_filename))
|
|
481 {
|
|
482 Extbyte *fnout;
|
|
483
|
|
484 CHECK_STRING (value);
|
|
485 LOCAL_FILE_FORMAT_TO_TSTR (value, fnout);
|
|
486 xetcscpy (fnbuf, fnout);
|
|
487 }
|
|
488 else if (EQ (key, Q_title))
|
|
489 {
|
|
490 CHECK_STRING (value);
|
|
491 LISP_STRING_TO_EXTERNAL (value, ofn.lpstrTitle, Qmswindows_tstr);
|
|
492 }
|
|
493 else if (EQ (key, Q_initial_directory))
|
|
494 LOCAL_FILE_FORMAT_TO_TSTR (Fexpand_file_name (value, Qnil),
|
|
495 ofn.lpstrInitialDir);
|
|
496 else if (EQ (key, Q_file_must_exist))
|
|
497 {
|
|
498 if (!NILP (value))
|
|
499 ofn.Flags |= OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
|
|
500 else
|
|
501 ofn.Flags &= ~(OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST);
|
|
502 }
|
|
503 else
|
563
|
504 invalid_constant ("Unrecognized file-dialog keyword", key);
|
442
|
505 }
|
|
506 }
|
|
507
|
|
508 if (!GetOpenFileName (&ofn))
|
|
509 {
|
|
510 DWORD err = CommDlgExtendedError ();
|
|
511 if (!err)
|
|
512 {
|
|
513 while (1)
|
|
514 signal_quit ();
|
|
515 }
|
|
516 else
|
|
517 {
|
|
518 int i;
|
|
519
|
|
520 for (i = 0; i < countof (common_dialog_errors); i++)
|
|
521 {
|
|
522 if (common_dialog_errors[i].errmess == err)
|
563
|
523 signal_error (Qdialog_box_error,
|
|
524 "Creating file-dialog-box",
|
|
525 build_string
|
|
526 (common_dialog_errors[i].errname));
|
442
|
527 }
|
|
528
|
563
|
529 signal_error (Qdialog_box_error,
|
|
530 "Unknown common dialog box error???",
|
|
531 make_int (err));
|
442
|
532 }
|
|
533 }
|
|
534
|
|
535 return tstr_to_local_file_format (ofn.lpstrFile);
|
|
536 }
|
|
537
|
|
538 static Lisp_Object
|
|
539 handle_question_dialog_box (struct frame *f, Lisp_Object keys)
|
428
|
540 {
|
|
541 Lisp_Object_dynarr *dialog_items = Dynarr_new (Lisp_Object);
|
593
|
542 unsigned_char_dynarr *template_ = Dynarr_new (unsigned_char);
|
647
|
543 int button_row_width = 0;
|
|
544 int text_width, text_height;
|
442
|
545 Lisp_Object question = Qnil, title = Qnil;
|
428
|
546
|
|
547 int unbind_count = specpdl_depth ();
|
|
548 record_unwind_protect (free_dynarr_opaque_ptr,
|
|
549 make_opaque_ptr (dialog_items));
|
|
550 record_unwind_protect (free_dynarr_opaque_ptr,
|
593
|
551 make_opaque_ptr (template_));
|
428
|
552
|
|
553 /* A big NO NEED to GCPRO gui_items stored in the array: they are just
|
442
|
554 pointers into KEYS list, which is GC-protected by the caller */
|
428
|
555
|
|
556 {
|
442
|
557 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, keys)
|
428
|
558 {
|
442
|
559 if (EQ (key, Q_question))
|
|
560 {
|
|
561 CHECK_STRING (value);
|
|
562 question = value;
|
|
563 }
|
|
564 else if (EQ (key, Q_title))
|
|
565 {
|
|
566 CHECK_STRING (value);
|
|
567 title = value;
|
|
568 }
|
|
569 else if (EQ (key, Q_buttons))
|
428
|
570 {
|
442
|
571 Lisp_Object item_cons;
|
|
572
|
|
573 /* Parse each item in the dialog into gui_item structs,
|
|
574 and stuff a dynarr of these. Calculate button row width
|
|
575 in this loop too */
|
|
576 EXTERNAL_LIST_LOOP (item_cons, value)
|
|
577 {
|
|
578 if (!NILP (XCAR (item_cons)))
|
|
579 {
|
|
580 Lisp_Object gitem =
|
|
581 gui_parse_item_keywords (XCAR (item_cons));
|
|
582 Dynarr_add (dialog_items, gitem);
|
|
583 button_row_width += button_width (XGUI_ITEM (gitem)->name)
|
|
584 + X_BUTTON_MARGIN;
|
|
585 }
|
|
586 }
|
|
587
|
|
588 button_row_width -= X_BUTTON_MARGIN;
|
428
|
589 }
|
442
|
590 else
|
563
|
591 invalid_constant ("Unrecognized question-dialog keyword", key);
|
428
|
592 }
|
|
593 }
|
|
594
|
442
|
595 if (Dynarr_length (dialog_items) == 0)
|
563
|
596 sferror ("Dialog descriptor provides no buttons", keys);
|
442
|
597
|
|
598 if (NILP (question))
|
563
|
599 sferror ("Dialog descriptor provides no question", keys);
|
442
|
600
|
428
|
601 /* Determine the final width layout */
|
|
602 {
|
665
|
603 Intbyte *p = XSTRING_DATA (question);
|
428
|
604 Charcount string_max = 0, this_length = 0;
|
|
605 while (1)
|
|
606 {
|
|
607 Emchar ch = charptr_emchar (p);
|
|
608 INC_CHARPTR (p);
|
|
609
|
|
610 if (ch == (Emchar)'\n' || ch == (Emchar)'\0')
|
|
611 {
|
|
612 string_max = max (this_length, string_max);
|
|
613 this_length = 0;
|
|
614 }
|
|
615 else
|
|
616 ++this_length;
|
|
617
|
|
618 if (ch == (Emchar)'\0')
|
|
619 break;
|
|
620 }
|
|
621
|
|
622 if (string_max * X_DLU_PER_CHAR > max (X_MAX_TEXT, button_row_width))
|
|
623 text_width = X_AVE_TEXT;
|
|
624 else if (string_max * X_DLU_PER_CHAR < X_MIN_TEXT)
|
|
625 text_width = X_MIN_TEXT;
|
|
626 else
|
|
627 text_width = string_max * X_DLU_PER_CHAR;
|
|
628 text_width = max (text_width, button_row_width);
|
|
629 }
|
|
630
|
|
631 /* Now calculate the height for the text control */
|
|
632 {
|
665
|
633 Intbyte *p = XSTRING_DATA (question);
|
428
|
634 Charcount break_at = text_width / X_DLU_PER_CHAR;
|
|
635 Charcount char_pos = 0;
|
|
636 int num_lines = 1;
|
|
637 Emchar ch;
|
|
638
|
442
|
639 while ((ch = charptr_emchar (p)) != (Emchar) '\0')
|
428
|
640 {
|
|
641 INC_CHARPTR (p);
|
442
|
642 char_pos += ch != (Emchar) '\n';
|
|
643 if (ch == (Emchar) '\n' || char_pos == break_at)
|
428
|
644 {
|
|
645 ++num_lines;
|
|
646 char_pos = 0;
|
|
647 }
|
|
648 }
|
|
649 text_height = Y_TEXT_MARGIN + Y_DLU_PER_CHAR * num_lines;
|
|
650 }
|
|
651
|
|
652 /* Ok, now we are ready to stuff the dialog template and lay out controls */
|
|
653 {
|
|
654 DLGTEMPLATE dlg_tem;
|
|
655 DLGITEMTEMPLATE item_tem;
|
|
656 int i;
|
|
657 const unsigned int zeroes = 0;
|
|
658 const unsigned int ones = 0xFFFFFFFF;
|
|
659 const WORD static_class_id = 0x0082;
|
|
660 const WORD button_class_id = 0x0080;
|
|
661
|
|
662 /* Create and stuff in DLGTEMPLATE header */
|
|
663 dlg_tem.style = (DS_CENTER | DS_MODALFRAME | DS_SETFONT
|
|
664 | WS_CAPTION | WS_POPUP | WS_VISIBLE);
|
|
665 dlg_tem.dwExtendedStyle = 0;
|
|
666 dlg_tem.cdit = Dynarr_length (dialog_items) + 1;
|
|
667 dlg_tem.x = 0;
|
|
668 dlg_tem.y = 0;
|
|
669 dlg_tem.cx = text_width + 2 * X_TEXT_FROM_EDGE;
|
|
670 dlg_tem.cy = (Y_TEXT_FROM_EDGE + text_height + Y_TEXT_FROM_BUTTON
|
|
671 + Y_BUTTON + Y_BUTTON_FROM_EDGE);
|
593
|
672 Dynarr_add_many (template_, &dlg_tem, sizeof (dlg_tem));
|
428
|
673
|
|
674 /* We want no menu and standard class */
|
593
|
675 Dynarr_add_many (template_, &zeroes, 4);
|
428
|
676
|
442
|
677 /* And the third is the dialog title. "XEmacs" unless one is supplied.
|
|
678 Note that the string must be in Unicode. */
|
|
679 if (NILP (title))
|
593
|
680 Dynarr_add_many (template_, L"XEmacs", 14);
|
442
|
681 else
|
593
|
682 push_lisp_string_as_unicode (template_, title);
|
428
|
683
|
|
684 /* We want standard dialog font */
|
593
|
685 Dynarr_add_many (template_, L"\x08MS Shell Dlg", 28);
|
428
|
686
|
|
687 /* Next add text control. */
|
|
688 item_tem.style = WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX;
|
|
689 item_tem.dwExtendedStyle = 0;
|
|
690 item_tem.x = X_TEXT_FROM_EDGE;
|
|
691 item_tem.y = Y_TEXT_FROM_EDGE;
|
|
692 item_tem.cx = text_width;
|
|
693 item_tem.cy = text_height;
|
|
694 item_tem.id = 0xFFFF;
|
|
695
|
|
696 ALIGN_TEMPLATE;
|
593
|
697 Dynarr_add_many (template_, &item_tem, sizeof (item_tem));
|
428
|
698
|
|
699 /* Right after class id follows */
|
593
|
700 Dynarr_add_many (template_, &ones, 2);
|
|
701 Dynarr_add_many (template_, &static_class_id, sizeof (static_class_id));
|
428
|
702
|
|
703 /* Next thing to add is control text, as Unicode string */
|
593
|
704 push_lisp_string_as_unicode (template_, question);
|
428
|
705
|
|
706 /* Specify 0 length creation data */
|
593
|
707 Dynarr_add_many (template_, &zeroes, 2);
|
428
|
708
|
|
709 /* Now it's the button time */
|
|
710 item_tem.y = Y_TEXT_FROM_EDGE + text_height + Y_TEXT_FROM_BUTTON;
|
|
711 item_tem.x = X_BUTTON_FROM_EDGE + (button_row_width < text_width
|
|
712 ? (text_width - button_row_width) / 2
|
|
713 : 0);
|
|
714 item_tem.cy = Y_BUTTON;
|
|
715 item_tem.dwExtendedStyle = 0;
|
|
716
|
|
717 for (i = 0; i < Dynarr_length (dialog_items); ++i)
|
|
718 {
|
|
719 Lisp_Object* gui_item = Dynarr_atp (dialog_items, i);
|
440
|
720 Lisp_Gui_Item *pgui_item = XGUI_ITEM (*gui_item);
|
428
|
721
|
|
722 item_tem.style = (WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON
|
|
723 | (gui_item_active_p (*gui_item) ? 0 : WS_DISABLED));
|
|
724 item_tem.cx = button_width (pgui_item->name);
|
|
725 /* Item ids are indices into dialog_items plus offset, to avoid having
|
|
726 items by reserved ids (IDOK, IDCANCEL) */
|
|
727 item_tem.id = i + ID_ITEM_BIAS;
|
|
728
|
|
729 ALIGN_TEMPLATE;
|
593
|
730 Dynarr_add_many (template_, &item_tem, sizeof (item_tem));
|
428
|
731
|
|
732 /* Right after 0xFFFF and class id atom follows */
|
593
|
733 Dynarr_add_many (template_, &ones, 2);
|
|
734 Dynarr_add_many (template_, &button_class_id,
|
|
735 sizeof (button_class_id));
|
428
|
736
|
|
737 /* Next thing to add is control text, as Unicode string */
|
442
|
738 {
|
|
739 Lisp_Object ctext = pgui_item->name;
|
|
740 Emchar accel_unused;
|
665
|
741 Intbyte *trans = (Intbyte *) alloca (2 * XSTRING_LENGTH (ctext) + 3);
|
442
|
742 Bytecount translen;
|
|
743
|
|
744 memcpy (trans, XSTRING_DATA (ctext), XSTRING_LENGTH (ctext) + 1);
|
|
745 translen =
|
|
746 mswindows_translate_menu_or_dialog_item (trans,
|
|
747 XSTRING_LENGTH (ctext),
|
|
748 2 * XSTRING_LENGTH (ctext) + 3,
|
|
749 &accel_unused,
|
|
750 ctext);
|
665
|
751 push_intbyte_string_as_unicode (template_, trans, translen);
|
442
|
752 }
|
428
|
753
|
|
754 /* Specify 0 length creation data. */
|
593
|
755 Dynarr_add_many (template_, &zeroes, 2);
|
428
|
756
|
|
757 item_tem.x += item_tem.cx + X_BUTTON_SPACING;
|
|
758 }
|
|
759 }
|
|
760
|
|
761 /* Now the Windows dialog structure is ready. We need to prepare a
|
|
762 data structure for the new dialog, which will contain callbacks
|
442
|
763 and the frame for these callbacks. This structure has to be
|
|
764 GC-protected and thus it is put into a statically protected
|
|
765 list. */
|
428
|
766 {
|
442
|
767 Lisp_Object dialog_data;
|
428
|
768 int i;
|
442
|
769 struct mswindows_dialog_id *did =
|
|
770 alloc_lcrecord_type (struct mswindows_dialog_id,
|
|
771 &lrecord_mswindows_dialog_id);
|
|
772
|
|
773 XSETMSWINDOWS_DIALOG_ID (dialog_data, did);
|
|
774
|
|
775 did->frame = wrap_frame (f);
|
|
776 did->callbacks = make_vector (Dynarr_length (dialog_items), Qunbound);
|
|
777 for (i = 0; i < Dynarr_length (dialog_items); i++)
|
|
778 XVECTOR_DATA (did->callbacks) [i] =
|
|
779 XGUI_ITEM (*Dynarr_atp (dialog_items, i))->callback;
|
428
|
780
|
|
781 /* Woof! Everything is ready. Pop pop pop in now! */
|
442
|
782 did->hwnd =
|
|
783 CreateDialogIndirectParam (NULL,
|
593
|
784 (LPDLGTEMPLATE) Dynarr_atp (template_, 0),
|
442
|
785 FRAME_MSWINDOWS_HANDLE (f), dialog_proc,
|
|
786 (LPARAM) LISP_TO_VOID (dialog_data));
|
|
787 if (!did->hwnd)
|
428
|
788 /* Something went wrong creating the dialog */
|
563
|
789 signal_error (Qdialog_box_error, "Creating dialog", keys);
|
428
|
790
|
|
791 Vdialog_data_list = Fcons (dialog_data, Vdialog_data_list);
|
442
|
792
|
|
793 /* Cease protection and free dynarrays */
|
|
794 unbind_to (unbind_count, Qnil);
|
|
795 return dialog_data;
|
428
|
796 }
|
442
|
797 }
|
428
|
798
|
442
|
799 static Lisp_Object
|
|
800 mswindows_make_dialog_box_internal (struct frame* f, Lisp_Object type,
|
|
801 Lisp_Object keys)
|
|
802 {
|
707
|
803 int unbind_count = specpdl_depth ();
|
|
804 record_unwind_protect (dialog_popped_down, Qnil);
|
|
805 popup_up_p++;
|
|
806
|
442
|
807 if (EQ (type, Qfile))
|
707
|
808 return unbind_to (unbind_count, handle_file_dialog_box (f, keys));
|
673
|
809 else if (EQ (type, Qdirectory))
|
707
|
810 return unbind_to (unbind_count, handle_directory_dialog_box (f, keys));
|
442
|
811 else if (EQ (type, Qquestion))
|
707
|
812 return unbind_to (unbind_count, handle_question_dialog_box (f, keys));
|
442
|
813 else if (EQ (type, Qprint))
|
707
|
814 return unbind_to (unbind_count, mswindows_handle_print_dialog_box (f, keys));
|
442
|
815 else if (EQ (type, Qpage_setup))
|
707
|
816 return unbind_to (unbind_count,
|
|
817 mswindows_handle_page_setup_dialog_box (f, keys));
|
442
|
818 else
|
563
|
819 signal_error (Qunimplemented, "Dialog box type", type);
|
442
|
820 return Qnil;
|
428
|
821 }
|
|
822
|
|
823 void
|
|
824 console_type_create_dialog_mswindows (void)
|
|
825 {
|
442
|
826 CONSOLE_HAS_METHOD (mswindows, make_dialog_box_internal);
|
|
827 }
|
|
828
|
|
829 void
|
|
830 syms_of_dialog_mswindows (void)
|
|
831 {
|
|
832 INIT_LRECORD_IMPLEMENTATION (mswindows_dialog_id);
|
|
833
|
|
834 DEFKEYWORD (Q_initial_directory);
|
|
835 DEFKEYWORD (Q_initial_filename);
|
|
836 DEFKEYWORD (Q_filter_list);
|
|
837 DEFKEYWORD (Q_title);
|
|
838 DEFKEYWORD (Q_allow_multi_select);
|
|
839 DEFKEYWORD (Q_create_prompt_on_nonexistent);
|
|
840 DEFKEYWORD (Q_overwrite_prompt);
|
|
841 DEFKEYWORD (Q_file_must_exist);
|
|
842 DEFKEYWORD (Q_no_network_button);
|
|
843 DEFKEYWORD (Q_no_read_only_return);
|
|
844
|
|
845 /* Errors */
|
563
|
846 DEFERROR_STANDARD (Qdialog_box_error, Qgui_error);
|
428
|
847 }
|
|
848
|
|
849 void
|
|
850 vars_of_dialog_mswindows (void)
|
|
851 {
|
442
|
852 Vpopup_frame_list = Qnil;
|
|
853 staticpro (&Vpopup_frame_list);
|
|
854
|
428
|
855 Vdialog_data_list = Qnil;
|
|
856 staticpro (&Vdialog_data_list);
|
442
|
857
|
|
858 DEFVAR_LISP ("default-file-dialog-filter-alist",
|
|
859 &Vdefault_file_dialog_filter_alist /*
|
|
860 */ );
|
|
861 Vdefault_file_dialog_filter_alist =
|
|
862 list5 (Fcons (build_string ("Text Files"), build_string ("*.txt")),
|
|
863 Fcons (build_string ("C Files"), build_string ("*.c;*.h")),
|
|
864 Fcons (build_string ("Elisp Files"), build_string ("*.el")),
|
|
865 Fcons (build_string ("HTML Files"), build_string ("*.html;*.html")),
|
|
866 Fcons (build_string ("All Files"), build_string ("*.*")));
|
428
|
867 }
|