X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/eb1dd42a729ab1b9cdcd278767951679480cd4c3..8131fcfd622dd2b7fccf09b0f6a1c3f4747619a7:/src/nsmenu.m diff --git a/src/nsmenu.m b/src/nsmenu.m index 15878dc74d..5af813ac75 100644 --- a/src/nsmenu.m +++ b/src/nsmenu.m @@ -45,8 +45,6 @@ Carbon version by Yamamoto Mitsuharu. */ #include #endif -#define MenuStagger 10.0 - #if 0 int menu_trace_num = 0; #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \ @@ -112,14 +110,13 @@ popup_activated (void) 2) deep_p, submenu = nil: Recompute all submenus. 3) deep_p, submenu = non-nil: Update contents of a single submenu. -------------------------------------------------------------------------- */ -void +static void ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) { NSAutoreleasePool *pool; id menu = [NSApp mainMenu]; static EmacsMenu *last_submenu = nil; BOOL needsSet = NO; - const char *submenuTitle = [[submenu title] UTF8String]; bool owfi; Lisp_Object items; widget_value *wv, *first_wv, *prev_wv = 0; @@ -239,7 +236,7 @@ ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) /* FIXME: we'd like to only parse the needed submenu, but this was causing crashes in the _common parsing code.. need to make sure proper initialization done.. */ -/* if (submenu && strcmp (submenuTitle, SSDATA (string))) +/* if (submenu && strcmp ([[submenu title] UTF8String], SSDATA (string))) continue; */ submenu_start[i] = menu_items_used; @@ -259,7 +256,7 @@ ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) { /* should have found a menu for this one but didn't */ fprintf (stderr, "ERROR: did not find lisp menu for submenu '%s'.\n", - submenuTitle); + [[submenu title] UTF8String]); discard_menu_items (); unbind_to (specpdl_count, Qnil); [pool release]; @@ -346,8 +343,6 @@ ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) string = AREF (items, i + 1); if (NILP (string)) break; -/* if (submenu && strcmp (submenuTitle, SSDATA (string))) - continue; */ wv->name = SSDATA (string); update_submenu_strings (wv->contents); @@ -358,6 +353,7 @@ ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) create a new menu for each sub and fill it. */ if (submenu) { + const char *submenuTitle = [[submenu title] UTF8String]; for (wv = first_wv->contents; wv; wv = wv->next) { if (!strcmp (submenuTitle, wv->name)) @@ -370,7 +366,7 @@ ns_update_menubar (struct frame *f, bool deep_p, EmacsMenu *submenu) } else { - [menu fillWithWidgetValue: first_wv->contents]; + [menu fillWithWidgetValue: first_wv->contents frame: f]; } } @@ -507,22 +503,12 @@ set_frame_menubar (struct frame *f, bool first_time, bool deep_p) void x_activate_menubar (struct frame *f) { - NSArray *a = [[NSApp mainMenu] itemArray]; - /* Update each submenu separately so ns_update_menubar doesn't reset - the delegate. */ - int i = 0; - while (i < [a count]) - { - EmacsMenu *menu = (EmacsMenu *)[[a objectAtIndex:i] submenu]; - const char *title = [[menu title] UTF8String]; - if (strcmp (title, ns_get_pending_menu_title ()) == 0) - { - ns_update_menubar (f, true, menu); - break; - } - ++i; - } +#ifdef NS_IMPL_COCOA +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 + ns_update_menubar (f, true, nil); ns_check_pending_open_menu (); +#endif +#endif } @@ -543,6 +529,7 @@ x_activate_menubar (struct frame *f) /* override designated initializer */ - initWithTitle: (NSString *)title { + frame = 0; if ((self = [super initWithTitle: title])) [self setAutoenablesItems: NO]; return self; @@ -578,17 +565,36 @@ extern NSString *NSMenuDidBeginTrackingNotification; /* Update menu in menuNeedsUpdate only while tracking menus. */ trackingMenu = ([notification name] == NSMenuDidBeginTrackingNotification ? 1 : 0); +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 + if (! trackingMenu) ns_check_menu_open (nil); +#endif } #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 - (void)menuWillOpen:(NSMenu *)menu { - ns_check_menu_open (menu); -} -#endif + ++trackingMenu; +#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 + // On 10.6 we get repeated calls, only the one for NSSystemDefined is "real". + if ([[NSApp currentEvent] type] != NSSystemDefined) return; #endif + /* When dragging from one menu to another, we get willOpen followed by didClose, + i.e. trackingMenu == 3 in willOpen and then 2 after didClose. + We have updated all menus, so avoid doing it when trackingMenu == 3. */ + if (trackingMenu == 2) + ns_check_menu_open (menu); +} + +- (void)menuDidClose:(NSMenu *)menu +{ + --trackingMenu; +} +#endif /* OSX >= 10.5 */ + +#endif /* NS_IMPL_COCOA */ + /* delegate method called when a submenu is being opened: run a 'deep' call to set_frame_menubar */ - (void)menuNeedsUpdate: (NSMenu *)menu @@ -614,8 +620,8 @@ extern NSString *NSMenuDidBeginTrackingNotification; if (trackingMenu == 0) return; /*fprintf (stderr, "Updating menu '%s'\n", [[self title] UTF8String]); NSLog (@"%@\n", event); */ -#if ! defined(NS_IMPL_COCOA) || \ - MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 +#if (! defined (NS_IMPL_COCOA) \ + || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5) /* Don't know how to do this for anything other than OSX >= 10.5 This is wrong, as it might run Lisp code in the event loop. */ ns_update_menubar (frame, true, self); @@ -723,6 +729,11 @@ extern NSString *NSMenuDidBeginTrackingNotification; - (void)fillWithWidgetValue: (void *)wvptr +{ + [self fillWithWidgetValue: wvptr frame:nil]; +} + +- (void)fillWithWidgetValue: (void *)wvptr frame: (struct frame *)f { widget_value *wv = (widget_value *)wvptr; @@ -737,12 +748,17 @@ extern NSString *NSMenuDidBeginTrackingNotification; if (wv->contents) { - EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: [item title]]; + EmacsMenu *submenu; + + if (f) + submenu = [[EmacsMenu alloc] initWithTitle: [item title] frame:f]; + else + submenu = [[EmacsMenu alloc] initWithTitle: [item title]]; [self setSubmenu: submenu forItem: item]; [submenu fillWithWidgetValue: wv->contents]; [submenu release]; - [item setAction: nil]; + [item setAction: (SEL)nil]; } } @@ -759,7 +775,7 @@ extern NSString *NSMenuDidBeginTrackingNotification; { NSString *titleStr = [NSString stringWithUTF8String: title]; NSMenuItem *item = [self addItemWithTitle: titleStr - action: nil /*@selector (menuDown:) */ + action: (SEL)nil /*@selector (menuDown:) */ keyEquivalent: @""]; EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: titleStr frame: f]; [self setSubmenu: submenu forItem: item]; @@ -808,7 +824,7 @@ extern NSString *NSMenuDidBeginTrackingNotification; ========================================================================== */ Lisp_Object -ns_menu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps, +ns_menu_show (struct frame *f, int x, int y, bool for_click, bool keymaps, Lisp_Object title, const char **error) { EmacsMenu *pmenu; @@ -1030,7 +1046,7 @@ ns_menu_show (FRAME_PTR f, int x, int y, bool for_click, bool keymaps, ========================================================================== */ void -free_frame_tool_bar (FRAME_PTR f) +free_frame_tool_bar (struct frame *f) /* -------------------------------------------------------------------------- Under NS we just hide the toolbar until it might be needed again. -------------------------------------------------------------------------- */ @@ -1042,18 +1058,23 @@ free_frame_tool_bar (FRAME_PTR f) } void -update_frame_tool_bar (FRAME_PTR f) +update_frame_tool_bar (struct frame *f) /* -------------------------------------------------------------------------- Update toolbar contents -------------------------------------------------------------------------- */ { - int i; + int i, k = 0; EmacsView *view = FRAME_NS_VIEW (f); NSWindow *window = [view window]; EmacsToolbar *toolbar = [view toolbar]; block_input (); + +#ifdef NS_IMPL_COCOA [toolbar clearActive]; +#else + [toolbar clearAll]; +#endif /* update EmacsToolbar as in GtkUtils, build items list */ for (i = 0; i < f->n_tool_bar_items; ++i) @@ -1069,6 +1090,15 @@ update_frame_tool_bar (FRAME_PTR f) Lisp_Object helpObj; const char *helpText; + /* Check if this is a separator. */ + if (EQ (TOOLPROP (TOOL_BAR_ITEM_TYPE), Qt)) + { + /* Skip separators. Newer OSX don't show them, and on GNUStep they + are wide as a button, thus overflowing the toolbar most of + the time. */ + continue; + } + /* If image is a vector, choose the image according to the button state. */ image = TOOLPROP (TOOL_BAR_ITEM_IMAGES); @@ -1105,7 +1135,10 @@ update_frame_tool_bar (FRAME_PTR f) continue; } - [toolbar addDisplayItemWithImage: img->pixmap idx: i helpText: helpText + [toolbar addDisplayItemWithImage: img->pixmap + idx: k++ + tag: i + helpText: helpText enabled: enabled_p]; #undef TOOLPROP } @@ -1113,6 +1146,7 @@ update_frame_tool_bar (FRAME_PTR f) if (![toolbar isVisible]) [toolbar setVisible: YES]; +#ifdef NS_IMPL_COCOA if ([toolbar changed]) { /* inform app that toolbar has changed */ @@ -1134,6 +1168,7 @@ update_frame_tool_bar (FRAME_PTR f) [toolbar setConfigurationFromDictionary: newDict]; [newDict release]; } +#endif FRAME_TOOLBAR_HEIGHT (f) = NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)]) @@ -1161,6 +1196,7 @@ update_frame_tool_bar (FRAME_PTR f) [self setDelegate: self]; identifierToItem = [[NSMutableDictionary alloc] initWithCapacity: 10]; activeIdentifiers = [[NSMutableArray alloc] initWithCapacity: 8]; + prevIdentifiers = nil; prevEnablement = enablement = 0L; return self; } @@ -1182,18 +1218,29 @@ update_frame_tool_bar (FRAME_PTR f) enablement = 0L; } +- (void) clearAll +{ + [self clearActive]; + while ([[self items] count] > 0) + [self removeItemAtIndex: 0]; +} + - (BOOL) changed { return [activeIdentifiers isEqualToArray: prevIdentifiers] && enablement == prevEnablement ? NO : YES; } -- (void) addDisplayItemWithImage: (EmacsImage *)img idx: (int)idx - helpText: (const char *)help enabled: (BOOL)enabled +- (void) addDisplayItemWithImage: (EmacsImage *)img + idx: (int)idx + tag: (int)tag + helpText: (const char *)help + enabled: (BOOL)enabled { /* 1) come up w/identifier */ NSString *identifier = [NSString stringWithFormat: @"%u", [img hash]]; + [activeIdentifiers addObject: identifier]; /* 2) create / reuse item */ NSToolbarItem *item = [identifierToItem objectForKey: identifier]; @@ -1205,20 +1252,25 @@ update_frame_tool_bar (FRAME_PTR f) [item setToolTip: [NSString stringWithUTF8String: help]]; [item setTarget: emacsView]; [item setAction: @selector (toolbarClicked:)]; + [identifierToItem setObject: item forKey: identifier]; } - [item setTag: idx]; +#ifdef NS_IMPL_GNUSTEP + [self insertItemWithItemIdentifier: identifier atIndex: idx]; +#endif + + [item setTag: tag]; [item setEnabled: enabled]; /* 3) update state */ - [identifierToItem setObject: item forKey: identifier]; - [activeIdentifiers addObject: identifier]; enablement = (enablement << 1) | (enabled == YES); } /* This overrides super's implementation, which automatically sets all items to enabled state (for some reason). */ -- (void)validateVisibleItems { } +- (void)validateVisibleItems +{ +} /* delegate methods */ @@ -1241,7 +1293,8 @@ update_frame_tool_bar (FRAME_PTR f) - (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar { /* return entire set... */ - return [identifierToItem allKeys]; + return activeIdentifiers; + //return [identifierToItem allKeys]; } /* optional and unneeded */ @@ -1375,10 +1428,10 @@ struct Popdown_data EmacsDialogPanel *dialog; }; -static Lisp_Object -pop_down_menu (Lisp_Object arg) +static void +pop_down_menu (void *arg) { - struct Popdown_data *unwind_data = XSAVE_POINTER (arg, 0); + struct Popdown_data *unwind_data = arg; block_input (); if (popup_activated_flag) @@ -1392,8 +1445,6 @@ pop_down_menu (Lisp_Object arg) xfree (unwind_data); unblock_input (); - - return Qnil; } @@ -1409,8 +1460,6 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) NSTRACE (x-popup-dialog); - check_ns (); - isQ = NILP (header); if (EQ (position, Qt) @@ -1448,6 +1497,8 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) else CHECK_WINDOW (window); + check_window_system (f); + p.x = (int)f->left_pos + ((int)FRAME_COLUMN_WIDTH (f) * f->text_cols)/2; p.y = (int)f->top_pos + (FRAME_LINE_HEIGHT (f) * f->text_lines)/2; @@ -1457,7 +1508,7 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) if (NILP (Fcar (Fcdr (contents)))) /* No buttons specified, add an "Ok" button so users can pop down the dialog. */ - contents = Fcons (title, Fcons (Fcons (build_string ("Ok"), Qt), Qnil)); + contents = list2 (title, Fcons (build_string ("Ok"), Qt)); block_input (); pool = [[NSAutoreleasePool alloc] init]; @@ -1471,7 +1522,7 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) unwind_data->pool = pool; unwind_data->dialog = dialog; - record_unwind_protect (pop_down_menu, make_save_pointer (unwind_data)); + record_unwind_protect_ptr (pop_down_menu, unwind_data); popup_activated_flag = 1; tem = [dialog runDialogAt: p]; unbind_to (specpdl_count, Qnil); /* calls pop_down_menu */ @@ -1533,7 +1584,7 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) [img autorelease]; [imgView autorelease]; - aStyle = NSTitledWindowMask; + aStyle = NSTitledWindowMask|NSClosableWindowMask|NSUtilityWindowMask; flag = YES; rows = 0; cols = 1; @@ -1601,9 +1652,6 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) [self setOneShot: YES]; [self setReleasedWhenClosed: YES]; [self setHidesOnDeactivate: YES]; - [self setStyleMask: - NSTitledWindowMask|NSClosableWindowMask|NSUtilityWindowMask]; - return self; } @@ -1635,7 +1683,7 @@ ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header) } if (buttons > 0) - button_values = (Lisp_Object *) xmalloc (buttons * sizeof (*button_values)); + button_values = xmalloc (buttons * sizeof *button_values); for (; XTYPE (list) == Lisp_Cons; list = XCDR (list)) {