aboutsummaryrefslogtreecommitdiffstats
path: root/src/macmenu.c
diff options
context:
space:
mode:
authorYAMAMOTO Mitsuharu2008-03-29 00:46:21 +0000
committerYAMAMOTO Mitsuharu2008-03-29 00:46:21 +0000
commitc98e277397009b29c62874aeadd153e480182a78 (patch)
tree2afece3030dc3a8375eab7f3f43c8cb426453306 /src/macmenu.c
parent693b73c4189a7c30f6dd62370b93d00d807d0f4c (diff)
downloademacs-c98e277397009b29c62874aeadd153e480182a78.tar.gz
emacs-c98e277397009b29c62874aeadd153e480182a78.zip
Replace MenuHandle and GetMenuHandle with MenuRef and
GetMenuRef, respectively. Replace WindowPtr with WindowRef. Replace ControlHandle with ControlRef. [!TARGET_API_MAC_CARBON]: Include Quickdraw.h instead of QuickDraw.h. (install_menu_quit_handler): Rename arg MENU_HANDLE to ROOT_MENU. [TARGET_API_MAC_CARBON] (menu_target_item_handler): Don't call next handler. Return immediately unless popup is activated. [TARGET_API_MAC_CARBON] (install_menu_target_item_handler): Remove argument. Install handler to application. (set_frame_menubar): Don't change deep_p. (mac_menu_show): Use FRAME_OUTER_TO_INNER_DIFF_X and FRAME_OUTER_TO_INNER_DIFF_Y. (DIALOG_BUTTON_COMMAND_ID_OFFSET, DIALOG_BUTTON_COMMAND_ID_P) (DIALOG_BUTTON_COMMAND_ID_VALUE, DIALOG_BUTTON_MAKE_COMMAND_ID) [HAVE_DIALOGS]: New macros. [HAVE_DIALOGS] (mac_handle_dialog_event, create_and_show_dialog): Use them. (fill_menu) [TARGET_API_MAC_CARBON]: Use SetMenuItemHierarchicalMenu. (fill_menubar) [TARGET_API_MAC_CARBON]: Use CFString. (mac_dialog_modal_filter, Fx_popup_dialog) [MAC_OSX]: Put special treatment for Fmessage_box, Fyes_or_no_p, and Fy_or_n_p in #if 0 as it is not compatible with y-or-n-p-with-timeout. (timer_check) [TARGET_API_MAC_CARBON]: Add extern. [TARGET_API_MAC_CARBON] (mac_handle_dialog_event): Use QuitEventLoop instead of QuitAppModalLoopForWindow. Consolidate QuitEventLoop calls. (pop_down_dialog) [TARGET_API_MAC_CARBON]: New function. [TARGET_API_MAC_CARBON] (create_and_show_dialog): Use it for unwind. Run timers during dialog popup. (Fmenu_or_popup_active_p) [TARGET_API_MAC_CARBON]: Use popup_activated. (quit_dialog_event_loop) [TARGET_API_MAC_CARBON]: New variable. [TARGET_API_MAC_CARBON] (mac_handle_dialog_event): Set it if dialog event loop should be quit. [TARGET_API_MAC_CARBON] (create_and_show_dialog): Quit dialog event loop if quit_dialog_event_loop is set.
Diffstat (limited to 'src/macmenu.c')
-rw-r--r--src/macmenu.c313
1 files changed, 214 insertions, 99 deletions
diff --git a/src/macmenu.c b/src/macmenu.c
index e65d3863207..591010a4d65 100644
--- a/src/macmenu.c
+++ b/src/macmenu.c
@@ -39,7 +39,7 @@ Boston, MA 02110-1301, USA. */
39#if !TARGET_API_MAC_CARBON 39#if !TARGET_API_MAC_CARBON
40#include <MacTypes.h> 40#include <MacTypes.h>
41#include <Menus.h> 41#include <Menus.h>
42#include <QuickDraw.h> 42#include <Quickdraw.h>
43#include <ToolUtils.h> 43#include <ToolUtils.h>
44#include <Fonts.h> 44#include <Fonts.h>
45#include <Controls.h> 45#include <Controls.h>
@@ -200,7 +200,7 @@ static void list_of_items P_ ((Lisp_Object));
200 200
201static void find_and_call_menu_selection P_ ((FRAME_PTR, int, Lisp_Object, 201static void find_and_call_menu_selection P_ ((FRAME_PTR, int, Lisp_Object,
202 void *)); 202 void *));
203static int fill_menu P_ ((MenuHandle, widget_value *, enum mac_menu_kind, int)); 203static int fill_menu P_ ((MenuRef, widget_value *, enum mac_menu_kind, int));
204static void fill_menubar P_ ((widget_value *, int)); 204static void fill_menubar P_ ((widget_value *, int));
205static void dispose_menus P_ ((enum mac_menu_kind, int)); 205static void dispose_menus P_ ((enum mac_menu_kind, int));
206 206
@@ -882,7 +882,7 @@ no quit occurs and `x-popup-menu' returns nil. */)
882 882
883/* Regard ESC and C-g as Cancel even without the Cancel button. */ 883/* Regard ESC and C-g as Cancel even without the Cancel button. */
884 884
885#ifdef MAC_OSX 885#if 0 /* defined (MAC_OSX) */
886static Boolean 886static Boolean
887mac_dialog_modal_filter (dialog, event, item_hit) 887mac_dialog_modal_filter (dialog, event, item_hit)
888 DialogRef dialog; 888 DialogRef dialog;
@@ -991,7 +991,7 @@ for instance using the window manager, then this produces a quit and
991 but I don't want to make one now. */ 991 but I don't want to make one now. */
992 CHECK_WINDOW (window); 992 CHECK_WINDOW (window);
993 993
994#ifdef MAC_OSX 994#if 0 /* defined (MAC_OSX) */
995 /* Special treatment for Fmessage_box, Fyes_or_no_p, and Fy_or_n_p. */ 995 /* Special treatment for Fmessage_box, Fyes_or_no_p, and Fy_or_n_p. */
996 if (EQ (position, Qt) 996 if (EQ (position, Qt)
997 && STRINGP (Fcar (contents)) 997 && STRINGP (Fcar (contents))
@@ -1162,7 +1162,7 @@ x_activate_menubar (f)
1162#endif 1162#endif
1163 if (menu_id) 1163 if (menu_id)
1164 { 1164 {
1165 MenuHandle menu = GetMenuHandle (menu_id); 1165 MenuRef menu = GetMenuRef (menu_id);
1166 1166
1167 if (menu) 1167 if (menu)
1168 { 1168 {
@@ -1595,14 +1595,16 @@ menu_target_item_handler (next_handler, event, data)
1595 EventRef event; 1595 EventRef event;
1596 void *data; 1596 void *data;
1597{ 1597{
1598 OSStatus err, result; 1598 OSStatus err;
1599 MenuRef menu; 1599 MenuRef menu;
1600 MenuItemIndex menu_item; 1600 MenuItemIndex menu_item;
1601 Lisp_Object help; 1601 Lisp_Object help;
1602 GrafPtr port; 1602 GrafPtr port;
1603 int specpdl_count = SPECPDL_INDEX (); 1603 int specpdl_count = SPECPDL_INDEX ();
1604 1604
1605 result = CallNextEventHandler (next_handler, event); 1605 /* Don't be bothered with the overflowed toolbar items menu. */
1606 if (!popup_activated ())
1607 return eventNotHandledErr;
1606 1608
1607 err = GetEventParameter (event, kEventParamDirectObject, typeMenuRef, 1609 err = GetEventParameter (event, kEventParamDirectObject, typeMenuRef,
1608 NULL, sizeof (MenuRef), NULL, &menu); 1610 NULL, sizeof (MenuRef), NULL, &menu);
@@ -1626,30 +1628,21 @@ menu_target_item_handler (next_handler, event, data)
1626 SetPort (port); 1628 SetPort (port);
1627 unbind_to (specpdl_count, Qnil); 1629 unbind_to (specpdl_count, Qnil);
1628 1630
1629 return err == noErr ? noErr : result; 1631 return err == noErr ? noErr : eventNotHandledErr;
1630} 1632}
1631#endif
1632 1633
1633OSStatus 1634OSStatus
1634install_menu_target_item_handler (window) 1635install_menu_target_item_handler ()
1635 WindowPtr window;
1636{ 1636{
1637 OSStatus err = noErr;
1638#if TARGET_API_MAC_CARBON
1639 static const EventTypeSpec specs[] = 1637 static const EventTypeSpec specs[] =
1640 {{kEventClassMenu, kEventMenuTargetItem}}; 1638 {{kEventClassMenu, kEventMenuTargetItem}};
1641 static EventHandlerUPP menu_target_item_handlerUPP = NULL;
1642
1643 if (menu_target_item_handlerUPP == NULL)
1644 menu_target_item_handlerUPP =
1645 NewEventHandlerUPP (menu_target_item_handler);
1646 1639
1647 err = InstallWindowEventHandler (window, menu_target_item_handlerUPP, 1640 return InstallApplicationEventHandler (NewEventHandlerUPP
1648 GetEventTypeCount (specs), specs, 1641 (menu_target_item_handler),
1649 NULL, NULL); 1642 GetEventTypeCount (specs),
1650#endif 1643 specs, NULL, NULL);
1651 return err;
1652} 1644}
1645#endif /* TARGET_API_MAC_CARBON */
1653 1646
1654/* Event handler function that pops down a menu on C-g. We can only pop 1647/* Event handler function that pops down a menu on C-g. We can only pop
1655 down menus if CancelMenuTracking is present (OSX 10.3 or later). */ 1648 down menus if CancelMenuTracking is present (OSX 10.3 or later). */
@@ -1687,15 +1680,15 @@ menu_quit_handler (nextHandler, theEvent, userData)
1687} 1680}
1688#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ 1681#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
1689 1682
1690/* Add event handler to all menus that belong to KIND so we can detect C-g. 1683/* Add event handler to all menus that belong to KIND so we can detect
1691 MENU_HANDLE is the root menu of the tracking session to dismiss 1684 C-g. ROOT_MENU is the root menu of the tracking session to dismiss
1692 when C-g is detected. NULL means the menu bar. 1685 when C-g is detected. NULL means the menu bar. If
1693 If CancelMenuTracking isn't available, do nothing. */ 1686 CancelMenuTracking isn't available, do nothing. */
1694 1687
1695static void 1688static void
1696install_menu_quit_handler (kind, menu_handle) 1689install_menu_quit_handler (kind, root_menu)
1697 enum mac_menu_kind kind; 1690 enum mac_menu_kind kind;
1698 MenuHandle menu_handle; 1691 MenuRef root_menu;
1699{ 1692{
1700#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 1693#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
1701 static const EventTypeSpec typesList[] = 1694 static const EventTypeSpec typesList[] =
@@ -1708,13 +1701,13 @@ install_menu_quit_handler (kind, menu_handle)
1708#endif 1701#endif
1709 for (id = min_menu_id[kind]; id < min_menu_id[kind + 1]; id++) 1702 for (id = min_menu_id[kind]; id < min_menu_id[kind + 1]; id++)
1710 { 1703 {
1711 MenuHandle menu = GetMenuHandle (id); 1704 MenuRef menu = GetMenuRef (id);
1712 1705
1713 if (menu == NULL) 1706 if (menu == NULL)
1714 break; 1707 break;
1715 InstallMenuEventHandler (menu, menu_quit_handler, 1708 InstallMenuEventHandler (menu, menu_quit_handler,
1716 GetEventTypeCount (typesList), 1709 GetEventTypeCount (typesList),
1717 typesList, menu_handle, NULL); 1710 typesList, root_menu, NULL);
1718 } 1711 }
1719#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */ 1712#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030 */
1720} 1713}
@@ -1738,10 +1731,13 @@ set_frame_menubar (f, first_time, deep_p)
1738 1731
1739 XSETFRAME (Vmenu_updating_frame, f); 1732 XSETFRAME (Vmenu_updating_frame, f);
1740 1733
1734 /* This seems to be unnecessary for Carbon. */
1735#if 0
1741 if (! menubar_widget) 1736 if (! menubar_widget)
1742 deep_p = 1; 1737 deep_p = 1;
1743 else if (pending_menu_activation && !deep_p) 1738 else if (pending_menu_activation && !deep_p)
1744 deep_p = 1; 1739 deep_p = 1;
1740#endif
1745 1741
1746 if (deep_p) 1742 if (deep_p)
1747 { 1743 {
@@ -1978,7 +1974,7 @@ pop_down_menu (arg)
1978{ 1974{
1979 struct Lisp_Save_Value *p = XSAVE_VALUE (arg); 1975 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
1980 FRAME_PTR f = p->pointer; 1976 FRAME_PTR f = p->pointer;
1981 MenuHandle menu = GetMenuHandle (min_menu_id[MAC_MENU_POPUP]); 1977 MenuRef menu = GetMenuRef (min_menu_id[MAC_MENU_POPUP]);
1982 1978
1983 BLOCK_INPUT; 1979 BLOCK_INPUT;
1984 1980
@@ -2024,8 +2020,7 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error)
2024 int i; 2020 int i;
2025 int menu_item_choice; 2021 int menu_item_choice;
2026 UInt32 menu_item_selection; 2022 UInt32 menu_item_selection;
2027 MenuHandle menu; 2023 MenuRef menu;
2028 Point pos;
2029 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0; 2024 widget_value *wv, *save_wv = 0, *first_wv = 0, *prev_wv = 0;
2030 widget_value **submenu_stack 2025 widget_value **submenu_stack
2031 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *)); 2026 = (widget_value **) alloca (menu_items_used * sizeof (widget_value *));
@@ -2231,11 +2226,8 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error)
2231 free_menubar_widget_value_tree (first_wv); 2226 free_menubar_widget_value_tree (first_wv);
2232 2227
2233 /* Adjust coordinates to be root-window-relative. */ 2228 /* Adjust coordinates to be root-window-relative. */
2234 pos.h = x; 2229 x += f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2235 pos.v = y; 2230 y += f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2236
2237 SetPortWindowPort (FRAME_MAC_WINDOW (f));
2238 LocalToGlobal (&pos);
2239 2231
2240 /* No selection has been chosen yet. */ 2232 /* No selection has been chosen yet. */
2241 menu_item_selection = 0; 2233 menu_item_selection = 0;
@@ -2248,13 +2240,13 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error)
2248 2240
2249 /* Display the menu. */ 2241 /* Display the menu. */
2250 popup_activated_flag = 1; 2242 popup_activated_flag = 1;
2251 menu_item_choice = PopUpMenuSelect (menu, pos.v, pos.h, 0); 2243 menu_item_choice = PopUpMenuSelect (menu, y, x, 0);
2252 popup_activated_flag = 0; 2244 popup_activated_flag = 0;
2253 2245
2254 /* Get the refcon to find the correct item */ 2246 /* Get the refcon to find the correct item */
2255 if (menu_item_choice) 2247 if (menu_item_choice)
2256 { 2248 {
2257 MenuHandle sel_menu = GetMenuHandle (HiWord (menu_item_choice)); 2249 MenuRef sel_menu = GetMenuRef (HiWord (menu_item_choice));
2258 2250
2259 if (sel_menu) 2251 if (sel_menu)
2260 GetMenuItemRefCon (sel_menu, LoWord (menu_item_choice), 2252 GetMenuItemRefCon (sel_menu, LoWord (menu_item_choice),
@@ -2330,13 +2322,24 @@ mac_menu_show (f, x, y, for_click, keymaps, title, error)
2330 2322
2331#if TARGET_API_MAC_CARBON 2323#if TARGET_API_MAC_CARBON
2332 2324
2325#define DIALOG_BUTTON_COMMAND_ID_OFFSET 'Bt\0\0'
2326#define DIALOG_BUTTON_COMMAND_ID_P(id) \
2327 (((id) & ~0xffff) == DIALOG_BUTTON_COMMAND_ID_OFFSET)
2328#define DIALOG_BUTTON_COMMAND_ID_VALUE(id) \
2329 ((id) - DIALOG_BUTTON_COMMAND_ID_OFFSET)
2330#define DIALOG_BUTTON_MAKE_COMMAND_ID(value) \
2331 ((value) + DIALOG_BUTTON_COMMAND_ID_OFFSET)
2332
2333extern EMACS_TIME timer_check P_ ((int));
2334static int quit_dialog_event_loop;
2335
2333static pascal OSStatus 2336static pascal OSStatus
2334mac_handle_dialog_event (next_handler, event, data) 2337mac_handle_dialog_event (next_handler, event, data)
2335 EventHandlerCallRef next_handler; 2338 EventHandlerCallRef next_handler;
2336 EventRef event; 2339 EventRef event;
2337 void *data; 2340 void *data;
2338{ 2341{
2339 OSStatus err; 2342 OSStatus err, result = eventNotHandledErr;
2340 WindowRef window = (WindowRef) data; 2343 WindowRef window = (WindowRef) data;
2341 2344
2342 switch (GetEventClass (event)) 2345 switch (GetEventClass (event))
@@ -2349,15 +2352,14 @@ mac_handle_dialog_event (next_handler, event, data)
2349 typeHICommand, NULL, sizeof (HICommand), 2352 typeHICommand, NULL, sizeof (HICommand),
2350 NULL, &command); 2353 NULL, &command);
2351 if (err == noErr) 2354 if (err == noErr)
2352 if ((command.commandID & ~0xffff) == 'Bt\0\0') 2355 if (DIALOG_BUTTON_COMMAND_ID_P (command.commandID))
2353 { 2356 {
2354 SetWRefCon (window, command.commandID); 2357 SetWRefCon (window, command.commandID);
2355 err = QuitAppModalLoopForWindow (window); 2358 quit_dialog_event_loop = 1;
2356 2359 break;
2357 return err == noErr ? noErr : eventNotHandledErr;
2358 } 2360 }
2359 2361
2360 return CallNextEventHandler (next_handler, event); 2362 result = CallNextEventHandler (next_handler, event);
2361 } 2363 }
2362 break; 2364 break;
2363 2365
@@ -2367,8 +2369,8 @@ mac_handle_dialog_event (next_handler, event, data)
2367 char char_code; 2369 char char_code;
2368 2370
2369 result = CallNextEventHandler (next_handler, event); 2371 result = CallNextEventHandler (next_handler, event);
2370 if (result == noErr) 2372 if (result != eventNotHandledErr)
2371 return noErr; 2373 break;
2372 2374
2373 err = GetEventParameter (event, kEventParamKeyMacCharCodes, 2375 err = GetEventParameter (event, kEventParamKeyMacCharCodes,
2374 typeChar, NULL, sizeof (char), 2376 typeChar, NULL, sizeof (char),
@@ -2377,7 +2379,7 @@ mac_handle_dialog_event (next_handler, event, data)
2377 switch (char_code) 2379 switch (char_code)
2378 { 2380 {
2379 case kEscapeCharCode: 2381 case kEscapeCharCode:
2380 err = QuitAppModalLoopForWindow (window); 2382 quit_dialog_event_loop = 1;
2381 break; 2383 break;
2382 2384
2383 default: 2385 default:
@@ -2392,23 +2394,26 @@ mac_handle_dialog_event (next_handler, event, data)
2392 typeUInt32, NULL, sizeof (UInt32), 2394 typeUInt32, NULL, sizeof (UInt32),
2393 NULL, &key_code); 2395 NULL, &key_code);
2394 if (err == noErr) 2396 if (err == noErr)
2395 { 2397 if (mac_quit_char_key_p (modifiers, key_code))
2396 if (mac_quit_char_key_p (modifiers, key_code)) 2398 quit_dialog_event_loop = 1;
2397 err = QuitAppModalLoopForWindow (window);
2398 else
2399 err = eventNotHandledErr;
2400 }
2401 } 2399 }
2402 break; 2400 break;
2403 } 2401 }
2404
2405 return err == noErr ? noErr : result;
2406 } 2402 }
2407 break; 2403 break;
2408 2404
2409 default: 2405 default:
2410 abort (); 2406 abort ();
2411 } 2407 }
2408
2409 if (quit_dialog_event_loop)
2410 {
2411 err = QuitEventLoop (GetCurrentEventLoop ());
2412 if (err == noErr)
2413 result = noErr;
2414 }
2415
2416 return result;
2412} 2417}
2413 2418
2414static OSStatus 2419static OSStatus
@@ -2443,6 +2448,25 @@ install_dialog_event_handler (window)
2443#define DIALOG_ICON_LEFT_MARGIN (24) 2448#define DIALOG_ICON_LEFT_MARGIN (24)
2444#define DIALOG_ICON_TOP_MARGIN (15) 2449#define DIALOG_ICON_TOP_MARGIN (15)
2445 2450
2451static Lisp_Object
2452pop_down_dialog (arg)
2453 Lisp_Object arg;
2454{
2455 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
2456 WindowRef window = p->pointer;
2457
2458 BLOCK_INPUT;
2459
2460 if (popup_activated_flag)
2461 EndAppModalStateForWindow (window);
2462 DisposeWindow (window);
2463 popup_activated_flag = 0;
2464
2465 UNBLOCK_INPUT;
2466
2467 return Qnil;
2468}
2469
2446static int 2470static int
2447create_and_show_dialog (f, first_wv) 2471create_and_show_dialog (f, first_wv)
2448 FRAME_PTR f; 2472 FRAME_PTR f;
@@ -2456,6 +2480,7 @@ create_and_show_dialog (f, first_wv)
2456 Rect empty_rect, *rects; 2480 Rect empty_rect, *rects;
2457 WindowRef window = NULL; 2481 WindowRef window = NULL;
2458 ControlRef *buttons, default_button = NULL, text; 2482 ControlRef *buttons, default_button = NULL, text;
2483 int specpdl_count = SPECPDL_INDEX ();
2459 2484
2460 dialog_name = first_wv->name; 2485 dialog_name = first_wv->name;
2461 nb_buttons = dialog_name[1] - '0'; 2486 nb_buttons = dialog_name[1] - '0';
@@ -2472,8 +2497,11 @@ create_and_show_dialog (f, first_wv)
2472 kWindowStandardHandlerAttribute, 2497 kWindowStandardHandlerAttribute,
2473 &empty_rect, &window); 2498 &empty_rect, &window);
2474 if (err == noErr) 2499 if (err == noErr)
2475 err = SetThemeWindowBackground (window, kThemeBrushMovableModalBackground, 2500 {
2476 true); 2501 record_unwind_protect (pop_down_dialog, make_save_value (window, 0));
2502 err = SetThemeWindowBackground (window, kThemeBrushMovableModalBackground,
2503 true);
2504 }
2477 if (err == noErr) 2505 if (err == noErr)
2478 err = SetWindowTitleWithCFString (window, (dialog_name[0] == 'Q' 2506 err = SetWindowTitleWithCFString (window, (dialog_name[0] == 'Q'
2479 ? CFSTR ("Question") 2507 ? CFSTR ("Question")
@@ -2518,14 +2546,16 @@ create_and_show_dialog (f, first_wv)
2518 } 2546 }
2519 if (err == noErr) 2547 if (err == noErr)
2520 { 2548 {
2549 UInt32 command_id;
2550
2521 OffsetRect (&rects[i], -rects[i].left, -rects[i].top); 2551 OffsetRect (&rects[i], -rects[i].left, -rects[i].top);
2522 if (rects[i].right < DIALOG_BUTTON_MIN_WIDTH) 2552 if (rects[i].right < DIALOG_BUTTON_MIN_WIDTH)
2523 rects[i].right = DIALOG_BUTTON_MIN_WIDTH; 2553 rects[i].right = DIALOG_BUTTON_MIN_WIDTH;
2524 else if (rects[i].right > DIALOG_MAX_INNER_WIDTH) 2554 else if (rects[i].right > DIALOG_MAX_INNER_WIDTH)
2525 rects[i].right = DIALOG_MAX_INNER_WIDTH; 2555 rects[i].right = DIALOG_MAX_INNER_WIDTH;
2526 2556
2527 err = SetControlCommandID (buttons[i], 2557 command_id = DIALOG_BUTTON_MAKE_COMMAND_ID ((int) wv->call_data);
2528 'Bt\0\0' + (int) wv->call_data); 2558 err = SetControlCommandID (buttons[i], command_id);
2529 } 2559 }
2530 if (err != noErr) 2560 if (err != noErr)
2531 break; 2561 break;
@@ -2696,18 +2726,66 @@ create_and_show_dialog (f, first_wv)
2696 SetWRefCon (window, 0); 2726 SetWRefCon (window, 0);
2697 ShowWindow (window); 2727 ShowWindow (window);
2698 BringToFront (window); 2728 BringToFront (window);
2699 err = RunAppModalLoopForWindow (window); 2729 popup_activated_flag = 1;
2730 err = BeginAppModalStateForWindow (window);
2731 }
2732 if (err == noErr)
2733 {
2734 EventTargetRef toolbox_dispatcher = GetEventDispatcherTarget ();
2735
2736 quit_dialog_event_loop = 0;
2737 while (1)
2738 {
2739 EMACS_TIME next_time = timer_check (1);
2740 long secs = EMACS_SECS (next_time);
2741 long usecs = EMACS_USECS (next_time);
2742 EventTimeout timeout;
2743 EventRef event;
2744
2745 if (secs < 0 || (secs == 0 && usecs == 0))
2746 {
2747 /* Sometimes timer_check returns -1 (no timers) even if
2748 there are timers. So do a timeout anyway. */
2749 secs = 1;
2750 usecs = 0;
2751 }
2752
2753 timeout = (secs * kEventDurationSecond
2754 + usecs * kEventDurationMicrosecond);
2755 err = ReceiveNextEvent (0, NULL, timeout, kEventRemoveFromQueue,
2756 &event);
2757 if (err == noErr)
2758 {
2759 SendEventToEventTarget (event, toolbox_dispatcher);
2760 ReleaseEvent (event);
2761 }
2762#if 0 /* defined (MAC_OSX) */
2763 else if (err != eventLoopTimedOutErr)
2764 {
2765 if (err == eventLoopQuitErr)
2766 err = noErr;
2767 break;
2768 }
2769#else
2770 /* The return value of ReceiveNextEvent seems to be
2771 unreliable. Use our own global variable instead. */
2772 if (quit_dialog_event_loop)
2773 {
2774 err = noErr;
2775 break;
2776 }
2777#endif
2778 }
2700 } 2779 }
2701 if (err == noErr) 2780 if (err == noErr)
2702 { 2781 {
2703 UInt32 command_id = GetWRefCon (window); 2782 UInt32 command_id = GetWRefCon (window);
2704 2783
2705 if ((command_id & ~0xffff) == 'Bt\0\0') 2784 if (DIALOG_BUTTON_COMMAND_ID_P (command_id))
2706 result = command_id - 'Bt\0\0'; 2785 result = DIALOG_BUTTON_COMMAND_ID_VALUE (command_id);
2707 } 2786 }
2708 2787
2709 if (window) 2788 unbind_to (specpdl_count, Qnil);
2710 DisposeWindow (window);
2711 2789
2712 return result; 2790 return result;
2713} 2791}
@@ -2724,8 +2802,8 @@ mac_dialog (widget_value *wv)
2724 int i; 2802 int i;
2725 int dialog_width; 2803 int dialog_width;
2726 Rect rect; 2804 Rect rect;
2727 WindowPtr window_ptr; 2805 WindowRef window_ptr;
2728 ControlHandle ch; 2806 ControlRef ch;
2729 int left; 2807 int left;
2730 EventRecord event_record; 2808 EventRecord event_record;
2731 SInt16 part_code; 2809 SInt16 part_code;
@@ -2754,7 +2832,7 @@ mac_dialog (widget_value *wv)
2754 wv = wv->next; 2832 wv = wv->next;
2755 } 2833 }
2756 2834
2757 window_ptr = GetNewCWindow (DIALOG_WINDOW_RESOURCE, NULL, (WindowPtr) -1); 2835 window_ptr = GetNewCWindow (DIALOG_WINDOW_RESOURCE, NULL, (WindowRef) -1);
2758 2836
2759 SetPortWindowPort (window_ptr); 2837 SetPortWindowPort (window_ptr);
2760 2838
@@ -3031,7 +3109,7 @@ name_is_separator (name)
3031 3109
3032static void 3110static void
3033add_menu_item (menu, pos, wv) 3111add_menu_item (menu, pos, wv)
3034 MenuHandle menu; 3112 MenuRef menu;
3035 int pos; 3113 int pos;
3036 widget_value *wv; 3114 widget_value *wv;
3037{ 3115{
@@ -3108,7 +3186,7 @@ add_menu_item (menu, pos, wv)
3108 3186
3109static int 3187static int
3110fill_menu (menu, wv, kind, submenu_id) 3188fill_menu (menu, wv, kind, submenu_id)
3111 MenuHandle menu; 3189 MenuRef menu;
3112 widget_value *wv; 3190 widget_value *wv;
3113 enum mac_menu_kind kind; 3191 enum mac_menu_kind kind;
3114 int submenu_id; 3192 int submenu_id;
@@ -3120,10 +3198,14 @@ fill_menu (menu, wv, kind, submenu_id)
3120 add_menu_item (menu, pos, wv); 3198 add_menu_item (menu, pos, wv);
3121 if (wv->contents && submenu_id < min_menu_id[kind + 1]) 3199 if (wv->contents && submenu_id < min_menu_id[kind + 1])
3122 { 3200 {
3123 MenuHandle submenu = NewMenu (submenu_id, "\pX"); 3201 MenuRef submenu = NewMenu (submenu_id, "\pX");
3124 3202
3125 InsertMenu (submenu, -1); 3203 InsertMenu (submenu, -1);
3204#if TARGET_API_MAC_CARBON
3205 SetMenuItemHierarchicalMenu (menu, pos, submenu);
3206#else
3126 SetMenuItemHierarchicalID (menu, pos, submenu_id); 3207 SetMenuItemHierarchicalID (menu, pos, submenu_id);
3208#endif
3127 submenu_id = fill_menu (submenu, wv->contents, kind, submenu_id + 1); 3209 submenu_id = fill_menu (submenu, wv->contents, kind, submenu_id + 1);
3128 } 3210 }
3129 } 3211 }
@@ -3139,8 +3221,6 @@ fill_menubar (wv, deep_p)
3139 int deep_p; 3221 int deep_p;
3140{ 3222{
3141 int id, submenu_id; 3223 int id, submenu_id;
3142 MenuHandle menu;
3143 Str255 title;
3144#if !TARGET_API_MAC_CARBON 3224#if !TARGET_API_MAC_CARBON
3145 int title_changed_p = 0; 3225 int title_changed_p = 0;
3146#endif 3226#endif
@@ -3162,41 +3242,59 @@ fill_menubar (wv, deep_p)
3162 wv != NULL && id < min_menu_id[MAC_MENU_MENU_BAR + 1]; 3242 wv != NULL && id < min_menu_id[MAC_MENU_MENU_BAR + 1];
3163 wv = wv->next, id++) 3243 wv = wv->next, id++)
3164 { 3244 {
3245 OSStatus err = noErr;
3246 MenuRef menu;
3247#if TARGET_API_MAC_CARBON
3248 CFStringRef title;
3249
3250 title = CFStringCreateWithCString (NULL, wv->name,
3251 kCFStringEncodingMacRoman);
3252#else
3253 Str255 title;
3254
3165 strncpy (title, wv->name, 255); 3255 strncpy (title, wv->name, 255);
3166 title[255] = '\0'; 3256 title[255] = '\0';
3167 c2pstr (title); 3257 c2pstr (title);
3258#endif
3168 3259
3169 menu = GetMenuHandle (id); 3260 menu = GetMenuRef (id);
3170 if (menu) 3261 if (menu)
3171 { 3262 {
3172#if TARGET_API_MAC_CARBON 3263#if TARGET_API_MAC_CARBON
3173 Str255 old_title; 3264 CFStringRef old_title;
3174 3265
3175 GetMenuTitle (menu, old_title); 3266 err = CopyMenuTitleAsCFString (menu, &old_title);
3176 if (!EqualString (title, old_title, false, false)) 3267 if (err == noErr)
3177 { 3268 {
3178#ifdef MAC_OSX 3269 if (CFStringCompare (title, old_title, 0) != kCFCompareEqualTo)
3179 if (id + 1 == min_menu_id[MAC_MENU_MENU_BAR + 1]
3180 || GetMenuRef (id + 1) == NULL)
3181 { 3270 {
3182 /* This is a workaround for Mac OS X 10.5 where just 3271#ifdef MAC_OSX
3183 calling SetMenuTitle fails to change the title of 3272 if (id + 1 == min_menu_id[MAC_MENU_MENU_BAR + 1]
3184 the last (Help) menu in the menu bar. */ 3273 || GetMenuRef (id + 1) == NULL)
3185 DeleteMenu (id); 3274 {
3186 DisposeMenu (menu); 3275 /* This is a workaround for Mac OS X 10.5 where
3187 menu = NULL; 3276 just calling SetMenuTitleWithCFString fails
3188 } 3277 to change the title of the last (Help) menu
3189 else 3278 in the menu bar. */
3279 DeleteMenu (id);
3280 DisposeMenu (menu);
3281 menu = NULL;
3282 }
3283 else
3190#endif /* MAC_OSX */ 3284#endif /* MAC_OSX */
3191 SetMenuTitle (menu, title); 3285 err = SetMenuTitleWithCFString (menu, title);
3286 }
3287 CFRelease (old_title);
3192 } 3288 }
3289 else
3290 err = SetMenuTitleWithCFString (menu, title);
3193#else /* !TARGET_API_MAC_CARBON */ 3291#else /* !TARGET_API_MAC_CARBON */
3194 if (!EqualString (title, (*menu)->menuData, false, false)) 3292 if (!EqualString (title, (*menu)->menuData, false, false))
3195 { 3293 {
3196 DeleteMenu (id); 3294 DeleteMenu (id);
3197 DisposeMenu (menu); 3295 DisposeMenu (menu);
3198 menu = NewMenu (id, title); 3296 menu = NewMenu (id, title);
3199 InsertMenu (menu, GetMenuHandle (id + 1) ? id + 1 : 0); 3297 InsertMenu (menu, GetMenuRef (id + 1) ? id + 1 : 0);
3200 title_changed_p = 1; 3298 title_changed_p = 1;
3201 } 3299 }
3202#endif /* !TARGET_API_MAC_CARBON */ 3300#endif /* !TARGET_API_MAC_CARBON */
@@ -3204,19 +3302,32 @@ fill_menubar (wv, deep_p)
3204 3302
3205 if (!menu) 3303 if (!menu)
3206 { 3304 {
3305#if TARGET_API_MAC_CARBON
3306 err = CreateNewMenu (id, 0, &menu);
3307 if (err == noErr)
3308 err = SetMenuTitleWithCFString (menu, title);
3309#else
3207 menu = NewMenu (id, title); 3310 menu = NewMenu (id, title);
3208 InsertMenu (menu, 0); 3311#endif
3312 if (err == noErr)
3313 {
3314 InsertMenu (menu, 0);
3209#if !TARGET_API_MAC_CARBON 3315#if !TARGET_API_MAC_CARBON
3210 title_changed_p = 1; 3316 title_changed_p = 1;
3211#endif 3317#endif
3318 }
3212 } 3319 }
3320#if TARGET_API_MAC_CARBON
3321 CFRelease (title);
3322#endif
3213 3323
3214 if (wv->contents) 3324 if (err == noErr)
3215 submenu_id = fill_menu (menu, wv->contents, MAC_MENU_MENU_BAR_SUB, 3325 if (wv->contents)
3216 submenu_id); 3326 submenu_id = fill_menu (menu, wv->contents, MAC_MENU_MENU_BAR_SUB,
3327 submenu_id);
3217 } 3328 }
3218 3329
3219 if (id < min_menu_id[MAC_MENU_MENU_BAR + 1] && GetMenuHandle (id)) 3330 if (id < min_menu_id[MAC_MENU_MENU_BAR + 1] && GetMenuRef (id))
3220 { 3331 {
3221 dispose_menus (MAC_MENU_MENU_BAR, id); 3332 dispose_menus (MAC_MENU_MENU_BAR, id);
3222#if !TARGET_API_MAC_CARBON 3333#if !TARGET_API_MAC_CARBON
@@ -3240,7 +3351,7 @@ dispose_menus (kind, id)
3240{ 3351{
3241 for (id = max (id, min_menu_id[kind]); id < min_menu_id[kind + 1]; id++) 3352 for (id = max (id, min_menu_id[kind]); id < min_menu_id[kind + 1]; id++)
3242 { 3353 {
3243 MenuHandle menu = GetMenuHandle (id); 3354 MenuRef menu = GetMenuRef (id);
3244 3355
3245 if (menu == NULL) 3356 if (menu == NULL)
3246 break; 3357 break;
@@ -3265,9 +3376,13 @@ DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_
3265 doc: /* Return t if a menu or popup dialog is active. */) 3376 doc: /* Return t if a menu or popup dialog is active. */)
3266 () 3377 ()
3267{ 3378{
3379#if TARGET_API_MAC_CARBON
3380 return (popup_activated ()) ? Qt : Qnil;
3381#else
3268 /* Always return Qnil since menu selection functions do not return 3382 /* Always return Qnil since menu selection functions do not return
3269 until a selection has been made or cancelled. */ 3383 until a selection has been made or cancelled. */
3270 return Qnil; 3384 return Qnil;
3385#endif
3271} 3386}
3272 3387
3273void 3388void