1 /* Functions for the NeXT/Open/GNUstep and macOS window system.
3 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2017 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",
1238 /* default vertical scrollbars on right on Mac */
1241 #ifdef NS_IMPL_GNUSTEP
1246 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1247 "verticalScrollBars", "VerticalScrollBars",
1250 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
1251 "horizontalScrollBars", "HorizontalScrollBars",
1253 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1254 "foreground", "Foreground", RES_TYPE_STRING);
1255 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1256 "background", "Background", RES_TYPE_STRING);
1257 /* FIXME: not supported yet in Nextstep */
1258 x_default_parameter (f, parms, Qline_spacing, Qnil,
1259 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1260 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1261 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1262 x_default_parameter (f, parms, Qright_fringe, Qnil,
1263 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1264 x_default_parameter (f, parms, Qno_special_glyphs, Qnil,
1265 NULL, NULL, RES_TYPE_BOOLEAN);
1267 init_frame_faces (f);
1269 /* Read comment about this code in corresponding place in xfns.c. */
1270 tem = x_get_arg (dpyinfo, parms, Qmin_width, NULL, NULL, RES_TYPE_NUMBER);
1272 store_frame_param (f, Qmin_width, tem);
1273 tem = x_get_arg (dpyinfo, parms, Qmin_height, NULL, NULL, RES_TYPE_NUMBER);
1275 store_frame_param (f, Qmin_height, tem);
1276 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
1277 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1,
1280 tem = x_get_arg (dpyinfo, parms, Qundecorated, NULL, NULL, RES_TYPE_BOOLEAN);
1281 FRAME_UNDECORATED (f) = !NILP (tem) && !EQ (tem, Qunbound);
1282 store_frame_param (f, Qundecorated, FRAME_UNDECORATED (f) ? Qt : Qnil);
1284 #ifdef NS_IMPL_COCOA
1285 tem = x_get_arg (dpyinfo, parms, Qns_appearance, NULL, NULL, RES_TYPE_SYMBOL);
1286 FRAME_NS_APPEARANCE (f) = EQ (tem, Qdark)
1287 ? ns_appearance_vibrant_dark : ns_appearance_aqua;
1288 store_frame_param (f, Qns_appearance, tem);
1290 tem = x_get_arg (dpyinfo, parms, Qns_transparent_titlebar,
1291 NULL, NULL, RES_TYPE_BOOLEAN);
1292 FRAME_NS_TRANSPARENT_TITLEBAR (f) = !NILP (tem) && !EQ (tem, Qunbound);
1293 store_frame_param (f, Qns_transparent_titlebar, tem);
1296 parent_frame = x_get_arg (dpyinfo, parms, Qparent_frame, NULL, NULL,
1298 /* Accept parent-frame iff parent-id was not specified. */
1300 || EQ (parent_frame, Qunbound)
1301 || NILP (parent_frame)
1302 || !FRAMEP (parent_frame)
1303 || !FRAME_LIVE_P (XFRAME (parent_frame)))
1304 parent_frame = Qnil;
1306 fset_parent_frame (f, parent_frame);
1307 store_frame_param (f, Qparent_frame, parent_frame);
1309 x_default_parameter (f, parms, Qz_group, Qnil, NULL, NULL, RES_TYPE_SYMBOL);
1310 x_default_parameter (f, parms, Qno_focus_on_map, Qnil,
1311 NULL, NULL, RES_TYPE_BOOLEAN);
1312 x_default_parameter (f, parms, Qno_accept_focus, Qnil,
1313 NULL, NULL, RES_TYPE_BOOLEAN);
1315 /* The resources controlling the menu-bar and tool-bar are
1316 processed specially at startup, and reflected in the mode
1317 variables; ignore them here. */
1318 x_default_parameter (f, parms, Qmenu_bar_lines,
1319 NILP (Vmenu_bar_mode)
1320 ? make_number (0) : make_number (1),
1321 NULL, NULL, RES_TYPE_NUMBER);
1322 x_default_parameter (f, parms, Qtool_bar_lines,
1323 NILP (Vtool_bar_mode)
1324 ? make_number (0) : make_number (1),
1325 NULL, NULL, RES_TYPE_NUMBER);
1327 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1328 "BufferPredicate", RES_TYPE_SYMBOL);
1329 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1332 parms = get_geometry_from_preferences (dpyinfo, parms);
1333 window_prompting = x_figure_window_size (f, parms, true, &x_width, &x_height);
1335 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1336 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1338 /* NOTE: on other terms, this is done in set_mouse_color, however this
1339 was not getting called under Nextstep */
1340 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1341 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1342 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1343 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1344 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1345 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1346 f->output_data.ns->vertical_drag_cursor = [NSCursor resizeUpDownCursor];
1347 f->output_data.ns->left_edge_cursor = [NSCursor resizeLeftRightCursor];
1348 f->output_data.ns->top_left_corner_cursor = [NSCursor arrowCursor];
1349 f->output_data.ns->top_edge_cursor = [NSCursor resizeUpDownCursor];
1350 f->output_data.ns->top_right_corner_cursor = [NSCursor arrowCursor];
1351 f->output_data.ns->right_edge_cursor = [NSCursor resizeLeftRightCursor];
1352 f->output_data.ns->bottom_right_corner_cursor = [NSCursor arrowCursor];
1353 f->output_data.ns->bottom_edge_cursor = [NSCursor resizeUpDownCursor];
1354 f->output_data.ns->bottom_left_corner_cursor = [NSCursor arrowCursor];
1356 FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1357 = [NSCursor arrowCursor];
1358 FRAME_DISPLAY_INFO (f)->horizontal_scroll_bar_cursor
1359 = [NSCursor arrowCursor];
1360 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1362 f->output_data.ns->in_animation = NO;
1364 [[EmacsView alloc] initFrameFromEmacs: f];
1368 /* ns_display_info does not have a reference_count. */
1369 f->terminal->reference_count++;
1371 /* It is now ok to make the frame official even if we get an error below.
1372 The frame needs to be on Vframe_list or making it visible won't work. */
1373 Vframe_list = Fcons (frame, Vframe_list);
1375 x_default_parameter (f, parms, Qicon_type, Qnil,
1376 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1378 x_default_parameter (f, parms, Qauto_raise, Qnil,
1379 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1380 x_default_parameter (f, parms, Qauto_lower, Qnil,
1381 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1382 x_default_parameter (f, parms, Qcursor_type, Qbox,
1383 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1384 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1385 "scrollBarWidth", "ScrollBarWidth",
1387 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
1388 "scrollBarHeight", "ScrollBarHeight",
1390 x_default_parameter (f, parms, Qalpha, Qnil,
1391 "alpha", "Alpha", RES_TYPE_NUMBER);
1392 x_default_parameter (f, parms, Qfullscreen, Qnil,
1393 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1395 /* Allow x_set_window_size, now. */
1396 f->can_x_set_window_size = true;
1399 SET_FRAME_WIDTH (f, x_width);
1401 SET_FRAME_HEIGHT (f, x_height);
1403 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1,
1406 if (! f->output_data.ns->explicit_parent)
1408 Lisp_Object visibility;
1410 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1412 if (EQ (visibility, Qunbound))
1415 if (EQ (visibility, Qicon))
1416 x_iconify_frame (f);
1417 else if (! NILP (visibility))
1419 x_make_frame_visible (f);
1420 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1424 /* Must have been Qnil. */
1428 if (FRAME_HAS_MINIBUF_P (f)
1429 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1430 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1431 kset_default_minibuffer_frame (kb, frame);
1433 /* All remaining specified parameters, which have not been "used"
1434 by x_get_arg and friends, now go in the misc. alist of the frame. */
1435 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1436 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1437 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1439 if (window_prompting & USPosition)
1440 x_set_offset (f, f->left_pos, f->top_pos, 1);
1442 /* Make sure windows on this frame appear in calls to next-window
1443 and similar functions. */
1444 Vwindow_list = Qnil;
1446 return unbind_to (count, frame);
1450 x_focus_frame (struct frame *f, bool noactivate)
1452 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1454 if (dpyinfo->x_focus_frame != f)
1456 EmacsView *view = FRAME_NS_VIEW (f);
1458 [NSApp activateIgnoringOtherApps: YES];
1459 [[view window] makeKeyAndOrderFront: view];
1465 ns_window_is_ancestor (NSWindow *win, NSWindow *candidate)
1466 /* Test whether CANDIDATE is an ancestor window of WIN. */
1468 if (candidate == NULL)
1470 else if (win == candidate)
1473 return ns_window_is_ancestor(win, [candidate parentWindow]);
1476 DEFUN ("ns-frame-list-z-order", Fns_frame_list_z_order,
1477 Sns_frame_list_z_order, 0, 1, 0,
1478 doc: /* Return list of Emacs' frames, in Z (stacking) order.
1479 If TERMINAL is non-nil and specifies a live frame, return the child
1480 frames of that frame in Z (stacking) order.
1482 Frames are listed from topmost (first) to bottommost (last). */)
1483 (Lisp_Object terminal)
1485 Lisp_Object frames = Qnil;
1486 NSWindow *parent = nil;
1488 if (FRAMEP (terminal) && FRAME_LIVE_P (XFRAME (terminal)))
1489 parent = [FRAME_NS_VIEW (XFRAME (terminal)) window];
1491 for (NSWindow *win in [[NSApp orderedWindows] reverseObjectEnumerator])
1495 /* Check against [win parentWindow] so that it doesn't match itself. */
1496 if (parent == nil || ns_window_is_ancestor (parent, [win parentWindow]))
1498 XSETFRAME (frame, ((EmacsView *)[win delegate])->emacsframe);
1499 frames = Fcons(frame, frames);
1506 DEFUN ("ns-frame-restack", Fns_frame_restack, Sns_frame_restack, 2, 3, 0,
1507 doc: /* Restack FRAME1 below FRAME2.
1508 This means that if both frames are visible and the display areas of
1509 these frames overlap, FRAME2 (partially) obscures FRAME1. If optional
1510 third argument ABOVE is non-nil, restack FRAME1 above FRAME2. This
1511 means that if both frames are visible and the display areas of these
1512 frames overlap, FRAME1 (partially) obscures FRAME2.
1514 Some window managers may refuse to restack windows. */)
1515 (Lisp_Object frame1, Lisp_Object frame2, Lisp_Object above)
1517 struct frame *f1 = decode_live_frame (frame1);
1518 struct frame *f2 = decode_live_frame (frame2);
1520 if (FRAME_NS_VIEW (f1) && FRAME_NS_VIEW (f2))
1522 NSWindow *window = [FRAME_NS_VIEW (f1) window];
1523 NSInteger window2 = [[FRAME_NS_VIEW (f2) window] windowNumber];
1524 NSWindowOrderingMode flag = NILP (above) ? NSWindowBelow : NSWindowAbove;
1526 [window orderWindow: flag
1527 relativeTo: window2];
1533 error ("Cannot restack frames");
1538 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1540 doc: /* Pop up the font panel. */)
1543 struct frame *f = decode_window_system_frame (frame);
1544 id fm = [NSFontManager sharedFontManager];
1545 struct font *font = f->output_data.ns->font;
1547 #ifdef NS_IMPL_GNUSTEP
1548 nsfont = ((struct nsfont_info *)font)->nsfont;
1550 #ifdef NS_IMPL_COCOA
1551 nsfont = (NSFont *) macfont_get_nsctfont (font);
1553 [fm setSelectedFont: nsfont isMultiple: NO];
1554 [fm orderFrontFontPanel: NSApp];
1559 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1561 doc: /* Pop up the color panel. */)
1564 check_window_system (NULL);
1565 [NSApp orderFrontColorPanel: NSApp];
1573 #ifdef NS_IMPL_GNUSTEP
1574 NSString *dirS, *initS;
1580 ns_run_file_dialog (void)
1582 if (ns_fd_data.panel == nil) return;
1583 #ifdef NS_IMPL_COCOA
1584 ns_fd_data.ret = [ns_fd_data.panel runModal];
1586 if (ns_fd_data.no_types)
1588 ns_fd_data.ret = [ns_fd_data.panel
1589 runModalForDirectory: ns_fd_data.dirS
1590 file: ns_fd_data.initS];
1594 ns_fd_data.ret = [ns_fd_data.panel
1595 runModalForDirectory: ns_fd_data.dirS
1596 file: ns_fd_data.initS
1600 ns_fd_data.panel = nil;
1603 #ifdef NS_IMPL_COCOA
1604 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1090
1605 #define MODAL_OK_RESPONSE NSModalResponseOK
1608 #ifndef MODAL_OK_RESPONSE
1609 #define MODAL_OK_RESPONSE NSOKButton
1612 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1613 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1614 Optional arg DIR, if non-nil, supplies a default directory.
1615 Optional arg MUSTMATCH, if non-nil, means the returned file or
1616 directory must exist.
1617 Optional arg INIT, if non-nil, provides a default file name to use.
1618 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1619 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1620 Lisp_Object init, Lisp_Object dir_only_p)
1622 static id fileDelegate = nil;
1623 BOOL isSave = NILP (mustmatch) && NILP (dir_only_p);
1625 Lisp_Object fname = Qnil;
1627 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1628 [NSString stringWithUTF8String: SSDATA (prompt)];
1629 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1630 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1631 [NSString stringWithUTF8String: SSDATA (dir)];
1632 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1633 [NSString stringWithUTF8String: SSDATA (init)];
1636 check_window_system (NULL);
1638 if (fileDelegate == nil)
1639 fileDelegate = [EmacsFileDelegate new];
1641 [NSCursor setHiddenUntilMouseMoves: NO];
1643 if ([dirS characterAtIndex: 0] == '~')
1644 dirS = [dirS stringByExpandingTildeInPath];
1647 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1649 [panel setTitle: promptS];
1651 [panel setAllowsOtherFileTypes: YES];
1652 [panel setTreatsFilePackagesAsDirectories: YES];
1653 [panel setDelegate: fileDelegate];
1655 if (! NILP (dir_only_p))
1657 [panel setCanChooseDirectories: YES];
1658 [panel setCanChooseFiles: NO];
1662 /* This is not quite what the documentation says, but it is compatible
1663 with the Gtk+ code. Also, the menu entry says "Open File...". */
1664 [panel setCanChooseDirectories: NO];
1665 [panel setCanChooseFiles: YES];
1669 ns_fd_data.panel = panel;
1670 ns_fd_data.ret = NO;
1671 #ifdef NS_IMPL_COCOA
1672 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1673 [panel setAllowedFileTypes: nil];
1674 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1675 if (initS && NILP (Ffile_directory_p (init)))
1676 [panel setNameFieldStringValue: [initS lastPathComponent]];
1678 [panel setNameFieldStringValue: @""];
1681 ns_fd_data.no_types = NILP (mustmatch) && NILP (dir_only_p);
1682 ns_fd_data.dirS = dirS;
1683 ns_fd_data.initS = initS;
1686 /* runModalForDirectory/runModal restarts the main event loop when done,
1687 so we must start an event loop and then pop up the file dialog.
1688 The file dialog may pop up a confirm dialog after Ok has been pressed,
1689 so we can not simply pop down on the Ok/Cancel press.
1691 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
1692 location: NSMakePoint (0, 0)
1695 windowNumber: [[NSApp mainWindow] windowNumber]
1696 context: [NSApp context]
1699 data2: NSAPP_DATA2_RUNFILEDIALOG];
1701 [NSApp postEvent: nxev atStart: NO];
1702 while (ns_fd_data.panel != nil)
1705 if (ns_fd_data.ret == MODAL_OK_RESPONSE)
1707 NSString *str = ns_filename_from_panel (panel);
1708 if (! str) str = ns_directory_from_panel (panel);
1709 if (str) fname = build_string ([str UTF8String]);
1712 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1719 ns_get_defaults_value (const char *key)
1721 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1722 objectForKey: [NSString stringWithUTF8String: key]];
1724 if (!obj) return NULL;
1726 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1730 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1731 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1732 If OWNER is nil, Emacs is assumed. */)
1733 (Lisp_Object owner, Lisp_Object name)
1737 check_window_system (NULL);
1739 owner = build_string([ns_app_name UTF8String]);
1740 CHECK_STRING (name);
1742 value = ns_get_defaults_value (SSDATA (name));
1745 return build_string (value);
1750 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1751 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1752 If OWNER is nil, Emacs is assumed.
1753 If VALUE is nil, the default is removed. */)
1754 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1756 check_window_system (NULL);
1758 owner = build_string ([ns_app_name UTF8String]);
1759 CHECK_STRING (name);
1762 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1763 [NSString stringWithUTF8String: SSDATA (name)]];
1767 CHECK_STRING (value);
1768 [[NSUserDefaults standardUserDefaults] setObject:
1769 [NSString stringWithUTF8String: SSDATA (value)]
1770 forKey: [NSString stringWithUTF8String:
1778 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1779 Sx_server_max_request_size,
1781 doc: /* This function is a no-op. It is only present for completeness. */)
1782 (Lisp_Object terminal)
1784 check_ns_display_info (terminal);
1785 /* This function has no real equivalent under NeXTstep. Return nil to
1791 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1792 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1793 \(Labeling every distributor as a "vendor" embodies the false assumption
1794 that operating systems cannot be developed and distributed noncommercially.)
1795 The optional argument TERMINAL specifies which display to ask about.
1796 TERMINAL should be a terminal object, a frame or a display name (a string).
1797 If omitted or nil, that stands for the selected frame's display. */)
1798 (Lisp_Object terminal)
1800 check_ns_display_info (terminal);
1801 #ifdef NS_IMPL_GNUSTEP
1802 return build_string ("GNU");
1804 return build_string ("Apple");
1809 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1810 doc: /* Return the version numbers of the server of display TERMINAL.
1811 The value is a list of three integers: the major and minor
1812 version numbers of the X Protocol in use, and the distributor-specific release
1813 number. See also the function `x-server-vendor'.
1815 The optional argument TERMINAL specifies which display to ask about.
1816 TERMINAL should be a terminal object, a frame or a display name (a string).
1817 If omitted or nil, that stands for the selected frame's display. */)
1818 (Lisp_Object terminal)
1820 check_ns_display_info (terminal);
1821 /*NOTE: it is unclear what would best correspond with "protocol";
1822 we return 10.3, meaning Panther, since this is roughly the
1823 level that GNUstep's APIs correspond to.
1824 The last number is where we distinguish between the Apple
1825 and GNUstep implementations ("distributor-specific release
1826 number") and give int'ized versions of major.minor. */
1827 return list3i (10, 3, ns_appkit_version_int ());
1831 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1832 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1833 The optional argument TERMINAL specifies which display to ask about.
1834 TERMINAL should be a terminal object, a frame or a display name (a string).
1835 If omitted or nil, that stands for the selected frame's display.
1837 Note: "screen" here is not in Nextstep terminology but in X11's. For
1838 the number of physical monitors, use `(length
1839 \(display-monitor-attributes-list TERMINAL))' instead. */)
1840 (Lisp_Object terminal)
1842 check_ns_display_info (terminal);
1843 return make_number (1);
1847 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1848 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1849 The optional argument TERMINAL specifies which display to ask about.
1850 TERMINAL should be a terminal object, a frame or a display name (a string).
1851 If omitted or nil, that stands for the selected frame's display.
1853 On \"multi-monitor\" setups this refers to the height in millimeters for
1854 all physical monitors associated with TERMINAL. To get information
1855 for each physical monitor, use `display-monitor-attributes-list'. */)
1856 (Lisp_Object terminal)
1858 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1860 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1864 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1865 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1866 The optional argument TERMINAL specifies which display to ask about.
1867 TERMINAL should be a terminal object, a frame or a display name (a string).
1868 If omitted or nil, that stands for the selected frame's display.
1870 On \"multi-monitor\" setups this refers to the width in millimeters for
1871 all physical monitors associated with TERMINAL. To get information
1872 for each physical monitor, use `display-monitor-attributes-list'. */)
1873 (Lisp_Object terminal)
1875 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1877 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1881 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1882 Sx_display_backing_store, 0, 1, 0,
1883 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1884 The value may be `buffered', `retained', or `non-retained'.
1885 The optional argument TERMINAL specifies which display to ask about.
1886 TERMINAL should be a terminal object, a frame or a display name (a string).
1887 If omitted or nil, that stands for the selected frame's display. */)
1888 (Lisp_Object terminal)
1890 check_ns_display_info (terminal);
1891 switch ([ns_get_window (terminal) backingType])
1893 case NSBackingStoreBuffered:
1894 return intern ("buffered");
1895 case NSBackingStoreRetained:
1896 return intern ("retained");
1897 case NSBackingStoreNonretained:
1898 return intern ("non-retained");
1900 error ("Strange value for backingType parameter of frame");
1902 return Qnil; /* not reached, shut compiler up */
1906 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1907 Sx_display_visual_class, 0, 1, 0,
1908 doc: /* Return the visual class of the Nextstep display TERMINAL.
1909 The value is one of the symbols `static-gray', `gray-scale',
1910 `static-color', `pseudo-color', `true-color', or `direct-color'.
1912 The optional argument TERMINAL specifies which display to ask about.
1913 TERMINAL should a terminal object, a frame or a display name (a string).
1914 If omitted or nil, that stands for the selected frame's display. */)
1915 (Lisp_Object terminal)
1917 NSWindowDepth depth;
1919 check_ns_display_info (terminal);
1920 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1922 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1923 return intern ("static-gray");
1924 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1925 return intern ("gray-scale");
1926 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1927 return intern ("pseudo-color");
1928 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1929 return intern ("true-color");
1930 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1931 return intern ("direct-color");
1933 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1934 return intern ("direct-color");
1938 DEFUN ("x-display-save-under", Fx_display_save_under,
1939 Sx_display_save_under, 0, 1, 0,
1940 doc: /* Return t if TERMINAL supports the save-under feature.
1941 The optional argument TERMINAL specifies which display to ask about.
1942 TERMINAL should be a terminal object, a frame or a display name (a string).
1943 If omitted or nil, that stands for the selected frame's display. */)
1944 (Lisp_Object terminal)
1946 check_ns_display_info (terminal);
1947 switch ([ns_get_window (terminal) backingType])
1949 case NSBackingStoreBuffered:
1952 case NSBackingStoreRetained:
1953 case NSBackingStoreNonretained:
1957 error ("Strange value for backingType parameter of frame");
1959 return Qnil; /* not reached, shut compiler up */
1963 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1965 doc: /* Open a connection to a display server.
1966 DISPLAY is the name of the display to connect to.
1967 Optional second arg XRM-STRING is a string of resources in xrdb format.
1968 If the optional third arg MUST-SUCCEED is non-nil,
1969 terminate Emacs if we can't open the connection.
1970 \(In the Nextstep version, the last two arguments are currently ignored.) */)
1971 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1973 struct ns_display_info *dpyinfo;
1975 CHECK_STRING (display);
1977 nxatoms_of_nsselect ();
1978 dpyinfo = ns_term_init (display);
1981 if (!NILP (must_succeed))
1982 fatal ("Display on %s not responding.\n",
1985 error ("Display on %s not responding.\n",
1993 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1995 doc: /* Close the connection to TERMINAL's Nextstep display server.
1996 For TERMINAL, specify a terminal object, a frame or a display name (a
1997 string). If TERMINAL is nil, that stands for the selected frame's
1999 (Lisp_Object terminal)
2001 check_ns_display_info (terminal);
2002 [NSApp terminate: NSApp];
2007 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
2008 doc: /* Return the list of display names that Emacs has connections to. */)
2011 Lisp_Object result = Qnil;
2012 struct ns_display_info *ndi;
2014 for (ndi = x_display_list; ndi; ndi = ndi->next)
2015 result = Fcons (XCAR (ndi->name_list_element), result);
2021 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
2023 doc: /* Hides all applications other than Emacs. */)
2026 check_window_system (NULL);
2027 [NSApp hideOtherApplications: NSApp];
2031 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
2033 doc: /* If ON is non-nil, the entire Emacs application is hidden.
2034 Otherwise if Emacs is hidden, it is unhidden.
2035 If ON is equal to `activate', Emacs is unhidden and becomes
2036 the active application. */)
2039 check_window_system (NULL);
2040 if (EQ (on, intern ("activate")))
2042 [NSApp unhide: NSApp];
2043 [NSApp activateIgnoringOtherApps: YES];
2046 [NSApp unhide: NSApp];
2048 [NSApp hide: NSApp];
2053 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
2055 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
2058 check_window_system (NULL);
2059 [NSApp orderFrontStandardAboutPanel: nil];
2064 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
2065 doc: /* Determine font PostScript or family name for font NAME.
2066 NAME should be a string containing either the font name or an XLFD
2067 font descriptor. If string contains `fontset' and not
2068 `fontset-startup', it is left alone. */)
2072 CHECK_STRING (name);
2077 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
2080 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
2084 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
2085 doc: /* Return a list of all available colors.
2086 The optional argument FRAME is currently ignored. */)
2089 Lisp_Object list = Qnil;
2090 NSEnumerator *colorlists;
2095 CHECK_FRAME (frame);
2096 if (! FRAME_NS_P (XFRAME (frame)))
2097 error ("non-Nextstep frame used in `ns-list-colors'");
2102 colorlists = [[NSColorList availableColorLists] objectEnumerator];
2103 while ((clist = [colorlists nextObject]))
2105 if ([[clist name] length] < 7 ||
2106 [[clist name] rangeOfString: @"PANTONE"].location == 0)
2108 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
2110 while ((cname = [cnames nextObject]))
2111 list = Fcons (build_string ([cname UTF8String]), list);
2112 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
2113 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
2114 UTF8String]), list); */
2124 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
2125 doc: /* List available Nextstep services by querying NSApp. */)
2128 #ifdef NS_IMPL_COCOA
2129 /* You can't get services like this in 10.6+. */
2132 Lisp_Object ret = Qnil;
2135 check_window_system (NULL);
2136 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
2137 [NSApp setServicesMenu: svcs];
2138 [NSApp registerServicesMenuSendTypes: ns_send_types
2139 returnTypes: ns_return_types];
2141 [svcs setAutoenablesItems: NO];
2143 ret = interpret_services_menu (svcs, Qnil, ret);
2149 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2151 doc: /* Perform Nextstep SERVICE on SEND.
2152 SEND should be either a string or nil.
2153 The return value is the result of the service, as string, or nil if
2154 there was no result. */)
2155 (Lisp_Object service, Lisp_Object send)
2161 CHECK_STRING (service);
2162 check_window_system (NULL);
2164 utfStr = SSDATA (service);
2165 svcName = [NSString stringWithUTF8String: utfStr];
2167 pb =[NSPasteboard pasteboardWithUniqueName];
2168 ns_string_to_pasteboard (pb, send);
2170 if (NSPerformService (svcName, pb) == NO)
2171 Fsignal (Qquit, list1 (build_string ("service not available")));
2173 if ([[pb types] count] == 0)
2174 return build_string ("");
2175 return ns_string_from_pasteboard (pb);
2179 #ifdef NS_IMPL_COCOA
2181 /* Compile and execute the AppleScript SCRIPT and return the error
2182 status as function value. A zero is returned if compilation and
2183 execution is successful, in which case *RESULT is set to a Lisp
2184 string or a number containing the resulting script value. Otherwise,
2187 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2189 NSAppleEventDescriptor *desc;
2190 NSDictionary *errorDict;
2191 NSAppleEventDescriptor *returnDescriptor = NULL;
2193 NSAppleScript *scriptObject =
2194 [[NSAppleScript alloc] initWithSource:
2195 [NSString stringWithUTF8String: SSDATA (script)]];
2197 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2198 [scriptObject release];
2201 if (returnDescriptor != NULL)
2203 // successful execution
2204 if (kAENullEvent != [returnDescriptor descriptorType])
2207 // script returned an AppleScript result
2208 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2209 #if defined (NS_IMPL_COCOA)
2210 (typeUTF16ExternalRepresentation
2211 == [returnDescriptor descriptorType]) ||
2213 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2214 (typeCString == [returnDescriptor descriptorType]))
2216 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2218 *result = build_string([[desc stringValue] UTF8String]);
2222 /* use typeUTF16ExternalRepresentation? */
2223 // coerce the result to the appropriate ObjC type
2224 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2226 *result = make_number([desc int32Value]);
2232 // no script result, return error
2238 /* Helper function called from sendEvent to run applescript
2239 from within the main event loop. */
2242 ns_run_ascript (void)
2244 if (! NILP (as_script))
2245 as_status = ns_do_applescript (as_script, as_result);
2249 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2250 doc: /* Execute AppleScript SCRIPT and return the result.
2251 If compilation and execution are successful, the resulting script value
2252 is returned as a string, a number or, in the case of other constructs, t.
2253 In case the execution fails, an error is signaled. */)
2254 (Lisp_Object script)
2259 struct input_event ev;
2261 CHECK_STRING (script);
2262 check_window_system (NULL);
2267 as_result = &result;
2269 /* executing apple script requires the event loop to run, otherwise
2270 errors aren't returned and executeAndReturnError hangs forever.
2271 Post an event that runs applescript and then start the event loop.
2272 The event loop is exited when the script is done. */
2273 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
2274 location: NSMakePoint (0, 0)
2277 windowNumber: [[NSApp mainWindow] windowNumber]
2278 context: [NSApp context]
2281 data2: NSAPP_DATA2_RUNASSCRIPT];
2283 [NSApp postEvent: nxev atStart: NO];
2285 // If there are other events, the event loop may exit. Keep running
2286 // until the script has been handled. */
2287 ns_init_events (&ev);
2288 while (! NILP (as_script))
2290 ns_finish_events ();
2298 else if (!STRINGP (result))
2299 error ("AppleScript error %d", status);
2301 error ("%s", SSDATA (result));
2307 /* ==========================================================================
2309 Miscellaneous functions not called through hooks
2311 ========================================================================== */
2313 /* called from frame.c */
2314 struct ns_display_info *
2315 check_x_display_info (Lisp_Object frame)
2317 return check_ns_display_info (frame);
2322 x_set_scroll_bar_default_width (struct frame *f)
2324 int wid = FRAME_COLUMN_WIDTH (f);
2325 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2326 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2331 x_set_scroll_bar_default_height (struct frame *f)
2333 int height = FRAME_LINE_HEIGHT (f);
2334 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2335 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) +
2336 height - 1) / height;
2339 /* terms impl this instead of x-get-resource directly */
2341 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
2343 /* remove appname prefix; TODO: allow for !="Emacs" */
2344 const char *res, *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2346 check_window_system (NULL);
2348 if (inhibit_x_resources)
2349 /* --quick was passed, so this is a no-op. */
2352 res = ns_get_defaults_value (toCheck);
2353 return (char *) (!res ? NULL
2354 : !c_strncasecmp (res, "YES", 3) ? "true"
2355 : !c_strncasecmp (res, "NO", 2) ? "false"
2361 x_get_focus_frame (struct frame *frame)
2363 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2364 Lisp_Object nsfocus;
2366 if (!dpyinfo->x_focus_frame)
2369 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2373 /* ==========================================================================
2375 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2377 ========================================================================== */
2380 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2381 doc: /* Internal function called by `color-defined-p', which see.
2382 \(Note that the Nextstep version of this function ignores FRAME.) */)
2383 (Lisp_Object color, Lisp_Object frame)
2386 check_window_system (NULL);
2387 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2391 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2392 doc: /* Internal function called by `color-values', which see. */)
2393 (Lisp_Object color, Lisp_Object frame)
2396 EmacsCGFloat red, green, blue, alpha;
2398 check_window_system (NULL);
2399 CHECK_STRING (color);
2402 if (ns_lisp_to_color (color, &col))
2408 [[col colorUsingDefaultColorSpace]
2409 getRed: &red green: &green blue: &blue alpha: &alpha];
2411 return list3i (lrint (red * 65280), lrint (green * 65280),
2412 lrint (blue * 65280));
2416 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2417 doc: /* Internal function called by `display-color-p', which see. */)
2418 (Lisp_Object terminal)
2420 NSWindowDepth depth;
2421 NSString *colorSpace;
2423 check_ns_display_info (terminal);
2424 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2425 colorSpace = NSColorSpaceFromDepth (depth);
2427 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2428 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2433 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2435 doc: /* Return t if the Nextstep display supports shades of gray.
2436 Note that color displays do support shades of gray.
2437 The optional argument TERMINAL specifies which display to ask about.
2438 TERMINAL should be a terminal object, a frame or a display name (a string).
2439 If omitted or nil, that stands for the selected frame's display. */)
2440 (Lisp_Object terminal)
2442 NSWindowDepth depth;
2444 check_ns_display_info (terminal);
2445 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2447 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2451 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2453 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2454 The optional argument TERMINAL specifies which display to ask about.
2455 TERMINAL should be a terminal object, a frame or a display name (a string).
2456 If omitted or nil, that stands for the selected frame's display.
2458 On \"multi-monitor\" setups this refers to the pixel width for all
2459 physical monitors associated with TERMINAL. To get information for
2460 each physical monitor, use `display-monitor-attributes-list'. */)
2461 (Lisp_Object terminal)
2463 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2465 return make_number (x_display_pixel_width (dpyinfo));
2469 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2470 Sx_display_pixel_height, 0, 1, 0,
2471 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2472 The optional argument TERMINAL specifies which display to ask about.
2473 TERMINAL should be a terminal object, a frame or a display name (a string).
2474 If omitted or nil, that stands for the selected frame's display.
2476 On \"multi-monitor\" setups this refers to the pixel height for all
2477 physical monitors associated with TERMINAL. To get information for
2478 each physical monitor, use `display-monitor-attributes-list'. */)
2479 (Lisp_Object terminal)
2481 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2483 return make_number (x_display_pixel_height (dpyinfo));
2486 #ifdef NS_IMPL_COCOA
2488 /* Returns the name for the screen that OBJ represents, or NULL.
2489 Caller must free return value.
2493 ns_get_name_from_ioreg (io_object_t obj)
2497 NSDictionary *info = (NSDictionary *)
2498 IODisplayCreateInfoDictionary (obj, kIODisplayOnlyPreferredName);
2499 NSDictionary *names = [info objectForKey:
2500 [NSString stringWithUTF8String:
2501 kDisplayProductName]];
2503 if ([names count] > 0)
2505 NSString *n = [names objectForKey: [[names allKeys]
2507 if (n != nil) name = xstrdup ([n UTF8String]);
2515 /* Returns the name for the screen that DID came from, or NULL.
2516 Caller must free return value.
2520 ns_screen_name (CGDirectDisplayID did)
2524 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
2525 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2526 if (CGDisplayIOServicePort == NULL)
2529 mach_port_t masterPort;
2533 /* CGDisplayIOServicePort is deprecated. Do it another (harder) way.
2535 Is this code OK for macOS < 10.9, and GNUstep? I suspect it is,
2536 in which case is it worth keeping the other method in here? */
2538 if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
2539 || IOServiceGetMatchingServices (masterPort,
2540 IOServiceMatching ("IONDRVDevice"),
2541 &it) != kIOReturnSuccess)
2544 /* Must loop until we find a name. Many devices can have the same unit
2545 number (represents different GPU parts), but only one has a name. */
2546 while (! name && (obj = IOIteratorNext (it)))
2548 CFMutableDictionaryRef props;
2551 if (IORegistryEntryCreateCFProperties (obj,
2553 kCFAllocatorDefault,
2554 kNilOptions) == kIOReturnSuccess
2556 && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
2558 unsigned nr = [(NSNumber *)val unsignedIntegerValue];
2559 if (nr == CGDisplayUnitNumber (did))
2560 name = ns_get_name_from_ioreg (obj);
2564 IOObjectRelease (obj);
2567 IOObjectRelease (it);
2569 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2572 #endif /* #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 */
2573 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
2574 name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
2578 #endif /* NS_IMPL_COCOA */
2581 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2583 int primary_monitor,
2586 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2587 Lisp_Object frame, rest;
2588 NSArray *screens = [NSScreen screens];
2591 FOR_EACH_FRAME (rest, frame)
2593 struct frame *f = XFRAME (frame);
2597 NSView *view = FRAME_NS_VIEW (f);
2598 NSScreen *screen = [[view window] screen];
2602 for (k = 0; i == -1 && k < [screens count]; ++k)
2604 if ([screens objectAtIndex: k] == screen)
2609 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2613 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2614 monitor_frames, source);
2617 DEFUN ("ns-display-monitor-attributes-list",
2618 Fns_display_monitor_attributes_list,
2619 Sns_display_monitor_attributes_list,
2621 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2623 The optional argument TERMINAL specifies which display to ask about.
2624 TERMINAL should be a terminal object, a frame or a display name (a string).
2625 If omitted or nil, that stands for the selected frame's display.
2627 In addition to the standard attribute keys listed in
2628 `display-monitor-attributes-list', the following keys are contained in
2631 source -- String describing the source from which multi-monitor
2632 information is obtained, \"NS\" is always the source."
2634 Internal use only, use `display-monitor-attributes-list' instead. */)
2635 (Lisp_Object terminal)
2637 struct terminal *term = decode_live_terminal (terminal);
2639 NSUInteger i, n_monitors;
2640 struct MonitorInfo *monitors;
2641 Lisp_Object attributes_list = Qnil;
2642 CGFloat primary_display_height = 0;
2644 if (term->type != output_ns)
2647 screens = [NSScreen screens];
2648 n_monitors = [screens count];
2649 if (n_monitors == 0)
2652 monitors = xzalloc (n_monitors * sizeof *monitors);
2654 for (i = 0; i < [screens count]; ++i)
2656 NSScreen *s = [screens objectAtIndex:i];
2657 struct MonitorInfo *m = &monitors[i];
2658 NSRect fr = [s frame];
2659 NSRect vfr = [s visibleFrame];
2662 #ifdef NS_IMPL_COCOA
2663 NSDictionary *dict = [s deviceDescription];
2664 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2665 CGDirectDisplayID did = [nid unsignedIntValue];
2669 primary_display_height = fr.size.height;
2670 y = (short) fr.origin.y;
2671 vy = (short) vfr.origin.y;
2675 // Flip y coordinate as NS has y starting from the bottom.
2676 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2677 vy = (short) (primary_display_height -
2678 vfr.size.height - vfr.origin.y);
2681 m->geom.x = (short) fr.origin.x;
2683 m->geom.width = (unsigned short) fr.size.width;
2684 m->geom.height = (unsigned short) fr.size.height;
2686 m->work.x = (short) vfr.origin.x;
2687 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2688 // and fr.size.height - vfr.size.height are pixels missing in total.
2689 // Pixels missing at top are
2690 // fr.size.height - vfr.size.height - vy + y.
2691 // work.y is then pixels missing at top + y.
2692 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2693 m->work.width = (unsigned short) vfr.size.width;
2694 m->work.height = (unsigned short) vfr.size.height;
2696 #ifdef NS_IMPL_COCOA
2697 m->name = ns_screen_name (did);
2700 CGSize mms = CGDisplayScreenSize (did);
2701 m->mm_width = (int) mms.width;
2702 m->mm_height = (int) mms.height;
2706 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2707 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2708 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2712 // Primary monitor is always first for NS.
2713 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2716 free_monitors (monitors, n_monitors);
2717 return attributes_list;
2721 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2723 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2724 The optional argument TERMINAL specifies which display to ask about.
2725 TERMINAL should be a terminal object, a frame or a display name (a string).
2726 If omitted or nil, that stands for the selected frame's display. */)
2727 (Lisp_Object terminal)
2729 check_ns_display_info (terminal);
2731 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2735 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2737 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2738 The optional argument TERMINAL specifies which display to ask about.
2739 TERMINAL should be a terminal object, a frame or a display name (a string).
2740 If omitted or nil, that stands for the selected frame's display. */)
2741 (Lisp_Object terminal)
2743 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2744 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2745 return make_number (1 << min (dpyinfo->n_planes, 24));
2749 /* Unused dummy def needed for compatibility. */
2750 Lisp_Object tip_frame;
2752 /* TODO: move to xdisp or similar */
2754 compute_tip_xy (struct frame *f,
2763 Lisp_Object left, top, right, bottom;
2767 /* Start with user-specified or mouse position. */
2768 left = Fcdr (Fassq (Qleft, parms));
2769 top = Fcdr (Fassq (Qtop, parms));
2770 right = Fcdr (Fassq (Qright, parms));
2771 bottom = Fcdr (Fassq (Qbottom, parms));
2773 if ((!INTEGERP (left) && !INTEGERP (right))
2774 || (!INTEGERP (top) && !INTEGERP (bottom)))
2775 pt = [NSEvent mouseLocation];
2778 /* Absolute coordinates. */
2779 pt.x = INTEGERP (left) ? XINT (left) : XINT (right);
2780 pt.y = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
2781 - (INTEGERP (top) ? XINT (top) : XINT (bottom))
2785 /* Find the screen that pt is on. */
2786 for (screen in [NSScreen screens])
2787 if (pt.x >= screen.frame.origin.x
2788 && pt.x < screen.frame.origin.x + screen.frame.size.width
2789 && pt.y >= screen.frame.origin.y
2790 && pt.y < screen.frame.origin.y + screen.frame.size.height)
2793 /* We could use this instead of the if above:
2795 if (CGRectContainsPoint ([screen frame], pt))
2797 which would be neater, but it causes problems building on old
2798 versions of macOS and in GNUstep. */
2800 /* Ensure in bounds. (Note, screen origin = lower left.) */
2801 if (INTEGERP (left) || INTEGERP (right))
2803 else if (pt.x + XINT (dx) <= screen.frame.origin.x)
2804 *root_x = screen.frame.origin.x; /* Can happen for negative dx */
2805 else if (pt.x + XINT (dx) + width
2806 <= screen.frame.origin.x + screen.frame.size.width)
2807 /* It fits to the right of the pointer. */
2808 *root_x = pt.x + XINT (dx);
2809 else if (width + XINT (dx) <= pt.x)
2810 /* It fits to the left of the pointer. */
2811 *root_x = pt.x - width - XINT (dx);
2813 /* Put it left justified on the screen -- it ought to fit that way. */
2814 *root_x = screen.frame.origin.x;
2816 if (INTEGERP (top) || INTEGERP (bottom))
2818 else if (pt.y - XINT (dy) - height >= screen.frame.origin.y)
2819 /* It fits below the pointer. */
2820 *root_y = pt.y - height - XINT (dy);
2821 else if (pt.y + XINT (dy) + height
2822 <= screen.frame.origin.y + screen.frame.size.height)
2823 /* It fits above the pointer */
2824 *root_y = pt.y + XINT (dy);
2826 /* Put it on the top. */
2827 *root_y = screen.frame.origin.y + screen.frame.size.height - height;
2831 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2832 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2833 A tooltip window is a small window displaying a string.
2835 This is an internal function; Lisp code should call `tooltip-show'.
2837 FRAME nil or omitted means use the selected frame.
2839 PARMS is an optional list of frame parameters which can be used to
2840 change the tooltip's appearance.
2842 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2843 means use the default timeout of 5 seconds.
2845 If the list of frame parameters PARMS contains a `left' parameter,
2846 display the tooltip at that x-position. If the list of frame parameters
2847 PARMS contains no `left' but a `right' parameter, display the tooltip
2848 right-adjusted at that x-position. Otherwise display it at the
2849 x-position of the mouse, with offset DX added (default is 5 if DX isn't
2852 Likewise for the y-position: If a `top' frame parameter is specified, it
2853 determines the position of the upper edge of the tooltip window. If a
2854 `bottom' parameter but no `top' frame parameter is specified, it
2855 determines the position of the lower edge of the tooltip window.
2856 Otherwise display the tooltip window at the y-position of the mouse,
2857 with offset DY added (default is -10).
2859 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2860 Text larger than the specified size is clipped. */)
2861 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2864 ptrdiff_t count = SPECPDL_INDEX ();
2869 specbind (Qinhibit_redisplay, Qt);
2871 CHECK_STRING (string);
2872 str = SSDATA (string);
2873 f = decode_window_system_frame (frame);
2875 timeout = make_number (5);
2877 CHECK_NATNUM (timeout);
2880 dx = make_number (5);
2885 dy = make_number (-10);
2890 if (ns_tooltip == nil)
2891 ns_tooltip = [[EmacsTooltip alloc] init];
2895 [ns_tooltip setText: str];
2896 size = [ns_tooltip frame].size;
2898 /* Move the tooltip window where the mouse pointer is. Resize and
2900 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2903 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2906 return unbind_to (count, Qnil);
2910 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2911 doc: /* Hide the current tooltip window, if there is any.
2912 Value is t if tooltip was open, nil otherwise. */)
2915 if (ns_tooltip == nil || ![ns_tooltip isActive])
2921 /* Return geometric attributes of FRAME. According to the value of
2922 ATTRIBUTES return the outer edges of FRAME (Qouter_edges), the inner
2923 edges of FRAME, the root window edges of frame (Qroot_edges). Any
2924 other value means to return the geometry as returned by
2925 Fx_frame_geometry. */
2927 frame_geometry (Lisp_Object frame, Lisp_Object attribute)
2929 struct frame *f = decode_live_frame (frame);
2930 Lisp_Object fullscreen_symbol = Fframe_parameter (frame, Qfullscreen);
2931 bool fullscreen = (EQ (fullscreen_symbol, Qfullboth)
2932 || EQ (fullscreen_symbol, Qfullscreen));
2933 int border = fullscreen ? 0 : f->border_width;
2934 int title_height = fullscreen ? 0 : FRAME_NS_TITLEBAR_HEIGHT (f);
2935 int native_width = FRAME_PIXEL_WIDTH (f);
2936 int native_height = FRAME_PIXEL_HEIGHT (f);
2937 int outer_width = native_width + 2 * border;
2938 int outer_height = native_height + 2 * border + title_height;
2939 int native_left = f->left_pos + border;
2940 int native_top = f->top_pos + border + title_height;
2941 int native_right = f->left_pos + outer_width - border;
2942 int native_bottom = f->top_pos + outer_height - border;
2943 int internal_border_width = FRAME_INTERNAL_BORDER_WIDTH (f);
2944 int tool_bar_height = FRAME_TOOLBAR_HEIGHT (f);
2945 int tool_bar_width = (tool_bar_height
2946 ? outer_width - 2 * internal_border_width
2949 /* Construct list. */
2950 if (EQ (attribute, Qouter_edges))
2951 return list4 (make_number (f->left_pos), make_number (f->top_pos),
2952 make_number (f->left_pos + outer_width),
2953 make_number (f->top_pos + outer_height));
2954 else if (EQ (attribute, Qnative_edges))
2955 return list4 (make_number (native_left), make_number (native_top),
2956 make_number (native_right), make_number (native_bottom));
2957 else if (EQ (attribute, Qinner_edges))
2958 return list4 (make_number (native_left + internal_border_width),
2959 make_number (native_top
2961 + internal_border_width),
2962 make_number (native_right - internal_border_width),
2963 make_number (native_bottom - internal_border_width));
2966 listn (CONSTYPE_HEAP, 10,
2967 Fcons (Qouter_position,
2968 Fcons (make_number (f->left_pos),
2969 make_number (f->top_pos))),
2971 Fcons (make_number (outer_width),
2972 make_number (outer_height))),
2973 Fcons (Qexternal_border_size,
2975 ? Fcons (make_number (0), make_number (0))
2976 : Fcons (make_number (border), make_number (border)))),
2977 Fcons (Qtitle_bar_size,
2978 Fcons (make_number (0), make_number (title_height))),
2979 Fcons (Qmenu_bar_external, Qnil),
2980 Fcons (Qmenu_bar_size, Fcons (make_number (0), make_number (0))),
2981 Fcons (Qtool_bar_external,
2982 FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
2983 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
2984 Fcons (Qtool_bar_size,
2985 Fcons (make_number (tool_bar_width),
2986 make_number (tool_bar_height))),
2987 Fcons (Qinternal_border_width,
2988 make_number (internal_border_width)));
2991 DEFUN ("ns-frame-geometry", Fns_frame_geometry, Sns_frame_geometry, 0, 1, 0,
2992 doc: /* Return geometric attributes of FRAME.
2993 FRAME must be a live frame and defaults to the selected one. The return
2994 value is an association list of the attributes listed below. All height
2995 and width values are in pixels.
2997 `outer-position' is a cons of the outer left and top edges of FRAME
2998 relative to the origin - the position (0, 0) - of FRAME's display.
3000 `outer-size' is a cons of the outer width and height of FRAME. The
3001 outer size includes the title bar and the external borders as well as
3002 any menu and/or tool bar of frame.
3004 `external-border-size' is a cons of the horizontal and vertical width of
3005 FRAME's external borders as supplied by the window manager.
3007 `title-bar-size' is a cons of the width and height of the title bar of
3008 FRAME as supplied by the window manager. If both of them are zero,
3009 FRAME has no title bar. If only the width is zero, Emacs was not
3010 able to retrieve the width information.
3012 `menu-bar-external', if non-nil, means the menu bar is external (never
3013 included in the inner edges of FRAME).
3015 `menu-bar-size' is a cons of the width and height of the menu bar of
3018 `tool-bar-external', if non-nil, means the tool bar is external (never
3019 included in the inner edges of FRAME).
3021 `tool-bar-position' tells on which side the tool bar on FRAME is and can
3022 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
3025 `tool-bar-size' is a cons of the width and height of the tool bar of
3028 `internal-border-width' is the width of the internal border of
3032 return frame_geometry (frame, Qnil);
3035 DEFUN ("ns-frame-edges", Fns_frame_edges, Sns_frame_edges, 0, 2, 0,
3036 doc: /* Return edge coordinates of FRAME.
3037 FRAME must be a live frame and defaults to the selected one. The return
3038 value is a list of the form (LEFT, TOP, RIGHT, BOTTOM). All values are
3039 in pixels relative to the origin - the position (0, 0) - of FRAME's
3042 If optional argument TYPE is the symbol `outer-edges', return the outer
3043 edges of FRAME. The outer edges comprise the decorations of the window
3044 manager (like the title bar or external borders) as well as any external
3045 menu or tool bar of FRAME. If optional argument TYPE is the symbol
3046 `native-edges' or nil, return the native edges of FRAME. The native
3047 edges exclude the decorations of the window manager and any external
3048 menu or tool bar of FRAME. If TYPE is the symbol `inner-edges', return
3049 the inner edges of FRAME. These edges exclude title bar, any borders,
3050 menu bar or tool bar of FRAME. */)
3051 (Lisp_Object frame, Lisp_Object type)
3053 return frame_geometry (frame, ((EQ (type, Qouter_edges)
3054 || EQ (type, Qinner_edges))
3059 DEFUN ("ns-set-mouse-absolute-pixel-position",
3060 Fns_set_mouse_absolute_pixel_position,
3061 Sns_set_mouse_absolute_pixel_position, 2, 2, 0,
3062 doc: /* Move mouse pointer to absolute pixel position (X, Y).
3063 The coordinates X and Y are interpreted in pixels relative to a position
3064 \(0, 0) of the selected frame's display. */)
3065 (Lisp_Object x, Lisp_Object y)
3067 #ifdef NS_IMPL_COCOA
3068 /* GNUstep doesn't support CGWarpMouseCursorPosition, so none of
3070 struct frame *f = SELECTED_FRAME ();
3071 EmacsView *view = FRAME_NS_VIEW (f);
3072 NSScreen *screen = [[view window] screen];
3073 NSRect screen_frame = [screen frame];
3074 int mouse_x, mouse_y;
3076 NSScreen *primary_screen = [[NSScreen screens] objectAtIndex:0];
3077 NSRect primary_screen_frame = [primary_screen frame];
3078 CGFloat primary_screen_height = primary_screen_frame.size.height;
3080 if (FRAME_INITIAL_P (f) || !FRAME_NS_P (f))
3083 CHECK_TYPE_RANGED_INTEGER (int, x);
3084 CHECK_TYPE_RANGED_INTEGER (int, y);
3086 mouse_x = screen_frame.origin.x + XINT (x);
3088 if (screen == primary_screen)
3089 mouse_y = screen_frame.origin.y + XINT (y);
3091 mouse_y = (primary_screen_height - screen_frame.size.height
3092 - screen_frame.origin.y) + XINT (y);
3094 CGPoint mouse_pos = CGPointMake(mouse_x, mouse_y);
3095 CGWarpMouseCursorPosition (mouse_pos);
3096 #endif /* NS_IMPL_COCOA */
3101 DEFUN ("ns-mouse-absolute-pixel-position",
3102 Fns_mouse_absolute_pixel_position,
3103 Sns_mouse_absolute_pixel_position, 0, 0, 0,
3104 doc: /* Return absolute position of mouse cursor in pixels.
3105 The position is returned as a cons cell (X . Y) of the
3106 coordinates of the mouse cursor position in pixels relative to a
3107 position (0, 0) of the selected frame's terminal. */)
3110 struct frame *f = SELECTED_FRAME ();
3111 EmacsView *view = FRAME_NS_VIEW (f);
3112 NSScreen *screen = [[view window] screen];
3113 NSPoint pt = [NSEvent mouseLocation];
3115 return Fcons(make_number(pt.x - screen.frame.origin.x),
3116 make_number(screen.frame.size.height -
3117 (pt.y - screen.frame.origin.y)));
3120 /* ==========================================================================
3122 Class implementations
3124 ========================================================================== */
3127 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
3128 Return YES if handled, NO if not.
3131 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
3137 if ([theEvent type] != NSEventTypeKeyDown) return NO;
3138 s = [theEvent characters];
3140 for (i = 0; i < [s length]; ++i)
3142 int ch = (int) [s characterAtIndex: i];
3145 case NSHomeFunctionKey:
3146 case NSDownArrowFunctionKey:
3147 case NSUpArrowFunctionKey:
3148 case NSLeftArrowFunctionKey:
3149 case NSRightArrowFunctionKey:
3150 case NSPageUpFunctionKey:
3151 case NSPageDownFunctionKey:
3152 case NSEndFunctionKey:
3153 /* Don't send command modified keys, as those are handled in the
3154 performKeyEquivalent method of the super class.
3156 if (! ([theEvent modifierFlags] & NSEventModifierFlagCommand))
3158 [panel sendEvent: theEvent];
3162 /* As we don't have the standard key commands for
3163 copy/paste/cut/select-all in our edit menu, we must handle
3164 them here. TODO: handle Emacs key bindings for copy/cut/select-all
3165 here, paste works, because we have that in our Edit menu.
3166 I.e. refactor out code in nsterm.m, keyDown: to figure out the
3172 case 'a': // Select all
3173 if ([theEvent modifierFlags] & NSEventModifierFlagCommand)
3182 : @selector(selectAll:))))
3187 // Send all control keys, as the text field supports C-a, C-f, C-e
3189 if ([theEvent modifierFlags] & NSEventModifierFlagControl)
3191 [panel sendEvent: theEvent];
3202 @implementation EmacsSavePanel
3203 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3205 BOOL ret = handlePanelKeys (self, theEvent);
3207 ret = [super performKeyEquivalent:theEvent];
3213 @implementation EmacsOpenPanel
3214 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3216 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
3217 BOOL ret = handlePanelKeys (self, theEvent);
3219 ret = [super performKeyEquivalent:theEvent];
3225 @implementation EmacsFileDelegate
3226 /* --------------------------------------------------------------------------
3227 Delegate methods for Open/Save panels
3228 -------------------------------------------------------------------------- */
3229 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
3233 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
3237 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
3238 confirmed: (BOOL)okFlag
3247 /* ==========================================================================
3249 Lisp interface declaration
3251 ========================================================================== */
3255 syms_of_nsfns (void)
3257 DEFSYM (Qfontsize, "fontsize");
3258 DEFSYM (Qframe_title_format, "frame-title-format");
3259 DEFSYM (Qicon_title_format, "icon-title-format");
3260 DEFSYM (Qdark, "dark");
3262 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
3263 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
3264 If the title of a frame matches REGEXP, then IMAGE.tiff is
3265 selected as the image of the icon representing the frame when it's
3266 miniaturized. If an element is t, then Emacs tries to select an icon
3267 based on the filetype of the visited file.
3269 The images have to be installed in a folder called English.lproj in the
3270 Emacs folder. You have to restart Emacs after installing new icons.
3272 Example: Install an icon Gnus.tiff and execute the following code
3274 (setq ns-icon-type-alist
3275 (append ns-icon-type-alist
3276 \\='((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
3279 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
3280 be used as the image of the icon representing the frame. */);
3281 Vns_icon_type_alist = list1 (Qt);
3283 DEFVAR_LISP ("ns-version-string", Vns_version_string,
3284 doc: /* Toolkit version for NS Windowing. */);
3285 Vns_version_string = ns_appkit_version_str ();
3287 defsubr (&Sns_read_file_name);
3288 defsubr (&Sns_get_resource);
3289 defsubr (&Sns_set_resource);
3290 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
3291 defsubr (&Sx_display_grayscale_p);
3292 defsubr (&Sns_font_name);
3293 defsubr (&Sns_list_colors);
3294 #ifdef NS_IMPL_COCOA
3295 defsubr (&Sns_do_applescript);
3297 defsubr (&Sxw_color_defined_p);
3298 defsubr (&Sxw_color_values);
3299 defsubr (&Sx_server_max_request_size);
3300 defsubr (&Sx_server_vendor);
3301 defsubr (&Sx_server_version);
3302 defsubr (&Sx_display_pixel_width);
3303 defsubr (&Sx_display_pixel_height);
3304 defsubr (&Sns_display_monitor_attributes_list);
3305 defsubr (&Sns_frame_geometry);
3306 defsubr (&Sns_frame_edges);
3307 defsubr (&Sns_frame_list_z_order);
3308 defsubr (&Sns_frame_restack);
3309 defsubr (&Sns_set_mouse_absolute_pixel_position);
3310 defsubr (&Sns_mouse_absolute_pixel_position);
3311 defsubr (&Sx_display_mm_width);
3312 defsubr (&Sx_display_mm_height);
3313 defsubr (&Sx_display_screens);
3314 defsubr (&Sx_display_planes);
3315 defsubr (&Sx_display_color_cells);
3316 defsubr (&Sx_display_visual_class);
3317 defsubr (&Sx_display_backing_store);
3318 defsubr (&Sx_display_save_under);
3319 defsubr (&Sx_create_frame);
3320 defsubr (&Sx_open_connection);
3321 defsubr (&Sx_close_connection);
3322 defsubr (&Sx_display_list);
3324 defsubr (&Sns_hide_others);
3325 defsubr (&Sns_hide_emacs);
3326 defsubr (&Sns_emacs_info_panel);
3327 defsubr (&Sns_list_services);
3328 defsubr (&Sns_perform_service);
3329 defsubr (&Sns_popup_font_panel);
3330 defsubr (&Sns_popup_color_panel);
3332 defsubr (&Sx_show_tip);
3333 defsubr (&Sx_hide_tip);