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