Commit | Line | Data |
---|---|---|
e9e23e23 | 1 | /* Menu support for GNU Emacs on the Microsoft W32 API. |
f456401b | 2 | Copyright (C) 1986,88,93,94,96,98,1999,2003 Free Software Foundation, Inc. |
ee78dc32 GV |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
ee78dc32 | 20 | |
ee78dc32 | 21 | #include <config.h> |
68c45bf0 | 22 | #include <signal.h> |
162d2499 | 23 | |
ee78dc32 GV |
24 | #include <stdio.h> |
25 | #include "lisp.h" | |
26 | #include "termhooks.h" | |
15d36dee | 27 | #include "keyboard.h" |
5ee707b8 | 28 | #include "keymap.h" |
ee78dc32 GV |
29 | #include "frame.h" |
30 | #include "window.h" | |
ee78dc32 | 31 | #include "blockinput.h" |
fdc12c4d | 32 | #include "buffer.h" |
6915ded0 JR |
33 | #include "charset.h" |
34 | #include "coding.h" | |
ee78dc32 GV |
35 | |
36 | /* This may include sys/types.h, and that somehow loses | |
37 | if this is not done before the other system files. */ | |
38 | #include "w32term.h" | |
39 | ||
40 | /* Load sys/types.h if not already loaded. | |
41 | In some systems loading it twice is suicidal. */ | |
42 | #ifndef makedev | |
43 | #include <sys/types.h> | |
44 | #endif | |
45 | ||
46 | #include "dispextern.h" | |
47 | ||
162d2499 | 48 | #undef HAVE_MULTILINGUAL_MENU |
222456a1 | 49 | #undef HAVE_DIALOGS /* TODO: Implement native dialogs. */ |
162d2499 | 50 | |
485015ca GV |
51 | /******************************************************************/ |
52 | /* Definitions copied from lwlib.h */ | |
ee78dc32 | 53 | |
485015ca GV |
54 | typedef void * XtPointer; |
55 | typedef char Boolean; | |
56 | ||
162d2499 JR |
57 | enum button_type |
58 | { | |
59 | BUTTON_TYPE_NONE, | |
60 | BUTTON_TYPE_TOGGLE, | |
61 | BUTTON_TYPE_RADIO | |
62 | }; | |
c8869655 | 63 | |
d1fffd1d JR |
64 | /* This structure is based on the one in ../lwlib/lwlib.h, modified |
65 | for Windows. */ | |
485015ca | 66 | typedef struct _widget_value |
ee78dc32 | 67 | { |
485015ca GV |
68 | /* name of widget */ |
69 | char* name; | |
70 | /* value (meaning depend on widget type) */ | |
71 | char* value; | |
177c0ea7 | 72 | /* keyboard equivalent. no implications for XtTranslations */ |
485015ca | 73 | char* key; |
d1fffd1d JR |
74 | /* Help string or nil if none. |
75 | GC finds this string through the frame's menu_bar_vector | |
76 | or through menu_items. */ | |
77 | Lisp_Object help; | |
485015ca GV |
78 | /* true if enabled */ |
79 | Boolean enabled; | |
80 | /* true if selected */ | |
81 | Boolean selected; | |
162d2499 JR |
82 | /* The type of a button. */ |
83 | enum button_type button_type; | |
485015ca GV |
84 | /* true if menu title */ |
85 | Boolean title; | |
86 | #if 0 | |
87 | /* true if was edited (maintained by get_value) */ | |
88 | Boolean edited; | |
89 | /* true if has changed (maintained by lw library) */ | |
90 | change_type change; | |
91 | /* true if this widget itself has changed, | |
92 | but not counting the other widgets found in the `next' field. */ | |
93 | change_type this_one_change; | |
94 | #endif | |
95 | /* Contents of the sub-widgets, also selected slot for checkbox */ | |
96 | struct _widget_value* contents; | |
97 | /* data passed to callback */ | |
98 | XtPointer call_data; | |
99 | /* next one in the list */ | |
100 | struct _widget_value* next; | |
101 | #if 0 | |
102 | /* slot for the toolkit dependent part. Always initialize to NULL. */ | |
103 | void* toolkit_data; | |
104 | /* tell us if we should free the toolkit data slot when freeing the | |
105 | widget_value itself. */ | |
106 | Boolean free_toolkit_data; | |
107 | ||
108 | /* we resource the widget_value structures; this points to the next | |
109 | one on the free list if this one has been deallocated. | |
110 | */ | |
111 | struct _widget_value *free_list; | |
112 | #endif | |
113 | } widget_value; | |
114 | ||
6ed09cf0 RS |
115 | /* Local memory management */ |
116 | #define local_heap (GetProcessHeap ()) | |
117 | #define local_alloc(n) (HeapAlloc (local_heap, HEAP_ZERO_MEMORY, (n))) | |
118 | #define local_free(p) (HeapFree (local_heap, 0, ((LPVOID) (p)))) | |
119 | ||
120 | #define malloc_widget_value() ((widget_value *) local_alloc (sizeof (widget_value))) | |
121 | #define free_widget_value(wv) (local_free ((wv))) | |
485015ca GV |
122 | |
123 | /******************************************************************/ | |
124 | ||
485015ca GV |
125 | #ifndef TRUE |
126 | #define TRUE 1 | |
127 | #define FALSE 0 | |
128 | #endif /* no TRUE */ | |
129 | ||
ace9b298 JR |
130 | static HMENU current_popup_menu; |
131 | ||
f60ae425 | 132 | void syms_of_w32menu (); |
9785d95b | 133 | void globals_of_w32menu (); |
f60ae425 BK |
134 | |
135 | typedef BOOL (WINAPI * GetMenuItemInfoA_Proc) ( | |
136 | IN HMENU, | |
137 | IN UINT, | |
138 | IN BOOL, | |
139 | IN OUT LPMENUITEMINFOA | |
140 | ); | |
141 | typedef BOOL (WINAPI * SetMenuItemInfoA_Proc) ( | |
142 | IN HMENU, | |
143 | IN UINT, | |
144 | IN BOOL, | |
145 | IN LPCMENUITEMINFOA | |
146 | ); | |
147 | ||
148 | GetMenuItemInfoA_Proc get_menu_item_info=NULL; | |
149 | SetMenuItemInfoA_Proc set_menu_item_info=NULL; | |
ace9b298 | 150 | |
485015ca | 151 | Lisp_Object Vmenu_updating_frame; |
ee78dc32 | 152 | |
fdc12c4d RS |
153 | Lisp_Object Qdebug_on_next_call; |
154 | ||
ee78dc32 | 155 | extern Lisp_Object Qmenu_bar; |
485015ca GV |
156 | extern Lisp_Object Qmouse_click, Qevent_kind; |
157 | ||
158 | extern Lisp_Object QCtoggle, QCradio; | |
ee78dc32 | 159 | |
fdc12c4d RS |
160 | extern Lisp_Object Voverriding_local_map; |
161 | extern Lisp_Object Voverriding_local_map_menu_flag; | |
162 | ||
163 | extern Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map; | |
164 | ||
165 | extern Lisp_Object Qmenu_bar_update_hook; | |
166 | ||
014510b0 GV |
167 | void set_frame_menubar (); |
168 | ||
162d2499 JR |
169 | static void push_menu_item P_ ((Lisp_Object, Lisp_Object, Lisp_Object, |
170 | Lisp_Object, Lisp_Object, Lisp_Object, | |
171 | Lisp_Object, Lisp_Object)); | |
1c5acb8c | 172 | #ifdef HAVE_DIALOGS |
fbd6baed | 173 | static Lisp_Object w32_dialog_show (); |
1c5acb8c | 174 | #endif |
162d2499 | 175 | static Lisp_Object w32_menu_show (); |
ee78dc32 | 176 | |
485015ca GV |
177 | static void keymap_panes (); |
178 | static void single_keymap_panes (); | |
179 | static void single_menu_item (); | |
180 | static void list_of_panes (); | |
181 | static void list_of_items (); | |
71f6b295 | 182 | void w32_free_menu_strings (HWND); |
485015ca GV |
183 | \f |
184 | /* This holds a Lisp vector that holds the results of decoding | |
185 | the keymaps or alist-of-alists that specify a menu. | |
186 | ||
187 | It describes the panes and items within the panes. | |
188 | ||
189 | Each pane is described by 3 elements in the vector: | |
190 | t, the pane name, the pane's prefix key. | |
191 | Then follow the pane's items, with 5 elements per item: | |
192 | the item string, the enable flag, the item's value, | |
193 | the definition, and the equivalent keyboard key's description string. | |
194 | ||
195 | In some cases, multiple levels of menus may be described. | |
196 | A single vector slot containing nil indicates the start of a submenu. | |
197 | A single vector slot containing lambda indicates the end of a submenu. | |
198 | The submenu follows a menu item which is the way to reach the submenu. | |
199 | ||
200 | A single vector slot containing quote indicates that the | |
201 | following items should appear on the right of a dialog box. | |
202 | ||
203 | Using a Lisp vector to hold this information while we decode it | |
204 | takes care of protecting all the data from GC. */ | |
205 | ||
206 | #define MENU_ITEMS_PANE_NAME 1 | |
207 | #define MENU_ITEMS_PANE_PREFIX 2 | |
208 | #define MENU_ITEMS_PANE_LENGTH 3 | |
209 | ||
162d2499 JR |
210 | enum menu_item_idx |
211 | { | |
212 | MENU_ITEMS_ITEM_NAME = 0, | |
213 | MENU_ITEMS_ITEM_ENABLE, | |
214 | MENU_ITEMS_ITEM_VALUE, | |
215 | MENU_ITEMS_ITEM_EQUIV_KEY, | |
216 | MENU_ITEMS_ITEM_DEFINITION, | |
217 | MENU_ITEMS_ITEM_TYPE, | |
218 | MENU_ITEMS_ITEM_SELECTED, | |
219 | MENU_ITEMS_ITEM_HELP, | |
220 | MENU_ITEMS_ITEM_LENGTH | |
221 | }; | |
485015ca GV |
222 | |
223 | static Lisp_Object menu_items; | |
224 | ||
225 | /* Number of slots currently allocated in menu_items. */ | |
226 | static int menu_items_allocated; | |
227 | ||
228 | /* This is the index in menu_items of the first empty slot. */ | |
229 | static int menu_items_used; | |
230 | ||
231 | /* The number of panes currently recorded in menu_items, | |
232 | excluding those within submenus. */ | |
233 | static int menu_items_n_panes; | |
234 | ||
235 | /* Current depth within submenus. */ | |
236 | static int menu_items_submenu_depth; | |
237 | ||
238 | /* Flag which when set indicates a dialog or menu has been posted by | |
239 | Xt on behalf of one of the widget sets. */ | |
240 | static int popup_activated_flag; | |
241 | ||
162d2499 JR |
242 | static int next_menubar_widget_id; |
243 | ||
485015ca GV |
244 | /* This is set nonzero after the user activates the menu bar, and set |
245 | to zero again after the menu bars are redisplayed by prepare_menu_bar. | |
246 | While it is nonzero, all calls to set_frame_menubar go deep. | |
247 | ||
248 | I don't understand why this is needed, but it does seem to be | |
249 | needed on Motif, according to Marcus Daniels <marcus@sysc.pdx.edu>. */ | |
ee78dc32 | 250 | |
485015ca GV |
251 | int pending_menu_activation; |
252 | \f | |
253 | ||
254 | /* Return the frame whose ->output_data.w32->menubar_widget equals | |
162d2499 | 255 | ID, or 0 if none. */ |
485015ca GV |
256 | |
257 | static struct frame * | |
162d2499 JR |
258 | menubar_id_to_frame (id) |
259 | HMENU id; | |
485015ca GV |
260 | { |
261 | Lisp_Object tail, frame; | |
262 | FRAME_PTR f; | |
ee78dc32 | 263 | |
8e713be6 | 264 | for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail)) |
485015ca | 265 | { |
8e713be6 | 266 | frame = XCAR (tail); |
485015ca GV |
267 | if (!GC_FRAMEP (frame)) |
268 | continue; | |
269 | f = XFRAME (frame); | |
162d2499 | 270 | if (!FRAME_WINDOW_P (f)) |
485015ca | 271 | continue; |
162d2499 | 272 | if (f->output_data.w32->menubar_widget == id) |
485015ca GV |
273 | return f; |
274 | } | |
275 | return 0; | |
276 | } | |
277 | \f | |
ee78dc32 GV |
278 | /* Initialize the menu_items structure if we haven't already done so. |
279 | Also mark it as currently empty. */ | |
280 | ||
485015ca GV |
281 | static void |
282 | init_menu_items () | |
ee78dc32 | 283 | { |
485015ca | 284 | if (NILP (menu_items)) |
ee78dc32 | 285 | { |
485015ca GV |
286 | menu_items_allocated = 60; |
287 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
ee78dc32 | 288 | } |
485015ca GV |
289 | |
290 | menu_items_used = 0; | |
291 | menu_items_n_panes = 0; | |
292 | menu_items_submenu_depth = 0; | |
ee78dc32 GV |
293 | } |
294 | ||
485015ca GV |
295 | /* Call at the end of generating the data in menu_items. |
296 | This fills in the number of items in the last pane. */ | |
ee78dc32 | 297 | |
485015ca GV |
298 | static void |
299 | finish_menu_items () | |
ee78dc32 | 300 | { |
ee78dc32 | 301 | } |
014510b0 GV |
302 | |
303 | /* Call when finished using the data for the current menu | |
304 | in menu_items. */ | |
305 | ||
485015ca GV |
306 | static void |
307 | discard_menu_items () | |
014510b0 | 308 | { |
485015ca GV |
309 | /* Free the structure if it is especially large. |
310 | Otherwise, hold on to it, to save time. */ | |
311 | if (menu_items_allocated > 200) | |
014510b0 | 312 | { |
485015ca GV |
313 | menu_items = Qnil; |
314 | menu_items_allocated = 0; | |
014510b0 | 315 | } |
014510b0 GV |
316 | } |
317 | ||
485015ca | 318 | /* Make the menu_items vector twice as large. */ |
ee78dc32 | 319 | |
485015ca GV |
320 | static void |
321 | grow_menu_items () | |
ee78dc32 | 322 | { |
485015ca GV |
323 | Lisp_Object old; |
324 | int old_size = menu_items_allocated; | |
325 | old = menu_items; | |
326 | ||
327 | menu_items_allocated *= 2; | |
328 | menu_items = Fmake_vector (make_number (menu_items_allocated), Qnil); | |
329 | bcopy (XVECTOR (old)->contents, XVECTOR (menu_items)->contents, | |
330 | old_size * sizeof (Lisp_Object)); | |
ee78dc32 GV |
331 | } |
332 | ||
485015ca | 333 | /* Begin a submenu. */ |
014510b0 | 334 | |
485015ca GV |
335 | static void |
336 | push_submenu_start () | |
337 | { | |
338 | if (menu_items_used + 1 > menu_items_allocated) | |
339 | grow_menu_items (); | |
014510b0 | 340 | |
6ed09cf0 | 341 | ASET (menu_items, menu_items_used++, Qnil); |
485015ca | 342 | menu_items_submenu_depth++; |
ee78dc32 | 343 | } |
485015ca GV |
344 | |
345 | /* End a submenu. */ | |
346 | ||
347 | static void | |
348 | push_submenu_end () | |
ee78dc32 | 349 | { |
485015ca GV |
350 | if (menu_items_used + 1 > menu_items_allocated) |
351 | grow_menu_items (); | |
ee78dc32 | 352 | |
6ed09cf0 | 353 | ASET (menu_items, menu_items_used++, Qlambda); |
485015ca | 354 | menu_items_submenu_depth--; |
ee78dc32 GV |
355 | } |
356 | ||
485015ca | 357 | /* Indicate boundary between left and right. */ |
ee78dc32 | 358 | |
485015ca GV |
359 | static void |
360 | push_left_right_boundary () | |
ee78dc32 | 361 | { |
485015ca GV |
362 | if (menu_items_used + 1 > menu_items_allocated) |
363 | grow_menu_items (); | |
364 | ||
6ed09cf0 | 365 | ASET (menu_items, menu_items_used++, Qquote); |
ee78dc32 GV |
366 | } |
367 | ||
2cd23960 | 368 | /* Start a new menu pane in menu_items. |
485015ca | 369 | NAME is the pane name. PREFIX_VEC is a prefix key for this pane. */ |
ee78dc32 | 370 | |
485015ca GV |
371 | static void |
372 | push_menu_pane (name, prefix_vec) | |
373 | Lisp_Object name, prefix_vec; | |
ee78dc32 | 374 | { |
485015ca GV |
375 | if (menu_items_used + MENU_ITEMS_PANE_LENGTH > menu_items_allocated) |
376 | grow_menu_items (); | |
377 | ||
378 | if (menu_items_submenu_depth == 0) | |
379 | menu_items_n_panes++; | |
6ed09cf0 RS |
380 | ASET (menu_items, menu_items_used++, Qt); |
381 | ASET (menu_items, menu_items_used++, name); | |
382 | ASET (menu_items, menu_items_used++, prefix_vec); | |
485015ca | 383 | } |
ee78dc32 | 384 | |
162d2499 JR |
385 | /* Push one menu item into the current pane. NAME is the string to |
386 | display. ENABLE if non-nil means this item can be selected. KEY | |
387 | is the key generated by choosing this item, or nil if this item | |
388 | doesn't really have a definition. DEF is the definition of this | |
389 | item. EQUIV is the textual description of the keyboard equivalent | |
390 | for this item (or nil if none). TYPE is the type of this menu | |
391 | item, one of nil, `toggle' or `radio'. */ | |
485015ca GV |
392 | |
393 | static void | |
162d2499 JR |
394 | push_menu_item (name, enable, key, def, equiv, type, selected, help) |
395 | Lisp_Object name, enable, key, def, equiv, type, selected, help; | |
485015ca GV |
396 | { |
397 | if (menu_items_used + MENU_ITEMS_ITEM_LENGTH > menu_items_allocated) | |
398 | grow_menu_items (); | |
399 | ||
6ed09cf0 RS |
400 | ASET (menu_items, menu_items_used++, name); |
401 | ASET (menu_items, menu_items_used++, enable); | |
402 | ASET (menu_items, menu_items_used++, key); | |
403 | ASET (menu_items, menu_items_used++, equiv); | |
404 | ASET (menu_items, menu_items_used++, def); | |
405 | ASET (menu_items, menu_items_used++, type); | |
406 | ASET (menu_items, menu_items_used++, selected); | |
407 | ASET (menu_items, menu_items_used++, help); | |
ee78dc32 GV |
408 | } |
409 | \f | |
410 | /* Look through KEYMAPS, a vector of keymaps that is NMAPS long, | |
411 | and generate menu panes for them in menu_items. | |
412 | If NOTREAL is nonzero, | |
413 | don't bother really computing whether an item is enabled. */ | |
414 | ||
485015ca GV |
415 | static void |
416 | keymap_panes (keymaps, nmaps, notreal) | |
ee78dc32 GV |
417 | Lisp_Object *keymaps; |
418 | int nmaps; | |
419 | int notreal; | |
420 | { | |
421 | int mapno; | |
014510b0 | 422 | |
485015ca | 423 | init_menu_items (); |
014510b0 | 424 | |
485015ca GV |
425 | /* Loop over the given keymaps, making a pane for each map. |
426 | But don't make a pane that is empty--ignore that map instead. | |
427 | P is the number of panes we have made so far. */ | |
428 | for (mapno = 0; mapno < nmaps; mapno++) | |
e5cd3d11 | 429 | single_keymap_panes (keymaps[mapno], |
5ee707b8 | 430 | Fkeymap_prompt (keymaps[mapno]), Qnil, notreal, 10); |
ee78dc32 | 431 | |
485015ca | 432 | finish_menu_items (); |
ee78dc32 GV |
433 | } |
434 | ||
435 | /* This is a recursive subroutine of keymap_panes. | |
436 | It handles one keymap, KEYMAP. | |
437 | The other arguments are passed along | |
438 | or point to local variables of the previous function. | |
485015ca GV |
439 | If NOTREAL is nonzero, only check for equivalent key bindings, don't |
440 | evaluate expressions in menu items and don't make any menu. | |
ee78dc32 | 441 | |
485015ca GV |
442 | If we encounter submenus deeper than MAXDEPTH levels, ignore them. */ |
443 | ||
444 | static void | |
445 | single_keymap_panes (keymap, pane_name, prefix, notreal, maxdepth) | |
ee78dc32 GV |
446 | Lisp_Object keymap; |
447 | Lisp_Object pane_name; | |
448 | Lisp_Object prefix; | |
449 | int notreal; | |
485015ca | 450 | int maxdepth; |
ee78dc32 | 451 | { |
485015ca GV |
452 | Lisp_Object pending_maps = Qnil; |
453 | Lisp_Object tail, item; | |
454 | struct gcpro gcpro1, gcpro2; | |
485015ca GV |
455 | |
456 | if (maxdepth <= 0) | |
457 | return; | |
458 | ||
459 | push_menu_pane (pane_name, prefix); | |
460 | ||
8e713be6 | 461 | for (tail = keymap; CONSP (tail); tail = XCDR (tail)) |
ee78dc32 | 462 | { |
485015ca GV |
463 | GCPRO2 (keymap, pending_maps); |
464 | /* Look at each key binding, and if it is a menu item add it | |
465 | to this menu. */ | |
8e713be6 | 466 | item = XCAR (tail); |
ee78dc32 | 467 | if (CONSP (item)) |
8e713be6 | 468 | single_menu_item (XCAR (item), XCDR (item), |
37ad8b63 | 469 | &pending_maps, notreal, maxdepth); |
ee78dc32 GV |
470 | else if (VECTORP (item)) |
471 | { | |
472 | /* Loop over the char values represented in the vector. */ | |
6ed09cf0 | 473 | int len = ASIZE (item); |
ee78dc32 GV |
474 | int c; |
475 | for (c = 0; c < len; c++) | |
476 | { | |
477 | Lisp_Object character; | |
478 | XSETFASTINT (character, c); | |
6ed09cf0 | 479 | single_menu_item (character, AREF (item, c), |
37ad8b63 | 480 | &pending_maps, notreal, maxdepth); |
ee78dc32 GV |
481 | } |
482 | } | |
485015ca | 483 | UNGCPRO; |
ee78dc32 GV |
484 | } |
485 | ||
486 | /* Process now any submenus which want to be panes at this level. */ | |
487 | while (!NILP (pending_maps)) | |
488 | { | |
489 | Lisp_Object elt, eltcdr, string; | |
490 | elt = Fcar (pending_maps); | |
8e713be6 KR |
491 | eltcdr = XCDR (elt); |
492 | string = XCAR (eltcdr); | |
ee78dc32 | 493 | /* We no longer discard the @ from the beginning of the string here. |
485015ca GV |
494 | Instead, we do this in w32_menu_show. */ |
495 | single_keymap_panes (Fcar (elt), string, | |
8e713be6 | 496 | XCDR (eltcdr), notreal, maxdepth - 1); |
485015ca GV |
497 | pending_maps = Fcdr (pending_maps); |
498 | } | |
499 | } | |
500 | \f | |
501 | /* This is a subroutine of single_keymap_panes that handles one | |
502 | keymap entry. | |
177c0ea7 | 503 | KEY is a key in a keymap and ITEM is its binding. |
485015ca GV |
504 | PENDING_MAPS_PTR points to a list of keymaps waiting to be made into |
505 | separate panes. | |
506 | If NOTREAL is nonzero, only check for equivalent key bindings, don't | |
507 | evaluate expressions in menu items and don't make any menu. | |
37ad8b63 | 508 | If we encounter submenus deeper than MAXDEPTH levels, ignore them. */ |
485015ca GV |
509 | |
510 | static void | |
37ad8b63 | 511 | single_menu_item (key, item, pending_maps_ptr, notreal, maxdepth) |
485015ca GV |
512 | Lisp_Object key, item; |
513 | Lisp_Object *pending_maps_ptr; | |
514 | int maxdepth, notreal; | |
485015ca | 515 | { |
162d2499 | 516 | Lisp_Object map, item_string, enabled; |
485015ca GV |
517 | struct gcpro gcpro1, gcpro2; |
518 | int res; | |
177c0ea7 | 519 | |
485015ca GV |
520 | /* Parse the menu item and leave the result in item_properties. */ |
521 | GCPRO2 (key, item); | |
522 | res = parse_menu_item (item, notreal, 0); | |
523 | UNGCPRO; | |
524 | if (!res) | |
525 | return; /* Not a menu item. */ | |
526 | ||
6ed09cf0 | 527 | map = AREF (item_properties, ITEM_PROPERTY_MAP); |
177c0ea7 | 528 | |
485015ca GV |
529 | if (notreal) |
530 | { | |
531 | /* We don't want to make a menu, just traverse the keymaps to | |
532 | precompute equivalent key bindings. */ | |
533 | if (!NILP (map)) | |
534 | single_keymap_panes (map, Qnil, key, 1, maxdepth - 1); | |
535 | return; | |
536 | } | |
537 | ||
6ed09cf0 | 538 | enabled = AREF (item_properties, ITEM_PROPERTY_ENABLE); |
177c0ea7 | 539 | item_string = AREF (item_properties, ITEM_PROPERTY_NAME); |
485015ca | 540 | |
d5db4077 | 541 | if (!NILP (map) && SREF (item_string, 0) == '@') |
485015ca GV |
542 | { |
543 | if (!NILP (enabled)) | |
544 | /* An enabled separate pane. Remember this to handle it later. */ | |
545 | *pending_maps_ptr = Fcons (Fcons (map, Fcons (item_string, key)), | |
546 | *pending_maps_ptr); | |
547 | return; | |
548 | } | |
549 | ||
485015ca | 550 | push_menu_item (item_string, enabled, key, |
6ed09cf0 RS |
551 | AREF (item_properties, ITEM_PROPERTY_DEF), |
552 | AREF (item_properties, ITEM_PROPERTY_KEYEQ), | |
553 | AREF (item_properties, ITEM_PROPERTY_TYPE), | |
554 | AREF (item_properties, ITEM_PROPERTY_SELECTED), | |
555 | AREF (item_properties, ITEM_PROPERTY_HELP)); | |
485015ca | 556 | |
485015ca GV |
557 | /* Display a submenu using the toolkit. */ |
558 | if (! (NILP (map) || NILP (enabled))) | |
559 | { | |
560 | push_submenu_start (); | |
561 | single_keymap_panes (map, Qnil, key, 0, maxdepth - 1); | |
562 | push_submenu_end (); | |
ee78dc32 | 563 | } |
ee78dc32 GV |
564 | } |
565 | \f | |
8e6208c5 | 566 | /* Push all the panes and items of a menu described by the |
ee78dc32 GV |
567 | alist-of-alists MENU. |
568 | This handles old-fashioned calls to x-popup-menu. */ | |
569 | ||
485015ca GV |
570 | static void |
571 | list_of_panes (menu) | |
ee78dc32 GV |
572 | Lisp_Object menu; |
573 | { | |
574 | Lisp_Object tail; | |
014510b0 | 575 | |
485015ca | 576 | init_menu_items (); |
014510b0 | 577 | |
485015ca | 578 | for (tail = menu; !NILP (tail); tail = Fcdr (tail)) |
ee78dc32 GV |
579 | { |
580 | Lisp_Object elt, pane_name, pane_data; | |
485015ca | 581 | elt = Fcar (tail); |
ee78dc32 | 582 | pane_name = Fcar (elt); |
b7826503 | 583 | CHECK_STRING (pane_name); |
485015ca | 584 | push_menu_pane (pane_name, Qnil); |
ee78dc32 | 585 | pane_data = Fcdr (elt); |
b7826503 | 586 | CHECK_CONS (pane_data); |
485015ca | 587 | list_of_items (pane_data); |
ee78dc32 | 588 | } |
485015ca GV |
589 | |
590 | finish_menu_items (); | |
ee78dc32 GV |
591 | } |
592 | ||
593 | /* Push the items in a single pane defined by the alist PANE. */ | |
594 | ||
485015ca GV |
595 | static void |
596 | list_of_items (pane) | |
ee78dc32 GV |
597 | Lisp_Object pane; |
598 | { | |
599 | Lisp_Object tail, item, item1; | |
ee78dc32 | 600 | |
ee78dc32 GV |
601 | for (tail = pane; !NILP (tail); tail = Fcdr (tail)) |
602 | { | |
603 | item = Fcar (tail); | |
604 | if (STRINGP (item)) | |
162d2499 | 605 | push_menu_item (item, Qnil, Qnil, Qt, Qnil, Qnil, Qnil, Qnil); |
ee78dc32 | 606 | else if (NILP (item)) |
485015ca | 607 | push_left_right_boundary (); |
ee78dc32 GV |
608 | else |
609 | { | |
b7826503 | 610 | CHECK_CONS (item); |
ee78dc32 | 611 | item1 = Fcar (item); |
b7826503 | 612 | CHECK_STRING (item1); |
162d2499 | 613 | push_menu_item (item1, Qt, Fcdr (item), Qt, Qnil, Qnil, Qnil, Qnil); |
ee78dc32 GV |
614 | } |
615 | } | |
ee78dc32 GV |
616 | } |
617 | \f | |
485015ca | 618 | DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0, |
33f09670 JR |
619 | doc: /* Pop up a deck-of-cards menu and return user's selection. |
620 | POSITION is a position specification. This is either a mouse button | |
621 | event or a list ((XOFFSET YOFFSET) WINDOW) where XOFFSET and YOFFSET | |
622 | are positions in pixels from the top left corner of WINDOW's frame | |
623 | \(WINDOW may be a frame object instead of a window). This controls the | |
624 | position of the center of the first line in the first pane of the | |
625 | menu, not the top left of the menu as a whole. If POSITION is t, it | |
626 | means to use the current mouse position. | |
627 | ||
628 | MENU is a specifier for a menu. For the simplest case, MENU is a keymap. | |
629 | The menu items come from key bindings that have a menu string as well as | |
630 | a definition; actually, the \"definition\" in such a key binding looks like | |
631 | \(STRING . REAL-DEFINITION). To give the menu a title, put a string into | |
632 | the keymap as a top-level element. | |
633 | ||
634 | If REAL-DEFINITION is nil, that puts a nonselectable string in the menu. | |
635 | Otherwise, REAL-DEFINITION should be a valid key binding definition. | |
636 | ||
637 | You can also use a list of keymaps as MENU. Then each keymap makes a | |
638 | separate pane. When MENU is a keymap or a list of keymaps, the return | |
639 | value is a list of events. | |
640 | ||
641 | Alternatively, you can specify a menu of multiple panes with a list of | |
642 | the form (TITLE PANE1 PANE2...), where each pane is a list of | |
643 | form (TITLE ITEM1 ITEM2...). | |
644 | Each ITEM is normally a cons cell (STRING . VALUE); but a string can | |
645 | appear as an item--that makes a nonselectable line in the menu. | |
646 | With this form of menu, the return value is VALUE from the chosen item. | |
647 | ||
648 | If POSITION is nil, don't display the menu at all, just precalculate the | |
649 | cached information about equivalent key sequences. */) | |
485015ca GV |
650 | (position, menu) |
651 | Lisp_Object position, menu; | |
ee78dc32 | 652 | { |
ee78dc32 | 653 | Lisp_Object keymap, tem; |
b2123f81 | 654 | int xpos = 0, ypos = 0; |
ee78dc32 GV |
655 | Lisp_Object title; |
656 | char *error_name; | |
657 | Lisp_Object selection; | |
b2123f81 | 658 | FRAME_PTR f = NULL; |
ee78dc32 GV |
659 | Lisp_Object x, y, window; |
660 | int keymaps = 0; | |
485015ca | 661 | int for_click = 0; |
ee78dc32 | 662 | struct gcpro gcpro1; |
485015ca GV |
663 | |
664 | #ifdef HAVE_MENUS | |
ee78dc32 GV |
665 | if (! NILP (position)) |
666 | { | |
485015ca GV |
667 | check_w32 (); |
668 | ||
ee78dc32 | 669 | /* Decode the first argument: find the window and the coordinates. */ |
ef06882d | 670 | if (EQ (position, Qt) |
0b6bb670 JR |
671 | || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar) |
672 | || EQ (XCAR (position), Qtool_bar)))) | |
ee78dc32 GV |
673 | { |
674 | /* Use the mouse's current position. */ | |
162d2499 | 675 | FRAME_PTR new_f = SELECTED_FRAME (); |
ee78dc32 | 676 | Lisp_Object bar_window; |
162d2499 | 677 | enum scroll_bar_part part; |
ee78dc32 | 678 | unsigned long time; |
485015ca | 679 | |
ee78dc32 | 680 | if (mouse_position_hook) |
485015ca GV |
681 | (*mouse_position_hook) (&new_f, 1, &bar_window, |
682 | &part, &x, &y, &time); | |
ee78dc32 GV |
683 | if (new_f != 0) |
684 | XSETFRAME (window, new_f); | |
685 | else | |
686 | { | |
687 | window = selected_window; | |
688 | XSETFASTINT (x, 0); | |
689 | XSETFASTINT (y, 0); | |
690 | } | |
691 | } | |
692 | else | |
693 | { | |
694 | tem = Fcar (position); | |
695 | if (CONSP (tem)) | |
696 | { | |
697 | window = Fcar (Fcdr (position)); | |
698 | x = Fcar (tem); | |
699 | y = Fcar (Fcdr (tem)); | |
700 | } | |
701 | else | |
702 | { | |
485015ca GV |
703 | for_click = 1; |
704 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ | |
705 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
ee78dc32 GV |
706 | tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */ |
707 | x = Fcar (tem); | |
708 | y = Fcdr (tem); | |
ee78dc32 GV |
709 | } |
710 | } | |
485015ca | 711 | |
b7826503 PJ |
712 | CHECK_NUMBER (x); |
713 | CHECK_NUMBER (y); | |
ee78dc32 GV |
714 | |
715 | /* Decode where to put the menu. */ | |
485015ca | 716 | |
ee78dc32 GV |
717 | if (FRAMEP (window)) |
718 | { | |
719 | f = XFRAME (window); | |
ee78dc32 GV |
720 | xpos = 0; |
721 | ypos = 0; | |
722 | } | |
723 | else if (WINDOWP (window)) | |
724 | { | |
b7826503 | 725 | CHECK_LIVE_WINDOW (window); |
ee78dc32 | 726 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); |
485015ca | 727 | |
90022f5a KS |
728 | xpos = WINDOW_LEFT_EDGE_X (XWINDOW (window)); |
729 | ypos = WINDOW_TOP_EDGE_Y (XWINDOW (window)); | |
ee78dc32 GV |
730 | } |
731 | else | |
732 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
733 | but I don't want to make one now. */ | |
b7826503 | 734 | CHECK_WINDOW (window); |
485015ca | 735 | |
ee78dc32 GV |
736 | xpos += XINT (x); |
737 | ypos += XINT (y); | |
c8869655 GV |
738 | |
739 | XSETFRAME (Vmenu_updating_frame, f); | |
ee78dc32 | 740 | } |
c8869655 | 741 | Vmenu_updating_frame = Qnil; |
485015ca | 742 | #endif /* HAVE_MENUS */ |
c8869655 | 743 | |
ee78dc32 GV |
744 | title = Qnil; |
745 | GCPRO1 (title); | |
485015ca GV |
746 | |
747 | /* Decode the menu items from what was specified. */ | |
748 | ||
02067692 SM |
749 | keymap = get_keymap (menu, 0, 0); |
750 | if (CONSP (keymap)) | |
485015ca GV |
751 | { |
752 | /* We were given a keymap. Extract menu info from the keymap. */ | |
753 | Lisp_Object prompt; | |
485015ca GV |
754 | |
755 | /* Extract the detailed info to make one pane. */ | |
756 | keymap_panes (&menu, 1, NILP (position)); | |
757 | ||
758 | /* Search for a string appearing directly as an element of the keymap. | |
759 | That string is the title of the menu. */ | |
5ee707b8 | 760 | prompt = Fkeymap_prompt (keymap); |
485015ca GV |
761 | if (NILP (title) && !NILP (prompt)) |
762 | title = prompt; | |
763 | ||
764 | /* Make that be the pane title of the first pane. */ | |
765 | if (!NILP (prompt) && menu_items_n_panes >= 0) | |
6ed09cf0 | 766 | ASET (menu_items, MENU_ITEMS_PANE_NAME, prompt); |
485015ca GV |
767 | |
768 | keymaps = 1; | |
769 | } | |
02067692 | 770 | else if (CONSP (menu) && KEYMAPP (XCAR (menu))) |
485015ca GV |
771 | { |
772 | /* We were given a list of keymaps. */ | |
773 | int nmaps = XFASTINT (Flength (menu)); | |
774 | Lisp_Object *maps | |
775 | = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object)); | |
776 | int i; | |
777 | ||
778 | title = Qnil; | |
779 | ||
780 | /* The first keymap that has a prompt string | |
781 | supplies the menu title. */ | |
782 | for (tem = menu, i = 0; CONSP (tem); tem = Fcdr (tem)) | |
783 | { | |
784 | Lisp_Object prompt; | |
785 | ||
02067692 | 786 | maps[i++] = keymap = get_keymap (Fcar (tem), 1, 0); |
485015ca | 787 | |
5ee707b8 | 788 | prompt = Fkeymap_prompt (keymap); |
485015ca GV |
789 | if (NILP (title) && !NILP (prompt)) |
790 | title = prompt; | |
791 | } | |
792 | ||
793 | /* Extract the detailed info to make one pane. */ | |
794 | keymap_panes (maps, nmaps, NILP (position)); | |
795 | ||
796 | /* Make the title be the pane title of the first pane. */ | |
797 | if (!NILP (title) && menu_items_n_panes >= 0) | |
6ed09cf0 | 798 | ASET (menu_items, MENU_ITEMS_PANE_NAME, title); |
485015ca GV |
799 | |
800 | keymaps = 1; | |
801 | } | |
802 | else | |
803 | { | |
804 | /* We were given an old-fashioned menu. */ | |
805 | title = Fcar (menu); | |
b7826503 | 806 | CHECK_STRING (title); |
485015ca GV |
807 | |
808 | list_of_panes (Fcdr (menu)); | |
809 | ||
810 | keymaps = 0; | |
811 | } | |
177c0ea7 | 812 | |
ee78dc32 GV |
813 | if (NILP (position)) |
814 | { | |
485015ca | 815 | discard_menu_items (); |
ee78dc32 GV |
816 | UNGCPRO; |
817 | return Qnil; | |
818 | } | |
485015ca GV |
819 | |
820 | #ifdef HAVE_MENUS | |
6ed09cf0 RS |
821 | /* If resources from a previous popup menu exist yet, does nothing |
822 | until the `menu_free_timer' has freed them (see w32fns.c). | |
823 | */ | |
824 | if (current_popup_menu) | |
825 | { | |
826 | discard_menu_items (); | |
827 | UNGCPRO; | |
828 | return Qnil; | |
177c0ea7 JB |
829 | } |
830 | ||
ee78dc32 GV |
831 | /* Display them in a menu. */ |
832 | BLOCK_INPUT; | |
485015ca GV |
833 | |
834 | selection = w32_menu_show (f, xpos, ypos, for_click, | |
835 | keymaps, title, &error_name); | |
ee78dc32 | 836 | UNBLOCK_INPUT; |
485015ca GV |
837 | |
838 | discard_menu_items (); | |
6ed09cf0 | 839 | #endif /* HAVE_MENUS */ |
485015ca | 840 | |
ee78dc32 | 841 | UNGCPRO; |
485015ca | 842 | |
ee78dc32 GV |
843 | if (error_name) error (error_name); |
844 | return selection; | |
845 | } | |
846 | ||
485015ca GV |
847 | #ifdef HAVE_MENUS |
848 | ||
ee78dc32 | 849 | DEFUN ("x-popup-dialog", Fx_popup_dialog, Sx_popup_dialog, 2, 2, 0, |
33f09670 JR |
850 | doc: /* Pop up a dialog box and return user's selection. |
851 | POSITION specifies which frame to use. | |
852 | This is normally a mouse button event or a window or frame. | |
853 | If POSITION is t, it means to use the frame the mouse is on. | |
854 | The dialog box appears in the middle of the specified frame. | |
855 | ||
856 | CONTENTS specifies the alternatives to display in the dialog box. | |
857 | It is a list of the form (TITLE ITEM1 ITEM2...). | |
858 | Each ITEM is a cons cell (STRING . VALUE). | |
859 | The return value is VALUE from the chosen item. | |
860 | ||
861 | An ITEM may also be just a string--that makes a nonselectable item. | |
862 | An ITEM may also be nil--that means to put all preceding items | |
863 | on the left of the dialog box and all following items on the right. | |
864 | \(By default, approximately half appear on each side.) */) | |
485015ca GV |
865 | (position, contents) |
866 | Lisp_Object position, contents; | |
ee78dc32 | 867 | { |
b2123f81 | 868 | FRAME_PTR f = NULL; |
ee78dc32 | 869 | Lisp_Object window; |
485015ca GV |
870 | |
871 | check_w32 (); | |
872 | ||
ee78dc32 | 873 | /* Decode the first argument: find the window or frame to use. */ |
485015ca | 874 | if (EQ (position, Qt) |
0b6bb670 JR |
875 | || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar) |
876 | || EQ (XCAR (position), Qtool_bar)))) | |
ee78dc32 | 877 | { |
485015ca GV |
878 | #if 0 /* Using the frame the mouse is on may not be right. */ |
879 | /* Use the mouse's current position. */ | |
162d2499 | 880 | FRAME_PTR new_f = SELECTED_FRAME (); |
485015ca | 881 | Lisp_Object bar_window; |
15d36dee | 882 | enum scroll_bar_part part; |
485015ca GV |
883 | unsigned long time; |
884 | Lisp_Object x, y; | |
885 | ||
886 | (*mouse_position_hook) (&new_f, 1, &bar_window, &part, &x, &y, &time); | |
887 | ||
888 | if (new_f != 0) | |
889 | XSETFRAME (window, new_f); | |
890 | else | |
ee78dc32 | 891 | window = selected_window; |
485015ca GV |
892 | #endif |
893 | window = selected_window; | |
ee78dc32 GV |
894 | } |
895 | else if (CONSP (position)) | |
896 | { | |
897 | Lisp_Object tem; | |
898 | tem = Fcar (position); | |
485015ca | 899 | if (CONSP (tem)) |
ee78dc32 GV |
900 | window = Fcar (Fcdr (position)); |
901 | else | |
902 | { | |
485015ca GV |
903 | tem = Fcar (Fcdr (position)); /* EVENT_START (position) */ |
904 | window = Fcar (tem); /* POSN_WINDOW (tem) */ | |
ee78dc32 GV |
905 | } |
906 | } | |
907 | else if (WINDOWP (position) || FRAMEP (position)) | |
908 | window = position; | |
485015ca GV |
909 | else |
910 | window = Qnil; | |
911 | ||
ee78dc32 | 912 | /* Decode where to put the menu. */ |
485015ca | 913 | |
ee78dc32 GV |
914 | if (FRAMEP (window)) |
915 | f = XFRAME (window); | |
916 | else if (WINDOWP (window)) | |
917 | { | |
b7826503 | 918 | CHECK_LIVE_WINDOW (window); |
ee78dc32 GV |
919 | f = XFRAME (WINDOW_FRAME (XWINDOW (window))); |
920 | } | |
921 | else | |
922 | /* ??? Not really clean; should be CHECK_WINDOW_OR_FRAME, | |
923 | but I don't want to make one now. */ | |
b7826503 | 924 | CHECK_WINDOW (window); |
485015ca | 925 | |
162d2499 | 926 | #ifndef HAVE_DIALOGS |
ee78dc32 GV |
927 | /* Display a menu with these alternatives |
928 | in the middle of frame F. */ | |
929 | { | |
930 | Lisp_Object x, y, frame, newpos; | |
931 | XSETFRAME (frame, f); | |
932 | XSETINT (x, x_pixel_width (f) / 2); | |
933 | XSETINT (y, x_pixel_height (f) / 2); | |
934 | newpos = Fcons (Fcons (x, Fcons (y, Qnil)), Fcons (frame, Qnil)); | |
935 | ||
936 | return Fx_popup_menu (newpos, | |
937 | Fcons (Fcar (contents), Fcons (contents, Qnil))); | |
938 | } | |
162d2499 | 939 | #else /* HAVE_DIALOGS */ |
ee78dc32 GV |
940 | { |
941 | Lisp_Object title; | |
942 | char *error_name; | |
943 | Lisp_Object selection; | |
944 | ||
945 | /* Decode the dialog items from what was specified. */ | |
946 | title = Fcar (contents); | |
b7826503 | 947 | CHECK_STRING (title); |
ee78dc32 GV |
948 | |
949 | list_of_panes (Fcons (contents, Qnil)); | |
950 | ||
951 | /* Display them in a dialog box. */ | |
952 | BLOCK_INPUT; | |
485015ca | 953 | selection = w32_dialog_show (f, 0, title, &error_name); |
ee78dc32 GV |
954 | UNBLOCK_INPUT; |
955 | ||
956 | discard_menu_items (); | |
957 | ||
958 | if (error_name) error (error_name); | |
959 | return selection; | |
960 | } | |
162d2499 | 961 | #endif /* HAVE_DIALOGS */ |
ee78dc32 GV |
962 | } |
963 | ||
014510b0 GV |
964 | /* Activate the menu bar of frame F. |
965 | This is called from keyboard.c when it gets the | |
3b8f9651 | 966 | MENU_BAR_ACTIVATE_EVENT out of the Emacs event queue. |
014510b0 GV |
967 | |
968 | To activate the menu bar, we signal to the input thread that it can | |
969 | return from the WM_INITMENU message, allowing the normal Windows | |
970 | processing of the menus. | |
971 | ||
972 | But first we recompute the menu bar contents (the whole tree). | |
973 | ||
974 | This way we can safely execute Lisp code. */ | |
177c0ea7 | 975 | |
162d2499 | 976 | void |
014510b0 GV |
977 | x_activate_menubar (f) |
978 | FRAME_PTR f; | |
979 | { | |
980 | set_frame_menubar (f, 0, 1); | |
981 | ||
982 | /* Lock out further menubar changes while active. */ | |
983 | f->output_data.w32->menubar_active = 1; | |
984 | ||
985 | /* Signal input thread to return from WM_INITMENU. */ | |
986 | complete_deferred_msg (FRAME_W32_WINDOW (f), WM_INITMENU, 0); | |
987 | } | |
988 | ||
485015ca GV |
989 | /* This callback is called from the menu bar pulldown menu |
990 | when the user makes a selection. | |
991 | Figure out what the user chose | |
992 | and put the appropriate events into the keyboard buffer. */ | |
993 | ||
994 | void | |
995 | menubar_selection_callback (FRAME_PTR f, void * client_data) | |
ee78dc32 | 996 | { |
485015ca GV |
997 | Lisp_Object prefix, entry; |
998 | Lisp_Object vector; | |
999 | Lisp_Object *subprefix_stack; | |
1000 | int submenu_depth = 0; | |
ee78dc32 | 1001 | int i; |
fdc12c4d | 1002 | |
485015ca | 1003 | if (!f) |
014510b0 | 1004 | return; |
fe5a0162 | 1005 | entry = Qnil; |
485015ca GV |
1006 | subprefix_stack = (Lisp_Object *) alloca (f->menu_bar_items_used * sizeof (Lisp_Object)); |
1007 | vector = f->menu_bar_vector; | |
1008 | prefix = Qnil; | |
1009 | i = 0; | |
1010 | while (i < f->menu_bar_items_used) | |
1011 | { | |
6ed09cf0 | 1012 | if (EQ (AREF (vector, i), Qnil)) |
485015ca GV |
1013 | { |
1014 | subprefix_stack[submenu_depth++] = prefix; | |
1015 | prefix = entry; | |
1016 | i++; | |
1017 | } | |
6ed09cf0 | 1018 | else if (EQ (AREF (vector, i), Qlambda)) |
485015ca GV |
1019 | { |
1020 | prefix = subprefix_stack[--submenu_depth]; | |
1021 | i++; | |
1022 | } | |
6ed09cf0 | 1023 | else if (EQ (AREF (vector, i), Qt)) |
485015ca | 1024 | { |
6ed09cf0 | 1025 | prefix = AREF (vector, i + MENU_ITEMS_PANE_PREFIX); |
485015ca GV |
1026 | i += MENU_ITEMS_PANE_LENGTH; |
1027 | } | |
1028 | else | |
1029 | { | |
6ed09cf0 | 1030 | entry = AREF (vector, i + MENU_ITEMS_ITEM_VALUE); |
485015ca GV |
1031 | /* The EMACS_INT cast avoids a warning. There's no problem |
1032 | as long as pointers have enough bits to hold small integers. */ | |
1033 | if ((int) (EMACS_INT) client_data == i) | |
1034 | { | |
1035 | int j; | |
1036 | struct input_event buf; | |
1037 | Lisp_Object frame; | |
f456401b | 1038 | EVENT_INIT (buf); |
014510b0 | 1039 | |
485015ca | 1040 | XSETFRAME (frame, f); |
20501278 GM |
1041 | buf.kind = MENU_BAR_EVENT; |
1042 | buf.frame_or_window = frame; | |
1043 | buf.arg = frame; | |
485015ca | 1044 | kbd_buffer_store_event (&buf); |
014510b0 | 1045 | |
485015ca GV |
1046 | for (j = 0; j < submenu_depth; j++) |
1047 | if (!NILP (subprefix_stack[j])) | |
1048 | { | |
20501278 GM |
1049 | buf.kind = MENU_BAR_EVENT; |
1050 | buf.frame_or_window = frame; | |
1051 | buf.arg = subprefix_stack[j]; | |
485015ca GV |
1052 | kbd_buffer_store_event (&buf); |
1053 | } | |
fdc12c4d | 1054 | |
485015ca GV |
1055 | if (!NILP (prefix)) |
1056 | { | |
20501278 GM |
1057 | buf.kind = MENU_BAR_EVENT; |
1058 | buf.frame_or_window = frame; | |
1059 | buf.arg = prefix; | |
485015ca GV |
1060 | kbd_buffer_store_event (&buf); |
1061 | } | |
fdc12c4d | 1062 | |
20501278 GM |
1063 | buf.kind = MENU_BAR_EVENT; |
1064 | buf.frame_or_window = frame; | |
1065 | buf.arg = entry; | |
485015ca | 1066 | kbd_buffer_store_event (&buf); |
fdc12c4d | 1067 | |
71f6b295 JR |
1068 | /* Free memory used by owner-drawn and help-echo strings. */ |
1069 | w32_free_menu_strings (FRAME_W32_WINDOW (f)); | |
1070 | f->output_data.w32->menu_command_in_progress = 0; | |
1071 | f->output_data.w32->menubar_active = 0; | |
485015ca GV |
1072 | return; |
1073 | } | |
1074 | i += MENU_ITEMS_ITEM_LENGTH; | |
1075 | } | |
014510b0 | 1076 | } |
71f6b295 JR |
1077 | /* Free memory used by owner-drawn and help-echo strings. */ |
1078 | w32_free_menu_strings (FRAME_W32_WINDOW (f)); | |
1079 | f->output_data.w32->menu_command_in_progress = 0; | |
1080 | f->output_data.w32->menubar_active = 0; | |
485015ca | 1081 | } |
014510b0 | 1082 | |
485015ca | 1083 | /* Allocate a widget_value, blocking input. */ |
014510b0 | 1084 | |
485015ca GV |
1085 | widget_value * |
1086 | xmalloc_widget_value () | |
1087 | { | |
1088 | widget_value *value; | |
014510b0 | 1089 | |
fdc12c4d | 1090 | BLOCK_INPUT; |
485015ca | 1091 | value = malloc_widget_value (); |
ee78dc32 | 1092 | UNBLOCK_INPUT; |
485015ca GV |
1093 | |
1094 | return value; | |
ee78dc32 GV |
1095 | } |
1096 | ||
485015ca GV |
1097 | /* This recursively calls free_widget_value on the tree of widgets. |
1098 | It must free all data that was malloc'ed for these widget_values. | |
1099 | In Emacs, many slots are pointers into the data of Lisp_Strings, and | |
1100 | must be left alone. */ | |
1101 | ||
1102 | void | |
1103 | free_menubar_widget_value_tree (wv) | |
1104 | widget_value *wv; | |
ee78dc32 | 1105 | { |
485015ca | 1106 | if (! wv) return; |
177c0ea7 | 1107 | |
485015ca GV |
1108 | wv->name = wv->value = wv->key = (char *) 0xDEADBEEF; |
1109 | ||
1110 | if (wv->contents && (wv->contents != (widget_value*)1)) | |
1111 | { | |
1112 | free_menubar_widget_value_tree (wv->contents); | |
1113 | wv->contents = (widget_value *) 0xDEADBEEF; | |
1114 | } | |
1115 | if (wv->next) | |
1116 | { | |
1117 | free_menubar_widget_value_tree (wv->next); | |
1118 | wv->next = (widget_value *) 0xDEADBEEF; | |
1119 | } | |
1120 | BLOCK_INPUT; | |
1121 | free_widget_value (wv); | |
ee78dc32 GV |
1122 | UNBLOCK_INPUT; |
1123 | } | |
ee78dc32 | 1124 | \f |
6ed09cf0 | 1125 | /* Set up data i menu_items for a menu bar item |
485015ca GV |
1126 | whose event type is ITEM_KEY (with string ITEM_NAME) |
1127 | and whose contents come from the list of keymaps MAPS. */ | |
ee78dc32 | 1128 | |
6ed09cf0 RS |
1129 | static int |
1130 | parse_single_submenu (item_key, item_name, maps) | |
485015ca | 1131 | Lisp_Object item_key, item_name, maps; |
ee78dc32 | 1132 | { |
485015ca GV |
1133 | Lisp_Object length; |
1134 | int len; | |
1135 | Lisp_Object *mapvec; | |
6ed09cf0 | 1136 | int i; |
485015ca | 1137 | int top_level_items = 0; |
ee78dc32 | 1138 | |
485015ca GV |
1139 | length = Flength (maps); |
1140 | len = XINT (length); | |
ee78dc32 | 1141 | |
485015ca GV |
1142 | /* Convert the list MAPS into a vector MAPVEC. */ |
1143 | mapvec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object)); | |
1144 | for (i = 0; i < len; i++) | |
ee78dc32 | 1145 | { |
485015ca GV |
1146 | mapvec[i] = Fcar (maps); |
1147 | maps = Fcdr (maps); | |
ee78dc32 GV |
1148 | } |
1149 | ||
485015ca GV |
1150 | /* Loop over the given keymaps, making a pane for each map. |
1151 | But don't make a pane that is empty--ignore that map instead. */ | |
1152 | for (i = 0; i < len; i++) | |
ee78dc32 | 1153 | { |
485015ca | 1154 | if (SYMBOLP (mapvec[i]) |
02067692 | 1155 | || (CONSP (mapvec[i]) && !KEYMAPP (mapvec[i]))) |
485015ca GV |
1156 | { |
1157 | /* Here we have a command at top level in the menu bar | |
1158 | as opposed to a submenu. */ | |
1159 | top_level_items = 1; | |
1160 | push_menu_pane (Qnil, Qnil); | |
162d2499 JR |
1161 | push_menu_item (item_name, Qt, item_key, mapvec[i], |
1162 | Qnil, Qnil, Qnil, Qnil); | |
485015ca GV |
1163 | } |
1164 | else | |
fe04b8c8 JR |
1165 | { |
1166 | Lisp_Object prompt; | |
1167 | prompt = Fkeymap_prompt (mapvec[i]); | |
1168 | single_keymap_panes (mapvec[i], | |
1169 | !NILP (prompt) ? prompt : item_name, | |
1170 | item_key, 0, 10); | |
1171 | } | |
ee78dc32 | 1172 | } |
177c0ea7 | 1173 | |
6ed09cf0 RS |
1174 | return top_level_items; |
1175 | } | |
485015ca | 1176 | |
6ed09cf0 RS |
1177 | |
1178 | /* Create a tree of widget_value objects | |
1179 | representing the panes and items | |
1180 | in menu_items starting at index START, up to index END. */ | |
1181 | ||
1182 | static widget_value * | |
1183 | digest_single_submenu (start, end, top_level_items) | |
fe04b8c8 | 1184 | int start, end, top_level_items; |
6ed09cf0 RS |
1185 | { |
1186 | widget_value *wv, *prev_wv, *save_wv, *first_wv; | |
1187 | int i; | |
1188 | int submenu_depth = 0; | |
1189 | widget_value **submenu_stack; | |
485015ca GV |
1190 | |
1191 | submenu_stack | |
1192 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
1193 | wv = xmalloc_widget_value (); | |
1194 | wv->name = "menu"; | |
1195 | wv->value = 0; | |
1196 | wv->enabled = 1; | |
162d2499 | 1197 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 1198 | wv->help = Qnil; |
485015ca GV |
1199 | first_wv = wv; |
1200 | save_wv = 0; | |
1201 | prev_wv = 0; | |
177c0ea7 | 1202 | |
fe04b8c8 JR |
1203 | /* Loop over all panes and items made by the preceding call |
1204 | to parse_single_submenu and construct a tree of widget_value objects. | |
1205 | Ignore the panes and items used by previous calls to | |
1206 | digest_single_submenu, even though those are also in menu_items. */ | |
6ed09cf0 RS |
1207 | i = start; |
1208 | while (i < end) | |
ee78dc32 | 1209 | { |
6ed09cf0 | 1210 | if (EQ (AREF (menu_items, i), Qnil)) |
ee78dc32 | 1211 | { |
485015ca GV |
1212 | submenu_stack[submenu_depth++] = save_wv; |
1213 | save_wv = prev_wv; | |
1214 | prev_wv = 0; | |
ee78dc32 GV |
1215 | i++; |
1216 | } | |
6ed09cf0 | 1217 | else if (EQ (AREF (menu_items, i), Qlambda)) |
ee78dc32 | 1218 | { |
485015ca GV |
1219 | prev_wv = save_wv; |
1220 | save_wv = submenu_stack[--submenu_depth]; | |
ee78dc32 GV |
1221 | i++; |
1222 | } | |
6ed09cf0 | 1223 | else if (EQ (AREF (menu_items, i), Qt) |
485015ca GV |
1224 | && submenu_depth != 0) |
1225 | i += MENU_ITEMS_PANE_LENGTH; | |
ee78dc32 GV |
1226 | /* Ignore a nil in the item list. |
1227 | It's meaningful only for dialog boxes. */ | |
6ed09cf0 | 1228 | else if (EQ (AREF (menu_items, i), Qquote)) |
485015ca | 1229 | i += 1; |
6ed09cf0 | 1230 | else if (EQ (AREF (menu_items, i), Qt)) |
485015ca GV |
1231 | { |
1232 | /* Create a new pane. */ | |
1233 | Lisp_Object pane_name, prefix; | |
1234 | char *pane_string; | |
29250851 | 1235 | |
6ed09cf0 RS |
1236 | pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME); |
1237 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); | |
29250851 | 1238 | |
162d2499 JR |
1239 | #ifndef HAVE_MULTILINGUAL_MENU |
1240 | if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name)) | |
29250851 JR |
1241 | { |
1242 | pane_name = ENCODE_SYSTEM (pane_name); | |
6ed09cf0 | 1243 | ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name); |
29250851 | 1244 | } |
162d2499 | 1245 | #endif |
485015ca | 1246 | pane_string = (NILP (pane_name) |
d5db4077 | 1247 | ? "" : (char *) SDATA (pane_name)); |
485015ca GV |
1248 | /* If there is just one top-level pane, put all its items directly |
1249 | under the top-level menu. */ | |
1250 | if (menu_items_n_panes == 1) | |
1251 | pane_string = ""; | |
1252 | ||
1253 | /* If the pane has a meaningful name, | |
1254 | make the pane a top-level menu item | |
1255 | with its items as a submenu beneath it. */ | |
1256 | if (strcmp (pane_string, "")) | |
1257 | { | |
1258 | wv = xmalloc_widget_value (); | |
1259 | if (save_wv) | |
1260 | save_wv->next = wv; | |
1261 | else | |
1262 | first_wv->contents = wv; | |
1263 | wv->name = pane_string; | |
1264 | /* Ignore the @ that means "separate pane". | |
1265 | This is a kludge, but this isn't worth more time. */ | |
1266 | if (!NILP (prefix) && wv->name[0] == '@') | |
1267 | wv->name++; | |
1268 | wv->value = 0; | |
1269 | wv->enabled = 1; | |
162d2499 | 1270 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 1271 | wv->help = Qnil; |
485015ca GV |
1272 | } |
1273 | save_wv = wv; | |
1274 | prev_wv = 0; | |
1275 | i += MENU_ITEMS_PANE_LENGTH; | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | /* Create a new item within current pane. */ | |
162d2499 JR |
1280 | Lisp_Object item_name, enable, descrip, def, type, selected; |
1281 | Lisp_Object help; | |
7d8e57da | 1282 | |
29250851 JR |
1283 | item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); |
1284 | enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); | |
1285 | descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); | |
1286 | def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION); | |
1287 | type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE); | |
1288 | selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED); | |
1289 | help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP); | |
162d2499 JR |
1290 | |
1291 | #ifndef HAVE_MULTILINGUAL_MENU | |
1292 | if (STRING_MULTIBYTE (item_name)) | |
29250851 JR |
1293 | { |
1294 | item_name = ENCODE_SYSTEM (item_name); | |
6ed09cf0 | 1295 | ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name); |
29250851 JR |
1296 | } |
1297 | ||
162d2499 | 1298 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) |
29250851 JR |
1299 | { |
1300 | descrip = ENCODE_SYSTEM (descrip); | |
6ed09cf0 | 1301 | ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip); |
29250851 JR |
1302 | } |
1303 | #endif /* not HAVE_MULTILINGUAL_MENU */ | |
485015ca GV |
1304 | |
1305 | wv = xmalloc_widget_value (); | |
177c0ea7 | 1306 | if (prev_wv) |
485015ca GV |
1307 | prev_wv->next = wv; |
1308 | else | |
1309 | save_wv->contents = wv; | |
1310 | ||
d5db4077 | 1311 | wv->name = (char *) SDATA (item_name); |
485015ca | 1312 | if (!NILP (descrip)) |
d5db4077 | 1313 | wv->key = (char *) SDATA (descrip); |
485015ca GV |
1314 | wv->value = 0; |
1315 | /* The EMACS_INT cast avoids a warning. There's no problem | |
1316 | as long as pointers have enough bits to hold small integers. */ | |
1317 | wv->call_data = (!NILP (def) ? (void *) (EMACS_INT) i : 0); | |
1318 | wv->enabled = !NILP (enable); | |
162d2499 JR |
1319 | |
1320 | if (NILP (type)) | |
1321 | wv->button_type = BUTTON_TYPE_NONE; | |
1322 | else if (EQ (type, QCradio)) | |
1323 | wv->button_type = BUTTON_TYPE_RADIO; | |
1324 | else if (EQ (type, QCtoggle)) | |
1325 | wv->button_type = BUTTON_TYPE_TOGGLE; | |
1326 | else | |
1327 | abort (); | |
1328 | ||
1329 | wv->selected = !NILP (selected); | |
d1fffd1d JR |
1330 | if (!STRINGP (help)) |
1331 | help = Qnil; | |
1332 | ||
1333 | wv->help = help; | |
7d8e57da | 1334 | |
485015ca GV |
1335 | prev_wv = wv; |
1336 | ||
1337 | i += MENU_ITEMS_ITEM_LENGTH; | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | /* If we have just one "menu item" | |
1342 | that was originally a button, return it by itself. */ | |
1343 | if (top_level_items && first_wv->contents && first_wv->contents->next == 0) | |
1344 | { | |
1345 | wv = first_wv->contents; | |
1346 | free_widget_value (first_wv); | |
1347 | return wv; | |
1348 | } | |
1349 | ||
1350 | return first_wv; | |
1351 | } | |
1352 | \f | |
1353 | /* Set the contents of the menubar widgets of frame F. | |
1354 | The argument FIRST_TIME is currently ignored; | |
1355 | it is set the first time this is called, from initialize_frame_menubar. */ | |
1356 | ||
1357 | void | |
1358 | set_frame_menubar (f, first_time, deep_p) | |
1359 | FRAME_PTR f; | |
1360 | int first_time; | |
1361 | int deep_p; | |
1362 | { | |
1363 | HMENU menubar_widget = f->output_data.w32->menubar_widget; | |
162d2499 | 1364 | Lisp_Object items; |
485015ca | 1365 | widget_value *wv, *first_wv, *prev_wv = 0; |
6ed09cf0 RS |
1366 | int i, last_i; |
1367 | int *submenu_start, *submenu_end; | |
fe04b8c8 | 1368 | int *submenu_top_level_items, *submenu_n_panes; |
485015ca GV |
1369 | |
1370 | /* We must not change the menubar when actually in use. */ | |
1371 | if (f->output_data.w32->menubar_active) | |
1372 | return; | |
1373 | ||
1374 | XSETFRAME (Vmenu_updating_frame, f); | |
1375 | ||
1376 | if (! menubar_widget) | |
1377 | deep_p = 1; | |
1378 | else if (pending_menu_activation && !deep_p) | |
1379 | deep_p = 1; | |
1380 | ||
485015ca GV |
1381 | if (deep_p) |
1382 | { | |
1383 | /* Make a widget-value tree representing the entire menu trees. */ | |
1384 | ||
1385 | struct buffer *prev = current_buffer; | |
1386 | Lisp_Object buffer; | |
aed13378 | 1387 | int specpdl_count = SPECPDL_INDEX (); |
485015ca GV |
1388 | int previous_menu_items_used = f->menu_bar_items_used; |
1389 | Lisp_Object *previous_items | |
1390 | = (Lisp_Object *) alloca (previous_menu_items_used | |
1391 | * sizeof (Lisp_Object)); | |
1392 | ||
1393 | /* If we are making a new widget, its contents are empty, | |
1394 | do always reinitialize them. */ | |
1395 | if (! menubar_widget) | |
1396 | previous_menu_items_used = 0; | |
1397 | ||
1398 | buffer = XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer; | |
1399 | specbind (Qinhibit_quit, Qt); | |
1400 | /* Don't let the debugger step into this code | |
1401 | because it is not reentrant. */ | |
1402 | specbind (Qdebug_on_next_call, Qnil); | |
1403 | ||
1404 | record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil)); | |
1405 | if (NILP (Voverriding_local_map_menu_flag)) | |
1406 | { | |
1407 | specbind (Qoverriding_terminal_local_map, Qnil); | |
1408 | specbind (Qoverriding_local_map, Qnil); | |
1409 | } | |
1410 | ||
1411 | set_buffer_internal_1 (XBUFFER (buffer)); | |
1412 | ||
1413 | /* Run the Lucid hook. */ | |
c53b6500 | 1414 | safe_run_hooks (Qactivate_menubar_hook); |
485015ca GV |
1415 | /* If it has changed current-menubar from previous value, |
1416 | really recompute the menubar from the value. */ | |
1417 | if (! NILP (Vlucid_menu_bar_dirty_flag)) | |
1418 | call0 (Qrecompute_lucid_menubar); | |
1419 | safe_run_hooks (Qmenu_bar_update_hook); | |
1420 | FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f)); | |
1421 | ||
1422 | items = FRAME_MENU_BAR_ITEMS (f); | |
1423 | ||
485015ca | 1424 | /* Save the frame's previous menu bar contents data. */ |
c30dce3d AI |
1425 | if (previous_menu_items_used) |
1426 | bcopy (XVECTOR (f->menu_bar_vector)->contents, previous_items, | |
1427 | previous_menu_items_used * sizeof (Lisp_Object)); | |
485015ca | 1428 | |
6ed09cf0 RS |
1429 | /* Fill in menu_items with the current menu bar contents. |
1430 | This can evaluate Lisp code. */ | |
485015ca | 1431 | menu_items = f->menu_bar_vector; |
c30dce3d | 1432 | menu_items_allocated = VECTORP (menu_items) ? ASIZE (menu_items) : 0; |
6ed09cf0 RS |
1433 | submenu_start = (int *) alloca (XVECTOR (items)->size * sizeof (int *)); |
1434 | submenu_end = (int *) alloca (XVECTOR (items)->size * sizeof (int *)); | |
fe04b8c8 | 1435 | submenu_n_panes = (int *) alloca (XVECTOR (items)->size * sizeof (int)); |
6ed09cf0 RS |
1436 | submenu_top_level_items |
1437 | = (int *) alloca (XVECTOR (items)->size * sizeof (int *)); | |
485015ca | 1438 | init_menu_items (); |
6ed09cf0 | 1439 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
1440 | { |
1441 | Lisp_Object key, string, maps; | |
1442 | ||
6ed09cf0 RS |
1443 | last_i = i; |
1444 | ||
1445 | key = AREF (items, i); | |
1446 | string = AREF (items, i + 1); | |
1447 | maps = AREF (items, i + 2); | |
485015ca GV |
1448 | if (NILP (string)) |
1449 | break; | |
1450 | ||
6ed09cf0 RS |
1451 | submenu_start[i] = menu_items_used; |
1452 | ||
1453 | menu_items_n_panes = 0; | |
1454 | submenu_top_level_items[i] | |
1455 | = parse_single_submenu (key, string, maps); | |
fe04b8c8 | 1456 | submenu_n_panes[i] = menu_items_n_panes; |
6ed09cf0 RS |
1457 | |
1458 | submenu_end[i] = menu_items_used; | |
1459 | } | |
1460 | ||
1461 | finish_menu_items (); | |
1462 | ||
1463 | /* Convert menu_items into widget_value trees | |
1464 | to display the menu. This cannot evaluate Lisp code. */ | |
1465 | ||
1466 | wv = xmalloc_widget_value (); | |
1467 | wv->name = "menubar"; | |
1468 | wv->value = 0; | |
1469 | wv->enabled = 1; | |
1470 | wv->button_type = BUTTON_TYPE_NONE; | |
1471 | wv->help = Qnil; | |
1472 | first_wv = wv; | |
1473 | ||
1474 | for (i = 0; i < last_i; i += 4) | |
1475 | { | |
fe04b8c8 | 1476 | menu_items_n_panes = submenu_n_panes[i]; |
6ed09cf0 RS |
1477 | wv = digest_single_submenu (submenu_start[i], submenu_end[i], |
1478 | submenu_top_level_items[i]); | |
177c0ea7 | 1479 | if (prev_wv) |
485015ca GV |
1480 | prev_wv->next = wv; |
1481 | else | |
1482 | first_wv->contents = wv; | |
1483 | /* Don't set wv->name here; GC during the loop might relocate it. */ | |
1484 | wv->enabled = 1; | |
162d2499 | 1485 | wv->button_type = BUTTON_TYPE_NONE; |
485015ca GV |
1486 | prev_wv = wv; |
1487 | } | |
1488 | ||
485015ca GV |
1489 | set_buffer_internal_1 (prev); |
1490 | unbind_to (specpdl_count, Qnil); | |
1491 | ||
1492 | /* If there has been no change in the Lisp-level contents | |
1493 | of the menu bar, skip redisplaying it. Just exit. */ | |
1494 | ||
1495 | for (i = 0; i < previous_menu_items_used; i++) | |
1496 | if (menu_items_used == i | |
6ed09cf0 | 1497 | || (!EQ (previous_items[i], AREF (menu_items, i)))) |
485015ca GV |
1498 | break; |
1499 | if (i == menu_items_used && i == previous_menu_items_used && i != 0) | |
1500 | { | |
1501 | free_menubar_widget_value_tree (first_wv); | |
1502 | menu_items = Qnil; | |
1503 | ||
1504 | return; | |
1505 | } | |
1506 | ||
1507 | /* Now GC cannot happen during the lifetime of the widget_value, | |
ace9b298 JR |
1508 | so it's safe to store data from a Lisp_String, as long as |
1509 | local copies are made when the actual menu is created. | |
1510 | Windows takes care of this for normal string items, but | |
1511 | not for owner-drawn items or additional item-info. */ | |
485015ca | 1512 | wv = first_wv->contents; |
6ed09cf0 | 1513 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
1514 | { |
1515 | Lisp_Object string; | |
6ed09cf0 | 1516 | string = AREF (items, i + 1); |
485015ca GV |
1517 | if (NILP (string)) |
1518 | break; | |
d5db4077 | 1519 | wv->name = (char *) SDATA (string); |
485015ca GV |
1520 | wv = wv->next; |
1521 | } | |
1522 | ||
1523 | f->menu_bar_vector = menu_items; | |
1524 | f->menu_bar_items_used = menu_items_used; | |
1525 | menu_items = Qnil; | |
1526 | } | |
1527 | else | |
1528 | { | |
1529 | /* Make a widget-value tree containing | |
162d2499 | 1530 | just the top level menu bar strings. */ |
485015ca | 1531 | |
6ed09cf0 RS |
1532 | wv = xmalloc_widget_value (); |
1533 | wv->name = "menubar"; | |
1534 | wv->value = 0; | |
1535 | wv->enabled = 1; | |
1536 | wv->button_type = BUTTON_TYPE_NONE; | |
1537 | wv->help = Qnil; | |
1538 | first_wv = wv; | |
1539 | ||
485015ca | 1540 | items = FRAME_MENU_BAR_ITEMS (f); |
6ed09cf0 | 1541 | for (i = 0; i < ASIZE (items); i += 4) |
485015ca GV |
1542 | { |
1543 | Lisp_Object string; | |
1544 | ||
6ed09cf0 | 1545 | string = AREF (items, i + 1); |
485015ca GV |
1546 | if (NILP (string)) |
1547 | break; | |
1548 | ||
1549 | wv = xmalloc_widget_value (); | |
d5db4077 | 1550 | wv->name = (char *) SDATA (string); |
485015ca GV |
1551 | wv->value = 0; |
1552 | wv->enabled = 1; | |
162d2499 | 1553 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 1554 | wv->help = Qnil; |
485015ca GV |
1555 | /* This prevents lwlib from assuming this |
1556 | menu item is really supposed to be empty. */ | |
1557 | /* The EMACS_INT cast avoids a warning. | |
1558 | This value just has to be different from small integers. */ | |
1559 | wv->call_data = (void *) (EMACS_INT) (-1); | |
1560 | ||
177c0ea7 | 1561 | if (prev_wv) |
485015ca GV |
1562 | prev_wv->next = wv; |
1563 | else | |
1564 | first_wv->contents = wv; | |
1565 | prev_wv = wv; | |
1566 | } | |
1567 | ||
162d2499 JR |
1568 | /* Forget what we thought we knew about what is in the |
1569 | detailed contents of the menu bar menus. | |
1570 | Changing the top level always destroys the contents. */ | |
1571 | f->menu_bar_items_used = 0; | |
485015ca GV |
1572 | } |
1573 | ||
1574 | /* Create or update the menu bar widget. */ | |
ee78dc32 | 1575 | |
485015ca | 1576 | BLOCK_INPUT; |
ee78dc32 | 1577 | |
485015ca GV |
1578 | if (menubar_widget) |
1579 | { | |
1580 | /* Empty current menubar, rather than creating a fresh one. */ | |
1581 | while (DeleteMenu (menubar_widget, 0, MF_BYPOSITION)) | |
1582 | ; | |
1583 | } | |
1584 | else | |
1585 | { | |
1586 | menubar_widget = CreateMenu (); | |
1587 | } | |
1588 | fill_in_menu (menubar_widget, first_wv->contents); | |
ee78dc32 | 1589 | |
485015ca | 1590 | free_menubar_widget_value_tree (first_wv); |
ee78dc32 | 1591 | |
485015ca GV |
1592 | { |
1593 | HMENU old_widget = f->output_data.w32->menubar_widget; | |
ee78dc32 | 1594 | |
485015ca GV |
1595 | f->output_data.w32->menubar_widget = menubar_widget; |
1596 | SetMenu (FRAME_W32_WINDOW (f), f->output_data.w32->menubar_widget); | |
177c0ea7 | 1597 | /* Causes flicker when menu bar is updated |
485015ca | 1598 | DrawMenuBar (FRAME_W32_WINDOW (f)); */ |
ee78dc32 | 1599 | |
485015ca GV |
1600 | /* Force the window size to be recomputed so that the frame's text |
1601 | area remains the same, if menubar has just been created. */ | |
1602 | if (old_widget == NULL) | |
90022f5a | 1603 | x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f)); |
ee78dc32 | 1604 | } |
485015ca GV |
1605 | |
1606 | UNBLOCK_INPUT; | |
ee78dc32 GV |
1607 | } |
1608 | ||
485015ca GV |
1609 | /* Called from Fx_create_frame to create the initial menubar of a frame |
1610 | before it is mapped, so that the window is mapped with the menubar already | |
1611 | there instead of us tacking it on later and thrashing the window after it | |
1612 | is visible. */ | |
1613 | ||
1614 | void | |
1615 | initialize_frame_menubar (f) | |
1616 | FRAME_PTR f; | |
1617 | { | |
1618 | /* This function is called before the first chance to redisplay | |
1619 | the frame. It has to be, so the frame will have the right size. */ | |
1620 | FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f)); | |
1621 | set_frame_menubar (f, 1, 1); | |
ee78dc32 GV |
1622 | } |
1623 | ||
485015ca GV |
1624 | /* Get rid of the menu bar of frame F, and free its storage. |
1625 | This is used when deleting a frame, and when turning off the menu bar. */ | |
1626 | ||
1627 | void | |
1628 | free_frame_menubar (f) | |
1629 | FRAME_PTR f; | |
1630 | { | |
1631 | BLOCK_INPUT; | |
1632 | ||
1633 | { | |
1634 | HMENU old = GetMenu (FRAME_W32_WINDOW (f)); | |
1635 | SetMenu (FRAME_W32_WINDOW (f), NULL); | |
1636 | f->output_data.w32->menubar_widget = NULL; | |
1637 | DestroyMenu (old); | |
1638 | } | |
29250851 | 1639 | |
485015ca GV |
1640 | UNBLOCK_INPUT; |
1641 | } | |
ee78dc32 | 1642 | |
485015ca GV |
1643 | \f |
1644 | /* w32_menu_show actually displays a menu using the panes and items in | |
1645 | menu_items and returns the value selected from it; we assume input | |
1646 | is blocked by the caller. */ | |
ee78dc32 GV |
1647 | |
1648 | /* F is the frame the menu is for. | |
1649 | X and Y are the frame-relative specified position, | |
1650 | relative to the inside upper left corner of the frame F. | |
485015ca | 1651 | FOR_CLICK is nonzero if this menu was invoked for a mouse click. |
ee78dc32 | 1652 | KEYMAPS is 1 if this menu was specified with keymaps; |
485015ca GV |
1653 | in that case, we return a list containing the chosen item's value |
1654 | and perhaps also the pane's prefix. | |
ee78dc32 GV |
1655 | TITLE is the specified menu title. |
1656 | ERROR is a place to store an error message string in case of failure. | |
1657 | (We return nil on failure, but the value doesn't actually matter.) */ | |
1658 | ||
485015ca GV |
1659 | static Lisp_Object |
1660 | w32_menu_show (f, x, y, for_click, keymaps, title, error) | |
ee78dc32 GV |
1661 | FRAME_PTR f; |
1662 | int x; | |
1663 | int y; | |
485015ca GV |
1664 | int for_click; |
1665 | int keymaps; | |
1666 | Lisp_Object title; | |
ee78dc32 GV |
1667 | char **error; |
1668 | { | |
485015ca GV |
1669 | int i; |
1670 | int menu_item_selection; | |
1671 | HMENU menu; | |
ee78dc32 | 1672 | POINT pos; |
485015ca GV |
1673 | widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; |
1674 | widget_value **submenu_stack | |
1675 | = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); | |
1676 | Lisp_Object *subprefix_stack | |
1677 | = (Lisp_Object *) alloca (menu_items_used * sizeof (Lisp_Object)); | |
1678 | int submenu_depth = 0; | |
485015ca | 1679 | int first_pane; |
485015ca | 1680 | |
ee78dc32 | 1681 | *error = NULL; |
485015ca GV |
1682 | |
1683 | if (menu_items_used <= MENU_ITEMS_PANE_LENGTH) | |
ee78dc32 GV |
1684 | { |
1685 | *error = "Empty menu"; | |
1686 | return Qnil; | |
1687 | } | |
485015ca GV |
1688 | |
1689 | /* Create a tree of widget_value objects | |
1690 | representing the panes and their items. */ | |
1691 | wv = xmalloc_widget_value (); | |
1692 | wv->name = "menu"; | |
1693 | wv->value = 0; | |
1694 | wv->enabled = 1; | |
162d2499 | 1695 | wv->button_type = BUTTON_TYPE_NONE; |
e6bb685b | 1696 | wv->help = Qnil; |
485015ca GV |
1697 | first_wv = wv; |
1698 | first_pane = 1; | |
177c0ea7 | 1699 | |
485015ca GV |
1700 | /* Loop over all panes and items, filling in the tree. */ |
1701 | i = 0; | |
1702 | while (i < menu_items_used) | |
1703 | { | |
6ed09cf0 | 1704 | if (EQ (AREF (menu_items, i), Qnil)) |
485015ca GV |
1705 | { |
1706 | submenu_stack[submenu_depth++] = save_wv; | |
1707 | save_wv = prev_wv; | |
1708 | prev_wv = 0; | |
1709 | first_pane = 1; | |
1710 | i++; | |
1711 | } | |
6ed09cf0 | 1712 | else if (EQ (AREF (menu_items, i), Qlambda)) |
485015ca GV |
1713 | { |
1714 | prev_wv = save_wv; | |
1715 | save_wv = submenu_stack[--submenu_depth]; | |
1716 | first_pane = 0; | |
1717 | i++; | |
1718 | } | |
6ed09cf0 | 1719 | else if (EQ (AREF (menu_items, i), Qt) |
485015ca GV |
1720 | && submenu_depth != 0) |
1721 | i += MENU_ITEMS_PANE_LENGTH; | |
1722 | /* Ignore a nil in the item list. | |
1723 | It's meaningful only for dialog boxes. */ | |
6ed09cf0 | 1724 | else if (EQ (AREF (menu_items, i), Qquote)) |
485015ca | 1725 | i += 1; |
6ed09cf0 | 1726 | else if (EQ (AREF (menu_items, i), Qt)) |
485015ca GV |
1727 | { |
1728 | /* Create a new pane. */ | |
1729 | Lisp_Object pane_name, prefix; | |
1730 | char *pane_string; | |
29250851 JR |
1731 | pane_name = AREF (menu_items, i + MENU_ITEMS_PANE_NAME); |
1732 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); | |
162d2499 | 1733 | #ifndef HAVE_MULTILINGUAL_MENU |
29250851 JR |
1734 | if (STRINGP (pane_name) && STRING_MULTIBYTE (pane_name)) |
1735 | { | |
1736 | pane_name = ENCODE_SYSTEM (pane_name); | |
6ed09cf0 | 1737 | ASET (menu_items, i + MENU_ITEMS_PANE_NAME, pane_name); |
29250851 | 1738 | } |
162d2499 | 1739 | #endif |
485015ca | 1740 | pane_string = (NILP (pane_name) |
d5db4077 | 1741 | ? "" : (char *) SDATA (pane_name)); |
485015ca GV |
1742 | /* If there is just one top-level pane, put all its items directly |
1743 | under the top-level menu. */ | |
1744 | if (menu_items_n_panes == 1) | |
1745 | pane_string = ""; | |
1746 | ||
1747 | /* If the pane has a meaningful name, | |
1748 | make the pane a top-level menu item | |
1749 | with its items as a submenu beneath it. */ | |
1750 | if (!keymaps && strcmp (pane_string, "")) | |
1751 | { | |
1752 | wv = xmalloc_widget_value (); | |
1753 | if (save_wv) | |
1754 | save_wv->next = wv; | |
1755 | else | |
1756 | first_wv->contents = wv; | |
1757 | wv->name = pane_string; | |
1758 | if (keymaps && !NILP (prefix)) | |
1759 | wv->name++; | |
1760 | wv->value = 0; | |
1761 | wv->enabled = 1; | |
162d2499 | 1762 | wv->button_type = BUTTON_TYPE_NONE; |
2d10309f | 1763 | wv->help = Qnil; |
485015ca GV |
1764 | save_wv = wv; |
1765 | prev_wv = 0; | |
1766 | } | |
1767 | else if (first_pane) | |
1768 | { | |
1769 | save_wv = wv; | |
1770 | prev_wv = 0; | |
1771 | } | |
1772 | first_pane = 0; | |
1773 | i += MENU_ITEMS_PANE_LENGTH; | |
1774 | } | |
1775 | else | |
1776 | { | |
1777 | /* Create a new item within current pane. */ | |
b65a2f83 | 1778 | Lisp_Object item_name, enable, descrip, def, type, selected, help; |
b65a2f83 | 1779 | |
29250851 JR |
1780 | item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); |
1781 | enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); | |
1782 | descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); | |
1783 | def = AREF (menu_items, i + MENU_ITEMS_ITEM_DEFINITION); | |
1784 | type = AREF (menu_items, i + MENU_ITEMS_ITEM_TYPE); | |
1785 | selected = AREF (menu_items, i + MENU_ITEMS_ITEM_SELECTED); | |
1786 | help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP); | |
162d2499 JR |
1787 | |
1788 | #ifndef HAVE_MULTILINGUAL_MENU | |
1789 | if (STRINGP (item_name) && STRING_MULTIBYTE (item_name)) | |
29250851 JR |
1790 | { |
1791 | item_name = ENCODE_SYSTEM (item_name); | |
6ed09cf0 | 1792 | ASET (menu_items, i + MENU_ITEMS_ITEM_NAME, item_name); |
29250851 | 1793 | } |
162d2499 | 1794 | if (STRINGP (descrip) && STRING_MULTIBYTE (descrip)) |
29250851 JR |
1795 | { |
1796 | descrip = ENCODE_SYSTEM (descrip); | |
6ed09cf0 | 1797 | ASET (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY, descrip); |
29250851 JR |
1798 | } |
1799 | #endif /* not HAVE_MULTILINGUAL_MENU */ | |
485015ca GV |
1800 | |
1801 | wv = xmalloc_widget_value (); | |
177c0ea7 | 1802 | if (prev_wv) |
485015ca | 1803 | prev_wv->next = wv; |
177c0ea7 | 1804 | else |
485015ca | 1805 | save_wv->contents = wv; |
d5db4077 | 1806 | wv->name = (char *) SDATA (item_name); |
485015ca | 1807 | if (!NILP (descrip)) |
d5db4077 | 1808 | wv->key = (char *) SDATA (descrip); |
485015ca GV |
1809 | wv->value = 0; |
1810 | /* Use the contents index as call_data, since we are | |
2cd23960 | 1811 | restricted to 16-bits. */ |
e59fe83b | 1812 | wv->call_data = !NILP (def) ? (void *) (EMACS_INT) i : 0; |
485015ca | 1813 | wv->enabled = !NILP (enable); |
162d2499 JR |
1814 | |
1815 | if (NILP (type)) | |
1816 | wv->button_type = BUTTON_TYPE_NONE; | |
1817 | else if (EQ (type, QCtoggle)) | |
1818 | wv->button_type = BUTTON_TYPE_TOGGLE; | |
1819 | else if (EQ (type, QCradio)) | |
1820 | wv->button_type = BUTTON_TYPE_RADIO; | |
1821 | else | |
1822 | abort (); | |
1823 | ||
1824 | wv->selected = !NILP (selected); | |
d1fffd1d JR |
1825 | if (!STRINGP (help)) |
1826 | help = Qnil; | |
1827 | ||
1828 | wv->help = help; | |
7d8e57da | 1829 | |
485015ca GV |
1830 | prev_wv = wv; |
1831 | ||
1832 | i += MENU_ITEMS_ITEM_LENGTH; | |
1833 | } | |
1834 | } | |
1835 | ||
1836 | /* Deal with the title, if it is non-nil. */ | |
1837 | if (!NILP (title)) | |
1838 | { | |
1839 | widget_value *wv_title = xmalloc_widget_value (); | |
1840 | widget_value *wv_sep = xmalloc_widget_value (); | |
1841 | ||
1842 | /* Maybe replace this separator with a bitmap or owner-draw item | |
1843 | so that it looks better. Having two separators looks odd. */ | |
1844 | wv_sep->name = "--"; | |
1845 | wv_sep->next = first_wv->contents; | |
2d10309f | 1846 | wv_sep->help = Qnil; |
485015ca | 1847 | |
162d2499 JR |
1848 | #ifndef HAVE_MULTILINGUAL_MENU |
1849 | if (STRING_MULTIBYTE (title)) | |
6915ded0 | 1850 | title = ENCODE_SYSTEM (title); |
162d2499 | 1851 | #endif |
d5db4077 | 1852 | wv_title->name = (char *) SDATA (title); |
02067692 SM |
1853 | wv_title->enabled = TRUE; |
1854 | wv_title->title = TRUE; | |
162d2499 | 1855 | wv_title->button_type = BUTTON_TYPE_NONE; |
2d10309f | 1856 | wv_title->help = Qnil; |
485015ca GV |
1857 | wv_title->next = wv_sep; |
1858 | first_wv->contents = wv_title; | |
1859 | } | |
1860 | ||
1861 | /* Actually create the menu. */ | |
ace9b298 | 1862 | current_popup_menu = menu = CreatePopupMenu (); |
485015ca | 1863 | fill_in_menu (menu, first_wv->contents); |
162d2499 | 1864 | |
485015ca | 1865 | /* Adjust coordinates to be root-window-relative. */ |
ee78dc32 GV |
1866 | pos.x = x; |
1867 | pos.y = y; | |
fbd6baed | 1868 | ClientToScreen (FRAME_W32_WINDOW (f), &pos); |
485015ca | 1869 | |
485015ca GV |
1870 | /* No selection has been chosen yet. */ |
1871 | menu_item_selection = 0; | |
014510b0 | 1872 | |
ee78dc32 | 1873 | /* Display the menu. */ |
177c0ea7 | 1874 | menu_item_selection = SendMessage (FRAME_W32_WINDOW (f), |
485015ca GV |
1875 | WM_EMACS_TRACKPOPUPMENU, |
1876 | (WPARAM)menu, (LPARAM)&pos); | |
014510b0 GV |
1877 | |
1878 | /* Clean up extraneous mouse events which might have been generated | |
1879 | during the call. */ | |
1880 | discard_mouse_events (); | |
1881 | ||
86fb1a79 JR |
1882 | /* Free the widget_value objects we used to specify the contents. */ |
1883 | free_menubar_widget_value_tree (first_wv); | |
1884 | ||
485015ca GV |
1885 | DestroyMenu (menu); |
1886 | ||
ee78dc32 GV |
1887 | /* Find the selected item, and its pane, to return |
1888 | the proper value. */ | |
485015ca | 1889 | if (menu_item_selection != 0) |
ee78dc32 | 1890 | { |
485015ca GV |
1891 | Lisp_Object prefix, entry; |
1892 | ||
fe5a0162 | 1893 | prefix = entry = Qnil; |
485015ca GV |
1894 | i = 0; |
1895 | while (i < menu_items_used) | |
1896 | { | |
6ed09cf0 | 1897 | if (EQ (AREF (menu_items, i), Qnil)) |
485015ca GV |
1898 | { |
1899 | subprefix_stack[submenu_depth++] = prefix; | |
1900 | prefix = entry; | |
1901 | i++; | |
1902 | } | |
6ed09cf0 | 1903 | else if (EQ (AREF (menu_items, i), Qlambda)) |
485015ca GV |
1904 | { |
1905 | prefix = subprefix_stack[--submenu_depth]; | |
1906 | i++; | |
1907 | } | |
6ed09cf0 | 1908 | else if (EQ (AREF (menu_items, i), Qt)) |
485015ca | 1909 | { |
6ed09cf0 | 1910 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); |
485015ca GV |
1911 | i += MENU_ITEMS_PANE_LENGTH; |
1912 | } | |
1913 | /* Ignore a nil in the item list. | |
1914 | It's meaningful only for dialog boxes. */ | |
6ed09cf0 | 1915 | else if (EQ (AREF (menu_items, i), Qquote)) |
485015ca GV |
1916 | i += 1; |
1917 | else | |
1918 | { | |
6ed09cf0 | 1919 | entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE); |
485015ca GV |
1920 | if (menu_item_selection == i) |
1921 | { | |
1922 | if (keymaps != 0) | |
1923 | { | |
1924 | int j; | |
1925 | ||
1926 | entry = Fcons (entry, Qnil); | |
1927 | if (!NILP (prefix)) | |
1928 | entry = Fcons (prefix, entry); | |
1929 | for (j = submenu_depth - 1; j >= 0; j--) | |
1930 | if (!NILP (subprefix_stack[j])) | |
1931 | entry = Fcons (subprefix_stack[j], entry); | |
1932 | } | |
1933 | return entry; | |
1934 | } | |
1935 | i += MENU_ITEMS_ITEM_LENGTH; | |
1936 | } | |
1937 | } | |
ee78dc32 | 1938 | } |
014510b0 | 1939 | |
ee78dc32 GV |
1940 | return Qnil; |
1941 | } | |
485015ca | 1942 | \f |
ee78dc32 | 1943 | |
ace9b298 | 1944 | #ifdef HAVE_DIALOGS |
485015ca | 1945 | static char * button_names [] = { |
ee78dc32 | 1946 | "button1", "button2", "button3", "button4", "button5", |
485015ca | 1947 | "button6", "button7", "button8", "button9", "button10" }; |
ee78dc32 GV |
1948 | |
1949 | static Lisp_Object | |
485015ca | 1950 | w32_dialog_show (f, keymaps, title, error) |
ee78dc32 | 1951 | FRAME_PTR f; |
ee78dc32 GV |
1952 | int keymaps; |
1953 | Lisp_Object title; | |
1954 | char **error; | |
1955 | { | |
1956 | int i, nb_buttons=0; | |
ee78dc32 | 1957 | char dialog_name[6]; |
485015ca GV |
1958 | int menu_item_selection; |
1959 | ||
fe5a0162 | 1960 | widget_value *wv, *first_wv = 0, *prev_wv = 0; |
485015ca | 1961 | |
ee78dc32 GV |
1962 | /* Number of elements seen so far, before boundary. */ |
1963 | int left_count = 0; | |
1964 | /* 1 means we've seen the boundary between left-hand elts and right-hand. */ | |
1965 | int boundary_seen = 0; | |
485015ca | 1966 | |
ee78dc32 | 1967 | *error = NULL; |
485015ca | 1968 | |
ee78dc32 GV |
1969 | if (menu_items_n_panes > 1) |
1970 | { | |
1971 | *error = "Multiple panes in dialog box"; | |
1972 | return Qnil; | |
1973 | } | |
485015ca | 1974 | |
ee78dc32 GV |
1975 | /* Create a tree of widget_value objects |
1976 | representing the text label and buttons. */ | |
1977 | { | |
1978 | Lisp_Object pane_name, prefix; | |
1979 | char *pane_string; | |
6ed09cf0 RS |
1980 | pane_name = AREF (menu_items, MENU_ITEMS_PANE_NAME); |
1981 | prefix = AREF (menu_items, MENU_ITEMS_PANE_PREFIX); | |
ee78dc32 | 1982 | pane_string = (NILP (pane_name) |
177c0ea7 | 1983 | ? "" : (char *) SDATA (pane_name)); |
485015ca | 1984 | prev_wv = xmalloc_widget_value (); |
ee78dc32 GV |
1985 | prev_wv->value = pane_string; |
1986 | if (keymaps && !NILP (prefix)) | |
1987 | prev_wv->name++; | |
1988 | prev_wv->enabled = 1; | |
1989 | prev_wv->name = "message"; | |
2d10309f | 1990 | prev_wv->help = Qnil; |
ee78dc32 | 1991 | first_wv = prev_wv; |
177c0ea7 | 1992 | |
ee78dc32 GV |
1993 | /* Loop over all panes and items, filling in the tree. */ |
1994 | i = MENU_ITEMS_PANE_LENGTH; | |
1995 | while (i < menu_items_used) | |
1996 | { | |
177c0ea7 | 1997 | |
ee78dc32 | 1998 | /* Create a new item within current pane. */ |
b65a2f83 | 1999 | Lisp_Object item_name, enable, descrip, help; |
b65a2f83 | 2000 | |
6ed09cf0 RS |
2001 | item_name = AREF (menu_items, i + MENU_ITEMS_ITEM_NAME); |
2002 | enable = AREF (menu_items, i + MENU_ITEMS_ITEM_ENABLE); | |
2003 | descrip = AREF (menu_items, i + MENU_ITEMS_ITEM_EQUIV_KEY); | |
2004 | help = AREF (menu_items, i + MENU_ITEMS_ITEM_HELP); | |
177c0ea7 | 2005 | |
ee78dc32 GV |
2006 | if (NILP (item_name)) |
2007 | { | |
2008 | free_menubar_widget_value_tree (first_wv); | |
2009 | *error = "Submenu in dialog items"; | |
2010 | return Qnil; | |
2011 | } | |
2012 | if (EQ (item_name, Qquote)) | |
2013 | { | |
2014 | /* This is the boundary between left-side elts | |
2015 | and right-side elts. Stop incrementing right_count. */ | |
2016 | boundary_seen = 1; | |
2017 | i++; | |
2018 | continue; | |
2019 | } | |
485015ca | 2020 | if (nb_buttons >= 9) |
ee78dc32 GV |
2021 | { |
2022 | free_menubar_widget_value_tree (first_wv); | |
2023 | *error = "Too many dialog items"; | |
2024 | return Qnil; | |
2025 | } | |
485015ca GV |
2026 | |
2027 | wv = xmalloc_widget_value (); | |
ee78dc32 GV |
2028 | prev_wv->next = wv; |
2029 | wv->name = (char *) button_names[nb_buttons]; | |
2030 | if (!NILP (descrip)) | |
d5db4077 KR |
2031 | wv->key = (char *) SDATA (descrip); |
2032 | wv->value = (char *) SDATA (item_name); | |
6ed09cf0 | 2033 | wv->call_data = (void *) &AREF (menu_items, i); |
ee78dc32 | 2034 | wv->enabled = !NILP (enable); |
2d10309f | 2035 | wv->help = Qnil; |
ee78dc32 | 2036 | prev_wv = wv; |
485015ca | 2037 | |
ee78dc32 GV |
2038 | if (! boundary_seen) |
2039 | left_count++; | |
485015ca | 2040 | |
ee78dc32 GV |
2041 | nb_buttons++; |
2042 | i += MENU_ITEMS_ITEM_LENGTH; | |
2043 | } | |
485015ca | 2044 | |
ee78dc32 GV |
2045 | /* If the boundary was not specified, |
2046 | by default put half on the left and half on the right. */ | |
2047 | if (! boundary_seen) | |
2048 | left_count = nb_buttons - nb_buttons / 2; | |
485015ca GV |
2049 | |
2050 | wv = xmalloc_widget_value (); | |
ee78dc32 | 2051 | wv->name = dialog_name; |
2d10309f | 2052 | wv->help = Qnil; |
485015ca | 2053 | |
ee78dc32 GV |
2054 | /* Dialog boxes use a really stupid name encoding |
2055 | which specifies how many buttons to use | |
2056 | and how many buttons are on the right. | |
2057 | The Q means something also. */ | |
2058 | dialog_name[0] = 'Q'; | |
2059 | dialog_name[1] = '0' + nb_buttons; | |
2060 | dialog_name[2] = 'B'; | |
2061 | dialog_name[3] = 'R'; | |
2062 | /* Number of buttons to put on the right. */ | |
2063 | dialog_name[4] = '0' + nb_buttons - left_count; | |
2064 | dialog_name[5] = 0; | |
2065 | wv->contents = first_wv; | |
2066 | first_wv = wv; | |
2067 | } | |
485015ca | 2068 | |
ee78dc32 | 2069 | /* Actually create the dialog. */ |
485015ca | 2070 | dialog_id = widget_id_tick++; |
ee78dc32 | 2071 | menu = lw_create_widget (first_wv->name, "dialog", dialog_id, first_wv, |
fbd6baed | 2072 | f->output_data.w32->widget, 1, 0, |
ee78dc32 | 2073 | dialog_selection_callback, 0); |
02067692 | 2074 | lw_modify_all_widgets (dialog_id, first_wv->contents, TRUE); |
485015ca | 2075 | |
ee78dc32 GV |
2076 | /* Free the widget_value objects we used to specify the contents. */ |
2077 | free_menubar_widget_value_tree (first_wv); | |
485015ca | 2078 | |
ee78dc32 GV |
2079 | /* No selection has been chosen yet. */ |
2080 | menu_item_selection = 0; | |
485015ca | 2081 | |
ee78dc32 GV |
2082 | /* Display the menu. */ |
2083 | lw_pop_up_all_widgets (dialog_id); | |
485015ca GV |
2084 | popup_activated_flag = 1; |
2085 | ||
ee78dc32 | 2086 | /* Process events that apply to the menu. */ |
485015ca | 2087 | popup_get_selection ((XEvent *) 0, FRAME_X_DISPLAY_INFO (f), dialog_id); |
ee78dc32 | 2088 | |
177c0ea7 | 2089 | lw_destroy_all_widgets (dialog_id); |
ee78dc32 | 2090 | |
ee78dc32 GV |
2091 | /* Find the selected item, and its pane, to return |
2092 | the proper value. */ | |
2093 | if (menu_item_selection != 0) | |
2094 | { | |
2095 | Lisp_Object prefix; | |
485015ca | 2096 | |
ee78dc32 GV |
2097 | prefix = Qnil; |
2098 | i = 0; | |
2099 | while (i < menu_items_used) | |
2100 | { | |
2101 | Lisp_Object entry; | |
2102 | ||
6ed09cf0 | 2103 | if (EQ (AREF (menu_items, i), Qt)) |
ee78dc32 | 2104 | { |
6ed09cf0 | 2105 | prefix = AREF (menu_items, i + MENU_ITEMS_PANE_PREFIX); |
ee78dc32 GV |
2106 | i += MENU_ITEMS_PANE_LENGTH; |
2107 | } | |
2108 | else | |
2109 | { | |
6ed09cf0 | 2110 | entry = AREF (menu_items, i + MENU_ITEMS_ITEM_VALUE); |
485015ca | 2111 | if (menu_item_selection == i) |
ee78dc32 GV |
2112 | { |
2113 | if (keymaps != 0) | |
2114 | { | |
2115 | entry = Fcons (entry, Qnil); | |
2116 | if (!NILP (prefix)) | |
2117 | entry = Fcons (prefix, entry); | |
2118 | } | |
2119 | return entry; | |
2120 | } | |
2121 | i += MENU_ITEMS_ITEM_LENGTH; | |
2122 | } | |
2123 | } | |
2124 | } | |
485015ca | 2125 | |
ee78dc32 GV |
2126 | return Qnil; |
2127 | } | |
ace9b298 | 2128 | #endif /* HAVE_DIALOGS */ |
485015ca GV |
2129 | \f |
2130 | ||
2131 | /* Is this item a separator? */ | |
2132 | static int | |
2133 | name_is_separator (name) | |
2134 | char *name; | |
2135 | { | |
fe5a0162 SM |
2136 | char *start = name; |
2137 | ||
2138 | /* Check if name string consists of only dashes ('-'). */ | |
485015ca | 2139 | while (*name == '-') name++; |
fe5a0162 SM |
2140 | /* Separators can also be of the form "--:TripleSuperMegaEtched" |
2141 | or "--deep-shadow". We don't implement them yet, se we just treat | |
2142 | them like normal separators. */ | |
2143 | return (*name == '\0' || start + 2 == name); | |
485015ca GV |
2144 | } |
2145 | ||
2146 | ||
2147 | /* Indicate boundary between left and right. */ | |
2148 | static int | |
2149 | add_left_right_boundary (HMENU menu) | |
2150 | { | |
2151 | return AppendMenu (menu, MF_MENUBARBREAK, 0, NULL); | |
2152 | } | |
2153 | ||
2154 | static int | |
2155 | add_menu_item (HMENU menu, widget_value *wv, HMENU item) | |
2156 | { | |
2157 | UINT fuFlags; | |
2158 | char *out_string; | |
37ad8b63 | 2159 | int return_value; |
485015ca GV |
2160 | |
2161 | if (name_is_separator (wv->name)) | |
222456a1 JR |
2162 | { |
2163 | fuFlags = MF_SEPARATOR; | |
2164 | out_string = NULL; | |
2165 | } | |
177c0ea7 | 2166 | else |
485015ca | 2167 | { |
070d1949 | 2168 | if (wv->enabled) |
485015ca GV |
2169 | fuFlags = MF_STRING; |
2170 | else | |
2171 | fuFlags = MF_STRING | MF_GRAYED; | |
2172 | ||
2173 | if (wv->key != NULL) | |
2174 | { | |
2175 | out_string = alloca (strlen (wv->name) + strlen (wv->key) + 2); | |
2176 | strcpy (out_string, wv->name); | |
2177 | strcat (out_string, "\t"); | |
2178 | strcat (out_string, wv->key); | |
2179 | } | |
2180 | else | |
2181 | out_string = wv->name; | |
2182 | ||
d1fffd1d JR |
2183 | if (item != NULL) |
2184 | fuFlags = MF_POPUP; | |
2185 | else if (wv->title || wv->call_data == 0) | |
485015ca | 2186 | { |
ace9b298 JR |
2187 | /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since |
2188 | we can't deallocate the memory otherwise. */ | |
2189 | if (get_menu_item_info) | |
2190 | { | |
6ed09cf0 RS |
2191 | out_string = (char *) local_alloc (strlen (wv->name) + 1); |
2192 | strcpy (out_string, wv->name); | |
d1fffd1d | 2193 | #ifdef MENU_DEBUG |
6ed09cf0 | 2194 | DebPrint ("Menu: allocing %ld for owner-draw", out_string); |
d1fffd1d | 2195 | #endif |
ace9b298 JR |
2196 | fuFlags = MF_OWNERDRAW | MF_DISABLED; |
2197 | } | |
2198 | else | |
2199 | fuFlags = MF_DISABLED; | |
485015ca | 2200 | } |
1c5acb8c | 2201 | |
162d2499 | 2202 | /* Draw radio buttons and tickboxes. */ |
4cdf6a6c | 2203 | else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE || |
6f665da9 | 2204 | wv->button_type == BUTTON_TYPE_RADIO)) |
4cdf6a6c | 2205 | fuFlags |= MF_CHECKED; |
37ad8b63 | 2206 | else |
4cdf6a6c | 2207 | fuFlags |= MF_UNCHECKED; |
485015ca | 2208 | } |
4cdf6a6c | 2209 | |
37ad8b63 JR |
2210 | return_value = |
2211 | AppendMenu (menu, | |
2212 | fuFlags, | |
2213 | item != NULL ? (UINT) item : (UINT) wv->call_data, | |
222456a1 | 2214 | out_string ); |
37ad8b63 JR |
2215 | |
2216 | /* This must be done after the menu item is created. */ | |
6f665da9 | 2217 | if (!wv->title && wv->call_data != 0) |
4cdf6a6c | 2218 | { |
4cdf6a6c AI |
2219 | if (set_menu_item_info) |
2220 | { | |
2221 | MENUITEMINFO info; | |
2222 | bzero (&info, sizeof (info)); | |
2223 | info.cbSize = sizeof (info); | |
2224 | info.fMask = MIIM_DATA; | |
2225 | ||
d1fffd1d JR |
2226 | /* Set help string for menu item. Leave it as a Lisp_Object |
2227 | until it is ready to be displayed, since GC can happen while | |
2228 | menus are active. */ | |
ace9b298 | 2229 | if (wv->help) |
d1fffd1d | 2230 | info.dwItemData = (DWORD) wv->help; |
4cdf6a6c AI |
2231 | |
2232 | if (wv->button_type == BUTTON_TYPE_RADIO) | |
2233 | { | |
2234 | /* CheckMenuRadioItem allows us to differentiate TOGGLE and | |
2235 | RADIO items, but is not available on NT 3.51 and earlier. */ | |
2236 | info.fMask |= MIIM_TYPE | MIIM_STATE; | |
2237 | info.fType = MFT_RADIOCHECK | MFT_STRING; | |
2238 | info.dwTypeData = out_string; | |
2239 | info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED; | |
2240 | } | |
2241 | ||
2242 | set_menu_item_info (menu, | |
2243 | item != NULL ? (UINT) item : (UINT) wv->call_data, | |
2244 | FALSE, &info); | |
2245 | } | |
2246 | } | |
37ad8b63 | 2247 | return return_value; |
485015ca GV |
2248 | } |
2249 | ||
2250 | /* Construct native Windows menu(bar) based on widget_value tree. */ | |
15d36dee | 2251 | int |
485015ca GV |
2252 | fill_in_menu (HMENU menu, widget_value *wv) |
2253 | { | |
2254 | int items_added = 0; | |
ee78dc32 | 2255 | |
485015ca GV |
2256 | for ( ; wv != NULL; wv = wv->next) |
2257 | { | |
2258 | if (wv->contents) | |
2259 | { | |
2260 | HMENU sub_menu = CreatePopupMenu (); | |
2261 | ||
2262 | if (sub_menu == NULL) | |
2263 | return 0; | |
2264 | ||
2265 | if (!fill_in_menu (sub_menu, wv->contents) || | |
2266 | !add_menu_item (menu, wv, sub_menu)) | |
2267 | { | |
2268 | DestroyMenu (sub_menu); | |
2269 | return 0; | |
2270 | } | |
2271 | } | |
2272 | else | |
2273 | { | |
2274 | if (!add_menu_item (menu, wv, NULL)) | |
2275 | return 0; | |
2276 | } | |
2277 | } | |
2278 | return 1; | |
2279 | } | |
2280 | ||
162d2499 JR |
2281 | int |
2282 | popup_activated () | |
2283 | { | |
2284 | /* popup_activated_flag not actually used on W32 */ | |
2285 | return 0; | |
2286 | } | |
2287 | ||
7d8e57da JR |
2288 | /* Display help string for currently pointed to menu item. Not |
2289 | supported on NT 3.51 and earlier, as GetMenuItemInfo is not | |
2290 | available. */ | |
2291 | void | |
59a86c99 | 2292 | w32_menu_display_help (HWND owner, HMENU menu, UINT item, UINT flags) |
7d8e57da | 2293 | { |
7d8e57da JR |
2294 | if (get_menu_item_info) |
2295 | { | |
1c5acb8c JR |
2296 | struct frame *f = x_window_to_frame (&one_w32_display_info, owner); |
2297 | Lisp_Object frame, help; | |
7d8e57da | 2298 | |
ace9b298 JR |
2299 | // No help echo on owner-draw menu items. |
2300 | if (flags & MF_OWNERDRAW || flags & MF_POPUP) | |
2301 | help = Qnil; | |
2302 | else | |
2303 | { | |
2304 | MENUITEMINFO info; | |
7d8e57da | 2305 | |
ace9b298 JR |
2306 | bzero (&info, sizeof (info)); |
2307 | info.cbSize = sizeof (info); | |
2308 | info.fMask = MIIM_DATA; | |
2309 | get_menu_item_info (menu, item, FALSE, &info); | |
2310 | ||
d1fffd1d | 2311 | help = info.dwItemData ? (Lisp_Object) info.dwItemData : Qnil; |
ace9b298 | 2312 | } |
e5cd3d11 | 2313 | |
1c5acb8c JR |
2314 | /* Store the help echo in the keyboard buffer as the X toolkit |
2315 | version does, rather than directly showing it. This seems to | |
2316 | solve the GC problems that were present when we based the | |
2317 | Windows code on the non-toolkit version. */ | |
2318 | if (f) | |
2319 | { | |
2320 | XSETFRAME (frame, f); | |
2321 | kbd_buffer_store_help_event (frame, help); | |
2322 | } | |
2323 | else | |
2324 | /* X version has a loop through frames here, which doesn't | |
2325 | appear to do anything, unless it has some side effect. */ | |
2326 | show_help_echo (help, Qnil, Qnil, Qnil, 1); | |
7d8e57da JR |
2327 | } |
2328 | } | |
2329 | ||
d1fffd1d | 2330 | /* Free memory used by owner-drawn strings. */ |
ace9b298 JR |
2331 | static void |
2332 | w32_free_submenu_strings (menu) | |
2333 | HMENU menu; | |
2334 | { | |
2335 | int i, num = GetMenuItemCount (menu); | |
2336 | for (i = 0; i < num; i++) | |
2337 | { | |
2338 | MENUITEMINFO info; | |
37647b2d | 2339 | bzero (&info, sizeof (info)); |
ace9b298 | 2340 | info.cbSize = sizeof (info); |
d1fffd1d | 2341 | info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU; |
7d8e57da | 2342 | |
ace9b298 JR |
2343 | get_menu_item_info (menu, i, TRUE, &info); |
2344 | ||
d1fffd1d JR |
2345 | /* Owner-drawn names are held in dwItemData. */ |
2346 | if ((info.fType & MF_OWNERDRAW) && info.dwItemData) | |
2347 | { | |
2348 | #ifdef MENU_DEBUG | |
2349 | DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData); | |
2350 | #endif | |
6ed09cf0 | 2351 | local_free (info.dwItemData); |
d1fffd1d | 2352 | } |
ace9b298 JR |
2353 | |
2354 | /* Recurse down submenus. */ | |
2355 | if (info.hSubMenu) | |
2356 | w32_free_submenu_strings (info.hSubMenu); | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | void | |
2361 | w32_free_menu_strings (hwnd) | |
2362 | HWND hwnd; | |
2363 | { | |
2364 | HMENU menu = current_popup_menu; | |
2365 | ||
2366 | if (get_menu_item_info) | |
2367 | { | |
2368 | /* If there is no popup menu active, free the strings from the frame's | |
2369 | menubar. */ | |
2370 | if (!menu) | |
2371 | menu = GetMenu (hwnd); | |
2372 | ||
2373 | if (menu) | |
2374 | w32_free_submenu_strings (menu); | |
2375 | } | |
2376 | ||
2377 | current_popup_menu = NULL; | |
2378 | } | |
7d8e57da | 2379 | |
485015ca | 2380 | #endif /* HAVE_MENUS */ |
ace9b298 | 2381 | |
f60ae425 | 2382 | void syms_of_w32menu () |
ee78dc32 | 2383 | { |
9785d95b | 2384 | globals_of_w32menu (); |
485015ca GV |
2385 | staticpro (&menu_items); |
2386 | menu_items = Qnil; | |
2387 | ||
ace9b298 JR |
2388 | current_popup_menu = NULL; |
2389 | ||
fdc12c4d RS |
2390 | Qdebug_on_next_call = intern ("debug-on-next-call"); |
2391 | staticpro (&Qdebug_on_next_call); | |
2392 | ||
c8869655 | 2393 | DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame, |
33f09670 JR |
2394 | doc: /* Frame for which we are updating a menu. |
2395 | The enable predicate for a menu command should check this variable. */); | |
c8869655 GV |
2396 | Vmenu_updating_frame = Qnil; |
2397 | ||
ee78dc32 | 2398 | defsubr (&Sx_popup_menu); |
485015ca | 2399 | #ifdef HAVE_MENUS |
ee78dc32 | 2400 | defsubr (&Sx_popup_dialog); |
485015ca | 2401 | #endif |
ee78dc32 | 2402 | } |
9785d95b BK |
2403 | |
2404 | /* | |
2405 | globals_of_w32menu is used to initialize those global variables that | |
2406 | must always be initialized on startup even when the global variable | |
2407 | initialized is non zero (see the function main in emacs.c). | |
2408 | globals_of_w32menu is called from syms_of_w32menu when the global | |
2409 | variable initialized is 0 and directly from main when initialized | |
2410 | is non zero. | |
2411 | */ | |
2412 | void globals_of_w32menu () | |
2413 | { | |
2414 | /* See if Get/SetMenuItemInfo functions are available. */ | |
2415 | HMODULE user32 = GetModuleHandle ("user32.dll"); | |
2416 | get_menu_item_info = (GetMenuItemInfoA_Proc) GetProcAddress (user32, "GetMenuItemInfoA"); | |
2417 | set_menu_item_info = (SetMenuItemInfoA_Proc) GetProcAddress (user32, "SetMenuItemInfoA"); | |
2418 | } |