1 /* Functions for the NeXT/Open/GNUstep and MacOSX window system.
3 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2013 Free Software
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
29 /* This should be the first include, as it may set up #defines affecting
30 interpretation of even the system includes. */
34 #include <c-strcase.h>
37 #include "blockinput.h"
40 #include "character.h"
43 #include "termhooks.h"
48 #include <IOKit/graphics/IOGraphicsLib.h>
52 int fns_trace_num = 1;
53 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
54 __FILE__, __LINE__, ++fns_trace_num)
61 extern NSArray *ns_send_types, *ns_return_types, *ns_drag_types;
63 extern Lisp_Object Qforeground_color;
64 extern Lisp_Object Qbackground_color;
65 extern Lisp_Object Qcursor_color;
66 extern Lisp_Object Qinternal_border_width;
67 extern Lisp_Object Qvisibility;
68 extern Lisp_Object Qcursor_type;
69 extern Lisp_Object Qicon_type;
70 extern Lisp_Object Qicon_name;
71 extern Lisp_Object Qicon_left;
72 extern Lisp_Object Qicon_top;
73 extern Lisp_Object Qleft;
74 extern Lisp_Object Qright;
75 extern Lisp_Object Qtop;
76 extern Lisp_Object Qdisplay;
77 extern Lisp_Object Qvertical_scroll_bars;
78 extern Lisp_Object Qauto_raise;
79 extern Lisp_Object Qauto_lower;
80 extern Lisp_Object Qbox;
81 extern Lisp_Object Qscroll_bar_width;
82 extern Lisp_Object Qx_resource_name;
83 extern Lisp_Object Qface_set_after_frame_default;
84 extern Lisp_Object Qunderline, Qundefined;
85 extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
86 extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
89 Lisp_Object Qbuffered;
90 Lisp_Object Qfontsize;
92 EmacsTooltip *ns_tooltip = nil;
94 /* Need forward declaration here to preserve organizational integrity of file */
95 Lisp_Object Fx_open_connection (Lisp_Object, Lisp_Object, Lisp_Object);
97 /* Static variables to handle applescript execution. */
98 static Lisp_Object as_script, *as_result;
102 static ptrdiff_t image_cache_refcount;
106 /* ==========================================================================
108 Internal utility functions
110 ========================================================================== */
112 /* Let the user specify a Nextstep display with a Lisp object.
113 OBJECT may be nil, a frame or a terminal object.
114 nil stands for the selected frame--or, if that is not a Nextstep frame,
115 the first Nextstep display on the list. */
117 static struct ns_display_info *
118 check_ns_display_info (Lisp_Object object)
120 struct ns_display_info *dpyinfo = NULL;
124 struct frame *sf = XFRAME (selected_frame);
126 if (FRAME_NS_P (sf) && FRAME_LIVE_P (sf))
127 dpyinfo = FRAME_NS_DISPLAY_INFO (sf);
128 else if (x_display_list != 0)
129 dpyinfo = x_display_list;
131 error ("Nextstep windows are not in use or not initialized");
133 else if (TERMINALP (object))
135 struct terminal *t = get_terminal (object, 1);
137 if (t->type != output_ns)
138 error ("Terminal %d is not a Nextstep display", t->id);
140 dpyinfo = t->display_info.ns;
142 else if (STRINGP (object))
143 dpyinfo = ns_display_info_for_name (object);
146 struct frame *f = decode_window_system_frame (object);
147 dpyinfo = FRAME_NS_DISPLAY_INFO (f);
155 ns_get_window (Lisp_Object maybeFrame)
157 id view =nil, window =nil;
159 if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
160 maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */
162 if (!NILP (maybeFrame))
163 view = FRAME_NS_VIEW (XFRAME (maybeFrame));
164 if (view) window =[view window];
170 /* Return the X display structure for the display named NAME.
171 Open a new connection if necessary. */
172 struct ns_display_info *
173 ns_display_info_for_name (Lisp_Object name)
176 struct ns_display_info *dpyinfo;
180 for (dpyinfo = x_display_list, names = ns_display_name_list;
182 dpyinfo = dpyinfo->next, names = XCDR (names))
185 tem = Fstring_equal (XCAR (XCAR (names)), name);
190 error ("Emacs for OpenStep does not yet support multi-display.");
192 Fx_open_connection (name, Qnil, Qnil);
193 dpyinfo = x_display_list;
196 error ("OpenStep on %s not responding.\n", SDATA (name));
202 ns_filename_from_panel (NSSavePanel *panel)
204 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
205 NSURL *url = [panel URL];
206 NSString *str = [url path];
209 return [panel filename];
214 ns_directory_from_panel (NSSavePanel *panel)
216 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
217 NSURL *url = [panel directoryURL];
218 NSString *str = [url path];
221 return [panel directory];
226 interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
227 /* --------------------------------------------------------------------------
228 Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
229 -------------------------------------------------------------------------- */
239 count = [menu numberOfItems];
240 for (i = 0; i<count; i++)
242 item = [menu itemAtIndex: i];
243 name = [[item title] UTF8String];
246 nameStr = build_string (name);
248 if ([item hasSubmenu])
250 old = interpret_services_menu ([item submenu],
251 Fcons (nameStr, prefix), old);
255 keys = [item keyEquivalent];
256 if (keys && [keys length] )
258 key = [keys characterAtIndex: 0];
259 res = make_number (key|super_modifier);
265 old = Fcons (Fcons (res,
266 Freverse (Fcons (nameStr,
276 /* ==========================================================================
278 Frame parameter setters
280 ========================================================================== */
284 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
287 EmacsCGFloat r, g, b, alpha;
289 if (ns_lisp_to_color (arg, &col))
291 store_frame_param (f, Qforeground_color, oldval);
292 error ("Unknown color");
296 [f->output_data.ns->foreground_color release];
297 f->output_data.ns->foreground_color = col;
299 [col getRed: &r green: &g blue: &b alpha: &alpha];
300 FRAME_FOREGROUND_PIXEL (f) =
301 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
303 if (FRAME_NS_VIEW (f))
305 update_face_from_frame_parameter (f, Qforeground_color, arg);
306 /*recompute_basic_faces (f); */
307 if (FRAME_VISIBLE_P (f))
314 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
318 NSView *view = FRAME_NS_VIEW (f);
319 EmacsCGFloat r, g, b, alpha;
321 if (ns_lisp_to_color (arg, &col))
323 store_frame_param (f, Qbackground_color, oldval);
324 error ("Unknown color");
327 /* clear the frame; in some instances the NS-internal GC appears not to
328 update, or it does update and cannot clear old text properly */
329 if (FRAME_VISIBLE_P (f))
333 [f->output_data.ns->background_color release];
334 f->output_data.ns->background_color = col;
336 [col getRed: &r green: &g blue: &b alpha: &alpha];
337 FRAME_BACKGROUND_PIXEL (f) =
338 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
342 [[view window] setBackgroundColor: col];
344 if (alpha != (EmacsCGFloat) 1.0)
345 [[view window] setOpaque: NO];
347 [[view window] setOpaque: YES];
349 face = FRAME_DEFAULT_FACE (f);
352 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
353 face->background = ns_index_color
354 ([col colorWithAlphaComponent: alpha], f);
356 update_face_from_frame_parameter (f, Qbackground_color, arg);
359 if (FRAME_VISIBLE_P (f))
366 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
370 if (ns_lisp_to_color (arg, &col))
372 store_frame_param (f, Qcursor_color, oldval);
373 error ("Unknown color");
376 [FRAME_CURSOR_COLOR (f) release];
377 FRAME_CURSOR_COLOR (f) = [col retain];
379 if (FRAME_VISIBLE_P (f))
381 x_update_cursor (f, 0);
382 x_update_cursor (f, 1);
384 update_face_from_frame_parameter (f, Qcursor_color, arg);
389 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
391 NSView *view = FRAME_NS_VIEW (f);
392 NSTRACE (x_set_icon_name);
394 /* see if it's changed */
397 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
400 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
403 fset_icon_name (f, arg);
407 if (!NILP (f->title))
410 /* explicit name and no icon-name -> explicit_name */
411 if (f->explicit_name)
415 /* no explicit name and no icon-name ->
416 name has to be rebuild from icon_title_format */
417 windows_or_buffers_changed++;
422 /* Don't change the name if it's already NAME. */
423 if ([[view window] miniwindowTitle] &&
424 ([[[view window] miniwindowTitle]
425 isEqualToString: [NSString stringWithUTF8String:
429 [[view window] setMiniwindowTitle:
430 [NSString stringWithUTF8String: SSDATA (arg)]];
434 ns_set_name_internal (struct frame *f, Lisp_Object name)
437 Lisp_Object encoded_name, encoded_icon_name;
439 NSView *view = FRAME_NS_VIEW (f);
442 encoded_name = ENCODE_UTF_8 (name);
445 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
447 /* Don't change the name if it's already NAME. */
448 if (! [[[view window] title] isEqualToString: str])
449 [[view window] setTitle: str];
451 if (!STRINGP (f->icon_name))
452 encoded_icon_name = encoded_name;
454 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
456 str = [NSString stringWithUTF8String: SSDATA (encoded_icon_name)];
458 if ([[view window] miniwindowTitle] &&
459 ! [[[view window] miniwindowTitle] isEqualToString: str])
460 [[view window] setMiniwindowTitle: str];
465 ns_set_name (struct frame *f, Lisp_Object name, int explicit)
467 NSTRACE (ns_set_name);
469 /* Make sure that requests from lisp code override requests from
470 Emacs redisplay code. */
473 /* If we're switching from explicit to implicit, we had better
474 update the mode lines and thereby update the title. */
475 if (f->explicit_name && NILP (name))
476 update_mode_lines = 1;
478 f->explicit_name = ! NILP (name);
480 else if (f->explicit_name)
484 name = build_string([ns_app_name UTF8String]);
488 /* Don't change the name if it's already NAME. */
489 if (! NILP (Fstring_equal (name, f->name)))
494 /* title overrides explicit name */
495 if (! NILP (f->title))
498 ns_set_name_internal (f, name);
502 /* This function should be called when the user's lisp code has
503 specified a name for the frame; the name will override any set by the
506 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
508 NSTRACE (x_explicitly_set_name);
509 ns_set_name (f, arg, 1);
513 /* This function should be called by Emacs redisplay code to set the
514 name; names set this way will never override names set by the user's
517 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
519 NSTRACE (x_implicitly_set_name);
521 /* Deal with NS specific format t. */
522 if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (Vicon_title_format, Qt))
523 || EQ (Vframe_title_format, Qt)))
524 ns_set_name_as_filename (f);
526 ns_set_name (f, arg, 0);
530 /* Change the title of frame F to NAME.
531 If NAME is nil, use the frame name as the title. */
534 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
536 NSTRACE (x_set_title);
537 /* Don't change the title if it's already NAME. */
538 if (EQ (name, f->title))
541 update_mode_lines = 1;
543 fset_title (f, name);
550 ns_set_name_internal (f, name);
555 ns_set_name_as_filename (struct frame *f)
558 Lisp_Object name, filename;
559 Lisp_Object buf = XWINDOW (f->selected_window)->contents;
561 NSAutoreleasePool *pool;
563 Lisp_Object encoded_name, encoded_filename;
565 NSTRACE (ns_set_name_as_filename);
567 if (f->explicit_name || ! NILP (f->title))
571 pool = [[NSAutoreleasePool alloc] init];
572 filename = BVAR (XBUFFER (buf), filename);
573 name = BVAR (XBUFFER (buf), name);
577 if (! NILP (filename))
578 name = Ffile_name_nondirectory (filename);
580 name = build_string ([ns_app_name UTF8String]);
584 encoded_name = ENCODE_UTF_8 (name);
587 view = FRAME_NS_VIEW (f);
589 title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
590 : [[[view window] title] UTF8String];
592 if (title && (! strcmp (title, SSDATA (encoded_name))))
599 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
600 if (str == nil) str = @"Bad coding";
602 if (FRAME_ICONIFIED_P (f))
603 [[view window] setMiniwindowTitle: str];
608 if (! NILP (filename))
611 encoded_filename = ENCODE_UTF_8 (filename);
614 fstr = [NSString stringWithUTF8String: SSDATA (encoded_filename)];
615 if (fstr == nil) fstr = @"";
617 /* work around a bug observed on 10.3 and later where
618 setTitleWithRepresentedFilename does not clear out previous state
619 if given filename does not exist */
620 if (! [[NSFileManager defaultManager] fileExistsAtPath: fstr])
621 [[view window] setRepresentedFilename: @""];
627 [[view window] setRepresentedFilename: fstr];
628 [[view window] setTitle: str];
638 ns_set_doc_edited (struct frame *f, Lisp_Object arg)
640 NSView *view = FRAME_NS_VIEW (f);
641 NSAutoreleasePool *pool;
642 if (!MINI_WINDOW_P (XWINDOW (f->selected_window)))
645 pool = [[NSAutoreleasePool alloc] init];
646 [[view window] setDocumentEdited: !NILP (arg)];
654 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
657 if (FRAME_MINIBUF_ONLY_P (f))
660 if (TYPE_RANGED_INTEGERP (int, value))
661 nlines = XINT (value);
665 FRAME_MENU_BAR_LINES (f) = 0;
668 FRAME_EXTERNAL_MENU_BAR (f) = 1;
669 /* does for all frames, whereas we just want for one frame
670 [NSMenu setMenuBarVisible: YES]; */
674 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
675 free_frame_menubar (f);
676 /* [NSMenu setMenuBarVisible: NO]; */
677 FRAME_EXTERNAL_MENU_BAR (f) = 0;
682 /* toolbar support */
684 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
688 if (FRAME_MINIBUF_ONLY_P (f))
691 if (RANGED_INTEGERP (0, value, INT_MAX))
692 nlines = XFASTINT (value);
698 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
699 update_frame_tool_bar (f);
703 if (FRAME_EXTERNAL_TOOL_BAR (f))
705 free_frame_tool_bar (f);
706 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
710 x_set_window_size (f, 0, f->text_cols, f->text_lines);
715 ns_implicitly_set_icon_type (struct frame *f)
718 EmacsView *view = FRAME_NS_VIEW (f);
720 Lisp_Object chain, elt;
721 NSAutoreleasePool *pool;
724 NSTRACE (ns_implicitly_set_icon_type);
727 pool = [[NSAutoreleasePool alloc] init];
728 if (f->output_data.ns->miniimage
729 && [[NSString stringWithUTF8String: SSDATA (f->name)]
730 isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
737 tem = assq_no_quit (Qicon_type, f->param_alist);
738 if (CONSP (tem) && ! NILP (XCDR (tem)))
745 for (chain = Vns_icon_type_alist;
746 image == nil && CONSP (chain);
747 chain = XCDR (chain))
750 /* special case: 't' means go by file type */
751 if (SYMBOLP (elt) && EQ (elt, Qt) && SSDATA (f->name)[0] == '/')
754 = [NSString stringWithUTF8String: SSDATA (f->name)];
755 if ([[NSFileManager defaultManager] fileExistsAtPath: str])
756 image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
758 else if (CONSP (elt) &&
759 STRINGP (XCAR (elt)) &&
760 STRINGP (XCDR (elt)) &&
761 fast_string_match (XCAR (elt), f->name) >= 0)
763 image = [EmacsImage allocInitFromFile: XCDR (elt)];
765 image = [[NSImage imageNamed:
766 [NSString stringWithUTF8String:
767 SSDATA (XCDR (elt))]] retain];
773 image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
777 [f->output_data.ns->miniimage release];
778 f->output_data.ns->miniimage = image;
779 [view setMiniwindowImage: setMini];
786 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
788 EmacsView *view = FRAME_NS_VIEW (f);
792 NSTRACE (x_set_icon_type);
794 if (!NILP (arg) && SYMBOLP (arg))
796 arg =build_string (SSDATA (SYMBOL_NAME (arg)));
797 store_frame_param (f, Qicon_type, arg);
800 /* do it the implicit way */
803 ns_implicitly_set_icon_type (f);
809 image = [EmacsImage allocInitFromFile: arg];
811 image =[NSImage imageNamed: [NSString stringWithUTF8String:
816 image = [NSImage imageNamed: @"text"];
820 f->output_data.ns->miniimage = image;
821 [view setMiniwindowImage: setMini];
825 /* TODO: move to nsterm? */
827 ns_lisp_to_cursor_type (Lisp_Object arg)
830 if (XTYPE (arg) == Lisp_String)
832 else if (XTYPE (arg) == Lisp_Symbol)
833 str = SSDATA (SYMBOL_NAME (arg));
835 if (!strcmp (str, "box")) return FILLED_BOX_CURSOR;
836 if (!strcmp (str, "hollow")) return HOLLOW_BOX_CURSOR;
837 if (!strcmp (str, "hbar")) return HBAR_CURSOR;
838 if (!strcmp (str, "bar")) return BAR_CURSOR;
839 if (!strcmp (str, "no")) return NO_CURSOR;
845 ns_cursor_type_to_lisp (int arg)
849 case FILLED_BOX_CURSOR: return Qbox;
850 case HOLLOW_BOX_CURSOR: return intern ("hollow");
851 case HBAR_CURSOR: return intern ("hbar");
852 case BAR_CURSOR: return intern ("bar");
854 default: return intern ("no");
858 /* This is the same as the xfns.c definition. */
860 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
862 set_frame_cursor_types (f, arg);
864 /* Make sure the cursor gets redrawn. */
865 cursor_type_changed = 1;
869 /* called to set mouse pointer color, but all other terms use it to
870 initialize pointer types (and don't set the color ;) */
872 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
874 /* don't think we can do this on Nextstep */
879 #define Xstr(x) Str(x)
882 ns_appkit_version_str (void)
886 #ifdef NS_IMPL_GNUSTEP
887 sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
888 #elif defined (NS_IMPL_COCOA)
889 sprintf(tmp, "apple-appkit-%.2f", NSAppKitVersionNumber);
893 return build_string (tmp);
897 /* This is for use by x-server-version and collapses all version info we
898 have into a single int. For a better picture of the implementation
899 running, use ns_appkit_version_str.*/
901 ns_appkit_version_int (void)
903 #ifdef NS_IMPL_GNUSTEP
904 return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
905 #elif defined (NS_IMPL_COCOA)
906 return (int)NSAppKitVersionNumber;
913 x_icon (struct frame *f, Lisp_Object parms)
914 /* --------------------------------------------------------------------------
915 Strangely-named function to set icon position parameters in frame.
916 This is irrelevant under OS X, but might be needed under GNUstep,
917 depending on the window manager used. Note, this is not a standard
918 frame parameter-setter; it is called directly from x-create-frame.
919 -------------------------------------------------------------------------- */
921 Lisp_Object icon_x, icon_y;
922 struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);
924 f->output_data.ns->icon_top = Qnil;
925 f->output_data.ns->icon_left = Qnil;
927 /* Set the position of the icon. */
928 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
929 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
930 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
932 CHECK_NUMBER (icon_x);
933 CHECK_NUMBER (icon_y);
934 f->output_data.ns->icon_top = icon_y;
935 f->output_data.ns->icon_left = icon_x;
937 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
938 error ("Both left and top icon corners of icon must be specified");
942 /* Note: see frame.c for template, also where generic functions are impl */
943 frame_parm_handler ns_frame_parm_handlers[] =
945 x_set_autoraise, /* generic OK */
946 x_set_autolower, /* generic OK */
947 x_set_background_color,
948 0, /* x_set_border_color, may be impossible under Nextstep */
949 0, /* x_set_border_width, may be impossible under Nextstep */
952 x_set_font, /* generic OK */
953 x_set_foreground_color,
956 x_set_internal_border_width, /* generic OK */
957 x_set_menu_bar_lines,
959 x_explicitly_set_name,
960 x_set_scroll_bar_width, /* generic OK */
962 x_set_unsplittable, /* generic OK */
963 x_set_vertical_scroll_bars, /* generic OK */
964 x_set_visibility, /* generic OK */
965 x_set_tool_bar_lines,
966 0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
967 0, /* x_set_scroll_bar_background, will ignore (not possible on NS) */
968 x_set_screen_gamma, /* generic OK */
969 x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
970 x_set_fringe_width, /* generic OK */
971 x_set_fringe_width, /* generic OK */
972 0, /* x_set_wait_for_wm, will ignore */
973 x_set_fullscreen, /* generic OK */
974 x_set_font_backend, /* generic OK */
976 0, /* x_set_sticky */
977 0, /* x_set_tool_bar_position */
981 /* Handler for signals raised during x_create_frame.
982 FRAME is the frame which is partially constructed. */
985 unwind_create_frame (Lisp_Object frame)
987 struct frame *f = XFRAME (frame);
989 /* If frame is already dead, nothing to do. This can happen if the
990 display is disconnected after the frame has become official, but
991 before x_create_frame removes the unwind protect. */
992 if (!FRAME_LIVE_P (f))
995 /* If frame is ``official'', nothing to do. */
996 if (NILP (Fmemq (frame, Vframe_list)))
998 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
999 struct ns_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1002 x_free_frame_resources (f);
1006 /* Check that reference counts are indeed correct. */
1007 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
1013 * Read geometry related parameters from preferences if not in PARMS.
1014 * Returns the union of parms and any preferences read.
1018 get_geometry_from_preferences (struct ns_display_info *dpyinfo,
1026 { "width", "Width", Qwidth },
1027 { "height", "Height", Qheight },
1028 { "left", "Left", Qleft },
1029 { "top", "Top", Qtop },
1033 for (i = 0; i < sizeof (r)/sizeof (r[0]); ++i)
1035 if (NILP (Fassq (r[i].tem, parms)))
1038 = x_get_arg (dpyinfo, parms, r[i].tem, r[i].val, r[i].cls,
1040 if (! EQ (value, Qunbound))
1041 parms = Fcons (Fcons (r[i].tem, value), parms);
1048 /* ==========================================================================
1052 ========================================================================== */
1054 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
1056 doc: /* Make a new Nextstep window, called a "frame" in Emacs terms.
1057 Return an Emacs frame object.
1058 PARMS is an alist of frame parameters.
1059 If the parameters specify that the frame should not have a minibuffer,
1060 and do not specify a specific minibuffer window to use,
1061 then `default-minibuffer-frame' must be a frame whose minibuffer can
1062 be shared by the new frame.
1064 This function is an internal primitive--use `make-frame' instead. */)
1068 Lisp_Object frame, tem;
1070 int minibuffer_only = 0;
1071 long window_prompting = 0;
1073 ptrdiff_t count = specpdl_ptr - specpdl;
1074 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1075 Lisp_Object display;
1076 struct ns_display_info *dpyinfo = NULL;
1079 static int desc_ctr = 1;
1081 /* x_get_arg modifies parms. */
1082 parms = Fcopy_alist (parms);
1084 /* Use this general default value to start with
1085 until we know if this frame has a specified name. */
1086 Vx_resource_name = Vinvocation_name;
1088 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
1089 if (EQ (display, Qunbound))
1091 dpyinfo = check_ns_display_info (display);
1092 kb = dpyinfo->terminal->kboard;
1094 if (!dpyinfo->terminal->name)
1095 error ("Terminal is not live, can't create new frames on it");
1097 name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
1099 && ! EQ (name, Qunbound)
1101 error ("Invalid frame name--not a string or nil");
1104 Vx_resource_name = name;
1106 parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
1107 if (EQ (parent, Qunbound))
1109 if (! NILP (parent))
1110 CHECK_NUMBER (parent);
1112 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
1113 /* No need to protect DISPLAY because that's not used after passing
1114 it to make_frame_without_minibuffer. */
1116 GCPRO4 (parms, parent, name, frame);
1117 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
1119 if (EQ (tem, Qnone) || NILP (tem))
1120 f = make_frame_without_minibuffer (Qnil, kb, display);
1121 else if (EQ (tem, Qonly))
1123 f = make_minibuffer_frame ();
1124 minibuffer_only = 1;
1126 else if (WINDOWP (tem))
1127 f = make_frame_without_minibuffer (tem, kb, display);
1131 XSETFRAME (frame, f);
1133 f->terminal = dpyinfo->terminal;
1135 f->output_method = output_ns;
1136 f->output_data.ns = xzalloc (sizeof *f->output_data.ns);
1138 FRAME_FONTSET (f) = -1;
1140 fset_icon_name (f, x_get_arg (dpyinfo, parms, Qicon_name,
1141 "iconName", "Title",
1143 if (! STRINGP (f->icon_name))
1144 fset_icon_name (f, Qnil);
1146 FRAME_NS_DISPLAY_INFO (f) = dpyinfo;
1148 /* With FRAME_NS_DISPLAY_INFO set up, this unwind-protect is safe. */
1149 record_unwind_protect (unwind_create_frame, frame);
1151 f->output_data.ns->window_desc = desc_ctr++;
1152 if (TYPE_RANGED_INTEGERP (Window, parent))
1154 f->output_data.ns->parent_desc = XFASTINT (parent);
1155 f->output_data.ns->explicit_parent = 1;
1159 f->output_data.ns->parent_desc = FRAME_NS_DISPLAY_INFO (f)->root_window;
1160 f->output_data.ns->explicit_parent = 0;
1163 /* Set the name; the functions to which we pass f expect the name to
1165 if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
1167 fset_name (f, build_string ([ns_app_name UTF8String]));
1168 f->explicit_name = 0;
1172 fset_name (f, name);
1173 f->explicit_name = 1;
1174 specbind (Qx_resource_name, name);
1178 register_font_driver (&nsfont_driver, f);
1179 x_default_parameter (f, parms, Qfont_backend, Qnil,
1180 "fontBackend", "FontBackend", RES_TYPE_STRING);
1183 /* use for default font name */
1184 id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
1185 x_default_parameter (f, parms, Qfontsize,
1186 make_number (0 /*(int)[font pointSize]*/),
1187 "fontSize", "FontSize", RES_TYPE_NUMBER);
1188 x_default_parameter (f, parms, Qfont,
1189 build_string ([[font fontName] UTF8String]),
1190 "font", "Font", RES_TYPE_STRING);
1194 x_default_parameter (f, parms, Qborder_width, make_number (0),
1195 "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
1196 x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
1197 "internalBorderWidth", "InternalBorderWidth",
1200 /* default scrollbars on right on Mac */
1203 #ifdef NS_IMPL_GNUSTEP
1208 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1209 "verticalScrollBars", "VerticalScrollBars",
1212 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1213 "foreground", "Foreground", RES_TYPE_STRING);
1214 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1215 "background", "Background", RES_TYPE_STRING);
1216 /* FIXME: not supported yet in Nextstep */
1217 x_default_parameter (f, parms, Qline_spacing, Qnil,
1218 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1219 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1220 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1221 x_default_parameter (f, parms, Qright_fringe, Qnil,
1222 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1225 image_cache_refcount =
1226 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
1229 init_frame_faces (f);
1231 /* The resources controlling the menu-bar and tool-bar are
1232 processed specially at startup, and reflected in the mode
1233 variables; ignore them here. */
1234 x_default_parameter (f, parms, Qmenu_bar_lines,
1235 NILP (Vmenu_bar_mode)
1236 ? make_number (0) : make_number (1),
1237 NULL, NULL, RES_TYPE_NUMBER);
1238 x_default_parameter (f, parms, Qtool_bar_lines,
1239 NILP (Vtool_bar_mode)
1240 ? make_number (0) : make_number (1),
1241 NULL, NULL, RES_TYPE_NUMBER);
1243 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1244 "BufferPredicate", RES_TYPE_SYMBOL);
1245 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1248 parms = get_geometry_from_preferences (dpyinfo, parms);
1249 window_prompting = x_figure_window_size (f, parms, 1);
1251 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1252 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1254 /* NOTE: on other terms, this is done in set_mouse_color, however this
1255 was not getting called under Nextstep */
1256 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1257 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1258 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1259 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1260 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1261 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1262 FRAME_NS_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1263 = [NSCursor arrowCursor];
1264 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1266 [[EmacsView alloc] initFrameFromEmacs: f];
1270 /* ns_display_info does not have a reference_count. */
1271 f->terminal->reference_count++;
1273 /* It is now ok to make the frame official even if we get an error below.
1274 The frame needs to be on Vframe_list or making it visible won't work. */
1275 Vframe_list = Fcons (frame, Vframe_list);
1277 x_default_parameter (f, parms, Qicon_type, Qnil,
1278 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1280 x_default_parameter (f, parms, Qauto_raise, Qnil,
1281 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1282 x_default_parameter (f, parms, Qauto_lower, Qnil,
1283 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1284 x_default_parameter (f, parms, Qcursor_type, Qbox,
1285 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1286 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1287 "scrollBarWidth", "ScrollBarWidth",
1289 x_default_parameter (f, parms, Qalpha, Qnil,
1290 "alpha", "Alpha", RES_TYPE_NUMBER);
1291 x_default_parameter (f, parms, Qfullscreen, Qnil,
1292 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1294 width = FRAME_COLS (f);
1295 height = FRAME_LINES (f);
1297 SET_FRAME_COLS (f, 0);
1298 FRAME_LINES (f) = 0;
1299 change_frame_size (f, height, width, 1, 0, 0);
1301 if (! f->output_data.ns->explicit_parent)
1303 Lisp_Object visibility;
1305 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1307 if (EQ (visibility, Qunbound))
1310 if (EQ (visibility, Qicon))
1311 x_iconify_frame (f);
1312 else if (! NILP (visibility))
1314 x_make_frame_visible (f);
1315 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1319 /* Must have been Qnil. */
1323 if (FRAME_HAS_MINIBUF_P (f)
1324 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1325 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1326 kset_default_minibuffer_frame (kb, frame);
1328 /* All remaining specified parameters, which have not been "used"
1329 by x_get_arg and friends, now go in the misc. alist of the frame. */
1330 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1331 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1332 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1336 if (window_prompting & USPosition)
1337 x_set_offset (f, f->left_pos, f->top_pos, 1);
1339 /* Make sure windows on this frame appear in calls to next-window
1340 and similar functions. */
1341 Vwindow_list = Qnil;
1343 return unbind_to (count, frame);
1347 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
1348 doc: /* Set the input focus to FRAME.
1349 FRAME nil means use the selected frame. */)
1352 struct frame *f = decode_window_system_frame (frame);
1353 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1355 if (dpyinfo->x_focus_frame != f)
1357 EmacsView *view = FRAME_NS_VIEW (f);
1359 [NSApp activateIgnoringOtherApps: YES];
1360 [[view window] makeKeyAndOrderFront: view];
1368 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1370 doc: /* Pop up the font panel. */)
1373 struct frame *f = decode_window_system_frame (frame);
1374 id fm = [NSFontManager sharedFontManager];
1376 [fm setSelectedFont: ((struct nsfont_info *)f->output_data.ns->font)->nsfont
1378 [fm orderFrontFontPanel: NSApp];
1383 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1385 doc: /* Pop up the color panel. */)
1388 check_window_system (NULL);
1389 [NSApp orderFrontColorPanel: NSApp];
1397 #if ! defined (NS_IMPL_COCOA) || \
1398 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
1399 NSString *dirS, *initS;
1405 ns_run_file_dialog (void)
1407 if (ns_fd_data.panel == nil) return;
1408 #if defined (NS_IMPL_COCOA) && \
1409 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1410 ns_fd_data.ret = [ns_fd_data.panel runModal];
1412 if (ns_fd_data.no_types)
1414 ns_fd_data.ret = [ns_fd_data.panel
1415 runModalForDirectory: ns_fd_data.dirS
1416 file: ns_fd_data.initS];
1420 ns_fd_data.ret = [ns_fd_data.panel
1421 runModalForDirectory: ns_fd_data.dirS
1422 file: ns_fd_data.initS
1426 ns_fd_data.panel = nil;
1429 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1430 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1431 Optional arg DIR, if non-nil, supplies a default directory.
1432 Optional arg MUSTMATCH, if non-nil, means the returned file or
1433 directory must exist.
1434 Optional arg INIT, if non-nil, provides a default file name to use.
1435 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1436 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1437 Lisp_Object init, Lisp_Object dir_only_p)
1439 static id fileDelegate = nil;
1441 BOOL isSave = NILP (mustmatch) && NILP (dir_only_p);
1445 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1446 [NSString stringWithUTF8String: SSDATA (prompt)];
1447 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1448 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1449 [NSString stringWithUTF8String: SSDATA (dir)];
1450 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1451 [NSString stringWithUTF8String: SSDATA (init)];
1454 check_window_system (NULL);
1456 if (fileDelegate == nil)
1457 fileDelegate = [EmacsFileDelegate new];
1459 [NSCursor setHiddenUntilMouseMoves: NO];
1461 if ([dirS characterAtIndex: 0] == '~')
1462 dirS = [dirS stringByExpandingTildeInPath];
1465 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1467 [panel setTitle: promptS];
1469 [panel setAllowsOtherFileTypes: YES];
1470 [panel setTreatsFilePackagesAsDirectories: YES];
1471 [panel setDelegate: fileDelegate];
1473 if (! NILP (dir_only_p))
1475 [panel setCanChooseDirectories: YES];
1476 [panel setCanChooseFiles: NO];
1480 /* This is not quite what the documentation says, but it is compatible
1481 with the Gtk+ code. Also, the menu entry says "Open File...". */
1482 [panel setCanChooseDirectories: NO];
1483 [panel setCanChooseFiles: YES];
1487 ns_fd_data.panel = panel;
1488 ns_fd_data.ret = NO;
1489 #if defined (NS_IMPL_COCOA) && \
1490 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1491 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1492 [panel setAllowedFileTypes: nil];
1493 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1494 if (initS && NILP (Ffile_directory_p (init)))
1495 [panel setNameFieldStringValue: [initS lastPathComponent]];
1497 [panel setNameFieldStringValue: @""];
1500 ns_fd_data.no_types = NILP (mustmatch) && NILP (dir_only_p);
1501 ns_fd_data.dirS = dirS;
1502 ns_fd_data.initS = initS;
1505 /* runModalForDirectory/runModal restarts the main event loop when done,
1506 so we must start an event loop and then pop up the file dialog.
1507 The file dialog may pop up a confirm dialog after Ok has been pressed,
1508 so we can not simply pop down on the Ok/Cancel press.
1510 nxev = [NSEvent otherEventWithType: NSApplicationDefined
1511 location: NSMakePoint (0, 0)
1514 windowNumber: [[NSApp mainWindow] windowNumber]
1515 context: [NSApp context]
1518 data2: NSAPP_DATA2_RUNFILEDIALOG];
1520 [NSApp postEvent: nxev atStart: NO];
1521 while (ns_fd_data.panel != nil)
1524 ret = (ns_fd_data.ret == NSOKButton);
1528 NSString *str = ns_filename_from_panel (panel);
1529 if (! str) str = ns_directory_from_panel (panel);
1530 if (! str) ret = NO;
1531 else fname = build_string ([str UTF8String]);
1534 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1537 return ret ? fname : Qnil;
1541 ns_get_defaults_value (const char *key)
1543 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1544 objectForKey: [NSString stringWithUTF8String: key]];
1546 if (!obj) return NULL;
1548 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1552 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1553 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1554 If OWNER is nil, Emacs is assumed. */)
1555 (Lisp_Object owner, Lisp_Object name)
1559 check_window_system (NULL);
1561 owner = build_string([ns_app_name UTF8String]);
1562 CHECK_STRING (name);
1564 value = ns_get_defaults_value (SSDATA (name));
1567 return build_string (value);
1572 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1573 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1574 If OWNER is nil, Emacs is assumed.
1575 If VALUE is nil, the default is removed. */)
1576 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1578 check_window_system (NULL);
1580 owner = build_string ([ns_app_name UTF8String]);
1581 CHECK_STRING (name);
1584 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1585 [NSString stringWithUTF8String: SSDATA (name)]];
1589 CHECK_STRING (value);
1590 [[NSUserDefaults standardUserDefaults] setObject:
1591 [NSString stringWithUTF8String: SSDATA (value)]
1592 forKey: [NSString stringWithUTF8String:
1600 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1601 Sx_server_max_request_size,
1603 doc: /* This function is a no-op. It is only present for completeness. */)
1604 (Lisp_Object terminal)
1606 check_ns_display_info (terminal);
1607 /* This function has no real equivalent under NeXTstep. Return nil to
1613 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1614 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1615 \(Labeling every distributor as a "vendor" embodies the false assumption
1616 that operating systems cannot be developed and distributed noncommercially.)
1617 The optional argument TERMINAL specifies which display to ask about.
1618 TERMINAL should be a terminal object, a frame or a display name (a string).
1619 If omitted or nil, that stands for the selected frame's display. */)
1620 (Lisp_Object terminal)
1622 check_ns_display_info (terminal);
1623 #ifdef NS_IMPL_GNUSTEP
1624 return build_string ("GNU");
1626 return build_string ("Apple");
1631 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1632 doc: /* Return the version numbers of the server of display TERMINAL.
1633 The value is a list of three integers: the major and minor
1634 version numbers of the X Protocol in use, and the distributor-specific release
1635 number. See also the function `x-server-vendor'.
1637 The optional argument TERMINAL specifies which display to ask about.
1638 TERMINAL should be a terminal object, a frame or a display name (a string).
1639 If omitted or nil, that stands for the selected frame's display. */)
1640 (Lisp_Object terminal)
1642 check_ns_display_info (terminal);
1643 /*NOTE: it is unclear what would best correspond with "protocol";
1644 we return 10.3, meaning Panther, since this is roughly the
1645 level that GNUstep's APIs correspond to.
1646 The last number is where we distinguish between the Apple
1647 and GNUstep implementations ("distributor-specific release
1648 number") and give int'ized versions of major.minor. */
1649 return list3i (10, 3, ns_appkit_version_int ());
1653 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1654 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1655 The optional argument TERMINAL specifies which display to ask about.
1656 TERMINAL should be a terminal object, a frame or a display name (a string).
1657 If omitted or nil, that stands for the selected frame's display.
1659 Note: "screen" here is not in Nextstep terminology but in X11's. For
1660 the number of physical monitors, use `(length
1661 (display-monitor-attributes-list TERMINAL))' instead. */)
1662 (Lisp_Object terminal)
1664 check_ns_display_info (terminal);
1665 return make_number (1);
1669 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1670 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1671 The optional argument TERMINAL specifies which display to ask about.
1672 TERMINAL should be a terminal object, a frame or a display name (a string).
1673 If omitted or nil, that stands for the selected frame's display.
1675 On \"multi-monitor\" setups this refers to the height in millimeters for
1676 all physical monitors associated with TERMINAL. To get information
1677 for each physical monitor, use `display-monitor-attributes-list'. */)
1678 (Lisp_Object terminal)
1680 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1682 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1686 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1687 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1688 The optional argument TERMINAL specifies which display to ask about.
1689 TERMINAL should be a terminal object, a frame or a display name (a string).
1690 If omitted or nil, that stands for the selected frame's display.
1692 On \"multi-monitor\" setups this refers to the width in millimeters for
1693 all physical monitors associated with TERMINAL. To get information
1694 for each physical monitor, use `display-monitor-attributes-list'. */)
1695 (Lisp_Object terminal)
1697 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1699 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1703 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1704 Sx_display_backing_store, 0, 1, 0,
1705 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1706 The value may be `buffered', `retained', or `non-retained'.
1707 The optional argument TERMINAL specifies which display to ask about.
1708 TERMINAL should be a terminal object, a frame or a display name (a string).
1709 If omitted or nil, that stands for the selected frame's display. */)
1710 (Lisp_Object terminal)
1712 check_ns_display_info (terminal);
1713 switch ([ns_get_window (terminal) backingType])
1715 case NSBackingStoreBuffered:
1716 return intern ("buffered");
1717 case NSBackingStoreRetained:
1718 return intern ("retained");
1719 case NSBackingStoreNonretained:
1720 return intern ("non-retained");
1722 error ("Strange value for backingType parameter of frame");
1724 return Qnil; /* not reached, shut compiler up */
1728 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1729 Sx_display_visual_class, 0, 1, 0,
1730 doc: /* Return the visual class of the Nextstep display TERMINAL.
1731 The value is one of the symbols `static-gray', `gray-scale',
1732 `static-color', `pseudo-color', `true-color', or `direct-color'.
1734 The optional argument TERMINAL specifies which display to ask about.
1735 TERMINAL should a terminal object, a frame or a display name (a string).
1736 If omitted or nil, that stands for the selected frame's display. */)
1737 (Lisp_Object terminal)
1739 NSWindowDepth depth;
1741 check_ns_display_info (terminal);
1742 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1744 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1745 return intern ("static-gray");
1746 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1747 return intern ("gray-scale");
1748 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1749 return intern ("pseudo-color");
1750 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1751 return intern ("true-color");
1752 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1753 return intern ("direct-color");
1755 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1756 return intern ("direct-color");
1760 DEFUN ("x-display-save-under", Fx_display_save_under,
1761 Sx_display_save_under, 0, 1, 0,
1762 doc: /* Return t if TERMINAL supports the save-under feature.
1763 The optional argument TERMINAL specifies which display to ask about.
1764 TERMINAL should be a terminal object, a frame or a display name (a string).
1765 If omitted or nil, that stands for the selected frame's display. */)
1766 (Lisp_Object terminal)
1768 check_ns_display_info (terminal);
1769 switch ([ns_get_window (terminal) backingType])
1771 case NSBackingStoreBuffered:
1774 case NSBackingStoreRetained:
1775 case NSBackingStoreNonretained:
1779 error ("Strange value for backingType parameter of frame");
1781 return Qnil; /* not reached, shut compiler up */
1785 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1787 doc: /* Open a connection to a display server.
1788 DISPLAY is the name of the display to connect to.
1789 Optional second arg XRM-STRING is a string of resources in xrdb format.
1790 If the optional third arg MUST-SUCCEED is non-nil,
1791 terminate Emacs if we can't open the connection.
1792 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1793 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1795 struct ns_display_info *dpyinfo;
1797 CHECK_STRING (display);
1799 nxatoms_of_nsselect ();
1800 dpyinfo = ns_term_init (display);
1803 if (!NILP (must_succeed))
1804 fatal ("OpenStep on %s not responding.\n",
1807 error ("OpenStep on %s not responding.\n",
1815 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1817 doc: /* Close the connection to TERMINAL's Nextstep display server.
1818 For TERMINAL, specify a terminal object, a frame or a display name (a
1819 string). If TERMINAL is nil, that stands for the selected frame's
1821 (Lisp_Object terminal)
1823 check_ns_display_info (terminal);
1824 [NSApp terminate: NSApp];
1829 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
1830 doc: /* Return the list of display names that Emacs has connections to. */)
1833 Lisp_Object tail, result;
1836 for (tail = ns_display_name_list; CONSP (tail); tail = XCDR (tail))
1837 result = Fcons (XCAR (XCAR (tail)), result);
1843 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
1845 doc: /* Hides all applications other than Emacs. */)
1848 check_window_system (NULL);
1849 [NSApp hideOtherApplications: NSApp];
1853 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
1855 doc: /* If ON is non-nil, the entire Emacs application is hidden.
1856 Otherwise if Emacs is hidden, it is unhidden.
1857 If ON is equal to `activate', Emacs is unhidden and becomes
1858 the active application. */)
1861 check_window_system (NULL);
1862 if (EQ (on, intern ("activate")))
1864 [NSApp unhide: NSApp];
1865 [NSApp activateIgnoringOtherApps: YES];
1868 [NSApp unhide: NSApp];
1870 [NSApp hide: NSApp];
1875 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
1877 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
1880 check_window_system (NULL);
1881 [NSApp orderFrontStandardAboutPanel: nil];
1886 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
1887 doc: /* Determine font PostScript or family name for font NAME.
1888 NAME should be a string containing either the font name or an XLFD
1889 font descriptor. If string contains `fontset' and not
1890 `fontset-startup', it is left alone. */)
1894 CHECK_STRING (name);
1899 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
1902 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
1906 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
1907 doc: /* Return a list of all available colors.
1908 The optional argument FRAME is currently ignored. */)
1911 Lisp_Object list = Qnil;
1912 NSEnumerator *colorlists;
1917 CHECK_FRAME (frame);
1918 if (! FRAME_NS_P (XFRAME (frame)))
1919 error ("non-Nextstep frame used in `ns-list-colors'");
1924 colorlists = [[NSColorList availableColorLists] objectEnumerator];
1925 while ((clist = [colorlists nextObject]))
1927 if ([[clist name] length] < 7 ||
1928 [[clist name] rangeOfString: @"PANTONE"].location == 0)
1930 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
1932 while ((cname = [cnames nextObject]))
1933 list = Fcons (build_string ([cname UTF8String]), list);
1934 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
1935 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
1936 UTF8String]), list); */
1946 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
1947 doc: /* List available Nextstep services by querying NSApp. */)
1950 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1951 /* You can't get services like this in 10.6+. */
1954 Lisp_Object ret = Qnil;
1956 #ifdef NS_IMPL_COCOA
1960 check_window_system (NULL);
1961 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
1962 [NSApp setServicesMenu: svcs];
1963 [NSApp registerServicesMenuSendTypes: ns_send_types
1964 returnTypes: ns_return_types];
1966 /* On Tiger, services menu updating was made lazier (waits for user to
1967 actually click on the menu), so we have to force things along: */
1968 #ifdef NS_IMPL_COCOA
1969 delegate = [svcs delegate];
1970 if (delegate != nil)
1972 if ([delegate respondsToSelector: @selector (menuNeedsUpdate:)])
1973 [delegate menuNeedsUpdate: svcs];
1974 if ([delegate respondsToSelector:
1975 @selector (menu:updateItem:atIndex:shouldCancel:)])
1977 int i, len = [delegate numberOfItemsInMenu: svcs];
1978 for (i =0; i<len; i++)
1979 [svcs addItemWithTitle: @"" action: NULL keyEquivalent: @""];
1980 for (i =0; i<len; i++)
1981 if (![delegate menu: svcs
1982 updateItem: (NSMenuItem *)[svcs itemAtIndex: i]
1983 atIndex: i shouldCancel: NO])
1989 [svcs setAutoenablesItems: NO];
1990 #ifdef NS_IMPL_COCOA
1991 [svcs update]; /* on OS X, converts from '/' structure */
1994 ret = interpret_services_menu (svcs, Qnil, ret);
2000 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2002 doc: /* Perform Nextstep SERVICE on SEND.
2003 SEND should be either a string or nil.
2004 The return value is the result of the service, as string, or nil if
2005 there was no result. */)
2006 (Lisp_Object service, Lisp_Object send)
2012 CHECK_STRING (service);
2013 check_window_system (NULL);
2015 utfStr = SSDATA (service);
2016 svcName = [NSString stringWithUTF8String: utfStr];
2018 pb =[NSPasteboard pasteboardWithUniqueName];
2019 ns_string_to_pasteboard (pb, send);
2021 if (NSPerformService (svcName, pb) == NO)
2022 Fsignal (Qquit, list1 (build_string ("service not available")));
2024 if ([[pb types] count] == 0)
2025 return build_string ("");
2026 return ns_string_from_pasteboard (pb);
2030 DEFUN ("ns-convert-utf8-nfd-to-nfc", Fns_convert_utf8_nfd_to_nfc,
2031 Sns_convert_utf8_nfd_to_nfc, 1, 1, 0,
2032 doc: /* Return an NFC string that matches the UTF-8 NFD string STR. */)
2035 /* TODO: If GNUstep ever implements precomposedStringWithCanonicalMapping,
2040 utfStr = [NSString stringWithUTF8String: SSDATA (str)];
2041 #ifdef NS_IMPL_COCOA
2042 utfStr = [utfStr precomposedStringWithCanonicalMapping];
2044 return build_string ([utfStr UTF8String]);
2048 #ifdef NS_IMPL_COCOA
2050 /* Compile and execute the AppleScript SCRIPT and return the error
2051 status as function value. A zero is returned if compilation and
2052 execution is successful, in which case *RESULT is set to a Lisp
2053 string or a number containing the resulting script value. Otherwise,
2056 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2058 NSAppleEventDescriptor *desc;
2059 NSDictionary* errorDict;
2060 NSAppleEventDescriptor* returnDescriptor = NULL;
2062 NSAppleScript* scriptObject =
2063 [[NSAppleScript alloc] initWithSource:
2064 [NSString stringWithUTF8String: SSDATA (script)]];
2066 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2067 [scriptObject release];
2071 if (returnDescriptor != NULL)
2073 // successful execution
2074 if (kAENullEvent != [returnDescriptor descriptorType])
2077 // script returned an AppleScript result
2078 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2079 #if defined (NS_IMPL_COCOA)
2080 (typeUTF16ExternalRepresentation
2081 == [returnDescriptor descriptorType]) ||
2083 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2084 (typeCString == [returnDescriptor descriptorType]))
2086 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2088 *result = build_string([[desc stringValue] UTF8String]);
2092 /* use typeUTF16ExternalRepresentation? */
2093 // coerce the result to the appropriate ObjC type
2094 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2096 *result = make_number([desc int32Value]);
2102 // no script result, return error
2108 /* Helper function called from sendEvent to run applescript
2109 from within the main event loop. */
2112 ns_run_ascript (void)
2114 if (! NILP (as_script))
2115 as_status = ns_do_applescript (as_script, as_result);
2119 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2120 doc: /* Execute AppleScript SCRIPT and return the result.
2121 If compilation and execution are successful, the resulting script value
2122 is returned as a string, a number or, in the case of other constructs, t.
2123 In case the execution fails, an error is signaled. */)
2124 (Lisp_Object script)
2130 CHECK_STRING (script);
2131 check_window_system (NULL);
2136 as_result = &result;
2138 /* executing apple script requires the event loop to run, otherwise
2139 errors aren't returned and executeAndReturnError hangs forever.
2140 Post an event that runs applescript and then start the event loop.
2141 The event loop is exited when the script is done. */
2142 nxev = [NSEvent otherEventWithType: NSApplicationDefined
2143 location: NSMakePoint (0, 0)
2146 windowNumber: [[NSApp mainWindow] windowNumber]
2147 context: [NSApp context]
2150 data2: NSAPP_DATA2_RUNASSCRIPT];
2152 [NSApp postEvent: nxev atStart: NO];
2154 // If there are other events, the event loop may exit. Keep running
2155 // until the script has been handled. */
2156 while (! NILP (as_script))
2165 else if (!STRINGP (result))
2166 error ("AppleScript error %d", status);
2168 error ("%s", SSDATA (result));
2174 /* ==========================================================================
2176 Miscellaneous functions not called through hooks
2178 ========================================================================== */
2180 /* called from frame.c */
2181 struct ns_display_info *
2182 check_x_display_info (Lisp_Object frame)
2184 return check_ns_display_info (frame);
2189 x_set_scroll_bar_default_width (struct frame *f)
2191 int wid = FRAME_COLUMN_WIDTH (f);
2192 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2193 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2198 extern const char *x_get_string_resource (XrmDatabase, char *, char *);
2201 /* terms impl this instead of x-get-resource directly */
2203 x_get_string_resource (XrmDatabase rdb, char *name, char *class)
2205 /* remove appname prefix; TODO: allow for !="Emacs" */
2206 char *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2208 check_window_system (NULL);
2210 if (inhibit_x_resources)
2211 /* --quick was passed, so this is a no-op. */
2214 res = ns_get_defaults_value (toCheck);
2215 return !res ? NULL :
2216 (!c_strncasecmp (res, "YES", 3) ? "true" :
2217 (!c_strncasecmp (res, "NO", 2) ? "false" : res));
2222 x_get_focus_frame (struct frame *frame)
2224 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (frame);
2225 Lisp_Object nsfocus;
2227 if (!dpyinfo->x_focus_frame)
2230 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2235 x_sync (struct frame *f)
2237 /* XXX Not implemented XXX */
2243 /* ==========================================================================
2245 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2247 ========================================================================== */
2250 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2251 doc: /* Internal function called by `color-defined-p', which see.
2252 \(Note that the Nextstep version of this function ignores FRAME.) */)
2253 (Lisp_Object color, Lisp_Object frame)
2256 check_window_system (NULL);
2257 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2261 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2262 doc: /* Internal function called by `color-values', which see. */)
2263 (Lisp_Object color, Lisp_Object frame)
2266 EmacsCGFloat red, green, blue, alpha;
2268 check_window_system (NULL);
2269 CHECK_STRING (color);
2271 if (ns_lisp_to_color (color, &col))
2274 [[col colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
2275 getRed: &red green: &green blue: &blue alpha: &alpha];
2276 return list3i (lrint (red * 65280), lrint (green * 65280),
2277 lrint (blue * 65280));
2281 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2282 doc: /* Internal function called by `display-color-p', which see. */)
2283 (Lisp_Object terminal)
2285 NSWindowDepth depth;
2286 NSString *colorSpace;
2288 check_ns_display_info (terminal);
2289 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2290 colorSpace = NSColorSpaceFromDepth (depth);
2292 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2293 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2298 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2300 doc: /* Return t if the Nextstep display supports shades of gray.
2301 Note that color displays do support shades of gray.
2302 The optional argument TERMINAL specifies which display to ask about.
2303 TERMINAL should be a terminal object, a frame or a display name (a string).
2304 If omitted or nil, that stands for the selected frame's display. */)
2305 (Lisp_Object terminal)
2307 NSWindowDepth depth;
2309 check_ns_display_info (terminal);
2310 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2312 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2316 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2318 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2319 The optional argument TERMINAL specifies which display to ask about.
2320 TERMINAL should be a terminal object, a frame or a display name (a string).
2321 If omitted or nil, that stands for the selected frame's display.
2323 On \"multi-monitor\" setups this refers to the pixel width for all
2324 physical monitors associated with TERMINAL. To get information for
2325 each physical monitor, use `display-monitor-attributes-list'. */)
2326 (Lisp_Object terminal)
2328 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2330 return make_number (x_display_pixel_width (dpyinfo));
2334 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2335 Sx_display_pixel_height, 0, 1, 0,
2336 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2337 The optional argument TERMINAL specifies which display to ask about.
2338 TERMINAL should be a terminal object, a frame or a display name (a string).
2339 If omitted or nil, that stands for the selected frame's display.
2341 On \"multi-monitor\" setups this refers to the pixel height for all
2342 physical monitors associated with TERMINAL. To get information for
2343 each physical monitor, use `display-monitor-attributes-list'. */)
2344 (Lisp_Object terminal)
2346 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2348 return make_number (x_display_pixel_height (dpyinfo));
2351 #ifdef NS_IMPL_COCOA
2352 /* Returns the name for the screen that DICT came from, or NULL.
2353 Caller must free return value.
2357 ns_screen_name (CGDirectDisplayID did)
2360 NSDictionary *info = (NSDictionary *)
2361 IODisplayCreateInfoDictionary (CGDisplayIOServicePort (did),
2362 kIODisplayOnlyPreferredName);
2364 = [info objectForKey:
2365 [NSString stringWithUTF8String:kDisplayProductName]];
2367 if ([names count] > 0) {
2368 NSString *n = [names objectForKey: [[names allKeys] objectAtIndex:0]];
2370 name = xstrdup ([n UTF8String]);
2379 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2381 int primary_monitor,
2384 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2385 Lisp_Object frame, rest;
2386 NSArray *screens = [NSScreen screens];
2389 FOR_EACH_FRAME (rest, frame)
2391 struct frame *f = XFRAME (frame);
2395 NSView *view = FRAME_NS_VIEW (f);
2396 NSScreen *screen = [[view window] screen];
2400 for (k = 0; i == -1 && k < [screens count]; ++k)
2402 if ([screens objectAtIndex: k] == screen)
2407 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2411 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2412 monitor_frames, source);
2415 DEFUN ("ns-display-monitor-attributes-list",
2416 Fns_display_monitor_attributes_list,
2417 Sns_display_monitor_attributes_list,
2419 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2421 The optional argument TERMINAL specifies which display to ask about.
2422 TERMINAL should be a terminal object, a frame or a display name (a string).
2423 If omitted or nil, that stands for the selected frame's display.
2425 In addition to the standard attribute keys listed in
2426 `display-monitor-attributes-list', the following keys are contained in
2429 source -- String describing the source from which multi-monitor
2430 information is obtained, \"NS\" is always the source."
2432 Internal use only, use `display-monitor-attributes-list' instead. */)
2433 (Lisp_Object terminal)
2435 struct terminal *term = get_terminal (terminal, 1);
2437 NSUInteger i, n_monitors;
2438 struct MonitorInfo *monitors;
2439 Lisp_Object attributes_list = Qnil;
2440 CGFloat primary_display_height = 0;
2442 if (term->type != output_ns)
2445 screens = [NSScreen screens];
2446 n_monitors = [screens count];
2447 if (n_monitors == 0)
2450 monitors = (struct MonitorInfo *) xzalloc (n_monitors * sizeof (*monitors));
2452 for (i = 0; i < [screens count]; ++i)
2454 NSScreen *s = [screens objectAtIndex:i];
2455 struct MonitorInfo *m = &monitors[i];
2456 NSRect fr = [s frame];
2457 NSRect vfr = [s visibleFrame];
2460 #ifdef NS_IMPL_COCOA
2461 NSDictionary *dict = [s deviceDescription];
2462 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2463 CGDirectDisplayID did = [nid unsignedIntValue];
2467 primary_display_height = fr.size.height;
2468 y = (short) fr.origin.y;
2469 vy = (short) vfr.origin.y;
2473 // Flip y coordinate as NS has y starting from the bottom.
2474 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2475 vy = (short) (primary_display_height -
2476 vfr.size.height - vfr.origin.y);
2479 m->geom.x = (short) fr.origin.x;
2481 m->geom.width = (unsigned short) fr.size.width;
2482 m->geom.height = (unsigned short) fr.size.height;
2484 m->work.x = (short) vfr.origin.x;
2485 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2486 // and fr.size.height - vfr.size.height are pixels missing in total.
2487 // Pixels missing at top are
2488 // fr.size.height - vfr.size.height - vy + y.
2489 // work.y is then pixels missing at top + y.
2490 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2491 m->work.width = (unsigned short) vfr.size.width;
2492 m->work.height = (unsigned short) vfr.size.height;
2494 #ifdef NS_IMPL_COCOA
2495 m->name = ns_screen_name (did);
2498 CGSize mms = CGDisplayScreenSize (did);
2499 m->mm_width = (int) mms.width;
2500 m->mm_height = (int) mms.height;
2504 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2505 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2506 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2510 // Primary monitor is always first for NS.
2511 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2514 free_monitors (monitors, n_monitors);
2515 return attributes_list;
2519 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2521 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2522 The optional argument TERMINAL specifies which display to ask about.
2523 TERMINAL should be a terminal object, a frame or a display name (a string).
2524 If omitted or nil, that stands for the selected frame's display. */)
2525 (Lisp_Object terminal)
2527 check_ns_display_info (terminal);
2529 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2533 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2535 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2536 The optional argument TERMINAL specifies which display to ask about.
2537 TERMINAL should be a terminal object, a frame or a display name (a string).
2538 If omitted or nil, that stands for the selected frame's display. */)
2539 (Lisp_Object terminal)
2541 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2542 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2543 return make_number (1 << min (dpyinfo->n_planes, 24));
2547 /* Unused dummy def needed for compatibility. */
2548 Lisp_Object tip_frame;
2550 /* TODO: move to xdisp or similar */
2552 compute_tip_xy (struct frame *f,
2561 Lisp_Object left, top;
2562 EmacsView *view = FRAME_NS_VIEW (f);
2565 /* Start with user-specified or mouse position. */
2566 left = Fcdr (Fassq (Qleft, parms));
2567 top = Fcdr (Fassq (Qtop, parms));
2569 if (!INTEGERP (left) || !INTEGERP (top))
2571 pt = last_mouse_motion_position;
2572 /* Convert to screen coordinates */
2573 pt = [view convertPoint: pt toView: nil];
2574 pt = [[view window] convertBaseToScreen: pt];
2578 /* Absolute coordinates. */
2580 pt.y = x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)) - XINT (top)
2584 /* Ensure in bounds. (Note, screen origin = lower left.) */
2585 if (INTEGERP (left))
2587 else if (pt.x + XINT (dx) <= 0)
2588 *root_x = 0; /* Can happen for negative dx */
2589 else if (pt.x + XINT (dx) + width
2590 <= x_display_pixel_width (FRAME_NS_DISPLAY_INFO (f)))
2591 /* It fits to the right of the pointer. */
2592 *root_x = pt.x + XINT (dx);
2593 else if (width + XINT (dx) <= pt.x)
2594 /* It fits to the left of the pointer. */
2595 *root_x = pt.x - width - XINT (dx);
2597 /* Put it left justified on the screen -- it ought to fit that way. */
2602 else if (pt.y - XINT (dy) - height >= 0)
2603 /* It fits below the pointer. */
2604 *root_y = pt.y - height - XINT (dy);
2605 else if (pt.y + XINT (dy) + height
2606 <= x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)))
2607 /* It fits above the pointer */
2608 *root_y = pt.y + XINT (dy);
2610 /* Put it on the top. */
2611 *root_y = x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)) - height;
2615 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2616 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2617 A tooltip window is a small window displaying a string.
2619 This is an internal function; Lisp code should call `tooltip-show'.
2621 FRAME nil or omitted means use the selected frame.
2623 PARMS is an optional list of frame parameters which can be used to
2624 change the tooltip's appearance.
2626 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2627 means use the default timeout of 5 seconds.
2629 If the list of frame parameters PARMS contains a `left' parameter,
2630 the tooltip is displayed at that x-position. Otherwise it is
2631 displayed at the mouse position, with offset DX added (default is 5 if
2632 DX isn't specified). Likewise for the y-position; if a `top' frame
2633 parameter is specified, it determines the y-position of the tooltip
2634 window, otherwise it is displayed at the mouse position, with offset
2635 DY added (default is -10).
2637 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2638 Text larger than the specified size is clipped. */)
2639 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2642 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2643 ptrdiff_t count = SPECPDL_INDEX ();
2648 specbind (Qinhibit_redisplay, Qt);
2650 GCPRO4 (string, parms, frame, timeout);
2652 CHECK_STRING (string);
2653 str = SSDATA (string);
2654 f = decode_window_system_frame (frame);
2656 timeout = make_number (5);
2658 CHECK_NATNUM (timeout);
2661 dx = make_number (5);
2666 dy = make_number (-10);
2671 if (ns_tooltip == nil)
2672 ns_tooltip = [[EmacsTooltip alloc] init];
2676 [ns_tooltip setText: str];
2677 size = [ns_tooltip frame].size;
2679 /* Move the tooltip window where the mouse pointer is. Resize and
2681 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2684 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2688 return unbind_to (count, Qnil);
2692 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2693 doc: /* Hide the current tooltip window, if there is any.
2694 Value is t if tooltip was open, nil otherwise. */)
2697 if (ns_tooltip == nil || ![ns_tooltip isActive])
2704 /* ==========================================================================
2706 Class implementations
2708 ========================================================================== */
2711 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
2712 Return YES if handled, NO if not.
2715 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
2721 if ([theEvent type] != NSKeyDown) return NO;
2722 s = [theEvent characters];
2724 for (i = 0; i < [s length]; ++i)
2726 int ch = (int) [s characterAtIndex: i];
2729 case NSHomeFunctionKey:
2730 case NSDownArrowFunctionKey:
2731 case NSUpArrowFunctionKey:
2732 case NSLeftArrowFunctionKey:
2733 case NSRightArrowFunctionKey:
2734 case NSPageUpFunctionKey:
2735 case NSPageDownFunctionKey:
2736 case NSEndFunctionKey:
2737 /* Don't send command modified keys, as those are handled in the
2738 performKeyEquivalent method of the super class.
2740 if (! ([theEvent modifierFlags] & NSCommandKeyMask))
2742 [panel sendEvent: theEvent];
2746 /* As we don't have the standard key commands for
2747 copy/paste/cut/select-all in our edit menu, we must handle
2748 them here. TODO: handle Emacs key bindings for copy/cut/select-all
2749 here, paste works, because we have that in our Edit menu.
2750 I.e. refactor out code in nsterm.m, keyDown: to figure out the
2756 case 'a': // Select all
2757 if ([theEvent modifierFlags] & NSCommandKeyMask)
2766 : @selector(selectAll:))))
2771 // Send all control keys, as the text field supports C-a, C-f, C-e
2773 if ([theEvent modifierFlags] & NSControlKeyMask)
2775 [panel sendEvent: theEvent];
2786 @implementation EmacsSavePanel
2787 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
2789 BOOL ret = handlePanelKeys (self, theEvent);
2791 ret = [super performKeyEquivalent:theEvent];
2797 @implementation EmacsOpenPanel
2798 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
2800 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
2801 BOOL ret = handlePanelKeys (self, theEvent);
2803 ret = [super performKeyEquivalent:theEvent];
2809 @implementation EmacsFileDelegate
2810 /* --------------------------------------------------------------------------
2811 Delegate methods for Open/Save panels
2812 -------------------------------------------------------------------------- */
2813 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
2817 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
2821 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
2822 confirmed: (BOOL)okFlag
2831 /* ==========================================================================
2833 Lisp interface declaration
2835 ========================================================================== */
2839 syms_of_nsfns (void)
2841 Qfontsize = intern_c_string ("fontsize");
2842 staticpro (&Qfontsize);
2844 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
2845 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
2846 If the title of a frame matches REGEXP, then IMAGE.tiff is
2847 selected as the image of the icon representing the frame when it's
2848 miniaturized. If an element is t, then Emacs tries to select an icon
2849 based on the filetype of the visited file.
2851 The images have to be installed in a folder called English.lproj in the
2852 Emacs folder. You have to restart Emacs after installing new icons.
2854 Example: Install an icon Gnus.tiff and execute the following code
2856 (setq ns-icon-type-alist
2857 (append ns-icon-type-alist
2858 '((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
2861 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
2862 be used as the image of the icon representing the frame. */);
2863 Vns_icon_type_alist = list1 (Qt);
2865 DEFVAR_LISP ("ns-version-string", Vns_version_string,
2866 doc: /* Toolkit version for NS Windowing. */);
2867 Vns_version_string = ns_appkit_version_str ();
2869 defsubr (&Sns_read_file_name);
2870 defsubr (&Sns_get_resource);
2871 defsubr (&Sns_set_resource);
2872 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
2873 defsubr (&Sx_display_grayscale_p);
2874 defsubr (&Sns_font_name);
2875 defsubr (&Sns_list_colors);
2876 #ifdef NS_IMPL_COCOA
2877 defsubr (&Sns_do_applescript);
2879 defsubr (&Sxw_color_defined_p);
2880 defsubr (&Sxw_color_values);
2881 defsubr (&Sx_server_max_request_size);
2882 defsubr (&Sx_server_vendor);
2883 defsubr (&Sx_server_version);
2884 defsubr (&Sx_display_pixel_width);
2885 defsubr (&Sx_display_pixel_height);
2886 defsubr (&Sns_display_monitor_attributes_list);
2887 defsubr (&Sx_display_mm_width);
2888 defsubr (&Sx_display_mm_height);
2889 defsubr (&Sx_display_screens);
2890 defsubr (&Sx_display_planes);
2891 defsubr (&Sx_display_color_cells);
2892 defsubr (&Sx_display_visual_class);
2893 defsubr (&Sx_display_backing_store);
2894 defsubr (&Sx_display_save_under);
2895 defsubr (&Sx_create_frame);
2896 defsubr (&Sx_open_connection);
2897 defsubr (&Sx_close_connection);
2898 defsubr (&Sx_display_list);
2900 defsubr (&Sns_hide_others);
2901 defsubr (&Sns_hide_emacs);
2902 defsubr (&Sns_emacs_info_panel);
2903 defsubr (&Sns_list_services);
2904 defsubr (&Sns_perform_service);
2905 defsubr (&Sns_convert_utf8_nfd_to_nfc);
2906 defsubr (&Sx_focus_frame);
2907 defsubr (&Sns_popup_font_panel);
2908 defsubr (&Sns_popup_color_panel);
2910 defsubr (&Sx_show_tip);
2911 defsubr (&Sx_hide_tip);