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 case NSBackingStoreRetained:
1900 return intern ("retained");
1901 case NSBackingStoreNonretained:
1902 return intern ("non-retained");
1904 error ("Strange value for backingType parameter of frame");
1906 return Qnil; /* not reached, shut compiler up */
1910 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1911 Sx_display_visual_class, 0, 1, 0,
1912 doc: /* Return the visual class of the Nextstep display TERMINAL.
1913 The value is one of the symbols `static-gray', `gray-scale',
1914 `static-color', `pseudo-color', `true-color', or `direct-color'.
1916 The optional argument TERMINAL specifies which display to ask about.
1917 TERMINAL should a terminal object, a frame or a display name (a string).
1918 If omitted or nil, that stands for the selected frame's display. */)
1919 (Lisp_Object terminal)
1921 NSWindowDepth depth;
1923 check_ns_display_info (terminal);
1924 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1926 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1927 return intern ("static-gray");
1928 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1929 return intern ("gray-scale");
1930 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1931 return intern ("pseudo-color");
1932 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1933 return intern ("true-color");
1934 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1935 return intern ("direct-color");
1937 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1938 return intern ("direct-color");
1942 DEFUN ("x-display-save-under", Fx_display_save_under,
1943 Sx_display_save_under, 0, 1, 0,
1944 doc: /* Return t if TERMINAL supports the save-under feature.
1945 The optional argument TERMINAL specifies which display to ask about.
1946 TERMINAL should be a terminal object, a frame or a display name (a string).
1947 If omitted or nil, that stands for the selected frame's display. */)
1948 (Lisp_Object terminal)
1950 check_ns_display_info (terminal);
1951 switch ([ns_get_window (terminal) backingType])
1953 case NSBackingStoreBuffered:
1956 case NSBackingStoreRetained:
1957 case NSBackingStoreNonretained:
1961 error ("Strange value for backingType parameter of frame");
1963 return Qnil; /* not reached, shut compiler up */
1967 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1969 doc: /* Open a connection to a display server.
1970 DISPLAY is the name of the display to connect to.
1971 Optional second arg XRM-STRING is a string of resources in xrdb format.
1972 If the optional third arg MUST-SUCCEED is non-nil,
1973 terminate Emacs if we can't open the connection.
1974 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1975 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1977 struct ns_display_info *dpyinfo;
1979 CHECK_STRING (display);
1981 nxatoms_of_nsselect ();
1982 dpyinfo = ns_term_init (display);
1985 if (!NILP (must_succeed))
1986 fatal ("Display on %s not responding.\n",
1989 error ("Display on %s not responding.\n",
1997 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1999 doc: /* Close the connection to TERMINAL's Nextstep display server.
2000 For TERMINAL, specify a terminal object, a frame or a display name (a
2001 string). If TERMINAL is nil, that stands for the selected frame's
2003 (Lisp_Object terminal)
2005 check_ns_display_info (terminal);
2006 [NSApp terminate: NSApp];
2011 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
2012 doc: /* Return the list of display names that Emacs has connections to. */)
2015 Lisp_Object result = Qnil;
2016 struct ns_display_info *ndi;
2018 for (ndi = x_display_list; ndi; ndi = ndi->next)
2019 result = Fcons (XCAR (ndi->name_list_element), result);
2025 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
2027 doc: /* Hides all applications other than Emacs. */)
2030 check_window_system (NULL);
2031 [NSApp hideOtherApplications: NSApp];
2035 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
2037 doc: /* If ON is non-nil, the entire Emacs application is hidden.
2038 Otherwise if Emacs is hidden, it is unhidden.
2039 If ON is equal to `activate', Emacs is unhidden and becomes
2040 the active application. */)
2043 check_window_system (NULL);
2044 if (EQ (on, intern ("activate")))
2046 [NSApp unhide: NSApp];
2047 [NSApp activateIgnoringOtherApps: YES];
2050 [NSApp unhide: NSApp];
2052 [NSApp hide: NSApp];
2057 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
2059 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
2062 check_window_system (NULL);
2063 [NSApp orderFrontStandardAboutPanel: nil];
2068 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
2069 doc: /* Determine font PostScript or family name for font NAME.
2070 NAME should be a string containing either the font name or an XLFD
2071 font descriptor. If string contains `fontset' and not
2072 `fontset-startup', it is left alone. */)
2076 CHECK_STRING (name);
2081 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
2084 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
2088 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
2089 doc: /* Return a list of all available colors.
2090 The optional argument FRAME is currently ignored. */)
2093 Lisp_Object list = Qnil;
2094 NSEnumerator *colorlists;
2099 CHECK_FRAME (frame);
2100 if (! FRAME_NS_P (XFRAME (frame)))
2101 error ("non-Nextstep frame used in `ns-list-colors'");
2106 colorlists = [[NSColorList availableColorLists] objectEnumerator];
2107 while ((clist = [colorlists nextObject]))
2109 if ([[clist name] length] < 7 ||
2110 [[clist name] rangeOfString: @"PANTONE"].location == 0)
2112 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
2114 while ((cname = [cnames nextObject]))
2115 list = Fcons (build_string ([cname UTF8String]), list);
2116 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
2117 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
2118 UTF8String]), list); */
2128 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
2129 doc: /* List available Nextstep services by querying NSApp. */)
2132 #ifdef NS_IMPL_COCOA
2133 /* You can't get services like this in 10.6+. */
2136 Lisp_Object ret = Qnil;
2139 check_window_system (NULL);
2140 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
2141 [NSApp setServicesMenu: svcs];
2142 [NSApp registerServicesMenuSendTypes: ns_send_types
2143 returnTypes: ns_return_types];
2145 [svcs setAutoenablesItems: NO];
2147 ret = interpret_services_menu (svcs, Qnil, ret);
2153 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2155 doc: /* Perform Nextstep SERVICE on SEND.
2156 SEND should be either a string or nil.
2157 The return value is the result of the service, as string, or nil if
2158 there was no result. */)
2159 (Lisp_Object service, Lisp_Object send)
2165 CHECK_STRING (service);
2166 check_window_system (NULL);
2168 utfStr = SSDATA (service);
2169 svcName = [NSString stringWithUTF8String: utfStr];
2171 pb =[NSPasteboard pasteboardWithUniqueName];
2172 ns_string_to_pasteboard (pb, send);
2174 if (NSPerformService (svcName, pb) == NO)
2175 Fsignal (Qquit, list1 (build_string ("service not available")));
2177 if ([[pb types] count] == 0)
2178 return build_string ("");
2179 return ns_string_from_pasteboard (pb);
2183 #ifdef NS_IMPL_COCOA
2185 /* Compile and execute the AppleScript SCRIPT and return the error
2186 status as function value. A zero is returned if compilation and
2187 execution is successful, in which case *RESULT is set to a Lisp
2188 string or a number containing the resulting script value. Otherwise,
2191 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2193 NSAppleEventDescriptor *desc;
2194 NSDictionary *errorDict;
2195 NSAppleEventDescriptor *returnDescriptor = NULL;
2197 NSAppleScript *scriptObject =
2198 [[NSAppleScript alloc] initWithSource:
2199 [NSString stringWithUTF8String: SSDATA (script)]];
2201 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2202 [scriptObject release];
2205 if (returnDescriptor != NULL)
2207 // successful execution
2208 if (kAENullEvent != [returnDescriptor descriptorType])
2211 // script returned an AppleScript result
2212 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2213 #if defined (NS_IMPL_COCOA)
2214 (typeUTF16ExternalRepresentation
2215 == [returnDescriptor descriptorType]) ||
2217 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2218 (typeCString == [returnDescriptor descriptorType]))
2220 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2222 *result = build_string([[desc stringValue] UTF8String]);
2226 /* use typeUTF16ExternalRepresentation? */
2227 // coerce the result to the appropriate ObjC type
2228 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2230 *result = make_number([desc int32Value]);
2236 // no script result, return error
2242 /* Helper function called from sendEvent to run applescript
2243 from within the main event loop. */
2246 ns_run_ascript (void)
2248 if (! NILP (as_script))
2249 as_status = ns_do_applescript (as_script, as_result);
2253 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2254 doc: /* Execute AppleScript SCRIPT and return the result.
2255 If compilation and execution are successful, the resulting script value
2256 is returned as a string, a number or, in the case of other constructs, t.
2257 In case the execution fails, an error is signaled. */)
2258 (Lisp_Object script)
2263 struct input_event ev;
2265 CHECK_STRING (script);
2266 check_window_system (NULL);
2271 as_result = &result;
2273 /* executing apple script requires the event loop to run, otherwise
2274 errors aren't returned and executeAndReturnError hangs forever.
2275 Post an event that runs applescript and then start the event loop.
2276 The event loop is exited when the script is done. */
2277 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
2278 location: NSMakePoint (0, 0)
2281 windowNumber: [[NSApp mainWindow] windowNumber]
2282 context: [NSApp context]
2285 data2: NSAPP_DATA2_RUNASSCRIPT];
2287 [NSApp postEvent: nxev atStart: NO];
2289 // If there are other events, the event loop may exit. Keep running
2290 // until the script has been handled. */
2291 ns_init_events (&ev);
2292 while (! NILP (as_script))
2294 ns_finish_events ();
2302 else if (!STRINGP (result))
2303 error ("AppleScript error %d", status);
2305 error ("%s", SSDATA (result));
2311 /* ==========================================================================
2313 Miscellaneous functions not called through hooks
2315 ========================================================================== */
2317 /* called from frame.c */
2318 struct ns_display_info *
2319 check_x_display_info (Lisp_Object frame)
2321 return check_ns_display_info (frame);
2326 x_set_scroll_bar_default_width (struct frame *f)
2328 int wid = FRAME_COLUMN_WIDTH (f);
2329 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2330 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2335 x_set_scroll_bar_default_height (struct frame *f)
2337 int height = FRAME_LINE_HEIGHT (f);
2338 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2339 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) +
2340 height - 1) / height;
2343 /* terms impl this instead of x-get-resource directly */
2345 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
2347 /* remove appname prefix; TODO: allow for !="Emacs" */
2348 const char *res, *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2350 check_window_system (NULL);
2352 if (inhibit_x_resources)
2353 /* --quick was passed, so this is a no-op. */
2356 res = ns_get_defaults_value (toCheck);
2357 return (char *) (!res ? NULL
2358 : !c_strncasecmp (res, "YES", 3) ? "true"
2359 : !c_strncasecmp (res, "NO", 2) ? "false"
2365 x_get_focus_frame (struct frame *frame)
2367 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2368 Lisp_Object nsfocus;
2370 if (!dpyinfo->x_focus_frame)
2373 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2377 /* ==========================================================================
2379 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2381 ========================================================================== */
2384 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2385 doc: /* Internal function called by `color-defined-p', which see.
2386 \(Note that the Nextstep version of this function ignores FRAME.) */)
2387 (Lisp_Object color, Lisp_Object frame)
2390 check_window_system (NULL);
2391 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2395 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2396 doc: /* Internal function called by `color-values', which see. */)
2397 (Lisp_Object color, Lisp_Object frame)
2400 EmacsCGFloat red, green, blue, alpha;
2402 check_window_system (NULL);
2403 CHECK_STRING (color);
2406 if (ns_lisp_to_color (color, &col))
2412 [[col colorUsingDefaultColorSpace]
2413 getRed: &red green: &green blue: &blue alpha: &alpha];
2415 return list3i (lrint (red * 65280), lrint (green * 65280),
2416 lrint (blue * 65280));
2420 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2421 doc: /* Internal function called by `display-color-p', which see. */)
2422 (Lisp_Object terminal)
2424 NSWindowDepth depth;
2425 NSString *colorSpace;
2427 check_ns_display_info (terminal);
2428 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2429 colorSpace = NSColorSpaceFromDepth (depth);
2431 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2432 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2437 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2439 doc: /* Return t if the Nextstep display supports shades of gray.
2440 Note that color displays do support shades of gray.
2441 The optional argument TERMINAL specifies which display to ask about.
2442 TERMINAL should be a terminal object, a frame or a display name (a string).
2443 If omitted or nil, that stands for the selected frame's display. */)
2444 (Lisp_Object terminal)
2446 NSWindowDepth depth;
2448 check_ns_display_info (terminal);
2449 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2451 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2455 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2457 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2458 The optional argument TERMINAL specifies which display to ask about.
2459 TERMINAL should be a terminal object, a frame or a display name (a string).
2460 If omitted or nil, that stands for the selected frame's display.
2462 On \"multi-monitor\" setups this refers to the pixel width for all
2463 physical monitors associated with TERMINAL. To get information for
2464 each physical monitor, use `display-monitor-attributes-list'. */)
2465 (Lisp_Object terminal)
2467 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2469 return make_number (x_display_pixel_width (dpyinfo));
2473 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2474 Sx_display_pixel_height, 0, 1, 0,
2475 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2476 The optional argument TERMINAL specifies which display to ask about.
2477 TERMINAL should be a terminal object, a frame or a display name (a string).
2478 If omitted or nil, that stands for the selected frame's display.
2480 On \"multi-monitor\" setups this refers to the pixel height for all
2481 physical monitors associated with TERMINAL. To get information for
2482 each physical monitor, use `display-monitor-attributes-list'. */)
2483 (Lisp_Object terminal)
2485 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2487 return make_number (x_display_pixel_height (dpyinfo));
2490 #ifdef NS_IMPL_COCOA
2492 /* Returns the name for the screen that OBJ represents, or NULL.
2493 Caller must free return value.
2497 ns_get_name_from_ioreg (io_object_t obj)
2501 NSDictionary *info = (NSDictionary *)
2502 IODisplayCreateInfoDictionary (obj, kIODisplayOnlyPreferredName);
2503 NSDictionary *names = [info objectForKey:
2504 [NSString stringWithUTF8String:
2505 kDisplayProductName]];
2507 if ([names count] > 0)
2509 NSString *n = [names objectForKey: [[names allKeys]
2511 if (n != nil) name = xstrdup ([n UTF8String]);
2519 /* Returns the name for the screen that DID came from, or NULL.
2520 Caller must free return value.
2524 ns_screen_name (CGDirectDisplayID did)
2528 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
2529 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2530 if (CGDisplayIOServicePort == NULL)
2533 mach_port_t masterPort;
2537 /* CGDisplayIOServicePort is deprecated. Do it another (harder) way.
2539 Is this code OK for macOS < 10.9, and GNUstep? I suspect it is,
2540 in which case is it worth keeping the other method in here? */
2542 if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
2543 || IOServiceGetMatchingServices (masterPort,
2544 IOServiceMatching ("IONDRVDevice"),
2545 &it) != kIOReturnSuccess)
2548 /* Must loop until we find a name. Many devices can have the same unit
2549 number (represents different GPU parts), but only one has a name. */
2550 while (! name && (obj = IOIteratorNext (it)))
2552 CFMutableDictionaryRef props;
2555 if (IORegistryEntryCreateCFProperties (obj,
2557 kCFAllocatorDefault,
2558 kNilOptions) == kIOReturnSuccess
2560 && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
2562 unsigned nr = [(NSNumber *)val unsignedIntegerValue];
2563 if (nr == CGDisplayUnitNumber (did))
2564 name = ns_get_name_from_ioreg (obj);
2568 IOObjectRelease (obj);
2571 IOObjectRelease (it);
2573 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2576 #endif /* #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 */
2577 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2578 name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
2582 #endif /* NS_IMPL_COCOA */
2585 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2587 int primary_monitor,
2590 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2591 Lisp_Object frame, rest;
2592 NSArray *screens = [NSScreen screens];
2595 FOR_EACH_FRAME (rest, frame)
2597 struct frame *f = XFRAME (frame);
2601 NSView *view = FRAME_NS_VIEW (f);
2602 NSScreen *screen = [[view window] screen];
2606 for (k = 0; i == -1 && k < [screens count]; ++k)
2608 if ([screens objectAtIndex: k] == screen)
2613 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2617 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2618 monitor_frames, source);
2621 DEFUN ("ns-display-monitor-attributes-list",
2622 Fns_display_monitor_attributes_list,
2623 Sns_display_monitor_attributes_list,
2625 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2627 The optional argument TERMINAL specifies which display to ask about.
2628 TERMINAL should be a terminal object, a frame or a display name (a string).
2629 If omitted or nil, that stands for the selected frame's display.
2631 In addition to the standard attribute keys listed in
2632 `display-monitor-attributes-list', the following keys are contained in
2635 source -- String describing the source from which multi-monitor
2636 information is obtained, \"NS\" is always the source."
2638 Internal use only, use `display-monitor-attributes-list' instead. */)
2639 (Lisp_Object terminal)
2641 struct terminal *term = decode_live_terminal (terminal);
2643 NSUInteger i, n_monitors;
2644 struct MonitorInfo *monitors;
2645 Lisp_Object attributes_list = Qnil;
2646 CGFloat primary_display_height = 0;
2648 if (term->type != output_ns)
2651 screens = [NSScreen screens];
2652 n_monitors = [screens count];
2653 if (n_monitors == 0)
2656 monitors = xzalloc (n_monitors * sizeof *monitors);
2658 for (i = 0; i < [screens count]; ++i)
2660 NSScreen *s = [screens objectAtIndex:i];
2661 struct MonitorInfo *m = &monitors[i];
2662 NSRect fr = [s frame];
2663 NSRect vfr = [s visibleFrame];
2666 #ifdef NS_IMPL_COCOA
2667 NSDictionary *dict = [s deviceDescription];
2668 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2669 CGDirectDisplayID did = [nid unsignedIntValue];
2673 primary_display_height = fr.size.height;
2674 y = (short) fr.origin.y;
2675 vy = (short) vfr.origin.y;
2679 // Flip y coordinate as NS has y starting from the bottom.
2680 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2681 vy = (short) (primary_display_height -
2682 vfr.size.height - vfr.origin.y);
2685 m->geom.x = (short) fr.origin.x;
2687 m->geom.width = (unsigned short) fr.size.width;
2688 m->geom.height = (unsigned short) fr.size.height;
2690 m->work.x = (short) vfr.origin.x;
2691 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2692 // and fr.size.height - vfr.size.height are pixels missing in total.
2693 // Pixels missing at top are
2694 // fr.size.height - vfr.size.height - vy + y.
2695 // work.y is then pixels missing at top + y.
2696 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2697 m->work.width = (unsigned short) vfr.size.width;
2698 m->work.height = (unsigned short) vfr.size.height;
2700 #ifdef NS_IMPL_COCOA
2701 m->name = ns_screen_name (did);
2704 CGSize mms = CGDisplayScreenSize (did);
2705 m->mm_width = (int) mms.width;
2706 m->mm_height = (int) mms.height;
2710 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2711 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2712 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2716 // Primary monitor is always first for NS.
2717 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2720 free_monitors (monitors, n_monitors);
2721 return attributes_list;
2725 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2727 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2728 The optional argument TERMINAL specifies which display to ask about.
2729 TERMINAL should be a terminal object, a frame or a display name (a string).
2730 If omitted or nil, that stands for the selected frame's display. */)
2731 (Lisp_Object terminal)
2733 check_ns_display_info (terminal);
2735 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2739 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2741 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2742 The optional argument TERMINAL specifies which display to ask about.
2743 TERMINAL should be a terminal object, a frame or a display name (a string).
2744 If omitted or nil, that stands for the selected frame's display. */)
2745 (Lisp_Object terminal)
2747 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2748 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2749 return make_number (1 << min (dpyinfo->n_planes, 24));
2753 /* Unused dummy def needed for compatibility. */
2754 Lisp_Object tip_frame;
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 ();
2873 specbind (Qinhibit_redisplay, Qt);
2875 CHECK_STRING (string);
2876 str = SSDATA (string);
2877 f = decode_window_system_frame (frame);
2879 timeout = make_number (5);
2881 CHECK_NATNUM (timeout);
2884 dx = make_number (5);
2889 dy = make_number (-10);
2894 if (ns_tooltip == nil)
2895 ns_tooltip = [[EmacsTooltip alloc] init];
2899 [ns_tooltip setText: str];
2900 size = [ns_tooltip frame].size;
2902 /* Move the tooltip window where the mouse pointer is. Resize and
2904 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2907 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2910 return unbind_to (count, Qnil);
2914 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2915 doc: /* Hide the current tooltip window, if there is any.
2916 Value is t if tooltip was open, nil otherwise. */)
2919 if (ns_tooltip == nil || ![ns_tooltip isActive])
2925 /* Return geometric attributes of FRAME. According to the value of
2926 ATTRIBUTES return the outer edges of FRAME (Qouter_edges), the inner
2927 edges of FRAME, the root window edges of frame (Qroot_edges). Any
2928 other value means to return the geometry as returned by
2929 Fx_frame_geometry. */
2931 frame_geometry (Lisp_Object frame, Lisp_Object attribute)
2933 struct frame *f = decode_live_frame (frame);
2934 Lisp_Object fullscreen_symbol = Fframe_parameter (frame, Qfullscreen);
2935 bool fullscreen = (EQ (fullscreen_symbol, Qfullboth)
2936 || EQ (fullscreen_symbol, Qfullscreen));
2937 int border = fullscreen ? 0 : f->border_width;
2938 int title_height = fullscreen ? 0 : FRAME_NS_TITLEBAR_HEIGHT (f);
2939 int native_width = FRAME_PIXEL_WIDTH (f);
2940 int native_height = FRAME_PIXEL_HEIGHT (f);
2941 int outer_width = native_width + 2 * border;
2942 int outer_height = native_height + 2 * border + title_height;
2943 int native_left = f->left_pos + border;
2944 int native_top = f->top_pos + border + title_height;
2945 int native_right = f->left_pos + outer_width - border;
2946 int native_bottom = f->top_pos + outer_height - border;
2947 int internal_border_width = FRAME_INTERNAL_BORDER_WIDTH (f);
2948 int tool_bar_height = FRAME_TOOLBAR_HEIGHT (f);
2949 int tool_bar_width = (tool_bar_height
2950 ? outer_width - 2 * internal_border_width
2953 /* Construct list. */
2954 if (EQ (attribute, Qouter_edges))
2955 return list4 (make_number (f->left_pos), make_number (f->top_pos),
2956 make_number (f->left_pos + outer_width),
2957 make_number (f->top_pos + outer_height));
2958 else if (EQ (attribute, Qnative_edges))
2959 return list4 (make_number (native_left), make_number (native_top),
2960 make_number (native_right), make_number (native_bottom));
2961 else if (EQ (attribute, Qinner_edges))
2962 return list4 (make_number (native_left + internal_border_width),
2963 make_number (native_top
2965 + internal_border_width),
2966 make_number (native_right - internal_border_width),
2967 make_number (native_bottom - internal_border_width));
2970 listn (CONSTYPE_HEAP, 10,
2971 Fcons (Qouter_position,
2972 Fcons (make_number (f->left_pos),
2973 make_number (f->top_pos))),
2975 Fcons (make_number (outer_width),
2976 make_number (outer_height))),
2977 Fcons (Qexternal_border_size,
2979 ? Fcons (make_number (0), make_number (0))
2980 : Fcons (make_number (border), make_number (border)))),
2981 Fcons (Qtitle_bar_size,
2982 Fcons (make_number (0), make_number (title_height))),
2983 Fcons (Qmenu_bar_external, Qnil),
2984 Fcons (Qmenu_bar_size, Fcons (make_number (0), make_number (0))),
2985 Fcons (Qtool_bar_external,
2986 FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
2987 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
2988 Fcons (Qtool_bar_size,
2989 Fcons (make_number (tool_bar_width),
2990 make_number (tool_bar_height))),
2991 Fcons (Qinternal_border_width,
2992 make_number (internal_border_width)));
2995 DEFUN ("ns-frame-geometry", Fns_frame_geometry, Sns_frame_geometry, 0, 1, 0,
2996 doc: /* Return geometric attributes of FRAME.
2997 FRAME must be a live frame and defaults to the selected one. The return
2998 value is an association list of the attributes listed below. All height
2999 and width values are in pixels.
3001 `outer-position' is a cons of the outer left and top edges of FRAME
3002 relative to the origin - the position (0, 0) - of FRAME's display.
3004 `outer-size' is a cons of the outer width and height of FRAME. The
3005 outer size includes the title bar and the external borders as well as
3006 any menu and/or tool bar of frame.
3008 `external-border-size' is a cons of the horizontal and vertical width of
3009 FRAME's external borders as supplied by the window manager.
3011 `title-bar-size' is a cons of the width and height of the title bar of
3012 FRAME as supplied by the window manager. If both of them are zero,
3013 FRAME has no title bar. If only the width is zero, Emacs was not
3014 able to retrieve the width information.
3016 `menu-bar-external', if non-nil, means the menu bar is external (never
3017 included in the inner edges of FRAME).
3019 `menu-bar-size' is a cons of the width and height of the menu bar of
3022 `tool-bar-external', if non-nil, means the tool bar is external (never
3023 included in the inner edges of FRAME).
3025 `tool-bar-position' tells on which side the tool bar on FRAME is and can
3026 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
3029 `tool-bar-size' is a cons of the width and height of the tool bar of
3032 `internal-border-width' is the width of the internal border of
3036 return frame_geometry (frame, Qnil);
3039 DEFUN ("ns-frame-edges", Fns_frame_edges, Sns_frame_edges, 0, 2, 0,
3040 doc: /* Return edge coordinates of FRAME.
3041 FRAME must be a live frame and defaults to the selected one. The return
3042 value is a list of the form (LEFT, TOP, RIGHT, BOTTOM). All values are
3043 in pixels relative to the origin - the position (0, 0) - of FRAME's
3046 If optional argument TYPE is the symbol `outer-edges', return the outer
3047 edges of FRAME. The outer edges comprise the decorations of the window
3048 manager (like the title bar or external borders) as well as any external
3049 menu or tool bar of FRAME. If optional argument TYPE is the symbol
3050 `native-edges' or nil, return the native edges of FRAME. The native
3051 edges exclude the decorations of the window manager and any external
3052 menu or tool bar of FRAME. If TYPE is the symbol `inner-edges', return
3053 the inner edges of FRAME. These edges exclude title bar, any borders,
3054 menu bar or tool bar of FRAME. */)
3055 (Lisp_Object frame, Lisp_Object type)
3057 return frame_geometry (frame, ((EQ (type, Qouter_edges)
3058 || EQ (type, Qinner_edges))
3063 DEFUN ("ns-set-mouse-absolute-pixel-position",
3064 Fns_set_mouse_absolute_pixel_position,
3065 Sns_set_mouse_absolute_pixel_position, 2, 2, 0,
3066 doc: /* Move mouse pointer to absolute pixel position (X, Y).
3067 The coordinates X and Y are interpreted in pixels relative to a position
3068 \(0, 0) of the selected frame's display. */)
3069 (Lisp_Object x, Lisp_Object y)
3071 #ifdef NS_IMPL_COCOA
3072 /* GNUstep doesn't support CGWarpMouseCursorPosition, so none of
3074 struct frame *f = SELECTED_FRAME ();
3075 EmacsView *view = FRAME_NS_VIEW (f);
3076 NSScreen *screen = [[view window] screen];
3077 NSRect screen_frame = [screen frame];
3078 int mouse_x, mouse_y;
3080 NSScreen *primary_screen = [[NSScreen screens] objectAtIndex:0];
3081 NSRect primary_screen_frame = [primary_screen frame];
3082 CGFloat primary_screen_height = primary_screen_frame.size.height;
3084 if (FRAME_INITIAL_P (f) || !FRAME_NS_P (f))
3087 CHECK_TYPE_RANGED_INTEGER (int, x);
3088 CHECK_TYPE_RANGED_INTEGER (int, y);
3090 mouse_x = screen_frame.origin.x + XINT (x);
3092 if (screen == primary_screen)
3093 mouse_y = screen_frame.origin.y + XINT (y);
3095 mouse_y = (primary_screen_height - screen_frame.size.height
3096 - screen_frame.origin.y) + XINT (y);
3098 CGPoint mouse_pos = CGPointMake(mouse_x, mouse_y);
3099 CGWarpMouseCursorPosition (mouse_pos);
3100 #endif /* NS_IMPL_COCOA */
3105 DEFUN ("ns-mouse-absolute-pixel-position",
3106 Fns_mouse_absolute_pixel_position,
3107 Sns_mouse_absolute_pixel_position, 0, 0, 0,
3108 doc: /* Return absolute position of mouse cursor in pixels.
3109 The position is returned as a cons cell (X . Y) of the
3110 coordinates of the mouse cursor position in pixels relative to a
3111 position (0, 0) of the selected frame's terminal. */)
3114 struct frame *f = SELECTED_FRAME ();
3115 EmacsView *view = FRAME_NS_VIEW (f);
3116 NSScreen *screen = [[view window] screen];
3117 NSPoint pt = [NSEvent mouseLocation];
3119 return Fcons(make_number(pt.x - screen.frame.origin.x),
3120 make_number(screen.frame.size.height -
3121 (pt.y - screen.frame.origin.y)));
3124 /* ==========================================================================
3126 Class implementations
3128 ========================================================================== */
3131 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
3132 Return YES if handled, NO if not.
3135 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
3141 if ([theEvent type] != NSEventTypeKeyDown) return NO;
3142 s = [theEvent characters];
3144 for (i = 0; i < [s length]; ++i)
3146 int ch = (int) [s characterAtIndex: i];
3149 case NSHomeFunctionKey:
3150 case NSDownArrowFunctionKey:
3151 case NSUpArrowFunctionKey:
3152 case NSLeftArrowFunctionKey:
3153 case NSRightArrowFunctionKey:
3154 case NSPageUpFunctionKey:
3155 case NSPageDownFunctionKey:
3156 case NSEndFunctionKey:
3157 /* Don't send command modified keys, as those are handled in the
3158 performKeyEquivalent method of the super class.
3160 if (! ([theEvent modifierFlags] & NSEventModifierFlagCommand))
3162 [panel sendEvent: theEvent];
3166 /* As we don't have the standard key commands for
3167 copy/paste/cut/select-all in our edit menu, we must handle
3168 them here. TODO: handle Emacs key bindings for copy/cut/select-all
3169 here, paste works, because we have that in our Edit menu.
3170 I.e. refactor out code in nsterm.m, keyDown: to figure out the
3176 case 'a': // Select all
3177 if ([theEvent modifierFlags] & NSEventModifierFlagCommand)
3186 : @selector(selectAll:))))
3191 // Send all control keys, as the text field supports C-a, C-f, C-e
3193 if ([theEvent modifierFlags] & NSEventModifierFlagControl)
3195 [panel sendEvent: theEvent];
3206 @implementation EmacsSavePanel
3207 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3209 BOOL ret = handlePanelKeys (self, theEvent);
3211 ret = [super performKeyEquivalent:theEvent];
3217 @implementation EmacsOpenPanel
3218 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3220 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
3221 BOOL ret = handlePanelKeys (self, theEvent);
3223 ret = [super performKeyEquivalent:theEvent];
3229 @implementation EmacsFileDelegate
3230 /* --------------------------------------------------------------------------
3231 Delegate methods for Open/Save panels
3232 -------------------------------------------------------------------------- */
3233 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
3237 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
3241 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
3242 confirmed: (BOOL)okFlag
3251 /* ==========================================================================
3253 Lisp interface declaration
3255 ========================================================================== */
3259 syms_of_nsfns (void)
3261 DEFSYM (Qfontsize, "fontsize");
3262 DEFSYM (Qframe_title_format, "frame-title-format");
3263 DEFSYM (Qicon_title_format, "icon-title-format");
3264 DEFSYM (Qdark, "dark");
3266 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
3267 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
3268 If the title of a frame matches REGEXP, then IMAGE.tiff is
3269 selected as the image of the icon representing the frame when it's
3270 miniaturized. If an element is t, then Emacs tries to select an icon
3271 based on the filetype of the visited file.
3273 The images have to be installed in a folder called English.lproj in the
3274 Emacs folder. You have to restart Emacs after installing new icons.
3276 Example: Install an icon Gnus.tiff and execute the following code
3278 (setq ns-icon-type-alist
3279 (append ns-icon-type-alist
3280 \\='((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
3283 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
3284 be used as the image of the icon representing the frame. */);
3285 Vns_icon_type_alist = list1 (Qt);
3287 DEFVAR_LISP ("ns-version-string", Vns_version_string,
3288 doc: /* Toolkit version for NS Windowing. */);
3289 Vns_version_string = ns_appkit_version_str ();
3291 defsubr (&Sns_read_file_name);
3292 defsubr (&Sns_get_resource);
3293 defsubr (&Sns_set_resource);
3294 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
3295 defsubr (&Sx_display_grayscale_p);
3296 defsubr (&Sns_font_name);
3297 defsubr (&Sns_list_colors);
3298 #ifdef NS_IMPL_COCOA
3299 defsubr (&Sns_do_applescript);
3301 defsubr (&Sxw_color_defined_p);
3302 defsubr (&Sxw_color_values);
3303 defsubr (&Sx_server_max_request_size);
3304 defsubr (&Sx_server_vendor);
3305 defsubr (&Sx_server_version);
3306 defsubr (&Sx_display_pixel_width);
3307 defsubr (&Sx_display_pixel_height);
3308 defsubr (&Sns_display_monitor_attributes_list);
3309 defsubr (&Sns_frame_geometry);
3310 defsubr (&Sns_frame_edges);
3311 defsubr (&Sns_frame_list_z_order);
3312 defsubr (&Sns_frame_restack);
3313 defsubr (&Sns_set_mouse_absolute_pixel_position);
3314 defsubr (&Sns_mouse_absolute_pixel_position);
3315 defsubr (&Sx_display_mm_width);
3316 defsubr (&Sx_display_mm_height);
3317 defsubr (&Sx_display_screens);
3318 defsubr (&Sx_display_planes);
3319 defsubr (&Sx_display_color_cells);
3320 defsubr (&Sx_display_visual_class);
3321 defsubr (&Sx_display_backing_store);
3322 defsubr (&Sx_display_save_under);
3323 defsubr (&Sx_create_frame);
3324 defsubr (&Sx_open_connection);
3325 defsubr (&Sx_close_connection);
3326 defsubr (&Sx_display_list);
3328 defsubr (&Sns_hide_others);
3329 defsubr (&Sns_hide_emacs);
3330 defsubr (&Sns_emacs_info_panel);
3331 defsubr (&Sns_list_services);
3332 defsubr (&Sns_perform_service);
3333 defsubr (&Sns_popup_font_panel);
3334 defsubr (&Sns_popup_color_panel);
3336 defsubr (&Sx_show_tip);
3337 defsubr (&Sx_hide_tip);