* nsterm.m (fd_handler:): Alloc and release a NSAutoreleasePool in
[bpt/emacs.git] / src / nsterm.m
1 /* NeXT/Open/GNUstep / MacOSX communication module.
2
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2012
4 Free Software 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 <sys/types.h>
35 #include <time.h>
36 #include <signal.h>
37 #include <unistd.h>
38 #include <setjmp.h>
39
40 #include <c-ctype.h>
41 #include <c-strcase.h>
42 #include <ftoastr.h>
43
44 #ifdef HAVE_FCNTL_H
45 #include <fcntl.h>
46 #endif
47
48 #include "lisp.h"
49 #include "blockinput.h"
50 #include "sysselect.h"
51 #include "nsterm.h"
52 #include "systime.h"
53 #include "character.h"
54 #include "fontset.h"
55 #include "composite.h"
56 #include "ccl.h"
57
58 #include "termhooks.h"
59 #include "termopts.h"
60 #include "termchar.h"
61
62 #include "window.h"
63 #include "keyboard.h"
64 #include "buffer.h"
65 #include "font.h"
66
67 /* call tracing */
68 #if 0
69 int term_trace_num = 0;
70 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
71 __FILE__, __LINE__, ++term_trace_num)
72 #else
73 #define NSTRACE(x)
74 #endif
75
76 extern NSString *NSMenuDidBeginTrackingNotification;
77
78 /* ==========================================================================
79
80 Local declarations
81
82 ========================================================================== */
83
84 /* Convert a symbol indexed with an NSxxx value to a value as defined
85 in keyboard.c (lispy_function_key). I hope this is a correct way
86 of doing things... */
87 static unsigned convert_ns_to_X_keysym[] =
88 {
89 NSHomeFunctionKey, 0x50,
90 NSLeftArrowFunctionKey, 0x51,
91 NSUpArrowFunctionKey, 0x52,
92 NSRightArrowFunctionKey, 0x53,
93 NSDownArrowFunctionKey, 0x54,
94 NSPageUpFunctionKey, 0x55,
95 NSPageDownFunctionKey, 0x56,
96 NSEndFunctionKey, 0x57,
97 NSBeginFunctionKey, 0x58,
98 NSSelectFunctionKey, 0x60,
99 NSPrintFunctionKey, 0x61,
100 NSExecuteFunctionKey, 0x62,
101 NSInsertFunctionKey, 0x63,
102 NSUndoFunctionKey, 0x65,
103 NSRedoFunctionKey, 0x66,
104 NSMenuFunctionKey, 0x67,
105 NSFindFunctionKey, 0x68,
106 NSHelpFunctionKey, 0x6A,
107 NSBreakFunctionKey, 0x6B,
108
109 NSF1FunctionKey, 0xBE,
110 NSF2FunctionKey, 0xBF,
111 NSF3FunctionKey, 0xC0,
112 NSF4FunctionKey, 0xC1,
113 NSF5FunctionKey, 0xC2,
114 NSF6FunctionKey, 0xC3,
115 NSF7FunctionKey, 0xC4,
116 NSF8FunctionKey, 0xC5,
117 NSF9FunctionKey, 0xC6,
118 NSF10FunctionKey, 0xC7,
119 NSF11FunctionKey, 0xC8,
120 NSF12FunctionKey, 0xC9,
121 NSF13FunctionKey, 0xCA,
122 NSF14FunctionKey, 0xCB,
123 NSF15FunctionKey, 0xCC,
124 NSF16FunctionKey, 0xCD,
125 NSF17FunctionKey, 0xCE,
126 NSF18FunctionKey, 0xCF,
127 NSF19FunctionKey, 0xD0,
128 NSF20FunctionKey, 0xD1,
129 NSF21FunctionKey, 0xD2,
130 NSF22FunctionKey, 0xD3,
131 NSF23FunctionKey, 0xD4,
132 NSF24FunctionKey, 0xD5,
133
134 NSBackspaceCharacter, 0x08, /* 8: Not on some KBs. */
135 NSDeleteCharacter, 0xFF, /* 127: Big 'delete' key upper right. */
136 NSDeleteFunctionKey, 0x9F, /* 63272: Del forw key off main array. */
137
138 NSTabCharacter, 0x09,
139 0x19, 0x09, /* left tab->regular since pass shift */
140 NSCarriageReturnCharacter, 0x0D,
141 NSNewlineCharacter, 0x0D,
142 NSEnterCharacter, 0x8D,
143
144 0x1B, 0x1B /* escape */
145 };
146
147 static Lisp_Object Qmodifier_value;
148 Lisp_Object Qalt, Qcontrol, Qhyper, Qmeta, Qsuper;
149 extern Lisp_Object Qcursor_color, Qcursor_type, Qns, Qleft;
150
151 static Lisp_Object QUTF8_STRING;
152
153 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
154 the maximum font size to NOT antialias. On GNUstep there is currently
155 no way to control this behavior. */
156 float ns_antialias_threshold;
157
158 /* Used to pick up AppleHighlightColor on OS X */
159 NSString *ns_selection_color;
160
161 NSArray *ns_send_types =0, *ns_return_types =0, *ns_drag_types =0;
162 NSString *ns_app_name = @"Emacs"; /* default changed later */
163
164 /* Display variables */
165 struct ns_display_info *x_display_list; /* Chain of existing displays */
166 Lisp_Object ns_display_name_list;
167 long context_menu_value = 0;
168
169 /* display update */
170 NSPoint last_mouse_motion_position;
171 static NSRect last_mouse_glyph;
172 static Time last_mouse_movement_time = 0;
173 static Lisp_Object last_mouse_motion_frame;
174 static EmacsScroller *last_mouse_scroll_bar = nil;
175 static struct frame *ns_updating_frame;
176 static NSView *focus_view = NULL;
177 static int ns_window_num = 0;
178 #ifdef NS_IMPL_GNUSTEP
179 static NSRect uRect;
180 #endif
181 static BOOL gsaved = NO;
182 BOOL ns_in_resize = NO;
183 static BOOL ns_fake_keydown = NO;
184 int ns_tmp_flags; /* FIXME */
185 struct nsfont_info *ns_tmp_font; /* FIXME */
186 static BOOL ns_menu_bar_is_hidden = NO;
187 /*static int debug_lock = 0; */
188
189 /* event loop */
190 static BOOL send_appdefined = YES;
191 static NSEvent *last_appdefined_event = 0;
192 static NSTimer *timed_entry = 0;
193 static NSTimer *scroll_repeat_entry = nil;
194 static fd_set select_readfds, select_writefds;
195 enum { SELECT_HAVE_READ = 1, SELECT_HAVE_WRITE = 2, SELECT_HAVE_TMO = 4 };
196 static int select_nfds = 0, select_valid = 0;
197 static EMACS_TIME select_timeout = { 0, 0 };
198 static int selfds[2] = { -1, -1 };
199 static pthread_mutex_t select_mutex;
200 static int apploopnr = 0;
201 static NSAutoreleasePool *outerpool;
202 static struct input_event *emacs_event = NULL;
203 static struct input_event *q_event_ptr = NULL;
204 static int n_emacs_events_pending = 0;
205 static NSMutableArray *ns_pending_files, *ns_pending_service_names,
206 *ns_pending_service_args;
207 static BOOL ns_do_open_file = NO;
208
209 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
210 #define NS_FUNCTION_KEY_MASK 0x800000
211 #define NSLeftControlKeyMask (0x000001 | NSControlKeyMask)
212 #define NSRightControlKeyMask (0x002000 | NSControlKeyMask)
213 #define NSLeftCommandKeyMask (0x000008 | NSCommandKeyMask)
214 #define NSRightCommandKeyMask (0x000010 | NSCommandKeyMask)
215 #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
216 #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
217 #define EV_MODIFIERS(e) \
218 ((([e modifierFlags] & NSHelpKeyMask) ? \
219 hyper_modifier : 0) \
220 | (!EQ (ns_right_alternate_modifier, Qleft) && \
221 (([e modifierFlags] & NSRightAlternateKeyMask) \
222 == NSRightAlternateKeyMask) ? \
223 parse_solitary_modifier (ns_right_alternate_modifier) : 0) \
224 | (([e modifierFlags] & NSAlternateKeyMask) ? \
225 parse_solitary_modifier (ns_alternate_modifier) : 0) \
226 | (([e modifierFlags] & NSShiftKeyMask) ? \
227 shift_modifier : 0) \
228 | (!EQ (ns_right_control_modifier, Qleft) && \
229 (([e modifierFlags] & NSRightControlKeyMask) \
230 == NSRightControlKeyMask) ? \
231 parse_solitary_modifier (ns_right_control_modifier) : 0) \
232 | (([e modifierFlags] & NSControlKeyMask) ? \
233 parse_solitary_modifier (ns_control_modifier) : 0) \
234 | (([e modifierFlags] & NS_FUNCTION_KEY_MASK) ? \
235 parse_solitary_modifier (ns_function_modifier) : 0) \
236 | (!EQ (ns_right_command_modifier, Qleft) && \
237 (([e modifierFlags] & NSRightCommandKeyMask) \
238 == NSRightCommandKeyMask) ? \
239 parse_solitary_modifier (ns_right_command_modifier) : 0) \
240 | (([e modifierFlags] & NSCommandKeyMask) ? \
241 parse_solitary_modifier (ns_command_modifier):0))
242
243 #define EV_UDMODIFIERS(e) \
244 ((([e type] == NSLeftMouseDown) ? down_modifier : 0) \
245 | (([e type] == NSRightMouseDown) ? down_modifier : 0) \
246 | (([e type] == NSOtherMouseDown) ? down_modifier : 0) \
247 | (([e type] == NSLeftMouseDragged) ? down_modifier : 0) \
248 | (([e type] == NSRightMouseDragged) ? down_modifier : 0) \
249 | (([e type] == NSOtherMouseDragged) ? down_modifier : 0) \
250 | (([e type] == NSLeftMouseUp) ? up_modifier : 0) \
251 | (([e type] == NSRightMouseUp) ? up_modifier : 0) \
252 | (([e type] == NSOtherMouseUp) ? up_modifier : 0))
253
254 #define EV_BUTTON(e) \
255 ((([e type] == NSLeftMouseDown) || ([e type] == NSLeftMouseUp)) ? 0 : \
256 (([e type] == NSRightMouseDown) || ([e type] == NSRightMouseUp)) ? 2 : \
257 [e buttonNumber] - 1)
258
259 /* Convert the time field to a timestamp in milliseconds. */
260 #define EV_TIMESTAMP(e) ([e timestamp] * 1000)
261
262 /* This is a piece of code which is common to all the event handling
263 methods. Maybe it should even be a function. */
264 #define EV_TRAILER(e) \
265 { \
266 XSETFRAME (emacs_event->frame_or_window, emacsframe); \
267 if (e) emacs_event->timestamp = EV_TIMESTAMP (e); \
268 if (q_event_ptr) \
269 { \
270 n_emacs_events_pending++; \
271 kbd_buffer_store_event_hold (emacs_event, q_event_ptr); \
272 } \
273 else \
274 kbd_buffer_store_event (emacs_event); \
275 EVENT_INIT (*emacs_event); \
276 ns_send_appdefined (-1); \
277 }
278
279 void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
280
281 /* TODO: get rid of need for these forward declarations */
282 static void ns_condemn_scroll_bars (struct frame *f);
283 static void ns_judge_scroll_bars (struct frame *f);
284 void x_set_frame_alpha (struct frame *f);
285
286
287 /* ==========================================================================
288
289 Utilities
290
291 ========================================================================== */
292
293
294 static Lisp_Object
295 append2 (Lisp_Object list, Lisp_Object item)
296 /* --------------------------------------------------------------------------
297 Utility to append to a list
298 -------------------------------------------------------------------------- */
299 {
300 Lisp_Object array[2];
301 array[0] = list;
302 array[1] = Fcons (item, Qnil);
303 return Fnconc (2, &array[0]);
304 }
305
306
307 const char *
308 ns_etc_directory (void)
309 /* If running as a self-contained app bundle, return as a string the
310 filename of the etc directory, if present; else nil. */
311 {
312 NSBundle *bundle = [NSBundle mainBundle];
313 NSString *resourceDir = [bundle resourcePath];
314 NSString *resourcePath;
315 NSFileManager *fileManager = [NSFileManager defaultManager];
316 BOOL isDir;
317
318 resourcePath = [resourceDir stringByAppendingPathComponent: @"etc"];
319 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
320 {
321 if (isDir) return [resourcePath UTF8String];
322 }
323 return NULL;
324 }
325
326
327 const char *
328 ns_exec_path (void)
329 /* If running as a self-contained app bundle, return as a path string
330 the filenames of the libexec and bin directories, ie libexec:bin.
331 Otherwise, return nil.
332 Normally, Emacs does not add its own bin/ directory to the PATH.
333 However, a self-contained NS build has a different layout, with
334 bin/ and libexec/ subdirectories in the directory that contains
335 Emacs.app itself.
336 We put libexec first, because init_callproc_1 uses the first
337 element to initialize exec-directory. An alternative would be
338 for init_callproc to check for invocation-directory/libexec.
339 */
340 {
341 NSBundle *bundle = [NSBundle mainBundle];
342 NSString *resourceDir = [bundle resourcePath];
343 NSString *binDir = [bundle bundlePath];
344 NSString *resourcePath, *resourcePaths;
345 NSRange range;
346 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
347 NSFileManager *fileManager = [NSFileManager defaultManager];
348 NSArray *paths;
349 NSEnumerator *pathEnum;
350 BOOL isDir;
351
352 range = [resourceDir rangeOfString: @"Contents"];
353 if (range.location != NSNotFound)
354 {
355 binDir = [binDir stringByAppendingPathComponent: @"Contents"];
356 #ifdef NS_IMPL_COCOA
357 binDir = [binDir stringByAppendingPathComponent: @"MacOS"];
358 #endif
359 }
360
361 paths = [binDir stringsByAppendingPaths:
362 [NSArray arrayWithObjects: @"libexec", @"bin", nil]];
363 pathEnum = [paths objectEnumerator];
364 resourcePaths = @"";
365
366 while ((resourcePath = [pathEnum nextObject]))
367 {
368 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
369 if (isDir)
370 {
371 if ([resourcePaths length] > 0)
372 resourcePaths
373 = [resourcePaths stringByAppendingString: pathSeparator];
374 resourcePaths
375 = [resourcePaths stringByAppendingString: resourcePath];
376 }
377 }
378 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
379
380 return NULL;
381 }
382
383
384 const char *
385 ns_load_path (void)
386 /* If running as a self-contained app bundle, return as a path string
387 the filenames of the site-lisp, lisp and leim directories.
388 Ie, site-lisp:lisp:leim. Otherwise, return nil. */
389 {
390 NSBundle *bundle = [NSBundle mainBundle];
391 NSString *resourceDir = [bundle resourcePath];
392 NSString *resourcePath, *resourcePaths;
393 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
394 NSFileManager *fileManager = [NSFileManager defaultManager];
395 BOOL isDir;
396 NSArray *paths = [resourceDir stringsByAppendingPaths:
397 [NSArray arrayWithObjects:
398 @"site-lisp", @"lisp", @"leim", nil]];
399 NSEnumerator *pathEnum = [paths objectEnumerator];
400 resourcePaths = @"";
401
402 /* Hack to skip site-lisp. */
403 if (no_site_lisp) resourcePath = [pathEnum nextObject];
404
405 while ((resourcePath = [pathEnum nextObject]))
406 {
407 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
408 if (isDir)
409 {
410 if ([resourcePaths length] > 0)
411 resourcePaths
412 = [resourcePaths stringByAppendingString: pathSeparator];
413 resourcePaths
414 = [resourcePaths stringByAppendingString: resourcePath];
415 }
416 }
417 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
418
419 return NULL;
420 }
421
422 static void
423 ns_timeout (int usecs)
424 /* --------------------------------------------------------------------------
425 Blocking timer utility used by ns_ring_bell
426 -------------------------------------------------------------------------- */
427 {
428 EMACS_TIME wakeup = add_emacs_time (current_emacs_time (),
429 make_emacs_time (0, usecs * 1000));
430
431 /* Keep waiting until past the time wakeup. */
432 while (1)
433 {
434 EMACS_TIME timeout, now = current_emacs_time ();
435 if (EMACS_TIME_LE (wakeup, now))
436 break;
437 timeout = sub_emacs_time (wakeup, now);
438
439 /* Try to wait that long--but we might wake up sooner. */
440 pselect (0, NULL, NULL, NULL, &timeout, NULL);
441 }
442 }
443
444
445 void
446 ns_release_object (void *obj)
447 /* --------------------------------------------------------------------------
448 Release an object (callable from C)
449 -------------------------------------------------------------------------- */
450 {
451 [(id)obj release];
452 }
453
454
455 void
456 ns_retain_object (void *obj)
457 /* --------------------------------------------------------------------------
458 Retain an object (callable from C)
459 -------------------------------------------------------------------------- */
460 {
461 [(id)obj retain];
462 }
463
464
465 void *
466 ns_alloc_autorelease_pool (void)
467 /* --------------------------------------------------------------------------
468 Allocate a pool for temporary objects (callable from C)
469 -------------------------------------------------------------------------- */
470 {
471 return [[NSAutoreleasePool alloc] init];
472 }
473
474
475 void
476 ns_release_autorelease_pool (void *pool)
477 /* --------------------------------------------------------------------------
478 Free a pool and temporary objects it refers to (callable from C)
479 -------------------------------------------------------------------------- */
480 {
481 ns_release_object (pool);
482 }
483
484
485
486 /* ==========================================================================
487
488 Focus (clipping) and screen update
489
490 ========================================================================== */
491
492 static NSRect
493 ns_resize_handle_rect (NSWindow *window)
494 {
495 NSRect r = [window frame];
496 r.origin.x = r.size.width - RESIZE_HANDLE_SIZE;
497 r.origin.y = 0;
498 r.size.width = r.size.height = RESIZE_HANDLE_SIZE;
499 return r;
500 }
501
502
503 //
504 // Window constraining
505 // -------------------
506 //
507 // To ensure that the windows are not placed under the menu bar, they
508 // are typically moved by the call-back constrainFrameRect. However,
509 // by overriding it, it's possible to inhibit this, leaving the window
510 // in it's original position.
511 //
512 // It's possible to hide the menu bar. However, technically, it's only
513 // possible to hide it when the application is active. To ensure that
514 // this work properly, the menu bar and window constraining are
515 // deferred until the application becomes active.
516 //
517 // Even though it's not possible to manually move a window above the
518 // top of the screen, it is allowed if it's done programmatically,
519 // when the menu is hidden. This allows the editable area to cover the
520 // full screen height.
521 //
522 // Test cases
523 // ----------
524 //
525 // Use the following extra files:
526 //
527 // init.el:
528 // ;; Hide menu and place frame slightly above the top of the screen.
529 // (setq ns-auto-hide-menu-bar t)
530 // (set-frame-position (selected-frame) 0 -20)
531 //
532 // Test 1:
533 //
534 // emacs -Q -l init.el
535 //
536 // Result: No menu bar, and the title bar should be above the screen.
537 //
538 // Test 2:
539 //
540 // emacs -Q
541 //
542 // Result: Menu bar visible, frame placed immediately below the menu.
543 //
544
545 static void
546 ns_constrain_all_frames (void)
547 {
548 Lisp_Object tail, frame;
549
550 FOR_EACH_FRAME (tail, frame)
551 {
552 struct frame *f = XFRAME (frame);
553 if (FRAME_NS_P (f))
554 {
555 NSView *view = FRAME_NS_VIEW (f);
556 /* This no-op will trigger the default window placing
557 * constraint system. */
558 f->output_data.ns->dont_constrain = 0;
559 [[view window] setFrameOrigin:[[view window] frame].origin];
560 }
561 }
562 }
563
564
565 /* True, if the menu bar should be hidden. */
566
567 static BOOL
568 ns_menu_bar_should_be_hidden (void)
569 {
570 return !NILP (ns_auto_hide_menu_bar)
571 && [NSApp respondsToSelector:@selector(setPresentationOptions:)];
572 }
573
574
575 /* Show or hide the menu bar, based on user setting. */
576
577 static void
578 ns_update_auto_hide_menu_bar (void)
579 {
580 #ifndef MAC_OS_X_VERSION_10_6
581 #define MAC_OS_X_VERSION_10_6 1060
582 #endif
583 #ifdef NS_IMPL_COCOA
584 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
585 BLOCK_INPUT;
586
587 NSTRACE (ns_update_auto_hide_menu_bar);
588
589 if (NSApp != nil
590 && [NSApp isActive]
591 && [NSApp respondsToSelector:@selector(setPresentationOptions:)])
592 {
593 // Note, "setPresentationOptions" triggers an error unless the
594 // application is active.
595 BOOL menu_bar_should_be_hidden = ns_menu_bar_should_be_hidden ();
596
597 if (menu_bar_should_be_hidden != ns_menu_bar_is_hidden)
598 {
599 NSApplicationPresentationOptions options
600 = NSApplicationPresentationAutoHideDock;
601
602 if (menu_bar_should_be_hidden)
603 options |= NSApplicationPresentationAutoHideMenuBar;
604
605 [NSApp setPresentationOptions: options];
606
607 ns_menu_bar_is_hidden = menu_bar_should_be_hidden;
608
609 if (!ns_menu_bar_is_hidden)
610 {
611 ns_constrain_all_frames ();
612 }
613 }
614 }
615
616 UNBLOCK_INPUT;
617 #endif
618 #endif
619 }
620
621
622 static void
623 ns_update_begin (struct frame *f)
624 /* --------------------------------------------------------------------------
625 Prepare for a grouped sequence of drawing calls
626 external (RIF) call; whole frame, called before update_window_begin
627 -------------------------------------------------------------------------- */
628 {
629 NSView *view = FRAME_NS_VIEW (f);
630 NSTRACE (ns_update_begin);
631
632 ns_update_auto_hide_menu_bar ();
633
634 ns_updating_frame = f;
635 [view lockFocus];
636
637 #ifdef NS_IMPL_GNUSTEP
638 uRect = NSMakeRect (0, 0, 0, 0);
639 #endif
640 }
641
642
643 static void
644 ns_update_window_begin (struct window *w)
645 /* --------------------------------------------------------------------------
646 Prepare for a grouped sequence of drawing calls
647 external (RIF) call; for one window, called after update_begin
648 -------------------------------------------------------------------------- */
649 {
650 struct frame *f = XFRAME (WINDOW_FRAME (w));
651 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
652 NSTRACE (ns_update_window_begin);
653
654 updated_window = w;
655 set_output_cursor (&w->cursor);
656
657 BLOCK_INPUT;
658
659 if (f == hlinfo->mouse_face_mouse_frame)
660 {
661 /* Don't do highlighting for mouse motion during the update. */
662 hlinfo->mouse_face_defer = 1;
663
664 /* If the frame needs to be redrawn,
665 simply forget about any prior mouse highlighting. */
666 if (FRAME_GARBAGED_P (f))
667 hlinfo->mouse_face_window = Qnil;
668
669 /* (further code for mouse faces ifdef'd out in other terms elided) */
670 }
671
672 UNBLOCK_INPUT;
673 }
674
675
676 static void
677 ns_update_window_end (struct window *w, int cursor_on_p,
678 int mouse_face_overwritten_p)
679 /* --------------------------------------------------------------------------
680 Finished a grouped sequence of drawing calls
681 external (RIF) call; for one window called before update_end
682 -------------------------------------------------------------------------- */
683 {
684 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
685
686 /* note: this fn is nearly identical in all terms */
687 if (!w->pseudo_window_p)
688 {
689 BLOCK_INPUT;
690
691 if (cursor_on_p)
692 display_and_set_cursor (w, 1,
693 output_cursor.hpos, output_cursor.vpos,
694 output_cursor.x, output_cursor.y);
695
696 if (draw_window_fringes (w, 1))
697 x_draw_vertical_border (w);
698
699 UNBLOCK_INPUT;
700 }
701
702 /* If a row with mouse-face was overwritten, arrange for
703 frame_up_to_date to redisplay the mouse highlight. */
704 if (mouse_face_overwritten_p)
705 {
706 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
707 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
708 hlinfo->mouse_face_window = Qnil;
709 }
710
711 updated_window = NULL;
712 NSTRACE (update_window_end);
713 }
714
715
716 static void
717 ns_update_end (struct frame *f)
718 /* --------------------------------------------------------------------------
719 Finished a grouped sequence of drawing calls
720 external (RIF) call; for whole frame, called after update_window_end
721 -------------------------------------------------------------------------- */
722 {
723 NSView *view = FRAME_NS_VIEW (f);
724
725 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
726 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
727
728 BLOCK_INPUT;
729
730 #ifdef NS_IMPL_GNUSTEP
731 /* trigger flush only in the rectangle we tracked as being drawn */
732 [view unlockFocusNeedsFlush: NO];
733 /*fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", uRect.origin.x, uRect.origin.y, uRect.size.width, uRect.size.height); */
734 [view lockFocusInRect: uRect];
735 #endif
736
737 [view unlockFocus];
738 [[view window] flushWindow];
739
740 UNBLOCK_INPUT;
741 ns_updating_frame = NULL;
742 NSTRACE (ns_update_end);
743 }
744
745
746 static void
747 ns_flush (struct frame *f)
748 /* --------------------------------------------------------------------------
749 external (RIF) call
750 NS impl is no-op since currently we flush in ns_update_end and elsewhere
751 -------------------------------------------------------------------------- */
752 {
753 NSTRACE (ns_flush);
754 }
755
756
757 static void
758 ns_focus (struct frame *f, NSRect *r, int n)
759 /* --------------------------------------------------------------------------
760 Internal: Focus on given frame. During small local updates this is used to
761 draw, however during large updates, ns_update_begin and ns_update_end are
762 called to wrap the whole thing, in which case these calls are stubbed out.
763 Except, on GNUstep, we accumulate the rectangle being drawn into, because
764 the back end won't do this automatically, and will just end up flushing
765 the entire window.
766 -------------------------------------------------------------------------- */
767 {
768 // NSTRACE (ns_focus);
769 #ifdef NS_IMPL_GNUSTEP
770 NSRect u;
771 if (n == 2)
772 u = NSUnionRect (r[0], r[1]);
773 else if (r)
774 u = *r;
775 #endif
776 /* static int c =0;
777 fprintf (stderr, "focus: %d", c++);
778 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
779 fprintf (stderr, "\n"); */
780
781 if (f != ns_updating_frame)
782 {
783 NSView *view = FRAME_NS_VIEW (f);
784 if (view != focus_view)
785 {
786 if (focus_view != NULL)
787 {
788 [focus_view unlockFocus];
789 [[focus_view window] flushWindow];
790 /*debug_lock--; */
791 }
792
793 if (view)
794 #ifdef NS_IMPL_GNUSTEP
795 r ? [view lockFocusInRect: u] : [view lockFocus];
796 #else
797 [view lockFocus];
798 #endif
799 focus_view = view;
800 /*if (view) debug_lock++; */
801 }
802 #ifdef NS_IMPL_GNUSTEP
803 else
804 {
805 /* more than one rect being drawn into */
806 if (view && r)
807 {
808 [view unlockFocus]; /* add prev rect to redraw list */
809 [view lockFocusInRect: u]; /* focus for draw in new rect */
810 }
811 }
812 #endif
813 }
814 #ifdef NS_IMPL_GNUSTEP
815 else
816 {
817 /* in batch mode, but in GNUstep must still track rectangles explicitly */
818 uRect = (r ? NSUnionRect (uRect, u) : [FRAME_NS_VIEW (f) visibleRect]);
819 }
820 #endif
821
822 /* clipping */
823 if (r)
824 {
825 [[NSGraphicsContext currentContext] saveGraphicsState];
826 if (n == 2)
827 NSRectClipList (r, 2);
828 else
829 NSRectClip (*r);
830 gsaved = YES;
831 }
832 }
833
834
835 static void
836 ns_unfocus (struct frame *f)
837 /* --------------------------------------------------------------------------
838 Internal: Remove focus on given frame
839 -------------------------------------------------------------------------- */
840 {
841 // NSTRACE (ns_unfocus);
842
843 if (gsaved)
844 {
845 [[NSGraphicsContext currentContext] restoreGraphicsState];
846 gsaved = NO;
847 }
848
849 if (f != ns_updating_frame)
850 {
851 if (focus_view != NULL)
852 {
853 [focus_view unlockFocus];
854 [[focus_view window] flushWindow];
855 focus_view = NULL;
856 /*debug_lock--; */
857 }
858 }
859 }
860
861
862 static void
863 ns_clip_to_row (struct window *w, struct glyph_row *row, int area, BOOL gc)
864 /* --------------------------------------------------------------------------
865 Internal (but parallels other terms): Focus drawing on given row
866 -------------------------------------------------------------------------- */
867 {
868 struct frame *f = XFRAME (WINDOW_FRAME (w));
869 NSRect clip_rect;
870 int window_x, window_y, window_width;
871
872 window_box (w, area, &window_x, &window_y, &window_width, 0);
873
874 clip_rect.origin.x = window_x - FRAME_INTERNAL_BORDER_WIDTH (f);
875 clip_rect.origin.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
876 clip_rect.origin.y = max (clip_rect.origin.y, window_y);
877 clip_rect.size.width = window_width + 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
878 clip_rect.size.height = row->visible_height;
879
880 /* allow a full-height row at the top when requested
881 (used to draw fringe all the way through internal border area) */
882 if (gc && clip_rect.origin.y < 5)
883 {
884 clip_rect.origin.y -= FRAME_INTERNAL_BORDER_WIDTH (f);
885 clip_rect.size.height += FRAME_INTERNAL_BORDER_WIDTH (f);
886 }
887
888 /* likewise at bottom */
889 if (gc &&
890 FRAME_PIXEL_HEIGHT (f) - (clip_rect.origin.y + clip_rect.size.height) < 5)
891 clip_rect.size.height += FRAME_INTERNAL_BORDER_WIDTH (f);
892
893 ns_focus (f, &clip_rect, 1);
894 }
895
896
897 static void
898 ns_ring_bell (struct frame *f)
899 /* --------------------------------------------------------------------------
900 "Beep" routine
901 -------------------------------------------------------------------------- */
902 {
903 NSTRACE (ns_ring_bell);
904 if (visible_bell)
905 {
906 NSAutoreleasePool *pool;
907 struct frame *frame = SELECTED_FRAME ();
908 NSView *view;
909
910 BLOCK_INPUT;
911 pool = [[NSAutoreleasePool alloc] init];
912
913 view = FRAME_NS_VIEW (frame);
914 if (view != nil)
915 {
916 NSRect r, surr;
917 NSPoint dim = NSMakePoint (128, 128);
918
919 r = [view bounds];
920 r.origin.x += (r.size.width - dim.x) / 2;
921 r.origin.y += (r.size.height - dim.y) / 2;
922 r.size.width = dim.x;
923 r.size.height = dim.y;
924 surr = NSInsetRect (r, -2, -2);
925 ns_focus (frame, &surr, 1);
926 [[view window] cacheImageInRect: [view convertRect: surr toView:nil]];
927 [ns_lookup_indexed_color (NS_FACE_FOREGROUND
928 (FRAME_DEFAULT_FACE (frame)), frame) set];
929 NSRectFill (r);
930 [[view window] flushWindow];
931 ns_timeout (150000);
932 [[view window] restoreCachedImage];
933 [[view window] flushWindow];
934 ns_unfocus (frame);
935 }
936 [pool release];
937 UNBLOCK_INPUT;
938 }
939 else
940 {
941 NSBeep ();
942 }
943 }
944
945
946 static void
947 ns_reset_terminal_modes (struct terminal *terminal)
948 /* Externally called as hook */
949 {
950 NSTRACE (ns_reset_terminal_modes);
951 }
952
953
954 static void
955 ns_set_terminal_modes (struct terminal *terminal)
956 /* Externally called as hook */
957 {
958 NSTRACE (ns_set_terminal_modes);
959 }
960
961
962
963 /* ==========================================================================
964
965 Frame / window manager related functions
966
967 ========================================================================== */
968
969
970 static void
971 ns_raise_frame (struct frame *f)
972 /* --------------------------------------------------------------------------
973 Bring window to foreground and make it active
974 -------------------------------------------------------------------------- */
975 {
976 NSView *view = FRAME_NS_VIEW (f);
977 check_ns ();
978 BLOCK_INPUT;
979 FRAME_SAMPLE_VISIBILITY (f);
980 if (FRAME_VISIBLE_P (f))
981 {
982 [[view window] makeKeyAndOrderFront: NSApp];
983 }
984 UNBLOCK_INPUT;
985 }
986
987
988 static void
989 ns_lower_frame (struct frame *f)
990 /* --------------------------------------------------------------------------
991 Send window to back
992 -------------------------------------------------------------------------- */
993 {
994 NSView *view = FRAME_NS_VIEW (f);
995 check_ns ();
996 BLOCK_INPUT;
997 [[view window] orderBack: NSApp];
998 UNBLOCK_INPUT;
999 }
1000
1001
1002 static void
1003 ns_frame_raise_lower (struct frame *f, int raise)
1004 /* --------------------------------------------------------------------------
1005 External (hook)
1006 -------------------------------------------------------------------------- */
1007 {
1008 NSTRACE (ns_frame_raise_lower);
1009
1010 if (raise)
1011 ns_raise_frame (f);
1012 else
1013 ns_lower_frame (f);
1014 }
1015
1016
1017 static void
1018 ns_frame_rehighlight (struct frame *frame)
1019 /* --------------------------------------------------------------------------
1020 External (hook): called on things like window switching within frame
1021 -------------------------------------------------------------------------- */
1022 {
1023 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (frame);
1024 struct frame *old_highlight = dpyinfo->x_highlight_frame;
1025
1026 NSTRACE (ns_frame_rehighlight);
1027 if (dpyinfo->x_focus_frame)
1028 {
1029 dpyinfo->x_highlight_frame
1030 = (FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1031 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1032 : dpyinfo->x_focus_frame);
1033 if (!FRAME_LIVE_P (dpyinfo->x_highlight_frame))
1034 {
1035 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
1036 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
1037 }
1038 }
1039 else
1040 dpyinfo->x_highlight_frame = 0;
1041
1042 if (dpyinfo->x_highlight_frame &&
1043 dpyinfo->x_highlight_frame != old_highlight)
1044 {
1045 if (old_highlight)
1046 {
1047 x_update_cursor (old_highlight, 1);
1048 x_set_frame_alpha (old_highlight);
1049 }
1050 if (dpyinfo->x_highlight_frame)
1051 {
1052 x_update_cursor (dpyinfo->x_highlight_frame, 1);
1053 x_set_frame_alpha (dpyinfo->x_highlight_frame);
1054 }
1055 }
1056 }
1057
1058
1059 void
1060 x_make_frame_visible (struct frame *f)
1061 /* --------------------------------------------------------------------------
1062 External: Show the window (X11 semantics)
1063 -------------------------------------------------------------------------- */
1064 {
1065 NSTRACE (x_make_frame_visible);
1066 /* XXX: at some points in past this was not needed, as the only place that
1067 called this (frame.c:Fraise_frame ()) also called raise_lower;
1068 if this ends up the case again, comment this out again. */
1069 if (!FRAME_VISIBLE_P (f))
1070 {
1071 f->async_visible = 1;
1072 ns_raise_frame (f);
1073 }
1074 }
1075
1076
1077 void
1078 x_make_frame_invisible (struct frame *f)
1079 /* --------------------------------------------------------------------------
1080 External: Hide the window (X11 semantics)
1081 -------------------------------------------------------------------------- */
1082 {
1083 NSView * view = FRAME_NS_VIEW (f);
1084 NSTRACE (x_make_frame_invisible);
1085 check_ns ();
1086 [[view window] orderOut: NSApp];
1087 f->async_visible = 0;
1088 f->async_iconified = 0;
1089 }
1090
1091
1092 void
1093 x_iconify_frame (struct frame *f)
1094 /* --------------------------------------------------------------------------
1095 External: Iconify window
1096 -------------------------------------------------------------------------- */
1097 {
1098 NSView * view = FRAME_NS_VIEW (f);
1099 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1100 NSTRACE (x_iconify_frame);
1101 check_ns ();
1102
1103 if (dpyinfo->x_highlight_frame == f)
1104 dpyinfo->x_highlight_frame = 0;
1105
1106 if ([[view window] windowNumber] <= 0)
1107 {
1108 /* the window is still deferred. Make it very small, bring it
1109 on screen and order it out. */
1110 NSRect s = { { 100, 100}, {0, 0} };
1111 NSRect t;
1112 t = [[view window] frame];
1113 [[view window] setFrame: s display: NO];
1114 [[view window] orderBack: NSApp];
1115 [[view window] orderOut: NSApp];
1116 [[view window] setFrame: t display: NO];
1117 }
1118 [[view window] miniaturize: NSApp];
1119 }
1120
1121 /* Free X resources of frame F. */
1122
1123 void
1124 x_free_frame_resources (struct frame *f)
1125 {
1126 NSView *view = FRAME_NS_VIEW (f);
1127 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1128 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1129 NSTRACE (x_free_frame_resources);
1130 check_ns ();
1131
1132 [(EmacsView *)view setWindowClosing: YES]; /* may not have been informed */
1133
1134 BLOCK_INPUT;
1135
1136 free_frame_menubar (f);
1137
1138 if (FRAME_FACE_CACHE (f))
1139 free_frame_faces (f);
1140
1141 if (f == dpyinfo->x_focus_frame)
1142 dpyinfo->x_focus_frame = 0;
1143 if (f == dpyinfo->x_highlight_frame)
1144 dpyinfo->x_highlight_frame = 0;
1145 if (f == hlinfo->mouse_face_mouse_frame)
1146 {
1147 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1148 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1149 hlinfo->mouse_face_window = Qnil;
1150 hlinfo->mouse_face_deferred_gc = 0;
1151 hlinfo->mouse_face_mouse_frame = 0;
1152 }
1153
1154 if (f->output_data.ns->miniimage != nil)
1155 [f->output_data.ns->miniimage release];
1156
1157 [[view window] close];
1158 [view release];
1159
1160 xfree (f->output_data.ns);
1161
1162 UNBLOCK_INPUT;
1163 }
1164
1165 void
1166 x_destroy_window (struct frame *f)
1167 /* --------------------------------------------------------------------------
1168 External: Delete the window
1169 -------------------------------------------------------------------------- */
1170 {
1171 NSTRACE (x_destroy_window);
1172 check_ns ();
1173 x_free_frame_resources (f);
1174 ns_window_num--;
1175 }
1176
1177
1178 void
1179 x_set_offset (struct frame *f, int xoff, int yoff, int change_grav)
1180 /* --------------------------------------------------------------------------
1181 External: Position the window
1182 -------------------------------------------------------------------------- */
1183 {
1184 NSView *view = FRAME_NS_VIEW (f);
1185 NSArray *screens = [NSScreen screens];
1186 NSScreen *fscreen = [screens objectAtIndex: 0];
1187 NSScreen *screen = [[view window] screen];
1188
1189 NSTRACE (x_set_offset);
1190
1191 BLOCK_INPUT;
1192
1193 f->left_pos = xoff;
1194 f->top_pos = yoff;
1195
1196 if (view != nil && screen && fscreen)
1197 {
1198 f->left_pos = f->size_hint_flags & XNegative
1199 ? [screen visibleFrame].size.width + f->left_pos - FRAME_PIXEL_WIDTH (f)
1200 : f->left_pos;
1201 /* We use visibleFrame here to take menu bar into account.
1202 Ideally we should also adjust left/top with visibleFrame.origin. */
1203
1204 f->top_pos = f->size_hint_flags & YNegative
1205 ? ([screen visibleFrame].size.height + f->top_pos
1206 - FRAME_PIXEL_HEIGHT (f) - FRAME_NS_TITLEBAR_HEIGHT (f)
1207 - FRAME_TOOLBAR_HEIGHT (f))
1208 : f->top_pos;
1209 #ifdef NS_IMPL_GNUSTEP
1210 if (f->left_pos < 100)
1211 f->left_pos = 100; /* don't overlap menu */
1212 #endif
1213 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1214 menu bar. */
1215 f->output_data.ns->dont_constrain = 0;
1216 [[view window] setFrameTopLeftPoint:
1217 NSMakePoint (SCREENMAXBOUND (f->left_pos),
1218 SCREENMAXBOUND ([fscreen frame].size.height
1219 - NS_TOP_POS (f)))];
1220 f->size_hint_flags &= ~(XNegative|YNegative);
1221 }
1222
1223 UNBLOCK_INPUT;
1224 }
1225
1226
1227 void
1228 x_set_window_size (struct frame *f, int change_grav, int cols, int rows)
1229 /* --------------------------------------------------------------------------
1230 Adjust window pixel size based on given character grid size
1231 Impl is a bit more complex than other terms, need to do some
1232 internal clipping.
1233 -------------------------------------------------------------------------- */
1234 {
1235 EmacsView *view = FRAME_NS_VIEW (f);
1236 NSWindow *window = [view window];
1237 NSRect wr = [window frame];
1238 int tb = FRAME_EXTERNAL_TOOL_BAR (f);
1239 int pixelwidth, pixelheight;
1240 static int oldRows, oldCols, oldFontWidth, oldFontHeight;
1241 static int oldTB;
1242 static struct frame *oldF;
1243
1244 NSTRACE (x_set_window_size);
1245
1246 if (view == nil ||
1247 (f == oldF
1248 && rows == oldRows && cols == oldCols
1249 && oldFontWidth == FRAME_COLUMN_WIDTH (f)
1250 && oldFontHeight == FRAME_LINE_HEIGHT (f)
1251 && oldTB == tb))
1252 return;
1253
1254 /*fprintf (stderr, "\tsetWindowSize: %d x %d, font size %d x %d\n", cols, rows, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f)); */
1255
1256 BLOCK_INPUT;
1257
1258 check_frame_size (f, &rows, &cols);
1259 oldF = f;
1260 oldRows = rows;
1261 oldCols = cols;
1262 oldFontWidth = FRAME_COLUMN_WIDTH (f);
1263 oldFontHeight = FRAME_LINE_HEIGHT (f);
1264 oldTB = tb;
1265
1266 f->scroll_bar_actual_width = NS_SCROLL_BAR_WIDTH (f);
1267 compute_fringe_widths (f, 0);
1268
1269 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
1270 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
1271
1272 /* If we have a toolbar, take its height into account. */
1273 if (tb)
1274 /* NOTE: previously this would generate wrong result if toolbar not
1275 yet displayed and fixing toolbar_height=32 helped, but
1276 now (200903) seems no longer needed */
1277 FRAME_TOOLBAR_HEIGHT (f) =
1278 NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)])
1279 - FRAME_NS_TITLEBAR_HEIGHT (f);
1280 else
1281 FRAME_TOOLBAR_HEIGHT (f) = 0;
1282
1283 wr.size.width = pixelwidth + f->border_width;
1284 wr.size.height = pixelheight + FRAME_NS_TITLEBAR_HEIGHT (f)
1285 + FRAME_TOOLBAR_HEIGHT (f);
1286
1287 /* Do not try to constrain to this screen. We may have multiple
1288 screens, and want Emacs to span those. Constraining to screen
1289 prevents that, and that is not nice to the user. */
1290 if (f->output_data.ns->zooming)
1291 f->output_data.ns->zooming = 0;
1292 else
1293 wr.origin.y += FRAME_PIXEL_HEIGHT (f) - pixelheight;
1294
1295 [view setRows: rows andColumns: cols];
1296 [window setFrame: wr display: YES];
1297
1298 /*fprintf (stderr, "\tx_set_window_size %d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight); */
1299
1300 /* This is a trick to compensate for Emacs' managing the scrollbar area
1301 as a fixed number of standard character columns. Instead of leaving
1302 blank space for the extra, we chopped it off above. Now for
1303 left-hand scrollbars, we shift all rendering to the left by the
1304 difference between the real width and Emacs' imagined one. For
1305 right-hand bars, don't worry about it since the extra is never used.
1306 (Obviously doesn't work for vertically split windows tho..) */
1307 {
1308 NSPoint origin = FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f)
1309 ? NSMakePoint (FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)
1310 - NS_SCROLL_BAR_WIDTH (f), 0)
1311 : NSMakePoint (0, 0);
1312 [view setFrame: NSMakeRect (0, 0, pixelwidth, pixelheight)];
1313 [view setBoundsOrigin: origin];
1314 }
1315
1316 change_frame_size (f, rows, cols, 0, 1, 0); /* pretend, delay, safe */
1317 FRAME_PIXEL_WIDTH (f) = pixelwidth;
1318 FRAME_PIXEL_HEIGHT (f) = pixelheight;
1319 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1320
1321 mark_window_cursors_off (XWINDOW (f->root_window));
1322 cancel_mouse_face (f);
1323
1324 UNBLOCK_INPUT;
1325 }
1326
1327
1328
1329 /* ==========================================================================
1330
1331 Color management
1332
1333 ========================================================================== */
1334
1335
1336 NSColor *
1337 ns_lookup_indexed_color (unsigned long idx, struct frame *f)
1338 {
1339 struct ns_color_table *color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1340 if (idx < 1 || idx >= color_table->avail)
1341 return nil;
1342 return color_table->colors[idx];
1343 }
1344
1345
1346 unsigned long
1347 ns_index_color (NSColor *color, struct frame *f)
1348 {
1349 struct ns_color_table *color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1350 ptrdiff_t idx;
1351 ptrdiff_t i;
1352
1353 if (!color_table->colors)
1354 {
1355 color_table->size = NS_COLOR_CAPACITY;
1356 color_table->avail = 1; /* skip idx=0 as marker */
1357 color_table->colors = xmalloc (color_table->size * sizeof (NSColor *));
1358 color_table->colors[0] = nil;
1359 color_table->empty_indices = [[NSMutableSet alloc] init];
1360 }
1361
1362 /* do we already have this color ? */
1363 for (i = 1; i < color_table->avail; i++)
1364 if (color_table->colors[i] && [color_table->colors[i] isEqual: color])
1365 return i;
1366
1367 if ([color_table->empty_indices count] > 0)
1368 {
1369 NSNumber *index = [color_table->empty_indices anyObject];
1370 [color_table->empty_indices removeObject: index];
1371 idx = [index unsignedLongValue];
1372 }
1373 else
1374 {
1375 if (color_table->avail == color_table->size)
1376 color_table->colors =
1377 xpalloc (color_table->colors, &color_table->size, 1,
1378 min (ULONG_MAX, PTRDIFF_MAX), sizeof *color_table->colors);
1379 idx = color_table->avail++;
1380 }
1381
1382 color_table->colors[idx] = color;
1383 [color retain];
1384 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1385 return idx;
1386 }
1387
1388
1389 void
1390 ns_free_indexed_color (unsigned long idx, struct frame *f)
1391 {
1392 struct ns_color_table *color_table;
1393 NSColor *color;
1394 NSNumber *index;
1395
1396 if (!f)
1397 return;
1398
1399 color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1400
1401 if (idx <= 0 || idx >= color_table->size) {
1402 message1 ("ns_free_indexed_color: Color index out of range.\n");
1403 return;
1404 }
1405
1406 index = [NSNumber numberWithUnsignedInt: idx];
1407 if ([color_table->empty_indices containsObject: index]) {
1408 message1 ("ns_free_indexed_color: attempt to free already freed color.\n");
1409 return;
1410 }
1411
1412 color = color_table->colors[idx];
1413 [color release];
1414 color_table->colors[idx] = nil;
1415 [color_table->empty_indices addObject: index];
1416 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1417 }
1418
1419
1420 static int
1421 ns_get_color (const char *name, NSColor **col)
1422 /* --------------------------------------------------------------------------
1423 Parse a color name
1424 -------------------------------------------------------------------------- */
1425 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
1426 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1427 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1428 {
1429 NSColor *new = nil;
1430 static char hex[20];
1431 int scaling;
1432 float r = -1.0, g, b;
1433 NSString *nsname = [NSString stringWithUTF8String: name];
1434
1435 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1436 BLOCK_INPUT;
1437
1438 if ([nsname isEqualToString: @"ns_selection_color"])
1439 {
1440 nsname = ns_selection_color;
1441 name = [ns_selection_color UTF8String];
1442 }
1443
1444 /* First, check for some sort of numeric specification. */
1445 hex[0] = '\0';
1446
1447 if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* RGB decimal */
1448 {
1449 NSScanner *scanner = [NSScanner scannerWithString: nsname];
1450 [scanner scanFloat: &r];
1451 [scanner scanFloat: &g];
1452 [scanner scanFloat: &b];
1453 }
1454 else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
1455 scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
1456 else if (name[0] == '#') /* An old X11 format; convert to newer */
1457 {
1458 int len = (strlen(name) - 1);
1459 int start = (len % 3 == 0) ? 1 : len / 4 + 1;
1460 int i;
1461 scaling = strlen(name+start) / 3;
1462 for (i = 0; i < 3; i++)
1463 sprintf (hex + i * (scaling + 1), "%.*s/", scaling,
1464 name + start + i * scaling);
1465 hex[3 * (scaling + 1) - 1] = '\0';
1466 }
1467
1468 if (hex[0])
1469 {
1470 int rr, gg, bb;
1471 float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0);
1472 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
1473 {
1474 r = rr / fscale;
1475 g = gg / fscale;
1476 b = bb / fscale;
1477 }
1478 }
1479
1480 if (r >= 0.0)
1481 {
1482 *col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
1483 UNBLOCK_INPUT;
1484 return 0;
1485 }
1486
1487 /* Otherwise, color is expected to be from a list */
1488 {
1489 NSEnumerator *lenum, *cenum;
1490 NSString *name;
1491 NSColorList *clist;
1492
1493 #ifdef NS_IMPL_GNUSTEP
1494 /* XXX: who is wrong, the requestor or the implementation? */
1495 if ([nsname compare: @"Highlight" options: NSCaseInsensitiveSearch]
1496 == NSOrderedSame)
1497 nsname = @"highlightColor";
1498 #endif
1499
1500 lenum = [[NSColorList availableColorLists] objectEnumerator];
1501 while ( (clist = [lenum nextObject]) && new == nil)
1502 {
1503 cenum = [[clist allKeys] objectEnumerator];
1504 while ( (name = [cenum nextObject]) && new == nil )
1505 {
1506 if ([name compare: nsname
1507 options: NSCaseInsensitiveSearch] == NSOrderedSame )
1508 new = [clist colorWithKey: name];
1509 }
1510 }
1511 }
1512
1513 if (new)
1514 *col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
1515 UNBLOCK_INPUT;
1516 return new ? 0 : 1;
1517 }
1518
1519
1520 int
1521 ns_lisp_to_color (Lisp_Object color, NSColor **col)
1522 /* --------------------------------------------------------------------------
1523 Convert a Lisp string object to a NS color
1524 -------------------------------------------------------------------------- */
1525 {
1526 NSTRACE (ns_lisp_to_color);
1527 if (STRINGP (color))
1528 return ns_get_color (SSDATA (color), col);
1529 else if (SYMBOLP (color))
1530 return ns_get_color (SSDATA (SYMBOL_NAME (color)), col);
1531 return 1;
1532 }
1533
1534
1535 Lisp_Object
1536 ns_color_to_lisp (NSColor *col)
1537 /* --------------------------------------------------------------------------
1538 Convert a color to a lisp string with the RGB equivalent
1539 -------------------------------------------------------------------------- */
1540 {
1541 CGFloat red, green, blue, alpha, gray;
1542 char buf[1024];
1543 const char *str;
1544 NSTRACE (ns_color_to_lisp);
1545
1546 BLOCK_INPUT;
1547 if ([[col colorSpaceName] isEqualToString: NSNamedColorSpace])
1548
1549 if ((str =[[col colorNameComponent] UTF8String]))
1550 {
1551 UNBLOCK_INPUT;
1552 return build_string ((char *)str);
1553 }
1554
1555 [[col colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
1556 getRed: &red green: &green blue: &blue alpha: &alpha];
1557 if (red ==green && red ==blue)
1558 {
1559 [[col colorUsingColorSpaceName: NSCalibratedWhiteColorSpace]
1560 getWhite: &gray alpha: &alpha];
1561 snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
1562 lrint (gray * 0xff), lrint (gray * 0xff), lrint (gray * 0xff));
1563 UNBLOCK_INPUT;
1564 return build_string (buf);
1565 }
1566
1567 snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
1568 lrint (red*0xff), lrint (green*0xff), lrint (blue*0xff));
1569
1570 UNBLOCK_INPUT;
1571 return build_string (buf);
1572 }
1573
1574
1575 void
1576 ns_query_color(void *col, XColor *color_def, int setPixel)
1577 /* --------------------------------------------------------------------------
1578 Get ARGB values out of NSColor col and put them into color_def.
1579 If setPixel, set the pixel to a concatenated version.
1580 and set color_def pixel to the resulting index.
1581 -------------------------------------------------------------------------- */
1582 {
1583 CGFloat r, g, b, a;
1584
1585 [((NSColor *)col) getRed: &r green: &g blue: &b alpha: &a];
1586 color_def->red = r * 65535;
1587 color_def->green = g * 65535;
1588 color_def->blue = b * 65535;
1589
1590 if (setPixel == YES)
1591 color_def->pixel
1592 = ARGB_TO_ULONG((int)(a*255),
1593 (int)(r*255), (int)(g*255), (int)(b*255));
1594 }
1595
1596
1597 int
1598 ns_defined_color (struct frame *f,
1599 const char *name,
1600 XColor *color_def,
1601 int alloc,
1602 char makeIndex)
1603 /* --------------------------------------------------------------------------
1604 Return 1 if named color found, and set color_def rgb accordingly.
1605 If makeIndex and alloc are nonzero put the color in the color_table,
1606 and set color_def pixel to the resulting index.
1607 If makeIndex is zero, set color_def pixel to ARGB.
1608 Return 0 if not found
1609 -------------------------------------------------------------------------- */
1610 {
1611 NSColor *col;
1612 NSTRACE (ns_defined_color);
1613
1614 BLOCK_INPUT;
1615 if (ns_get_color (name, &col) != 0) /* Color not found */
1616 {
1617 UNBLOCK_INPUT;
1618 return 0;
1619 }
1620 if (makeIndex && alloc)
1621 color_def->pixel = ns_index_color (col, f);
1622 ns_query_color (col, color_def, !makeIndex);
1623 UNBLOCK_INPUT;
1624 return 1;
1625 }
1626
1627
1628 unsigned long
1629 ns_get_rgb_color (struct frame *f, float r, float g, float b, float a)
1630 /* --------------------------------------------------------------------------
1631 return an autoreleased RGB color
1632 -------------------------------------------------------------------------- */
1633 {
1634 /*static int c = 1; fprintf (stderr, "color request %d\n", c++); */
1635 if (r < 0.0) r = 0.0;
1636 else if (r > 1.0) r = 1.0;
1637 if (g < 0.0) g = 0.0;
1638 else if (g > 1.0) g = 1.0;
1639 if (b < 0.0) b = 0.0;
1640 else if (b > 1.0) b = 1.0;
1641 if (a < 0.0) a = 0.0;
1642 else if (a > 1.0) a = 1.0;
1643 return (unsigned long) ns_index_color(
1644 [NSColor colorWithCalibratedRed: r green: g blue: b alpha: a], f);
1645 }
1646
1647
1648 void
1649 x_set_frame_alpha (struct frame *f)
1650 /* --------------------------------------------------------------------------
1651 change the entire-frame transparency
1652 -------------------------------------------------------------------------- */
1653 {
1654 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1655 EmacsView *view = FRAME_NS_VIEW (f);
1656 double alpha = 1.0;
1657 double alpha_min = 1.0;
1658
1659 if (dpyinfo->x_highlight_frame == f)
1660 alpha = f->alpha[0];
1661 else
1662 alpha = f->alpha[1];
1663
1664 if (FLOATP (Vframe_alpha_lower_limit))
1665 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
1666 else if (INTEGERP (Vframe_alpha_lower_limit))
1667 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
1668
1669 if (alpha < 0.0)
1670 return;
1671 else if (1.0 < alpha)
1672 alpha = 1.0;
1673 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
1674 alpha = alpha_min;
1675
1676 #ifdef NS_IMPL_COCOA
1677 [[view window] setAlphaValue: alpha];
1678 #endif
1679 }
1680
1681
1682 /* ==========================================================================
1683
1684 Mouse handling
1685
1686 ========================================================================== */
1687
1688
1689 void
1690 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
1691 /* --------------------------------------------------------------------------
1692 Programmatically reposition mouse pointer in pixel coordinates
1693 -------------------------------------------------------------------------- */
1694 {
1695 NSTRACE (x_set_mouse_pixel_position);
1696 ns_raise_frame (f);
1697 #if 0
1698 /* FIXME: this does not work, and what about GNUstep? */
1699 #ifdef NS_IMPL_COCOA
1700 [FRAME_NS_VIEW (f) lockFocus];
1701 PSsetmouse ((float)pix_x, (float)pix_y);
1702 [FRAME_NS_VIEW (f) unlockFocus];
1703 #endif
1704 #endif
1705 }
1706
1707
1708 void
1709 x_set_mouse_position (struct frame *f, int h, int v)
1710 /* --------------------------------------------------------------------------
1711 Programmatically reposition mouse pointer in character coordinates
1712 -------------------------------------------------------------------------- */
1713 {
1714 int pix_x, pix_y;
1715
1716 pix_x = FRAME_COL_TO_PIXEL_X (f, h) + FRAME_COLUMN_WIDTH (f) / 2;
1717 pix_y = FRAME_LINE_TO_PIXEL_Y (f, v) + FRAME_LINE_HEIGHT (f) / 2;
1718
1719 if (pix_x < 0) pix_x = 0;
1720 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
1721
1722 if (pix_y < 0) pix_y = 0;
1723 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
1724
1725 x_set_mouse_pixel_position (f, pix_x, pix_y);
1726 }
1727
1728
1729 static int
1730 note_mouse_movement (struct frame *frame, float x, float y)
1731 /* ------------------------------------------------------------------------
1732 Called by EmacsView on mouseMovement events. Passes on
1733 to emacs mainstream code if we moved off of a rect of interest
1734 known as last_mouse_glyph.
1735 ------------------------------------------------------------------------ */
1736 {
1737 // NSTRACE (note_mouse_movement);
1738
1739 XSETFRAME (last_mouse_motion_frame, frame);
1740
1741 /* Note, this doesn't get called for enter/leave, since we don't have a
1742 position. Those are taken care of in the corresponding NSView methods. */
1743
1744 /* has movement gone beyond last rect we were tracking? */
1745 if (x < last_mouse_glyph.origin.x ||
1746 x >= (last_mouse_glyph.origin.x + last_mouse_glyph.size.width) ||
1747 y < last_mouse_glyph.origin.y ||
1748 y >= (last_mouse_glyph.origin.y + last_mouse_glyph.size.height))
1749 {
1750 ns_update_begin(frame);
1751 frame->mouse_moved = 1;
1752 note_mouse_highlight (frame, x, y);
1753 remember_mouse_glyph (frame, x, y, &last_mouse_glyph);
1754 ns_update_end(frame);
1755 return 1;
1756 }
1757
1758 return 0;
1759 }
1760
1761
1762 static void
1763 ns_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
1764 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
1765 Time *time)
1766 /* --------------------------------------------------------------------------
1767 External (hook): inform emacs about mouse position and hit parts.
1768 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1769 x & y should be position in the scrollbar (the whole bar, not the handle)
1770 and length of scrollbar respectively
1771 -------------------------------------------------------------------------- */
1772 {
1773 id view;
1774 NSPoint position;
1775 Lisp_Object frame, tail;
1776 struct frame *f;
1777 struct ns_display_info *dpyinfo;
1778
1779 NSTRACE (ns_mouse_position);
1780
1781 if (*fp == NULL)
1782 {
1783 fprintf (stderr, "Warning: ns_mouse_position () called with null *fp.\n");
1784 return;
1785 }
1786
1787 dpyinfo = FRAME_NS_DISPLAY_INFO (*fp);
1788
1789 BLOCK_INPUT;
1790
1791 if (last_mouse_scroll_bar != nil && insist == 0)
1792 {
1793 /* TODO: we do not use this path at the moment because drag events will
1794 go directly to the EmacsScroller. Leaving code in for now. */
1795 [last_mouse_scroll_bar getMouseMotionPart: (int *)part window: bar_window
1796 x: x y: y];
1797 if (time) *time = last_mouse_movement_time;
1798 last_mouse_scroll_bar = nil;
1799 }
1800 else
1801 {
1802 /* Clear the mouse-moved flag for every frame on this display. */
1803 FOR_EACH_FRAME (tail, frame)
1804 if (FRAME_NS_P (XFRAME (frame))
1805 && FRAME_NS_DISPLAY (XFRAME (frame)) == FRAME_NS_DISPLAY (*fp))
1806 XFRAME (frame)->mouse_moved = 0;
1807
1808 last_mouse_scroll_bar = nil;
1809 if (last_mouse_frame && FRAME_LIVE_P (last_mouse_frame))
1810 f = last_mouse_frame;
1811 else
1812 f = dpyinfo->x_focus_frame ? dpyinfo->x_focus_frame
1813 : SELECTED_FRAME ();
1814
1815 if (f && f->output_data.ns) /* TODO: 2nd check no longer needed? */
1816 {
1817 view = FRAME_NS_VIEW (*fp);
1818
1819 position = [[view window] mouseLocationOutsideOfEventStream];
1820 position = [view convertPoint: position fromView: nil];
1821 remember_mouse_glyph (f, position.x, position.y, &last_mouse_glyph);
1822 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1823
1824 if (bar_window) *bar_window = Qnil;
1825 if (part) *part = 0; /*scroll_bar_handle; */
1826
1827 if (x) XSETINT (*x, lrint (position.x));
1828 if (y) XSETINT (*y, lrint (position.y));
1829 if (time) *time = last_mouse_movement_time;
1830 *fp = f;
1831 }
1832 }
1833
1834 UNBLOCK_INPUT;
1835 }
1836
1837
1838 static void
1839 ns_frame_up_to_date (struct frame *f)
1840 /* --------------------------------------------------------------------------
1841 External (hook): Fix up mouse highlighting right after a full update.
1842 Some highlighting was deferred if GC was happening during
1843 note_mouse_highlight (), while other highlighting was deferred for update.
1844 -------------------------------------------------------------------------- */
1845 {
1846 NSTRACE (ns_frame_up_to_date);
1847
1848 if (FRAME_NS_P (f))
1849 {
1850 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1851 if ((hlinfo->mouse_face_deferred_gc || f ==hlinfo->mouse_face_mouse_frame)
1852 /*&& hlinfo->mouse_face_mouse_frame*/)
1853 {
1854 BLOCK_INPUT;
1855 ns_update_begin(f);
1856 if (hlinfo->mouse_face_mouse_frame)
1857 note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
1858 hlinfo->mouse_face_mouse_x,
1859 hlinfo->mouse_face_mouse_y);
1860 hlinfo->mouse_face_deferred_gc = 0;
1861 ns_update_end(f);
1862 UNBLOCK_INPUT;
1863 }
1864 }
1865 }
1866
1867
1868 static void
1869 ns_define_frame_cursor (struct frame *f, Cursor cursor)
1870 /* --------------------------------------------------------------------------
1871 External (RIF): set frame mouse pointer type.
1872 -------------------------------------------------------------------------- */
1873 {
1874 NSTRACE (ns_define_frame_cursor);
1875 if (FRAME_POINTER_TYPE (f) != cursor)
1876 {
1877 EmacsView *view = FRAME_NS_VIEW (f);
1878 FRAME_POINTER_TYPE (f) = cursor;
1879 [[view window] invalidateCursorRectsForView: view];
1880 /* Redisplay assumes this function also draws the changed frame
1881 cursor, but this function doesn't, so do it explicitly. */
1882 x_update_cursor (f, 1);
1883 }
1884 }
1885
1886
1887
1888 /* ==========================================================================
1889
1890 Keyboard handling
1891
1892 ========================================================================== */
1893
1894
1895 static unsigned
1896 ns_convert_key (unsigned code)
1897 /* --------------------------------------------------------------------------
1898 Internal call used by NSView-keyDown.
1899 -------------------------------------------------------------------------- */
1900 {
1901 const unsigned last_keysym = (sizeof (convert_ns_to_X_keysym)
1902 / sizeof (convert_ns_to_X_keysym[0]));
1903 unsigned keysym;
1904 /* An array would be faster, but less easy to read. */
1905 for (keysym = 0; keysym < last_keysym; keysym += 2)
1906 if (code == convert_ns_to_X_keysym[keysym])
1907 return 0xFF00 | convert_ns_to_X_keysym[keysym+1];
1908 return 0;
1909 /* if decide to use keyCode and Carbon table, use this line:
1910 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1911 }
1912
1913
1914 char *
1915 x_get_keysym_name (int keysym)
1916 /* --------------------------------------------------------------------------
1917 Called by keyboard.c. Not sure if the return val is important, except
1918 that it be unique.
1919 -------------------------------------------------------------------------- */
1920 {
1921 static char value[16];
1922 NSTRACE (x_get_keysym_name);
1923 sprintf (value, "%d", keysym);
1924 return value;
1925 }
1926
1927
1928
1929 /* ==========================================================================
1930
1931 Block drawing operations
1932
1933 ========================================================================== */
1934
1935
1936 static void
1937 ns_redraw_scroll_bars (struct frame *f)
1938 {
1939 int i;
1940 id view;
1941 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
1942 NSTRACE (ns_judge_scroll_bars);
1943 for (i =[subviews count]-1; i >= 0; i--)
1944 {
1945 view = [subviews objectAtIndex: i];
1946 if (![view isKindOfClass: [EmacsScroller class]]) continue;
1947 [view display];
1948 }
1949 }
1950
1951
1952 void
1953 ns_clear_frame (struct frame *f)
1954 /* --------------------------------------------------------------------------
1955 External (hook): Erase the entire frame
1956 -------------------------------------------------------------------------- */
1957 {
1958 NSView *view = FRAME_NS_VIEW (f);
1959 NSRect r;
1960
1961 NSTRACE (ns_clear_frame);
1962 if (ns_in_resize)
1963 return;
1964
1965 /* comes on initial frame because we have
1966 after-make-frame-functions = select-frame */
1967 if (!FRAME_DEFAULT_FACE (f))
1968 return;
1969
1970 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
1971
1972 output_cursor.hpos = output_cursor.vpos = 0;
1973 output_cursor.x = -1;
1974
1975 r = [view bounds];
1976
1977 BLOCK_INPUT;
1978 ns_focus (f, &r, 1);
1979 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (FRAME_DEFAULT_FACE (f)), f) set];
1980 NSRectFill (r);
1981 ns_unfocus (f);
1982
1983 #ifdef NS_IMPL_COCOA
1984 [[view window] display]; /* redraw resize handle */
1985 #endif
1986
1987 /* as of 2006/11 or so this is now needed */
1988 ns_redraw_scroll_bars (f);
1989 UNBLOCK_INPUT;
1990 }
1991
1992
1993 static void
1994 ns_clear_frame_area (struct frame *f, int x, int y, int width, int height)
1995 /* --------------------------------------------------------------------------
1996 External (RIF): Clear section of frame
1997 -------------------------------------------------------------------------- */
1998 {
1999 NSRect r = NSMakeRect (x, y, width, height);
2000 NSView *view = FRAME_NS_VIEW (f);
2001 struct face *face = FRAME_DEFAULT_FACE (f);
2002
2003 if (!view || !face)
2004 return;
2005
2006 NSTRACE (ns_clear_frame_area);
2007
2008 r = NSIntersectionRect (r, [view frame]);
2009 ns_focus (f, &r, 1);
2010 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f) set];
2011
2012 #ifdef NS_IMPL_COCOA
2013 {
2014 /* clip out the resize handle */
2015 NSWindow *window = [FRAME_NS_VIEW (f) window];
2016 NSRect ir
2017 = [view convertRect: ns_resize_handle_rect (window) fromView: nil];
2018
2019 ir = NSIntersectionRect (r, ir);
2020 if (NSIsEmptyRect (ir))
2021 {
2022 #endif
2023
2024 NSRectFill (r);
2025
2026 #ifdef NS_IMPL_COCOA
2027 }
2028 else
2029 {
2030 NSRect r1 = r, r2 = r; /* upper and lower non-intersecting */
2031 r1.size.height -= ir.size.height;
2032 r2.origin.y += r1.size.height;
2033 r2.size.width -= ir.size.width;
2034 r2.size.height = ir.size.height;
2035 NSRectFill (r1);
2036 NSRectFill (r2);
2037 }
2038 }
2039 #endif
2040
2041 ns_unfocus (f);
2042 return;
2043 }
2044
2045
2046 static void
2047 ns_scroll_run (struct window *w, struct run *run)
2048 /* --------------------------------------------------------------------------
2049 External (RIF): Insert or delete n lines at line vpos
2050 -------------------------------------------------------------------------- */
2051 {
2052 struct frame *f = XFRAME (w->frame);
2053 int x, y, width, height, from_y, to_y, bottom_y;
2054
2055 NSTRACE (ns_scroll_run);
2056
2057 /* begin copy from other terms */
2058 /* Get frame-relative bounding box of the text display area of W,
2059 without mode lines. Include in this box the left and right
2060 fringe of W. */
2061 window_box (w, -1, &x, &y, &width, &height);
2062
2063 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2064 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2065 bottom_y = y + height;
2066
2067 if (to_y < from_y)
2068 {
2069 /* Scrolling up. Make sure we don't copy part of the mode
2070 line at the bottom. */
2071 if (from_y + run->height > bottom_y)
2072 height = bottom_y - from_y;
2073 else
2074 height = run->height;
2075 }
2076 else
2077 {
2078 /* Scrolling down. Make sure we don't copy over the mode line.
2079 at the bottom. */
2080 if (to_y + run->height > bottom_y)
2081 height = bottom_y - to_y;
2082 else
2083 height = run->height;
2084 }
2085 /* end copy from other terms */
2086
2087 if (height == 0)
2088 return;
2089
2090 BLOCK_INPUT;
2091
2092 updated_window = w;
2093 x_clear_cursor (w);
2094
2095 {
2096 NSRect srcRect = NSMakeRect (x, from_y, width, height);
2097 NSRect dstRect = NSMakeRect (x, to_y, width, height);
2098 NSPoint dstOrigin = NSMakePoint (x, to_y);
2099
2100 ns_focus (f, &dstRect, 1);
2101 NSCopyBits (0, srcRect , dstOrigin);
2102 ns_unfocus (f);
2103 }
2104
2105 UNBLOCK_INPUT;
2106 }
2107
2108
2109 static void
2110 ns_after_update_window_line (struct glyph_row *desired_row)
2111 /* --------------------------------------------------------------------------
2112 External (RIF): preparatory to fringe update after text was updated
2113 -------------------------------------------------------------------------- */
2114 {
2115 struct window *w = updated_window;
2116 struct frame *f;
2117 int width, height;
2118
2119 NSTRACE (ns_after_update_window_line);
2120
2121 /* begin copy from other terms */
2122 eassert (w);
2123
2124 if (!desired_row->mode_line_p && !w->pseudo_window_p)
2125 desired_row->redraw_fringe_bitmaps_p = 1;
2126
2127 /* When a window has disappeared, make sure that no rest of
2128 full-width rows stays visible in the internal border.
2129 Under NS this is drawn inside the fringes. */
2130 if (windows_or_buffers_changed
2131 && (f = XFRAME (w->frame),
2132 width = FRAME_INTERNAL_BORDER_WIDTH (f),
2133 width != 0)
2134 && (height = desired_row->visible_height,
2135 height > 0))
2136 {
2137 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
2138
2139 /* Internal border is drawn below the tool bar. */
2140 if (WINDOWP (f->tool_bar_window)
2141 && w == XWINDOW (f->tool_bar_window))
2142 y -= width;
2143 /* end copy from other terms */
2144
2145 BLOCK_INPUT;
2146 if (!desired_row->full_width_p)
2147 {
2148 int x1 = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2149 + WINDOW_LEFT_FRINGE_WIDTH (w);
2150 int x2 = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2151 + FRAME_PIXEL_WIDTH (f) - NS_SCROLL_BAR_WIDTH (f)
2152 - WINDOW_RIGHT_FRINGE_WIDTH (w)
2153 - FRAME_INTERNAL_BORDER_WIDTH (f);
2154 ns_clear_frame_area (f, x1, y, width, height);
2155 ns_clear_frame_area (f, x2, y, width, height);
2156 }
2157 UNBLOCK_INPUT;
2158 }
2159 }
2160
2161
2162 static void
2163 ns_shift_glyphs_for_insert (struct frame *f,
2164 int x, int y, int width, int height,
2165 int shift_by)
2166 /* --------------------------------------------------------------------------
2167 External (RIF): copy an area horizontally, don't worry about clearing src
2168 -------------------------------------------------------------------------- */
2169 {
2170 NSRect srcRect = NSMakeRect (x, y, width, height);
2171 NSRect dstRect = NSMakeRect (x+shift_by, y, width, height);
2172 NSPoint dstOrigin = dstRect.origin;
2173
2174 NSTRACE (ns_shift_glyphs_for_insert);
2175
2176 ns_focus (f, &dstRect, 1);
2177 NSCopyBits (0, srcRect, dstOrigin);
2178 ns_unfocus (f);
2179 }
2180
2181
2182
2183 /* ==========================================================================
2184
2185 Character encoding and metrics
2186
2187 ========================================================================== */
2188
2189
2190 static inline void
2191 ns_compute_glyph_string_overhangs (struct glyph_string *s)
2192 /* --------------------------------------------------------------------------
2193 External (RIF); compute left/right overhang of whole string and set in s
2194 -------------------------------------------------------------------------- */
2195 {
2196 struct font *font = s->font;
2197
2198 if (s->char2b)
2199 {
2200 struct font_metrics metrics;
2201 unsigned int codes[2];
2202 codes[0] = *(s->char2b);
2203 codes[1] = *(s->char2b + s->nchars - 1);
2204
2205 font->driver->text_extents (font, codes, 2, &metrics);
2206 s->left_overhang = -metrics.lbearing;
2207 s->right_overhang
2208 = metrics.rbearing > metrics.width
2209 ? metrics.rbearing - metrics.width : 0;
2210 }
2211 else
2212 {
2213 s->left_overhang = 0;
2214 s->right_overhang = ((struct nsfont_info *)font)->ital ?
2215 FONT_HEIGHT (font) * 0.2 : 0;
2216 }
2217 }
2218
2219
2220
2221 /* ==========================================================================
2222
2223 Fringe and cursor drawing
2224
2225 ========================================================================== */
2226
2227
2228 extern int max_used_fringe_bitmap;
2229 static void
2230 ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
2231 struct draw_fringe_bitmap_params *p)
2232 /* --------------------------------------------------------------------------
2233 External (RIF); fringe-related
2234 -------------------------------------------------------------------------- */
2235 {
2236 struct frame *f = XFRAME (WINDOW_FRAME (w));
2237 struct face *face = p->face;
2238 int rowY;
2239 static EmacsImage **bimgs = NULL;
2240 static int nBimgs = 0;
2241 /* NS-specific: move internal border inside fringe */
2242 int x = p->bx < 0 ? p->x : p->bx;
2243 int wd = p->bx < 0 ? p->wd : p->nx;
2244 BOOL fringeOnVeryLeft
2245 = x - WINDOW_LEFT_SCROLL_BAR_COLS (w) * WINDOW_FRAME_COLUMN_WIDTH (w)
2246 - FRAME_INTERNAL_BORDER_WIDTH (f) < 10;
2247 BOOL fringeOnVeryRight
2248 = FRAME_PIXEL_WIDTH (f) - x - wd - FRAME_INTERNAL_BORDER_WIDTH (f)
2249 - WINDOW_RIGHT_SCROLL_BAR_COLS (w) * WINDOW_FRAME_COLUMN_WIDTH (w) < 10;
2250 int xAdjust = FRAME_INTERNAL_BORDER_WIDTH (f) *
2251 (fringeOnVeryLeft ? -1 : (fringeOnVeryRight ? 1 : 0));
2252
2253 /* grow bimgs if needed */
2254 if (nBimgs < max_used_fringe_bitmap)
2255 {
2256 bimgs = xrealloc (bimgs, max_used_fringe_bitmap * sizeof *bimgs);
2257 memset (bimgs + nBimgs, 0,
2258 (max_used_fringe_bitmap - nBimgs) * sizeof *bimgs);
2259 nBimgs = max_used_fringe_bitmap;
2260 }
2261
2262 /* Must clip because of partially visible lines. */
2263 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
2264 ns_clip_to_row (w, row, -1, YES);
2265
2266 if (p->bx >= 0 && !p->overlay_p)
2267 {
2268 int yAdjust = rowY - FRAME_INTERNAL_BORDER_WIDTH (f) < 5 ?
2269 -FRAME_INTERNAL_BORDER_WIDTH (f) : 0;
2270 int yIncr = FRAME_PIXEL_HEIGHT (f) - (p->by+yAdjust + p->ny) < 5 ?
2271 FRAME_INTERNAL_BORDER_WIDTH (f) : 0
2272 + (yAdjust ? FRAME_INTERNAL_BORDER_WIDTH (f) : 0);
2273 NSRect r = NSMakeRect (p->bx+xAdjust, p->by+yAdjust, p->nx, p->ny+yIncr);
2274 NSRectClip (r);
2275 [ns_lookup_indexed_color(face->background, f) set];
2276 NSRectFill (r);
2277 }
2278
2279 if (p->which)
2280 {
2281 NSRect r = NSMakeRect (p->x+xAdjust, p->y, p->wd, p->h);
2282 EmacsImage *img = bimgs[p->which - 1];
2283
2284 if (!img)
2285 {
2286 unsigned short *bits = p->bits + p->dh;
2287 int len = p->h;
2288 int i;
2289 unsigned char *cbits = xmalloc (len);
2290
2291 for (i =0; i<len; i++)
2292 cbits[i] = ~(bits[i] & 0xff);
2293 img = [[EmacsImage alloc] initFromXBM: cbits width: 8 height: p->h
2294 flip: NO];
2295 bimgs[p->which - 1] = img;
2296 xfree (cbits);
2297 }
2298
2299 NSRectClip (r);
2300 /* Since we composite the bitmap instead of just blitting it, we need
2301 to erase the whole background. */
2302 [ns_lookup_indexed_color(face->background, f) set];
2303 NSRectFill (r);
2304 [img setXBMColor: ns_lookup_indexed_color(face->foreground, f)];
2305 [img drawInRect: r
2306 fromRect: NSZeroRect
2307 operation: NSCompositeSourceOver
2308 fraction: 1.0
2309 respectFlipped: YES
2310 hints: nil];
2311 }
2312 ns_unfocus (f);
2313 }
2314
2315
2316 static void
2317 ns_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
2318 int x, int y, int cursor_type, int cursor_width,
2319 int on_p, int active_p)
2320 /* --------------------------------------------------------------------------
2321 External call (RIF): draw cursor.
2322 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
2323 -------------------------------------------------------------------------- */
2324 {
2325 NSRect r, s;
2326 int fx, fy, h, cursor_height;
2327 struct frame *f = WINDOW_XFRAME (w);
2328 struct glyph *phys_cursor_glyph;
2329 int overspill;
2330 struct glyph *cursor_glyph;
2331 struct face *face;
2332 NSColor *hollow_color = FRAME_BACKGROUND_COLOR (f);
2333
2334 /* If cursor is out of bounds, don't draw garbage. This can happen
2335 in mini-buffer windows when switching between echo area glyphs
2336 and mini-buffer. */
2337
2338 NSTRACE (dumpcursor);
2339
2340 if (!on_p)
2341 return;
2342
2343 w->phys_cursor_type = cursor_type;
2344 w->phys_cursor_on_p = on_p;
2345
2346 if (cursor_type == NO_CURSOR)
2347 {
2348 w->phys_cursor_width = 0;
2349 return;
2350 }
2351
2352 if ((phys_cursor_glyph = get_phys_cursor_glyph (w)) == NULL)
2353 {
2354 if (glyph_row->exact_window_width_line_p
2355 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
2356 {
2357 glyph_row->cursor_in_fringe_p = 1;
2358 draw_fringe_bitmap (w, glyph_row, 0);
2359 }
2360 return;
2361 }
2362
2363 /* We draw the cursor (with NSRectFill), then draw the glyph on top
2364 (other terminals do it the other way round). We must set
2365 w->phys_cursor_width to the cursor width. For bar cursors, that
2366 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
2367 get_phys_cursor_geometry (w, glyph_row, phys_cursor_glyph, &fx, &fy, &h);
2368
2369 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
2370 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
2371 if (cursor_type == BAR_CURSOR)
2372 {
2373 if (cursor_width < 1)
2374 cursor_width = max (FRAME_CURSOR_WIDTH (f), 1);
2375 w->phys_cursor_width = cursor_width;
2376 }
2377 /* If we have an HBAR, "cursor_width" MAY specify height. */
2378 else if (cursor_type == HBAR_CURSOR)
2379 {
2380 cursor_height = (cursor_width < 1) ? lrint (0.25 * h) : cursor_width;
2381 fy += h - cursor_height;
2382 h = cursor_height;
2383 }
2384
2385 r.origin.x = fx, r.origin.y = fy;
2386 r.size.height = h;
2387 r.size.width = w->phys_cursor_width;
2388
2389 /* FIXME: if we overwrite the internal border area, it does not get erased;
2390 fix by truncating cursor, but better would be to erase properly */
2391 overspill = r.origin.x + r.size.width -
2392 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, WINDOW_BOX_RIGHT_EDGE_X (w)
2393 - WINDOW_TOTAL_FRINGE_WIDTH (w) - FRAME_INTERNAL_BORDER_WIDTH (f));
2394 if (overspill > 0)
2395 r.size.width -= overspill;
2396
2397 /* TODO: only needed in rare cases with last-resort font in HELLO..
2398 should we do this more efficiently? */
2399 ns_clip_to_row (w, glyph_row, -1, NO); /* do ns_focus(f, &r, 1); if remove */
2400
2401
2402 face = FACE_FROM_ID (f, phys_cursor_glyph->face_id);
2403 if (face && NS_FACE_BACKGROUND (face)
2404 == ns_index_color (FRAME_CURSOR_COLOR (f), f))
2405 {
2406 [ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), f) set];
2407 hollow_color = FRAME_CURSOR_COLOR (f);
2408 }
2409 else
2410 [FRAME_CURSOR_COLOR (f) set];
2411
2412 #ifdef NS_IMPL_COCOA
2413 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2414 atomic. Cleaner ways of doing this should be investigated.
2415 One way would be to set a global variable DRAWING_CURSOR
2416 when making the call to draw_phys..(), don't focus in that
2417 case, then move the ns_unfocus() here after that call. */
2418 NSDisableScreenUpdates ();
2419 #endif
2420
2421 switch (cursor_type)
2422 {
2423 case NO_CURSOR:
2424 break;
2425 case FILLED_BOX_CURSOR:
2426 NSRectFill (r);
2427 break;
2428 case HOLLOW_BOX_CURSOR:
2429 NSRectFill (r);
2430 [hollow_color set];
2431 NSRectFill (NSInsetRect (r, 1, 1));
2432 [FRAME_CURSOR_COLOR (f) set];
2433 break;
2434 case HBAR_CURSOR:
2435 NSRectFill (r);
2436 break;
2437 case BAR_CURSOR:
2438 s = r;
2439 /* If the character under cursor is R2L, draw the bar cursor
2440 on the right of its glyph, rather than on the left. */
2441 cursor_glyph = get_phys_cursor_glyph (w);
2442 if ((cursor_glyph->resolved_level & 1) != 0)
2443 s.origin.x += cursor_glyph->pixel_width - s.size.width;
2444
2445 NSRectFill (s);
2446 break;
2447 }
2448 ns_unfocus (f);
2449
2450 /* draw the character under the cursor */
2451 if (cursor_type != NO_CURSOR)
2452 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
2453
2454 #ifdef NS_IMPL_COCOA
2455 NSEnableScreenUpdates ();
2456 #endif
2457
2458 }
2459
2460
2461 static void
2462 ns_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
2463 /* --------------------------------------------------------------------------
2464 External (RIF): Draw a vertical line.
2465 -------------------------------------------------------------------------- */
2466 {
2467 struct frame *f = XFRAME (WINDOW_FRAME (w));
2468 struct face *face;
2469 NSRect r = NSMakeRect (x, y0, 1, y1-y0);
2470
2471 NSTRACE (ns_draw_vertical_window_border);
2472
2473 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
2474 if (face)
2475 [ns_lookup_indexed_color(face->foreground, f) set];
2476
2477 ns_focus (f, &r, 1);
2478 NSRectFill(r);
2479 ns_unfocus (f);
2480 }
2481
2482
2483 void
2484 show_hourglass (struct atimer *timer)
2485 {
2486 if (hourglass_shown_p)
2487 return;
2488
2489 BLOCK_INPUT;
2490
2491 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2492
2493 hourglass_shown_p = 1;
2494 UNBLOCK_INPUT;
2495 }
2496
2497
2498 void
2499 hide_hourglass (void)
2500 {
2501 if (!hourglass_shown_p)
2502 return;
2503
2504 BLOCK_INPUT;
2505
2506 /* TODO: remove NSProgressIndicator from all frames */
2507
2508 hourglass_shown_p = 0;
2509 UNBLOCK_INPUT;
2510 }
2511
2512
2513
2514 /* ==========================================================================
2515
2516 Glyph drawing operations
2517
2518 ========================================================================== */
2519
2520
2521 static inline NSRect
2522 ns_fix_rect_ibw (NSRect r, int fibw, int frame_pixel_width)
2523 /* --------------------------------------------------------------------------
2524 Under NS we draw internal borders inside fringes, and want full-width
2525 rendering to go all the way to edge. This function makes that correction.
2526 -------------------------------------------------------------------------- */
2527 {
2528 if (r.origin.y <= fibw+1)
2529 {
2530 r.size.height += r.origin.y;
2531 r.origin.y = 0;
2532 }
2533 if (r.origin.x <= fibw+1)
2534 {
2535 r.size.width += r.origin.x;
2536 r.origin.x = 0;
2537 }
2538 if (frame_pixel_width - (r.origin.x+r.size.width) <= fibw+1)
2539 r.size.width += fibw;
2540
2541 return r;
2542 }
2543
2544
2545 static int
2546 ns_get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2547 /* --------------------------------------------------------------------------
2548 Wrapper utility to account for internal border width on full-width lines,
2549 and allow top full-width rows to hit the frame top. nr should be pointer
2550 to two successive NSRects. Number of rects actually used is returned.
2551 -------------------------------------------------------------------------- */
2552 {
2553 int n = get_glyph_string_clip_rects (s, nr, 2);
2554 if (s->row->full_width_p)
2555 {
2556 *nr = ns_fix_rect_ibw (*nr, FRAME_INTERNAL_BORDER_WIDTH (s->f),
2557 FRAME_PIXEL_WIDTH (s->f));
2558 if (n == 2)
2559 *nr = ns_fix_rect_ibw (*(nr+1), FRAME_INTERNAL_BORDER_WIDTH (s->f),
2560 FRAME_PIXEL_WIDTH (s->f));
2561 }
2562 return n;
2563 }
2564
2565 /* --------------------------------------------------------------------
2566 Draw a wavy line under glyph string s. The wave fills wave_height
2567 pixels from y.
2568
2569 x wave_length = 3
2570 --
2571 y * * * * *
2572 |* * * * * * * * *
2573 wave_height = 3 | * * * *
2574 --------------------------------------------------------------------- */
2575
2576 static void
2577 ns_draw_underwave (struct glyph_string *s, CGFloat width, CGFloat x)
2578 {
2579 int wave_height = 3, wave_length = 3;
2580 int y, dx, dy, odd, xmax;
2581 NSPoint a, b;
2582 NSRect waveClip;
2583
2584 dx = wave_length;
2585 dy = wave_height - 1;
2586 y = s->ybase + 1;
2587 xmax = x + width;
2588
2589 /* Find and set clipping rectangle */
2590 waveClip = NSMakeRect (x, y, width, wave_height);
2591 [[NSGraphicsContext currentContext] saveGraphicsState];
2592 NSRectClip (waveClip);
2593
2594 /* Draw the waves */
2595 a.x = x - ((int)(x) % dx);
2596 b.x = a.x + dx;
2597 odd = (int)(a.x/dx) % 2;
2598 a.y = b.y = y;
2599
2600 if (odd)
2601 a.y += dy;
2602 else
2603 b.y += dy;
2604
2605 while (a.x <= xmax)
2606 {
2607 [NSBezierPath strokeLineFromPoint:a toPoint:b];
2608 a.x = b.x, a.y = b.y;
2609 b.x += dx, b.y = y + odd*dy;
2610 odd = !odd;
2611 }
2612
2613 /* Restore previous clipping rectangle(s) */
2614 [[NSGraphicsContext currentContext] restoreGraphicsState];
2615 }
2616
2617
2618
2619 void
2620 ns_draw_text_decoration (struct glyph_string *s, struct face *face,
2621 NSColor *defaultCol, CGFloat width, CGFloat x)
2622 /* --------------------------------------------------------------------------
2623 Draw underline, overline, and strike-through on glyph string s.
2624 -------------------------------------------------------------------------- */
2625 {
2626 if (s->for_overlaps)
2627 return;
2628
2629 /* Do underline. */
2630 if (face->underline_p)
2631 {
2632 if (s->face->underline_type == FACE_UNDER_WAVE)
2633 {
2634 if (face->underline_defaulted_p)
2635 [defaultCol set];
2636 else
2637 [ns_lookup_indexed_color (face->underline_color, s->f) set];
2638
2639 ns_draw_underwave (s, width, x);
2640 }
2641 else if (s->face->underline_type == FACE_UNDER_LINE)
2642 {
2643
2644 NSRect r;
2645 unsigned long thickness, position;
2646
2647 /* If the prev was underlined, match its appearance. */
2648 if (s->prev && s->prev->face->underline_p
2649 && s->prev->underline_thickness > 0)
2650 {
2651 thickness = s->prev->underline_thickness;
2652 position = s->prev->underline_position;
2653 }
2654 else
2655 {
2656 struct font *font;
2657 unsigned long descent;
2658
2659 font=s->font;
2660 descent = s->y + s->height - s->ybase;
2661
2662 /* Use underline thickness of font, defaulting to 1. */
2663 thickness = (font && font->underline_thickness > 0)
2664 ? font->underline_thickness : 1;
2665
2666 /* Determine the offset of underlining from the baseline. */
2667 if (x_underline_at_descent_line)
2668 position = descent - thickness;
2669 else if (x_use_underline_position_properties
2670 && font && font->underline_position >= 0)
2671 position = font->underline_position;
2672 else if (font)
2673 position = lround (font->descent / 2);
2674 else
2675 position = underline_minimum_offset;
2676
2677 position = max (position, underline_minimum_offset);
2678
2679 /* Ensure underlining is not cropped. */
2680 if (descent <= position)
2681 {
2682 position = descent - 1;
2683 thickness = 1;
2684 }
2685 else if (descent < position + thickness)
2686 thickness = 1;
2687 }
2688
2689 s->underline_thickness = thickness;
2690 s->underline_position = position;
2691
2692 r = NSMakeRect (x, s->ybase + position, width, thickness);
2693
2694 if (face->underline_defaulted_p)
2695 [defaultCol set];
2696 else
2697 [ns_lookup_indexed_color (face->underline_color, s->f) set];
2698 NSRectFill (r);
2699 }
2700 }
2701 /* Do overline. We follow other terms in using a thickness of 1
2702 and ignoring overline_margin. */
2703 if (face->overline_p)
2704 {
2705 NSRect r;
2706 r = NSMakeRect (x, s->y, width, 1);
2707
2708 if (face->overline_color_defaulted_p)
2709 [defaultCol set];
2710 else
2711 [ns_lookup_indexed_color (face->overline_color, s->f) set];
2712 NSRectFill (r);
2713 }
2714
2715 /* Do strike-through. We follow other terms for thickness and
2716 vertical position.*/
2717 if (face->strike_through_p)
2718 {
2719 NSRect r;
2720 unsigned long dy;
2721
2722 dy = lrint ((s->height - 1) / 2);
2723 r = NSMakeRect (x, s->y + dy, width, 1);
2724
2725 if (face->strike_through_color_defaulted_p)
2726 [defaultCol set];
2727 else
2728 [ns_lookup_indexed_color (face->strike_through_color, s->f) set];
2729 NSRectFill (r);
2730 }
2731 }
2732
2733 static void
2734 ns_draw_box (NSRect r, float thickness, NSColor *col, char left_p, char right_p)
2735 /* --------------------------------------------------------------------------
2736 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2737 Note we can't just use an NSDrawRect command, because of the possibility
2738 of some sides not being drawn, and because the rect will be filled.
2739 -------------------------------------------------------------------------- */
2740 {
2741 NSRect s = r;
2742 [col set];
2743
2744 /* top, bottom */
2745 s.size.height = thickness;
2746 NSRectFill (s);
2747 s.origin.y += r.size.height - thickness;
2748 NSRectFill (s);
2749
2750 s.size.height = r.size.height;
2751 s.origin.y = r.origin.y;
2752
2753 /* left, right (optional) */
2754 s.size.width = thickness;
2755 if (left_p)
2756 NSRectFill (s);
2757 if (right_p)
2758 {
2759 s.origin.x += r.size.width - thickness;
2760 NSRectFill (s);
2761 }
2762 }
2763
2764
2765 static void
2766 ns_draw_relief (NSRect r, int thickness, char raised_p,
2767 char top_p, char bottom_p, char left_p, char right_p,
2768 struct glyph_string *s)
2769 /* --------------------------------------------------------------------------
2770 Draw a relief rect inside r, optionally leaving some sides open.
2771 Note we can't just use an NSDrawBezel command, because of the possibility
2772 of some sides not being drawn, and because the rect will be filled.
2773 -------------------------------------------------------------------------- */
2774 {
2775 static NSColor *baseCol = nil, *lightCol = nil, *darkCol = nil;
2776 NSColor *newBaseCol = nil;
2777 NSRect sr = r;
2778
2779 NSTRACE (ns_draw_relief);
2780
2781 /* set up colors */
2782
2783 if (s->face->use_box_color_for_shadows_p)
2784 {
2785 newBaseCol = ns_lookup_indexed_color (s->face->box_color, s->f);
2786 }
2787 /* else if (s->first_glyph->type == IMAGE_GLYPH
2788 && s->img->pixmap
2789 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2790 {
2791 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2792 } */
2793 else
2794 {
2795 newBaseCol = ns_lookup_indexed_color (s->face->background, s->f);
2796 }
2797
2798 if (newBaseCol == nil)
2799 newBaseCol = [NSColor grayColor];
2800
2801 if (newBaseCol != baseCol) /* TODO: better check */
2802 {
2803 [baseCol release];
2804 baseCol = [newBaseCol retain];
2805 [lightCol release];
2806 lightCol = [[baseCol highlightWithLevel: 0.2] retain];
2807 [darkCol release];
2808 darkCol = [[baseCol shadowWithLevel: 0.3] retain];
2809 }
2810
2811 [(raised_p ? lightCol : darkCol) set];
2812
2813 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2814
2815 /* top */
2816 sr.size.height = thickness;
2817 if (top_p) NSRectFill (sr);
2818
2819 /* left */
2820 sr.size.height = r.size.height;
2821 sr.size.width = thickness;
2822 if (left_p) NSRectFill (sr);
2823
2824 [(raised_p ? darkCol : lightCol) set];
2825
2826 /* bottom */
2827 sr.size.width = r.size.width;
2828 sr.size.height = thickness;
2829 sr.origin.y += r.size.height - thickness;
2830 if (bottom_p) NSRectFill (sr);
2831
2832 /* right */
2833 sr.size.height = r.size.height;
2834 sr.origin.y = r.origin.y;
2835 sr.size.width = thickness;
2836 sr.origin.x += r.size.width - thickness;
2837 if (right_p) NSRectFill (sr);
2838 }
2839
2840
2841 static void
2842 ns_dumpglyphs_box_or_relief (struct glyph_string *s)
2843 /* --------------------------------------------------------------------------
2844 Function modeled after x_draw_glyph_string_box ().
2845 Sets up parameters for drawing.
2846 -------------------------------------------------------------------------- */
2847 {
2848 int right_x, last_x;
2849 char left_p, right_p;
2850 struct glyph *last_glyph;
2851 NSRect r;
2852 int thickness;
2853 struct face *face;
2854
2855 if (s->hl == DRAW_MOUSE_FACE)
2856 {
2857 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
2858 if (!face)
2859 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2860 }
2861 else
2862 face = s->face;
2863
2864 thickness = face->box_line_width;
2865
2866 NSTRACE (ns_dumpglyphs_box_or_relief);
2867
2868 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2869 ? WINDOW_RIGHT_EDGE_X (s->w)
2870 : window_box_right (s->w, s->area));
2871 last_glyph = (s->cmp || s->img
2872 ? s->first_glyph : s->first_glyph + s->nchars-1);
2873
2874 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
2875 ? last_x - 1 : min (last_x, s->x + s->background_width) - 1));
2876
2877 left_p = (s->first_glyph->left_box_line_p
2878 || (s->hl == DRAW_MOUSE_FACE
2879 && (s->prev == NULL || s->prev->hl != s->hl)));
2880 right_p = (last_glyph->right_box_line_p
2881 || (s->hl == DRAW_MOUSE_FACE
2882 && (s->next == NULL || s->next->hl != s->hl)));
2883
2884 r = NSMakeRect (s->x, s->y, right_x - s->x + 1, s->height);
2885
2886 /* expand full-width row over internal borders */
2887 if (s->row->full_width_p)
2888 r = ns_fix_rect_ibw (r, FRAME_INTERNAL_BORDER_WIDTH (s->f),
2889 FRAME_PIXEL_WIDTH (s->f));
2890
2891 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
2892 if (s->face->box == FACE_SIMPLE_BOX && s->face->box_color)
2893 {
2894 ns_draw_box (r, abs (thickness),
2895 ns_lookup_indexed_color (face->box_color, s->f),
2896 left_p, right_p);
2897 }
2898 else
2899 {
2900 ns_draw_relief (r, abs (thickness), s->face->box == FACE_RAISED_BOX,
2901 1, 1, left_p, right_p, s);
2902 }
2903 }
2904
2905
2906 static void
2907 ns_maybe_dumpglyphs_background (struct glyph_string *s, char force_p)
2908 /* --------------------------------------------------------------------------
2909 Modeled after x_draw_glyph_string_background, which draws BG in
2910 certain cases. Others are left to the text rendering routine.
2911 -------------------------------------------------------------------------- */
2912 {
2913 NSTRACE (ns_maybe_dumpglyphs_background);
2914
2915 if (!s->background_filled_p/* || s->hl == DRAW_MOUSE_FACE*/)
2916 {
2917 int box_line_width = max (s->face->box_line_width, 0);
2918 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
2919 || s->font_not_found_p || s->extends_to_end_of_line_p || force_p)
2920 {
2921 struct face *face;
2922 if (s->hl == DRAW_MOUSE_FACE)
2923 {
2924 face = FACE_FROM_ID (s->f,
2925 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
2926 if (!face)
2927 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2928 }
2929 else
2930 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
2931 if (!face->stipple)
2932 [(NS_FACE_BACKGROUND (face) != 0
2933 ? ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f)
2934 : FRAME_BACKGROUND_COLOR (s->f)) set];
2935 else
2936 {
2937 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (s->f);
2938 [[dpyinfo->bitmaps[face->stipple-1].img stippleMask] set];
2939 }
2940
2941 if (s->hl != DRAW_CURSOR)
2942 {
2943 NSRect r = NSMakeRect (s->x, s->y + box_line_width,
2944 s->background_width,
2945 s->height-2*box_line_width);
2946
2947 /* expand full-width row over internal borders */
2948 if (s->row->full_width_p)
2949 {
2950 int fibw = FRAME_INTERNAL_BORDER_WIDTH (s->f);
2951 if (r.origin.y <= fibw+1 + box_line_width)
2952 {
2953 r.size.height += r.origin.y;
2954 r.origin.y = 0;
2955 }
2956 if (r.origin.x <= fibw+1)
2957 {
2958 r.size.width += 2*r.origin.x;
2959 r.origin.x = 0;
2960 }
2961 if (FRAME_PIXEL_WIDTH (s->f) - (r.origin.x + r.size.width)
2962 <= fibw+1)
2963 r.size.width += fibw;
2964 }
2965
2966 NSRectFill (r);
2967 }
2968
2969 s->background_filled_p = 1;
2970 }
2971 }
2972 }
2973
2974
2975 static void
2976 ns_dumpglyphs_image (struct glyph_string *s, NSRect r)
2977 /* --------------------------------------------------------------------------
2978 Renders an image and associated borders.
2979 -------------------------------------------------------------------------- */
2980 {
2981 EmacsImage *img = s->img->pixmap;
2982 int box_line_vwidth = max (s->face->box_line_width, 0);
2983 int x = s->x, y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2984 int bg_x, bg_y, bg_height;
2985 int th;
2986 char raised_p;
2987 NSRect br;
2988 struct face *face;
2989 NSColor *tdCol;
2990
2991 NSTRACE (ns_dumpglyphs_image);
2992
2993 if (s->face->box != FACE_NO_BOX
2994 && s->first_glyph->left_box_line_p && s->slice.x == 0)
2995 x += abs (s->face->box_line_width);
2996
2997 bg_x = x;
2998 bg_y = s->slice.y == 0 ? s->y : s->y + box_line_vwidth;
2999 bg_height = s->height;
3000 /* other terms have this, but was causing problems w/tabbar mode */
3001 /* - 2 * box_line_vwidth; */
3002
3003 if (s->slice.x == 0) x += s->img->hmargin;
3004 if (s->slice.y == 0) y += s->img->vmargin;
3005
3006 /* Draw BG: if we need larger area than image itself cleared, do that,
3007 otherwise, since we composite the image under NS (instead of mucking
3008 with its background color), we must clear just the image area. */
3009 if (s->hl == DRAW_MOUSE_FACE)
3010 {
3011 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3012 if (!face)
3013 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3014 }
3015 else
3016 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3017
3018 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f) set];
3019
3020 if (bg_height > s->slice.height || s->img->hmargin || s->img->vmargin
3021 || s->img->mask || s->img->pixmap == 0 || s->width != s->background_width)
3022 {
3023 br = NSMakeRect (bg_x, bg_y, s->background_width, bg_height);
3024 s->background_filled_p = 1;
3025 }
3026 else
3027 {
3028 br = NSMakeRect (x, y, s->slice.width, s->slice.height);
3029 }
3030
3031 /* expand full-width row over internal borders */
3032 if (s->row->full_width_p)
3033 {
3034 int fibw = FRAME_INTERNAL_BORDER_WIDTH (s->f);
3035 if (br.origin.y <= fibw+1 + box_line_vwidth)
3036 {
3037 br.size.height += br.origin.y;
3038 br.origin.y = 0;
3039 }
3040 if (br.origin.x <= fibw+1 + box_line_vwidth)
3041 {
3042 br.size.width += br.origin.x;
3043 br.origin.x = 0;
3044 }
3045 if (FRAME_PIXEL_WIDTH (s->f) - (br.origin.x + br.size.width) <= fibw+1)
3046 br.size.width += fibw;
3047 }
3048
3049 NSRectFill (br);
3050
3051 /* Draw the image.. do we need to draw placeholder if img ==nil? */
3052 if (img != nil)
3053 [img drawInRect: br
3054 fromRect: NSZeroRect
3055 operation: NSCompositeSourceOver
3056 fraction: 1.0
3057 respectFlipped: YES
3058 hints: nil];
3059
3060 if (s->hl == DRAW_CURSOR)
3061 {
3062 [FRAME_CURSOR_COLOR (s->f) set];
3063 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3064 tdCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3065 else
3066 /* Currently on NS img->mask is always 0. Since
3067 get_window_cursor_type specifies a hollow box cursor when on
3068 a non-masked image we never reach this clause. But we put it
3069 in in anticipation of better support for image masks on
3070 NS. */
3071 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3072 }
3073 else
3074 {
3075 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3076 }
3077
3078 /* Draw underline, overline, strike-through. */
3079 ns_draw_text_decoration (s, face, tdCol, br.size.width, br.origin.x);
3080
3081 /* Draw relief, if requested */
3082 if (s->img->relief || s->hl ==DRAW_IMAGE_RAISED || s->hl ==DRAW_IMAGE_SUNKEN)
3083 {
3084 if (s->hl == DRAW_IMAGE_SUNKEN || s->hl == DRAW_IMAGE_RAISED)
3085 {
3086 th = tool_bar_button_relief >= 0 ?
3087 tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3088 raised_p = (s->hl == DRAW_IMAGE_RAISED);
3089 }
3090 else
3091 {
3092 th = abs (s->img->relief);
3093 raised_p = (s->img->relief > 0);
3094 }
3095
3096 r.origin.x = x - th;
3097 r.origin.y = y - th;
3098 r.size.width = s->slice.width + 2*th-1;
3099 r.size.height = s->slice.height + 2*th-1;
3100 ns_draw_relief (r, th, raised_p,
3101 s->slice.y == 0,
3102 s->slice.y + s->slice.height == s->img->height,
3103 s->slice.x == 0,
3104 s->slice.x + s->slice.width == s->img->width, s);
3105 }
3106
3107 /* If there is no mask, the background won't be seen,
3108 so draw a rectangle on the image for the cursor.
3109 Do this for all images, getting transparency right is not reliable. */
3110 if (s->hl == DRAW_CURSOR)
3111 {
3112 int thickness = abs (s->img->relief);
3113 if (thickness == 0) thickness = 1;
3114 ns_draw_box (br, thickness, FRAME_CURSOR_COLOR (s->f), 1, 1);
3115 }
3116 }
3117
3118
3119 static void
3120 ns_dumpglyphs_stretch (struct glyph_string *s)
3121 {
3122 NSRect r[2];
3123 int n, i;
3124 struct face *face;
3125 NSColor *fgCol, *bgCol;
3126
3127 if (!s->background_filled_p)
3128 {
3129 n = ns_get_glyph_string_clip_rect (s, r);
3130 *r = NSMakeRect (s->x, s->y, s->background_width, s->height);
3131
3132 ns_focus (s->f, r, n);
3133
3134 if (s->hl == DRAW_MOUSE_FACE)
3135 {
3136 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3137 if (!face)
3138 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3139 }
3140 else
3141 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3142
3143 bgCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3144 fgCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3145
3146 for (i=0; i<n; i++)
3147 {
3148 if (!s->row->full_width_p)
3149 {
3150 int overrun, leftoverrun;
3151
3152 /* truncate to avoid overwriting fringe and/or scrollbar */
3153 overrun = max (0, (s->x + s->background_width)
3154 - (WINDOW_BOX_RIGHT_EDGE_X (s->w)
3155 - WINDOW_RIGHT_FRINGE_WIDTH (s->w)));
3156 r[i].size.width -= overrun;
3157
3158 /* truncate to avoid overwriting to left of the window box */
3159 leftoverrun = (WINDOW_BOX_LEFT_EDGE_X (s->w)
3160 + WINDOW_LEFT_FRINGE_WIDTH (s->w)) - s->x;
3161
3162 if (leftoverrun > 0)
3163 {
3164 r[i].origin.x += leftoverrun;
3165 r[i].size.width -= leftoverrun;
3166 }
3167
3168 /* XXX: Try to work between problem where a stretch glyph on
3169 a partially-visible bottom row will clear part of the
3170 modeline, and another where list-buffers headers and similar
3171 rows erroneously have visible_height set to 0. Not sure
3172 where this is coming from as other terms seem not to show. */
3173 r[i].size.height = min (s->height, s->row->visible_height);
3174 }
3175
3176 /* expand full-width rows over internal borders */
3177 else
3178 {
3179 r[i] = ns_fix_rect_ibw (r[i], FRAME_INTERNAL_BORDER_WIDTH (s->f),
3180 FRAME_PIXEL_WIDTH (s->f));
3181 }
3182
3183 [bgCol set];
3184
3185 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3186 overwriting cursor (usually when cursor on a tab) */
3187 if (s->hl == DRAW_CURSOR)
3188 {
3189 CGFloat x, width;
3190
3191 x = r[i].origin.x;
3192 width = s->w->phys_cursor_width;
3193 r[i].size.width -= width;
3194 r[i].origin.x += width;
3195
3196 NSRectFill (r[i]);
3197
3198 /* Draw overlining, etc. on the cursor. */
3199 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3200 ns_draw_text_decoration (s, face, bgCol, width, x);
3201 else
3202 ns_draw_text_decoration (s, face, fgCol, width, x);
3203 }
3204 else
3205 {
3206 NSRectFill (r[i]);
3207 }
3208
3209 /* Draw overlining, etc. on the stretch glyph (or the part
3210 of the stretch glyph after the cursor). */
3211 ns_draw_text_decoration (s, face, fgCol, r[i].size.width,
3212 r[i].origin.x);
3213 }
3214 ns_unfocus (s->f);
3215 s->background_filled_p = 1;
3216 }
3217 }
3218
3219
3220 static void
3221 ns_draw_glyph_string (struct glyph_string *s)
3222 /* --------------------------------------------------------------------------
3223 External (RIF): Main draw-text call.
3224 -------------------------------------------------------------------------- */
3225 {
3226 /* TODO (optimize): focus for box and contents draw */
3227 NSRect r[2];
3228 int n;
3229 char box_drawn_p = 0;
3230
3231 NSTRACE (ns_draw_glyph_string);
3232
3233 if (s->next && s->right_overhang && !s->for_overlaps/*&&s->hl!=DRAW_CURSOR*/)
3234 {
3235 int width;
3236 struct glyph_string *next;
3237
3238 for (width = 0, next = s->next;
3239 next && width < s->right_overhang;
3240 width += next->width, next = next->next)
3241 if (next->first_glyph->type != IMAGE_GLYPH)
3242 {
3243 if (next->first_glyph->type != STRETCH_GLYPH)
3244 {
3245 n = ns_get_glyph_string_clip_rect (s->next, r);
3246 ns_focus (s->f, r, n);
3247 ns_maybe_dumpglyphs_background (s->next, 1);
3248 ns_unfocus (s->f);
3249 }
3250 else
3251 {
3252 ns_dumpglyphs_stretch (s->next);
3253 }
3254 next->num_clips = 0;
3255 }
3256 }
3257
3258 if (!s->for_overlaps && s->face->box != FACE_NO_BOX
3259 && (s->first_glyph->type == CHAR_GLYPH
3260 || s->first_glyph->type == COMPOSITE_GLYPH))
3261 {
3262 n = ns_get_glyph_string_clip_rect (s, r);
3263 ns_focus (s->f, r, n);
3264 ns_maybe_dumpglyphs_background (s, 1);
3265 ns_dumpglyphs_box_or_relief (s);
3266 ns_unfocus (s->f);
3267 box_drawn_p = 1;
3268 }
3269
3270 switch (s->first_glyph->type)
3271 {
3272
3273 case IMAGE_GLYPH:
3274 n = ns_get_glyph_string_clip_rect (s, r);
3275 ns_focus (s->f, r, n);
3276 ns_dumpglyphs_image (s, r[0]);
3277 ns_unfocus (s->f);
3278 break;
3279
3280 case STRETCH_GLYPH:
3281 ns_dumpglyphs_stretch (s);
3282 break;
3283
3284 case CHAR_GLYPH:
3285 case COMPOSITE_GLYPH:
3286 n = ns_get_glyph_string_clip_rect (s, r);
3287 ns_focus (s->f, r, n);
3288
3289 if (s->for_overlaps || (s->cmp_from > 0
3290 && ! s->first_glyph->u.cmp.automatic))
3291 s->background_filled_p = 1;
3292 else
3293 ns_maybe_dumpglyphs_background
3294 (s, s->first_glyph->type == COMPOSITE_GLYPH);
3295
3296 ns_tmp_flags = s->hl == DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR :
3297 (s->hl == DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE :
3298 (s->for_overlaps ? NS_DUMPGLYPH_FOREGROUND :
3299 NS_DUMPGLYPH_NORMAL));
3300 ns_tmp_font = (struct nsfont_info *)s->face->font;
3301 if (ns_tmp_font == NULL)
3302 ns_tmp_font = (struct nsfont_info *)FRAME_FONT (s->f);
3303
3304 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3305 {
3306 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
3307 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
3308 NS_FACE_FOREGROUND (s->face) = tmp;
3309 }
3310
3311 ns_tmp_font->font.driver->draw
3312 (s, 0, s->nchars, s->x, s->y,
3313 (ns_tmp_flags == NS_DUMPGLYPH_NORMAL && !s->background_filled_p)
3314 || ns_tmp_flags == NS_DUMPGLYPH_MOUSEFACE);
3315
3316 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3317 {
3318 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
3319 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
3320 NS_FACE_FOREGROUND (s->face) = tmp;
3321 }
3322
3323 ns_unfocus (s->f);
3324 break;
3325
3326 case GLYPHLESS_GLYPH:
3327 n = ns_get_glyph_string_clip_rect (s, r);
3328 ns_focus (s->f, r, n);
3329
3330 if (s->for_overlaps || (s->cmp_from > 0
3331 && ! s->first_glyph->u.cmp.automatic))
3332 s->background_filled_p = 1;
3333 else
3334 ns_maybe_dumpglyphs_background
3335 (s, s->first_glyph->type == COMPOSITE_GLYPH);
3336 /* ... */
3337 /* Not yet implemented. */
3338 /* ... */
3339 ns_unfocus (s->f);
3340 break;
3341
3342 default:
3343 abort ();
3344 }
3345
3346 /* Draw box if not done already. */
3347 if (!s->for_overlaps && !box_drawn_p && s->face->box != FACE_NO_BOX)
3348 {
3349 n = ns_get_glyph_string_clip_rect (s, r);
3350 ns_focus (s->f, r, n);
3351 ns_dumpglyphs_box_or_relief (s);
3352 ns_unfocus (s->f);
3353 }
3354
3355 s->num_clips = 0;
3356 }
3357
3358
3359
3360 /* ==========================================================================
3361
3362 Event loop
3363
3364 ========================================================================== */
3365
3366
3367 static void
3368 ns_send_appdefined (int value)
3369 /* --------------------------------------------------------------------------
3370 Internal: post an appdefined event which EmacsApp-sendEvent will
3371 recognize and take as a command to halt the event loop.
3372 -------------------------------------------------------------------------- */
3373 {
3374 /*NSTRACE (ns_send_appdefined); */
3375
3376 /* Only post this event if we haven't already posted one. This will end
3377 the [NXApp run] main loop after having processed all events queued at
3378 this moment. */
3379 if (send_appdefined)
3380 {
3381 NSEvent *nxev;
3382
3383 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3384 send_appdefined = NO;
3385
3386 /* Don't need wakeup timer any more */
3387 if (timed_entry)
3388 {
3389 [timed_entry invalidate];
3390 [timed_entry release];
3391 timed_entry = nil;
3392 }
3393
3394 nxev = [NSEvent otherEventWithType: NSApplicationDefined
3395 location: NSMakePoint (0, 0)
3396 modifierFlags: 0
3397 timestamp: 0
3398 windowNumber: [[NSApp mainWindow] windowNumber]
3399 context: [NSApp context]
3400 subtype: 0
3401 data1: value
3402 data2: 0];
3403
3404 /* Post an application defined event on the event queue. When this is
3405 received the [NXApp run] will return, thus having processed all
3406 events which are currently queued. */
3407 [NSApp postEvent: nxev atStart: NO];
3408 }
3409 }
3410
3411 static int
3412 ns_read_socket (struct terminal *terminal, int expected,
3413 struct input_event *hold_quit)
3414 /* --------------------------------------------------------------------------
3415 External (hook): Post an event to ourself and keep reading events until
3416 we read it back again. In effect process all events which were waiting.
3417 From 21+ we have to manage the event buffer ourselves.
3418 -------------------------------------------------------------------------- */
3419 {
3420 struct input_event ev;
3421 int nevents;
3422
3423 /* NSTRACE (ns_read_socket); */
3424
3425 if ([NSApp modalWindow] != nil)
3426 return -1;
3427
3428 if (interrupt_input_blocked)
3429 {
3430 interrupt_input_pending = 1;
3431 #ifdef SYNC_INPUT
3432 pending_signals = 1;
3433 #endif
3434 return -1;
3435 }
3436
3437 interrupt_input_pending = 0;
3438 #ifdef SYNC_INPUT
3439 pending_signals = pending_atimers;
3440 #endif
3441
3442 BLOCK_INPUT;
3443 n_emacs_events_pending = 0;
3444 EVENT_INIT (ev);
3445 emacs_event = &ev;
3446 q_event_ptr = hold_quit;
3447
3448 /* we manage autorelease pools by allocate/reallocate each time around
3449 the loop; strict nesting is occasionally violated but seems not to
3450 matter.. earlier methods using full nesting caused major memory leaks */
3451 [outerpool release];
3452 outerpool = [[NSAutoreleasePool alloc] init];
3453
3454 /* If have pending open-file requests, attend to the next one of those. */
3455 if (ns_pending_files && [ns_pending_files count] != 0
3456 && [(EmacsApp *)NSApp openFile: [ns_pending_files objectAtIndex: 0]])
3457 {
3458 [ns_pending_files removeObjectAtIndex: 0];
3459 }
3460 /* Deal with pending service requests. */
3461 else if (ns_pending_service_names && [ns_pending_service_names count] != 0
3462 && [(EmacsApp *)
3463 NSApp fulfillService: [ns_pending_service_names objectAtIndex: 0]
3464 withArg: [ns_pending_service_args objectAtIndex: 0]])
3465 {
3466 [ns_pending_service_names removeObjectAtIndex: 0];
3467 [ns_pending_service_args removeObjectAtIndex: 0];
3468 }
3469 else
3470 {
3471 /* Run and wait for events. We must always send one NX_APPDEFINED event
3472 to ourself, otherwise [NXApp run] will never exit. */
3473 send_appdefined = YES;
3474 ns_send_appdefined (-1);
3475
3476 if (++apploopnr != 1)
3477 {
3478 abort ();
3479 }
3480 [NSApp run];
3481 --apploopnr;
3482 }
3483
3484 nevents = n_emacs_events_pending;
3485 n_emacs_events_pending = 0;
3486 emacs_event = q_event_ptr = NULL;
3487 UNBLOCK_INPUT;
3488
3489 return nevents;
3490 }
3491
3492
3493 int
3494 ns_select (int nfds, fd_set *readfds, fd_set *writefds,
3495 fd_set *exceptfds, EMACS_TIME *timeout, sigset_t *sigmask)
3496 /* --------------------------------------------------------------------------
3497 Replacement for select, checking for events
3498 -------------------------------------------------------------------------- */
3499 {
3500 int result;
3501 NSEvent *ev;
3502 int k, nr = 0;
3503 struct input_event event;
3504 char c;
3505
3506 /* NSTRACE (ns_select); */
3507
3508 for (k = 0; readfds && k < nfds+1; k++)
3509 if (FD_ISSET(k, readfds)) ++nr;
3510
3511 if (NSApp == nil
3512 || (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0))
3513 return pselect (nfds, readfds, writefds, exceptfds, timeout, sigmask);
3514
3515 [outerpool release];
3516 outerpool = [[NSAutoreleasePool alloc] init];
3517
3518
3519 send_appdefined = YES;
3520 if (nr > 0)
3521 {
3522 pthread_mutex_lock (&select_mutex);
3523 select_nfds = nfds;
3524 select_valid = 0;
3525 if (readfds)
3526 {
3527 select_readfds = *readfds;
3528 select_valid += SELECT_HAVE_READ;
3529 }
3530 if (writefds)
3531 {
3532 select_writefds = *writefds;
3533 select_valid += SELECT_HAVE_WRITE;
3534 }
3535
3536 if (timeout)
3537 {
3538 select_timeout = *timeout;
3539 select_valid += SELECT_HAVE_TMO;
3540 }
3541
3542 pthread_mutex_unlock (&select_mutex);
3543
3544 /* Inform fd_handler that select should be called */
3545 c = 'g';
3546 write (selfds[1], &c, 1);
3547 }
3548 else if (nr == 0 && timeout)
3549 {
3550 /* No file descriptor, just a timeout, no need to wake fd_handler */
3551 double time = EMACS_TIME_TO_DOUBLE (*timeout);
3552 timed_entry = [[NSTimer scheduledTimerWithTimeInterval: time
3553 target: NSApp
3554 selector:
3555 @selector (timeout_handler:)
3556 userInfo: 0
3557 repeats: NO]
3558 retain];
3559 }
3560 else /* No timeout and no file descriptors, can this happen? */
3561 {
3562 /* Send appdefined so we exit from the loop */
3563 ns_send_appdefined (-1);
3564 }
3565
3566 EVENT_INIT (event);
3567 BLOCK_INPUT;
3568 emacs_event = &event;
3569 if (++apploopnr != 1)
3570 {
3571 abort();
3572 }
3573 [NSApp run];
3574 --apploopnr;
3575 emacs_event = NULL;
3576 if (nr > 0 && readfds)
3577 {
3578 c = 's';
3579 write (selfds[1], &c, 1);
3580 }
3581 UNBLOCK_INPUT;
3582
3583 ev = last_appdefined_event;
3584
3585 if (ev)
3586 {
3587 int t;
3588 if ([ev type] != NSApplicationDefined)
3589 abort ();
3590
3591 t = [ev data1];
3592 last_appdefined_event = 0;
3593
3594 if (t == -2)
3595 {
3596 /* The NX_APPDEFINED event we received was a timeout. */
3597 result = 0;
3598 }
3599 else if (t == -1)
3600 {
3601 /* The NX_APPDEFINED event we received was the result of
3602 at least one real input event arriving. */
3603 errno = EINTR;
3604 result = -1;
3605 }
3606 else
3607 {
3608 /* Received back from select () in fd_handler; copy the results */
3609 pthread_mutex_lock (&select_mutex);
3610 if (readfds) *readfds = select_readfds;
3611 if (writefds) *writefds = select_writefds;
3612 if (timeout) *timeout = select_timeout;
3613 pthread_mutex_unlock (&select_mutex);
3614 result = t;
3615 }
3616 }
3617
3618 return result;
3619 }
3620
3621
3622
3623 /* ==========================================================================
3624
3625 Scrollbar handling
3626
3627 ========================================================================== */
3628
3629
3630 static void
3631 ns_set_vertical_scroll_bar (struct window *window,
3632 int portion, int whole, int position)
3633 /* --------------------------------------------------------------------------
3634 External (hook): Update or add scrollbar
3635 -------------------------------------------------------------------------- */
3636 {
3637 Lisp_Object win;
3638 NSRect r, v;
3639 struct frame *f = XFRAME (WINDOW_FRAME (window));
3640 EmacsView *view = FRAME_NS_VIEW (f);
3641 int window_y, window_height;
3642 BOOL barOnVeryLeft, barOnVeryRight;
3643 int top, left, height, width, sb_width, sb_left;
3644 EmacsScroller *bar;
3645
3646 /* optimization; display engine sends WAY too many of these.. */
3647 if (!NILP (window->vertical_scroll_bar))
3648 {
3649 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3650 if ([bar checkSamePosition: position portion: portion whole: whole])
3651 {
3652 if (view->scrollbarsNeedingUpdate == 0)
3653 {
3654 if (!windows_or_buffers_changed)
3655 return;
3656 }
3657 else
3658 view->scrollbarsNeedingUpdate--;
3659 }
3660 }
3661
3662 NSTRACE (ns_set_vertical_scroll_bar);
3663
3664 /* Get dimensions. */
3665 window_box (window, -1, 0, &window_y, 0, &window_height);
3666 top = window_y;
3667 height = window_height;
3668 width = WINDOW_CONFIG_SCROLL_BAR_COLS (window) * FRAME_COLUMN_WIDTH (f);
3669 left = WINDOW_SCROLL_BAR_AREA_X (window);
3670
3671 if (top < 5) /* top scrollbar adjustment */
3672 {
3673 top -= FRAME_INTERNAL_BORDER_WIDTH (f);
3674 height += FRAME_INTERNAL_BORDER_WIDTH (f);
3675 }
3676
3677 /* allow for displaying a skinnier scrollbar than char area allotted */
3678 sb_width = (WINDOW_CONFIG_SCROLL_BAR_WIDTH (window) > 0) ?
3679 WINDOW_CONFIG_SCROLL_BAR_WIDTH (window) : width;
3680
3681 barOnVeryLeft = left < 5;
3682 barOnVeryRight = FRAME_PIXEL_WIDTH (f) - left - width < 5;
3683 sb_left = left + FRAME_INTERNAL_BORDER_WIDTH (f)
3684 * (barOnVeryLeft ? -1 : (barOnVeryRight ? 1 : 0));
3685
3686 r = NSMakeRect (sb_left, top, sb_width, height);
3687 /* the parent view is flipped, so we need to flip y value */
3688 v = [view frame];
3689 r.origin.y = (v.size.height - r.size.height - r.origin.y);
3690
3691 XSETWINDOW (win, window);
3692 BLOCK_INPUT;
3693
3694 /* we want at least 5 lines to display a scrollbar */
3695 if (WINDOW_TOTAL_LINES (window) < 5)
3696 {
3697 if (!NILP (window->vertical_scroll_bar))
3698 {
3699 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3700 [bar removeFromSuperview];
3701 wset_vertical_scroll_bar (window, Qnil);
3702 }
3703 ns_clear_frame_area (f, sb_left, top, width, height);
3704 UNBLOCK_INPUT;
3705 return;
3706 }
3707
3708 if (NILP (window->vertical_scroll_bar))
3709 {
3710 ns_clear_frame_area (f, sb_left, top, width, height);
3711 bar = [[EmacsScroller alloc] initFrame: r window: win];
3712 wset_vertical_scroll_bar (window, make_save_value (bar, 0));
3713 }
3714 else
3715 {
3716 NSRect oldRect;
3717 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3718 oldRect = [bar frame];
3719 r.size.width = oldRect.size.width;
3720 if (FRAME_LIVE_P (f) && !NSEqualRects (oldRect, r))
3721 {
3722 if (oldRect.origin.x != r.origin.x)
3723 ns_clear_frame_area (f, sb_left, top, width, height);
3724 [bar setFrame: r];
3725 }
3726 }
3727
3728 [bar setPosition: position portion: portion whole: whole];
3729 UNBLOCK_INPUT;
3730 }
3731
3732
3733 static void
3734 ns_condemn_scroll_bars (struct frame *f)
3735 /* --------------------------------------------------------------------------
3736 External (hook): arrange for all frame's scrollbars to be removed
3737 at next call to judge_scroll_bars, except for those redeemed.
3738 -------------------------------------------------------------------------- */
3739 {
3740 int i;
3741 id view;
3742 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
3743
3744 NSTRACE (ns_condemn_scroll_bars);
3745
3746 for (i =[subviews count]-1; i >= 0; i--)
3747 {
3748 view = [subviews objectAtIndex: i];
3749 if ([view isKindOfClass: [EmacsScroller class]])
3750 [view condemn];
3751 }
3752 }
3753
3754
3755 static void
3756 ns_redeem_scroll_bar (struct window *window)
3757 /* --------------------------------------------------------------------------
3758 External (hook): arrange to spare this window's scrollbar
3759 at next call to judge_scroll_bars.
3760 -------------------------------------------------------------------------- */
3761 {
3762 id bar;
3763 NSTRACE (ns_redeem_scroll_bar);
3764 if (!NILP (window->vertical_scroll_bar))
3765 {
3766 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3767 [bar reprieve];
3768 }
3769 }
3770
3771
3772 static void
3773 ns_judge_scroll_bars (struct frame *f)
3774 /* --------------------------------------------------------------------------
3775 External (hook): destroy all scrollbars on frame that weren't
3776 redeemed after call to condemn_scroll_bars.
3777 -------------------------------------------------------------------------- */
3778 {
3779 int i;
3780 id view;
3781 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
3782 NSTRACE (ns_judge_scroll_bars);
3783 for (i =[subviews count]-1; i >= 0; i--)
3784 {
3785 view = [subviews objectAtIndex: i];
3786 if (![view isKindOfClass: [EmacsScroller class]]) continue;
3787 [view judge];
3788 }
3789 }
3790
3791
3792 void
3793 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
3794 {
3795 /* XXX irrelevant under NS */
3796 }
3797
3798
3799
3800 /* ==========================================================================
3801
3802 Initialization
3803
3804 ========================================================================== */
3805
3806 int
3807 x_display_pixel_height (struct ns_display_info *dpyinfo)
3808 {
3809 NSScreen *screen = [NSScreen mainScreen];
3810 return [screen frame].size.height;
3811 }
3812
3813 int
3814 x_display_pixel_width (struct ns_display_info *dpyinfo)
3815 {
3816 NSScreen *screen = [NSScreen mainScreen];
3817 return [screen frame].size.width;
3818 }
3819
3820
3821 static Lisp_Object ns_string_to_lispmod (const char *s)
3822 /* --------------------------------------------------------------------------
3823 Convert modifier name to lisp symbol
3824 -------------------------------------------------------------------------- */
3825 {
3826 if (!strncmp (SSDATA (SYMBOL_NAME (Qmeta)), s, 10))
3827 return Qmeta;
3828 else if (!strncmp (SSDATA (SYMBOL_NAME (Qsuper)), s, 10))
3829 return Qsuper;
3830 else if (!strncmp (SSDATA (SYMBOL_NAME (Qcontrol)), s, 10))
3831 return Qcontrol;
3832 else if (!strncmp (SSDATA (SYMBOL_NAME (Qalt)), s, 10))
3833 return Qalt;
3834 else if (!strncmp (SSDATA (SYMBOL_NAME (Qhyper)), s, 10))
3835 return Qhyper;
3836 else if (!strncmp (SSDATA (SYMBOL_NAME (Qnone)), s, 10))
3837 return Qnone;
3838 else
3839 return Qnil;
3840 }
3841
3842
3843 static void
3844 ns_default (const char *parameter, Lisp_Object *result,
3845 Lisp_Object yesval, Lisp_Object noval,
3846 BOOL is_float, BOOL is_modstring)
3847 /* --------------------------------------------------------------------------
3848 Check a parameter value in user's preferences
3849 -------------------------------------------------------------------------- */
3850 {
3851 const char *value = ns_get_defaults_value (parameter);
3852
3853 if (value)
3854 {
3855 double f;
3856 char *pos;
3857 if (c_strcasecmp (value, "YES") == 0)
3858 *result = yesval;
3859 else if (c_strcasecmp (value, "NO") == 0)
3860 *result = noval;
3861 else if (is_float && (f = strtod (value, &pos), pos != value))
3862 *result = make_float (f);
3863 else if (is_modstring && value)
3864 *result = ns_string_to_lispmod (value);
3865 else fprintf (stderr,
3866 "Bad value for default \"%s\": \"%s\"\n", parameter, value);
3867 }
3868 }
3869
3870
3871 static void
3872 ns_initialize_display_info (struct ns_display_info *dpyinfo)
3873 /* --------------------------------------------------------------------------
3874 Initialize global info and storage for display.
3875 -------------------------------------------------------------------------- */
3876 {
3877 NSScreen *screen = [NSScreen mainScreen];
3878 NSWindowDepth depth = [screen depth];
3879 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
3880
3881 dpyinfo->resx = 72.27; /* used 75.0, but this makes pt == pixel, expected */
3882 dpyinfo->resy = 72.27;
3883 dpyinfo->color_p = ![NSDeviceWhiteColorSpace isEqualToString:
3884 NSColorSpaceFromDepth (depth)]
3885 && ![NSCalibratedWhiteColorSpace isEqualToString:
3886 NSColorSpaceFromDepth (depth)];
3887 dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth);
3888 dpyinfo->image_cache = make_image_cache ();
3889 dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table);
3890 dpyinfo->color_table->colors = NULL;
3891 dpyinfo->root_window = 42; /* a placeholder.. */
3892
3893 hlinfo->mouse_face_mouse_frame = NULL;
3894 hlinfo->mouse_face_deferred_gc = 0;
3895 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
3896 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
3897 hlinfo->mouse_face_face_id = DEFAULT_FACE_ID;
3898 hlinfo->mouse_face_window = hlinfo->mouse_face_overlay = Qnil;
3899 hlinfo->mouse_face_hidden = 0;
3900
3901 hlinfo->mouse_face_mouse_x = hlinfo->mouse_face_mouse_y = 0;
3902 hlinfo->mouse_face_defer = 0;
3903
3904 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame = NULL;
3905
3906 dpyinfo->n_fonts = 0;
3907 dpyinfo->smallest_font_height = 1;
3908 dpyinfo->smallest_char_width = 1;
3909 }
3910
3911
3912 /* This and next define (many of the) public functions in this file. */
3913 /* x_... are generic versions in xdisp.c that we, and other terms, get away
3914 with using despite presence in the "system dependent" redisplay
3915 interface. In addition, many of the ns_ methods have code that is
3916 shared with all terms, indicating need for further refactoring. */
3917 extern frame_parm_handler ns_frame_parm_handlers[];
3918 static struct redisplay_interface ns_redisplay_interface =
3919 {
3920 ns_frame_parm_handlers,
3921 x_produce_glyphs,
3922 x_write_glyphs,
3923 x_insert_glyphs,
3924 x_clear_end_of_line,
3925 ns_scroll_run,
3926 ns_after_update_window_line,
3927 ns_update_window_begin,
3928 ns_update_window_end,
3929 x_cursor_to,
3930 ns_flush,
3931 0, /* flush_display_optional */
3932 x_clear_window_mouse_face,
3933 x_get_glyph_overhangs,
3934 x_fix_overlapping_area,
3935 ns_draw_fringe_bitmap,
3936 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
3937 0, /* destroy_fringe_bitmap */
3938 ns_compute_glyph_string_overhangs,
3939 ns_draw_glyph_string, /* interface to nsfont.m */
3940 ns_define_frame_cursor,
3941 ns_clear_frame_area,
3942 ns_draw_window_cursor,
3943 ns_draw_vertical_window_border,
3944 ns_shift_glyphs_for_insert
3945 };
3946
3947
3948 static void
3949 ns_delete_display (struct ns_display_info *dpyinfo)
3950 {
3951 /* TODO... */
3952 }
3953
3954
3955 /* This function is called when the last frame on a display is deleted. */
3956 static void
3957 ns_delete_terminal (struct terminal *terminal)
3958 {
3959 struct ns_display_info *dpyinfo = terminal->display_info.ns;
3960
3961 /* Protect against recursive calls. delete_frame in
3962 delete_terminal calls us back when it deletes our last frame. */
3963 if (!terminal->name)
3964 return;
3965
3966 BLOCK_INPUT;
3967
3968 x_destroy_all_bitmaps (dpyinfo);
3969 ns_delete_display (dpyinfo);
3970 UNBLOCK_INPUT;
3971 }
3972
3973
3974 static struct terminal *
3975 ns_create_terminal (struct ns_display_info *dpyinfo)
3976 /* --------------------------------------------------------------------------
3977 Set up use of NS before we make the first connection.
3978 -------------------------------------------------------------------------- */
3979 {
3980 struct terminal *terminal;
3981
3982 NSTRACE (ns_create_terminal);
3983
3984 terminal = create_terminal ();
3985
3986 terminal->type = output_ns;
3987 terminal->display_info.ns = dpyinfo;
3988 dpyinfo->terminal = terminal;
3989
3990 terminal->rif = &ns_redisplay_interface;
3991
3992 terminal->clear_frame_hook = ns_clear_frame;
3993 terminal->ins_del_lines_hook = 0; /* XXX vestigial? */
3994 terminal->delete_glyphs_hook = 0; /* XXX vestigial? */
3995 terminal->ring_bell_hook = ns_ring_bell;
3996 terminal->reset_terminal_modes_hook = ns_reset_terminal_modes;
3997 terminal->set_terminal_modes_hook = ns_set_terminal_modes;
3998 terminal->update_begin_hook = ns_update_begin;
3999 terminal->update_end_hook = ns_update_end;
4000 terminal->set_terminal_window_hook = NULL; /* XXX vestigial? */
4001 terminal->read_socket_hook = ns_read_socket;
4002 terminal->frame_up_to_date_hook = ns_frame_up_to_date;
4003 terminal->mouse_position_hook = ns_mouse_position;
4004 terminal->frame_rehighlight_hook = ns_frame_rehighlight;
4005 terminal->frame_raise_lower_hook = ns_frame_raise_lower;
4006
4007 terminal->fullscreen_hook = 0; /* see XTfullscreen_hook */
4008
4009 terminal->set_vertical_scroll_bar_hook = ns_set_vertical_scroll_bar;
4010 terminal->condemn_scroll_bars_hook = ns_condemn_scroll_bars;
4011 terminal->redeem_scroll_bar_hook = ns_redeem_scroll_bar;
4012 terminal->judge_scroll_bars_hook = ns_judge_scroll_bars;
4013
4014 terminal->delete_frame_hook = x_destroy_window;
4015 terminal->delete_terminal_hook = ns_delete_terminal;
4016
4017 terminal->scroll_region_ok = 1;
4018 terminal->char_ins_del_ok = 1;
4019 terminal->line_ins_del_ok = 1;
4020 terminal->fast_clear_end_of_line = 1;
4021 terminal->memory_below_frame = 0;
4022
4023 return terminal;
4024 }
4025
4026
4027 struct ns_display_info *
4028 ns_term_init (Lisp_Object display_name)
4029 /* --------------------------------------------------------------------------
4030 Start the Application and get things rolling.
4031 -------------------------------------------------------------------------- */
4032 {
4033 struct terminal *terminal;
4034 struct ns_display_info *dpyinfo;
4035 static int ns_initialized = 0;
4036 Lisp_Object tmp;
4037
4038 NSTRACE (ns_term_init);
4039
4040 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
4041 /*GSDebugAllocationActive (YES); */
4042 BLOCK_INPUT;
4043 handling_signal = 0;
4044
4045 if (!ns_initialized)
4046 {
4047 baud_rate = 38400;
4048 Fset_input_interrupt_mode (Qnil);
4049
4050 if (selfds[0] == -1)
4051 {
4052 if (pipe (selfds) == -1)
4053 {
4054 fprintf (stderr, "Failed to create pipe: %s\n",
4055 emacs_strerror (errno));
4056 abort ();
4057 }
4058
4059 fcntl (selfds[0], F_SETFL, O_NONBLOCK|fcntl (selfds[0], F_GETFL));
4060 FD_ZERO (&select_readfds);
4061 FD_ZERO (&select_writefds);
4062 pthread_mutex_init (&select_mutex, NULL);
4063 }
4064 ns_initialized = 1;
4065 }
4066
4067 ns_pending_files = [[NSMutableArray alloc] init];
4068 ns_pending_service_names = [[NSMutableArray alloc] init];
4069 ns_pending_service_args = [[NSMutableArray alloc] init];
4070
4071 /* Start app and create the main menu, window, view.
4072 Needs to be here because ns_initialize_display_info () uses AppKit classes.
4073 The view will then ask the NSApp to stop and return to Emacs. */
4074 [EmacsApp sharedApplication];
4075 if (NSApp == nil)
4076 return NULL;
4077 [NSApp setDelegate: NSApp];
4078
4079 /* Start the select thread. */
4080 [NSThread detachNewThreadSelector:@selector (fd_handler:)
4081 toTarget:NSApp
4082 withObject:nil];
4083
4084 /* debugging: log all notifications */
4085 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
4086 selector: @selector (logNotification:)
4087 name: nil object: nil]; */
4088
4089 dpyinfo = xzalloc (sizeof *dpyinfo);
4090
4091 ns_initialize_display_info (dpyinfo);
4092 terminal = ns_create_terminal (dpyinfo);
4093
4094 terminal->kboard = xmalloc (sizeof *terminal->kboard);
4095 init_kboard (terminal->kboard);
4096 kset_window_system (terminal->kboard, Qns);
4097 terminal->kboard->next_kboard = all_kboards;
4098 all_kboards = terminal->kboard;
4099 /* Don't let the initial kboard remain current longer than necessary.
4100 That would cause problems if a file loaded on startup tries to
4101 prompt in the mini-buffer. */
4102 if (current_kboard == initial_kboard)
4103 current_kboard = terminal->kboard;
4104 terminal->kboard->reference_count++;
4105
4106 dpyinfo->next = x_display_list;
4107 x_display_list = dpyinfo;
4108
4109 /* Put it on ns_display_name_list */
4110 ns_display_name_list = Fcons (Fcons (display_name, Qnil),
4111 ns_display_name_list);
4112 dpyinfo->name_list_element = XCAR (ns_display_name_list);
4113
4114 terminal->name = xstrdup (SSDATA (display_name));
4115
4116 UNBLOCK_INPUT;
4117
4118 if (!inhibit_x_resources)
4119 {
4120 ns_default ("GSFontAntiAlias", &ns_antialias_text,
4121 Qt, Qnil, NO, NO);
4122 tmp = Qnil;
4123 /* this is a standard variable */
4124 ns_default ("AppleAntiAliasingThreshold", &tmp,
4125 make_float (10.0), make_float (6.0), YES, NO);
4126 ns_antialias_threshold = NILP (tmp) ? 10.0 : XFLOATINT (tmp);
4127 }
4128
4129 ns_selection_color = [[NSUserDefaults standardUserDefaults]
4130 stringForKey: @"AppleHighlightColor"];
4131 if (ns_selection_color == nil)
4132 ns_selection_color = NS_SELECTION_COLOR_DEFAULT;
4133
4134 {
4135 NSColorList *cl = [NSColorList colorListNamed: @"Emacs"];
4136
4137 if ( cl == nil )
4138 {
4139 Lisp_Object color_file, color_map, color;
4140 unsigned long c;
4141 char *name;
4142
4143 color_file = Fexpand_file_name (build_string ("rgb.txt"),
4144 Fsymbol_value (intern ("data-directory")));
4145 if (NILP (Ffile_readable_p (color_file)))
4146 fatal ("Could not find %s.\n", SDATA (color_file));
4147
4148 color_map = Fx_load_color_file (color_file);
4149 if (NILP (color_map))
4150 fatal ("Could not read %s.\n", SDATA (color_file));
4151
4152 cl = [[NSColorList alloc] initWithName: @"Emacs"];
4153 for ( ; CONSP (color_map); color_map = XCDR (color_map))
4154 {
4155 color = XCAR (color_map);
4156 name = SSDATA (XCAR (color));
4157 c = XINT (XCDR (color));
4158 [cl setColor:
4159 [NSColor colorWithCalibratedRed: RED_FROM_ULONG (c) / 255.0
4160 green: GREEN_FROM_ULONG (c) / 255.0
4161 blue: BLUE_FROM_ULONG (c) / 255.0
4162 alpha: 1.0]
4163 forKey: [NSString stringWithUTF8String: name]];
4164 }
4165 [cl writeToFile: nil];
4166 }
4167 }
4168
4169 {
4170 #ifdef NS_IMPL_GNUSTEP
4171 Vwindow_system_version = build_string (gnustep_base_version);
4172 #else
4173 /*PSnextrelease (128, c); */
4174 char c[DBL_BUFSIZE_BOUND];
4175 int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber);
4176 Vwindow_system_version = make_unibyte_string (c, len);
4177 #endif
4178 }
4179
4180 delete_keyboard_wait_descriptor (0);
4181
4182 ns_app_name = [[NSProcessInfo processInfo] processName];
4183
4184 /* Set up OS X app menu */
4185 #ifdef NS_IMPL_COCOA
4186 {
4187 NSMenu *appMenu;
4188 NSMenuItem *item;
4189 /* set up the application menu */
4190 svcsMenu = [[EmacsMenu alloc] initWithTitle: @"Services"];
4191 [svcsMenu setAutoenablesItems: NO];
4192 appMenu = [[EmacsMenu alloc] initWithTitle: @"Emacs"];
4193 [appMenu setAutoenablesItems: NO];
4194 mainMenu = [[EmacsMenu alloc] initWithTitle: @""];
4195 dockMenu = [[EmacsMenu alloc] initWithTitle: @""];
4196
4197 [appMenu insertItemWithTitle: @"About Emacs"
4198 action: @selector (orderFrontStandardAboutPanel:)
4199 keyEquivalent: @""
4200 atIndex: 0];
4201 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 1];
4202 [appMenu insertItemWithTitle: @"Preferences..."
4203 action: @selector (showPreferencesWindow:)
4204 keyEquivalent: @","
4205 atIndex: 2];
4206 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 3];
4207 item = [appMenu insertItemWithTitle: @"Services"
4208 action: @selector (menuDown:)
4209 keyEquivalent: @""
4210 atIndex: 4];
4211 [appMenu setSubmenu: svcsMenu forItem: item];
4212 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 5];
4213 [appMenu insertItemWithTitle: @"Hide Emacs"
4214 action: @selector (hide:)
4215 keyEquivalent: @"h"
4216 atIndex: 6];
4217 item = [appMenu insertItemWithTitle: @"Hide Others"
4218 action: @selector (hideOtherApplications:)
4219 keyEquivalent: @"h"
4220 atIndex: 7];
4221 [item setKeyEquivalentModifierMask: NSCommandKeyMask | NSAlternateKeyMask];
4222 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 8];
4223 [appMenu insertItemWithTitle: @"Quit Emacs"
4224 action: @selector (terminate:)
4225 keyEquivalent: @"q"
4226 atIndex: 9];
4227
4228 item = [mainMenu insertItemWithTitle: ns_app_name
4229 action: @selector (menuDown:)
4230 keyEquivalent: @""
4231 atIndex: 0];
4232 [mainMenu setSubmenu: appMenu forItem: item];
4233 [dockMenu insertItemWithTitle: @"New Frame"
4234 action: @selector (newFrame:)
4235 keyEquivalent: @""
4236 atIndex: 0];
4237
4238 [NSApp setMainMenu: mainMenu];
4239 [NSApp setAppleMenu: appMenu];
4240 [NSApp setServicesMenu: svcsMenu];
4241 /* Needed at least on Cocoa, to get dock menu to show windows */
4242 [NSApp setWindowsMenu: [[NSMenu alloc] init]];
4243
4244 [[NSNotificationCenter defaultCenter]
4245 addObserver: mainMenu
4246 selector: @selector (trackingNotification:)
4247 name: NSMenuDidBeginTrackingNotification object: mainMenu];
4248 [[NSNotificationCenter defaultCenter]
4249 addObserver: mainMenu
4250 selector: @selector (trackingNotification:)
4251 name: NSMenuDidEndTrackingNotification object: mainMenu];
4252 }
4253 #endif /* MAC OS X menu setup */
4254
4255 [NSApp run];
4256 ns_do_open_file = YES;
4257 return dpyinfo;
4258 }
4259
4260
4261 void
4262 ns_term_shutdown (int sig)
4263 {
4264 [[NSUserDefaults standardUserDefaults] synchronize];
4265
4266 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4267 if (STRINGP (Vauto_save_list_file_name))
4268 unlink (SSDATA (Vauto_save_list_file_name));
4269
4270 if (sig == 0 || sig == SIGTERM)
4271 {
4272 [NSApp terminate: NSApp];
4273 }
4274 else // force a stack trace to happen
4275 {
4276 abort();
4277 }
4278 }
4279
4280
4281 /* ==========================================================================
4282
4283 EmacsApp implementation
4284
4285 ========================================================================== */
4286
4287
4288 @implementation EmacsApp
4289
4290 - (void)logNotification: (NSNotification *)notification
4291 {
4292 const char *name = [[notification name] UTF8String];
4293 if (!strstr (name, "Update") && !strstr (name, "NSMenu")
4294 && !strstr (name, "WindowNumber"))
4295 NSLog (@"notification: '%@'", [notification name]);
4296 }
4297
4298
4299 - (void)sendEvent: (NSEvent *)theEvent
4300 /* --------------------------------------------------------------------------
4301 Called when NSApp is running for each event received. Used to stop
4302 the loop when we choose, since there's no way to just run one iteration.
4303 -------------------------------------------------------------------------- */
4304 {
4305 int type = [theEvent type];
4306 NSWindow *window = [theEvent window];
4307 /* NSTRACE (sendEvent); */
4308 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4309
4310 #ifdef NS_IMPL_COCOA
4311 if (type == NSApplicationDefined
4312 && [theEvent data2] == NSAPP_DATA2_RUNASSCRIPT)
4313 {
4314 ns_run_ascript ();
4315 [self stop: self];
4316 return;
4317 }
4318 #endif
4319
4320 if (type == NSCursorUpdate && window == nil)
4321 {
4322 fprintf (stderr, "Dropping external cursor update event.\n");
4323 return;
4324 }
4325
4326 #ifdef NS_IMPL_COCOA
4327 /* pass mouse down in resize handle and subsequent drags directly to
4328 EmacsWindow so we can generate continuous redisplays */
4329 if (ns_in_resize)
4330 {
4331 if (type == NSLeftMouseDragged)
4332 {
4333 [window mouseDragged: theEvent];
4334 return;
4335 }
4336 else if (type == NSLeftMouseUp)
4337 {
4338 [window mouseUp: theEvent];
4339 return;
4340 }
4341 }
4342 else if (type == NSLeftMouseDown)
4343 {
4344 NSRect r = ns_resize_handle_rect (window);
4345 if (NSPointInRect ([theEvent locationInWindow], r))
4346 {
4347 ns_in_resize = YES;
4348 [window mouseDown: theEvent];
4349 return;
4350 }
4351 }
4352 #endif
4353
4354 if (type == NSApplicationDefined)
4355 {
4356 /* Events posted by ns_send_appdefined interrupt the run loop here.
4357 But, if a modal window is up, an appdefined can still come through,
4358 (e.g., from a makeKeyWindow event) but stopping self also stops the
4359 modal loop. Just defer it until later. */
4360 if ([NSApp modalWindow] == nil)
4361 {
4362 last_appdefined_event = theEvent;
4363 [self stop: self];
4364 }
4365 else
4366 {
4367 send_appdefined = YES;
4368 }
4369 }
4370
4371 [super sendEvent: theEvent];
4372 }
4373
4374
4375 - (void)showPreferencesWindow: (id)sender
4376 {
4377 struct frame *emacsframe = SELECTED_FRAME ();
4378 NSEvent *theEvent = [NSApp currentEvent];
4379
4380 if (!emacs_event)
4381 return;
4382 emacs_event->kind = NS_NONKEY_EVENT;
4383 emacs_event->code = KEY_NS_SHOW_PREFS;
4384 emacs_event->modifiers = 0;
4385 EV_TRAILER (theEvent);
4386 }
4387
4388
4389 - (void)newFrame: (id)sender
4390 {
4391 struct frame *emacsframe = SELECTED_FRAME ();
4392 NSEvent *theEvent = [NSApp currentEvent];
4393
4394 if (!emacs_event)
4395 return;
4396 emacs_event->kind = NS_NONKEY_EVENT;
4397 emacs_event->code = KEY_NS_NEW_FRAME;
4398 emacs_event->modifiers = 0;
4399 EV_TRAILER (theEvent);
4400 }
4401
4402
4403 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4404 - (BOOL) openFile: (NSString *)fileName
4405 {
4406 struct frame *emacsframe = SELECTED_FRAME ();
4407 NSEvent *theEvent = [NSApp currentEvent];
4408
4409 if (!emacs_event)
4410 return NO;
4411
4412 emacs_event->kind = NS_NONKEY_EVENT;
4413 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
4414 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
4415 ns_input_line = Qnil; /* can be start or cons start,end */
4416 emacs_event->modifiers =0;
4417 EV_TRAILER (theEvent);
4418
4419 return YES;
4420 }
4421
4422
4423 /* **************************************************************************
4424
4425 EmacsApp delegate implementation
4426
4427 ************************************************************************** */
4428
4429 - (void)applicationDidFinishLaunching: (NSNotification *)notification
4430 /* --------------------------------------------------------------------------
4431 When application is loaded, terminate event loop in ns_term_init
4432 -------------------------------------------------------------------------- */
4433 {
4434 NSTRACE (applicationDidFinishLaunching);
4435 [NSApp setServicesProvider: NSApp];
4436 ns_send_appdefined (-2);
4437 }
4438
4439
4440 /* Termination sequences:
4441 C-x C-c:
4442 Cmd-Q:
4443 MenuBar | File | Exit:
4444 Select Quit from App menubar:
4445 -terminate
4446 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4447 ns_term_shutdown()
4448
4449 Select Quit from Dock menu:
4450 Logout attempt:
4451 -appShouldTerminate
4452 Cancel -> Nothing else
4453 Accept ->
4454
4455 -terminate
4456 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4457 ns_term_shutdown()
4458
4459 */
4460
4461 - (void) terminate: (id)sender
4462 {
4463 struct frame *emacsframe = SELECTED_FRAME ();
4464
4465 if (!emacs_event)
4466 return;
4467
4468 emacs_event->kind = NS_NONKEY_EVENT;
4469 emacs_event->code = KEY_NS_POWER_OFF;
4470 emacs_event->arg = Qt; /* mark as non-key event */
4471 EV_TRAILER ((id)nil);
4472 }
4473
4474
4475 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
4476 {
4477 int ret;
4478
4479 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
4480 return NSTerminateNow;
4481
4482 ret = NSRunAlertPanel(ns_app_name,
4483 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
4484 @"Save Buffers and Exit", @"Cancel", nil);
4485
4486 if (ret == NSAlertDefaultReturn)
4487 return NSTerminateNow;
4488 else if (ret == NSAlertAlternateReturn)
4489 return NSTerminateCancel;
4490 return NSTerminateNow; /* just in case */
4491 }
4492
4493 static int
4494 not_in_argv (NSString *arg)
4495 {
4496 int k;
4497 const char *a = [arg UTF8String];
4498 for (k = 1; k < initial_argc; ++k)
4499 if (strcmp (a, initial_argv[k]) == 0) return 0;
4500 return 1;
4501 }
4502
4503 /* Notification from the Workspace to open a file */
4504 - (BOOL)application: sender openFile: (NSString *)file
4505 {
4506 if (ns_do_open_file || not_in_argv (file))
4507 [ns_pending_files addObject: file];
4508 return YES;
4509 }
4510
4511
4512 /* Open a file as a temporary file */
4513 - (BOOL)application: sender openTempFile: (NSString *)file
4514 {
4515 if (ns_do_open_file || not_in_argv (file))
4516 [ns_pending_files addObject: file];
4517 return YES;
4518 }
4519
4520
4521 /* Notification from the Workspace to open a file noninteractively (?) */
4522 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
4523 {
4524 if (ns_do_open_file || not_in_argv (file))
4525 [ns_pending_files addObject: file];
4526 return YES;
4527 }
4528
4529 /* Notification from the Workspace to open multiple files */
4530 - (void)application: sender openFiles: (NSArray *)fileList
4531 {
4532 NSEnumerator *files = [fileList objectEnumerator];
4533 NSString *file;
4534 /* Don't open files from the command line unconditionally,
4535 Cocoa parses the command line wrong, --option value tries to open value
4536 if --option is the last option. */
4537 while ((file = [files nextObject]) != nil)
4538 if (ns_do_open_file || not_in_argv (file))
4539 [ns_pending_files addObject: file];
4540
4541 [self replyToOpenOrPrint: NSApplicationDelegateReplySuccess];
4542
4543 }
4544
4545
4546 /* Handle dock menu requests. */
4547 - (NSMenu *)applicationDockMenu: (NSApplication *) sender
4548 {
4549 return dockMenu;
4550 }
4551
4552
4553 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4554 - (void)applicationWillBecomeActive: (NSNotification *)notification
4555 {
4556 //ns_app_active=YES;
4557 }
4558 - (void)applicationDidBecomeActive: (NSNotification *)notification
4559 {
4560 NSTRACE (applicationDidBecomeActive);
4561
4562 //ns_app_active=YES;
4563
4564 ns_update_auto_hide_menu_bar ();
4565 // No constraining takes place when the application is not active.
4566 ns_constrain_all_frames ();
4567 }
4568 - (void)applicationDidResignActive: (NSNotification *)notification
4569 {
4570 //ns_app_active=NO;
4571 ns_send_appdefined (-1);
4572 }
4573
4574
4575
4576 /* ==========================================================================
4577
4578 EmacsApp aux handlers for managing event loop
4579
4580 ========================================================================== */
4581
4582
4583 - (void)timeout_handler: (NSTimer *)timedEntry
4584 /* --------------------------------------------------------------------------
4585 The timeout specified to ns_select has passed.
4586 -------------------------------------------------------------------------- */
4587 {
4588 /*NSTRACE (timeout_handler); */
4589 ns_send_appdefined (-2);
4590 }
4591
4592 - (void)fd_handler:(id)unused
4593 /* --------------------------------------------------------------------------
4594 Check data waiting on file descriptors and terminate if so
4595 -------------------------------------------------------------------------- */
4596 {
4597 int result;
4598 int waiting = 1, nfds;
4599 char c;
4600
4601 SELECT_TYPE readfds, writefds, *wfds;
4602 EMACS_TIME timeout, *tmo;
4603 NSAutoreleasePool *pool = nil;
4604
4605 /* NSTRACE (fd_handler); */
4606
4607 for (;;)
4608 {
4609 [pool release];
4610 pool = [[NSAutoreleasePool alloc] init];
4611
4612 if (waiting)
4613 {
4614 SELECT_TYPE fds;
4615
4616 FD_SET (selfds[0], &fds);
4617 result = select (selfds[0]+1, &fds, NULL, NULL, NULL);
4618 if (result > 0)
4619 {
4620 read (selfds[0], &c, 1);
4621 if (c == 'g') waiting = 0;
4622 }
4623 }
4624 else
4625 {
4626 pthread_mutex_lock (&select_mutex);
4627 nfds = select_nfds;
4628
4629 if (select_valid & SELECT_HAVE_READ)
4630 readfds = select_readfds;
4631 else
4632 FD_ZERO (&readfds);
4633
4634 if (select_valid & SELECT_HAVE_WRITE)
4635 {
4636 writefds = select_writefds;
4637 wfds = &writefds;
4638 }
4639 else
4640 wfds = NULL;
4641 if (select_valid & SELECT_HAVE_TMO)
4642 {
4643 timeout = select_timeout;
4644 tmo = &timeout;
4645 }
4646 else
4647 tmo = NULL;
4648
4649 pthread_mutex_unlock (&select_mutex);
4650
4651 FD_SET (selfds[0], &readfds);
4652 if (selfds[0] >= nfds) nfds = selfds[0]+1;
4653
4654 result = pselect (nfds, &readfds, wfds, NULL, tmo, NULL);
4655
4656 if (result == 0)
4657 ns_send_appdefined (-2);
4658 else if (result > 0)
4659 {
4660 if (FD_ISSET (selfds[0], &readfds))
4661 {
4662 read (selfds[0], &c, 1);
4663 if (c == 's') waiting = 1;
4664 }
4665 else
4666 {
4667 pthread_mutex_lock (&select_mutex);
4668 if (select_valid & SELECT_HAVE_READ)
4669 select_readfds = readfds;
4670 if (select_valid & SELECT_HAVE_WRITE)
4671 select_writefds = writefds;
4672 if (select_valid & SELECT_HAVE_TMO)
4673 select_timeout = timeout;
4674 pthread_mutex_unlock (&select_mutex);
4675
4676 ns_send_appdefined (result);
4677 }
4678 }
4679 waiting = 1;
4680 }
4681 }
4682 }
4683
4684
4685
4686 /* ==========================================================================
4687
4688 Service provision
4689
4690 ========================================================================== */
4691
4692 /* called from system: queue for next pass through event loop */
4693 - (void)requestService: (NSPasteboard *)pboard
4694 userData: (NSString *)userData
4695 error: (NSString **)error
4696 {
4697 [ns_pending_service_names addObject: userData];
4698 [ns_pending_service_args addObject: [NSString stringWithUTF8String:
4699 SSDATA (ns_string_from_pasteboard (pboard))]];
4700 }
4701
4702
4703 /* called from ns_read_socket to clear queue */
4704 - (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg
4705 {
4706 struct frame *emacsframe = SELECTED_FRAME ();
4707 NSEvent *theEvent = [NSApp currentEvent];
4708
4709 if (!emacs_event)
4710 return NO;
4711
4712 emacs_event->kind = NS_NONKEY_EVENT;
4713 emacs_event->code = KEY_NS_SPI_SERVICE_CALL;
4714 ns_input_spi_name = build_string ([name UTF8String]);
4715 ns_input_spi_arg = build_string ([arg UTF8String]);
4716 emacs_event->modifiers = EV_MODIFIERS (theEvent);
4717 EV_TRAILER (theEvent);
4718
4719 return YES;
4720 }
4721
4722
4723 @end /* EmacsApp */
4724
4725
4726
4727 /* ==========================================================================
4728
4729 EmacsView implementation
4730
4731 ========================================================================== */
4732
4733
4734 @implementation EmacsView
4735
4736 /* needed to inform when window closed from LISP */
4737 - (void) setWindowClosing: (BOOL)closing
4738 {
4739 windowClosing = closing;
4740 }
4741
4742
4743 - (void)dealloc
4744 {
4745 NSTRACE (EmacsView_dealloc);
4746 [toolbar release];
4747 [super dealloc];
4748 }
4749
4750
4751 /* called on font panel selection */
4752 - (void)changeFont: (id)sender
4753 {
4754 NSEvent *e =[[self window] currentEvent];
4755 struct face *face =FRAME_DEFAULT_FACE (emacsframe);
4756 id newFont;
4757 float size;
4758
4759 NSTRACE (changeFont);
4760 if (!emacs_event)
4761 return;
4762
4763 if ((newFont = [sender convertFont:
4764 ((struct nsfont_info *)face->font)->nsfont]))
4765 {
4766 SET_FRAME_GARBAGED (emacsframe); /* now needed as of 2008/10 */
4767
4768 emacs_event->kind = NS_NONKEY_EVENT;
4769 emacs_event->modifiers = 0;
4770 emacs_event->code = KEY_NS_CHANGE_FONT;
4771
4772 size = [newFont pointSize];
4773 ns_input_fontsize = make_number (lrint (size));
4774 ns_input_font = build_string ([[newFont familyName] UTF8String]);
4775 EV_TRAILER (e);
4776 }
4777 }
4778
4779
4780 - (BOOL)acceptsFirstResponder
4781 {
4782 NSTRACE (acceptsFirstResponder);
4783 return YES;
4784 }
4785
4786
4787 - (void)resetCursorRects
4788 {
4789 NSRect visible = [self visibleRect];
4790 NSCursor *currentCursor = FRAME_POINTER_TYPE (emacsframe);
4791 NSTRACE (resetCursorRects);
4792
4793 if (currentCursor == nil)
4794 currentCursor = [NSCursor arrowCursor];
4795
4796 if (!NSIsEmptyRect (visible))
4797 [self addCursorRect: visible cursor: currentCursor];
4798 [currentCursor setOnMouseEntered: YES];
4799 }
4800
4801
4802
4803 /*****************************************************************************/
4804 /* Keyboard handling. */
4805 #define NS_KEYLOG 0
4806
4807 - (void)keyDown: (NSEvent *)theEvent
4808 {
4809 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
4810 int code;
4811 unsigned fnKeysym = 0;
4812 int flags;
4813 static NSMutableArray *nsEvArray;
4814 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
4815 static BOOL firstTime = YES;
4816 #endif
4817 int left_is_none;
4818
4819 NSTRACE (keyDown);
4820
4821 /* Rhapsody and OS X give up and down events for the arrow keys */
4822 if (ns_fake_keydown == YES)
4823 ns_fake_keydown = NO;
4824 else if ([theEvent type] != NSKeyDown)
4825 return;
4826
4827 if (!emacs_event)
4828 return;
4829
4830 if (![[self window] isKeyWindow]
4831 && [[theEvent window] isKindOfClass: [EmacsWindow class]]
4832 /* we must avoid an infinite loop here. */
4833 && (EmacsView *)[[theEvent window] delegate] != self)
4834 {
4835 /* XXX: There is an occasional condition in which, when Emacs display
4836 updates a different frame from the current one, and temporarily
4837 selects it, then processes some interrupt-driven input
4838 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
4839 for some reason that window has its first responder set to the NSView
4840 most recently updated (I guess), which is not the correct one. */
4841 [(EmacsView *)[[theEvent window] delegate] keyDown: theEvent];
4842 return;
4843 }
4844
4845 if (nsEvArray == nil)
4846 nsEvArray = [[NSMutableArray alloc] initWithCapacity: 1];
4847
4848 [NSCursor setHiddenUntilMouseMoves: YES];
4849
4850 if (hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
4851 {
4852 clear_mouse_face (hlinfo);
4853 hlinfo->mouse_face_hidden = 1;
4854 }
4855
4856 if (!processingCompose)
4857 {
4858 /* When using screen sharing, no left or right information is sent,
4859 so use Left key in those cases. */
4860 int is_left_key, is_right_key;
4861
4862 code = ([[theEvent charactersIgnoringModifiers] length] == 0) ?
4863 0 : [[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
4864
4865 /* (Carbon way: [theEvent keyCode]) */
4866
4867 /* is it a "function key"? */
4868 fnKeysym = ns_convert_key (code);
4869 if (fnKeysym)
4870 {
4871 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
4872 because Emacs treats Delete and KP-Delete same (in simple.el). */
4873 if (fnKeysym == 0xFFFF && [theEvent keyCode] == 0x33)
4874 code = 0xFF08; /* backspace */
4875 else
4876 code = fnKeysym;
4877 }
4878
4879 /* are there modifiers? */
4880 emacs_event->modifiers = 0;
4881 flags = [theEvent modifierFlags];
4882
4883 if (flags & NSHelpKeyMask)
4884 emacs_event->modifiers |= hyper_modifier;
4885
4886 if (flags & NSShiftKeyMask)
4887 emacs_event->modifiers |= shift_modifier;
4888
4889 is_right_key = (flags & NSRightCommandKeyMask) == NSRightCommandKeyMask;
4890 is_left_key = (flags & NSLeftCommandKeyMask) == NSLeftCommandKeyMask
4891 || (! is_right_key && (flags & NSCommandKeyMask) == NSCommandKeyMask);
4892
4893 if (is_right_key)
4894 emacs_event->modifiers |= parse_solitary_modifier
4895 (EQ (ns_right_command_modifier, Qleft)
4896 ? ns_command_modifier
4897 : ns_right_command_modifier);
4898
4899 if (is_left_key)
4900 {
4901 emacs_event->modifiers |= parse_solitary_modifier
4902 (ns_command_modifier);
4903
4904 /* if super (default), take input manager's word so things like
4905 dvorak / qwerty layout work */
4906 if (EQ (ns_command_modifier, Qsuper)
4907 && !fnKeysym
4908 && [[theEvent characters] length] != 0)
4909 {
4910 /* XXX: the code we get will be unshifted, so if we have
4911 a shift modifier, must convert ourselves */
4912 if (!(flags & NSShiftKeyMask))
4913 code = [[theEvent characters] characterAtIndex: 0];
4914 #if 0
4915 /* this is ugly and also requires linking w/Carbon framework
4916 (for LMGetKbdType) so for now leave this rare (?) case
4917 undealt with.. in future look into CGEvent methods */
4918 else
4919 {
4920 long smv = GetScriptManagerVariable (smKeyScript);
4921 Handle uchrHandle = GetResource
4922 ('uchr', GetScriptVariable (smv, smScriptKeys));
4923 UInt32 dummy = 0;
4924 UCKeyTranslate ((UCKeyboardLayout*)*uchrHandle,
4925 [[theEvent characters] characterAtIndex: 0],
4926 kUCKeyActionDisplay,
4927 (flags & ~NSCommandKeyMask) >> 8,
4928 LMGetKbdType (), kUCKeyTranslateNoDeadKeysMask,
4929 &dummy, 1, &dummy, &code);
4930 code &= 0xFF;
4931 }
4932 #endif
4933 }
4934 }
4935
4936 is_right_key = (flags & NSRightControlKeyMask) == NSRightControlKeyMask;
4937 is_left_key = (flags & NSLeftControlKeyMask) == NSLeftControlKeyMask
4938 || (! is_right_key && (flags & NSControlKeyMask) == NSControlKeyMask);
4939
4940 if (is_right_key)
4941 emacs_event->modifiers |= parse_solitary_modifier
4942 (EQ (ns_right_control_modifier, Qleft)
4943 ? ns_control_modifier
4944 : ns_right_control_modifier);
4945
4946 if (is_left_key)
4947 emacs_event->modifiers |= parse_solitary_modifier
4948 (ns_control_modifier);
4949
4950 if (flags & NS_FUNCTION_KEY_MASK && !fnKeysym)
4951 emacs_event->modifiers |=
4952 parse_solitary_modifier (ns_function_modifier);
4953
4954 left_is_none = NILP (ns_alternate_modifier)
4955 || EQ (ns_alternate_modifier, Qnone);
4956
4957 is_right_key = (flags & NSRightAlternateKeyMask)
4958 == NSRightAlternateKeyMask;
4959 is_left_key = (flags & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask
4960 || (! is_right_key
4961 && (flags & NSAlternateKeyMask) == NSAlternateKeyMask);
4962
4963 if (is_right_key)
4964 {
4965 if ((NILP (ns_right_alternate_modifier)
4966 || EQ (ns_right_alternate_modifier, Qnone)
4967 || (EQ (ns_right_alternate_modifier, Qleft) && left_is_none))
4968 && !fnKeysym)
4969 { /* accept pre-interp alt comb */
4970 if ([[theEvent characters] length] > 0)
4971 code = [[theEvent characters] characterAtIndex: 0];
4972 /*HACK: clear lone shift modifier to stop next if from firing */
4973 if (emacs_event->modifiers == shift_modifier)
4974 emacs_event->modifiers = 0;
4975 }
4976 else
4977 emacs_event->modifiers |= parse_solitary_modifier
4978 (EQ (ns_right_alternate_modifier, Qleft)
4979 ? ns_alternate_modifier
4980 : ns_right_alternate_modifier);
4981 }
4982
4983 if (is_left_key) /* default = meta */
4984 {
4985 if (left_is_none && !fnKeysym)
4986 { /* accept pre-interp alt comb */
4987 if ([[theEvent characters] length] > 0)
4988 code = [[theEvent characters] characterAtIndex: 0];
4989 /*HACK: clear lone shift modifier to stop next if from firing */
4990 if (emacs_event->modifiers == shift_modifier)
4991 emacs_event->modifiers = 0;
4992 }
4993 else
4994 emacs_event->modifiers |=
4995 parse_solitary_modifier (ns_alternate_modifier);
4996 }
4997
4998 if (NS_KEYLOG)
4999 fprintf (stderr, "keyDown: code =%x\tfnKey =%x\tflags = %x\tmods = %x\n",
5000 code, fnKeysym, flags, emacs_event->modifiers);
5001
5002 /* if it was a function key or had modifiers, pass it directly to emacs */
5003 if (fnKeysym || (emacs_event->modifiers
5004 && (emacs_event->modifiers != shift_modifier)
5005 && [[theEvent charactersIgnoringModifiers] length] > 0))
5006 /*[[theEvent characters] length] */
5007 {
5008 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
5009 if (code < 0x20)
5010 code |= (1<<28)|(3<<16);
5011 else if (code == 0x7f)
5012 code |= (1<<28)|(3<<16);
5013 else if (!fnKeysym)
5014 emacs_event->kind = code > 0xFF
5015 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
5016
5017 emacs_event->code = code;
5018 EV_TRAILER (theEvent);
5019 return;
5020 }
5021 }
5022
5023
5024 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
5025 /* if we get here we should send the key for input manager processing */
5026 if (firstTime && [[NSInputManager currentInputManager]
5027 wantsToDelayTextChangeNotifications] == NO)
5028 fprintf (stderr,
5029 "Emacs: WARNING: TextInput mgr wants marked text to be permanent!\n");
5030 firstTime = NO;
5031 #endif
5032 if (NS_KEYLOG && !processingCompose)
5033 fprintf (stderr, "keyDown: Begin compose sequence.\n");
5034
5035 processingCompose = YES;
5036 [nsEvArray addObject: theEvent];
5037 [self interpretKeyEvents: nsEvArray];
5038 [nsEvArray removeObject: theEvent];
5039 }
5040
5041
5042 #ifdef NS_IMPL_COCOA
5043 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
5044 decided not to send key-down for.
5045 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
5046 This only applies on Tiger and earlier.
5047 If it matches one of these, send it on to keyDown. */
5048 -(void)keyUp: (NSEvent *)theEvent
5049 {
5050 int flags = [theEvent modifierFlags];
5051 int code = [theEvent keyCode];
5052 if (floor (NSAppKitVersionNumber) <= 824 /*NSAppKitVersionNumber10_4*/ &&
5053 code == 0x30 && (flags & NSControlKeyMask) && !(flags & NSCommandKeyMask))
5054 {
5055 if (NS_KEYLOG)
5056 fprintf (stderr, "keyUp: passed test");
5057 ns_fake_keydown = YES;
5058 [self keyDown: theEvent];
5059 }
5060 }
5061 #endif
5062
5063
5064 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
5065
5066
5067 /* <NSTextInput>: called when done composing;
5068 NOTE: also called when we delete over working text, followed immed.
5069 by doCommandBySelector: deleteBackward: */
5070 - (void)insertText: (id)aString
5071 {
5072 int code;
5073 int len = [(NSString *)aString length];
5074 int i;
5075
5076 if (NS_KEYLOG)
5077 NSLog (@"insertText '%@'\tlen = %d", aString, len);
5078 processingCompose = NO;
5079
5080 if (!emacs_event)
5081 return;
5082
5083 /* first, clear any working text */
5084 if (workingText != nil)
5085 [self deleteWorkingText];
5086
5087 /* now insert the string as keystrokes */
5088 for (i =0; i<len; i++)
5089 {
5090 code = [aString characterAtIndex: i];
5091 /* TODO: still need this? */
5092 if (code == 0x2DC)
5093 code = '~'; /* 0x7E */
5094 emacs_event->modifiers = 0;
5095 emacs_event->kind
5096 = code > 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
5097 emacs_event->code = code;
5098 EV_TRAILER ((id)nil);
5099 }
5100 }
5101
5102
5103 /* <NSTextInput>: inserts display of composing characters */
5104 - (void)setMarkedText: (id)aString selectedRange: (NSRange)selRange
5105 {
5106 NSString *str = [aString respondsToSelector: @selector (string)] ?
5107 [aString string] : aString;
5108 if (NS_KEYLOG)
5109 NSLog (@"setMarkedText '%@' len =%d range %d from %d", str, [str length],
5110 selRange.length, selRange.location);
5111
5112 if (workingText != nil)
5113 [self deleteWorkingText];
5114 if ([str length] == 0)
5115 return;
5116
5117 if (!emacs_event)
5118 return;
5119
5120 processingCompose = YES;
5121 workingText = [str copy];
5122 ns_working_text = build_string ([workingText UTF8String]);
5123
5124 emacs_event->kind = NS_TEXT_EVENT;
5125 emacs_event->code = KEY_NS_PUT_WORKING_TEXT;
5126 EV_TRAILER ((id)nil);
5127 }
5128
5129
5130 /* delete display of composing characters [not in <NSTextInput>] */
5131 - (void)deleteWorkingText
5132 {
5133 if (workingText == nil)
5134 return;
5135 if (NS_KEYLOG)
5136 NSLog(@"deleteWorkingText len =%d\n", [workingText length]);
5137 [workingText release];
5138 workingText = nil;
5139 processingCompose = NO;
5140
5141 if (!emacs_event)
5142 return;
5143
5144 emacs_event->kind = NS_TEXT_EVENT;
5145 emacs_event->code = KEY_NS_UNPUT_WORKING_TEXT;
5146 EV_TRAILER ((id)nil);
5147 }
5148
5149
5150 - (BOOL)hasMarkedText
5151 {
5152 return workingText != nil;
5153 }
5154
5155
5156 - (NSRange)markedRange
5157 {
5158 NSRange rng = workingText != nil
5159 ? NSMakeRange (0, [workingText length]) : NSMakeRange (NSNotFound, 0);
5160 if (NS_KEYLOG)
5161 NSLog (@"markedRange request");
5162 return rng;
5163 }
5164
5165
5166 - (void)unmarkText
5167 {
5168 if (NS_KEYLOG)
5169 NSLog (@"unmark (accept) text");
5170 [self deleteWorkingText];
5171 processingCompose = NO;
5172 }
5173
5174
5175 /* used to position char selection windows, etc. */
5176 - (NSRect)firstRectForCharacterRange: (NSRange)theRange
5177 {
5178 NSRect rect;
5179 NSPoint pt;
5180 struct window *win = XWINDOW (FRAME_SELECTED_WINDOW (emacsframe));
5181 if (NS_KEYLOG)
5182 NSLog (@"firstRectForCharRange request");
5183
5184 rect.size.width = theRange.length * FRAME_COLUMN_WIDTH (emacsframe);
5185 rect.size.height = FRAME_LINE_HEIGHT (emacsframe);
5186 pt.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (win, win->phys_cursor.x);
5187 pt.y = WINDOW_TO_FRAME_PIXEL_Y (win, win->phys_cursor.y
5188 +FRAME_LINE_HEIGHT (emacsframe));
5189
5190 pt = [self convertPoint: pt toView: nil];
5191 pt = [[self window] convertBaseToScreen: pt];
5192 rect.origin = pt;
5193 return rect;
5194 }
5195
5196
5197 - (NSInteger)conversationIdentifier
5198 {
5199 return (NSInteger)self;
5200 }
5201
5202
5203 - (void)doCommandBySelector: (SEL)aSelector
5204 {
5205 if (NS_KEYLOG)
5206 NSLog (@"doCommandBySelector: %@", NSStringFromSelector (aSelector));
5207
5208 if (aSelector == @selector (deleteBackward:))
5209 {
5210 /* happens when user backspaces over an ongoing composition:
5211 throw a 'delete' into the event queue */
5212 if (!emacs_event)
5213 return;
5214 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
5215 emacs_event->code = 0xFF08;
5216 EV_TRAILER ((id)nil);
5217 }
5218 }
5219
5220 - (NSArray *)validAttributesForMarkedText
5221 {
5222 static NSArray *arr = nil;
5223 if (arr == nil) arr = [NSArray new];
5224 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
5225 return arr;
5226 }
5227
5228 - (NSRange)selectedRange
5229 {
5230 if (NS_KEYLOG)
5231 NSLog (@"selectedRange request");
5232 return NSMakeRange (NSNotFound, 0);
5233 }
5234
5235 - (NSUInteger)characterIndexForPoint: (NSPoint)thePoint
5236 {
5237 if (NS_KEYLOG)
5238 NSLog (@"characterIndexForPoint request");
5239 return 0;
5240 }
5241
5242 - (NSAttributedString *)attributedSubstringFromRange: (NSRange)theRange
5243 {
5244 static NSAttributedString *str = nil;
5245 if (str == nil) str = [NSAttributedString new];
5246 if (NS_KEYLOG)
5247 NSLog (@"attributedSubstringFromRange request");
5248 return str;
5249 }
5250
5251 /* End <NSTextInput> impl. */
5252 /*****************************************************************************/
5253
5254
5255 /* This is what happens when the user presses a mouse button. */
5256 - (void)mouseDown: (NSEvent *)theEvent
5257 {
5258 NSPoint p = [self convertPoint: [theEvent locationInWindow] fromView: nil];
5259
5260 NSTRACE (mouseDown);
5261
5262 [self deleteWorkingText];
5263
5264 if (!emacs_event)
5265 return;
5266
5267 last_mouse_frame = emacsframe;
5268 /* appears to be needed to prevent spurious movement events generated on
5269 button clicks */
5270 last_mouse_frame->mouse_moved = 0;
5271
5272 if ([theEvent type] == NSScrollWheel)
5273 {
5274 float delta = [theEvent deltaY];
5275 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
5276 if (delta == 0)
5277 return;
5278 emacs_event->kind = WHEEL_EVENT;
5279 emacs_event->code = 0;
5280 emacs_event->modifiers = EV_MODIFIERS (theEvent) |
5281 ((delta > 0) ? up_modifier : down_modifier);
5282 }
5283 else
5284 {
5285 emacs_event->kind = MOUSE_CLICK_EVENT;
5286 emacs_event->code = EV_BUTTON (theEvent);
5287 emacs_event->modifiers = EV_MODIFIERS (theEvent)
5288 | EV_UDMODIFIERS (theEvent);
5289 }
5290 XSETINT (emacs_event->x, lrint (p.x));
5291 XSETINT (emacs_event->y, lrint (p.y));
5292 EV_TRAILER (theEvent);
5293 }
5294
5295
5296 - (void)rightMouseDown: (NSEvent *)theEvent
5297 {
5298 NSTRACE (rightMouseDown);
5299 [self mouseDown: theEvent];
5300 }
5301
5302
5303 - (void)otherMouseDown: (NSEvent *)theEvent
5304 {
5305 NSTRACE (otherMouseDown);
5306 [self mouseDown: theEvent];
5307 }
5308
5309
5310 - (void)mouseUp: (NSEvent *)theEvent
5311 {
5312 NSTRACE (mouseUp);
5313 [self mouseDown: theEvent];
5314 }
5315
5316
5317 - (void)rightMouseUp: (NSEvent *)theEvent
5318 {
5319 NSTRACE (rightMouseUp);
5320 [self mouseDown: theEvent];
5321 }
5322
5323
5324 - (void)otherMouseUp: (NSEvent *)theEvent
5325 {
5326 NSTRACE (otherMouseUp);
5327 [self mouseDown: theEvent];
5328 }
5329
5330
5331 - (void) scrollWheel: (NSEvent *)theEvent
5332 {
5333 NSTRACE (scrollWheel);
5334 [self mouseDown: theEvent];
5335 }
5336
5337
5338 /* Tell emacs the mouse has moved. */
5339 - (void)mouseMoved: (NSEvent *)e
5340 {
5341 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
5342 Lisp_Object frame;
5343
5344 // NSTRACE (mouseMoved);
5345
5346 last_mouse_movement_time = EV_TIMESTAMP (e);
5347 last_mouse_motion_position
5348 = [self convertPoint: [e locationInWindow] fromView: nil];
5349
5350 /* update any mouse face */
5351 if (hlinfo->mouse_face_hidden)
5352 {
5353 hlinfo->mouse_face_hidden = 0;
5354 clear_mouse_face (hlinfo);
5355 }
5356
5357 /* tooltip handling */
5358 previous_help_echo_string = help_echo_string;
5359 help_echo_string = Qnil;
5360
5361 if (!note_mouse_movement (emacsframe, last_mouse_motion_position.x,
5362 last_mouse_motion_position.y))
5363 help_echo_string = previous_help_echo_string;
5364
5365 XSETFRAME (frame, emacsframe);
5366 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
5367 {
5368 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
5369 (note_mouse_highlight), which is called through the
5370 note_mouse_movement () call above */
5371 gen_help_event (help_echo_string, frame, help_echo_window,
5372 help_echo_object, help_echo_pos);
5373 }
5374 else
5375 {
5376 help_echo_string = Qnil;
5377 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5378 }
5379
5380 if (emacsframe->mouse_moved && send_appdefined)
5381 ns_send_appdefined (-1);
5382 }
5383
5384
5385 - (void)mouseDragged: (NSEvent *)e
5386 {
5387 NSTRACE (mouseDragged);
5388 [self mouseMoved: e];
5389 }
5390
5391
5392 - (void)rightMouseDragged: (NSEvent *)e
5393 {
5394 NSTRACE (rightMouseDragged);
5395 [self mouseMoved: e];
5396 }
5397
5398
5399 - (void)otherMouseDragged: (NSEvent *)e
5400 {
5401 NSTRACE (otherMouseDragged);
5402 [self mouseMoved: e];
5403 }
5404
5405
5406 - (BOOL)windowShouldClose: (id)sender
5407 {
5408 NSEvent *e =[[self window] currentEvent];
5409
5410 NSTRACE (windowShouldClose);
5411 windowClosing = YES;
5412 if (!emacs_event)
5413 return NO;
5414 emacs_event->kind = DELETE_WINDOW_EVENT;
5415 emacs_event->modifiers = 0;
5416 emacs_event->code = 0;
5417 EV_TRAILER (e);
5418 /* Don't close this window, let this be done from lisp code. */
5419 return NO;
5420 }
5421
5422
5423 - (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
5424 /* normalize frame to gridded text size */
5425 {
5426 NSTRACE (windowWillResize);
5427 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5428
5429 cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (emacsframe,
5430 #ifdef NS_IMPL_GNUSTEP
5431 frameSize.width + 3);
5432 #else
5433 frameSize.width);
5434 #endif
5435 if (cols < MINWIDTH)
5436 cols = MINWIDTH;
5437
5438 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (emacsframe, frameSize.height
5439 #ifdef NS_IMPL_GNUSTEP
5440 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe) + 3
5441 - FRAME_TOOLBAR_HEIGHT (emacsframe));
5442 #else
5443 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
5444 - FRAME_TOOLBAR_HEIGHT (emacsframe));
5445 #endif
5446 if (rows < MINHEIGHT)
5447 rows = MINHEIGHT;
5448 #ifdef NS_IMPL_COCOA
5449 {
5450 /* this sets window title to have size in it; the wm does this under GS */
5451 NSRect r = [[self window] frame];
5452 if (r.size.height == frameSize.height && r.size.width == frameSize.width)
5453 {
5454 if (old_title != 0)
5455 {
5456 xfree (old_title);
5457 old_title = 0;
5458 }
5459 }
5460 else
5461 {
5462 char *size_title;
5463 NSWindow *window = [self window];
5464 if (old_title == 0)
5465 {
5466 const char *t = [[[self window] title] UTF8String];
5467 char *pos = strstr (t, " — ");
5468 if (pos)
5469 *pos = '\0';
5470 old_title = xstrdup (t);
5471 }
5472 size_title = xmalloc (strlen (old_title) + 40);
5473 esprintf (size_title, "%s — (%d x %d)", old_title, cols, rows);
5474 [window setTitle: [NSString stringWithUTF8String: size_title]];
5475 [window display];
5476 xfree (size_title);
5477 }
5478 }
5479 #endif /* NS_IMPL_COCOA */
5480 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5481
5482 return frameSize;
5483 }
5484
5485
5486 - (void)windowDidResize: (NSNotification *)notification
5487 {
5488 #ifdef NS_IMPL_GNUSTEP
5489 NSWindow *theWindow = [notification object];
5490
5491 /* in GNUstep, at least currently, it's possible to get a didResize
5492 without getting a willResize.. therefore we need to act as if we got
5493 the willResize now */
5494 NSSize sz = [theWindow frame].size;
5495 sz = [self windowWillResize: theWindow toSize: sz];
5496 #endif /* NS_IMPL_GNUSTEP */
5497
5498 NSTRACE (windowDidResize);
5499 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5500
5501 #ifdef NS_IMPL_COCOA
5502 if (old_title != 0)
5503 {
5504 xfree (old_title);
5505 old_title = 0;
5506 }
5507 #endif /* NS_IMPL_COCOA */
5508
5509 /* Avoid loop under GNUstep due to call at beginning of this function.
5510 (x_set_window_size causes a resize which causes
5511 a "windowDidResize" which calls x_set_window_size). */
5512 #ifndef NS_IMPL_GNUSTEP
5513 if (cols > 0 && rows > 0)
5514 {
5515 if (ns_in_resize)
5516 x_set_window_size (emacsframe, 0, cols, rows);
5517 else
5518 {
5519 NSWindow *window = [self window];
5520 NSRect wr = [window frame];
5521 FRAME_PIXEL_WIDTH (emacsframe) = (int)wr.size.width
5522 - emacsframe->border_width;
5523 FRAME_PIXEL_HEIGHT (emacsframe) = (int)wr.size.height
5524 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
5525 - FRAME_TOOLBAR_HEIGHT (emacsframe);
5526 change_frame_size (emacsframe, rows, cols, 0, 0, 1);
5527 SET_FRAME_GARBAGED (emacsframe);
5528 cancel_mouse_face (emacsframe);
5529 }
5530 }
5531 #endif
5532
5533 ns_send_appdefined (-1);
5534 }
5535
5536
5537 - (void)windowDidBecomeKey: (NSNotification *)notification
5538 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5539 {
5540 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (emacsframe);
5541 struct frame *old_focus = dpyinfo->x_focus_frame;
5542
5543 NSTRACE (windowDidBecomeKey);
5544
5545 if (emacsframe != old_focus)
5546 dpyinfo->x_focus_frame = emacsframe;
5547
5548 ns_frame_rehighlight (emacsframe);
5549
5550 if (emacs_event)
5551 {
5552 emacs_event->kind = FOCUS_IN_EVENT;
5553 EV_TRAILER ((id)nil);
5554 }
5555 }
5556
5557
5558 - (void)windowDidResignKey: (NSNotification *)notification
5559 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5560 {
5561 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (emacsframe);
5562 NSTRACE (windowDidResignKey);
5563
5564 if (dpyinfo->x_focus_frame == emacsframe)
5565 dpyinfo->x_focus_frame = 0;
5566
5567 ns_frame_rehighlight (emacsframe);
5568
5569 /* FIXME: for some reason needed on second and subsequent clicks away
5570 from sole-frame Emacs to get hollow box to show */
5571 if (!windowClosing && [[self window] isVisible] == YES)
5572 {
5573 x_update_cursor (emacsframe, 1);
5574 x_set_frame_alpha (emacsframe);
5575 }
5576
5577 if (emacs_event)
5578 {
5579 [self deleteWorkingText];
5580 emacs_event->kind = FOCUS_IN_EVENT;
5581 EV_TRAILER ((id)nil);
5582 }
5583 }
5584
5585
5586 - (void)windowWillMiniaturize: sender
5587 {
5588 NSTRACE (windowWillMiniaturize);
5589 }
5590
5591
5592 - (BOOL)isFlipped
5593 {
5594 return YES;
5595 }
5596
5597
5598 - (BOOL)isOpaque
5599 {
5600 return NO;
5601 }
5602
5603
5604 - initFrameFromEmacs: (struct frame *)f
5605 {
5606 NSRect r, wr;
5607 Lisp_Object tem;
5608 NSWindow *win;
5609 NSButton *toggleButton;
5610 NSSize sz;
5611 NSColor *col;
5612 NSString *name;
5613
5614 NSTRACE (initFrameFromEmacs);
5615
5616 windowClosing = NO;
5617 processingCompose = NO;
5618 scrollbarsNeedingUpdate = 0;
5619
5620 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5621
5622 ns_userRect = NSMakeRect (0, 0, 0, 0);
5623 r = NSMakeRect (0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, f->text_cols),
5624 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, f->text_lines));
5625 [self initWithFrame: r];
5626 [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
5627
5628 FRAME_NS_VIEW (f) = self;
5629 emacsframe = f;
5630 old_title = 0;
5631
5632 win = [[EmacsWindow alloc]
5633 initWithContentRect: r
5634 styleMask: (NSResizableWindowMask |
5635 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
5636 NSTitledWindowMask |
5637 #endif
5638 NSMiniaturizableWindowMask |
5639 NSClosableWindowMask)
5640 backing: NSBackingStoreBuffered
5641 defer: YES];
5642
5643 wr = [win frame];
5644 f->border_width = wr.size.width - r.size.width;
5645 FRAME_NS_TITLEBAR_HEIGHT (f) = wr.size.height - r.size.height;
5646
5647 [win setAcceptsMouseMovedEvents: YES];
5648 [win setDelegate: self];
5649 [win useOptimizedDrawing: YES];
5650
5651 sz.width = FRAME_COLUMN_WIDTH (f);
5652 sz.height = FRAME_LINE_HEIGHT (f);
5653 [win setResizeIncrements: sz];
5654
5655 [[win contentView] addSubview: self];
5656
5657 if (ns_drag_types)
5658 [self registerForDraggedTypes: ns_drag_types];
5659
5660 tem = f->name;
5661 name = [NSString stringWithUTF8String:
5662 NILP (tem) ? "Emacs" : SSDATA (tem)];
5663 [win setTitle: name];
5664
5665 /* toolbar support */
5666 toolbar = [[EmacsToolbar alloc] initForView: self withIdentifier:
5667 [NSString stringWithFormat: @"Emacs Frame %d",
5668 ns_window_num]];
5669 [win setToolbar: toolbar];
5670 [toolbar setVisible: NO];
5671 #ifdef NS_IMPL_COCOA
5672 toggleButton = [win standardWindowButton: NSWindowToolbarButton];
5673 [toggleButton setTarget: self];
5674 [toggleButton setAction: @selector (toggleToolbar: )];
5675 #endif
5676 FRAME_TOOLBAR_HEIGHT (f) = 0;
5677
5678 tem = f->icon_name;
5679 if (!NILP (tem))
5680 [win setMiniwindowTitle:
5681 [NSString stringWithUTF8String: SSDATA (tem)]];
5682
5683 {
5684 NSScreen *screen = [win screen];
5685
5686 if (screen != 0)
5687 [win setFrameTopLeftPoint: NSMakePoint
5688 (IN_BOUND (-SCREENMAX, f->left_pos, SCREENMAX),
5689 IN_BOUND (-SCREENMAX,
5690 [screen frame].size.height - NS_TOP_POS (f), SCREENMAX))];
5691 }
5692
5693 [win makeFirstResponder: self];
5694
5695 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
5696 (FRAME_DEFAULT_FACE (emacsframe)), emacsframe);
5697 [win setBackgroundColor: col];
5698 if ([col alphaComponent] != 1.0)
5699 [win setOpaque: NO];
5700
5701 [self allocateGState];
5702
5703 [NSApp registerServicesMenuSendTypes: ns_send_types
5704 returnTypes: nil];
5705
5706 ns_window_num++;
5707 return self;
5708 }
5709
5710
5711 - (void)windowDidMove: sender
5712 {
5713 NSWindow *win = [self window];
5714 NSRect r = [win frame];
5715 NSArray *screens = [NSScreen screens];
5716 NSScreen *screen = [screens objectAtIndex: 0];
5717
5718 NSTRACE (windowDidMove);
5719
5720 if (!emacsframe->output_data.ns)
5721 return;
5722 if (screen != nil)
5723 {
5724 emacsframe->left_pos = r.origin.x;
5725 emacsframe->top_pos =
5726 [screen frame].size.height - (r.origin.y + r.size.height);
5727 }
5728 }
5729
5730
5731 /* Called AFTER method below, but before our windowWillResize call there leads
5732 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
5733 location so set_window_size moves the frame. */
5734 - (BOOL)windowShouldZoom: (NSWindow *)sender toFrame: (NSRect)newFrame
5735 {
5736 emacsframe->output_data.ns->zooming = 1;
5737 return YES;
5738 }
5739
5740
5741 /* Override to do something slightly nonstandard, but nice. First click on
5742 zoom button will zoom vertically. Second will zoom completely. Third
5743 returns to original. */
5744 - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender
5745 defaultFrame:(NSRect)defaultFrame
5746 {
5747 NSRect result = [sender frame];
5748
5749 NSTRACE (windowWillUseStandardFrame);
5750
5751 if (abs (defaultFrame.size.height - result.size.height)
5752 > FRAME_LINE_HEIGHT (emacsframe))
5753 {
5754 /* first click */
5755 ns_userRect = result;
5756 result.size.height = defaultFrame.size.height;
5757 result.origin.y = defaultFrame.origin.y;
5758 }
5759 else
5760 {
5761 if (abs (defaultFrame.size.width - result.size.width)
5762 > FRAME_COLUMN_WIDTH (emacsframe))
5763 result = defaultFrame; /* second click */
5764 else
5765 {
5766 /* restore */
5767 result = ns_userRect.size.height ? ns_userRect : result;
5768 ns_userRect = NSMakeRect (0, 0, 0, 0);
5769 }
5770 }
5771
5772 [self windowWillResize: sender toSize: result.size];
5773 return result;
5774 }
5775
5776
5777 - (void)windowDidDeminiaturize: sender
5778 {
5779 NSTRACE (windowDidDeminiaturize);
5780 if (!emacsframe->output_data.ns)
5781 return;
5782 emacsframe->async_iconified = 0;
5783 emacsframe->async_visible = 1;
5784 windows_or_buffers_changed++;
5785
5786 if (emacs_event)
5787 {
5788 emacs_event->kind = ICONIFY_EVENT;
5789 EV_TRAILER ((id)nil);
5790 }
5791 }
5792
5793
5794 - (void)windowDidExpose: sender
5795 {
5796 NSTRACE (windowDidExpose);
5797 if (!emacsframe->output_data.ns)
5798 return;
5799 emacsframe->async_visible = 1;
5800 SET_FRAME_GARBAGED (emacsframe);
5801
5802 if (send_appdefined)
5803 ns_send_appdefined (-1);
5804 }
5805
5806
5807 - (void)windowDidMiniaturize: sender
5808 {
5809 NSTRACE (windowDidMiniaturize);
5810 if (!emacsframe->output_data.ns)
5811 return;
5812
5813 emacsframe->async_iconified = 1;
5814 emacsframe->async_visible = 0;
5815
5816 if (emacs_event)
5817 {
5818 emacs_event->kind = ICONIFY_EVENT;
5819 EV_TRAILER ((id)nil);
5820 }
5821 }
5822
5823
5824 - (void)mouseEntered: (NSEvent *)theEvent
5825 {
5826 NSTRACE (mouseEntered);
5827 last_mouse_movement_time = EV_TIMESTAMP (theEvent);
5828 }
5829
5830
5831 - (void)mouseExited: (NSEvent *)theEvent
5832 {
5833 Mouse_HLInfo *hlinfo = emacsframe ? MOUSE_HL_INFO (emacsframe) : NULL;
5834
5835 NSTRACE (mouseExited);
5836
5837 if (!hlinfo)
5838 return;
5839
5840 last_mouse_movement_time = EV_TIMESTAMP (theEvent);
5841
5842 if (emacsframe == hlinfo->mouse_face_mouse_frame)
5843 {
5844 clear_mouse_face (hlinfo);
5845 hlinfo->mouse_face_mouse_frame = 0;
5846 }
5847 }
5848
5849
5850 - menuDown: sender
5851 {
5852 NSTRACE (menuDown);
5853 if (context_menu_value == -1)
5854 context_menu_value = [sender tag];
5855 else
5856 {
5857 NSInteger tag = [sender tag];
5858 find_and_call_menu_selection (emacsframe, emacsframe->menu_bar_items_used,
5859 emacsframe->menu_bar_vector,
5860 (void *)tag);
5861 }
5862
5863 ns_send_appdefined (-1);
5864 return self;
5865 }
5866
5867
5868 - (EmacsToolbar *)toolbar
5869 {
5870 return toolbar;
5871 }
5872
5873
5874 /* this gets called on toolbar button click */
5875 - toolbarClicked: (id)item
5876 {
5877 NSEvent *theEvent;
5878 int idx = [item tag] * TOOL_BAR_ITEM_NSLOTS;
5879
5880 NSTRACE (toolbarClicked);
5881
5882 if (!emacs_event)
5883 return self;
5884
5885 /* send first event (for some reason two needed) */
5886 theEvent = [[self window] currentEvent];
5887 emacs_event->kind = TOOL_BAR_EVENT;
5888 XSETFRAME (emacs_event->arg, emacsframe);
5889 EV_TRAILER (theEvent);
5890
5891 emacs_event->kind = TOOL_BAR_EVENT;
5892 /* XSETINT (emacs_event->code, 0); */
5893 emacs_event->arg = AREF (emacsframe->tool_bar_items,
5894 idx + TOOL_BAR_ITEM_KEY);
5895 emacs_event->modifiers = EV_MODIFIERS (theEvent);
5896 EV_TRAILER (theEvent);
5897 return self;
5898 }
5899
5900
5901 - toggleToolbar: (id)sender
5902 {
5903 if (!emacs_event)
5904 return self;
5905
5906 emacs_event->kind = NS_NONKEY_EVENT;
5907 emacs_event->code = KEY_NS_TOGGLE_TOOLBAR;
5908 EV_TRAILER ((id)nil);
5909 return self;
5910 }
5911
5912
5913 - (void)drawRect: (NSRect)rect
5914 {
5915 int x = NSMinX (rect), y = NSMinY (rect);
5916 int width = NSWidth (rect), height = NSHeight (rect);
5917
5918 NSTRACE (drawRect);
5919
5920 if (!emacsframe || !emacsframe->output_data.ns || ns_in_resize)
5921 return;
5922
5923 ns_clear_frame_area (emacsframe, x, y, width, height);
5924 expose_frame (emacsframe, x, y, width, height);
5925
5926 /*
5927 drawRect: may be called (at least in OS X 10.5) for invisible
5928 views as well for some reason. Thus, do not infer visibility
5929 here.
5930
5931 emacsframe->async_visible = 1;
5932 emacsframe->async_iconified = 0;
5933 */
5934 }
5935
5936
5937 /* NSDraggingDestination protocol methods. Actually this is not really a
5938 protocol, but a category of Object. O well... */
5939
5940 -(NSUInteger) draggingEntered: (id <NSDraggingInfo>) sender
5941 {
5942 NSTRACE (draggingEntered);
5943 return NSDragOperationGeneric;
5944 }
5945
5946
5947 -(BOOL)prepareForDragOperation: (id <NSDraggingInfo>) sender
5948 {
5949 return YES;
5950 }
5951
5952
5953 -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
5954 {
5955 id pb;
5956 int x, y;
5957 NSString *type;
5958 NSEvent *theEvent = [[self window] currentEvent];
5959 NSPoint position;
5960
5961 NSTRACE (performDragOperation);
5962
5963 if (!emacs_event)
5964 return NO;
5965
5966 position = [self convertPoint: [sender draggingLocation] fromView: nil];
5967 x = lrint (position.x); y = lrint (position.y);
5968
5969 pb = [sender draggingPasteboard];
5970 type = [pb availableTypeFromArray: ns_drag_types];
5971 if (type == 0)
5972 {
5973 return NO;
5974 }
5975 else if ([type isEqualToString: NSFilenamesPboardType])
5976 {
5977 NSArray *files;
5978 NSEnumerator *fenum;
5979 NSString *file;
5980
5981 if (!(files = [pb propertyListForType: type]))
5982 return NO;
5983
5984 fenum = [files objectEnumerator];
5985 while ( (file = [fenum nextObject]) )
5986 {
5987 emacs_event->kind = NS_NONKEY_EVENT;
5988 emacs_event->code = KEY_NS_DRAG_FILE;
5989 XSETINT (emacs_event->x, x);
5990 XSETINT (emacs_event->y, y);
5991 ns_input_file = append2 (ns_input_file,
5992 build_string ([file UTF8String]));
5993 emacs_event->modifiers = EV_MODIFIERS (theEvent);
5994 EV_TRAILER (theEvent);
5995 }
5996 return YES;
5997 }
5998 else if ([type isEqualToString: NSURLPboardType])
5999 {
6000 NSString *file;
6001 NSURL *fileURL;
6002
6003 if (!(fileURL = [NSURL URLFromPasteboard: pb]) ||
6004 [fileURL isFileURL] == NO)
6005 return NO;
6006
6007 file = [fileURL path];
6008 emacs_event->kind = NS_NONKEY_EVENT;
6009 emacs_event->code = KEY_NS_DRAG_FILE;
6010 XSETINT (emacs_event->x, x);
6011 XSETINT (emacs_event->y, y);
6012 ns_input_file = append2 (ns_input_file, build_string ([file UTF8String]));
6013 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6014 EV_TRAILER (theEvent);
6015 return YES;
6016 }
6017 else if ([type isEqualToString: NSStringPboardType]
6018 || [type isEqualToString: NSTabularTextPboardType])
6019 {
6020 NSString *data;
6021
6022 if (! (data = [pb stringForType: type]))
6023 return NO;
6024
6025 emacs_event->kind = NS_NONKEY_EVENT;
6026 emacs_event->code = KEY_NS_DRAG_TEXT;
6027 XSETINT (emacs_event->x, x);
6028 XSETINT (emacs_event->y, y);
6029 ns_input_text = build_string ([data UTF8String]);
6030 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6031 EV_TRAILER (theEvent);
6032 return YES;
6033 }
6034 else if ([type isEqualToString: NSColorPboardType])
6035 {
6036 NSColor *c = [NSColor colorFromPasteboard: pb];
6037 emacs_event->kind = NS_NONKEY_EVENT;
6038 emacs_event->code = KEY_NS_DRAG_COLOR;
6039 XSETINT (emacs_event->x, x);
6040 XSETINT (emacs_event->y, y);
6041 ns_input_color = ns_color_to_lisp (c);
6042 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6043 EV_TRAILER (theEvent);
6044 return YES;
6045 }
6046 else if ([type isEqualToString: NSFontPboardType])
6047 {
6048 /* impl based on GNUstep NSTextView.m */
6049 NSData *data = [pb dataForType: NSFontPboardType];
6050 NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: data];
6051 NSFont *font = [dict objectForKey: NSFontAttributeName];
6052 char fontSize[10];
6053
6054 if (font == nil)
6055 return NO;
6056
6057 emacs_event->kind = NS_NONKEY_EVENT;
6058 emacs_event->code = KEY_NS_CHANGE_FONT;
6059 XSETINT (emacs_event->x, x);
6060 XSETINT (emacs_event->y, y);
6061 ns_input_font = build_string ([[font fontName] UTF8String]);
6062 snprintf (fontSize, 10, "%f", [font pointSize]);
6063 ns_input_fontsize = build_string (fontSize);
6064 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6065 EV_TRAILER (theEvent);
6066 return YES;
6067 }
6068 else
6069 {
6070 error ("Invalid data type in dragging pasteboard.");
6071 return NO;
6072 }
6073 }
6074
6075
6076 - (id) validRequestorForSendType: (NSString *)typeSent
6077 returnType: (NSString *)typeReturned
6078 {
6079 NSTRACE (validRequestorForSendType);
6080 if (typeSent != nil && [ns_send_types indexOfObject: typeSent] != NSNotFound
6081 && typeReturned == nil)
6082 {
6083 if (! NILP (ns_get_local_selection (QPRIMARY, QUTF8_STRING)))
6084 return self;
6085 }
6086
6087 return [super validRequestorForSendType: typeSent
6088 returnType: typeReturned];
6089 }
6090
6091
6092 /* The next two methods are part of NSServicesRequests informal protocol,
6093 supposedly called when a services menu item is chosen from this app.
6094 But this should not happen because we override the services menu with our
6095 own entries which call ns-perform-service.
6096 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
6097 So let's at least stub them out until further investigation can be done. */
6098
6099 - (BOOL) readSelectionFromPasteboard: (NSPasteboard *)pb
6100 {
6101 /* we could call ns_string_from_pasteboard(pboard) here but then it should
6102 be written into the buffer in place of the existing selection..
6103 ordinary service calls go through functions defined in ns-win.el */
6104 return NO;
6105 }
6106
6107 - (BOOL) writeSelectionToPasteboard: (NSPasteboard *)pb types: (NSArray *)types
6108 {
6109 NSArray *typesDeclared;
6110 Lisp_Object val;
6111
6112 /* We only support NSStringPboardType */
6113 if ([types containsObject:NSStringPboardType] == NO) {
6114 return NO;
6115 }
6116
6117 val = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6118 if (CONSP (val) && SYMBOLP (XCAR (val)))
6119 {
6120 val = XCDR (val);
6121 if (CONSP (val) && NILP (XCDR (val)))
6122 val = XCAR (val);
6123 }
6124 if (! STRINGP (val))
6125 return NO;
6126
6127 typesDeclared = [NSArray arrayWithObject:NSStringPboardType];
6128 [pb declareTypes:typesDeclared owner:nil];
6129 ns_string_to_pasteboard (pb, val);
6130 return YES;
6131 }
6132
6133
6134 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
6135 (gives a miniaturized version of the window); currently we use the latter for
6136 frames whose active buffer doesn't correspond to any file
6137 (e.g., '*scratch*') */
6138 - setMiniwindowImage: (BOOL) setMini
6139 {
6140 id image = [[self window] miniwindowImage];
6141 NSTRACE (setMiniwindowImage);
6142
6143 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
6144 about "AppleDockIconEnabled" notwithstanding, however the set message
6145 below has its effect nonetheless. */
6146 if (image != emacsframe->output_data.ns->miniimage)
6147 {
6148 if (image && [image isKindOfClass: [EmacsImage class]])
6149 [image release];
6150 [[self window] setMiniwindowImage:
6151 setMini ? emacsframe->output_data.ns->miniimage : nil];
6152 }
6153
6154 return self;
6155 }
6156
6157
6158 - (void) setRows: (int) r andColumns: (int) c
6159 {
6160 rows = r;
6161 cols = c;
6162 }
6163
6164 @end /* EmacsView */
6165
6166
6167
6168 /* ==========================================================================
6169
6170 EmacsWindow implementation
6171
6172 ========================================================================== */
6173
6174 @implementation EmacsWindow
6175
6176 #ifdef NS_IMPL_COCOA
6177 - (id)accessibilityAttributeValue:(NSString *)attribute
6178 {
6179 Lisp_Object str = Qnil;
6180 struct frame *f = SELECTED_FRAME ();
6181 struct buffer *curbuf = XBUFFER (XWINDOW (f->selected_window)->buffer);
6182
6183 if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
6184 return NSAccessibilityTextFieldRole;
6185
6186 if ([attribute isEqualToString:NSAccessibilitySelectedTextAttribute]
6187 && curbuf && ! NILP (BVAR (curbuf, mark_active)))
6188 {
6189 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6190 }
6191 else if (curbuf && [attribute isEqualToString:NSAccessibilityValueAttribute])
6192 {
6193 if (! NILP (BVAR (curbuf, mark_active)))
6194 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6195
6196 if (NILP (str))
6197 {
6198 ptrdiff_t start_byte = BUF_BEGV_BYTE (curbuf);
6199 ptrdiff_t byte_range = BUF_ZV_BYTE (curbuf) - start_byte;
6200 ptrdiff_t range = BUF_ZV (curbuf) - BUF_BEGV (curbuf);
6201
6202 if (! NILP (BVAR (curbuf, enable_multibyte_characters)))
6203 str = make_uninit_multibyte_string (range, byte_range);
6204 else
6205 str = make_uninit_string (range);
6206 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
6207 Is this a problem? */
6208 memcpy (SDATA (str), BYTE_POS_ADDR (start_byte), byte_range);
6209 }
6210 }
6211
6212
6213 if (! NILP (str))
6214 {
6215 if (CONSP (str) && SYMBOLP (XCAR (str)))
6216 {
6217 str = XCDR (str);
6218 if (CONSP (str) && NILP (XCDR (str)))
6219 str = XCAR (str);
6220 }
6221 if (STRINGP (str))
6222 {
6223 const char *utfStr = SSDATA (str);
6224 NSString *nsStr = [NSString stringWithUTF8String: utfStr];
6225 return nsStr;
6226 }
6227 }
6228
6229 return [super accessibilityAttributeValue:attribute];
6230 }
6231 #endif /* NS_IMPL_COCOA */
6232
6233 /* If we have multiple monitors, one above the other, we don't want to
6234 restrict the height to just one monitor. So we override this. */
6235 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
6236 {
6237 /* When making the frame visible for the first time or if there is just
6238 one screen, we want to constrain. Other times not. */
6239 NSUInteger nr_screens = [[NSScreen screens] count];
6240 struct frame *f = ((EmacsView *)[self delegate])->emacsframe;
6241 NSTRACE (constrainFrameRect);
6242
6243 if (nr_screens == 1)
6244 return [super constrainFrameRect:frameRect toScreen:screen];
6245
6246 if (f->output_data.ns->dont_constrain
6247 || ns_menu_bar_should_be_hidden ())
6248 return frameRect;
6249
6250 f->output_data.ns->dont_constrain = 1;
6251 return [super constrainFrameRect:frameRect toScreen:screen];
6252 }
6253
6254
6255 /* called only on resize clicks by special case in EmacsApp-sendEvent */
6256 - (void)mouseDown: (NSEvent *)theEvent
6257 {
6258 if (ns_in_resize)
6259 {
6260 NSSize size = [[theEvent window] frame].size;
6261 grabOffset = [theEvent locationInWindow];
6262 grabOffset.x = size.width - grabOffset.x;
6263 }
6264 else
6265 [super mouseDown: theEvent];
6266 }
6267
6268
6269 /* stop resizing */
6270 - (void)mouseUp: (NSEvent *)theEvent
6271 {
6272 if (ns_in_resize)
6273 {
6274 struct frame *f = ((EmacsView *)[self delegate])->emacsframe;
6275 ns_in_resize = NO;
6276 ns_set_name_as_filename (f);
6277 [self display];
6278 ns_send_appdefined (-1);
6279 }
6280 else
6281 [super mouseUp: theEvent];
6282 }
6283
6284
6285 /* send resize events */
6286 - (void)mouseDragged: (NSEvent *)theEvent
6287 {
6288 if (ns_in_resize)
6289 {
6290 NSPoint p = [theEvent locationInWindow];
6291 NSSize size, vettedSize, origSize = [self frame].size;
6292
6293 size.width = p.x + grabOffset.x;
6294 size.height = origSize.height - p.y + grabOffset.y;
6295
6296 if (size.width == origSize.width && size.height == origSize.height)
6297 return;
6298
6299 vettedSize = [[self delegate] windowWillResize: self toSize: size];
6300 [[NSNotificationCenter defaultCenter]
6301 postNotificationName: NSWindowDidResizeNotification
6302 object: self];
6303 }
6304 else
6305 [super mouseDragged: theEvent];
6306 }
6307
6308 @end /* EmacsWindow */
6309
6310
6311 /* ==========================================================================
6312
6313 EmacsScroller implementation
6314
6315 ========================================================================== */
6316
6317
6318 @implementation EmacsScroller
6319
6320 /* for repeat button push */
6321 #define SCROLL_BAR_FIRST_DELAY 0.5
6322 #define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15)
6323
6324 + (CGFloat) scrollerWidth
6325 {
6326 /* TODO: if we want to allow variable widths, this is the place to do it,
6327 however neither GNUstep nor Cocoa support it very well */
6328 return [NSScroller scrollerWidth];
6329 }
6330
6331
6332 - initFrame: (NSRect )r window: (Lisp_Object)nwin
6333 {
6334 NSTRACE (EmacsScroller_initFrame);
6335
6336 r.size.width = [EmacsScroller scrollerWidth];
6337 [super initWithFrame: r/*NSMakeRect (0, 0, 0, 0)*/];
6338 [self setContinuous: YES];
6339 [self setEnabled: YES];
6340
6341 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
6342 locked against the top and bottom edges, and right edge on OS X, where
6343 scrollers are on right. */
6344 #ifdef NS_IMPL_GNUSTEP
6345 [self setAutoresizingMask: NSViewMaxXMargin | NSViewHeightSizable];
6346 #else
6347 [self setAutoresizingMask: NSViewMinXMargin | NSViewHeightSizable];
6348 #endif
6349
6350 win = nwin;
6351 condemned = NO;
6352 pixel_height = NSHeight (r);
6353 if (pixel_height == 0) pixel_height = 1;
6354 min_portion = 20 / pixel_height;
6355
6356 frame = XFRAME (XWINDOW (win)->frame);
6357 if (FRAME_LIVE_P (frame))
6358 {
6359 int i;
6360 EmacsView *view = FRAME_NS_VIEW (frame);
6361 NSView *sview = [[view window] contentView];
6362 NSArray *subs = [sview subviews];
6363
6364 /* disable optimization stopping redraw of other scrollbars */
6365 view->scrollbarsNeedingUpdate = 0;
6366 for (i =[subs count]-1; i >= 0; i--)
6367 if ([[subs objectAtIndex: i] isKindOfClass: [EmacsScroller class]])
6368 view->scrollbarsNeedingUpdate++;
6369 [sview addSubview: self];
6370 }
6371
6372 /* [self setFrame: r]; */
6373
6374 return self;
6375 }
6376
6377
6378 - (void)setFrame: (NSRect)newRect
6379 {
6380 NSTRACE (EmacsScroller_setFrame);
6381 /* BLOCK_INPUT; */
6382 pixel_height = NSHeight (newRect);
6383 if (pixel_height == 0) pixel_height = 1;
6384 min_portion = 20 / pixel_height;
6385 [super setFrame: newRect];
6386 [self display];
6387 /* UNBLOCK_INPUT; */
6388 }
6389
6390
6391 - (void)dealloc
6392 {
6393 NSTRACE (EmacsScroller_dealloc);
6394 if (!NILP (win))
6395 wset_vertical_scroll_bar (XWINDOW (win), Qnil);
6396 [super dealloc];
6397 }
6398
6399
6400 - condemn
6401 {
6402 NSTRACE (condemn);
6403 condemned =YES;
6404 return self;
6405 }
6406
6407
6408 - reprieve
6409 {
6410 NSTRACE (reprieve);
6411 condemned =NO;
6412 return self;
6413 }
6414
6415
6416 - judge
6417 {
6418 NSTRACE (judge);
6419 if (condemned)
6420 {
6421 EmacsView *view;
6422 BLOCK_INPUT;
6423 /* ensure other scrollbar updates after deletion */
6424 view = (EmacsView *)FRAME_NS_VIEW (frame);
6425 if (view != nil)
6426 view->scrollbarsNeedingUpdate++;
6427 [self removeFromSuperview];
6428 [self release];
6429 UNBLOCK_INPUT;
6430 }
6431 return self;
6432 }
6433
6434
6435 - (void)resetCursorRects
6436 {
6437 NSRect visible = [self visibleRect];
6438 NSTRACE (resetCursorRects);
6439
6440 if (!NSIsEmptyRect (visible))
6441 [self addCursorRect: visible cursor: [NSCursor arrowCursor]];
6442 [[NSCursor arrowCursor] setOnMouseEntered: YES];
6443 }
6444
6445
6446 - (int) checkSamePosition: (int) position portion: (int) portion
6447 whole: (int) whole
6448 {
6449 return em_position ==position && em_portion ==portion && em_whole ==whole
6450 && portion != whole; /* needed for resize empty buf */
6451 }
6452
6453
6454 - setPosition: (int)position portion: (int)portion whole: (int)whole
6455 {
6456 NSTRACE (setPosition);
6457
6458 em_position = position;
6459 em_portion = portion;
6460 em_whole = whole;
6461
6462 if (portion >= whole)
6463 {
6464 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
6465 [self setKnobProportion: 1.0];
6466 [self setDoubleValue: 1.0];
6467 #else
6468 [self setFloatValue: 0.0 knobProportion: 1.0];
6469 #endif
6470 }
6471 else
6472 {
6473 float pos, por;
6474 portion = max ((float)whole*min_portion/pixel_height, portion);
6475 pos = (float)position / (whole - portion);
6476 por = (float)portion/whole;
6477 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
6478 [self setKnobProportion: por];
6479 [self setDoubleValue: pos];
6480 #else
6481 [self setFloatValue: pos knobProportion: por];
6482 #endif
6483 }
6484 return self;
6485 }
6486
6487 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
6488 drag events will go directly to the EmacsScroller. Leaving in for now. */
6489 -(void)getMouseMotionPart: (int *)part window: (Lisp_Object *)window
6490 x: (Lisp_Object *)x y: ( Lisp_Object *)y
6491 {
6492 *part = last_hit_part;
6493 *window = win;
6494 XSETINT (*y, pixel_height);
6495 if ([self floatValue] > 0.999)
6496 XSETINT (*x, pixel_height);
6497 else
6498 XSETINT (*x, pixel_height * [self floatValue]);
6499 }
6500
6501
6502 /* set up emacs_event */
6503 - (void) sendScrollEventAtLoc: (float)loc fromEvent: (NSEvent *)e
6504 {
6505 if (!emacs_event)
6506 return;
6507
6508 emacs_event->part = last_hit_part;
6509 emacs_event->code = 0;
6510 emacs_event->modifiers = EV_MODIFIERS (e) | down_modifier;
6511 emacs_event->frame_or_window = win;
6512 emacs_event->timestamp = EV_TIMESTAMP (e);
6513 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
6514 emacs_event->arg = Qnil;
6515 XSETINT (emacs_event->x, loc * pixel_height);
6516 XSETINT (emacs_event->y, pixel_height-20);
6517
6518 if (q_event_ptr)
6519 {
6520 n_emacs_events_pending++;
6521 kbd_buffer_store_event_hold (emacs_event, q_event_ptr);
6522 }
6523 else
6524 kbd_buffer_store_event (emacs_event);
6525 EVENT_INIT (*emacs_event);
6526 ns_send_appdefined (-1);
6527 }
6528
6529
6530 /* called manually thru timer to implement repeated button action w/hold-down */
6531 - repeatScroll: (NSTimer *)scrollEntry
6532 {
6533 NSEvent *e = [[self window] currentEvent];
6534 NSPoint p = [[self window] mouseLocationOutsideOfEventStream];
6535 BOOL inKnob = [self testPart: p] == NSScrollerKnob;
6536
6537 /* clear timer if need be */
6538 if (inKnob || [scroll_repeat_entry timeInterval] == SCROLL_BAR_FIRST_DELAY)
6539 {
6540 [scroll_repeat_entry invalidate];
6541 [scroll_repeat_entry release];
6542 scroll_repeat_entry = nil;
6543
6544 if (inKnob)
6545 return self;
6546
6547 scroll_repeat_entry
6548 = [[NSTimer scheduledTimerWithTimeInterval:
6549 SCROLL_BAR_CONTINUOUS_DELAY
6550 target: self
6551 selector: @selector (repeatScroll:)
6552 userInfo: 0
6553 repeats: YES]
6554 retain];
6555 }
6556
6557 [self sendScrollEventAtLoc: 0 fromEvent: e];
6558 return self;
6559 }
6560
6561
6562 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
6563 mouseDragged events without going into a modal loop. */
6564 - (void)mouseDown: (NSEvent *)e
6565 {
6566 NSRect sr, kr;
6567 /* hitPart is only updated AFTER event is passed on */
6568 NSScrollerPart part = [self testPart: [e locationInWindow]];
6569 double inc = 0.0, loc, kloc, pos;
6570 int edge = 0;
6571
6572 NSTRACE (EmacsScroller_mouseDown);
6573
6574 switch (part)
6575 {
6576 case NSScrollerDecrementPage:
6577 last_hit_part = scroll_bar_above_handle; inc = -1.0; break;
6578 case NSScrollerIncrementPage:
6579 last_hit_part = scroll_bar_below_handle; inc = 1.0; break;
6580 case NSScrollerDecrementLine:
6581 last_hit_part = scroll_bar_up_arrow; inc = -0.1; break;
6582 case NSScrollerIncrementLine:
6583 last_hit_part = scroll_bar_down_arrow; inc = 0.1; break;
6584 case NSScrollerKnob:
6585 last_hit_part = scroll_bar_handle; break;
6586 case NSScrollerKnobSlot: /* GNUstep-only */
6587 last_hit_part = scroll_bar_move_ratio; break;
6588 default: /* NSScrollerNoPart? */
6589 fprintf (stderr, "EmacsScoller-mouseDown: unexpected part %ld\n",
6590 (long) part);
6591 return;
6592 }
6593
6594 if (inc != 0.0)
6595 {
6596 pos = 0; /* ignored */
6597
6598 /* set a timer to repeat, as we can't let superclass do this modally */
6599 scroll_repeat_entry
6600 = [[NSTimer scheduledTimerWithTimeInterval: SCROLL_BAR_FIRST_DELAY
6601 target: self
6602 selector: @selector (repeatScroll:)
6603 userInfo: 0
6604 repeats: YES]
6605 retain];
6606 }
6607 else
6608 {
6609 /* handle, or on GNUstep possibly slot */
6610 NSEvent *fake_event;
6611
6612 /* compute float loc in slot and mouse offset on knob */
6613 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
6614 toView: nil];
6615 loc = NSHeight (sr) - ([e locationInWindow].y - NSMinY (sr));
6616 if (loc <= 0.0)
6617 {
6618 loc = 0.0;
6619 edge = -1;
6620 }
6621 else if (loc >= NSHeight (sr))
6622 {
6623 loc = NSHeight (sr);
6624 edge = 1;
6625 }
6626
6627 if (edge)
6628 kloc = 0.5 * edge;
6629 else
6630 {
6631 kr = [self convertRect: [self rectForPart: NSScrollerKnob]
6632 toView: nil];
6633 kloc = NSHeight (kr) - ([e locationInWindow].y - NSMinY (kr));
6634 }
6635 last_mouse_offset = kloc;
6636
6637 /* if knob, tell emacs a location offset by knob pos
6638 (to indicate top of handle) */
6639 if (part == NSScrollerKnob)
6640 pos = (loc - last_mouse_offset) / NSHeight (sr);
6641 else
6642 /* else this is a slot click on GNUstep: go straight there */
6643 pos = loc / NSHeight (sr);
6644
6645 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
6646 fake_event = [NSEvent mouseEventWithType: NSLeftMouseUp
6647 location: [e locationInWindow]
6648 modifierFlags: [e modifierFlags]
6649 timestamp: [e timestamp]
6650 windowNumber: [e windowNumber]
6651 context: [e context]
6652 eventNumber: [e eventNumber]
6653 clickCount: [e clickCount]
6654 pressure: [e pressure]];
6655 [super mouseUp: fake_event];
6656 }
6657
6658 if (part != NSScrollerKnob)
6659 [self sendScrollEventAtLoc: pos fromEvent: e];
6660 }
6661
6662
6663 /* Called as we manually track scroller drags, rather than superclass. */
6664 - (void)mouseDragged: (NSEvent *)e
6665 {
6666 NSRect sr;
6667 double loc, pos;
6668 int edge = 0;
6669
6670 NSTRACE (EmacsScroller_mouseDragged);
6671
6672 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
6673 toView: nil];
6674 loc = NSHeight (sr) - ([e locationInWindow].y - NSMinY (sr));
6675
6676 if (loc <= 0.0)
6677 {
6678 loc = 0.0;
6679 edge = -1;
6680 }
6681 else if (loc >= NSHeight (sr) + last_mouse_offset)
6682 {
6683 loc = NSHeight (sr) + last_mouse_offset;
6684 edge = 1;
6685 }
6686
6687 pos = /*(edge ? loc :*/ (loc - last_mouse_offset) / NSHeight (sr);
6688 [self sendScrollEventAtLoc: pos fromEvent: e];
6689 }
6690
6691
6692 - (void)mouseUp: (NSEvent *)e
6693 {
6694 if (scroll_repeat_entry)
6695 {
6696 [scroll_repeat_entry invalidate];
6697 [scroll_repeat_entry release];
6698 scroll_repeat_entry = nil;
6699 }
6700 last_hit_part = 0;
6701 }
6702
6703
6704 /* treat scrollwheel events in the bar as though they were in the main window */
6705 - (void) scrollWheel: (NSEvent *)theEvent
6706 {
6707 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (frame);
6708 [view mouseDown: theEvent];
6709 }
6710
6711 @end /* EmacsScroller */
6712
6713
6714
6715
6716 /* ==========================================================================
6717
6718 Font-related functions; these used to be in nsfaces.m
6719
6720 ========================================================================== */
6721
6722
6723 Lisp_Object
6724 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
6725 {
6726 struct font *font = XFONT_OBJECT (font_object);
6727
6728 if (fontset < 0)
6729 fontset = fontset_from_font (font_object);
6730 FRAME_FONTSET (f) = fontset;
6731
6732 if (FRAME_FONT (f) == font)
6733 /* This font is already set in frame F. There's nothing more to
6734 do. */
6735 return font_object;
6736
6737 FRAME_FONT (f) = font;
6738
6739 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
6740 FRAME_COLUMN_WIDTH (f) = font->average_width;
6741 FRAME_SPACE_WIDTH (f) = font->space_width;
6742 FRAME_LINE_HEIGHT (f) = font->height;
6743
6744 compute_fringe_widths (f, 1);
6745
6746 /* Compute the scroll bar width in character columns. */
6747 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
6748 {
6749 int wid = FRAME_COLUMN_WIDTH (f);
6750 FRAME_CONFIG_SCROLL_BAR_COLS (f)
6751 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
6752 }
6753 else
6754 {
6755 int wid = FRAME_COLUMN_WIDTH (f);
6756 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
6757 }
6758
6759 /* Now make the frame display the given font. */
6760 if (FRAME_NS_WINDOW (f) != 0)
6761 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
6762
6763 return font_object;
6764 }
6765
6766
6767 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
6768 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
6769 in 1.43. */
6770
6771 const char *
6772 ns_xlfd_to_fontname (const char *xlfd)
6773 /* --------------------------------------------------------------------------
6774 Convert an X font name (XLFD) to an NS font name.
6775 Only family is used.
6776 The string returned is temporarily allocated.
6777 -------------------------------------------------------------------------- */
6778 {
6779 char *name = xmalloc (180);
6780 int i, len;
6781 const char *ret;
6782
6783 if (!strncmp (xlfd, "--", 2))
6784 sscanf (xlfd, "--%*[^-]-%[^-]179-", name);
6785 else
6786 sscanf (xlfd, "-%*[^-]-%[^-]179-", name);
6787
6788 /* stopgap for malformed XLFD input */
6789 if (strlen (name) == 0)
6790 strcpy (name, "Monaco");
6791
6792 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
6793 also uppercase after '-' or ' ' */
6794 name[0] = c_toupper (name[0]);
6795 for (len =strlen (name), i =0; i<len; i++)
6796 {
6797 if (name[i] == '$')
6798 {
6799 name[i] = '-';
6800 if (i+1<len)
6801 name[i+1] = c_toupper (name[i+1]);
6802 }
6803 else if (name[i] == '_')
6804 {
6805 name[i] = ' ';
6806 if (i+1<len)
6807 name[i+1] = c_toupper (name[i+1]);
6808 }
6809 }
6810 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
6811 ret = [[NSString stringWithUTF8String: name] UTF8String];
6812 xfree (name);
6813 return ret;
6814 }
6815
6816
6817 void
6818 syms_of_nsterm (void)
6819 {
6820 NSTRACE (syms_of_nsterm);
6821
6822 ns_antialias_threshold = 10.0;
6823
6824 /* from 23+ we need to tell emacs what modifiers there are.. */
6825 DEFSYM (Qmodifier_value, "modifier-value");
6826 DEFSYM (Qalt, "alt");
6827 DEFSYM (Qhyper, "hyper");
6828 DEFSYM (Qmeta, "meta");
6829 DEFSYM (Qsuper, "super");
6830 DEFSYM (Qcontrol, "control");
6831 DEFSYM (QUTF8_STRING, "UTF8_STRING");
6832
6833 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
6834 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
6835 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
6836 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
6837 Fput (Qcontrol, Qmodifier_value, make_number (ctrl_modifier));
6838
6839 DEFVAR_LISP ("ns-input-file", ns_input_file,
6840 "The file specified in the last NS event.");
6841 ns_input_file =Qnil;
6842
6843 DEFVAR_LISP ("ns-input-text", ns_input_text,
6844 "The data received in the last NS text drag event.");
6845 ns_input_text =Qnil;
6846
6847 DEFVAR_LISP ("ns-working-text", ns_working_text,
6848 "String for visualizing working composition sequence.");
6849 ns_working_text =Qnil;
6850
6851 DEFVAR_LISP ("ns-input-font", ns_input_font,
6852 "The font specified in the last NS event.");
6853 ns_input_font =Qnil;
6854
6855 DEFVAR_LISP ("ns-input-fontsize", ns_input_fontsize,
6856 "The fontsize specified in the last NS event.");
6857 ns_input_fontsize =Qnil;
6858
6859 DEFVAR_LISP ("ns-input-line", ns_input_line,
6860 "The line specified in the last NS event.");
6861 ns_input_line =Qnil;
6862
6863 DEFVAR_LISP ("ns-input-color", ns_input_color,
6864 "The color specified in the last NS event.");
6865 ns_input_color =Qnil;
6866
6867 DEFVAR_LISP ("ns-input-spi-name", ns_input_spi_name,
6868 "The service name specified in the last NS event.");
6869 ns_input_spi_name =Qnil;
6870
6871 DEFVAR_LISP ("ns-input-spi-arg", ns_input_spi_arg,
6872 "The service argument specified in the last NS event.");
6873 ns_input_spi_arg =Qnil;
6874
6875 DEFVAR_LISP ("ns-alternate-modifier", ns_alternate_modifier,
6876 "This variable describes the behavior of the alternate or option key.\n\
6877 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6878 Set to none means that the alternate / option key is not interpreted by Emacs\n\
6879 at all, allowing it to be used at a lower level for accented character entry.");
6880 ns_alternate_modifier = Qmeta;
6881
6882 DEFVAR_LISP ("ns-right-alternate-modifier", ns_right_alternate_modifier,
6883 "This variable describes the behavior of the right alternate or option key.\n\
6884 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6885 Set to left means be the same key as `ns-alternate-modifier'.\n\
6886 Set to none means that the alternate / option key is not interpreted by Emacs\n\
6887 at all, allowing it to be used at a lower level for accented character entry.");
6888 ns_right_alternate_modifier = Qleft;
6889
6890 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
6891 "This variable describes the behavior of the command key.\n\
6892 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
6893 ns_command_modifier = Qsuper;
6894
6895 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
6896 "This variable describes the behavior of the right command key.\n\
6897 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6898 Set to left means be the same key as `ns-command-modifier'.\n\
6899 Set to none means that the command / option key is not interpreted by Emacs\n\
6900 at all, allowing it to be used at a lower level for accented character entry.");
6901 ns_right_command_modifier = Qleft;
6902
6903 DEFVAR_LISP ("ns-control-modifier", ns_control_modifier,
6904 "This variable describes the behavior of the control key.\n\
6905 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
6906 ns_control_modifier = Qcontrol;
6907
6908 DEFVAR_LISP ("ns-right-control-modifier", ns_right_control_modifier,
6909 "This variable describes the behavior of the right control key.\n\
6910 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6911 Set to left means be the same key as `ns-control-modifier'.\n\
6912 Set to none means that the control / option key is not interpreted by Emacs\n\
6913 at all, allowing it to be used at a lower level for accented character entry.");
6914 ns_right_control_modifier = Qleft;
6915
6916 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
6917 "This variable describes the behavior of the function key (on laptops).\n\
6918 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6919 Set to none means that the function key is not interpreted by Emacs at all,\n\
6920 allowing it to be used at a lower level for accented character entry.");
6921 ns_function_modifier = Qnone;
6922
6923 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
6924 "Non-nil (the default) means to render text antialiased. Only has an effect on OS X Panther and above.");
6925 ns_antialias_text = Qt;
6926
6927 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
6928 "Whether to confirm application quit using dialog.");
6929 ns_confirm_quit = Qnil;
6930
6931 staticpro (&ns_display_name_list);
6932 ns_display_name_list = Qnil;
6933
6934 staticpro (&last_mouse_motion_frame);
6935 last_mouse_motion_frame = Qnil;
6936
6937 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
6938 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
6939 Only works on OSX 10.6 or later. */);
6940 ns_auto_hide_menu_bar = Qnil;
6941
6942 /* TODO: move to common code */
6943 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
6944 doc: /* Which toolkit scroll bars Emacs uses, if any.
6945 A value of nil means Emacs doesn't use toolkit scroll bars.
6946 With the X Window system, the value is a symbol describing the
6947 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
6948 With MS Windows or Nextstep, the value is t. */);
6949 Vx_toolkit_scroll_bars = Qt;
6950
6951 DEFVAR_BOOL ("x-use-underline-position-properties",
6952 x_use_underline_position_properties,
6953 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
6954 A value of nil means ignore them. If you encounter fonts with bogus
6955 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6956 to 4.1, set this to nil. */);
6957 x_use_underline_position_properties = 0;
6958
6959 DEFVAR_BOOL ("x-underline-at-descent-line",
6960 x_underline_at_descent_line,
6961 doc: /* Non-nil means to draw the underline at the same place as the descent line.
6962 A value of nil means to draw the underline according to the value of the
6963 variable `x-use-underline-position-properties', which is usually at the
6964 baseline level. The default value is nil. */);
6965 x_underline_at_descent_line = 0;
6966
6967 /* Tell emacs about this window system. */
6968 Fprovide (intern ("ns"), Qnil);
6969 }