1 /* NeXT/Open/GNUstep / MacOSX communication module.
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2012
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
29 /* This should be the first include, as it may set up #defines affecting
30 interpretation of even the system includes. */
34 #include <sys/types.h>
39 #include <c-strcase.h>
47 #include "blockinput.h"
48 #include "sysselect.h"
51 #include "character.h"
53 #include "composite.h"
56 #include "termhooks.h"
67 int term_trace_num = 0;
68 #define NSTRACE(x) fprintf (stderr, "%s:%d: [%d] " #x "\n", \
69 __FILE__, __LINE__, ++term_trace_num)
74 extern NSString *NSMenuDidBeginTrackingNotification;
76 /* ==========================================================================
80 ========================================================================== */
82 /* Convert a symbol indexed with an NSxxx value to a value as defined
83 in keyboard.c (lispy_function_key). I hope this is a correct way
85 static unsigned convert_ns_to_X_keysym[] =
87 NSHomeFunctionKey, 0x50,
88 NSLeftArrowFunctionKey, 0x51,
89 NSUpArrowFunctionKey, 0x52,
90 NSRightArrowFunctionKey, 0x53,
91 NSDownArrowFunctionKey, 0x54,
92 NSPageUpFunctionKey, 0x55,
93 NSPageDownFunctionKey, 0x56,
94 NSEndFunctionKey, 0x57,
95 NSBeginFunctionKey, 0x58,
96 NSSelectFunctionKey, 0x60,
97 NSPrintFunctionKey, 0x61,
98 NSExecuteFunctionKey, 0x62,
99 NSInsertFunctionKey, 0x63,
100 NSUndoFunctionKey, 0x65,
101 NSRedoFunctionKey, 0x66,
102 NSMenuFunctionKey, 0x67,
103 NSFindFunctionKey, 0x68,
104 NSHelpFunctionKey, 0x6A,
105 NSBreakFunctionKey, 0x6B,
107 NSF1FunctionKey, 0xBE,
108 NSF2FunctionKey, 0xBF,
109 NSF3FunctionKey, 0xC0,
110 NSF4FunctionKey, 0xC1,
111 NSF5FunctionKey, 0xC2,
112 NSF6FunctionKey, 0xC3,
113 NSF7FunctionKey, 0xC4,
114 NSF8FunctionKey, 0xC5,
115 NSF9FunctionKey, 0xC6,
116 NSF10FunctionKey, 0xC7,
117 NSF11FunctionKey, 0xC8,
118 NSF12FunctionKey, 0xC9,
119 NSF13FunctionKey, 0xCA,
120 NSF14FunctionKey, 0xCB,
121 NSF15FunctionKey, 0xCC,
122 NSF16FunctionKey, 0xCD,
123 NSF17FunctionKey, 0xCE,
124 NSF18FunctionKey, 0xCF,
125 NSF19FunctionKey, 0xD0,
126 NSF20FunctionKey, 0xD1,
127 NSF21FunctionKey, 0xD2,
128 NSF22FunctionKey, 0xD3,
129 NSF23FunctionKey, 0xD4,
130 NSF24FunctionKey, 0xD5,
132 NSBackspaceCharacter, 0x08, /* 8: Not on some KBs. */
133 NSDeleteCharacter, 0xFF, /* 127: Big 'delete' key upper right. */
134 NSDeleteFunctionKey, 0x9F, /* 63272: Del forw key off main array. */
136 NSTabCharacter, 0x09,
137 0x19, 0x09, /* left tab->regular since pass shift */
138 NSCarriageReturnCharacter, 0x0D,
139 NSNewlineCharacter, 0x0D,
140 NSEnterCharacter, 0x8D,
142 0x1B, 0x1B /* escape */
145 static Lisp_Object Qmodifier_value;
146 Lisp_Object Qalt, Qcontrol, Qhyper, Qmeta, Qsuper;
147 extern Lisp_Object Qcursor_color, Qcursor_type, Qns, Qleft;
149 static Lisp_Object QUTF8_STRING;
151 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
152 the maximum font size to NOT antialias. On GNUstep there is currently
153 no way to control this behavior. */
154 float ns_antialias_threshold;
156 /* Used to pick up AppleHighlightColor on OS X */
157 NSString *ns_selection_color;
159 NSArray *ns_send_types =0, *ns_return_types =0, *ns_drag_types =0;
160 NSString *ns_app_name = @"Emacs"; /* default changed later */
162 /* Display variables */
163 struct ns_display_info *x_display_list; /* Chain of existing displays */
164 Lisp_Object ns_display_name_list;
165 long context_menu_value = 0;
168 NSPoint last_mouse_motion_position;
169 static NSRect last_mouse_glyph;
170 static Time last_mouse_movement_time = 0;
171 static Lisp_Object last_mouse_motion_frame;
172 static EmacsScroller *last_mouse_scroll_bar = nil;
173 static struct frame *ns_updating_frame;
174 static NSView *focus_view = NULL;
175 static int ns_window_num = 0;
176 #ifdef NS_IMPL_GNUSTEP
179 static BOOL gsaved = NO;
180 BOOL ns_in_resize = NO;
181 static BOOL ns_fake_keydown = NO;
182 int ns_tmp_flags; /* FIXME */
183 struct nsfont_info *ns_tmp_font; /* FIXME */
184 static BOOL ns_menu_bar_is_hidden = NO;
185 /*static int debug_lock = 0; */
188 static BOOL send_appdefined = YES;
189 static NSEvent *last_appdefined_event = 0;
190 static NSTimer *timed_entry = 0;
191 static NSTimer *scroll_repeat_entry = nil;
192 static fd_set select_readfds, select_writefds;
193 enum { SELECT_HAVE_READ = 1, SELECT_HAVE_WRITE = 2, SELECT_HAVE_TMO = 4 };
194 static int select_nfds = 0, select_valid = 0;
195 static EMACS_TIME select_timeout = { 0, 0 };
196 static int selfds[2] = { -1, -1 };
197 static pthread_mutex_t select_mutex;
198 static int apploopnr = 0;
199 static NSAutoreleasePool *outerpool;
200 static struct input_event *emacs_event = NULL;
201 static struct input_event *q_event_ptr = NULL;
202 static int n_emacs_events_pending = 0;
203 static NSMutableArray *ns_pending_files, *ns_pending_service_names,
204 *ns_pending_service_args;
205 static BOOL ns_do_open_file = NO;
207 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
208 #define NS_FUNCTION_KEY_MASK 0x800000
209 #define NSLeftControlKeyMask (0x000001 | NSControlKeyMask)
210 #define NSRightControlKeyMask (0x002000 | NSControlKeyMask)
211 #define NSLeftCommandKeyMask (0x000008 | NSCommandKeyMask)
212 #define NSRightCommandKeyMask (0x000010 | NSCommandKeyMask)
213 #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
214 #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
215 #define EV_MODIFIERS(e) \
216 ((([e modifierFlags] & NSHelpKeyMask) ? \
217 hyper_modifier : 0) \
218 | (!EQ (ns_right_alternate_modifier, Qleft) && \
219 (([e modifierFlags] & NSRightAlternateKeyMask) \
220 == NSRightAlternateKeyMask) ? \
221 parse_solitary_modifier (ns_right_alternate_modifier) : 0) \
222 | (([e modifierFlags] & NSAlternateKeyMask) ? \
223 parse_solitary_modifier (ns_alternate_modifier) : 0) \
224 | (([e modifierFlags] & NSShiftKeyMask) ? \
225 shift_modifier : 0) \
226 | (!EQ (ns_right_control_modifier, Qleft) && \
227 (([e modifierFlags] & NSRightControlKeyMask) \
228 == NSRightControlKeyMask) ? \
229 parse_solitary_modifier (ns_right_control_modifier) : 0) \
230 | (([e modifierFlags] & NSControlKeyMask) ? \
231 parse_solitary_modifier (ns_control_modifier) : 0) \
232 | (([e modifierFlags] & NS_FUNCTION_KEY_MASK) ? \
233 parse_solitary_modifier (ns_function_modifier) : 0) \
234 | (!EQ (ns_right_command_modifier, Qleft) && \
235 (([e modifierFlags] & NSRightCommandKeyMask) \
236 == NSRightCommandKeyMask) ? \
237 parse_solitary_modifier (ns_right_command_modifier) : 0) \
238 | (([e modifierFlags] & NSCommandKeyMask) ? \
239 parse_solitary_modifier (ns_command_modifier):0))
241 #define EV_UDMODIFIERS(e) \
242 ((([e type] == NSLeftMouseDown) ? down_modifier : 0) \
243 | (([e type] == NSRightMouseDown) ? down_modifier : 0) \
244 | (([e type] == NSOtherMouseDown) ? down_modifier : 0) \
245 | (([e type] == NSLeftMouseDragged) ? down_modifier : 0) \
246 | (([e type] == NSRightMouseDragged) ? down_modifier : 0) \
247 | (([e type] == NSOtherMouseDragged) ? down_modifier : 0) \
248 | (([e type] == NSLeftMouseUp) ? up_modifier : 0) \
249 | (([e type] == NSRightMouseUp) ? up_modifier : 0) \
250 | (([e type] == NSOtherMouseUp) ? up_modifier : 0))
252 #define EV_BUTTON(e) \
253 ((([e type] == NSLeftMouseDown) || ([e type] == NSLeftMouseUp)) ? 0 : \
254 (([e type] == NSRightMouseDown) || ([e type] == NSRightMouseUp)) ? 2 : \
255 [e buttonNumber] - 1)
257 /* Convert the time field to a timestamp in milliseconds. */
258 #define EV_TIMESTAMP(e) ([e timestamp] * 1000)
260 /* This is a piece of code which is common to all the event handling
261 methods. Maybe it should even be a function. */
262 #define EV_TRAILER(e) \
264 XSETFRAME (emacs_event->frame_or_window, emacsframe); \
265 if (e) emacs_event->timestamp = EV_TIMESTAMP (e); \
268 n_emacs_events_pending++; \
269 kbd_buffer_store_event_hold (emacs_event, q_event_ptr); \
272 kbd_buffer_store_event (emacs_event); \
273 EVENT_INIT (*emacs_event); \
274 ns_send_appdefined (-1); \
277 void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
279 /* TODO: get rid of need for these forward declarations */
280 static void ns_condemn_scroll_bars (struct frame *f);
281 static void ns_judge_scroll_bars (struct frame *f);
282 void x_set_frame_alpha (struct frame *f);
285 /* ==========================================================================
289 ========================================================================== */
293 append2 (Lisp_Object list, Lisp_Object item)
294 /* --------------------------------------------------------------------------
295 Utility to append to a list
296 -------------------------------------------------------------------------- */
298 Lisp_Object array[2];
300 array[1] = Fcons (item, Qnil);
301 return Fnconc (2, &array[0]);
306 ns_etc_directory (void)
307 /* If running as a self-contained app bundle, return as a string the
308 filename of the etc directory, if present; else nil. */
310 NSBundle *bundle = [NSBundle mainBundle];
311 NSString *resourceDir = [bundle resourcePath];
312 NSString *resourcePath;
313 NSFileManager *fileManager = [NSFileManager defaultManager];
316 resourcePath = [resourceDir stringByAppendingPathComponent: @"etc"];
317 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
319 if (isDir) return [resourcePath UTF8String];
327 /* If running as a self-contained app bundle, return as a path string
328 the filenames of the libexec and bin directories, ie libexec:bin.
329 Otherwise, return nil.
330 Normally, Emacs does not add its own bin/ directory to the PATH.
331 However, a self-contained NS build has a different layout, with
332 bin/ and libexec/ subdirectories in the directory that contains
334 We put libexec first, because init_callproc_1 uses the first
335 element to initialize exec-directory. An alternative would be
336 for init_callproc to check for invocation-directory/libexec.
339 NSBundle *bundle = [NSBundle mainBundle];
340 NSString *resourceDir = [bundle resourcePath];
341 NSString *binDir = [bundle bundlePath];
342 NSString *resourcePath, *resourcePaths;
344 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
345 NSFileManager *fileManager = [NSFileManager defaultManager];
347 NSEnumerator *pathEnum;
350 range = [resourceDir rangeOfString: @"Contents"];
351 if (range.location != NSNotFound)
353 binDir = [binDir stringByAppendingPathComponent: @"Contents"];
355 binDir = [binDir stringByAppendingPathComponent: @"MacOS"];
359 paths = [binDir stringsByAppendingPaths:
360 [NSArray arrayWithObjects: @"libexec", @"bin", nil]];
361 pathEnum = [paths objectEnumerator];
364 while ((resourcePath = [pathEnum nextObject]))
366 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
369 if ([resourcePaths length] > 0)
371 = [resourcePaths stringByAppendingString: pathSeparator];
373 = [resourcePaths stringByAppendingString: resourcePath];
376 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
384 /* If running as a self-contained app bundle, return as a path string
385 the filenames of the site-lisp, lisp and leim directories.
386 Ie, site-lisp:lisp:leim. Otherwise, return nil. */
388 NSBundle *bundle = [NSBundle mainBundle];
389 NSString *resourceDir = [bundle resourcePath];
390 NSString *resourcePath, *resourcePaths;
391 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
392 NSFileManager *fileManager = [NSFileManager defaultManager];
394 NSArray *paths = [resourceDir stringsByAppendingPaths:
395 [NSArray arrayWithObjects:
396 @"site-lisp", @"lisp", @"leim", nil]];
397 NSEnumerator *pathEnum = [paths objectEnumerator];
400 /* Hack to skip site-lisp. */
401 if (no_site_lisp) resourcePath = [pathEnum nextObject];
403 while ((resourcePath = [pathEnum nextObject]))
405 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
408 if ([resourcePaths length] > 0)
410 = [resourcePaths stringByAppendingString: pathSeparator];
412 = [resourcePaths stringByAppendingString: resourcePath];
415 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
421 ns_timeout (int usecs)
422 /* --------------------------------------------------------------------------
423 Blocking timer utility used by ns_ring_bell
424 -------------------------------------------------------------------------- */
426 EMACS_TIME wakeup = add_emacs_time (current_emacs_time (),
427 make_emacs_time (0, usecs * 1000));
429 /* Keep waiting until past the time wakeup. */
432 EMACS_TIME timeout, now = current_emacs_time ();
433 if (EMACS_TIME_LE (wakeup, now))
435 timeout = sub_emacs_time (wakeup, now);
437 /* Try to wait that long--but we might wake up sooner. */
438 pselect (0, NULL, NULL, NULL, &timeout, NULL);
444 ns_release_object (void *obj)
445 /* --------------------------------------------------------------------------
446 Release an object (callable from C)
447 -------------------------------------------------------------------------- */
454 ns_retain_object (void *obj)
455 /* --------------------------------------------------------------------------
456 Retain an object (callable from C)
457 -------------------------------------------------------------------------- */
464 ns_alloc_autorelease_pool (void)
465 /* --------------------------------------------------------------------------
466 Allocate a pool for temporary objects (callable from C)
467 -------------------------------------------------------------------------- */
469 return [[NSAutoreleasePool alloc] init];
474 ns_release_autorelease_pool (void *pool)
475 /* --------------------------------------------------------------------------
476 Free a pool and temporary objects it refers to (callable from C)
477 -------------------------------------------------------------------------- */
479 ns_release_object (pool);
484 /* ==========================================================================
486 Focus (clipping) and screen update
488 ========================================================================== */
491 ns_resize_handle_rect (NSWindow *window)
493 NSRect r = [window frame];
494 r.origin.x = r.size.width - RESIZE_HANDLE_SIZE;
496 r.size.width = r.size.height = RESIZE_HANDLE_SIZE;
502 // Window constraining
503 // -------------------
505 // To ensure that the windows are not placed under the menu bar, they
506 // are typically moved by the call-back constrainFrameRect. However,
507 // by overriding it, it's possible to inhibit this, leaving the window
508 // in it's original position.
510 // It's possible to hide the menu bar. However, technically, it's only
511 // possible to hide it when the application is active. To ensure that
512 // this work properly, the menu bar and window constraining are
513 // deferred until the application becomes active.
515 // Even though it's not possible to manually move a window above the
516 // top of the screen, it is allowed if it's done programmatically,
517 // when the menu is hidden. This allows the editable area to cover the
518 // full screen height.
523 // Use the following extra files:
526 // ;; Hide menu and place frame slightly above the top of the screen.
527 // (setq ns-auto-hide-menu-bar t)
528 // (set-frame-position (selected-frame) 0 -20)
532 // emacs -Q -l init.el
534 // Result: No menu bar, and the title bar should be above the screen.
540 // Result: Menu bar visible, frame placed immediately below the menu.
544 ns_constrain_all_frames (void)
546 Lisp_Object tail, frame;
548 FOR_EACH_FRAME (tail, frame)
550 struct frame *f = XFRAME (frame);
553 NSView *view = FRAME_NS_VIEW (f);
554 /* This no-op will trigger the default window placing
555 * constraint system. */
556 f->output_data.ns->dont_constrain = 0;
557 [[view window] setFrameOrigin:[[view window] frame].origin];
563 /* True, if the menu bar should be hidden. */
566 ns_menu_bar_should_be_hidden (void)
568 return !NILP (ns_auto_hide_menu_bar)
569 && [NSApp respondsToSelector:@selector(setPresentationOptions:)];
573 /* Show or hide the menu bar, based on user setting. */
576 ns_update_auto_hide_menu_bar (void)
578 #ifndef MAC_OS_X_VERSION_10_6
579 #define MAC_OS_X_VERSION_10_6 1060
582 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
585 NSTRACE (ns_update_auto_hide_menu_bar);
589 && [NSApp respondsToSelector:@selector(setPresentationOptions:)])
591 // Note, "setPresentationOptions" triggers an error unless the
592 // application is active.
593 BOOL menu_bar_should_be_hidden = ns_menu_bar_should_be_hidden ();
595 if (menu_bar_should_be_hidden != ns_menu_bar_is_hidden)
597 NSApplicationPresentationOptions options
598 = NSApplicationPresentationAutoHideDock;
600 if (menu_bar_should_be_hidden)
601 options |= NSApplicationPresentationAutoHideMenuBar;
603 [NSApp setPresentationOptions: options];
605 ns_menu_bar_is_hidden = menu_bar_should_be_hidden;
607 if (!ns_menu_bar_is_hidden)
609 ns_constrain_all_frames ();
621 ns_update_begin (struct frame *f)
622 /* --------------------------------------------------------------------------
623 Prepare for a grouped sequence of drawing calls
624 external (RIF) call; whole frame, called before update_window_begin
625 -------------------------------------------------------------------------- */
627 NSView *view = FRAME_NS_VIEW (f);
628 NSTRACE (ns_update_begin);
630 ns_update_auto_hide_menu_bar ();
632 ns_updating_frame = f;
635 #ifdef NS_IMPL_GNUSTEP
636 uRect = NSMakeRect (0, 0, 0, 0);
642 ns_update_window_begin (struct window *w)
643 /* --------------------------------------------------------------------------
644 Prepare for a grouped sequence of drawing calls
645 external (RIF) call; for one window, called after update_begin
646 -------------------------------------------------------------------------- */
648 struct frame *f = XFRAME (WINDOW_FRAME (w));
649 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
650 NSTRACE (ns_update_window_begin);
653 set_output_cursor (&w->cursor);
657 if (f == hlinfo->mouse_face_mouse_frame)
659 /* Don't do highlighting for mouse motion during the update. */
660 hlinfo->mouse_face_defer = 1;
662 /* If the frame needs to be redrawn,
663 simply forget about any prior mouse highlighting. */
664 if (FRAME_GARBAGED_P (f))
665 hlinfo->mouse_face_window = Qnil;
667 /* (further code for mouse faces ifdef'd out in other terms elided) */
675 ns_update_window_end (struct window *w, int cursor_on_p,
676 int mouse_face_overwritten_p)
677 /* --------------------------------------------------------------------------
678 Finished a grouped sequence of drawing calls
679 external (RIF) call; for one window called before update_end
680 -------------------------------------------------------------------------- */
682 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
684 /* note: this fn is nearly identical in all terms */
685 if (!w->pseudo_window_p)
690 display_and_set_cursor (w, 1,
691 output_cursor.hpos, output_cursor.vpos,
692 output_cursor.x, output_cursor.y);
694 if (draw_window_fringes (w, 1))
695 x_draw_vertical_border (w);
700 /* If a row with mouse-face was overwritten, arrange for
701 frame_up_to_date to redisplay the mouse highlight. */
702 if (mouse_face_overwritten_p)
704 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
705 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
706 hlinfo->mouse_face_window = Qnil;
709 updated_window = NULL;
710 NSTRACE (update_window_end);
715 ns_update_end (struct frame *f)
716 /* --------------------------------------------------------------------------
717 Finished a grouped sequence of drawing calls
718 external (RIF) call; for whole frame, called after update_window_end
719 -------------------------------------------------------------------------- */
721 NSView *view = FRAME_NS_VIEW (f);
723 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
724 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
728 #ifdef NS_IMPL_GNUSTEP
729 /* trigger flush only in the rectangle we tracked as being drawn */
730 [view unlockFocusNeedsFlush: NO];
731 /*fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", uRect.origin.x, uRect.origin.y, uRect.size.width, uRect.size.height); */
732 [view lockFocusInRect: uRect];
736 [[view window] flushWindow];
739 ns_updating_frame = NULL;
740 NSTRACE (ns_update_end);
745 ns_flush (struct frame *f)
746 /* --------------------------------------------------------------------------
748 NS impl is no-op since currently we flush in ns_update_end and elsewhere
749 -------------------------------------------------------------------------- */
756 ns_focus (struct frame *f, NSRect *r, int n)
757 /* --------------------------------------------------------------------------
758 Internal: Focus on given frame. During small local updates this is used to
759 draw, however during large updates, ns_update_begin and ns_update_end are
760 called to wrap the whole thing, in which case these calls are stubbed out.
761 Except, on GNUstep, we accumulate the rectangle being drawn into, because
762 the back end won't do this automatically, and will just end up flushing
764 -------------------------------------------------------------------------- */
766 // NSTRACE (ns_focus);
767 #ifdef NS_IMPL_GNUSTEP
770 u = NSUnionRect (r[0], r[1]);
775 fprintf (stderr, "focus: %d", c++);
776 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
777 fprintf (stderr, "\n"); */
779 if (f != ns_updating_frame)
781 NSView *view = FRAME_NS_VIEW (f);
782 if (view != focus_view)
784 if (focus_view != NULL)
786 [focus_view unlockFocus];
787 [[focus_view window] flushWindow];
792 #ifdef NS_IMPL_GNUSTEP
793 r ? [view lockFocusInRect: u] : [view lockFocus];
798 /*if (view) debug_lock++; */
800 #ifdef NS_IMPL_GNUSTEP
803 /* more than one rect being drawn into */
806 [view unlockFocus]; /* add prev rect to redraw list */
807 [view lockFocusInRect: u]; /* focus for draw in new rect */
812 #ifdef NS_IMPL_GNUSTEP
815 /* in batch mode, but in GNUstep must still track rectangles explicitly */
816 uRect = (r ? NSUnionRect (uRect, u) : [FRAME_NS_VIEW (f) visibleRect]);
823 [[NSGraphicsContext currentContext] saveGraphicsState];
825 NSRectClipList (r, 2);
834 ns_unfocus (struct frame *f)
835 /* --------------------------------------------------------------------------
836 Internal: Remove focus on given frame
837 -------------------------------------------------------------------------- */
839 // NSTRACE (ns_unfocus);
843 [[NSGraphicsContext currentContext] restoreGraphicsState];
847 if (f != ns_updating_frame)
849 if (focus_view != NULL)
851 [focus_view unlockFocus];
852 [[focus_view window] flushWindow];
861 ns_clip_to_row (struct window *w, struct glyph_row *row, int area, BOOL gc)
862 /* --------------------------------------------------------------------------
863 Internal (but parallels other terms): Focus drawing on given row
864 -------------------------------------------------------------------------- */
866 struct frame *f = XFRAME (WINDOW_FRAME (w));
868 int window_x, window_y, window_width;
870 window_box (w, area, &window_x, &window_y, &window_width, 0);
872 clip_rect.origin.x = window_x - FRAME_INTERNAL_BORDER_WIDTH (f);
873 clip_rect.origin.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
874 clip_rect.origin.y = max (clip_rect.origin.y, window_y);
875 clip_rect.size.width = window_width + 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
876 clip_rect.size.height = row->visible_height;
878 /* allow a full-height row at the top when requested
879 (used to draw fringe all the way through internal border area) */
880 if (gc && clip_rect.origin.y < 5)
882 clip_rect.origin.y -= FRAME_INTERNAL_BORDER_WIDTH (f);
883 clip_rect.size.height += FRAME_INTERNAL_BORDER_WIDTH (f);
886 /* likewise at bottom */
888 FRAME_PIXEL_HEIGHT (f) - (clip_rect.origin.y + clip_rect.size.height) < 5)
889 clip_rect.size.height += FRAME_INTERNAL_BORDER_WIDTH (f);
891 ns_focus (f, &clip_rect, 1);
896 ns_ring_bell (struct frame *f)
897 /* --------------------------------------------------------------------------
899 -------------------------------------------------------------------------- */
901 NSTRACE (ns_ring_bell);
904 NSAutoreleasePool *pool;
905 struct frame *frame = SELECTED_FRAME ();
909 pool = [[NSAutoreleasePool alloc] init];
911 view = FRAME_NS_VIEW (frame);
915 NSPoint dim = NSMakePoint (128, 128);
918 r.origin.x += (r.size.width - dim.x) / 2;
919 r.origin.y += (r.size.height - dim.y) / 2;
920 r.size.width = dim.x;
921 r.size.height = dim.y;
922 surr = NSInsetRect (r, -2, -2);
923 ns_focus (frame, &surr, 1);
924 [[view window] cacheImageInRect: [view convertRect: surr toView:nil]];
925 [ns_lookup_indexed_color (NS_FACE_FOREGROUND
926 (FRAME_DEFAULT_FACE (frame)), frame) set];
928 [[view window] flushWindow];
930 [[view window] restoreCachedImage];
931 [[view window] flushWindow];
945 ns_reset_terminal_modes (struct terminal *terminal)
946 /* Externally called as hook */
948 NSTRACE (ns_reset_terminal_modes);
953 ns_set_terminal_modes (struct terminal *terminal)
954 /* Externally called as hook */
956 NSTRACE (ns_set_terminal_modes);
961 /* ==========================================================================
963 Frame / window manager related functions
965 ========================================================================== */
969 ns_raise_frame (struct frame *f)
970 /* --------------------------------------------------------------------------
971 Bring window to foreground and make it active
972 -------------------------------------------------------------------------- */
974 NSView *view = FRAME_NS_VIEW (f);
977 FRAME_SAMPLE_VISIBILITY (f);
978 if (FRAME_VISIBLE_P (f))
980 [[view window] makeKeyAndOrderFront: NSApp];
987 ns_lower_frame (struct frame *f)
988 /* --------------------------------------------------------------------------
990 -------------------------------------------------------------------------- */
992 NSView *view = FRAME_NS_VIEW (f);
995 [[view window] orderBack: NSApp];
1001 ns_frame_raise_lower (struct frame *f, int raise)
1002 /* --------------------------------------------------------------------------
1004 -------------------------------------------------------------------------- */
1006 NSTRACE (ns_frame_raise_lower);
1016 ns_frame_rehighlight (struct frame *frame)
1017 /* --------------------------------------------------------------------------
1018 External (hook): called on things like window switching within frame
1019 -------------------------------------------------------------------------- */
1021 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (frame);
1022 struct frame *old_highlight = dpyinfo->x_highlight_frame;
1024 NSTRACE (ns_frame_rehighlight);
1025 if (dpyinfo->x_focus_frame)
1027 dpyinfo->x_highlight_frame
1028 = (FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1029 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1030 : dpyinfo->x_focus_frame);
1031 if (!FRAME_LIVE_P (dpyinfo->x_highlight_frame))
1033 FSET (dpyinfo->x_focus_frame, focus_frame, Qnil);
1034 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
1038 dpyinfo->x_highlight_frame = 0;
1040 if (dpyinfo->x_highlight_frame &&
1041 dpyinfo->x_highlight_frame != old_highlight)
1045 x_update_cursor (old_highlight, 1);
1046 x_set_frame_alpha (old_highlight);
1048 if (dpyinfo->x_highlight_frame)
1050 x_update_cursor (dpyinfo->x_highlight_frame, 1);
1051 x_set_frame_alpha (dpyinfo->x_highlight_frame);
1058 x_make_frame_visible (struct frame *f)
1059 /* --------------------------------------------------------------------------
1060 External: Show the window (X11 semantics)
1061 -------------------------------------------------------------------------- */
1063 NSTRACE (x_make_frame_visible);
1064 /* XXX: at some points in past this was not needed, as the only place that
1065 called this (frame.c:Fraise_frame ()) also called raise_lower;
1066 if this ends up the case again, comment this out again. */
1067 if (!FRAME_VISIBLE_P (f))
1069 f->async_visible = 1;
1076 x_make_frame_invisible (struct frame *f)
1077 /* --------------------------------------------------------------------------
1078 External: Hide the window (X11 semantics)
1079 -------------------------------------------------------------------------- */
1081 NSView * view = FRAME_NS_VIEW (f);
1082 NSTRACE (x_make_frame_invisible);
1084 [[view window] orderOut: NSApp];
1085 f->async_visible = 0;
1086 f->async_iconified = 0;
1091 x_iconify_frame (struct frame *f)
1092 /* --------------------------------------------------------------------------
1093 External: Iconify window
1094 -------------------------------------------------------------------------- */
1096 NSView * view = FRAME_NS_VIEW (f);
1097 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1098 NSTRACE (x_iconify_frame);
1101 if (dpyinfo->x_highlight_frame == f)
1102 dpyinfo->x_highlight_frame = 0;
1104 if ([[view window] windowNumber] <= 0)
1106 /* the window is still deferred. Make it very small, bring it
1107 on screen and order it out. */
1108 NSRect s = { { 100, 100}, {0, 0} };
1110 t = [[view window] frame];
1111 [[view window] setFrame: s display: NO];
1112 [[view window] orderBack: NSApp];
1113 [[view window] orderOut: NSApp];
1114 [[view window] setFrame: t display: NO];
1116 [[view window] miniaturize: NSApp];
1119 /* Free X resources of frame F. */
1122 x_free_frame_resources (struct frame *f)
1124 NSView *view = FRAME_NS_VIEW (f);
1125 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1126 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1127 NSTRACE (x_free_frame_resources);
1130 [(EmacsView *)view setWindowClosing: YES]; /* may not have been informed */
1134 free_frame_menubar (f);
1136 if (FRAME_FACE_CACHE (f))
1137 free_frame_faces (f);
1139 if (f == dpyinfo->x_focus_frame)
1140 dpyinfo->x_focus_frame = 0;
1141 if (f == dpyinfo->x_highlight_frame)
1142 dpyinfo->x_highlight_frame = 0;
1143 if (f == hlinfo->mouse_face_mouse_frame)
1145 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1146 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1147 hlinfo->mouse_face_window = Qnil;
1148 hlinfo->mouse_face_deferred_gc = 0;
1149 hlinfo->mouse_face_mouse_frame = 0;
1152 if (f->output_data.ns->miniimage != nil)
1153 [f->output_data.ns->miniimage release];
1155 [[view window] close];
1158 xfree (f->output_data.ns);
1164 x_destroy_window (struct frame *f)
1165 /* --------------------------------------------------------------------------
1166 External: Delete the window
1167 -------------------------------------------------------------------------- */
1169 NSTRACE (x_destroy_window);
1171 x_free_frame_resources (f);
1177 x_set_offset (struct frame *f, int xoff, int yoff, int change_grav)
1178 /* --------------------------------------------------------------------------
1179 External: Position the window
1180 -------------------------------------------------------------------------- */
1182 NSView *view = FRAME_NS_VIEW (f);
1183 NSArray *screens = [NSScreen screens];
1184 NSScreen *fscreen = [screens objectAtIndex: 0];
1185 NSScreen *screen = [[view window] screen];
1187 NSTRACE (x_set_offset);
1194 if (view != nil && screen && fscreen)
1196 f->left_pos = f->size_hint_flags & XNegative
1197 ? [screen visibleFrame].size.width + f->left_pos - FRAME_PIXEL_WIDTH (f)
1199 /* We use visibleFrame here to take menu bar into account.
1200 Ideally we should also adjust left/top with visibleFrame.origin. */
1202 f->top_pos = f->size_hint_flags & YNegative
1203 ? ([screen visibleFrame].size.height + f->top_pos
1204 - FRAME_PIXEL_HEIGHT (f) - FRAME_NS_TITLEBAR_HEIGHT (f)
1205 - FRAME_TOOLBAR_HEIGHT (f))
1207 #ifdef NS_IMPL_GNUSTEP
1208 if (f->left_pos < 100)
1209 f->left_pos = 100; /* don't overlap menu */
1211 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1213 f->output_data.ns->dont_constrain = 0;
1214 [[view window] setFrameTopLeftPoint:
1215 NSMakePoint (SCREENMAXBOUND (f->left_pos),
1216 SCREENMAXBOUND ([fscreen frame].size.height
1217 - NS_TOP_POS (f)))];
1218 f->size_hint_flags &= ~(XNegative|YNegative);
1226 x_set_window_size (struct frame *f, int change_grav, int cols, int rows)
1227 /* --------------------------------------------------------------------------
1228 Adjust window pixel size based on given character grid size
1229 Impl is a bit more complex than other terms, need to do some
1231 -------------------------------------------------------------------------- */
1233 EmacsView *view = FRAME_NS_VIEW (f);
1234 NSWindow *window = [view window];
1235 NSRect wr = [window frame];
1236 int tb = FRAME_EXTERNAL_TOOL_BAR (f);
1237 int pixelwidth, pixelheight;
1238 static int oldRows, oldCols, oldFontWidth, oldFontHeight;
1240 static struct frame *oldF;
1242 NSTRACE (x_set_window_size);
1246 && rows == oldRows && cols == oldCols
1247 && oldFontWidth == FRAME_COLUMN_WIDTH (f)
1248 && oldFontHeight == FRAME_LINE_HEIGHT (f)
1252 /*fprintf (stderr, "\tsetWindowSize: %d x %d, font size %d x %d\n", cols, rows, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f)); */
1256 check_frame_size (f, &rows, &cols);
1260 oldFontWidth = FRAME_COLUMN_WIDTH (f);
1261 oldFontHeight = FRAME_LINE_HEIGHT (f);
1264 f->scroll_bar_actual_width = NS_SCROLL_BAR_WIDTH (f);
1265 compute_fringe_widths (f, 0);
1267 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
1268 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
1270 /* If we have a toolbar, take its height into account. */
1272 /* NOTE: previously this would generate wrong result if toolbar not
1273 yet displayed and fixing toolbar_height=32 helped, but
1274 now (200903) seems no longer needed */
1275 FRAME_TOOLBAR_HEIGHT (f) =
1276 NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)])
1277 - FRAME_NS_TITLEBAR_HEIGHT (f);
1279 FRAME_TOOLBAR_HEIGHT (f) = 0;
1281 wr.size.width = pixelwidth + f->border_width;
1282 wr.size.height = pixelheight + FRAME_NS_TITLEBAR_HEIGHT (f)
1283 + FRAME_TOOLBAR_HEIGHT (f);
1285 /* Do not try to constrain to this screen. We may have multiple
1286 screens, and want Emacs to span those. Constraining to screen
1287 prevents that, and that is not nice to the user. */
1288 if (f->output_data.ns->zooming)
1289 f->output_data.ns->zooming = 0;
1291 wr.origin.y += FRAME_PIXEL_HEIGHT (f) - pixelheight;
1293 [view setRows: rows andColumns: cols];
1294 [window setFrame: wr display: YES];
1296 /*fprintf (stderr, "\tx_set_window_size %d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight); */
1298 /* This is a trick to compensate for Emacs' managing the scrollbar area
1299 as a fixed number of standard character columns. Instead of leaving
1300 blank space for the extra, we chopped it off above. Now for
1301 left-hand scrollbars, we shift all rendering to the left by the
1302 difference between the real width and Emacs' imagined one. For
1303 right-hand bars, don't worry about it since the extra is never used.
1304 (Obviously doesn't work for vertically split windows tho..) */
1306 NSPoint origin = FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f)
1307 ? NSMakePoint (FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)
1308 - NS_SCROLL_BAR_WIDTH (f), 0)
1309 : NSMakePoint (0, 0);
1310 [view setFrame: NSMakeRect (0, 0, pixelwidth, pixelheight)];
1311 [view setBoundsOrigin: origin];
1314 change_frame_size (f, rows, cols, 0, 1, 0); /* pretend, delay, safe */
1315 FRAME_PIXEL_WIDTH (f) = pixelwidth;
1316 FRAME_PIXEL_HEIGHT (f) = pixelheight;
1317 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1319 mark_window_cursors_off (XWINDOW (f->root_window));
1320 cancel_mouse_face (f);
1327 /* ==========================================================================
1331 ========================================================================== */
1335 ns_lookup_indexed_color (unsigned long idx, struct frame *f)
1337 struct ns_color_table *color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1338 if (idx < 1 || idx >= color_table->avail)
1340 return color_table->colors[idx];
1345 ns_index_color (NSColor *color, struct frame *f)
1347 struct ns_color_table *color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1351 if (!color_table->colors)
1353 color_table->size = NS_COLOR_CAPACITY;
1354 color_table->avail = 1; /* skip idx=0 as marker */
1355 color_table->colors = xmalloc (color_table->size * sizeof (NSColor *));
1356 color_table->colors[0] = nil;
1357 color_table->empty_indices = [[NSMutableSet alloc] init];
1360 /* do we already have this color ? */
1361 for (i = 1; i < color_table->avail; i++)
1362 if (color_table->colors[i] && [color_table->colors[i] isEqual: color])
1365 if ([color_table->empty_indices count] > 0)
1367 NSNumber *index = [color_table->empty_indices anyObject];
1368 [color_table->empty_indices removeObject: index];
1369 idx = [index unsignedLongValue];
1373 if (color_table->avail == color_table->size)
1374 color_table->colors =
1375 xpalloc (color_table->colors, &color_table->size, 1,
1376 min (ULONG_MAX, PTRDIFF_MAX), sizeof *color_table->colors);
1377 idx = color_table->avail++;
1380 color_table->colors[idx] = color;
1382 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1388 ns_free_indexed_color (unsigned long idx, struct frame *f)
1390 struct ns_color_table *color_table;
1397 color_table = FRAME_NS_DISPLAY_INFO (f)->color_table;
1399 if (idx <= 0 || idx >= color_table->size) {
1400 message1 ("ns_free_indexed_color: Color index out of range.\n");
1404 index = [NSNumber numberWithUnsignedInt: idx];
1405 if ([color_table->empty_indices containsObject: index]) {
1406 message1 ("ns_free_indexed_color: attempt to free already freed color.\n");
1410 color = color_table->colors[idx];
1412 color_table->colors[idx] = nil;
1413 [color_table->empty_indices addObject: index];
1414 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1419 ns_get_color (const char *name, NSColor **col)
1420 /* --------------------------------------------------------------------------
1422 -------------------------------------------------------------------------- */
1423 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
1424 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1425 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1428 static char hex[20];
1430 float r = -1.0, g, b;
1431 NSString *nsname = [NSString stringWithUTF8String: name];
1433 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1436 if ([nsname isEqualToString: @"ns_selection_color"])
1438 nsname = ns_selection_color;
1439 name = [ns_selection_color UTF8String];
1442 /* First, check for some sort of numeric specification. */
1445 if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* RGB decimal */
1447 NSScanner *scanner = [NSScanner scannerWithString: nsname];
1448 [scanner scanFloat: &r];
1449 [scanner scanFloat: &g];
1450 [scanner scanFloat: &b];
1452 else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
1453 scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
1454 else if (name[0] == '#') /* An old X11 format; convert to newer */
1456 int len = (strlen(name) - 1);
1457 int start = (len % 3 == 0) ? 1 : len / 4 + 1;
1459 scaling = strlen(name+start) / 3;
1460 for (i = 0; i < 3; i++)
1461 sprintf (hex + i * (scaling + 1), "%.*s/", scaling,
1462 name + start + i * scaling);
1463 hex[3 * (scaling + 1) - 1] = '\0';
1469 float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0);
1470 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
1480 *col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
1485 /* Otherwise, color is expected to be from a list */
1487 NSEnumerator *lenum, *cenum;
1491 #ifdef NS_IMPL_GNUSTEP
1492 /* XXX: who is wrong, the requestor or the implementation? */
1493 if ([nsname compare: @"Highlight" options: NSCaseInsensitiveSearch]
1495 nsname = @"highlightColor";
1498 lenum = [[NSColorList availableColorLists] objectEnumerator];
1499 while ( (clist = [lenum nextObject]) && new == nil)
1501 cenum = [[clist allKeys] objectEnumerator];
1502 while ( (name = [cenum nextObject]) && new == nil )
1504 if ([name compare: nsname
1505 options: NSCaseInsensitiveSearch] == NSOrderedSame )
1506 new = [clist colorWithKey: name];
1512 *col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
1519 ns_lisp_to_color (Lisp_Object color, NSColor **col)
1520 /* --------------------------------------------------------------------------
1521 Convert a Lisp string object to a NS color
1522 -------------------------------------------------------------------------- */
1524 NSTRACE (ns_lisp_to_color);
1525 if (STRINGP (color))
1526 return ns_get_color (SSDATA (color), col);
1527 else if (SYMBOLP (color))
1528 return ns_get_color (SSDATA (SYMBOL_NAME (color)), col);
1534 ns_color_to_lisp (NSColor *col)
1535 /* --------------------------------------------------------------------------
1536 Convert a color to a lisp string with the RGB equivalent
1537 -------------------------------------------------------------------------- */
1539 CGFloat red, green, blue, alpha, gray;
1542 NSTRACE (ns_color_to_lisp);
1545 if ([[col colorSpaceName] isEqualToString: NSNamedColorSpace])
1547 if ((str =[[col colorNameComponent] UTF8String]))
1550 return build_string ((char *)str);
1553 [[col colorUsingColorSpaceName: NSCalibratedRGBColorSpace]
1554 getRed: &red green: &green blue: &blue alpha: &alpha];
1555 if (red ==green && red ==blue)
1557 [[col colorUsingColorSpaceName: NSCalibratedWhiteColorSpace]
1558 getWhite: &gray alpha: &alpha];
1559 snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
1560 lrint (gray * 0xff), lrint (gray * 0xff), lrint (gray * 0xff));
1562 return build_string (buf);
1565 snprintf (buf, sizeof (buf), "#%2.2lx%2.2lx%2.2lx",
1566 lrint (red*0xff), lrint (green*0xff), lrint (blue*0xff));
1569 return build_string (buf);
1574 ns_query_color(void *col, XColor *color_def, int setPixel)
1575 /* --------------------------------------------------------------------------
1576 Get ARGB values out of NSColor col and put them into color_def.
1577 If setPixel, set the pixel to a concatenated version.
1578 and set color_def pixel to the resulting index.
1579 -------------------------------------------------------------------------- */
1583 [((NSColor *)col) getRed: &r green: &g blue: &b alpha: &a];
1584 color_def->red = r * 65535;
1585 color_def->green = g * 65535;
1586 color_def->blue = b * 65535;
1588 if (setPixel == YES)
1590 = ARGB_TO_ULONG((int)(a*255),
1591 (int)(r*255), (int)(g*255), (int)(b*255));
1596 ns_defined_color (struct frame *f,
1601 /* --------------------------------------------------------------------------
1602 Return 1 if named color found, and set color_def rgb accordingly.
1603 If makeIndex and alloc are nonzero put the color in the color_table,
1604 and set color_def pixel to the resulting index.
1605 If makeIndex is zero, set color_def pixel to ARGB.
1606 Return 0 if not found
1607 -------------------------------------------------------------------------- */
1610 NSTRACE (ns_defined_color);
1613 if (ns_get_color (name, &col) != 0) /* Color not found */
1618 if (makeIndex && alloc)
1619 color_def->pixel = ns_index_color (col, f);
1620 ns_query_color (col, color_def, !makeIndex);
1627 ns_get_rgb_color (struct frame *f, float r, float g, float b, float a)
1628 /* --------------------------------------------------------------------------
1629 return an autoreleased RGB color
1630 -------------------------------------------------------------------------- */
1632 /*static int c = 1; fprintf (stderr, "color request %d\n", c++); */
1633 if (r < 0.0) r = 0.0;
1634 else if (r > 1.0) r = 1.0;
1635 if (g < 0.0) g = 0.0;
1636 else if (g > 1.0) g = 1.0;
1637 if (b < 0.0) b = 0.0;
1638 else if (b > 1.0) b = 1.0;
1639 if (a < 0.0) a = 0.0;
1640 else if (a > 1.0) a = 1.0;
1641 return (unsigned long) ns_index_color(
1642 [NSColor colorWithCalibratedRed: r green: g blue: b alpha: a], f);
1647 x_set_frame_alpha (struct frame *f)
1648 /* --------------------------------------------------------------------------
1649 change the entire-frame transparency
1650 -------------------------------------------------------------------------- */
1652 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
1653 EmacsView *view = FRAME_NS_VIEW (f);
1655 double alpha_min = 1.0;
1657 if (dpyinfo->x_highlight_frame == f)
1658 alpha = f->alpha[0];
1660 alpha = f->alpha[1];
1662 if (FLOATP (Vframe_alpha_lower_limit))
1663 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
1664 else if (INTEGERP (Vframe_alpha_lower_limit))
1665 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
1669 else if (1.0 < alpha)
1671 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
1674 #ifdef NS_IMPL_COCOA
1675 [[view window] setAlphaValue: alpha];
1680 /* ==========================================================================
1684 ========================================================================== */
1688 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
1689 /* --------------------------------------------------------------------------
1690 Programmatically reposition mouse pointer in pixel coordinates
1691 -------------------------------------------------------------------------- */
1693 NSTRACE (x_set_mouse_pixel_position);
1696 /* FIXME: this does not work, and what about GNUstep? */
1697 #ifdef NS_IMPL_COCOA
1698 [FRAME_NS_VIEW (f) lockFocus];
1699 PSsetmouse ((float)pix_x, (float)pix_y);
1700 [FRAME_NS_VIEW (f) unlockFocus];
1707 x_set_mouse_position (struct frame *f, int h, int v)
1708 /* --------------------------------------------------------------------------
1709 Programmatically reposition mouse pointer in character coordinates
1710 -------------------------------------------------------------------------- */
1714 pix_x = FRAME_COL_TO_PIXEL_X (f, h) + FRAME_COLUMN_WIDTH (f) / 2;
1715 pix_y = FRAME_LINE_TO_PIXEL_Y (f, v) + FRAME_LINE_HEIGHT (f) / 2;
1717 if (pix_x < 0) pix_x = 0;
1718 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
1720 if (pix_y < 0) pix_y = 0;
1721 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
1723 x_set_mouse_pixel_position (f, pix_x, pix_y);
1728 note_mouse_movement (struct frame *frame, float x, float y)
1729 /* ------------------------------------------------------------------------
1730 Called by EmacsView on mouseMovement events. Passes on
1731 to emacs mainstream code if we moved off of a rect of interest
1732 known as last_mouse_glyph.
1733 ------------------------------------------------------------------------ */
1735 // NSTRACE (note_mouse_movement);
1737 XSETFRAME (last_mouse_motion_frame, frame);
1739 /* Note, this doesn't get called for enter/leave, since we don't have a
1740 position. Those are taken care of in the corresponding NSView methods. */
1742 /* has movement gone beyond last rect we were tracking? */
1743 if (x < last_mouse_glyph.origin.x ||
1744 x >= (last_mouse_glyph.origin.x + last_mouse_glyph.size.width) ||
1745 y < last_mouse_glyph.origin.y ||
1746 y >= (last_mouse_glyph.origin.y + last_mouse_glyph.size.height))
1748 ns_update_begin(frame);
1749 frame->mouse_moved = 1;
1750 note_mouse_highlight (frame, x, y);
1751 remember_mouse_glyph (frame, x, y, &last_mouse_glyph);
1752 ns_update_end(frame);
1761 ns_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
1762 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
1764 /* --------------------------------------------------------------------------
1765 External (hook): inform emacs about mouse position and hit parts.
1766 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1767 x & y should be position in the scrollbar (the whole bar, not the handle)
1768 and length of scrollbar respectively
1769 -------------------------------------------------------------------------- */
1773 Lisp_Object frame, tail;
1775 struct ns_display_info *dpyinfo;
1777 NSTRACE (ns_mouse_position);
1781 fprintf (stderr, "Warning: ns_mouse_position () called with null *fp.\n");
1785 dpyinfo = FRAME_NS_DISPLAY_INFO (*fp);
1789 if (last_mouse_scroll_bar != nil && insist == 0)
1791 /* TODO: we do not use this path at the moment because drag events will
1792 go directly to the EmacsScroller. Leaving code in for now. */
1793 [last_mouse_scroll_bar getMouseMotionPart: (int *)part window: bar_window
1795 if (time) *time = last_mouse_movement_time;
1796 last_mouse_scroll_bar = nil;
1800 /* Clear the mouse-moved flag for every frame on this display. */
1801 FOR_EACH_FRAME (tail, frame)
1802 if (FRAME_NS_P (XFRAME (frame))
1803 && FRAME_NS_DISPLAY (XFRAME (frame)) == FRAME_NS_DISPLAY (*fp))
1804 XFRAME (frame)->mouse_moved = 0;
1806 last_mouse_scroll_bar = nil;
1807 if (last_mouse_frame && FRAME_LIVE_P (last_mouse_frame))
1808 f = last_mouse_frame;
1810 f = dpyinfo->x_focus_frame ? dpyinfo->x_focus_frame
1811 : SELECTED_FRAME ();
1813 if (f && f->output_data.ns) /* TODO: 2nd check no longer needed? */
1815 view = FRAME_NS_VIEW (*fp);
1817 position = [[view window] mouseLocationOutsideOfEventStream];
1818 position = [view convertPoint: position fromView: nil];
1819 remember_mouse_glyph (f, position.x, position.y, &last_mouse_glyph);
1820 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1822 if (bar_window) *bar_window = Qnil;
1823 if (part) *part = 0; /*scroll_bar_handle; */
1825 if (x) XSETINT (*x, lrint (position.x));
1826 if (y) XSETINT (*y, lrint (position.y));
1827 if (time) *time = last_mouse_movement_time;
1837 ns_frame_up_to_date (struct frame *f)
1838 /* --------------------------------------------------------------------------
1839 External (hook): Fix up mouse highlighting right after a full update.
1840 Some highlighting was deferred if GC was happening during
1841 note_mouse_highlight (), while other highlighting was deferred for update.
1842 -------------------------------------------------------------------------- */
1844 NSTRACE (ns_frame_up_to_date);
1848 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1849 if ((hlinfo->mouse_face_deferred_gc || f ==hlinfo->mouse_face_mouse_frame)
1850 /*&& hlinfo->mouse_face_mouse_frame*/)
1854 if (hlinfo->mouse_face_mouse_frame)
1855 note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
1856 hlinfo->mouse_face_mouse_x,
1857 hlinfo->mouse_face_mouse_y);
1858 hlinfo->mouse_face_deferred_gc = 0;
1867 ns_define_frame_cursor (struct frame *f, Cursor cursor)
1868 /* --------------------------------------------------------------------------
1869 External (RIF): set frame mouse pointer type.
1870 -------------------------------------------------------------------------- */
1872 NSTRACE (ns_define_frame_cursor);
1873 if (FRAME_POINTER_TYPE (f) != cursor)
1875 EmacsView *view = FRAME_NS_VIEW (f);
1876 FRAME_POINTER_TYPE (f) = cursor;
1877 [[view window] invalidateCursorRectsForView: view];
1878 /* Redisplay assumes this function also draws the changed frame
1879 cursor, but this function doesn't, so do it explicitly. */
1880 x_update_cursor (f, 1);
1886 /* ==========================================================================
1890 ========================================================================== */
1894 ns_convert_key (unsigned code)
1895 /* --------------------------------------------------------------------------
1896 Internal call used by NSView-keyDown.
1897 -------------------------------------------------------------------------- */
1899 const unsigned last_keysym = (sizeof (convert_ns_to_X_keysym)
1900 / sizeof (convert_ns_to_X_keysym[0]));
1902 /* An array would be faster, but less easy to read. */
1903 for (keysym = 0; keysym < last_keysym; keysym += 2)
1904 if (code == convert_ns_to_X_keysym[keysym])
1905 return 0xFF00 | convert_ns_to_X_keysym[keysym+1];
1907 /* if decide to use keyCode and Carbon table, use this line:
1908 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1913 x_get_keysym_name (int keysym)
1914 /* --------------------------------------------------------------------------
1915 Called by keyboard.c. Not sure if the return val is important, except
1917 -------------------------------------------------------------------------- */
1919 static char value[16];
1920 NSTRACE (x_get_keysym_name);
1921 sprintf (value, "%d", keysym);
1927 /* ==========================================================================
1929 Block drawing operations
1931 ========================================================================== */
1935 ns_redraw_scroll_bars (struct frame *f)
1939 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
1940 NSTRACE (ns_judge_scroll_bars);
1941 for (i =[subviews count]-1; i >= 0; i--)
1943 view = [subviews objectAtIndex: i];
1944 if (![view isKindOfClass: [EmacsScroller class]]) continue;
1951 ns_clear_frame (struct frame *f)
1952 /* --------------------------------------------------------------------------
1953 External (hook): Erase the entire frame
1954 -------------------------------------------------------------------------- */
1956 NSView *view = FRAME_NS_VIEW (f);
1959 NSTRACE (ns_clear_frame);
1963 /* comes on initial frame because we have
1964 after-make-frame-functions = select-frame */
1965 if (!FRAME_DEFAULT_FACE (f))
1968 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
1970 output_cursor.hpos = output_cursor.vpos = 0;
1971 output_cursor.x = -1;
1976 ns_focus (f, &r, 1);
1977 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (FRAME_DEFAULT_FACE (f)), f) set];
1981 #ifdef NS_IMPL_COCOA
1982 [[view window] display]; /* redraw resize handle */
1985 /* as of 2006/11 or so this is now needed */
1986 ns_redraw_scroll_bars (f);
1992 ns_clear_frame_area (struct frame *f, int x, int y, int width, int height)
1993 /* --------------------------------------------------------------------------
1994 External (RIF): Clear section of frame
1995 -------------------------------------------------------------------------- */
1997 NSRect r = NSMakeRect (x, y, width, height);
1998 NSView *view = FRAME_NS_VIEW (f);
1999 struct face *face = FRAME_DEFAULT_FACE (f);
2004 NSTRACE (ns_clear_frame_area);
2006 r = NSIntersectionRect (r, [view frame]);
2007 ns_focus (f, &r, 1);
2008 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f) set];
2010 #ifdef NS_IMPL_COCOA
2012 /* clip out the resize handle */
2013 NSWindow *window = [FRAME_NS_VIEW (f) window];
2015 = [view convertRect: ns_resize_handle_rect (window) fromView: nil];
2017 ir = NSIntersectionRect (r, ir);
2018 if (NSIsEmptyRect (ir))
2024 #ifdef NS_IMPL_COCOA
2028 NSRect r1 = r, r2 = r; /* upper and lower non-intersecting */
2029 r1.size.height -= ir.size.height;
2030 r2.origin.y += r1.size.height;
2031 r2.size.width -= ir.size.width;
2032 r2.size.height = ir.size.height;
2045 ns_scroll_run (struct window *w, struct run *run)
2046 /* --------------------------------------------------------------------------
2047 External (RIF): Insert or delete n lines at line vpos
2048 -------------------------------------------------------------------------- */
2050 struct frame *f = XFRAME (w->frame);
2051 int x, y, width, height, from_y, to_y, bottom_y;
2053 NSTRACE (ns_scroll_run);
2055 /* begin copy from other terms */
2056 /* Get frame-relative bounding box of the text display area of W,
2057 without mode lines. Include in this box the left and right
2059 window_box (w, -1, &x, &y, &width, &height);
2061 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2062 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2063 bottom_y = y + height;
2067 /* Scrolling up. Make sure we don't copy part of the mode
2068 line at the bottom. */
2069 if (from_y + run->height > bottom_y)
2070 height = bottom_y - from_y;
2072 height = run->height;
2076 /* Scrolling down. Make sure we don't copy over the mode line.
2078 if (to_y + run->height > bottom_y)
2079 height = bottom_y - to_y;
2081 height = run->height;
2083 /* end copy from other terms */
2094 NSRect srcRect = NSMakeRect (x, from_y, width, height);
2095 NSRect dstRect = NSMakeRect (x, to_y, width, height);
2096 NSPoint dstOrigin = NSMakePoint (x, to_y);
2098 ns_focus (f, &dstRect, 1);
2099 NSCopyBits (0, srcRect , dstOrigin);
2108 ns_after_update_window_line (struct glyph_row *desired_row)
2109 /* --------------------------------------------------------------------------
2110 External (RIF): preparatory to fringe update after text was updated
2111 -------------------------------------------------------------------------- */
2113 struct window *w = updated_window;
2117 NSTRACE (ns_after_update_window_line);
2119 /* begin copy from other terms */
2122 if (!desired_row->mode_line_p && !w->pseudo_window_p)
2123 desired_row->redraw_fringe_bitmaps_p = 1;
2125 /* When a window has disappeared, make sure that no rest of
2126 full-width rows stays visible in the internal border.
2127 Under NS this is drawn inside the fringes. */
2128 if (windows_or_buffers_changed
2129 && (f = XFRAME (w->frame),
2130 width = FRAME_INTERNAL_BORDER_WIDTH (f),
2132 && (height = desired_row->visible_height,
2135 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
2137 /* Internal border is drawn below the tool bar. */
2138 if (WINDOWP (f->tool_bar_window)
2139 && w == XWINDOW (f->tool_bar_window))
2141 /* end copy from other terms */
2144 if (!desired_row->full_width_p)
2146 int x1 = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 + WINDOW_LEFT_FRINGE_WIDTH (w);
2148 int x2 = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2149 + FRAME_PIXEL_WIDTH (f) - NS_SCROLL_BAR_WIDTH (f)
2150 - WINDOW_RIGHT_FRINGE_WIDTH (w)
2151 - FRAME_INTERNAL_BORDER_WIDTH (f);
2152 ns_clear_frame_area (f, x1, y, width, height);
2153 ns_clear_frame_area (f, x2, y, width, height);
2161 ns_shift_glyphs_for_insert (struct frame *f,
2162 int x, int y, int width, int height,
2164 /* --------------------------------------------------------------------------
2165 External (RIF): copy an area horizontally, don't worry about clearing src
2166 -------------------------------------------------------------------------- */
2168 NSRect srcRect = NSMakeRect (x, y, width, height);
2169 NSRect dstRect = NSMakeRect (x+shift_by, y, width, height);
2170 NSPoint dstOrigin = dstRect.origin;
2172 NSTRACE (ns_shift_glyphs_for_insert);
2174 ns_focus (f, &dstRect, 1);
2175 NSCopyBits (0, srcRect, dstOrigin);
2181 /* ==========================================================================
2183 Character encoding and metrics
2185 ========================================================================== */
2189 ns_compute_glyph_string_overhangs (struct glyph_string *s)
2190 /* --------------------------------------------------------------------------
2191 External (RIF); compute left/right overhang of whole string and set in s
2192 -------------------------------------------------------------------------- */
2194 struct font *font = s->font;
2198 struct font_metrics metrics;
2199 unsigned int codes[2];
2200 codes[0] = *(s->char2b);
2201 codes[1] = *(s->char2b + s->nchars - 1);
2203 font->driver->text_extents (font, codes, 2, &metrics);
2204 s->left_overhang = -metrics.lbearing;
2206 = metrics.rbearing > metrics.width
2207 ? metrics.rbearing - metrics.width : 0;
2211 s->left_overhang = 0;
2212 s->right_overhang = ((struct nsfont_info *)font)->ital ?
2213 FONT_HEIGHT (font) * 0.2 : 0;
2219 /* ==========================================================================
2221 Fringe and cursor drawing
2223 ========================================================================== */
2226 extern int max_used_fringe_bitmap;
2228 ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
2229 struct draw_fringe_bitmap_params *p)
2230 /* --------------------------------------------------------------------------
2231 External (RIF); fringe-related
2232 -------------------------------------------------------------------------- */
2234 struct frame *f = XFRAME (WINDOW_FRAME (w));
2235 struct face *face = p->face;
2237 static EmacsImage **bimgs = NULL;
2238 static int nBimgs = 0;
2239 /* NS-specific: move internal border inside fringe */
2240 int x = p->bx < 0 ? p->x : p->bx;
2241 int wd = p->bx < 0 ? p->wd : p->nx;
2242 BOOL fringeOnVeryLeft
2243 = x - WINDOW_LEFT_SCROLL_BAR_COLS (w) * WINDOW_FRAME_COLUMN_WIDTH (w)
2244 - FRAME_INTERNAL_BORDER_WIDTH (f) < 10;
2245 BOOL fringeOnVeryRight
2246 = FRAME_PIXEL_WIDTH (f) - x - wd - FRAME_INTERNAL_BORDER_WIDTH (f)
2247 - WINDOW_RIGHT_SCROLL_BAR_COLS (w) * WINDOW_FRAME_COLUMN_WIDTH (w) < 10;
2248 int xAdjust = FRAME_INTERNAL_BORDER_WIDTH (f) *
2249 (fringeOnVeryLeft ? -1 : (fringeOnVeryRight ? 1 : 0));
2251 /* grow bimgs if needed */
2252 if (nBimgs < max_used_fringe_bitmap)
2254 bimgs = xrealloc (bimgs, max_used_fringe_bitmap * sizeof *bimgs);
2255 memset (bimgs + nBimgs, 0,
2256 (max_used_fringe_bitmap - nBimgs) * sizeof *bimgs);
2257 nBimgs = max_used_fringe_bitmap;
2260 /* Must clip because of partially visible lines. */
2261 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
2262 ns_clip_to_row (w, row, -1, YES);
2264 if (p->bx >= 0 && !p->overlay_p)
2266 int yAdjust = rowY - FRAME_INTERNAL_BORDER_WIDTH (f) < 5 ?
2267 -FRAME_INTERNAL_BORDER_WIDTH (f) : 0;
2268 int yIncr = FRAME_PIXEL_HEIGHT (f) - (p->by+yAdjust + p->ny) < 5 ?
2269 FRAME_INTERNAL_BORDER_WIDTH (f) : 0
2270 + (yAdjust ? FRAME_INTERNAL_BORDER_WIDTH (f) : 0);
2271 NSRect r = NSMakeRect (p->bx+xAdjust, p->by+yAdjust, p->nx, p->ny+yIncr);
2273 [ns_lookup_indexed_color(face->background, f) set];
2279 NSRect r = NSMakeRect (p->x+xAdjust, p->y, p->wd, p->h);
2280 EmacsImage *img = bimgs[p->which - 1];
2284 unsigned short *bits = p->bits + p->dh;
2287 unsigned char *cbits = xmalloc (len);
2289 for (i =0; i<len; i++)
2290 cbits[i] = ~(bits[i] & 0xff);
2291 img = [[EmacsImage alloc] initFromXBM: cbits width: 8 height: p->h
2293 bimgs[p->which - 1] = img;
2298 /* Since we composite the bitmap instead of just blitting it, we need
2299 to erase the whole background. */
2300 [ns_lookup_indexed_color(face->background, f) set];
2302 [img setXBMColor: ns_lookup_indexed_color(face->foreground, f)];
2304 fromRect: NSZeroRect
2305 operation: NSCompositeSourceOver
2315 ns_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
2316 int x, int y, int cursor_type, int cursor_width,
2317 int on_p, int active_p)
2318 /* --------------------------------------------------------------------------
2319 External call (RIF): draw cursor.
2320 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
2321 -------------------------------------------------------------------------- */
2324 int fx, fy, h, cursor_height;
2325 struct frame *f = WINDOW_XFRAME (w);
2326 struct glyph *phys_cursor_glyph;
2328 struct glyph *cursor_glyph;
2330 NSColor *hollow_color = FRAME_BACKGROUND_COLOR (f);
2332 /* If cursor is out of bounds, don't draw garbage. This can happen
2333 in mini-buffer windows when switching between echo area glyphs
2336 NSTRACE (dumpcursor);
2341 w->phys_cursor_type = cursor_type;
2342 w->phys_cursor_on_p = on_p;
2344 if (cursor_type == NO_CURSOR)
2346 w->phys_cursor_width = 0;
2350 if ((phys_cursor_glyph = get_phys_cursor_glyph (w)) == NULL)
2352 if (glyph_row->exact_window_width_line_p
2353 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
2355 glyph_row->cursor_in_fringe_p = 1;
2356 draw_fringe_bitmap (w, glyph_row, 0);
2361 /* We draw the cursor (with NSRectFill), then draw the glyph on top
2362 (other terminals do it the other way round). We must set
2363 w->phys_cursor_width to the cursor width. For bar cursors, that
2364 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
2365 get_phys_cursor_geometry (w, glyph_row, phys_cursor_glyph, &fx, &fy, &h);
2367 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
2368 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
2369 if (cursor_type == BAR_CURSOR)
2371 if (cursor_width < 1)
2372 cursor_width = max (FRAME_CURSOR_WIDTH (f), 1);
2373 w->phys_cursor_width = cursor_width;
2375 /* If we have an HBAR, "cursor_width" MAY specify height. */
2376 else if (cursor_type == HBAR_CURSOR)
2378 cursor_height = (cursor_width < 1) ? lrint (0.25 * h) : cursor_width;
2379 fy += h - cursor_height;
2383 r.origin.x = fx, r.origin.y = fy;
2385 r.size.width = w->phys_cursor_width;
2387 /* FIXME: if we overwrite the internal border area, it does not get erased;
2388 fix by truncating cursor, but better would be to erase properly */
2389 overspill = r.origin.x + r.size.width -
2390 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, WINDOW_BOX_RIGHT_EDGE_X (w)
2391 - WINDOW_TOTAL_FRINGE_WIDTH (w) - FRAME_INTERNAL_BORDER_WIDTH (f));
2393 r.size.width -= overspill;
2395 /* TODO: only needed in rare cases with last-resort font in HELLO..
2396 should we do this more efficiently? */
2397 ns_clip_to_row (w, glyph_row, -1, NO); /* do ns_focus(f, &r, 1); if remove */
2400 face = FACE_FROM_ID (f, phys_cursor_glyph->face_id);
2401 if (face && NS_FACE_BACKGROUND (face)
2402 == ns_index_color (FRAME_CURSOR_COLOR (f), f))
2404 [ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), f) set];
2405 hollow_color = FRAME_CURSOR_COLOR (f);
2408 [FRAME_CURSOR_COLOR (f) set];
2410 #ifdef NS_IMPL_COCOA
2411 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2412 atomic. Cleaner ways of doing this should be investigated.
2413 One way would be to set a global variable DRAWING_CURSOR
2414 when making the call to draw_phys..(), don't focus in that
2415 case, then move the ns_unfocus() here after that call. */
2416 NSDisableScreenUpdates ();
2419 switch (cursor_type)
2423 case FILLED_BOX_CURSOR:
2426 case HOLLOW_BOX_CURSOR:
2429 NSRectFill (NSInsetRect (r, 1, 1));
2430 [FRAME_CURSOR_COLOR (f) set];
2437 /* If the character under cursor is R2L, draw the bar cursor
2438 on the right of its glyph, rather than on the left. */
2439 cursor_glyph = get_phys_cursor_glyph (w);
2440 if ((cursor_glyph->resolved_level & 1) != 0)
2441 s.origin.x += cursor_glyph->pixel_width - s.size.width;
2448 /* draw the character under the cursor */
2449 if (cursor_type != NO_CURSOR)
2450 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
2452 #ifdef NS_IMPL_COCOA
2453 NSEnableScreenUpdates ();
2460 ns_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
2461 /* --------------------------------------------------------------------------
2462 External (RIF): Draw a vertical line.
2463 -------------------------------------------------------------------------- */
2465 struct frame *f = XFRAME (WINDOW_FRAME (w));
2467 NSRect r = NSMakeRect (x, y0, 1, y1-y0);
2469 NSTRACE (ns_draw_vertical_window_border);
2471 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
2473 [ns_lookup_indexed_color(face->foreground, f) set];
2475 ns_focus (f, &r, 1);
2482 show_hourglass (struct atimer *timer)
2484 if (hourglass_shown_p)
2489 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2491 hourglass_shown_p = 1;
2497 hide_hourglass (void)
2499 if (!hourglass_shown_p)
2504 /* TODO: remove NSProgressIndicator from all frames */
2506 hourglass_shown_p = 0;
2512 /* ==========================================================================
2514 Glyph drawing operations
2516 ========================================================================== */
2519 static inline NSRect
2520 ns_fix_rect_ibw (NSRect r, int fibw, int frame_pixel_width)
2521 /* --------------------------------------------------------------------------
2522 Under NS we draw internal borders inside fringes, and want full-width
2523 rendering to go all the way to edge. This function makes that correction.
2524 -------------------------------------------------------------------------- */
2526 if (r.origin.y <= fibw+1)
2528 r.size.height += r.origin.y;
2531 if (r.origin.x <= fibw+1)
2533 r.size.width += r.origin.x;
2536 if (frame_pixel_width - (r.origin.x+r.size.width) <= fibw+1)
2537 r.size.width += fibw;
2544 ns_get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2545 /* --------------------------------------------------------------------------
2546 Wrapper utility to account for internal border width on full-width lines,
2547 and allow top full-width rows to hit the frame top. nr should be pointer
2548 to two successive NSRects. Number of rects actually used is returned.
2549 -------------------------------------------------------------------------- */
2551 int n = get_glyph_string_clip_rects (s, nr, 2);
2552 if (s->row->full_width_p)
2554 *nr = ns_fix_rect_ibw (*nr, FRAME_INTERNAL_BORDER_WIDTH (s->f),
2555 FRAME_PIXEL_WIDTH (s->f));
2557 *nr = ns_fix_rect_ibw (*(nr+1), FRAME_INTERNAL_BORDER_WIDTH (s->f),
2558 FRAME_PIXEL_WIDTH (s->f));
2563 /* --------------------------------------------------------------------
2564 Draw a wavy line under glyph string s. The wave fills wave_height
2571 wave_height = 3 | * * * *
2572 --------------------------------------------------------------------- */
2575 ns_draw_underwave (struct glyph_string *s, CGFloat width, CGFloat x)
2577 int wave_height = 3, wave_length = 3;
2578 int y, dx, dy, odd, xmax;
2583 dy = wave_height - 1;
2587 /* Find and set clipping rectangle */
2588 waveClip = NSMakeRect (x, y, width, wave_height);
2589 [[NSGraphicsContext currentContext] saveGraphicsState];
2590 NSRectClip (waveClip);
2592 /* Draw the waves */
2593 a.x = x - ((int)(x) % dx);
2595 odd = (int)(a.x/dx) % 2;
2605 [NSBezierPath strokeLineFromPoint:a toPoint:b];
2606 a.x = b.x, a.y = b.y;
2607 b.x += dx, b.y = y + odd*dy;
2611 /* Restore previous clipping rectangle(s) */
2612 [[NSGraphicsContext currentContext] restoreGraphicsState];
2618 ns_draw_text_decoration (struct glyph_string *s, struct face *face,
2619 NSColor *defaultCol, CGFloat width, CGFloat x)
2620 /* --------------------------------------------------------------------------
2621 Draw underline, overline, and strike-through on glyph string s.
2622 -------------------------------------------------------------------------- */
2624 if (s->for_overlaps)
2628 if (face->underline_p)
2630 if (s->face->underline_type == FACE_UNDER_WAVE)
2632 if (face->underline_defaulted_p)
2635 [ns_lookup_indexed_color (face->underline_color, s->f) set];
2637 ns_draw_underwave (s, width, x);
2639 else if (s->face->underline_type == FACE_UNDER_LINE)
2643 unsigned long thickness, position;
2645 /* If the prev was underlined, match its appearance. */
2646 if (s->prev && s->prev->face->underline_p
2647 && s->prev->underline_thickness > 0)
2649 thickness = s->prev->underline_thickness;
2650 position = s->prev->underline_position;
2655 unsigned long descent;
2658 descent = s->y + s->height - s->ybase;
2660 /* Use underline thickness of font, defaulting to 1. */
2661 thickness = (font && font->underline_thickness > 0)
2662 ? font->underline_thickness : 1;
2664 /* Determine the offset of underlining from the baseline. */
2665 if (x_underline_at_descent_line)
2666 position = descent - thickness;
2667 else if (x_use_underline_position_properties
2668 && font && font->underline_position >= 0)
2669 position = font->underline_position;
2671 position = lround (font->descent / 2);
2673 position = underline_minimum_offset;
2675 position = max (position, underline_minimum_offset);
2677 /* Ensure underlining is not cropped. */
2678 if (descent <= position)
2680 position = descent - 1;
2683 else if (descent < position + thickness)
2687 s->underline_thickness = thickness;
2688 s->underline_position = position;
2690 r = NSMakeRect (x, s->ybase + position, width, thickness);
2692 if (face->underline_defaulted_p)
2695 [ns_lookup_indexed_color (face->underline_color, s->f) set];
2699 /* Do overline. We follow other terms in using a thickness of 1
2700 and ignoring overline_margin. */
2701 if (face->overline_p)
2704 r = NSMakeRect (x, s->y, width, 1);
2706 if (face->overline_color_defaulted_p)
2709 [ns_lookup_indexed_color (face->overline_color, s->f) set];
2713 /* Do strike-through. We follow other terms for thickness and
2714 vertical position.*/
2715 if (face->strike_through_p)
2720 dy = lrint ((s->height - 1) / 2);
2721 r = NSMakeRect (x, s->y + dy, width, 1);
2723 if (face->strike_through_color_defaulted_p)
2726 [ns_lookup_indexed_color (face->strike_through_color, s->f) set];
2732 ns_draw_box (NSRect r, float thickness, NSColor *col, char left_p, char right_p)
2733 /* --------------------------------------------------------------------------
2734 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2735 Note we can't just use an NSDrawRect command, because of the possibility
2736 of some sides not being drawn, and because the rect will be filled.
2737 -------------------------------------------------------------------------- */
2743 s.size.height = thickness;
2745 s.origin.y += r.size.height - thickness;
2748 s.size.height = r.size.height;
2749 s.origin.y = r.origin.y;
2751 /* left, right (optional) */
2752 s.size.width = thickness;
2757 s.origin.x += r.size.width - thickness;
2764 ns_draw_relief (NSRect r, int thickness, char raised_p,
2765 char top_p, char bottom_p, char left_p, char right_p,
2766 struct glyph_string *s)
2767 /* --------------------------------------------------------------------------
2768 Draw a relief rect inside r, optionally leaving some sides open.
2769 Note we can't just use an NSDrawBezel command, because of the possibility
2770 of some sides not being drawn, and because the rect will be filled.
2771 -------------------------------------------------------------------------- */
2773 static NSColor *baseCol = nil, *lightCol = nil, *darkCol = nil;
2774 NSColor *newBaseCol = nil;
2777 NSTRACE (ns_draw_relief);
2781 if (s->face->use_box_color_for_shadows_p)
2783 newBaseCol = ns_lookup_indexed_color (s->face->box_color, s->f);
2785 /* else if (s->first_glyph->type == IMAGE_GLYPH
2787 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2789 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2793 newBaseCol = ns_lookup_indexed_color (s->face->background, s->f);
2796 if (newBaseCol == nil)
2797 newBaseCol = [NSColor grayColor];
2799 if (newBaseCol != baseCol) /* TODO: better check */
2802 baseCol = [newBaseCol retain];
2804 lightCol = [[baseCol highlightWithLevel: 0.2] retain];
2806 darkCol = [[baseCol shadowWithLevel: 0.3] retain];
2809 [(raised_p ? lightCol : darkCol) set];
2811 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2814 sr.size.height = thickness;
2815 if (top_p) NSRectFill (sr);
2818 sr.size.height = r.size.height;
2819 sr.size.width = thickness;
2820 if (left_p) NSRectFill (sr);
2822 [(raised_p ? darkCol : lightCol) set];
2825 sr.size.width = r.size.width;
2826 sr.size.height = thickness;
2827 sr.origin.y += r.size.height - thickness;
2828 if (bottom_p) NSRectFill (sr);
2831 sr.size.height = r.size.height;
2832 sr.origin.y = r.origin.y;
2833 sr.size.width = thickness;
2834 sr.origin.x += r.size.width - thickness;
2835 if (right_p) NSRectFill (sr);
2840 ns_dumpglyphs_box_or_relief (struct glyph_string *s)
2841 /* --------------------------------------------------------------------------
2842 Function modeled after x_draw_glyph_string_box ().
2843 Sets up parameters for drawing.
2844 -------------------------------------------------------------------------- */
2846 int right_x, last_x;
2847 char left_p, right_p;
2848 struct glyph *last_glyph;
2853 if (s->hl == DRAW_MOUSE_FACE)
2855 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
2857 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2862 thickness = face->box_line_width;
2864 NSTRACE (ns_dumpglyphs_box_or_relief);
2866 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2867 ? WINDOW_RIGHT_EDGE_X (s->w)
2868 : window_box_right (s->w, s->area));
2869 last_glyph = (s->cmp || s->img
2870 ? s->first_glyph : s->first_glyph + s->nchars-1);
2872 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
2873 ? last_x - 1 : min (last_x, s->x + s->background_width) - 1));
2875 left_p = (s->first_glyph->left_box_line_p
2876 || (s->hl == DRAW_MOUSE_FACE
2877 && (s->prev == NULL || s->prev->hl != s->hl)));
2878 right_p = (last_glyph->right_box_line_p
2879 || (s->hl == DRAW_MOUSE_FACE
2880 && (s->next == NULL || s->next->hl != s->hl)));
2882 r = NSMakeRect (s->x, s->y, right_x - s->x + 1, s->height);
2884 /* expand full-width row over internal borders */
2885 if (s->row->full_width_p)
2886 r = ns_fix_rect_ibw (r, FRAME_INTERNAL_BORDER_WIDTH (s->f),
2887 FRAME_PIXEL_WIDTH (s->f));
2889 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
2890 if (s->face->box == FACE_SIMPLE_BOX && s->face->box_color)
2892 ns_draw_box (r, abs (thickness),
2893 ns_lookup_indexed_color (face->box_color, s->f),
2898 ns_draw_relief (r, abs (thickness), s->face->box == FACE_RAISED_BOX,
2899 1, 1, left_p, right_p, s);
2905 ns_maybe_dumpglyphs_background (struct glyph_string *s, char force_p)
2906 /* --------------------------------------------------------------------------
2907 Modeled after x_draw_glyph_string_background, which draws BG in
2908 certain cases. Others are left to the text rendering routine.
2909 -------------------------------------------------------------------------- */
2911 NSTRACE (ns_maybe_dumpglyphs_background);
2913 if (!s->background_filled_p/* || s->hl == DRAW_MOUSE_FACE*/)
2915 int box_line_width = max (s->face->box_line_width, 0);
2916 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
2917 || s->font_not_found_p || s->extends_to_end_of_line_p || force_p)
2920 if (s->hl == DRAW_MOUSE_FACE)
2922 face = FACE_FROM_ID (s->f,
2923 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
2925 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2928 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
2930 [(NS_FACE_BACKGROUND (face) != 0
2931 ? ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f)
2932 : FRAME_BACKGROUND_COLOR (s->f)) set];
2935 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (s->f);
2936 [[dpyinfo->bitmaps[face->stipple-1].img stippleMask] set];
2939 if (s->hl != DRAW_CURSOR)
2941 NSRect r = NSMakeRect (s->x, s->y + box_line_width,
2942 s->background_width,
2943 s->height-2*box_line_width);
2945 /* expand full-width row over internal borders */
2946 if (s->row->full_width_p)
2948 int fibw = FRAME_INTERNAL_BORDER_WIDTH (s->f);
2949 if (r.origin.y <= fibw+1 + box_line_width)
2951 r.size.height += r.origin.y;
2954 if (r.origin.x <= fibw+1)
2956 r.size.width += 2*r.origin.x;
2959 if (FRAME_PIXEL_WIDTH (s->f) - (r.origin.x + r.size.width)
2961 r.size.width += fibw;
2967 s->background_filled_p = 1;
2974 ns_dumpglyphs_image (struct glyph_string *s, NSRect r)
2975 /* --------------------------------------------------------------------------
2976 Renders an image and associated borders.
2977 -------------------------------------------------------------------------- */
2979 EmacsImage *img = s->img->pixmap;
2980 int box_line_vwidth = max (s->face->box_line_width, 0);
2981 int x = s->x, y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2982 int bg_x, bg_y, bg_height;
2989 NSTRACE (ns_dumpglyphs_image);
2991 if (s->face->box != FACE_NO_BOX
2992 && s->first_glyph->left_box_line_p && s->slice.x == 0)
2993 x += abs (s->face->box_line_width);
2996 bg_y = s->slice.y == 0 ? s->y : s->y + box_line_vwidth;
2997 bg_height = s->height;
2998 /* other terms have this, but was causing problems w/tabbar mode */
2999 /* - 2 * box_line_vwidth; */
3001 if (s->slice.x == 0) x += s->img->hmargin;
3002 if (s->slice.y == 0) y += s->img->vmargin;
3004 /* Draw BG: if we need larger area than image itself cleared, do that,
3005 otherwise, since we composite the image under NS (instead of mucking
3006 with its background color), we must clear just the image area. */
3007 if (s->hl == DRAW_MOUSE_FACE)
3009 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3011 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3014 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3016 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f) set];
3018 if (bg_height > s->slice.height || s->img->hmargin || s->img->vmargin
3019 || s->img->mask || s->img->pixmap == 0 || s->width != s->background_width)
3021 br = NSMakeRect (bg_x, bg_y, s->background_width, bg_height);
3022 s->background_filled_p = 1;
3026 br = NSMakeRect (x, y, s->slice.width, s->slice.height);
3029 /* expand full-width row over internal borders */
3030 if (s->row->full_width_p)
3032 int fibw = FRAME_INTERNAL_BORDER_WIDTH (s->f);
3033 if (br.origin.y <= fibw+1 + box_line_vwidth)
3035 br.size.height += br.origin.y;
3038 if (br.origin.x <= fibw+1 + box_line_vwidth)
3040 br.size.width += br.origin.x;
3043 if (FRAME_PIXEL_WIDTH (s->f) - (br.origin.x + br.size.width) <= fibw+1)
3044 br.size.width += fibw;
3049 /* Draw the image.. do we need to draw placeholder if img ==nil? */
3052 fromRect: NSZeroRect
3053 operation: NSCompositeSourceOver
3058 if (s->hl == DRAW_CURSOR)
3060 [FRAME_CURSOR_COLOR (s->f) set];
3061 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3062 tdCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3064 /* Currently on NS img->mask is always 0. Since
3065 get_window_cursor_type specifies a hollow box cursor when on
3066 a non-masked image we never reach this clause. But we put it
3067 in in anticipation of better support for image masks on
3069 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3073 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3076 /* Draw underline, overline, strike-through. */
3077 ns_draw_text_decoration (s, face, tdCol, br.size.width, br.origin.x);
3079 /* Draw relief, if requested */
3080 if (s->img->relief || s->hl ==DRAW_IMAGE_RAISED || s->hl ==DRAW_IMAGE_SUNKEN)
3082 if (s->hl == DRAW_IMAGE_SUNKEN || s->hl == DRAW_IMAGE_RAISED)
3084 th = tool_bar_button_relief >= 0 ?
3085 tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3086 raised_p = (s->hl == DRAW_IMAGE_RAISED);
3090 th = abs (s->img->relief);
3091 raised_p = (s->img->relief > 0);
3094 r.origin.x = x - th;
3095 r.origin.y = y - th;
3096 r.size.width = s->slice.width + 2*th-1;
3097 r.size.height = s->slice.height + 2*th-1;
3098 ns_draw_relief (r, th, raised_p,
3100 s->slice.y + s->slice.height == s->img->height,
3102 s->slice.x + s->slice.width == s->img->width, s);
3105 /* If there is no mask, the background won't be seen,
3106 so draw a rectangle on the image for the cursor.
3107 Do this for all images, getting transparency right is not reliable. */
3108 if (s->hl == DRAW_CURSOR)
3110 int thickness = abs (s->img->relief);
3111 if (thickness == 0) thickness = 1;
3112 ns_draw_box (br, thickness, FRAME_CURSOR_COLOR (s->f), 1, 1);
3118 ns_dumpglyphs_stretch (struct glyph_string *s)
3123 NSColor *fgCol, *bgCol;
3125 if (!s->background_filled_p)
3127 n = ns_get_glyph_string_clip_rect (s, r);
3128 *r = NSMakeRect (s->x, s->y, s->background_width, s->height);
3130 ns_focus (s->f, r, n);
3132 if (s->hl == DRAW_MOUSE_FACE)
3134 face = FACE_FROM_ID (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3136 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3139 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3141 bgCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3142 fgCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3146 if (!s->row->full_width_p)
3148 int overrun, leftoverrun;
3150 /* truncate to avoid overwriting fringe and/or scrollbar */
3151 overrun = max (0, (s->x + s->background_width)
3152 - (WINDOW_BOX_RIGHT_EDGE_X (s->w)
3153 - WINDOW_RIGHT_FRINGE_WIDTH (s->w)));
3154 r[i].size.width -= overrun;
3156 /* truncate to avoid overwriting to left of the window box */
3157 leftoverrun = (WINDOW_BOX_LEFT_EDGE_X (s->w)
3158 + WINDOW_LEFT_FRINGE_WIDTH (s->w)) - s->x;
3160 if (leftoverrun > 0)
3162 r[i].origin.x += leftoverrun;
3163 r[i].size.width -= leftoverrun;
3166 /* XXX: Try to work between problem where a stretch glyph on
3167 a partially-visible bottom row will clear part of the
3168 modeline, and another where list-buffers headers and similar
3169 rows erroneously have visible_height set to 0. Not sure
3170 where this is coming from as other terms seem not to show. */
3171 r[i].size.height = min (s->height, s->row->visible_height);
3174 /* expand full-width rows over internal borders */
3177 r[i] = ns_fix_rect_ibw (r[i], FRAME_INTERNAL_BORDER_WIDTH (s->f),
3178 FRAME_PIXEL_WIDTH (s->f));
3183 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3184 overwriting cursor (usually when cursor on a tab) */
3185 if (s->hl == DRAW_CURSOR)
3190 width = s->w->phys_cursor_width;
3191 r[i].size.width -= width;
3192 r[i].origin.x += width;
3196 /* Draw overlining, etc. on the cursor. */
3197 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3198 ns_draw_text_decoration (s, face, bgCol, width, x);
3200 ns_draw_text_decoration (s, face, fgCol, width, x);
3207 /* Draw overlining, etc. on the stretch glyph (or the part
3208 of the stretch glyph after the cursor). */
3209 ns_draw_text_decoration (s, face, fgCol, r[i].size.width,
3213 s->background_filled_p = 1;
3219 ns_draw_glyph_string (struct glyph_string *s)
3220 /* --------------------------------------------------------------------------
3221 External (RIF): Main draw-text call.
3222 -------------------------------------------------------------------------- */
3224 /* TODO (optimize): focus for box and contents draw */
3227 char box_drawn_p = 0;
3229 NSTRACE (ns_draw_glyph_string);
3231 if (s->next && s->right_overhang && !s->for_overlaps/*&&s->hl!=DRAW_CURSOR*/)
3234 struct glyph_string *next;
3236 for (width = 0, next = s->next;
3237 next && width < s->right_overhang;
3238 width += next->width, next = next->next)
3239 if (next->first_glyph->type != IMAGE_GLYPH)
3241 if (next->first_glyph->type != STRETCH_GLYPH)
3243 n = ns_get_glyph_string_clip_rect (s->next, r);
3244 ns_focus (s->f, r, n);
3245 ns_maybe_dumpglyphs_background (s->next, 1);
3250 ns_dumpglyphs_stretch (s->next);
3252 next->num_clips = 0;
3256 if (!s->for_overlaps && s->face->box != FACE_NO_BOX
3257 && (s->first_glyph->type == CHAR_GLYPH
3258 || s->first_glyph->type == COMPOSITE_GLYPH))
3260 n = ns_get_glyph_string_clip_rect (s, r);
3261 ns_focus (s->f, r, n);
3262 ns_maybe_dumpglyphs_background (s, 1);
3263 ns_dumpglyphs_box_or_relief (s);
3268 switch (s->first_glyph->type)
3272 n = ns_get_glyph_string_clip_rect (s, r);
3273 ns_focus (s->f, r, n);
3274 ns_dumpglyphs_image (s, r[0]);
3279 ns_dumpglyphs_stretch (s);
3283 case COMPOSITE_GLYPH:
3284 n = ns_get_glyph_string_clip_rect (s, r);
3285 ns_focus (s->f, r, n);
3287 if (s->for_overlaps || (s->cmp_from > 0
3288 && ! s->first_glyph->u.cmp.automatic))
3289 s->background_filled_p = 1;
3291 ns_maybe_dumpglyphs_background
3292 (s, s->first_glyph->type == COMPOSITE_GLYPH);
3294 ns_tmp_flags = s->hl == DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR :
3295 (s->hl == DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE :
3296 (s->for_overlaps ? NS_DUMPGLYPH_FOREGROUND :
3297 NS_DUMPGLYPH_NORMAL));
3298 ns_tmp_font = (struct nsfont_info *)s->face->font;
3299 if (ns_tmp_font == NULL)
3300 ns_tmp_font = (struct nsfont_info *)FRAME_FONT (s->f);
3302 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3304 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
3305 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
3306 NS_FACE_FOREGROUND (s->face) = tmp;
3309 ns_tmp_font->font.driver->draw
3310 (s, 0, s->nchars, s->x, s->y,
3311 (ns_tmp_flags == NS_DUMPGLYPH_NORMAL && !s->background_filled_p)
3312 || ns_tmp_flags == NS_DUMPGLYPH_MOUSEFACE);
3314 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3316 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
3317 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
3318 NS_FACE_FOREGROUND (s->face) = tmp;
3324 case GLYPHLESS_GLYPH:
3325 n = ns_get_glyph_string_clip_rect (s, r);
3326 ns_focus (s->f, r, n);
3328 if (s->for_overlaps || (s->cmp_from > 0
3329 && ! s->first_glyph->u.cmp.automatic))
3330 s->background_filled_p = 1;
3332 ns_maybe_dumpglyphs_background
3333 (s, s->first_glyph->type == COMPOSITE_GLYPH);
3335 /* Not yet implemented. */
3344 /* Draw box if not done already. */
3345 if (!s->for_overlaps && !box_drawn_p && s->face->box != FACE_NO_BOX)
3347 n = ns_get_glyph_string_clip_rect (s, r);
3348 ns_focus (s->f, r, n);
3349 ns_dumpglyphs_box_or_relief (s);
3358 /* ==========================================================================
3362 ========================================================================== */
3366 ns_send_appdefined (int value)
3367 /* --------------------------------------------------------------------------
3368 Internal: post an appdefined event which EmacsApp-sendEvent will
3369 recognize and take as a command to halt the event loop.
3370 -------------------------------------------------------------------------- */
3372 /*NSTRACE (ns_send_appdefined); */
3374 /* Only post this event if we haven't already posted one. This will end
3375 the [NXApp run] main loop after having processed all events queued at
3377 if (send_appdefined)
3381 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3382 send_appdefined = NO;
3384 /* Don't need wakeup timer any more */
3387 [timed_entry invalidate];
3388 [timed_entry release];
3392 nxev = [NSEvent otherEventWithType: NSApplicationDefined
3393 location: NSMakePoint (0, 0)
3396 windowNumber: [[NSApp mainWindow] windowNumber]
3397 context: [NSApp context]
3402 /* Post an application defined event on the event queue. When this is
3403 received the [NXApp run] will return, thus having processed all
3404 events which are currently queued. */
3405 [NSApp postEvent: nxev atStart: NO];
3410 ns_read_socket (struct terminal *terminal, int expected,
3411 struct input_event *hold_quit)
3412 /* --------------------------------------------------------------------------
3413 External (hook): Post an event to ourself and keep reading events until
3414 we read it back again. In effect process all events which were waiting.
3415 From 21+ we have to manage the event buffer ourselves.
3416 -------------------------------------------------------------------------- */
3418 struct input_event ev;
3421 /* NSTRACE (ns_read_socket); */
3423 if ([NSApp modalWindow] != nil)
3426 if (interrupt_input_blocked)
3428 interrupt_input_pending = 1;
3430 pending_signals = 1;
3435 interrupt_input_pending = 0;
3437 pending_signals = pending_atimers;
3441 n_emacs_events_pending = 0;
3444 q_event_ptr = hold_quit;
3446 /* we manage autorelease pools by allocate/reallocate each time around
3447 the loop; strict nesting is occasionally violated but seems not to
3448 matter.. earlier methods using full nesting caused major memory leaks */
3449 [outerpool release];
3450 outerpool = [[NSAutoreleasePool alloc] init];
3452 /* If have pending open-file requests, attend to the next one of those. */
3453 if (ns_pending_files && [ns_pending_files count] != 0
3454 && [(EmacsApp *)NSApp openFile: [ns_pending_files objectAtIndex: 0]])
3456 [ns_pending_files removeObjectAtIndex: 0];
3458 /* Deal with pending service requests. */
3459 else if (ns_pending_service_names && [ns_pending_service_names count] != 0
3461 NSApp fulfillService: [ns_pending_service_names objectAtIndex: 0]
3462 withArg: [ns_pending_service_args objectAtIndex: 0]])
3464 [ns_pending_service_names removeObjectAtIndex: 0];
3465 [ns_pending_service_args removeObjectAtIndex: 0];
3469 /* Run and wait for events. We must always send one NX_APPDEFINED event
3470 to ourself, otherwise [NXApp run] will never exit. */
3471 send_appdefined = YES;
3472 ns_send_appdefined (-1);
3474 if (++apploopnr != 1)
3482 nevents = n_emacs_events_pending;
3483 n_emacs_events_pending = 0;
3484 emacs_event = q_event_ptr = NULL;
3492 ns_select (int nfds, fd_set *readfds, fd_set *writefds,
3493 fd_set *exceptfds, EMACS_TIME *timeout, sigset_t *sigmask)
3494 /* --------------------------------------------------------------------------
3495 Replacement for select, checking for events
3496 -------------------------------------------------------------------------- */
3501 struct input_event event;
3504 /* NSTRACE (ns_select); */
3506 for (k = 0; readfds && k < nfds+1; k++)
3507 if (FD_ISSET(k, readfds)) ++nr;
3510 || (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0))
3511 return pselect (nfds, readfds, writefds, exceptfds, timeout, sigmask);
3513 [outerpool release];
3514 outerpool = [[NSAutoreleasePool alloc] init];
3517 send_appdefined = YES;
3520 pthread_mutex_lock (&select_mutex);
3525 select_readfds = *readfds;
3526 select_valid += SELECT_HAVE_READ;
3530 select_writefds = *writefds;
3531 select_valid += SELECT_HAVE_WRITE;
3536 select_timeout = *timeout;
3537 select_valid += SELECT_HAVE_TMO;
3540 pthread_mutex_unlock (&select_mutex);
3542 /* Inform fd_handler that select should be called */
3544 write (selfds[1], &c, 1);
3546 else if (nr == 0 && timeout)
3548 /* No file descriptor, just a timeout, no need to wake fd_handler */
3549 double time = EMACS_TIME_TO_DOUBLE (*timeout);
3550 timed_entry = [[NSTimer scheduledTimerWithTimeInterval: time
3553 @selector (timeout_handler:)
3558 else /* No timeout and no file descriptors, can this happen? */
3560 /* Send appdefined so we exit from the loop */
3561 ns_send_appdefined (-1);
3566 emacs_event = &event;
3567 if (++apploopnr != 1)
3574 if (nr > 0 && readfds)
3577 write (selfds[1], &c, 1);
3581 ev = last_appdefined_event;
3586 if ([ev type] != NSApplicationDefined)
3590 last_appdefined_event = 0;
3594 /* The NX_APPDEFINED event we received was a timeout. */
3599 /* The NX_APPDEFINED event we received was the result of
3600 at least one real input event arriving. */
3606 /* Received back from select () in fd_handler; copy the results */
3607 pthread_mutex_lock (&select_mutex);
3608 if (readfds) *readfds = select_readfds;
3609 if (writefds) *writefds = select_writefds;
3610 if (timeout) *timeout = select_timeout;
3611 pthread_mutex_unlock (&select_mutex);
3621 /* ==========================================================================
3625 ========================================================================== */
3629 ns_set_vertical_scroll_bar (struct window *window,
3630 int portion, int whole, int position)
3631 /* --------------------------------------------------------------------------
3632 External (hook): Update or add scrollbar
3633 -------------------------------------------------------------------------- */
3637 struct frame *f = XFRAME (WINDOW_FRAME (window));
3638 EmacsView *view = FRAME_NS_VIEW (f);
3639 int window_y, window_height;
3640 BOOL barOnVeryLeft, barOnVeryRight;
3641 int top, left, height, width, sb_width, sb_left;
3644 /* optimization; display engine sends WAY too many of these.. */
3645 if (!NILP (window->vertical_scroll_bar))
3647 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3648 if ([bar checkSamePosition: position portion: portion whole: whole])
3650 if (view->scrollbarsNeedingUpdate == 0)
3652 if (!windows_or_buffers_changed)
3656 view->scrollbarsNeedingUpdate--;
3660 NSTRACE (ns_set_vertical_scroll_bar);
3662 /* Get dimensions. */
3663 window_box (window, -1, 0, &window_y, 0, &window_height);
3665 height = window_height;
3666 width = WINDOW_CONFIG_SCROLL_BAR_COLS (window) * FRAME_COLUMN_WIDTH (f);
3667 left = WINDOW_SCROLL_BAR_AREA_X (window);
3669 if (top < 5) /* top scrollbar adjustment */
3671 top -= FRAME_INTERNAL_BORDER_WIDTH (f);
3672 height += FRAME_INTERNAL_BORDER_WIDTH (f);
3675 /* allow for displaying a skinnier scrollbar than char area allotted */
3676 sb_width = (WINDOW_CONFIG_SCROLL_BAR_WIDTH (window) > 0) ?
3677 WINDOW_CONFIG_SCROLL_BAR_WIDTH (window) : width;
3679 barOnVeryLeft = left < 5;
3680 barOnVeryRight = FRAME_PIXEL_WIDTH (f) - left - width < 5;
3681 sb_left = left + FRAME_INTERNAL_BORDER_WIDTH (f)
3682 * (barOnVeryLeft ? -1 : (barOnVeryRight ? 1 : 0));
3684 r = NSMakeRect (sb_left, top, sb_width, height);
3685 /* the parent view is flipped, so we need to flip y value */
3687 r.origin.y = (v.size.height - r.size.height - r.origin.y);
3689 XSETWINDOW (win, window);
3692 /* we want at least 5 lines to display a scrollbar */
3693 if (WINDOW_TOTAL_LINES (window) < 5)
3695 if (!NILP (window->vertical_scroll_bar))
3697 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3698 [bar removeFromSuperview];
3699 WSET (window, vertical_scroll_bar, Qnil);
3701 ns_clear_frame_area (f, sb_left, top, width, height);
3706 if (NILP (window->vertical_scroll_bar))
3708 ns_clear_frame_area (f, sb_left, top, width, height);
3709 bar = [[EmacsScroller alloc] initFrame: r window: win];
3710 WSET (window, vertical_scroll_bar, make_save_value (bar, 0));
3715 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3716 oldRect = [bar frame];
3717 r.size.width = oldRect.size.width;
3718 if (FRAME_LIVE_P (f) && !NSEqualRects (oldRect, r))
3720 if (oldRect.origin.x != r.origin.x)
3721 ns_clear_frame_area (f, sb_left, top, width, height);
3726 [bar setPosition: position portion: portion whole: whole];
3732 ns_condemn_scroll_bars (struct frame *f)
3733 /* --------------------------------------------------------------------------
3734 External (hook): arrange for all frame's scrollbars to be removed
3735 at next call to judge_scroll_bars, except for those redeemed.
3736 -------------------------------------------------------------------------- */
3740 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
3742 NSTRACE (ns_condemn_scroll_bars);
3744 for (i =[subviews count]-1; i >= 0; i--)
3746 view = [subviews objectAtIndex: i];
3747 if ([view isKindOfClass: [EmacsScroller class]])
3754 ns_redeem_scroll_bar (struct window *window)
3755 /* --------------------------------------------------------------------------
3756 External (hook): arrange to spare this window's scrollbar
3757 at next call to judge_scroll_bars.
3758 -------------------------------------------------------------------------- */
3761 NSTRACE (ns_redeem_scroll_bar);
3762 if (!NILP (window->vertical_scroll_bar))
3764 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
3771 ns_judge_scroll_bars (struct frame *f)
3772 /* --------------------------------------------------------------------------
3773 External (hook): destroy all scrollbars on frame that weren't
3774 redeemed after call to condemn_scroll_bars.
3775 -------------------------------------------------------------------------- */
3779 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
3780 NSTRACE (ns_judge_scroll_bars);
3781 for (i =[subviews count]-1; i >= 0; i--)
3783 view = [subviews objectAtIndex: i];
3784 if (![view isKindOfClass: [EmacsScroller class]]) continue;
3791 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
3793 /* XXX irrelevant under NS */
3798 /* ==========================================================================
3802 ========================================================================== */
3805 x_display_pixel_height (struct ns_display_info *dpyinfo)
3807 NSScreen *screen = [NSScreen mainScreen];
3808 return [screen frame].size.height;
3812 x_display_pixel_width (struct ns_display_info *dpyinfo)
3814 NSScreen *screen = [NSScreen mainScreen];
3815 return [screen frame].size.width;
3819 static Lisp_Object ns_string_to_lispmod (const char *s)
3820 /* --------------------------------------------------------------------------
3821 Convert modifier name to lisp symbol
3822 -------------------------------------------------------------------------- */
3824 if (!strncmp (SSDATA (SYMBOL_NAME (Qmeta)), s, 10))
3826 else if (!strncmp (SSDATA (SYMBOL_NAME (Qsuper)), s, 10))
3828 else if (!strncmp (SSDATA (SYMBOL_NAME (Qcontrol)), s, 10))
3830 else if (!strncmp (SSDATA (SYMBOL_NAME (Qalt)), s, 10))
3832 else if (!strncmp (SSDATA (SYMBOL_NAME (Qhyper)), s, 10))
3834 else if (!strncmp (SSDATA (SYMBOL_NAME (Qnone)), s, 10))
3842 ns_default (const char *parameter, Lisp_Object *result,
3843 Lisp_Object yesval, Lisp_Object noval,
3844 BOOL is_float, BOOL is_modstring)
3845 /* --------------------------------------------------------------------------
3846 Check a parameter value in user's preferences
3847 -------------------------------------------------------------------------- */
3849 const char *value = ns_get_defaults_value (parameter);
3855 if (c_strcasecmp (value, "YES") == 0)
3857 else if (c_strcasecmp (value, "NO") == 0)
3859 else if (is_float && (f = strtod (value, &pos), pos != value))
3860 *result = make_float (f);
3861 else if (is_modstring && value)
3862 *result = ns_string_to_lispmod (value);
3863 else fprintf (stderr,
3864 "Bad value for default \"%s\": \"%s\"\n", parameter, value);
3870 ns_initialize_display_info (struct ns_display_info *dpyinfo)
3871 /* --------------------------------------------------------------------------
3872 Initialize global info and storage for display.
3873 -------------------------------------------------------------------------- */
3875 NSScreen *screen = [NSScreen mainScreen];
3876 NSWindowDepth depth = [screen depth];
3877 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
3879 dpyinfo->resx = 72.27; /* used 75.0, but this makes pt == pixel, expected */
3880 dpyinfo->resy = 72.27;
3881 dpyinfo->color_p = ![NSDeviceWhiteColorSpace isEqualToString:
3882 NSColorSpaceFromDepth (depth)]
3883 && ![NSCalibratedWhiteColorSpace isEqualToString:
3884 NSColorSpaceFromDepth (depth)];
3885 dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth);
3886 dpyinfo->image_cache = make_image_cache ();
3887 dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table);
3888 dpyinfo->color_table->colors = NULL;
3889 dpyinfo->root_window = 42; /* a placeholder.. */
3891 hlinfo->mouse_face_mouse_frame = NULL;
3892 hlinfo->mouse_face_deferred_gc = 0;
3893 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
3894 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
3895 hlinfo->mouse_face_face_id = DEFAULT_FACE_ID;
3896 hlinfo->mouse_face_window = hlinfo->mouse_face_overlay = Qnil;
3897 hlinfo->mouse_face_hidden = 0;
3899 hlinfo->mouse_face_mouse_x = hlinfo->mouse_face_mouse_y = 0;
3900 hlinfo->mouse_face_defer = 0;
3902 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame = NULL;
3904 dpyinfo->n_fonts = 0;
3905 dpyinfo->smallest_font_height = 1;
3906 dpyinfo->smallest_char_width = 1;
3910 /* This and next define (many of the) public functions in this file. */
3911 /* x_... are generic versions in xdisp.c that we, and other terms, get away
3912 with using despite presence in the "system dependent" redisplay
3913 interface. In addition, many of the ns_ methods have code that is
3914 shared with all terms, indicating need for further refactoring. */
3915 extern frame_parm_handler ns_frame_parm_handlers[];
3916 static struct redisplay_interface ns_redisplay_interface =
3918 ns_frame_parm_handlers,
3922 x_clear_end_of_line,
3924 ns_after_update_window_line,
3925 ns_update_window_begin,
3926 ns_update_window_end,
3929 0, /* flush_display_optional */
3930 x_clear_window_mouse_face,
3931 x_get_glyph_overhangs,
3932 x_fix_overlapping_area,
3933 ns_draw_fringe_bitmap,
3934 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
3935 0, /* destroy_fringe_bitmap */
3936 ns_compute_glyph_string_overhangs,
3937 ns_draw_glyph_string, /* interface to nsfont.m */
3938 ns_define_frame_cursor,
3939 ns_clear_frame_area,
3940 ns_draw_window_cursor,
3941 ns_draw_vertical_window_border,
3942 ns_shift_glyphs_for_insert
3947 ns_delete_display (struct ns_display_info *dpyinfo)
3953 /* This function is called when the last frame on a display is deleted. */
3955 ns_delete_terminal (struct terminal *terminal)
3957 struct ns_display_info *dpyinfo = terminal->display_info.ns;
3959 /* Protect against recursive calls. delete_frame in
3960 delete_terminal calls us back when it deletes our last frame. */
3961 if (!terminal->name)
3966 x_destroy_all_bitmaps (dpyinfo);
3967 ns_delete_display (dpyinfo);
3972 static struct terminal *
3973 ns_create_terminal (struct ns_display_info *dpyinfo)
3974 /* --------------------------------------------------------------------------
3975 Set up use of NS before we make the first connection.
3976 -------------------------------------------------------------------------- */
3978 struct terminal *terminal;
3980 NSTRACE (ns_create_terminal);
3982 terminal = create_terminal ();
3984 terminal->type = output_ns;
3985 terminal->display_info.ns = dpyinfo;
3986 dpyinfo->terminal = terminal;
3988 terminal->rif = &ns_redisplay_interface;
3990 terminal->clear_frame_hook = ns_clear_frame;
3991 terminal->ins_del_lines_hook = 0; /* XXX vestigial? */
3992 terminal->delete_glyphs_hook = 0; /* XXX vestigial? */
3993 terminal->ring_bell_hook = ns_ring_bell;
3994 terminal->reset_terminal_modes_hook = ns_reset_terminal_modes;
3995 terminal->set_terminal_modes_hook = ns_set_terminal_modes;
3996 terminal->update_begin_hook = ns_update_begin;
3997 terminal->update_end_hook = ns_update_end;
3998 terminal->set_terminal_window_hook = NULL; /* XXX vestigial? */
3999 terminal->read_socket_hook = ns_read_socket;
4000 terminal->frame_up_to_date_hook = ns_frame_up_to_date;
4001 terminal->mouse_position_hook = ns_mouse_position;
4002 terminal->frame_rehighlight_hook = ns_frame_rehighlight;
4003 terminal->frame_raise_lower_hook = ns_frame_raise_lower;
4005 terminal->fullscreen_hook = 0; /* see XTfullscreen_hook */
4007 terminal->set_vertical_scroll_bar_hook = ns_set_vertical_scroll_bar;
4008 terminal->condemn_scroll_bars_hook = ns_condemn_scroll_bars;
4009 terminal->redeem_scroll_bar_hook = ns_redeem_scroll_bar;
4010 terminal->judge_scroll_bars_hook = ns_judge_scroll_bars;
4012 terminal->delete_frame_hook = x_destroy_window;
4013 terminal->delete_terminal_hook = ns_delete_terminal;
4015 terminal->scroll_region_ok = 1;
4016 terminal->char_ins_del_ok = 1;
4017 terminal->line_ins_del_ok = 1;
4018 terminal->fast_clear_end_of_line = 1;
4019 terminal->memory_below_frame = 0;
4025 struct ns_display_info *
4026 ns_term_init (Lisp_Object display_name)
4027 /* --------------------------------------------------------------------------
4028 Start the Application and get things rolling.
4029 -------------------------------------------------------------------------- */
4031 struct terminal *terminal;
4032 struct ns_display_info *dpyinfo;
4033 static int ns_initialized = 0;
4036 NSTRACE (ns_term_init);
4038 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
4039 /*GSDebugAllocationActive (YES); */
4041 handling_signal = 0;
4043 if (!ns_initialized)
4046 Fset_input_interrupt_mode (Qnil);
4048 if (selfds[0] == -1)
4050 if (pipe (selfds) == -1)
4052 fprintf (stderr, "Failed to create pipe: %s\n",
4053 emacs_strerror (errno));
4057 fcntl (selfds[0], F_SETFL, O_NONBLOCK|fcntl (selfds[0], F_GETFL));
4058 FD_ZERO (&select_readfds);
4059 FD_ZERO (&select_writefds);
4060 pthread_mutex_init (&select_mutex, NULL);
4065 ns_pending_files = [[NSMutableArray alloc] init];
4066 ns_pending_service_names = [[NSMutableArray alloc] init];
4067 ns_pending_service_args = [[NSMutableArray alloc] init];
4069 /* Start app and create the main menu, window, view.
4070 Needs to be here because ns_initialize_display_info () uses AppKit classes.
4071 The view will then ask the NSApp to stop and return to Emacs. */
4072 [EmacsApp sharedApplication];
4075 [NSApp setDelegate: NSApp];
4077 /* Start the select thread. */
4078 [NSThread detachNewThreadSelector:@selector (fd_handler:)
4082 /* debugging: log all notifications */
4083 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
4084 selector: @selector (logNotification:)
4085 name: nil object: nil]; */
4087 dpyinfo = xzalloc (sizeof *dpyinfo);
4089 ns_initialize_display_info (dpyinfo);
4090 terminal = ns_create_terminal (dpyinfo);
4092 terminal->kboard = xmalloc (sizeof *terminal->kboard);
4093 init_kboard (terminal->kboard);
4094 KSET (terminal->kboard, Vwindow_system, Qns);
4095 terminal->kboard->next_kboard = all_kboards;
4096 all_kboards = terminal->kboard;
4097 /* Don't let the initial kboard remain current longer than necessary.
4098 That would cause problems if a file loaded on startup tries to
4099 prompt in the mini-buffer. */
4100 if (current_kboard == initial_kboard)
4101 current_kboard = terminal->kboard;
4102 terminal->kboard->reference_count++;
4104 dpyinfo->next = x_display_list;
4105 x_display_list = dpyinfo;
4107 /* Put it on ns_display_name_list */
4108 ns_display_name_list = Fcons (Fcons (display_name, Qnil),
4109 ns_display_name_list);
4110 dpyinfo->name_list_element = XCAR (ns_display_name_list);
4112 terminal->name = xstrdup (SSDATA (display_name));
4116 if (!inhibit_x_resources)
4118 ns_default ("GSFontAntiAlias", &ns_antialias_text,
4121 /* this is a standard variable */
4122 ns_default ("AppleAntiAliasingThreshold", &tmp,
4123 make_float (10.0), make_float (6.0), YES, NO);
4124 ns_antialias_threshold = NILP (tmp) ? 10.0 : XFLOATINT (tmp);
4127 ns_selection_color = [[NSUserDefaults standardUserDefaults]
4128 stringForKey: @"AppleHighlightColor"];
4129 if (ns_selection_color == nil)
4130 ns_selection_color = NS_SELECTION_COLOR_DEFAULT;
4133 NSColorList *cl = [NSColorList colorListNamed: @"Emacs"];
4137 Lisp_Object color_file, color_map, color;
4141 color_file = Fexpand_file_name (build_string ("rgb.txt"),
4142 Fsymbol_value (intern ("data-directory")));
4143 if (NILP (Ffile_readable_p (color_file)))
4144 fatal ("Could not find %s.\n", SDATA (color_file));
4146 color_map = Fx_load_color_file (color_file);
4147 if (NILP (color_map))
4148 fatal ("Could not read %s.\n", SDATA (color_file));
4150 cl = [[NSColorList alloc] initWithName: @"Emacs"];
4151 for ( ; CONSP (color_map); color_map = XCDR (color_map))
4153 color = XCAR (color_map);
4154 name = SSDATA (XCAR (color));
4155 c = XINT (XCDR (color));
4157 [NSColor colorWithCalibratedRed: RED_FROM_ULONG (c) / 255.0
4158 green: GREEN_FROM_ULONG (c) / 255.0
4159 blue: BLUE_FROM_ULONG (c) / 255.0
4161 forKey: [NSString stringWithUTF8String: name]];
4163 [cl writeToFile: nil];
4168 #ifdef NS_IMPL_GNUSTEP
4169 Vwindow_system_version = build_string (gnustep_base_version);
4171 /*PSnextrelease (128, c); */
4172 char c[DBL_BUFSIZE_BOUND];
4173 int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber);
4174 Vwindow_system_version = make_unibyte_string (c, len);
4178 delete_keyboard_wait_descriptor (0);
4180 ns_app_name = [[NSProcessInfo processInfo] processName];
4182 /* Set up OS X app menu */
4183 #ifdef NS_IMPL_COCOA
4187 /* set up the application menu */
4188 svcsMenu = [[EmacsMenu alloc] initWithTitle: @"Services"];
4189 [svcsMenu setAutoenablesItems: NO];
4190 appMenu = [[EmacsMenu alloc] initWithTitle: @"Emacs"];
4191 [appMenu setAutoenablesItems: NO];
4192 mainMenu = [[EmacsMenu alloc] initWithTitle: @""];
4193 dockMenu = [[EmacsMenu alloc] initWithTitle: @""];
4195 [appMenu insertItemWithTitle: @"About Emacs"
4196 action: @selector (orderFrontStandardAboutPanel:)
4199 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 1];
4200 [appMenu insertItemWithTitle: @"Preferences..."
4201 action: @selector (showPreferencesWindow:)
4204 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 3];
4205 item = [appMenu insertItemWithTitle: @"Services"
4206 action: @selector (menuDown:)
4209 [appMenu setSubmenu: svcsMenu forItem: item];
4210 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 5];
4211 [appMenu insertItemWithTitle: @"Hide Emacs"
4212 action: @selector (hide:)
4215 item = [appMenu insertItemWithTitle: @"Hide Others"
4216 action: @selector (hideOtherApplications:)
4219 [item setKeyEquivalentModifierMask: NSCommandKeyMask | NSAlternateKeyMask];
4220 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 8];
4221 [appMenu insertItemWithTitle: @"Quit Emacs"
4222 action: @selector (terminate:)
4226 item = [mainMenu insertItemWithTitle: ns_app_name
4227 action: @selector (menuDown:)
4230 [mainMenu setSubmenu: appMenu forItem: item];
4231 [dockMenu insertItemWithTitle: @"New Frame"
4232 action: @selector (newFrame:)
4236 [NSApp setMainMenu: mainMenu];
4237 [NSApp setAppleMenu: appMenu];
4238 [NSApp setServicesMenu: svcsMenu];
4239 /* Needed at least on Cocoa, to get dock menu to show windows */
4240 [NSApp setWindowsMenu: [[NSMenu alloc] init]];
4242 [[NSNotificationCenter defaultCenter]
4243 addObserver: mainMenu
4244 selector: @selector (trackingNotification:)
4245 name: NSMenuDidBeginTrackingNotification object: mainMenu];
4246 [[NSNotificationCenter defaultCenter]
4247 addObserver: mainMenu
4248 selector: @selector (trackingNotification:)
4249 name: NSMenuDidEndTrackingNotification object: mainMenu];
4251 #endif /* MAC OS X menu setup */
4254 ns_do_open_file = YES;
4260 ns_term_shutdown (int sig)
4262 [[NSUserDefaults standardUserDefaults] synchronize];
4264 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4265 if (STRINGP (Vauto_save_list_file_name))
4266 unlink (SSDATA (Vauto_save_list_file_name));
4268 if (sig == 0 || sig == SIGTERM)
4270 [NSApp terminate: NSApp];
4272 else // force a stack trace to happen
4279 /* ==========================================================================
4281 EmacsApp implementation
4283 ========================================================================== */
4286 @implementation EmacsApp
4288 - (void)logNotification: (NSNotification *)notification
4290 const char *name = [[notification name] UTF8String];
4291 if (!strstr (name, "Update") && !strstr (name, "NSMenu")
4292 && !strstr (name, "WindowNumber"))
4293 NSLog (@"notification: '%@'", [notification name]);
4297 - (void)sendEvent: (NSEvent *)theEvent
4298 /* --------------------------------------------------------------------------
4299 Called when NSApp is running for each event received. Used to stop
4300 the loop when we choose, since there's no way to just run one iteration.
4301 -------------------------------------------------------------------------- */
4303 int type = [theEvent type];
4304 NSWindow *window = [theEvent window];
4305 /* NSTRACE (sendEvent); */
4306 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4308 #ifdef NS_IMPL_COCOA
4309 if (type == NSApplicationDefined
4310 && [theEvent data2] == NSAPP_DATA2_RUNASSCRIPT)
4318 if (type == NSCursorUpdate && window == nil)
4320 fprintf (stderr, "Dropping external cursor update event.\n");
4324 #ifdef NS_IMPL_COCOA
4325 /* pass mouse down in resize handle and subsequent drags directly to
4326 EmacsWindow so we can generate continuous redisplays */
4329 if (type == NSLeftMouseDragged)
4331 [window mouseDragged: theEvent];
4334 else if (type == NSLeftMouseUp)
4336 [window mouseUp: theEvent];
4340 else if (type == NSLeftMouseDown)
4342 NSRect r = ns_resize_handle_rect (window);
4343 if (NSPointInRect ([theEvent locationInWindow], r))
4346 [window mouseDown: theEvent];
4352 if (type == NSApplicationDefined)
4354 /* Events posted by ns_send_appdefined interrupt the run loop here.
4355 But, if a modal window is up, an appdefined can still come through,
4356 (e.g., from a makeKeyWindow event) but stopping self also stops the
4357 modal loop. Just defer it until later. */
4358 if ([NSApp modalWindow] == nil)
4360 last_appdefined_event = theEvent;
4365 send_appdefined = YES;
4369 [super sendEvent: theEvent];
4373 - (void)showPreferencesWindow: (id)sender
4375 struct frame *emacsframe = SELECTED_FRAME ();
4376 NSEvent *theEvent = [NSApp currentEvent];
4380 emacs_event->kind = NS_NONKEY_EVENT;
4381 emacs_event->code = KEY_NS_SHOW_PREFS;
4382 emacs_event->modifiers = 0;
4383 EV_TRAILER (theEvent);
4387 - (void)newFrame: (id)sender
4389 struct frame *emacsframe = SELECTED_FRAME ();
4390 NSEvent *theEvent = [NSApp currentEvent];
4394 emacs_event->kind = NS_NONKEY_EVENT;
4395 emacs_event->code = KEY_NS_NEW_FRAME;
4396 emacs_event->modifiers = 0;
4397 EV_TRAILER (theEvent);
4401 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4402 - (BOOL) openFile: (NSString *)fileName
4404 struct frame *emacsframe = SELECTED_FRAME ();
4405 NSEvent *theEvent = [NSApp currentEvent];
4410 emacs_event->kind = NS_NONKEY_EVENT;
4411 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
4412 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
4413 ns_input_line = Qnil; /* can be start or cons start,end */
4414 emacs_event->modifiers =0;
4415 EV_TRAILER (theEvent);
4421 /* **************************************************************************
4423 EmacsApp delegate implementation
4425 ************************************************************************** */
4427 - (void)applicationDidFinishLaunching: (NSNotification *)notification
4428 /* --------------------------------------------------------------------------
4429 When application is loaded, terminate event loop in ns_term_init
4430 -------------------------------------------------------------------------- */
4432 NSTRACE (applicationDidFinishLaunching);
4433 [NSApp setServicesProvider: NSApp];
4434 ns_send_appdefined (-2);
4438 /* Termination sequences:
4441 MenuBar | File | Exit:
4442 Select Quit from App menubar:
4444 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4447 Select Quit from Dock menu:
4450 Cancel -> Nothing else
4454 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4459 - (void) terminate: (id)sender
4461 struct frame *emacsframe = SELECTED_FRAME ();
4466 emacs_event->kind = NS_NONKEY_EVENT;
4467 emacs_event->code = KEY_NS_POWER_OFF;
4468 emacs_event->arg = Qt; /* mark as non-key event */
4469 EV_TRAILER ((id)nil);
4473 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
4477 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
4478 return NSTerminateNow;
4480 ret = NSRunAlertPanel(ns_app_name,
4481 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
4482 @"Save Buffers and Exit", @"Cancel", nil);
4484 if (ret == NSAlertDefaultReturn)
4485 return NSTerminateNow;
4486 else if (ret == NSAlertAlternateReturn)
4487 return NSTerminateCancel;
4488 return NSTerminateNow; /* just in case */
4492 not_in_argv (NSString *arg)
4495 const char *a = [arg UTF8String];
4496 for (k = 1; k < initial_argc; ++k)
4497 if (strcmp (a, initial_argv[k]) == 0) return 0;
4501 /* Notification from the Workspace to open a file */
4502 - (BOOL)application: sender openFile: (NSString *)file
4504 if (ns_do_open_file || not_in_argv (file))
4505 [ns_pending_files addObject: file];
4510 /* Open a file as a temporary file */
4511 - (BOOL)application: sender openTempFile: (NSString *)file
4513 if (ns_do_open_file || not_in_argv (file))
4514 [ns_pending_files addObject: file];
4519 /* Notification from the Workspace to open a file noninteractively (?) */
4520 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
4522 if (ns_do_open_file || not_in_argv (file))
4523 [ns_pending_files addObject: file];
4527 /* Notification from the Workspace to open multiple files */
4528 - (void)application: sender openFiles: (NSArray *)fileList
4530 NSEnumerator *files = [fileList objectEnumerator];
4532 /* Don't open files from the command line unconditionally,
4533 Cocoa parses the command line wrong, --option value tries to open value
4534 if --option is the last option. */
4535 while ((file = [files nextObject]) != nil)
4536 if (ns_do_open_file || not_in_argv (file))
4537 [ns_pending_files addObject: file];
4539 [self replyToOpenOrPrint: NSApplicationDelegateReplySuccess];
4544 /* Handle dock menu requests. */
4545 - (NSMenu *)applicationDockMenu: (NSApplication *) sender
4551 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4552 - (void)applicationWillBecomeActive: (NSNotification *)notification
4554 //ns_app_active=YES;
4556 - (void)applicationDidBecomeActive: (NSNotification *)notification
4558 NSTRACE (applicationDidBecomeActive);
4560 //ns_app_active=YES;
4562 ns_update_auto_hide_menu_bar ();
4563 // No constraining takes place when the application is not active.
4564 ns_constrain_all_frames ();
4566 - (void)applicationDidResignActive: (NSNotification *)notification
4569 ns_send_appdefined (-1);
4574 /* ==========================================================================
4576 EmacsApp aux handlers for managing event loop
4578 ========================================================================== */
4581 - (void)timeout_handler: (NSTimer *)timedEntry
4582 /* --------------------------------------------------------------------------
4583 The timeout specified to ns_select has passed.
4584 -------------------------------------------------------------------------- */
4586 /*NSTRACE (timeout_handler); */
4587 ns_send_appdefined (-2);
4590 - (void)fd_handler:(id)unused
4591 /* --------------------------------------------------------------------------
4592 Check data waiting on file descriptors and terminate if so
4593 -------------------------------------------------------------------------- */
4596 int waiting = 1, nfds;
4599 SELECT_TYPE readfds, writefds, *wfds;
4600 EMACS_TIME timeout, *tmo;
4602 /* NSTRACE (fd_handler); */
4610 FD_SET (selfds[0], &fds);
4611 result = select (selfds[0]+1, &fds, NULL, NULL, NULL);
4614 read (selfds[0], &c, 1);
4615 if (c == 'g') waiting = 0;
4620 pthread_mutex_lock (&select_mutex);
4623 if (select_valid & SELECT_HAVE_READ)
4624 readfds = select_readfds;
4628 if (select_valid & SELECT_HAVE_WRITE)
4630 writefds = select_writefds;
4635 if (select_valid & SELECT_HAVE_TMO)
4637 timeout = select_timeout;
4643 pthread_mutex_unlock (&select_mutex);
4645 FD_SET (selfds[0], &readfds);
4646 if (selfds[0] >= nfds) nfds = selfds[0]+1;
4648 result = pselect (nfds, &readfds, wfds, NULL, tmo, NULL);
4651 ns_send_appdefined (-2);
4652 else if (result > 0)
4654 if (FD_ISSET (selfds[0], &readfds))
4656 read (selfds[0], &c, 1);
4657 if (c == 's') waiting = 1;
4661 pthread_mutex_lock (&select_mutex);
4662 if (select_valid & SELECT_HAVE_READ)
4663 select_readfds = readfds;
4664 if (select_valid & SELECT_HAVE_WRITE)
4665 select_writefds = writefds;
4666 if (select_valid & SELECT_HAVE_TMO)
4667 select_timeout = timeout;
4668 pthread_mutex_unlock (&select_mutex);
4670 ns_send_appdefined (result);
4680 /* ==========================================================================
4684 ========================================================================== */
4686 /* called from system: queue for next pass through event loop */
4687 - (void)requestService: (NSPasteboard *)pboard
4688 userData: (NSString *)userData
4689 error: (NSString **)error
4691 [ns_pending_service_names addObject: userData];
4692 [ns_pending_service_args addObject: [NSString stringWithUTF8String:
4693 SSDATA (ns_string_from_pasteboard (pboard))]];
4697 /* called from ns_read_socket to clear queue */
4698 - (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg
4700 struct frame *emacsframe = SELECTED_FRAME ();
4701 NSEvent *theEvent = [NSApp currentEvent];
4706 emacs_event->kind = NS_NONKEY_EVENT;
4707 emacs_event->code = KEY_NS_SPI_SERVICE_CALL;
4708 ns_input_spi_name = build_string ([name UTF8String]);
4709 ns_input_spi_arg = build_string ([arg UTF8String]);
4710 emacs_event->modifiers = EV_MODIFIERS (theEvent);
4711 EV_TRAILER (theEvent);
4721 /* ==========================================================================
4723 EmacsView implementation
4725 ========================================================================== */
4728 @implementation EmacsView
4730 /* needed to inform when window closed from LISP */
4731 - (void) setWindowClosing: (BOOL)closing
4733 windowClosing = closing;
4739 NSTRACE (EmacsView_dealloc);
4745 /* called on font panel selection */
4746 - (void)changeFont: (id)sender
4748 NSEvent *e =[[self window] currentEvent];
4749 struct face *face =FRAME_DEFAULT_FACE (emacsframe);
4753 NSTRACE (changeFont);
4757 if ((newFont = [sender convertFont:
4758 ((struct nsfont_info *)face->font)->nsfont]))
4760 SET_FRAME_GARBAGED (emacsframe); /* now needed as of 2008/10 */
4762 emacs_event->kind = NS_NONKEY_EVENT;
4763 emacs_event->modifiers = 0;
4764 emacs_event->code = KEY_NS_CHANGE_FONT;
4766 size = [newFont pointSize];
4767 ns_input_fontsize = make_number (lrint (size));
4768 ns_input_font = build_string ([[newFont familyName] UTF8String]);
4774 - (BOOL)acceptsFirstResponder
4776 NSTRACE (acceptsFirstResponder);
4781 - (void)resetCursorRects
4783 NSRect visible = [self visibleRect];
4784 NSCursor *currentCursor = FRAME_POINTER_TYPE (emacsframe);
4785 NSTRACE (resetCursorRects);
4787 if (currentCursor == nil)
4788 currentCursor = [NSCursor arrowCursor];
4790 if (!NSIsEmptyRect (visible))
4791 [self addCursorRect: visible cursor: currentCursor];
4792 [currentCursor setOnMouseEntered: YES];
4797 /*****************************************************************************/
4798 /* Keyboard handling. */
4801 - (void)keyDown: (NSEvent *)theEvent
4803 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
4805 unsigned fnKeysym = 0;
4807 static NSMutableArray *nsEvArray;
4808 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
4809 static BOOL firstTime = YES;
4815 /* Rhapsody and OS X give up and down events for the arrow keys */
4816 if (ns_fake_keydown == YES)
4817 ns_fake_keydown = NO;
4818 else if ([theEvent type] != NSKeyDown)
4824 if (![[self window] isKeyWindow]
4825 && [[theEvent window] isKindOfClass: [EmacsWindow class]]
4826 /* we must avoid an infinite loop here. */
4827 && (EmacsView *)[[theEvent window] delegate] != self)
4829 /* XXX: There is an occasional condition in which, when Emacs display
4830 updates a different frame from the current one, and temporarily
4831 selects it, then processes some interrupt-driven input
4832 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
4833 for some reason that window has its first responder set to the NSView
4834 most recently updated (I guess), which is not the correct one. */
4835 [(EmacsView *)[[theEvent window] delegate] keyDown: theEvent];
4839 if (nsEvArray == nil)
4840 nsEvArray = [[NSMutableArray alloc] initWithCapacity: 1];
4842 [NSCursor setHiddenUntilMouseMoves: YES];
4844 if (hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
4846 clear_mouse_face (hlinfo);
4847 hlinfo->mouse_face_hidden = 1;
4850 if (!processingCompose)
4852 /* When using screen sharing, no left or right information is sent,
4853 so use Left key in those cases. */
4854 int is_left_key, is_right_key;
4856 code = ([[theEvent charactersIgnoringModifiers] length] == 0) ?
4857 0 : [[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
4859 /* (Carbon way: [theEvent keyCode]) */
4861 /* is it a "function key"? */
4862 fnKeysym = ns_convert_key (code);
4865 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
4866 because Emacs treats Delete and KP-Delete same (in simple.el). */
4867 if (fnKeysym == 0xFFFF && [theEvent keyCode] == 0x33)
4868 code = 0xFF08; /* backspace */
4873 /* are there modifiers? */
4874 emacs_event->modifiers = 0;
4875 flags = [theEvent modifierFlags];
4877 if (flags & NSHelpKeyMask)
4878 emacs_event->modifiers |= hyper_modifier;
4880 if (flags & NSShiftKeyMask)
4881 emacs_event->modifiers |= shift_modifier;
4883 is_right_key = (flags & NSRightCommandKeyMask) == NSRightCommandKeyMask;
4884 is_left_key = (flags & NSLeftCommandKeyMask) == NSLeftCommandKeyMask
4885 || (! is_right_key && (flags & NSCommandKeyMask) == NSCommandKeyMask);
4888 emacs_event->modifiers |= parse_solitary_modifier
4889 (EQ (ns_right_command_modifier, Qleft)
4890 ? ns_command_modifier
4891 : ns_right_command_modifier);
4895 emacs_event->modifiers |= parse_solitary_modifier
4896 (ns_command_modifier);
4898 /* if super (default), take input manager's word so things like
4899 dvorak / qwerty layout work */
4900 if (EQ (ns_command_modifier, Qsuper)
4902 && [[theEvent characters] length] != 0)
4904 /* XXX: the code we get will be unshifted, so if we have
4905 a shift modifier, must convert ourselves */
4906 if (!(flags & NSShiftKeyMask))
4907 code = [[theEvent characters] characterAtIndex: 0];
4909 /* this is ugly and also requires linking w/Carbon framework
4910 (for LMGetKbdType) so for now leave this rare (?) case
4911 undealt with.. in future look into CGEvent methods */
4914 long smv = GetScriptManagerVariable (smKeyScript);
4915 Handle uchrHandle = GetResource
4916 ('uchr', GetScriptVariable (smv, smScriptKeys));
4918 UCKeyTranslate ((UCKeyboardLayout*)*uchrHandle,
4919 [[theEvent characters] characterAtIndex: 0],
4920 kUCKeyActionDisplay,
4921 (flags & ~NSCommandKeyMask) >> 8,
4922 LMGetKbdType (), kUCKeyTranslateNoDeadKeysMask,
4923 &dummy, 1, &dummy, &code);
4930 is_right_key = (flags & NSRightControlKeyMask) == NSRightControlKeyMask;
4931 is_left_key = (flags & NSLeftControlKeyMask) == NSLeftControlKeyMask
4932 || (! is_right_key && (flags & NSControlKeyMask) == NSControlKeyMask);
4935 emacs_event->modifiers |= parse_solitary_modifier
4936 (EQ (ns_right_control_modifier, Qleft)
4937 ? ns_control_modifier
4938 : ns_right_control_modifier);
4941 emacs_event->modifiers |= parse_solitary_modifier
4942 (ns_control_modifier);
4944 if (flags & NS_FUNCTION_KEY_MASK && !fnKeysym)
4945 emacs_event->modifiers |=
4946 parse_solitary_modifier (ns_function_modifier);
4948 left_is_none = NILP (ns_alternate_modifier)
4949 || EQ (ns_alternate_modifier, Qnone);
4951 is_right_key = (flags & NSRightAlternateKeyMask)
4952 == NSRightAlternateKeyMask;
4953 is_left_key = (flags & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask
4955 && (flags & NSAlternateKeyMask) == NSAlternateKeyMask);
4959 if ((NILP (ns_right_alternate_modifier)
4960 || EQ (ns_right_alternate_modifier, Qnone)
4961 || (EQ (ns_right_alternate_modifier, Qleft) && left_is_none))
4963 { /* accept pre-interp alt comb */
4964 if ([[theEvent characters] length] > 0)
4965 code = [[theEvent characters] characterAtIndex: 0];
4966 /*HACK: clear lone shift modifier to stop next if from firing */
4967 if (emacs_event->modifiers == shift_modifier)
4968 emacs_event->modifiers = 0;
4971 emacs_event->modifiers |= parse_solitary_modifier
4972 (EQ (ns_right_alternate_modifier, Qleft)
4973 ? ns_alternate_modifier
4974 : ns_right_alternate_modifier);
4977 if (is_left_key) /* default = meta */
4979 if (left_is_none && !fnKeysym)
4980 { /* accept pre-interp alt comb */
4981 if ([[theEvent characters] length] > 0)
4982 code = [[theEvent characters] characterAtIndex: 0];
4983 /*HACK: clear lone shift modifier to stop next if from firing */
4984 if (emacs_event->modifiers == shift_modifier)
4985 emacs_event->modifiers = 0;
4988 emacs_event->modifiers |=
4989 parse_solitary_modifier (ns_alternate_modifier);
4993 fprintf (stderr, "keyDown: code =%x\tfnKey =%x\tflags = %x\tmods = %x\n",
4994 code, fnKeysym, flags, emacs_event->modifiers);
4996 /* if it was a function key or had modifiers, pass it directly to emacs */
4997 if (fnKeysym || (emacs_event->modifiers
4998 && (emacs_event->modifiers != shift_modifier)
4999 && [[theEvent charactersIgnoringModifiers] length] > 0))
5000 /*[[theEvent characters] length] */
5002 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
5004 code |= (1<<28)|(3<<16);
5005 else if (code == 0x7f)
5006 code |= (1<<28)|(3<<16);
5008 emacs_event->kind = code > 0xFF
5009 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
5011 emacs_event->code = code;
5012 EV_TRAILER (theEvent);
5018 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
5019 /* if we get here we should send the key for input manager processing */
5020 if (firstTime && [[NSInputManager currentInputManager]
5021 wantsToDelayTextChangeNotifications] == NO)
5023 "Emacs: WARNING: TextInput mgr wants marked text to be permanent!\n");
5026 if (NS_KEYLOG && !processingCompose)
5027 fprintf (stderr, "keyDown: Begin compose sequence.\n");
5029 processingCompose = YES;
5030 [nsEvArray addObject: theEvent];
5031 [self interpretKeyEvents: nsEvArray];
5032 [nsEvArray removeObject: theEvent];
5036 #ifdef NS_IMPL_COCOA
5037 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
5038 decided not to send key-down for.
5039 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
5040 This only applies on Tiger and earlier.
5041 If it matches one of these, send it on to keyDown. */
5042 -(void)keyUp: (NSEvent *)theEvent
5044 int flags = [theEvent modifierFlags];
5045 int code = [theEvent keyCode];
5046 if (floor (NSAppKitVersionNumber) <= 824 /*NSAppKitVersionNumber10_4*/ &&
5047 code == 0x30 && (flags & NSControlKeyMask) && !(flags & NSCommandKeyMask))
5050 fprintf (stderr, "keyUp: passed test");
5051 ns_fake_keydown = YES;
5052 [self keyDown: theEvent];
5058 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
5061 /* <NSTextInput>: called when done composing;
5062 NOTE: also called when we delete over working text, followed immed.
5063 by doCommandBySelector: deleteBackward: */
5064 - (void)insertText: (id)aString
5067 int len = [(NSString *)aString length];
5071 NSLog (@"insertText '%@'\tlen = %d", aString, len);
5072 processingCompose = NO;
5077 /* first, clear any working text */
5078 if (workingText != nil)
5079 [self deleteWorkingText];
5081 /* now insert the string as keystrokes */
5082 for (i =0; i<len; i++)
5084 code = [aString characterAtIndex: i];
5085 /* TODO: still need this? */
5087 code = '~'; /* 0x7E */
5088 emacs_event->modifiers = 0;
5090 = code > 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
5091 emacs_event->code = code;
5092 EV_TRAILER ((id)nil);
5097 /* <NSTextInput>: inserts display of composing characters */
5098 - (void)setMarkedText: (id)aString selectedRange: (NSRange)selRange
5100 NSString *str = [aString respondsToSelector: @selector (string)] ?
5101 [aString string] : aString;
5103 NSLog (@"setMarkedText '%@' len =%d range %d from %d", str, [str length],
5104 selRange.length, selRange.location);
5106 if (workingText != nil)
5107 [self deleteWorkingText];
5108 if ([str length] == 0)
5114 processingCompose = YES;
5115 workingText = [str copy];
5116 ns_working_text = build_string ([workingText UTF8String]);
5118 emacs_event->kind = NS_TEXT_EVENT;
5119 emacs_event->code = KEY_NS_PUT_WORKING_TEXT;
5120 EV_TRAILER ((id)nil);
5124 /* delete display of composing characters [not in <NSTextInput>] */
5125 - (void)deleteWorkingText
5127 if (workingText == nil)
5130 NSLog(@"deleteWorkingText len =%d\n", [workingText length]);
5131 [workingText release];
5133 processingCompose = NO;
5138 emacs_event->kind = NS_TEXT_EVENT;
5139 emacs_event->code = KEY_NS_UNPUT_WORKING_TEXT;
5140 EV_TRAILER ((id)nil);
5144 - (BOOL)hasMarkedText
5146 return workingText != nil;
5150 - (NSRange)markedRange
5152 NSRange rng = workingText != nil
5153 ? NSMakeRange (0, [workingText length]) : NSMakeRange (NSNotFound, 0);
5155 NSLog (@"markedRange request");
5163 NSLog (@"unmark (accept) text");
5164 [self deleteWorkingText];
5165 processingCompose = NO;
5169 /* used to position char selection windows, etc. */
5170 - (NSRect)firstRectForCharacterRange: (NSRange)theRange
5174 struct window *win = XWINDOW (FRAME_SELECTED_WINDOW (emacsframe));
5176 NSLog (@"firstRectForCharRange request");
5178 rect.size.width = theRange.length * FRAME_COLUMN_WIDTH (emacsframe);
5179 rect.size.height = FRAME_LINE_HEIGHT (emacsframe);
5180 pt.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (win, win->phys_cursor.x);
5181 pt.y = WINDOW_TO_FRAME_PIXEL_Y (win, win->phys_cursor.y
5182 +FRAME_LINE_HEIGHT (emacsframe));
5184 pt = [self convertPoint: pt toView: nil];
5185 pt = [[self window] convertBaseToScreen: pt];
5191 - (NSInteger)conversationIdentifier
5193 return (NSInteger)self;
5197 - (void)doCommandBySelector: (SEL)aSelector
5200 NSLog (@"doCommandBySelector: %@", NSStringFromSelector (aSelector));
5202 if (aSelector == @selector (deleteBackward:))
5204 /* happens when user backspaces over an ongoing composition:
5205 throw a 'delete' into the event queue */
5208 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
5209 emacs_event->code = 0xFF08;
5210 EV_TRAILER ((id)nil);
5214 - (NSArray *)validAttributesForMarkedText
5216 static NSArray *arr = nil;
5217 if (arr == nil) arr = [NSArray new];
5218 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
5222 - (NSRange)selectedRange
5225 NSLog (@"selectedRange request");
5226 return NSMakeRange (NSNotFound, 0);
5229 - (NSUInteger)characterIndexForPoint: (NSPoint)thePoint
5232 NSLog (@"characterIndexForPoint request");
5236 - (NSAttributedString *)attributedSubstringFromRange: (NSRange)theRange
5238 static NSAttributedString *str = nil;
5239 if (str == nil) str = [NSAttributedString new];
5241 NSLog (@"attributedSubstringFromRange request");
5245 /* End <NSTextInput> impl. */
5246 /*****************************************************************************/
5249 /* This is what happens when the user presses a mouse button. */
5250 - (void)mouseDown: (NSEvent *)theEvent
5252 NSPoint p = [self convertPoint: [theEvent locationInWindow] fromView: nil];
5254 NSTRACE (mouseDown);
5256 [self deleteWorkingText];
5261 last_mouse_frame = emacsframe;
5262 /* appears to be needed to prevent spurious movement events generated on
5264 last_mouse_frame->mouse_moved = 0;
5266 if ([theEvent type] == NSScrollWheel)
5268 float delta = [theEvent deltaY];
5269 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
5272 emacs_event->kind = WHEEL_EVENT;
5273 emacs_event->code = 0;
5274 emacs_event->modifiers = EV_MODIFIERS (theEvent) |
5275 ((delta > 0) ? up_modifier : down_modifier);
5279 emacs_event->kind = MOUSE_CLICK_EVENT;
5280 emacs_event->code = EV_BUTTON (theEvent);
5281 emacs_event->modifiers = EV_MODIFIERS (theEvent)
5282 | EV_UDMODIFIERS (theEvent);
5284 XSETINT (emacs_event->x, lrint (p.x));
5285 XSETINT (emacs_event->y, lrint (p.y));
5286 EV_TRAILER (theEvent);
5290 - (void)rightMouseDown: (NSEvent *)theEvent
5292 NSTRACE (rightMouseDown);
5293 [self mouseDown: theEvent];
5297 - (void)otherMouseDown: (NSEvent *)theEvent
5299 NSTRACE (otherMouseDown);
5300 [self mouseDown: theEvent];
5304 - (void)mouseUp: (NSEvent *)theEvent
5307 [self mouseDown: theEvent];
5311 - (void)rightMouseUp: (NSEvent *)theEvent
5313 NSTRACE (rightMouseUp);
5314 [self mouseDown: theEvent];
5318 - (void)otherMouseUp: (NSEvent *)theEvent
5320 NSTRACE (otherMouseUp);
5321 [self mouseDown: theEvent];
5325 - (void) scrollWheel: (NSEvent *)theEvent
5327 NSTRACE (scrollWheel);
5328 [self mouseDown: theEvent];
5332 /* Tell emacs the mouse has moved. */
5333 - (void)mouseMoved: (NSEvent *)e
5335 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
5338 // NSTRACE (mouseMoved);
5340 last_mouse_movement_time = EV_TIMESTAMP (e);
5341 last_mouse_motion_position
5342 = [self convertPoint: [e locationInWindow] fromView: nil];
5344 /* update any mouse face */
5345 if (hlinfo->mouse_face_hidden)
5347 hlinfo->mouse_face_hidden = 0;
5348 clear_mouse_face (hlinfo);
5351 /* tooltip handling */
5352 previous_help_echo_string = help_echo_string;
5353 help_echo_string = Qnil;
5355 if (!note_mouse_movement (emacsframe, last_mouse_motion_position.x,
5356 last_mouse_motion_position.y))
5357 help_echo_string = previous_help_echo_string;
5359 XSETFRAME (frame, emacsframe);
5360 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
5362 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
5363 (note_mouse_highlight), which is called through the
5364 note_mouse_movement () call above */
5365 gen_help_event (help_echo_string, frame, help_echo_window,
5366 help_echo_object, help_echo_pos);
5370 help_echo_string = Qnil;
5371 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
5374 if (emacsframe->mouse_moved && send_appdefined)
5375 ns_send_appdefined (-1);
5379 - (void)mouseDragged: (NSEvent *)e
5381 NSTRACE (mouseDragged);
5382 [self mouseMoved: e];
5386 - (void)rightMouseDragged: (NSEvent *)e
5388 NSTRACE (rightMouseDragged);
5389 [self mouseMoved: e];
5393 - (void)otherMouseDragged: (NSEvent *)e
5395 NSTRACE (otherMouseDragged);
5396 [self mouseMoved: e];
5400 - (BOOL)windowShouldClose: (id)sender
5402 NSEvent *e =[[self window] currentEvent];
5404 NSTRACE (windowShouldClose);
5405 windowClosing = YES;
5408 emacs_event->kind = DELETE_WINDOW_EVENT;
5409 emacs_event->modifiers = 0;
5410 emacs_event->code = 0;
5412 /* Don't close this window, let this be done from lisp code. */
5417 - (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
5418 /* normalize frame to gridded text size */
5420 NSTRACE (windowWillResize);
5421 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5423 cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (emacsframe,
5424 #ifdef NS_IMPL_GNUSTEP
5425 frameSize.width + 3);
5429 if (cols < MINWIDTH)
5432 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (emacsframe, frameSize.height
5433 #ifdef NS_IMPL_GNUSTEP
5434 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe) + 3
5435 - FRAME_TOOLBAR_HEIGHT (emacsframe));
5437 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
5438 - FRAME_TOOLBAR_HEIGHT (emacsframe));
5440 if (rows < MINHEIGHT)
5442 #ifdef NS_IMPL_COCOA
5444 /* this sets window title to have size in it; the wm does this under GS */
5445 NSRect r = [[self window] frame];
5446 if (r.size.height == frameSize.height && r.size.width == frameSize.width)
5457 NSWindow *window = [self window];
5460 const char *t = [[[self window] title] UTF8String];
5461 char *pos = strstr (t, " — ");
5464 old_title = xstrdup (t);
5466 size_title = xmalloc (strlen (old_title) + 40);
5467 esprintf (size_title, "%s — (%d x %d)", old_title, cols, rows);
5468 [window setTitle: [NSString stringWithUTF8String: size_title]];
5473 #endif /* NS_IMPL_COCOA */
5474 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5480 - (void)windowDidResize: (NSNotification *)notification
5482 #ifdef NS_IMPL_GNUSTEP
5483 NSWindow *theWindow = [notification object];
5485 /* in GNUstep, at least currently, it's possible to get a didResize
5486 without getting a willResize.. therefore we need to act as if we got
5487 the willResize now */
5488 NSSize sz = [theWindow frame].size;
5489 sz = [self windowWillResize: theWindow toSize: sz];
5490 #endif /* NS_IMPL_GNUSTEP */
5492 NSTRACE (windowDidResize);
5493 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5495 #ifdef NS_IMPL_COCOA
5501 #endif /* NS_IMPL_COCOA */
5503 /* Avoid loop under GNUstep due to call at beginning of this function.
5504 (x_set_window_size causes a resize which causes
5505 a "windowDidResize" which calls x_set_window_size). */
5506 #ifndef NS_IMPL_GNUSTEP
5507 if (cols > 0 && rows > 0)
5510 x_set_window_size (emacsframe, 0, cols, rows);
5513 NSWindow *window = [self window];
5514 NSRect wr = [window frame];
5515 FRAME_PIXEL_WIDTH (emacsframe) = (int)wr.size.width
5516 - emacsframe->border_width;
5517 FRAME_PIXEL_HEIGHT (emacsframe) = (int)wr.size.height
5518 - FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
5519 - FRAME_TOOLBAR_HEIGHT (emacsframe);
5520 change_frame_size (emacsframe, rows, cols, 0, 0, 1);
5521 SET_FRAME_GARBAGED (emacsframe);
5522 cancel_mouse_face (emacsframe);
5527 ns_send_appdefined (-1);
5531 - (void)windowDidBecomeKey: (NSNotification *)notification
5532 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5534 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (emacsframe);
5535 struct frame *old_focus = dpyinfo->x_focus_frame;
5537 NSTRACE (windowDidBecomeKey);
5539 if (emacsframe != old_focus)
5540 dpyinfo->x_focus_frame = emacsframe;
5542 ns_frame_rehighlight (emacsframe);
5546 emacs_event->kind = FOCUS_IN_EVENT;
5547 EV_TRAILER ((id)nil);
5552 - (void)windowDidResignKey: (NSNotification *)notification
5553 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5555 struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (emacsframe);
5556 NSTRACE (windowDidResignKey);
5558 if (dpyinfo->x_focus_frame == emacsframe)
5559 dpyinfo->x_focus_frame = 0;
5561 ns_frame_rehighlight (emacsframe);
5563 /* FIXME: for some reason needed on second and subsequent clicks away
5564 from sole-frame Emacs to get hollow box to show */
5565 if (!windowClosing && [[self window] isVisible] == YES)
5567 x_update_cursor (emacsframe, 1);
5568 x_set_frame_alpha (emacsframe);
5573 [self deleteWorkingText];
5574 emacs_event->kind = FOCUS_IN_EVENT;
5575 EV_TRAILER ((id)nil);
5580 - (void)windowWillMiniaturize: sender
5582 NSTRACE (windowWillMiniaturize);
5598 - initFrameFromEmacs: (struct frame *)f
5603 NSButton *toggleButton;
5608 NSTRACE (initFrameFromEmacs);
5611 processingCompose = NO;
5612 scrollbarsNeedingUpdate = 0;
5614 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5616 ns_userRect = NSMakeRect (0, 0, 0, 0);
5617 r = NSMakeRect (0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, f->text_cols),
5618 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, f->text_lines));
5619 [self initWithFrame: r];
5620 [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
5622 FRAME_NS_VIEW (f) = self;
5626 win = [[EmacsWindow alloc]
5627 initWithContentRect: r
5628 styleMask: (NSResizableWindowMask |
5629 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
5630 NSTitledWindowMask |
5632 NSMiniaturizableWindowMask |
5633 NSClosableWindowMask)
5634 backing: NSBackingStoreBuffered
5638 f->border_width = wr.size.width - r.size.width;
5639 FRAME_NS_TITLEBAR_HEIGHT (f) = wr.size.height - r.size.height;
5641 [win setAcceptsMouseMovedEvents: YES];
5642 [win setDelegate: self];
5643 [win useOptimizedDrawing: YES];
5645 sz.width = FRAME_COLUMN_WIDTH (f);
5646 sz.height = FRAME_LINE_HEIGHT (f);
5647 [win setResizeIncrements: sz];
5649 [[win contentView] addSubview: self];
5652 [self registerForDraggedTypes: ns_drag_types];
5655 name = [NSString stringWithUTF8String:
5656 NILP (tem) ? "Emacs" : SSDATA (tem)];
5657 [win setTitle: name];
5659 /* toolbar support */
5660 toolbar = [[EmacsToolbar alloc] initForView: self withIdentifier:
5661 [NSString stringWithFormat: @"Emacs Frame %d",
5663 [win setToolbar: toolbar];
5664 [toolbar setVisible: NO];
5665 #ifdef NS_IMPL_COCOA
5666 toggleButton = [win standardWindowButton: NSWindowToolbarButton];
5667 [toggleButton setTarget: self];
5668 [toggleButton setAction: @selector (toggleToolbar: )];
5670 FRAME_TOOLBAR_HEIGHT (f) = 0;
5674 [win setMiniwindowTitle:
5675 [NSString stringWithUTF8String: SSDATA (tem)]];
5678 NSScreen *screen = [win screen];
5681 [win setFrameTopLeftPoint: NSMakePoint
5682 (IN_BOUND (-SCREENMAX, f->left_pos, SCREENMAX),
5683 IN_BOUND (-SCREENMAX,
5684 [screen frame].size.height - NS_TOP_POS (f), SCREENMAX))];
5687 [win makeFirstResponder: self];
5689 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
5690 (FRAME_DEFAULT_FACE (emacsframe)), emacsframe);
5691 [win setBackgroundColor: col];
5692 if ([col alphaComponent] != 1.0)
5693 [win setOpaque: NO];
5695 [self allocateGState];
5697 [NSApp registerServicesMenuSendTypes: ns_send_types
5705 - (void)windowDidMove: sender
5707 NSWindow *win = [self window];
5708 NSRect r = [win frame];
5709 NSArray *screens = [NSScreen screens];
5710 NSScreen *screen = [screens objectAtIndex: 0];
5712 NSTRACE (windowDidMove);
5714 if (!emacsframe->output_data.ns)
5718 emacsframe->left_pos = r.origin.x;
5719 emacsframe->top_pos =
5720 [screen frame].size.height - (r.origin.y + r.size.height);
5725 /* Called AFTER method below, but before our windowWillResize call there leads
5726 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
5727 location so set_window_size moves the frame. */
5728 - (BOOL)windowShouldZoom: (NSWindow *)sender toFrame: (NSRect)newFrame
5730 emacsframe->output_data.ns->zooming = 1;
5735 /* Override to do something slightly nonstandard, but nice. First click on
5736 zoom button will zoom vertically. Second will zoom completely. Third
5737 returns to original. */
5738 - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender
5739 defaultFrame:(NSRect)defaultFrame
5741 NSRect result = [sender frame];
5743 NSTRACE (windowWillUseStandardFrame);
5745 if (abs (defaultFrame.size.height - result.size.height)
5746 > FRAME_LINE_HEIGHT (emacsframe))
5749 ns_userRect = result;
5750 result.size.height = defaultFrame.size.height;
5751 result.origin.y = defaultFrame.origin.y;
5755 if (abs (defaultFrame.size.width - result.size.width)
5756 > FRAME_COLUMN_WIDTH (emacsframe))
5757 result = defaultFrame; /* second click */
5761 result = ns_userRect.size.height ? ns_userRect : result;
5762 ns_userRect = NSMakeRect (0, 0, 0, 0);
5766 [self windowWillResize: sender toSize: result.size];
5771 - (void)windowDidDeminiaturize: sender
5773 NSTRACE (windowDidDeminiaturize);
5774 if (!emacsframe->output_data.ns)
5776 emacsframe->async_iconified = 0;
5777 emacsframe->async_visible = 1;
5778 windows_or_buffers_changed++;
5782 emacs_event->kind = ICONIFY_EVENT;
5783 EV_TRAILER ((id)nil);
5788 - (void)windowDidExpose: sender
5790 NSTRACE (windowDidExpose);
5791 if (!emacsframe->output_data.ns)
5793 emacsframe->async_visible = 1;
5794 SET_FRAME_GARBAGED (emacsframe);
5796 if (send_appdefined)
5797 ns_send_appdefined (-1);
5801 - (void)windowDidMiniaturize: sender
5803 NSTRACE (windowDidMiniaturize);
5804 if (!emacsframe->output_data.ns)
5807 emacsframe->async_iconified = 1;
5808 emacsframe->async_visible = 0;
5812 emacs_event->kind = ICONIFY_EVENT;
5813 EV_TRAILER ((id)nil);
5818 - (void)mouseEntered: (NSEvent *)theEvent
5820 NSTRACE (mouseEntered);
5821 last_mouse_movement_time = EV_TIMESTAMP (theEvent);
5825 - (void)mouseExited: (NSEvent *)theEvent
5827 Mouse_HLInfo *hlinfo = emacsframe ? MOUSE_HL_INFO (emacsframe) : NULL;
5829 NSTRACE (mouseExited);
5834 last_mouse_movement_time = EV_TIMESTAMP (theEvent);
5836 if (emacsframe == hlinfo->mouse_face_mouse_frame)
5838 clear_mouse_face (hlinfo);
5839 hlinfo->mouse_face_mouse_frame = 0;
5847 if (context_menu_value == -1)
5848 context_menu_value = [sender tag];
5851 NSInteger tag = [sender tag];
5852 find_and_call_menu_selection (emacsframe, emacsframe->menu_bar_items_used,
5853 emacsframe->menu_bar_vector,
5857 ns_send_appdefined (-1);
5862 - (EmacsToolbar *)toolbar
5868 /* this gets called on toolbar button click */
5869 - toolbarClicked: (id)item
5872 int idx = [item tag] * TOOL_BAR_ITEM_NSLOTS;
5874 NSTRACE (toolbarClicked);
5879 /* send first event (for some reason two needed) */
5880 theEvent = [[self window] currentEvent];
5881 emacs_event->kind = TOOL_BAR_EVENT;
5882 XSETFRAME (emacs_event->arg, emacsframe);
5883 EV_TRAILER (theEvent);
5885 emacs_event->kind = TOOL_BAR_EVENT;
5886 /* XSETINT (emacs_event->code, 0); */
5887 emacs_event->arg = AREF (emacsframe->tool_bar_items,
5888 idx + TOOL_BAR_ITEM_KEY);
5889 emacs_event->modifiers = EV_MODIFIERS (theEvent);
5890 EV_TRAILER (theEvent);
5895 - toggleToolbar: (id)sender
5900 emacs_event->kind = NS_NONKEY_EVENT;
5901 emacs_event->code = KEY_NS_TOGGLE_TOOLBAR;
5902 EV_TRAILER ((id)nil);
5907 - (void)drawRect: (NSRect)rect
5909 int x = NSMinX (rect), y = NSMinY (rect);
5910 int width = NSWidth (rect), height = NSHeight (rect);
5914 if (!emacsframe || !emacsframe->output_data.ns || ns_in_resize)
5917 ns_clear_frame_area (emacsframe, x, y, width, height);
5918 expose_frame (emacsframe, x, y, width, height);
5921 drawRect: may be called (at least in OS X 10.5) for invisible
5922 views as well for some reason. Thus, do not infer visibility
5925 emacsframe->async_visible = 1;
5926 emacsframe->async_iconified = 0;
5931 /* NSDraggingDestination protocol methods. Actually this is not really a
5932 protocol, but a category of Object. O well... */
5934 -(NSUInteger) draggingEntered: (id <NSDraggingInfo>) sender
5936 NSTRACE (draggingEntered);
5937 return NSDragOperationGeneric;
5941 -(BOOL)prepareForDragOperation: (id <NSDraggingInfo>) sender
5947 -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
5952 NSEvent *theEvent = [[self window] currentEvent];
5955 NSTRACE (performDragOperation);
5960 position = [self convertPoint: [sender draggingLocation] fromView: nil];
5961 x = lrint (position.x); y = lrint (position.y);
5963 pb = [sender draggingPasteboard];
5964 type = [pb availableTypeFromArray: ns_drag_types];
5969 else if ([type isEqualToString: NSFilenamesPboardType])
5972 NSEnumerator *fenum;
5975 if (!(files = [pb propertyListForType: type]))
5978 fenum = [files objectEnumerator];
5979 while ( (file = [fenum nextObject]) )
5981 emacs_event->kind = NS_NONKEY_EVENT;
5982 emacs_event->code = KEY_NS_DRAG_FILE;
5983 XSETINT (emacs_event->x, x);
5984 XSETINT (emacs_event->y, y);
5985 ns_input_file = append2 (ns_input_file,
5986 build_string ([file UTF8String]));
5987 emacs_event->modifiers = EV_MODIFIERS (theEvent);
5988 EV_TRAILER (theEvent);
5992 else if ([type isEqualToString: NSURLPboardType])
5997 if (!(fileURL = [NSURL URLFromPasteboard: pb]) ||
5998 [fileURL isFileURL] == NO)
6001 file = [fileURL path];
6002 emacs_event->kind = NS_NONKEY_EVENT;
6003 emacs_event->code = KEY_NS_DRAG_FILE;
6004 XSETINT (emacs_event->x, x);
6005 XSETINT (emacs_event->y, y);
6006 ns_input_file = append2 (ns_input_file, build_string ([file UTF8String]));
6007 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6008 EV_TRAILER (theEvent);
6011 else if ([type isEqualToString: NSStringPboardType]
6012 || [type isEqualToString: NSTabularTextPboardType])
6016 if (! (data = [pb stringForType: type]))
6019 emacs_event->kind = NS_NONKEY_EVENT;
6020 emacs_event->code = KEY_NS_DRAG_TEXT;
6021 XSETINT (emacs_event->x, x);
6022 XSETINT (emacs_event->y, y);
6023 ns_input_text = build_string ([data UTF8String]);
6024 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6025 EV_TRAILER (theEvent);
6028 else if ([type isEqualToString: NSColorPboardType])
6030 NSColor *c = [NSColor colorFromPasteboard: pb];
6031 emacs_event->kind = NS_NONKEY_EVENT;
6032 emacs_event->code = KEY_NS_DRAG_COLOR;
6033 XSETINT (emacs_event->x, x);
6034 XSETINT (emacs_event->y, y);
6035 ns_input_color = ns_color_to_lisp (c);
6036 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6037 EV_TRAILER (theEvent);
6040 else if ([type isEqualToString: NSFontPboardType])
6042 /* impl based on GNUstep NSTextView.m */
6043 NSData *data = [pb dataForType: NSFontPboardType];
6044 NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: data];
6045 NSFont *font = [dict objectForKey: NSFontAttributeName];
6051 emacs_event->kind = NS_NONKEY_EVENT;
6052 emacs_event->code = KEY_NS_CHANGE_FONT;
6053 XSETINT (emacs_event->x, x);
6054 XSETINT (emacs_event->y, y);
6055 ns_input_font = build_string ([[font fontName] UTF8String]);
6056 snprintf (fontSize, 10, "%f", [font pointSize]);
6057 ns_input_fontsize = build_string (fontSize);
6058 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6059 EV_TRAILER (theEvent);
6064 error ("Invalid data type in dragging pasteboard.");
6070 - (id) validRequestorForSendType: (NSString *)typeSent
6071 returnType: (NSString *)typeReturned
6073 NSTRACE (validRequestorForSendType);
6074 if (typeSent != nil && [ns_send_types indexOfObject: typeSent] != NSNotFound
6075 && typeReturned == nil)
6077 if (! NILP (ns_get_local_selection (QPRIMARY, QUTF8_STRING)))
6081 return [super validRequestorForSendType: typeSent
6082 returnType: typeReturned];
6086 /* The next two methods are part of NSServicesRequests informal protocol,
6087 supposedly called when a services menu item is chosen from this app.
6088 But this should not happen because we override the services menu with our
6089 own entries which call ns-perform-service.
6090 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
6091 So let's at least stub them out until further investigation can be done. */
6093 - (BOOL) readSelectionFromPasteboard: (NSPasteboard *)pb
6095 /* we could call ns_string_from_pasteboard(pboard) here but then it should
6096 be written into the buffer in place of the existing selection..
6097 ordinary service calls go through functions defined in ns-win.el */
6101 - (BOOL) writeSelectionToPasteboard: (NSPasteboard *)pb types: (NSArray *)types
6103 NSArray *typesDeclared;
6106 /* We only support NSStringPboardType */
6107 if ([types containsObject:NSStringPboardType] == NO) {
6111 val = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6112 if (CONSP (val) && SYMBOLP (XCAR (val)))
6115 if (CONSP (val) && NILP (XCDR (val)))
6118 if (! STRINGP (val))
6121 typesDeclared = [NSArray arrayWithObject:NSStringPboardType];
6122 [pb declareTypes:typesDeclared owner:nil];
6123 ns_string_to_pasteboard (pb, val);
6128 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
6129 (gives a miniaturized version of the window); currently we use the latter for
6130 frames whose active buffer doesn't correspond to any file
6131 (e.g., '*scratch*') */
6132 - setMiniwindowImage: (BOOL) setMini
6134 id image = [[self window] miniwindowImage];
6135 NSTRACE (setMiniwindowImage);
6137 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
6138 about "AppleDockIconEnabled" notwithstanding, however the set message
6139 below has its effect nonetheless. */
6140 if (image != emacsframe->output_data.ns->miniimage)
6142 if (image && [image isKindOfClass: [EmacsImage class]])
6144 [[self window] setMiniwindowImage:
6145 setMini ? emacsframe->output_data.ns->miniimage : nil];
6152 - (void) setRows: (int) r andColumns: (int) c
6158 @end /* EmacsView */
6162 /* ==========================================================================
6164 EmacsWindow implementation
6166 ========================================================================== */
6168 @implementation EmacsWindow
6170 #ifdef NS_IMPL_COCOA
6171 - (id)accessibilityAttributeValue:(NSString *)attribute
6173 Lisp_Object str = Qnil;
6174 struct frame *f = SELECTED_FRAME ();
6175 struct buffer *curbuf = XBUFFER (XWINDOW (f->selected_window)->buffer);
6177 if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
6178 return NSAccessibilityTextFieldRole;
6180 if ([attribute isEqualToString:NSAccessibilitySelectedTextAttribute]
6181 && curbuf && ! NILP (BVAR (curbuf, mark_active)))
6183 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6185 else if (curbuf && [attribute isEqualToString:NSAccessibilityValueAttribute])
6187 if (! NILP (BVAR (curbuf, mark_active)))
6188 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
6192 ptrdiff_t start_byte = BUF_BEGV_BYTE (curbuf);
6193 ptrdiff_t byte_range = BUF_ZV_BYTE (curbuf) - start_byte;
6194 ptrdiff_t range = BUF_ZV (curbuf) - BUF_BEGV (curbuf);
6196 if (! NILP (BVAR (curbuf, enable_multibyte_characters)))
6197 str = make_uninit_multibyte_string (range, byte_range);
6199 str = make_uninit_string (range);
6200 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
6201 Is this a problem? */
6202 memcpy (SDATA (str), BYTE_POS_ADDR (start_byte), byte_range);
6209 if (CONSP (str) && SYMBOLP (XCAR (str)))
6212 if (CONSP (str) && NILP (XCDR (str)))
6217 const char *utfStr = SSDATA (str);
6218 NSString *nsStr = [NSString stringWithUTF8String: utfStr];
6223 return [super accessibilityAttributeValue:attribute];
6225 #endif /* NS_IMPL_COCOA */
6227 /* If we have multiple monitors, one above the other, we don't want to
6228 restrict the height to just one monitor. So we override this. */
6229 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
6231 /* When making the frame visible for the first time or if there is just
6232 one screen, we want to constrain. Other times not. */
6233 NSUInteger nr_screens = [[NSScreen screens] count];
6234 struct frame *f = ((EmacsView *)[self delegate])->emacsframe;
6235 NSTRACE (constrainFrameRect);
6237 if (nr_screens == 1)
6238 return [super constrainFrameRect:frameRect toScreen:screen];
6240 if (f->output_data.ns->dont_constrain
6241 || ns_menu_bar_should_be_hidden ())
6244 f->output_data.ns->dont_constrain = 1;
6245 return [super constrainFrameRect:frameRect toScreen:screen];
6249 /* called only on resize clicks by special case in EmacsApp-sendEvent */
6250 - (void)mouseDown: (NSEvent *)theEvent
6254 NSSize size = [[theEvent window] frame].size;
6255 grabOffset = [theEvent locationInWindow];
6256 grabOffset.x = size.width - grabOffset.x;
6259 [super mouseDown: theEvent];
6264 - (void)mouseUp: (NSEvent *)theEvent
6268 struct frame *f = ((EmacsView *)[self delegate])->emacsframe;
6270 ns_set_name_as_filename (f);
6272 ns_send_appdefined (-1);
6275 [super mouseUp: theEvent];
6279 /* send resize events */
6280 - (void)mouseDragged: (NSEvent *)theEvent
6284 NSPoint p = [theEvent locationInWindow];
6285 NSSize size, vettedSize, origSize = [self frame].size;
6287 size.width = p.x + grabOffset.x;
6288 size.height = origSize.height - p.y + grabOffset.y;
6290 if (size.width == origSize.width && size.height == origSize.height)
6293 vettedSize = [[self delegate] windowWillResize: self toSize: size];
6294 [[NSNotificationCenter defaultCenter]
6295 postNotificationName: NSWindowDidResizeNotification
6299 [super mouseDragged: theEvent];
6302 @end /* EmacsWindow */
6305 /* ==========================================================================
6307 EmacsScroller implementation
6309 ========================================================================== */
6312 @implementation EmacsScroller
6314 /* for repeat button push */
6315 #define SCROLL_BAR_FIRST_DELAY 0.5
6316 #define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15)
6318 + (CGFloat) scrollerWidth
6320 /* TODO: if we want to allow variable widths, this is the place to do it,
6321 however neither GNUstep nor Cocoa support it very well */
6322 return [NSScroller scrollerWidth];
6326 - initFrame: (NSRect )r window: (Lisp_Object)nwin
6328 NSTRACE (EmacsScroller_initFrame);
6330 r.size.width = [EmacsScroller scrollerWidth];
6331 [super initWithFrame: r/*NSMakeRect (0, 0, 0, 0)*/];
6332 [self setContinuous: YES];
6333 [self setEnabled: YES];
6335 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
6336 locked against the top and bottom edges, and right edge on OS X, where
6337 scrollers are on right. */
6338 #ifdef NS_IMPL_GNUSTEP
6339 [self setAutoresizingMask: NSViewMaxXMargin | NSViewHeightSizable];
6341 [self setAutoresizingMask: NSViewMinXMargin | NSViewHeightSizable];
6346 pixel_height = NSHeight (r);
6347 if (pixel_height == 0) pixel_height = 1;
6348 min_portion = 20 / pixel_height;
6350 frame = XFRAME (XWINDOW (win)->frame);
6351 if (FRAME_LIVE_P (frame))
6354 EmacsView *view = FRAME_NS_VIEW (frame);
6355 NSView *sview = [[view window] contentView];
6356 NSArray *subs = [sview subviews];
6358 /* disable optimization stopping redraw of other scrollbars */
6359 view->scrollbarsNeedingUpdate = 0;
6360 for (i =[subs count]-1; i >= 0; i--)
6361 if ([[subs objectAtIndex: i] isKindOfClass: [EmacsScroller class]])
6362 view->scrollbarsNeedingUpdate++;
6363 [sview addSubview: self];
6366 /* [self setFrame: r]; */
6372 - (void)setFrame: (NSRect)newRect
6374 NSTRACE (EmacsScroller_setFrame);
6376 pixel_height = NSHeight (newRect);
6377 if (pixel_height == 0) pixel_height = 1;
6378 min_portion = 20 / pixel_height;
6379 [super setFrame: newRect];
6381 /* UNBLOCK_INPUT; */
6387 NSTRACE (EmacsScroller_dealloc);
6389 WSET (XWINDOW (win), vertical_scroll_bar, Qnil);
6417 /* ensure other scrollbar updates after deletion */
6418 view = (EmacsView *)FRAME_NS_VIEW (frame);
6420 view->scrollbarsNeedingUpdate++;
6421 [self removeFromSuperview];
6429 - (void)resetCursorRects
6431 NSRect visible = [self visibleRect];
6432 NSTRACE (resetCursorRects);
6434 if (!NSIsEmptyRect (visible))
6435 [self addCursorRect: visible cursor: [NSCursor arrowCursor]];
6436 [[NSCursor arrowCursor] setOnMouseEntered: YES];
6440 - (int) checkSamePosition: (int) position portion: (int) portion
6443 return em_position ==position && em_portion ==portion && em_whole ==whole
6444 && portion != whole; /* needed for resize empty buf */
6448 - setPosition: (int)position portion: (int)portion whole: (int)whole
6450 NSTRACE (setPosition);
6452 em_position = position;
6453 em_portion = portion;
6456 if (portion >= whole)
6458 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
6459 [self setKnobProportion: 1.0];
6460 [self setDoubleValue: 1.0];
6462 [self setFloatValue: 0.0 knobProportion: 1.0];
6468 portion = max ((float)whole*min_portion/pixel_height, portion);
6469 pos = (float)position / (whole - portion);
6470 por = (float)portion/whole;
6471 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
6472 [self setKnobProportion: por];
6473 [self setDoubleValue: pos];
6475 [self setFloatValue: pos knobProportion: por];
6481 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
6482 drag events will go directly to the EmacsScroller. Leaving in for now. */
6483 -(void)getMouseMotionPart: (int *)part window: (Lisp_Object *)window
6484 x: (Lisp_Object *)x y: ( Lisp_Object *)y
6486 *part = last_hit_part;
6488 XSETINT (*y, pixel_height);
6489 if ([self floatValue] > 0.999)
6490 XSETINT (*x, pixel_height);
6492 XSETINT (*x, pixel_height * [self floatValue]);
6496 /* set up emacs_event */
6497 - (void) sendScrollEventAtLoc: (float)loc fromEvent: (NSEvent *)e
6502 emacs_event->part = last_hit_part;
6503 emacs_event->code = 0;
6504 emacs_event->modifiers = EV_MODIFIERS (e) | down_modifier;
6505 emacs_event->frame_or_window = win;
6506 emacs_event->timestamp = EV_TIMESTAMP (e);
6507 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
6508 emacs_event->arg = Qnil;
6509 XSETINT (emacs_event->x, loc * pixel_height);
6510 XSETINT (emacs_event->y, pixel_height-20);
6514 n_emacs_events_pending++;
6515 kbd_buffer_store_event_hold (emacs_event, q_event_ptr);
6518 kbd_buffer_store_event (emacs_event);
6519 EVENT_INIT (*emacs_event);
6520 ns_send_appdefined (-1);
6524 /* called manually thru timer to implement repeated button action w/hold-down */
6525 - repeatScroll: (NSTimer *)scrollEntry
6527 NSEvent *e = [[self window] currentEvent];
6528 NSPoint p = [[self window] mouseLocationOutsideOfEventStream];
6529 BOOL inKnob = [self testPart: p] == NSScrollerKnob;
6531 /* clear timer if need be */
6532 if (inKnob || [scroll_repeat_entry timeInterval] == SCROLL_BAR_FIRST_DELAY)
6534 [scroll_repeat_entry invalidate];
6535 [scroll_repeat_entry release];
6536 scroll_repeat_entry = nil;
6542 = [[NSTimer scheduledTimerWithTimeInterval:
6543 SCROLL_BAR_CONTINUOUS_DELAY
6545 selector: @selector (repeatScroll:)
6551 [self sendScrollEventAtLoc: 0 fromEvent: e];
6556 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
6557 mouseDragged events without going into a modal loop. */
6558 - (void)mouseDown: (NSEvent *)e
6561 /* hitPart is only updated AFTER event is passed on */
6562 NSScrollerPart part = [self testPart: [e locationInWindow]];
6563 double inc = 0.0, loc, kloc, pos;
6566 NSTRACE (EmacsScroller_mouseDown);
6570 case NSScrollerDecrementPage:
6571 last_hit_part = scroll_bar_above_handle; inc = -1.0; break;
6572 case NSScrollerIncrementPage:
6573 last_hit_part = scroll_bar_below_handle; inc = 1.0; break;
6574 case NSScrollerDecrementLine:
6575 last_hit_part = scroll_bar_up_arrow; inc = -0.1; break;
6576 case NSScrollerIncrementLine:
6577 last_hit_part = scroll_bar_down_arrow; inc = 0.1; break;
6578 case NSScrollerKnob:
6579 last_hit_part = scroll_bar_handle; break;
6580 case NSScrollerKnobSlot: /* GNUstep-only */
6581 last_hit_part = scroll_bar_move_ratio; break;
6582 default: /* NSScrollerNoPart? */
6583 fprintf (stderr, "EmacsScoller-mouseDown: unexpected part %ld\n",
6590 pos = 0; /* ignored */
6592 /* set a timer to repeat, as we can't let superclass do this modally */
6594 = [[NSTimer scheduledTimerWithTimeInterval: SCROLL_BAR_FIRST_DELAY
6596 selector: @selector (repeatScroll:)
6603 /* handle, or on GNUstep possibly slot */
6604 NSEvent *fake_event;
6606 /* compute float loc in slot and mouse offset on knob */
6607 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
6609 loc = NSHeight (sr) - ([e locationInWindow].y - NSMinY (sr));
6615 else if (loc >= NSHeight (sr))
6617 loc = NSHeight (sr);
6625 kr = [self convertRect: [self rectForPart: NSScrollerKnob]
6627 kloc = NSHeight (kr) - ([e locationInWindow].y - NSMinY (kr));
6629 last_mouse_offset = kloc;
6631 /* if knob, tell emacs a location offset by knob pos
6632 (to indicate top of handle) */
6633 if (part == NSScrollerKnob)
6634 pos = (loc - last_mouse_offset) / NSHeight (sr);
6636 /* else this is a slot click on GNUstep: go straight there */
6637 pos = loc / NSHeight (sr);
6639 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
6640 fake_event = [NSEvent mouseEventWithType: NSLeftMouseUp
6641 location: [e locationInWindow]
6642 modifierFlags: [e modifierFlags]
6643 timestamp: [e timestamp]
6644 windowNumber: [e windowNumber]
6645 context: [e context]
6646 eventNumber: [e eventNumber]
6647 clickCount: [e clickCount]
6648 pressure: [e pressure]];
6649 [super mouseUp: fake_event];
6652 if (part != NSScrollerKnob)
6653 [self sendScrollEventAtLoc: pos fromEvent: e];
6657 /* Called as we manually track scroller drags, rather than superclass. */
6658 - (void)mouseDragged: (NSEvent *)e
6664 NSTRACE (EmacsScroller_mouseDragged);
6666 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
6668 loc = NSHeight (sr) - ([e locationInWindow].y - NSMinY (sr));
6675 else if (loc >= NSHeight (sr) + last_mouse_offset)
6677 loc = NSHeight (sr) + last_mouse_offset;
6681 pos = /*(edge ? loc :*/ (loc - last_mouse_offset) / NSHeight (sr);
6682 [self sendScrollEventAtLoc: pos fromEvent: e];
6686 - (void)mouseUp: (NSEvent *)e
6688 if (scroll_repeat_entry)
6690 [scroll_repeat_entry invalidate];
6691 [scroll_repeat_entry release];
6692 scroll_repeat_entry = nil;
6698 /* treat scrollwheel events in the bar as though they were in the main window */
6699 - (void) scrollWheel: (NSEvent *)theEvent
6701 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (frame);
6702 [view mouseDown: theEvent];
6705 @end /* EmacsScroller */
6710 /* ==========================================================================
6712 Font-related functions; these used to be in nsfaces.m
6714 ========================================================================== */
6718 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
6720 struct font *font = XFONT_OBJECT (font_object);
6723 fontset = fontset_from_font (font_object);
6724 FRAME_FONTSET (f) = fontset;
6726 if (FRAME_FONT (f) == font)
6727 /* This font is already set in frame F. There's nothing more to
6731 FRAME_FONT (f) = font;
6733 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
6734 FRAME_COLUMN_WIDTH (f) = font->average_width;
6735 FRAME_SPACE_WIDTH (f) = font->space_width;
6736 FRAME_LINE_HEIGHT (f) = font->height;
6738 compute_fringe_widths (f, 1);
6740 /* Compute the scroll bar width in character columns. */
6741 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
6743 int wid = FRAME_COLUMN_WIDTH (f);
6744 FRAME_CONFIG_SCROLL_BAR_COLS (f)
6745 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
6749 int wid = FRAME_COLUMN_WIDTH (f);
6750 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
6753 /* Now make the frame display the given font. */
6754 if (FRAME_NS_WINDOW (f) != 0)
6755 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
6761 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
6762 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
6766 ns_xlfd_to_fontname (const char *xlfd)
6767 /* --------------------------------------------------------------------------
6768 Convert an X font name (XLFD) to an NS font name.
6769 Only family is used.
6770 The string returned is temporarily allocated.
6771 -------------------------------------------------------------------------- */
6773 char *name = xmalloc (180);
6777 if (!strncmp (xlfd, "--", 2))
6778 sscanf (xlfd, "--%*[^-]-%[^-]179-", name);
6780 sscanf (xlfd, "-%*[^-]-%[^-]179-", name);
6782 /* stopgap for malformed XLFD input */
6783 if (strlen (name) == 0)
6784 strcpy (name, "Monaco");
6786 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
6787 also uppercase after '-' or ' ' */
6788 name[0] = toupper (name[0]);
6789 for (len =strlen (name), i =0; i<len; i++)
6795 name[i+1] = toupper (name[i+1]);
6797 else if (name[i] == '_')
6801 name[i+1] = toupper (name[i+1]);
6804 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
6805 ret = [[NSString stringWithUTF8String: name] UTF8String];
6812 syms_of_nsterm (void)
6814 NSTRACE (syms_of_nsterm);
6816 ns_antialias_threshold = 10.0;
6818 /* from 23+ we need to tell emacs what modifiers there are.. */
6819 DEFSYM (Qmodifier_value, "modifier-value");
6820 DEFSYM (Qalt, "alt");
6821 DEFSYM (Qhyper, "hyper");
6822 DEFSYM (Qmeta, "meta");
6823 DEFSYM (Qsuper, "super");
6824 DEFSYM (Qcontrol, "control");
6825 DEFSYM (QUTF8_STRING, "UTF8_STRING");
6827 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
6828 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
6829 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
6830 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
6831 Fput (Qcontrol, Qmodifier_value, make_number (ctrl_modifier));
6833 DEFVAR_LISP ("ns-input-file", ns_input_file,
6834 "The file specified in the last NS event.");
6835 ns_input_file =Qnil;
6837 DEFVAR_LISP ("ns-input-text", ns_input_text,
6838 "The data received in the last NS text drag event.");
6839 ns_input_text =Qnil;
6841 DEFVAR_LISP ("ns-working-text", ns_working_text,
6842 "String for visualizing working composition sequence.");
6843 ns_working_text =Qnil;
6845 DEFVAR_LISP ("ns-input-font", ns_input_font,
6846 "The font specified in the last NS event.");
6847 ns_input_font =Qnil;
6849 DEFVAR_LISP ("ns-input-fontsize", ns_input_fontsize,
6850 "The fontsize specified in the last NS event.");
6851 ns_input_fontsize =Qnil;
6853 DEFVAR_LISP ("ns-input-line", ns_input_line,
6854 "The line specified in the last NS event.");
6855 ns_input_line =Qnil;
6857 DEFVAR_LISP ("ns-input-color", ns_input_color,
6858 "The color specified in the last NS event.");
6859 ns_input_color =Qnil;
6861 DEFVAR_LISP ("ns-input-spi-name", ns_input_spi_name,
6862 "The service name specified in the last NS event.");
6863 ns_input_spi_name =Qnil;
6865 DEFVAR_LISP ("ns-input-spi-arg", ns_input_spi_arg,
6866 "The service argument specified in the last NS event.");
6867 ns_input_spi_arg =Qnil;
6869 DEFVAR_LISP ("ns-alternate-modifier", ns_alternate_modifier,
6870 "This variable describes the behavior of the alternate or option key.\n\
6871 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6872 Set to none means that the alternate / option key is not interpreted by Emacs\n\
6873 at all, allowing it to be used at a lower level for accented character entry.");
6874 ns_alternate_modifier = Qmeta;
6876 DEFVAR_LISP ("ns-right-alternate-modifier", ns_right_alternate_modifier,
6877 "This variable describes the behavior of the right alternate or option key.\n\
6878 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6879 Set to left means be the same key as `ns-alternate-modifier'.\n\
6880 Set to none means that the alternate / option key is not interpreted by Emacs\n\
6881 at all, allowing it to be used at a lower level for accented character entry.");
6882 ns_right_alternate_modifier = Qleft;
6884 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
6885 "This variable describes the behavior of the command key.\n\
6886 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
6887 ns_command_modifier = Qsuper;
6889 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
6890 "This variable describes the behavior of the right command key.\n\
6891 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6892 Set to left means be the same key as `ns-command-modifier'.\n\
6893 Set to none means that the command / option key is not interpreted by Emacs\n\
6894 at all, allowing it to be used at a lower level for accented character entry.");
6895 ns_right_command_modifier = Qleft;
6897 DEFVAR_LISP ("ns-control-modifier", ns_control_modifier,
6898 "This variable describes the behavior of the control key.\n\
6899 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
6900 ns_control_modifier = Qcontrol;
6902 DEFVAR_LISP ("ns-right-control-modifier", ns_right_control_modifier,
6903 "This variable describes the behavior of the right control key.\n\
6904 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6905 Set to left means be the same key as `ns-control-modifier'.\n\
6906 Set to none means that the control / option key is not interpreted by Emacs\n\
6907 at all, allowing it to be used at a lower level for accented character entry.");
6908 ns_right_control_modifier = Qleft;
6910 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
6911 "This variable describes the behavior of the function key (on laptops).\n\
6912 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
6913 Set to none means that the function key is not interpreted by Emacs at all,\n\
6914 allowing it to be used at a lower level for accented character entry.");
6915 ns_function_modifier = Qnone;
6917 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
6918 "Non-nil (the default) means to render text antialiased. Only has an effect on OS X Panther and above.");
6919 ns_antialias_text = Qt;
6921 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
6922 "Whether to confirm application quit using dialog.");
6923 ns_confirm_quit = Qnil;
6925 staticpro (&ns_display_name_list);
6926 ns_display_name_list = Qnil;
6928 staticpro (&last_mouse_motion_frame);
6929 last_mouse_motion_frame = Qnil;
6931 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
6932 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
6933 Only works on OSX 10.6 or later. */);
6934 ns_auto_hide_menu_bar = Qnil;
6936 /* TODO: move to common code */
6937 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
6938 doc: /* Which toolkit scroll bars Emacs uses, if any.
6939 A value of nil means Emacs doesn't use toolkit scroll bars.
6940 With the X Window system, the value is a symbol describing the
6941 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
6942 With MS Windows or Nextstep, the value is t. */);
6943 Vx_toolkit_scroll_bars = Qt;
6945 DEFVAR_BOOL ("x-use-underline-position-properties",
6946 x_use_underline_position_properties,
6947 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
6948 A value of nil means ignore them. If you encounter fonts with bogus
6949 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6950 to 4.1, set this to nil. */);
6951 x_use_underline_position_properties = 0;
6953 DEFVAR_BOOL ("x-underline-at-descent-line",
6954 x_underline_at_descent_line,
6955 doc: /* Non-nil means to draw the underline at the same place as the descent line.
6956 A value of nil means to draw the underline according to the value of the
6957 variable `x-use-underline-position-properties', which is usually at the
6958 baseline level. The default value is nil. */);
6959 x_underline_at_descent_line = 0;
6961 /* Tell emacs about this window system. */
6962 Fprovide (intern ("ns"), Qnil);