Commit | Line | Data |
---|---|---|
b46a6a83 | 1 | /* Menu support for GNU Emacs on the Microsoft Windows API. |
acaf905b | 2 | Copyright (C) 1986, 1988, 1993-1994, 1996, 1998-1999, 2001-2012 |
8cabe764 | 3 | Free Software Foundation, Inc. |
ee78dc32 GV |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
ee78dc32 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
ee78dc32 GV |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
ee78dc32 | 19 | |
ee78dc32 | 20 | #include <config.h> |
162d2499 | 21 | |
7ab7006c | 22 | #include <signal.h> |
ee78dc32 | 23 | #include <stdio.h> |
d7306fe6 | 24 | #include <setjmp.h> |
7ab7006c | 25 | |
ee78dc32 | 26 | #include "lisp.h" |
15d36dee | 27 | #include "keyboard.h" |
5ee707b8 | 28 | #include "keymap.h" |
ee78dc32 | 29 | #include "frame.h" |
7ab7006c | 30 | #include "termhooks.h" |
ee78dc32 | 31 | #include "window.h" |
ee78dc32 | 32 | #include "blockinput.h" |
e5560ff7 | 33 | #include "character.h" |
fdc12c4d | 34 | #include "buffer.h" |
6915ded0 JR |
35 | #include "charset.h" |
36 | #include "coding.h" | |
ef7417fd | 37 | #include "menu.h" |
ee78dc32 GV |
38 | |
39 | /* This may include sys/types.h, and that somehow loses | |
40 | if this is not done before the other system files. */ | |
41 | #include "w32term.h" | |
42 | ||
0fda9b75 DC |
43 | /* Cygwin does not support the multibyte string functions declared in |
44 | * mbstring.h below --- but that's okay: because Cygwin is | |
45 | * UNICODE-only, we don't need to use these functions anyway. */ | |
46 | ||
47 | #ifndef NTGUI_UNICODE | |
48 | #include <mbstring.h> | |
49 | #endif /* !NTGUI_UNICODE */ | |
50 | ||
ee78dc32 GV |
51 | /* Load sys/types.h if not already loaded. |
52 | In some systems loading it twice is suicidal. */ | |
53 | #ifndef makedev | |
54 | #include <sys/types.h> | |
55 | #endif | |
56 | ||
57 | #include "dispextern.h" | |
58 | ||
1c6900d9 EZ |
59 | #include "w32heap.h" /* for osinfo_cache */ |
60 | ||
222456a1 | 61 | #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */ |
162d2499 | 62 | |
485015ca GV |
63 | #ifndef TRUE |
64 | #define TRUE 1 | |
65 | #define FALSE 0 | |
66 | #endif /* no TRUE */ | |
67 | ||
b2a916a0 | 68 | HMENU current_popup_menu; |
ace9b298 | 69 | |
b56ceb92 JB |
70 | void syms_of_w32menu (void); |
71 | void globals_of_w32menu (void); | |
f60ae425 BK |
72 | |
73 | typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) ( | |
74 | IN HMENU, | |
75 | IN UINT, | |
76 | IN BOOL, | |
1f06d367 | 77 | IN OUT LPMENUITEMINFOA); |
f60ae425 BK |
78 | typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) ( |
79 | IN HMENU, | |
80 | IN UINT, | |
81 | IN BOOL, | |
1f06d367 | 82 | IN LPCMENUITEMINFOA); |
1c9b4129 JR |
83 | typedef int (WINAPI * MessageBoxW_Proc) ( |
84 | IN HWND window, | |
85 | IN WCHAR *text, | |
86 | IN WCHAR *caption, | |
87 | IN UINT type); | |
f60ae425 | 88 | |
0fda9b75 DC |
89 | #ifdef NTGUI_UNICODE |
90 | #define get_menu_item_info GetMenuItemInfoA | |
91 | #define set_menu_item_info SetMenuItemInfoA | |
92 | #define unicode_append_menu AppendMenuW | |
93 | #define unicode_message_box MessageBoxW | |
94 | #else /* !NTGUI_UNICODE */ | |
1f06d367 JR |
95 | GetMenuItemInfoA_Proc get_menu_item_info = NULL; |
96 | SetMenuItemInfoA_Proc set_menu_item_info = NULL; | |
97 | AppendMenuW_Proc unicode_append_menu = NULL; | |
1c9b4129 | 98 | MessageBoxW_Proc unicode_message_box = NULL; |
0fda9b75 | 99 | #endif /* NTGUI_UNICODE */ |
ace9b298 | 100 | |
fdc12c4d RS |
101 | Lisp_Object Qdebug_on_next_call; |
102 | ||
f57e2426 | 103 | void set_frame_menubar (FRAME_PTR, int, int); |
014510b0 | 104 | |
1c5acb8c | 105 | #ifdef HAVE_DIALOGS |
f57e2426 | 106 | static Lisp_Object w32_dialog_show (FRAME_PTR, int, Lisp_Object, char**); |
31403b24 | 107 | #else |
f57e2426 J |
108 | static int is_simple_dialog (Lisp_Object); |
109 | static Lisp_Object simple_dialog_show (FRAME_PTR, Lisp_Object, Lisp_Object); | |
1c5acb8c | 110 | #endif |
31403b24 | 111 | |
1c9b4129 | 112 | static void utf8to16 (unsigned char *, int, WCHAR *); |
24f981c9 | 113 | static int fill_in_menu (HMENU, widget_value *); |
1c9b4129 | 114 | |
f57e2426 | 115 | void w32_free_menu_strings (HWND); |
0fda9b75 | 116 | |
485015ca | 117 | \f |
6fc032ed | 118 | |
485015ca GV |
119 | /* This is set nonzero after the user activates the menu bar, and set |
120 | to zero again after the menu bars are redisplayed by prepare_menu_bar. | |
121 | While it is nonzero, all calls to set_frame_menubar go deep. | |
122 | ||
123 | I don't understand why this is needed, but it does seem to be | |
124 | needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */ | |
ee78dc32 | 125 | |
485015ca GV |
126 | int pending_menu_activation; |
127 | \f | |
485015ca GV |
128 | #ifdef HAVE_MENUS |
129 | ||
16b12668 | 130 | DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 3, 0, |
33f09670 JR |
131 | doc: /* Pop up a dialog box and return user's selection. |
132 | POSITION specifies which frame to use. | |
133 | This is normally a mouse button event or a window or frame. | |
134 | If POSITION is t, it means to use the frame the mouse is on. | |
135 | The dialog box appears in the middle of the specified frame. | |
136 | ||
137 | CONTENTS specifies the alternatives to display in the dialog box. | |
138 | It is a list of the form (TITLE ITEM1 ITEM2...). | |
139 | Each ITEM is a cons cell (STRING . VALUE). | |
140 | The return value is VALUE from the chosen item. | |
141 | ||
142 | An ITEM may also be just a string--that makes a nonselectable item. | |
143 | An ITEM may also be nil--that means to put all preceding items | |
144 | on the left of the dialog box and all following items on the right. | |
d23928c7 NR |
145 | \(By default, approximately half appear on each side.) |
146 | ||
147 | If HEADER is non-nil, the frame title for the box is "Information", | |
148 | otherwise it is "Question". */) | |
5842a27b | 149 | (Lisp_Object position, Lisp_Object contents, Lisp_Object header) |
ee78dc32 | 150 | { |
b2123f81 | 151 | FRAME_PTR f = NULL; |
ee78dc32 | 152 | Lisp_Object window; |
485015ca GV |
153 | |
154 | check_w32 (); | |
155 | ||
ee78dc32 | 156 | /* Decode the first argument: find the window or frame to use. */ |
485015ca | 157 | if (EQ (position, Qt) |
0b6bb670 JR |
158 | || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar) |
159 | || EQ (XCAR (position), Qtool_bar)))) | |
ee78dc32 | 160 | { |
485015ca GV |
161 | #if 0 /* Using the frame the mouse is on may not be right. */ |
162 | /* Use the mouse's current position. */ | |
162d2499 | 163 | FRAME_PTR new_f = SELECTED_FRAME (); |
485015ca | 164 | Lisp_Object bar_window; |
15d36dee | 165 | enum scroll_bar_part part; |
07156b35 | 166 | Time time; |
485015ca GV |
167 | Lisp_Object x, y; |
168 | ||
169 | (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time); | |
170 | ||
171 | if (new_f != 0) | |
172 | XSETFRAME (window, new_f); | |
173 | else | |
ee78dc32 | 174 | window = selected_window; |
485015ca GV |
175 | #endif |
176 | window = selected_window; | |
ee78dc32 GV |
177 | } |
178 | else if (CONSP (position)) | |
179 | { | |
7d7bbefd | 180 | Lisp_Object tem = XCAR (position); |
485015ca | 181 | if (CONSP (tem)) |
34348bd4 | 182 | window = Fcar (XCDR (position)); |
ee78dc32 GV |
183 | else |
184 | { | |
34348bd4 | 185 | tem = Fcar (XCDR (position)); /* EVENT_START (position) */ |
485015ca | 186 | window = Fcar (tem); /* POSN_WINDOW (tem) */ |
ee78dc32 GV |
187 | } |
188 | } | |
189 | else if (WINDOWP (position) || FRAMEP (position)) | |
190 | window = position; | |
485015ca GV |
191 | else |
192 | window = Qnil; | |
193 | ||
ee78dc32 | 194 | /* Decode where to put the menu. */ |
485015ca | 195 | |
ee78dc32 GV |
196 | if (FRAMEP (window)) |
197 | f = XFRAME (window); | |
198 | else if (WINDOWP (window)) | |
199 | { | |
b7826503 | 200 | CHECK_LIVE_WINDOW (window); |
ee78dc32 GV |
201 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); |
202 | } | |
203 | else | |
204 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
205 | but I don't want to make one now. */ | |
b7826503 | 206 | CHECK_WINDOW (window); |
485015ca | 207 | |
162d2499 | 208 | #ifndef HAVE_DIALOGS |
31403b24 | 209 | |
ee78dc32 | 210 | { |
31403b24 JR |
211 | /* Handle simple Yes/No choices as MessageBox popups. */ |
212 | if (is_simple_dialog (contents)) | |
213 | return simple_dialog_show (f, contents, header); | |
214 | else | |
215 | { | |
216 | /* Display a menu with these alternatives | |
217 | in the middle of frame F. */ | |
218 | Lisp_Object x, y, frame, newpos; | |
219 | XSETFRAME (frame, f); | |
220 | XSETINT (x, x_pixel_width (f) / 2); | |
221 | XSETINT (y, x_pixel_height (f) / 2); | |
222 | newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil)); | |
223 | return Fx_popup_menu (newpos, | |
224 | Fcons (Fcar (contents), Fcons (contents, Qnil))); | |
225 | } | |
ee78dc32 | 226 | } |
162d2499 | 227 | #else /* HAVE_DIALOGS */ |
ee78dc32 GV |
228 | { |
229 | Lisp_Object title; | |
230 | char *error_name; | |
231 | Lisp_Object selection; | |
232 | ||
233 | /* Decode the dialog items from what was specified. */ | |
234 | title = Fcar (contents); | |
b7826503 | 235 | CHECK_STRING (title); |
ee78dc32 GV |
236 | |
237 | list_of_panes (Fcons (contents, Qnil)); | |
238 | ||
239 | /* Display them in a dialog box. */ | |
240 | BLOCK_INPUT; | |
d23928c7 | 241 | selection = w32_dialog_show (f, 0, title, header, &error_name); |
ee78dc32 GV |
242 | UNBLOCK_INPUT; |
243 | ||
244 | discard_menu_items (); | |
38f2b112 | 245 | FRAME_X_DISPLAY_INFO (f)->grabbed = 0; |
ee78dc32 GV |
246 | |
247 | if (error_name) error (error_name); | |
248 | return selection; | |
249 | } | |
162d2499 | 250 | #endif /* HAVE_DIALOGS */ |
ee78dc32 GV |
251 | } |
252 | ||
014510b0 GV |
253 | /* Activate the menu bar of frame F. |
254 | This is called from keyboard.c when it gets the | |
3b8f9651 | 255 | MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue. |
014510b0 GV |
256 | |
257 | To activate the menu bar, we signal to the input thread that it can | |
258 | return from the WM_INITMENU message, allowing the normal Windows | |
259 | processing of the menus. | |
260 | ||
261 | But first we recompute the menu bar contents (the whole tree). | |
262 | ||
263 | This way we can safely execute Lisp code. */ | |
177c0ea7 | 264 | |
162d2499 | 265 | void |
b56ceb92 | 266 | x_activate_menubar (FRAME_PTR f) |
014510b0 GV |
267 | { |
268 | set_frame_menubar (f, 0, 1); | |
269 | ||
270 | /* Lock out further menubar changes while active. */ | |
271 | f->output_data.w32->menubar_active = 1; | |
272 | ||
273 | /* Signal input thread to return from WM_INITMENU. */ | |
274 | complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0); | |
275 | } | |
276 | ||
485015ca GV |
277 | /* This callback is called from the menu bar pulldown menu |
278 | when the user makes a selection. | |
279 | Figure out what the user chose | |
280 | and put the appropriate events into the keyboard buffer. */ | |
281 | ||
282 | void | |
283 | menubar_selection_callback (FRAME_PTR f, void * client_data) | |
ee78dc32 | 284 | { |
485015ca GV |
285 | Lisp_Object prefix, entry; |
286 | Lisp_Object vector; | |
287 | Lisp_Object *subprefix_stack; | |
288 | int submenu_depth = 0; | |
ee78dc32 | 289 | int i; |
fdc12c4d | 290 | |
485015ca | 291 | if (!f) |
014510b0 | 292 | return; |
fe5a0162 | 293 | entry = Qnil; |
663e2b3f | 294 | subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * word_size); |
e69b0960 | 295 | vector = f->menu_bar_vector; |
485015ca GV |
296 | prefix = Qnil; |
297 | i = 0; | |
298 | while (i < f->menu_bar_items_used) | |
299 | { | |
6ed09cf0 | 300 | if (EQ (AREF (vector, i), Qnil)) |
485015ca GV |
301 | { |
302 | subprefix_stack[submenu_depth++] = prefix; | |
303 | prefix = entry; | |
304 | i++; | |
305 | } | |
6ed09cf0 | 306 | else if (EQ (AREF (vector, i), Qlambda)) |
485015ca GV |
307 | { |
308 | prefix = subprefix_stack[--submenu_depth]; | |
309 | i++; | |
310 | } | |
6ed09cf0 | 311 | else if (EQ (AREF (vector, i), Qt)) |
485015ca | 312 | { |
6ed09cf0 | 313 | prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX); |
485015ca GV |
314 | i += MENU_ITEMS_PANE_LENGTH; |
315 | } | |
316 | else | |
317 | { | |
6ed09cf0 | 318 | entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE); |
485015ca GV |
319 | /* The EMACS_INT cast avoids a warning. There's no problem |
320 | as long as pointers have enough bits to hold small integers. */ | |
321 | if ((int) (EMACS_INT) client_data == i) | |
322 | { | |
323 | int j; | |
324 | struct input_event buf; | |
325 | Lisp_Object frame; | |
f456401b | 326 | EVENT_INIT (buf); |
014510b0 | 327 | |
485015ca | 328 | XSETFRAME (frame, f); |
20501278 GM |
329 | buf.kind = MENU_BAR_EVENT; |
330 | buf.frame_or_window = frame; | |
331 | buf.arg = frame; | |
485015ca | 332 | kbd_buffer_store_event (&buf); |
014510b0 | 333 | |
485015ca GV |
334 | for (j = 0; j < submenu_depth; j++) |
335 | if (!NILP (subprefix_stack[j])) | |
336 | { | |
20501278 GM |
337 | buf.kind = MENU_BAR_EVENT; |
338 | buf.frame_or_window = frame; | |
339 | buf.arg = subprefix_stack[j]; | |
485015ca GV |
340 | kbd_buffer_store_event (&buf); |
341 | } | |
fdc12c4d | 342 | |
485015ca GV |
343 | if (!NILP (prefix)) |
344 | { | |
20501278 GM |
345 | buf.kind = MENU_BAR_EVENT; |
346 | buf.frame_or_window = frame; | |
347 | buf.arg = prefix; | |
485015ca GV |
348 | kbd_buffer_store_event (&buf); |
349 | } | |
fdc12c4d | 350 | |
20501278 GM |
351 | buf.kind = MENU_BAR_EVENT; |
352 | buf.frame_or_window = frame; | |
353 | buf.arg = entry; | |
71f6b295 JR |
354 | /* Free memory used by owner-drawn and help-echo strings. */ |
355 | w32_free_menu_strings (FRAME_W32_WINDOW (f)); | |
b2a916a0 JR |
356 | kbd_buffer_store_event (&buf); |
357 | ||
71f6b295 | 358 | f->output_data.w32->menubar_active = 0; |
485015ca GV |
359 | return; |
360 | } | |
361 | i += MENU_ITEMS_ITEM_LENGTH; | |
362 | } | |
014510b0 | 363 | } |
71f6b295 JR |
364 | /* Free memory used by owner-drawn and help-echo strings. */ |
365 | w32_free_menu_strings (FRAME_W32_WINDOW (f)); | |
71f6b295 | 366 | f->output_data.w32->menubar_active = 0; |
485015ca | 367 | } |
014510b0 | 368 | |
ee78dc32 | 369 | \f |
485015ca GV |
370 | /* Set the contents of the menubar widgets of frame F. |
371 | The argument FIRST_TIME is currently ignored; | |
372 | it is set the first time this is called, from initialize_frame_menubar. */ | |
373 | ||
374 | void | |
b56ceb92 | 375 | set_frame_menubar (FRAME_PTR f, int first_time, int deep_p) |
485015ca GV |
376 | { |
377 | HMENU menubar_widget = f->output_data.w32->menubar_widget; | |
162d2499 | 378 | Lisp_Object items; |
485015ca | 379 | widget_value *wv, *first_wv, *prev_wv = 0; |
6ed09cf0 RS |
380 | int i, last_i; |
381 | int *submenu_start, *submenu_end; | |
fe04b8c8 | 382 | int *submenu_top_level_items, *submenu_n_panes; |
485015ca GV |
383 | |
384 | /* We must not change the menubar when actually in use. */ | |
385 | if (f->output_data.w32->menubar_active) | |
386 | return; | |
387 | ||
388 | XSETFRAME (Vmenu_updating_frame, f); | |
389 | ||
390 | if (! menubar_widget) | |
391 | deep_p = 1; | |
392 | else if (pending_menu_activation && !deep_p) | |
393 | deep_p = 1; | |
394 | ||
485015ca GV |
395 | if (deep_p) |
396 | { | |
397 | /* Make a widget-value tree representing the entire menu trees. */ | |
398 | ||
399 | struct buffer *prev = current_buffer; | |
400 | Lisp_Object buffer; | |
d311d28c | 401 | ptrdiff_t specpdl_count = SPECPDL_INDEX (); |
485015ca GV |
402 | int previous_menu_items_used = f->menu_bar_items_used; |
403 | Lisp_Object *previous_items | |
404 | = (Lisp_Object *) alloca (previous_menu_items_used | |
663e2b3f | 405 | * word_size); |
485015ca GV |
406 | |
407 | /* If we are making a new widget, its contents are empty, | |
408 | do always reinitialize them. */ | |
409 | if (! menubar_widget) | |
410 | previous_menu_items_used = 0; | |
411 | ||
d3d50620 | 412 | buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer; |
485015ca GV |
413 | specbind (Qinhibit_quit, Qt); |
414 | /* Don't let the debugger step into this code | |
415 | because it is not reentrant. */ | |
416 | specbind (Qdebug_on_next_call, Qnil); | |
417 | ||
89f2614d KS |
418 | record_unwind_save_match_data (); |
419 | ||
485015ca GV |
420 | if (NILP (Voverriding_local_map_menu_flag)) |
421 | { | |
422 | specbind (Qoverriding_terminal_local_map, Qnil); | |
423 | specbind (Qoverriding_local_map, Qnil); | |
424 | } | |
425 | ||
426 | set_buffer_internal_1 (XBUFFER (buffer)); | |
427 | ||
cc477da7 | 428 | /* Run the hooks. */ |
c53b6500 | 429 | safe_run_hooks (Qactivate_menubar_hook); |
485015ca | 430 | safe_run_hooks (Qmenu_bar_update_hook); |
f00af5b1 | 431 | fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f))); |
485015ca GV |
432 | |
433 | items = FRAME_MENU_BAR_ITEMS (f); | |
434 | ||
485015ca | 435 | /* Save the frame's previous menu bar contents data. */ |
c30dce3d | 436 | if (previous_menu_items_used) |
e69b0960 | 437 | memcpy (previous_items, XVECTOR (f->menu_bar_vector)->contents, |
663e2b3f | 438 | previous_menu_items_used * word_size); |
485015ca | 439 | |
6ed09cf0 RS |
440 | /* Fill in menu_items with the current menu bar contents. |
441 | This can evaluate Lisp code. */ | |
df80fd9d JR |
442 | save_menu_items (); |
443 | ||
e69b0960 | 444 | menu_items = f->menu_bar_vector; |
c30dce3d | 445 | menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0; |
57679c86 AS |
446 | submenu_start = (int *) alloca (ASIZE (items) * sizeof (int)); |
447 | submenu_end = (int *) alloca (ASIZE (items) * sizeof (int)); | |
77b37c05 | 448 | submenu_n_panes = (int *) alloca (ASIZE (items) * sizeof (int)); |
57679c86 | 449 | submenu_top_level_items = (int *) alloca (ASIZE (items) * sizeof (int)); |
485015ca | 450 | init_menu_items (); |
6ed09cf0 | 451 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
452 | { |
453 | Lisp_Object key, string, maps; | |
454 | ||
6ed09cf0 RS |
455 | last_i = i; |
456 | ||
457 | key = AREF (items, i); | |
458 | string = AREF (items, i + 1); | |
459 | maps = AREF (items, i + 2); | |
485015ca GV |
460 | if (NILP (string)) |
461 | break; | |
462 | ||
6ed09cf0 RS |
463 | submenu_start[i] = menu_items_used; |
464 | ||
465 | menu_items_n_panes = 0; | |
466 | submenu_top_level_items[i] | |
467 | = parse_single_submenu (key, string, maps); | |
fe04b8c8 | 468 | submenu_n_panes[i] = menu_items_n_panes; |
6ed09cf0 RS |
469 | |
470 | submenu_end[i] = menu_items_used; | |
471 | } | |
472 | ||
473 | finish_menu_items (); | |
474 | ||
475 | /* Convert menu_items into widget_value trees | |
476 | to display the menu. This cannot evaluate Lisp code. */ | |
477 | ||
478 | wv = xmalloc_widget_value (); | |
479 | wv->name = "menubar"; | |
480 | wv->value = 0; | |
481 | wv->enabled = 1; | |
482 | wv->button_type = BUTTON_TYPE_NONE; | |
483 | wv->help = Qnil; | |
484 | first_wv = wv; | |
485 | ||
486 | for (i = 0; i < last_i; i += 4) | |
487 | { | |
fe04b8c8 | 488 | menu_items_n_panes = submenu_n_panes[i]; |
6ed09cf0 RS |
489 | wv = digest_single_submenu (submenu_start[i], submenu_end[i], |
490 | submenu_top_level_items[i]); | |
177c0ea7 | 491 | if (prev_wv) |
485015ca GV |
492 | prev_wv->next = wv; |
493 | else | |
494 | first_wv->contents = wv; | |
495 | /* Don't set wv->name here; GC during the loop might relocate it. */ | |
496 | wv->enabled = 1; | |
162d2499 | 497 | wv->button_type = BUTTON_TYPE_NONE; |
485015ca GV |
498 | prev_wv = wv; |
499 | } | |
500 | ||
485015ca | 501 | set_buffer_internal_1 (prev); |
485015ca GV |
502 | |
503 | /* If there has been no change in the Lisp-level contents | |
504 | of the menu bar, skip redisplaying it. Just exit. */ | |
505 | ||
506 | for (i = 0; i < previous_menu_items_used; i++) | |
507 | if (menu_items_used == i | |
6ed09cf0 | 508 | || (!EQ (previous_items[i], AREF (menu_items, i)))) |
485015ca GV |
509 | break; |
510 | if (i == menu_items_used && i == previous_menu_items_used && i != 0) | |
511 | { | |
512 | free_menubar_widget_value_tree (first_wv); | |
df80fd9d JR |
513 | discard_menu_items (); |
514 | unbind_to (specpdl_count, Qnil); | |
485015ca GV |
515 | return; |
516 | } | |
517 | ||
f00af5b1 | 518 | fset_menu_bar_vector (f, menu_items); |
df80fd9d JR |
519 | f->menu_bar_items_used = menu_items_used; |
520 | ||
521 | /* This undoes save_menu_items. */ | |
522 | unbind_to (specpdl_count, Qnil); | |
523 | ||
485015ca | 524 | /* Now GC cannot happen during the lifetime of the widget_value, |
ace9b298 JR |
525 | so it's safe to store data from a Lisp_String, as long as |
526 | local copies are made when the actual menu is created. | |
527 | Windows takes care of this for normal string items, but | |
528 | not for owner-drawn items or additional item-info. */ | |
485015ca | 529 | wv = first_wv->contents; |
6ed09cf0 | 530 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
531 | { |
532 | Lisp_Object string; | |
6ed09cf0 | 533 | string = AREF (items, i + 1); |
485015ca GV |
534 | if (NILP (string)) |
535 | break; | |
51b59d79 | 536 | wv->name = SSDATA (string); |
1f06d367 | 537 | update_submenu_strings (wv->contents); |
485015ca GV |
538 | wv = wv->next; |
539 | } | |
485015ca GV |
540 | } |
541 | else | |
542 | { | |
543 | /* Make a widget-value tree containing | |
162d2499 | 544 | just the top level menu bar strings. */ |
485015ca | 545 | |
6ed09cf0 RS |
546 | wv = xmalloc_widget_value (); |
547 | wv->name = "menubar"; | |
548 | wv->value = 0; | |
549 | wv->enabled = 1; | |
550 | wv->button_type = BUTTON_TYPE_NONE; | |
551 | wv->help = Qnil; | |
552 | first_wv = wv; | |
553 | ||
485015ca | 554 | items = FRAME_MENU_BAR_ITEMS (f); |
6ed09cf0 | 555 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
556 | { |
557 | Lisp_Object string; | |
558 | ||
6ed09cf0 | 559 | string = AREF (items, i + 1); |
485015ca GV |
560 | if (NILP (string)) |
561 | break; | |
562 | ||
563 | wv = xmalloc_widget_value (); | |
51b59d79 | 564 | wv->name = SSDATA (string); |
485015ca GV |
565 | wv->value = 0; |
566 | wv->enabled = 1; | |
162d2499 | 567 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 568 | wv->help = Qnil; |
485015ca GV |
569 | /* This prevents lwlib from assuming this |
570 | menu item is really supposed to be empty. */ | |
571 | /* The EMACS_INT cast avoids a warning. | |
572 | This value just has to be different from small integers. */ | |
573 | wv->call_data = (void *) (EMACS_INT) (-1); | |
574 | ||
177c0ea7 | 575 | if (prev_wv) |
485015ca GV |
576 | prev_wv->next = wv; |
577 | else | |
578 | first_wv->contents = wv; | |
579 | prev_wv = wv; | |
580 | } | |
581 | ||
162d2499 JR |
582 | /* Forget what we thought we knew about what is in the |
583 | detailed contents of the menu bar menus. | |
584 | Changing the top level always destroys the contents. */ | |
585 | f->menu_bar_items_used = 0; | |
485015ca GV |
586 | } |
587 | ||
588 | /* Create or update the menu bar widget. */ | |
ee78dc32 | 589 | |
485015ca | 590 | BLOCK_INPUT; |
ee78dc32 | 591 | |
485015ca GV |
592 | if (menubar_widget) |
593 | { | |
594 | /* Empty current menubar, rather than creating a fresh one. */ | |
595 | while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION)) | |
596 | ; | |
597 | } | |
598 | else | |
599 | { | |
600 | menubar_widget = CreateMenu (); | |
601 | } | |
602 | fill_in_menu (menubar_widget, first_wv->contents); | |
ee78dc32 | 603 | |
485015ca | 604 | free_menubar_widget_value_tree (first_wv); |
ee78dc32 | 605 | |
485015ca GV |
606 | { |
607 | HMENU old_widget = f->output_data.w32->menubar_widget; | |
ee78dc32 | 608 | |
485015ca GV |
609 | f->output_data.w32->menubar_widget = menubar_widget; |
610 | SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget); | |
177c0ea7 | 611 | /* Causes flicker when menu bar is updated |
485015ca | 612 | DrawMenuBar (FRAME_W32_WINDOW (f)); */ |
ee78dc32 | 613 | |
485015ca GV |
614 | /* Force the window size to be recomputed so that the frame's text |
615 | area remains the same, if menubar has just been created. */ | |
616 | if (old_widget == NULL) | |
90022f5a | 617 | x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f)); |
ee78dc32 | 618 | } |
485015ca GV |
619 | |
620 | UNBLOCK_INPUT; | |
ee78dc32 GV |
621 | } |
622 | ||
485015ca GV |
623 | /* Called from Fx_create_frame to create the initial menubar of a frame |
624 | before it is mapped, so that the window is mapped with the menubar already | |
625 | there instead of us tacking it on later and thrashing the window after it | |
626 | is visible. */ | |
627 | ||
628 | void | |
b56ceb92 | 629 | initialize_frame_menubar (FRAME_PTR f) |
485015ca GV |
630 | { |
631 | /* This function is called before the first chance to redisplay | |
632 | the frame. It has to be, so the frame will have the right size. */ | |
f00af5b1 | 633 | fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f))); |
485015ca | 634 | set_frame_menubar (f, 1, 1); |
ee78dc32 GV |
635 | } |
636 | ||
485015ca GV |
637 | /* Get rid of the menu bar of frame F, and free its storage. |
638 | This is used when deleting a frame, and when turning off the menu bar. */ | |
639 | ||
640 | void | |
b56ceb92 | 641 | free_frame_menubar (FRAME_PTR f) |
485015ca GV |
642 | { |
643 | BLOCK_INPUT; | |
644 | ||
645 | { | |
646 | HMENU old = GetMenu (FRAME_W32_WINDOW (f)); | |
647 | SetMenu (FRAME_W32_WINDOW (f), NULL); | |
648 | f->output_data.w32->menubar_widget = NULL; | |
649 | DestroyMenu (old); | |
650 | } | |
29250851 | 651 | |
485015ca GV |
652 | UNBLOCK_INPUT; |
653 | } | |
ee78dc32 | 654 | |
485015ca GV |
655 | \f |
656 | /* w32_menu_show actually displays a menu using the panes and items in | |
657 | menu_items and returns the value selected from it; we assume input | |
658 | is blocked by the caller. */ | |
ee78dc32 GV |
659 | |
660 | /* F is the frame the menu is for. | |
661 | X and Y are the frame-relative specified position, | |
662 | relative to the inside upper left corner of the frame F. | |
485015ca | 663 | FOR_CLICK is nonzero if this menu was invoked for a mouse click. |
ee78dc32 | 664 | KEYMAPS is 1 if this menu was specified with keymaps; |
485015ca GV |
665 | in that case, we return a list containing the chosen item's value |
666 | and perhaps also the pane's prefix. | |
ee78dc32 GV |
667 | TITLE is the specified menu title. |
668 | ERROR is a place to store an error message string in case of failure. | |
669 | (We return nil on failure, but the value doesn't actually matter.) */ | |
670 | ||
ef7417fd SM |
671 | Lisp_Object |
672 | w32_menu_show (FRAME_PTR f, int x, int y, int for_click, int keymaps, | |
42ca4633 | 673 | Lisp_Object title, const char **error) |
ee78dc32 | 674 | { |
485015ca GV |
675 | int i; |
676 | int menu_item_selection; | |
677 | HMENU menu; | |
ee78dc32 | 678 | POINT pos; |
485015ca GV |
679 | widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; |
680 | widget_value **submenu_stack | |
681 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
682 | Lisp_Object *subprefix_stack | |
663e2b3f | 683 | = (Lisp_Object *) alloca (menu_items_used * word_size); |
485015ca | 684 | int submenu_depth = 0; |
485015ca | 685 | int first_pane; |
485015ca | 686 | |
ee78dc32 | 687 | *error = NULL; |
485015ca | 688 | |
df80fd9d JR |
689 | if (menu_items_n_panes == 0) |
690 | return Qnil; | |
691 | ||
485015ca | 692 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) |
ee78dc32 GV |
693 | { |
694 | *error = "Empty menu"; | |
695 | return Qnil; | |
696 | } | |
485015ca GV |
697 | |
698 | /* Create a tree of widget_value objects | |
699 | representing the panes and their items. */ | |
700 | wv = xmalloc_widget_value (); | |
701 | wv->name = "menu"; | |
702 | wv->value = 0; | |
703 | wv->enabled = 1; | |
162d2499 | 704 | wv->button_type = BUTTON_TYPE_NONE; |
e6bb685b | 705 | wv->help = Qnil; |
485015ca GV |
706 | first_wv = wv; |
707 | first_pane = 1; | |
177c0ea7 | 708 | |
485015ca GV |
709 | /* Loop over all panes and items, filling in the tree. */ |
710 | i = 0; | |
711 | while (i < menu_items_used) | |
712 | { | |
6ed09cf0 | 713 | if (EQ (AREF (menu_items, i), Qnil)) |
485015ca GV |
714 | { |
715 | submenu_stack[submenu_depth++] = save_wv; | |
716 | save_wv = prev_wv; | |
717 | prev_wv = 0; | |
718 | first_pane = 1; | |
719 | i++; | |
720 | } | |
6ed09cf0 | 721 | else if (EQ (AREF (menu_items, i), Qlambda)) |
485015ca GV |
722 | { |
723 | prev_wv = save_wv; | |
724 | save_wv = submenu_stack[--submenu_depth]; | |
725 | first_pane = 0; | |
726 | i++; | |
727 | } | |
6ed09cf0 | 728 | else if (EQ (AREF (menu_items, i), Qt) |
485015ca GV |
729 | && submenu_depth != 0) |
730 | i += MENU_ITEMS_PANE_LENGTH; | |
731 | /* Ignore a nil in the item list. | |
732 | It's meaningful only for dialog boxes. */ | |
6ed09cf0 | 733 | else if (EQ (AREF (menu_items, i), Qquote)) |
485015ca | 734 | i += 1; |
6ed09cf0 | 735 | else if (EQ (AREF (menu_items, i), Qt)) |
485015ca GV |
736 | { |
737 | /* Create a new pane. */ | |
738 | Lisp_Object pane_name, prefix; | |
739 | char *pane_string; | |
29250851 JR |
740 | pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME); |
741 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); | |
1f06d367 JR |
742 | |
743 | if (STRINGP (pane_name)) | |
29250851 | 744 | { |
1f06d367 JR |
745 | if (unicode_append_menu) |
746 | pane_name = ENCODE_UTF_8 (pane_name); | |
747 | else if (STRING_MULTIBYTE (pane_name)) | |
748 | pane_name = ENCODE_SYSTEM (pane_name); | |
749 | ||
6ed09cf0 | 750 | ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name); |
29250851 | 751 | } |
1f06d367 | 752 | |
485015ca | 753 | pane_string = (NILP (pane_name) |
51b59d79 | 754 | ? "" : SSDATA (pane_name)); |
485015ca GV |
755 | /* If there is just one top-level pane, put all its items directly |
756 | under the top-level menu. */ | |
757 | if (menu_items_n_panes == 1) | |
758 | pane_string = ""; | |
759 | ||
760 | /* If the pane has a meaningful name, | |
761 | make the pane a top-level menu item | |
762 | with its items as a submenu beneath it. */ | |
763 | if (!keymaps && strcmp (pane_string, "")) | |
764 | { | |
765 | wv = xmalloc_widget_value (); | |
766 | if (save_wv) | |
767 | save_wv->next = wv; | |
768 | else | |
769 | first_wv->contents = wv; | |
770 | wv->name = pane_string; | |
771 | if (keymaps && !NILP (prefix)) | |
772 | wv->name++; | |
773 | wv->value = 0; | |
774 | wv->enabled = 1; | |
162d2499 | 775 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 776 | wv->help = Qnil; |
485015ca GV |
777 | save_wv = wv; |
778 | prev_wv = 0; | |
779 | } | |
780 | else if (first_pane) | |
781 | { | |
782 | save_wv = wv; | |
783 | prev_wv = 0; | |
784 | } | |
785 | first_pane = 0; | |
786 | i += MENU_ITEMS_PANE_LENGTH; | |
787 | } | |
788 | else | |
789 | { | |
790 | /* Create a new item within current pane. */ | |
b65a2f83 | 791 | Lisp_Object item_name, enable, descrip, def, type, selected, help; |
b65a2f83 | 792 | |
29250851 JR |
793 | item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); |
794 | enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); | |
795 | descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); | |
796 | def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION); | |
797 | type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE); | |
798 | selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED); | |
799 | help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP); | |
162d2499 | 800 | |
1f06d367 | 801 | if (STRINGP (item_name)) |
29250851 | 802 | { |
1f06d367 JR |
803 | if (unicode_append_menu) |
804 | item_name = ENCODE_UTF_8 (item_name); | |
805 | else if (STRING_MULTIBYTE (item_name)) | |
806 | item_name = ENCODE_SYSTEM (item_name); | |
807 | ||
6ed09cf0 | 808 | ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name); |
29250851 | 809 | } |
1f06d367 JR |
810 | |
811 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) | |
29250851 JR |
812 | { |
813 | descrip = ENCODE_SYSTEM (descrip); | |
6ed09cf0 | 814 | ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip); |
29250851 | 815 | } |
485015ca GV |
816 | |
817 | wv = xmalloc_widget_value (); | |
177c0ea7 | 818 | if (prev_wv) |
485015ca | 819 | prev_wv->next = wv; |
177c0ea7 | 820 | else |
485015ca | 821 | save_wv->contents = wv; |
51b59d79 | 822 | wv->name = SSDATA (item_name); |
485015ca | 823 | if (!NILP (descrip)) |
51b59d79 | 824 | wv->key = SSDATA (descrip); |
485015ca GV |
825 | wv->value = 0; |
826 | /* Use the contents index as call_data, since we are | |
2cd23960 | 827 | restricted to 16-bits. */ |
e59fe83b | 828 | wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0; |
485015ca | 829 | wv->enabled = !NILP (enable); |
162d2499 JR |
830 | |
831 | if (NILP (type)) | |
832 | wv->button_type = BUTTON_TYPE_NONE; | |
833 | else if (EQ (type, QCtoggle)) | |
834 | wv->button_type = BUTTON_TYPE_TOGGLE; | |
835 | else if (EQ (type, QCradio)) | |
836 | wv->button_type = BUTTON_TYPE_RADIO; | |
837 | else | |
838 | abort (); | |
839 | ||
840 | wv->selected = !NILP (selected); | |
df80fd9d | 841 | |
d1fffd1d JR |
842 | if (!STRINGP (help)) |
843 | help = Qnil; | |
844 | ||
845 | wv->help = help; | |
7d8e57da | 846 | |
485015ca GV |
847 | prev_wv = wv; |
848 | ||
849 | i += MENU_ITEMS_ITEM_LENGTH; | |
850 | } | |
851 | } | |
852 | ||
853 | /* Deal with the title, if it is non-nil. */ | |
854 | if (!NILP (title)) | |
855 | { | |
856 | widget_value *wv_title = xmalloc_widget_value (); | |
857 | widget_value *wv_sep = xmalloc_widget_value (); | |
858 | ||
859 | /* Maybe replace this separator with a bitmap or owner-draw item | |
860 | so that it looks better. Having two separators looks odd. */ | |
861 | wv_sep->name = "--"; | |
862 | wv_sep->next = first_wv->contents; | |
2d10309f | 863 | wv_sep->help = Qnil; |
485015ca | 864 | |
1f06d367 JR |
865 | if (unicode_append_menu) |
866 | title = ENCODE_UTF_8 (title); | |
867 | else if (STRING_MULTIBYTE (title)) | |
6915ded0 | 868 | title = ENCODE_SYSTEM (title); |
1f06d367 | 869 | |
51b59d79 | 870 | wv_title->name = SSDATA (title); |
02067692 | 871 | wv_title->enabled = TRUE; |
5b20b3cc | 872 | wv_title->title = TRUE; |
162d2499 | 873 | wv_title->button_type = BUTTON_TYPE_NONE; |
2d10309f | 874 | wv_title->help = Qnil; |
485015ca GV |
875 | wv_title->next = wv_sep; |
876 | first_wv->contents = wv_title; | |
877 | } | |
878 | ||
df80fd9d JR |
879 | /* No selection has been chosen yet. */ |
880 | menu_item_selection = 0; | |
881 | ||
485015ca | 882 | /* Actually create the menu. */ |
ace9b298 | 883 | current_popup_menu = menu = CreatePopupMenu (); |
485015ca | 884 | fill_in_menu (menu, first_wv->contents); |
162d2499 | 885 | |
485015ca | 886 | /* Adjust coordinates to be root-window-relative. */ |
ee78dc32 GV |
887 | pos.x = x; |
888 | pos.y = y; | |
fbd6baed | 889 | ClientToScreen (FRAME_W32_WINDOW (f), &pos); |
485015ca | 890 | |
ee78dc32 | 891 | /* Display the menu. */ |
177c0ea7 | 892 | menu_item_selection = SendMessage (FRAME_W32_WINDOW (f), |
485015ca GV |
893 | WM_EMACS_TRACKPOPUPMENU, |
894 | (WPARAM)menu, (LPARAM)&pos); | |
014510b0 GV |
895 | |
896 | /* Clean up extraneous mouse events which might have been generated | |
897 | during the call. */ | |
898 | discard_mouse_events (); | |
38f2b112 | 899 | FRAME_X_DISPLAY_INFO (f)->grabbed = 0; |
014510b0 | 900 | |
86fb1a79 JR |
901 | /* Free the widget_value objects we used to specify the contents. */ |
902 | free_menubar_widget_value_tree (first_wv); | |
903 | ||
485015ca GV |
904 | DestroyMenu (menu); |
905 | ||
563cd4c0 JR |
906 | /* Free the owner-drawn and help-echo menu strings. */ |
907 | w32_free_menu_strings (FRAME_W32_WINDOW (f)); | |
5339f50c | 908 | f->output_data.w32->menubar_active = 0; |
563cd4c0 | 909 | |
ee78dc32 GV |
910 | /* Find the selected item, and its pane, to return |
911 | the proper value. */ | |
485015ca | 912 | if (menu_item_selection != 0) |
ee78dc32 | 913 | { |
485015ca GV |
914 | Lisp_Object prefix, entry; |
915 | ||
fe5a0162 | 916 | prefix = entry = Qnil; |
485015ca GV |
917 | i = 0; |
918 | while (i < menu_items_used) | |
919 | { | |
6ed09cf0 | 920 | if (EQ (AREF (menu_items, i), Qnil)) |
485015ca GV |
921 | { |
922 | subprefix_stack[submenu_depth++] = prefix; | |
923 | prefix = entry; | |
924 | i++; | |
925 | } | |
6ed09cf0 | 926 | else if (EQ (AREF (menu_items, i), Qlambda)) |
485015ca GV |
927 | { |
928 | prefix = subprefix_stack[--submenu_depth]; | |
929 | i++; | |
930 | } | |
6ed09cf0 | 931 | else if (EQ (AREF (menu_items, i), Qt)) |
485015ca | 932 | { |
6ed09cf0 | 933 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); |
485015ca GV |
934 | i += MENU_ITEMS_PANE_LENGTH; |
935 | } | |
936 | /* Ignore a nil in the item list. | |
937 | It's meaningful only for dialog boxes. */ | |
6ed09cf0 | 938 | else if (EQ (AREF (menu_items, i), Qquote)) |
485015ca GV |
939 | i += 1; |
940 | else | |
941 | { | |
6ed09cf0 | 942 | entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE); |
485015ca GV |
943 | if (menu_item_selection == i) |
944 | { | |
945 | if (keymaps != 0) | |
946 | { | |
947 | int j; | |
948 | ||
949 | entry = Fcons (entry, Qnil); | |
950 | if (!NILP (prefix)) | |
951 | entry = Fcons (prefix, entry); | |
952 | for (j = submenu_depth - 1; j >= 0; j--) | |
953 | if (!NILP (subprefix_stack[j])) | |
954 | entry = Fcons (subprefix_stack[j], entry); | |
955 | } | |
956 | return entry; | |
957 | } | |
958 | i += MENU_ITEMS_ITEM_LENGTH; | |
959 | } | |
960 | } | |
ee78dc32 | 961 | } |
f3e0a6de JR |
962 | else if (!for_click) |
963 | /* Make "Cancel" equivalent to C-g. */ | |
964 | Fsignal (Qquit, Qnil); | |
014510b0 | 965 | |
ee78dc32 GV |
966 | return Qnil; |
967 | } | |
485015ca | 968 | \f |
ee78dc32 | 969 | |
ace9b298 | 970 | #ifdef HAVE_DIALOGS |
31403b24 JR |
971 | /* TODO: On Windows, there are two ways of defining a dialog. |
972 | ||
973 | 1. Create a predefined dialog resource and include it in nt/emacs.rc. | |
974 | Using this method, we could then set the titles and make unneeded | |
975 | buttons invisible before displaying the dialog. Everything would | |
976 | be a fixed size though, so there is a risk that text does not | |
977 | fit on a button. | |
978 | 2. Create the dialog template in memory on the fly. This allows us | |
979 | to size the dialog and buttons dynamically, probably giving more | |
980 | natural looking results for dialogs with few buttons, and eliminating | |
981 | the problem of text overflowing the buttons. But the API for this is | |
982 | quite complex - structures have to be allocated in particular ways, | |
983 | text content is tacked onto the end of structures in variable length | |
984 | arrays with further structures tacked on after these, there are | |
985 | certain alignment requirements for all this, and we have to | |
986 | measure all the text and convert to "dialog coordinates" to figure | |
987 | out how big to make everything. | |
988 | ||
989 | For now, we'll just stick with menus for dialogs that are more | |
990 | complicated than simple yes/no type questions for which we can use | |
991 | the MessageBox function. | |
992 | */ | |
38fecd52 | 993 | |
485015ca | 994 | static char * button_names [] = { |
ee78dc32 | 995 | "button1", "button2", "button3", "button4", "button5", |
485015ca | 996 | "button6", "button7", "button8", "button9", "button10" }; |
ee78dc32 GV |
997 | |
998 | static Lisp_Object | |
b56ceb92 JB |
999 | w32_dialog_show (FRAME_PTR f, int keymaps, |
1000 | Lisp_Object title, Lisp_Object header, | |
1001 | char **error) | |
ee78dc32 | 1002 | { |
9d4f32e8 | 1003 | int i, nb_buttons = 0; |
ee78dc32 | 1004 | char dialog_name[6]; |
485015ca GV |
1005 | int menu_item_selection; |
1006 | ||
fe5a0162 | 1007 | widget_value *wv, *first_wv = 0, *prev_wv = 0; |
485015ca | 1008 | |
ee78dc32 GV |
1009 | /* Number of elements seen so far, before boundary. */ |
1010 | int left_count = 0; | |
1011 | /* 1 means we've seen the boundary between left-hand elts and right-hand. */ | |
1012 | int boundary_seen = 0; | |
485015ca | 1013 | |
ee78dc32 | 1014 | *error = NULL; |
485015ca | 1015 | |
ee78dc32 GV |
1016 | if (menu_items_n_panes > 1) |
1017 | { | |
1018 | *error = "Multiple panes in dialog box"; | |
1019 | return Qnil; | |
1020 | } | |
485015ca | 1021 | |
ee78dc32 GV |
1022 | /* Create a tree of widget_value objects |
1023 | representing the text label and buttons. */ | |
1024 | { | |
1025 | Lisp_Object pane_name, prefix; | |
1026 | char *pane_string; | |
6ed09cf0 RS |
1027 | pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME); |
1028 | prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX); | |
ee78dc32 | 1029 | pane_string = (NILP (pane_name) |
51b59d79 | 1030 | ? "" : SSDATA (pane_name)); |
485015ca | 1031 | prev_wv = xmalloc_widget_value (); |
ee78dc32 GV |
1032 | prev_wv->value = pane_string; |
1033 | if (keymaps && !NILP (prefix)) | |
1034 | prev_wv->name++; | |
1035 | prev_wv->enabled = 1; | |
1036 | prev_wv->name = "message"; | |
2d10309f | 1037 | prev_wv->help = Qnil; |
ee78dc32 | 1038 | first_wv = prev_wv; |
177c0ea7 | 1039 | |
ee78dc32 GV |
1040 | /* Loop over all panes and items, filling in the tree. */ |
1041 | i = MENU_ITEMS_PANE_LENGTH; | |
1042 | while (i < menu_items_used) | |
1043 | { | |
177c0ea7 | 1044 | |
ee78dc32 | 1045 | /* Create a new item within current pane. */ |
b65a2f83 | 1046 | Lisp_Object item_name, enable, descrip, help; |
b65a2f83 | 1047 | |
6ed09cf0 RS |
1048 | item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); |
1049 | enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); | |
1050 | descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); | |
1051 | help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP); | |
177c0ea7 | 1052 | |
ee78dc32 GV |
1053 | if (NILP (item_name)) |
1054 | { | |
1055 | free_menubar_widget_value_tree (first_wv); | |
1056 | *error = "Submenu in dialog items"; | |
1057 | return Qnil; | |
1058 | } | |
1059 | if (EQ (item_name, Qquote)) | |
1060 | { | |
1061 | /* This is the boundary between left-side elts | |
1062 | and right-side elts. Stop incrementing right_count. */ | |
1063 | boundary_seen = 1; | |
1064 | i++; | |
1065 | continue; | |
1066 | } | |
485015ca | 1067 | if (nb_buttons >= 9) |
ee78dc32 GV |
1068 | { |
1069 | free_menubar_widget_value_tree (first_wv); | |
1070 | *error = "Too many dialog items"; | |
1071 | return Qnil; | |
1072 | } | |
485015ca GV |
1073 | |
1074 | wv = xmalloc_widget_value (); | |
ee78dc32 GV |
1075 | prev_wv->next = wv; |
1076 | wv->name = (char *) button_names[nb_buttons]; | |
1077 | if (!NILP (descrip)) | |
51b59d79 PE |
1078 | wv->key = SSDATA (descrip); |
1079 | wv->value = SSDATA (item_name); | |
4939150c | 1080 | wv->call_data = aref_addr (menu_items, i); |
ee78dc32 | 1081 | wv->enabled = !NILP (enable); |
2d10309f | 1082 | wv->help = Qnil; |
ee78dc32 | 1083 | prev_wv = wv; |
485015ca | 1084 | |
ee78dc32 GV |
1085 | if (! boundary_seen) |
1086 | left_count++; | |
485015ca | 1087 | |
ee78dc32 GV |
1088 | nb_buttons++; |
1089 | i += MENU_ITEMS_ITEM_LENGTH; | |
1090 | } | |
485015ca | 1091 | |
ee78dc32 GV |
1092 | /* If the boundary was not specified, |
1093 | by default put half on the left and half on the right. */ | |
1094 | if (! boundary_seen) | |
1095 | left_count = nb_buttons - nb_buttons / 2; | |
485015ca GV |
1096 | |
1097 | wv = xmalloc_widget_value (); | |
ee78dc32 | 1098 | wv->name = dialog_name; |
2d10309f | 1099 | wv->help = Qnil; |
485015ca | 1100 | |
d23928c7 NR |
1101 | /* Frame title: 'Q' = Question, 'I' = Information. |
1102 | Can also have 'E' = Error if, one day, we want | |
1103 | a popup for errors. */ | |
ed3751c8 | 1104 | if (NILP (header)) |
d23928c7 NR |
1105 | dialog_name[0] = 'Q'; |
1106 | else | |
1107 | dialog_name[0] = 'I'; | |
1108 | ||
ee78dc32 GV |
1109 | /* Dialog boxes use a really stupid name encoding |
1110 | which specifies how many buttons to use | |
d23928c7 | 1111 | and how many buttons are on the right. */ |
ee78dc32 GV |
1112 | dialog_name[1] = '0' + nb_buttons; |
1113 | dialog_name[2] = 'B'; | |
1114 | dialog_name[3] = 'R'; | |
1115 | /* Number of buttons to put on the right. */ | |
1116 | dialog_name[4] = '0' + nb_buttons - left_count; | |
1117 | dialog_name[5] = 0; | |
1118 | wv->contents = first_wv; | |
1119 | first_wv = wv; | |
1120 | } | |
485015ca | 1121 | |
ee78dc32 | 1122 | /* Actually create the dialog. */ |
485015ca | 1123 | dialog_id = widget_id_tick++; |
ee78dc32 | 1124 | menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv, |
fbd6baed | 1125 | f->output_data.w32->widget, 1, 0, |
ee78dc32 | 1126 | dialog_selection_callback, 0); |
02067692 | 1127 | lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE); |
485015ca | 1128 | |
ee78dc32 GV |
1129 | /* Free the widget_value objects we used to specify the contents. */ |
1130 | free_menubar_widget_value_tree (first_wv); | |
485015ca | 1131 | |
ee78dc32 GV |
1132 | /* No selection has been chosen yet. */ |
1133 | menu_item_selection = 0; | |
485015ca | 1134 | |
ee78dc32 GV |
1135 | /* Display the menu. */ |
1136 | lw_pop_up_all_widgets (dialog_id); | |
485015ca | 1137 | |
ee78dc32 | 1138 | /* Process events that apply to the menu. */ |
485015ca | 1139 | popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id); |
ee78dc32 | 1140 | |
177c0ea7 | 1141 | lw_destroy_all_widgets (dialog_id); |
ee78dc32 | 1142 | |
ee78dc32 GV |
1143 | /* Find the selected item, and its pane, to return |
1144 | the proper value. */ | |
1145 | if (menu_item_selection != 0) | |
1146 | { | |
1147 | Lisp_Object prefix; | |
485015ca | 1148 | |
ee78dc32 GV |
1149 | prefix = Qnil; |
1150 | i = 0; | |
1151 | while (i < menu_items_used) | |
1152 | { | |
1153 | Lisp_Object entry; | |
1154 | ||
6ed09cf0 | 1155 | if (EQ (AREF (menu_items, i), Qt)) |
ee78dc32 | 1156 | { |
6ed09cf0 | 1157 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); |
ee78dc32 GV |
1158 | i += MENU_ITEMS_PANE_LENGTH; |
1159 | } | |
1160 | else | |
1161 | { | |
6ed09cf0 | 1162 | entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE); |
485015ca | 1163 | if (menu_item_selection == i) |
ee78dc32 GV |
1164 | { |
1165 | if (keymaps != 0) | |
1166 | { | |
1167 | entry = Fcons (entry, Qnil); | |
1168 | if (!NILP (prefix)) | |
1169 | entry = Fcons (prefix, entry); | |
1170 | } | |
1171 | return entry; | |
1172 | } | |
1173 | i += MENU_ITEMS_ITEM_LENGTH; | |
1174 | } | |
1175 | } | |
1176 | } | |
f3e0a6de JR |
1177 | else |
1178 | /* Make "Cancel" equivalent to C-g. */ | |
1179 | Fsignal (Qquit, Qnil); | |
485015ca | 1180 | |
ee78dc32 GV |
1181 | return Qnil; |
1182 | } | |
31403b24 JR |
1183 | #else /* !HAVE_DIALOGS */ |
1184 | ||
1185 | /* Currently we only handle Yes No dialogs (y-or-n-p and yes-or-no-p) as | |
1186 | simple dialogs. We could handle a few more, but I'm not aware of | |
1187 | anywhere in Emacs that uses the other specific dialog choices that | |
1188 | MessageBox provides. */ | |
1189 | ||
b56ceb92 JB |
1190 | static int |
1191 | is_simple_dialog (Lisp_Object contents) | |
31403b24 | 1192 | { |
3b0512a3 | 1193 | Lisp_Object options; |
31403b24 JR |
1194 | Lisp_Object name, yes, no, other; |
1195 | ||
3b0512a3 AS |
1196 | if (!CONSP (contents)) |
1197 | return 0; | |
1198 | options = XCDR (contents); | |
1199 | ||
31403b24 JR |
1200 | yes = build_string ("Yes"); |
1201 | no = build_string ("No"); | |
1202 | ||
1203 | if (!CONSP (options)) | |
1204 | return 0; | |
1205 | ||
3b0512a3 AS |
1206 | name = XCAR (options); |
1207 | if (!CONSP (name)) | |
31403b24 | 1208 | return 0; |
3b0512a3 | 1209 | name = XCAR (name); |
31403b24 JR |
1210 | |
1211 | if (!NILP (Fstring_equal (name, yes))) | |
1212 | other = no; | |
1213 | else if (!NILP (Fstring_equal (name, no))) | |
1214 | other = yes; | |
1215 | else | |
1216 | return 0; | |
1217 | ||
1218 | options = XCDR (options); | |
1219 | if (!CONSP (options)) | |
1220 | return 0; | |
1221 | ||
3b0512a3 AS |
1222 | name = XCAR (options); |
1223 | if (!CONSP (name)) | |
1224 | return 0; | |
1225 | name = XCAR (name); | |
31403b24 JR |
1226 | if (NILP (Fstring_equal (name, other))) |
1227 | return 0; | |
1228 | ||
1229 | /* Check there are no more options. */ | |
1230 | options = XCDR (options); | |
1231 | return !(CONSP (options)); | |
1232 | } | |
1233 | ||
b56ceb92 JB |
1234 | static Lisp_Object |
1235 | simple_dialog_show (FRAME_PTR f, Lisp_Object contents, Lisp_Object header) | |
31403b24 JR |
1236 | { |
1237 | int answer; | |
1238 | UINT type; | |
31403b24 JR |
1239 | Lisp_Object lispy_answer = Qnil, temp = XCAR (contents); |
1240 | ||
1c9b4129 JR |
1241 | type = MB_YESNO; |
1242 | ||
1243 | /* Since we only handle Yes/No dialogs, and we already checked | |
1244 | is_simple_dialog, we don't need to worry about checking contents | |
1245 | to see what type of dialog to use. */ | |
31403b24 | 1246 | |
fe7a3057 | 1247 | /* Use Unicode if possible, so any language can be displayed. */ |
1c9b4129 | 1248 | if (unicode_message_box) |
31403b24 | 1249 | { |
1c9b4129 | 1250 | WCHAR *text, *title; |
8bc53d00 | 1251 | USE_SAFE_ALLOCA; |
1c9b4129 JR |
1252 | |
1253 | if (STRINGP (temp)) | |
1254 | { | |
1255 | char *utf8_text = SDATA (ENCODE_UTF_8 (temp)); | |
1256 | /* Be pessimistic about the number of characters needed. | |
1257 | Remember characters outside the BMP will take more than | |
1258 | one utf16 word, so we cannot simply use the character | |
1259 | length of temp. */ | |
1260 | int utf8_len = strlen (utf8_text); | |
98c6f1e3 | 1261 | text = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR)); |
1c9b4129 JR |
1262 | utf8to16 (utf8_text, utf8_len, text); |
1263 | } | |
1264 | else | |
1265 | { | |
1266 | text = L""; | |
1267 | } | |
1268 | ||
1269 | if (NILP (header)) | |
1270 | { | |
1271 | title = L"Question"; | |
1272 | type |= MB_ICONQUESTION; | |
1273 | } | |
1274 | else | |
1275 | { | |
1276 | title = L"Information"; | |
1277 | type |= MB_ICONINFORMATION; | |
1278 | } | |
1279 | ||
1280 | answer = unicode_message_box (FRAME_W32_WINDOW (f), text, title, type); | |
8bc53d00 | 1281 | SAFE_FREE (); |
31403b24 JR |
1282 | } |
1283 | else | |
1284 | { | |
1c9b4129 | 1285 | char *text, *title; |
31403b24 | 1286 | |
1c9b4129 JR |
1287 | /* Fall back on ANSI message box, but at least use system |
1288 | encoding so questions representable by the system codepage | |
1289 | are encoded properly. */ | |
1290 | if (STRINGP (temp)) | |
1291 | text = SDATA (ENCODE_SYSTEM (temp)); | |
1292 | else | |
1293 | text = ""; | |
1294 | ||
1295 | if (NILP (header)) | |
1296 | { | |
1297 | title = "Question"; | |
1298 | type |= MB_ICONQUESTION; | |
1299 | } | |
1300 | else | |
1301 | { | |
1302 | title = "Information"; | |
1303 | type |= MB_ICONINFORMATION; | |
1304 | } | |
1305 | ||
1306 | answer = MessageBox (FRAME_W32_WINDOW (f), text, title, type); | |
1307 | } | |
31403b24 JR |
1308 | |
1309 | if (answer == IDYES) | |
1310 | lispy_answer = build_string ("Yes"); | |
1311 | else if (answer == IDNO) | |
1312 | lispy_answer = build_string ("No"); | |
1313 | else | |
1314 | Fsignal (Qquit, Qnil); | |
1315 | ||
1316 | for (temp = XCDR (contents); CONSP (temp); temp = XCDR (temp)) | |
1317 | { | |
1318 | Lisp_Object item, name, value; | |
1319 | item = XCAR (temp); | |
1320 | if (CONSP (item)) | |
1321 | { | |
1322 | name = XCAR (item); | |
1323 | value = XCDR (item); | |
1324 | } | |
1325 | else | |
1326 | { | |
1327 | name = item; | |
1328 | value = Qnil; | |
1329 | } | |
1330 | ||
1331 | if (!NILP (Fstring_equal (name, lispy_answer))) | |
1332 | { | |
1333 | return value; | |
1334 | } | |
1335 | } | |
1336 | Fsignal (Qquit, Qnil); | |
1337 | return Qnil; | |
1338 | } | |
1339 | #endif /* !HAVE_DIALOGS */ | |
485015ca GV |
1340 | \f |
1341 | ||
1f06d367 JR |
1342 | /* UTF8: 0xxxxxxx, 110xxxxx 10xxxxxx, 1110xxxx, 10xxxxxx, 10xxxxxx */ |
1343 | static void | |
1344 | utf8to16 (unsigned char * src, int len, WCHAR * dest) | |
1345 | { | |
1346 | while (len > 0) | |
1347 | { | |
1f06d367 JR |
1348 | if (*src < 0x80) |
1349 | { | |
1350 | *dest = (WCHAR) *src; | |
1351 | dest++; src++; len--; | |
1352 | } | |
1353 | /* Since we might get >3 byte sequences which we don't handle, ignore the extra parts. */ | |
1354 | else if (*src < 0xC0) | |
1355 | { | |
1356 | src++; len--; | |
1357 | } | |
1358 | /* 2 char UTF-8 sequence. */ | |
1359 | else if (*src < 0xE0) | |
1360 | { | |
1361 | *dest = (WCHAR) (((*src & 0x1f) << 6) | |
1362 | | (*(src + 1) & 0x3f)); | |
1363 | src += 2; len -= 2; dest++; | |
1364 | } | |
1365 | else if (*src < 0xF0) | |
1366 | { | |
1367 | *dest = (WCHAR) (((*src & 0x0f) << 12) | |
1368 | | ((*(src + 1) & 0x3f) << 6) | |
1369 | | (*(src + 2) & 0x3f)); | |
1370 | src += 3; len -= 3; dest++; | |
1371 | } | |
1372 | else /* Not encodable. Insert Unicode Substitution char. */ | |
1373 | { | |
1374 | *dest = (WCHAR) 0xfffd; | |
1375 | src++; len--; dest++; | |
1376 | } | |
1377 | } | |
1378 | *dest = 0; | |
1379 | } | |
1380 | ||
485015ca GV |
1381 | static int |
1382 | add_menu_item (HMENU menu, widget_value *wv, HMENU item) | |
1383 | { | |
1384 | UINT fuFlags; | |
af41f8a8 | 1385 | char *out_string, *p, *q; |
37ad8b63 | 1386 | int return_value; |
af41f8a8 | 1387 | size_t nlen, orig_len; |
8bc53d00 | 1388 | USE_SAFE_ALLOCA; |
485015ca | 1389 | |
4039c786 | 1390 | if (menu_separator_name_p (wv->name)) |
222456a1 JR |
1391 | { |
1392 | fuFlags = MF_SEPARATOR; | |
1393 | out_string = NULL; | |
1394 | } | |
177c0ea7 | 1395 | else |
485015ca | 1396 | { |
070d1949 | 1397 | if (wv->enabled) |
485015ca GV |
1398 | fuFlags = MF_STRING; |
1399 | else | |
1400 | fuFlags = MF_STRING | MF_GRAYED; | |
1401 | ||
1402 | if (wv->key != NULL) | |
1403 | { | |
98c6f1e3 | 1404 | out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2); |
485015ca GV |
1405 | strcpy (out_string, wv->name); |
1406 | strcat (out_string, "\t"); | |
1407 | strcat (out_string, wv->key); | |
1408 | } | |
1409 | else | |
94c97d85 | 1410 | out_string = (char *)wv->name; |
485015ca | 1411 | |
af41f8a8 EZ |
1412 | /* Quote any special characters within the menu item's text and |
1413 | key binding. */ | |
1414 | nlen = orig_len = strlen (out_string); | |
ff25d115 JR |
1415 | if (unicode_append_menu) |
1416 | { | |
1417 | /* With UTF-8, & cannot be part of a multibyte character. */ | |
1418 | for (p = out_string; *p; p++) | |
1419 | { | |
1420 | if (*p == '&') | |
1421 | nlen++; | |
1422 | } | |
1423 | } | |
0fda9b75 | 1424 | #ifndef NTGUI_UNICODE |
ff25d115 JR |
1425 | else |
1426 | { | |
1427 | /* If encoded with the system codepage, use multibyte string | |
1428 | functions in case of multibyte characters that contain '&'. */ | |
1429 | for (p = out_string; *p; p = _mbsinc (p)) | |
1430 | { | |
1431 | if (_mbsnextc (p) == '&') | |
1432 | nlen++; | |
1433 | } | |
1434 | } | |
0fda9b75 | 1435 | #endif /* !NTGUI_UNICODE */ |
ff25d115 | 1436 | |
af41f8a8 | 1437 | if (nlen > orig_len) |
ff25d115 JR |
1438 | { |
1439 | p = out_string; | |
98c6f1e3 | 1440 | out_string = SAFE_ALLOCA (nlen + 1); |
ff25d115 JR |
1441 | q = out_string; |
1442 | while (*p) | |
1443 | { | |
1444 | if (unicode_append_menu) | |
1445 | { | |
1446 | if (*p == '&') | |
1447 | *q++ = *p; | |
1448 | *q++ = *p++; | |
1449 | } | |
0fda9b75 | 1450 | #ifndef NTGUI_UNICODE |
ff25d115 JR |
1451 | else |
1452 | { | |
1453 | if (_mbsnextc (p) == '&') | |
1454 | { | |
1455 | _mbsncpy (q, p, 1); | |
1456 | q = _mbsinc (q); | |
1457 | } | |
1458 | _mbsncpy (q, p, 1); | |
1459 | p = _mbsinc (p); | |
1460 | q = _mbsinc (q); | |
1461 | } | |
0fda9b75 | 1462 | #endif /* !NTGUI_UNICODE */ |
ff25d115 JR |
1463 | } |
1464 | *q = '\0'; | |
1465 | } | |
af41f8a8 | 1466 | |
d1fffd1d JR |
1467 | if (item != NULL) |
1468 | fuFlags = MF_POPUP; | |
1469 | else if (wv->title || wv->call_data == 0) | |
485015ca | 1470 | { |
ace9b298 JR |
1471 | /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since |
1472 | we can't deallocate the memory otherwise. */ | |
1473 | if (get_menu_item_info) | |
1474 | { | |
6ed09cf0 RS |
1475 | out_string = (char *) local_alloc (strlen (wv->name) + 1); |
1476 | strcpy (out_string, wv->name); | |
d1fffd1d | 1477 | #ifdef MENU_DEBUG |
91af3942 | 1478 | DebPrint ("Menu: allocating %ld for owner-draw", out_string); |
d1fffd1d | 1479 | #endif |
ace9b298 JR |
1480 | fuFlags = MF_OWNERDRAW | MF_DISABLED; |
1481 | } | |
1482 | else | |
1483 | fuFlags = MF_DISABLED; | |
485015ca | 1484 | } |
1c5acb8c | 1485 | |
162d2499 | 1486 | /* Draw radio buttons and tickboxes. */ |
4cdf6a6c | 1487 | else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE || |
6f665da9 | 1488 | wv->button_type == BUTTON_TYPE_RADIO)) |
4cdf6a6c | 1489 | fuFlags |= MF_CHECKED; |
37ad8b63 | 1490 | else |
4cdf6a6c | 1491 | fuFlags |= MF_UNCHECKED; |
485015ca | 1492 | } |
4cdf6a6c | 1493 | |
1f06d367 JR |
1494 | if (unicode_append_menu && out_string) |
1495 | { | |
1496 | /* Convert out_string from UTF-8 to UTF-16-LE. */ | |
1497 | int utf8_len = strlen (out_string); | |
1498 | WCHAR * utf16_string; | |
1499 | if (fuFlags & MF_OWNERDRAW) | |
1500 | utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR)); | |
1501 | else | |
98c6f1e3 | 1502 | utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR)); |
1f06d367 JR |
1503 | |
1504 | utf8to16 (out_string, utf8_len, utf16_string); | |
1505 | return_value = unicode_append_menu (menu, fuFlags, | |
1506 | item != NULL ? (UINT) item | |
1507 | : (UINT) wv->call_data, | |
1508 | utf16_string); | |
0fda9b75 DC |
1509 | |
1510 | #ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */ | |
d749b6fa JR |
1511 | if (!return_value) |
1512 | { | |
fe7a3057 | 1513 | /* On W9x/ME, Unicode menus are not supported, though AppendMenuW |
d749b6fa JR |
1514 | apparently does exist at least in some cases and appears to be |
1515 | stubbed out to do nothing. out_string is UTF-8, but since | |
1516 | our standard menus are in English and this is only going to | |
1517 | happen the first time a menu is used, the encoding is | |
1518 | of minor importance compared with menus not working at all. */ | |
1519 | return_value = | |
1520 | AppendMenu (menu, fuFlags, | |
1521 | item != NULL ? (UINT) item: (UINT) wv->call_data, | |
1522 | out_string); | |
1c6900d9 EZ |
1523 | /* Don't use Unicode menus in future, unless this is Windows |
1524 | NT or later, where a failure of AppendMenuW does NOT mean | |
1525 | Unicode menus are unsupported. */ | |
1526 | if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT) | |
1527 | unicode_append_menu = NULL; | |
d749b6fa | 1528 | } |
0fda9b75 | 1529 | #endif /* NTGUI_UNICODE */ |
d749b6fa JR |
1530 | |
1531 | if (unicode_append_menu && (fuFlags & MF_OWNERDRAW)) | |
1f06d367 JR |
1532 | local_free (out_string); |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | return_value = | |
1537 | AppendMenu (menu, | |
1538 | fuFlags, | |
1539 | item != NULL ? (UINT) item : (UINT) wv->call_data, | |
1540 | out_string ); | |
1541 | } | |
37ad8b63 JR |
1542 | |
1543 | /* This must be done after the menu item is created. */ | |
6f665da9 | 1544 | if (!wv->title && wv->call_data != 0) |
4cdf6a6c | 1545 | { |
4cdf6a6c AI |
1546 | if (set_menu_item_info) |
1547 | { | |
1548 | MENUITEMINFO info; | |
72af86bd | 1549 | memset (&info, 0, sizeof (info)); |
4cdf6a6c AI |
1550 | info.cbSize = sizeof (info); |
1551 | info.fMask = MIIM_DATA; | |
1552 | ||
d1fffd1d JR |
1553 | /* Set help string for menu item. Leave it as a Lisp_Object |
1554 | until it is ready to be displayed, since GC can happen while | |
1555 | menus are active. */ | |
24ee9763 | 1556 | if (!NILP (wv->help)) |
2d5c5f7d EZ |
1557 | { |
1558 | /* As of Jul-2012, w32api headers say that dwItemData | |
1559 | has DWORD type, but that's a bug: it should actually | |
37a9eac8 | 1560 | be ULONG_PTR, which is correct for 32-bit and 64-bit |
2d5c5f7d EZ |
1561 | Windows alike. MSVC headers get it right; hopefully, |
1562 | MinGW headers will, too. */ | |
37a9eac8 | 1563 | info.dwItemData = (ULONG_PTR) XLI (wv->help); |
2d5c5f7d | 1564 | } |
4cdf6a6c AI |
1565 | if (wv->button_type == BUTTON_TYPE_RADIO) |
1566 | { | |
1567 | /* CheckMenuRadioItem allows us to differentiate TOGGLE and | |
1568 | RADIO items, but is not available on NT 3.51 and earlier. */ | |
1569 | info.fMask |= MIIM_TYPE | MIIM_STATE; | |
1570 | info.fType = MFT_RADIOCHECK | MFT_STRING; | |
1571 | info.dwTypeData = out_string; | |
1572 | info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED; | |
1573 | } | |
1574 | ||
1575 | set_menu_item_info (menu, | |
1576 | item != NULL ? (UINT) item : (UINT) wv->call_data, | |
1577 | FALSE, &info); | |
1578 | } | |
1579 | } | |
8bc53d00 | 1580 | SAFE_FREE (); |
37ad8b63 | 1581 | return return_value; |
485015ca GV |
1582 | } |
1583 | ||
1584 | /* Construct native Windows menu(bar) based on widget_value tree. */ | |
24f981c9 | 1585 | static int |
485015ca GV |
1586 | fill_in_menu (HMENU menu, widget_value *wv) |
1587 | { | |
485015ca GV |
1588 | for ( ; wv != NULL; wv = wv->next) |
1589 | { | |
1590 | if (wv->contents) | |
1591 | { | |
1592 | HMENU sub_menu = CreatePopupMenu (); | |
1593 | ||
1594 | if (sub_menu == NULL) | |
1595 | return 0; | |
1596 | ||
1597 | if (!fill_in_menu (sub_menu, wv->contents) || | |
1598 | !add_menu_item (menu, wv, sub_menu)) | |
1599 | { | |
1600 | DestroyMenu (sub_menu); | |
1601 | return 0; | |
1602 | } | |
1603 | } | |
1604 | else | |
1605 | { | |
1606 | if (!add_menu_item (menu, wv, NULL)) | |
1607 | return 0; | |
1608 | } | |
1609 | } | |
1610 | return 1; | |
1611 | } | |
1612 | ||
7d8e57da JR |
1613 | /* Display help string for currently pointed to menu item. Not |
1614 | supported on NT 3.51 and earlier, as GetMenuItemInfo is not | |
1615 | available. */ | |
1616 | void | |
59a86c99 | 1617 | w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags) |
7d8e57da | 1618 | { |
7d8e57da JR |
1619 | if (get_menu_item_info) |
1620 | { | |
1c5acb8c JR |
1621 | struct frame *f = x_window_to_frame (&one_w32_display_info, owner); |
1622 | Lisp_Object frame, help; | |
7d8e57da | 1623 | |
94c7f257 JR |
1624 | /* No help echo on owner-draw menu items, or when the keyboard is used |
1625 | to navigate the menus, since tooltips are distracting if they pop | |
1626 | up elsewhere. */ | |
1627 | if (flags & MF_OWNERDRAW || flags & MF_POPUP | |
1628 | || !(flags & MF_MOUSESELECT)) | |
ace9b298 JR |
1629 | help = Qnil; |
1630 | else | |
1631 | { | |
1632 | MENUITEMINFO info; | |
7d8e57da | 1633 | |
72af86bd | 1634 | memset (&info, 0, sizeof (info)); |
ace9b298 JR |
1635 | info.cbSize = sizeof (info); |
1636 | info.fMask = MIIM_DATA; | |
1637 | get_menu_item_info (menu, item, FALSE, &info); | |
1638 | ||
646b5f55 | 1639 | help = info.dwItemData ? XIL (info.dwItemData) : Qnil; |
ace9b298 | 1640 | } |
e5cd3d11 | 1641 | |
1c5acb8c JR |
1642 | /* Store the help echo in the keyboard buffer as the X toolkit |
1643 | version does, rather than directly showing it. This seems to | |
1644 | solve the GC problems that were present when we based the | |
1645 | Windows code on the non-toolkit version. */ | |
1646 | if (f) | |
1647 | { | |
1648 | XSETFRAME (frame, f); | |
1649 | kbd_buffer_store_help_event (frame, help); | |
1650 | } | |
1651 | else | |
1652 | /* X version has a loop through frames here, which doesn't | |
1653 | appear to do anything, unless it has some side effect. */ | |
f868cd8a | 1654 | show_help_echo (help, Qnil, Qnil, Qnil); |
7d8e57da JR |
1655 | } |
1656 | } | |
1657 | ||
d1fffd1d | 1658 | /* Free memory used by owner-drawn strings. */ |
ace9b298 | 1659 | static void |
b56ceb92 | 1660 | w32_free_submenu_strings (HMENU menu) |
ace9b298 JR |
1661 | { |
1662 | int i, num = GetMenuItemCount (menu); | |
1663 | for (i = 0; i < num; i++) | |
1664 | { | |
1665 | MENUITEMINFO info; | |
72af86bd | 1666 | memset (&info, 0, sizeof (info)); |
ace9b298 | 1667 | info.cbSize = sizeof (info); |
d1fffd1d | 1668 | info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU; |
7d8e57da | 1669 | |
ace9b298 JR |
1670 | get_menu_item_info (menu, i, TRUE, &info); |
1671 | ||
d1fffd1d JR |
1672 | /* Owner-drawn names are held in dwItemData. */ |
1673 | if ((info.fType & MF_OWNERDRAW) && info.dwItemData) | |
1674 | { | |
1675 | #ifdef MENU_DEBUG | |
1676 | DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData); | |
1677 | #endif | |
6ed09cf0 | 1678 | local_free (info.dwItemData); |
d1fffd1d | 1679 | } |
ace9b298 JR |
1680 | |
1681 | /* Recurse down submenus. */ | |
1682 | if (info.hSubMenu) | |
1683 | w32_free_submenu_strings (info.hSubMenu); | |
1684 | } | |
1685 | } | |
1686 | ||
1687 | void | |
b56ceb92 | 1688 | w32_free_menu_strings (HWND hwnd) |
ace9b298 JR |
1689 | { |
1690 | HMENU menu = current_popup_menu; | |
1691 | ||
1692 | if (get_menu_item_info) | |
1693 | { | |
1694 | /* If there is no popup menu active, free the strings from the frame's | |
1695 | menubar. */ | |
1696 | if (!menu) | |
1697 | menu = GetMenu (hwnd); | |
1698 | ||
1699 | if (menu) | |
1700 | w32_free_submenu_strings (menu); | |
1701 | } | |
1702 | ||
1703 | current_popup_menu = NULL; | |
1704 | } | |
7d8e57da | 1705 | |
485015ca | 1706 | #endif /* HAVE_MENUS */ |
ace9b298 | 1707 | |
e3135734 CY |
1708 | /* The following is used by delayed window autoselection. */ |
1709 | ||
1710 | DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0, | |
1711 | doc: /* Return t if a menu or popup dialog is active on selected frame. */) | |
5842a27b | 1712 | (void) |
e3135734 CY |
1713 | { |
1714 | #ifdef HAVE_MENUS | |
1715 | FRAME_PTR f; | |
1716 | f = SELECTED_FRAME (); | |
1717 | return (f->output_data.w32->menubar_active > 0) ? Qt : Qnil; | |
1718 | #else | |
1719 | return Qnil; | |
1720 | #endif /* HAVE_MENUS */ | |
1721 | } | |
1722 | ||
b56ceb92 JB |
1723 | void |
1724 | syms_of_w32menu (void) | |
ee78dc32 | 1725 | { |
019e3c1a | 1726 | globals_of_w32menu (); |
485015ca | 1727 | |
ace9b298 JR |
1728 | current_popup_menu = NULL; |
1729 | ||
019e3c1a | 1730 | DEFSYM (Qdebug_on_next_call, "debug-on-next-call"); |
fdc12c4d | 1731 | |
e5d3d18d | 1732 | defsubr (&Smenu_or_popup_active_p); |
485015ca | 1733 | #ifdef HAVE_MENUS |
ee78dc32 | 1734 | defsubr (&Sx_popup_dialog); |
485015ca | 1735 | #endif |
ee78dc32 | 1736 | } |
9785d95b BK |
1737 | |
1738 | /* | |
1739 | globals_of_w32menu is used to initialize those global variables that | |
1740 | must always be initialized on startup even when the global variable | |
1741 | initialized is non zero (see the function main in emacs.c). | |
1742 | globals_of_w32menu is called from syms_of_w32menu when the global | |
1743 | variable initialized is 0 and directly from main when initialized | |
1744 | is non zero. | |
1745 | */ | |
b56ceb92 JB |
1746 | void |
1747 | globals_of_w32menu (void) | |
9785d95b | 1748 | { |
0fda9b75 | 1749 | #ifndef NTGUI_UNICODE |
9d4f32e8 | 1750 | /* See if Get/SetMenuItemInfo functions are available. */ |
9785d95b BK |
1751 | HMODULE user32 = GetModuleHandle ("user32.dll"); |
1752 | get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA"); | |
1753 | set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA"); | |
1f06d367 | 1754 | unicode_append_menu = (AppendMenuW_Proc) GetProcAddress (user32, "AppendMenuW"); |
1c9b4129 | 1755 | unicode_message_box = (MessageBoxW_Proc) GetProcAddress (user32, "MessageBoxW"); |
0fda9b75 | 1756 | #endif /* !NTGUI_UNICODE */ |
9785d95b | 1757 | } |