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