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