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 /* hack for OS X file panels */
95 EmacsTooltip *ns_tooltip = nil;
97 /* Need forward declaration here to preserve organizational integrity of file */
98 Lisp_Object Fx_open_connection (Lisp_Object, Lisp_Object, Lisp_Object);
100 /* Static variables to handle applescript execution. */
101 static Lisp_Object as_script, *as_result;
102 static int as_status;
105 static ptrdiff_t image_cache_refcount;
109 /* ==========================================================================
111 Internal utility functions
113 ========================================================================== */
115 /* Let the user specify a Nextstep display with a Lisp object.
116 OBJECT may be nil, a frame or a terminal object.
117 nil stands for the selected frame--or, if that is not a Nextstep frame,
118 the first Nextstep display on the list. */
120 static struct ns_display_info *
121 check_ns_display_info (Lisp_Object object)
123 struct ns_display_info *dpyinfo = NULL;
127 struct frame *sf = XFRAME (selected_frame);
129 if (FRAME_NS_P (sf) && FRAME_LIVE_P (sf))
130 dpyinfo = FRAME_NS_DISPLAY_INFO (sf);
131 else if (x_display_list != 0)
132 dpyinfo = x_display_list;
134 error ("Nextstep windows are not in use or not initialized");
136 else if (TERMINALP (object))
138 struct terminal *t = get_terminal (object, 1);
140 if (t->type != output_ns)
141 error ("Terminal %d is not a Nextstep display", t->id);
143 dpyinfo = t->display_info.ns;
145 else if (STRINGP (object))
146 dpyinfo = ns_display_info_for_name (object);
149 FRAME_PTR f = decode_window_system_frame (object);
150 dpyinfo = FRAME_NS_DISPLAY_INFO (f);
158 ns_get_window (Lisp_Object maybeFrame)
160 id view =nil, window =nil;
162 if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
163 maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */
165 if (!NILP (maybeFrame))
166 view = FRAME_NS_VIEW (XFRAME (maybeFrame));
167 if (view) window =[view window];
173 /* Return the X display structure for the display named NAME.
174 Open a new connection if necessary. */
175 struct ns_display_info *
176 ns_display_info_for_name (Lisp_Object name)
179 struct ns_display_info *dpyinfo;
183 for (dpyinfo = x_display_list, names = ns_display_name_list;
185 dpyinfo = dpyinfo->next, names = XCDR (names))
188 tem = Fstring_equal (XCAR (XCAR (names)), name);
193 error ("Emacs for OpenStep does not yet support multi-display.");
195 Fx_open_connection (name, Qnil, Qnil);
196 dpyinfo = x_display_list;
199 error ("OpenStep on %s not responding.\n", SDATA (name));
205 ns_filename_from_panel (NSSavePanel *panel)
207 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
208 NSURL *url = [panel URL];
209 NSString *str = [url path];
212 return [panel filename];
217 ns_directory_from_panel (NSSavePanel *panel)
219 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
220 NSURL *url = [panel directoryURL];
221 NSString *str = [url path];
224 return [panel directory];
229 interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
230 /* --------------------------------------------------------------------------
231 Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
232 -------------------------------------------------------------------------- */
242 count = [menu numberOfItems];
243 for (i = 0; i<count; i++)
245 item = [menu itemAtIndex: i];
246 name = [[item title] UTF8String];
249 nameStr = build_string (name);
251 if ([item hasSubmenu])
253 old = interpret_services_menu ([item submenu],
254 Fcons (nameStr, prefix), old);
258 keys = [item keyEquivalent];
259 if (keys && [keys length] )
261 key = [keys characterAtIndex: 0];
262 res = make_number (key|super_modifier);
268 old = Fcons (Fcons (res,
269 Freverse (Fcons (nameStr,
279 /* ==========================================================================
281 Frame parameter setters
283 ========================================================================== */
287 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
290 CGFloat r, g, b, alpha;
292 if (ns_lisp_to_color (arg, &col))
294 store_frame_param (f, Qforeground_color, oldval);
295 error ("Unknown color");
299 [f->output_data.ns->foreground_color release];
300 f->output_data.ns->foreground_color = col;
302 [col getRed: &r green: &g blue: &b alpha: &alpha];
303 FRAME_FOREGROUND_PIXEL (f) =
304 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
306 if (FRAME_NS_VIEW (f))
308 update_face_from_frame_parameter (f, Qforeground_color, arg);
309 /*recompute_basic_faces (f); */
310 if (FRAME_VISIBLE_P (f))
317 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
321 NSView *view = FRAME_NS_VIEW (f);
322 CGFloat r, g, b, alpha;
324 if (ns_lisp_to_color (arg, &col))
326 store_frame_param (f, Qbackground_color, oldval);
327 error ("Unknown color");
330 /* clear the frame; in some instances the NS-internal GC appears not to
331 update, or it does update and cannot clear old text properly */
332 if (FRAME_VISIBLE_P (f))
336 [f->output_data.ns->background_color release];
337 f->output_data.ns->background_color = col;
339 [col getRed: &r green: &g blue: &b alpha: &alpha];
340 FRAME_BACKGROUND_PIXEL (f) =
341 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
345 [[view window] setBackgroundColor: col];
348 [[view window] setOpaque: NO];
350 [[view window] setOpaque: YES];
352 face = FRAME_DEFAULT_FACE (f);
355 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
356 face->background = ns_index_color
357 ([col colorWithAlphaComponent: alpha], f);
359 update_face_from_frame_parameter (f, Qbackground_color, arg);
362 if (FRAME_VISIBLE_P (f))
369 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
373 if (ns_lisp_to_color (arg, &col))
375 store_frame_param (f, Qcursor_color, oldval);
376 error ("Unknown color");
379 [FRAME_CURSOR_COLOR (f) release];
380 FRAME_CURSOR_COLOR (f) = [col retain];
382 if (FRAME_VISIBLE_P (f))
384 x_update_cursor (f, 0);
385 x_update_cursor (f, 1);
387 update_face_from_frame_parameter (f, Qcursor_color, arg);
392 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
394 NSView *view = FRAME_NS_VIEW (f);
395 NSTRACE (x_set_icon_name);
397 /* see if it's changed */
400 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
403 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
406 fset_icon_name (f, arg);
410 if (!NILP (f->title))
413 /* explicit name and no icon-name -> explicit_name */
414 if (f->explicit_name)
418 /* no explicit name and no icon-name ->
419 name has to be rebuild from icon_title_format */
420 windows_or_buffers_changed++;
425 /* Don't change the name if it's already NAME. */
426 if ([[view window] miniwindowTitle] &&
427 ([[[view window] miniwindowTitle]
428 isEqualToString: [NSString stringWithUTF8String:
432 [[view window] setMiniwindowTitle:
433 [NSString stringWithUTF8String: SSDATA (arg)]];
437 ns_set_name_internal (FRAME_PTR f, Lisp_Object name)
440 Lisp_Object encoded_name, encoded_icon_name;
442 NSView *view = FRAME_NS_VIEW (f);
445 encoded_name = ENCODE_UTF_8 (name);
448 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
450 /* Don't change the name if it's already NAME. */
451 if (! [[[view window] title] isEqualToString: str])
452 [[view window] setTitle: str];
454 if (!STRINGP (f->icon_name))
455 encoded_icon_name = encoded_name;
457 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
459 str = [NSString stringWithUTF8String: SSDATA (encoded_icon_name)];
461 if ([[view window] miniwindowTitle] &&
462 ! [[[view window] miniwindowTitle] isEqualToString: str])
463 [[view window] setMiniwindowTitle: str];
468 ns_set_name (struct frame *f, Lisp_Object name, int explicit)
470 NSTRACE (ns_set_name);
472 /* Make sure that requests from lisp code override requests from
473 Emacs redisplay code. */
476 /* If we're switching from explicit to implicit, we had better
477 update the mode lines and thereby update the title. */
478 if (f->explicit_name && NILP (name))
479 update_mode_lines = 1;
481 f->explicit_name = ! NILP (name);
483 else if (f->explicit_name)
487 name = build_string([ns_app_name UTF8String]);
491 /* Don't change the name if it's already NAME. */
492 if (! NILP (Fstring_equal (name, f->name)))
497 /* title overrides explicit name */
498 if (! NILP (f->title))
501 ns_set_name_internal (f, name);
505 /* This function should be called when the user's lisp code has
506 specified a name for the frame; the name will override any set by the
509 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
511 NSTRACE (x_explicitly_set_name);
512 ns_set_name (f, arg, 1);
516 /* This function should be called by Emacs redisplay code to set the
517 name; names set this way will never override names set by the user's
520 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
522 NSTRACE (x_implicitly_set_name);
524 /* Deal with NS specific format t. */
525 if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (Vicon_title_format, Qt))
526 || EQ (Vframe_title_format, Qt)))
527 ns_set_name_as_filename (f);
529 ns_set_name (f, arg, 0);
533 /* Change the title of frame F to NAME.
534 If NAME is nil, use the frame name as the title. */
537 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
539 NSTRACE (x_set_title);
540 /* Don't change the title if it's already NAME. */
541 if (EQ (name, f->title))
544 update_mode_lines = 1;
546 fset_title (f, name);
553 ns_set_name_internal (f, name);
558 ns_set_name_as_filename (struct frame *f)
561 Lisp_Object name, filename;
562 Lisp_Object buf = XWINDOW (f->selected_window)->contents;
564 NSAutoreleasePool *pool;
566 Lisp_Object encoded_name, encoded_filename;
568 NSTRACE (ns_set_name_as_filename);
570 if (f->explicit_name || ! NILP (f->title))
574 pool = [[NSAutoreleasePool alloc] init];
575 filename = BVAR (XBUFFER (buf), filename);
576 name = BVAR (XBUFFER (buf), name);
580 if (! NILP (filename))
581 name = Ffile_name_nondirectory (filename);
583 name = build_string ([ns_app_name UTF8String]);
587 encoded_name = ENCODE_UTF_8 (name);
590 view = FRAME_NS_VIEW (f);
592 title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
593 : [[[view window] title] UTF8String];
595 if (title && (! strcmp (title, SSDATA (encoded_name))))
602 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
603 if (str == nil) str = @"Bad coding";
605 if (FRAME_ICONIFIED_P (f))
606 [[view window] setMiniwindowTitle: str];
611 if (! NILP (filename))
614 encoded_filename = ENCODE_UTF_8 (filename);
617 fstr = [NSString stringWithUTF8String: SSDATA (encoded_filename)];
618 if (fstr == nil) fstr = @"";
620 /* work around a bug observed on 10.3 and later where
621 setTitleWithRepresentedFilename does not clear out previous state
622 if given filename does not exist */
623 if (! [[NSFileManager defaultManager] fileExistsAtPath: fstr])
624 [[view window] setRepresentedFilename: @""];
630 [[view window] setRepresentedFilename: fstr];
631 [[view window] setTitle: str];
641 ns_set_doc_edited (struct frame *f, Lisp_Object arg)
643 NSView *view = FRAME_NS_VIEW (f);
644 NSAutoreleasePool *pool;
645 if (!MINI_WINDOW_P (XWINDOW (f->selected_window)))
648 pool = [[NSAutoreleasePool alloc] init];
649 [[view window] setDocumentEdited: !NILP (arg)];
657 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
660 if (FRAME_MINIBUF_ONLY_P (f))
663 if (TYPE_RANGED_INTEGERP (int, value))
664 nlines = XINT (value);
668 FRAME_MENU_BAR_LINES (f) = 0;
671 FRAME_EXTERNAL_MENU_BAR (f) = 1;
672 /* does for all frames, whereas we just want for one frame
673 [NSMenu setMenuBarVisible: YES]; */
677 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
678 free_frame_menubar (f);
679 /* [NSMenu setMenuBarVisible: NO]; */
680 FRAME_EXTERNAL_MENU_BAR (f) = 0;
685 /* toolbar support */
687 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
691 if (FRAME_MINIBUF_ONLY_P (f))
694 if (RANGED_INTEGERP (0, value, INT_MAX))
695 nlines = XFASTINT (value);
701 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
702 update_frame_tool_bar (f);
706 if (FRAME_EXTERNAL_TOOL_BAR (f))
708 free_frame_tool_bar (f);
709 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
713 x_set_window_size (f, 0, f->text_cols, f->text_lines);
718 ns_implicitly_set_icon_type (struct frame *f)
721 EmacsView *view = FRAME_NS_VIEW (f);
723 Lisp_Object chain, elt;
724 NSAutoreleasePool *pool;
727 NSTRACE (ns_implicitly_set_icon_type);
730 pool = [[NSAutoreleasePool alloc] init];
731 if (f->output_data.ns->miniimage
732 && [[NSString stringWithUTF8String: SSDATA (f->name)]
733 isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
740 tem = assq_no_quit (Qicon_type, f->param_alist);
741 if (CONSP (tem) && ! NILP (XCDR (tem)))
748 for (chain = Vns_icon_type_alist;
749 image == nil && CONSP (chain);
750 chain = XCDR (chain))
753 /* special case: 't' means go by file type */
754 if (SYMBOLP (elt) && EQ (elt, Qt) && SSDATA (f->name)[0] == '/')
757 = [NSString stringWithUTF8String: SSDATA (f->name)];
758 if ([[NSFileManager defaultManager] fileExistsAtPath: str])
759 image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
761 else if (CONSP (elt) &&
762 STRINGP (XCAR (elt)) &&
763 STRINGP (XCDR (elt)) &&
764 fast_string_match (XCAR (elt), f->name) >= 0)
766 image = [EmacsImage allocInitFromFile: XCDR (elt)];
768 image = [[NSImage imageNamed:
769 [NSString stringWithUTF8String:
770 SSDATA (XCDR (elt))]] retain];
776 image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
780 [f->output_data.ns->miniimage release];
781 f->output_data.ns->miniimage = image;
782 [view setMiniwindowImage: setMini];
789 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
791 EmacsView *view = FRAME_NS_VIEW (f);
795 NSTRACE (x_set_icon_type);
797 if (!NILP (arg) && SYMBOLP (arg))
799 arg =build_string (SSDATA (SYMBOL_NAME (arg)));
800 store_frame_param (f, Qicon_type, arg);
803 /* do it the implicit way */
806 ns_implicitly_set_icon_type (f);
812 image = [EmacsImage allocInitFromFile: arg];
814 image =[NSImage imageNamed: [NSString stringWithUTF8String:
819 image = [NSImage imageNamed: @"text"];
823 f->output_data.ns->miniimage = image;
824 [view setMiniwindowImage: setMini];
828 /* TODO: move to nsterm? */
830 ns_lisp_to_cursor_type (Lisp_Object arg)
833 if (XTYPE (arg) == Lisp_String)
835 else if (XTYPE (arg) == Lisp_Symbol)
836 str = SSDATA (SYMBOL_NAME (arg));
838 if (!strcmp (str, "box")) return FILLED_BOX_CURSOR;
839 if (!strcmp (str, "hollow")) return HOLLOW_BOX_CURSOR;
840 if (!strcmp (str, "hbar")) return HBAR_CURSOR;
841 if (!strcmp (str, "bar")) return BAR_CURSOR;
842 if (!strcmp (str, "no")) return NO_CURSOR;
848 ns_cursor_type_to_lisp (int arg)
852 case FILLED_BOX_CURSOR: return Qbox;
853 case HOLLOW_BOX_CURSOR: return intern ("hollow");
854 case HBAR_CURSOR: return intern ("hbar");
855 case BAR_CURSOR: return intern ("bar");
857 default: return intern ("no");
861 /* This is the same as the xfns.c definition. */
863 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
865 set_frame_cursor_types (f, arg);
867 /* Make sure the cursor gets redrawn. */
868 cursor_type_changed = 1;
872 /* called to set mouse pointer color, but all other terms use it to
873 initialize pointer types (and don't set the color ;) */
875 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
877 /* don't think we can do this on Nextstep */
882 #define Xstr(x) Str(x)
885 ns_appkit_version_str (void)
889 #ifdef NS_IMPL_GNUSTEP
890 sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
891 #elif defined(NS_IMPL_COCOA)
892 sprintf(tmp, "apple-appkit-%.2f", NSAppKitVersionNumber);
896 return build_string (tmp);
900 /* This is for use by x-server-version and collapses all version info we
901 have into a single int. For a better picture of the implementation
902 running, use ns_appkit_version_str.*/
904 ns_appkit_version_int (void)
906 #ifdef NS_IMPL_GNUSTEP
907 return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
908 #elif defined(NS_IMPL_COCOA)
909 return (int)NSAppKitVersionNumber;
916 x_icon (struct frame *f, Lisp_Object parms)
917 /* --------------------------------------------------------------------------
918 Strangely-named function to set icon position parameters in frame.
919 This is irrelevant under OS X, but might be needed under GNUstep,
920 depending on the window manager used. Note, this is not a standard
921 frame parameter-setter; it is called directly from x-create-frame.
922 -------------------------------------------------------------------------- */
924 Lisp_Object icon_x, icon_y;
925 struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);
927 f->output_data.ns->icon_top = Qnil;
928 f->output_data.ns->icon_left = Qnil;
930 /* Set the position of the icon. */
931 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
932 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
933 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
935 CHECK_NUMBER (icon_x);
936 CHECK_NUMBER (icon_y);
937 f->output_data.ns->icon_top = icon_y;
938 f->output_data.ns->icon_left = icon_x;
940 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
941 error ("Both left and top icon corners of icon must be specified");
945 /* Note: see frame.c for template, also where generic functions are impl */
946 frame_parm_handler ns_frame_parm_handlers[] =
948 x_set_autoraise, /* generic OK */
949 x_set_autolower, /* generic OK */
950 x_set_background_color,
951 0, /* x_set_border_color, may be impossible under Nextstep */
952 0, /* x_set_border_width, may be impossible under Nextstep */
955 x_set_font, /* generic OK */
956 x_set_foreground_color,
959 x_set_internal_border_width, /* generic OK */
960 x_set_menu_bar_lines,
962 x_explicitly_set_name,
963 x_set_scroll_bar_width, /* generic OK */
965 x_set_unsplittable, /* generic OK */
966 x_set_vertical_scroll_bars, /* generic OK */
967 x_set_visibility, /* generic OK */
968 x_set_tool_bar_lines,
969 0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
970 0, /* x_set_scroll_bar_background, will ignore (not possible on NS) */
971 x_set_screen_gamma, /* generic OK */
972 x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
973 x_set_fringe_width, /* generic OK */
974 x_set_fringe_width, /* generic OK */
975 0, /* x_set_wait_for_wm, will ignore */
976 x_set_fullscreen, /* generic OK */
977 x_set_font_backend, /* generic OK */
979 0, /* x_set_sticky */
980 0, /* x_set_tool_bar_position */
984 /* Handler for signals raised during x_create_frame.
985 FRAME is the frame which is partially constructed. */
988 unwind_create_frame (Lisp_Object frame)
990 struct frame *f = XFRAME (frame);
992 /* If frame is already dead, nothing to do. This can happen if the
993 display is disconnected after the frame has become official, but
994 before x_create_frame removes the unwind protect. */
995 if (!FRAME_LIVE_P (f))
998 /* If frame is ``official'', nothing to do. */
999 if (NILP (Fmemq (frame, Vframe_list)))
1001 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1002 struct ns_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1005 x_free_frame_resources (f);
1009 /* Check that reference counts are indeed correct. */
1010 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
1019 * Read geometry related parameters from preferences if not in PARMS.
1020 * Returns the union of parms and any preferences read.
1024 get_geometry_from_preferences (struct ns_display_info *dpyinfo,
1032 { "width", "Width", Qwidth },
1033 { "height", "Height", Qheight },
1034 { "left", "Left", Qleft },
1035 { "top", "Top", Qtop },
1039 for (i = 0; i < sizeof (r)/sizeof (r[0]); ++i)
1041 if (NILP (Fassq (r[i].tem, parms)))
1044 = x_get_arg (dpyinfo, parms, r[i].tem, r[i].val, r[i].cls,
1046 if (! EQ (value, Qunbound))
1047 parms = Fcons (Fcons (r[i].tem, value), parms);
1054 /* ==========================================================================
1058 ========================================================================== */
1060 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
1062 doc: /* Make a new Nextstep window, called a "frame" in Emacs terms.
1063 Return an Emacs frame object.
1064 PARMS is an alist of frame parameters.
1065 If the parameters specify that the frame should not have a minibuffer,
1066 and do not specify a specific minibuffer window to use,
1067 then `default-minibuffer-frame' must be a frame whose minibuffer can
1068 be shared by the new frame.
1070 This function is an internal primitive--use `make-frame' instead. */)
1074 Lisp_Object frame, tem;
1076 int minibuffer_only = 0;
1077 int window_prompting = 0;
1079 ptrdiff_t count = specpdl_ptr - specpdl;
1080 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1081 Lisp_Object display;
1082 struct ns_display_info *dpyinfo = NULL;
1085 Lisp_Object tfont, tfontsize;
1086 static int desc_ctr = 1;
1088 /* x_get_arg modifies parms. */
1089 parms = Fcopy_alist (parms);
1091 /* Use this general default value to start with
1092 until we know if this frame has a specified name. */
1093 Vx_resource_name = Vinvocation_name;
1095 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
1096 if (EQ (display, Qunbound))
1098 dpyinfo = check_ns_display_info (display);
1099 kb = dpyinfo->terminal->kboard;
1101 if (!dpyinfo->terminal->name)
1102 error ("Terminal is not live, can't create new frames on it");
1104 name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
1106 && ! EQ (name, Qunbound)
1108 error ("Invalid frame name--not a string or nil");
1111 Vx_resource_name = name;
1113 parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
1114 if (EQ (parent, Qunbound))
1116 if (! NILP (parent))
1117 CHECK_NUMBER (parent);
1119 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
1120 /* No need to protect DISPLAY because that's not used after passing
1121 it to make_frame_without_minibuffer. */
1123 GCPRO4 (parms, parent, name, frame);
1124 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
1126 if (EQ (tem, Qnone) || NILP (tem))
1127 f = make_frame_without_minibuffer (Qnil, kb, display);
1128 else if (EQ (tem, Qonly))
1130 f = make_minibuffer_frame ();
1131 minibuffer_only = 1;
1133 else if (WINDOWP (tem))
1134 f = make_frame_without_minibuffer (tem, kb, display);
1138 XSETFRAME (frame, f);
1140 f->terminal = dpyinfo->terminal;
1142 f->output_method = output_ns;
1143 f->output_data.ns = xzalloc (sizeof *f->output_data.ns);
1145 FRAME_FONTSET (f) = -1;
1147 fset_icon_name (f, x_get_arg (dpyinfo, parms, Qicon_name,
1148 "iconName", "Title",
1150 if (! STRINGP (f->icon_name))
1151 fset_icon_name (f, Qnil);
1153 FRAME_NS_DISPLAY_INFO (f) = dpyinfo;
1155 /* With FRAME_NS_DISPLAY_INFO set up, this unwind-protect is safe. */
1156 record_unwind_protect (unwind_create_frame, frame);
1158 f->output_data.ns->window_desc = desc_ctr++;
1159 if (TYPE_RANGED_INTEGERP (Window, parent))
1161 f->output_data.ns->parent_desc = XFASTINT (parent);
1162 f->output_data.ns->explicit_parent = 1;
1166 f->output_data.ns->parent_desc = FRAME_NS_DISPLAY_INFO (f)->root_window;
1167 f->output_data.ns->explicit_parent = 0;
1170 /* Set the name; the functions to which we pass f expect the name to
1172 if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
1174 fset_name (f, build_string ([ns_app_name UTF8String]));
1175 f->explicit_name = 0;
1179 fset_name (f, name);
1180 f->explicit_name = 1;
1181 specbind (Qx_resource_name, name);
1185 register_font_driver (&nsfont_driver, f);
1186 x_default_parameter (f, parms, Qfont_backend, Qnil,
1187 "fontBackend", "FontBackend", RES_TYPE_STRING);
1190 /* use for default font name */
1191 id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
1192 tfontsize = x_default_parameter (f, parms, Qfontsize,
1193 make_number (0 /*(int)[font pointSize]*/),
1194 "fontSize", "FontSize", RES_TYPE_NUMBER);
1195 tfont = x_default_parameter (f, parms, Qfont,
1196 build_string ([[font fontName] UTF8String]),
1197 "font", "Font", RES_TYPE_STRING);
1201 x_default_parameter (f, parms, Qborder_width, make_number (0),
1202 "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
1203 x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
1204 "internalBorderWidth", "InternalBorderWidth",
1207 /* default scrollbars on right on Mac */
1210 #ifdef NS_IMPL_GNUSTEP
1215 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1216 "verticalScrollBars", "VerticalScrollBars",
1219 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1220 "foreground", "Foreground", RES_TYPE_STRING);
1221 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1222 "background", "Background", RES_TYPE_STRING);
1223 /* FIXME: not supported yet in Nextstep */
1224 x_default_parameter (f, parms, Qline_spacing, Qnil,
1225 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1226 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1227 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1228 x_default_parameter (f, parms, Qright_fringe, Qnil,
1229 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1232 image_cache_refcount =
1233 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
1236 init_frame_faces (f);
1238 /* The resources controlling the menu-bar and tool-bar are
1239 processed specially at startup, and reflected in the mode
1240 variables; ignore them here. */
1241 x_default_parameter (f, parms, Qmenu_bar_lines,
1242 NILP (Vmenu_bar_mode)
1243 ? make_number (0) : make_number (1),
1244 NULL, NULL, RES_TYPE_NUMBER);
1245 x_default_parameter (f, parms, Qtool_bar_lines,
1246 NILP (Vtool_bar_mode)
1247 ? make_number (0) : make_number (1),
1248 NULL, NULL, RES_TYPE_NUMBER);
1250 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1251 "BufferPredicate", RES_TYPE_SYMBOL);
1252 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1255 parms = get_geometry_from_preferences (dpyinfo, parms);
1256 window_prompting = x_figure_window_size (f, parms, 1);
1258 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1259 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1261 /* NOTE: on other terms, this is done in set_mouse_color, however this
1262 was not getting called under Nextstep */
1263 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1264 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1265 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1266 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1267 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1268 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1269 FRAME_NS_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1270 = [NSCursor arrowCursor];
1271 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1273 [[EmacsView alloc] initFrameFromEmacs: f];
1277 /* ns_display_info does not have a reference_count. */
1278 f->terminal->reference_count++;
1280 /* It is now ok to make the frame official even if we get an error below.
1281 The frame needs to be on Vframe_list or making it visible won't work. */
1282 Vframe_list = Fcons (frame, Vframe_list);
1284 x_default_parameter (f, parms, Qicon_type, Qnil,
1285 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1287 x_default_parameter (f, parms, Qauto_raise, Qnil,
1288 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1289 x_default_parameter (f, parms, Qauto_lower, Qnil,
1290 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1291 x_default_parameter (f, parms, Qcursor_type, Qbox,
1292 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1293 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1294 "scrollBarWidth", "ScrollBarWidth",
1296 x_default_parameter (f, parms, Qalpha, Qnil,
1297 "alpha", "Alpha", RES_TYPE_NUMBER);
1298 x_default_parameter (f, parms, Qfullscreen, Qnil,
1299 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1301 width = FRAME_COLS (f);
1302 height = FRAME_LINES (f);
1304 SET_FRAME_COLS (f, 0);
1305 FRAME_LINES (f) = 0;
1306 change_frame_size (f, height, width, 1, 0, 0);
1308 if (! f->output_data.ns->explicit_parent)
1310 Lisp_Object visibility;
1312 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1314 if (EQ (visibility, Qunbound))
1317 if (EQ (visibility, Qicon))
1318 x_iconify_frame (f);
1319 else if (! NILP (visibility))
1321 x_make_frame_visible (f);
1322 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1326 /* Must have been Qnil. */
1330 if (FRAME_HAS_MINIBUF_P (f)
1331 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1332 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1333 kset_default_minibuffer_frame (kb, frame);
1335 /* All remaining specified parameters, which have not been "used"
1336 by x_get_arg and friends, now go in the misc. alist of the frame. */
1337 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1338 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1339 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1343 if (window_prompting & USPosition)
1344 x_set_offset (f, f->left_pos, f->top_pos, 1);
1346 /* Make sure windows on this frame appear in calls to next-window
1347 and similar functions. */
1348 Vwindow_list = Qnil;
1350 return unbind_to (count, frame);
1354 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
1355 doc: /* Set the input focus to FRAME.
1356 FRAME nil means use the selected frame. */)
1359 struct frame *f = decode_window_system_frame (frame);
1360 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1362 if (dpyinfo->x_focus_frame != f)
1364 EmacsView *view = FRAME_NS_VIEW (f);
1366 [NSApp activateIgnoringOtherApps: YES];
1367 [[view window] makeKeyAndOrderFront: view];
1375 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1377 doc: /* Pop up the font panel. */)
1380 struct frame *f = decode_window_system_frame (frame);
1381 id fm = [NSFontManager sharedFontManager];
1383 [fm setSelectedFont: ((struct nsfont_info *)f->output_data.ns->font)->nsfont
1385 [fm orderFrontFontPanel: NSApp];
1390 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1392 doc: /* Pop up the color panel. */)
1395 check_window_system (NULL);
1396 [NSApp orderFrontColorPanel: NSApp];
1401 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1402 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1403 Optional arg DIR, if non-nil, supplies a default directory.
1404 Optional arg MUSTMATCH, if non-nil, means the returned file or
1405 directory must exist.
1406 Optional arg INIT, if non-nil, provides a default file name to use.
1407 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1408 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1409 Lisp_Object init, Lisp_Object dir_only_p)
1411 static id fileDelegate = nil;
1416 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1417 [NSString stringWithUTF8String: SSDATA (prompt)];
1418 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1419 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1420 [NSString stringWithUTF8String: SSDATA (dir)];
1421 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1422 [NSString stringWithUTF8String: SSDATA (init)];
1424 check_window_system (NULL);
1426 if (fileDelegate == nil)
1427 fileDelegate = [EmacsFileDelegate new];
1429 [NSCursor setHiddenUntilMouseMoves: NO];
1431 if ([dirS characterAtIndex: 0] == '~')
1432 dirS = [dirS stringByExpandingTildeInPath];
1434 panel = NILP (mustmatch) && NILP (dir_only_p) ?
1435 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1437 [panel setTitle: promptS];
1439 [panel setAllowsOtherFileTypes: YES];
1440 [panel setTreatsFilePackagesAsDirectories: YES];
1441 [panel setDelegate: fileDelegate];
1444 if (! NILP (dir_only_p))
1446 [panel setCanChooseDirectories: YES];
1447 [panel setCanChooseFiles: NO];
1451 /* This is not quite what the documentation says, but it is compatible
1452 with the Gtk+ code. Also, the menu entry says "Open File...". */
1453 [panel setCanChooseDirectories: NO];
1454 [panel setCanChooseFiles: YES];
1458 #if defined (NS_IMPL_COCOA) && \
1459 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1460 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1461 [panel setAllowedFileTypes: nil];
1462 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1463 if (initS && NILP (Ffile_directory_p (init)))
1464 [panel setNameFieldStringValue: [initS lastPathComponent]];
1466 [panel setNameFieldStringValue: @""];
1468 ret = [panel runModal];
1470 if (NILP (mustmatch) && NILP (dir_only_p))
1472 ret = [panel runModalForDirectory: dirS file: initS];
1476 ret = [panel runModalForDirectory: dirS file: initS types: nil];
1480 ret = (ret == NSOKButton) || panelOK;
1484 NSString *str = [panel getFilename];
1485 if (! str) str = [panel getDirectory];
1486 if (! str) ret = NO;
1487 else fname = build_string ([str UTF8String]);
1490 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1493 return ret ? fname : Qnil;
1497 ns_get_defaults_value (const char *key)
1499 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1500 objectForKey: [NSString stringWithUTF8String: key]];
1502 if (!obj) return NULL;
1504 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1508 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1509 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1510 If OWNER is nil, Emacs is assumed. */)
1511 (Lisp_Object owner, Lisp_Object name)
1515 check_window_system (NULL);
1517 owner = build_string([ns_app_name UTF8String]);
1518 CHECK_STRING (name);
1520 value = ns_get_defaults_value (SSDATA (name));
1523 return build_string (value);
1528 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1529 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1530 If OWNER is nil, Emacs is assumed.
1531 If VALUE is nil, the default is removed. */)
1532 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1534 check_window_system (NULL);
1536 owner = build_string ([ns_app_name UTF8String]);
1537 CHECK_STRING (name);
1540 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1541 [NSString stringWithUTF8String: SSDATA (name)]];
1545 CHECK_STRING (value);
1546 [[NSUserDefaults standardUserDefaults] setObject:
1547 [NSString stringWithUTF8String: SSDATA (value)]
1548 forKey: [NSString stringWithUTF8String:
1556 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1557 Sx_server_max_request_size,
1559 doc: /* This function is a no-op. It is only present for completeness. */)
1560 (Lisp_Object terminal)
1562 check_ns_display_info (terminal);
1563 /* This function has no real equivalent under NeXTstep. Return nil to
1569 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1570 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1571 \(Labeling every distributor as a "vendor" embodies the false assumption
1572 that operating systems cannot be developed and distributed noncommercially.)
1573 The optional argument TERMINAL specifies which display to ask about.
1574 TERMINAL should be a terminal object, a frame or a display name (a string).
1575 If omitted or nil, that stands for the selected frame's display. */)
1576 (Lisp_Object terminal)
1578 check_ns_display_info (terminal);
1579 #ifdef NS_IMPL_GNUSTEP
1580 return build_string ("GNU");
1582 return build_string ("Apple");
1587 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1588 doc: /* Return the version numbers of the server of display TERMINAL.
1589 The value is a list of three integers: the major and minor
1590 version numbers of the X Protocol in use, and the distributor-specific release
1591 number. See also the function `x-server-vendor'.
1593 The optional argument TERMINAL specifies which display to ask about.
1594 TERMINAL should be a terminal object, a frame or a display name (a string).
1595 If omitted or nil, that stands for the selected frame's display. */)
1596 (Lisp_Object terminal)
1598 check_ns_display_info (terminal);
1599 /*NOTE: it is unclear what would best correspond with "protocol";
1600 we return 10.3, meaning Panther, since this is roughly the
1601 level that GNUstep's APIs correspond to.
1602 The last number is where we distinguish between the Apple
1603 and GNUstep implementations ("distributor-specific release
1604 number") and give int'ized versions of major.minor. */
1605 return list3i (10, 3, ns_appkit_version_int ());
1609 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1610 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1611 The optional argument TERMINAL specifies which display to ask about.
1612 TERMINAL should be a terminal object, a frame or a display name (a string).
1613 If omitted or nil, that stands for the selected frame's display.
1615 Note: "screen" here is not in Nextstep terminology but in X11's. For
1616 the number of physical monitors, use `(length
1617 (display-monitor-attributes-list TERMINAL))' instead. */)
1618 (Lisp_Object terminal)
1620 check_ns_display_info (terminal);
1621 return make_number (1);
1625 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1626 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1627 The optional argument TERMINAL specifies which display to ask about.
1628 TERMINAL should be a terminal object, a frame or a display name (a string).
1629 If omitted or nil, that stands for the selected frame's display.
1631 On \"multi-monitor\" setups this refers to the height in millimeters for
1632 all physical monitors associated with TERMINAL. To get information
1633 for each physical monitor, use `display-monitor-attributes-list'. */)
1634 (Lisp_Object terminal)
1636 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1638 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1642 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1643 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1644 The optional argument TERMINAL specifies which display to ask about.
1645 TERMINAL should be a terminal object, a frame or a display name (a string).
1646 If omitted or nil, that stands for the selected frame's display.
1648 On \"multi-monitor\" setups this refers to the width in millimeters for
1649 all physical monitors associated with TERMINAL. To get information
1650 for each physical monitor, use `display-monitor-attributes-list'. */)
1651 (Lisp_Object terminal)
1653 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1655 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1659 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1660 Sx_display_backing_store, 0, 1, 0,
1661 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1662 The value may be `buffered', `retained', or `non-retained'.
1663 The optional argument TERMINAL specifies which display to ask about.
1664 TERMINAL should be a terminal object, a frame or a display name (a string).
1665 If omitted or nil, that stands for the selected frame's display. */)
1666 (Lisp_Object terminal)
1668 check_ns_display_info (terminal);
1669 switch ([ns_get_window (terminal) backingType])
1671 case NSBackingStoreBuffered:
1672 return intern ("buffered");
1673 case NSBackingStoreRetained:
1674 return intern ("retained");
1675 case NSBackingStoreNonretained:
1676 return intern ("non-retained");
1678 error ("Strange value for backingType parameter of frame");
1680 return Qnil; /* not reached, shut compiler up */
1684 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1685 Sx_display_visual_class, 0, 1, 0,
1686 doc: /* Return the visual class of the Nextstep display TERMINAL.
1687 The value is one of the symbols `static-gray', `gray-scale',
1688 `static-color', `pseudo-color', `true-color', or `direct-color'.
1690 The optional argument TERMINAL specifies which display to ask about.
1691 TERMINAL should a terminal object, a frame or a display name (a string).
1692 If omitted or nil, that stands for the selected frame's display. */)
1693 (Lisp_Object terminal)
1695 NSWindowDepth depth;
1697 check_ns_display_info (terminal);
1698 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1700 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1701 return intern ("static-gray");
1702 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1703 return intern ("gray-scale");
1704 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1705 return intern ("pseudo-color");
1706 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1707 return intern ("true-color");
1708 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1709 return intern ("direct-color");
1711 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1712 return intern ("direct-color");
1716 DEFUN ("x-display-save-under", Fx_display_save_under,
1717 Sx_display_save_under, 0, 1, 0,
1718 doc: /* Return t if TERMINAL supports the save-under feature.
1719 The optional argument TERMINAL specifies which display to ask about.
1720 TERMINAL should be a terminal object, a frame or a display name (a string).
1721 If omitted or nil, that stands for the selected frame's display. */)
1722 (Lisp_Object terminal)
1724 check_ns_display_info (terminal);
1725 switch ([ns_get_window (terminal) backingType])
1727 case NSBackingStoreBuffered:
1730 case NSBackingStoreRetained:
1731 case NSBackingStoreNonretained:
1735 error ("Strange value for backingType parameter of frame");
1737 return Qnil; /* not reached, shut compiler up */
1741 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1743 doc: /* Open a connection to a display server.
1744 DISPLAY is the name of the display to connect to.
1745 Optional second arg XRM-STRING is a string of resources in xrdb format.
1746 If the optional third arg MUST-SUCCEED is non-nil,
1747 terminate Emacs if we can't open the connection.
1748 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1749 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1751 struct ns_display_info *dpyinfo;
1753 CHECK_STRING (display);
1755 nxatoms_of_nsselect ();
1756 dpyinfo = ns_term_init (display);
1759 if (!NILP (must_succeed))
1760 fatal ("OpenStep on %s not responding.\n",
1763 error ("OpenStep on %s not responding.\n",
1771 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1773 doc: /* Close the connection to TERMINAL's Nextstep display server.
1774 For TERMINAL, specify a terminal object, a frame or a display name (a
1775 string). If TERMINAL is nil, that stands for the selected frame's
1777 (Lisp_Object terminal)
1779 check_ns_display_info (terminal);
1780 [NSApp terminate: NSApp];
1785 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
1786 doc: /* Return the list of display names that Emacs has connections to. */)
1789 Lisp_Object tail, result;
1792 for (tail = ns_display_name_list; CONSP (tail); tail = XCDR (tail))
1793 result = Fcons (XCAR (XCAR (tail)), result);
1799 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
1801 doc: /* Hides all applications other than Emacs. */)
1804 check_window_system (NULL);
1805 [NSApp hideOtherApplications: NSApp];
1809 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
1811 doc: /* If ON is non-nil, the entire Emacs application is hidden.
1812 Otherwise if Emacs is hidden, it is unhidden.
1813 If ON is equal to `activate', Emacs is unhidden and becomes
1814 the active application. */)
1817 check_window_system (NULL);
1818 if (EQ (on, intern ("activate")))
1820 [NSApp unhide: NSApp];
1821 [NSApp activateIgnoringOtherApps: YES];
1824 [NSApp unhide: NSApp];
1826 [NSApp hide: NSApp];
1831 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
1833 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
1836 check_window_system (NULL);
1837 [NSApp orderFrontStandardAboutPanel: nil];
1842 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
1843 doc: /* Determine font PostScript or family name for font NAME.
1844 NAME should be a string containing either the font name or an XLFD
1845 font descriptor. If string contains `fontset' and not
1846 `fontset-startup', it is left alone. */)
1850 CHECK_STRING (name);
1855 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
1858 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
1862 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
1863 doc: /* Return a list of all available colors.
1864 The optional argument FRAME is currently ignored. */)
1867 Lisp_Object list = Qnil;
1868 NSEnumerator *colorlists;
1873 CHECK_FRAME (frame);
1874 if (! FRAME_NS_P (XFRAME (frame)))
1875 error ("non-Nextstep frame used in `ns-list-colors'");
1880 colorlists = [[NSColorList availableColorLists] objectEnumerator];
1881 while ((clist = [colorlists nextObject]))
1883 if ([[clist name] length] < 7 ||
1884 [[clist name] rangeOfString: @"PANTONE"].location == 0)
1886 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
1888 while ((cname = [cnames nextObject]))
1889 list = Fcons (build_string ([cname UTF8String]), list);
1890 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
1891 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
1892 UTF8String]), list); */
1902 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
1903 doc: /* List available Nextstep services by querying NSApp. */)
1906 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1907 /* You can't get services like this in 10.6+. */
1910 Lisp_Object ret = Qnil;
1914 check_window_system (NULL);
1915 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
1916 [NSApp setServicesMenu: svcs];
1917 [NSApp registerServicesMenuSendTypes: ns_send_types
1918 returnTypes: ns_return_types];
1920 /* On Tiger, services menu updating was made lazier (waits for user to
1921 actually click on the menu), so we have to force things along: */
1922 #ifdef NS_IMPL_COCOA
1923 delegate = [svcs delegate];
1924 if (delegate != nil)
1926 if ([delegate respondsToSelector: @selector (menuNeedsUpdate:)])
1927 [delegate menuNeedsUpdate: svcs];
1928 if ([delegate respondsToSelector:
1929 @selector (menu:updateItem:atIndex:shouldCancel:)])
1931 int i, len = [delegate numberOfItemsInMenu: svcs];
1932 for (i =0; i<len; i++)
1933 [svcs addItemWithTitle: @"" action: NULL keyEquivalent: @""];
1934 for (i =0; i<len; i++)
1935 if (![delegate menu: svcs
1936 updateItem: (NSMenuItem *)[svcs itemAtIndex: i]
1937 atIndex: i shouldCancel: NO])
1943 [svcs setAutoenablesItems: NO];
1944 #ifdef NS_IMPL_COCOA
1945 [svcs update]; /* on OS X, converts from '/' structure */
1948 ret = interpret_services_menu (svcs, Qnil, ret);
1954 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
1956 doc: /* Perform Nextstep SERVICE on SEND.
1957 SEND should be either a string or nil.
1958 The return value is the result of the service, as string, or nil if
1959 there was no result. */)
1960 (Lisp_Object service, Lisp_Object send)
1966 CHECK_STRING (service);
1967 check_window_system (NULL);
1969 utfStr = SSDATA (service);
1970 svcName = [NSString stringWithUTF8String: utfStr];
1972 pb =[NSPasteboard pasteboardWithUniqueName];
1973 ns_string_to_pasteboard (pb, send);
1975 if (NSPerformService (svcName, pb) == NO)
1976 Fsignal (Qquit, Fcons (build_string ("service not available"), Qnil));
1978 if ([[pb types] count] == 0)
1979 return build_string ("");
1980 return ns_string_from_pasteboard (pb);
1984 DEFUN ("ns-convert-utf8-nfd-to-nfc", Fns_convert_utf8_nfd_to_nfc,
1985 Sns_convert_utf8_nfd_to_nfc, 1, 1, 0,
1986 doc: /* Return an NFC string that matches the UTF-8 NFD string STR. */)
1989 /* TODO: If GNUstep ever implements precomposedStringWithCanonicalMapping,
1994 utfStr = [NSString stringWithUTF8String: SSDATA (str)];
1995 if (![utfStr respondsToSelector:
1996 @selector (precomposedStringWithCanonicalMapping)])
1999 ("Warning: ns-convert-utf8-nfd-to-nfc unsupported under GNUstep.\n");
2003 utfStr = [utfStr precomposedStringWithCanonicalMapping];
2004 return build_string ([utfStr UTF8String]);
2008 #ifdef NS_IMPL_COCOA
2010 /* Compile and execute the AppleScript SCRIPT and return the error
2011 status as function value. A zero is returned if compilation and
2012 execution is successful, in which case *RESULT is set to a Lisp
2013 string or a number containing the resulting script value. Otherwise,
2016 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2018 NSAppleEventDescriptor *desc;
2019 NSDictionary* errorDict;
2020 NSAppleEventDescriptor* returnDescriptor = NULL;
2022 NSAppleScript* scriptObject =
2023 [[NSAppleScript alloc] initWithSource:
2024 [NSString stringWithUTF8String: SSDATA (script)]];
2026 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2027 [scriptObject release];
2031 if (returnDescriptor != NULL)
2033 // successful execution
2034 if (kAENullEvent != [returnDescriptor descriptorType])
2037 // script returned an AppleScript result
2038 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2039 #if defined (NS_IMPL_COCOA)
2040 (typeUTF16ExternalRepresentation
2041 == [returnDescriptor descriptorType]) ||
2043 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2044 (typeCString == [returnDescriptor descriptorType]))
2046 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2048 *result = build_string([[desc stringValue] UTF8String]);
2052 /* use typeUTF16ExternalRepresentation? */
2053 // coerce the result to the appropriate ObjC type
2054 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2056 *result = make_number([desc int32Value]);
2062 // no script result, return error
2068 /* Helper function called from sendEvent to run applescript
2069 from within the main event loop. */
2072 ns_run_ascript (void)
2074 if (! NILP (as_script))
2075 as_status = ns_do_applescript (as_script, as_result);
2079 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2080 doc: /* Execute AppleScript SCRIPT and return the result.
2081 If compilation and execution are successful, the resulting script value
2082 is returned as a string, a number or, in the case of other constructs, t.
2083 In case the execution fails, an error is signaled. */)
2084 (Lisp_Object script)
2090 CHECK_STRING (script);
2091 check_window_system (NULL);
2096 as_result = &result;
2098 /* executing apple script requires the event loop to run, otherwise
2099 errors aren't returned and executeAndReturnError hangs forever.
2100 Post an event that runs applescript and then start the event loop.
2101 The event loop is exited when the script is done. */
2102 nxev = [NSEvent otherEventWithType: NSApplicationDefined
2103 location: NSMakePoint (0, 0)
2106 windowNumber: [[NSApp mainWindow] windowNumber]
2107 context: [NSApp context]
2110 data2: NSAPP_DATA2_RUNASSCRIPT];
2112 [NSApp postEvent: nxev atStart: NO];
2114 // If there are other events, the event loop may exit. Keep running
2115 // until the script has been handled. */
2116 while (! NILP (as_script))
2125 else if (!STRINGP (result))
2126 error ("AppleScript error %d", status);
2128 error ("%s", SSDATA (result));
2134 /* ==========================================================================
2136 Miscellaneous functions not called through hooks
2138 ========================================================================== */
2140 /* called from frame.c */
2141 struct ns_display_info *
2142 check_x_display_info (Lisp_Object frame)
2144 return check_ns_display_info (frame);
2149 x_set_scroll_bar_default_width (struct frame *f)
2151 int wid = FRAME_COLUMN_WIDTH (f);
2152 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2153 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2158 /* terms impl this instead of x-get-resource directly */
2160 x_get_string_resource (XrmDatabase rdb, char *name, char *class)
2162 /* remove appname prefix; TODO: allow for !="Emacs" */
2163 char *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2165 check_window_system (NULL);
2167 if (inhibit_x_resources)
2168 /* --quick was passed, so this is a no-op. */
2171 res = ns_get_defaults_value (toCheck);
2172 return !res ? NULL :
2173 (!c_strncasecmp (res, "YES", 3) ? "true" :
2174 (!c_strncasecmp (res, "NO", 2) ? "false" : res));
2179 x_get_focus_frame (struct frame *frame)
2181 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (frame);
2182 Lisp_Object nsfocus;
2184 if (!dpyinfo->x_focus_frame)
2187 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2193 x_pixel_width (struct frame *f)
2195 return FRAME_PIXEL_WIDTH (f);
2200 x_pixel_height (struct frame *f)
2202 return FRAME_PIXEL_HEIGHT (f);
2207 x_screen_planes (struct frame *f)
2209 return FRAME_NS_DISPLAY_INFO (f)->n_planes;
2214 x_sync (struct frame *f)
2216 /* XXX Not implemented XXX */
2222 /* ==========================================================================
2224 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2226 ========================================================================== */
2229 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2230 doc: /* Internal function called by `color-defined-p', which see.
2231 \(Note that the Nextstep version of this function ignores FRAME.) */)
2232 (Lisp_Object color, Lisp_Object frame)
2235 check_window_system (NULL);
2236 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2240 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2241 doc: /* Internal function called by `color-values', which see. */)
2242 (Lisp_Object color, Lisp_Object frame)
2245 CGFloat red, green, blue, alpha;
2247 check_window_system (NULL);
2248 CHECK_STRING (color);
2250 if (ns_lisp_to_color (color, &col))
2253 [[col colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
2254 getRed: &red green: &green blue: &blue alpha: &alpha];
2255 return list3i (lrint (red * 65280), lrint (green * 65280),
2256 lrint (blue * 65280));
2260 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2261 doc: /* Internal function called by `display-color-p', which see. */)
2262 (Lisp_Object terminal)
2264 NSWindowDepth depth;
2265 NSString *colorSpace;
2267 check_ns_display_info (terminal);
2268 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2269 colorSpace = NSColorSpaceFromDepth (depth);
2271 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2272 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2277 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2279 doc: /* Return t if the Nextstep display supports shades of gray.
2280 Note that color displays do support shades of gray.
2281 The optional argument TERMINAL specifies which display to ask about.
2282 TERMINAL should be a terminal object, a frame or a display name (a string).
2283 If omitted or nil, that stands for the selected frame's display. */)
2284 (Lisp_Object terminal)
2286 NSWindowDepth depth;
2288 check_ns_display_info (terminal);
2289 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2291 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2295 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2297 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2298 The optional argument TERMINAL specifies which display to ask about.
2299 TERMINAL should be a terminal object, a frame or a display name (a string).
2300 If omitted or nil, that stands for the selected frame's display.
2302 On \"multi-monitor\" setups this refers to the pixel width for all
2303 physical monitors associated with TERMINAL. To get information for
2304 each physical monitor, use `display-monitor-attributes-list'. */)
2305 (Lisp_Object terminal)
2307 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2309 return make_number (x_display_pixel_width (dpyinfo));
2313 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2314 Sx_display_pixel_height, 0, 1, 0,
2315 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2316 The optional argument TERMINAL specifies which display to ask about.
2317 TERMINAL should be a terminal object, a frame or a display name (a string).
2318 If omitted or nil, that stands for the selected frame's display.
2320 On \"multi-monitor\" setups this refers to the pixel height for all
2321 physical monitors associated with TERMINAL. To get information for
2322 each physical monitor, use `display-monitor-attributes-list'. */)
2323 (Lisp_Object terminal)
2325 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2327 return make_number (x_display_pixel_height (dpyinfo));
2330 #ifdef NS_IMPL_COCOA
2331 /* Returns the name for the screen that DICT came from, or NULL.
2332 Caller must free return value.
2336 ns_screen_name (CGDirectDisplayID did)
2339 NSDictionary *info = (NSDictionary *)
2340 IODisplayCreateInfoDictionary (CGDisplayIOServicePort (did),
2341 kIODisplayOnlyPreferredName);
2343 = [info objectForKey:
2344 [NSString stringWithUTF8String:kDisplayProductName]];
2346 if ([names count] > 0) {
2347 NSString *n = [names objectForKey: [[names allKeys] objectAtIndex:0]];
2349 name = xstrdup ([n UTF8String]);
2358 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2360 int primary_monitor,
2363 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2364 Lisp_Object frame, rest;
2365 NSArray *screens = [NSScreen screens];
2368 FOR_EACH_FRAME (rest, frame)
2370 struct frame *f = XFRAME (frame);
2374 NSView *view = FRAME_NS_VIEW (f);
2375 NSScreen *screen = [[view window] screen];
2379 for (k = 0; i == -1 && k < [screens count]; ++k)
2381 if ([screens objectAtIndex: k] == screen)
2386 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2390 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2391 monitor_frames, source);
2394 DEFUN ("ns-display-monitor-attributes-list",
2395 Fns_display_monitor_attributes_list,
2396 Sns_display_monitor_attributes_list,
2398 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2400 The optional argument TERMINAL specifies which display to ask about.
2401 TERMINAL should be a terminal object, a frame or a display name (a string).
2402 If omitted or nil, that stands for the selected frame's display.
2404 In addition to the standard attribute keys listed in
2405 `display-monitor-attributes-list', the following keys are contained in
2408 source -- String describing the source from which multi-monitor
2409 information is obtained, \"NS\" is always the source."
2411 Internal use only, use `display-monitor-attributes-list' instead. */)
2412 (Lisp_Object terminal)
2414 struct terminal *term = get_terminal (terminal, 1);
2416 NSUInteger i, n_monitors;
2417 struct MonitorInfo *monitors;
2418 Lisp_Object attributes_list = Qnil;
2419 CGFloat primary_display_height = 0;
2421 if (term->type != output_ns)
2424 screens = [NSScreen screens];
2425 n_monitors = [screens count];
2426 if (n_monitors == 0)
2429 monitors = (struct MonitorInfo *) xzalloc (n_monitors * sizeof (*monitors));
2431 for (i = 0; i < [screens count]; ++i)
2433 NSScreen *s = [screens objectAtIndex:i];
2434 struct MonitorInfo *m = &monitors[i];
2435 NSRect fr = [s frame];
2436 NSRect vfr = [s visibleFrame];
2437 NSDictionary *dict = [s deviceDescription];
2438 NSValue *resval = [dict valueForKey:NSDeviceResolution];
2441 #ifdef NS_IMPL_COCOA
2442 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2443 CGDirectDisplayID did = [nid unsignedIntValue];
2447 primary_display_height = fr.size.height;
2448 y = (short) fr.origin.y;
2449 vy = (short) vfr.origin.y;
2453 // Flip y coordinate as NS has y starting from the bottom.
2454 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2455 vy = (short) (primary_display_height -
2456 vfr.size.height - vfr.origin.y);
2459 m->geom.x = (short) fr.origin.x;
2461 m->geom.width = (unsigned short) fr.size.width;
2462 m->geom.height = (unsigned short) fr.size.height;
2464 m->work.x = (short) vfr.origin.x;
2465 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2466 // and fr.size.height - vfr.size.height are pixels missing in total.
2467 // Pixels missing at top are
2468 // fr.size.height - vfr.size.height - vy + y.
2469 // work.y is then pixels missing at top + y.
2470 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2471 m->work.width = (unsigned short) vfr.size.width;
2472 m->work.height = (unsigned short) vfr.size.height;
2474 #ifdef NS_IMPL_COCOA
2475 m->name = ns_screen_name (did);
2478 CGSize mms = CGDisplayScreenSize (did);
2479 m->mm_width = (int) mms.width;
2480 m->mm_height = (int) mms.height;
2484 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2485 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2486 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2490 // Primary monitor is always first for NS.
2491 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2494 free_monitors (monitors, n_monitors);
2495 return attributes_list;
2499 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2501 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2502 The optional argument TERMINAL specifies which display to ask about.
2503 TERMINAL should be a terminal object, a frame or a display name (a string).
2504 If omitted or nil, that stands for the selected frame's display. */)
2505 (Lisp_Object terminal)
2507 check_ns_display_info (terminal);
2509 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2513 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2515 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2516 The optional argument TERMINAL specifies which display to ask about.
2517 TERMINAL should be a terminal object, a frame or a display name (a string).
2518 If omitted or nil, that stands for the selected frame's display. */)
2519 (Lisp_Object terminal)
2521 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2522 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2523 return make_number (1 << min (dpyinfo->n_planes, 24));
2527 /* Unused dummy def needed for compatibility. */
2528 Lisp_Object tip_frame;
2530 /* TODO: move to xdisp or similar */
2532 compute_tip_xy (struct frame *f,
2541 Lisp_Object left, top;
2542 EmacsView *view = FRAME_NS_VIEW (f);
2545 /* Start with user-specified or mouse position. */
2546 left = Fcdr (Fassq (Qleft, parms));
2547 top = Fcdr (Fassq (Qtop, parms));
2549 if (!INTEGERP (left) || !INTEGERP (top))
2551 pt = last_mouse_motion_position;
2552 /* Convert to screen coordinates */
2553 pt = [view convertPoint: pt toView: nil];
2554 pt = [[view window] convertBaseToScreen: pt];
2558 /* Absolute coordinates. */
2560 pt.y = x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)) - XINT (top)
2564 /* Ensure in bounds. (Note, screen origin = lower left.) */
2565 if (INTEGERP (left))
2567 else if (pt.x + XINT (dx) <= 0)
2568 *root_x = 0; /* Can happen for negative dx */
2569 else if (pt.x + XINT (dx) + width
2570 <= x_display_pixel_width (FRAME_NS_DISPLAY_INFO (f)))
2571 /* It fits to the right of the pointer. */
2572 *root_x = pt.x + XINT (dx);
2573 else if (width + XINT (dx) <= pt.x)
2574 /* It fits to the left of the pointer. */
2575 *root_x = pt.x - width - XINT (dx);
2577 /* Put it left justified on the screen -- it ought to fit that way. */
2582 else if (pt.y - XINT (dy) - height >= 0)
2583 /* It fits below the pointer. */
2584 *root_y = pt.y - height - XINT (dy);
2585 else if (pt.y + XINT (dy) + height
2586 <= x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)))
2587 /* It fits above the pointer */
2588 *root_y = pt.y + XINT (dy);
2590 /* Put it on the top. */
2591 *root_y = x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)) - height;
2595 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2596 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2597 A tooltip window is a small window displaying a string.
2599 This is an internal function; Lisp code should call `tooltip-show'.
2601 FRAME nil or omitted means use the selected frame.
2603 PARMS is an optional list of frame parameters which can be used to
2604 change the tooltip's appearance.
2606 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2607 means use the default timeout of 5 seconds.
2609 If the list of frame parameters PARMS contains a `left' parameter,
2610 the tooltip is displayed at that x-position. Otherwise it is
2611 displayed at the mouse position, with offset DX added (default is 5 if
2612 DX isn't specified). Likewise for the y-position; if a `top' frame
2613 parameter is specified, it determines the y-position of the tooltip
2614 window, otherwise it is displayed at the mouse position, with offset
2615 DY added (default is -10).
2617 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2618 Text larger than the specified size is clipped. */)
2619 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2622 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2623 ptrdiff_t count = SPECPDL_INDEX ();
2628 specbind (Qinhibit_redisplay, Qt);
2630 GCPRO4 (string, parms, frame, timeout);
2632 CHECK_STRING (string);
2633 str = SSDATA (string);
2634 f = decode_window_system_frame (frame);
2636 timeout = make_number (5);
2638 CHECK_NATNUM (timeout);
2641 dx = make_number (5);
2646 dy = make_number (-10);
2651 if (ns_tooltip == nil)
2652 ns_tooltip = [[EmacsTooltip alloc] init];
2656 [ns_tooltip setText: str];
2657 size = [ns_tooltip frame].size;
2659 /* Move the tooltip window where the mouse pointer is. Resize and
2661 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2664 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2668 return unbind_to (count, Qnil);
2672 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2673 doc: /* Hide the current tooltip window, if there is any.
2674 Value is t if tooltip was open, nil otherwise. */)
2677 if (ns_tooltip == nil || ![ns_tooltip isActive])
2684 /* ==========================================================================
2686 Class implementations
2688 ========================================================================== */
2691 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
2692 Return YES if handled, NO if not.
2695 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
2701 if ([theEvent type] != NSKeyDown) return NO;
2702 s = [theEvent characters];
2704 for (i = 0; i < [s length]; ++i)
2706 int ch = (int) [s characterAtIndex: i];
2709 case NSHomeFunctionKey:
2710 case NSDownArrowFunctionKey:
2711 case NSUpArrowFunctionKey:
2712 case NSLeftArrowFunctionKey:
2713 case NSRightArrowFunctionKey:
2714 case NSPageUpFunctionKey:
2715 case NSPageDownFunctionKey:
2716 case NSEndFunctionKey:
2717 [panel sendEvent: theEvent];
2720 /* As we don't have the standard key commands for
2721 copy/paste/cut/select-all in our edit menu, we must handle
2722 them here. TODO: handle Emacs key bindings for copy/cut/select-all
2723 here, paste works, because we have that in our Edit menu.
2724 I.e. refactor out code in nsterm.m, keyDown: to figure out the
2730 case 'a': // Select all
2731 if ([theEvent modifierFlags] & NSCommandKeyMask)
2740 : @selector(selectAll:))))
2745 // Send all control keys, as the text field supports C-a, C-f, C-e
2747 if ([theEvent modifierFlags] & NSControlKeyMask)
2749 [panel sendEvent: theEvent];
2760 @implementation EmacsSavePanel
2761 #ifdef NS_IMPL_COCOA
2762 /* --------------------------------------------------------------------------
2763 These are overridden to intercept on OS X: ending panel restarts NSApp
2764 event loop if it is stopped. Not sure if this is correct behavior,
2765 perhaps should check if running and if so send an appdefined.
2766 -------------------------------------------------------------------------- */
2767 - (void) ok: (id)sender
2773 - (void) cancel: (id)sender
2775 [super cancel: sender];
2779 - (NSString *) getFilename
2781 return ns_filename_from_panel (self);
2783 - (NSString *) getDirectory
2785 return ns_directory_from_panel (self);
2788 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
2790 BOOL ret = handlePanelKeys (self, theEvent);
2792 ret = [super performKeyEquivalent:theEvent];
2798 @implementation EmacsOpenPanel
2799 #ifdef NS_IMPL_COCOA
2800 /* --------------------------------------------------------------------------
2801 These are overridden to intercept on OS X: ending panel restarts NSApp
2802 event loop if it is stopped. Not sure if this is correct behavior,
2803 perhaps should check if running and if so send an appdefined.
2804 -------------------------------------------------------------------------- */
2805 - (void) ok: (id)sender
2809 // If not choosing directories, and Open is pressed on a directory, return.
2810 if (! [self canChooseDirectories] && [self getDirectory] &&
2811 ! [self getFilename])
2817 - (void) cancel: (id)sender
2819 [super cancel: sender];
2824 - (NSString *) getFilename
2826 return ns_filename_from_panel (self);
2828 - (NSString *) getDirectory
2830 return ns_directory_from_panel (self);
2832 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
2834 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
2835 BOOL ret = handlePanelKeys (self, theEvent);
2837 ret = [super performKeyEquivalent:theEvent];
2843 @implementation EmacsFileDelegate
2844 /* --------------------------------------------------------------------------
2845 Delegate methods for Open/Save panels
2846 -------------------------------------------------------------------------- */
2847 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
2851 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
2855 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
2856 confirmed: (BOOL)okFlag
2865 /* ==========================================================================
2867 Lisp interface declaration
2869 ========================================================================== */
2873 syms_of_nsfns (void)
2875 Qfontsize = intern_c_string ("fontsize");
2876 staticpro (&Qfontsize);
2878 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
2879 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
2880 If the title of a frame matches REGEXP, then IMAGE.tiff is
2881 selected as the image of the icon representing the frame when it's
2882 miniaturized. If an element is t, then Emacs tries to select an icon
2883 based on the filetype of the visited file.
2885 The images have to be installed in a folder called English.lproj in the
2886 Emacs folder. You have to restart Emacs after installing new icons.
2888 Example: Install an icon Gnus.tiff and execute the following code
2890 (setq ns-icon-type-alist
2891 (append ns-icon-type-alist
2892 '((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
2895 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
2896 be used as the image of the icon representing the frame. */);
2897 Vns_icon_type_alist = Fcons (Qt, Qnil);
2899 DEFVAR_LISP ("ns-version-string", Vns_version_string,
2900 doc: /* Toolkit version for NS Windowing. */);
2901 Vns_version_string = ns_appkit_version_str ();
2903 defsubr (&Sns_read_file_name);
2904 defsubr (&Sns_get_resource);
2905 defsubr (&Sns_set_resource);
2906 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
2907 defsubr (&Sx_display_grayscale_p);
2908 defsubr (&Sns_font_name);
2909 defsubr (&Sns_list_colors);
2910 #ifdef NS_IMPL_COCOA
2911 defsubr (&Sns_do_applescript);
2913 defsubr (&Sxw_color_defined_p);
2914 defsubr (&Sxw_color_values);
2915 defsubr (&Sx_server_max_request_size);
2916 defsubr (&Sx_server_vendor);
2917 defsubr (&Sx_server_version);
2918 defsubr (&Sx_display_pixel_width);
2919 defsubr (&Sx_display_pixel_height);
2920 defsubr (&Sns_display_monitor_attributes_list);
2921 defsubr (&Sx_display_mm_width);
2922 defsubr (&Sx_display_mm_height);
2923 defsubr (&Sx_display_screens);
2924 defsubr (&Sx_display_planes);
2925 defsubr (&Sx_display_color_cells);
2926 defsubr (&Sx_display_visual_class);
2927 defsubr (&Sx_display_backing_store);
2928 defsubr (&Sx_display_save_under);
2929 defsubr (&Sx_create_frame);
2930 defsubr (&Sx_open_connection);
2931 defsubr (&Sx_close_connection);
2932 defsubr (&Sx_display_list);
2934 defsubr (&Sns_hide_others);
2935 defsubr (&Sns_hide_emacs);
2936 defsubr (&Sns_emacs_info_panel);
2937 defsubr (&Sns_list_services);
2938 defsubr (&Sns_perform_service);
2939 defsubr (&Sns_convert_utf8_nfd_to_nfc);
2940 defsubr (&Sx_focus_frame);
2941 defsubr (&Sns_popup_font_panel);
2942 defsubr (&Sns_popup_color_panel);
2944 defsubr (&Sx_show_tip);
2945 defsubr (&Sx_hide_tip);