1 /* Functions for the NeXT/Open/GNUstep and MacOSX window system.
3 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2015 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>
53 int fns_trace_num = 1;
54 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
55 __FILE__, __LINE__, ++fns_trace_num)
62 extern NSArray *ns_send_types, *ns_return_types, *ns_drag_types;
64 EmacsTooltip *ns_tooltip = nil;
66 /* Need forward declaration here to preserve organizational integrity of file */
67 Lisp_Object Fx_open_connection (Lisp_Object, Lisp_Object, Lisp_Object);
69 /* Static variables to handle applescript execution. */
70 static Lisp_Object as_script, *as_result;
74 static ptrdiff_t image_cache_refcount;
78 /* ==========================================================================
80 Internal utility functions
82 ========================================================================== */
84 /* Let the user specify a Nextstep display with a Lisp object.
85 OBJECT may be nil, a frame or a terminal object.
86 nil stands for the selected frame--or, if that is not a Nextstep frame,
87 the first Nextstep display on the list. */
89 static struct ns_display_info *
90 check_ns_display_info (Lisp_Object object)
92 struct ns_display_info *dpyinfo = NULL;
96 struct frame *sf = XFRAME (selected_frame);
98 if (FRAME_NS_P (sf) && FRAME_LIVE_P (sf))
99 dpyinfo = FRAME_DISPLAY_INFO (sf);
100 else if (x_display_list != 0)
101 dpyinfo = x_display_list;
103 error ("Nextstep windows are not in use or not initialized");
105 else if (TERMINALP (object))
107 struct terminal *t = decode_live_terminal (object);
109 if (t->type != output_ns)
110 error ("Terminal %d is not a Nextstep display", t->id);
112 dpyinfo = t->display_info.ns;
114 else if (STRINGP (object))
115 dpyinfo = ns_display_info_for_name (object);
118 struct frame *f = decode_window_system_frame (object);
119 dpyinfo = FRAME_DISPLAY_INFO (f);
127 ns_get_window (Lisp_Object maybeFrame)
129 id view =nil, window =nil;
131 if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
132 maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */
134 if (!NILP (maybeFrame))
135 view = FRAME_NS_VIEW (XFRAME (maybeFrame));
136 if (view) window =[view window];
142 /* Return the X display structure for the display named NAME.
143 Open a new connection if necessary. */
144 struct ns_display_info *
145 ns_display_info_for_name (Lisp_Object name)
147 struct ns_display_info *dpyinfo;
151 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
152 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
155 error ("Emacs for Nextstep does not yet support multi-display");
157 Fx_open_connection (name, Qnil, Qnil);
158 dpyinfo = x_display_list;
161 error ("Display on %s not responding.\n", SDATA (name));
167 ns_filename_from_panel (NSSavePanel *panel)
170 NSURL *url = [panel URL];
171 NSString *str = [url path];
174 return [panel filename];
179 ns_directory_from_panel (NSSavePanel *panel)
182 NSURL *url = [panel directoryURL];
183 NSString *str = [url path];
186 return [panel directory];
191 interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
192 /* --------------------------------------------------------------------------
193 Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
194 -------------------------------------------------------------------------- */
204 count = [menu numberOfItems];
205 for (i = 0; i<count; i++)
207 item = [menu itemAtIndex: i];
208 name = [[item title] UTF8String];
211 nameStr = build_string (name);
213 if ([item hasSubmenu])
215 old = interpret_services_menu ([item submenu],
216 Fcons (nameStr, prefix), old);
220 keys = [item keyEquivalent];
221 if (keys && [keys length] )
223 key = [keys characterAtIndex: 0];
224 res = make_number (key|super_modifier);
230 old = Fcons (Fcons (res,
231 Freverse (Fcons (nameStr,
241 /* ==========================================================================
243 Frame parameter setters
245 ========================================================================== */
249 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
252 EmacsCGFloat r, g, b, alpha;
254 /* Must block_input, because ns_lisp_to_color does block/unblock_input
255 which means that col may be deallocated in its unblock_input if there
256 is user input, unless we also block_input. */
258 if (ns_lisp_to_color (arg, &col))
260 store_frame_param (f, Qforeground_color, oldval);
262 error ("Unknown color");
266 [f->output_data.ns->foreground_color release];
267 f->output_data.ns->foreground_color = col;
269 [col getRed: &r green: &g blue: &b alpha: &alpha];
270 FRAME_FOREGROUND_PIXEL (f) =
271 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
273 if (FRAME_NS_VIEW (f))
275 update_face_from_frame_parameter (f, Qforeground_color, arg);
276 /*recompute_basic_faces (f); */
277 if (FRAME_VISIBLE_P (f))
278 SET_FRAME_GARBAGED (f);
285 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
289 NSView *view = FRAME_NS_VIEW (f);
290 EmacsCGFloat r, g, b, alpha;
293 if (ns_lisp_to_color (arg, &col))
295 store_frame_param (f, Qbackground_color, oldval);
297 error ("Unknown color");
300 /* clear the frame; in some instances the NS-internal GC appears not to
301 update, or it does update and cannot clear old text properly */
302 if (FRAME_VISIBLE_P (f))
306 [f->output_data.ns->background_color release];
307 f->output_data.ns->background_color = col;
309 [col getRed: &r green: &g blue: &b alpha: &alpha];
310 FRAME_BACKGROUND_PIXEL (f) =
311 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
315 [[view window] setBackgroundColor: col];
317 if (alpha != (EmacsCGFloat) 1.0)
318 [[view window] setOpaque: NO];
320 [[view window] setOpaque: YES];
322 face = FRAME_DEFAULT_FACE (f);
325 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
326 face->background = ns_index_color
327 ([col colorWithAlphaComponent: alpha], f);
329 update_face_from_frame_parameter (f, Qbackground_color, arg);
332 if (FRAME_VISIBLE_P (f))
333 SET_FRAME_GARBAGED (f);
340 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
345 if (ns_lisp_to_color (arg, &col))
347 store_frame_param (f, Qcursor_color, oldval);
349 error ("Unknown color");
352 [FRAME_CURSOR_COLOR (f) release];
353 FRAME_CURSOR_COLOR (f) = [col retain];
355 if (FRAME_VISIBLE_P (f))
357 x_update_cursor (f, 0);
358 x_update_cursor (f, 1);
360 update_face_from_frame_parameter (f, Qcursor_color, arg);
366 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
368 NSView *view = FRAME_NS_VIEW (f);
369 NSTRACE (x_set_icon_name);
371 /* see if it's changed */
374 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
377 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
380 fset_icon_name (f, arg);
384 if (!NILP (f->title))
387 /* Explicit name and no icon-name -> explicit_name. */
388 if (f->explicit_name)
392 /* No explicit name and no icon-name ->
393 name has to be rebuild from icon_title_format. */
394 windows_or_buffers_changed = 62;
399 /* Don't change the name if it's already NAME. */
400 if ([[view window] miniwindowTitle]
401 && ([[[view window] miniwindowTitle]
402 isEqualToString: [NSString stringWithUTF8String:
406 [[view window] setMiniwindowTitle:
407 [NSString stringWithUTF8String: SSDATA (arg)]];
411 ns_set_name_internal (struct frame *f, Lisp_Object name)
414 Lisp_Object encoded_name, encoded_icon_name;
416 NSView *view = FRAME_NS_VIEW (f);
419 encoded_name = ENCODE_UTF_8 (name);
422 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
424 /* Don't change the name if it's already NAME. */
425 if (! [[[view window] title] isEqualToString: str])
426 [[view window] setTitle: str];
428 if (!STRINGP (f->icon_name))
429 encoded_icon_name = encoded_name;
431 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
433 str = [NSString stringWithUTF8String: SSDATA (encoded_icon_name)];
435 if ([[view window] miniwindowTitle]
436 && ! [[[view window] miniwindowTitle] isEqualToString: str])
437 [[view window] setMiniwindowTitle: str];
442 ns_set_name (struct frame *f, Lisp_Object name, int explicit)
444 NSTRACE (ns_set_name);
446 /* Make sure that requests from lisp code override requests from
447 Emacs redisplay code. */
450 /* If we're switching from explicit to implicit, we had better
451 update the mode lines and thereby update the title. */
452 if (f->explicit_name && NILP (name))
453 update_mode_lines = 21;
455 f->explicit_name = ! NILP (name);
457 else if (f->explicit_name)
461 name = build_string ([ns_app_name UTF8String]);
465 /* Don't change the name if it's already NAME. */
466 if (! NILP (Fstring_equal (name, f->name)))
471 /* Title overrides explicit name. */
472 if (! NILP (f->title))
475 ns_set_name_internal (f, name);
479 /* This function should be called when the user's lisp code has
480 specified a name for the frame; the name will override any set by the
483 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
485 NSTRACE (x_explicitly_set_name);
486 ns_set_name (f, arg, 1);
490 /* This function should be called by Emacs redisplay code to set the
491 name; names set this way will never override names set by the user's
494 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
496 NSTRACE (x_implicitly_set_name);
498 /* Deal with NS specific format t. */
499 if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (Vicon_title_format, Qt))
500 || EQ (Vframe_title_format, Qt)))
501 ns_set_name_as_filename (f);
503 ns_set_name (f, arg, 0);
507 /* Change the title of frame F to NAME.
508 If NAME is nil, use the frame name as the title. */
511 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
513 NSTRACE (x_set_title);
514 /* Don't change the title if it's already NAME. */
515 if (EQ (name, f->title))
518 update_mode_lines = 22;
520 fset_title (f, name);
527 ns_set_name_internal (f, name);
532 ns_set_name_as_filename (struct frame *f)
535 Lisp_Object name, filename;
536 Lisp_Object buf = XWINDOW (f->selected_window)->contents;
538 NSAutoreleasePool *pool;
540 Lisp_Object encoded_name, encoded_filename;
542 NSTRACE (ns_set_name_as_filename);
544 if (f->explicit_name || ! NILP (f->title))
548 pool = [[NSAutoreleasePool alloc] init];
549 filename = BVAR (XBUFFER (buf), filename);
550 name = BVAR (XBUFFER (buf), name);
554 if (! NILP (filename))
555 name = Ffile_name_nondirectory (filename);
557 name = build_string ([ns_app_name UTF8String]);
561 encoded_name = ENCODE_UTF_8 (name);
564 view = FRAME_NS_VIEW (f);
566 title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
567 : [[[view window] title] UTF8String];
569 if (title && (! strcmp (title, SSDATA (encoded_name))))
576 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
577 if (str == nil) str = @"Bad coding";
579 if (FRAME_ICONIFIED_P (f))
580 [[view window] setMiniwindowTitle: str];
585 if (! NILP (filename))
588 encoded_filename = ENCODE_UTF_8 (filename);
591 fstr = [NSString stringWithUTF8String: SSDATA (encoded_filename)];
592 if (fstr == nil) fstr = @"";
597 ns_set_represented_filename (fstr, f);
598 [[view window] setTitle: str];
608 ns_set_doc_edited (void)
610 NSAutoreleasePool *pool;
611 Lisp_Object tail, frame;
613 pool = [[NSAutoreleasePool alloc] init];
614 FOR_EACH_FRAME (tail, frame)
617 struct frame *f = XFRAME (frame);
621 if (! FRAME_NS_P (f)) continue;
622 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
623 view = FRAME_NS_VIEW (f);
624 if (!MINI_WINDOW_P (w))
625 edited = ! NILP (Fbuffer_modified_p (w->contents)) &&
626 ! NILP (Fbuffer_file_name (w->contents));
627 [[view window] setDocumentEdited: edited];
636 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
639 if (FRAME_MINIBUF_ONLY_P (f))
642 if (TYPE_RANGED_INTEGERP (int, value))
643 nlines = XINT (value);
647 FRAME_MENU_BAR_LINES (f) = 0;
650 FRAME_EXTERNAL_MENU_BAR (f) = 1;
651 /* does for all frames, whereas we just want for one frame
652 [NSMenu setMenuBarVisible: YES]; */
656 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
657 free_frame_menubar (f);
658 /* [NSMenu setMenuBarVisible: NO]; */
659 FRAME_EXTERNAL_MENU_BAR (f) = 0;
664 /* toolbar support */
666 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
670 if (FRAME_MINIBUF_ONLY_P (f))
673 if (RANGED_INTEGERP (0, value, INT_MAX))
674 nlines = XFASTINT (value);
680 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
681 update_frame_tool_bar (f);
685 if (FRAME_EXTERNAL_TOOL_BAR (f))
687 free_frame_tool_bar (f);
688 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
692 x_set_window_size (f, 0, f->text_cols, f->text_lines, 0);
697 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
699 int old_width = FRAME_INTERNAL_BORDER_WIDTH (f);
701 CHECK_TYPE_RANGED_INTEGER (int, arg);
702 FRAME_INTERNAL_BORDER_WIDTH (f) = XINT (arg);
703 if (FRAME_INTERNAL_BORDER_WIDTH (f) < 0)
704 FRAME_INTERNAL_BORDER_WIDTH (f) = 0;
706 if (FRAME_INTERNAL_BORDER_WIDTH (f) == old_width)
709 if (FRAME_X_WINDOW (f) != 0)
710 adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
712 SET_FRAME_GARBAGED (f);
717 ns_implicitly_set_icon_type (struct frame *f)
720 EmacsView *view = FRAME_NS_VIEW (f);
722 Lisp_Object chain, elt;
723 NSAutoreleasePool *pool;
726 NSTRACE (ns_implicitly_set_icon_type);
729 pool = [[NSAutoreleasePool alloc] init];
730 if (f->output_data.ns->miniimage
731 && [[NSString stringWithUTF8String: SSDATA (f->name)]
732 isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
739 tem = assq_no_quit (Qicon_type, f->param_alist);
740 if (CONSP (tem) && ! NILP (XCDR (tem)))
747 for (chain = Vns_icon_type_alist;
748 image == nil && CONSP (chain);
749 chain = XCDR (chain))
752 /* special case: 't' means go by file type */
753 if (SYMBOLP (elt) && EQ (elt, Qt) && SSDATA (f->name)[0] == '/')
756 = [NSString stringWithUTF8String: SSDATA (f->name)];
757 if ([[NSFileManager defaultManager] fileExistsAtPath: str])
758 image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
760 else if (CONSP (elt) &&
761 STRINGP (XCAR (elt)) &&
762 STRINGP (XCDR (elt)) &&
763 fast_string_match (XCAR (elt), f->name) >= 0)
765 image = [EmacsImage allocInitFromFile: XCDR (elt)];
767 image = [[NSImage imageNamed:
768 [NSString stringWithUTF8String:
769 SSDATA (XCDR (elt))]] retain];
775 image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
779 [f->output_data.ns->miniimage release];
780 f->output_data.ns->miniimage = image;
781 [view setMiniwindowImage: setMini];
788 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
790 EmacsView *view = FRAME_NS_VIEW (f);
794 NSTRACE (x_set_icon_type);
796 if (!NILP (arg) && SYMBOLP (arg))
798 arg =build_string (SSDATA (SYMBOL_NAME (arg)));
799 store_frame_param (f, Qicon_type, arg);
802 /* do it the implicit way */
805 ns_implicitly_set_icon_type (f);
811 image = [EmacsImage allocInitFromFile: arg];
813 image =[NSImage imageNamed: [NSString stringWithUTF8String:
818 image = [NSImage imageNamed: @"text"];
822 f->output_data.ns->miniimage = image;
823 [view setMiniwindowImage: setMini];
827 /* TODO: move to nsterm? */
829 ns_lisp_to_cursor_type (Lisp_Object arg)
832 if (XTYPE (arg) == Lisp_String)
834 else if (XTYPE (arg) == Lisp_Symbol)
835 str = SSDATA (SYMBOL_NAME (arg));
837 if (!strcmp (str, "box")) return FILLED_BOX_CURSOR;
838 if (!strcmp (str, "hollow")) return HOLLOW_BOX_CURSOR;
839 if (!strcmp (str, "hbar")) return HBAR_CURSOR;
840 if (!strcmp (str, "bar")) return BAR_CURSOR;
841 if (!strcmp (str, "no")) return NO_CURSOR;
847 ns_cursor_type_to_lisp (int arg)
851 case FILLED_BOX_CURSOR: return Qbox;
852 case HOLLOW_BOX_CURSOR: return Qhollow;
853 case HBAR_CURSOR: return Qhbar;
854 case BAR_CURSOR: return Qbar;
856 default: return intern ("no");
860 /* This is the same as the xfns.c definition. */
862 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
864 set_frame_cursor_types (f, arg);
867 /* called to set mouse pointer color, but all other terms use it to
868 initialize pointer types (and don't set the color ;) */
870 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
872 /* don't think we can do this on Nextstep */
877 #define Xstr(x) Str(x)
880 ns_appkit_version_str (void)
884 #ifdef NS_IMPL_GNUSTEP
885 sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
886 #elif defined (NS_IMPL_COCOA)
888 = [[NSProcessInfo processInfo] operatingSystemVersionString];
889 sprintf(tmp, "appkit-%.2f %s",
890 NSAppKitVersionNumber,
891 [osversion UTF8String]);
895 return build_string (tmp);
899 /* This is for use by x-server-version and collapses all version info we
900 have into a single int. For a better picture of the implementation
901 running, use ns_appkit_version_str.*/
903 ns_appkit_version_int (void)
905 #ifdef NS_IMPL_GNUSTEP
906 return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
907 #elif defined (NS_IMPL_COCOA)
908 return (int)NSAppKitVersionNumber;
915 x_icon (struct frame *f, Lisp_Object parms)
916 /* --------------------------------------------------------------------------
917 Strangely-named function to set icon position parameters in frame.
918 This is irrelevant under OS X, but might be needed under GNUstep,
919 depending on the window manager used. Note, this is not a standard
920 frame parameter-setter; it is called directly from x-create-frame.
921 -------------------------------------------------------------------------- */
923 Lisp_Object icon_x, icon_y;
924 struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);
926 f->output_data.ns->icon_top = -1;
927 f->output_data.ns->icon_left = -1;
929 /* Set the position of the icon. */
930 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
931 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
932 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
934 CHECK_NUMBER (icon_x);
935 CHECK_NUMBER (icon_y);
936 f->output_data.ns->icon_top = XINT (icon_y);
937 f->output_data.ns->icon_left = XINT (icon_x);
939 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
940 error ("Both left and top icon corners of icon must be specified");
944 /* Note: see frame.c for template, also where generic functions are impl */
945 frame_parm_handler ns_frame_parm_handlers[] =
947 x_set_autoraise, /* generic OK */
948 x_set_autolower, /* generic OK */
949 x_set_background_color,
950 0, /* x_set_border_color, may be impossible under Nextstep */
951 0, /* x_set_border_width, may be impossible under Nextstep */
954 x_set_font, /* generic OK */
955 x_set_foreground_color,
958 x_set_internal_border_width, /* generic OK */
959 0, /* x_set_right_divider_width */
960 0, /* x_set_bottom_divider_width */
961 x_set_menu_bar_lines,
963 x_explicitly_set_name,
964 x_set_scroll_bar_width, /* generic OK */
965 x_set_scroll_bar_height, /* generic OK */
967 x_set_unsplittable, /* generic OK */
968 x_set_vertical_scroll_bars, /* generic OK */
969 x_set_horizontal_scroll_bars, /* generic OK */
970 x_set_visibility, /* generic OK */
971 x_set_tool_bar_lines,
972 0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
973 0, /* x_set_scroll_bar_background, will ignore (not possible on NS) */
974 x_set_screen_gamma, /* generic OK */
975 x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
976 x_set_left_fringe, /* generic OK */
977 x_set_right_fringe, /* generic OK */
978 0, /* x_set_wait_for_wm, will ignore */
979 x_set_fullscreen, /* generic OK */
980 x_set_font_backend, /* generic OK */
982 0, /* x_set_sticky */
983 0, /* x_set_tool_bar_position */
987 /* Handler for signals raised during x_create_frame.
988 FRAME is the frame which is partially constructed. */
991 unwind_create_frame (Lisp_Object frame)
993 struct frame *f = XFRAME (frame);
995 /* If frame is already dead, nothing to do. This can happen if the
996 display is disconnected after the frame has become official, but
997 before x_create_frame removes the unwind protect. */
998 if (!FRAME_LIVE_P (f))
1001 /* If frame is ``official'', nothing to do. */
1002 if (NILP (Fmemq (frame, Vframe_list)))
1004 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1005 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1008 x_free_frame_resources (f);
1012 /* Check that reference counts are indeed correct. */
1013 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 < ARRAYELTS (r); ++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 long 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 static int desc_ctr = 1;
1087 /* x_get_arg modifies parms. */
1088 parms = Fcopy_alist (parms);
1090 /* Use this general default value to start with
1091 until we know if this frame has a specified name. */
1092 Vx_resource_name = Vinvocation_name;
1094 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
1095 if (EQ (display, Qunbound))
1097 dpyinfo = check_ns_display_info (display);
1098 kb = dpyinfo->terminal->kboard;
1100 if (!dpyinfo->terminal->name)
1101 error ("Terminal is not live, can't create new frames on it");
1103 name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
1105 && ! EQ (name, Qunbound)
1107 error ("Invalid frame name--not a string or nil");
1110 Vx_resource_name = name;
1112 parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
1113 if (EQ (parent, Qunbound))
1115 if (! NILP (parent))
1116 CHECK_NUMBER (parent);
1118 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
1119 /* No need to protect DISPLAY because that's not used after passing
1120 it to make_frame_without_minibuffer. */
1122 GCPRO4 (parms, parent, name, frame);
1123 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
1125 if (EQ (tem, Qnone) || NILP (tem))
1126 f = make_frame_without_minibuffer (Qnil, kb, display);
1127 else if (EQ (tem, Qonly))
1129 f = make_minibuffer_frame ();
1130 minibuffer_only = 1;
1132 else if (WINDOWP (tem))
1133 f = make_frame_without_minibuffer (tem, kb, display);
1137 XSETFRAME (frame, f);
1139 f->terminal = dpyinfo->terminal;
1141 f->output_method = output_ns;
1142 f->output_data.ns = xzalloc (sizeof *f->output_data.ns);
1144 FRAME_FONTSET (f) = -1;
1146 fset_icon_name (f, x_get_arg (dpyinfo, parms, Qicon_name,
1147 "iconName", "Title",
1149 if (! STRINGP (f->icon_name))
1150 fset_icon_name (f, Qnil);
1152 FRAME_DISPLAY_INFO (f) = dpyinfo;
1154 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
1155 record_unwind_protect (unwind_create_frame, frame);
1157 f->output_data.ns->window_desc = desc_ctr++;
1158 if (TYPE_RANGED_INTEGERP (Window, parent))
1160 f->output_data.ns->parent_desc = XFASTINT (parent);
1161 f->output_data.ns->explicit_parent = 1;
1165 f->output_data.ns->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
1166 f->output_data.ns->explicit_parent = 0;
1169 /* Set the name; the functions to which we pass f expect the name to
1171 if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
1173 fset_name (f, build_string ([ns_app_name UTF8String]));
1174 f->explicit_name = 0;
1178 fset_name (f, name);
1179 f->explicit_name = 1;
1180 specbind (Qx_resource_name, name);
1185 #ifdef NS_IMPL_COCOA
1186 mac_register_font_driver (f);
1188 register_font_driver (&nsfont_driver, f);
1191 x_default_parameter (f, parms, Qfont_backend, Qnil,
1192 "fontBackend", "FontBackend", RES_TYPE_STRING);
1195 /* use for default font name */
1196 id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
1197 x_default_parameter (f, parms, Qfontsize,
1198 make_number (0 /*(int)[font pointSize]*/),
1199 "fontSize", "FontSize", RES_TYPE_NUMBER);
1200 // Remove ' Regular', not handled by backends.
1201 char *fontname = xstrdup ([[font displayName] UTF8String]);
1202 int len = strlen (fontname);
1203 if (len > 8 && strcmp (fontname + len - 8, " Regular") == 0)
1204 fontname[len-8] = '\0';
1205 x_default_parameter (f, parms, Qfont,
1206 build_string (fontname),
1207 "font", "Font", RES_TYPE_STRING);
1212 x_default_parameter (f, parms, Qborder_width, make_number (0),
1213 "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
1214 x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
1215 "internalBorderWidth", "InternalBorderWidth",
1218 /* default vertical scrollbars on right on Mac */
1221 #ifdef NS_IMPL_GNUSTEP
1226 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1227 "verticalScrollBars", "VerticalScrollBars",
1230 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
1231 "horizontalScrollBars", "HorizontalScrollBars",
1233 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1234 "foreground", "Foreground", RES_TYPE_STRING);
1235 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1236 "background", "Background", RES_TYPE_STRING);
1237 /* FIXME: not supported yet in Nextstep */
1238 x_default_parameter (f, parms, Qline_spacing, Qnil,
1239 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1240 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1241 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1242 x_default_parameter (f, parms, Qright_fringe, Qnil,
1243 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1246 image_cache_refcount =
1247 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
1250 init_frame_faces (f);
1252 /* Read comment about this code in corresponding place in xfns.c. */
1253 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
1254 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1,
1257 /* The resources controlling the menu-bar and tool-bar are
1258 processed specially at startup, and reflected in the mode
1259 variables; ignore them here. */
1260 x_default_parameter (f, parms, Qmenu_bar_lines,
1261 NILP (Vmenu_bar_mode)
1262 ? make_number (0) : make_number (1),
1263 NULL, NULL, RES_TYPE_NUMBER);
1264 x_default_parameter (f, parms, Qtool_bar_lines,
1265 NILP (Vtool_bar_mode)
1266 ? make_number (0) : make_number (1),
1267 NULL, NULL, RES_TYPE_NUMBER);
1269 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1270 "BufferPredicate", RES_TYPE_SYMBOL);
1271 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1274 parms = get_geometry_from_preferences (dpyinfo, parms);
1275 window_prompting = x_figure_window_size (f, parms, 1);
1277 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1278 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1280 /* NOTE: on other terms, this is done in set_mouse_color, however this
1281 was not getting called under Nextstep */
1282 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1283 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1284 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1285 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1286 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1287 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1288 f->output_data.ns->vertical_drag_cursor = [NSCursor resizeUpDownCursor];
1289 FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1290 = [NSCursor arrowCursor];
1291 FRAME_DISPLAY_INFO (f)->horizontal_scroll_bar_cursor
1292 = [NSCursor arrowCursor];
1293 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1295 [[EmacsView alloc] initFrameFromEmacs: f];
1299 /* ns_display_info does not have a reference_count. */
1300 f->terminal->reference_count++;
1302 /* It is now ok to make the frame official even if we get an error below.
1303 The frame needs to be on Vframe_list or making it visible won't work. */
1304 Vframe_list = Fcons (frame, Vframe_list);
1306 x_default_parameter (f, parms, Qicon_type, Qnil,
1307 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1309 x_default_parameter (f, parms, Qauto_raise, Qnil,
1310 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1311 x_default_parameter (f, parms, Qauto_lower, Qnil,
1312 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1313 x_default_parameter (f, parms, Qcursor_type, Qbox,
1314 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1315 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1316 "scrollBarWidth", "ScrollBarWidth",
1318 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
1319 "scrollBarHeight", "ScrollBarHeight",
1321 x_default_parameter (f, parms, Qalpha, Qnil,
1322 "alpha", "Alpha", RES_TYPE_NUMBER);
1323 x_default_parameter (f, parms, Qfullscreen, Qnil,
1324 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1326 /* Allow x_set_window_size, now. */
1327 f->can_x_set_window_size = true;
1329 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1,
1332 if (! f->output_data.ns->explicit_parent)
1334 Lisp_Object visibility;
1336 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1338 if (EQ (visibility, Qunbound))
1341 if (EQ (visibility, Qicon))
1342 x_iconify_frame (f);
1343 else if (! NILP (visibility))
1345 x_make_frame_visible (f);
1346 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1350 /* Must have been Qnil. */
1354 if (FRAME_HAS_MINIBUF_P (f)
1355 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1356 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1357 kset_default_minibuffer_frame (kb, frame);
1359 /* All remaining specified parameters, which have not been "used"
1360 by x_get_arg and friends, now go in the misc. alist of the frame. */
1361 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1362 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1363 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1367 if (window_prompting & USPosition)
1368 x_set_offset (f, f->left_pos, f->top_pos, 1);
1370 /* Make sure windows on this frame appear in calls to next-window
1371 and similar functions. */
1372 Vwindow_list = Qnil;
1374 return unbind_to (count, frame);
1378 x_focus_frame (struct frame *f)
1380 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1382 if (dpyinfo->x_focus_frame != f)
1384 EmacsView *view = FRAME_NS_VIEW (f);
1386 [NSApp activateIgnoringOtherApps: YES];
1387 [[view window] makeKeyAndOrderFront: view];
1393 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1395 doc: /* Pop up the font panel. */)
1398 struct frame *f = decode_window_system_frame (frame);
1399 id fm = [NSFontManager sharedFontManager];
1400 struct font *font = f->output_data.ns->font;
1402 #ifdef NS_IMPL_GNUSTEP
1403 nsfont = ((struct nsfont_info *)font)->nsfont;
1405 #ifdef NS_IMPL_COCOA
1406 nsfont = (NSFont *) macfont_get_nsctfont (font);
1408 [fm setSelectedFont: nsfont isMultiple: NO];
1409 [fm orderFrontFontPanel: NSApp];
1414 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1416 doc: /* Pop up the color panel. */)
1419 check_window_system (NULL);
1420 [NSApp orderFrontColorPanel: NSApp];
1428 #ifdef NS_IMPL_GNUSTEP
1429 NSString *dirS, *initS;
1435 ns_run_file_dialog (void)
1437 if (ns_fd_data.panel == nil) return;
1438 #ifdef NS_IMPL_COCOA
1439 ns_fd_data.ret = [ns_fd_data.panel runModal];
1441 if (ns_fd_data.no_types)
1443 ns_fd_data.ret = [ns_fd_data.panel
1444 runModalForDirectory: ns_fd_data.dirS
1445 file: ns_fd_data.initS];
1449 ns_fd_data.ret = [ns_fd_data.panel
1450 runModalForDirectory: ns_fd_data.dirS
1451 file: ns_fd_data.initS
1455 ns_fd_data.panel = nil;
1458 #ifdef NS_IMPL_COCOA
1459 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_9
1460 #define MODAL_OK_RESPONSE NSModalResponseOK
1463 #ifndef MODAL_OK_RESPONSE
1464 #define MODAL_OK_RESPONSE NSOKButton
1467 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1468 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1469 Optional arg DIR, if non-nil, supplies a default directory.
1470 Optional arg MUSTMATCH, if non-nil, means the returned file or
1471 directory must exist.
1472 Optional arg INIT, if non-nil, provides a default file name to use.
1473 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1474 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1475 Lisp_Object init, Lisp_Object dir_only_p)
1477 static id fileDelegate = nil;
1479 BOOL isSave = NILP (mustmatch) && NILP (dir_only_p);
1483 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1484 [NSString stringWithUTF8String: SSDATA (prompt)];
1485 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1486 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1487 [NSString stringWithUTF8String: SSDATA (dir)];
1488 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1489 [NSString stringWithUTF8String: SSDATA (init)];
1492 check_window_system (NULL);
1494 if (fileDelegate == nil)
1495 fileDelegate = [EmacsFileDelegate new];
1497 [NSCursor setHiddenUntilMouseMoves: NO];
1499 if ([dirS characterAtIndex: 0] == '~')
1500 dirS = [dirS stringByExpandingTildeInPath];
1503 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1505 [panel setTitle: promptS];
1507 [panel setAllowsOtherFileTypes: YES];
1508 [panel setTreatsFilePackagesAsDirectories: YES];
1509 [panel setDelegate: fileDelegate];
1511 if (! NILP (dir_only_p))
1513 [panel setCanChooseDirectories: YES];
1514 [panel setCanChooseFiles: NO];
1518 /* This is not quite what the documentation says, but it is compatible
1519 with the Gtk+ code. Also, the menu entry says "Open File...". */
1520 [panel setCanChooseDirectories: NO];
1521 [panel setCanChooseFiles: YES];
1525 ns_fd_data.panel = panel;
1526 ns_fd_data.ret = NO;
1527 #ifdef NS_IMPL_COCOA
1528 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1529 [panel setAllowedFileTypes: nil];
1530 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1531 if (initS && NILP (Ffile_directory_p (init)))
1532 [panel setNameFieldStringValue: [initS lastPathComponent]];
1534 [panel setNameFieldStringValue: @""];
1537 ns_fd_data.no_types = NILP (mustmatch) && NILP (dir_only_p);
1538 ns_fd_data.dirS = dirS;
1539 ns_fd_data.initS = initS;
1542 /* runModalForDirectory/runModal restarts the main event loop when done,
1543 so we must start an event loop and then pop up the file dialog.
1544 The file dialog may pop up a confirm dialog after Ok has been pressed,
1545 so we can not simply pop down on the Ok/Cancel press.
1547 nxev = [NSEvent otherEventWithType: NSApplicationDefined
1548 location: NSMakePoint (0, 0)
1551 windowNumber: [[NSApp mainWindow] windowNumber]
1552 context: [NSApp context]
1555 data2: NSAPP_DATA2_RUNFILEDIALOG];
1557 [NSApp postEvent: nxev atStart: NO];
1558 while (ns_fd_data.panel != nil)
1561 ret = (ns_fd_data.ret == MODAL_OK_RESPONSE);
1565 NSString *str = ns_filename_from_panel (panel);
1566 if (! str) str = ns_directory_from_panel (panel);
1567 if (! str) ret = NO;
1568 else fname = build_string ([str UTF8String]);
1571 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1574 return ret ? fname : Qnil;
1578 ns_get_defaults_value (const char *key)
1580 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1581 objectForKey: [NSString stringWithUTF8String: key]];
1583 if (!obj) return NULL;
1585 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1589 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1590 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1591 If OWNER is nil, Emacs is assumed. */)
1592 (Lisp_Object owner, Lisp_Object name)
1596 check_window_system (NULL);
1598 owner = build_string([ns_app_name UTF8String]);
1599 CHECK_STRING (name);
1601 value = ns_get_defaults_value (SSDATA (name));
1604 return build_string (value);
1609 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1610 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1611 If OWNER is nil, Emacs is assumed.
1612 If VALUE is nil, the default is removed. */)
1613 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1615 check_window_system (NULL);
1617 owner = build_string ([ns_app_name UTF8String]);
1618 CHECK_STRING (name);
1621 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1622 [NSString stringWithUTF8String: SSDATA (name)]];
1626 CHECK_STRING (value);
1627 [[NSUserDefaults standardUserDefaults] setObject:
1628 [NSString stringWithUTF8String: SSDATA (value)]
1629 forKey: [NSString stringWithUTF8String:
1637 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1638 Sx_server_max_request_size,
1640 doc: /* This function is a no-op. It is only present for completeness. */)
1641 (Lisp_Object terminal)
1643 check_ns_display_info (terminal);
1644 /* This function has no real equivalent under NeXTstep. Return nil to
1650 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1651 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1652 \(Labeling every distributor as a "vendor" embodies the false assumption
1653 that operating systems cannot be developed and distributed noncommercially.)
1654 The optional argument TERMINAL specifies which display to ask about.
1655 TERMINAL should be a terminal object, a frame or a display name (a string).
1656 If omitted or nil, that stands for the selected frame's display. */)
1657 (Lisp_Object terminal)
1659 check_ns_display_info (terminal);
1660 #ifdef NS_IMPL_GNUSTEP
1661 return build_string ("GNU");
1663 return build_string ("Apple");
1668 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1669 doc: /* Return the version numbers of the server of display TERMINAL.
1670 The value is a list of three integers: the major and minor
1671 version numbers of the X Protocol in use, and the distributor-specific release
1672 number. See also the function `x-server-vendor'.
1674 The optional argument TERMINAL specifies which display to ask about.
1675 TERMINAL should be a terminal object, a frame or a display name (a string).
1676 If omitted or nil, that stands for the selected frame's display. */)
1677 (Lisp_Object terminal)
1679 check_ns_display_info (terminal);
1680 /*NOTE: it is unclear what would best correspond with "protocol";
1681 we return 10.3, meaning Panther, since this is roughly the
1682 level that GNUstep's APIs correspond to.
1683 The last number is where we distinguish between the Apple
1684 and GNUstep implementations ("distributor-specific release
1685 number") and give int'ized versions of major.minor. */
1686 return list3i (10, 3, ns_appkit_version_int ());
1690 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1691 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1692 The optional argument TERMINAL specifies which display to ask about.
1693 TERMINAL should be a terminal object, a frame or a display name (a string).
1694 If omitted or nil, that stands for the selected frame's display.
1696 Note: "screen" here is not in Nextstep terminology but in X11's. For
1697 the number of physical monitors, use `(length
1698 (display-monitor-attributes-list TERMINAL))' instead. */)
1699 (Lisp_Object terminal)
1701 check_ns_display_info (terminal);
1702 return make_number (1);
1706 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1707 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1708 The optional argument TERMINAL specifies which display to ask about.
1709 TERMINAL should be a terminal object, a frame or a display name (a string).
1710 If omitted or nil, that stands for the selected frame's display.
1712 On \"multi-monitor\" setups this refers to the height in millimeters for
1713 all physical monitors associated with TERMINAL. To get information
1714 for each physical monitor, use `display-monitor-attributes-list'. */)
1715 (Lisp_Object terminal)
1717 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1719 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1723 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1724 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1725 The optional argument TERMINAL specifies which display to ask about.
1726 TERMINAL should be a terminal object, a frame or a display name (a string).
1727 If omitted or nil, that stands for the selected frame's display.
1729 On \"multi-monitor\" setups this refers to the width in millimeters for
1730 all physical monitors associated with TERMINAL. To get information
1731 for each physical monitor, use `display-monitor-attributes-list'. */)
1732 (Lisp_Object terminal)
1734 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1736 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1740 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1741 Sx_display_backing_store, 0, 1, 0,
1742 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1743 The value may be `buffered', `retained', or `non-retained'.
1744 The optional argument TERMINAL specifies which display to ask about.
1745 TERMINAL should be a terminal object, a frame or a display name (a string).
1746 If omitted or nil, that stands for the selected frame's display. */)
1747 (Lisp_Object terminal)
1749 check_ns_display_info (terminal);
1750 switch ([ns_get_window (terminal) backingType])
1752 case NSBackingStoreBuffered:
1753 return intern ("buffered");
1754 case NSBackingStoreRetained:
1755 return intern ("retained");
1756 case NSBackingStoreNonretained:
1757 return intern ("non-retained");
1759 error ("Strange value for backingType parameter of frame");
1761 return Qnil; /* not reached, shut compiler up */
1765 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1766 Sx_display_visual_class, 0, 1, 0,
1767 doc: /* Return the visual class of the Nextstep display TERMINAL.
1768 The value is one of the symbols `static-gray', `gray-scale',
1769 `static-color', `pseudo-color', `true-color', or `direct-color'.
1771 The optional argument TERMINAL specifies which display to ask about.
1772 TERMINAL should a terminal object, a frame or a display name (a string).
1773 If omitted or nil, that stands for the selected frame's display. */)
1774 (Lisp_Object terminal)
1776 NSWindowDepth depth;
1778 check_ns_display_info (terminal);
1779 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1781 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1782 return intern ("static-gray");
1783 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1784 return intern ("gray-scale");
1785 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1786 return intern ("pseudo-color");
1787 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1788 return intern ("true-color");
1789 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1790 return intern ("direct-color");
1792 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1793 return intern ("direct-color");
1797 DEFUN ("x-display-save-under", Fx_display_save_under,
1798 Sx_display_save_under, 0, 1, 0,
1799 doc: /* Return t if TERMINAL supports the save-under feature.
1800 The optional argument TERMINAL specifies which display to ask about.
1801 TERMINAL should be a terminal object, a frame or a display name (a string).
1802 If omitted or nil, that stands for the selected frame's display. */)
1803 (Lisp_Object terminal)
1805 check_ns_display_info (terminal);
1806 switch ([ns_get_window (terminal) backingType])
1808 case NSBackingStoreBuffered:
1811 case NSBackingStoreRetained:
1812 case NSBackingStoreNonretained:
1816 error ("Strange value for backingType parameter of frame");
1818 return Qnil; /* not reached, shut compiler up */
1822 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1824 doc: /* Open a connection to a display server.
1825 DISPLAY is the name of the display to connect to.
1826 Optional second arg XRM-STRING is a string of resources in xrdb format.
1827 If the optional third arg MUST-SUCCEED is non-nil,
1828 terminate Emacs if we can't open the connection.
1829 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1830 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1832 struct ns_display_info *dpyinfo;
1834 CHECK_STRING (display);
1836 nxatoms_of_nsselect ();
1837 dpyinfo = ns_term_init (display);
1840 if (!NILP (must_succeed))
1841 fatal ("Display on %s not responding.\n",
1844 error ("Display on %s not responding.\n",
1852 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1854 doc: /* Close the connection to TERMINAL's Nextstep display server.
1855 For TERMINAL, specify a terminal object, a frame or a display name (a
1856 string). If TERMINAL is nil, that stands for the selected frame's
1858 (Lisp_Object terminal)
1860 check_ns_display_info (terminal);
1861 [NSApp terminate: NSApp];
1866 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
1867 doc: /* Return the list of display names that Emacs has connections to. */)
1870 Lisp_Object result = Qnil;
1871 struct ns_display_info *ndi;
1873 for (ndi = x_display_list; ndi; ndi = ndi->next)
1874 result = Fcons (XCAR (ndi->name_list_element), result);
1880 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
1882 doc: /* Hides all applications other than Emacs. */)
1885 check_window_system (NULL);
1886 [NSApp hideOtherApplications: NSApp];
1890 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
1892 doc: /* If ON is non-nil, the entire Emacs application is hidden.
1893 Otherwise if Emacs is hidden, it is unhidden.
1894 If ON is equal to `activate', Emacs is unhidden and becomes
1895 the active application. */)
1898 check_window_system (NULL);
1899 if (EQ (on, intern ("activate")))
1901 [NSApp unhide: NSApp];
1902 [NSApp activateIgnoringOtherApps: YES];
1905 [NSApp unhide: NSApp];
1907 [NSApp hide: NSApp];
1912 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
1914 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
1917 check_window_system (NULL);
1918 [NSApp orderFrontStandardAboutPanel: nil];
1923 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
1924 doc: /* Determine font PostScript or family name for font NAME.
1925 NAME should be a string containing either the font name or an XLFD
1926 font descriptor. If string contains `fontset' and not
1927 `fontset-startup', it is left alone. */)
1931 CHECK_STRING (name);
1936 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
1939 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
1943 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
1944 doc: /* Return a list of all available colors.
1945 The optional argument FRAME is currently ignored. */)
1948 Lisp_Object list = Qnil;
1949 NSEnumerator *colorlists;
1954 CHECK_FRAME (frame);
1955 if (! FRAME_NS_P (XFRAME (frame)))
1956 error ("non-Nextstep frame used in `ns-list-colors'");
1961 colorlists = [[NSColorList availableColorLists] objectEnumerator];
1962 while ((clist = [colorlists nextObject]))
1964 if ([[clist name] length] < 7 ||
1965 [[clist name] rangeOfString: @"PANTONE"].location == 0)
1967 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
1969 while ((cname = [cnames nextObject]))
1970 list = Fcons (build_string ([cname UTF8String]), list);
1971 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
1972 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
1973 UTF8String]), list); */
1983 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
1984 doc: /* List available Nextstep services by querying NSApp. */)
1987 #ifdef NS_IMPL_COCOA
1988 /* You can't get services like this in 10.6+. */
1991 Lisp_Object ret = Qnil;
1993 #ifdef NS_IMPL_COCOA
1997 check_window_system (NULL);
1998 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
1999 [NSApp setServicesMenu: svcs];
2000 [NSApp registerServicesMenuSendTypes: ns_send_types
2001 returnTypes: ns_return_types];
2003 /* On Tiger, services menu updating was made lazier (waits for user to
2004 actually click on the menu), so we have to force things along: */
2005 #ifdef NS_IMPL_COCOA
2006 delegate = [svcs delegate];
2007 if (delegate != nil)
2009 if ([delegate respondsToSelector: @selector (menuNeedsUpdate:)])
2010 [delegate menuNeedsUpdate: svcs];
2011 if ([delegate respondsToSelector:
2012 @selector (menu:updateItem:atIndex:shouldCancel:)])
2014 int i, len = [delegate numberOfItemsInMenu: svcs];
2015 for (i =0; i<len; i++)
2016 [svcs addItemWithTitle: @"" action: NULL keyEquivalent: @""];
2017 for (i =0; i<len; i++)
2018 if (![delegate menu: svcs
2019 updateItem: (NSMenuItem *)[svcs itemAtIndex: i]
2020 atIndex: i shouldCancel: NO])
2026 [svcs setAutoenablesItems: NO];
2027 #ifdef NS_IMPL_COCOA
2028 [svcs update]; /* on OS X, converts from '/' structure */
2031 ret = interpret_services_menu (svcs, Qnil, ret);
2037 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2039 doc: /* Perform Nextstep SERVICE on SEND.
2040 SEND should be either a string or nil.
2041 The return value is the result of the service, as string, or nil if
2042 there was no result. */)
2043 (Lisp_Object service, Lisp_Object send)
2049 CHECK_STRING (service);
2050 check_window_system (NULL);
2052 utfStr = SSDATA (service);
2053 svcName = [NSString stringWithUTF8String: utfStr];
2055 pb =[NSPasteboard pasteboardWithUniqueName];
2056 ns_string_to_pasteboard (pb, send);
2058 if (NSPerformService (svcName, pb) == NO)
2059 Fsignal (Qquit, list1 (build_string ("service not available")));
2061 if ([[pb types] count] == 0)
2062 return build_string ("");
2063 return ns_string_from_pasteboard (pb);
2067 DEFUN ("ns-convert-utf8-nfd-to-nfc", Fns_convert_utf8_nfd_to_nfc,
2068 Sns_convert_utf8_nfd_to_nfc, 1, 1, 0,
2069 doc: /* Return an NFC string that matches the UTF-8 NFD string STR. */)
2072 /* TODO: If GNUstep ever implements precomposedStringWithCanonicalMapping,
2075 Lisp_Object ret = Qnil;
2076 NSAutoreleasePool *pool;
2079 pool = [[NSAutoreleasePool alloc] init];
2080 utfStr = [NSString stringWithUTF8String: SSDATA (str)];
2081 #ifdef NS_IMPL_COCOA
2083 utfStr = [utfStr precomposedStringWithCanonicalMapping];
2087 const char *cstr = [utfStr UTF8String];
2089 ret = build_string (cstr);
2094 error ("Invalid UTF-8");
2100 #ifdef NS_IMPL_COCOA
2102 /* Compile and execute the AppleScript SCRIPT and return the error
2103 status as function value. A zero is returned if compilation and
2104 execution is successful, in which case *RESULT is set to a Lisp
2105 string or a number containing the resulting script value. Otherwise,
2108 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2110 NSAppleEventDescriptor *desc;
2111 NSDictionary* errorDict;
2112 NSAppleEventDescriptor* returnDescriptor = NULL;
2114 NSAppleScript* scriptObject =
2115 [[NSAppleScript alloc] initWithSource:
2116 [NSString stringWithUTF8String: SSDATA (script)]];
2118 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2119 [scriptObject release];
2122 if (returnDescriptor != NULL)
2124 // successful execution
2125 if (kAENullEvent != [returnDescriptor descriptorType])
2128 // script returned an AppleScript result
2129 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2130 #if defined (NS_IMPL_COCOA)
2131 (typeUTF16ExternalRepresentation
2132 == [returnDescriptor descriptorType]) ||
2134 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2135 (typeCString == [returnDescriptor descriptorType]))
2137 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2139 *result = build_string([[desc stringValue] UTF8String]);
2143 /* use typeUTF16ExternalRepresentation? */
2144 // coerce the result to the appropriate ObjC type
2145 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2147 *result = make_number([desc int32Value]);
2153 // no script result, return error
2159 /* Helper function called from sendEvent to run applescript
2160 from within the main event loop. */
2163 ns_run_ascript (void)
2165 if (! NILP (as_script))
2166 as_status = ns_do_applescript (as_script, as_result);
2170 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2171 doc: /* Execute AppleScript SCRIPT and return the result.
2172 If compilation and execution are successful, the resulting script value
2173 is returned as a string, a number or, in the case of other constructs, t.
2174 In case the execution fails, an error is signaled. */)
2175 (Lisp_Object script)
2180 struct input_event ev;
2182 CHECK_STRING (script);
2183 check_window_system (NULL);
2188 as_result = &result;
2190 /* executing apple script requires the event loop to run, otherwise
2191 errors aren't returned and executeAndReturnError hangs forever.
2192 Post an event that runs applescript and then start the event loop.
2193 The event loop is exited when the script is done. */
2194 nxev = [NSEvent otherEventWithType: NSApplicationDefined
2195 location: NSMakePoint (0, 0)
2198 windowNumber: [[NSApp mainWindow] windowNumber]
2199 context: [NSApp context]
2202 data2: NSAPP_DATA2_RUNASSCRIPT];
2204 [NSApp postEvent: nxev atStart: NO];
2206 // If there are other events, the event loop may exit. Keep running
2207 // until the script has been handled. */
2208 ns_init_events (&ev);
2209 while (! NILP (as_script))
2211 ns_finish_events ();
2219 else if (!STRINGP (result))
2220 error ("AppleScript error %d", status);
2222 error ("%s", SSDATA (result));
2228 /* ==========================================================================
2230 Miscellaneous functions not called through hooks
2232 ========================================================================== */
2234 /* called from frame.c */
2235 struct ns_display_info *
2236 check_x_display_info (Lisp_Object frame)
2238 return check_ns_display_info (frame);
2243 x_set_scroll_bar_default_width (struct frame *f)
2245 int wid = FRAME_COLUMN_WIDTH (f);
2246 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2247 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2252 x_set_scroll_bar_default_height (struct frame *f)
2254 int height = FRAME_LINE_HEIGHT (f);
2255 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2256 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) +
2257 height - 1) / height;
2260 /* terms impl this instead of x-get-resource directly */
2262 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
2264 /* remove appname prefix; TODO: allow for !="Emacs" */
2265 const char *res, *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2267 check_window_system (NULL);
2269 if (inhibit_x_resources)
2270 /* --quick was passed, so this is a no-op. */
2273 res = ns_get_defaults_value (toCheck);
2274 return (!res ? NULL :
2275 (!c_strncasecmp (res, "YES", 3) ? "true" :
2276 (!c_strncasecmp (res, "NO", 2) ? "false" : (char *) res)));
2281 x_get_focus_frame (struct frame *frame)
2283 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2284 Lisp_Object nsfocus;
2286 if (!dpyinfo->x_focus_frame)
2289 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2293 /* ==========================================================================
2295 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2297 ========================================================================== */
2300 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2301 doc: /* Internal function called by `color-defined-p', which see.
2302 \(Note that the Nextstep version of this function ignores FRAME.) */)
2303 (Lisp_Object color, Lisp_Object frame)
2306 check_window_system (NULL);
2307 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2311 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2312 doc: /* Internal function called by `color-values', which see. */)
2313 (Lisp_Object color, Lisp_Object frame)
2316 EmacsCGFloat red, green, blue, alpha;
2318 check_window_system (NULL);
2319 CHECK_STRING (color);
2322 if (ns_lisp_to_color (color, &col))
2328 [[col colorUsingDefaultColorSpace]
2329 getRed: &red green: &green blue: &blue alpha: &alpha];
2331 return list3i (lrint (red * 65280), lrint (green * 65280),
2332 lrint (blue * 65280));
2336 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2337 doc: /* Internal function called by `display-color-p', which see. */)
2338 (Lisp_Object terminal)
2340 NSWindowDepth depth;
2341 NSString *colorSpace;
2343 check_ns_display_info (terminal);
2344 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2345 colorSpace = NSColorSpaceFromDepth (depth);
2347 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2348 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2353 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2355 doc: /* Return t if the Nextstep display supports shades of gray.
2356 Note that color displays do support shades of gray.
2357 The optional argument TERMINAL specifies which display to ask about.
2358 TERMINAL should be a terminal object, a frame or a display name (a string).
2359 If omitted or nil, that stands for the selected frame's display. */)
2360 (Lisp_Object terminal)
2362 NSWindowDepth depth;
2364 check_ns_display_info (terminal);
2365 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2367 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2371 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2373 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2374 The optional argument TERMINAL specifies which display to ask about.
2375 TERMINAL should be a terminal object, a frame or a display name (a string).
2376 If omitted or nil, that stands for the selected frame's display.
2378 On \"multi-monitor\" setups this refers to the pixel width for all
2379 physical monitors associated with TERMINAL. To get information for
2380 each physical monitor, use `display-monitor-attributes-list'. */)
2381 (Lisp_Object terminal)
2383 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2385 return make_number (x_display_pixel_width (dpyinfo));
2389 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2390 Sx_display_pixel_height, 0, 1, 0,
2391 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2392 The optional argument TERMINAL specifies which display to ask about.
2393 TERMINAL should be a terminal object, a frame or a display name (a string).
2394 If omitted or nil, that stands for the selected frame's display.
2396 On \"multi-monitor\" setups this refers to the pixel height for all
2397 physical monitors associated with TERMINAL. To get information for
2398 each physical monitor, use `display-monitor-attributes-list'. */)
2399 (Lisp_Object terminal)
2401 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2403 return make_number (x_display_pixel_height (dpyinfo));
2406 #ifdef NS_IMPL_COCOA
2408 /* Returns the name for the screen that OBJ represents, or NULL.
2409 Caller must free return value.
2413 ns_get_name_from_ioreg (io_object_t obj)
2417 NSDictionary *info = (NSDictionary *)
2418 IODisplayCreateInfoDictionary (obj, kIODisplayOnlyPreferredName);
2419 NSDictionary *names = [info objectForKey:
2420 [NSString stringWithUTF8String:
2421 kDisplayProductName]];
2423 if ([names count] > 0)
2425 NSString *n = [names objectForKey: [[names allKeys]
2427 if (n != nil) name = xstrdup ([n UTF8String]);
2435 /* Returns the name for the screen that DID came from, or NULL.
2436 Caller must free return value.
2440 ns_screen_name (CGDirectDisplayID did)
2444 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9
2445 mach_port_t masterPort;
2449 // CGDisplayIOServicePort is deprecated. Do it another (harder) way.
2451 if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
2452 || IOServiceGetMatchingServices (masterPort,
2453 IOServiceMatching ("IONDRVDevice"),
2454 &it) != kIOReturnSuccess)
2457 /* Must loop until we find a name. Many devices can have the same unit
2458 number (represents different GPU parts), but only one has a name. */
2459 while (! name && (obj = IOIteratorNext (it)))
2461 CFMutableDictionaryRef props;
2464 if (IORegistryEntryCreateCFProperties (obj,
2466 kCFAllocatorDefault,
2467 kNilOptions) == kIOReturnSuccess
2469 && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
2471 unsigned nr = [(NSNumber *)val unsignedIntegerValue];
2472 if (nr == CGDisplayUnitNumber (did))
2473 name = ns_get_name_from_ioreg (obj);
2477 IOObjectRelease (obj);
2480 IOObjectRelease (it);
2484 name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
2492 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2494 int primary_monitor,
2497 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2498 Lisp_Object frame, rest;
2499 NSArray *screens = [NSScreen screens];
2502 FOR_EACH_FRAME (rest, frame)
2504 struct frame *f = XFRAME (frame);
2508 NSView *view = FRAME_NS_VIEW (f);
2509 NSScreen *screen = [[view window] screen];
2513 for (k = 0; i == -1 && k < [screens count]; ++k)
2515 if ([screens objectAtIndex: k] == screen)
2520 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2524 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2525 monitor_frames, source);
2528 DEFUN ("ns-display-monitor-attributes-list",
2529 Fns_display_monitor_attributes_list,
2530 Sns_display_monitor_attributes_list,
2532 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2534 The optional argument TERMINAL specifies which display to ask about.
2535 TERMINAL should be a terminal object, a frame or a display name (a string).
2536 If omitted or nil, that stands for the selected frame's display.
2538 In addition to the standard attribute keys listed in
2539 `display-monitor-attributes-list', the following keys are contained in
2542 source -- String describing the source from which multi-monitor
2543 information is obtained, \"NS\" is always the source."
2545 Internal use only, use `display-monitor-attributes-list' instead. */)
2546 (Lisp_Object terminal)
2548 struct terminal *term = decode_live_terminal (terminal);
2550 NSUInteger i, n_monitors;
2551 struct MonitorInfo *monitors;
2552 Lisp_Object attributes_list = Qnil;
2553 CGFloat primary_display_height = 0;
2555 if (term->type != output_ns)
2558 screens = [NSScreen screens];
2559 n_monitors = [screens count];
2560 if (n_monitors == 0)
2563 monitors = xzalloc (n_monitors * sizeof *monitors);
2565 for (i = 0; i < [screens count]; ++i)
2567 NSScreen *s = [screens objectAtIndex:i];
2568 struct MonitorInfo *m = &monitors[i];
2569 NSRect fr = [s frame];
2570 NSRect vfr = [s visibleFrame];
2573 #ifdef NS_IMPL_COCOA
2574 NSDictionary *dict = [s deviceDescription];
2575 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2576 CGDirectDisplayID did = [nid unsignedIntValue];
2580 primary_display_height = fr.size.height;
2581 y = (short) fr.origin.y;
2582 vy = (short) vfr.origin.y;
2586 // Flip y coordinate as NS has y starting from the bottom.
2587 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2588 vy = (short) (primary_display_height -
2589 vfr.size.height - vfr.origin.y);
2592 m->geom.x = (short) fr.origin.x;
2594 m->geom.width = (unsigned short) fr.size.width;
2595 m->geom.height = (unsigned short) fr.size.height;
2597 m->work.x = (short) vfr.origin.x;
2598 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2599 // and fr.size.height - vfr.size.height are pixels missing in total.
2600 // Pixels missing at top are
2601 // fr.size.height - vfr.size.height - vy + y.
2602 // work.y is then pixels missing at top + y.
2603 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2604 m->work.width = (unsigned short) vfr.size.width;
2605 m->work.height = (unsigned short) vfr.size.height;
2607 #ifdef NS_IMPL_COCOA
2608 m->name = ns_screen_name (did);
2611 CGSize mms = CGDisplayScreenSize (did);
2612 m->mm_width = (int) mms.width;
2613 m->mm_height = (int) mms.height;
2617 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2618 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2619 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2623 // Primary monitor is always first for NS.
2624 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2627 free_monitors (monitors, n_monitors);
2628 return attributes_list;
2632 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2634 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2635 The optional argument TERMINAL specifies which display to ask about.
2636 TERMINAL should be a terminal object, a frame or a display name (a string).
2637 If omitted or nil, that stands for the selected frame's display. */)
2638 (Lisp_Object terminal)
2640 check_ns_display_info (terminal);
2642 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2646 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2648 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2649 The optional argument TERMINAL specifies which display to ask about.
2650 TERMINAL should be a terminal object, a frame or a display name (a string).
2651 If omitted or nil, that stands for the selected frame's display. */)
2652 (Lisp_Object terminal)
2654 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2655 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2656 return make_number (1 << min (dpyinfo->n_planes, 24));
2660 /* Unused dummy def needed for compatibility. */
2661 Lisp_Object tip_frame;
2663 /* TODO: move to xdisp or similar */
2665 compute_tip_xy (struct frame *f,
2674 Lisp_Object left, top;
2675 EmacsView *view = FRAME_NS_VIEW (f);
2676 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2679 /* Start with user-specified or mouse position. */
2680 left = Fcdr (Fassq (Qleft, parms));
2681 top = Fcdr (Fassq (Qtop, parms));
2683 if (!INTEGERP (left) || !INTEGERP (top))
2685 pt.x = dpyinfo->last_mouse_motion_x;
2686 pt.y = dpyinfo->last_mouse_motion_y;
2687 /* Convert to screen coordinates */
2688 pt = [view convertPoint: pt toView: nil];
2689 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
2690 pt = [[view window] convertBaseToScreen: pt];
2693 NSRect r = NSMakeRect (pt.x, pt.y, 0, 0);
2694 r = [[view window] convertRectToScreen: r];
2702 /* Absolute coordinates. */
2704 pt.y = x_display_pixel_height (FRAME_DISPLAY_INFO (f)) - XINT (top)
2708 /* Ensure in bounds. (Note, screen origin = lower left.) */
2709 if (INTEGERP (left))
2711 else if (pt.x + XINT (dx) <= 0)
2712 *root_x = 0; /* Can happen for negative dx */
2713 else if (pt.x + XINT (dx) + width
2714 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
2715 /* It fits to the right of the pointer. */
2716 *root_x = pt.x + XINT (dx);
2717 else if (width + XINT (dx) <= pt.x)
2718 /* It fits to the left of the pointer. */
2719 *root_x = pt.x - width - XINT (dx);
2721 /* Put it left justified on the screen -- it ought to fit that way. */
2726 else if (pt.y - XINT (dy) - height >= 0)
2727 /* It fits below the pointer. */
2728 *root_y = pt.y - height - XINT (dy);
2729 else if (pt.y + XINT (dy) + height
2730 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
2731 /* It fits above the pointer */
2732 *root_y = pt.y + XINT (dy);
2734 /* Put it on the top. */
2735 *root_y = x_display_pixel_height (FRAME_DISPLAY_INFO (f)) - height;
2739 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2740 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2741 A tooltip window is a small window displaying a string.
2743 This is an internal function; Lisp code should call `tooltip-show'.
2745 FRAME nil or omitted means use the selected frame.
2747 PARMS is an optional list of frame parameters which can be used to
2748 change the tooltip's appearance.
2750 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2751 means use the default timeout of 5 seconds.
2753 If the list of frame parameters PARMS contains a `left' parameter,
2754 the tooltip is displayed at that x-position. Otherwise it is
2755 displayed at the mouse position, with offset DX added (default is 5 if
2756 DX isn't specified). Likewise for the y-position; if a `top' frame
2757 parameter is specified, it determines the y-position of the tooltip
2758 window, otherwise it is displayed at the mouse position, with offset
2759 DY added (default is -10).
2761 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2762 Text larger than the specified size is clipped. */)
2763 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2766 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2767 ptrdiff_t count = SPECPDL_INDEX ();
2772 specbind (Qinhibit_redisplay, Qt);
2774 GCPRO4 (string, parms, frame, timeout);
2776 CHECK_STRING (string);
2777 str = SSDATA (string);
2778 f = decode_window_system_frame (frame);
2780 timeout = make_number (5);
2782 CHECK_NATNUM (timeout);
2785 dx = make_number (5);
2790 dy = make_number (-10);
2795 if (ns_tooltip == nil)
2796 ns_tooltip = [[EmacsTooltip alloc] init];
2800 [ns_tooltip setText: str];
2801 size = [ns_tooltip frame].size;
2803 /* Move the tooltip window where the mouse pointer is. Resize and
2805 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2808 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2812 return unbind_to (count, Qnil);
2816 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2817 doc: /* Hide the current tooltip window, if there is any.
2818 Value is t if tooltip was open, nil otherwise. */)
2821 if (ns_tooltip == nil || ![ns_tooltip isActive])
2827 DEFUN ("x-frame-geometry", Fx_frame_geometry, Sx_frame_geometry, 0, 1, 0,
2828 doc: /* Return geometric attributes of frame FRAME.
2830 FRAME must be a live frame and defaults to the selected one.
2832 The return value is an association list containing the following
2833 elements (all size values are in pixels).
2835 - `frame-outer-size' is a cons of the outer width and height of FRAME.
2836 The outer size include the title bar and the external borders as well
2837 as any menu and/or tool bar of frame.
2839 - `border' is a cons of the horizontal and vertical width of FRAME's
2842 - `title-bar-height' is the height of the title bar of FRAME.
2844 - `menu-bar-external' if `t' means the menu bar is external (not
2845 included in the inner edges of FRAME).
2847 - `menu-bar-size' is a cons of the width and height of the menu bar of
2850 - `tool-bar-external' if `t' means the tool bar is external (not
2851 included in the inner edges of FRAME).
2853 - `tool-bar-side' tells tells on which side the tool bar on FRAME is and
2854 can be one of `left', `top', `right' or `bottom'.
2856 - `tool-bar-size' is a cons of the width and height of the tool bar of
2859 - `frame-inner-size' is a cons of the inner width and height of FRAME.
2860 This excludes FRAME's title bar and external border as well as any
2861 external menu and/or tool bar. */)
2864 struct frame *f = decode_live_frame (frame);
2865 int inner_width = FRAME_PIXEL_WIDTH (f);
2866 int inner_height = FRAME_PIXEL_HEIGHT (f);
2867 Lisp_Object fullscreen = Fframe_parameter (frame, Qfullscreen);
2868 int border = f->border_width;
2869 int title = FRAME_NS_TITLEBAR_HEIGHT (f);
2870 int outer_width = FRAME_PIXEL_WIDTH (f) + 2 * border;
2871 int outer_height = FRAME_PIXEL_HEIGHT (f) + 2 * border;
2872 int tool_bar_height = FRAME_TOOLBAR_HEIGHT (f);
2873 int tool_bar_width = tool_bar_height > 0
2874 ? outer_width - 2 * FRAME_INTERNAL_BORDER_WIDTH (f)
2877 int menu_bar_height = 0;
2878 int menu_bar_width = 0;
2881 listn (CONSTYPE_HEAP, 10,
2882 Fcons (Qframe_position,
2883 Fcons (make_number (f->left_pos), make_number (f->top_pos))),
2884 Fcons (Qframe_outer_size,
2885 Fcons (make_number (outer_width), make_number (outer_height))),
2886 Fcons (Qexternal_border_size,
2887 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
2888 ? Fcons (make_number (0), make_number (0))
2889 : Fcons (make_number (border), make_number (border)))),
2890 Fcons (Qtitle_height,
2891 ((EQ (fullscreen, Qfullboth) || EQ (fullscreen, Qfullscreen))
2893 : make_number (title))),
2894 Fcons (Qmenu_bar_external, FRAME_EXTERNAL_MENU_BAR (f) ? Qt : Qnil),
2895 Fcons (Qmenu_bar_size,
2896 Fcons (make_number (menu_bar_width),
2897 make_number (menu_bar_height))),
2898 Fcons (Qtool_bar_external, FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
2899 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
2900 Fcons (Qtool_bar_size,
2901 Fcons (make_number (tool_bar_width),
2902 make_number (tool_bar_height))),
2903 Fcons (Qframe_inner_size,
2904 Fcons (make_number (inner_width),
2905 make_number (inner_height))));
2909 /* ==========================================================================
2911 Class implementations
2913 ========================================================================== */
2916 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
2917 Return YES if handled, NO if not.
2920 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
2926 if ([theEvent type] != NSKeyDown) return NO;
2927 s = [theEvent characters];
2929 for (i = 0; i < [s length]; ++i)
2931 int ch = (int) [s characterAtIndex: i];
2934 case NSHomeFunctionKey:
2935 case NSDownArrowFunctionKey:
2936 case NSUpArrowFunctionKey:
2937 case NSLeftArrowFunctionKey:
2938 case NSRightArrowFunctionKey:
2939 case NSPageUpFunctionKey:
2940 case NSPageDownFunctionKey:
2941 case NSEndFunctionKey:
2942 /* Don't send command modified keys, as those are handled in the
2943 performKeyEquivalent method of the super class.
2945 if (! ([theEvent modifierFlags] & NSCommandKeyMask))
2947 [panel sendEvent: theEvent];
2951 /* As we don't have the standard key commands for
2952 copy/paste/cut/select-all in our edit menu, we must handle
2953 them here. TODO: handle Emacs key bindings for copy/cut/select-all
2954 here, paste works, because we have that in our Edit menu.
2955 I.e. refactor out code in nsterm.m, keyDown: to figure out the
2961 case 'a': // Select all
2962 if ([theEvent modifierFlags] & NSCommandKeyMask)
2971 : @selector(selectAll:))))
2976 // Send all control keys, as the text field supports C-a, C-f, C-e
2978 if ([theEvent modifierFlags] & NSControlKeyMask)
2980 [panel sendEvent: theEvent];
2991 @implementation EmacsSavePanel
2992 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
2994 BOOL ret = handlePanelKeys (self, theEvent);
2996 ret = [super performKeyEquivalent:theEvent];
3002 @implementation EmacsOpenPanel
3003 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3005 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
3006 BOOL ret = handlePanelKeys (self, theEvent);
3008 ret = [super performKeyEquivalent:theEvent];
3014 @implementation EmacsFileDelegate
3015 /* --------------------------------------------------------------------------
3016 Delegate methods for Open/Save panels
3017 -------------------------------------------------------------------------- */
3018 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
3022 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
3026 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
3027 confirmed: (BOOL)okFlag
3036 /* ==========================================================================
3038 Lisp interface declaration
3040 ========================================================================== */
3044 syms_of_nsfns (void)
3046 DEFSYM (Qfontsize, "fontsize");
3048 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
3049 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
3050 If the title of a frame matches REGEXP, then IMAGE.tiff is
3051 selected as the image of the icon representing the frame when it's
3052 miniaturized. If an element is t, then Emacs tries to select an icon
3053 based on the filetype of the visited file.
3055 The images have to be installed in a folder called English.lproj in the
3056 Emacs folder. You have to restart Emacs after installing new icons.
3058 Example: Install an icon Gnus.tiff and execute the following code
3060 (setq ns-icon-type-alist
3061 (append ns-icon-type-alist
3062 '((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
3065 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
3066 be used as the image of the icon representing the frame. */);
3067 Vns_icon_type_alist = list1 (Qt);
3069 DEFVAR_LISP ("ns-version-string", Vns_version_string,
3070 doc: /* Toolkit version for NS Windowing. */);
3071 Vns_version_string = ns_appkit_version_str ();
3073 defsubr (&Sns_read_file_name);
3074 defsubr (&Sns_get_resource);
3075 defsubr (&Sns_set_resource);
3076 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
3077 defsubr (&Sx_display_grayscale_p);
3078 defsubr (&Sns_font_name);
3079 defsubr (&Sns_list_colors);
3080 #ifdef NS_IMPL_COCOA
3081 defsubr (&Sns_do_applescript);
3083 defsubr (&Sxw_color_defined_p);
3084 defsubr (&Sxw_color_values);
3085 defsubr (&Sx_server_max_request_size);
3086 defsubr (&Sx_server_vendor);
3087 defsubr (&Sx_server_version);
3088 defsubr (&Sx_display_pixel_width);
3089 defsubr (&Sx_display_pixel_height);
3090 defsubr (&Sns_display_monitor_attributes_list);
3091 defsubr (&Sx_frame_geometry);
3092 defsubr (&Sx_display_mm_width);
3093 defsubr (&Sx_display_mm_height);
3094 defsubr (&Sx_display_screens);
3095 defsubr (&Sx_display_planes);
3096 defsubr (&Sx_display_color_cells);
3097 defsubr (&Sx_display_visual_class);
3098 defsubr (&Sx_display_backing_store);
3099 defsubr (&Sx_display_save_under);
3100 defsubr (&Sx_create_frame);
3101 defsubr (&Sx_open_connection);
3102 defsubr (&Sx_close_connection);
3103 defsubr (&Sx_display_list);
3105 defsubr (&Sns_hide_others);
3106 defsubr (&Sns_hide_emacs);
3107 defsubr (&Sns_emacs_info_panel);
3108 defsubr (&Sns_list_services);
3109 defsubr (&Sns_perform_service);
3110 defsubr (&Sns_convert_utf8_nfd_to_nfc);
3111 defsubr (&Sns_popup_font_panel);
3112 defsubr (&Sns_popup_color_panel);
3114 defsubr (&Sx_show_tip);
3115 defsubr (&Sx_hide_tip);