/* NeXT/Open/GNUstep and MacOSX Cocoa menu and toolbar module.
- Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+ Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Emacs.
Christian Limpach, Scott Bender, Christophe de Dinechin) and code in the
Carbon version by Yamamoto Mitsuharu. */
+/* This should be the first include, as it may set up #defines affecting
+ interpretation of even the system includes. */
#include "config.h"
+
#include "lisp.h"
#include "window.h"
#include "buffer.h"
#include "termhooks.h"
#include "keyboard.h"
-/* for profiling */
+#define NSMENUPROFILE 0
+
+#if NSMENUPROFILE
#include <sys/timeb.h>
#include <sys/types.h>
+#endif
#define MenuStagger 10.0
#include "nsmenu_common.c"
#endif
-extern struct widget_value;
-
extern Lisp_Object Qundefined, Qmenu_enable, Qmenu_bar_update_hook;
extern Lisp_Object QCtoggle, QCradio;
Qoverriding_local_map, Qoverriding_terminal_local_map;
extern long context_menu_value;
-EmacsMenu *mainMenu, *svcsMenu;
+EmacsMenu *mainMenu, *svcsMenu, *dockMenu;
+
+/* Nonzero means a menu is currently active. */
+static int popup_activated_flag;
+static NSModalSession popupSession;
/* NOTE: toolbar implementation is at end,
following complete menu implementation. */
========================================================================== */
-/*23: FIXME: not currently used, but should normalize with other terms. */
+/* FIXME: not currently used, but should normalize with other terms. */
void
x_activate_menubar (struct frame *f)
{
}
+int
+popup_activated ()
+{
+ return popup_activated_flag;
+}
+
+
/* --------------------------------------------------------------------------
Update menubar. Three cases:
1) deep_p = 0, submenu = nil: Fresh switch onto a frame -- either set up
2) deep_p = 1, submenu = nil: Recompute all submenus.
3) deep_p = 1, submenu = non-nil: Update contents of a single submenu.
-------------------------------------------------------------------------- */
-/*#define NSMENUPROFILE 1 */
void
ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu)
{
widget_value *wv, *first_wv, *prev_wv = 0;
int i;
-#ifdef NSMENUPROFILE
+#if NSMENUPROFILE
struct timeb tb;
long t;
#endif
[attMenu close];
}
-#ifdef NSMENUPROFILE
+#if NSMENUPROFILE
ftime (&tb);
t = -(1000*tb.time+tb.millitm);
#endif
{
/* No change.. */
-#ifdef NSMENUPROFILE
+#if NSMENUPROFILE
ftime (&tb);
t += 1000*tb.time+tb.millitm;
fprintf (stderr, "NO CHANGE! CUTTING OUT after %ld msec.\n", t);
free_menubar_widget_value_tree (first_wv);
-#ifdef NSMENUPROFILE
+#if NSMENUPROFILE
ftime (&tb);
t += 1000*tb.time+tb.millitm;
fprintf (stderr, "Menu update took %ld msec.\n", t);
since key equivalents are handled through emacs.
On Leopard, even keystroke events generate SystemDefined events, but
their subtype is 8. */
- if ([event type] != NSSystemDefined || [event subtype] == 8)
+ if ([event type] != NSSystemDefined || [event subtype] == 8
+ /* Also, don't try this if from an event picked up asynchronously,
+ as lots of lisp evaluation happens in ns_update_menubar. */
+ || handling_signal != 0)
return;
/*fprintf (stderr, "Updating menu '%s'\n", [[self title] UTF8String]); NSLog (@"%@\n", event); */
ns_update_menubar (frame, 1, self);
}
-/* parse a wdiget_value's key rep (examples: 's-p', 's-S', '(C-x C-s)', '<f13>')
- into an accelerator string */
+/* Parse a widget_value's key rep (examples: 's-p', 's-S', '(C-x C-s)', '<f13>')
+ into an accelerator string. We are only able to display a single character
+ for an accelerator, together with an optional modifier combination. (Under
+ Carbon more control was possible, but in Cocoa multi-char strings passed to
+ NSMenuItem get ignored. For now we try to display a super-single letter
+ combo, and return the others as strings to be appended to the item title.
+ (This is signaled by setting keyEquivModMask to 0 for now.) */
-(NSString *)parseKeyEquiv: (char *)key
{
char *tpos = key;
- keyEquivModMask = 0;
- /* currently we just parse 'super' combinations;
- later we'll set keyEquivModMask */
+ keyEquivModMask = NSCommandKeyMask;
+
if (!key || !strlen (key))
return @"";
while (*tpos == ' ' || *tpos == '(')
tpos++;
- if (*tpos != 's'/* || tpos[3] != ')'*/)
- return @"";
+ if (*tpos != 's') {
+ keyEquivModMask = 0; /* signal */
+ return [NSString stringWithUTF8String: tpos];
+ }
return [NSString stringWithFormat: @"%c", tpos[2]];
}
-- (id <NSMenuItem>)addItemWithWidgetValue: (void *)wvptr
+
+- (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr
{
- id <NSMenuItem> item;
+ NSMenuItem *item;
widget_value *wv = (widget_value *)wvptr;
if (name_is_separator (wv->name))
title = @"< ? >"; /* (get out in the open so we know about it) */
keyEq = [self parseKeyEquiv: wv->key];
+#ifdef NS_IMPL_COCOA
+ /* OS X just ignores modifier strings longer than one character */
+ if (keyEquivModMask == 0)
+ title = [title stringByAppendingFormat: @" (%@)", keyEq];
+#endif
item = [self addItemWithTitle: (NSString *)title
action: @selector (menuDown:)
keyEquivalent: keyEq];
- if (keyEquivModMask)
- [item setKeyEquivalentModifierMask: keyEquivModMask];
+ [item setKeyEquivalentModifierMask: keyEquivModMask];
[item setEnabled: wv->enabled];
/* add new contents */
for (; wv != NULL; wv = wv->next)
{
- id <NSMenuItem> item = [self addItemWithWidgetValue: wv];
+ NSMenuItem *item = [self addItemWithWidgetValue: wv];
if (wv->contents)
{
- EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: @"Submenu"];
+ EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: [item title]];
[self setSubmenu: submenu forItem: item];
[submenu fillWithWidgetValue: wv->contents];
- (EmacsMenu *)addSubmenuWithTitle: (char *)title forFrame: (struct frame *)f
{
NSString *titleStr = [NSString stringWithUTF8String: title];
- id <NSMenuItem> item
- = [self addItemWithTitle: titleStr
- action: nil /*@selector (menuDown:) */
- keyEquivalent: @""];
+ NSMenuItem *item = [self addItemWithTitle: titleStr
+ action: nil /*@selector (menuDown:) */
+ keyEquivalent: @""];
EmacsMenu *submenu = [[EmacsMenu alloc] initWithTitle: titleStr frame: f];
[self setSubmenu: submenu forItem: item];
[submenu release];
#endif
wv_title->name = (char *) SDATA (title);
- wv_title->enabled = NULL;
+ wv_title->enabled = NO;
wv_title->button_type = BUTTON_TYPE_NONE;
wv_title->help = Qnil;
wv_title->next = wv_sep;
free_menubar_widget_value_tree (first_wv);
unbind_to (specpdl_count2, Qnil);
+ popup_activated_flag = 1;
tem = [pmenu runMenuAt: p forFrame: f keymaps: keymaps];
+ popup_activated_flag = 0;
[[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
UNBLOCK_INPUT;
+ discard_menu_items ();
unbind_to (specpdl_count, Qnil);
UNGCPRO;
{
NSString *str = [NSString stringWithUTF8String: text];
NSRect r = [textField frame];
- r.size.width = [[[textField font] screenFont] widthOfString: str] + 8;
+ NSSize textSize = [str sizeWithAttributes:
+ [NSDictionary dictionaryWithObject: [[textField font] screenFont]
+ forKey: NSFontAttributeName]];
+ NSSize padSize = [[[textField font] screenFont]
+ boundingRectForFont].size;
+
+ r.size.width = textSize.width + padSize.width/2;
+ r.size.height = textSize.height + padSize.height/2;
[textField setFrame: r];
[textField setStringValue: str];
}
========================================================================== */
+
+static Lisp_Object
+pop_down_menu (Lisp_Object arg)
+{
+ struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
+ if (popup_activated_flag)
+ {
+ popup_activated_flag = 0;
+ BLOCK_INPUT;
+ [NSApp endModalSession: popupSession];
+ [((EmacsDialogPanel *) (p->pointer)) close];
+ [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
+ UNBLOCK_INPUT;
+ }
+ return Qnil;
+}
+
+
Lisp_Object
ns_popup_dialog (Lisp_Object position, Lisp_Object contents, Lisp_Object header)
{
isQ = NILP (header);
- if (EQ (position, Qt))
+ if (EQ (position, Qt)
+ || (CONSP (position) && (EQ (XCAR (position), Qmenu_bar)
+ || EQ (XCAR (position), Qtool_bar))))
{
window = selected_window;
}
window = Fcar (tem); /* POSN_WINDOW (tem) */
}
}
- else if (FRAMEP (position))
+ else if (WINDOWP (position) || FRAMEP (position))
{
window = position;
}
else
- {
- CHECK_LIVE_WINDOW (position);
- window = position;
- }
-
+ window = Qnil;
+
if (FRAMEP (window))
f = XFRAME (window);
- else
+ else if (WINDOWP (window))
{
CHECK_LIVE_WINDOW (window);
f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
}
+ else
+ CHECK_WINDOW (window);
+
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;
+
+ BLOCK_INPUT;
dialog = [[EmacsDialogPanel alloc] initFromContents: contents
isQuestion: isQ];
+ {
+ int specpdl_count = SPECPDL_INDEX ();
+ record_unwind_protect (pop_down_menu, make_save_value (dialog, 0));
+ popup_activated_flag = 1;
+ tem = [dialog runDialogAt: p];
+ unbind_to (specpdl_count, Qnil); /* calls pop_down_menu */
+ }
+ UNBLOCK_INPUT;
- tem = [dialog runDialogAt: p];
-
- [dialog close];
-
- [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
return tem;
}
item = XCAR (list);
if (XTYPE (item) == Lisp_String)
{
- [window addString: XSTRING (item)->data row: row++];
+ [window addString: SDATA (item) row: row++];
}
else if (XTYPE (item) == Lisp_Cons)
{
- [window addButton: XSTRING (XCAR (item))->data
+ [window addButton: SDATA (XCAR (item))
value: XCDR (item) row: row++];
}
else if (NILP (item))
return self;
seltag = [[sellist objectAtIndex: 0] tag];
- if (seltag == XHASH (Qundefined)) // FIXME: BIG UGLY HACK!!
+ if (seltag != XHASH (Qundefined)) // FIXME: BIG UGLY HACK!!
[NSApp stopModalWithCode: seltag];
return self;
}
if (XTYPE (head) == Lisp_String)
[title setStringValue:
- [NSString stringWithUTF8String: XSTRING (head)->data]];
+ [NSString stringWithUTF8String: SDATA (head)]];
else if (isQ == YES)
[title setStringValue: @"Question"];
else
- (Lisp_Object)runDialogAt: (NSPoint)p
{
- NSEvent *e;
- NSModalSession session;
int ret;
- [self center]; /*XXX p ignored? */
- [self orderFront: NSApp];
-
- session = [NSApp beginModalSessionForWindow: self];
- while ((ret = [NSApp runModalSession: session]) == NSRunContinuesResponse)
+ /* initiate a session that will be ended by pop_down_menu */
+ popupSession = [NSApp beginModalSessionForWindow: self];
+ while (popup_activated_flag
+ && (ret = [NSApp runModalSession: popupSession])
+ == NSRunContinuesResponse)
{
- (e = [NSApp nextEventMatchingMask: NSAnyEventMask
- untilDate: [NSDate distantFuture]
- inMode: NSEventTrackingRunLoopMode
- dequeue: NO]);
-/*fprintf (stderr, "ret = %d\te = %p\n", ret, e); */
+ /* Run this for timers.el, indep of atimers; might not return.
+ TODO: use return value to avoid calling every iteration. */
+ timer_check (1);
+ [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.1]];
}
- [NSApp endModalSession: session];
- { // FIXME: BIG UGLY HACK!!!
+ { /* FIXME: BIG UGLY HACK!!! */
Lisp_Object tmp;
*(EMACS_INT*)(&tmp) = ret;
return tmp;
@end
-
/* ==========================================================================
Lisp definitions
========================================================================== */
DEFUN ("ns-reset-menu", Fns_reset_menu, Sns_reset_menu, 0, 0, 0,
- "Cause the NS menu to be re-calculated.")
+ doc: /* Cause the NS menu to be re-calculated. */)
()
{
set_frame_menubar (SELECTED_FRAME (), 1, 0);
DEFUN ("x-popup-menu", Fx_popup_menu, Sx_popup_menu, 2, 2, 0,
- "Pop up a deck-of-cards menu and return user's selection.\n\
-POSITION is a position specification. This is either a mouse button event\n\
-or a list ((XOFFSET YOFFSET) WINDOW)\n\
-where XOFFSET and YOFFSET are positions in pixels from the top left\n\
-corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
-This controls the position of the center of the first line\n\
-in the first pane of the menu, not the top left of the menu as a whole.\n\
-\n\
-MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
-The menu items come from key bindings that have a menu string as well as\n\
-a definition; actually, the \"definition\" in such a key binding looks like\n\
-\(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
-the keymap as a top-level element.\n\n\
-You can also use a list of keymaps as MENU.\n\
- Then each keymap makes a separate pane.\n\
-When MENU is a keymap or a list of keymaps, the return value\n\
-is a list of events.\n\n\
-Alternatively, you can specify a menu of multiple panes\n\
- with a list of the form (TITLE PANE1 PANE2...),\n\
-where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
-Each ITEM is normally a cons cell (STRING . VALUE);\n\
-but a string can appear as an item--that makes a nonselectable line\n\
-in the menu.\n\
-With this form of menu, the return value is VALUE from the chosen item.")
+ doc: /* Pop up a deck-of-cards menu and return user's selection.
+POSITION is a position specification. This is either a mouse button event
+or a list ((XOFFSET YOFFSET) WINDOW)
+where XOFFSET and YOFFSET are positions in pixels from the top left
+corner of WINDOW. (WINDOW may be a window or a frame object.)
+This controls the position of the top left of the menu as a whole.
+If POSITION is t, it means to use the current mouse position.
+
+MENU is a specifier for a menu. For the simplest case, MENU is a keymap.
+The menu items come from key bindings that have a menu string as well as
+a definition; actually, the \"definition\" in such a key binding looks like
+\(STRING . REAL-DEFINITION). To give the menu a title, put a string into
+the keymap as a top-level element.
+
+If REAL-DEFINITION is nil, that puts a nonselectable string in the menu.
+Otherwise, REAL-DEFINITION should be a valid key binding definition.
+
+You can also use a list of keymaps as MENU.
+ Then each keymap makes a separate pane.
+
+When MENU is a keymap or a list of keymaps, the return value is the
+list of events corresponding to the user's choice. Note that
+`x-popup-menu' does not actually execute the command bound to that
+sequence of events.
+
+Alternatively, you can specify a menu of multiple panes
+ with a list of the form (TITLE PANE1 PANE2...),
+where each pane is a list of form (TITLE ITEM1 ITEM2...).
+Each ITEM is normally a cons cell (STRING . VALUE);
+but a string can appear as an item--that makes a nonselectable line
+in the menu.
+With this form of menu, the return value is VALUE from the chosen item. */)
(position, menu)
Lisp_Object position, menu;
{
return ns_popup_dialog (position, contents, header);
}
+DEFUN ("menu-or-popup-active-p", Fmenu_or_popup_active_p, Smenu_or_popup_active_p, 0, 0, 0,
+ doc: /* Return t if a menu or popup dialog is active. */)
+ ()
+{
+ return popup_activated () ? Qt : Qnil;
+}
/* ==========================================================================
defsubr (&Sx_popup_menu);
defsubr (&Sx_popup_dialog);
defsubr (&Sns_reset_menu);
+ defsubr (&Smenu_or_popup_active_p);
staticpro (&menu_items);
menu_items = Qnil;