]> code.delx.au - gnu-emacs/blob - src/nsfns.m
94294de1bac7dd83f39cf134769ca1ecef5fa3ac
[gnu-emacs] / src / nsfns.m
1 /* Functions for the NeXT/Open/GNUstep and MacOSX window system.
2
3 Copyright (C) 1989, 1992-1994, 2005-2006, 2008-2015 Free Software
4 Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /*
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
27 */
28
29 /* This should be the first include, as it may set up #defines affecting
30 interpretation of even the system includes. */
31 #include <config.h>
32
33 #include <math.h>
34 #include <c-strcase.h>
35
36 #include "lisp.h"
37 #include "blockinput.h"
38 #include "nsterm.h"
39 #include "window.h"
40 #include "character.h"
41 #include "buffer.h"
42 #include "keyboard.h"
43 #include "termhooks.h"
44 #include "fontset.h"
45 #include "font.h"
46
47 #ifdef NS_IMPL_COCOA
48 #include <IOKit/graphics/IOGraphicsLib.h>
49 #include "macfont.h"
50 #endif
51
52
53 #ifdef HAVE_NS
54
55 extern NSArray *ns_send_types, *ns_return_types, *ns_drag_types;
56
57 EmacsTooltip *ns_tooltip = nil;
58
59 /* Need forward declaration here to preserve organizational integrity of file */
60 Lisp_Object Fx_open_connection (Lisp_Object, Lisp_Object, Lisp_Object);
61
62 /* Static variables to handle applescript execution. */
63 static Lisp_Object as_script, *as_result;
64 static int as_status;
65
66 static ptrdiff_t image_cache_refcount;
67
68
69 /* ==========================================================================
70
71 Internal utility functions
72
73 ========================================================================== */
74
75 /* Let the user specify a Nextstep display with a Lisp object.
76 OBJECT may be nil, a frame or a terminal object.
77 nil stands for the selected frame--or, if that is not a Nextstep frame,
78 the first Nextstep display on the list. */
79
80 static struct ns_display_info *
81 check_ns_display_info (Lisp_Object object)
82 {
83 struct ns_display_info *dpyinfo = NULL;
84
85 if (NILP (object))
86 {
87 struct frame *sf = XFRAME (selected_frame);
88
89 if (FRAME_NS_P (sf) && FRAME_LIVE_P (sf))
90 dpyinfo = FRAME_DISPLAY_INFO (sf);
91 else if (x_display_list != 0)
92 dpyinfo = x_display_list;
93 else
94 error ("Nextstep windows are not in use or not initialized");
95 }
96 else if (TERMINALP (object))
97 {
98 struct terminal *t = decode_live_terminal (object);
99
100 if (t->type != output_ns)
101 error ("Terminal %d is not a Nextstep display", t->id);
102
103 dpyinfo = t->display_info.ns;
104 }
105 else if (STRINGP (object))
106 dpyinfo = ns_display_info_for_name (object);
107 else
108 {
109 struct frame *f = decode_window_system_frame (object);
110 dpyinfo = FRAME_DISPLAY_INFO (f);
111 }
112
113 return dpyinfo;
114 }
115
116
117 static id
118 ns_get_window (Lisp_Object maybeFrame)
119 {
120 id view =nil, window =nil;
121
122 if (!FRAMEP (maybeFrame) || !FRAME_NS_P (XFRAME (maybeFrame)))
123 maybeFrame = selected_frame;/*wrong_type_argument (Qframep, maybeFrame); */
124
125 if (!NILP (maybeFrame))
126 view = FRAME_NS_VIEW (XFRAME (maybeFrame));
127 if (view) window =[view window];
128
129 return window;
130 }
131
132
133 /* Return the X display structure for the display named NAME.
134 Open a new connection if necessary. */
135 struct ns_display_info *
136 ns_display_info_for_name (Lisp_Object name)
137 {
138 struct ns_display_info *dpyinfo;
139
140 CHECK_STRING (name);
141
142 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
143 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
144 return dpyinfo;
145
146 error ("Emacs for Nextstep does not yet support multi-display");
147
148 Fx_open_connection (name, Qnil, Qnil);
149 dpyinfo = x_display_list;
150
151 if (dpyinfo == 0)
152 error ("Display on %s not responding.\n", SDATA (name));
153
154 return dpyinfo;
155 }
156
157 static NSString *
158 ns_filename_from_panel (NSSavePanel *panel)
159 {
160 #ifdef NS_IMPL_COCOA
161 NSURL *url = [panel URL];
162 NSString *str = [url path];
163 return str;
164 #else
165 return [panel filename];
166 #endif
167 }
168
169 static NSString *
170 ns_directory_from_panel (NSSavePanel *panel)
171 {
172 #ifdef NS_IMPL_COCOA
173 NSURL *url = [panel directoryURL];
174 NSString *str = [url path];
175 return str;
176 #else
177 return [panel directory];
178 #endif
179 }
180
181 static Lisp_Object
182 interpret_services_menu (NSMenu *menu, Lisp_Object prefix, Lisp_Object old)
183 /* --------------------------------------------------------------------------
184 Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side
185 -------------------------------------------------------------------------- */
186 {
187 int i, count;
188 NSMenuItem *item;
189 const char *name;
190 Lisp_Object nameStr;
191 unsigned short key;
192 NSString *keys;
193 Lisp_Object res;
194
195 count = [menu numberOfItems];
196 for (i = 0; i<count; i++)
197 {
198 item = [menu itemAtIndex: i];
199 name = [[item title] UTF8String];
200 if (!name) continue;
201
202 nameStr = build_string (name);
203
204 if ([item hasSubmenu])
205 {
206 old = interpret_services_menu ([item submenu],
207 Fcons (nameStr, prefix), old);
208 }
209 else
210 {
211 keys = [item keyEquivalent];
212 if (keys && [keys length] )
213 {
214 key = [keys characterAtIndex: 0];
215 res = make_number (key|super_modifier);
216 }
217 else
218 {
219 res = Qundefined;
220 }
221 old = Fcons (Fcons (res,
222 Freverse (Fcons (nameStr,
223 prefix))),
224 old);
225 }
226 }
227 return old;
228 }
229
230
231
232 /* ==========================================================================
233
234 Frame parameter setters
235
236 ========================================================================== */
237
238
239 static void
240 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
241 {
242 NSColor *col;
243 EmacsCGFloat r, g, b, alpha;
244
245 /* Must block_input, because ns_lisp_to_color does block/unblock_input
246 which means that col may be deallocated in its unblock_input if there
247 is user input, unless we also block_input. */
248 block_input ();
249 if (ns_lisp_to_color (arg, &col))
250 {
251 store_frame_param (f, Qforeground_color, oldval);
252 unblock_input ();
253 error ("Unknown color");
254 }
255
256 [col retain];
257 [f->output_data.ns->foreground_color release];
258 f->output_data.ns->foreground_color = col;
259
260 [col getRed: &r green: &g blue: &b alpha: &alpha];
261 FRAME_FOREGROUND_PIXEL (f) =
262 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
263
264 if (FRAME_NS_VIEW (f))
265 {
266 update_face_from_frame_parameter (f, Qforeground_color, arg);
267 /*recompute_basic_faces (f); */
268 if (FRAME_VISIBLE_P (f))
269 SET_FRAME_GARBAGED (f);
270 }
271 unblock_input ();
272 }
273
274
275 static void
276 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
277 {
278 struct face *face;
279 NSColor *col;
280 NSView *view = FRAME_NS_VIEW (f);
281 EmacsCGFloat r, g, b, alpha;
282
283 block_input ();
284 if (ns_lisp_to_color (arg, &col))
285 {
286 store_frame_param (f, Qbackground_color, oldval);
287 unblock_input ();
288 error ("Unknown color");
289 }
290
291 /* clear the frame; in some instances the NS-internal GC appears not to
292 update, or it does update and cannot clear old text properly */
293 if (FRAME_VISIBLE_P (f))
294 ns_clear_frame (f);
295
296 [col retain];
297 [f->output_data.ns->background_color release];
298 f->output_data.ns->background_color = col;
299
300 [col getRed: &r green: &g blue: &b alpha: &alpha];
301 FRAME_BACKGROUND_PIXEL (f) =
302 ARGB_TO_ULONG ((int)(alpha*0xff), (int)(r*0xff), (int)(g*0xff), (int)(b*0xff));
303
304 if (view != nil)
305 {
306 [[view window] setBackgroundColor: col];
307
308 if (alpha != (EmacsCGFloat) 1.0)
309 [[view window] setOpaque: NO];
310 else
311 [[view window] setOpaque: YES];
312
313 face = FRAME_DEFAULT_FACE (f);
314 if (face)
315 {
316 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f);
317 face->background = ns_index_color
318 ([col colorWithAlphaComponent: alpha], f);
319
320 update_face_from_frame_parameter (f, Qbackground_color, arg);
321 }
322
323 if (FRAME_VISIBLE_P (f))
324 SET_FRAME_GARBAGED (f);
325 }
326 unblock_input ();
327 }
328
329
330 static void
331 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
332 {
333 NSColor *col;
334
335 block_input ();
336 if (ns_lisp_to_color (arg, &col))
337 {
338 store_frame_param (f, Qcursor_color, oldval);
339 unblock_input ();
340 error ("Unknown color");
341 }
342
343 [FRAME_CURSOR_COLOR (f) release];
344 FRAME_CURSOR_COLOR (f) = [col retain];
345
346 if (FRAME_VISIBLE_P (f))
347 {
348 x_update_cursor (f, 0);
349 x_update_cursor (f, 1);
350 }
351 update_face_from_frame_parameter (f, Qcursor_color, arg);
352 unblock_input ();
353 }
354
355
356 static void
357 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
358 {
359 NSView *view = FRAME_NS_VIEW (f);
360 NSTRACE ("x_set_icon_name");
361
362 /* see if it's changed */
363 if (STRINGP (arg))
364 {
365 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
366 return;
367 }
368 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
369 return;
370
371 fset_icon_name (f, arg);
372
373 if (NILP (arg))
374 {
375 if (!NILP (f->title))
376 arg = f->title;
377 else
378 /* Explicit name and no icon-name -> explicit_name. */
379 if (f->explicit_name)
380 arg = f->name;
381 else
382 {
383 /* No explicit name and no icon-name ->
384 name has to be rebuild from icon_title_format. */
385 windows_or_buffers_changed = 62;
386 return;
387 }
388 }
389
390 /* Don't change the name if it's already NAME. */
391 if ([[view window] miniwindowTitle]
392 && ([[[view window] miniwindowTitle]
393 isEqualToString: [NSString stringWithUTF8String:
394 SSDATA (arg)]]))
395 return;
396
397 [[view window] setMiniwindowTitle:
398 [NSString stringWithUTF8String: SSDATA (arg)]];
399 }
400
401 static void
402 ns_set_name_internal (struct frame *f, Lisp_Object name)
403 {
404 Lisp_Object encoded_name, encoded_icon_name;
405 NSString *str;
406 NSView *view = FRAME_NS_VIEW (f);
407
408 encoded_name = ENCODE_UTF_8 (name);
409
410 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
411
412 /* Don't change the name if it's already NAME. */
413 if (! [[[view window] title] isEqualToString: str])
414 [[view window] setTitle: str];
415
416 if (!STRINGP (f->icon_name))
417 encoded_icon_name = encoded_name;
418 else
419 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
420
421 str = [NSString stringWithUTF8String: SSDATA (encoded_icon_name)];
422
423 if ([[view window] miniwindowTitle]
424 && ! [[[view window] miniwindowTitle] isEqualToString: str])
425 [[view window] setMiniwindowTitle: str];
426
427 }
428
429 static void
430 ns_set_name (struct frame *f, Lisp_Object name, int explicit)
431 {
432 NSTRACE ("ns_set_name");
433
434 /* Make sure that requests from lisp code override requests from
435 Emacs redisplay code. */
436 if (explicit)
437 {
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;
442
443 f->explicit_name = ! NILP (name);
444 }
445 else if (f->explicit_name)
446 return;
447
448 if (NILP (name))
449 name = build_string ([ns_app_name UTF8String]);
450 else
451 CHECK_STRING (name);
452
453 /* Don't change the name if it's already NAME. */
454 if (! NILP (Fstring_equal (name, f->name)))
455 return;
456
457 fset_name (f, name);
458
459 /* Title overrides explicit name. */
460 if (! NILP (f->title))
461 name = f->title;
462
463 ns_set_name_internal (f, name);
464 }
465
466
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
469 redisplay code. */
470 static void
471 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
472 {
473 NSTRACE ("x_explicitly_set_name");
474 ns_set_name (f, arg, 1);
475 }
476
477
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
480 lisp code. */
481 void
482 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
483 {
484 NSTRACE ("x_implicitly_set_name");
485
486 /* Deal with NS specific format t. */
487 if (FRAME_NS_P (f) && ((FRAME_ICONIFIED_P (f) && EQ (Vicon_title_format, Qt))
488 || EQ (Vframe_title_format, Qt)))
489 ns_set_name_as_filename (f);
490 else
491 ns_set_name (f, arg, 0);
492 }
493
494
495 /* Change the title of frame F to NAME.
496 If NAME is nil, use the frame name as the title. */
497
498 static void
499 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
500 {
501 NSTRACE ("x_set_title");
502 /* Don't change the title if it's already NAME. */
503 if (EQ (name, f->title))
504 return;
505
506 update_mode_lines = 22;
507
508 fset_title (f, name);
509
510 if (NILP (name))
511 name = f->name;
512 else
513 CHECK_STRING (name);
514
515 ns_set_name_internal (f, name);
516 }
517
518
519 void
520 ns_set_name_as_filename (struct frame *f)
521 {
522 NSView *view;
523 Lisp_Object name, filename;
524 Lisp_Object buf = XWINDOW (f->selected_window)->contents;
525 const char *title;
526 NSAutoreleasePool *pool;
527 Lisp_Object encoded_name, encoded_filename;
528 NSString *str;
529 NSTRACE ("ns_set_name_as_filename");
530
531 if (f->explicit_name || ! NILP (f->title))
532 return;
533
534 block_input ();
535 pool = [[NSAutoreleasePool alloc] init];
536 filename = BVAR (XBUFFER (buf), filename);
537 name = BVAR (XBUFFER (buf), name);
538
539 if (NILP (name))
540 {
541 if (! NILP (filename))
542 name = Ffile_name_nondirectory (filename);
543 else
544 name = build_string ([ns_app_name UTF8String]);
545 }
546
547 encoded_name = ENCODE_UTF_8 (name);
548
549 view = FRAME_NS_VIEW (f);
550
551 title = FRAME_ICONIFIED_P (f) ? [[[view window] miniwindowTitle] UTF8String]
552 : [[[view window] title] UTF8String];
553
554 if (title && (! strcmp (title, SSDATA (encoded_name))))
555 {
556 [pool release];
557 unblock_input ();
558 return;
559 }
560
561 str = [NSString stringWithUTF8String: SSDATA (encoded_name)];
562 if (str == nil) str = @"Bad coding";
563
564 if (FRAME_ICONIFIED_P (f))
565 [[view window] setMiniwindowTitle: str];
566 else
567 {
568 NSString *fstr;
569
570 if (! NILP (filename))
571 {
572 encoded_filename = ENCODE_UTF_8 (filename);
573
574 fstr = [NSString stringWithUTF8String: SSDATA (encoded_filename)];
575 if (fstr == nil) fstr = @"";
576 }
577 else
578 fstr = @"";
579
580 ns_set_represented_filename (fstr, f);
581 [[view window] setTitle: str];
582 fset_name (f, name);
583 }
584
585 [pool release];
586 unblock_input ();
587 }
588
589
590 void
591 ns_set_doc_edited (void)
592 {
593 NSAutoreleasePool *pool;
594 Lisp_Object tail, frame;
595 block_input ();
596 pool = [[NSAutoreleasePool alloc] init];
597 FOR_EACH_FRAME (tail, frame)
598 {
599 BOOL edited = NO;
600 struct frame *f = XFRAME (frame);
601 struct window *w;
602 NSView *view;
603
604 if (! FRAME_NS_P (f)) continue;
605 w = XWINDOW (FRAME_SELECTED_WINDOW (f));
606 view = FRAME_NS_VIEW (f);
607 if (!MINI_WINDOW_P (w))
608 edited = ! NILP (Fbuffer_modified_p (w->contents)) &&
609 ! NILP (Fbuffer_file_name (w->contents));
610 [[view window] setDocumentEdited: edited];
611 }
612
613 [pool release];
614 unblock_input ();
615 }
616
617
618 void
619 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
620 {
621 int nlines;
622 if (FRAME_MINIBUF_ONLY_P (f))
623 return;
624
625 if (TYPE_RANGED_INTEGERP (int, value))
626 nlines = XINT (value);
627 else
628 nlines = 0;
629
630 FRAME_MENU_BAR_LINES (f) = 0;
631 if (nlines)
632 {
633 FRAME_EXTERNAL_MENU_BAR (f) = 1;
634 /* does for all frames, whereas we just want for one frame
635 [NSMenu setMenuBarVisible: YES]; */
636 }
637 else
638 {
639 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
640 free_frame_menubar (f);
641 /* [NSMenu setMenuBarVisible: NO]; */
642 FRAME_EXTERNAL_MENU_BAR (f) = 0;
643 }
644 }
645
646
647 /* toolbar support */
648 void
649 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
650 {
651 /* Currently, when the tool bar change state, the frame is resized.
652
653 TODO: It would be better if this didn't occur when 1) the frame
654 is full height or maximized or 2) when specified by
655 `frame-inhibit-implied-resize'. */
656 int nlines;
657
658 if (FRAME_MINIBUF_ONLY_P (f))
659 return;
660
661 if (RANGED_INTEGERP (0, value, INT_MAX))
662 nlines = XFASTINT (value);
663 else
664 nlines = 0;
665
666 if (nlines)
667 {
668 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
669 update_frame_tool_bar (f);
670 }
671 else
672 {
673 if (FRAME_EXTERNAL_TOOL_BAR (f))
674 {
675 free_frame_tool_bar (f);
676 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
677
678 {
679 EmacsView *view = FRAME_NS_VIEW (f);
680 int fs_state = [view fullscreenState];
681
682 if (fs_state == FULLSCREEN_MAXIMIZED)
683 {
684 [view setFSValue:FULLSCREEN_WIDTH];
685 }
686 else if (fs_state == FULLSCREEN_HEIGHT)
687 {
688 [view setFSValue:FULLSCREEN_NONE];
689 }
690 }
691 }
692 }
693
694 {
695 int inhibit
696 = ((f->after_make_frame
697 && !f->tool_bar_resized
698 && (EQ (frame_inhibit_implied_resize, Qt)
699 || (CONSP (frame_inhibit_implied_resize)
700 && !NILP (Fmemq (Qtool_bar_lines,
701 frame_inhibit_implied_resize))))
702 && NILP (get_frame_param (f, Qfullscreen)))
703 ? 0
704 : 2);
705
706 frame_size_history_add (f, Qupdate_frame_tool_bar, 0, 0, Qnil);
707 adjust_frame_size (f, -1, -1, inhibit, 0, Qtool_bar_lines);
708 }
709 }
710
711
712 void
713 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
714 {
715 int old_width = FRAME_INTERNAL_BORDER_WIDTH (f);
716
717 CHECK_TYPE_RANGED_INTEGER (int, arg);
718 FRAME_INTERNAL_BORDER_WIDTH (f) = XINT (arg);
719 if (FRAME_INTERNAL_BORDER_WIDTH (f) < 0)
720 FRAME_INTERNAL_BORDER_WIDTH (f) = 0;
721
722 if (FRAME_INTERNAL_BORDER_WIDTH (f) == old_width)
723 return;
724
725 if (FRAME_X_WINDOW (f) != 0)
726 adjust_frame_size (f, -1, -1, 3, 0, Qinternal_border_width);
727
728 SET_FRAME_GARBAGED (f);
729 }
730
731
732 static void
733 ns_implicitly_set_icon_type (struct frame *f)
734 {
735 Lisp_Object tem;
736 EmacsView *view = FRAME_NS_VIEW (f);
737 id image = nil;
738 Lisp_Object chain, elt;
739 NSAutoreleasePool *pool;
740 BOOL setMini = YES;
741
742 NSTRACE ("ns_implicitly_set_icon_type");
743
744 block_input ();
745 pool = [[NSAutoreleasePool alloc] init];
746 if (f->output_data.ns->miniimage
747 && [[NSString stringWithUTF8String: SSDATA (f->name)]
748 isEqualToString: [(NSImage *)f->output_data.ns->miniimage name]])
749 {
750 [pool release];
751 unblock_input ();
752 return;
753 }
754
755 tem = assq_no_quit (Qicon_type, f->param_alist);
756 if (CONSP (tem) && ! NILP (XCDR (tem)))
757 {
758 [pool release];
759 unblock_input ();
760 return;
761 }
762
763 for (chain = Vns_icon_type_alist;
764 image == nil && CONSP (chain);
765 chain = XCDR (chain))
766 {
767 elt = XCAR (chain);
768 /* special case: t means go by file type */
769 if (SYMBOLP (elt) && EQ (elt, Qt) && SSDATA (f->name)[0] == '/')
770 {
771 NSString *str
772 = [NSString stringWithUTF8String: SSDATA (f->name)];
773 if ([[NSFileManager defaultManager] fileExistsAtPath: str])
774 image = [[[NSWorkspace sharedWorkspace] iconForFile: str] retain];
775 }
776 else if (CONSP (elt) &&
777 STRINGP (XCAR (elt)) &&
778 STRINGP (XCDR (elt)) &&
779 fast_string_match (XCAR (elt), f->name) >= 0)
780 {
781 image = [EmacsImage allocInitFromFile: XCDR (elt)];
782 if (image == nil)
783 image = [[NSImage imageNamed:
784 [NSString stringWithUTF8String:
785 SSDATA (XCDR (elt))]] retain];
786 }
787 }
788
789 if (image == nil)
790 {
791 image = [[[NSWorkspace sharedWorkspace] iconForFileType: @"text"] retain];
792 setMini = NO;
793 }
794
795 [f->output_data.ns->miniimage release];
796 f->output_data.ns->miniimage = image;
797 [view setMiniwindowImage: setMini];
798 [pool release];
799 unblock_input ();
800 }
801
802
803 static void
804 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
805 {
806 EmacsView *view = FRAME_NS_VIEW (f);
807 id image = nil;
808 BOOL setMini = YES;
809
810 NSTRACE ("x_set_icon_type");
811
812 if (!NILP (arg) && SYMBOLP (arg))
813 {
814 arg =build_string (SSDATA (SYMBOL_NAME (arg)));
815 store_frame_param (f, Qicon_type, arg);
816 }
817
818 /* do it the implicit way */
819 if (NILP (arg))
820 {
821 ns_implicitly_set_icon_type (f);
822 return;
823 }
824
825 CHECK_STRING (arg);
826
827 image = [EmacsImage allocInitFromFile: arg];
828 if (image == nil)
829 image =[NSImage imageNamed: [NSString stringWithUTF8String:
830 SSDATA (arg)]];
831
832 if (image == nil)
833 {
834 image = [NSImage imageNamed: @"text"];
835 setMini = NO;
836 }
837
838 f->output_data.ns->miniimage = image;
839 [view setMiniwindowImage: setMini];
840 }
841
842
843 /* TODO: move to nsterm? */
844 int
845 ns_lisp_to_cursor_type (Lisp_Object arg)
846 {
847 char *str;
848 if (XTYPE (arg) == Lisp_String)
849 str = SSDATA (arg);
850 else if (XTYPE (arg) == Lisp_Symbol)
851 str = SSDATA (SYMBOL_NAME (arg));
852 else return -1;
853 if (!strcmp (str, "box")) return FILLED_BOX_CURSOR;
854 if (!strcmp (str, "hollow")) return HOLLOW_BOX_CURSOR;
855 if (!strcmp (str, "hbar")) return HBAR_CURSOR;
856 if (!strcmp (str, "bar")) return BAR_CURSOR;
857 if (!strcmp (str, "no")) return NO_CURSOR;
858 return -1;
859 }
860
861
862 Lisp_Object
863 ns_cursor_type_to_lisp (int arg)
864 {
865 switch (arg)
866 {
867 case FILLED_BOX_CURSOR: return Qbox;
868 case HOLLOW_BOX_CURSOR: return Qhollow;
869 case HBAR_CURSOR: return Qhbar;
870 case BAR_CURSOR: return Qbar;
871 case NO_CURSOR:
872 default: return intern ("no");
873 }
874 }
875
876 /* This is the same as the xfns.c definition. */
877 static void
878 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
879 {
880 set_frame_cursor_types (f, arg);
881 }
882
883 /* called to set mouse pointer color, but all other terms use it to
884 initialize pointer types (and don't set the color ;) */
885 static void
886 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
887 {
888 /* don't think we can do this on Nextstep */
889 }
890
891
892 #define Str(x) #x
893 #define Xstr(x) Str(x)
894
895 static Lisp_Object
896 ns_appkit_version_str (void)
897 {
898 char tmp[256];
899
900 #ifdef NS_IMPL_GNUSTEP
901 sprintf(tmp, "gnustep-gui-%s", Xstr(GNUSTEP_GUI_VERSION));
902 #elif defined (NS_IMPL_COCOA)
903 NSString *osversion
904 = [[NSProcessInfo processInfo] operatingSystemVersionString];
905 sprintf(tmp, "appkit-%.2f %s",
906 NSAppKitVersionNumber,
907 [osversion UTF8String]);
908 #else
909 tmp = "ns-unknown";
910 #endif
911 return build_string (tmp);
912 }
913
914
915 /* This is for use by x-server-version and collapses all version info we
916 have into a single int. For a better picture of the implementation
917 running, use ns_appkit_version_str.*/
918 static int
919 ns_appkit_version_int (void)
920 {
921 #ifdef NS_IMPL_GNUSTEP
922 return GNUSTEP_GUI_MAJOR_VERSION * 100 + GNUSTEP_GUI_MINOR_VERSION;
923 #elif defined (NS_IMPL_COCOA)
924 return (int)NSAppKitVersionNumber;
925 #endif
926 return 0;
927 }
928
929
930 static void
931 x_icon (struct frame *f, Lisp_Object parms)
932 /* --------------------------------------------------------------------------
933 Strangely-named function to set icon position parameters in frame.
934 This is irrelevant under OS X, but might be needed under GNUstep,
935 depending on the window manager used. Note, this is not a standard
936 frame parameter-setter; it is called directly from x-create-frame.
937 -------------------------------------------------------------------------- */
938 {
939 Lisp_Object icon_x, icon_y;
940 struct ns_display_info *dpyinfo = check_ns_display_info (Qnil);
941
942 f->output_data.ns->icon_top = -1;
943 f->output_data.ns->icon_left = -1;
944
945 /* Set the position of the icon. */
946 icon_x = x_get_arg (dpyinfo, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
947 icon_y = x_get_arg (dpyinfo, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
948 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
949 {
950 CHECK_NUMBER (icon_x);
951 CHECK_NUMBER (icon_y);
952 f->output_data.ns->icon_top = XINT (icon_y);
953 f->output_data.ns->icon_left = XINT (icon_x);
954 }
955 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
956 error ("Both left and top icon corners of icon must be specified");
957 }
958
959
960 /* Note: see frame.c for template, also where generic functions are impl */
961 frame_parm_handler ns_frame_parm_handlers[] =
962 {
963 x_set_autoraise, /* generic OK */
964 x_set_autolower, /* generic OK */
965 x_set_background_color,
966 0, /* x_set_border_color, may be impossible under Nextstep */
967 0, /* x_set_border_width, may be impossible under Nextstep */
968 x_set_cursor_color,
969 x_set_cursor_type,
970 x_set_font, /* generic OK */
971 x_set_foreground_color,
972 x_set_icon_name,
973 x_set_icon_type,
974 x_set_internal_border_width, /* generic OK */
975 0, /* x_set_right_divider_width */
976 0, /* x_set_bottom_divider_width */
977 x_set_menu_bar_lines,
978 x_set_mouse_color,
979 x_explicitly_set_name,
980 x_set_scroll_bar_width, /* generic OK */
981 x_set_scroll_bar_height, /* generic OK */
982 x_set_title,
983 x_set_unsplittable, /* generic OK */
984 x_set_vertical_scroll_bars, /* generic OK */
985 x_set_horizontal_scroll_bars, /* generic OK */
986 x_set_visibility, /* generic OK */
987 x_set_tool_bar_lines,
988 0, /* x_set_scroll_bar_foreground, will ignore (not possible on NS) */
989 0, /* x_set_scroll_bar_background, will ignore (not possible on NS) */
990 x_set_screen_gamma, /* generic OK */
991 x_set_line_spacing, /* generic OK, sets f->extra_line_spacing to int */
992 x_set_left_fringe, /* generic OK */
993 x_set_right_fringe, /* generic OK */
994 0, /* x_set_wait_for_wm, will ignore */
995 x_set_fullscreen, /* generic OK */
996 x_set_font_backend, /* generic OK */
997 x_set_alpha,
998 0, /* x_set_sticky */
999 0, /* x_set_tool_bar_position */
1000 };
1001
1002
1003 /* Handler for signals raised during x_create_frame.
1004 FRAME is the frame which is partially constructed. */
1005
1006 static void
1007 unwind_create_frame (Lisp_Object frame)
1008 {
1009 struct frame *f = XFRAME (frame);
1010
1011 /* If frame is already dead, nothing to do. This can happen if the
1012 display is disconnected after the frame has become official, but
1013 before x_create_frame removes the unwind protect. */
1014 if (!FRAME_LIVE_P (f))
1015 return;
1016
1017 /* If frame is ``official'', nothing to do. */
1018 if (NILP (Fmemq (frame, Vframe_list)))
1019 {
1020 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1021 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1022 #endif
1023
1024 /* If the frame's image cache refcount is still the same as our
1025 private shadow variable, it means we are unwinding a frame
1026 for which we didn't yet call init_frame_faces, where the
1027 refcount is incremented. Therefore, we increment it here, so
1028 that free_frame_faces, called in x_free_frame_resources
1029 below, will not mistakenly decrement the counter that was not
1030 incremented yet to account for this new frame. */
1031 if (FRAME_IMAGE_CACHE (f) != NULL
1032 && FRAME_IMAGE_CACHE (f)->refcount == image_cache_refcount)
1033 FRAME_IMAGE_CACHE (f)->refcount++;
1034
1035 x_free_frame_resources (f);
1036 free_glyphs (f);
1037
1038 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1039 /* Check that reference counts are indeed correct. */
1040 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
1041 #endif
1042 }
1043 }
1044
1045 /*
1046 * Read geometry related parameters from preferences if not in PARMS.
1047 * Returns the union of parms and any preferences read.
1048 */
1049
1050 static Lisp_Object
1051 get_geometry_from_preferences (struct ns_display_info *dpyinfo,
1052 Lisp_Object parms)
1053 {
1054 struct {
1055 const char *val;
1056 const char *cls;
1057 Lisp_Object tem;
1058 } r[] = {
1059 { "width", "Width", Qwidth },
1060 { "height", "Height", Qheight },
1061 { "left", "Left", Qleft },
1062 { "top", "Top", Qtop },
1063 };
1064
1065 int i;
1066 for (i = 0; i < ARRAYELTS (r); ++i)
1067 {
1068 if (NILP (Fassq (r[i].tem, parms)))
1069 {
1070 Lisp_Object value
1071 = x_get_arg (dpyinfo, parms, r[i].tem, r[i].val, r[i].cls,
1072 RES_TYPE_NUMBER);
1073 if (! EQ (value, Qunbound))
1074 parms = Fcons (Fcons (r[i].tem, value), parms);
1075 }
1076 }
1077
1078 return parms;
1079 }
1080
1081 /* ==========================================================================
1082
1083 Lisp definitions
1084
1085 ========================================================================== */
1086
1087 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
1088 1, 1, 0,
1089 doc: /* Make a new Nextstep window, called a "frame" in Emacs terms.
1090 Return an Emacs frame object.
1091 PARMS is an alist of frame parameters.
1092 If the parameters specify that the frame should not have a minibuffer,
1093 and do not specify a specific minibuffer window to use,
1094 then `default-minibuffer-frame' must be a frame whose minibuffer can
1095 be shared by the new frame.
1096
1097 This function is an internal primitive--use `make-frame' instead. */)
1098 (Lisp_Object parms)
1099 {
1100 struct frame *f;
1101 Lisp_Object frame, tem;
1102 Lisp_Object name;
1103 int minibuffer_only = 0;
1104 long window_prompting = 0;
1105 ptrdiff_t count = specpdl_ptr - specpdl;
1106 Lisp_Object display;
1107 struct ns_display_info *dpyinfo = NULL;
1108 Lisp_Object parent;
1109 struct kboard *kb;
1110 static int desc_ctr = 1;
1111 int x_width = 0, x_height = 0;
1112
1113 /* x_get_arg modifies parms. */
1114 parms = Fcopy_alist (parms);
1115
1116 /* Use this general default value to start with
1117 until we know if this frame has a specified name. */
1118 Vx_resource_name = Vinvocation_name;
1119
1120 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_STRING);
1121 if (EQ (display, Qunbound))
1122 display = Qnil;
1123 dpyinfo = check_ns_display_info (display);
1124 kb = dpyinfo->terminal->kboard;
1125
1126 if (!dpyinfo->terminal->name)
1127 error ("Terminal is not live, can't create new frames on it");
1128
1129 name = x_get_arg (dpyinfo, parms, Qname, 0, 0, RES_TYPE_STRING);
1130 if (!STRINGP (name)
1131 && ! EQ (name, Qunbound)
1132 && ! NILP (name))
1133 error ("Invalid frame name--not a string or nil");
1134
1135 if (STRINGP (name))
1136 Vx_resource_name = name;
1137
1138 parent = x_get_arg (dpyinfo, parms, Qparent_id, 0, 0, RES_TYPE_NUMBER);
1139 if (EQ (parent, Qunbound))
1140 parent = Qnil;
1141 if (! NILP (parent))
1142 CHECK_NUMBER (parent);
1143
1144 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
1145 /* No need to protect DISPLAY because that's not used after passing
1146 it to make_frame_without_minibuffer. */
1147 frame = Qnil;
1148 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
1149 RES_TYPE_SYMBOL);
1150 if (EQ (tem, Qnone) || NILP (tem))
1151 f = make_frame_without_minibuffer (Qnil, kb, display);
1152 else if (EQ (tem, Qonly))
1153 {
1154 f = make_minibuffer_frame ();
1155 minibuffer_only = 1;
1156 }
1157 else if (WINDOWP (tem))
1158 f = make_frame_without_minibuffer (tem, kb, display);
1159 else
1160 f = make_frame (1);
1161
1162 XSETFRAME (frame, f);
1163
1164 f->terminal = dpyinfo->terminal;
1165
1166 f->output_method = output_ns;
1167 f->output_data.ns = xzalloc (sizeof *f->output_data.ns);
1168
1169 FRAME_FONTSET (f) = -1;
1170
1171 fset_icon_name (f, x_get_arg (dpyinfo, parms, Qicon_name,
1172 "iconName", "Title",
1173 RES_TYPE_STRING));
1174 if (! STRINGP (f->icon_name))
1175 fset_icon_name (f, Qnil);
1176
1177 FRAME_DISPLAY_INFO (f) = dpyinfo;
1178
1179 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
1180 record_unwind_protect (unwind_create_frame, frame);
1181
1182 f->output_data.ns->window_desc = desc_ctr++;
1183 if (TYPE_RANGED_INTEGERP (Window, parent))
1184 {
1185 f->output_data.ns->parent_desc = XFASTINT (parent);
1186 f->output_data.ns->explicit_parent = 1;
1187 }
1188 else
1189 {
1190 f->output_data.ns->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
1191 f->output_data.ns->explicit_parent = 0;
1192 }
1193
1194 /* Set the name; the functions to which we pass f expect the name to
1195 be set. */
1196 if (EQ (name, Qunbound) || NILP (name) || ! STRINGP (name))
1197 {
1198 fset_name (f, build_string ([ns_app_name UTF8String]));
1199 f->explicit_name = 0;
1200 }
1201 else
1202 {
1203 fset_name (f, name);
1204 f->explicit_name = 1;
1205 specbind (Qx_resource_name, name);
1206 }
1207
1208 block_input ();
1209
1210 #ifdef NS_IMPL_COCOA
1211 mac_register_font_driver (f);
1212 #else
1213 register_font_driver (&nsfont_driver, f);
1214 #endif
1215
1216 image_cache_refcount =
1217 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
1218
1219 x_default_parameter (f, parms, Qfont_backend, Qnil,
1220 "fontBackend", "FontBackend", RES_TYPE_STRING);
1221
1222 {
1223 /* use for default font name */
1224 id font = [NSFont userFixedPitchFontOfSize: -1.0]; /* default */
1225 x_default_parameter (f, parms, Qfontsize,
1226 make_number (0 /*(int)[font pointSize]*/),
1227 "fontSize", "FontSize", RES_TYPE_NUMBER);
1228 // Remove ' Regular', not handled by backends.
1229 char *fontname = xstrdup ([[font displayName] UTF8String]);
1230 int len = strlen (fontname);
1231 if (len > 8 && strcmp (fontname + len - 8, " Regular") == 0)
1232 fontname[len-8] = '\0';
1233 x_default_parameter (f, parms, Qfont,
1234 build_string (fontname),
1235 "font", "Font", RES_TYPE_STRING);
1236 xfree (fontname);
1237 }
1238 unblock_input ();
1239
1240 x_default_parameter (f, parms, Qborder_width, make_number (0),
1241 "borderwidth", "BorderWidth", RES_TYPE_NUMBER);
1242 x_default_parameter (f, parms, Qinternal_border_width, make_number (2),
1243 "internalBorderWidth", "InternalBorderWidth",
1244 RES_TYPE_NUMBER);
1245
1246 /* default vertical scrollbars on right on Mac */
1247 {
1248 Lisp_Object spos
1249 #ifdef NS_IMPL_GNUSTEP
1250 = Qt;
1251 #else
1252 = Qright;
1253 #endif
1254 x_default_parameter (f, parms, Qvertical_scroll_bars, spos,
1255 "verticalScrollBars", "VerticalScrollBars",
1256 RES_TYPE_SYMBOL);
1257 }
1258 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
1259 "horizontalScrollBars", "HorizontalScrollBars",
1260 RES_TYPE_SYMBOL);
1261 x_default_parameter (f, parms, Qforeground_color, build_string ("Black"),
1262 "foreground", "Foreground", RES_TYPE_STRING);
1263 x_default_parameter (f, parms, Qbackground_color, build_string ("White"),
1264 "background", "Background", RES_TYPE_STRING);
1265 /* FIXME: not supported yet in Nextstep */
1266 x_default_parameter (f, parms, Qline_spacing, Qnil,
1267 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
1268 x_default_parameter (f, parms, Qleft_fringe, Qnil,
1269 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
1270 x_default_parameter (f, parms, Qright_fringe, Qnil,
1271 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
1272
1273 init_frame_faces (f);
1274
1275 /* Read comment about this code in corresponding place in xfns.c. */
1276 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
1277 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1,
1278 Qx_create_frame_1);
1279
1280 /* The resources controlling the menu-bar and tool-bar are
1281 processed specially at startup, and reflected in the mode
1282 variables; ignore them here. */
1283 x_default_parameter (f, parms, Qmenu_bar_lines,
1284 NILP (Vmenu_bar_mode)
1285 ? make_number (0) : make_number (1),
1286 NULL, NULL, RES_TYPE_NUMBER);
1287 x_default_parameter (f, parms, Qtool_bar_lines,
1288 NILP (Vtool_bar_mode)
1289 ? make_number (0) : make_number (1),
1290 NULL, NULL, RES_TYPE_NUMBER);
1291
1292 x_default_parameter (f, parms, Qbuffer_predicate, Qnil, "bufferPredicate",
1293 "BufferPredicate", RES_TYPE_SYMBOL);
1294 x_default_parameter (f, parms, Qtitle, Qnil, "title", "Title",
1295 RES_TYPE_STRING);
1296
1297 parms = get_geometry_from_preferences (dpyinfo, parms);
1298 window_prompting = x_figure_window_size (f, parms, true, &x_width, &x_height);
1299
1300 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
1301 f->no_split = minibuffer_only || (!EQ (tem, Qunbound) && !EQ (tem, Qnil));
1302
1303 /* NOTE: on other terms, this is done in set_mouse_color, however this
1304 was not getting called under Nextstep */
1305 f->output_data.ns->text_cursor = [NSCursor IBeamCursor];
1306 f->output_data.ns->nontext_cursor = [NSCursor arrowCursor];
1307 f->output_data.ns->modeline_cursor = [NSCursor pointingHandCursor];
1308 f->output_data.ns->hand_cursor = [NSCursor pointingHandCursor];
1309 f->output_data.ns->hourglass_cursor = [NSCursor disappearingItemCursor];
1310 f->output_data.ns->horizontal_drag_cursor = [NSCursor resizeLeftRightCursor];
1311 f->output_data.ns->vertical_drag_cursor = [NSCursor resizeUpDownCursor];
1312 FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor
1313 = [NSCursor arrowCursor];
1314 FRAME_DISPLAY_INFO (f)->horizontal_scroll_bar_cursor
1315 = [NSCursor arrowCursor];
1316 f->output_data.ns->current_pointer = f->output_data.ns->text_cursor;
1317
1318 f->output_data.ns->in_animation = NO;
1319
1320 [[EmacsView alloc] initFrameFromEmacs: f];
1321
1322 x_icon (f, parms);
1323
1324 /* ns_display_info does not have a reference_count. */
1325 f->terminal->reference_count++;
1326
1327 /* It is now ok to make the frame official even if we get an error below.
1328 The frame needs to be on Vframe_list or making it visible won't work. */
1329 Vframe_list = Fcons (frame, Vframe_list);
1330
1331 x_default_parameter (f, parms, Qicon_type, Qnil,
1332 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
1333
1334 x_default_parameter (f, parms, Qauto_raise, Qnil,
1335 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
1336 x_default_parameter (f, parms, Qauto_lower, Qnil,
1337 "autoLower", "AutoLower", RES_TYPE_BOOLEAN);
1338 x_default_parameter (f, parms, Qcursor_type, Qbox,
1339 "cursorType", "CursorType", RES_TYPE_SYMBOL);
1340 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
1341 "scrollBarWidth", "ScrollBarWidth",
1342 RES_TYPE_NUMBER);
1343 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
1344 "scrollBarHeight", "ScrollBarHeight",
1345 RES_TYPE_NUMBER);
1346 x_default_parameter (f, parms, Qalpha, Qnil,
1347 "alpha", "Alpha", RES_TYPE_NUMBER);
1348 x_default_parameter (f, parms, Qfullscreen, Qnil,
1349 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
1350
1351 /* Allow x_set_window_size, now. */
1352 f->can_x_set_window_size = true;
1353
1354 if (x_width > 0)
1355 SET_FRAME_WIDTH (f, x_width);
1356 if (x_height > 0)
1357 SET_FRAME_HEIGHT (f, x_height);
1358
1359 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1,
1360 Qx_create_frame_2);
1361
1362 if (! f->output_data.ns->explicit_parent)
1363 {
1364 Lisp_Object visibility;
1365
1366 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
1367 RES_TYPE_SYMBOL);
1368 if (EQ (visibility, Qunbound))
1369 visibility = Qt;
1370
1371 if (EQ (visibility, Qicon))
1372 x_iconify_frame (f);
1373 else if (! NILP (visibility))
1374 {
1375 x_make_frame_visible (f);
1376 [[FRAME_NS_VIEW (f) window] makeKeyWindow];
1377 }
1378 else
1379 {
1380 /* Must have been Qnil. */
1381 }
1382 }
1383
1384 if (FRAME_HAS_MINIBUF_P (f)
1385 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
1386 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
1387 kset_default_minibuffer_frame (kb, frame);
1388
1389 /* All remaining specified parameters, which have not been "used"
1390 by x_get_arg and friends, now go in the misc. alist of the frame. */
1391 for (tem = parms; CONSP (tem); tem = XCDR (tem))
1392 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
1393 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
1394
1395 if (window_prompting & USPosition)
1396 x_set_offset (f, f->left_pos, f->top_pos, 1);
1397
1398 /* Make sure windows on this frame appear in calls to next-window
1399 and similar functions. */
1400 Vwindow_list = Qnil;
1401
1402 return unbind_to (count, frame);
1403 }
1404
1405 void
1406 x_focus_frame (struct frame *f)
1407 {
1408 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1409
1410 if (dpyinfo->x_focus_frame != f)
1411 {
1412 EmacsView *view = FRAME_NS_VIEW (f);
1413 block_input ();
1414 [NSApp activateIgnoringOtherApps: YES];
1415 [[view window] makeKeyAndOrderFront: view];
1416 unblock_input ();
1417 }
1418 }
1419
1420
1421 DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel,
1422 0, 1, "",
1423 doc: /* Pop up the font panel. */)
1424 (Lisp_Object frame)
1425 {
1426 struct frame *f = decode_window_system_frame (frame);
1427 id fm = [NSFontManager sharedFontManager];
1428 struct font *font = f->output_data.ns->font;
1429 NSFont *nsfont;
1430 #ifdef NS_IMPL_GNUSTEP
1431 nsfont = ((struct nsfont_info *)font)->nsfont;
1432 #endif
1433 #ifdef NS_IMPL_COCOA
1434 nsfont = (NSFont *) macfont_get_nsctfont (font);
1435 #endif
1436 [fm setSelectedFont: nsfont isMultiple: NO];
1437 [fm orderFrontFontPanel: NSApp];
1438 return Qnil;
1439 }
1440
1441
1442 DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel,
1443 0, 1, "",
1444 doc: /* Pop up the color panel. */)
1445 (Lisp_Object frame)
1446 {
1447 check_window_system (NULL);
1448 [NSApp orderFrontColorPanel: NSApp];
1449 return Qnil;
1450 }
1451
1452 static struct
1453 {
1454 id panel;
1455 BOOL ret;
1456 #ifdef NS_IMPL_GNUSTEP
1457 NSString *dirS, *initS;
1458 BOOL no_types;
1459 #endif
1460 } ns_fd_data;
1461
1462 void
1463 ns_run_file_dialog (void)
1464 {
1465 if (ns_fd_data.panel == nil) return;
1466 #ifdef NS_IMPL_COCOA
1467 ns_fd_data.ret = [ns_fd_data.panel runModal];
1468 #else
1469 if (ns_fd_data.no_types)
1470 {
1471 ns_fd_data.ret = [ns_fd_data.panel
1472 runModalForDirectory: ns_fd_data.dirS
1473 file: ns_fd_data.initS];
1474 }
1475 else
1476 {
1477 ns_fd_data.ret = [ns_fd_data.panel
1478 runModalForDirectory: ns_fd_data.dirS
1479 file: ns_fd_data.initS
1480 types: nil];
1481 }
1482 #endif
1483 ns_fd_data.panel = nil;
1484 }
1485
1486 #ifdef NS_IMPL_COCOA
1487 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_9
1488 #define MODAL_OK_RESPONSE NSModalResponseOK
1489 #endif
1490 #endif
1491 #ifndef MODAL_OK_RESPONSE
1492 #define MODAL_OK_RESPONSE NSOKButton
1493 #endif
1494
1495 DEFUN ("ns-read-file-name", Fns_read_file_name, Sns_read_file_name, 1, 5, 0,
1496 doc: /* Use a graphical panel to read a file name, using prompt PROMPT.
1497 Optional arg DIR, if non-nil, supplies a default directory.
1498 Optional arg MUSTMATCH, if non-nil, means the returned file or
1499 directory must exist.
1500 Optional arg INIT, if non-nil, provides a default file name to use.
1501 Optional arg DIR_ONLY_P, if non-nil, means choose only directories. */)
1502 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object mustmatch,
1503 Lisp_Object init, Lisp_Object dir_only_p)
1504 {
1505 static id fileDelegate = nil;
1506 BOOL isSave = NILP (mustmatch) && NILP (dir_only_p);
1507 id panel;
1508 Lisp_Object fname = Qnil;
1509
1510 NSString *promptS = NILP (prompt) || !STRINGP (prompt) ? nil :
1511 [NSString stringWithUTF8String: SSDATA (prompt)];
1512 NSString *dirS = NILP (dir) || !STRINGP (dir) ?
1513 [NSString stringWithUTF8String: SSDATA (BVAR (current_buffer, directory))] :
1514 [NSString stringWithUTF8String: SSDATA (dir)];
1515 NSString *initS = NILP (init) || !STRINGP (init) ? nil :
1516 [NSString stringWithUTF8String: SSDATA (init)];
1517 NSEvent *nxev;
1518
1519 check_window_system (NULL);
1520
1521 if (fileDelegate == nil)
1522 fileDelegate = [EmacsFileDelegate new];
1523
1524 [NSCursor setHiddenUntilMouseMoves: NO];
1525
1526 if ([dirS characterAtIndex: 0] == '~')
1527 dirS = [dirS stringByExpandingTildeInPath];
1528
1529 panel = isSave ?
1530 (id)[EmacsSavePanel savePanel] : (id)[EmacsOpenPanel openPanel];
1531
1532 [panel setTitle: promptS];
1533
1534 [panel setAllowsOtherFileTypes: YES];
1535 [panel setTreatsFilePackagesAsDirectories: YES];
1536 [panel setDelegate: fileDelegate];
1537
1538 if (! NILP (dir_only_p))
1539 {
1540 [panel setCanChooseDirectories: YES];
1541 [panel setCanChooseFiles: NO];
1542 }
1543 else if (! isSave)
1544 {
1545 /* This is not quite what the documentation says, but it is compatible
1546 with the Gtk+ code. Also, the menu entry says "Open File...". */
1547 [panel setCanChooseDirectories: NO];
1548 [panel setCanChooseFiles: YES];
1549 }
1550
1551 block_input ();
1552 ns_fd_data.panel = panel;
1553 ns_fd_data.ret = NO;
1554 #ifdef NS_IMPL_COCOA
1555 if (! NILP (mustmatch) || ! NILP (dir_only_p))
1556 [panel setAllowedFileTypes: nil];
1557 if (dirS) [panel setDirectoryURL: [NSURL fileURLWithPath: dirS]];
1558 if (initS && NILP (Ffile_directory_p (init)))
1559 [panel setNameFieldStringValue: [initS lastPathComponent]];
1560 else
1561 [panel setNameFieldStringValue: @""];
1562
1563 #else
1564 ns_fd_data.no_types = NILP (mustmatch) && NILP (dir_only_p);
1565 ns_fd_data.dirS = dirS;
1566 ns_fd_data.initS = initS;
1567 #endif
1568
1569 /* runModalForDirectory/runModal restarts the main event loop when done,
1570 so we must start an event loop and then pop up the file dialog.
1571 The file dialog may pop up a confirm dialog after Ok has been pressed,
1572 so we can not simply pop down on the Ok/Cancel press.
1573 */
1574 nxev = [NSEvent otherEventWithType: NSApplicationDefined
1575 location: NSMakePoint (0, 0)
1576 modifierFlags: 0
1577 timestamp: 0
1578 windowNumber: [[NSApp mainWindow] windowNumber]
1579 context: [NSApp context]
1580 subtype: 0
1581 data1: 0
1582 data2: NSAPP_DATA2_RUNFILEDIALOG];
1583
1584 [NSApp postEvent: nxev atStart: NO];
1585 while (ns_fd_data.panel != nil)
1586 [NSApp run];
1587
1588 if (ns_fd_data.ret == MODAL_OK_RESPONSE)
1589 {
1590 NSString *str = ns_filename_from_panel (panel);
1591 if (! str) str = ns_directory_from_panel (panel);
1592 if (str) fname = build_string ([str UTF8String]);
1593 }
1594
1595 [[FRAME_NS_VIEW (SELECTED_FRAME ()) window] makeKeyWindow];
1596 unblock_input ();
1597
1598 return fname;
1599 }
1600
1601 const char *
1602 ns_get_defaults_value (const char *key)
1603 {
1604 NSObject *obj = [[NSUserDefaults standardUserDefaults]
1605 objectForKey: [NSString stringWithUTF8String: key]];
1606
1607 if (!obj) return NULL;
1608
1609 return [[NSString stringWithFormat: @"%@", obj] UTF8String];
1610 }
1611
1612
1613 DEFUN ("ns-get-resource", Fns_get_resource, Sns_get_resource, 2, 2, 0,
1614 doc: /* Return the value of the property NAME of OWNER from the defaults database.
1615 If OWNER is nil, Emacs is assumed. */)
1616 (Lisp_Object owner, Lisp_Object name)
1617 {
1618 const char *value;
1619
1620 check_window_system (NULL);
1621 if (NILP (owner))
1622 owner = build_string([ns_app_name UTF8String]);
1623 CHECK_STRING (name);
1624
1625 value = ns_get_defaults_value (SSDATA (name));
1626
1627 if (value)
1628 return build_string (value);
1629 return Qnil;
1630 }
1631
1632
1633 DEFUN ("ns-set-resource", Fns_set_resource, Sns_set_resource, 3, 3, 0,
1634 doc: /* Set property NAME of OWNER to VALUE, from the defaults database.
1635 If OWNER is nil, Emacs is assumed.
1636 If VALUE is nil, the default is removed. */)
1637 (Lisp_Object owner, Lisp_Object name, Lisp_Object value)
1638 {
1639 check_window_system (NULL);
1640 if (NILP (owner))
1641 owner = build_string ([ns_app_name UTF8String]);
1642 CHECK_STRING (name);
1643 if (NILP (value))
1644 {
1645 [[NSUserDefaults standardUserDefaults] removeObjectForKey:
1646 [NSString stringWithUTF8String: SSDATA (name)]];
1647 }
1648 else
1649 {
1650 CHECK_STRING (value);
1651 [[NSUserDefaults standardUserDefaults] setObject:
1652 [NSString stringWithUTF8String: SSDATA (value)]
1653 forKey: [NSString stringWithUTF8String:
1654 SSDATA (name)]];
1655 }
1656
1657 return Qnil;
1658 }
1659
1660
1661 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
1662 Sx_server_max_request_size,
1663 0, 1, 0,
1664 doc: /* This function is a no-op. It is only present for completeness. */)
1665 (Lisp_Object terminal)
1666 {
1667 check_ns_display_info (terminal);
1668 /* This function has no real equivalent under NeXTstep. Return nil to
1669 indicate this. */
1670 return Qnil;
1671 }
1672
1673
1674 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
1675 doc: /* Return the "vendor ID" string of Nextstep display server TERMINAL.
1676 (Labeling every distributor as a "vendor" embodies the false assumption
1677 that operating systems cannot be developed and distributed noncommercially.)
1678 The optional argument TERMINAL specifies which display to ask about.
1679 TERMINAL should be a terminal object, a frame or a display name (a string).
1680 If omitted or nil, that stands for the selected frame's display. */)
1681 (Lisp_Object terminal)
1682 {
1683 check_ns_display_info (terminal);
1684 #ifdef NS_IMPL_GNUSTEP
1685 return build_string ("GNU");
1686 #else
1687 return build_string ("Apple");
1688 #endif
1689 }
1690
1691
1692 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
1693 doc: /* Return the version numbers of the server of display TERMINAL.
1694 The value is a list of three integers: the major and minor
1695 version numbers of the X Protocol in use, and the distributor-specific release
1696 number. See also the function `x-server-vendor'.
1697
1698 The optional argument TERMINAL specifies which display to ask about.
1699 TERMINAL should be a terminal object, a frame or a display name (a string).
1700 If omitted or nil, that stands for the selected frame's display. */)
1701 (Lisp_Object terminal)
1702 {
1703 check_ns_display_info (terminal);
1704 /*NOTE: it is unclear what would best correspond with "protocol";
1705 we return 10.3, meaning Panther, since this is roughly the
1706 level that GNUstep's APIs correspond to.
1707 The last number is where we distinguish between the Apple
1708 and GNUstep implementations ("distributor-specific release
1709 number") and give int'ized versions of major.minor. */
1710 return list3i (10, 3, ns_appkit_version_int ());
1711 }
1712
1713
1714 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
1715 doc: /* Return the number of screens on Nextstep display server TERMINAL.
1716 The optional argument TERMINAL specifies which display to ask about.
1717 TERMINAL should be a terminal object, a frame or a display name (a string).
1718 If omitted or nil, that stands for the selected frame's display.
1719
1720 Note: "screen" here is not in Nextstep terminology but in X11's. For
1721 the number of physical monitors, use `(length
1722 (display-monitor-attributes-list TERMINAL))' instead. */)
1723 (Lisp_Object terminal)
1724 {
1725 check_ns_display_info (terminal);
1726 return make_number (1);
1727 }
1728
1729
1730 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
1731 doc: /* Return the height in millimeters of the Nextstep display TERMINAL.
1732 The optional argument TERMINAL specifies which display to ask about.
1733 TERMINAL should be a terminal object, a frame or a display name (a string).
1734 If omitted or nil, that stands for the selected frame's display.
1735
1736 On \"multi-monitor\" setups this refers to the height in millimeters for
1737 all physical monitors associated with TERMINAL. To get information
1738 for each physical monitor, use `display-monitor-attributes-list'. */)
1739 (Lisp_Object terminal)
1740 {
1741 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1742
1743 return make_number (x_display_pixel_height (dpyinfo) / (92.0/25.4));
1744 }
1745
1746
1747 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
1748 doc: /* Return the width in millimeters of the Nextstep display TERMINAL.
1749 The optional argument TERMINAL specifies which display to ask about.
1750 TERMINAL should be a terminal object, a frame or a display name (a string).
1751 If omitted or nil, that stands for the selected frame's display.
1752
1753 On \"multi-monitor\" setups this refers to the width in millimeters for
1754 all physical monitors associated with TERMINAL. To get information
1755 for each physical monitor, use `display-monitor-attributes-list'. */)
1756 (Lisp_Object terminal)
1757 {
1758 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
1759
1760 return make_number (x_display_pixel_width (dpyinfo) / (92.0/25.4));
1761 }
1762
1763
1764 DEFUN ("x-display-backing-store", Fx_display_backing_store,
1765 Sx_display_backing_store, 0, 1, 0,
1766 doc: /* Return an indication of whether the Nextstep display TERMINAL does backing store.
1767 The value may be `buffered', `retained', or `non-retained'.
1768 The optional argument TERMINAL specifies which display to ask about.
1769 TERMINAL should be a terminal object, a frame or a display name (a string).
1770 If omitted or nil, that stands for the selected frame's display. */)
1771 (Lisp_Object terminal)
1772 {
1773 check_ns_display_info (terminal);
1774 switch ([ns_get_window (terminal) backingType])
1775 {
1776 case NSBackingStoreBuffered:
1777 return intern ("buffered");
1778 case NSBackingStoreRetained:
1779 return intern ("retained");
1780 case NSBackingStoreNonretained:
1781 return intern ("non-retained");
1782 default:
1783 error ("Strange value for backingType parameter of frame");
1784 }
1785 return Qnil; /* not reached, shut compiler up */
1786 }
1787
1788
1789 DEFUN ("x-display-visual-class", Fx_display_visual_class,
1790 Sx_display_visual_class, 0, 1, 0,
1791 doc: /* Return the visual class of the Nextstep display TERMINAL.
1792 The value is one of the symbols `static-gray', `gray-scale',
1793 `static-color', `pseudo-color', `true-color', or `direct-color'.
1794
1795 The optional argument TERMINAL specifies which display to ask about.
1796 TERMINAL should 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)
1799 {
1800 NSWindowDepth depth;
1801
1802 check_ns_display_info (terminal);
1803 depth = [[[NSScreen screens] objectAtIndex:0] depth];
1804
1805 if ( depth == NSBestDepth (NSCalibratedWhiteColorSpace, 2, 2, YES, NULL))
1806 return intern ("static-gray");
1807 else if (depth == NSBestDepth (NSCalibratedWhiteColorSpace, 8, 8, YES, NULL))
1808 return intern ("gray-scale");
1809 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 8, YES, NULL))
1810 return intern ("pseudo-color");
1811 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 4, 12, NO, NULL))
1812 return intern ("true-color");
1813 else if ( depth == NSBestDepth (NSCalibratedRGBColorSpace, 8, 24, NO, NULL))
1814 return intern ("direct-color");
1815 else
1816 /* color mgmt as far as we do it is really handled by Nextstep itself anyway */
1817 return intern ("direct-color");
1818 }
1819
1820
1821 DEFUN ("x-display-save-under", Fx_display_save_under,
1822 Sx_display_save_under, 0, 1, 0,
1823 doc: /* Return t if TERMINAL supports the save-under feature.
1824 The optional argument TERMINAL specifies which display to ask about.
1825 TERMINAL should be a terminal object, a frame or a display name (a string).
1826 If omitted or nil, that stands for the selected frame's display. */)
1827 (Lisp_Object terminal)
1828 {
1829 check_ns_display_info (terminal);
1830 switch ([ns_get_window (terminal) backingType])
1831 {
1832 case NSBackingStoreBuffered:
1833 return Qt;
1834
1835 case NSBackingStoreRetained:
1836 case NSBackingStoreNonretained:
1837 return Qnil;
1838
1839 default:
1840 error ("Strange value for backingType parameter of frame");
1841 }
1842 return Qnil; /* not reached, shut compiler up */
1843 }
1844
1845
1846 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
1847 1, 3, 0,
1848 doc: /* Open a connection to a display server.
1849 DISPLAY is the name of the display to connect to.
1850 Optional second arg XRM-STRING is a string of resources in xrdb format.
1851 If the optional third arg MUST-SUCCEED is non-nil,
1852 terminate Emacs if we can't open the connection.
1853 (In the Nextstep version, the last two arguments are currently ignored.) */)
1854 (Lisp_Object display, Lisp_Object resource_string, Lisp_Object must_succeed)
1855 {
1856 struct ns_display_info *dpyinfo;
1857
1858 CHECK_STRING (display);
1859
1860 nxatoms_of_nsselect ();
1861 dpyinfo = ns_term_init (display);
1862 if (dpyinfo == 0)
1863 {
1864 if (!NILP (must_succeed))
1865 fatal ("Display on %s not responding.\n",
1866 SSDATA (display));
1867 else
1868 error ("Display on %s not responding.\n",
1869 SSDATA (display));
1870 }
1871
1872 return Qnil;
1873 }
1874
1875
1876 DEFUN ("x-close-connection", Fx_close_connection, Sx_close_connection,
1877 1, 1, 0,
1878 doc: /* Close the connection to TERMINAL's Nextstep display server.
1879 For TERMINAL, specify a terminal object, a frame or a display name (a
1880 string). If TERMINAL is nil, that stands for the selected frame's
1881 terminal. */)
1882 (Lisp_Object terminal)
1883 {
1884 check_ns_display_info (terminal);
1885 [NSApp terminate: NSApp];
1886 return Qnil;
1887 }
1888
1889
1890 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
1891 doc: /* Return the list of display names that Emacs has connections to. */)
1892 (void)
1893 {
1894 Lisp_Object result = Qnil;
1895 struct ns_display_info *ndi;
1896
1897 for (ndi = x_display_list; ndi; ndi = ndi->next)
1898 result = Fcons (XCAR (ndi->name_list_element), result);
1899
1900 return result;
1901 }
1902
1903
1904 DEFUN ("ns-hide-others", Fns_hide_others, Sns_hide_others,
1905 0, 0, 0,
1906 doc: /* Hides all applications other than Emacs. */)
1907 (void)
1908 {
1909 check_window_system (NULL);
1910 [NSApp hideOtherApplications: NSApp];
1911 return Qnil;
1912 }
1913
1914 DEFUN ("ns-hide-emacs", Fns_hide_emacs, Sns_hide_emacs,
1915 1, 1, 0,
1916 doc: /* If ON is non-nil, the entire Emacs application is hidden.
1917 Otherwise if Emacs is hidden, it is unhidden.
1918 If ON is equal to `activate', Emacs is unhidden and becomes
1919 the active application. */)
1920 (Lisp_Object on)
1921 {
1922 check_window_system (NULL);
1923 if (EQ (on, intern ("activate")))
1924 {
1925 [NSApp unhide: NSApp];
1926 [NSApp activateIgnoringOtherApps: YES];
1927 }
1928 else if (NILP (on))
1929 [NSApp unhide: NSApp];
1930 else
1931 [NSApp hide: NSApp];
1932 return Qnil;
1933 }
1934
1935
1936 DEFUN ("ns-emacs-info-panel", Fns_emacs_info_panel, Sns_emacs_info_panel,
1937 0, 0, 0,
1938 doc: /* Shows the 'Info' or 'About' panel for Emacs. */)
1939 (void)
1940 {
1941 check_window_system (NULL);
1942 [NSApp orderFrontStandardAboutPanel: nil];
1943 return Qnil;
1944 }
1945
1946
1947 DEFUN ("ns-font-name", Fns_font_name, Sns_font_name, 1, 1, 0,
1948 doc: /* Determine font PostScript or family name for font NAME.
1949 NAME should be a string containing either the font name or an XLFD
1950 font descriptor. If string contains `fontset' and not
1951 `fontset-startup', it is left alone. */)
1952 (Lisp_Object name)
1953 {
1954 char *nm;
1955 CHECK_STRING (name);
1956 nm = SSDATA (name);
1957
1958 if (nm[0] != '-')
1959 return name;
1960 if (strstr (nm, "fontset") && !strstr (nm, "fontset-startup"))
1961 return name;
1962
1963 return build_string (ns_xlfd_to_fontname (SSDATA (name)));
1964 }
1965
1966
1967 DEFUN ("ns-list-colors", Fns_list_colors, Sns_list_colors, 0, 1, 0,
1968 doc: /* Return a list of all available colors.
1969 The optional argument FRAME is currently ignored. */)
1970 (Lisp_Object frame)
1971 {
1972 Lisp_Object list = Qnil;
1973 NSEnumerator *colorlists;
1974 NSColorList *clist;
1975
1976 if (!NILP (frame))
1977 {
1978 CHECK_FRAME (frame);
1979 if (! FRAME_NS_P (XFRAME (frame)))
1980 error ("non-Nextstep frame used in `ns-list-colors'");
1981 }
1982
1983 block_input ();
1984
1985 colorlists = [[NSColorList availableColorLists] objectEnumerator];
1986 while ((clist = [colorlists nextObject]))
1987 {
1988 if ([[clist name] length] < 7 ||
1989 [[clist name] rangeOfString: @"PANTONE"].location == 0)
1990 {
1991 NSEnumerator *cnames = [[clist allKeys] reverseObjectEnumerator];
1992 NSString *cname;
1993 while ((cname = [cnames nextObject]))
1994 list = Fcons (build_string ([cname UTF8String]), list);
1995 /* for (i = [[clist allKeys] count] - 1; i >= 0; i--)
1996 list = Fcons (build_string ([[[clist allKeys] objectAtIndex: i]
1997 UTF8String]), list); */
1998 }
1999 }
2000
2001 unblock_input ();
2002
2003 return list;
2004 }
2005
2006
2007 DEFUN ("ns-list-services", Fns_list_services, Sns_list_services, 0, 0, 0,
2008 doc: /* List available Nextstep services by querying NSApp. */)
2009 (void)
2010 {
2011 #ifdef NS_IMPL_COCOA
2012 /* You can't get services like this in 10.6+. */
2013 return Qnil;
2014 #else
2015 Lisp_Object ret = Qnil;
2016 NSMenu *svcs;
2017 #ifdef NS_IMPL_COCOA
2018 id delegate;
2019 #endif
2020
2021 check_window_system (NULL);
2022 svcs = [[NSMenu alloc] initWithTitle: @"Services"];
2023 [NSApp setServicesMenu: svcs];
2024 [NSApp registerServicesMenuSendTypes: ns_send_types
2025 returnTypes: ns_return_types];
2026
2027 /* On Tiger, services menu updating was made lazier (waits for user to
2028 actually click on the menu), so we have to force things along: */
2029 #ifdef NS_IMPL_COCOA
2030 delegate = [svcs delegate];
2031 if (delegate != nil)
2032 {
2033 if ([delegate respondsToSelector: @selector (menuNeedsUpdate:)])
2034 [delegate menuNeedsUpdate: svcs];
2035 if ([delegate respondsToSelector:
2036 @selector (menu:updateItem:atIndex:shouldCancel:)])
2037 {
2038 int i, len = [delegate numberOfItemsInMenu: svcs];
2039 for (i =0; i<len; i++)
2040 [svcs addItemWithTitle: @"" action: NULL keyEquivalent: @""];
2041 for (i =0; i<len; i++)
2042 if (![delegate menu: svcs
2043 updateItem: (NSMenuItem *)[svcs itemAtIndex: i]
2044 atIndex: i shouldCancel: NO])
2045 break;
2046 }
2047 }
2048 #endif
2049
2050 [svcs setAutoenablesItems: NO];
2051 #ifdef NS_IMPL_COCOA
2052 [svcs update]; /* on OS X, converts from '/' structure */
2053 #endif
2054
2055 ret = interpret_services_menu (svcs, Qnil, ret);
2056 return ret;
2057 #endif
2058 }
2059
2060
2061 DEFUN ("ns-perform-service", Fns_perform_service, Sns_perform_service,
2062 2, 2, 0,
2063 doc: /* Perform Nextstep SERVICE on SEND.
2064 SEND should be either a string or nil.
2065 The return value is the result of the service, as string, or nil if
2066 there was no result. */)
2067 (Lisp_Object service, Lisp_Object send)
2068 {
2069 id pb;
2070 NSString *svcName;
2071 char *utfStr;
2072
2073 CHECK_STRING (service);
2074 check_window_system (NULL);
2075
2076 utfStr = SSDATA (service);
2077 svcName = [NSString stringWithUTF8String: utfStr];
2078
2079 pb =[NSPasteboard pasteboardWithUniqueName];
2080 ns_string_to_pasteboard (pb, send);
2081
2082 if (NSPerformService (svcName, pb) == NO)
2083 Fsignal (Qquit, list1 (build_string ("service not available")));
2084
2085 if ([[pb types] count] == 0)
2086 return build_string ("");
2087 return ns_string_from_pasteboard (pb);
2088 }
2089
2090
2091 DEFUN ("ns-convert-utf8-nfd-to-nfc", Fns_convert_utf8_nfd_to_nfc,
2092 Sns_convert_utf8_nfd_to_nfc, 1, 1, 0,
2093 doc: /* Return an NFC string that matches the UTF-8 NFD string STR. */)
2094 (Lisp_Object str)
2095 {
2096 /* TODO: If GNUstep ever implements precomposedStringWithCanonicalMapping,
2097 remove this. */
2098 NSString *utfStr;
2099 Lisp_Object ret = Qnil;
2100 NSAutoreleasePool *pool;
2101
2102 CHECK_STRING (str);
2103 pool = [[NSAutoreleasePool alloc] init];
2104 utfStr = [NSString stringWithUTF8String: SSDATA (str)];
2105 #ifdef NS_IMPL_COCOA
2106 if (utfStr)
2107 utfStr = [utfStr precomposedStringWithCanonicalMapping];
2108 #endif
2109 if (utfStr)
2110 {
2111 const char *cstr = [utfStr UTF8String];
2112 if (cstr)
2113 ret = build_string (cstr);
2114 }
2115
2116 [pool release];
2117 if (NILP (ret))
2118 error ("Invalid UTF-8");
2119
2120 return ret;
2121 }
2122
2123
2124 #ifdef NS_IMPL_COCOA
2125
2126 /* Compile and execute the AppleScript SCRIPT and return the error
2127 status as function value. A zero is returned if compilation and
2128 execution is successful, in which case *RESULT is set to a Lisp
2129 string or a number containing the resulting script value. Otherwise,
2130 1 is returned. */
2131 static int
2132 ns_do_applescript (Lisp_Object script, Lisp_Object *result)
2133 {
2134 NSAppleEventDescriptor *desc;
2135 NSDictionary* errorDict;
2136 NSAppleEventDescriptor* returnDescriptor = NULL;
2137
2138 NSAppleScript* scriptObject =
2139 [[NSAppleScript alloc] initWithSource:
2140 [NSString stringWithUTF8String: SSDATA (script)]];
2141
2142 returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
2143 [scriptObject release];
2144 *result = Qnil;
2145
2146 if (returnDescriptor != NULL)
2147 {
2148 // successful execution
2149 if (kAENullEvent != [returnDescriptor descriptorType])
2150 {
2151 *result = Qt;
2152 // script returned an AppleScript result
2153 if ((typeUnicodeText == [returnDescriptor descriptorType]) ||
2154 #if defined (NS_IMPL_COCOA)
2155 (typeUTF16ExternalRepresentation
2156 == [returnDescriptor descriptorType]) ||
2157 #endif
2158 (typeUTF8Text == [returnDescriptor descriptorType]) ||
2159 (typeCString == [returnDescriptor descriptorType]))
2160 {
2161 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2162 if (desc)
2163 *result = build_string([[desc stringValue] UTF8String]);
2164 }
2165 else
2166 {
2167 /* use typeUTF16ExternalRepresentation? */
2168 // coerce the result to the appropriate ObjC type
2169 desc = [returnDescriptor coerceToDescriptorType: typeUTF8Text];
2170 if (desc)
2171 *result = make_number([desc int32Value]);
2172 }
2173 }
2174 }
2175 else
2176 {
2177 // no script result, return error
2178 return 1;
2179 }
2180 return 0;
2181 }
2182
2183 /* Helper function called from sendEvent to run applescript
2184 from within the main event loop. */
2185
2186 void
2187 ns_run_ascript (void)
2188 {
2189 if (! NILP (as_script))
2190 as_status = ns_do_applescript (as_script, as_result);
2191 as_script = Qnil;
2192 }
2193
2194 DEFUN ("ns-do-applescript", Fns_do_applescript, Sns_do_applescript, 1, 1, 0,
2195 doc: /* Execute AppleScript SCRIPT and return the result.
2196 If compilation and execution are successful, the resulting script value
2197 is returned as a string, a number or, in the case of other constructs, t.
2198 In case the execution fails, an error is signaled. */)
2199 (Lisp_Object script)
2200 {
2201 Lisp_Object result;
2202 int status;
2203 NSEvent *nxev;
2204 struct input_event ev;
2205
2206 CHECK_STRING (script);
2207 check_window_system (NULL);
2208
2209 block_input ();
2210
2211 as_script = script;
2212 as_result = &result;
2213
2214 /* executing apple script requires the event loop to run, otherwise
2215 errors aren't returned and executeAndReturnError hangs forever.
2216 Post an event that runs applescript and then start the event loop.
2217 The event loop is exited when the script is done. */
2218 nxev = [NSEvent otherEventWithType: NSApplicationDefined
2219 location: NSMakePoint (0, 0)
2220 modifierFlags: 0
2221 timestamp: 0
2222 windowNumber: [[NSApp mainWindow] windowNumber]
2223 context: [NSApp context]
2224 subtype: 0
2225 data1: 0
2226 data2: NSAPP_DATA2_RUNASSCRIPT];
2227
2228 [NSApp postEvent: nxev atStart: NO];
2229
2230 // If there are other events, the event loop may exit. Keep running
2231 // until the script has been handled. */
2232 ns_init_events (&ev);
2233 while (! NILP (as_script))
2234 [NSApp run];
2235 ns_finish_events ();
2236
2237 status = as_status;
2238 as_status = 0;
2239 as_result = 0;
2240 unblock_input ();
2241 if (status == 0)
2242 return result;
2243 else if (!STRINGP (result))
2244 error ("AppleScript error %d", status);
2245 else
2246 error ("%s", SSDATA (result));
2247 }
2248 #endif
2249
2250
2251
2252 /* ==========================================================================
2253
2254 Miscellaneous functions not called through hooks
2255
2256 ========================================================================== */
2257
2258 /* called from frame.c */
2259 struct ns_display_info *
2260 check_x_display_info (Lisp_Object frame)
2261 {
2262 return check_ns_display_info (frame);
2263 }
2264
2265
2266 void
2267 x_set_scroll_bar_default_width (struct frame *f)
2268 {
2269 int wid = FRAME_COLUMN_WIDTH (f);
2270 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2271 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) +
2272 wid - 1) / wid;
2273 }
2274
2275 void
2276 x_set_scroll_bar_default_height (struct frame *f)
2277 {
2278 int height = FRAME_LINE_HEIGHT (f);
2279 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = NS_SCROLL_BAR_WIDTH_DEFAULT;
2280 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) +
2281 height - 1) / height;
2282 }
2283
2284 /* terms impl this instead of x-get-resource directly */
2285 char *
2286 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
2287 {
2288 /* remove appname prefix; TODO: allow for !="Emacs" */
2289 const char *res, *toCheck = class + (!strncmp (class, "Emacs.", 6) ? 6 : 0);
2290
2291 check_window_system (NULL);
2292
2293 if (inhibit_x_resources)
2294 /* --quick was passed, so this is a no-op. */
2295 return NULL;
2296
2297 res = ns_get_defaults_value (toCheck);
2298 return (!res ? NULL :
2299 (!c_strncasecmp (res, "YES", 3) ? "true" :
2300 (!c_strncasecmp (res, "NO", 2) ? "false" : (char *) res)));
2301 }
2302
2303
2304 Lisp_Object
2305 x_get_focus_frame (struct frame *frame)
2306 {
2307 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2308 Lisp_Object nsfocus;
2309
2310 if (!dpyinfo->x_focus_frame)
2311 return Qnil;
2312
2313 XSETFRAME (nsfocus, dpyinfo->x_focus_frame);
2314 return nsfocus;
2315 }
2316
2317 /* ==========================================================================
2318
2319 Lisp definitions that, for whatever reason, we can't alias as 'ns-XXX'.
2320
2321 ========================================================================== */
2322
2323
2324 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
2325 doc: /* Internal function called by `color-defined-p', which see.
2326 (Note that the Nextstep version of this function ignores FRAME.) */)
2327 (Lisp_Object color, Lisp_Object frame)
2328 {
2329 NSColor * col;
2330 check_window_system (NULL);
2331 return ns_lisp_to_color (color, &col) ? Qnil : Qt;
2332 }
2333
2334
2335 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
2336 doc: /* Internal function called by `color-values', which see. */)
2337 (Lisp_Object color, Lisp_Object frame)
2338 {
2339 NSColor * col;
2340 EmacsCGFloat red, green, blue, alpha;
2341
2342 check_window_system (NULL);
2343 CHECK_STRING (color);
2344
2345 block_input ();
2346 if (ns_lisp_to_color (color, &col))
2347 {
2348 unblock_input ();
2349 return Qnil;
2350 }
2351
2352 [[col colorUsingDefaultColorSpace]
2353 getRed: &red green: &green blue: &blue alpha: &alpha];
2354 unblock_input ();
2355 return list3i (lrint (red * 65280), lrint (green * 65280),
2356 lrint (blue * 65280));
2357 }
2358
2359
2360 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
2361 doc: /* Internal function called by `display-color-p', which see. */)
2362 (Lisp_Object terminal)
2363 {
2364 NSWindowDepth depth;
2365 NSString *colorSpace;
2366
2367 check_ns_display_info (terminal);
2368 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2369 colorSpace = NSColorSpaceFromDepth (depth);
2370
2371 return [colorSpace isEqualToString: NSDeviceWhiteColorSpace]
2372 || [colorSpace isEqualToString: NSCalibratedWhiteColorSpace]
2373 ? Qnil : Qt;
2374 }
2375
2376
2377 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
2378 0, 1, 0,
2379 doc: /* Return t if the Nextstep display supports shades of gray.
2380 Note that color displays do support shades of gray.
2381 The optional argument TERMINAL specifies which display to ask about.
2382 TERMINAL should be a terminal object, a frame or a display name (a string).
2383 If omitted or nil, that stands for the selected frame's display. */)
2384 (Lisp_Object terminal)
2385 {
2386 NSWindowDepth depth;
2387
2388 check_ns_display_info (terminal);
2389 depth = [[[NSScreen screens] objectAtIndex:0] depth];
2390
2391 return NSBitsPerPixelFromDepth (depth) > 1 ? Qt : Qnil;
2392 }
2393
2394
2395 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
2396 0, 1, 0,
2397 doc: /* Return the width in pixels of the Nextstep display TERMINAL.
2398 The optional argument TERMINAL specifies which display to ask about.
2399 TERMINAL should be a terminal object, a frame or a display name (a string).
2400 If omitted or nil, that stands for the selected frame's display.
2401
2402 On \"multi-monitor\" setups this refers to the pixel width for all
2403 physical monitors associated with TERMINAL. To get information for
2404 each physical monitor, use `display-monitor-attributes-list'. */)
2405 (Lisp_Object terminal)
2406 {
2407 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2408
2409 return make_number (x_display_pixel_width (dpyinfo));
2410 }
2411
2412
2413 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
2414 Sx_display_pixel_height, 0, 1, 0,
2415 doc: /* Return the height in pixels of the Nextstep display TERMINAL.
2416 The optional argument TERMINAL specifies which display to ask about.
2417 TERMINAL should be a terminal object, a frame or a display name (a string).
2418 If omitted or nil, that stands for the selected frame's display.
2419
2420 On \"multi-monitor\" setups this refers to the pixel height for all
2421 physical monitors associated with TERMINAL. To get information for
2422 each physical monitor, use `display-monitor-attributes-list'. */)
2423 (Lisp_Object terminal)
2424 {
2425 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2426
2427 return make_number (x_display_pixel_height (dpyinfo));
2428 }
2429
2430 #ifdef NS_IMPL_COCOA
2431
2432 /* Returns the name for the screen that OBJ represents, or NULL.
2433 Caller must free return value.
2434 */
2435
2436 static char *
2437 ns_get_name_from_ioreg (io_object_t obj)
2438 {
2439 char *name = NULL;
2440
2441 NSDictionary *info = (NSDictionary *)
2442 IODisplayCreateInfoDictionary (obj, kIODisplayOnlyPreferredName);
2443 NSDictionary *names = [info objectForKey:
2444 [NSString stringWithUTF8String:
2445 kDisplayProductName]];
2446
2447 if ([names count] > 0)
2448 {
2449 NSString *n = [names objectForKey: [[names allKeys]
2450 objectAtIndex:0]];
2451 if (n != nil) name = xstrdup ([n UTF8String]);
2452 }
2453
2454 [info release];
2455
2456 return name;
2457 }
2458
2459 /* Returns the name for the screen that DID came from, or NULL.
2460 Caller must free return value.
2461 */
2462
2463 static char *
2464 ns_screen_name (CGDirectDisplayID did)
2465 {
2466 char *name = NULL;
2467
2468 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9
2469 mach_port_t masterPort;
2470 io_iterator_t it;
2471 io_object_t obj;
2472
2473 // CGDisplayIOServicePort is deprecated. Do it another (harder) way.
2474
2475 if (IOMasterPort (MACH_PORT_NULL, &masterPort) != kIOReturnSuccess
2476 || IOServiceGetMatchingServices (masterPort,
2477 IOServiceMatching ("IONDRVDevice"),
2478 &it) != kIOReturnSuccess)
2479 return name;
2480
2481 /* Must loop until we find a name. Many devices can have the same unit
2482 number (represents different GPU parts), but only one has a name. */
2483 while (! name && (obj = IOIteratorNext (it)))
2484 {
2485 CFMutableDictionaryRef props;
2486 const void *val;
2487
2488 if (IORegistryEntryCreateCFProperties (obj,
2489 &props,
2490 kCFAllocatorDefault,
2491 kNilOptions) == kIOReturnSuccess
2492 && props != nil
2493 && (val = CFDictionaryGetValue(props, @"IOFBDependentIndex")))
2494 {
2495 unsigned nr = [(NSNumber *)val unsignedIntegerValue];
2496 if (nr == CGDisplayUnitNumber (did))
2497 name = ns_get_name_from_ioreg (obj);
2498 }
2499
2500 CFRelease (props);
2501 IOObjectRelease (obj);
2502 }
2503
2504 IOObjectRelease (it);
2505
2506 #else
2507
2508 name = ns_get_name_from_ioreg (CGDisplayIOServicePort (did));
2509
2510 #endif
2511 return name;
2512 }
2513 #endif
2514
2515 static Lisp_Object
2516 ns_make_monitor_attribute_list (struct MonitorInfo *monitors,
2517 int n_monitors,
2518 int primary_monitor,
2519 const char *source)
2520 {
2521 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
2522 Lisp_Object frame, rest;
2523 NSArray *screens = [NSScreen screens];
2524 int i;
2525
2526 FOR_EACH_FRAME (rest, frame)
2527 {
2528 struct frame *f = XFRAME (frame);
2529
2530 if (FRAME_NS_P (f))
2531 {
2532 NSView *view = FRAME_NS_VIEW (f);
2533 NSScreen *screen = [[view window] screen];
2534 NSUInteger k;
2535
2536 i = -1;
2537 for (k = 0; i == -1 && k < [screens count]; ++k)
2538 {
2539 if ([screens objectAtIndex: k] == screen)
2540 i = (int)k;
2541 }
2542
2543 if (i > -1)
2544 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
2545 }
2546 }
2547
2548 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
2549 monitor_frames, source);
2550 }
2551
2552 DEFUN ("ns-display-monitor-attributes-list",
2553 Fns_display_monitor_attributes_list,
2554 Sns_display_monitor_attributes_list,
2555 0, 1, 0,
2556 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
2557
2558 The optional argument TERMINAL specifies which display to ask about.
2559 TERMINAL should be a terminal object, a frame or a display name (a string).
2560 If omitted or nil, that stands for the selected frame's display.
2561
2562 In addition to the standard attribute keys listed in
2563 `display-monitor-attributes-list', the following keys are contained in
2564 the attributes:
2565
2566 source -- String describing the source from which multi-monitor
2567 information is obtained, \"NS\" is always the source."
2568
2569 Internal use only, use `display-monitor-attributes-list' instead. */)
2570 (Lisp_Object terminal)
2571 {
2572 struct terminal *term = decode_live_terminal (terminal);
2573 NSArray *screens;
2574 NSUInteger i, n_monitors;
2575 struct MonitorInfo *monitors;
2576 Lisp_Object attributes_list = Qnil;
2577 CGFloat primary_display_height = 0;
2578
2579 if (term->type != output_ns)
2580 return Qnil;
2581
2582 screens = [NSScreen screens];
2583 n_monitors = [screens count];
2584 if (n_monitors == 0)
2585 return Qnil;
2586
2587 monitors = xzalloc (n_monitors * sizeof *monitors);
2588
2589 for (i = 0; i < [screens count]; ++i)
2590 {
2591 NSScreen *s = [screens objectAtIndex:i];
2592 struct MonitorInfo *m = &monitors[i];
2593 NSRect fr = [s frame];
2594 NSRect vfr = [s visibleFrame];
2595 short y, vy;
2596
2597 #ifdef NS_IMPL_COCOA
2598 NSDictionary *dict = [s deviceDescription];
2599 NSNumber *nid = [dict objectForKey:@"NSScreenNumber"];
2600 CGDirectDisplayID did = [nid unsignedIntValue];
2601 #endif
2602 if (i == 0)
2603 {
2604 primary_display_height = fr.size.height;
2605 y = (short) fr.origin.y;
2606 vy = (short) vfr.origin.y;
2607 }
2608 else
2609 {
2610 // Flip y coordinate as NS has y starting from the bottom.
2611 y = (short) (primary_display_height - fr.size.height - fr.origin.y);
2612 vy = (short) (primary_display_height -
2613 vfr.size.height - vfr.origin.y);
2614 }
2615
2616 m->geom.x = (short) fr.origin.x;
2617 m->geom.y = y;
2618 m->geom.width = (unsigned short) fr.size.width;
2619 m->geom.height = (unsigned short) fr.size.height;
2620
2621 m->work.x = (short) vfr.origin.x;
2622 // y is flipped on NS, so vy - y are pixels missing at the bottom,
2623 // and fr.size.height - vfr.size.height are pixels missing in total.
2624 // Pixels missing at top are
2625 // fr.size.height - vfr.size.height - vy + y.
2626 // work.y is then pixels missing at top + y.
2627 m->work.y = (short) (fr.size.height - vfr.size.height) - vy + y + y;
2628 m->work.width = (unsigned short) vfr.size.width;
2629 m->work.height = (unsigned short) vfr.size.height;
2630
2631 #ifdef NS_IMPL_COCOA
2632 m->name = ns_screen_name (did);
2633
2634 {
2635 CGSize mms = CGDisplayScreenSize (did);
2636 m->mm_width = (int) mms.width;
2637 m->mm_height = (int) mms.height;
2638 }
2639
2640 #else
2641 // Assume 92 dpi as x-display-mm-height/x-display-mm-width does.
2642 m->mm_width = (int) (25.4 * fr.size.width / 92.0);
2643 m->mm_height = (int) (25.4 * fr.size.height / 92.0);
2644 #endif
2645 }
2646
2647 // Primary monitor is always first for NS.
2648 attributes_list = ns_make_monitor_attribute_list (monitors, n_monitors,
2649 0, "NS");
2650
2651 free_monitors (monitors, n_monitors);
2652 return attributes_list;
2653 }
2654
2655
2656 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
2657 0, 1, 0,
2658 doc: /* Return the number of bitplanes of the Nextstep display TERMINAL.
2659 The optional argument TERMINAL specifies which display to ask about.
2660 TERMINAL should be a terminal object, a frame or a display name (a string).
2661 If omitted or nil, that stands for the selected frame's display. */)
2662 (Lisp_Object terminal)
2663 {
2664 check_ns_display_info (terminal);
2665 return make_number
2666 (NSBitsPerPixelFromDepth ([[[NSScreen screens] objectAtIndex:0] depth]));
2667 }
2668
2669
2670 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
2671 0, 1, 0,
2672 doc: /* Returns the number of color cells of the Nextstep display TERMINAL.
2673 The optional argument TERMINAL specifies which display to ask about.
2674 TERMINAL should be a terminal object, a frame or a display name (a string).
2675 If omitted or nil, that stands for the selected frame's display. */)
2676 (Lisp_Object terminal)
2677 {
2678 struct ns_display_info *dpyinfo = check_ns_display_info (terminal);
2679 /* We force 24+ bit depths to 24-bit to prevent an overflow. */
2680 return make_number (1 << min (dpyinfo->n_planes, 24));
2681 }
2682
2683
2684 /* Unused dummy def needed for compatibility. */
2685 Lisp_Object tip_frame;
2686
2687 /* TODO: move to xdisp or similar */
2688 static void
2689 compute_tip_xy (struct frame *f,
2690 Lisp_Object parms,
2691 Lisp_Object dx,
2692 Lisp_Object dy,
2693 int width,
2694 int height,
2695 int *root_x,
2696 int *root_y)
2697 {
2698 Lisp_Object left, top, right, bottom;
2699 EmacsView *view = FRAME_NS_VIEW (f);
2700 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2701 NSPoint pt;
2702
2703 /* Start with user-specified or mouse position. */
2704 left = Fcdr (Fassq (Qleft, parms));
2705 top = Fcdr (Fassq (Qtop, parms));
2706 right = Fcdr (Fassq (Qright, parms));
2707 bottom = Fcdr (Fassq (Qbottom, parms));
2708
2709 if ((!INTEGERP (left) && !INTEGERP (right))
2710 || (!INTEGERP (top) && !INTEGERP (bottom)))
2711 {
2712 pt.x = dpyinfo->last_mouse_motion_x;
2713 pt.y = dpyinfo->last_mouse_motion_y;
2714 /* Convert to screen coordinates */
2715 pt = [view convertPoint: pt toView: nil];
2716 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
2717 pt = [[view window] convertBaseToScreen: pt];
2718 #else
2719 {
2720 NSRect r = NSMakeRect (pt.x, pt.y, 0, 0);
2721 r = [[view window] convertRectToScreen: r];
2722 pt.x = r.origin.x;
2723 pt.y = r.origin.y;
2724 }
2725 #endif
2726 }
2727 else
2728 {
2729 /* Absolute coordinates. */
2730 pt.x = INTEGERP (left) ? XINT (left) : XINT (right);
2731 pt.y = (x_display_pixel_height (FRAME_DISPLAY_INFO (f))
2732 - (INTEGERP (top) ? XINT (top) : XINT (bottom))
2733 - height);
2734 }
2735
2736 /* Ensure in bounds. (Note, screen origin = lower left.) */
2737 if (INTEGERP (left) || INTEGERP (right))
2738 *root_x = pt.x;
2739 else if (pt.x + XINT (dx) <= 0)
2740 *root_x = 0; /* Can happen for negative dx */
2741 else if (pt.x + XINT (dx) + width
2742 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
2743 /* It fits to the right of the pointer. */
2744 *root_x = pt.x + XINT (dx);
2745 else if (width + XINT (dx) <= pt.x)
2746 /* It fits to the left of the pointer. */
2747 *root_x = pt.x - width - XINT (dx);
2748 else
2749 /* Put it left justified on the screen -- it ought to fit that way. */
2750 *root_x = 0;
2751
2752 if (INTEGERP (top) || INTEGERP (bottom))
2753 *root_y = pt.y;
2754 else if (pt.y - XINT (dy) - height >= 0)
2755 /* It fits below the pointer. */
2756 *root_y = pt.y - height - XINT (dy);
2757 else if (pt.y + XINT (dy) + height
2758 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
2759 /* It fits above the pointer */
2760 *root_y = pt.y + XINT (dy);
2761 else
2762 /* Put it on the top. */
2763 *root_y = x_display_pixel_height (FRAME_DISPLAY_INFO (f)) - height;
2764 }
2765
2766
2767 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
2768 doc: /* Show STRING in a \"tooltip\" window on frame FRAME.
2769 A tooltip window is a small window displaying a string.
2770
2771 This is an internal function; Lisp code should call `tooltip-show'.
2772
2773 FRAME nil or omitted means use the selected frame.
2774
2775 PARMS is an optional list of frame parameters which can be used to
2776 change the tooltip's appearance.
2777
2778 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
2779 means use the default timeout of 5 seconds.
2780
2781 If the list of frame parameters PARMS contains a `left' parameter,
2782 display the tooltip at that x-position. If the list of frame parameters
2783 PARMS contains no `left' but a `right' parameter, display the tooltip
2784 right-adjusted at that x-position. Otherwise display it at the
2785 x-position of the mouse, with offset DX added (default is 5 if DX isn't
2786 specified).
2787
2788 Likewise for the y-position: If a `top' frame parameter is specified, it
2789 determines the position of the upper edge of the tooltip window. If a
2790 `bottom' parameter but no `top' frame parameter is specified, it
2791 determines the position of the lower edge of the tooltip window.
2792 Otherwise display the tooltip window at the y-position of the mouse,
2793 with offset DY added (default is -10).
2794
2795 A tooltip's maximum size is specified by `x-max-tooltip-size'.
2796 Text larger than the specified size is clipped. */)
2797 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
2798 {
2799 int root_x, root_y;
2800 ptrdiff_t count = SPECPDL_INDEX ();
2801 struct frame *f;
2802 char *str;
2803 NSSize size;
2804
2805 specbind (Qinhibit_redisplay, Qt);
2806
2807 CHECK_STRING (string);
2808 str = SSDATA (string);
2809 f = decode_window_system_frame (frame);
2810 if (NILP (timeout))
2811 timeout = make_number (5);
2812 else
2813 CHECK_NATNUM (timeout);
2814
2815 if (NILP (dx))
2816 dx = make_number (5);
2817 else
2818 CHECK_NUMBER (dx);
2819
2820 if (NILP (dy))
2821 dy = make_number (-10);
2822 else
2823 CHECK_NUMBER (dy);
2824
2825 block_input ();
2826 if (ns_tooltip == nil)
2827 ns_tooltip = [[EmacsTooltip alloc] init];
2828 else
2829 Fx_hide_tip ();
2830
2831 [ns_tooltip setText: str];
2832 size = [ns_tooltip frame].size;
2833
2834 /* Move the tooltip window where the mouse pointer is. Resize and
2835 show it. */
2836 compute_tip_xy (f, parms, dx, dy, (int)size.width, (int)size.height,
2837 &root_x, &root_y);
2838
2839 [ns_tooltip showAtX: root_x Y: root_y for: XINT (timeout)];
2840 unblock_input ();
2841
2842 return unbind_to (count, Qnil);
2843 }
2844
2845
2846 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
2847 doc: /* Hide the current tooltip window, if there is any.
2848 Value is t if tooltip was open, nil otherwise. */)
2849 (void)
2850 {
2851 if (ns_tooltip == nil || ![ns_tooltip isActive])
2852 return Qnil;
2853 [ns_tooltip hide];
2854 return Qt;
2855 }
2856
2857 /* Return geometric attributes of FRAME. According to the value of
2858 ATTRIBUTES return the outer edges of FRAME (Qouter_edges), the inner
2859 edges of FRAME, the root window edges of frame (Qroot_edges). Any
2860 other value means to return the geometry as returned by
2861 Fx_frame_geometry. */
2862 static Lisp_Object
2863 frame_geometry (Lisp_Object frame, Lisp_Object attribute)
2864 {
2865 struct frame *f = decode_live_frame (frame);
2866 Lisp_Object fullscreen_symbol = Fframe_parameter (frame, Qfullscreen);
2867 bool fullscreen = (EQ (fullscreen_symbol, Qfullboth)
2868 || EQ (fullscreen_symbol, Qfullscreen));
2869 int border = fullscreen ? 0 : f->border_width;
2870 int title_height = fullscreen ? 0 : FRAME_NS_TITLEBAR_HEIGHT (f);
2871 int native_width = FRAME_PIXEL_WIDTH (f);
2872 int native_height = FRAME_PIXEL_HEIGHT (f);
2873 int outer_width = native_width + 2 * border;
2874 int outer_height = native_height + 2 * border + title_height;
2875 int native_left = f->left_pos + border;
2876 int native_top = f->top_pos + border + title_height;
2877 int native_right = f->left_pos + outer_width - border;
2878 int native_bottom = f->top_pos + outer_height - border;
2879 int internal_border_width = FRAME_INTERNAL_BORDER_WIDTH (f);
2880 int tool_bar_height = FRAME_TOOLBAR_HEIGHT (f);
2881 int tool_bar_width = (tool_bar_height
2882 ? outer_width - 2 * internal_border_width
2883 : 0);
2884
2885 /* Construct list. */
2886 if (EQ (attribute, Qouter_edges))
2887 return list4 (make_number (f->left_pos), make_number (f->top_pos),
2888 make_number (f->left_pos + outer_width),
2889 make_number (f->top_pos + outer_height));
2890 else if (EQ (attribute, Qnative_edges))
2891 return list4 (make_number (native_left), make_number (native_top),
2892 make_number (native_right), make_number (native_bottom));
2893 else if (EQ (attribute, Qinner_edges))
2894 return list4 (make_number (native_left + internal_border_width),
2895 make_number (native_top
2896 + tool_bar_height
2897 + internal_border_width),
2898 make_number (native_right - internal_border_width),
2899 make_number (native_bottom - internal_border_width));
2900 else
2901 return
2902 listn (CONSTYPE_HEAP, 10,
2903 Fcons (Qouter_position,
2904 Fcons (make_number (f->left_pos),
2905 make_number (f->top_pos))),
2906 Fcons (Qouter_size,
2907 Fcons (make_number (outer_width),
2908 make_number (outer_height))),
2909 Fcons (Qexternal_border_size,
2910 (fullscreen
2911 ? Fcons (make_number (0), make_number (0))
2912 : Fcons (make_number (border), make_number (border)))),
2913 Fcons (Qtitle_bar_size,
2914 Fcons (make_number (0), make_number (title_height))),
2915 Fcons (Qmenu_bar_external, Qnil),
2916 Fcons (Qmenu_bar_size, Fcons (make_number (0), make_number (0))),
2917 Fcons (Qtool_bar_external,
2918 FRAME_EXTERNAL_TOOL_BAR (f) ? Qt : Qnil),
2919 Fcons (Qtool_bar_position, FRAME_TOOL_BAR_POSITION (f)),
2920 Fcons (Qtool_bar_size,
2921 Fcons (make_number (tool_bar_width),
2922 make_number (tool_bar_height))),
2923 Fcons (Qinternal_border_width,
2924 make_number (internal_border_width)));
2925 }
2926
2927 DEFUN ("ns-frame-geometry", Fns_frame_geometry, Sns_frame_geometry, 0, 1, 0,
2928 doc: /* Return geometric attributes of FRAME.
2929 FRAME must be a live frame and defaults to the selected one. The return
2930 value is an association list of the attributes listed below. All height
2931 and width values are in pixels.
2932
2933 `outer-position' is a cons of the outer left and top edges of FRAME
2934 relative to the origin - the position (0, 0) - of FRAME's display.
2935
2936 `outer-size' is a cons of the outer width and height of FRAME. The
2937 outer size includes the title bar and the external borders as well as
2938 any menu and/or tool bar of frame.
2939
2940 `external-border-size' is a cons of the horizontal and vertical width of
2941 FRAME's external borders as supplied by the window manager.
2942
2943 `title-bar-size' is a cons of the width and height of the title bar of
2944 FRAME as supplied by the window manager. If both of them are zero,
2945 FRAME has no title bar. If only the width is zero, Emacs was not
2946 able to retrieve the width information.
2947
2948 `menu-bar-external', if non-nil, means the menu bar is external (never
2949 included in the inner edges of FRAME).
2950
2951 `menu-bar-size' is a cons of the width and height of the menu bar of
2952 FRAME.
2953
2954 `tool-bar-external', if non-nil, means the tool bar is external (never
2955 included in the inner edges of FRAME).
2956
2957 `tool-bar-position' tells on which side the tool bar on FRAME is and can
2958 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
2959 has no tool bar.
2960
2961 `tool-bar-size' is a cons of the width and height of the tool bar of
2962 FRAME.
2963
2964 `internal-border-width' is the width of the internal border of
2965 FRAME. */)
2966 (Lisp_Object frame)
2967 {
2968 return frame_geometry (frame, Qnil);
2969 }
2970
2971 DEFUN ("ns-frame-edges", Fns_frame_edges, Sns_frame_edges, 0, 2, 0,
2972 doc: /* Return edge coordinates of FRAME.
2973 FRAME must be a live frame and defaults to the selected one. The return
2974 value is a list of the form (LEFT, TOP, RIGHT, BOTTOM). All values are
2975 in pixels relative to the origin - the position (0, 0) - of FRAME's
2976 display.
2977
2978 If optional argument TYPE is the symbol `outer-edges', return the outer
2979 edges of FRAME. The outer edges comprise the decorations of the window
2980 manager (like the title bar or external borders) as well as any external
2981 menu or tool bar of FRAME. If optional argument TYPE is the symbol
2982 `native-edges' or nil, return the native edges of FRAME. The native
2983 edges exclude the decorations of the window manager and any external
2984 menu or tool bar of FRAME. If TYPE is the symbol `inner-edges', return
2985 the inner edges of FRAME. These edges exclude title bar, any borders,
2986 menu bar or tool bar of FRAME. */)
2987 (Lisp_Object frame, Lisp_Object type)
2988 {
2989 return frame_geometry (frame, ((EQ (type, Qouter_edges)
2990 || EQ (type, Qinner_edges))
2991 ? type
2992 : Qnative_edges));
2993 }
2994
2995 /* ==========================================================================
2996
2997 Class implementations
2998
2999 ========================================================================== */
3000
3001 /*
3002 Handle arrow/function/control keys and copy/paste/cut in file dialogs.
3003 Return YES if handled, NO if not.
3004 */
3005 static BOOL
3006 handlePanelKeys (NSSavePanel *panel, NSEvent *theEvent)
3007 {
3008 NSString *s;
3009 int i;
3010 BOOL ret = NO;
3011
3012 if ([theEvent type] != NSKeyDown) return NO;
3013 s = [theEvent characters];
3014
3015 for (i = 0; i < [s length]; ++i)
3016 {
3017 int ch = (int) [s characterAtIndex: i];
3018 switch (ch)
3019 {
3020 case NSHomeFunctionKey:
3021 case NSDownArrowFunctionKey:
3022 case NSUpArrowFunctionKey:
3023 case NSLeftArrowFunctionKey:
3024 case NSRightArrowFunctionKey:
3025 case NSPageUpFunctionKey:
3026 case NSPageDownFunctionKey:
3027 case NSEndFunctionKey:
3028 /* Don't send command modified keys, as those are handled in the
3029 performKeyEquivalent method of the super class.
3030 */
3031 if (! ([theEvent modifierFlags] & NSCommandKeyMask))
3032 {
3033 [panel sendEvent: theEvent];
3034 ret = YES;
3035 }
3036 break;
3037 /* As we don't have the standard key commands for
3038 copy/paste/cut/select-all in our edit menu, we must handle
3039 them here. TODO: handle Emacs key bindings for copy/cut/select-all
3040 here, paste works, because we have that in our Edit menu.
3041 I.e. refactor out code in nsterm.m, keyDown: to figure out the
3042 correct modifier.
3043 */
3044 case 'x': // Cut
3045 case 'c': // Copy
3046 case 'v': // Paste
3047 case 'a': // Select all
3048 if ([theEvent modifierFlags] & NSCommandKeyMask)
3049 {
3050 [NSApp sendAction:
3051 (ch == 'x'
3052 ? @selector(cut:)
3053 : (ch == 'c'
3054 ? @selector(copy:)
3055 : (ch == 'v'
3056 ? @selector(paste:)
3057 : @selector(selectAll:))))
3058 to:nil from:panel];
3059 ret = YES;
3060 }
3061 default:
3062 // Send all control keys, as the text field supports C-a, C-f, C-e
3063 // C-b and more.
3064 if ([theEvent modifierFlags] & NSControlKeyMask)
3065 {
3066 [panel sendEvent: theEvent];
3067 ret = YES;
3068 }
3069 break;
3070 }
3071 }
3072
3073
3074 return ret;
3075 }
3076
3077 @implementation EmacsSavePanel
3078 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3079 {
3080 BOOL ret = handlePanelKeys (self, theEvent);
3081 if (! ret)
3082 ret = [super performKeyEquivalent:theEvent];
3083 return ret;
3084 }
3085 @end
3086
3087
3088 @implementation EmacsOpenPanel
3089 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
3090 {
3091 // NSOpenPanel inherits NSSavePanel, so passing self is OK.
3092 BOOL ret = handlePanelKeys (self, theEvent);
3093 if (! ret)
3094 ret = [super performKeyEquivalent:theEvent];
3095 return ret;
3096 }
3097 @end
3098
3099
3100 @implementation EmacsFileDelegate
3101 /* --------------------------------------------------------------------------
3102 Delegate methods for Open/Save panels
3103 -------------------------------------------------------------------------- */
3104 - (BOOL)panel: (id)sender isValidFilename: (NSString *)filename
3105 {
3106 return YES;
3107 }
3108 - (BOOL)panel: (id)sender shouldShowFilename: (NSString *)filename
3109 {
3110 return YES;
3111 }
3112 - (NSString *)panel: (id)sender userEnteredFilename: (NSString *)filename
3113 confirmed: (BOOL)okFlag
3114 {
3115 return filename;
3116 }
3117 @end
3118
3119 #endif
3120
3121
3122 /* ==========================================================================
3123
3124 Lisp interface declaration
3125
3126 ========================================================================== */
3127
3128
3129 void
3130 syms_of_nsfns (void)
3131 {
3132 DEFSYM (Qfontsize, "fontsize");
3133
3134 DEFVAR_LISP ("ns-icon-type-alist", Vns_icon_type_alist,
3135 doc: /* Alist of elements (REGEXP . IMAGE) for images of icons associated to frames.
3136 If the title of a frame matches REGEXP, then IMAGE.tiff is
3137 selected as the image of the icon representing the frame when it's
3138 miniaturized. If an element is t, then Emacs tries to select an icon
3139 based on the filetype of the visited file.
3140
3141 The images have to be installed in a folder called English.lproj in the
3142 Emacs folder. You have to restart Emacs after installing new icons.
3143
3144 Example: Install an icon Gnus.tiff and execute the following code
3145
3146 (setq ns-icon-type-alist
3147 (append ns-icon-type-alist
3148 '((\"^\\\\*\\\\(Group\\\\*$\\\\|Summary \\\\|Article\\\\*$\\\\)\"
3149 . \"Gnus\"))))
3150
3151 When you miniaturize a Group, Summary or Article frame, Gnus.tiff will
3152 be used as the image of the icon representing the frame. */);
3153 Vns_icon_type_alist = list1 (Qt);
3154
3155 DEFVAR_LISP ("ns-version-string", Vns_version_string,
3156 doc: /* Toolkit version for NS Windowing. */);
3157 Vns_version_string = ns_appkit_version_str ();
3158
3159 defsubr (&Sns_read_file_name);
3160 defsubr (&Sns_get_resource);
3161 defsubr (&Sns_set_resource);
3162 defsubr (&Sxw_display_color_p); /* this and next called directly by C code */
3163 defsubr (&Sx_display_grayscale_p);
3164 defsubr (&Sns_font_name);
3165 defsubr (&Sns_list_colors);
3166 #ifdef NS_IMPL_COCOA
3167 defsubr (&Sns_do_applescript);
3168 #endif
3169 defsubr (&Sxw_color_defined_p);
3170 defsubr (&Sxw_color_values);
3171 defsubr (&Sx_server_max_request_size);
3172 defsubr (&Sx_server_vendor);
3173 defsubr (&Sx_server_version);
3174 defsubr (&Sx_display_pixel_width);
3175 defsubr (&Sx_display_pixel_height);
3176 defsubr (&Sns_display_monitor_attributes_list);
3177 defsubr (&Sns_frame_geometry);
3178 defsubr (&Sns_frame_edges);
3179 defsubr (&Sx_display_mm_width);
3180 defsubr (&Sx_display_mm_height);
3181 defsubr (&Sx_display_screens);
3182 defsubr (&Sx_display_planes);
3183 defsubr (&Sx_display_color_cells);
3184 defsubr (&Sx_display_visual_class);
3185 defsubr (&Sx_display_backing_store);
3186 defsubr (&Sx_display_save_under);
3187 defsubr (&Sx_create_frame);
3188 defsubr (&Sx_open_connection);
3189 defsubr (&Sx_close_connection);
3190 defsubr (&Sx_display_list);
3191
3192 defsubr (&Sns_hide_others);
3193 defsubr (&Sns_hide_emacs);
3194 defsubr (&Sns_emacs_info_panel);
3195 defsubr (&Sns_list_services);
3196 defsubr (&Sns_perform_service);
3197 defsubr (&Sns_convert_utf8_nfd_to_nfc);
3198 defsubr (&Sns_popup_font_panel);
3199 defsubr (&Sns_popup_color_panel);
3200
3201 defsubr (&Sx_show_tip);
3202 defsubr (&Sx_hide_tip);
3203
3204 as_status = 0;
3205 as_script = Qnil;
3206 as_result = 0;
3207 }