1 /* Functions for the NeXT/Open/GNUstep and macOS window system.
3 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2018 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 (at
11 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 <https://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 macOS/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>
55 static EmacsTooltip *ns_tooltip = nil;
57 /* Static variables to handle applescript execution. */
58 static Lisp_Object as_script, *as_result;
61 static ptrdiff_t image_cache_refcount;
63 static struct ns_display_info *ns_display_info_for_name (Lisp_Object);
64 static void ns_set_name_as_filename (struct frame *);
66 /* ==========================================================================
68 Internal utility functions
70 ========================================================================== */
72 /* Let the user specify a Nextstep display with a Lisp object.
73 OBJECT may be nil, a frame or a terminal object.
74 nil stands for the selected frame--or, if that is not a Nextstep frame,
75 the first Nextstep display on the list. */
77 static struct ns_display_info *
78 check_ns_display_info (Lisp_Object object)
80 struct ns_display_info *dpyinfo = NULL;
84 struct frame *sf = XFRAME (selected_frame);
86 if (FRAME_NS_P (sf) && FRAME_LIVE_P (sf))
87 dpyinfo = FRAME_DISPLAY_INFO (sf);
88 else if (x_display_list != 0)
89 dpyinfo = x_display_list;
91 error ("Nextstep windows are not in use or not initialized");
93 else if (TERMINALP (object))
95 struct terminal *t = decode_live_terminal (object);
97 if (t->type != output_ns)
98 error ("Terminal %d is not a Nextstep display", t->id);
100 dpyinfo = t->display_info.ns;
102 else if (STRINGP (object))
103 dpyinfo = ns_display_info_for_name (object);
106 struct frame *f = decode_window_system_frame (object);
107 dpyinfo = FRAME_DISPLAY_INFO (f);
115 ns_get_window (Lisp_Object maybeFrame)
117 id view =nil, window =nil;
119 if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
120 maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */
122 if (!NILP (maybeFrame))
123 view = FRAME_NS_VIEW (XFRAME (maybeFrame));
124 if (view) window =[view window];
130 /* Return the X display structure for the display named NAME.
131 Open a new connection if necessary. */
132 static struct ns_display_info *
133 ns_display_info_for_name (Lisp_Object name)
135 struct ns_display_info *dpyinfo;
139 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
140 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
143 error ("Emacs for Nextstep does not yet support multi-display");
145 Fx_open_connection (name, Qnil, Qnil);
146 dpyinfo = x_display_list;
149 error ("Display on %s not responding.\n", SDATA (name));
155 ns_filename_from_panel (NSSavePanel *panel)
158 NSURL *url = [panel URL];
159 NSString *str = [url path];
162 return [panel filename];
167 ns_directory_from_panel (NSSavePanel *panel)
170 NSURL *url = [panel directoryURL];
171 NSString *str = [url path];
174 return [panel directory];
178 #ifndef NS_IMPL_COCOA
180 interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
181 /* --------------------------------------------------------------------------
182 Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
183 -------------------------------------------------------------------------- */
193 count = [menu numberOfItems];
194 for (i = 0; i<count; i++)
196 item = [menu itemAtIndex: i];
197 name = [[item title] UTF8String];
200 nameStr = build_string (name);
202 if ([item hasSubmenu])
204 old = interpret_services_menu ([item submenu],
205 Fcons (nameStr, prefix), old);
209 keys = [item keyEquivalent];
210 if (keys && [keys length] )
212 key = [keys characterAtIndex: 0];
213 res = make_number (key|super_modifier);
219 old = Fcons (Fcons (res,
220 Freverse (Fcons (nameStr,
230 /* ==========================================================================
232 Frame parameter setters
234 ========================================================================== */
238 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
241 EmacsCGFloat r, g, b, alpha;
243 /* Must block_input, because ns_lisp_to_color does block/unblock_input
244 which means that col may be deallocated in its unblock_input if there
245 is user input, unless we also block_input. */
247 if (ns_lisp_to_color (arg, &col))
249 store_frame_param (f, Qforeground_color, oldval);
251 error ("Unknown color");
255 [f->output_data.ns->foreground_color release];
256 f->output_data.ns->foreground_color = col;
258 [col getRed: &r green: &g blue: &b alpha: &alpha];
259 FRAME_FOREGROUND_PIXEL (f) =
260 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
262 if (FRAME_NS_VIEW (f))
264 update_face_from_frame_parameter (f, Qforeground_color, arg);
265 /*recompute_basic_faces (f); */
266 if (FRAME_VISIBLE_P (f))
267 SET_FRAME_GARBAGED (f);
274 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
278 NSView *view = FRAME_NS_VIEW (f);
279 EmacsCGFloat r, g, b, alpha;
282 if (ns_lisp_to_color (arg, &col))
284 store_frame_param (f, Qbackground_color, oldval);
286 error ("Unknown color");
289 /* clear the frame; in some instances the NS-internal GC appears not to
290 update, or it does update and cannot clear old text properly */
291 if (FRAME_VISIBLE_P (f))
295 [f->output_data.ns->background_color release];
296 f->output_data.ns->background_color = col;
298 [col getRed: &r green: &g blue: &b alpha: &alpha];
299 FRAME_BACKGROUND_PIXEL (f) =
300 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
304 [[view window] setBackgroundColor: col];
306 if (alpha != (EmacsCGFloat) 1.0)
307 [[view window] setOpaque: NO];
309 [[view window] setOpaque: YES];
311 face = FRAME_DEFAULT_FACE (f);
314 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
315 face->background = ns_index_color
316 ([col colorWithAlphaComponent: alpha], f);
318 update_face_from_frame_parameter (f, Qbackground_color, arg);
321 if (FRAME_VISIBLE_P (f))
322 SET_FRAME_GARBAGED (f);
329 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
334 if (ns_lisp_to_color (arg, &col))
336 store_frame_param (f, Qcursor_color, oldval);
338 error ("Unknown color");
341 [FRAME_CURSOR_COLOR (f) release];
342 FRAME_CURSOR_COLOR (f) = [col retain];
344 if (FRAME_VISIBLE_P (f))
346 x_update_cursor (f, 0);
347 x_update_cursor (f, 1);
349 update_face_from_frame_parameter (f, Qcursor_color, arg);
355 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
357 NSView *view = FRAME_NS_VIEW (f);
358 NSTRACE ("x_set_icon_name");
360 /* see if it's changed */
363 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
366 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
369 fset_icon_name (f, arg);
373 if (!NILP (f->title))
376 /* Explicit name and no icon-name -> explicit_name. */
377 if (f->explicit_name)
381 /* No explicit name and no icon-name ->
382 name has to be rebuild from icon_title_format. */
383 windows_or_buffers_changed = 62;
388 /* Don't change the name if it's already NAME. */
389 if ([[view window] miniwindowTitle]
390 && ([[[view window] miniwindowTitle]
391 isEqualToString: [NSString stringWithUTF8String:
395 [[view window] setMiniwindowTitle:
396 [NSString stringWithUTF8String: SSDATA (arg)]];
400 ns_set_name_internal (struct frame *f, Lisp_Object name)
402 Lisp_Object encoded_name, encoded_icon_name;
404 NSView *view = FRAME_NS_VIEW (f);
407 encoded_name = ENCODE_UTF_8 (name);
409 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
412 /* Don't change the name if it's already NAME. */
413 if (! [[[view window] title] isEqualToString: str])
414 [[view window] setTitle: str];
416 if (!STRINGP (f->icon_name))
417 encoded_icon_name = encoded_name;
419 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
421 str = [NSString stringWithUTF8String: SSDATA (encoded_icon_name)];
423 if ([[view window] miniwindowTitle]
424 && ! [[[view window] miniwindowTitle] isEqualToString: str])
425 [[view window] setMiniwindowTitle: str];
430 ns_set_name (struct frame *f, Lisp_Object name, int explicit)
432 NSTRACE ("ns_set_name");
434 /* Make sure that requests from lisp code override requests from
435 Emacs redisplay code. */
438 /* If we're switching from explicit to implicit, we had better
439 update the mode lines and thereby update the title. */
440 if (f->explicit_name && NILP (name))
441 update_mode_lines = 21;
443 f->explicit_name = ! NILP (name);
445 else if (f->explicit_name)
449 name = build_string ([ns_app_name UTF8String]);
453 /* Don't change the name if it's already NAME. */
454 if (! NILP (Fstring_equal (name, f->name)))
459 /* Title overrides explicit name. */
460 if (! NILP (f->title))
463 ns_set_name_internal (f, name);
467 /* This function should be called when the user's lisp code has
468 specified a name for the frame; the name will override any set by the
471 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
473 NSTRACE ("x_explicitly_set_name");
474 ns_set_name (f, arg, 1);
478 /* This function should be called by Emacs redisplay code to set the
479 name; names set this way will never override names set by the user's
482 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
484 NSTRACE ("x_implicitly_set_name");
486 Lisp_Object frame_title = buffer_local_value
487 (Qframe_title_format, XWINDOW (f->selected_window)->contents);
488 Lisp_Object icon_title = buffer_local_value
489 (Qicon_title_format, XWINDOW (f->selected_window)->contents);
491 /* Deal with NS specific format t. */
492 if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (icon_title, Qt))
493 || EQ (frame_title, Qt)))
494 ns_set_name_as_filename (f);
496 ns_set_name (f, arg, 0);
500 /* Change the title of frame F to NAME.
501 If NAME is nil, use the frame name as the title. */
504 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
506 NSTRACE ("x_set_title");
507 /* Don't change the title if it's already NAME. */
508 if (EQ (name, f->title))
511 update_mode_lines = 22;
513 fset_title (f, name);
520 ns_set_name_internal (f, name);
525 ns_set_name_as_filename (struct frame *f)
528 Lisp_Object name, filename;
529 Lisp_Object buf = XWINDOW (f->selected_window)->contents;
531 NSAutoreleasePool *pool;
532 Lisp_Object encoded_name, encoded_filename;
534 NSTRACE ("ns_set_name_as_filename");
536 if (f->explicit_name || ! NILP (f->title))
540 pool = [[NSAutoreleasePool alloc] init];
541 filename = BVAR (XBUFFER (buf), filename);
542 name = BVAR (XBUFFER (buf), name);
546 if (! NILP (filename))
547 name = Ffile_name_nondirectory (filename);
549 name = build_string ([ns_app_name UTF8String]);
552 encoded_name = ENCODE_UTF_8 (name);
554 view = FRAME_NS_VIEW (f);
556 title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
557 : [[[view window] title] UTF8String];
559 if (title && (! strcmp (title, SSDATA (encoded_name))))
566 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
567 if (str == nil) str = @"Bad coding";
569 if (FRAME_ICONIFIED_P (f))
570 [[view window] setMiniwindowTitle: str];
575 if (! NILP (filename))
577 encoded_filename = ENCODE_UTF_8 (filename);
579 fstr = [NSString stringWithUTF8String: SSDATA (encoded_filename)];
580 if (fstr == nil) fstr = @"";
585 ns_set_represented_filename (fstr, f);
586 [[view window] setTitle: str];
596 ns_set_doc_edited (void)
598 NSAutoreleasePool *pool;
599 Lisp_Object tail, frame;
601 pool = [[NSAutoreleasePool alloc] init];
602 FOR_EACH_FRAME (tail, frame)
605 struct frame *f = XFRAME (frame);
609 if (! FRAME_NS_P (f)) continue;
610 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
611 view = FRAME_NS_VIEW (f);
612 if (!MINI_WINDOW_P (w))
613 edited = ! NILP (Fbuffer_modified_p (w->contents)) &&
614 ! NILP (Fbuffer_file_name (w->contents));
615 [[view window] setDocumentEdited: edited];
624 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
627 if (FRAME_MINIBUF_ONLY_P (f))
630 if (TYPE_RANGED_INTEGERP (int, value))
631 nlines = XINT (value);
635 FRAME_MENU_BAR_LINES (f) = 0;
638 FRAME_EXTERNAL_MENU_BAR (f) = 1;
639 /* does for all frames, whereas we just want for one frame
640 [NSMenu setMenuBarVisible: YES]; */
644 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
645 free_frame_menubar (f);
646 /* [NSMenu setMenuBarVisible: NO]; */
647 FRAME_EXTERNAL_MENU_BAR (f) = 0;
652 /* toolbar support */
654 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
656 /* Currently, when the tool bar change state, the frame is resized.
658 TODO: It would be better if this didn't occur when 1) the frame
659 is full height or maximized or 2) when specified by
660 `frame-inhibit-implied-resize'. */
663 NSTRACE ("x_set_tool_bar_lines");
665 if (FRAME_MINIBUF_ONLY_P (f))
668 if (RANGED_INTEGERP (0, value, INT_MAX))
669 nlines = XFASTINT (value);
675 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
676 update_frame_tool_bar (f);
680 if (FRAME_EXTERNAL_TOOL_BAR (f))
682 free_frame_tool_bar (f);
683 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
686 EmacsView *view = FRAME_NS_VIEW (f);
687 int fs_state = [view fullscreenState];
689 if (fs_state == FULLSCREEN_MAXIMIZED)
691 [view setFSValue:FULLSCREEN_WIDTH];
693 else if (fs_state == FULLSCREEN_HEIGHT)
695 [view setFSValue:FULLSCREEN_NONE];
703 = ((f->after_make_frame
704 && !f->tool_bar_resized
705 && (EQ (frame_inhibit_implied_resize, Qt)
706 || (CONSP (frame_inhibit_implied_resize)
707 && !NILP (Fmemq (Qtool_bar_lines,
708 frame_inhibit_implied_resize))))
709 && NILP (get_frame_param (f, Qfullscreen)))
713 NSTRACE_MSG ("inhibit:%d", inhibit);
715 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
716 adjust_frame_size (f, -1, -1, inhibit, 0, Qtool_bar_lines);
722 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
724 int old_width = FRAME_INTERNAL_BORDER_WIDTH (f);
726 CHECK_TYPE_RANGED_INTEGER (int, arg);
727 f->internal_border_width = XINT (arg);
728 if (FRAME_INTERNAL_BORDER_WIDTH (f) < 0)
729 f->internal_border_width = 0;
731 if (FRAME_INTERNAL_BORDER_WIDTH (f) == old_width)
734 if (FRAME_X_WINDOW (f) != 0)
735 adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
737 SET_FRAME_GARBAGED (f);
742 ns_implicitly_set_icon_type (struct frame *f)
745 EmacsView *view = FRAME_NS_VIEW (f);
747 Lisp_Object chain, elt;
748 NSAutoreleasePool *pool;
751 NSTRACE ("ns_implicitly_set_icon_type");
754 pool = [[NSAutoreleasePool alloc] init];
755 if (f->output_data.ns->miniimage
756 && [[NSString stringWithUTF8String: SSDATA (f->name)]
757 isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
764 tem = assq_no_quit (Qicon_type, f->param_alist);
765 if (CONSP (tem) && ! NILP (XCDR (tem)))
772 for (chain = Vns_icon_type_alist;
773 image == nil && CONSP (chain);
774 chain = XCDR (chain))
777 /* special case: t means go by file type */
778 if (SYMBOLP (elt) && EQ (elt, Qt) && SSDATA (f->name)[0] == '/')
781 = [NSString stringWithUTF8String: SSDATA (f->name)];
782 if ([[NSFileManager defaultManager] fileExistsAtPath: str])
783 image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
785 else if (CONSP (elt) &&
786 STRINGP (XCAR (elt)) &&
787 STRINGP (XCDR (elt)) &&
788 fast_string_match (XCAR (elt), f->name) >= 0)
790 image = [EmacsImage allocInitFromFile: XCDR (elt)];
792 image = [[NSImage imageNamed:
793 [NSString stringWithUTF8String:
794 SSDATA (XCDR (elt))]] retain];
800 image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
804 [f->output_data.ns->miniimage release];
805 f->output_data.ns->miniimage = image;
806 [view setMiniwindowImage: setMini];
813 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
815 EmacsView *view = FRAME_NS_VIEW (f);
819 NSTRACE ("x_set_icon_type");
821 if (!NILP (arg) && SYMBOLP (arg))
823 arg =build_string (SSDATA (SYMBOL_NAME (arg)));
824 store_frame_param (f, Qicon_type, arg);
827 /* do it the implicit way */
830 ns_implicitly_set_icon_type (f);
836 image = [EmacsImage allocInitFromFile: arg];
838 image =[NSImage imageNamed: [NSString stringWithUTF8String:
843 image = [NSImage imageNamed: @"text"];
847 f->output_data.ns->miniimage = image;
848 [view setMiniwindowImage: setMini];
851 /* This is the same as the xfns.c definition. */
853 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
855 set_frame_cursor_types (f, arg);
858 /* called to set mouse pointer color, but all other terms use it to
859 initialize pointer types (and don't set the color ;) */
861 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
863 /* don't think we can do this on Nextstep */
868 #define Xstr(x) Str(x)
871 ns_appkit_version_str (void)
875 #ifdef NS_IMPL_GNUSTEP
876 sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
877 #elif defined (NS_IMPL_COCOA)
879 = [[NSProcessInfo processInfo] operatingSystemVersionString];
880 sprintf(tmp, "appkit-%.2f %s",
881 NSAppKitVersionNumber,
882 [osversion UTF8String]);
886 return build_string (tmp);
890 /* This is for use by x-server-version and collapses all version info we
891 have into a single int. For a better picture of the implementation
892 running, use ns_appkit_version_str.*/
894 ns_appkit_version_int (void)
896 #ifdef NS_IMPL_GNUSTEP
897 return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
898 #elif defined (NS_IMPL_COCOA)
899 return (int)NSAppKitVersionNumber;
906 x_icon (struct frame *f, Lisp_Object parms)
907 /* --------------------------------------------------------------------------
908 Strangely-named function to set icon position parameters in frame.
909 This is irrelevant under macOS, but might be needed under GNUstep,
910 depending on the window manager used. Note, this is not a standard
911 frame parameter-setter; it is called directly from x-create-frame.
912 -------------------------------------------------------------------------- */
914 Lisp_Object icon_x, icon_y;
915 struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);
917 f->output_data.ns->icon_top = -1;
918 f->output_data.ns->icon_left = -1;
920 /* Set the position of the icon. */
921 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
922 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
923 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
925 CHECK_NUMBER (icon_x);
926 CHECK_NUMBER (icon_y);
927 f->output_data.ns->icon_top = XINT (icon_y);
928 f->output_data.ns->icon_left = XINT (icon_x);
930 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
931 error ("Both left and top icon corners of icon must be specified");
935 /* Note: see frame.c for template, also where generic functions are impl */
936 frame_parm_handler ns_frame_parm_handlers[] =
938 x_set_autoraise, /* generic OK */
939 x_set_autolower, /* generic OK */
940 x_set_background_color,
941 0, /* x_set_border_color, may be impossible under Nextstep */
942 0, /* x_set_border_width, may be impossible under Nextstep */
945 x_set_font, /* generic OK */
946 x_set_foreground_color,
949 x_set_internal_border_width, /* generic OK */
950 x_set_right_divider_width,
951 x_set_bottom_divider_width,
952 x_set_menu_bar_lines,
954 x_explicitly_set_name,
955 x_set_scroll_bar_width, /* generic OK */
956 x_set_scroll_bar_height, /* generic OK */
958 x_set_unsplittable, /* generic OK */
959 x_set_vertical_scroll_bars, /* generic OK */
960 x_set_horizontal_scroll_bars, /* generic OK */
961 x_set_visibility, /* generic OK */
962 x_set_tool_bar_lines,
963 0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
964 0, /* x_set_scroll_bar_background, will ignore (not possible on NS) */
965 x_set_screen_gamma, /* generic OK */
966 x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
967 x_set_left_fringe, /* generic OK */
968 x_set_right_fringe, /* generic OK */
969 0, /* x_set_wait_for_wm, will ignore */
970 x_set_fullscreen, /* generic OK */
971 x_set_font_backend, /* generic OK */
973 0, /* x_set_sticky */
974 0, /* x_set_tool_bar_position */
975 0, /* x_set_inhibit_double_buffering */
979 0, /*x_set_undecorated */
982 0, /* x_set_skip_taskbar */
983 x_set_no_focus_on_map,
984 x_set_no_accept_focus,
985 x_set_z_group, /* x_set_z_group */
986 0, /* x_set_override_redirect */
987 x_set_no_special_glyphs,
990 ns_set_transparent_titlebar,
995 /* Handler for signals raised during x_create_frame.
996 FRAME is the frame which is partially constructed. */
999 unwind_create_frame (Lisp_Object frame)
1001 struct frame *f = XFRAME (frame);
1003 /* If frame is already dead, nothing to do. This can happen if the
1004 display is disconnected after the frame has become official, but
1005 before x_create_frame removes the unwind protect. */
1006 if (!FRAME_LIVE_P (f))
1009 /* If frame is ``official'', nothing to do. */
1010 if (NILP (Fmemq (frame, Vframe_list)))
1012 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1013 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1016 /* If the frame's image cache refcount is still the same as our
1017 private shadow variable, it means we are unwinding a frame
1018 for which we didn't yet call init_frame_faces, where the
1019 refcount is incremented. Therefore, we increment it here, so
1020 that free_frame_faces, called in x_free_frame_resources
1021 below, will not mistakenly decrement the counter that was not
1022 incremented yet to account for this new frame. */
1023 if (FRAME_IMAGE_CACHE (f) != NULL
1024 && FRAME_IMAGE_CACHE (f)->refcount == image_cache_refcount)
1025 FRAME_IMAGE_CACHE (f)->refcount++;
1027 x_free_frame_resources (f);
1030 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1031 /* Check that reference counts are indeed correct. */
1032 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
1038 * Read geometry related parameters from preferences if not in PARMS.
1039 * Returns the union of parms and any preferences read.
1043 get_geometry_from_preferences (struct ns_display_info *dpyinfo,
1051 { "width", "Width", Qwidth },
1052 { "height", "Height", Qheight },
1053 { "left", "Left", Qleft },
1054 { "top", "Top", Qtop },
1058 for (i = 0; i < ARRAYELTS (r); ++i)
1060 if (NILP (Fassq (r[i].tem, parms)))
1063 = x_get_arg (dpyinfo, parms, r[i].tem, r[i].val, r[i].cls,
1065 if (! EQ (value, Qunbound))
1066 parms = Fcons (Fcons (r[i].tem, value), parms);
1073 /* ==========================================================================
1077 ========================================================================== */
1079 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
1081 doc: /* Make a new Nextstep window, called a "frame" in Emacs terms.
1082 Return an Emacs frame object.
1083 PARMS is an alist of frame parameters.
1084 If the parameters specify that the frame should not have a minibuffer,
1085 and do not specify a specific minibuffer window to use,
1086 then `default-minibuffer-frame' must be a frame whose minibuffer can
1087 be shared by the new frame.
1089 This function is an internal primitive--use `make-frame' instead. */)
1093 Lisp_Object frame, tem;
1095 int minibuffer_only = 0;
1096 long window_prompting = 0;
1097 ptrdiff_t count = specpdl_ptr - specpdl;
1098 Lisp_Object display;
1099 struct ns_display_info *dpyinfo = NULL;
1100 Lisp_Object parent, parent_frame;
1102 static int desc_ctr = 1;
1103 int x_width = 0, x_height = 0;
1105 /* x_get_arg modifies parms. */
1106 parms = Fcopy_alist (parms);
1108 /* Use this general default value to start with
1109 until we know if this frame has a specified name. */
1110 Vx_resource_name = Vinvocation_name;
1112 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
1113 if (EQ (display, Qunbound))
1115 dpyinfo = check_ns_display_info (display);
1116 kb = dpyinfo->terminal->kboard;
1118 if (!dpyinfo->terminal->name)
1119 error ("Terminal is not live, can't create new frames on it");
1121 name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
1123 && ! EQ (name, Qunbound)
1125 error ("Invalid frame name--not a string or nil");
1128 Vx_resource_name = name;
1130 parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
1131 if (EQ (parent, Qunbound))
1133 if (! NILP (parent))
1134 CHECK_NUMBER (parent);
1136 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
1137 /* No need to protect DISPLAY because that's not used after passing
1138 it to make_frame_without_minibuffer. */
1140 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
1142 if (EQ (tem, Qnone) || NILP (tem))
1143 f = make_frame_without_minibuffer (Qnil, kb, display);
1144 else if (EQ (tem, Qonly))
1146 f = make_minibuffer_frame ();
1147 minibuffer_only = 1;
1149 else if (WINDOWP (tem))
1150 f = make_frame_without_minibuffer (tem, kb, display);
1154 XSETFRAME (frame, f);
1156 f->terminal = dpyinfo->terminal;
1158 f->output_method = output_ns;
1159 f->output_data.ns = xzalloc (sizeof *f->output_data.ns);
1161 FRAME_FONTSET (f) = -1;
1163 fset_icon_name (f, x_get_arg (dpyinfo, parms, Qicon_name,
1164 "iconName", "Title",
1166 if (! STRINGP (f->icon_name))
1167 fset_icon_name (f, Qnil);
1169 FRAME_DISPLAY_INFO (f) = dpyinfo;
1171 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
1172 record_unwind_protect (unwind_create_frame, frame);
1174 f->output_data.ns->window_desc = desc_ctr++;
1175 if (TYPE_RANGED_INTEGERP (Window, parent))
1177 f->output_data.ns->parent_desc = XFASTINT (parent);
1178 f->output_data.ns->explicit_parent = 1;
1182 f->output_data.ns->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
1183 f->output_data.ns->explicit_parent = 0;
1186 /* Set the name; the functions to which we pass f expect the name to
1188 if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
1190 fset_name (f, build_string ([ns_app_name UTF8String]));
1191 f->explicit_name = 0;
1195 fset_name (f, name);
1196 f->explicit_name = 1;
1197 specbind (Qx_resource_name, name);
1202 #ifdef NS_IMPL_COCOA
1203 mac_register_font_driver (f);
1205 register_font_driver (&nsfont_driver, f);
1208 image_cache_refcount =
1209 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
1211 x_default_parameter (f, parms, Qfont_backend, Qnil,
1212 "fontBackend", "FontBackend", RES_TYPE_STRING);
1215 /* use for default font name */
1216 id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
1217 x_default_parameter (f, parms, Qfontsize,
1218 make_number (0 /*(int)[font pointSize]*/),
1219 "fontSize", "FontSize", RES_TYPE_NUMBER);
1220 // Remove ' Regular', not handled by backends.
1221 char *fontname = xstrdup ([[font displayName] UTF8String]);
1222 int len = strlen (fontname);
1223 if (len > 8 && strcmp (fontname + len - 8, " Regular") == 0)
1224 fontname[len-8] = '\0';
1225 x_default_parameter (f, parms, Qfont,
1226 build_string (fontname),
1227 "font", "Font", RES_TYPE_STRING);
1232 x_default_parameter (f, parms, Qborder_width, make_number (0),
1233 "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
1234 x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
1235 "internalBorderWidth", "InternalBorderWidth",
1237 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
1238 NULL, NULL, RES_TYPE_NUMBER);
1239 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
1240 NULL, NULL, RES_TYPE_NUMBER);
1242 /* default vertical scrollbars on right on Mac */
1245 #ifdef NS_IMPL_GNUSTEP
1250 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1251 "verticalScrollBars", "VerticalScrollBars",
1254 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
1255 "horizontalScrollBars", "HorizontalScrollBars",
1257 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1258 "foreground", "Foreground", RES_TYPE_STRING);
1259 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1260 "background", "Background", RES_TYPE_STRING);
1261 /* FIXME: not supported yet in Nextstep */
1262 x_default_parameter (f, parms, Qline_spacing, Qnil,
1263 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1264 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1265 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1266 x_default_parameter (f, parms, Qright_fringe, Qnil,
1267 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1268 x_default_parameter (f, parms, Qno_special_glyphs, Qnil,
1269 NULL, NULL, RES_TYPE_BOOLEAN);
1271 init_frame_faces (f);
1273 /* Read comment about this code in corresponding place in xfns.c. */
1274 tem = x_get_arg (dpyinfo, parms, Qmin_width, NULL, NULL, RES_TYPE_NUMBER);
1276 store_frame_param (f, Qmin_width, tem);
1277 tem = x_get_arg (dpyinfo, parms, Qmin_height, NULL, NULL, RES_TYPE_NUMBER);
1279 store_frame_param (f, Qmin_height, tem);
1280 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
1281 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1,
1284 tem = x_get_arg (dpyinfo, parms, Qundecorated, NULL, NULL, RES_TYPE_BOOLEAN);
1285 FRAME_UNDECORATED (f) = !NILP (tem) && !EQ (tem, Qunbound);
1286 store_frame_param (f, Qundecorated, FRAME_UNDECORATED (f) ? Qt : Qnil);
1288 #ifdef NS_IMPL_COCOA
1289 tem = x_get_arg (dpyinfo, parms, Qns_appearance, NULL, NULL, RES_TYPE_SYMBOL);
1290 FRAME_NS_APPEARANCE (f) = EQ (tem, Qdark)
1291 ? ns_appearance_vibrant_dark : ns_appearance_aqua;
1292 store_frame_param (f, Qns_appearance, tem);
1294 tem = x_get_arg (dpyinfo, parms, Qns_transparent_titlebar,
1295 NULL, NULL, RES_TYPE_BOOLEAN);
1296 FRAME_NS_TRANSPARENT_TITLEBAR (f) = !NILP (tem) && !EQ (tem, Qunbound);
1297 store_frame_param (f, Qns_transparent_titlebar, tem);
1300 parent_frame = x_get_arg (dpyinfo, parms, Qparent_frame, NULL, NULL,
1302 /* Accept parent-frame iff parent-id was not specified. */
1304 || EQ (parent_frame, Qunbound)
1305 || NILP (parent_frame)
1306 || !FRAMEP (parent_frame)
1307 || !FRAME_LIVE_P (XFRAME (parent_frame)))
1308 parent_frame = Qnil;
1310 fset_parent_frame (f, parent_frame);
1311 store_frame_param (f, Qparent_frame, parent_frame);
1313 x_default_parameter (f, parms, Qz_group, Qnil, NULL, NULL, RES_TYPE_SYMBOL);
1314 x_default_parameter (f, parms, Qno_focus_on_map, Qnil,
1315 NULL, NULL, RES_TYPE_BOOLEAN);
1316 x_default_parameter (f, parms, Qno_accept_focus, Qnil,
1317 NULL, NULL, RES_TYPE_BOOLEAN);
1319 /* The resources controlling the menu-bar and tool-bar are
1320 processed specially at startup, and reflected in the mode
1321 variables; ignore them here. */
1322 x_default_parameter (f, parms, Qmenu_bar_lines,
1323 NILP (Vmenu_bar_mode)
1324 ? make_number (0) : make_number (1),
1325 NULL, NULL, RES_TYPE_NUMBER);
1326 x_default_parameter (f, parms, Qtool_bar_lines,
1327 NILP (Vtool_bar_mode)
1328 ? make_number (0) : make_number (1),
1329 NULL, NULL, RES_TYPE_NUMBER);
1331 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1332 "BufferPredicate", RES_TYPE_SYMBOL);
1333 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1336 parms = get_geometry_from_preferences (dpyinfo, parms);
1337 window_prompting = x_figure_window_size (f, parms, true, &x_width, &x_height);
1339 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1340 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1342 /* NOTE: on other terms, this is done in set_mouse_color, however this
1343 was not getting called under Nextstep */
1344 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1345 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1346 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1347 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1348 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1349 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1350 f->output_data.ns->vertical_drag_cursor = [NSCursor resizeUpDownCursor];
1351 f->output_data.ns->left_edge_cursor = [NSCursor resizeLeftRightCursor];
1352 f->output_data.ns->top_left_corner_cursor = [NSCursor arrowCursor];
1353 f->output_data.ns->top_edge_cursor = [NSCursor resizeUpDownCursor];
1354 f->output_data.ns->top_right_corner_cursor = [NSCursor arrowCursor];
1355 f->output_data.ns->right_edge_cursor = [NSCursor resizeLeftRightCursor];
1356 f->output_data.ns->bottom_right_corner_cursor = [NSCursor arrowCursor];
1357 f->output_data.ns->bottom_edge_cursor = [NSCursor resizeUpDownCursor];
1358 f->output_data.ns->bottom_left_corner_cursor = [NSCursor arrowCursor];
1360 FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1361 = [NSCursor arrowCursor];
1362 FRAME_DISPLAY_INFO (f)->horizontal_scroll_bar_cursor
1363 = [NSCursor arrowCursor];
1364 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1366 f->output_data.ns->in_animation = NO;
1368 [[EmacsView alloc] initFrameFromEmacs: f];
1372 /* ns_display_info does not have a reference_count. */
1373 f->terminal->reference_count++;
1375 /* It is now ok to make the frame official even if we get an error below.
1376 The frame needs to be on Vframe_list or making it visible won't work. */
1377 Vframe_list = Fcons (frame, Vframe_list);
1379 x_default_parameter (f, parms, Qicon_type, Qnil,
1380 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1382 x_default_parameter (f, parms, Qauto_raise, Qnil,
1383 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1384 x_default_parameter (f, parms, Qauto_lower, Qnil,
1385 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1386 x_default_parameter (f, parms, Qcursor_type, Qbox,
1387 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1388 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1389 "scrollBarWidth", "ScrollBarWidth",
1391 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
1392 "scrollBarHeight", "ScrollBarHeight",
1394 x_default_parameter (f, parms, Qalpha, Qnil,
1395 "alpha", "Alpha", RES_TYPE_NUMBER);
1396 x_default_parameter (f, parms, Qfullscreen, Qnil,
1397 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1399 /* Allow x_set_window_size, now. */
1400 f->can_x_set_window_size = true;
1403 SET_FRAME_WIDTH (f, x_width);
1405 SET_FRAME_HEIGHT (f, x_height);
1407 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1,
1410 if (! f->output_data.ns->explicit_parent)
1412 Lisp_Object visibility;
1414 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1416 if (EQ (visibility, Qunbound))
1419 if (EQ (visibility, Qicon))
1420 x_iconify_frame (f);
1421 else if (! NILP (visibility))
1423 x_make_frame_visible (f);
1424 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1428 /* Must have been Qnil. */
1432 if (FRAME_HAS_MINIBUF_P (f)
1433 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1434 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1435 kset_default_minibuffer_frame (kb, frame);
1437 /* All remaining specified parameters, which have not been "used"
1438 by x_get_arg and friends, now go in the misc. alist of the frame. */
1439 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1440 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1441 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1443 if (window_prompting & USPosition)
1444 x_set_offset (f, f->left_pos, f->top_pos, 1);
1446 /* Make sure windows on this frame appear in calls to next-window
1447 and similar functions. */
1448 Vwindow_list = Qnil;
1450 return unbind_to (count, frame);
1454 x_focus_frame (struct frame *f, bool noactivate)
1456 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1458 if (dpyinfo->x_focus_frame != f)
1460 EmacsView *view = FRAME_NS_VIEW (f);
1462 [NSApp activateIgnoringOtherApps: YES];
1463 [[view window] makeKeyAndOrderFront: view];
1469 ns_window_is_ancestor (NSWindow *win, NSWindow *candidate)
1470 /* Test whether CANDIDATE is an ancestor window of WIN. */
1472 if (candidate == NULL)
1474 else if (win == candidate)
1477 return ns_window_is_ancestor(win, [candidate parentWindow]);
1480 DEFUN ("ns-frame-list-z-order", Fns_frame_list_z_order,
1481 Sns_frame_list_z_order, 0, 1, 0,
1482 doc: /* Return list of Emacs' frames, in Z (stacking) order.
1483 If TERMINAL is non-nil and specifies a live frame, return the child
1484 frames of that frame in Z (stacking) order.
1486 Frames are listed from topmost (first) to bottommost (last). */)
1487 (Lisp_Object terminal)
1489 Lisp_Object frames = Qnil;
1490 NSWindow *parent = nil;
1492 if (FRAMEP (terminal) && FRAME_LIVE_P (XFRAME (terminal)))
1493 parent = [FRAME_NS_VIEW (XFRAME (terminal)) window];
1495 for (NSWindow *win in [[NSApp orderedWindows] reverseObjectEnumerator])
1499 /* Check against [win parentWindow] so that it doesn't match itself. */
1500 if (parent == nil || ns_window_is_ancestor (parent, [win parentWindow]))
1502 XSETFRAME (frame, ((EmacsView *)[win delegate])->emacsframe);
1503 frames = Fcons(frame, frames);
1510 DEFUN ("ns-frame-restack", Fns_frame_restack, Sns_frame_restack, 2, 3, 0,
1511 doc: /* Restack FRAME1 below FRAME2.
1512 This means that if both frames are visible and the display areas of
1513 these frames overlap, FRAME2 (partially) obscures FRAME1. If optional
1514 third argument ABOVE is non-nil, restack FRAME1 above FRAME2. This
1515 means that if both frames are visible and the display areas of these
1516 frames overlap, FRAME1 (partially) obscures FRAME2.
1518 Some window managers may refuse to restack windows. */)
1519 (Lisp_Object frame1, Lisp_Object frame2, Lisp_Object above)
1521 struct frame *f1 = decode_live_frame (frame1);
1522 struct frame *f2 = decode_live_frame (frame2);
1524 if (FRAME_NS_VIEW (f1) && FRAME_NS_VIEW (f2))
1526 NSWindow *window = [FRAME_NS_VIEW (f1) window];
1527 NSInteger window2 = [[FRAME_NS_VIEW (f2) window] windowNumber];
1528 NSWindowOrderingMode flag = NILP (above) ? NSWindowBelow : NSWindowAbove;
1530 [window orderWindow: flag
1531 relativeTo: window2];
1537 error ("Cannot restack frames");
1542 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1544 doc: /* Pop up the font panel. */)
1547 struct frame *f = decode_window_system_frame (frame);
1548 id fm = [NSFontManager sharedFontManager];
1549 struct font *font = f->output_data.ns->font;
1551 #ifdef NS_IMPL_GNUSTEP
1552 nsfont = ((struct nsfont_info *)font)->nsfont;
1554 #ifdef NS_IMPL_COCOA
1555 nsfont = (NSFont *) macfont_get_nsctfont (font);
1557 [fm setSelectedFont: nsfont isMultiple: NO];
1558 [fm orderFrontFontPanel: NSApp];
1563 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1565 doc: /* Pop up the color panel. */)
1568 check_window_system (NULL);
1569 [NSApp orderFrontColorPanel: NSApp];
1577 #ifdef NS_IMPL_GNUSTEP
1578 NSString *dirS, *initS;
1584 ns_run_file_dialog (void)
1586 if (ns_fd_data.panel == nil) return;
1587 #ifdef NS_IMPL_COCOA
1588 ns_fd_data.ret = [ns_fd_data.panel runModal];
1590 if (ns_fd_data.no_types)
1592 ns_fd_data.ret = [ns_fd_data.panel
1593 runModalForDirectory: ns_fd_data.dirS
1594 file: ns_fd_data.initS];
1598 ns_fd_data.ret = [ns_fd_data.panel
1599 runModalForDirectory: ns_fd_data.dirS
1600 file: ns_fd_data.initS
1604 ns_fd_data.panel = nil;
1607 #ifdef NS_IMPL_COCOA
1608 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1090
1609 #define MODAL_OK_RESPONSE NSModalResponseOK
1612 #ifndef MODAL_OK_RESPONSE
1613 #define MODAL_OK_RESPONSE NSOKButton
1616 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1617 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1618 Optional arg DIR, if non-nil, supplies a default directory.
1619 Optional arg MUSTMATCH, if non-nil, means the returned file or
1620 directory must exist.
1621 Optional arg INIT, if non-nil, provides a default file name to use.
1622 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1623 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1624 Lisp_Object init, Lisp_Object dir_only_p)
1626 static id fileDelegate = nil;
1627 BOOL isSave = NILP (mustmatch) && NILP (dir_only_p);
1629 Lisp_Object fname = Qnil;
1631 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1632 [NSString stringWithUTF8String: SSDATA (prompt)];
1633 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1634 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1635 [NSString stringWithUTF8String: SSDATA (dir)];
1636 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1637 [NSString stringWithUTF8String: SSDATA (init)];
1640 check_window_system (NULL);
1642 if (fileDelegate == nil)
1643 fileDelegate = [EmacsFileDelegate new];
1645 [NSCursor setHiddenUntilMouseMoves: NO];
1647 if ([dirS characterAtIndex: 0] == '~')
1648 dirS = [dirS stringByExpandingTildeInPath];
1651 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1653 [panel setTitle: promptS];
1655 [panel setAllowsOtherFileTypes: YES];
1656 [panel setTreatsFilePackagesAsDirectories: YES];
1657 [panel setDelegate: fileDelegate];
1659 if (! NILP (dir_only_p))
1661 [panel setCanChooseDirectories: YES];
1662 [panel setCanChooseFiles: NO];
1666 /* This is not quite what the documentation says, but it is compatible
1667 with the Gtk+ code. Also, the menu entry says "Open File...". */
1668 [panel setCanChooseDirectories: NO];
1669 [panel setCanChooseFiles: YES];
1673 ns_fd_data.panel = panel;
1674 ns_fd_data.ret = NO;
1675 #ifdef NS_IMPL_COCOA
1676 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1677 [panel setAllowedFileTypes: nil];
1678 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1679 if (initS && NILP (Ffile_directory_p (init)))
1680 [panel setNameFieldStringValue: [initS lastPathComponent]];
1682 [panel setNameFieldStringValue: @""];
1685 ns_fd_data.no_types = NILP (mustmatch) && NILP (dir_only_p);
1686 ns_fd_data.dirS = dirS;
1687 ns_fd_data.initS = initS;
1690 /* runModalForDirectory/runModal restarts the main event loop when done,
1691 so we must start an event loop and then pop up the file dialog.
1692 The file dialog may pop up a confirm dialog after Ok has been pressed,
1693 so we can not simply pop down on the Ok/Cancel press.
1695 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
1696 location: NSMakePoint (0, 0)
1699 windowNumber: [[NSApp mainWindow] windowNumber]
1700 context: [NSApp context]
1703 data2: NSAPP_DATA2_RUNFILEDIALOG];
1705 [NSApp postEvent: nxev atStart: NO];
1706 while (ns_fd_data.panel != nil)
1709 if (ns_fd_data.ret == MODAL_OK_RESPONSE)
1711 NSString *str = ns_filename_from_panel (panel);
1712 if (! str) str = ns_directory_from_panel (panel);
1713 if (str) fname = build_string ([str UTF8String]);
1716 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1723 ns_get_defaults_value (const char *key)
1725 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1726 objectForKey: [NSString stringWithUTF8String: key]];
1728 if (!obj) return NULL;
1730 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1734 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1735 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1736 If OWNER is nil, Emacs is assumed. */)
1737 (Lisp_Object owner, Lisp_Object name)
1741 check_window_system (NULL);
1743 owner = build_string([ns_app_name UTF8String]);
1744 CHECK_STRING (name);
1746 value = ns_get_defaults_value (SSDATA (name));
1749 return build_string (value);
1754 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1755 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1756 If OWNER is nil, Emacs is assumed.
1757 If VALUE is nil, the default is removed. */)
1758 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1760 check_window_system (NULL);
1762 owner = build_string ([ns_app_name UTF8String]);
1763 CHECK_STRING (name);
1766 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1767 [NSString stringWithUTF8String: SSDATA (name)]];
1771 CHECK_STRING (value);
1772 [[NSUserDefaults standardUserDefaults] setObject:
1773 [NSString stringWithUTF8String: SSDATA (value)]
1774 forKey: [NSString stringWithUTF8String:
1782 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1783 Sx_server_max_request_size,
1785 doc: /* This function is a no-op. It is only present for completeness. */)
1786 (Lisp_Object terminal)
1788 check_ns_display_info (terminal);
1789 /* This function has no real equivalent under NeXTstep. Return nil to
1795 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1796 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1797 \(Labeling every distributor as a "vendor" embodies the false assumption
1798 that operating systems cannot be developed and distributed noncommercially.)
1799 The optional argument TERMINAL specifies which display to ask about.
1800 TERMINAL should be a terminal object, a frame or a display name (a string).
1801 If omitted or nil, that stands for the selected frame's display. */)
1802 (Lisp_Object terminal)
1804 check_ns_display_info (terminal);
1805 #ifdef NS_IMPL_GNUSTEP
1806 return build_string ("GNU");
1808 return build_string ("Apple");
1813 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1814 doc: /* Return the version numbers of the server of display TERMINAL.
1815 The value is a list of three integers: the major and minor
1816 version numbers of the X Protocol in use, and the distributor-specific release
1817 number. See also the function `x-server-vendor'.
1819 The optional argument TERMINAL specifies which display to ask about.
1820 TERMINAL should be a terminal object, a frame or a display name (a string).
1821 If omitted or nil, that stands for the selected frame's display. */)
1822 (Lisp_Object terminal)
1824 check_ns_display_info (terminal);
1825 /*NOTE: it is unclear what would best correspond with "protocol";
1826 we return 10.3, meaning Panther, since this is roughly the
1827 level that GNUstep's APIs correspond to.
1828 The last number is where we distinguish between the Apple
1829 and GNUstep implementations ("distributor-specific release
1830 number") and give int'ized versions of major.minor. */
1831 return list3i (10, 3, ns_appkit_version_int ());
1835 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1836 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1837 The optional argument TERMINAL specifies which display to ask about.
1838 TERMINAL should be a terminal object, a frame or a display name (a string).
1839 If omitted or nil, that stands for the selected frame's display.
1841 Note: "screen" here is not in Nextstep terminology but in X11's. For
1842 the number of physical monitors, use `(length
1843 \(display-monitor-attributes-list TERMINAL))' instead. */)
1844 (Lisp_Object terminal)
1846 check_ns_display_info (terminal);
1847 return make_number (1);
1851 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1852 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1853 The optional argument TERMINAL specifies which display to ask about.
1854 TERMINAL should be a terminal object, a frame or a display name (a string).
1855 If omitted or nil, that stands for the selected frame's display.
1857 On \"multi-monitor\" setups this refers to the height in millimeters for
1858 all physical monitors associated with TERMINAL. To get information
1859 for each physical monitor, use `display-monitor-attributes-list'. */)
1860 (Lisp_Object terminal)
1862 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1864 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1868 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1869 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1870 The optional argument TERMINAL specifies which display to ask about.
1871 TERMINAL should be a terminal object, a frame or a display name (a string).
1872 If omitted or nil, that stands for the selected frame's display.
1874 On \"multi-monitor\" setups this refers to the width in millimeters for
1875 all physical monitors associated with TERMINAL. To get information
1876 for each physical monitor, use `display-monitor-attributes-list'. */)
1877 (Lisp_Object terminal)
1879 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1881 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1885 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1886 Sx_display_backing_store, 0, 1, 0,
1887 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1888 The value may be `buffered', `retained', or `non-retained'.
1889 The optional argument TERMINAL specifies which display to ask about.
1890 TERMINAL should be a terminal object, a frame or a display name (a string).
1891 If omitted or nil, that stands for the selected frame's display. */)
1892 (Lisp_Object terminal)
1894 check_ns_display_info (terminal);
1895 switch ([ns_get_window (terminal) backingType])
1897 case NSBackingStoreBuffered:
1898 return intern ("buffered");
1899 #if defined (NS_IMPL_GNUSTEP) || MAC_OS_X_VERSION_MIN_REQUIRED < 101300
1900 case NSBackingStoreRetained:
1901 return intern ("retained");
1902 case NSBackingStoreNonretained:
1903 return intern ("non-retained");
1906 error ("Strange value for backingType parameter of frame");
1908 return Qnil; /* not reached, shut compiler up */
1912 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1913 Sx_display_visual_class, 0, 1, 0,
1914 doc: /* Return the visual class of the Nextstep display TERMINAL.
1915 The value is one of the symbols `static-gray', `gray-scale',
1916 `static-color', `pseudo-color', `true-color', or `direct-color'.
1918 The optional argument TERMINAL specifies which display to ask about.
1919 TERMINAL should a terminal object, a frame or a display name (a string).
1920 If omitted or nil, that stands for the selected frame's display. */)
1921 (Lisp_Object terminal)
1923 NSWindowDepth depth;
1925 check_ns_display_info (terminal);
1926 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1928 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1929 return intern ("static-gray");
1930 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1931 return intern ("gray-scale");
1932 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1933 return intern ("pseudo-color");
1934 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1935 return intern ("true-color");
1936 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1937 return intern ("direct-color");
1939 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1940 return intern ("direct-color");
1944 DEFUN ("x-display-save-under", Fx_display_save_under,
1945 Sx_display_save_under, 0, 1, 0,
1946 doc: /* Return t if TERMINAL supports the save-under feature.
1947 The optional argument TERMINAL specifies which display to ask about.
1948 TERMINAL should be a terminal object, a frame or a display name (a string).
1949 If omitted or nil, that stands for the selected frame's display. */)
1950 (Lisp_Object terminal)
1952 check_ns_display_info (terminal);
1953 switch ([ns_get_window (terminal) backingType])
1955 case NSBackingStoreBuffered:
1958 #if defined (NS_IMPL_GNUSTEP) || MAC_OS_X_VERSION_MIN_REQUIRED < 101300
1959 case NSBackingStoreRetained:
1960 case NSBackingStoreNonretained:
1965 error ("Strange value for backingType parameter of frame");
1967 return Qnil; /* not reached, shut compiler up */
1971 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1973 doc: /* Open a connection to a display server.
1974 DISPLAY is the name of the display to connect to.
1975 Optional second arg XRM-STRING is a string of resources in xrdb format.
1976 If the optional third arg MUST-SUCCEED is non-nil,
1977 terminate Emacs if we can't open the connection.
1978 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1979 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1981 struct ns_display_info *dpyinfo;
1983 CHECK_STRING (display);
1985 nxatoms_of_nsselect ();
1986 dpyinfo = ns_term_init (display);
1989 if (!NILP (must_succeed))
1990 fatal ("Display on %s not responding.\n",
1993 error ("Display on %s not responding.\n",
2001 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
2003 doc: /* Close the connection to TERMINAL's Nextstep display server.
2004 For TERMINAL, specify a terminal object, a frame or a display name (a
2005 string). If TERMINAL is nil, that stands for the selected frame's
2007 (Lisp_Object terminal)
2009 check_ns_display_info (terminal);
2010 [NSApp terminate: NSApp];
2015 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
2016 doc: /* Return the list of display names that Emacs has connections to. */)
2019 Lisp_Object result = Qnil;
2020 struct ns_display_info *ndi;
2022 for (ndi = x_display_list; ndi; ndi = ndi->next)
2023 result = Fcons (XCAR (ndi->name_list_element), result);
2029 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
2031 doc: /* Hides all applications other than Emacs. */)
2034 check_window_system (NULL);
2035 [NSApp hideOtherApplications: NSApp];
2039 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
2041 doc: /* If ON is non-nil, the entire Emacs application is hidden.
2042 Otherwise if Emacs is hidden, it is unhidden.
2043 If ON is equal to `activate', Emacs is unhidden and becomes
2044 the active application. */)
2047 check_window_system (NULL);
2048 if (EQ (on, intern ("activate")))
2050 [NSApp unhide: NSApp];
2051 [NSApp activateIgnoringOtherApps: YES];
2054 [NSApp unhide: NSApp];
2056 [NSApp hide: NSApp];
2061 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
2063 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
2066 check_window_system (NULL);
2067 [NSApp orderFrontStandardAboutPanel: nil];
2072 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
2073 doc: /* Determine font PostScript or family name for font NAME.
2074 NAME should be a string containing either the font name or an XLFD
2075 font descriptor. If string contains `fontset' and not
2076 `fontset-startup', it is left alone. */)
2080 CHECK_STRING (name);
2085 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
2088 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
2092 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
2093 doc: /* Return a list of all available colors.
2094 The optional argument FRAME is currently ignored. */)
2097 Lisp_Object list = Qnil;
2098 NSEnumerator *colorlists;
2103 CHECK_FRAME (frame);
2104 if (! FRAME_NS_P (XFRAME (frame)))
2105 error ("non-Nextstep frame used in `ns-list-colors'");
2110 colorlists = [[NSColorList availableColorLists] objectEnumerator];
2111 while ((clist = [colorlists nextObject]))
2113 if ([[clist name] length] < 7 ||
2114 [[clist name] rangeOfString: @"PANTONE"].location == 0)
2116 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
2118 while ((cname = [cnames nextObject]))
2119 list = Fcons (build_string ([cname UTF8String]), list);
2120 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
2121 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
2122 UTF8String]), list); */
2132 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
2133 doc: /* List available Nextstep services by querying NSApp. */)
2136 #ifdef NS_IMPL_COCOA
2137 /* You can't get services like this in 10.6+. */
2140 Lisp_Object ret = Qnil;
2143 check_window_system (NULL);
2144 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
2145 [NSApp setServicesMenu: svcs];
2146 [NSApp registerServicesMenuSendTypes: ns_send_types
2147 returnTypes: ns_return_types];
2149 [svcs setAutoenablesItems: NO];
2151 ret = interpret_services_menu (svcs, Qnil, ret);
2157 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2159 doc: /* Perform Nextstep SERVICE on SEND.
2160 SEND should be either a string or nil.
2161 The return value is the result of the service, as string, or nil if
2162 there was no result. */)
2163 (Lisp_Object service, Lisp_Object send)
2169 CHECK_STRING (service);
2170 check_window_system (NULL);
2172 utfStr = SSDATA (service);
2173 svcName = [NSString stringWithUTF8String: utfStr];
2175 pb =[NSPasteboard pasteboardWithUniqueName];
2176 ns_string_to_pasteboard (pb, send);
2178 if (NSPerformService (svcName, pb) == NO)
2179 Fsignal (Qquit, list1 (build_string ("service not available")));
2181 if ([[pb types] count] == 0)
2182 return build_string ("");
2183 return ns_string_from_pasteboard (pb);
2187 #ifdef NS_IMPL_COCOA
2189 /* Compile and execute the AppleScript SCRIPT and return the error
2190 status as function value. A zero is returned if compilation and
2191 execution is successful, in which case *RESULT is set to a Lisp
2192 string or a number containing the resulting script value. Otherwise,
2195 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2197 NSAppleEventDescriptor *desc;
2198 NSDictionary *errorDict;
2199 NSAppleEventDescriptor *returnDescriptor = NULL;
2201 NSAppleScript *scriptObject =
2202 [[NSAppleScript alloc] initWithSource:
2203 [NSString stringWithUTF8String: SSDATA (script)]];
2205 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2206 [scriptObject release];
2209 if (returnDescriptor != NULL)
2211 // successful execution
2212 if (kAENullEvent != [returnDescriptor descriptorType])
2215 // script returned an AppleScript result
2216 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2217 #if defined (NS_IMPL_COCOA)
2218 (typeUTF16ExternalRepresentation
2219 == [returnDescriptor descriptorType]) ||
2221 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2222 (typeCString == [returnDescriptor descriptorType]))
2224 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2226 *result = build_string([[desc stringValue] UTF8String]);
2230 /* use typeUTF16ExternalRepresentation? */
2231 // coerce the result to the appropriate ObjC type
2232 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2234 *result = make_number([desc int32Value]);
2240 // no script result, return error
2246 /* Helper function called from sendEvent to run applescript
2247 from within the main event loop. */
2250 ns_run_ascript (void)
2252 if (! NILP (as_script))
2253 as_status = ns_do_applescript (as_script, as_result);
2257 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2258 doc: /* Execute AppleScript SCRIPT and return the result.
2259 If compilation and execution are successful, the resulting script value
2260 is returned as a string, a number or, in the case of other constructs, t.
2261 In case the execution fails, an error is signaled. */)
2262 (Lisp_Object script)
2267 struct input_event ev;
2269 CHECK_STRING (script);
2270 check_window_system (NULL);
2275 as_result = &result;
2277 /* executing apple script requires the event loop to run, otherwise
2278 errors aren't returned and executeAndReturnError hangs forever.
2279 Post an event that runs applescript and then start the event loop.
2280 The event loop is exited when the script is done. */
2281 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
2282 location: NSMakePoint (0, 0)
2285 windowNumber: [[NSApp mainWindow] windowNumber]
2286 context: [NSApp context]
2289 data2: NSAPP_DATA2_RUNASSCRIPT];
2291 [NSApp postEvent: nxev atStart: NO];
2293 // If there are other events, the event loop may exit. Keep running
2294 // until the script has been handled. */
2295 ns_init_events (&ev);
2296 while (! NILP (as_script))
2298 ns_finish_events ();
2306 else if (!STRINGP (result))
2307 error ("AppleScript error %d", status);
2309 error ("%s", SSDATA (result));
2315 /* ==========================================================================
2317 Miscellaneous functions not called through hooks
2319 ========================================================================== */
2321 /* called from frame.c */
2322 struct ns_display_info *
2323 check_x_display_info (Lisp_Object frame)
2325 return check_ns_display_info (frame);
2330 x_set_scroll_bar_default_width (struct frame *f)
2332 int wid = FRAME_COLUMN_WIDTH (f);
2333 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2334 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2339 x_set_scroll_bar_default_height (struct frame *f)
2341 int height = FRAME_LINE_HEIGHT (f);
2342 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2343 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) +
2344 height - 1) / height;
2347 /* terms impl this instead of x-get-resource directly */
2349 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
2351 /* remove appname prefix; TODO: allow for !="Emacs" */
2352 const char *res, *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2354 check_window_system (NULL);
2356 if (inhibit_x_resources)
2357 /* --quick was passed, so this is a no-op. */
2360 res = ns_get_defaults_value (toCheck);
2361 return (char *) (!res ? NULL
2362 : !c_strncasecmp (res, "YES", 3) ? "true"
2363 : !c_strncasecmp (res, "NO", 2) ? "false"
2369 x_get_focus_frame (struct frame *frame)
2371 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2372 Lisp_Object nsfocus;
2374 if (!dpyinfo->x_focus_frame)
2377 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2381 /* ==========================================================================
2383 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2385 ========================================================================== */
2388 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2389 doc: /* Internal function called by `color-defined-p', which see.
2390 \(Note that the Nextstep version of this function ignores FRAME.) */)
2391 (Lisp_Object color, Lisp_Object frame)
2394 check_window_system (NULL);
2395 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2399 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2400 doc: /* Internal function called by `color-values', which see. */)
2401 (Lisp_Object color, Lisp_Object frame)
2404 EmacsCGFloat red, green, blue, alpha;
2406 check_window_system (NULL);
2407 CHECK_STRING (color);
2410 if (ns_lisp_to_color (color, &col))
2416 [[col colorUsingDefaultColorSpace]
2417 getRed: &red green: &green blue: &blue alpha: &alpha];
2419 return list3i (lrint (red * 65280), lrint (green * 65280),
2420 lrint (blue * 65280));
2424 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2425 doc: /* Internal function called by `display-color-p', which see. */)
2426 (Lisp_Object terminal)
2428 NSWindowDepth depth;
2429 NSString *colorSpace;
2431 check_ns_display_info (terminal);
2432 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2433 colorSpace = NSColorSpaceFromDepth (depth);
2435 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2436 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2441 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2443 doc: /* Return t if the Nextstep display supports shades of gray.
2444 Note that color displays do support shades of gray.
2445 The optional argument TERMINAL specifies which display to ask about.
2446 TERMINAL should be a terminal object, a frame or a display name (a string).
2447 If omitted or nil, that stands for the selected frame's display. */)
2448 (Lisp_Object terminal)
2450 NSWindowDepth depth;
2452 check_ns_display_info (terminal);
2453 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2455 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2459 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2461 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2462 The optional argument TERMINAL specifies which display to ask about.
2463 TERMINAL should be a terminal object, a frame or a display name (a string).
2464 If omitted or nil, that stands for the selected frame's display.
2466 On \"multi-monitor\" setups this refers to the pixel width for all
2467 physical monitors associated with TERMINAL. To get information for
2468 each physical monitor, use `display-monitor-attributes-list'. */)
2469 (Lisp_Object terminal)
2471 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2473 return make_number (x_display_pixel_width (dpyinfo));
2477 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2478 Sx_display_pixel_height, 0, 1, 0,
2479 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2480 The optional argument TERMINAL specifies which display to ask about.
2481 TERMINAL should be a terminal object, a frame or a display name (a string).
2482 If omitted or nil, that stands for the selected frame's display.
2484 On \"multi-monitor\" setups this refers to the pixel height for all
2485 physical monitors associated with TERMINAL. To get information for
2486 each physical monitor, use `display-monitor-attributes-list'. */)
2487 (Lisp_Object terminal)
2489 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2491 return make_number (x_display_pixel_height (dpyinfo));
2494 #ifdef NS_IMPL_COCOA
2496 /* Returns the name for the screen that OBJ represents, or NULL.
2497 Caller must free return value.
2501 ns_get_name_from_ioreg (io_object_t obj)
2505 NSDictionary *info = (NSDictionary *)
2506 IODisplayCreateInfoDictionary (obj, kIODisplayOnlyPreferredName);
2507 NSDictionary *names = [info objectForKey:
2508 [NSString stringWithUTF8String:
2509 kDisplayProductName]];
2511 if ([names count] > 0)
2513 NSString *n = [names objectForKey: [[names allKeys]
2515 if (n != nil) name = xstrdup ([n UTF8String]);
2523 /* Returns the name for the screen that DID came from, or NULL.
2524 Caller must free return value.
2528 ns_screen_name (CGDirectDisplayID did)
2532 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
2533 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2534 if (CGDisplayIOServicePort == NULL)
2537 mach_port_t masterPort;
2541 /* CGDisplayIOServicePort is deprecated. Do it another (harder) way.
2543 Is this code OK for macOS < 10.9, and GNUstep? I suspect it is,
2544 in which case is it worth keeping the other method in here? */
2546 if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
2547 || IOServiceGetMatchingServices (masterPort,
2548 IOServiceMatching ("IONDRVDevice"),
2549 &it) != kIOReturnSuccess)
2552 /* Must loop until we find a name. Many devices can have the same unit
2553 number (represents different GPU parts), but only one has a name. */
2554 while (! name && (obj = IOIteratorNext (it)))
2556 CFMutableDictionaryRef props;
2559 if (IORegistryEntryCreateCFProperties (obj,
2561 kCFAllocatorDefault,
2562 kNilOptions) == kIOReturnSuccess
2564 && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
2566 unsigned nr = [(NSNumber *)val unsignedIntegerValue];
2567 if (nr == CGDisplayUnitNumber (did))
2568 name = ns_get_name_from_ioreg (obj);
2572 IOObjectRelease (obj);
2575 IOObjectRelease (it);
2577 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2580 #endif /* #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 */
2581 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2582 name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
2586 #endif /* NS_IMPL_COCOA */
2589 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2591 int primary_monitor,
2594 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2595 Lisp_Object frame, rest;
2596 NSArray *screens = [NSScreen screens];
2599 FOR_EACH_FRAME (rest, frame)
2601 struct frame *f = XFRAME (frame);
2605 NSView *view = FRAME_NS_VIEW (f);
2606 NSScreen *screen = [[view window] screen];
2610 for (k = 0; i == -1 && k < [screens count]; ++k)
2612 if ([screens objectAtIndex: k] == screen)
2617 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2621 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2622 monitor_frames, source);
2625 DEFUN ("ns-display-monitor-attributes-list",
2626 Fns_display_monitor_attributes_list,
2627 Sns_display_monitor_attributes_list,
2629 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2631 The optional argument TERMINAL specifies which display to ask about.
2632 TERMINAL should be a terminal object, a frame or a display name (a string).
2633 If omitted or nil, that stands for the selected frame's display.
2635 In addition to the standard attribute keys listed in
2636 `display-monitor-attributes-list', the following keys are contained in
2639 source -- String describing the source from which multi-monitor
2640 information is obtained, \"NS\" is always the source."
2642 Internal use only, use `display-monitor-attributes-list' instead. */)
2643 (Lisp_Object terminal)
2645 struct terminal *term = decode_live_terminal (terminal);
2647 NSUInteger i, n_monitors;
2648 struct MonitorInfo *monitors;
2649 Lisp_Object attributes_list = Qnil;
2650 CGFloat primary_display_height = 0;
2652 if (term->type != output_ns)
2655 screens = [NSScreen screens];
2656 n_monitors = [screens count];
2657 if (n_monitors == 0)
2660 monitors = xzalloc (n_monitors * sizeof *monitors);
2662 for (i = 0; i < [screens count]; ++i)
2664 NSScreen *s = [screens objectAtIndex:i];
2665 struct MonitorInfo *m = &monitors[i];
2666 NSRect fr = [s frame];
2667 NSRect vfr = [s visibleFrame];
2670 #ifdef NS_IMPL_COCOA
2671 NSDictionary *dict = [s deviceDescription];
2672 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2673 CGDirectDisplayID did = [nid unsignedIntValue];
2677 primary_display_height = fr.size.height;
2678 y = (short) fr.origin.y;
2679 vy = (short) vfr.origin.y;
2683 // Flip y coordinate as NS has y starting from the bottom.
2684 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2685 vy = (short) (primary_display_height -
2686 vfr.size.height - vfr.origin.y);
2689 m->geom.x = (short) fr.origin.x;
2691 m->geom.width = (unsigned short) fr.size.width;
2692 m->geom.height = (unsigned short) fr.size.height;
2694 m->work.x = (short) vfr.origin.x;
2695 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2696 // and fr.size.height - vfr.size.height are pixels missing in total.
2697 // Pixels missing at top are
2698 // fr.size.height - vfr.size.height - vy + y.
2699 // work.y is then pixels missing at top + y.
2700 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2701 m->work.width = (unsigned short) vfr.size.width;
2702 m->work.height = (unsigned short) vfr.size.height;
2704 #ifdef NS_IMPL_COCOA
2705 m->name = ns_screen_name (did);
2708 CGSize mms = CGDisplayScreenSize (did);
2709 m->mm_width = (int) mms.width;
2710 m->mm_height = (int) mms.height;
2714 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2715 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2716 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2720 // Primary monitor is always first for NS.
2721 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2724 free_monitors (monitors, n_monitors);
2725 return attributes_list;
2729 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2731 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2732 The optional argument TERMINAL specifies which display to ask about.
2733 TERMINAL should be a terminal object, a frame or a display name (a string).
2734 If omitted or nil, that stands for the selected frame's display. */)
2735 (Lisp_Object terminal)
2737 check_ns_display_info (terminal);
2739 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2743 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2745 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2746 The optional argument TERMINAL specifies which display to ask about.
2747 TERMINAL should be a terminal object, a frame or a display name (a string).
2748 If omitted or nil, that stands for the selected frame's display. */)
2749 (Lisp_Object terminal)
2751 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2752 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2753 return make_number (1 << min (dpyinfo->n_planes, 24));
2756 /* TODO: move to xdisp or similar */
2758 compute_tip_xy (struct frame *f,
2767 Lisp_Object left, top, right, bottom;
2771 /* Start with user-specified or mouse position. */
2772 left = Fcdr (Fassq (Qleft, parms));
2773 top = Fcdr (Fassq (Qtop, parms));
2774 right = Fcdr (Fassq (Qright, parms));
2775 bottom = Fcdr (Fassq (Qbottom, parms));
2777 if ((!INTEGERP (left) && !INTEGERP (right))
2778 || (!INTEGERP (top) && !INTEGERP (bottom)))
2779 pt = [NSEvent mouseLocation];
2782 /* Absolute coordinates. */
2783 pt.x = INTEGERP (left) ? XINT (left) : XINT (right);
2784 pt.y = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
2785 - (INTEGERP (top) ? XINT (top) : XINT (bottom))
2789 /* Find the screen that pt is on. */
2790 for (screen in [NSScreen screens])
2791 if (pt.x >= screen.frame.origin.x
2792 && pt.x < screen.frame.origin.x + screen.frame.size.width
2793 && pt.y >= screen.frame.origin.y
2794 && pt.y < screen.frame.origin.y + screen.frame.size.height)
2797 /* We could use this instead of the if above:
2799 if (CGRectContainsPoint ([screen frame], pt))
2801 which would be neater, but it causes problems building on old
2802 versions of macOS and in GNUstep. */
2804 /* Ensure in bounds. (Note, screen origin = lower left.) */
2805 if (INTEGERP (left) || INTEGERP (right))
2807 else if (pt.x + XINT (dx) <= screen.frame.origin.x)
2808 *root_x = screen.frame.origin.x; /* Can happen for negative dx */
2809 else if (pt.x + XINT (dx) + width
2810 <= screen.frame.origin.x + screen.frame.size.width)
2811 /* It fits to the right of the pointer. */
2812 *root_x = pt.x + XINT (dx);
2813 else if (width + XINT (dx) <= pt.x)
2814 /* It fits to the left of the pointer. */
2815 *root_x = pt.x - width - XINT (dx);
2817 /* Put it left justified on the screen -- it ought to fit that way. */
2818 *root_x = screen.frame.origin.x;
2820 if (INTEGERP (top) || INTEGERP (bottom))
2822 else if (pt.y - XINT (dy) - height >= screen.frame.origin.y)
2823 /* It fits below the pointer. */
2824 *root_y = pt.y - height - XINT (dy);
2825 else if (pt.y + XINT (dy) + height
2826 <= screen.frame.origin.y + screen.frame.size.height)
2827 /* It fits above the pointer */
2828 *root_y = pt.y + XINT (dy);
2830 /* Put it on the top. */
2831 *root_y = screen.frame.origin.y + screen.frame.size.height - height;
2835 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2836 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2837 A tooltip window is a small window displaying a string.
2839 This is an internal function; Lisp code should call `tooltip-show'.
2841 FRAME nil or omitted means use the selected frame.
2843 PARMS is an optional list of frame parameters which can be used to
2844 change the tooltip's appearance.
2846 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2847 means use the default timeout of 5 seconds.
2849 If the list of frame parameters PARMS contains a `left' parameter,
2850 display the tooltip at that x-position. If the list of frame parameters
2851 PARMS contains no `left' but a `right' parameter, display the tooltip
2852 right-adjusted at that x-position. Otherwise display it at the
2853 x-position of the mouse, with offset DX added (default is 5 if DX isn't
2856 Likewise for the y-position: If a `top' frame parameter is specified, it
2857 determines the position of the upper edge of the tooltip window. If a
2858 `bottom' parameter but no `top' frame parameter is specified, it
2859 determines the position of the lower edge of the tooltip window.
2860 Otherwise display the tooltip window at the y-position of the mouse,
2861 with offset DY added (default is -10).
2863 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2864 Text larger than the specified size is clipped. */)
2865 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2868 ptrdiff_t count = SPECPDL_INDEX ();
2875 specbind (Qinhibit_redisplay, Qt);
2877 CHECK_STRING (string);
2878 str = SSDATA (string);
2879 f = decode_window_system_frame (frame);
2881 timeout = make_number (5);
2883 CHECK_NATNUM (timeout);
2886 dx = make_number (5);
2891 dy = make_number (-10);
2896 if (ns_tooltip == nil)
2897 ns_tooltip = [[EmacsTooltip alloc] init];
2901 t = x_get_arg (NULL, parms, Qbackground_color, NULL, NULL, RES_TYPE_STRING);
2902 if (ns_lisp_to_color (t, &color) == 0)
2903 [ns_tooltip setBackgroundColor: color];
2905 t = x_get_arg (NULL, parms, Qforeground_color, NULL, NULL, RES_TYPE_STRING);
2906 if (ns_lisp_to_color (t, &color) == 0)
2907 [ns_tooltip setForegroundColor: color];
2909 [ns_tooltip setText: str];
2910 size = [ns_tooltip frame].size;
2912 /* Move the tooltip window where the mouse pointer is. Resize and
2914 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2917 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2920 return unbind_to (count, Qnil);
2924 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2925 doc: /* Hide the current tooltip window, if there is any.
2926 Value is t if tooltip was open, nil otherwise. */)
2929 if (ns_tooltip == nil || ![ns_tooltip isActive])
2935 /* Return geometric attributes of FRAME. According to the value of
2936 ATTRIBUTES return the outer edges of FRAME (Qouter_edges), the inner
2937 edges of FRAME, the root window edges of frame (Qroot_edges). Any
2938 other value means to return the geometry as returned by
2939 Fx_frame_geometry. */
2941 frame_geometry (Lisp_Object frame, Lisp_Object attribute)
2943 struct frame *f = decode_live_frame (frame);
2944 Lisp_Object fullscreen_symbol = Fframe_parameter (frame, Qfullscreen);
2945 bool fullscreen = (EQ (fullscreen_symbol, Qfullboth)
2946 || EQ (fullscreen_symbol, Qfullscreen));
2947 int border = fullscreen ? 0 : f->border_width;
2948 int title_height = fullscreen ? 0 : FRAME_NS_TITLEBAR_HEIGHT (f);
2949 int native_width = FRAME_PIXEL_WIDTH (f);
2950 int native_height = FRAME_PIXEL_HEIGHT (f);
2951 int outer_width = native_width + 2 * border;
2952 int outer_height = native_height + 2 * border + title_height;
2953 int native_left = f->left_pos + border;
2954 int native_top = f->top_pos + border + title_height;
2955 int native_right = f->left_pos + outer_width - border;
2956 int native_bottom = f->top_pos + outer_height - border;
2957 int internal_border_width = FRAME_INTERNAL_BORDER_WIDTH (f);
2958 int tool_bar_height = FRAME_TOOLBAR_HEIGHT (f);
2959 int tool_bar_width = (tool_bar_height
2960 ? outer_width - 2 * internal_border_width
2963 /* Construct list. */
2964 if (EQ (attribute, Qouter_edges))
2965 return list4 (make_number (f->left_pos), make_number (f->top_pos),
2966 make_number (f->left_pos + outer_width),
2967 make_number (f->top_pos + outer_height));
2968 else if (EQ (attribute, Qnative_edges))
2969 return list4 (make_number (native_left), make_number (native_top),
2970 make_number (native_right), make_number (native_bottom));
2971 else if (EQ (attribute, Qinner_edges))
2972 return list4 (make_number (native_left + internal_border_width),
2973 make_number (native_top
2975 + internal_border_width),
2976 make_number (native_right - internal_border_width),
2977 make_number (native_bottom - internal_border_width));
2980 listn (CONSTYPE_HEAP, 10,
2981 Fcons (Qouter_position,
2982 Fcons (make_number (f->left_pos),
2983 make_number (f->top_pos))),
2985 Fcons (make_number (outer_width),
2986 make_number (outer_height))),
2987 Fcons (Qexternal_border_size,
2989 ? Fcons (make_number (0), make_number (0))
2990 : Fcons (make_number (border), make_number (border)))),
2991 Fcons (Qtitle_bar_size,
2992 Fcons (make_number (0), make_number (title_height))),
2993 Fcons (Qmenu_bar_external, Qnil),
2994 Fcons (Qmenu_bar_size, Fcons (make_number (0), make_number (0))),
2995 Fcons (Qtool_bar_external,
2996 FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
2997 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
2998 Fcons (Qtool_bar_size,
2999 Fcons (make_number (tool_bar_width),
3000 make_number (tool_bar_height))),
3001 Fcons (Qinternal_border_width,
3002 make_number (internal_border_width)));
3005 DEFUN ("ns-frame-geometry", Fns_frame_geometry, Sns_frame_geometry, 0, 1, 0,
3006 doc: /* Return geometric attributes of FRAME.
3007 FRAME must be a live frame and defaults to the selected one. The return
3008 value is an association list of the attributes listed below. All height
3009 and width values are in pixels.
3011 `outer-position' is a cons of the outer left and top edges of FRAME
3012 relative to the origin - the position (0, 0) - of FRAME's display.
3014 `outer-size' is a cons of the outer width and height of FRAME. The
3015 outer size includes the title bar and the external borders as well as
3016 any menu and/or tool bar of frame.
3018 `external-border-size' is a cons of the horizontal and vertical width of
3019 FRAME's external borders as supplied by the window manager.
3021 `title-bar-size' is a cons of the width and height of the title bar of
3022 FRAME as supplied by the window manager. If both of them are zero,
3023 FRAME has no title bar. If only the width is zero, Emacs was not
3024 able to retrieve the width information.
3026 `menu-bar-external', if non-nil, means the menu bar is external (never
3027 included in the inner edges of FRAME).
3029 `menu-bar-size' is a cons of the width and height of the menu bar of
3032 `tool-bar-external', if non-nil, means the tool bar is external (never
3033 included in the inner edges of FRAME).
3035 `tool-bar-position' tells on which side the tool bar on FRAME is and can
3036 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
3039 `tool-bar-size' is a cons of the width and height of the tool bar of
3042 `internal-border-width' is the width of the internal border of
3046 return frame_geometry (frame, Qnil);
3049 DEFUN ("ns-frame-edges", Fns_frame_edges, Sns_frame_edges, 0, 2, 0,
3050 doc: /* Return edge coordinates of FRAME.
3051 FRAME must be a live frame and defaults to the selected one. The return
3052 value is a list of the form (LEFT, TOP, RIGHT, BOTTOM). All values are
3053 in pixels relative to the origin - the position (0, 0) - of FRAME's
3056 If optional argument TYPE is the symbol `outer-edges', return the outer
3057 edges of FRAME. The outer edges comprise the decorations of the window
3058 manager (like the title bar or external borders) as well as any external
3059 menu or tool bar of FRAME. If optional argument TYPE is the symbol
3060 `native-edges' or nil, return the native edges of FRAME. The native
3061 edges exclude the decorations of the window manager and any external
3062 menu or tool bar of FRAME. If TYPE is the symbol `inner-edges', return
3063 the inner edges of FRAME. These edges exclude title bar, any borders,
3064 menu bar or tool bar of FRAME. */)
3065 (Lisp_Object frame, Lisp_Object type)
3067 return frame_geometry (frame, ((EQ (type, Qouter_edges)
3068 || EQ (type, Qinner_edges))
3073 DEFUN ("ns-set-mouse-absolute-pixel-position",
3074 Fns_set_mouse_absolute_pixel_position,
3075 Sns_set_mouse_absolute_pixel_position, 2, 2, 0,
3076 doc: /* Move mouse pointer to absolute pixel position (X, Y).
3077 The coordinates X and Y are interpreted in pixels relative to a position
3078 \(0, 0) of the selected frame's display. */)
3079 (Lisp_Object x, Lisp_Object y)
3081 #ifdef NS_IMPL_COCOA
3082 /* GNUstep doesn't support CGWarpMouseCursorPosition, so none of
3084 struct frame *f = SELECTED_FRAME ();
3085 EmacsView *view = FRAME_NS_VIEW (f);
3086 NSScreen *screen = [[view window] screen];
3087 NSRect screen_frame = [screen frame];
3088 int mouse_x, mouse_y;
3090 NSScreen *primary_screen = [[NSScreen screens] objectAtIndex:0];
3091 NSRect primary_screen_frame = [primary_screen frame];
3092 CGFloat primary_screen_height = primary_screen_frame.size.height;
3094 if (FRAME_INITIAL_P (f) || !FRAME_NS_P (f))
3097 CHECK_TYPE_RANGED_INTEGER (int, x);
3098 CHECK_TYPE_RANGED_INTEGER (int, y);
3100 mouse_x = screen_frame.origin.x + XINT (x);
3102 if (screen == primary_screen)
3103 mouse_y = screen_frame.origin.y + XINT (y);
3105 mouse_y = (primary_screen_height - screen_frame.size.height
3106 - screen_frame.origin.y) + XINT (y);
3108 CGPoint mouse_pos = CGPointMake(mouse_x, mouse_y);
3109 CGWarpMouseCursorPosition (mouse_pos);
3110 #endif /* NS_IMPL_COCOA */
3115 DEFUN ("ns-mouse-absolute-pixel-position",
3116 Fns_mouse_absolute_pixel_position,
3117 Sns_mouse_absolute_pixel_position, 0, 0, 0,
3118 doc: /* Return absolute position of mouse cursor in pixels.
3119 The position is returned as a cons cell (X . Y) of the
3120 coordinates of the mouse cursor position in pixels relative to a
3121 position (0, 0) of the selected frame's terminal. */)
3124 struct frame *f = SELECTED_FRAME ();
3125 EmacsView *view = FRAME_NS_VIEW (f);
3126 NSScreen *screen = [[view window] screen];
3127 NSPoint pt = [NSEvent mouseLocation];
3129 return Fcons(make_number(pt.x - screen.frame.origin.x),
3130 make_number(screen.frame.size.height -
3131 (pt.y - screen.frame.origin.y)));
3134 DEFUN ("ns-show-character-palette",
3135 Fns_show_character_palette,
3136 Sns_show_character_palette, 0, 0, 0,
3137 doc: /* Show the macOS character palette. */)
3140 struct frame *f = SELECTED_FRAME ();
3141 EmacsView *view = FRAME_NS_VIEW (f);
3142 [NSApp orderFrontCharacterPalette:view];
3147 /* ==========================================================================
3149 Class implementations
3151 ========================================================================== */
3154 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
3155 Return YES if handled, NO if not.
3158 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
3164 if ([theEvent type] != NSEventTypeKeyDown) return NO;
3165 s = [theEvent characters];
3167 for (i = 0; i < [s length]; ++i)
3169 int ch = (int) [s characterAtIndex: i];
3172 case NSHomeFunctionKey:
3173 case NSDownArrowFunctionKey:
3174 case NSUpArrowFunctionKey:
3175 case NSLeftArrowFunctionKey:
3176 case NSRightArrowFunctionKey:
3177 case NSPageUpFunctionKey:
3178 case NSPageDownFunctionKey:
3179 case NSEndFunctionKey:
3180 /* Don't send command modified keys, as those are handled in the
3181 performKeyEquivalent method of the super class.
3183 if (! ([theEvent modifierFlags] & NSEventModifierFlagCommand))
3185 [panel sendEvent: theEvent];
3189 /* As we don't have the standard key commands for
3190 copy/paste/cut/select-all in our edit menu, we must handle
3191 them here. TODO: handle Emacs key bindings for copy/cut/select-all
3192 here, paste works, because we have that in our Edit menu.
3193 I.e. refactor out code in nsterm.m, keyDown: to figure out the
3199 case 'a': // Select all
3200 if ([theEvent modifierFlags] & NSEventModifierFlagCommand)
3209 : @selector(selectAll:))))
3214 // Send all control keys, as the text field supports C-a, C-f, C-e
3216 if ([theEvent modifierFlags] & NSEventModifierFlagControl)
3218 [panel sendEvent: theEvent];
3229 @implementation EmacsSavePanel
3230 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3232 BOOL ret = handlePanelKeys (self, theEvent);
3234 ret = [super performKeyEquivalent:theEvent];
3240 @implementation EmacsOpenPanel
3241 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3243 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
3244 BOOL ret = handlePanelKeys (self, theEvent);
3246 ret = [super performKeyEquivalent:theEvent];
3252 @implementation EmacsFileDelegate
3253 /* --------------------------------------------------------------------------
3254 Delegate methods for Open/Save panels
3255 -------------------------------------------------------------------------- */
3256 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
3260 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
3264 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
3265 confirmed: (BOOL)okFlag
3274 /* ==========================================================================
3276 Lisp interface declaration
3278 ========================================================================== */
3282 syms_of_nsfns (void)
3284 DEFSYM (Qfontsize, "fontsize");
3285 DEFSYM (Qframe_title_format, "frame-title-format");
3286 DEFSYM (Qicon_title_format, "icon-title-format");
3287 DEFSYM (Qdark, "dark");
3289 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
3290 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
3291 If the title of a frame matches REGEXP, then IMAGE.tiff is
3292 selected as the image of the icon representing the frame when it's
3293 miniaturized. If an element is t, then Emacs tries to select an icon
3294 based on the filetype of the visited file.
3296 The images have to be installed in a folder called English.lproj in the
3297 Emacs folder. You have to restart Emacs after installing new icons.
3299 Example: Install an icon Gnus.tiff and execute the following code
3301 (setq ns-icon-type-alist
3302 (append ns-icon-type-alist
3303 \\='((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
3306 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
3307 be used as the image of the icon representing the frame. */);
3308 Vns_icon_type_alist = list1 (Qt);
3310 DEFVAR_LISP ("ns-version-string", Vns_version_string,
3311 doc: /* Toolkit version for NS Windowing. */);
3312 Vns_version_string = ns_appkit_version_str ();
3314 defsubr (&Sns_read_file_name);
3315 defsubr (&Sns_get_resource);
3316 defsubr (&Sns_set_resource);
3317 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
3318 defsubr (&Sx_display_grayscale_p);
3319 defsubr (&Sns_font_name);
3320 defsubr (&Sns_list_colors);
3321 #ifdef NS_IMPL_COCOA
3322 defsubr (&Sns_do_applescript);
3324 defsubr (&Sxw_color_defined_p);
3325 defsubr (&Sxw_color_values);
3326 defsubr (&Sx_server_max_request_size);
3327 defsubr (&Sx_server_vendor);
3328 defsubr (&Sx_server_version);
3329 defsubr (&Sx_display_pixel_width);
3330 defsubr (&Sx_display_pixel_height);
3331 defsubr (&Sns_display_monitor_attributes_list);
3332 defsubr (&Sns_frame_geometry);
3333 defsubr (&Sns_frame_edges);
3334 defsubr (&Sns_frame_list_z_order);
3335 defsubr (&Sns_frame_restack);
3336 defsubr (&Sns_set_mouse_absolute_pixel_position);
3337 defsubr (&Sns_mouse_absolute_pixel_position);
3338 defsubr (&Sns_show_character_palette);
3339 defsubr (&Sx_display_mm_width);
3340 defsubr (&Sx_display_mm_height);
3341 defsubr (&Sx_display_screens);
3342 defsubr (&Sx_display_planes);
3343 defsubr (&Sx_display_color_cells);
3344 defsubr (&Sx_display_visual_class);
3345 defsubr (&Sx_display_backing_store);
3346 defsubr (&Sx_display_save_under);
3347 defsubr (&Sx_create_frame);
3348 defsubr (&Sx_open_connection);
3349 defsubr (&Sx_close_connection);
3350 defsubr (&Sx_display_list);
3352 defsubr (&Sns_hide_others);
3353 defsubr (&Sns_hide_emacs);
3354 defsubr (&Sns_emacs_info_panel);
3355 defsubr (&Sns_list_services);
3356 defsubr (&Sns_perform_service);
3357 defsubr (&Sns_popup_font_panel);
3358 defsubr (&Sns_popup_color_panel);
3360 defsubr (&Sx_show_tip);
3361 defsubr (&Sx_hide_tip);