1 /* NeXT/Open/GNUstep / macOS communication module. -*- coding: utf-8 -*-
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2018 Free Software
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 (at
11 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 <https://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 macOS/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. */
36 #include <sys/types.h>
42 #include <c-strcase.h>
46 #include "blockinput.h"
47 #include "sysselect.h"
50 #include "character.h"
52 #include "composite.h"
55 #include "termhooks.h"
63 #ifdef NS_IMPL_GNUSTEP
71 static EmacsMenu *dockMenu;
73 static EmacsMenu *mainMenu;
76 /* ==========================================================================
78 NSTRACE, Trace support.
80 ========================================================================== */
84 /* The following use "volatile" since they can be accessed from
86 volatile int nstrace_num = 0;
87 volatile int nstrace_depth = 0;
89 /* When 0, no trace is emitted. This is used by NSTRACE_WHEN and
90 NSTRACE_UNLESS to silence functions called.
92 TODO: This should really be a thread-local variable, to avoid that
93 a function with disabled trace thread silence trace output in
94 another. However, in practice this seldom is a problem. */
95 volatile int nstrace_enabled_global = 1;
97 /* Called when nstrace_enabled goes out of scope. */
98 void nstrace_leave(int * pointer_to_nstrace_enabled)
100 if (*pointer_to_nstrace_enabled)
107 /* Called when nstrace_saved_enabled_global goes out of scope. */
108 void nstrace_restore_global_trace_state(int * pointer_to_saved_enabled_global)
110 nstrace_enabled_global = *pointer_to_saved_enabled_global;
114 char const * nstrace_fullscreen_type_name (int fs_type)
118 case -1: return "-1";
119 case FULLSCREEN_NONE: return "FULLSCREEN_NONE";
120 case FULLSCREEN_WIDTH: return "FULLSCREEN_WIDTH";
121 case FULLSCREEN_HEIGHT: return "FULLSCREEN_HEIGHT";
122 case FULLSCREEN_BOTH: return "FULLSCREEN_BOTH";
123 case FULLSCREEN_MAXIMIZED: return "FULLSCREEN_MAXIMIZED";
124 default: return "FULLSCREEN_?????";
130 /* ==========================================================================
132 NSColor, EmacsColor category.
134 ========================================================================== */
135 @implementation NSColor (EmacsColor)
136 + (NSColor *)colorForEmacsRed:(CGFloat)red green:(CGFloat)green
137 blue:(CGFloat)blue alpha:(CGFloat)alpha
139 #if defined (NS_IMPL_COCOA) \
140 && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
141 if (ns_use_srgb_colorspace
142 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
143 && [NSColor respondsToSelector:
144 @selector(colorWithSRGBRed:green:blue:alpha:)]
147 return [NSColor colorWithSRGBRed: red
152 return [NSColor colorWithCalibratedRed: red
158 - (NSColor *)colorUsingDefaultColorSpace
160 /* FIXMES: We're checking for colorWithSRGBRed here so this will
161 only work in the same place as in the method above. It should
162 really be a check whether we're on macOS 10.7 or above. */
163 #if defined (NS_IMPL_COCOA) \
164 && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
165 if (ns_use_srgb_colorspace
166 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
167 && [NSColor respondsToSelector:
168 @selector(colorWithSRGBRed:green:blue:alpha:)]
171 return [self colorUsingColorSpace: [NSColorSpace sRGBColorSpace]];
173 return [self colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
178 /* ==========================================================================
182 ========================================================================== */
184 /* Convert a symbol indexed with an NSxxx value to a value as defined
185 in keyboard.c (lispy_function_key). I hope this is a correct way
186 of doing things... */
187 static unsigned convert_ns_to_X_keysym[] =
189 NSHomeFunctionKey, 0x50,
190 NSLeftArrowFunctionKey, 0x51,
191 NSUpArrowFunctionKey, 0x52,
192 NSRightArrowFunctionKey, 0x53,
193 NSDownArrowFunctionKey, 0x54,
194 NSPageUpFunctionKey, 0x55,
195 NSPageDownFunctionKey, 0x56,
196 NSEndFunctionKey, 0x57,
197 NSBeginFunctionKey, 0x58,
198 NSSelectFunctionKey, 0x60,
199 NSPrintFunctionKey, 0x61,
200 NSClearLineFunctionKey, 0x0B,
201 NSExecuteFunctionKey, 0x62,
202 NSInsertFunctionKey, 0x63,
203 NSUndoFunctionKey, 0x65,
204 NSRedoFunctionKey, 0x66,
205 NSMenuFunctionKey, 0x67,
206 NSFindFunctionKey, 0x68,
207 NSHelpFunctionKey, 0x6A,
208 NSBreakFunctionKey, 0x6B,
210 NSF1FunctionKey, 0xBE,
211 NSF2FunctionKey, 0xBF,
212 NSF3FunctionKey, 0xC0,
213 NSF4FunctionKey, 0xC1,
214 NSF5FunctionKey, 0xC2,
215 NSF6FunctionKey, 0xC3,
216 NSF7FunctionKey, 0xC4,
217 NSF8FunctionKey, 0xC5,
218 NSF9FunctionKey, 0xC6,
219 NSF10FunctionKey, 0xC7,
220 NSF11FunctionKey, 0xC8,
221 NSF12FunctionKey, 0xC9,
222 NSF13FunctionKey, 0xCA,
223 NSF14FunctionKey, 0xCB,
224 NSF15FunctionKey, 0xCC,
225 NSF16FunctionKey, 0xCD,
226 NSF17FunctionKey, 0xCE,
227 NSF18FunctionKey, 0xCF,
228 NSF19FunctionKey, 0xD0,
229 NSF20FunctionKey, 0xD1,
230 NSF21FunctionKey, 0xD2,
231 NSF22FunctionKey, 0xD3,
232 NSF23FunctionKey, 0xD4,
233 NSF24FunctionKey, 0xD5,
235 NSBackspaceCharacter, 0x08, /* 8: Not on some KBs. */
236 NSDeleteCharacter, 0xFF, /* 127: Big 'delete' key upper right. */
237 NSDeleteFunctionKey, 0x9F, /* 63272: Del forw key off main array. */
239 NSTabCharacter, 0x09,
240 0x19, 0x09, /* left tab->regular since pass shift */
241 NSCarriageReturnCharacter, 0x0D,
242 NSNewlineCharacter, 0x0D,
243 NSEnterCharacter, 0x8D,
245 0x41|NSEventModifierFlagNumericPad, 0xAE, /* KP_Decimal */
246 0x43|NSEventModifierFlagNumericPad, 0xAA, /* KP_Multiply */
247 0x45|NSEventModifierFlagNumericPad, 0xAB, /* KP_Add */
248 0x4B|NSEventModifierFlagNumericPad, 0xAF, /* KP_Divide */
249 0x4E|NSEventModifierFlagNumericPad, 0xAD, /* KP_Subtract */
250 0x51|NSEventModifierFlagNumericPad, 0xBD, /* KP_Equal */
251 0x52|NSEventModifierFlagNumericPad, 0xB0, /* KP_0 */
252 0x53|NSEventModifierFlagNumericPad, 0xB1, /* KP_1 */
253 0x54|NSEventModifierFlagNumericPad, 0xB2, /* KP_2 */
254 0x55|NSEventModifierFlagNumericPad, 0xB3, /* KP_3 */
255 0x56|NSEventModifierFlagNumericPad, 0xB4, /* KP_4 */
256 0x57|NSEventModifierFlagNumericPad, 0xB5, /* KP_5 */
257 0x58|NSEventModifierFlagNumericPad, 0xB6, /* KP_6 */
258 0x59|NSEventModifierFlagNumericPad, 0xB7, /* KP_7 */
259 0x5B|NSEventModifierFlagNumericPad, 0xB8, /* KP_8 */
260 0x5C|NSEventModifierFlagNumericPad, 0xB9, /* KP_9 */
262 0x1B, 0x1B /* escape */
265 /* On macOS picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
266 the maximum font size to NOT antialias. On GNUstep there is currently
267 no way to control this behavior. */
268 float ns_antialias_threshold;
270 NSArray *ns_send_types = 0, *ns_return_types = 0;
271 static NSArray *ns_drag_types = 0;
272 NSString *ns_app_name = @"Emacs"; /* default changed later */
274 /* Display variables */
275 struct ns_display_info *x_display_list; /* Chain of existing displays */
276 long context_menu_value = 0;
279 static struct frame *ns_updating_frame;
280 static NSView *focus_view = NULL;
281 static int ns_window_num = 0;
282 #ifdef NS_IMPL_GNUSTEP
283 static NSRect uRect; // TODO: This is dead, remove it?
285 static BOOL gsaved = NO;
286 static BOOL ns_fake_keydown = NO;
288 static BOOL ns_menu_bar_is_hidden = NO;
290 /*static int debug_lock = 0; */
293 static BOOL send_appdefined = YES;
294 #define NO_APPDEFINED_DATA (-8)
295 static int last_appdefined_event_data = NO_APPDEFINED_DATA;
296 static NSTimer *timed_entry = 0;
297 static NSTimer *scroll_repeat_entry = nil;
298 static fd_set select_readfds, select_writefds;
299 enum { SELECT_HAVE_READ = 1, SELECT_HAVE_WRITE = 2, SELECT_HAVE_TMO = 4 };
300 static int select_nfds = 0, select_valid = 0;
301 static struct timespec select_timeout = { 0, 0 };
302 static int selfds[2] = { -1, -1 };
303 static pthread_mutex_t select_mutex;
304 static NSAutoreleasePool *outerpool;
305 static struct input_event *emacs_event = NULL;
306 static struct input_event *q_event_ptr = NULL;
307 static int n_emacs_events_pending = 0;
308 static NSMutableArray *ns_pending_files, *ns_pending_service_names,
309 *ns_pending_service_args;
310 static BOOL ns_do_open_file = NO;
311 static BOOL ns_last_use_native_fullscreen;
313 /* Non-zero means that a HELP_EVENT has been generated since Emacs
316 static BOOL any_help_event_p = NO;
319 struct input_event *q;
325 static NSString *represented_filename = nil;
326 static struct frame *represented_frame = 0;
330 * State for pending menu activation:
331 * MENU_NONE Normal state
332 * MENU_PENDING A menu has been clicked on, but has been canceled so we can
333 * run lisp to update the menu.
334 * MENU_OPENING Menu is up to date, and the click event is redone so the menu
338 #define MENU_PENDING 1
339 #define MENU_OPENING 2
340 static int menu_will_open_state = MENU_NONE;
342 /* Saved position for menu click. */
343 static CGPoint menu_mouse_point;
346 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
347 #define NS_FUNCTION_KEY_MASK 0x800000
348 #define NSLeftControlKeyMask (0x000001 | NSEventModifierFlagControl)
349 #define NSRightControlKeyMask (0x002000 | NSEventModifierFlagControl)
350 #define NSLeftCommandKeyMask (0x000008 | NSEventModifierFlagCommand)
351 #define NSRightCommandKeyMask (0x000010 | NSEventModifierFlagCommand)
352 #define NSLeftAlternateKeyMask (0x000020 | NSEventModifierFlagOption)
353 #define NSRightAlternateKeyMask (0x000040 | NSEventModifierFlagOption)
354 #define EV_MODIFIERS2(flags) \
355 (((flags & NSEventModifierFlagHelp) ? \
356 hyper_modifier : 0) \
357 | (!EQ (ns_right_alternate_modifier, Qleft) && \
358 ((flags & NSRightAlternateKeyMask) \
359 == NSRightAlternateKeyMask) ? \
360 parse_solitary_modifier (ns_right_alternate_modifier) : 0) \
361 | ((flags & NSEventModifierFlagOption) ? \
362 parse_solitary_modifier (ns_alternate_modifier) : 0) \
363 | ((flags & NSEventModifierFlagShift) ? \
364 shift_modifier : 0) \
365 | (!EQ (ns_right_control_modifier, Qleft) && \
366 ((flags & NSRightControlKeyMask) \
367 == NSRightControlKeyMask) ? \
368 parse_solitary_modifier (ns_right_control_modifier) : 0) \
369 | ((flags & NSEventModifierFlagControl) ? \
370 parse_solitary_modifier (ns_control_modifier) : 0) \
371 | ((flags & NS_FUNCTION_KEY_MASK) ? \
372 parse_solitary_modifier (ns_function_modifier) : 0) \
373 | (!EQ (ns_right_command_modifier, Qleft) && \
374 ((flags & NSRightCommandKeyMask) \
375 == NSRightCommandKeyMask) ? \
376 parse_solitary_modifier (ns_right_command_modifier) : 0) \
377 | ((flags & NSEventModifierFlagCommand) ? \
378 parse_solitary_modifier (ns_command_modifier):0))
379 #define EV_MODIFIERS(e) EV_MODIFIERS2 ([e modifierFlags])
381 #define EV_UDMODIFIERS(e) \
382 ((([e type] == NSEventTypeLeftMouseDown) ? down_modifier : 0) \
383 | (([e type] == NSEventTypeRightMouseDown) ? down_modifier : 0) \
384 | (([e type] == NSEventTypeOtherMouseDown) ? down_modifier : 0) \
385 | (([e type] == NSEventTypeLeftMouseDragged) ? down_modifier : 0) \
386 | (([e type] == NSEventTypeRightMouseDragged) ? down_modifier : 0) \
387 | (([e type] == NSEventTypeOtherMouseDragged) ? down_modifier : 0) \
388 | (([e type] == NSEventTypeLeftMouseUp) ? up_modifier : 0) \
389 | (([e type] == NSEventTypeRightMouseUp) ? up_modifier : 0) \
390 | (([e type] == NSEventTypeOtherMouseUp) ? up_modifier : 0))
392 #define EV_BUTTON(e) \
393 ((([e type] == NSEventTypeLeftMouseDown) || ([e type] == NSEventTypeLeftMouseUp)) ? 0 : \
394 (([e type] == NSEventTypeRightMouseDown) || ([e type] == NSEventTypeRightMouseUp)) ? 2 : \
395 [e buttonNumber] - 1)
397 /* Convert the time field to a timestamp in milliseconds. */
398 #define EV_TIMESTAMP(e) ([e timestamp] * 1000)
400 /* This is a piece of code which is common to all the event handling
401 methods. Maybe it should even be a function. */
402 #define EV_TRAILER(e) \
404 XSETFRAME (emacs_event->frame_or_window, emacsframe); \
408 #define EV_TRAILER2(e) \
410 if (e) emacs_event->timestamp = EV_TIMESTAMP (e); \
413 Lisp_Object tem = Vinhibit_quit; \
414 Vinhibit_quit = Qt; \
415 n_emacs_events_pending++; \
416 kbd_buffer_store_event_hold (emacs_event, q_event_ptr); \
417 Vinhibit_quit = tem; \
420 hold_event (emacs_event); \
421 EVENT_INIT (*emacs_event); \
422 ns_send_appdefined (-1); \
426 /* These flags will be OR'd or XOR'd with the NSWindow's styleMask
427 property depending on what we're doing. */
428 #define FRAME_DECORATED_FLAGS (NSWindowStyleMaskTitled \
429 | NSWindowStyleMaskResizable \
430 | NSWindowStyleMaskMiniaturizable \
431 | NSWindowStyleMaskClosable)
432 #define FRAME_UNDECORATED_FLAGS NSWindowStyleMaskBorderless
434 /* TODO: get rid of need for these forward declarations */
435 static void ns_condemn_scroll_bars (struct frame *f);
436 static void ns_judge_scroll_bars (struct frame *f);
439 /* ==========================================================================
443 ========================================================================== */
446 ns_set_represented_filename (NSString *fstr, struct frame *f)
448 represented_filename = [fstr retain];
449 represented_frame = f;
453 ns_init_events (struct input_event *ev)
460 ns_finish_events (void)
466 hold_event (struct input_event *event)
468 if (hold_event_q.nr == hold_event_q.cap)
470 if (hold_event_q.cap == 0) hold_event_q.cap = 10;
471 else hold_event_q.cap *= 2;
473 xrealloc (hold_event_q.q, hold_event_q.cap * sizeof *hold_event_q.q);
476 hold_event_q.q[hold_event_q.nr++] = *event;
477 /* Make sure ns_read_socket is called, i.e. we have input. */
479 send_appdefined = YES;
483 append2 (Lisp_Object list, Lisp_Object item)
484 /* --------------------------------------------------------------------------
485 Utility to append to a list
486 -------------------------------------------------------------------------- */
488 return CALLN (Fnconc, list, list1 (item));
493 ns_etc_directory (void)
494 /* If running as a self-contained app bundle, return as a string the
495 filename of the etc directory, if present; else nil. */
497 NSBundle *bundle = [NSBundle mainBundle];
498 NSString *resourceDir = [bundle resourcePath];
499 NSString *resourcePath;
500 NSFileManager *fileManager = [NSFileManager defaultManager];
503 resourcePath = [resourceDir stringByAppendingPathComponent: @"etc"];
504 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
506 if (isDir) return [resourcePath UTF8String];
514 /* If running as a self-contained app bundle, return as a path string
515 the filenames of the libexec and bin directories, ie libexec:bin.
516 Otherwise, return nil.
517 Normally, Emacs does not add its own bin/ directory to the PATH.
518 However, a self-contained NS build has a different layout, with
519 bin/ and libexec/ subdirectories in the directory that contains
521 We put libexec first, because init_callproc_1 uses the first
522 element to initialize exec-directory. An alternative would be
523 for init_callproc to check for invocation-directory/libexec.
526 NSBundle *bundle = [NSBundle mainBundle];
527 NSString *resourceDir = [bundle resourcePath];
528 NSString *binDir = [bundle bundlePath];
529 NSString *resourcePath, *resourcePaths;
531 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
532 NSFileManager *fileManager = [NSFileManager defaultManager];
534 NSEnumerator *pathEnum;
537 range = [resourceDir rangeOfString: @"Contents"];
538 if (range.location != NSNotFound)
540 binDir = [binDir stringByAppendingPathComponent: @"Contents"];
542 binDir = [binDir stringByAppendingPathComponent: @"MacOS"];
546 paths = [binDir stringsByAppendingPaths:
547 [NSArray arrayWithObjects: @"libexec", @"bin", nil]];
548 pathEnum = [paths objectEnumerator];
551 while ((resourcePath = [pathEnum nextObject]))
553 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
556 if ([resourcePaths length] > 0)
558 = [resourcePaths stringByAppendingString: pathSeparator];
560 = [resourcePaths stringByAppendingString: resourcePath];
563 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
571 /* If running as a self-contained app bundle, return as a path string
572 the filenames of the site-lisp and lisp directories.
573 Ie, site-lisp:lisp. Otherwise, return nil. */
575 NSBundle *bundle = [NSBundle mainBundle];
576 NSString *resourceDir = [bundle resourcePath];
577 NSString *resourcePath, *resourcePaths;
578 NSString *pathSeparator = [NSString stringWithFormat: @"%c", SEPCHAR];
579 NSFileManager *fileManager = [NSFileManager defaultManager];
581 NSArray *paths = [resourceDir stringsByAppendingPaths:
582 [NSArray arrayWithObjects:
583 @"site-lisp", @"lisp", nil]];
584 NSEnumerator *pathEnum = [paths objectEnumerator];
587 /* Hack to skip site-lisp. */
588 if (no_site_lisp) resourcePath = [pathEnum nextObject];
590 while ((resourcePath = [pathEnum nextObject]))
592 if ([fileManager fileExistsAtPath: resourcePath isDirectory: &isDir])
595 if ([resourcePaths length] > 0)
597 = [resourcePaths stringByAppendingString: pathSeparator];
599 = [resourcePaths stringByAppendingString: resourcePath];
602 if ([resourcePaths length] > 0) return [resourcePaths UTF8String];
609 ns_init_locale (void)
610 /* macOS doesn't set any environment variables for the locale when run
611 from the GUI. Get the locale from the OS and set LANG. */
613 NSLocale *locale = [NSLocale currentLocale];
615 NSTRACE ("ns_init_locale");
619 /* It seems macOS should probably use UTF-8 everywhere.
620 'localeIdentifier' does not specify the encoding, and I can't
621 find any way to get the OS to tell us which encoding to use,
622 so hard-code '.UTF-8'. */
623 NSString *localeID = [NSString stringWithFormat:@"%@.UTF-8",
624 [locale localeIdentifier]];
626 /* Set LANG to locale, but not if LANG is already set. */
627 setenv("LANG", [localeID UTF8String], 0);
629 @catch (NSException *e)
631 NSLog (@"Locale detection failed: %@: %@", [e name], [e reason]);
637 ns_release_object (void *obj)
638 /* --------------------------------------------------------------------------
639 Release an object (callable from C)
640 -------------------------------------------------------------------------- */
647 ns_retain_object (void *obj)
648 /* --------------------------------------------------------------------------
649 Retain an object (callable from C)
650 -------------------------------------------------------------------------- */
657 ns_alloc_autorelease_pool (void)
658 /* --------------------------------------------------------------------------
659 Allocate a pool for temporary objects (callable from C)
660 -------------------------------------------------------------------------- */
662 return [[NSAutoreleasePool alloc] init];
667 ns_release_autorelease_pool (void *pool)
668 /* --------------------------------------------------------------------------
669 Free a pool and temporary objects it refers to (callable from C)
670 -------------------------------------------------------------------------- */
672 ns_release_object (pool);
677 ns_menu_bar_should_be_hidden (void)
678 /* True, if the menu bar should be hidden. */
680 return !NILP (ns_auto_hide_menu_bar)
681 && [NSApp respondsToSelector:@selector(setPresentationOptions:)];
694 static struct EmacsMargins
695 ns_screen_margins (NSScreen *screen)
696 /* The parts of SCREEN used by the operating system. */
698 NSTRACE ("ns_screen_margins");
700 struct EmacsMargins margins;
702 NSRect screenFrame = [screen frame];
703 NSRect screenVisibleFrame = [screen visibleFrame];
705 /* Sometimes, visibleFrame isn't up-to-date with respect to a hidden
706 menu bar, check this explicitly. */
707 if (ns_menu_bar_should_be_hidden())
713 CGFloat frameTop = screenFrame.origin.y + screenFrame.size.height;
714 CGFloat visibleFrameTop = (screenVisibleFrame.origin.y
715 + screenVisibleFrame.size.height);
717 margins.top = frameTop - visibleFrameTop;
721 CGFloat frameRight = screenFrame.origin.x + screenFrame.size.width;
722 CGFloat visibleFrameRight = (screenVisibleFrame.origin.x
723 + screenVisibleFrame.size.width);
724 margins.right = frameRight - visibleFrameRight;
727 margins.bottom = screenVisibleFrame.origin.y - screenFrame.origin.y;
728 margins.left = screenVisibleFrame.origin.x - screenFrame.origin.x;
730 NSTRACE_MSG ("left:%g right:%g top:%g bottom:%g",
740 /* A screen margin between 1 and DOCK_IGNORE_LIMIT (inclusive) is
741 assumed to contain a hidden dock. macOS currently use 4 pixels for
742 this, however, to be future compatible, a larger value is used. */
743 #define DOCK_IGNORE_LIMIT 6
745 static struct EmacsMargins
746 ns_screen_margins_ignoring_hidden_dock (NSScreen *screen)
747 /* The parts of SCREEN used by the operating system, excluding the parts
748 reserved for an hidden dock. */
750 NSTRACE ("ns_screen_margins_ignoring_hidden_dock");
752 struct EmacsMargins margins = ns_screen_margins(screen);
754 /* macOS (currently) reserved 4 pixels along the edge where a hidden
755 dock is located. Unfortunately, it's not possible to find the
756 location and information about if the dock is hidden. Instead,
757 it is assumed that if the margin of an edge is less than
758 DOCK_IGNORE_LIMIT, it contains a hidden dock. */
759 if (margins.left <= DOCK_IGNORE_LIMIT)
763 if (margins.right <= DOCK_IGNORE_LIMIT)
767 if (margins.top <= DOCK_IGNORE_LIMIT)
771 /* Note: This doesn't occur in current versions of macOS, but
772 included for completeness and future compatibility. */
773 if (margins.bottom <= DOCK_IGNORE_LIMIT)
778 NSTRACE_MSG ("left:%g right:%g top:%g bottom:%g",
789 ns_menu_bar_height (NSScreen *screen)
790 /* The height of the menu bar, if visible.
792 Note: Don't use this when fullscreen is enabled -- the screen
793 sometimes includes, sometimes excludes the menu bar area. */
795 struct EmacsMargins margins = ns_screen_margins(screen);
797 CGFloat res = margins.top;
799 NSTRACE ("ns_menu_bar_height " NSTRACE_FMT_RETURN " %.0f", res);
805 /* ==========================================================================
807 Focus (clipping) and screen update
809 ========================================================================== */
812 // Window constraining
813 // -------------------
815 // To ensure that the windows are not placed under the menu bar, they
816 // are typically moved by the call-back constrainFrameRect. However,
817 // by overriding it, it's possible to inhibit this, leaving the window
818 // in it's original position.
820 // It's possible to hide the menu bar. However, technically, it's only
821 // possible to hide it when the application is active. To ensure that
822 // this work properly, the menu bar and window constraining are
823 // deferred until the application becomes active.
825 // Even though it's not possible to manually move a window above the
826 // top of the screen, it is allowed if it's done programmatically,
827 // when the menu is hidden. This allows the editable area to cover the
828 // full screen height.
833 // Use the following extra files:
836 // ;; Hide menu and place frame slightly above the top of the screen.
837 // (setq ns-auto-hide-menu-bar t)
838 // (set-frame-position (selected-frame) 0 -20)
842 // emacs -Q -l init.el
844 // Result: No menu bar, and the title bar should be above the screen.
850 // Result: Menu bar visible, frame placed immediately below the menu.
853 static NSRect constrain_frame_rect(NSRect frameRect, bool isFullscreen)
855 NSTRACE ("constrain_frame_rect(" NSTRACE_FMT_RECT ")",
856 NSTRACE_ARG_RECT (frameRect));
858 // --------------------
859 // Collect information about the screen the frame is covering.
862 NSArray *screens = [NSScreen screens];
863 NSUInteger nr_screens = [screens count];
867 // The height of the menu bar, if present in any screen the frame is
869 int menu_bar_height = 0;
871 // A rectangle covering all the screen the frame is displayed in.
872 NSRect multiscreenRect = NSMakeRect(0, 0, 0, 0);
873 for (i = 0; i < nr_screens; ++i )
875 NSScreen *s = [screens objectAtIndex: i];
876 NSRect scrRect = [s frame];
878 NSTRACE_MSG ("Screen %d: " NSTRACE_FMT_RECT,
879 i, NSTRACE_ARG_RECT (scrRect));
881 if (NSIntersectionRect (frameRect, scrRect).size.height != 0)
883 multiscreenRect = NSUnionRect (multiscreenRect, scrRect);
887 CGFloat screen_menu_bar_height = ns_menu_bar_height (s);
888 menu_bar_height = max(menu_bar_height, screen_menu_bar_height);
893 NSTRACE_RECT ("multiscreenRect", multiscreenRect);
895 NSTRACE_MSG ("menu_bar_height: %d", menu_bar_height);
897 if (multiscreenRect.size.width == 0
898 || multiscreenRect.size.height == 0)
900 // Failed to find any monitor, give up.
901 NSTRACE_MSG ("multiscreenRect empty");
902 NSTRACE_RETURN_RECT (frameRect);
907 // --------------------
908 // Find a suitable placement.
911 if (ns_menu_bar_should_be_hidden())
913 // When the menu bar is hidden, the user may place part of the
914 // frame above the top of the screen, for example to hide the
917 // Hence, keep the original position.
921 // Ensure that the frame is below the menu bar, or below the top
924 // This assume that the menu bar is placed at the top in the
925 // rectangle that covers the monitors. (It doesn't have to be,
926 // but if it's not it's hard to do anything useful.)
927 CGFloat topOfWorkArea = (multiscreenRect.origin.y
928 + multiscreenRect.size.height
931 CGFloat topOfFrame = frameRect.origin.y + frameRect.size.height;
932 if (topOfFrame > topOfWorkArea)
934 frameRect.origin.y -= topOfFrame - topOfWorkArea;
935 NSTRACE_RECT ("After placement adjust", frameRect);
939 // Include the following section to restrict frame to the screens.
940 // (If so, update it to allow the frame to stretch down below the
943 // --------------------
944 // Ensure frame doesn't stretch below the screens.
947 CGFloat diff = multiscreenRect.origin.y - frameRect.origin.y;
951 frameRect.origin.y = multiscreenRect.origin.y;
952 frameRect.size.height -= diff;
956 NSTRACE_RETURN_RECT (frameRect);
962 ns_constrain_all_frames (void)
963 /* --------------------------------------------------------------------------
964 Ensure that the menu bar doesn't cover any frames.
965 -------------------------------------------------------------------------- */
967 Lisp_Object tail, frame;
969 NSTRACE ("ns_constrain_all_frames");
973 FOR_EACH_FRAME (tail, frame)
975 struct frame *f = XFRAME (frame);
978 EmacsView *view = FRAME_NS_VIEW (f);
980 if (![view isFullscreen])
983 setFrame:constrain_frame_rect([[view window] frame], false)
994 ns_update_auto_hide_menu_bar (void)
995 /* --------------------------------------------------------------------------
996 Show or hide the menu bar, based on user setting.
997 -------------------------------------------------------------------------- */
1000 NSTRACE ("ns_update_auto_hide_menu_bar");
1004 if (NSApp != nil && [NSApp isActive])
1006 // Note, "setPresentationOptions" triggers an error unless the
1007 // application is active.
1008 BOOL menu_bar_should_be_hidden = ns_menu_bar_should_be_hidden ();
1010 if (menu_bar_should_be_hidden != ns_menu_bar_is_hidden)
1012 NSApplicationPresentationOptions options
1013 = NSApplicationPresentationDefault;
1015 if (menu_bar_should_be_hidden)
1016 options |= NSApplicationPresentationAutoHideMenuBar
1017 | NSApplicationPresentationAutoHideDock;
1019 [NSApp setPresentationOptions: options];
1021 ns_menu_bar_is_hidden = menu_bar_should_be_hidden;
1023 if (!ns_menu_bar_is_hidden)
1025 ns_constrain_all_frames ();
1036 ns_update_begin (struct frame *f)
1037 /* --------------------------------------------------------------------------
1038 Prepare for a grouped sequence of drawing calls
1039 external (RIF) call; whole frame, called before update_window_begin
1040 -------------------------------------------------------------------------- */
1042 EmacsView *view = FRAME_NS_VIEW (f);
1043 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_update_begin");
1045 ns_update_auto_hide_menu_bar ();
1047 #ifdef NS_IMPL_COCOA
1048 if ([view isFullscreen] && [view fsIsNative])
1050 // Fix reappearing tool bar in fullscreen for Mac OS X 10.7
1051 BOOL tbar_visible = FRAME_EXTERNAL_TOOL_BAR (f) ? YES : NO;
1052 NSToolbar *toolbar = [FRAME_NS_VIEW (f) toolbar];
1053 if (! tbar_visible != ! [toolbar isVisible])
1054 [toolbar setVisible: tbar_visible];
1058 ns_updating_frame = f;
1061 /* drawRect may have been called for say the minibuffer, and then clip path
1062 is for the minibuffer. But the display engine may draw more because
1063 we have set the frame as garbaged. So reset clip path to the whole
1065 #ifdef NS_IMPL_COCOA
1068 NSRect r = [view frame];
1069 NSRect cr = [[view window] frame];
1070 /* If a large frame size is set, r may be larger than the window frame
1071 before constrained. In that case don't change the clip path, as we
1072 will clear in to the tool bar and title bar. */
1074 + FRAME_NS_TITLEBAR_HEIGHT (f)
1075 + FRAME_TOOLBAR_HEIGHT (f) <= cr.size.height)
1077 bp = [[NSBezierPath bezierPathWithRect: r] retain];
1084 #ifdef NS_IMPL_GNUSTEP
1085 uRect = NSMakeRect (0, 0, 0, 0);
1091 ns_update_window_begin (struct window *w)
1092 /* --------------------------------------------------------------------------
1093 Prepare for a grouped sequence of drawing calls
1094 external (RIF) call; for one window, called after update_begin
1095 -------------------------------------------------------------------------- */
1097 struct frame *f = XFRAME (WINDOW_FRAME (w));
1098 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1100 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_update_window_begin");
1101 w->output_cursor = w->cursor;
1105 if (f == hlinfo->mouse_face_mouse_frame)
1107 /* Don't do highlighting for mouse motion during the update. */
1108 hlinfo->mouse_face_defer = 1;
1110 /* If the frame needs to be redrawn,
1111 simply forget about any prior mouse highlighting. */
1112 if (FRAME_GARBAGED_P (f))
1113 hlinfo->mouse_face_window = Qnil;
1115 /* (further code for mouse faces ifdef'd out in other terms elided) */
1123 ns_update_window_end (struct window *w, bool cursor_on_p,
1124 bool mouse_face_overwritten_p)
1125 /* --------------------------------------------------------------------------
1126 Finished a grouped sequence of drawing calls
1127 external (RIF) call; for one window called before update_end
1128 -------------------------------------------------------------------------- */
1130 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_update_window_end");
1132 /* note: this fn is nearly identical in all terms */
1133 if (!w->pseudo_window_p)
1138 display_and_set_cursor (w, 1,
1139 w->output_cursor.hpos, w->output_cursor.vpos,
1140 w->output_cursor.x, w->output_cursor.y);
1142 if (draw_window_fringes (w, 1))
1144 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
1145 x_draw_right_divider (w);
1147 x_draw_vertical_border (w);
1153 /* If a row with mouse-face was overwritten, arrange for
1154 frame_up_to_date to redisplay the mouse highlight. */
1155 if (mouse_face_overwritten_p)
1156 reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
1161 ns_update_end (struct frame *f)
1162 /* --------------------------------------------------------------------------
1163 Finished a grouped sequence of drawing calls
1164 external (RIF) call; for whole frame, called after update_window_end
1165 -------------------------------------------------------------------------- */
1167 EmacsView *view = FRAME_NS_VIEW (f);
1169 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_update_end");
1171 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
1172 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
1177 [[view window] flushWindow];
1180 ns_updating_frame = NULL;
1184 ns_focus (struct frame *f, NSRect *r, int n)
1185 /* --------------------------------------------------------------------------
1186 Internal: Focus on given frame. During small local updates this is used to
1187 draw, however during large updates, ns_update_begin and ns_update_end are
1188 called to wrap the whole thing, in which case these calls are stubbed out.
1189 Except, on GNUstep, we accumulate the rectangle being drawn into, because
1190 the back end won't do this automatically, and will just end up flushing
1192 -------------------------------------------------------------------------- */
1194 NSTRACE_WHEN (NSTRACE_GROUP_FOCUS, "ns_focus");
1197 NSTRACE_RECT ("r", *r);
1200 if (f != ns_updating_frame)
1202 NSView *view = FRAME_NS_VIEW (f);
1203 if (view != focus_view)
1205 if (focus_view != NULL)
1207 [focus_view unlockFocus];
1208 [[focus_view window] flushWindow];
1215 /*if (view) debug_lock++; */
1222 [[NSGraphicsContext currentContext] saveGraphicsState];
1224 NSRectClipList (r, 2);
1233 ns_unfocus (struct frame *f)
1234 /* --------------------------------------------------------------------------
1235 Internal: Remove focus on given frame
1236 -------------------------------------------------------------------------- */
1238 NSTRACE_WHEN (NSTRACE_GROUP_FOCUS, "ns_unfocus");
1242 [[NSGraphicsContext currentContext] restoreGraphicsState];
1246 if (f != ns_updating_frame)
1248 if (focus_view != NULL)
1250 [focus_view unlockFocus];
1251 [[focus_view window] flushWindow];
1260 ns_clip_to_row (struct window *w, struct glyph_row *row,
1261 enum glyph_row_area area, BOOL gc)
1262 /* --------------------------------------------------------------------------
1263 Internal (but parallels other terms): Focus drawing on given row
1264 -------------------------------------------------------------------------- */
1266 struct frame *f = XFRAME (WINDOW_FRAME (w));
1268 int window_x, window_y, window_width;
1270 window_box (w, area, &window_x, &window_y, &window_width, 0);
1272 clip_rect.origin.x = window_x;
1273 clip_rect.origin.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
1274 clip_rect.origin.y = max (clip_rect.origin.y, window_y);
1275 clip_rect.size.width = window_width;
1276 clip_rect.size.height = row->visible_height;
1278 ns_focus (f, &clip_rect, 1);
1282 /* ==========================================================================
1284 Visible bell and beep.
1286 ========================================================================== */
1289 // This bell implementation shows the visual bell image asynchronously
1290 // from the rest of Emacs. This is done by adding a NSView to the
1291 // superview of the Emacs window and removing it using a timer.
1293 // Unfortunately, some Emacs operations, like scrolling, is done using
1294 // low-level primitives that copy the content of the window, including
1295 // the bell image. To some extent, this is handled by removing the
1296 // image prior to scrolling and marking that the window is in need for
1299 // To test this code, make sure that there is no artifacts of the bell
1300 // image in the following situations. Use a non-empty buffer (like the
1301 // tutorial) to ensure that a scroll is performed:
1303 // * Single-window: C-g C-v
1305 // * Side-by-windows: C-x 3 C-g C-v
1307 // * Windows above each other: C-x 2 C-g C-v
1309 @interface EmacsBell : NSImageView
1311 // Number of currently active bell:s.
1312 unsigned int nestCount;
1316 - (void)show:(NSView *)view;
1321 @implementation EmacsBell
1325 NSTRACE ("[EmacsBell init]");
1326 if ((self = [super init]))
1330 #ifdef NS_IMPL_GNUSTEP
1331 // GNUstep doesn't provide named images. This was reported in
1332 // 2011, see https://savannah.gnu.org/bugs/?33396
1334 // As a drop in replacement, a semitransparent gray square is used.
1335 self.image = [[NSImage alloc] initWithSize:NSMakeSize(32 * 5, 32 * 5)];
1336 [self.image lockFocus];
1337 [[NSColor colorForEmacsRed:0.5 green:0.5 blue:0.5 alpha:0.5] set];
1338 NSRectFill(NSMakeRect(0, 0, 32, 32));
1339 [self.image unlockFocus];
1341 self.image = [NSImage imageNamed:NSImageNameCaution];
1342 [self.image setSize:NSMakeSize(self.image.size.width * 5,
1343 self.image.size.height * 5)];
1349 - (void)show:(NSView *)view
1351 NSTRACE ("[EmacsBell show:]");
1352 NSTRACE_MSG ("nestCount: %u", nestCount);
1354 // Show the image, unless it's already shown.
1357 NSRect rect = [view bounds];
1359 pos.x = rect.origin.x + (rect.size.width - self.image.size.width )/2;
1360 pos.y = rect.origin.y + (rect.size.height - self.image.size.height)/2;
1362 [self setFrameOrigin:pos];
1363 [self setFrameSize:self.image.size];
1367 [[[view window] contentView] addSubview:self
1368 positioned:NSWindowAbove
1374 [self performSelector:@selector(hide) withObject:self afterDelay:0.5];
1380 // Note: Trace output from this method isn't shown, reason unknown.
1381 // NSTRACE ("[EmacsBell hide]");
1386 // Remove the image once the last bell became inactive.
1396 NSTRACE ("[EmacsBell remove]");
1399 NSTRACE_MSG ("removeFromSuperview");
1400 [self removeFromSuperview];
1401 mView.needsDisplay = YES;
1409 static EmacsBell * bell_view = nil;
1412 ns_ring_bell (struct frame *f)
1413 /* --------------------------------------------------------------------------
1415 -------------------------------------------------------------------------- */
1417 NSTRACE ("ns_ring_bell");
1420 struct frame *frame = SELECTED_FRAME ();
1423 if (bell_view == nil)
1425 bell_view = [[EmacsBell alloc] init];
1431 view = FRAME_NS_VIEW (frame);
1434 [bell_view show:view];
1448 /* --------------------------------------------------------------------------
1449 Ensure the bell is hidden.
1450 -------------------------------------------------------------------------- */
1452 NSTRACE ("hide_bell");
1454 if (bell_view != nil)
1461 /* ==========================================================================
1463 Frame / window manager related functions
1465 ========================================================================== */
1469 ns_raise_frame (struct frame *f, BOOL make_key)
1470 /* --------------------------------------------------------------------------
1471 Bring window to foreground and if make_key is YES, give it focus.
1472 -------------------------------------------------------------------------- */
1476 check_window_system (f);
1477 view = FRAME_NS_VIEW (f);
1479 if (FRAME_VISIBLE_P (f))
1482 [[view window] makeKeyAndOrderFront: NSApp];
1484 [[view window] orderFront: NSApp];
1491 ns_lower_frame (struct frame *f)
1492 /* --------------------------------------------------------------------------
1494 -------------------------------------------------------------------------- */
1498 check_window_system (f);
1499 view = FRAME_NS_VIEW (f);
1501 [[view window] orderBack: NSApp];
1507 ns_frame_raise_lower (struct frame *f, bool raise)
1508 /* --------------------------------------------------------------------------
1510 -------------------------------------------------------------------------- */
1512 NSTRACE ("ns_frame_raise_lower");
1515 ns_raise_frame (f, YES);
1522 ns_frame_rehighlight (struct frame *frame)
1523 /* --------------------------------------------------------------------------
1524 External (hook): called on things like window switching within frame
1525 -------------------------------------------------------------------------- */
1527 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
1528 struct frame *old_highlight = dpyinfo->x_highlight_frame;
1530 NSTRACE ("ns_frame_rehighlight");
1531 if (dpyinfo->x_focus_frame)
1533 dpyinfo->x_highlight_frame
1534 = (FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1535 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
1536 : dpyinfo->x_focus_frame);
1537 if (!FRAME_LIVE_P (dpyinfo->x_highlight_frame))
1539 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
1540 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
1544 dpyinfo->x_highlight_frame = 0;
1546 if (dpyinfo->x_highlight_frame &&
1547 dpyinfo->x_highlight_frame != old_highlight)
1551 x_update_cursor (old_highlight, 1);
1552 x_set_frame_alpha (old_highlight);
1554 if (dpyinfo->x_highlight_frame)
1556 x_update_cursor (dpyinfo->x_highlight_frame, 1);
1557 x_set_frame_alpha (dpyinfo->x_highlight_frame);
1564 x_make_frame_visible (struct frame *f)
1565 /* --------------------------------------------------------------------------
1566 External: Show the window (X11 semantics)
1567 -------------------------------------------------------------------------- */
1569 NSTRACE ("x_make_frame_visible");
1570 /* XXX: at some points in past this was not needed, as the only place that
1571 called this (frame.c:Fraise_frame ()) also called raise_lower;
1572 if this ends up the case again, comment this out again. */
1573 if (!FRAME_VISIBLE_P (f))
1575 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
1576 NSWindow *window = [view window];
1578 SET_FRAME_VISIBLE (f, 1);
1579 ns_raise_frame (f, ! FRAME_NO_FOCUS_ON_MAP (f));
1581 /* Making a new frame from a fullscreen frame will make the new frame
1582 fullscreen also. So skip handleFS as this will print an error. */
1583 if ([view fsIsNative] && f->want_fullscreen == FULLSCREEN_BOTH
1584 && [view isFullscreen])
1587 if (f->want_fullscreen != FULLSCREEN_NONE)
1594 /* Making a frame invisible seems to break the parent->child
1595 relationship, so reinstate it. */
1596 if ([window parentWindow] == nil && FRAME_PARENT_FRAME (f) != NULL)
1598 NSWindow *parent = [FRAME_NS_VIEW (FRAME_PARENT_FRAME (f)) window];
1601 [parent addChildWindow: window
1602 ordered: NSWindowAbove];
1605 /* If the parent frame moved while the child frame was
1606 invisible, the child frame's position won't have been
1607 updated. Make sure it's in the right place now. */
1608 x_set_offset(f, f->left_pos, f->top_pos, 0);
1615 x_make_frame_invisible (struct frame *f)
1616 /* --------------------------------------------------------------------------
1617 External: Hide the window (X11 semantics)
1618 -------------------------------------------------------------------------- */
1621 NSTRACE ("x_make_frame_invisible");
1622 check_window_system (f);
1623 view = FRAME_NS_VIEW (f);
1624 [[view window] orderOut: NSApp];
1625 SET_FRAME_VISIBLE (f, 0);
1626 SET_FRAME_ICONIFIED (f, 0);
1631 x_iconify_frame (struct frame *f)
1632 /* --------------------------------------------------------------------------
1633 External: Iconify window
1634 -------------------------------------------------------------------------- */
1637 struct ns_display_info *dpyinfo;
1639 NSTRACE ("x_iconify_frame");
1640 check_window_system (f);
1641 view = FRAME_NS_VIEW (f);
1642 dpyinfo = FRAME_DISPLAY_INFO (f);
1644 if (dpyinfo->x_highlight_frame == f)
1645 dpyinfo->x_highlight_frame = 0;
1647 if ([[view window] windowNumber] <= 0)
1649 /* the window is still deferred. Make it very small, bring it
1650 on screen and order it out. */
1651 NSRect s = { { 100, 100}, {0, 0} };
1653 t = [[view window] frame];
1654 [[view window] setFrame: s display: NO];
1655 [[view window] orderBack: NSApp];
1656 [[view window] orderOut: NSApp];
1657 [[view window] setFrame: t display: NO];
1660 /* Processing input while Emacs is being minimized can cause a
1661 crash, so block it for the duration. */
1663 [[view window] miniaturize: NSApp];
1667 /* Free X resources of frame F. */
1670 x_free_frame_resources (struct frame *f)
1673 struct ns_display_info *dpyinfo;
1674 Mouse_HLInfo *hlinfo;
1676 NSTRACE ("x_free_frame_resources");
1677 check_window_system (f);
1678 view = FRAME_NS_VIEW (f);
1679 dpyinfo = FRAME_DISPLAY_INFO (f);
1680 hlinfo = MOUSE_HL_INFO (f);
1682 [(EmacsView *)view setWindowClosing: YES]; /* may not have been informed */
1686 free_frame_menubar (f);
1687 free_frame_faces (f);
1689 if (f == dpyinfo->x_focus_frame)
1690 dpyinfo->x_focus_frame = 0;
1691 if (f == dpyinfo->x_highlight_frame)
1692 dpyinfo->x_highlight_frame = 0;
1693 if (f == hlinfo->mouse_face_mouse_frame)
1694 reset_mouse_highlight (hlinfo);
1695 /* Ensure that sendEvent does not attempt to dereference a freed
1696 frame. (bug#30800) */
1697 if (represented_frame == f)
1698 represented_frame = NULL;
1700 if (f->output_data.ns->miniimage != nil)
1701 [f->output_data.ns->miniimage release];
1703 [[view window] close];
1706 xfree (f->output_data.ns);
1712 x_destroy_window (struct frame *f)
1713 /* --------------------------------------------------------------------------
1714 External: Delete the window
1715 -------------------------------------------------------------------------- */
1717 NSTRACE ("x_destroy_window");
1719 /* If this frame has a parent window, detach it as not doing so can
1720 cause a crash in GNUStep. */
1721 if (FRAME_PARENT_FRAME (f) != NULL)
1723 NSWindow *child = [FRAME_NS_VIEW (f) window];
1724 NSWindow *parent = [FRAME_NS_VIEW (FRAME_PARENT_FRAME (f)) window];
1726 [parent removeChildWindow: child];
1729 check_window_system (f);
1730 x_free_frame_resources (f);
1736 x_set_offset (struct frame *f, int xoff, int yoff, int change_grav)
1737 /* --------------------------------------------------------------------------
1738 External: Position the window
1739 -------------------------------------------------------------------------- */
1741 NSView *view = FRAME_NS_VIEW (f);
1742 NSArray *screens = [NSScreen screens];
1743 NSScreen *screen = [[view window] screen];
1745 NSTRACE ("x_set_offset");
1754 if (FRAME_PARENT_FRAME (f) == NULL && screen)
1756 f->left_pos = f->size_hint_flags & XNegative
1757 ? [screen visibleFrame].size.width + f->left_pos - FRAME_PIXEL_WIDTH (f)
1759 /* We use visibleFrame here to take menu bar into account.
1760 Ideally we should also adjust left/top with visibleFrame.origin. */
1762 f->top_pos = f->size_hint_flags & YNegative
1763 ? ([screen visibleFrame].size.height + f->top_pos
1764 - FRAME_PIXEL_HEIGHT (f) - FRAME_NS_TITLEBAR_HEIGHT (f)
1765 - FRAME_TOOLBAR_HEIGHT (f))
1767 #ifdef NS_IMPL_GNUSTEP
1768 if (f->left_pos < 100)
1769 f->left_pos = 100; /* don't overlap menu */
1772 else if (FRAME_PARENT_FRAME (f) != NULL)
1774 struct frame *parent = FRAME_PARENT_FRAME (f);
1776 /* On X negative values for child frames always result in
1777 positioning relative to the bottom right corner of the
1779 if (f->left_pos < 0)
1780 f->left_pos = FRAME_PIXEL_WIDTH (parent) - FRAME_PIXEL_WIDTH (f) + f->left_pos;
1783 f->top_pos = FRAME_PIXEL_HEIGHT (parent) + FRAME_TOOLBAR_HEIGHT (parent)
1784 - FRAME_PIXEL_HEIGHT (f) + f->top_pos;
1787 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1789 NSPoint pt = NSMakePoint (SCREENMAXBOUND (f->left_pos
1790 + NS_PARENT_WINDOW_LEFT_POS (f)),
1791 SCREENMAXBOUND (NS_PARENT_WINDOW_TOP_POS (f)
1793 NSTRACE_POINT ("setFrameTopLeftPoint", pt);
1794 [[view window] setFrameTopLeftPoint: pt];
1795 f->size_hint_flags &= ~(XNegative|YNegative);
1803 x_set_window_size (struct frame *f,
1804 bool change_gravity,
1808 /* --------------------------------------------------------------------------
1809 Adjust window pixel size based on given character grid size
1810 Impl is a bit more complex than other terms, need to do some
1812 -------------------------------------------------------------------------- */
1814 EmacsView *view = FRAME_NS_VIEW (f);
1815 NSWindow *window = [view window];
1816 NSRect wr = [window frame];
1817 int pixelwidth, pixelheight;
1818 int orig_height = wr.size.height;
1820 NSTRACE ("x_set_window_size");
1825 NSTRACE_RECT ("current", wr);
1826 NSTRACE_MSG ("Width:%d Height:%d Pixelwise:%d", width, height, pixelwise);
1827 NSTRACE_MSG ("Font %d x %d", FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f));
1833 pixelwidth = FRAME_TEXT_TO_PIXEL_WIDTH (f, width);
1834 pixelheight = FRAME_TEXT_TO_PIXEL_HEIGHT (f, height);
1838 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, width);
1839 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, height);
1842 wr.size.width = pixelwidth + f->border_width;
1843 wr.size.height = pixelheight;
1844 if (! [view isFullscreen])
1845 wr.size.height += FRAME_NS_TITLEBAR_HEIGHT (f)
1846 + FRAME_TOOLBAR_HEIGHT (f);
1848 /* Do not try to constrain to this screen. We may have multiple
1849 screens, and want Emacs to span those. Constraining to screen
1850 prevents that, and that is not nice to the user. */
1851 if (f->output_data.ns->zooming)
1852 f->output_data.ns->zooming = 0;
1854 wr.origin.y += orig_height - wr.size.height;
1856 frame_size_history_add
1857 (f, Qx_set_window_size_1, width, height,
1858 list5 (Fcons (make_number (pixelwidth), make_number (pixelheight)),
1859 Fcons (make_number (wr.size.width), make_number (wr.size.height)),
1860 make_number (f->border_width),
1861 make_number (FRAME_NS_TITLEBAR_HEIGHT (f)),
1862 make_number (FRAME_TOOLBAR_HEIGHT (f))));
1864 [window setFrame: wr display: YES];
1866 [view updateFrameSize: NO];
1870 #ifdef NS_IMPL_COCOA
1872 x_set_undecorated (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
1873 /* --------------------------------------------------------------------------
1874 Set frame F's `undecorated' parameter. If non-nil, F's window-system
1875 window is drawn without decorations, title, minimize/maximize boxes
1876 and external borders. This usually means that the window cannot be
1877 dragged, resized, iconified, maximized or deleted with the mouse. If
1878 nil, draw the frame with all the elements listed above unless these
1879 have been suspended via window manager settings.
1881 GNUStep cannot change an existing window's style.
1882 -------------------------------------------------------------------------- */
1884 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
1885 NSWindow *window = [view window];
1887 NSTRACE ("x_set_undecorated");
1889 if (!EQ (new_value, old_value))
1893 if (NILP (new_value))
1895 FRAME_UNDECORATED (f) = false;
1896 [window setStyleMask: ((window.styleMask | FRAME_DECORATED_FLAGS)
1897 ^ FRAME_UNDECORATED_FLAGS)];
1899 [view createToolbar: f];
1903 [window setToolbar: nil];
1904 /* Do I need to release the toolbar here? */
1906 FRAME_UNDECORATED (f) = true;
1907 [window setStyleMask: ((window.styleMask | FRAME_UNDECORATED_FLAGS)
1908 ^ FRAME_DECORATED_FLAGS)];
1911 /* At this point it seems we don't have an active NSResponder,
1912 so some key presses (TAB) are swallowed by the system. */
1913 [window makeFirstResponder: view];
1915 [view updateFrameSize: NO];
1919 #endif /* NS_IMPL_COCOA */
1922 x_set_parent_frame (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
1923 /* --------------------------------------------------------------------------
1924 Set frame F's `parent-frame' parameter. If non-nil, make F a child
1925 frame of the frame specified by that parameter. Technically, this
1926 makes F's window-system window a child window of the parent frame's
1927 window-system window. If nil, make F's window-system window a
1928 top-level window--a child of its display's root window.
1930 A child frame's `left' and `top' parameters specify positions
1931 relative to the top-left corner of its parent frame's native
1932 rectangle. On macOS moving a parent frame moves all its child
1933 frames too, keeping their position relative to the parent
1934 unaltered. When a parent frame is iconified or made invisible, its
1935 child frames are made invisible. When a parent frame is deleted,
1936 its child frames are deleted too.
1938 Whether a child frame has a tool bar may be window-system or window
1939 manager dependent. It's advisable to disable it via the frame
1942 Some window managers may not honor this parameter.
1943 -------------------------------------------------------------------------- */
1945 struct frame *p = NULL;
1946 NSWindow *parent, *child;
1948 NSTRACE ("x_set_parent_frame");
1950 if (!NILP (new_value)
1951 && (!FRAMEP (new_value)
1952 || !FRAME_LIVE_P (p = XFRAME (new_value))
1953 || !FRAME_NS_P (p)))
1955 store_frame_param (f, Qparent_frame, old_value);
1956 error ("Invalid specification of `parent-frame'");
1959 if (p != FRAME_PARENT_FRAME (f))
1962 child = [FRAME_NS_VIEW (f) window];
1964 if ([child parentWindow] != nil)
1966 [[child parentWindow] removeChildWindow:child];
1967 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
1968 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
1969 if ([child respondsToSelector:@selector(setAccessibilitySubrole:)]
1971 [child setAccessibilitySubrole:NSAccessibilityStandardWindowSubrole];
1975 if (!NILP (new_value))
1977 parent = [FRAME_NS_VIEW (p) window];
1979 [parent addChildWindow: child
1980 ordered: NSWindowAbove];
1981 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
1982 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
1983 if ([child respondsToSelector:@selector(setAccessibilitySubrole:)]
1985 [child setAccessibilitySubrole:NSAccessibilityFloatingWindowSubrole];
1991 fset_parent_frame (f, new_value);
1996 x_set_no_focus_on_map (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
1997 /* Set frame F's `no-focus-on-map' parameter which, if non-nil, means
1998 * that F's window-system window does not want to receive input focus
1999 * when it is mapped. (A frame's window is mapped when the frame is
2000 * displayed for the first time and when the frame changes its state
2001 * from `iconified' or `invisible' to `visible'.)
2003 * Some window managers may not honor this parameter. */
2005 NSTRACE ("x_set_no_focus_on_map");
2007 if (!EQ (new_value, old_value))
2009 FRAME_NO_FOCUS_ON_MAP (f) = !NILP (new_value);
2014 x_set_no_accept_focus (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
2015 /* Set frame F's `no-accept-focus' parameter which, if non-nil, hints
2016 * that F's window-system window does not want to receive input focus
2017 * via mouse clicks or by moving the mouse into it.
2019 * If non-nil, this may have the unwanted side-effect that a user cannot
2020 * scroll a non-selected frame with the mouse.
2022 * Some window managers may not honor this parameter. */
2024 NSTRACE ("x_set_no_accept_focus");
2026 if (!EQ (new_value, old_value))
2027 FRAME_NO_ACCEPT_FOCUS (f) = !NILP (new_value);
2031 x_set_z_group (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
2032 /* Set frame F's `z-group' parameter. If `above', F's window-system
2033 window is displayed above all windows that do not have the `above'
2034 property set. If nil, F's window is shown below all windows that
2035 have the `above' property set and above all windows that have the
2036 `below' property set. If `below', F's window is displayed below
2037 all windows that do.
2039 Some window managers may not honor this parameter. */
2041 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
2042 NSWindow *window = [view window];
2044 NSTRACE ("x_set_z_group");
2046 if (NILP (new_value))
2048 window.level = NSNormalWindowLevel;
2049 FRAME_Z_GROUP (f) = z_group_none;
2051 else if (EQ (new_value, Qabove))
2053 window.level = NSNormalWindowLevel + 1;
2054 FRAME_Z_GROUP (f) = z_group_above;
2056 else if (EQ (new_value, Qabove_suspended))
2058 /* Not sure what level this should be. */
2059 window.level = NSNormalWindowLevel + 1;
2060 FRAME_Z_GROUP (f) = z_group_above_suspended;
2062 else if (EQ (new_value, Qbelow))
2064 window.level = NSNormalWindowLevel - 1;
2065 FRAME_Z_GROUP (f) = z_group_below;
2068 error ("Invalid z-group specification");
2071 #ifdef NS_IMPL_COCOA
2073 ns_set_appearance (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
2075 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
2076 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
2077 NSWindow *window = [view window];
2079 NSTRACE ("ns_set_appearance");
2081 #ifndef NSAppKitVersionNumber10_10
2082 #define NSAppKitVersionNumber10_10 1343
2085 if (NSAppKitVersionNumber < NSAppKitVersionNumber10_10)
2088 if (EQ (new_value, Qdark))
2090 window.appearance = [NSAppearance
2091 appearanceNamed: NSAppearanceNameVibrantDark];
2092 FRAME_NS_APPEARANCE (f) = ns_appearance_vibrant_dark;
2096 window.appearance = [NSAppearance
2097 appearanceNamed: NSAppearanceNameAqua];
2098 FRAME_NS_APPEARANCE (f) = ns_appearance_aqua;
2100 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 101000 */
2104 ns_set_transparent_titlebar (struct frame *f, Lisp_Object new_value,
2105 Lisp_Object old_value)
2107 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
2108 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
2109 NSWindow *window = [view window];
2111 NSTRACE ("ns_set_transparent_titlebar");
2113 if ([window respondsToSelector: @selector(titlebarAppearsTransparent)]
2114 && !EQ (new_value, old_value))
2116 window.titlebarAppearsTransparent = !NILP (new_value);
2117 FRAME_NS_TRANSPARENT_TITLEBAR (f) = !NILP (new_value);
2119 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 101000 */
2121 #endif /* NS_IMPL_COCOA */
2124 ns_fullscreen_hook (struct frame *f)
2126 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
2128 NSTRACE ("ns_fullscreen_hook");
2130 if (!FRAME_VISIBLE_P (f))
2133 if (! [view fsIsNative] && f->want_fullscreen == FULLSCREEN_BOTH)
2135 /* Old style fs don't initiate correctly if created from
2136 init/default-frame alist, so use a timer (not nice...).
2138 [NSTimer scheduledTimerWithTimeInterval: 0.5 target: view
2139 selector: @selector (handleFS)
2140 userInfo: nil repeats: NO];
2149 /* ==========================================================================
2153 ========================================================================== */
2157 ns_lookup_indexed_color (unsigned long idx, struct frame *f)
2159 struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table;
2160 if (idx < 1 || idx >= color_table->avail)
2162 return color_table->colors[idx];
2167 ns_index_color (NSColor *color, struct frame *f)
2169 struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table;
2173 if (!color_table->colors)
2175 color_table->size = NS_COLOR_CAPACITY;
2176 color_table->avail = 1; /* skip idx=0 as marker */
2177 color_table->colors = xmalloc (color_table->size * sizeof (NSColor *));
2178 color_table->colors[0] = nil;
2179 color_table->empty_indices = [[NSMutableSet alloc] init];
2182 /* Do we already have this color? */
2183 for (i = 1; i < color_table->avail; i++)
2184 if (color_table->colors[i] && [color_table->colors[i] isEqual: color])
2187 if ([color_table->empty_indices count] > 0)
2189 NSNumber *index = [color_table->empty_indices anyObject];
2190 [color_table->empty_indices removeObject: index];
2191 idx = [index unsignedLongValue];
2195 if (color_table->avail == color_table->size)
2196 color_table->colors =
2197 xpalloc (color_table->colors, &color_table->size, 1,
2198 min (ULONG_MAX, PTRDIFF_MAX), sizeof *color_table->colors);
2199 idx = color_table->avail++;
2202 color_table->colors[idx] = color;
2204 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
2210 ns_get_color (const char *name, NSColor **col)
2211 /* --------------------------------------------------------------------------
2213 -------------------------------------------------------------------------- */
2214 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
2215 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
2216 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
2219 static char hex[20];
2221 float r = -1.0, g, b;
2222 NSString *nsname = [NSString stringWithUTF8String: name];
2224 NSTRACE ("ns_get_color(%s, **)", name);
2228 if ([nsname isEqualToString: @"ns_selection_bg_color"])
2230 #ifdef NS_IMPL_COCOA
2231 NSString *defname = [[NSUserDefaults standardUserDefaults]
2232 stringForKey: @"AppleHighlightColor"];
2237 if ((new = [NSColor selectedTextBackgroundColor]) != nil)
2239 *col = [new colorUsingDefaultColorSpace];
2244 nsname = NS_SELECTION_BG_COLOR_DEFAULT;
2246 name = [nsname UTF8String];
2248 else if ([nsname isEqualToString: @"ns_selection_fg_color"])
2250 /* NOTE: macOS applications normally don't set foreground
2251 selection, but text may be unreadable if we don't.
2253 if ((new = [NSColor selectedTextColor]) != nil)
2255 *col = [new colorUsingDefaultColorSpace];
2260 nsname = NS_SELECTION_FG_COLOR_DEFAULT;
2261 name = [nsname UTF8String];
2264 /* First, check for some sort of numeric specification. */
2267 if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* RGB decimal */
2269 NSScanner *scanner = [NSScanner scannerWithString: nsname];
2270 [scanner scanFloat: &r];
2271 [scanner scanFloat: &g];
2272 [scanner scanFloat: &b];
2274 else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
2275 scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
2276 else if (name[0] == '#') /* An old X11 format; convert to newer */
2278 int len = (strlen(name) - 1);
2279 int start = (len % 3 == 0) ? 1 : len / 4 + 1;
2281 scaling = strlen(name+start) / 3;
2282 for (i = 0; i < 3; i++)
2283 sprintf (hex + i * (scaling + 1), "%.*s/", scaling,
2284 name + start + i * scaling);
2285 hex[3 * (scaling + 1) - 1] = '\0';
2290 unsigned int rr, gg, bb;
2291 float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0);
2292 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
2302 *col = [NSColor colorForEmacsRed: r green: g blue: b alpha: 1.0];
2307 /* Otherwise, color is expected to be from a list */
2309 NSEnumerator *lenum, *cenum;
2313 #ifdef NS_IMPL_GNUSTEP
2314 /* XXX: who is wrong, the requestor or the implementation? */
2315 if ([nsname compare: @"Highlight" options: NSCaseInsensitiveSearch]
2317 nsname = @"highlightColor";
2320 lenum = [[NSColorList availableColorLists] objectEnumerator];
2321 while ( (clist = [lenum nextObject]) && new == nil)
2323 cenum = [[clist allKeys] objectEnumerator];
2324 while ( (name = [cenum nextObject]) && new == nil )
2326 if ([name compare: nsname
2327 options: NSCaseInsensitiveSearch] == NSOrderedSame )
2328 new = [clist colorWithKey: name];
2334 *col = [new colorUsingDefaultColorSpace];
2341 ns_lisp_to_color (Lisp_Object color, NSColor **col)
2342 /* --------------------------------------------------------------------------
2343 Convert a Lisp string object to a NS color
2344 -------------------------------------------------------------------------- */
2346 NSTRACE ("ns_lisp_to_color");
2347 if (STRINGP (color))
2348 return ns_get_color (SSDATA (color), col);
2349 else if (SYMBOLP (color))
2350 return ns_get_color (SSDATA (SYMBOL_NAME (color)), col);
2356 ns_query_color(void *col, XColor *color_def, int setPixel)
2357 /* --------------------------------------------------------------------------
2358 Get ARGB values out of NSColor col and put them into color_def.
2359 If setPixel, set the pixel to a concatenated version.
2360 and set color_def pixel to the resulting index.
2361 -------------------------------------------------------------------------- */
2363 EmacsCGFloat r, g, b, a;
2365 [((NSColor *)col) getRed: &r green: &g blue: &b alpha: &a];
2366 color_def->red = r * 65535;
2367 color_def->green = g * 65535;
2368 color_def->blue = b * 65535;
2370 if (setPixel == YES)
2372 = ARGB_TO_ULONG((int)(a*255),
2373 (int)(r*255), (int)(g*255), (int)(b*255));
2378 ns_defined_color (struct frame *f,
2383 /* --------------------------------------------------------------------------
2384 Return true if named color found, and set color_def rgb accordingly.
2385 If makeIndex and alloc are nonzero put the color in the color_table,
2386 and set color_def pixel to the resulting index.
2387 If makeIndex is zero, set color_def pixel to ARGB.
2388 Return false if not found
2389 -------------------------------------------------------------------------- */
2392 NSTRACE_WHEN (NSTRACE_GROUP_COLOR, "ns_defined_color");
2395 if (ns_get_color (name, &col) != 0) /* Color not found */
2400 if (makeIndex && alloc)
2401 color_def->pixel = ns_index_color (col, f);
2402 ns_query_color (col, color_def, !makeIndex);
2409 x_set_frame_alpha (struct frame *f)
2410 /* --------------------------------------------------------------------------
2411 change the entire-frame transparency
2412 -------------------------------------------------------------------------- */
2414 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2416 double alpha_min = 1.0;
2418 NSTRACE ("x_set_frame_alpha");
2420 if (dpyinfo->x_highlight_frame == f)
2421 alpha = f->alpha[0];
2423 alpha = f->alpha[1];
2425 if (FLOATP (Vframe_alpha_lower_limit))
2426 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
2427 else if (INTEGERP (Vframe_alpha_lower_limit))
2428 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
2432 else if (1.0 < alpha)
2434 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
2437 #ifdef NS_IMPL_COCOA
2439 EmacsView *view = FRAME_NS_VIEW (f);
2440 [[view window] setAlphaValue: alpha];
2446 /* ==========================================================================
2450 ========================================================================== */
2454 frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
2455 /* --------------------------------------------------------------------------
2456 Programmatically reposition mouse pointer in pixel coordinates
2457 -------------------------------------------------------------------------- */
2459 NSTRACE ("frame_set_mouse_pixel_position");
2461 /* FIXME: what about GNUstep? */
2462 #ifdef NS_IMPL_COCOA
2464 CGPointMake(f->left_pos + pix_x,
2465 f->top_pos + pix_y +
2466 FRAME_NS_TITLEBAR_HEIGHT(f) + FRAME_TOOLBAR_HEIGHT(f));
2467 CGWarpMouseCursorPosition (mouse_pos);
2472 note_mouse_movement (struct frame *frame, CGFloat x, CGFloat y)
2473 /* ------------------------------------------------------------------------
2474 Called by EmacsView on mouseMovement events. Passes on
2475 to emacs mainstream code if we moved off of a rect of interest
2476 known as last_mouse_glyph.
2477 ------------------------------------------------------------------------ */
2479 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
2482 // NSTRACE ("note_mouse_movement");
2484 dpyinfo->last_mouse_motion_frame = frame;
2485 r = &dpyinfo->last_mouse_glyph;
2487 /* Note, this doesn't get called for enter/leave, since we don't have a
2488 position. Those are taken care of in the corresponding NSView methods. */
2490 /* has movement gone beyond last rect we were tracking? */
2491 if (x < r->origin.x || x >= r->origin.x + r->size.width
2492 || y < r->origin.y || y >= r->origin.y + r->size.height)
2494 ns_update_begin (frame);
2495 frame->mouse_moved = 1;
2496 note_mouse_highlight (frame, x, y);
2497 remember_mouse_glyph (frame, x, y, r);
2498 ns_update_end (frame);
2507 ns_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
2508 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
2510 /* --------------------------------------------------------------------------
2511 External (hook): inform emacs about mouse position and hit parts.
2512 If a scrollbar is being dragged, set bar_window, part, x, y, time.
2513 x & y should be position in the scrollbar (the whole bar, not the handle)
2514 and length of scrollbar respectively
2515 -------------------------------------------------------------------------- */
2519 Lisp_Object frame, tail;
2521 struct ns_display_info *dpyinfo;
2523 NSTRACE ("ns_mouse_position");
2527 fprintf (stderr, "Warning: ns_mouse_position () called with null *fp.\n");
2531 dpyinfo = FRAME_DISPLAY_INFO (*fp);
2535 /* Clear the mouse-moved flag for every frame on this display. */
2536 FOR_EACH_FRAME (tail, frame)
2537 if (FRAME_NS_P (XFRAME (frame))
2538 && FRAME_NS_DISPLAY (XFRAME (frame)) == FRAME_NS_DISPLAY (*fp))
2539 XFRAME (frame)->mouse_moved = 0;
2541 dpyinfo->last_mouse_scroll_bar = nil;
2542 if (dpyinfo->last_mouse_frame
2543 && FRAME_LIVE_P (dpyinfo->last_mouse_frame))
2544 f = dpyinfo->last_mouse_frame;
2546 f = dpyinfo->x_focus_frame ? dpyinfo->x_focus_frame : SELECTED_FRAME ();
2548 if (f && FRAME_NS_P (f))
2550 view = FRAME_NS_VIEW (f);
2552 position = [[view window] mouseLocationOutsideOfEventStream];
2553 position = [view convertPoint: position fromView: nil];
2554 remember_mouse_glyph (f, position.x, position.y,
2555 &dpyinfo->last_mouse_glyph);
2556 NSTRACE_POINT ("position", position);
2558 if (bar_window) *bar_window = Qnil;
2559 if (part) *part = scroll_bar_above_handle;
2561 if (x) XSETINT (*x, lrint (position.x));
2562 if (y) XSETINT (*y, lrint (position.y));
2564 *time = dpyinfo->last_mouse_movement_time;
2573 ns_frame_up_to_date (struct frame *f)
2574 /* --------------------------------------------------------------------------
2575 External (hook): Fix up mouse highlighting right after a full update.
2576 Can't use FRAME_MOUSE_UPDATE due to ns_frame_begin and ns_frame_end calls.
2577 -------------------------------------------------------------------------- */
2579 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_frame_up_to_date");
2583 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2584 if (f == hlinfo->mouse_face_mouse_frame)
2588 note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
2589 hlinfo->mouse_face_mouse_x,
2590 hlinfo->mouse_face_mouse_y);
2599 ns_define_frame_cursor (struct frame *f, Cursor cursor)
2600 /* --------------------------------------------------------------------------
2601 External (RIF): set frame mouse pointer type.
2602 -------------------------------------------------------------------------- */
2604 NSTRACE ("ns_define_frame_cursor");
2605 if (FRAME_POINTER_TYPE (f) != cursor)
2607 EmacsView *view = FRAME_NS_VIEW (f);
2608 FRAME_POINTER_TYPE (f) = cursor;
2609 [[view window] invalidateCursorRectsForView: view];
2610 /* Redisplay assumes this function also draws the changed frame
2611 cursor, but this function doesn't, so do it explicitly. */
2612 x_update_cursor (f, 1);
2618 /* ==========================================================================
2622 ========================================================================== */
2626 ns_convert_key (unsigned code)
2627 /* --------------------------------------------------------------------------
2628 Internal call used by NSView-keyDown.
2629 -------------------------------------------------------------------------- */
2631 const unsigned last_keysym = ARRAYELTS (convert_ns_to_X_keysym);
2633 /* An array would be faster, but less easy to read. */
2634 for (keysym = 0; keysym < last_keysym; keysym += 2)
2635 if (code == convert_ns_to_X_keysym[keysym])
2636 return 0xFF00 | convert_ns_to_X_keysym[keysym+1];
2638 /* if decide to use keyCode and Carbon table, use this line:
2639 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
2644 x_get_keysym_name (int keysym)
2645 /* --------------------------------------------------------------------------
2646 Called by keyboard.c. Not sure if the return val is important, except
2648 -------------------------------------------------------------------------- */
2650 static char value[16];
2651 NSTRACE ("x_get_keysym_name");
2652 sprintf (value, "%d", keysym);
2658 /* ==========================================================================
2660 Block drawing operations
2662 ========================================================================== */
2666 ns_redraw_scroll_bars (struct frame *f)
2670 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
2671 NSTRACE ("ns_redraw_scroll_bars");
2672 for (i =[subviews count]-1; i >= 0; i--)
2674 view = [subviews objectAtIndex: i];
2675 if (![view isKindOfClass: [EmacsScroller class]]) continue;
2682 ns_clear_frame (struct frame *f)
2683 /* --------------------------------------------------------------------------
2684 External (hook): Erase the entire frame
2685 -------------------------------------------------------------------------- */
2687 NSView *view = FRAME_NS_VIEW (f);
2690 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_clear_frame");
2692 /* comes on initial frame because we have
2693 after-make-frame-functions = select-frame */
2694 if (!FRAME_DEFAULT_FACE (f))
2697 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2702 ns_focus (f, &r, 1);
2703 [ns_lookup_indexed_color (NS_FACE_BACKGROUND
2704 (FACE_FROM_ID (f, DEFAULT_FACE_ID)), f) set];
2708 /* as of 2006/11 or so this is now needed */
2709 ns_redraw_scroll_bars (f);
2715 ns_clear_frame_area (struct frame *f, int x, int y, int width, int height)
2716 /* --------------------------------------------------------------------------
2717 External (RIF): Clear section of frame
2718 -------------------------------------------------------------------------- */
2720 NSRect r = NSMakeRect (x, y, width, height);
2721 NSView *view = FRAME_NS_VIEW (f);
2722 struct face *face = FRAME_DEFAULT_FACE (f);
2727 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_clear_frame_area");
2729 r = NSIntersectionRect (r, [view frame]);
2730 ns_focus (f, &r, 1);
2731 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), f) set];
2740 ns_copy_bits (struct frame *f, NSRect src, NSRect dest)
2742 NSTRACE ("ns_copy_bits");
2744 if (FRAME_NS_VIEW (f))
2746 hide_bell(); // Ensure the bell image isn't scrolled.
2748 ns_focus (f, &dest, 1);
2749 [FRAME_NS_VIEW (f) scrollRect: src
2750 by: NSMakeSize (dest.origin.x - src.origin.x,
2751 dest.origin.y - src.origin.y)];
2757 ns_scroll_run (struct window *w, struct run *run)
2758 /* --------------------------------------------------------------------------
2759 External (RIF): Insert or delete n lines at line vpos
2760 -------------------------------------------------------------------------- */
2762 struct frame *f = XFRAME (w->frame);
2763 int x, y, width, height, from_y, to_y, bottom_y;
2765 NSTRACE ("ns_scroll_run");
2767 /* begin copy from other terms */
2768 /* Get frame-relative bounding box of the text display area of W,
2769 without mode lines. Include in this box the left and right
2771 window_box (w, ANY_AREA, &x, &y, &width, &height);
2773 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
2774 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
2775 bottom_y = y + height;
2779 /* Scrolling up. Make sure we don't copy part of the mode
2780 line at the bottom. */
2781 if (from_y + run->height > bottom_y)
2782 height = bottom_y - from_y;
2784 height = run->height;
2788 /* Scrolling down. Make sure we don't copy over the mode line.
2790 if (to_y + run->height > bottom_y)
2791 height = bottom_y - to_y;
2793 height = run->height;
2795 /* end copy from other terms */
2805 NSRect srcRect = NSMakeRect (x, from_y, width, height);
2806 NSRect dstRect = NSMakeRect (x, to_y, width, height);
2808 ns_copy_bits (f, srcRect , dstRect);
2816 ns_after_update_window_line (struct window *w, struct glyph_row *desired_row)
2817 /* --------------------------------------------------------------------------
2818 External (RIF): preparatory to fringe update after text was updated
2819 -------------------------------------------------------------------------- */
2824 NSTRACE_WHEN (NSTRACE_GROUP_UPDATES, "ns_after_update_window_line");
2826 /* begin copy from other terms */
2829 if (!desired_row->mode_line_p && !w->pseudo_window_p)
2830 desired_row->redraw_fringe_bitmaps_p = 1;
2832 /* When a window has disappeared, make sure that no rest of
2833 full-width rows stays visible in the internal border. */
2834 if (windows_or_buffers_changed
2835 && desired_row->full_width_p
2836 && (f = XFRAME (w->frame),
2837 width = FRAME_INTERNAL_BORDER_WIDTH (f),
2839 && (height = desired_row->visible_height,
2842 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
2845 ns_clear_frame_area (f, 0, y, width, height);
2846 ns_clear_frame_area (f,
2847 FRAME_PIXEL_WIDTH (f) - width,
2855 ns_shift_glyphs_for_insert (struct frame *f,
2856 int x, int y, int width, int height,
2858 /* --------------------------------------------------------------------------
2859 External (RIF): copy an area horizontally, don't worry about clearing src
2860 -------------------------------------------------------------------------- */
2862 NSRect srcRect = NSMakeRect (x, y, width, height);
2863 NSRect dstRect = NSMakeRect (x+shift_by, y, width, height);
2865 NSTRACE ("ns_shift_glyphs_for_insert");
2867 ns_copy_bits (f, srcRect, dstRect);
2872 /* ==========================================================================
2874 Character encoding and metrics
2876 ========================================================================== */
2880 ns_compute_glyph_string_overhangs (struct glyph_string *s)
2881 /* --------------------------------------------------------------------------
2882 External (RIF); compute left/right overhang of whole string and set in s
2883 -------------------------------------------------------------------------- */
2885 struct font *font = s->font;
2889 struct font_metrics metrics;
2890 unsigned int codes[2];
2891 codes[0] = *(s->char2b);
2892 codes[1] = *(s->char2b + s->nchars - 1);
2894 font->driver->text_extents (font, codes, 2, &metrics);
2895 s->left_overhang = -metrics.lbearing;
2897 = metrics.rbearing > metrics.width
2898 ? metrics.rbearing - metrics.width : 0;
2902 s->left_overhang = 0;
2903 if (EQ (font->driver->type, Qns))
2904 s->right_overhang = ((struct nsfont_info *)font)->ital ?
2905 FONT_HEIGHT (font) * 0.2 : 0;
2907 s->right_overhang = 0;
2913 /* ==========================================================================
2915 Fringe and cursor drawing
2917 ========================================================================== */
2920 extern int max_used_fringe_bitmap;
2922 ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
2923 struct draw_fringe_bitmap_params *p)
2924 /* --------------------------------------------------------------------------
2925 External (RIF); fringe-related
2926 -------------------------------------------------------------------------- */
2928 /* Fringe bitmaps comes in two variants, normal and periodic. A
2929 periodic bitmap is used to create a continuous pattern. Since a
2930 bitmap is rendered one text line at a time, the start offset (dh)
2931 of the bitmap varies. Concretely, this is used for the empty
2934 For a bitmap, "h + dh" is the full height and is always
2935 invariant. For a normal bitmap "dh" is zero.
2937 For example, when the period is three and the full height is 72
2938 the following combinations exists:
2944 struct frame *f = XFRAME (WINDOW_FRAME (w));
2945 struct face *face = p->face;
2946 static EmacsImage **bimgs = NULL;
2947 static int nBimgs = 0;
2949 NSTRACE_WHEN (NSTRACE_GROUP_FRINGE, "ns_draw_fringe_bitmap");
2950 NSTRACE_MSG ("which:%d cursor:%d overlay:%d width:%d height:%d period:%d",
2951 p->which, p->cursor_p, p->overlay_p, p->wd, p->h, p->dh);
2953 /* grow bimgs if needed */
2954 if (nBimgs < max_used_fringe_bitmap)
2956 bimgs = xrealloc (bimgs, max_used_fringe_bitmap * sizeof *bimgs);
2957 memset (bimgs + nBimgs, 0,
2958 (max_used_fringe_bitmap - nBimgs) * sizeof *bimgs);
2959 nBimgs = max_used_fringe_bitmap;
2962 /* Must clip because of partially visible lines. */
2963 ns_clip_to_row (w, row, ANY_AREA, YES);
2967 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
2969 if (bx >= 0 && nx > 0)
2971 NSRect r = NSMakeRect (bx, by, nx, ny);
2973 [ns_lookup_indexed_color (face->background, f) set];
2980 NSRect r = NSMakeRect (p->x, p->y, p->wd, p->h);
2981 EmacsImage *img = bimgs[p->which - 1];
2985 // Note: For "periodic" images, allocate one EmacsImage for
2986 // the base image, and use it for all dh:s.
2987 unsigned short *bits = p->bits;
2988 int full_height = p->h + p->dh;
2990 unsigned char *cbits = xmalloc (full_height);
2992 for (i = 0; i < full_height; i++)
2994 img = [[EmacsImage alloc] initFromXBM: cbits width: 8
2997 bimgs[p->which - 1] = img;
3001 NSTRACE_RECT ("r", r);
3004 /* Since we composite the bitmap instead of just blitting it, we need
3005 to erase the whole background. */
3006 [ns_lookup_indexed_color(face->background, f) set];
3012 bm_color = ns_lookup_indexed_color(face->foreground, f);
3013 else if (p->overlay_p)
3014 bm_color = ns_lookup_indexed_color(face->background, f);
3016 bm_color = f->output_data.ns->cursor_color;
3017 [img setXBMColor: bm_color];
3020 #ifdef NS_IMPL_COCOA
3021 // Note: For periodic images, the full image height is "h + hd".
3022 // By using the height h, a suitable part of the image is used.
3023 NSRect fromRect = NSMakeRect(0, 0, p->wd, p->h);
3025 NSTRACE_RECT ("fromRect", fromRect);
3029 operation: NSCompositingOperationSourceOver
3035 NSPoint pt = r.origin;
3037 [img compositeToPoint: pt operation: NSCompositingOperationSourceOver];
3046 ns_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
3047 int x, int y, enum text_cursor_kinds cursor_type,
3048 int cursor_width, bool on_p, bool active_p)
3049 /* --------------------------------------------------------------------------
3050 External call (RIF): draw cursor.
3051 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
3052 -------------------------------------------------------------------------- */
3055 int fx, fy, h, cursor_height;
3056 struct frame *f = WINDOW_XFRAME (w);
3057 struct glyph *phys_cursor_glyph;
3058 struct glyph *cursor_glyph;
3060 NSColor *hollow_color = FRAME_BACKGROUND_COLOR (f);
3062 /* If cursor is out of bounds, don't draw garbage. This can happen
3063 in mini-buffer windows when switching between echo area glyphs
3066 NSTRACE ("ns_draw_window_cursor");
3071 w->phys_cursor_type = cursor_type;
3072 w->phys_cursor_on_p = on_p;
3074 if (cursor_type == NO_CURSOR)
3076 w->phys_cursor_width = 0;
3080 if ((phys_cursor_glyph = get_phys_cursor_glyph (w)) == NULL)
3082 if (glyph_row->exact_window_width_line_p
3083 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
3085 glyph_row->cursor_in_fringe_p = 1;
3086 draw_fringe_bitmap (w, glyph_row, 0);
3091 /* We draw the cursor (with NSRectFill), then draw the glyph on top
3092 (other terminals do it the other way round). We must set
3093 w->phys_cursor_width to the cursor width. For bar cursors, that
3094 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
3095 get_phys_cursor_geometry (w, glyph_row, phys_cursor_glyph, &fx, &fy, &h);
3097 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
3098 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
3099 if (cursor_type == BAR_CURSOR)
3101 if (cursor_width < 1)
3102 cursor_width = max (FRAME_CURSOR_WIDTH (f), 1);
3104 /* The bar cursor should never be wider than the glyph. */
3105 if (cursor_width < w->phys_cursor_width)
3106 w->phys_cursor_width = cursor_width;
3108 /* If we have an HBAR, "cursor_width" MAY specify height. */
3109 else if (cursor_type == HBAR_CURSOR)
3111 cursor_height = (cursor_width < 1) ? lrint (0.25 * h) : cursor_width;
3112 if (cursor_height > glyph_row->height)
3113 cursor_height = glyph_row->height;
3114 if (h > cursor_height) // Cursor smaller than line height, move down
3115 fy += h - cursor_height;
3119 r.origin.x = fx, r.origin.y = fy;
3121 r.size.width = w->phys_cursor_width;
3123 /* Prevent the cursor from being drawn outside the text area. */
3124 ns_clip_to_row (w, glyph_row, TEXT_AREA, NO); /* do ns_focus(f, &r, 1); if remove */
3127 face = FACE_FROM_ID_OR_NULL (f, phys_cursor_glyph->face_id);
3128 if (face && NS_FACE_BACKGROUND (face)
3129 == ns_index_color (FRAME_CURSOR_COLOR (f), f))
3131 [ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), f) set];
3132 hollow_color = FRAME_CURSOR_COLOR (f);
3135 [FRAME_CURSOR_COLOR (f) set];
3137 #ifdef NS_IMPL_COCOA
3138 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
3139 atomic. Cleaner ways of doing this should be investigated.
3140 One way would be to set a global variable DRAWING_CURSOR
3141 when making the call to draw_phys..(), don't focus in that
3142 case, then move the ns_unfocus() here after that call. */
3143 NSDisableScreenUpdates ();
3146 switch (cursor_type)
3148 case DEFAULT_CURSOR:
3151 case FILLED_BOX_CURSOR:
3154 case HOLLOW_BOX_CURSOR:
3157 NSRectFill (NSInsetRect (r, 1, 1));
3158 [FRAME_CURSOR_COLOR (f) set];
3165 /* If the character under cursor is R2L, draw the bar cursor
3166 on the right of its glyph, rather than on the left. */
3167 cursor_glyph = get_phys_cursor_glyph (w);
3168 if ((cursor_glyph->resolved_level & 1) != 0)
3169 s.origin.x += cursor_glyph->pixel_width - s.size.width;
3176 /* draw the character under the cursor */
3177 if (cursor_type != NO_CURSOR)
3178 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
3180 #ifdef NS_IMPL_COCOA
3181 NSEnableScreenUpdates ();
3188 ns_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
3189 /* --------------------------------------------------------------------------
3190 External (RIF): Draw a vertical line.
3191 -------------------------------------------------------------------------- */
3193 struct frame *f = XFRAME (WINDOW_FRAME (w));
3195 NSRect r = NSMakeRect (x, y0, 1, y1-y0);
3197 NSTRACE ("ns_draw_vertical_window_border");
3199 face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
3201 ns_focus (f, &r, 1);
3203 [ns_lookup_indexed_color(face->foreground, f) set];
3211 ns_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
3212 /* --------------------------------------------------------------------------
3213 External (RIF): Draw a window divider.
3214 -------------------------------------------------------------------------- */
3216 struct frame *f = XFRAME (WINDOW_FRAME (w));
3217 struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
3218 struct face *face_first
3219 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
3220 struct face *face_last
3221 = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
3222 unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
3223 unsigned long color_first = (face_first
3224 ? face_first->foreground
3225 : FRAME_FOREGROUND_PIXEL (f));
3226 unsigned long color_last = (face_last
3227 ? face_last->foreground
3228 : FRAME_FOREGROUND_PIXEL (f));
3229 NSRect divider = NSMakeRect (x0, y0, x1-x0, y1-y0);
3231 NSTRACE ("ns_draw_window_divider");
3233 ns_focus (f, ÷r, 1);
3235 if ((y1 - y0 > x1 - x0) && (x1 - x0 >= 3))
3236 /* A vertical divider, at least three pixels wide: Draw first and
3237 last pixels differently. */
3239 [ns_lookup_indexed_color(color_first, f) set];
3240 NSRectFill(NSMakeRect (x0, y0, 1, y1 - y0));
3241 [ns_lookup_indexed_color(color, f) set];
3242 NSRectFill(NSMakeRect (x0 + 1, y0, x1 - x0 - 2, y1 - y0));
3243 [ns_lookup_indexed_color(color_last, f) set];
3244 NSRectFill(NSMakeRect (x1 - 1, y0, 1, y1 - y0));
3246 else if ((x1 - x0 > y1 - y0) && (y1 - y0 >= 3))
3247 /* A horizontal divider, at least three pixels high: Draw first and
3248 last pixels differently. */
3250 [ns_lookup_indexed_color(color_first, f) set];
3251 NSRectFill(NSMakeRect (x0, y0, x1 - x0, 1));
3252 [ns_lookup_indexed_color(color, f) set];
3253 NSRectFill(NSMakeRect (x0, y0 + 1, x1 - x0, y1 - y0 - 2));
3254 [ns_lookup_indexed_color(color_last, f) set];
3255 NSRectFill(NSMakeRect (x0, y1 - 1, x1 - x0, 1));
3259 /* In any other case do not draw the first and last pixels
3261 [ns_lookup_indexed_color(color, f) set];
3262 NSRectFill(divider);
3269 ns_show_hourglass (struct frame *f)
3271 /* TODO: add NSProgressIndicator to all frames. */
3275 ns_hide_hourglass (struct frame *f)
3277 /* TODO: remove NSProgressIndicator from all frames. */
3280 /* ==========================================================================
3282 Glyph drawing operations
3284 ========================================================================== */
3287 ns_get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
3288 /* --------------------------------------------------------------------------
3289 Wrapper utility to account for internal border width on full-width lines,
3290 and allow top full-width rows to hit the frame top. nr should be pointer
3291 to two successive NSRects. Number of rects actually used is returned.
3292 -------------------------------------------------------------------------- */
3294 int n = get_glyph_string_clip_rects (s, nr, 2);
3298 /* --------------------------------------------------------------------
3299 Draw a wavy line under glyph string s. The wave fills wave_height
3306 wave_height = 3 | * * * *
3307 --------------------------------------------------------------------- */
3310 ns_draw_underwave (struct glyph_string *s, EmacsCGFloat width, EmacsCGFloat x)
3312 int wave_height = 3, wave_length = 2;
3313 int y, dx, dy, odd, xmax;
3318 dy = wave_height - 1;
3319 y = s->ybase - wave_height + 3;
3322 /* Find and set clipping rectangle */
3323 waveClip = NSMakeRect (x, y, width, wave_height);
3324 [[NSGraphicsContext currentContext] saveGraphicsState];
3325 NSRectClip (waveClip);
3327 /* Draw the waves */
3328 a.x = x - ((int)(x) % dx) + (EmacsCGFloat) 0.5;
3330 odd = (int)(a.x/dx) % 2;
3331 a.y = b.y = y + 0.5;
3340 [NSBezierPath strokeLineFromPoint:a toPoint:b];
3341 a.x = b.x, a.y = b.y;
3342 b.x += dx, b.y = y + 0.5 + odd*dy;
3346 /* Restore previous clipping rectangle(s) */
3347 [[NSGraphicsContext currentContext] restoreGraphicsState];
3353 ns_draw_text_decoration (struct glyph_string *s, struct face *face,
3354 NSColor *defaultCol, CGFloat width, CGFloat x)
3355 /* --------------------------------------------------------------------------
3356 Draw underline, overline, and strike-through on glyph string s.
3357 -------------------------------------------------------------------------- */
3359 if (s->for_overlaps)
3363 if (face->underline_p)
3365 if (s->face->underline_type == FACE_UNDER_WAVE)
3367 if (face->underline_defaulted_p)
3370 [ns_lookup_indexed_color (face->underline_color, s->f) set];
3372 ns_draw_underwave (s, width, x);
3374 else if (s->face->underline_type == FACE_UNDER_LINE)
3378 unsigned long thickness, position;
3380 /* If the prev was underlined, match its appearance. */
3381 if (s->prev && s->prev->face->underline_p
3382 && s->prev->face->underline_type == FACE_UNDER_LINE
3383 && s->prev->underline_thickness > 0)
3385 thickness = s->prev->underline_thickness;
3386 position = s->prev->underline_position;
3390 struct font *font = font_for_underline_metrics (s);
3391 unsigned long descent = s->y + s->height - s->ybase;
3393 /* Use underline thickness of font, defaulting to 1. */
3394 thickness = (font && font->underline_thickness > 0)
3395 ? font->underline_thickness : 1;
3397 /* Determine the offset of underlining from the baseline. */
3398 if (x_underline_at_descent_line)
3399 position = descent - thickness;
3400 else if (x_use_underline_position_properties
3401 && font && font->underline_position >= 0)
3402 position = font->underline_position;
3404 position = lround (font->descent / 2);
3406 position = underline_minimum_offset;
3408 position = max (position, underline_minimum_offset);
3410 /* Ensure underlining is not cropped. */
3411 if (descent <= position)
3413 position = descent - 1;
3416 else if (descent < position + thickness)
3420 s->underline_thickness = thickness;
3421 s->underline_position = position;
3423 r = NSMakeRect (x, s->ybase + position, width, thickness);
3425 if (face->underline_defaulted_p)
3428 [ns_lookup_indexed_color (face->underline_color, s->f) set];
3432 /* Do overline. We follow other terms in using a thickness of 1
3433 and ignoring overline_margin. */
3434 if (face->overline_p)
3437 r = NSMakeRect (x, s->y, width, 1);
3439 if (face->overline_color_defaulted_p)
3442 [ns_lookup_indexed_color (face->overline_color, s->f) set];
3446 /* Do strike-through. We follow other terms for thickness and
3447 vertical position.*/
3448 if (face->strike_through_p)
3451 /* Y-coordinate and height of the glyph string's first glyph.
3452 We cannot use s->y and s->height because those could be
3453 larger if there are taller display elements (e.g., characters
3454 displayed with a larger font) in the same glyph row. */
3455 int glyph_y = s->ybase - s->first_glyph->ascent;
3456 int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
3457 /* Strike-through width and offset from the glyph string's
3459 unsigned long h = 1;
3462 dy = lrint ((glyph_height - h) / 2);
3463 r = NSMakeRect (x, glyph_y + dy, width, 1);
3465 if (face->strike_through_color_defaulted_p)
3468 [ns_lookup_indexed_color (face->strike_through_color, s->f) set];
3474 ns_draw_box (NSRect r, CGFloat thickness, NSColor *col,
3475 char left_p, char right_p)
3476 /* --------------------------------------------------------------------------
3477 Draw an unfilled rect inside r, optionally leaving left and/or right open.
3478 Note we can't just use an NSDrawRect command, because of the possibility
3479 of some sides not being drawn, and because the rect will be filled.
3480 -------------------------------------------------------------------------- */
3486 s.size.height = thickness;
3488 s.origin.y += r.size.height - thickness;
3491 s.size.height = r.size.height;
3492 s.origin.y = r.origin.y;
3494 /* left, right (optional) */
3495 s.size.width = thickness;
3500 s.origin.x += r.size.width - thickness;
3507 ns_draw_relief (NSRect r, int thickness, char raised_p,
3508 char top_p, char bottom_p, char left_p, char right_p,
3509 struct glyph_string *s)
3510 /* --------------------------------------------------------------------------
3511 Draw a relief rect inside r, optionally leaving some sides open.
3512 Note we can't just use an NSDrawBezel command, because of the possibility
3513 of some sides not being drawn, and because the rect will be filled.
3514 -------------------------------------------------------------------------- */
3516 static NSColor *baseCol = nil, *lightCol = nil, *darkCol = nil;
3517 NSColor *newBaseCol = nil;
3520 NSTRACE ("ns_draw_relief");
3524 if (s->face->use_box_color_for_shadows_p)
3526 newBaseCol = ns_lookup_indexed_color (s->face->box_color, s->f);
3528 /* else if (s->first_glyph->type == IMAGE_GLYPH
3530 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3532 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
3536 newBaseCol = ns_lookup_indexed_color (s->face->background, s->f);
3539 if (newBaseCol == nil)
3540 newBaseCol = [NSColor grayColor];
3542 if (newBaseCol != baseCol) /* TODO: better check */
3545 baseCol = [newBaseCol retain];
3547 lightCol = [[baseCol highlightWithLevel: 0.2] retain];
3549 darkCol = [[baseCol shadowWithLevel: 0.3] retain];
3552 [(raised_p ? lightCol : darkCol) set];
3554 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
3557 sr.size.height = thickness;
3558 if (top_p) NSRectFill (sr);
3561 sr.size.height = r.size.height;
3562 sr.size.width = thickness;
3563 if (left_p) NSRectFill (sr);
3565 [(raised_p ? darkCol : lightCol) set];
3568 sr.size.width = r.size.width;
3569 sr.size.height = thickness;
3570 sr.origin.y += r.size.height - thickness;
3571 if (bottom_p) NSRectFill (sr);
3574 sr.size.height = r.size.height;
3575 sr.origin.y = r.origin.y;
3576 sr.size.width = thickness;
3577 sr.origin.x += r.size.width - thickness;
3578 if (right_p) NSRectFill (sr);
3583 ns_dumpglyphs_box_or_relief (struct glyph_string *s)
3584 /* --------------------------------------------------------------------------
3585 Function modeled after x_draw_glyph_string_box ().
3586 Sets up parameters for drawing.
3587 -------------------------------------------------------------------------- */
3589 int right_x, last_x;
3590 char left_p, right_p;
3591 struct glyph *last_glyph;
3596 if (s->hl == DRAW_MOUSE_FACE)
3598 face = FACE_FROM_ID_OR_NULL (s->f,
3599 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3601 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3606 thickness = face->box_line_width;
3608 NSTRACE ("ns_dumpglyphs_box_or_relief");
3610 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
3611 ? WINDOW_RIGHT_EDGE_X (s->w)
3612 : window_box_right (s->w, s->area));
3613 last_glyph = (s->cmp || s->img
3614 ? s->first_glyph : s->first_glyph + s->nchars-1);
3616 right_x = ((s->row->full_width_p && s->extends_to_end_of_line_p
3617 ? last_x - 1 : min (last_x, s->x + s->background_width) - 1));
3619 left_p = (s->first_glyph->left_box_line_p
3620 || (s->hl == DRAW_MOUSE_FACE
3621 && (s->prev == NULL || s->prev->hl != s->hl)));
3622 right_p = (last_glyph->right_box_line_p
3623 || (s->hl == DRAW_MOUSE_FACE
3624 && (s->next == NULL || s->next->hl != s->hl)));
3626 r = NSMakeRect (s->x, s->y, right_x - s->x + 1, s->height);
3628 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
3629 if (s->face->box == FACE_SIMPLE_BOX && s->face->box_color)
3631 ns_draw_box (r, abs (thickness),
3632 ns_lookup_indexed_color (face->box_color, s->f),
3637 ns_draw_relief (r, abs (thickness), s->face->box == FACE_RAISED_BOX,
3638 1, 1, left_p, right_p, s);
3644 ns_maybe_dumpglyphs_background (struct glyph_string *s, char force_p)
3645 /* --------------------------------------------------------------------------
3646 Modeled after x_draw_glyph_string_background, which draws BG in
3647 certain cases. Others are left to the text rendering routine.
3648 -------------------------------------------------------------------------- */
3650 NSTRACE ("ns_maybe_dumpglyphs_background");
3652 if (!s->background_filled_p/* || s->hl == DRAW_MOUSE_FACE*/)
3654 int box_line_width = max (s->face->box_line_width, 0);
3655 if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3656 /* When xdisp.c ignores FONT_HEIGHT, we cannot trust font
3657 dimensions, since the actual glyphs might be much
3658 smaller. So in that case we always clear the rectangle
3659 with background color. */
3660 || FONT_TOO_HIGH (s->font)
3661 || s->font_not_found_p || s->extends_to_end_of_line_p || force_p)
3664 if (s->hl == DRAW_MOUSE_FACE)
3667 = FACE_FROM_ID_OR_NULL (s->f,
3668 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3670 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3673 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3675 [(NS_FACE_BACKGROUND (face) != 0
3676 ? ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f)
3677 : FRAME_BACKGROUND_COLOR (s->f)) set];
3680 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (s->f);
3681 [[dpyinfo->bitmaps[face->stipple-1].img stippleMask] set];
3684 if (s->hl != DRAW_CURSOR)
3686 NSRect r = NSMakeRect (s->x, s->y + box_line_width,
3687 s->background_width,
3688 s->height-2*box_line_width);
3692 s->background_filled_p = 1;
3699 ns_dumpglyphs_image (struct glyph_string *s, NSRect r)
3700 /* --------------------------------------------------------------------------
3701 Renders an image and associated borders.
3702 -------------------------------------------------------------------------- */
3704 EmacsImage *img = s->img->pixmap;
3705 int box_line_vwidth = max (s->face->box_line_width, 0);
3706 int x = s->x, y = s->ybase - image_ascent (s->img, s->face, &s->slice);
3707 int bg_x, bg_y, bg_height;
3714 NSTRACE ("ns_dumpglyphs_image");
3716 if (s->face->box != FACE_NO_BOX
3717 && s->first_glyph->left_box_line_p && s->slice.x == 0)
3718 x += abs (s->face->box_line_width);
3721 bg_y = s->slice.y == 0 ? s->y : s->y + box_line_vwidth;
3722 bg_height = s->height;
3723 /* other terms have this, but was causing problems w/tabbar mode */
3724 /* - 2 * box_line_vwidth; */
3726 if (s->slice.x == 0) x += s->img->hmargin;
3727 if (s->slice.y == 0) y += s->img->vmargin;
3729 /* Draw BG: if we need larger area than image itself cleared, do that,
3730 otherwise, since we composite the image under NS (instead of mucking
3731 with its background color), we must clear just the image area. */
3732 if (s->hl == DRAW_MOUSE_FACE)
3734 face = FACE_FROM_ID_OR_NULL (s->f,
3735 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3737 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3740 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3742 [ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f) set];
3744 if (bg_height > s->slice.height || s->img->hmargin || s->img->vmargin
3745 || s->img->mask || s->img->pixmap == 0 || s->width != s->background_width)
3747 br = NSMakeRect (bg_x, bg_y, s->background_width, bg_height);
3748 s->background_filled_p = 1;
3752 br = NSMakeRect (x, y, s->slice.width, s->slice.height);
3757 /* Draw the image.. do we need to draw placeholder if img ==nil? */
3760 #ifdef NS_IMPL_COCOA
3761 NSRect dr = NSMakeRect (x, y, s->slice.width, s->slice.height);
3762 NSRect ir = NSMakeRect (s->slice.x,
3763 s->img->height - s->slice.y - s->slice.height,
3764 s->slice.width, s->slice.height);
3767 operation: NSCompositingOperationSourceOver
3772 [img compositeToPoint: NSMakePoint (x, y + s->slice.height)
3773 operation: NSCompositingOperationSourceOver];
3777 if (s->hl == DRAW_CURSOR)
3779 [FRAME_CURSOR_COLOR (s->f) set];
3780 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3781 tdCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3783 /* Currently on NS img->mask is always 0. Since
3784 get_window_cursor_type specifies a hollow box cursor when on
3785 a non-masked image we never reach this clause. But we put it
3786 in, in anticipation of better support for image masks on
3788 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3792 tdCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3795 /* Draw underline, overline, strike-through. */
3796 ns_draw_text_decoration (s, face, tdCol, br.size.width, br.origin.x);
3798 /* Draw relief, if requested */
3799 if (s->img->relief || s->hl ==DRAW_IMAGE_RAISED || s->hl ==DRAW_IMAGE_SUNKEN)
3801 if (s->hl == DRAW_IMAGE_SUNKEN || s->hl == DRAW_IMAGE_RAISED)
3803 th = tool_bar_button_relief >= 0 ?
3804 tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3805 raised_p = (s->hl == DRAW_IMAGE_RAISED);
3809 th = abs (s->img->relief);
3810 raised_p = (s->img->relief > 0);
3813 r.origin.x = x - th;
3814 r.origin.y = y - th;
3815 r.size.width = s->slice.width + 2*th-1;
3816 r.size.height = s->slice.height + 2*th-1;
3817 ns_draw_relief (r, th, raised_p,
3819 s->slice.y + s->slice.height == s->img->height,
3821 s->slice.x + s->slice.width == s->img->width, s);
3824 /* If there is no mask, the background won't be seen,
3825 so draw a rectangle on the image for the cursor.
3826 Do this for all images, getting transparency right is not reliable. */
3827 if (s->hl == DRAW_CURSOR)
3829 int thickness = abs (s->img->relief);
3830 if (thickness == 0) thickness = 1;
3831 ns_draw_box (br, thickness, FRAME_CURSOR_COLOR (s->f), 1, 1);
3837 ns_dumpglyphs_stretch (struct glyph_string *s)
3842 NSColor *fgCol, *bgCol;
3844 if (!s->background_filled_p)
3846 n = ns_get_glyph_string_clip_rect (s, r);
3847 *r = NSMakeRect (s->x, s->y, s->background_width, s->height);
3849 ns_focus (s->f, r, n);
3851 if (s->hl == DRAW_MOUSE_FACE)
3853 face = FACE_FROM_ID_OR_NULL (s->f,
3854 MOUSE_HL_INFO (s->f)->mouse_face_face_id);
3856 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
3859 face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
3861 bgCol = ns_lookup_indexed_color (NS_FACE_BACKGROUND (face), s->f);
3862 fgCol = ns_lookup_indexed_color (NS_FACE_FOREGROUND (face), s->f);
3864 for (i = 0; i < n; ++i)
3866 if (!s->row->full_width_p)
3868 int overrun, leftoverrun;
3870 /* truncate to avoid overwriting fringe and/or scrollbar */
3871 overrun = max (0, (s->x + s->background_width)
3872 - (WINDOW_BOX_RIGHT_EDGE_X (s->w)
3873 - WINDOW_RIGHT_FRINGE_WIDTH (s->w)));
3874 r[i].size.width -= overrun;
3876 /* truncate to avoid overwriting to left of the window box */
3877 leftoverrun = (WINDOW_BOX_LEFT_EDGE_X (s->w)
3878 + WINDOW_LEFT_FRINGE_WIDTH (s->w)) - s->x;
3880 if (leftoverrun > 0)
3882 r[i].origin.x += leftoverrun;
3883 r[i].size.width -= leftoverrun;
3886 /* XXX: Try to work between problem where a stretch glyph on
3887 a partially-visible bottom row will clear part of the
3888 modeline, and another where list-buffers headers and similar
3889 rows erroneously have visible_height set to 0. Not sure
3890 where this is coming from as other terms seem not to show. */
3891 r[i].size.height = min (s->height, s->row->visible_height);
3896 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3897 overwriting cursor (usually when cursor on a tab) */
3898 if (s->hl == DRAW_CURSOR)
3903 width = s->w->phys_cursor_width;
3904 r[i].size.width -= width;
3905 r[i].origin.x += width;
3909 /* Draw overlining, etc. on the cursor. */
3910 if (s->w->phys_cursor_type == FILLED_BOX_CURSOR)
3911 ns_draw_text_decoration (s, face, bgCol, width, x);
3913 ns_draw_text_decoration (s, face, fgCol, width, x);
3920 /* Draw overlining, etc. on the stretch glyph (or the part
3921 of the stretch glyph after the cursor). */
3922 ns_draw_text_decoration (s, face, fgCol, r[i].size.width,
3926 s->background_filled_p = 1;
3932 ns_draw_glyph_string_foreground (struct glyph_string *s)
3935 struct font *font = s->font;
3937 /* If first glyph of S has a left box line, start drawing the text
3938 of S to the right of that box line. */
3939 if (s->face && s->face->box != FACE_NO_BOX
3940 && s->first_glyph->left_box_line_p)
3941 x = s->x + eabs (s->face->box_line_width);
3945 flags = s->hl == DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR :
3946 (s->hl == DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE :
3947 (s->for_overlaps ? NS_DUMPGLYPH_FOREGROUND :
3948 NS_DUMPGLYPH_NORMAL));
3951 (s, s->cmp_from, s->nchars, x, s->ybase,
3952 (flags == NS_DUMPGLYPH_NORMAL && !s->background_filled_p)
3953 || flags == NS_DUMPGLYPH_MOUSEFACE);
3958 ns_draw_composite_glyph_string_foreground (struct glyph_string *s)
3961 struct font *font = s->font;
3963 /* If first glyph of S has a left box line, start drawing the text
3964 of S to the right of that box line. */
3965 if (s->face && s->face->box != FACE_NO_BOX
3966 && s->first_glyph->left_box_line_p)
3967 x = s->x + eabs (s->face->box_line_width);
3971 /* S is a glyph string for a composition. S->cmp_from is the index
3972 of the first character drawn for glyphs of this composition.
3973 S->cmp_from == 0 means we are drawing the very first character of
3974 this composition. */
3976 /* Draw a rectangle for the composition if the font for the very
3977 first character of the composition could not be loaded. */
3978 if (s->font_not_found_p)
3980 if (s->cmp_from == 0)
3982 NSRect r = NSMakeRect (s->x, s->y, s->width-1, s->height -1);
3983 ns_draw_box (r, 1, FRAME_CURSOR_COLOR (s->f), 1, 1);
3986 else if (! s->first_glyph->u.cmp.automatic)
3990 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
3991 /* TAB in a composition means display glyphs with padding
3992 space on the left or right. */
3993 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
3995 int xx = x + s->cmp->offsets[j * 2];
3996 int yy = y - s->cmp->offsets[j * 2 + 1];
3998 font->driver->draw (s, j, j + 1, xx, yy, false);
3999 if (s->face->overstrike)
4000 font->driver->draw (s, j, j + 1, xx + 1, yy, false);
4005 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
4010 for (i = j = s->cmp_from; i < s->cmp_to; i++)
4012 glyph = LGSTRING_GLYPH (gstring, i);
4013 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
4014 width += LGLYPH_WIDTH (glyph);
4017 int xoff, yoff, wadjust;
4021 font->driver->draw (s, j, i, x, y, false);
4022 if (s->face->overstrike)
4023 font->driver->draw (s, j, i, x + 1, y, false);
4026 xoff = LGLYPH_XOFF (glyph);
4027 yoff = LGLYPH_YOFF (glyph);
4028 wadjust = LGLYPH_WADJUST (glyph);
4029 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
4030 if (s->face->overstrike)
4031 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
4040 font->driver->draw (s, j, i, x, y, false);
4041 if (s->face->overstrike)
4042 font->driver->draw (s, j, i, x + 1, y, false);
4048 ns_draw_glyph_string (struct glyph_string *s)
4049 /* --------------------------------------------------------------------------
4050 External (RIF): Main draw-text call.
4051 -------------------------------------------------------------------------- */
4053 /* TODO (optimize): focus for box and contents draw */
4056 char box_drawn_p = 0;
4057 struct font *font = s->face->font;
4058 if (! font) font = FRAME_FONT (s->f);
4060 NSTRACE_WHEN (NSTRACE_GROUP_GLYPHS, "ns_draw_glyph_string");
4062 if (s->next && s->right_overhang && !s->for_overlaps/*&&s->hl!=DRAW_CURSOR*/)
4065 struct glyph_string *next;
4067 for (width = 0, next = s->next;
4068 next && width < s->right_overhang;
4069 width += next->width, next = next->next)
4070 if (next->first_glyph->type != IMAGE_GLYPH)
4072 if (next->first_glyph->type != STRETCH_GLYPH)
4074 n = ns_get_glyph_string_clip_rect (s->next, r);
4075 ns_focus (s->f, r, n);
4076 ns_maybe_dumpglyphs_background (s->next, 1);
4081 ns_dumpglyphs_stretch (s->next);
4083 next->num_clips = 0;
4087 if (!s->for_overlaps && s->face->box != FACE_NO_BOX
4088 && (s->first_glyph->type == CHAR_GLYPH
4089 || s->first_glyph->type == COMPOSITE_GLYPH))
4091 n = ns_get_glyph_string_clip_rect (s, r);
4092 ns_focus (s->f, r, n);
4093 ns_maybe_dumpglyphs_background (s, 1);
4094 ns_dumpglyphs_box_or_relief (s);
4099 switch (s->first_glyph->type)
4103 n = ns_get_glyph_string_clip_rect (s, r);
4104 ns_focus (s->f, r, n);
4105 ns_dumpglyphs_image (s, r[0]);
4110 ns_dumpglyphs_stretch (s);
4114 case COMPOSITE_GLYPH:
4115 n = ns_get_glyph_string_clip_rect (s, r);
4116 ns_focus (s->f, r, n);
4118 if (s->for_overlaps || (s->cmp_from > 0
4119 && ! s->first_glyph->u.cmp.automatic))
4120 s->background_filled_p = 1;
4122 ns_maybe_dumpglyphs_background
4123 (s, s->first_glyph->type == COMPOSITE_GLYPH);
4125 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
4127 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
4128 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
4129 NS_FACE_FOREGROUND (s->face) = tmp;
4133 BOOL isComposite = s->first_glyph->type == COMPOSITE_GLYPH;
4136 ns_draw_composite_glyph_string_foreground (s);
4138 ns_draw_glyph_string_foreground (s);
4142 NSColor *col = (NS_FACE_FOREGROUND (s->face) != 0
4143 ? ns_lookup_indexed_color (NS_FACE_FOREGROUND (s->face),
4145 : FRAME_FOREGROUND_COLOR (s->f));
4148 /* Draw underline, overline, strike-through. */
4149 ns_draw_text_decoration (s, s->face, col, s->width, s->x);
4152 if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
4154 unsigned long tmp = NS_FACE_BACKGROUND (s->face);
4155 NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
4156 NS_FACE_FOREGROUND (s->face) = tmp;
4162 case GLYPHLESS_GLYPH:
4163 n = ns_get_glyph_string_clip_rect (s, r);
4164 ns_focus (s->f, r, n);
4166 if (s->for_overlaps || (s->cmp_from > 0
4167 && ! s->first_glyph->u.cmp.automatic))
4168 s->background_filled_p = 1;
4170 ns_maybe_dumpglyphs_background
4171 (s, s->first_glyph->type == COMPOSITE_GLYPH);
4173 /* Not yet implemented. */
4182 /* Draw box if not done already. */
4183 if (!s->for_overlaps && !box_drawn_p && s->face->box != FACE_NO_BOX)
4185 n = ns_get_glyph_string_clip_rect (s, r);
4186 ns_focus (s->f, r, n);
4187 ns_dumpglyphs_box_or_relief (s);
4196 /* ==========================================================================
4200 ========================================================================== */
4204 ns_send_appdefined (int value)
4205 /* --------------------------------------------------------------------------
4206 Internal: post an appdefined event which EmacsApp-sendEvent will
4207 recognize and take as a command to halt the event loop.
4208 -------------------------------------------------------------------------- */
4210 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_send_appdefined(%d)", value);
4212 // GNUstep needs postEvent to happen on the main thread.
4213 // Cocoa needs nextEventMatchingMask to happen on the main thread too.
4214 if (! [[NSThread currentThread] isMainThread])
4216 EmacsApp *app = (EmacsApp *)NSApp;
4217 app->nextappdefined = value;
4218 [app performSelectorOnMainThread:@selector (sendFromMainThread:)
4224 /* Only post this event if we haven't already posted one. This will end
4225 the [NXApp run] main loop after having processed all events queued at
4228 #ifdef NS_IMPL_COCOA
4229 if (! send_appdefined)
4231 /* OS X 10.10.1 swallows the AppDefined event we are sending ourselves
4232 in certain situations (rapid incoming events).
4233 So check if we have one, if not add one. */
4234 NSEvent *appev = [NSApp nextEventMatchingMask:NSEventMaskApplicationDefined
4235 untilDate:[NSDate distantPast]
4236 inMode:NSDefaultRunLoopMode
4238 if (! appev) send_appdefined = YES;
4242 if (send_appdefined)
4246 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
4247 send_appdefined = NO;
4249 /* Don't need wakeup timer any more */
4252 [timed_entry invalidate];
4253 [timed_entry release];
4257 nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
4258 location: NSMakePoint (0, 0)
4261 windowNumber: [[NSApp mainWindow] windowNumber]
4262 context: [NSApp context]
4267 /* Post an application defined event on the event queue. When this is
4268 received the [NXApp run] will return, thus having processed all
4269 events which are currently queued. */
4270 [NSApp postEvent: nxev atStart: NO];
4274 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
4278 Lisp_Object frame, tail;
4280 if (ns_last_use_native_fullscreen == ns_use_native_fullscreen)
4283 ns_last_use_native_fullscreen = ns_use_native_fullscreen;
4285 FOR_EACH_FRAME (tail, frame)
4287 struct frame *f = XFRAME (frame);
4290 EmacsView *view = FRAME_NS_VIEW (f);
4291 [view updateCollectionBehavior];
4297 /* GNUstep does not have cancelTracking. */
4298 #ifdef NS_IMPL_COCOA
4299 /* Check if menu open should be canceled or continued as normal. */
4301 ns_check_menu_open (NSMenu *menu)
4303 /* Click in menu bar? */
4304 NSArray *a = [[NSApp mainMenu] itemArray];
4308 if (menu == nil) // Menu tracking ended.
4310 if (menu_will_open_state == MENU_OPENING)
4311 menu_will_open_state = MENU_NONE;
4315 for (i = 0; ! found && i < [a count]; i++)
4316 found = menu == [[a objectAtIndex:i] submenu];
4319 if (menu_will_open_state == MENU_NONE && emacs_event)
4321 NSEvent *theEvent = [NSApp currentEvent];
4322 struct frame *emacsframe = SELECTED_FRAME ();
4324 [menu cancelTracking];
4325 menu_will_open_state = MENU_PENDING;
4326 emacs_event->kind = MENU_BAR_ACTIVATE_EVENT;
4327 EV_TRAILER (theEvent);
4329 CGEventRef ourEvent = CGEventCreate (NULL);
4330 menu_mouse_point = CGEventGetLocation (ourEvent);
4331 CFRelease (ourEvent);
4333 else if (menu_will_open_state == MENU_OPENING)
4335 menu_will_open_state = MENU_NONE;
4340 /* Redo saved menu click if state is MENU_PENDING. */
4342 ns_check_pending_open_menu ()
4344 if (menu_will_open_state == MENU_PENDING)
4346 CGEventSourceRef source
4347 = CGEventSourceCreate (kCGEventSourceStateHIDSystemState);
4349 CGEventRef event = CGEventCreateMouseEvent (source,
4350 kCGEventLeftMouseDown,
4352 kCGMouseButtonLeft);
4353 CGEventSetType (event, kCGEventLeftMouseDown);
4354 CGEventPost (kCGHIDEventTap, event);
4358 menu_will_open_state = MENU_OPENING;
4361 #endif /* NS_IMPL_COCOA */
4364 ns_read_socket (struct terminal *terminal, struct input_event *hold_quit)
4365 /* --------------------------------------------------------------------------
4366 External (hook): Post an event to ourself and keep reading events until
4367 we read it back again. In effect process all events which were waiting.
4368 From 21+ we have to manage the event buffer ourselves.
4369 -------------------------------------------------------------------------- */
4371 struct input_event ev;
4374 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_read_socket");
4376 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
4380 if ([NSApp modalWindow] != nil)
4383 if (hold_event_q.nr > 0)
4386 for (i = 0; i < hold_event_q.nr; ++i)
4387 kbd_buffer_store_event_hold (&hold_event_q.q[i], hold_quit);
4388 hold_event_q.nr = 0;
4392 if ([NSThread isMainThread])
4395 n_emacs_events_pending = 0;
4396 ns_init_events (&ev);
4397 q_event_ptr = hold_quit;
4399 /* we manage autorelease pools by allocate/reallocate each time around
4400 the loop; strict nesting is occasionally violated but seems not to
4401 matter.. earlier methods using full nesting caused major memory leaks */
4402 [outerpool release];
4403 outerpool = [[NSAutoreleasePool alloc] init];
4405 /* If have pending open-file requests, attend to the next one of those. */
4406 if (ns_pending_files && [ns_pending_files count] != 0
4407 && [(EmacsApp *)NSApp openFile: [ns_pending_files objectAtIndex: 0]])
4409 [ns_pending_files removeObjectAtIndex: 0];
4411 /* Deal with pending service requests. */
4412 else if (ns_pending_service_names && [ns_pending_service_names count] != 0
4414 NSApp fulfillService: [ns_pending_service_names objectAtIndex: 0]
4415 withArg: [ns_pending_service_args objectAtIndex: 0]])
4417 [ns_pending_service_names removeObjectAtIndex: 0];
4418 [ns_pending_service_args removeObjectAtIndex: 0];
4422 /* Run and wait for events. We must always send one NX_APPDEFINED event
4423 to ourself, otherwise [NXApp run] will never exit. */
4424 send_appdefined = YES;
4425 ns_send_appdefined (-1);
4430 nevents = n_emacs_events_pending;
4431 n_emacs_events_pending = 0;
4432 ns_finish_events ();
4444 ns_select (int nfds, fd_set *readfds, fd_set *writefds,
4445 fd_set *exceptfds, struct timespec *timeout,
4447 /* --------------------------------------------------------------------------
4448 Replacement for select, checking for events
4449 -------------------------------------------------------------------------- */
4453 struct input_event event;
4456 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_select");
4458 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
4462 if (hold_event_q.nr > 0)
4464 /* We already have events pending. */
4470 for (k = 0; k < nfds+1; k++)
4472 if (readfds && FD_ISSET(k, readfds)) ++nr;
4473 if (writefds && FD_ISSET(k, writefds)) ++nr;
4477 || ![NSThread isMainThread]
4478 || (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0))
4479 return thread_select(pselect, nfds, readfds, writefds,
4480 exceptfds, timeout, sigmask);
4483 struct timespec t = {0, 0};
4484 thread_select(pselect, 0, NULL, NULL, NULL, &t, sigmask);
4487 [outerpool release];
4488 outerpool = [[NSAutoreleasePool alloc] init];
4491 send_appdefined = YES;
4494 pthread_mutex_lock (&select_mutex);
4499 select_readfds = *readfds;
4500 select_valid += SELECT_HAVE_READ;
4504 select_writefds = *writefds;
4505 select_valid += SELECT_HAVE_WRITE;
4510 select_timeout = *timeout;
4511 select_valid += SELECT_HAVE_TMO;
4514 pthread_mutex_unlock (&select_mutex);
4516 /* Inform fd_handler that select should be called */
4518 emacs_write_sig (selfds[1], &c, 1);
4520 else if (nr == 0 && timeout)
4522 /* No file descriptor, just a timeout, no need to wake fd_handler */
4523 double time = timespectod (*timeout);
4524 timed_entry = [[NSTimer scheduledTimerWithTimeInterval: time
4527 @selector (timeout_handler:)
4532 else /* No timeout and no file descriptors, can this happen? */
4534 /* Send appdefined so we exit from the loop */
4535 ns_send_appdefined (-1);
4539 ns_init_events (&event);
4543 ns_finish_events ();
4544 if (nr > 0 && readfds)
4547 emacs_write_sig (selfds[1], &c, 1);
4551 t = last_appdefined_event_data;
4553 if (t != NO_APPDEFINED_DATA)
4555 last_appdefined_event_data = NO_APPDEFINED_DATA;
4559 /* The NX_APPDEFINED event we received was a timeout. */
4564 /* The NX_APPDEFINED event we received was the result of
4565 at least one real input event arriving. */
4571 /* Received back from select () in fd_handler; copy the results */
4572 pthread_mutex_lock (&select_mutex);
4573 if (readfds) *readfds = select_readfds;
4574 if (writefds) *writefds = select_writefds;
4575 pthread_mutex_unlock (&select_mutex);
4590 ns_run_loop_break ()
4591 /* Break out of the NS run loop in ns_select or ns_read_socket. */
4593 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_run_loop_break");
4595 /* If we don't have a GUI, don't send the event. */
4597 ns_send_appdefined(-1);
4602 /* ==========================================================================
4606 ========================================================================== */
4610 ns_set_vertical_scroll_bar (struct window *window,
4611 int portion, int whole, int position)
4612 /* --------------------------------------------------------------------------
4613 External (hook): Update or add scrollbar
4614 -------------------------------------------------------------------------- */
4618 struct frame *f = XFRAME (WINDOW_FRAME (window));
4619 EmacsView *view = FRAME_NS_VIEW (f);
4621 int window_y, window_height;
4622 int top, left, height, width;
4623 BOOL update_p = YES;
4625 /* optimization; display engine sends WAY too many of these.. */
4626 if (!NILP (window->vertical_scroll_bar))
4628 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
4629 if ([bar checkSamePosition: position portion: portion whole: whole])
4631 if (view->scrollbarsNeedingUpdate == 0)
4633 if (!windows_or_buffers_changed)
4637 view->scrollbarsNeedingUpdate--;
4642 NSTRACE ("ns_set_vertical_scroll_bar");
4644 /* Get dimensions. */
4645 window_box (window, ANY_AREA, 0, &window_y, 0, &window_height);
4647 height = window_height;
4648 width = NS_SCROLL_BAR_WIDTH (f);
4649 left = WINDOW_SCROLL_BAR_AREA_X (window);
4651 r = NSMakeRect (left, top, width, height);
4652 /* the parent view is flipped, so we need to flip y value */
4654 r.origin.y = (v.size.height - r.size.height - r.origin.y);
4656 XSETWINDOW (win, window);
4659 /* we want at least 5 lines to display a scrollbar */
4660 if (WINDOW_TOTAL_LINES (window) < 5)
4662 if (!NILP (window->vertical_scroll_bar))
4664 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
4665 [bar removeFromSuperview];
4666 wset_vertical_scroll_bar (window, Qnil);
4669 ns_clear_frame_area (f, left, top, width, height);
4674 if (NILP (window->vertical_scroll_bar))
4676 if (width > 0 && height > 0)
4677 ns_clear_frame_area (f, left, top, width, height);
4679 bar = [[EmacsScroller alloc] initFrame: r window: win];
4680 wset_vertical_scroll_bar (window, make_save_ptr (bar));
4686 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
4687 oldRect = [bar frame];
4688 r.size.width = oldRect.size.width;
4689 if (FRAME_LIVE_P (f) && !NSEqualRects (oldRect, r))
4691 if (oldRect.origin.x != r.origin.x)
4692 ns_clear_frame_area (f, left, top, width, height);
4698 [bar setPosition: position portion: portion whole: whole];
4704 ns_set_horizontal_scroll_bar (struct window *window,
4705 int portion, int whole, int position)
4706 /* --------------------------------------------------------------------------
4707 External (hook): Update or add scrollbar
4708 -------------------------------------------------------------------------- */
4712 struct frame *f = XFRAME (WINDOW_FRAME (window));
4713 EmacsView *view = FRAME_NS_VIEW (f);
4715 int top, height, left, width;
4716 int window_x, window_width;
4717 BOOL update_p = YES;
4719 /* optimization; display engine sends WAY too many of these.. */
4720 if (!NILP (window->horizontal_scroll_bar))
4722 bar = XNS_SCROLL_BAR (window->horizontal_scroll_bar);
4723 if ([bar checkSamePosition: position portion: portion whole: whole])
4725 if (view->scrollbarsNeedingUpdate == 0)
4727 if (!windows_or_buffers_changed)
4731 view->scrollbarsNeedingUpdate--;
4736 NSTRACE ("ns_set_horizontal_scroll_bar");
4738 /* Get dimensions. */
4739 window_box (window, ANY_AREA, &window_x, 0, &window_width, 0);
4741 width = window_width;
4742 height = NS_SCROLL_BAR_HEIGHT (f);
4743 top = WINDOW_SCROLL_BAR_AREA_Y (window);
4745 r = NSMakeRect (left, top, width, height);
4746 /* the parent view is flipped, so we need to flip y value */
4748 r.origin.y = (v.size.height - r.size.height - r.origin.y);
4750 XSETWINDOW (win, window);
4753 if (NILP (window->horizontal_scroll_bar))
4755 if (width > 0 && height > 0)
4756 ns_clear_frame_area (f, left, top, width, height);
4758 bar = [[EmacsScroller alloc] initFrame: r window: win];
4759 wset_horizontal_scroll_bar (window, make_save_ptr (bar));
4765 bar = XNS_SCROLL_BAR (window->horizontal_scroll_bar);
4766 oldRect = [bar frame];
4767 if (FRAME_LIVE_P (f) && !NSEqualRects (oldRect, r))
4769 if (oldRect.origin.y != r.origin.y)
4770 ns_clear_frame_area (f, left, top, width, height);
4776 /* If there are both horizontal and vertical scroll-bars they leave
4777 a square that belongs to neither. We need to clear it otherwise
4778 it fills with junk. */
4779 if (!NILP (window->vertical_scroll_bar))
4780 ns_clear_frame_area (f, WINDOW_SCROLL_BAR_AREA_X (window), top,
4781 NS_SCROLL_BAR_HEIGHT (f), height);
4784 [bar setPosition: position portion: portion whole: whole];
4790 ns_condemn_scroll_bars (struct frame *f)
4791 /* --------------------------------------------------------------------------
4792 External (hook): arrange for all frame's scrollbars to be removed
4793 at next call to judge_scroll_bars, except for those redeemed.
4794 -------------------------------------------------------------------------- */
4798 NSArray *subviews = [[FRAME_NS_VIEW (f) superview] subviews];
4800 NSTRACE ("ns_condemn_scroll_bars");
4802 for (i =[subviews count]-1; i >= 0; i--)
4804 view = [subviews objectAtIndex: i];
4805 if ([view isKindOfClass: [EmacsScroller class]])
4812 ns_redeem_scroll_bar (struct window *window)
4813 /* --------------------------------------------------------------------------
4814 External (hook): arrange to spare this window's scrollbar
4815 at next call to judge_scroll_bars.
4816 -------------------------------------------------------------------------- */
4819 NSTRACE ("ns_redeem_scroll_bar");
4820 if (!NILP (window->vertical_scroll_bar)
4821 && WINDOW_HAS_VERTICAL_SCROLL_BAR (window))
4823 bar = XNS_SCROLL_BAR (window->vertical_scroll_bar);
4827 if (!NILP (window->horizontal_scroll_bar)
4828 && WINDOW_HAS_HORIZONTAL_SCROLL_BAR (window))
4830 bar = XNS_SCROLL_BAR (window->horizontal_scroll_bar);
4837 ns_judge_scroll_bars (struct frame *f)
4838 /* --------------------------------------------------------------------------
4839 External (hook): destroy all scrollbars on frame that weren't
4840 redeemed after call to condemn_scroll_bars.
4841 -------------------------------------------------------------------------- */
4845 EmacsView *eview = FRAME_NS_VIEW (f);
4846 NSArray *subviews = [[eview superview] subviews];
4849 NSTRACE ("ns_judge_scroll_bars");
4850 for (i = [subviews count]-1; i >= 0; --i)
4852 view = [subviews objectAtIndex: i];
4853 if (![view isKindOfClass: [EmacsScroller class]]) continue;
4859 [eview updateFrameSize: NO];
4862 /* ==========================================================================
4866 ========================================================================== */
4869 x_display_pixel_height (struct ns_display_info *dpyinfo)
4871 NSArray *screens = [NSScreen screens];
4872 NSEnumerator *enumerator = [screens objectEnumerator];
4877 while ((screen = [enumerator nextObject]) != nil)
4878 frame = NSUnionRect (frame, [screen frame]);
4880 return NSHeight (frame);
4884 x_display_pixel_width (struct ns_display_info *dpyinfo)
4886 NSArray *screens = [NSScreen screens];
4887 NSEnumerator *enumerator = [screens objectEnumerator];
4892 while ((screen = [enumerator nextObject]) != nil)
4893 frame = NSUnionRect (frame, [screen frame]);
4895 return NSWidth (frame);
4899 static Lisp_Object ns_string_to_lispmod (const char *s)
4900 /* --------------------------------------------------------------------------
4901 Convert modifier name to lisp symbol
4902 -------------------------------------------------------------------------- */
4904 if (!strncmp (SSDATA (SYMBOL_NAME (Qmeta)), s, 10))
4906 else if (!strncmp (SSDATA (SYMBOL_NAME (Qsuper)), s, 10))
4908 else if (!strncmp (SSDATA (SYMBOL_NAME (Qcontrol)), s, 10))
4910 else if (!strncmp (SSDATA (SYMBOL_NAME (Qalt)), s, 10))
4912 else if (!strncmp (SSDATA (SYMBOL_NAME (Qhyper)), s, 10))
4914 else if (!strncmp (SSDATA (SYMBOL_NAME (Qnone)), s, 10))
4922 ns_default (const char *parameter, Lisp_Object *result,
4923 Lisp_Object yesval, Lisp_Object noval,
4924 BOOL is_float, BOOL is_modstring)
4925 /* --------------------------------------------------------------------------
4926 Check a parameter value in user's preferences
4927 -------------------------------------------------------------------------- */
4929 const char *value = ns_get_defaults_value (parameter);
4935 if (c_strcasecmp (value, "YES") == 0)
4937 else if (c_strcasecmp (value, "NO") == 0)
4939 else if (is_float && (f = strtod (value, &pos), pos != value))
4940 *result = make_float (f);
4941 else if (is_modstring && value)
4942 *result = ns_string_to_lispmod (value);
4943 else fprintf (stderr,
4944 "Bad value for default \"%s\": \"%s\"\n", parameter, value);
4950 ns_initialize_display_info (struct ns_display_info *dpyinfo)
4951 /* --------------------------------------------------------------------------
4952 Initialize global info and storage for display.
4953 -------------------------------------------------------------------------- */
4955 NSScreen *screen = [NSScreen mainScreen];
4956 NSWindowDepth depth = [screen depth];
4958 dpyinfo->resx = 72.27; /* used 75.0, but this makes pt == pixel, expected */
4959 dpyinfo->resy = 72.27;
4960 dpyinfo->color_p = ![NSDeviceWhiteColorSpace isEqualToString:
4961 NSColorSpaceFromDepth (depth)]
4962 && ![NSCalibratedWhiteColorSpace isEqualToString:
4963 NSColorSpaceFromDepth (depth)];
4964 dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth);
4965 dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table);
4966 dpyinfo->color_table->colors = NULL;
4967 dpyinfo->root_window = 42; /* a placeholder.. */
4968 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame = NULL;
4969 dpyinfo->n_fonts = 0;
4970 dpyinfo->smallest_font_height = 1;
4971 dpyinfo->smallest_char_width = 1;
4973 reset_mouse_highlight (&dpyinfo->mouse_highlight);
4977 /* This and next define (many of the) public functions in this file. */
4978 /* x_... are generic versions in xdisp.c that we, and other terms, get away
4979 with using despite presence in the "system dependent" redisplay
4980 interface. In addition, many of the ns_ methods have code that is
4981 shared with all terms, indicating need for further refactoring. */
4982 extern frame_parm_handler ns_frame_parm_handlers[];
4983 static struct redisplay_interface ns_redisplay_interface =
4985 ns_frame_parm_handlers,
4989 x_clear_end_of_line,
4991 ns_after_update_window_line,
4992 ns_update_window_begin,
4993 ns_update_window_end,
4994 0, /* flush_display */
4995 x_clear_window_mouse_face,
4996 x_get_glyph_overhangs,
4997 x_fix_overlapping_area,
4998 ns_draw_fringe_bitmap,
4999 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
5000 0, /* destroy_fringe_bitmap */
5001 ns_compute_glyph_string_overhangs,
5002 ns_draw_glyph_string,
5003 ns_define_frame_cursor,
5004 ns_clear_frame_area,
5005 ns_draw_window_cursor,
5006 ns_draw_vertical_window_border,
5007 ns_draw_window_divider,
5008 ns_shift_glyphs_for_insert,
5015 ns_delete_display (struct ns_display_info *dpyinfo)
5021 /* This function is called when the last frame on a display is deleted. */
5023 ns_delete_terminal (struct terminal *terminal)
5025 struct ns_display_info *dpyinfo = terminal->display_info.ns;
5027 NSTRACE ("ns_delete_terminal");
5029 /* Protect against recursive calls. delete_frame in
5030 delete_terminal calls us back when it deletes our last frame. */
5031 if (!terminal->name)
5036 x_destroy_all_bitmaps (dpyinfo);
5037 ns_delete_display (dpyinfo);
5042 static struct terminal *
5043 ns_create_terminal (struct ns_display_info *dpyinfo)
5044 /* --------------------------------------------------------------------------
5045 Set up use of NS before we make the first connection.
5046 -------------------------------------------------------------------------- */
5048 struct terminal *terminal;
5050 NSTRACE ("ns_create_terminal");
5052 terminal = create_terminal (output_ns, &ns_redisplay_interface);
5054 terminal->display_info.ns = dpyinfo;
5055 dpyinfo->terminal = terminal;
5057 terminal->clear_frame_hook = ns_clear_frame;
5058 terminal->ring_bell_hook = ns_ring_bell;
5059 terminal->update_begin_hook = ns_update_begin;
5060 terminal->update_end_hook = ns_update_end;
5061 terminal->read_socket_hook = ns_read_socket;
5062 terminal->frame_up_to_date_hook = ns_frame_up_to_date;
5063 terminal->mouse_position_hook = ns_mouse_position;
5064 terminal->frame_rehighlight_hook = ns_frame_rehighlight;
5065 terminal->frame_raise_lower_hook = ns_frame_raise_lower;
5066 terminal->fullscreen_hook = ns_fullscreen_hook;
5067 terminal->menu_show_hook = ns_menu_show;
5068 terminal->popup_dialog_hook = ns_popup_dialog;
5069 terminal->set_vertical_scroll_bar_hook = ns_set_vertical_scroll_bar;
5070 terminal->set_horizontal_scroll_bar_hook = ns_set_horizontal_scroll_bar;
5071 terminal->condemn_scroll_bars_hook = ns_condemn_scroll_bars;
5072 terminal->redeem_scroll_bar_hook = ns_redeem_scroll_bar;
5073 terminal->judge_scroll_bars_hook = ns_judge_scroll_bars;
5074 terminal->delete_frame_hook = x_destroy_window;
5075 terminal->delete_terminal_hook = ns_delete_terminal;
5076 /* Other hooks are NULL by default. */
5082 struct ns_display_info *
5083 ns_term_init (Lisp_Object display_name)
5084 /* --------------------------------------------------------------------------
5085 Start the Application and get things rolling.
5086 -------------------------------------------------------------------------- */
5088 struct terminal *terminal;
5089 struct ns_display_info *dpyinfo;
5090 static int ns_initialized = 0;
5093 if (ns_initialized) return x_display_list;
5098 NSTRACE ("ns_term_init");
5100 [outerpool release];
5101 outerpool = [[NSAutoreleasePool alloc] init];
5103 /* count object allocs (About, click icon); on macOS use ObjectAlloc tool */
5104 /*GSDebugAllocationActive (YES); */
5108 Fset_input_interrupt_mode (Qnil);
5110 if (selfds[0] == -1)
5112 if (emacs_pipe (selfds) != 0)
5114 fprintf (stderr, "Failed to create pipe: %s\n",
5115 emacs_strerror (errno));
5119 fcntl (selfds[0], F_SETFL, O_NONBLOCK|fcntl (selfds[0], F_GETFL));
5120 FD_ZERO (&select_readfds);
5121 FD_ZERO (&select_writefds);
5122 pthread_mutex_init (&select_mutex, NULL);
5125 ns_pending_files = [[NSMutableArray alloc] init];
5126 ns_pending_service_names = [[NSMutableArray alloc] init];
5127 ns_pending_service_args = [[NSMutableArray alloc] init];
5129 /* Start app and create the main menu, window, view.
5130 Needs to be here because ns_initialize_display_info () uses AppKit classes.
5131 The view will then ask the NSApp to stop and return to Emacs. */
5132 [EmacsApp sharedApplication];
5135 [NSApp setDelegate: NSApp];
5137 /* Start the select thread. */
5138 [NSThread detachNewThreadSelector:@selector (fd_handler:)
5142 /* debugging: log all notifications */
5143 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
5144 selector: @selector (logNotification:)
5145 name: nil object: nil]; */
5147 dpyinfo = xzalloc (sizeof *dpyinfo);
5149 ns_initialize_display_info (dpyinfo);
5150 terminal = ns_create_terminal (dpyinfo);
5152 terminal->kboard = allocate_kboard (Qns);
5153 /* Don't let the initial kboard remain current longer than necessary.
5154 That would cause problems if a file loaded on startup tries to
5155 prompt in the mini-buffer. */
5156 if (current_kboard == initial_kboard)
5157 current_kboard = terminal->kboard;
5158 terminal->kboard->reference_count++;
5160 dpyinfo->next = x_display_list;
5161 x_display_list = dpyinfo;
5163 dpyinfo->name_list_element = Fcons (display_name, Qnil);
5165 terminal->name = xlispstrdup (display_name);
5169 if (!inhibit_x_resources)
5171 ns_default ("GSFontAntiAlias", &ns_antialias_text,
5174 /* this is a standard variable */
5175 ns_default ("AppleAntiAliasingThreshold", &tmp,
5176 make_float (10.0), make_float (6.0), YES, NO);
5177 ns_antialias_threshold = NILP (tmp) ? 10.0 : extract_float (tmp);
5180 NSTRACE_MSG ("Colors");
5183 NSColorList *cl = [NSColorList colorListNamed: @"Emacs"];
5187 Lisp_Object color_file, color_map, color;
5191 color_file = Fexpand_file_name (build_string ("rgb.txt"),
5192 Fsymbol_value (intern ("data-directory")));
5194 color_map = Fx_load_color_file (color_file);
5195 if (NILP (color_map))
5196 fatal ("Could not read %s.\n", SDATA (color_file));
5198 cl = [[NSColorList alloc] initWithName: @"Emacs"];
5199 for ( ; CONSP (color_map); color_map = XCDR (color_map))
5201 color = XCAR (color_map);
5202 name = SSDATA (XCAR (color));
5203 c = XINT (XCDR (color));
5205 [NSColor colorForEmacsRed: RED_FROM_ULONG (c) / 255.0
5206 green: GREEN_FROM_ULONG (c) / 255.0
5207 blue: BLUE_FROM_ULONG (c) / 255.0
5209 forKey: [NSString stringWithUTF8String: name]];
5211 [cl writeToFile: nil];
5215 NSTRACE_MSG ("Versions");
5218 #ifdef NS_IMPL_GNUSTEP
5219 Vwindow_system_version = build_string (gnustep_base_version);
5221 /*PSnextrelease (128, c); */
5222 char c[DBL_BUFSIZE_BOUND];
5223 int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber);
5224 Vwindow_system_version = make_unibyte_string (c, len);
5228 delete_keyboard_wait_descriptor (0);
5230 ns_app_name = [[NSProcessInfo processInfo] processName];
5232 /* Set up macOS app menu */
5234 NSTRACE_MSG ("Menu init");
5236 #ifdef NS_IMPL_COCOA
5240 /* set up the application menu */
5241 svcsMenu = [[EmacsMenu alloc] initWithTitle: @"Services"];
5242 [svcsMenu setAutoenablesItems: NO];
5243 appMenu = [[EmacsMenu alloc] initWithTitle: @"Emacs"];
5244 [appMenu setAutoenablesItems: NO];
5245 mainMenu = [[EmacsMenu alloc] initWithTitle: @""];
5246 dockMenu = [[EmacsMenu alloc] initWithTitle: @""];
5248 [appMenu insertItemWithTitle: @"About Emacs"
5249 action: @selector (orderFrontStandardAboutPanel:)
5252 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 1];
5253 [appMenu insertItemWithTitle: @"Preferences..."
5254 action: @selector (showPreferencesWindow:)
5257 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 3];
5258 item = [appMenu insertItemWithTitle: @"Services"
5259 action: @selector (menuDown:)
5262 [appMenu setSubmenu: svcsMenu forItem: item];
5263 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 5];
5264 [appMenu insertItemWithTitle: @"Hide Emacs"
5265 action: @selector (hide:)
5268 item = [appMenu insertItemWithTitle: @"Hide Others"
5269 action: @selector (hideOtherApplications:)
5272 [item setKeyEquivalentModifierMask: NSEventModifierFlagCommand | NSEventModifierFlagOption];
5273 [appMenu insertItem: [NSMenuItem separatorItem] atIndex: 8];
5274 [appMenu insertItemWithTitle: @"Quit Emacs"
5275 action: @selector (terminate:)
5279 item = [mainMenu insertItemWithTitle: ns_app_name
5280 action: @selector (menuDown:)
5283 [mainMenu setSubmenu: appMenu forItem: item];
5284 [dockMenu insertItemWithTitle: @"New Frame"
5285 action: @selector (newFrame:)
5289 [NSApp setMainMenu: mainMenu];
5290 [NSApp setAppleMenu: appMenu];
5291 [NSApp setServicesMenu: svcsMenu];
5292 /* Needed at least on Cocoa, to get dock menu to show windows */
5293 [NSApp setWindowsMenu: [[NSMenu alloc] init]];
5295 [[NSNotificationCenter defaultCenter]
5296 addObserver: mainMenu
5297 selector: @selector (trackingNotification:)
5298 name: NSMenuDidBeginTrackingNotification object: mainMenu];
5299 [[NSNotificationCenter defaultCenter]
5300 addObserver: mainMenu
5301 selector: @selector (trackingNotification:)
5302 name: NSMenuDidEndTrackingNotification object: mainMenu];
5304 #endif /* macOS menu setup */
5306 /* Register our external input/output types, used for determining
5307 applicable services and also drag/drop eligibility. */
5309 NSTRACE_MSG ("Input/output types");
5311 ns_send_types = [[NSArray arrayWithObjects: NSStringPboardType, nil] retain];
5312 ns_return_types = [[NSArray arrayWithObjects: NSStringPboardType, nil]
5314 ns_drag_types = [[NSArray arrayWithObjects:
5316 NSTabularTextPboardType,
5317 NSFilenamesPboardType,
5318 NSURLPboardType, nil] retain];
5320 /* If fullscreen is in init/default-frame-alist, focus isn't set
5321 right for fullscreen windows, so set this. */
5322 [NSApp activateIgnoringOtherApps:YES];
5324 NSTRACE_MSG ("Call NSApp run");
5327 ns_do_open_file = YES;
5329 #ifdef NS_IMPL_GNUSTEP
5330 /* GNUstep steals SIGCHLD for use in NSTask, but we don't use NSTask.
5331 We must re-catch it so subprocess works. */
5332 catch_child_signal ();
5335 NSTRACE_MSG ("ns_term_init done");
5344 ns_term_shutdown (int sig)
5346 [[NSUserDefaults standardUserDefaults] synchronize];
5348 /* code not reached in emacs.c after this is called by shut_down_emacs: */
5349 if (STRINGP (Vauto_save_list_file_name))
5350 unlink (SSDATA (Vauto_save_list_file_name));
5352 if (sig == 0 || sig == SIGTERM)
5354 [NSApp terminate: NSApp];
5356 else // force a stack trace to happen
5363 /* ==========================================================================
5365 EmacsApp implementation
5367 ========================================================================== */
5370 @implementation EmacsApp
5374 NSTRACE ("[EmacsApp init]");
5376 if ((self = [super init]))
5378 #ifdef NS_IMPL_COCOA
5379 self->isFirst = YES;
5381 #ifdef NS_IMPL_GNUSTEP
5382 self->applicationDidFinishLaunchingCalled = NO;
5389 #ifdef NS_IMPL_COCOA
5392 NSTRACE ("[EmacsApp run]");
5394 #ifndef NSAppKitVersionNumber10_9
5395 #define NSAppKitVersionNumber10_9 1265
5398 if ((int)NSAppKitVersionNumber != NSAppKitVersionNumber10_9)
5404 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
5406 if (isFirst) [self finishLaunching];
5409 shouldKeepRunning = YES;
5413 pool = [[NSAutoreleasePool alloc] init];
5416 [self nextEventMatchingMask:NSEventMaskAny
5417 untilDate:[NSDate distantFuture]
5418 inMode:NSDefaultRunLoopMode
5421 [self sendEvent:event];
5422 [self updateWindows];
5423 } while (shouldKeepRunning);
5428 - (void)stop: (id)sender
5430 NSTRACE ("[EmacsApp stop:]");
5432 shouldKeepRunning = NO;
5433 // Stop possible dialog also. Noop if no dialog present.
5434 // The file dialog still leaks 7k - 10k on 10.9 though.
5435 [super stop:sender];
5437 #endif /* NS_IMPL_COCOA */
5439 - (void)logNotification: (NSNotification *)notification
5441 NSTRACE ("[EmacsApp logNotification:]");
5443 const char *name = [[notification name] UTF8String];
5444 if (!strstr (name, "Update") && !strstr (name, "NSMenu")
5445 && !strstr (name, "WindowNumber"))
5446 NSLog (@"notification: '%@'", [notification name]);
5450 - (void)sendEvent: (NSEvent *)theEvent
5451 /* --------------------------------------------------------------------------
5452 Called when NSApp is running for each event received. Used to stop
5453 the loop when we choose, since there's no way to just run one iteration.
5454 -------------------------------------------------------------------------- */
5456 int type = [theEvent type];
5457 NSWindow *window = [theEvent window];
5459 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "[EmacsApp sendEvent:]");
5460 NSTRACE_MSG ("Type: %d", type);
5462 #ifdef NS_IMPL_GNUSTEP
5463 // Keyboard events aren't propagated to file dialogs for some reason.
5464 if ([NSApp modalWindow] != nil &&
5465 (type == NSEventTypeKeyDown || type == NSEventTypeKeyUp || type == NSEventTypeFlagsChanged))
5467 [[NSApp modalWindow] sendEvent: theEvent];
5472 if (represented_filename != nil && represented_frame)
5474 NSString *fstr = represented_filename;
5475 NSView *view = FRAME_NS_VIEW (represented_frame);
5476 #ifdef NS_IMPL_COCOA
5477 /* work around a bug observed on 10.3 and later where
5478 setTitleWithRepresentedFilename does not clear out previous state
5479 if given filename does not exist */
5480 if (! [[NSFileManager defaultManager] fileExistsAtPath: fstr])
5481 [[view window] setRepresentedFilename: @""];
5483 [[view window] setRepresentedFilename: fstr];
5484 [represented_filename release];
5485 represented_filename = nil;
5486 represented_frame = NULL;
5489 if (type == NSEventTypeApplicationDefined)
5491 switch ([theEvent data2])
5493 #ifdef NS_IMPL_COCOA
5494 case NSAPP_DATA2_RUNASSCRIPT:
5499 case NSAPP_DATA2_RUNFILEDIALOG:
5500 ns_run_file_dialog ();
5506 if (type == NSEventTypeCursorUpdate && window == nil)
5508 fprintf (stderr, "Dropping external cursor update event.\n");
5512 if (type == NSEventTypeApplicationDefined)
5514 /* Events posted by ns_send_appdefined interrupt the run loop here.
5515 But, if a modal window is up, an appdefined can still come through,
5516 (e.g., from a makeKeyWindow event) but stopping self also stops the
5517 modal loop. Just defer it until later. */
5518 if ([NSApp modalWindow] == nil)
5520 last_appdefined_event_data = [theEvent data1];
5525 send_appdefined = YES;
5530 #ifdef NS_IMPL_COCOA
5531 /* If no dialog and none of our frames have focus and it is a move, skip it.
5532 It is a mouse move in an auxiliary menu, i.e. on the top right on macOS,
5533 such as Wifi, sound, date or similar.
5534 This prevents "spooky" highlighting in the frame under the menu. */
5535 if (type == NSEventTypeMouseMoved && [NSApp modalWindow] == nil)
5537 struct ns_display_info *di;
5538 BOOL has_focus = NO;
5539 for (di = x_display_list; ! has_focus && di; di = di->next)
5540 has_focus = di->x_focus_frame != 0;
5546 NSTRACE_UNSILENCE();
5548 [super sendEvent: theEvent];
5552 - (void)showPreferencesWindow: (id)sender
5554 struct frame *emacsframe = SELECTED_FRAME ();
5555 NSEvent *theEvent = [NSApp currentEvent];
5559 emacs_event->kind = NS_NONKEY_EVENT;
5560 emacs_event->code = KEY_NS_SHOW_PREFS;
5561 emacs_event->modifiers = 0;
5562 EV_TRAILER (theEvent);
5566 - (void)newFrame: (id)sender
5568 NSTRACE ("[EmacsApp newFrame:]");
5570 struct frame *emacsframe = SELECTED_FRAME ();
5571 NSEvent *theEvent = [NSApp currentEvent];
5575 emacs_event->kind = NS_NONKEY_EVENT;
5576 emacs_event->code = KEY_NS_NEW_FRAME;
5577 emacs_event->modifiers = 0;
5578 EV_TRAILER (theEvent);
5582 /* Open a file (used by below, after going into queue read by ns_read_socket) */
5583 - (BOOL) openFile: (NSString *)fileName
5585 NSTRACE ("[EmacsApp openFile:]");
5587 struct frame *emacsframe = SELECTED_FRAME ();
5588 NSEvent *theEvent = [NSApp currentEvent];
5593 emacs_event->kind = NS_NONKEY_EVENT;
5594 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
5595 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
5596 ns_input_line = Qnil; /* can be start or cons start,end */
5597 emacs_event->modifiers =0;
5598 EV_TRAILER (theEvent);
5604 /* **************************************************************************
5606 EmacsApp delegate implementation
5608 ************************************************************************** */
5610 - (void)applicationDidFinishLaunching: (NSNotification *)notification
5611 /* --------------------------------------------------------------------------
5612 When application is loaded, terminate event loop in ns_term_init
5613 -------------------------------------------------------------------------- */
5615 NSTRACE ("[EmacsApp applicationDidFinishLaunching:]");
5617 #ifdef NS_IMPL_GNUSTEP
5618 ((EmacsApp *)self)->applicationDidFinishLaunchingCalled = YES;
5620 [NSApp setServicesProvider: NSApp];
5622 [self antialiasThresholdDidChange:nil];
5623 #ifdef NS_IMPL_COCOA
5624 [[NSNotificationCenter defaultCenter]
5626 selector:@selector(antialiasThresholdDidChange:)
5627 name:NSAntialiasThresholdChangedNotification
5631 #ifdef NS_IMPL_COCOA
5632 if ([NSApp activationPolicy] == NSApplicationActivationPolicyProhibited) {
5633 /* Set the app's activation policy to regular when we run outside
5634 of a bundle. This is already done for us by Info.plist when we
5635 run inside a bundle. */
5636 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
5637 [NSApp setApplicationIconImage:
5640 build_string("icons/hicolor/128x128/apps/emacs.png")]];
5644 ns_send_appdefined (-2);
5647 - (void)antialiasThresholdDidChange:(NSNotification *)notification
5649 #ifdef NS_IMPL_COCOA
5650 macfont_update_antialias_threshold ();
5655 /* Termination sequences:
5658 MenuBar | File | Exit:
5659 Select Quit from App menubar:
5661 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
5664 Select Quit from Dock menu:
5667 Cancel -> Nothing else
5671 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
5676 - (void) terminate: (id)sender
5678 NSTRACE ("[EmacsApp terminate:]");
5680 struct frame *emacsframe = SELECTED_FRAME ();
5685 emacs_event->kind = NS_NONKEY_EVENT;
5686 emacs_event->code = KEY_NS_POWER_OFF;
5687 emacs_event->arg = Qt; /* mark as non-key event */
5688 EV_TRAILER ((id)nil);
5692 runAlertPanel(NSString *title,
5693 NSString *msgFormat,
5694 NSString *defaultButton,
5695 NSString *alternateButton)
5697 #ifdef NS_IMPL_GNUSTEP
5698 return NSRunAlertPanel(title, msgFormat, defaultButton, alternateButton, nil)
5699 == NSAlertDefaultReturn;
5701 NSAlert *alert = [[NSAlert alloc] init];
5702 [alert setAlertStyle: NSAlertStyleCritical];
5703 [alert setMessageText: msgFormat];
5704 [alert addButtonWithTitle: defaultButton];
5705 [alert addButtonWithTitle: alternateButton];
5706 NSInteger ret = [alert runModal];
5708 return ret == NSAlertFirstButtonReturn;
5713 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
5715 NSTRACE ("[EmacsApp applicationShouldTerminate:]");
5719 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
5720 return NSTerminateNow;
5722 ret = runAlertPanel(ns_app_name,
5723 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
5724 @"Save Buffers and Exit", @"Cancel");
5726 return ret ? NSTerminateNow : NSTerminateCancel;
5730 not_in_argv (NSString *arg)
5733 const char *a = [arg UTF8String];
5734 for (k = 1; k < initial_argc; ++k)
5735 if (strcmp (a, initial_argv[k]) == 0) return 0;
5739 /* Notification from the Workspace to open a file */
5740 - (BOOL)application: sender openFile: (NSString *)file
5742 if (ns_do_open_file || not_in_argv (file))
5743 [ns_pending_files addObject: file];
5748 /* Open a file as a temporary file */
5749 - (BOOL)application: sender openTempFile: (NSString *)file
5751 if (ns_do_open_file || not_in_argv (file))
5752 [ns_pending_files addObject: file];
5757 /* Notification from the Workspace to open a file noninteractively (?) */
5758 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
5760 if (ns_do_open_file || not_in_argv (file))
5761 [ns_pending_files addObject: file];
5765 /* Notification from the Workspace to open multiple files */
5766 - (void)application: sender openFiles: (NSArray *)fileList
5768 NSEnumerator *files = [fileList objectEnumerator];
5770 /* Don't open files from the command line unconditionally,
5771 Cocoa parses the command line wrong, --option value tries to open value
5772 if --option is the last option. */
5773 while ((file = [files nextObject]) != nil)
5774 if (ns_do_open_file || not_in_argv (file))
5775 [ns_pending_files addObject: file];
5777 [self replyToOpenOrPrint: NSApplicationDelegateReplySuccess];
5782 /* Handle dock menu requests. */
5783 - (NSMenu *)applicationDockMenu: (NSApplication *) sender
5789 /* TODO: these may help w/IO switching btwn terminal and NSApp */
5790 - (void)applicationWillBecomeActive: (NSNotification *)notification
5792 NSTRACE ("[EmacsApp applicationWillBecomeActive:]");
5793 //ns_app_active=YES;
5796 - (void)applicationDidBecomeActive: (NSNotification *)notification
5798 NSTRACE ("[EmacsApp applicationDidBecomeActive:]");
5800 #ifdef NS_IMPL_GNUSTEP
5801 if (! applicationDidFinishLaunchingCalled)
5802 [self applicationDidFinishLaunching:notification];
5804 //ns_app_active=YES;
5806 ns_update_auto_hide_menu_bar ();
5807 // No constraining takes place when the application is not active.
5808 ns_constrain_all_frames ();
5810 - (void)applicationDidResignActive: (NSNotification *)notification
5812 NSTRACE ("[EmacsApp applicationDidResignActive:]");
5815 ns_send_appdefined (-1);
5820 /* ==========================================================================
5822 EmacsApp aux handlers for managing event loop
5824 ========================================================================== */
5827 - (void)timeout_handler: (NSTimer *)timedEntry
5828 /* --------------------------------------------------------------------------
5829 The timeout specified to ns_select has passed.
5830 -------------------------------------------------------------------------- */
5832 /*NSTRACE ("timeout_handler"); */
5833 ns_send_appdefined (-2);
5836 - (void)sendFromMainThread:(id)unused
5838 ns_send_appdefined (nextappdefined);
5841 - (void)fd_handler:(id)unused
5842 /* --------------------------------------------------------------------------
5843 Check data waiting on file descriptors and terminate if so
5844 -------------------------------------------------------------------------- */
5847 int waiting = 1, nfds;
5850 fd_set readfds, writefds, *wfds;
5851 struct timespec timeout, *tmo;
5852 NSAutoreleasePool *pool = nil;
5854 /* NSTRACE ("fd_handler"); */
5859 pool = [[NSAutoreleasePool alloc] init];
5865 FD_SET (selfds[0], &fds);
5866 result = select (selfds[0]+1, &fds, NULL, NULL, NULL);
5867 if (result > 0 && read (selfds[0], &c, 1) == 1 && c == 'g')
5872 pthread_mutex_lock (&select_mutex);
5875 if (select_valid & SELECT_HAVE_READ)
5876 readfds = select_readfds;
5880 if (select_valid & SELECT_HAVE_WRITE)
5882 writefds = select_writefds;
5887 if (select_valid & SELECT_HAVE_TMO)
5889 timeout = select_timeout;
5895 pthread_mutex_unlock (&select_mutex);
5897 FD_SET (selfds[0], &readfds);
5898 if (selfds[0] >= nfds) nfds = selfds[0]+1;
5900 result = pselect (nfds, &readfds, wfds, NULL, tmo, NULL);
5903 ns_send_appdefined (-2);
5904 else if (result > 0)
5906 if (FD_ISSET (selfds[0], &readfds))
5908 if (read (selfds[0], &c, 1) == 1 && c == 's')
5913 pthread_mutex_lock (&select_mutex);
5914 if (select_valid & SELECT_HAVE_READ)
5915 select_readfds = readfds;
5916 if (select_valid & SELECT_HAVE_WRITE)
5917 select_writefds = writefds;
5918 if (select_valid & SELECT_HAVE_TMO)
5919 select_timeout = timeout;
5920 pthread_mutex_unlock (&select_mutex);
5922 ns_send_appdefined (result);
5932 /* ==========================================================================
5936 ========================================================================== */
5938 /* called from system: queue for next pass through event loop */
5939 - (void)requestService: (NSPasteboard *)pboard
5940 userData: (NSString *)userData
5941 error: (NSString **)error
5943 [ns_pending_service_names addObject: userData];
5944 [ns_pending_service_args addObject: [NSString stringWithUTF8String:
5945 SSDATA (ns_string_from_pasteboard (pboard))]];
5949 /* called from ns_read_socket to clear queue */
5950 - (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg
5952 struct frame *emacsframe = SELECTED_FRAME ();
5953 NSEvent *theEvent = [NSApp currentEvent];
5955 NSTRACE ("[EmacsApp fulfillService:withArg:]");
5960 emacs_event->kind = NS_NONKEY_EVENT;
5961 emacs_event->code = KEY_NS_SPI_SERVICE_CALL;
5962 ns_input_spi_name = build_string ([name UTF8String]);
5963 ns_input_spi_arg = build_string ([arg UTF8String]);
5964 emacs_event->modifiers = EV_MODIFIERS (theEvent);
5965 EV_TRAILER (theEvent);
5975 /* ==========================================================================
5977 EmacsView implementation
5979 ========================================================================== */
5982 @implementation EmacsView
5984 /* needed to inform when window closed from LISP */
5985 - (void) setWindowClosing: (BOOL)closing
5987 NSTRACE ("[EmacsView setWindowClosing:%d]", closing);
5989 windowClosing = closing;
5995 NSTRACE ("[EmacsView dealloc]");
5997 if (fs_state == FULLSCREEN_BOTH)
5998 [nonfs_window release];
6003 /* called on font panel selection */
6004 - (void)changeFont: (id)sender
6006 NSEvent *e = [[self window] currentEvent];
6007 struct face *face = FACE_FROM_ID (emacsframe, DEFAULT_FACE_ID);
6008 struct font *font = face->font;
6013 NSTRACE ("[EmacsView changeFont:]");
6018 #ifdef NS_IMPL_GNUSTEP
6019 nsfont = ((struct nsfont_info *)font)->nsfont;
6021 #ifdef NS_IMPL_COCOA
6022 nsfont = (NSFont *) macfont_get_nsctfont (font);
6025 if ((newFont = [sender convertFont: nsfont]))
6027 SET_FRAME_GARBAGED (emacsframe); /* now needed as of 2008/10 */
6029 emacs_event->kind = NS_NONKEY_EVENT;
6030 emacs_event->modifiers = 0;
6031 emacs_event->code = KEY_NS_CHANGE_FONT;
6033 size = [newFont pointSize];
6034 ns_input_fontsize = make_number (lrint (size));
6035 ns_input_font = build_string ([[newFont familyName] UTF8String]);
6041 - (BOOL)acceptsFirstResponder
6043 NSTRACE ("[EmacsView acceptsFirstResponder]");
6048 - (void)resetCursorRects
6050 NSRect visible = [self visibleRect];
6051 NSCursor *currentCursor = FRAME_POINTER_TYPE (emacsframe);
6052 NSTRACE ("[EmacsView resetCursorRects]");
6054 if (currentCursor == nil)
6055 currentCursor = [NSCursor arrowCursor];
6057 if (!NSIsEmptyRect (visible))
6058 [self addCursorRect: visible cursor: currentCursor];
6059 [currentCursor setOnMouseEntered: YES];
6064 /*****************************************************************************/
6065 /* Keyboard handling. */
6068 - (void)keyDown: (NSEvent *)theEvent
6070 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
6072 unsigned fnKeysym = 0;
6073 static NSMutableArray *nsEvArray;
6075 unsigned int flags = [theEvent modifierFlags];
6077 NSTRACE ("[EmacsView keyDown:]");
6079 /* Rhapsody and macOS give up and down events for the arrow keys */
6080 if (ns_fake_keydown == YES)
6081 ns_fake_keydown = NO;
6082 else if ([theEvent type] != NSEventTypeKeyDown)
6088 if (![[self window] isKeyWindow]
6089 && [[theEvent window] isKindOfClass: [EmacsWindow class]]
6090 /* we must avoid an infinite loop here. */
6091 && (EmacsView *)[[theEvent window] delegate] != self)
6093 /* XXX: There is an occasional condition in which, when Emacs display
6094 updates a different frame from the current one, and temporarily
6095 selects it, then processes some interrupt-driven input
6096 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
6097 for some reason that window has its first responder set to the NSView
6098 most recently updated (I guess), which is not the correct one. */
6099 [(EmacsView *)[[theEvent window] delegate] keyDown: theEvent];
6103 if (nsEvArray == nil)
6104 nsEvArray = [[NSMutableArray alloc] initWithCapacity: 1];
6106 [NSCursor setHiddenUntilMouseMoves: YES];
6108 if (hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6110 clear_mouse_face (hlinfo);
6111 hlinfo->mouse_face_hidden = 1;
6114 if (!processingCompose)
6116 /* When using screen sharing, no left or right information is sent,
6117 so use Left key in those cases. */
6118 int is_left_key, is_right_key;
6120 code = ([[theEvent charactersIgnoringModifiers] length] == 0) ?
6121 0 : [[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
6123 /* (Carbon way: [theEvent keyCode]) */
6125 /* is it a "function key"? */
6126 /* Note: Sometimes a plain key will have the NSEventModifierFlagNumericPad
6127 flag set (this is probably a bug in the OS).
6129 if (code < 0x00ff && (flags&NSEventModifierFlagNumericPad))
6131 fnKeysym = ns_convert_key ([theEvent keyCode] | NSEventModifierFlagNumericPad);
6135 fnKeysym = ns_convert_key (code);
6140 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
6141 because Emacs treats Delete and KP-Delete same (in simple.el). */
6142 if ((fnKeysym == 0xFFFF && [theEvent keyCode] == 0x33)
6143 #ifdef NS_IMPL_GNUSTEP
6144 /* GNUstep uses incompatible keycodes, even for those that are
6145 supposed to be hardware independent. Just check for delete.
6146 Keypad delete does not have keysym 0xFFFF.
6147 See https://savannah.gnu.org/bugs/?25395
6149 || (fnKeysym == 0xFFFF && code == 127)
6152 code = 0xFF08; /* backspace */
6157 /* are there modifiers? */
6158 emacs_event->modifiers = 0;
6160 if (flags & NSEventModifierFlagHelp)
6161 emacs_event->modifiers |= hyper_modifier;
6163 if (flags & NSEventModifierFlagShift)
6164 emacs_event->modifiers |= shift_modifier;
6166 is_right_key = (flags & NSRightCommandKeyMask) == NSRightCommandKeyMask;
6167 is_left_key = (flags & NSLeftCommandKeyMask) == NSLeftCommandKeyMask
6168 || (! is_right_key && (flags & NSEventModifierFlagCommand) == NSEventModifierFlagCommand);
6171 emacs_event->modifiers |= parse_solitary_modifier
6172 (EQ (ns_right_command_modifier, Qleft)
6173 ? ns_command_modifier
6174 : ns_right_command_modifier);
6178 emacs_event->modifiers |= parse_solitary_modifier
6179 (ns_command_modifier);
6181 /* if super (default), take input manager's word so things like
6182 dvorak / qwerty layout work */
6183 if (EQ (ns_command_modifier, Qsuper)
6185 && [[theEvent characters] length] != 0)
6187 /* XXX: the code we get will be unshifted, so if we have
6188 a shift modifier, must convert ourselves */
6189 if (!(flags & NSEventModifierFlagShift))
6190 code = [[theEvent characters] characterAtIndex: 0];
6192 /* this is ugly and also requires linking w/Carbon framework
6193 (for LMGetKbdType) so for now leave this rare (?) case
6194 undealt with.. in future look into CGEvent methods */
6197 long smv = GetScriptManagerVariable (smKeyScript);
6198 Handle uchrHandle = GetResource
6199 ('uchr', GetScriptVariable (smv, smScriptKeys));
6201 UCKeyTranslate ((UCKeyboardLayout *) *uchrHandle,
6202 [[theEvent characters] characterAtIndex: 0],
6203 kUCKeyActionDisplay,
6204 (flags & ~NSEventModifierFlagCommand) >> 8,
6205 LMGetKbdType (), kUCKeyTranslateNoDeadKeysMask,
6206 &dummy, 1, &dummy, &code);
6213 is_right_key = (flags & NSRightControlKeyMask) == NSRightControlKeyMask;
6214 is_left_key = (flags & NSLeftControlKeyMask) == NSLeftControlKeyMask
6215 || (! is_right_key && (flags & NSEventModifierFlagControl) == NSEventModifierFlagControl);
6218 emacs_event->modifiers |= parse_solitary_modifier
6219 (EQ (ns_right_control_modifier, Qleft)
6220 ? ns_control_modifier
6221 : ns_right_control_modifier);
6224 emacs_event->modifiers |= parse_solitary_modifier
6225 (ns_control_modifier);
6227 if (flags & NS_FUNCTION_KEY_MASK && !fnKeysym)
6228 emacs_event->modifiers |=
6229 parse_solitary_modifier (ns_function_modifier);
6231 left_is_none = NILP (ns_alternate_modifier)
6232 || EQ (ns_alternate_modifier, Qnone);
6234 is_right_key = (flags & NSRightAlternateKeyMask)
6235 == NSRightAlternateKeyMask;
6236 is_left_key = (flags & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask
6238 && (flags & NSEventModifierFlagOption) == NSEventModifierFlagOption);
6242 if ((NILP (ns_right_alternate_modifier)
6243 || EQ (ns_right_alternate_modifier, Qnone)
6244 || (EQ (ns_right_alternate_modifier, Qleft) && left_is_none))
6246 { /* accept pre-interp alt comb */
6247 if ([[theEvent characters] length] > 0)
6248 code = [[theEvent characters] characterAtIndex: 0];
6249 /*HACK: clear lone shift modifier to stop next if from firing */
6250 if (emacs_event->modifiers == shift_modifier)
6251 emacs_event->modifiers = 0;
6254 emacs_event->modifiers |= parse_solitary_modifier
6255 (EQ (ns_right_alternate_modifier, Qleft)
6256 ? ns_alternate_modifier
6257 : ns_right_alternate_modifier);
6260 if (is_left_key) /* default = meta */
6262 if (left_is_none && !fnKeysym)
6263 { /* accept pre-interp alt comb */
6264 if ([[theEvent characters] length] > 0)
6265 code = [[theEvent characters] characterAtIndex: 0];
6266 /*HACK: clear lone shift modifier to stop next if from firing */
6267 if (emacs_event->modifiers == shift_modifier)
6268 emacs_event->modifiers = 0;
6271 emacs_event->modifiers |=
6272 parse_solitary_modifier (ns_alternate_modifier);
6276 fprintf (stderr, "keyDown: code =%x\tfnKey =%x\tflags = %x\tmods = %x\n",
6277 (unsigned) code, fnKeysym, flags, emacs_event->modifiers);
6279 /* if it was a function key or had modifiers, pass it directly to emacs */
6280 if (fnKeysym || (emacs_event->modifiers
6281 && (emacs_event->modifiers != shift_modifier)
6282 && [[theEvent charactersIgnoringModifiers] length] > 0))
6283 /*[[theEvent characters] length] */
6285 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
6287 code |= (1<<28)|(3<<16);
6288 else if (code == 0x7f)
6289 code |= (1<<28)|(3<<16);
6291 emacs_event->kind = code > 0xFF
6292 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
6294 emacs_event->code = code;
6295 EV_TRAILER (theEvent);
6296 processingCompose = NO;
6302 if (NS_KEYLOG && !processingCompose)
6303 fprintf (stderr, "keyDown: Begin compose sequence.\n");
6305 processingCompose = YES;
6306 [nsEvArray addObject: theEvent];
6307 [self interpretKeyEvents: nsEvArray];
6308 [nsEvArray removeObject: theEvent];
6312 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
6315 /* <NSTextInput>: called when done composing;
6316 NOTE: also called when we delete over working text, followed immed.
6317 by doCommandBySelector: deleteBackward: */
6318 - (void)insertText: (id)aString
6323 NSTRACE ("[EmacsView insertText:]");
6325 if ([aString isKindOfClass:[NSAttributedString class]])
6326 s = [aString string];
6333 NSLog (@"insertText '%@'\tlen = %lu", aString, (unsigned long) len);
6334 processingCompose = NO;
6339 /* first, clear any working text */
6340 if (workingText != nil)
6341 [self deleteWorkingText];
6343 /* It might be preferable to use getCharacters:range: below,
6344 cf. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaPerformance/Articles/StringDrawing.html#//apple_ref/doc/uid/TP40001445-112378.
6345 However, we probably can't use SAFE_NALLOCA here because it might
6348 /* now insert the string as keystrokes */
6349 for (NSUInteger i = 0; i < len; i++)
6351 NSUInteger code = [s characterAtIndex:i];
6352 if (UTF_16_HIGH_SURROGATE_P (code) && i < len - 1)
6354 unichar low = [s characterAtIndex:i + 1];
6355 if (UTF_16_LOW_SURROGATE_P (low))
6357 code = surrogates_to_codepoint (low, code);
6361 /* TODO: still need this? */
6363 code = '~'; /* 0x7E */
6364 if (code != 32) /* Space */
6365 emacs_event->modifiers = 0;
6367 = code > 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT : ASCII_KEYSTROKE_EVENT;
6368 emacs_event->code = code;
6369 EV_TRAILER ((id)nil);
6374 /* <NSTextInput>: inserts display of composing characters */
6375 - (void)setMarkedText: (id)aString selectedRange: (NSRange)selRange
6377 NSString *str = [aString respondsToSelector: @selector (string)] ?
6378 [aString string] : aString;
6380 NSTRACE ("[EmacsView setMarkedText:selectedRange:]");
6383 NSLog (@"setMarkedText '%@' len =%lu range %lu from %lu",
6384 str, (unsigned long)[str length],
6385 (unsigned long)selRange.length,
6386 (unsigned long)selRange.location);
6388 if (workingText != nil)
6389 [self deleteWorkingText];
6390 if ([str length] == 0)
6396 processingCompose = YES;
6397 workingText = [str copy];
6398 ns_working_text = build_string ([workingText UTF8String]);
6400 emacs_event->kind = NS_TEXT_EVENT;
6401 emacs_event->code = KEY_NS_PUT_WORKING_TEXT;
6402 EV_TRAILER ((id)nil);
6406 /* delete display of composing characters [not in <NSTextInput>] */
6407 - (void)deleteWorkingText
6409 NSTRACE ("[EmacsView deleteWorkingText]");
6411 if (workingText == nil)
6414 NSLog(@"deleteWorkingText len =%lu\n", (unsigned long)[workingText length]);
6415 [workingText release];
6417 processingCompose = NO;
6422 emacs_event->kind = NS_TEXT_EVENT;
6423 emacs_event->code = KEY_NS_UNPUT_WORKING_TEXT;
6424 EV_TRAILER ((id)nil);
6428 - (BOOL)hasMarkedText
6430 NSTRACE ("[EmacsView hasMarkedText]");
6432 return workingText != nil;
6436 - (NSRange)markedRange
6438 NSTRACE ("[EmacsView markedRange]");
6440 NSRange rng = workingText != nil
6441 ? NSMakeRange (0, [workingText length]) : NSMakeRange (NSNotFound, 0);
6443 NSLog (@"markedRange request");
6450 NSTRACE ("[EmacsView unmarkText]");
6453 NSLog (@"unmark (accept) text");
6454 [self deleteWorkingText];
6455 processingCompose = NO;
6459 /* used to position char selection windows, etc. */
6460 - (NSRect)firstRectForCharacterRange: (NSRange)theRange
6464 struct window *win = XWINDOW (FRAME_SELECTED_WINDOW (emacsframe));
6466 NSTRACE ("[EmacsView firstRectForCharacterRange:]");
6469 NSLog (@"firstRectForCharRange request");
6471 rect.size.width = theRange.length * FRAME_COLUMN_WIDTH (emacsframe);
6472 rect.size.height = FRAME_LINE_HEIGHT (emacsframe);
6473 pt.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (win, win->phys_cursor.x);
6474 pt.y = WINDOW_TO_FRAME_PIXEL_Y (win, win->phys_cursor.y
6475 +FRAME_LINE_HEIGHT (emacsframe));
6477 pt = [self convertPoint: pt toView: nil];
6479 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
6480 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
6481 if ([[self window] respondsToSelector: @selector(convertRectToScreen:)])
6485 rect = [(EmacsWindow *) [self window] convertRectToScreen: rect];
6486 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
6490 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 */
6491 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070 \
6492 || defined (NS_IMPL_GNUSTEP)
6494 pt = [[self window] convertBaseToScreen: pt];
6503 - (NSInteger)conversationIdentifier
6505 return (NSInteger)self;
6509 - (void)doCommandBySelector: (SEL)aSelector
6511 NSTRACE ("[EmacsView doCommandBySelector:]");
6514 NSLog (@"doCommandBySelector: %@", NSStringFromSelector (aSelector));
6516 processingCompose = NO;
6517 if (aSelector == @selector (deleteBackward:))
6519 /* happens when user backspaces over an ongoing composition:
6520 throw a 'delete' into the event queue */
6523 emacs_event->kind = NON_ASCII_KEYSTROKE_EVENT;
6524 emacs_event->code = 0xFF08;
6525 EV_TRAILER ((id)nil);
6529 - (NSArray *)validAttributesForMarkedText
6531 static NSArray *arr = nil;
6532 if (arr == nil) arr = [NSArray new];
6533 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
6537 - (NSRange)selectedRange
6540 NSLog (@"selectedRange request");
6541 return NSMakeRange (NSNotFound, 0);
6544 #if defined (NS_IMPL_COCOA) || GNUSTEP_GUI_MAJOR_VERSION > 0 || \
6545 GNUSTEP_GUI_MINOR_VERSION > 22
6546 - (NSUInteger)characterIndexForPoint: (NSPoint)thePoint
6548 - (unsigned int)characterIndexForPoint: (NSPoint)thePoint
6552 NSLog (@"characterIndexForPoint request");
6556 - (NSAttributedString *)attributedSubstringFromRange: (NSRange)theRange
6558 static NSAttributedString *str = nil;
6559 if (str == nil) str = [NSAttributedString new];
6561 NSLog (@"attributedSubstringFromRange request");
6565 /* End <NSTextInput> impl. */
6566 /*****************************************************************************/
6569 /* This is what happens when the user presses a mouse button. */
6570 - (void)mouseDown: (NSEvent *)theEvent
6572 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (emacsframe);
6573 NSPoint p = [self convertPoint: [theEvent locationInWindow] fromView: nil];
6575 NSTRACE ("[EmacsView mouseDown:]");
6577 [self deleteWorkingText];
6582 dpyinfo->last_mouse_frame = emacsframe;
6583 /* appears to be needed to prevent spurious movement events generated on
6585 emacsframe->mouse_moved = 0;
6587 if ([theEvent type] == NSEventTypeScrollWheel)
6589 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
6590 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
6591 if ([theEvent respondsToSelector:@selector(hasPreciseScrollingDeltas)])
6594 /* If the input device is a touchpad or similar, use precise
6595 * scrolling deltas. These are measured in pixels, so we
6596 * have to add them up until they exceed one line height,
6597 * then we can send a scroll wheel event.
6599 * If the device only has coarse scrolling deltas, like a
6600 * real mousewheel, the deltas represent a ratio of whole
6601 * lines, so round up the number of lines. This means we
6602 * always send one scroll event per click, but can still
6603 * scroll more than one line if the OS tells us to.
6609 /* FIXME: At the top or bottom of the buffer we should
6610 * ignore momentum-phase events. */
6611 if (! ns_use_mwheel_momentum
6612 && [theEvent momentumPhase] != NSEventPhaseNone)
6615 if ([theEvent hasPreciseScrollingDeltas])
6617 static int totalDeltaX, totalDeltaY;
6620 if (NUMBERP (ns_mwheel_line_height))
6621 lineHeight = XINT (ns_mwheel_line_height);
6624 /* FIXME: Use actual line height instead of the default. */
6625 lineHeight = default_line_pixel_height
6626 (XWINDOW (FRAME_SELECTED_WINDOW (emacsframe)));
6629 if ([theEvent phase] == NSEventPhaseBegan)
6635 totalDeltaX += [theEvent scrollingDeltaX];
6636 totalDeltaY += [theEvent scrollingDeltaY];
6638 /* Calculate the number of lines, if any, to scroll, and
6639 * reset the total delta for the direction we're NOT
6640 * scrolling so that small movements don't add up. */
6641 if (abs (totalDeltaX) > abs (totalDeltaY)
6642 && abs (totalDeltaX) > lineHeight)
6645 scrollUp = totalDeltaX > 0;
6647 lines = abs (totalDeltaX / lineHeight);
6648 totalDeltaX = totalDeltaX % lineHeight;
6651 else if (abs (totalDeltaY) >= abs (totalDeltaX)
6652 && abs (totalDeltaY) > lineHeight)
6655 scrollUp = totalDeltaY > 0;
6657 lines = abs (totalDeltaY / lineHeight);
6658 totalDeltaY = totalDeltaY % lineHeight;
6662 if (lines > 1 && ! ns_use_mwheel_acceleration)
6669 if ([theEvent scrollingDeltaY] == 0)
6672 delta = [theEvent scrollingDeltaX];
6677 delta = [theEvent scrollingDeltaY];
6680 lines = (ns_use_mwheel_acceleration)
6681 ? ceil (fabs (delta)) : 1;
6683 scrollUp = delta > 0;
6689 emacs_event->kind = horizontal ? HORIZ_WHEEL_EVENT : WHEEL_EVENT;
6690 emacs_event->arg = (make_number (lines));
6692 emacs_event->code = 0;
6693 emacs_event->modifiers = EV_MODIFIERS (theEvent) |
6694 (scrollUp ? up_modifier : down_modifier);
6695 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
6699 #endif /* defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 */
6700 #if defined (NS_IMPL_GNUSTEP) || MAC_OS_X_VERSION_MIN_REQUIRED < 1070
6702 CGFloat delta = [theEvent deltaY];
6703 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
6706 delta = [theEvent deltaX];
6709 NSTRACE_MSG ("deltaIsZero");
6712 emacs_event->kind = HORIZ_WHEEL_EVENT;
6715 emacs_event->kind = WHEEL_EVENT;
6717 emacs_event->code = 0;
6718 emacs_event->modifiers = EV_MODIFIERS (theEvent) |
6719 ((delta > 0) ? up_modifier : down_modifier);
6725 emacs_event->kind = MOUSE_CLICK_EVENT;
6726 emacs_event->code = EV_BUTTON (theEvent);
6727 emacs_event->modifiers = EV_MODIFIERS (theEvent)
6728 | EV_UDMODIFIERS (theEvent);
6731 XSETINT (emacs_event->x, lrint (p.x));
6732 XSETINT (emacs_event->y, lrint (p.y));
6733 EV_TRAILER (theEvent);
6738 - (void)rightMouseDown: (NSEvent *)theEvent
6740 NSTRACE ("[EmacsView rightMouseDown:]");
6741 [self mouseDown: theEvent];
6745 - (void)otherMouseDown: (NSEvent *)theEvent
6747 NSTRACE ("[EmacsView otherMouseDown:]");
6748 [self mouseDown: theEvent];
6752 - (void)mouseUp: (NSEvent *)theEvent
6754 NSTRACE ("[EmacsView mouseUp:]");
6755 [self mouseDown: theEvent];
6759 - (void)rightMouseUp: (NSEvent *)theEvent
6761 NSTRACE ("[EmacsView rightMouseUp:]");
6762 [self mouseDown: theEvent];
6766 - (void)otherMouseUp: (NSEvent *)theEvent
6768 NSTRACE ("[EmacsView otherMouseUp:]");
6769 [self mouseDown: theEvent];
6773 - (void) scrollWheel: (NSEvent *)theEvent
6775 NSTRACE ("[EmacsView scrollWheel:]");
6776 [self mouseDown: theEvent];
6780 /* Tell emacs the mouse has moved. */
6781 - (void)mouseMoved: (NSEvent *)e
6783 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (emacsframe);
6784 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (emacsframe);
6788 NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "[EmacsView mouseMoved:]");
6790 dpyinfo->last_mouse_movement_time = EV_TIMESTAMP (e);
6791 pt = [self convertPoint: [e locationInWindow] fromView: nil];
6792 dpyinfo->last_mouse_motion_x = pt.x;
6793 dpyinfo->last_mouse_motion_y = pt.y;
6795 /* update any mouse face */
6796 if (hlinfo->mouse_face_hidden)
6798 hlinfo->mouse_face_hidden = 0;
6799 clear_mouse_face (hlinfo);
6802 /* tooltip handling */
6803 previous_help_echo_string = help_echo_string;
6804 help_echo_string = Qnil;
6806 if (!NILP (Vmouse_autoselect_window))
6808 NSTRACE_MSG ("mouse_autoselect_window");
6809 static Lisp_Object last_mouse_window;
6811 = window_from_coordinates (emacsframe, pt.x, pt.y, 0, 0);
6813 if (WINDOWP (window)
6814 && !EQ (window, last_mouse_window)
6815 && !EQ (window, selected_window)
6816 && (!NILP (focus_follows_mouse)
6817 || (EQ (XWINDOW (window)->frame,
6818 XWINDOW (selected_window)->frame))))
6820 NSTRACE_MSG ("in_window");
6821 emacs_event->kind = SELECT_WINDOW_EVENT;
6822 emacs_event->frame_or_window = window;
6825 /* Remember the last window where we saw the mouse. */
6826 last_mouse_window = window;
6829 if (!note_mouse_movement (emacsframe, pt.x, pt.y))
6830 help_echo_string = previous_help_echo_string;
6832 XSETFRAME (frame, emacsframe);
6833 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
6835 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
6836 (note_mouse_highlight), which is called through the
6837 note_mouse_movement () call above */
6838 any_help_event_p = YES;
6839 gen_help_event (help_echo_string, frame, help_echo_window,
6840 help_echo_object, help_echo_pos);
6843 if (emacsframe->mouse_moved && send_appdefined)
6844 ns_send_appdefined (-1);
6848 - (void)mouseDragged: (NSEvent *)e
6850 NSTRACE ("[EmacsView mouseDragged:]");
6851 [self mouseMoved: e];
6855 - (void)rightMouseDragged: (NSEvent *)e
6857 NSTRACE ("[EmacsView rightMouseDragged:]");
6858 [self mouseMoved: e];
6862 - (void)otherMouseDragged: (NSEvent *)e
6864 NSTRACE ("[EmacsView otherMouseDragged:]");
6865 [self mouseMoved: e];
6869 - (BOOL)windowShouldClose: (id)sender
6871 NSEvent *e =[[self window] currentEvent];
6873 NSTRACE ("[EmacsView windowShouldClose:]");
6874 windowClosing = YES;
6877 emacs_event->kind = DELETE_WINDOW_EVENT;
6878 emacs_event->modifiers = 0;
6879 emacs_event->code = 0;
6881 /* Don't close this window, let this be done from lisp code. */
6885 - (void) updateFrameSize: (BOOL) delay
6887 NSWindow *window = [self window];
6888 NSRect wr = [window frame];
6890 int oldc = cols, oldr = rows;
6891 int oldw = FRAME_PIXEL_WIDTH (emacsframe);
6892 int oldh = FRAME_PIXEL_HEIGHT (emacsframe);
6895 NSTRACE ("[EmacsView updateFrameSize:]");
6896 NSTRACE_SIZE ("Original size", NSMakeSize (oldw, oldh));
6897 NSTRACE_RECT ("Original frame", wr);
6898 NSTRACE_MSG ("Original columns: %d", cols);
6899 NSTRACE_MSG ("Original rows: %d", rows);
6901 if (! [self isFullscreen])
6904 #ifdef NS_IMPL_GNUSTEP
6905 // GNUstep does not always update the tool bar height. Force it.
6906 if (toolbar && [toolbar isVisible])
6907 update_frame_tool_bar (emacsframe);
6910 toolbar_height = FRAME_TOOLBAR_HEIGHT (emacsframe);
6911 if (toolbar_height < 0)
6912 toolbar_height = 35;
6914 extra = FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
6918 if (wait_for_tool_bar)
6920 /* The toolbar height is always 0 in fullscreen and undecorated
6921 frames, so don't wait for it to become available. */
6922 if (FRAME_TOOLBAR_HEIGHT (emacsframe) == 0
6923 && FRAME_UNDECORATED (emacsframe) == false
6924 && ! [self isFullscreen])
6926 NSTRACE_MSG ("Waiting for toolbar");
6929 wait_for_tool_bar = NO;
6932 neww = (int)wr.size.width - emacsframe->border_width;
6933 newh = (int)wr.size.height - extra;
6935 NSTRACE_SIZE ("New size", NSMakeSize (neww, newh));
6936 NSTRACE_MSG ("FRAME_TOOLBAR_HEIGHT: %d", FRAME_TOOLBAR_HEIGHT (emacsframe));
6937 NSTRACE_MSG ("FRAME_NS_TITLEBAR_HEIGHT: %d", FRAME_NS_TITLEBAR_HEIGHT (emacsframe));
6939 cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (emacsframe, neww);
6940 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (emacsframe, newh);
6942 if (cols < MINWIDTH)
6945 if (rows < MINHEIGHT)
6948 NSTRACE_MSG ("New columns: %d", cols);
6949 NSTRACE_MSG ("New rows: %d", rows);
6951 if (oldr != rows || oldc != cols || neww != oldw || newh != oldh)
6953 NSView *view = FRAME_NS_VIEW (emacsframe);
6955 change_frame_size (emacsframe,
6956 FRAME_PIXEL_TO_TEXT_WIDTH (emacsframe, neww),
6957 FRAME_PIXEL_TO_TEXT_HEIGHT (emacsframe, newh),
6959 SET_FRAME_GARBAGED (emacsframe);
6960 cancel_mouse_face (emacsframe);
6962 /* The next two lines set the frame to the same size as we've
6963 already set above. We need to do this when we switch back
6964 from non-native fullscreen, in other circumstances it appears
6965 to be a noop. (bug#28872) */
6966 wr = NSMakeRect (0, 0, neww, newh);
6967 [view setFrame: wr];
6969 // to do: consider using [NSNotificationCenter postNotificationName:].
6970 [self windowDidMove: // Update top/left.
6971 [NSNotification notificationWithName:NSWindowDidMoveNotification
6972 object:[view window]]];
6976 NSTRACE_MSG ("No change");
6980 - (NSSize)windowWillResize: (NSWindow *)sender toSize: (NSSize)frameSize
6981 /* normalize frame to gridded text size */
6985 NSTRACE ("[EmacsView windowWillResize:toSize: " NSTRACE_FMT_SIZE "]",
6986 NSTRACE_ARG_SIZE (frameSize));
6987 NSTRACE_RECT ("[sender frame]", [sender frame]);
6988 NSTRACE_FSTYPE ("fs_state", fs_state);
6990 if (!FRAME_LIVE_P (emacsframe))
6993 if (fs_state == FULLSCREEN_MAXIMIZED
6994 && (maximized_width != (int)frameSize.width
6995 || maximized_height != (int)frameSize.height))
6996 [self setFSValue: FULLSCREEN_NONE];
6997 else if (fs_state == FULLSCREEN_WIDTH
6998 && maximized_width != (int)frameSize.width)
6999 [self setFSValue: FULLSCREEN_NONE];
7000 else if (fs_state == FULLSCREEN_HEIGHT
7001 && maximized_height != (int)frameSize.height)
7002 [self setFSValue: FULLSCREEN_NONE];
7004 if (fs_state == FULLSCREEN_NONE)
7005 maximized_width = maximized_height = -1;
7007 if (! [self isFullscreen])
7009 extra = FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
7010 + FRAME_TOOLBAR_HEIGHT (emacsframe);
7013 cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (emacsframe, frameSize.width);
7014 if (cols < MINWIDTH)
7017 rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (emacsframe,
7018 frameSize.height - extra);
7019 if (rows < MINHEIGHT)
7021 #ifdef NS_IMPL_COCOA
7023 /* this sets window title to have size in it; the wm does this under GS */
7024 NSRect r = [[self window] frame];
7025 if (r.size.height == frameSize.height && r.size.width == frameSize.width)
7033 else if (fs_state == FULLSCREEN_NONE && ! maximizing_resize
7034 && [[self window] title] != NULL)
7037 NSWindow *window = [self window];
7040 char *t = strdup ([[[self window] title] UTF8String]);
7041 char *pos = strstr (t, " — ");
7046 size_title = xmalloc (strlen (old_title) + 40);
7047 esprintf (size_title, "%s — (%d x %d)", old_title, cols, rows);
7048 [window setTitle: [NSString stringWithUTF8String: size_title]];
7053 #endif /* NS_IMPL_COCOA */
7055 NSTRACE_MSG ("cols: %d rows: %d", cols, rows);
7057 /* Restrict the new size to the text gird.
7059 Don't restrict the width if the user only adjusted the height, and
7060 vice versa. (Without this, the frame would shrink, and move
7061 slightly, if the window was resized by dragging one of its
7063 if (!frame_resize_pixelwise)
7065 NSRect r = [[self window] frame];
7067 if (r.size.width != frameSize.width)
7070 FRAME_TEXT_COLS_TO_PIXEL_WIDTH (emacsframe, cols);
7073 if (r.size.height != frameSize.height)
7076 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (emacsframe, rows) + extra;
7080 NSTRACE_RETURN_SIZE (frameSize);
7086 - (void)windowDidResize: (NSNotification *)notification
7088 NSTRACE ("[EmacsView windowDidResize:]");
7089 if (!FRAME_LIVE_P (emacsframe))
7091 NSTRACE_MSG ("Ignored (frame dead)");
7094 if (emacsframe->output_data.ns->in_animation)
7096 NSTRACE_MSG ("Ignored (in animation)");
7100 if (! [self fsIsNative])
7102 NSWindow *theWindow = [notification object];
7103 /* We can get notification on the non-FS window when in
7105 if ([self window] != theWindow) return;
7108 NSTRACE_RECT ("frame", [[notification object] frame]);
7110 #ifdef NS_IMPL_GNUSTEP
7111 NSWindow *theWindow = [notification object];
7113 /* In GNUstep, at least currently, it's possible to get a didResize
7114 without getting a willResize.. therefore we need to act as if we got
7115 the willResize now */
7116 NSSize sz = [theWindow frame].size;
7117 sz = [self windowWillResize: theWindow toSize: sz];
7118 #endif /* NS_IMPL_GNUSTEP */
7120 if (cols > 0 && rows > 0)
7122 [self updateFrameSize: YES];
7125 ns_send_appdefined (-1);
7128 #ifdef NS_IMPL_COCOA
7129 - (void)viewDidEndLiveResize
7131 NSTRACE ("[EmacsView viewDidEndLiveResize]");
7133 [super viewDidEndLiveResize];
7136 [[self window] setTitle: [NSString stringWithUTF8String: old_title]];
7140 maximizing_resize = NO;
7142 #endif /* NS_IMPL_COCOA */
7145 - (void)windowDidBecomeKey: (NSNotification *)notification
7146 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
7148 [self windowDidBecomeKey];
7152 - (void)windowDidBecomeKey /* for direct calls */
7154 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (emacsframe);
7155 struct frame *old_focus = dpyinfo->x_focus_frame;
7157 NSTRACE ("[EmacsView windowDidBecomeKey]");
7159 if (emacsframe != old_focus)
7160 dpyinfo->x_focus_frame = emacsframe;
7162 ns_frame_rehighlight (emacsframe);
7166 emacs_event->kind = FOCUS_IN_EVENT;
7167 EV_TRAILER ((id)nil);
7172 - (void)windowDidResignKey: (NSNotification *)notification
7173 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
7175 struct ns_display_info *dpyinfo = FRAME_DISPLAY_INFO (emacsframe);
7176 BOOL is_focus_frame = dpyinfo->x_focus_frame == emacsframe;
7177 NSTRACE ("[EmacsView windowDidResignKey:]");
7180 dpyinfo->x_focus_frame = 0;
7182 emacsframe->mouse_moved = 0;
7183 ns_frame_rehighlight (emacsframe);
7185 /* FIXME: for some reason needed on second and subsequent clicks away
7186 from sole-frame Emacs to get hollow box to show */
7187 if (!windowClosing && [[self window] isVisible] == YES)
7189 x_update_cursor (emacsframe, 1);
7190 x_set_frame_alpha (emacsframe);
7193 if (any_help_event_p)
7196 XSETFRAME (frame, emacsframe);
7197 help_echo_string = Qnil;
7198 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7201 if (emacs_event && is_focus_frame)
7203 [self deleteWorkingText];
7204 emacs_event->kind = FOCUS_OUT_EVENT;
7205 EV_TRAILER ((id)nil);
7210 - (void)windowWillMiniaturize: sender
7212 NSTRACE ("[EmacsView windowWillMiniaturize:]");
7216 - (void)setFrame:(NSRect)frameRect
7218 NSTRACE ("[EmacsView setFrame:" NSTRACE_FMT_RECT "]",
7219 NSTRACE_ARG_RECT (frameRect));
7221 [super setFrame:(NSRect)frameRect];
7237 - (void)createToolbar: (struct frame *)f
7239 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
7240 NSWindow *window = [view window];
7242 toolbar = [[EmacsToolbar alloc] initForView: self withIdentifier:
7243 [NSString stringWithFormat: @"Emacs Frame %d",
7245 [toolbar setVisible: NO];
7246 [window setToolbar: toolbar];
7248 /* Don't set frame garbaged until tool bar is up to date?
7249 This avoids an extra clear and redraw (flicker) at frame creation. */
7250 if (FRAME_EXTERNAL_TOOL_BAR (f)) wait_for_tool_bar = YES;
7251 else wait_for_tool_bar = NO;
7254 #ifdef NS_IMPL_COCOA
7256 NSButton *toggleButton;
7257 toggleButton = [window standardWindowButton: NSWindowToolbarButton];
7258 [toggleButton setTarget: self];
7259 [toggleButton setAction: @selector (toggleToolbar: )];
7265 - (instancetype) initFrameFromEmacs: (struct frame *)f
7273 NSTRACE ("[EmacsView initFrameFromEmacs:]");
7274 NSTRACE_MSG ("cols:%d lines:%d", f->text_cols, f->text_lines);
7277 processingCompose = NO;
7278 scrollbarsNeedingUpdate = 0;
7279 fs_state = FULLSCREEN_NONE;
7280 fs_before_fs = next_maximized = -1;
7283 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7284 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
7285 if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_7)
7287 fs_is_native = ns_use_native_fullscreen;
7290 maximized_width = maximized_height = -1;
7293 ns_userRect = NSMakeRect (0, 0, 0, 0);
7294 r = NSMakeRect (0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, f->text_cols),
7295 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, f->text_lines));
7296 [self initWithFrame: r];
7297 [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
7299 FRAME_NS_VIEW (f) = self;
7301 #ifdef NS_IMPL_COCOA
7303 maximizing_resize = NO;
7306 win = [[EmacsWindow alloc]
7307 initWithContentRect: r
7308 styleMask: (FRAME_UNDECORATED (f)
7309 ? FRAME_UNDECORATED_FLAGS
7310 : FRAME_DECORATED_FLAGS)
7311 backing: NSBackingStoreBuffered
7314 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7315 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
7316 if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_7)
7318 [win setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
7322 bwidth = f->border_width = wr.size.width - r.size.width;
7324 [win setAcceptsMouseMovedEvents: YES];
7325 [win setDelegate: self];
7326 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
7327 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1090
7328 if ([win respondsToSelector: @selector(useOptimizedDrawing:)])
7330 [win useOptimizedDrawing: YES];
7333 [[win contentView] addSubview: self];
7336 [self registerForDraggedTypes: ns_drag_types];
7339 name = [NSString stringWithUTF8String:
7340 NILP (tem) ? "Emacs" : SSDATA (tem)];
7341 [win setTitle: name];
7343 /* toolbar support */
7344 if (! FRAME_UNDECORATED (f))
7345 [self createToolbar: f];
7347 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
7348 #ifndef NSAppKitVersionNumber10_10
7349 #define NSAppKitVersionNumber10_10 1343
7352 if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_10
7353 && FRAME_NS_APPEARANCE (f) != ns_appearance_aqua)
7354 win.appearance = [NSAppearance
7355 appearanceNamed: NSAppearanceNameVibrantDark];
7358 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
7359 if ([win respondsToSelector: @selector(titlebarAppearsTransparent)])
7360 win.titlebarAppearsTransparent = FRAME_NS_TRANSPARENT_TITLEBAR (f);
7365 [win setMiniwindowTitle:
7366 [NSString stringWithUTF8String: SSDATA (tem)]];
7368 if (FRAME_PARENT_FRAME (f) != NULL)
7370 NSWindow *parent = [FRAME_NS_VIEW (FRAME_PARENT_FRAME (f)) window];
7371 [parent addChildWindow: win
7372 ordered: NSWindowAbove];
7375 if (FRAME_Z_GROUP (f) != z_group_none)
7376 win.level = NSNormalWindowLevel
7377 + (FRAME_Z_GROUP_BELOW (f) ? -1 : 1);
7380 NSScreen *screen = [win screen];
7384 NSPoint pt = NSMakePoint
7385 (IN_BOUND (-SCREENMAX, f->left_pos
7386 + NS_PARENT_WINDOW_LEFT_POS (f), SCREENMAX),
7387 IN_BOUND (-SCREENMAX,
7388 NS_PARENT_WINDOW_TOP_POS (f) - f->top_pos,
7391 [win setFrameTopLeftPoint: pt];
7393 NSTRACE_RECT ("new frame", [win frame]);
7397 [win makeFirstResponder: self];
7399 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
7400 (FACE_FROM_ID (emacsframe, DEFAULT_FACE_ID)),
7402 [win setBackgroundColor: col];
7403 if ([col alphaComponent] != (EmacsCGFloat) 1.0)
7404 [win setOpaque: NO];
7406 #if !defined (NS_IMPL_COCOA) \
7407 || MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
7408 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1090
7409 if ([self respondsToSelector: @selector(allocateGState)])
7411 [self allocateGState];
7413 [NSApp registerServicesMenuSendTypes: ns_send_types
7414 returnTypes: [NSArray array]];
7416 /* macOS Sierra automatically enables tabbed windows. We can't
7417 allow this to be enabled until it's available on a Free system.
7418 Currently it only happens by accident and is buggy anyway. */
7419 #if defined (NS_IMPL_COCOA) \
7420 && MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
7421 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
7422 if ([win respondsToSelector: @selector(setTabbingMode:)])
7424 [win setTabbingMode: NSWindowTabbingModeDisallowed];
7432 - (void)windowDidMove: sender
7434 NSWindow *win = [self window];
7435 NSRect r = [win frame];
7436 NSArray *screens = [NSScreen screens];
7437 NSScreen *screen = [screens objectAtIndex: 0];
7439 NSTRACE ("[EmacsView windowDidMove:]");
7441 if (!emacsframe->output_data.ns)
7445 emacsframe->left_pos = r.origin.x - NS_PARENT_WINDOW_LEFT_POS (emacsframe);
7446 emacsframe->top_pos =
7447 NS_PARENT_WINDOW_TOP_POS (emacsframe) - (r.origin.y + r.size.height);
7451 emacs_event->kind = MOVE_FRAME_EVENT;
7452 EV_TRAILER ((id)nil);
7458 /* Called AFTER method below, but before our windowWillResize call there leads
7459 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
7460 location so set_window_size moves the frame. */
7461 - (BOOL)windowShouldZoom: (NSWindow *)sender toFrame: (NSRect)newFrame
7463 NSTRACE (("[EmacsView windowShouldZoom:toFrame:" NSTRACE_FMT_RECT "]"
7464 NSTRACE_FMT_RETURN "YES"),
7465 NSTRACE_ARG_RECT (newFrame));
7467 emacsframe->output_data.ns->zooming = 1;
7472 /* Override to do something slightly nonstandard, but nice. First click on
7473 zoom button will zoom vertically. Second will zoom completely. Third
7474 returns to original. */
7475 - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender
7476 defaultFrame:(NSRect)defaultFrame
7478 // TODO: Rename to "currentFrame" and assign "result" properly in
7480 NSRect result = [sender frame];
7482 NSTRACE (("[EmacsView windowWillUseStandardFrame:defaultFrame:"
7483 NSTRACE_FMT_RECT "]"),
7484 NSTRACE_ARG_RECT (defaultFrame));
7485 NSTRACE_FSTYPE ("fs_state", fs_state);
7486 NSTRACE_FSTYPE ("fs_before_fs", fs_before_fs);
7487 NSTRACE_FSTYPE ("next_maximized", next_maximized);
7488 NSTRACE_RECT ("ns_userRect", ns_userRect);
7489 NSTRACE_RECT ("[sender frame]", [sender frame]);
7491 if (fs_before_fs != -1) /* Entering fullscreen */
7493 NSTRACE_MSG ("Entering fullscreen");
7494 result = defaultFrame;
7498 // Save the window size and position (frame) before the resize.
7499 if (fs_state != FULLSCREEN_MAXIMIZED
7500 && fs_state != FULLSCREEN_WIDTH)
7502 ns_userRect.size.width = result.size.width;
7503 ns_userRect.origin.x = result.origin.x;
7506 if (fs_state != FULLSCREEN_MAXIMIZED
7507 && fs_state != FULLSCREEN_HEIGHT)
7509 ns_userRect.size.height = result.size.height;
7510 ns_userRect.origin.y = result.origin.y;
7513 NSTRACE_RECT ("ns_userRect (2)", ns_userRect);
7515 if (next_maximized == FULLSCREEN_HEIGHT
7516 || (next_maximized == -1
7517 && abs ((int)(defaultFrame.size.height - result.size.height))
7518 > FRAME_LINE_HEIGHT (emacsframe)))
7521 NSTRACE_MSG ("FULLSCREEN_HEIGHT");
7522 maximized_height = result.size.height = defaultFrame.size.height;
7523 maximized_width = -1;
7524 result.origin.y = defaultFrame.origin.y;
7525 if (ns_userRect.size.height != 0)
7527 result.origin.x = ns_userRect.origin.x;
7528 result.size.width = ns_userRect.size.width;
7530 [self setFSValue: FULLSCREEN_HEIGHT];
7531 #ifdef NS_IMPL_COCOA
7532 maximizing_resize = YES;
7535 else if (next_maximized == FULLSCREEN_WIDTH)
7537 NSTRACE_MSG ("FULLSCREEN_WIDTH");
7538 maximized_width = result.size.width = defaultFrame.size.width;
7539 maximized_height = -1;
7540 result.origin.x = defaultFrame.origin.x;
7541 if (ns_userRect.size.width != 0)
7543 result.origin.y = ns_userRect.origin.y;
7544 result.size.height = ns_userRect.size.height;
7546 [self setFSValue: FULLSCREEN_WIDTH];
7548 else if (next_maximized == FULLSCREEN_MAXIMIZED
7549 || (next_maximized == -1
7550 && abs ((int)(defaultFrame.size.width - result.size.width))
7551 > FRAME_COLUMN_WIDTH (emacsframe)))
7553 NSTRACE_MSG ("FULLSCREEN_MAXIMIZED");
7555 result = defaultFrame; /* second click */
7556 maximized_width = result.size.width;
7557 maximized_height = result.size.height;
7558 [self setFSValue: FULLSCREEN_MAXIMIZED];
7559 #ifdef NS_IMPL_COCOA
7560 maximizing_resize = YES;
7566 NSTRACE_MSG ("Restore");
7567 result = ns_userRect.size.height ? ns_userRect : result;
7568 NSTRACE_RECT ("restore (2)", result);
7569 ns_userRect = NSMakeRect (0, 0, 0, 0);
7570 #ifdef NS_IMPL_COCOA
7571 maximizing_resize = fs_state != FULLSCREEN_NONE;
7573 [self setFSValue: FULLSCREEN_NONE];
7574 maximized_width = maximized_height = -1;
7578 if (fs_before_fs == -1) next_maximized = -1;
7580 NSTRACE_RECT ("Final ns_userRect", ns_userRect);
7581 NSTRACE_MSG ("Final maximized_width: %d", maximized_width);
7582 NSTRACE_MSG ("Final maximized_height: %d", maximized_height);
7583 NSTRACE_FSTYPE ("Final next_maximized", next_maximized);
7585 [self windowWillResize: sender toSize: result.size];
7587 NSTRACE_RETURN_RECT (result);
7593 - (void)windowDidDeminiaturize: sender
7595 NSTRACE ("[EmacsView windowDidDeminiaturize:]");
7596 if (!emacsframe->output_data.ns)
7599 SET_FRAME_ICONIFIED (emacsframe, 0);
7600 SET_FRAME_VISIBLE (emacsframe, 1);
7601 windows_or_buffers_changed = 63;
7605 emacs_event->kind = DEICONIFY_EVENT;
7606 EV_TRAILER ((id)nil);
7611 - (void)windowDidExpose: sender
7613 NSTRACE ("[EmacsView windowDidExpose:]");
7614 if (!emacsframe->output_data.ns)
7617 SET_FRAME_VISIBLE (emacsframe, 1);
7618 SET_FRAME_GARBAGED (emacsframe);
7620 if (send_appdefined)
7621 ns_send_appdefined (-1);
7625 - (void)windowDidMiniaturize: sender
7627 NSTRACE ("[EmacsView windowDidMiniaturize:]");
7628 if (!emacsframe->output_data.ns)
7631 SET_FRAME_ICONIFIED (emacsframe, 1);
7632 SET_FRAME_VISIBLE (emacsframe, 0);
7636 emacs_event->kind = ICONIFY_EVENT;
7637 EV_TRAILER ((id)nil);
7641 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7642 - (NSApplicationPresentationOptions)window:(NSWindow *)window
7643 willUseFullScreenPresentationOptions:
7644 (NSApplicationPresentationOptions)proposedOptions
7646 return proposedOptions|NSApplicationPresentationAutoHideToolbar;
7650 - (void)windowWillEnterFullScreen:(NSNotification *)notification
7652 NSTRACE ("[EmacsView windowWillEnterFullScreen:]");
7653 [self windowWillEnterFullScreen];
7655 - (void)windowWillEnterFullScreen /* provided for direct calls */
7657 NSTRACE ("[EmacsView windowWillEnterFullScreen]");
7658 fs_before_fs = fs_state;
7661 - (void)windowDidEnterFullScreen:(NSNotification *)notification
7663 NSTRACE ("[EmacsView windowDidEnterFullScreen:]");
7664 [self windowDidEnterFullScreen];
7667 - (void)windowDidEnterFullScreen /* provided for direct calls */
7669 NSTRACE ("[EmacsView windowDidEnterFullScreen]");
7670 [self setFSValue: FULLSCREEN_BOTH];
7671 if (! [self fsIsNative])
7673 [self windowDidBecomeKey];
7674 [nonfs_window orderOut:self];
7678 BOOL tbar_visible = FRAME_EXTERNAL_TOOL_BAR (emacsframe) ? YES : NO;
7679 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 \
7680 && MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
7681 unsigned val = (unsigned)[NSApp presentationOptions];
7683 // Mac OS X 10.7 bug fix, the menu won't appear without this.
7684 // val is non-zero on other macOS versions.
7687 NSApplicationPresentationOptions options
7688 = NSApplicationPresentationAutoHideDock
7689 | NSApplicationPresentationAutoHideMenuBar
7690 | NSApplicationPresentationFullScreen
7691 | NSApplicationPresentationAutoHideToolbar;
7693 [NSApp setPresentationOptions: options];
7696 [toolbar setVisible:tbar_visible];
7700 - (void)windowWillExitFullScreen:(NSNotification *)notification
7702 NSTRACE ("[EmacsView windowWillExitFullScreen:]");
7703 [self windowWillExitFullScreen];
7706 - (void)windowWillExitFullScreen /* provided for direct calls */
7708 NSTRACE ("[EmacsView windowWillExitFullScreen]");
7709 if (!FRAME_LIVE_P (emacsframe))
7711 NSTRACE_MSG ("Ignored (frame dead)");
7714 if (next_maximized != -1)
7715 fs_before_fs = next_maximized;
7718 - (void)windowDidExitFullScreen:(NSNotification *)notification
7720 NSTRACE ("[EmacsView windowDidExitFullScreen:]");
7721 [self windowDidExitFullScreen];
7724 - (void)windowDidExitFullScreen /* provided for direct calls */
7726 NSTRACE ("[EmacsView windowDidExitFullScreen]");
7727 if (!FRAME_LIVE_P (emacsframe))
7729 NSTRACE_MSG ("Ignored (frame dead)");
7732 [self setFSValue: fs_before_fs];
7734 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7735 [self updateCollectionBehavior];
7737 if (FRAME_EXTERNAL_TOOL_BAR (emacsframe))
7739 [toolbar setVisible:YES];
7740 update_frame_tool_bar (emacsframe);
7741 [self updateFrameSize:YES];
7742 [[self window] display];
7745 [toolbar setVisible:NO];
7747 if (next_maximized != -1)
7748 [[self window] performZoom:self];
7753 return fs_is_native;
7756 - (BOOL)isFullscreen
7762 res = (nonfs_window != nil);
7766 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7767 res = (([[self window] styleMask] & NSWindowStyleMaskFullScreen) != 0);
7773 NSTRACE ("[EmacsView isFullscreen] " NSTRACE_FMT_RETURN " %d",
7779 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7780 - (void)updateCollectionBehavior
7782 NSTRACE ("[EmacsView updateCollectionBehavior]");
7784 if (! [self isFullscreen])
7786 NSWindow *win = [self window];
7787 NSWindowCollectionBehavior b = [win collectionBehavior];
7788 if (ns_use_native_fullscreen)
7789 b |= NSWindowCollectionBehaviorFullScreenPrimary;
7791 b &= ~NSWindowCollectionBehaviorFullScreenPrimary;
7793 [win setCollectionBehavior: b];
7794 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
7795 if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_7)
7797 fs_is_native = ns_use_native_fullscreen;
7802 - (void)toggleFullScreen: (id)sender
7810 NSTRACE ("[EmacsView toggleFullScreen:]");
7814 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
7815 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
7816 if ([[self window] respondsToSelector: @selector(toggleFullScreen:)])
7818 [[self window] toggleFullScreen:sender];
7824 onFirstScreen = [[w screen] isEqual:[[NSScreen screens] objectAtIndex:0]];
7827 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
7828 (FACE_FROM_ID (f, DEFAULT_FACE_ID)),
7831 if (fs_state != FULLSCREEN_BOTH)
7833 NSScreen *screen = [w screen];
7835 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
7836 /* Hide ghost menu bar on secondary monitor? */
7838 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
7839 && [NSScreen respondsToSelector: @selector(screensHaveSeparateSpaces)]
7842 onFirstScreen = [NSScreen screensHaveSeparateSpaces];
7844 /* Hide dock and menubar if we are on the primary screen. */
7847 #ifdef NS_IMPL_COCOA
7848 NSApplicationPresentationOptions options
7849 = NSApplicationPresentationAutoHideDock
7850 | NSApplicationPresentationAutoHideMenuBar;
7852 [NSApp setPresentationOptions: options];
7854 [NSMenu setMenuBarVisible:NO];
7858 fw = [[EmacsFSWindow alloc]
7859 initWithContentRect:[w contentRectForFrameRect:wr]
7860 styleMask:NSWindowStyleMaskBorderless
7861 backing:NSBackingStoreBuffered
7865 [fw setContentView:[w contentView]];
7866 [fw setTitle:[w title]];
7867 [fw setDelegate:self];
7868 [fw setAcceptsMouseMovedEvents: YES];
7869 #if !defined (NS_IMPL_COCOA) \
7870 || MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
7871 #if MAC_OS_X_VERSION_MAX_ALLOWED > 1090
7872 if ([fw respondsToSelector: @selector(useOptimizedDrawing:)])
7874 [fw useOptimizedDrawing: YES];
7876 [fw setBackgroundColor: col];
7877 if ([col alphaComponent] != (EmacsCGFloat) 1.0)
7880 f->border_width = 0;
7884 [self windowWillEnterFullScreen];
7885 [fw makeKeyAndOrderFront:NSApp];
7886 [fw makeFirstResponder:self];
7888 r = [fw frameRectForContentRect:[screen frame]];
7889 [fw setFrame: r display:YES animate:ns_use_fullscreen_animation];
7890 [self windowDidEnterFullScreen];
7901 #ifdef NS_IMPL_COCOA
7902 [NSApp setPresentationOptions: NSApplicationPresentationDefault];
7904 [NSMenu setMenuBarVisible:YES];
7908 [w setContentView:[fw contentView]];
7909 [w setBackgroundColor: col];
7910 if ([col alphaComponent] != (EmacsCGFloat) 1.0)
7913 f->border_width = bwidth;
7915 // to do: consider using [NSNotificationCenter postNotificationName:] to send notifications.
7917 [self windowWillExitFullScreen];
7918 [fw setFrame: [w frame] display:YES animate:ns_use_fullscreen_animation];
7920 [w makeKeyAndOrderFront:NSApp];
7921 [self windowDidExitFullScreen];
7922 [self updateFrameSize:YES];
7928 NSTRACE ("[EmacsView handleFS]");
7930 if (fs_state != emacsframe->want_fullscreen)
7932 if (fs_state == FULLSCREEN_BOTH)
7934 NSTRACE_MSG ("fs_state == FULLSCREEN_BOTH");
7935 [self toggleFullScreen:self];
7938 switch (emacsframe->want_fullscreen)
7940 case FULLSCREEN_BOTH:
7941 NSTRACE_MSG ("FULLSCREEN_BOTH");
7942 [self toggleFullScreen:self];
7944 case FULLSCREEN_WIDTH:
7945 NSTRACE_MSG ("FULLSCREEN_WIDTH");
7946 next_maximized = FULLSCREEN_WIDTH;
7947 if (fs_state != FULLSCREEN_BOTH)
7948 [[self window] performZoom:self];
7950 case FULLSCREEN_HEIGHT:
7951 NSTRACE_MSG ("FULLSCREEN_HEIGHT");
7952 next_maximized = FULLSCREEN_HEIGHT;
7953 if (fs_state != FULLSCREEN_BOTH)
7954 [[self window] performZoom:self];
7956 case FULLSCREEN_MAXIMIZED:
7957 NSTRACE_MSG ("FULLSCREEN_MAXIMIZED");
7958 next_maximized = FULLSCREEN_MAXIMIZED;
7959 if (fs_state != FULLSCREEN_BOTH)
7960 [[self window] performZoom:self];
7962 case FULLSCREEN_NONE:
7963 NSTRACE_MSG ("FULLSCREEN_NONE");
7964 if (fs_state != FULLSCREEN_BOTH)
7966 next_maximized = FULLSCREEN_NONE;
7967 [[self window] performZoom:self];
7972 emacsframe->want_fullscreen = FULLSCREEN_NONE;
7977 - (void) setFSValue: (int)value
7979 NSTRACE ("[EmacsView setFSValue:" NSTRACE_FMT_FSTYPE "]",
7980 NSTRACE_ARG_FSTYPE(value));
7982 Lisp_Object lval = Qnil;
7985 case FULLSCREEN_BOTH:
7988 case FULLSCREEN_WIDTH:
7991 case FULLSCREEN_HEIGHT:
7994 case FULLSCREEN_MAXIMIZED:
7998 store_frame_param (emacsframe, Qfullscreen, lval);
8002 - (void)mouseEntered: (NSEvent *)theEvent
8004 NSTRACE ("[EmacsView mouseEntered:]");
8006 FRAME_DISPLAY_INFO (emacsframe)->last_mouse_movement_time
8007 = EV_TIMESTAMP (theEvent);
8011 - (void)mouseExited: (NSEvent *)theEvent
8013 Mouse_HLInfo *hlinfo = emacsframe ? MOUSE_HL_INFO (emacsframe) : NULL;
8015 NSTRACE ("[EmacsView mouseExited:]");
8020 FRAME_DISPLAY_INFO (emacsframe)->last_mouse_movement_time
8021 = EV_TIMESTAMP (theEvent);
8023 if (emacsframe == hlinfo->mouse_face_mouse_frame)
8025 clear_mouse_face (hlinfo);
8026 hlinfo->mouse_face_mouse_frame = 0;
8031 - (instancetype)menuDown: sender
8033 NSTRACE ("[EmacsView menuDown:]");
8034 if (context_menu_value == -1)
8035 context_menu_value = [sender tag];
8038 NSInteger tag = [sender tag];
8039 find_and_call_menu_selection (emacsframe, emacsframe->menu_bar_items_used,
8040 emacsframe->menu_bar_vector,
8044 ns_send_appdefined (-1);
8049 - (EmacsToolbar *)toolbar
8055 /* this gets called on toolbar button click */
8056 - (instancetype)toolbarClicked: (id)item
8059 int idx = [item tag] * TOOL_BAR_ITEM_NSLOTS;
8061 NSTRACE ("[EmacsView toolbarClicked:]");
8066 /* send first event (for some reason two needed) */
8067 theEvent = [[self window] currentEvent];
8068 emacs_event->kind = TOOL_BAR_EVENT;
8069 XSETFRAME (emacs_event->arg, emacsframe);
8070 EV_TRAILER (theEvent);
8072 emacs_event->kind = TOOL_BAR_EVENT;
8073 /* XSETINT (emacs_event->code, 0); */
8074 emacs_event->arg = AREF (emacsframe->tool_bar_items,
8075 idx + TOOL_BAR_ITEM_KEY);
8076 emacs_event->modifiers = EV_MODIFIERS (theEvent);
8077 EV_TRAILER (theEvent);
8082 - (instancetype)toggleToolbar: (id)sender
8084 NSTRACE ("[EmacsView toggleToolbar:]");
8089 emacs_event->kind = NS_NONKEY_EVENT;
8090 emacs_event->code = KEY_NS_TOGGLE_TOOLBAR;
8091 EV_TRAILER ((id)nil);
8096 - (void)drawRect: (NSRect)rect
8098 int x = NSMinX (rect), y = NSMinY (rect);
8099 int width = NSWidth (rect), height = NSHeight (rect);
8101 NSTRACE ("[EmacsView drawRect:" NSTRACE_FMT_RECT "]",
8102 NSTRACE_ARG_RECT(rect));
8104 if (!emacsframe || !emacsframe->output_data.ns)
8107 ns_clear_frame_area (emacsframe, x, y, width, height);
8109 expose_frame (emacsframe, x, y, width, height);
8113 drawRect: may be called (at least in Mac OS X 10.5) for invisible
8114 views as well for some reason. Thus, do not infer visibility
8117 emacsframe->async_visible = 1;
8118 emacsframe->async_iconified = 0;
8123 /* NSDraggingDestination protocol methods. Actually this is not really a
8124 protocol, but a category of Object. O well... */
8126 -(NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender
8128 NSTRACE ("[EmacsView draggingEntered:]");
8129 return NSDragOperationGeneric;
8133 -(BOOL)prepareForDragOperation: (id <NSDraggingInfo>) sender
8139 -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
8144 NSEvent *theEvent = [[self window] currentEvent];
8146 NSDragOperation op = [sender draggingSourceOperationMask];
8149 NSTRACE ("[EmacsView performDragOperation:]");
8154 position = [self convertPoint: [sender draggingLocation] fromView: nil];
8155 x = lrint (position.x); y = lrint (position.y);
8157 pb = [sender draggingPasteboard];
8158 type = [pb availableTypeFromArray: ns_drag_types];
8160 if (! (op & (NSDragOperationMove|NSDragOperationDelete)) &&
8161 // URL drags contain all operations (0xf), don't allow all to be set.
8164 if (op & NSDragOperationLink)
8165 modifiers |= NSEventModifierFlagControl;
8166 if (op & NSDragOperationCopy)
8167 modifiers |= NSEventModifierFlagOption;
8168 if (op & NSDragOperationGeneric)
8169 modifiers |= NSEventModifierFlagCommand;
8172 modifiers = EV_MODIFIERS2 (modifiers);
8177 else if ([type isEqualToString: NSFilenamesPboardType])
8180 NSEnumerator *fenum;
8183 if (!(files = [pb propertyListForType: type]))
8186 fenum = [files objectEnumerator];
8187 while ( (file = [fenum nextObject]) )
8189 emacs_event->kind = DRAG_N_DROP_EVENT;
8190 XSETINT (emacs_event->x, x);
8191 XSETINT (emacs_event->y, y);
8192 emacs_event->modifiers = modifiers;
8193 emacs_event->arg = list2 (Qfile, build_string ([file UTF8String]));
8194 EV_TRAILER (theEvent);
8198 else if ([type isEqualToString: NSURLPboardType])
8200 NSURL *url = [NSURL URLFromPasteboard: pb];
8201 if (url == nil) return NO;
8203 emacs_event->kind = DRAG_N_DROP_EVENT;
8204 XSETINT (emacs_event->x, x);
8205 XSETINT (emacs_event->y, y);
8206 emacs_event->modifiers = modifiers;
8207 emacs_event->arg = list2 (Qurl,
8208 build_string ([[url absoluteString]
8210 EV_TRAILER (theEvent);
8212 if ([url isFileURL] != NO)
8214 NSString *file = [url path];
8215 ns_input_file = append2 (ns_input_file,
8216 build_string ([file UTF8String]));
8220 else if ([type isEqualToString: NSStringPboardType]
8221 || [type isEqualToString: NSTabularTextPboardType])
8225 if (! (data = [pb stringForType: type]))
8228 emacs_event->kind = DRAG_N_DROP_EVENT;
8229 XSETINT (emacs_event->x, x);
8230 XSETINT (emacs_event->y, y);
8231 emacs_event->modifiers = modifiers;
8232 emacs_event->arg = list2 (Qnil, build_string ([data UTF8String]));
8233 EV_TRAILER (theEvent);
8238 fprintf (stderr, "Invalid data type in dragging pasteboard");
8244 - (id) validRequestorForSendType: (NSString *)typeSent
8245 returnType: (NSString *)typeReturned
8247 NSTRACE ("[EmacsView validRequestorForSendType:returnType:]");
8248 if (typeSent != nil && [ns_send_types indexOfObject: typeSent] != NSNotFound
8249 && typeReturned == nil)
8251 if (! NILP (ns_get_local_selection (QPRIMARY, QUTF8_STRING)))
8255 return [super validRequestorForSendType: typeSent
8256 returnType: typeReturned];
8260 /* The next two methods are part of NSServicesRequests informal protocol,
8261 supposedly called when a services menu item is chosen from this app.
8262 But this should not happen because we override the services menu with our
8263 own entries which call ns-perform-service.
8264 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
8265 So let's at least stub them out until further investigation can be done. */
8267 - (BOOL) readSelectionFromPasteboard: (NSPasteboard *)pb
8269 /* we could call ns_string_from_pasteboard(pboard) here but then it should
8270 be written into the buffer in place of the existing selection..
8271 ordinary service calls go through functions defined in ns-win.el */
8275 - (BOOL) writeSelectionToPasteboard: (NSPasteboard *)pb types: (NSArray *)types
8277 NSArray *typesDeclared;
8280 NSTRACE ("[EmacsView writeSelectionToPasteboard:types:]");
8282 /* We only support NSStringPboardType */
8283 if ([types containsObject:NSStringPboardType] == NO) {
8287 val = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
8288 if (CONSP (val) && SYMBOLP (XCAR (val)))
8291 if (CONSP (val) && NILP (XCDR (val)))
8294 if (! STRINGP (val))
8297 typesDeclared = [NSArray arrayWithObject:NSStringPboardType];
8298 [pb declareTypes:typesDeclared owner:nil];
8299 ns_string_to_pasteboard (pb, val);
8304 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
8305 (gives a miniaturized version of the window); currently we use the latter for
8306 frames whose active buffer doesn't correspond to any file
8307 (e.g., '*scratch*') */
8308 - (instancetype)setMiniwindowImage: (BOOL) setMini
8310 id image = [[self window] miniwindowImage];
8311 NSTRACE ("[EmacsView setMiniwindowImage:%d]", setMini);
8313 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
8314 about "AppleDockIconEnabled" notwithstanding, however the set message
8315 below has its effect nonetheless. */
8316 if (image != emacsframe->output_data.ns->miniimage)
8318 if (image && [image isKindOfClass: [EmacsImage class]])
8320 [[self window] setMiniwindowImage:
8321 setMini ? emacsframe->output_data.ns->miniimage : nil];
8328 - (void) setRows: (int) r andColumns: (int) c
8330 NSTRACE ("[EmacsView setRows:%d andColumns:%d]", r, c);
8335 - (int) fullscreenState
8340 @end /* EmacsView */
8344 /* ==========================================================================
8346 EmacsWindow implementation
8348 ========================================================================== */
8350 @implementation EmacsWindow
8352 #ifdef NS_IMPL_COCOA
8353 - (id)accessibilityAttributeValue:(NSString *)attribute
8355 Lisp_Object str = Qnil;
8356 struct frame *f = SELECTED_FRAME ();
8357 struct buffer *curbuf = XBUFFER (XWINDOW (f->selected_window)->contents);
8359 NSTRACE ("[EmacsWindow accessibilityAttributeValue:]");
8361 if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
8362 return NSAccessibilityTextFieldRole;
8364 if ([attribute isEqualToString:NSAccessibilitySelectedTextAttribute]
8365 && curbuf && ! NILP (BVAR (curbuf, mark_active)))
8367 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
8369 else if (curbuf && [attribute isEqualToString:NSAccessibilityValueAttribute])
8371 if (! NILP (BVAR (curbuf, mark_active)))
8372 str = ns_get_local_selection (QPRIMARY, QUTF8_STRING);
8376 ptrdiff_t start_byte = BUF_BEGV_BYTE (curbuf);
8377 ptrdiff_t byte_range = BUF_ZV_BYTE (curbuf) - start_byte;
8378 ptrdiff_t range = BUF_ZV (curbuf) - BUF_BEGV (curbuf);
8380 if (! NILP (BVAR (curbuf, enable_multibyte_characters)))
8381 str = make_uninit_multibyte_string (range, byte_range);
8383 str = make_uninit_string (range);
8384 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
8385 Is this a problem? */
8386 memcpy (SDATA (str), BYTE_POS_ADDR (start_byte), byte_range);
8393 if (CONSP (str) && SYMBOLP (XCAR (str)))
8396 if (CONSP (str) && NILP (XCDR (str)))
8401 const char *utfStr = SSDATA (str);
8402 NSString *nsStr = [NSString stringWithUTF8String: utfStr];
8407 return [super accessibilityAttributeValue:attribute];
8409 #endif /* NS_IMPL_COCOA */
8411 /* Constrain size and placement of a frame.
8413 By returning the original "frameRect", the frame is not
8414 constrained. This can lead to unwanted situations where, for
8415 example, the menu bar covers the frame.
8417 The default implementation (accessed using "super") constrains the
8418 frame to the visible area of SCREEN, minus the menu bar (if
8419 present) and the Dock. Note that default implementation also calls
8420 windowWillResize, with the frame it thinks should have. (This can
8421 make the frame exit maximized mode.)
8423 Note that this should work in situations where multiple monitors
8424 are present. Common configurations are side-by-side monitors and a
8425 monitor on top of another (e.g. when a laptop is placed under a
8427 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
8429 NSTRACE ("[EmacsWindow constrainFrameRect:" NSTRACE_FMT_RECT " toScreen:]",
8430 NSTRACE_ARG_RECT (frameRect));
8432 #ifdef NS_IMPL_COCOA
8433 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
8434 // If separate spaces is on, it is like each screen is independent. There is
8435 // no spanning of frames across screens.
8437 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
8438 [NSScreen respondsToSelector: @selector(screensHaveSeparateSpaces)] &&
8440 [NSScreen screensHaveSeparateSpaces])
8442 NSTRACE_MSG ("Screens have separate spaces");
8443 frameRect = [super constrainFrameRect:frameRect toScreen:screen];
8444 NSTRACE_RETURN_RECT (frameRect);
8448 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1090 */
8450 // Check that the proposed frameRect is visible in at least one
8451 // screen. If it is not, ask the system to reposition it (only
8452 // for non-child windows).
8454 if (!FRAME_PARENT_FRAME (((EmacsView *)[self delegate])->emacsframe))
8456 NSArray *screens = [NSScreen screens];
8457 NSUInteger nr_screens = [screens count];
8460 BOOL frame_on_screen = NO;
8462 for (i = 0; i < nr_screens; ++i)
8464 NSScreen *s = [screens objectAtIndex: i];
8465 NSRect scrRect = [s frame];
8467 if (NSIntersectsRect(frameRect, scrRect))
8469 frame_on_screen = YES;
8474 if (!frame_on_screen)
8476 NSTRACE_MSG ("Frame outside screens; constraining");
8477 frameRect = [super constrainFrameRect:frameRect toScreen:screen];
8478 NSTRACE_RETURN_RECT (frameRect);
8484 return constrain_frame_rect(frameRect,
8485 [(EmacsView *)[self delegate] isFullscreen]);
8489 - (void)performZoom:(id)sender
8491 NSTRACE ("[EmacsWindow performZoom:]");
8493 return [super performZoom:sender];
8496 - (void)zoom:(id)sender
8498 NSTRACE ("[EmacsWindow zoom:]");
8500 ns_update_auto_hide_menu_bar();
8502 // Below are three zoom implementations. In the final commit, the
8503 // idea is that the last should be included.
8506 // Native zoom done using the standard zoom animation. Size of the
8507 // resulting frame reduced to accommodate the Dock and, if present,
8509 [super zoom:sender];
8512 // Native zoom done using the standard zoom animation, plus an
8513 // explicit resize to cover the full screen, except the menu-bar and
8514 // dock, if present.
8515 [super zoom:sender];
8517 // After the native zoom, resize the resulting frame to fill the
8518 // entire screen, except the menu-bar.
8520 // This works for all practical purposes. (The only minor oddity is
8521 // when transiting from full-height frame to a maximized, the
8522 // animation reduces the height of the frame slightly (to the 4
8523 // pixels needed to accommodate the Doc) before it snaps back into
8524 // full height. The user would need a very trained eye to spot
8526 NSScreen * screen = [self screen];
8529 int fs_state = [(EmacsView *)[self delegate] fullscreenState];
8531 NSTRACE_FSTYPE ("fullscreenState", fs_state);
8533 NSRect sr = [screen frame];
8534 struct EmacsMargins margins
8535 = ns_screen_margins_ignoring_hidden_dock(screen);
8537 NSRect wr = [self frame];
8538 NSTRACE_RECT ("Rect after zoom", wr);
8542 if (fs_state == FULLSCREEN_MAXIMIZED
8543 || fs_state == FULLSCREEN_HEIGHT)
8545 newWr.origin.y = sr.origin.y + margins.bottom;
8546 newWr.size.height = sr.size.height - margins.top - margins.bottom;
8549 if (fs_state == FULLSCREEN_MAXIMIZED
8550 || fs_state == FULLSCREEN_WIDTH)
8552 newWr.origin.x = sr.origin.x + margins.left;
8553 newWr.size.width = sr.size.width - margins.right - margins.left;
8556 if (newWr.size.width != wr.size.width
8557 || newWr.size.height != wr.size.height
8558 || newWr.origin.x != wr.origin.x
8559 || newWr.origin.y != wr.origin.y)
8561 NSTRACE_MSG ("New frame different");
8562 [self setFrame: newWr display: NO];
8566 // Non-native zoom which is done instantaneously. The resulting
8567 // frame covers the entire screen, except the menu-bar and dock, if
8569 NSScreen * screen = [self screen];
8572 NSRect sr = [screen frame];
8573 struct EmacsMargins margins
8574 = ns_screen_margins_ignoring_hidden_dock(screen);
8576 sr.size.height -= (margins.top + margins.bottom);
8577 sr.size.width -= (margins.left + margins.right);
8578 sr.origin.x += margins.left;
8579 sr.origin.y += margins.bottom;
8581 sr = [[self delegate] windowWillUseStandardFrame:self
8583 [self setFrame: sr display: NO];
8588 - (void)setFrame:(NSRect)windowFrame
8589 display:(BOOL)displayViews
8591 NSTRACE ("[EmacsWindow setFrame:" NSTRACE_FMT_RECT " display:%d]",
8592 NSTRACE_ARG_RECT (windowFrame), displayViews);
8594 [super setFrame:windowFrame display:displayViews];
8597 - (void)setFrame:(NSRect)windowFrame
8598 display:(BOOL)displayViews
8599 animate:(BOOL)performAnimation
8601 NSTRACE ("[EmacsWindow setFrame:" NSTRACE_FMT_RECT
8602 " display:%d performAnimation:%d]",
8603 NSTRACE_ARG_RECT (windowFrame), displayViews, performAnimation);
8605 [super setFrame:windowFrame display:displayViews animate:performAnimation];
8608 - (void)setFrameTopLeftPoint:(NSPoint)point
8610 NSTRACE ("[EmacsWindow setFrameTopLeftPoint:" NSTRACE_FMT_POINT "]",
8611 NSTRACE_ARG_POINT (point));
8613 [super setFrameTopLeftPoint:point];
8616 - (BOOL)canBecomeKeyWindow
8618 return !FRAME_NO_ACCEPT_FOCUS (((EmacsView *)[self delegate])->emacsframe);
8620 @end /* EmacsWindow */
8623 @implementation EmacsFSWindow
8625 - (BOOL)canBecomeKeyWindow
8630 - (BOOL)canBecomeMainWindow
8637 /* ==========================================================================
8639 EmacsScroller implementation
8641 ========================================================================== */
8644 @implementation EmacsScroller
8646 /* for repeat button push */
8647 #define SCROLL_BAR_FIRST_DELAY 0.5
8648 #define SCROLL_BAR_CONTINUOUS_DELAY (1.0 / 15)
8650 + (CGFloat) scrollerWidth
8652 /* TODO: if we want to allow variable widths, this is the place to do it,
8653 however neither GNUstep nor Cocoa support it very well */
8655 #if defined (NS_IMPL_COCOA) \
8656 && MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
8657 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
8658 if ([NSScroller respondsToSelector:
8659 @selector(scrollerWidthForControlSize:scrollerStyle:)])
8661 r = [NSScroller scrollerWidthForControlSize: NSControlSizeRegular
8662 scrollerStyle: NSScrollerStyleLegacy];
8663 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
8666 #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 */
8667 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070 \
8668 || defined (NS_IMPL_GNUSTEP)
8669 r = [NSScroller scrollerWidth];
8674 - (instancetype)initFrame: (NSRect )r window: (Lisp_Object)nwin
8676 NSTRACE ("[EmacsScroller initFrame: window:]");
8678 if (r.size.width > r.size.height)
8683 [super initWithFrame: r/*NSMakeRect (0, 0, 0, 0)*/];
8684 [self setContinuous: YES];
8685 [self setEnabled: YES];
8687 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
8688 locked against the top and bottom edges, and right edge on macOS, where
8689 scrollers are on right. */
8690 #ifdef NS_IMPL_GNUSTEP
8691 [self setAutoresizingMask: NSViewMaxXMargin | NSViewHeightSizable];
8693 [self setAutoresizingMask: NSViewMinXMargin | NSViewHeightSizable];
8696 window = XWINDOW (nwin);
8699 pixel_length = NSWidth (r);
8701 pixel_length = NSHeight (r);
8702 if (pixel_length == 0) pixel_length = 1;
8703 min_portion = 20 / pixel_length;
8705 frame = XFRAME (window->frame);
8706 if (FRAME_LIVE_P (frame))
8709 EmacsView *view = FRAME_NS_VIEW (frame);
8710 NSView *sview = [[view window] contentView];
8711 NSArray *subs = [sview subviews];
8713 /* disable optimization stopping redraw of other scrollbars */
8714 view->scrollbarsNeedingUpdate = 0;
8715 for (i =[subs count]-1; i >= 0; i--)
8716 if ([[subs objectAtIndex: i] isKindOfClass: [EmacsScroller class]])
8717 view->scrollbarsNeedingUpdate++;
8718 [sview addSubview: self];
8721 /* [self setFrame: r]; */
8727 - (void)setFrame: (NSRect)newRect
8729 NSTRACE ("[EmacsScroller setFrame:]");
8731 /* block_input (); */
8733 pixel_length = NSWidth (newRect);
8735 pixel_length = NSHeight (newRect);
8736 if (pixel_length == 0) pixel_length = 1;
8737 min_portion = 20 / pixel_length;
8738 [super setFrame: newRect];
8739 /* unblock_input (); */
8745 NSTRACE ("[EmacsScroller dealloc]");
8749 wset_horizontal_scroll_bar (window, Qnil);
8751 wset_vertical_scroll_bar (window, Qnil);
8758 - (instancetype)condemn
8760 NSTRACE ("[EmacsScroller condemn]");
8766 - (instancetype)reprieve
8768 NSTRACE ("[EmacsScroller reprieve]");
8776 NSTRACE ("[EmacsScroller judge]");
8777 bool ret = condemned;
8782 /* ensure other scrollbar updates after deletion */
8783 view = (EmacsView *)FRAME_NS_VIEW (frame);
8785 view->scrollbarsNeedingUpdate++;
8789 wset_horizontal_scroll_bar (window, Qnil);
8791 wset_vertical_scroll_bar (window, Qnil);
8794 [self removeFromSuperview];
8802 - (void)resetCursorRects
8804 NSRect visible = [self visibleRect];
8805 NSTRACE ("[EmacsScroller resetCursorRects]");
8807 if (!NSIsEmptyRect (visible))
8808 [self addCursorRect: visible cursor: [NSCursor arrowCursor]];
8809 [[NSCursor arrowCursor] setOnMouseEntered: YES];
8813 - (int) checkSamePosition: (int) position portion: (int) portion
8816 return em_position ==position && em_portion ==portion && em_whole ==whole
8817 && portion != whole; /* needed for resize empty buf */
8821 - (instancetype)setPosition: (int)position portion: (int)portion whole: (int)whole
8823 NSTRACE ("[EmacsScroller setPosition:portion:whole:]");
8825 em_position = position;
8826 em_portion = portion;
8829 if (portion >= whole)
8831 #ifdef NS_IMPL_COCOA
8832 [self setKnobProportion: 1.0];
8833 [self setDoubleValue: 1.0];
8835 [self setFloatValue: 0.0 knobProportion: 1.0];
8842 portion = max ((float)whole*min_portion/pixel_length, portion);
8843 pos = (float)position / (whole - portion);
8844 por = (CGFloat)portion/whole;
8845 #ifdef NS_IMPL_COCOA
8846 [self setKnobProportion: por];
8847 [self setDoubleValue: pos];
8849 [self setFloatValue: pos knobProportion: por];
8856 /* set up emacs_event */
8857 - (void) sendScrollEventAtLoc: (float)loc fromEvent: (NSEvent *)e
8861 NSTRACE ("[EmacsScroller sendScrollEventAtLoc:fromEvent:]");
8866 emacs_event->part = last_hit_part;
8867 emacs_event->code = 0;
8868 emacs_event->modifiers = EV_MODIFIERS (e) | down_modifier;
8869 XSETWINDOW (win, window);
8870 emacs_event->frame_or_window = win;
8871 emacs_event->timestamp = EV_TIMESTAMP (e);
8872 emacs_event->arg = Qnil;
8876 emacs_event->kind = HORIZONTAL_SCROLL_BAR_CLICK_EVENT;
8877 XSETINT (emacs_event->x, em_whole * loc / pixel_length);
8878 XSETINT (emacs_event->y, em_whole);
8882 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
8883 XSETINT (emacs_event->x, loc);
8884 XSETINT (emacs_event->y, pixel_length-20);
8889 n_emacs_events_pending++;
8890 kbd_buffer_store_event_hold (emacs_event, q_event_ptr);
8893 hold_event (emacs_event);
8894 EVENT_INIT (*emacs_event);
8895 ns_send_appdefined (-1);
8899 /* called manually thru timer to implement repeated button action w/hold-down */
8900 - (instancetype)repeatScroll: (NSTimer *)scrollEntry
8902 NSEvent *e = [[self window] currentEvent];
8903 NSPoint p = [[self window] mouseLocationOutsideOfEventStream];
8904 BOOL inKnob = [self testPart: p] == NSScrollerKnob;
8906 NSTRACE ("[EmacsScroller repeatScroll:]");
8908 /* clear timer if need be */
8909 if (inKnob || [scroll_repeat_entry timeInterval] == SCROLL_BAR_FIRST_DELAY)
8911 [scroll_repeat_entry invalidate];
8912 [scroll_repeat_entry release];
8913 scroll_repeat_entry = nil;
8919 = [[NSTimer scheduledTimerWithTimeInterval:
8920 SCROLL_BAR_CONTINUOUS_DELAY
8922 selector: @selector (repeatScroll:)
8928 [self sendScrollEventAtLoc: 0 fromEvent: e];
8933 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
8934 mouseDragged events without going into a modal loop. */
8935 - (void)mouseDown: (NSEvent *)e
8938 /* hitPart is only updated AFTER event is passed on */
8939 NSScrollerPart part = [self testPart: [e locationInWindow]];
8940 CGFloat loc, kloc, pos UNINIT;
8943 NSTRACE ("[EmacsScroller mouseDown:]");
8947 case NSScrollerDecrementPage:
8948 last_hit_part = horizontal ? scroll_bar_before_handle : scroll_bar_above_handle; break;
8949 case NSScrollerIncrementPage:
8950 last_hit_part = horizontal ? scroll_bar_after_handle : scroll_bar_below_handle; break;
8951 case NSScrollerDecrementLine:
8952 last_hit_part = horizontal ? scroll_bar_left_arrow : scroll_bar_up_arrow; break;
8953 case NSScrollerIncrementLine:
8954 last_hit_part = horizontal ? scroll_bar_right_arrow : scroll_bar_down_arrow; break;
8955 case NSScrollerKnob:
8956 last_hit_part = horizontal ? scroll_bar_horizontal_handle : scroll_bar_handle; break;
8957 case NSScrollerKnobSlot: /* GNUstep-only */
8958 last_hit_part = scroll_bar_move_ratio; break;
8959 default: /* NSScrollerNoPart? */
8960 fprintf (stderr, "EmacsScroller-mouseDown: unexpected part %ld\n",
8965 if (part == NSScrollerKnob || part == NSScrollerKnobSlot)
8967 /* handle, or on GNUstep possibly slot */
8968 NSEvent *fake_event;
8971 /* compute float loc in slot and mouse offset on knob */
8972 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
8976 length = NSWidth (sr);
8977 loc = ([e locationInWindow].x - NSMinX (sr));
8981 length = NSHeight (sr);
8982 loc = length - ([e locationInWindow].y - NSMinY (sr));
8990 else if (loc >= length)
9000 kr = [self convertRect: [self rectForPart: NSScrollerKnob]
9003 kloc = ([e locationInWindow].x - NSMinX (kr));
9005 kloc = NSHeight (kr) - ([e locationInWindow].y - NSMinY (kr));
9007 last_mouse_offset = kloc;
9009 /* if knob, tell emacs a location offset by knob pos
9010 (to indicate top of handle) */
9011 if (part == NSScrollerKnob)
9012 pos = (loc - last_mouse_offset);
9014 /* else this is a slot click on GNUstep: go straight there */
9017 /* If there are buttons in the scroller area, we need to
9018 recalculate pos as emacs expects the scroller slot to take up
9019 the entire available length. */
9020 if (length != pixel_length)
9021 pos = pos * pixel_length / length;
9023 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
9024 fake_event = [NSEvent mouseEventWithType: NSEventTypeLeftMouseUp
9025 location: [e locationInWindow]
9026 modifierFlags: [e modifierFlags]
9027 timestamp: [e timestamp]
9028 windowNumber: [e windowNumber]
9030 eventNumber: [e eventNumber]
9031 clickCount: [e clickCount]
9032 pressure: [e pressure]];
9033 [super mouseUp: fake_event];
9037 pos = 0; /* ignored */
9039 /* set a timer to repeat, as we can't let superclass do this modally */
9041 = [[NSTimer scheduledTimerWithTimeInterval: SCROLL_BAR_FIRST_DELAY
9043 selector: @selector (repeatScroll:)
9049 if (part != NSScrollerKnob)
9050 [self sendScrollEventAtLoc: pos fromEvent: e];
9054 /* Called as we manually track scroller drags, rather than superclass. */
9055 - (void)mouseDragged: (NSEvent *)e
9061 NSTRACE ("[EmacsScroller mouseDragged:]");
9063 sr = [self convertRect: [self rectForPart: NSScrollerKnobSlot]
9068 length = NSWidth (sr);
9069 loc = ([e locationInWindow].x - NSMinX (sr));
9073 length = NSHeight (sr);
9074 loc = length - ([e locationInWindow].y - NSMinY (sr));
9081 else if (loc >= length + last_mouse_offset)
9083 loc = length + last_mouse_offset;
9086 pos = (loc - last_mouse_offset);
9088 /* If there are buttons in the scroller area, we need to
9089 recalculate pos as emacs expects the scroller slot to take up
9090 the entire available length. */
9091 if (length != pixel_length)
9092 pos = pos * pixel_length / length;
9094 [self sendScrollEventAtLoc: pos fromEvent: e];
9098 - (void)mouseUp: (NSEvent *)e
9100 NSTRACE ("[EmacsScroller mouseUp:]");
9102 if (scroll_repeat_entry)
9104 [scroll_repeat_entry invalidate];
9105 [scroll_repeat_entry release];
9106 scroll_repeat_entry = nil;
9108 last_hit_part = scroll_bar_above_handle;
9112 /* treat scrollwheel events in the bar as though they were in the main window */
9113 - (void) scrollWheel: (NSEvent *)theEvent
9115 NSTRACE ("[EmacsScroller scrollWheel:]");
9117 EmacsView *view = (EmacsView *)FRAME_NS_VIEW (frame);
9118 [view mouseDown: theEvent];
9121 @end /* EmacsScroller */
9124 #ifdef NS_IMPL_GNUSTEP
9125 /* Dummy class to get rid of startup warnings. */
9126 @implementation EmacsDocument
9132 /* ==========================================================================
9134 Font-related functions; these used to be in nsfaces.m
9136 ========================================================================== */
9140 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
9142 struct font *font = XFONT_OBJECT (font_object);
9143 EmacsView *view = FRAME_NS_VIEW (f);
9144 int font_ascent, font_descent;
9147 fontset = fontset_from_font (font_object);
9148 FRAME_FONTSET (f) = fontset;
9150 if (FRAME_FONT (f) == font)
9151 /* This font is already set in frame F. There's nothing more to
9155 FRAME_FONT (f) = font;
9157 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
9158 FRAME_COLUMN_WIDTH (f) = font->average_width;
9159 get_font_ascent_descent (font, &font_ascent, &font_descent);
9160 FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;
9162 /* Compute the scroll bar width in character columns. */
9163 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
9165 int wid = FRAME_COLUMN_WIDTH (f);
9166 FRAME_CONFIG_SCROLL_BAR_COLS (f)
9167 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
9171 int wid = FRAME_COLUMN_WIDTH (f);
9172 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
9175 /* Compute the scroll bar height in character lines. */
9176 if (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) > 0)
9178 int height = FRAME_LINE_HEIGHT (f);
9179 FRAME_CONFIG_SCROLL_BAR_LINES (f)
9180 = (FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) + height - 1) / height;
9184 int height = FRAME_LINE_HEIGHT (f);
9185 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
9188 /* Now make the frame display the given font. */
9189 if (FRAME_NS_WINDOW (f) != 0 && ! [view isFullscreen])
9190 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
9191 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
9198 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
9199 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
9203 ns_xlfd_to_fontname (const char *xlfd)
9204 /* --------------------------------------------------------------------------
9205 Convert an X font name (XLFD) to an NS font name.
9206 Only family is used.
9207 The string returned is temporarily allocated.
9208 -------------------------------------------------------------------------- */
9210 char *name = xmalloc (180);
9214 if (!strncmp (xlfd, "--", 2))
9215 sscanf (xlfd, "--%*[^-]-%179[^-]-", name);
9217 sscanf (xlfd, "-%*[^-]-%179[^-]-", name);
9219 /* stopgap for malformed XLFD input */
9220 if (strlen (name) == 0)
9221 strcpy (name, "Monaco");
9223 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
9224 also uppercase after '-' or ' ' */
9225 name[0] = c_toupper (name[0]);
9226 for (len =strlen (name), i =0; i<len; i++)
9232 name[i+1] = c_toupper (name[i+1]);
9234 else if (name[i] == '_')
9238 name[i+1] = c_toupper (name[i+1]);
9241 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
9242 ret = [[NSString stringWithUTF8String: name] UTF8String];
9249 syms_of_nsterm (void)
9251 NSTRACE ("syms_of_nsterm");
9253 ns_antialias_threshold = 10.0;
9255 /* from 23+ we need to tell emacs what modifiers there are.. */
9256 DEFSYM (Qmodifier_value, "modifier-value");
9257 DEFSYM (Qalt, "alt");
9258 DEFSYM (Qhyper, "hyper");
9259 DEFSYM (Qmeta, "meta");
9260 DEFSYM (Qsuper, "super");
9261 DEFSYM (Qcontrol, "control");
9262 DEFSYM (QUTF8_STRING, "UTF8_STRING");
9264 DEFSYM (Qfile, "file");
9265 DEFSYM (Qurl, "url");
9267 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
9268 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
9269 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
9270 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
9271 Fput (Qcontrol, Qmodifier_value, make_number (ctrl_modifier));
9273 DEFVAR_LISP ("ns-input-file", ns_input_file,
9274 "The file specified in the last NS event.");
9275 ns_input_file =Qnil;
9277 DEFVAR_LISP ("ns-working-text", ns_working_text,
9278 "String for visualizing working composition sequence.");
9279 ns_working_text =Qnil;
9281 DEFVAR_LISP ("ns-input-font", ns_input_font,
9282 "The font specified in the last NS event.");
9283 ns_input_font =Qnil;
9285 DEFVAR_LISP ("ns-input-fontsize", ns_input_fontsize,
9286 "The fontsize specified in the last NS event.");
9287 ns_input_fontsize =Qnil;
9289 DEFVAR_LISP ("ns-input-line", ns_input_line,
9290 "The line specified in the last NS event.");
9291 ns_input_line =Qnil;
9293 DEFVAR_LISP ("ns-input-spi-name", ns_input_spi_name,
9294 "The service name specified in the last NS event.");
9295 ns_input_spi_name =Qnil;
9297 DEFVAR_LISP ("ns-input-spi-arg", ns_input_spi_arg,
9298 "The service argument specified in the last NS event.");
9299 ns_input_spi_arg =Qnil;
9301 DEFVAR_LISP ("ns-alternate-modifier", ns_alternate_modifier,
9302 "This variable describes the behavior of the alternate or option key.\n\
9303 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9305 Set to none means that the alternate / option key is not interpreted by Emacs\n\
9306 at all, allowing it to be used at a lower level for accented character entry.");
9307 ns_alternate_modifier = Qmeta;
9309 DEFVAR_LISP ("ns-right-alternate-modifier", ns_right_alternate_modifier,
9310 "This variable describes the behavior of the right alternate or option key.\n\
9311 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9313 Set to left means be the same key as `ns-alternate-modifier'.\n\
9314 Set to none means that the alternate / option key is not interpreted by Emacs\n\
9315 at all, allowing it to be used at a lower level for accented character entry.");
9316 ns_right_alternate_modifier = Qleft;
9318 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
9319 "This variable describes the behavior of the command key.\n\
9320 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9322 ns_command_modifier = Qsuper;
9324 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
9325 "This variable describes the behavior of the right command key.\n\
9326 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9328 Set to left means be the same key as `ns-command-modifier'.\n\
9329 Set to none means that the command / option key is not interpreted by Emacs\n\
9330 at all, allowing it to be used at a lower level for accented character entry.");
9331 ns_right_command_modifier = Qleft;
9333 DEFVAR_LISP ("ns-control-modifier", ns_control_modifier,
9334 "This variable describes the behavior of the control key.\n\
9335 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9337 ns_control_modifier = Qcontrol;
9339 DEFVAR_LISP ("ns-right-control-modifier", ns_right_control_modifier,
9340 "This variable describes the behavior of the right control key.\n\
9341 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9343 Set to left means be the same key as `ns-control-modifier'.\n\
9344 Set to none means that the control / option key is not interpreted by Emacs\n\
9345 at all, allowing it to be used at a lower level for accented character entry.");
9346 ns_right_control_modifier = Qleft;
9348 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
9349 "This variable describes the behavior of the function key (on laptops).\n\
9350 Set to the symbol control, meta, alt, super, or hyper means it is taken to be\n\
9352 Set to none means that the function key is not interpreted by Emacs at all,\n\
9353 allowing it to be used at a lower level for accented character entry.");
9354 ns_function_modifier = Qnone;
9356 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
9357 "Non-nil (the default) means to render text antialiased.");
9358 ns_antialias_text = Qt;
9360 DEFVAR_LISP ("ns-use-thin-smoothing", ns_use_thin_smoothing,
9361 "Non-nil turns on a font smoothing method that produces thinner strokes.");
9362 ns_use_thin_smoothing = Qnil;
9364 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
9365 "Whether to confirm application quit using dialog.");
9366 ns_confirm_quit = Qnil;
9368 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
9369 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
9370 Only works on Mac OS X 10.6 or later. */);
9371 ns_auto_hide_menu_bar = Qnil;
9373 DEFVAR_BOOL ("ns-use-native-fullscreen", ns_use_native_fullscreen,
9374 doc: /*Non-nil means to use native fullscreen on Mac OS X 10.7 and later.
9375 Nil means use fullscreen the old (< 10.7) way. The old way works better with
9376 multiple monitors, but lacks tool bar. This variable is ignored on
9377 Mac OS X < 10.7. Default is t. */);
9378 ns_use_native_fullscreen = YES;
9379 ns_last_use_native_fullscreen = ns_use_native_fullscreen;
9381 DEFVAR_BOOL ("ns-use-fullscreen-animation", ns_use_fullscreen_animation,
9382 doc: /*Non-nil means use animation on non-native fullscreen.
9383 For native fullscreen, this does nothing.
9384 Default is nil. */);
9385 ns_use_fullscreen_animation = NO;
9387 DEFVAR_BOOL ("ns-use-srgb-colorspace", ns_use_srgb_colorspace,
9388 doc: /*Non-nil means to use sRGB colorspace on Mac OS X 10.7 and later.
9389 Note that this does not apply to images.
9390 This variable is ignored on Mac OS X < 10.7 and GNUstep. */);
9391 ns_use_srgb_colorspace = YES;
9393 DEFVAR_BOOL ("ns-use-mwheel-acceleration",
9394 ns_use_mwheel_acceleration,
9395 doc: /*Non-nil means use macOS's standard mouse wheel acceleration.
9396 This variable is ignored on macOS < 10.7 and GNUstep. Default is t. */);
9397 ns_use_mwheel_acceleration = YES;
9399 DEFVAR_LISP ("ns-mwheel-line-height", ns_mwheel_line_height,
9400 doc: /*The number of pixels touchpad scrolling considers one line.
9401 Nil or a non-number means use the default frame line height.
9402 This variable is ignored on macOS < 10.7 and GNUstep. Default is nil. */);
9403 ns_mwheel_line_height = Qnil;
9405 DEFVAR_BOOL ("ns-use-mwheel-momentum", ns_use_mwheel_momentum,
9406 doc: /*Non-nil means mouse wheel scrolling uses momentum.
9407 This variable is ignored on macOS < 10.7 and GNUstep. Default is t. */);
9408 ns_use_mwheel_momentum = YES;
9410 /* TODO: move to common code */
9411 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
9412 doc: /* Which toolkit scroll bars Emacs uses, if any.
9413 A value of nil means Emacs doesn't use toolkit scroll bars.
9414 With the X Window system, the value is a symbol describing the
9415 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
9416 With MS Windows or Nextstep, the value is t. */);
9417 Vx_toolkit_scroll_bars = Qt;
9419 DEFVAR_BOOL ("x-use-underline-position-properties",
9420 x_use_underline_position_properties,
9421 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
9422 A value of nil means ignore them. If you encounter fonts with bogus
9423 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
9424 to 4.1, set this to nil. */);
9425 x_use_underline_position_properties = 0;
9427 DEFVAR_BOOL ("x-underline-at-descent-line",
9428 x_underline_at_descent_line,
9429 doc: /* Non-nil means to draw the underline at the same place as the descent line.
9430 (If `line-spacing' is in effect, that moves the underline lower by
9432 A value of nil means to draw the underline according to the value of the
9433 variable `x-use-underline-position-properties', which is usually at the
9434 baseline level. The default value is nil. */);
9435 x_underline_at_descent_line = 0;
9437 /* Tell Emacs about this window system. */
9438 Fprovide (Qns, Qnil);
9440 DEFSYM (Qcocoa, "cocoa");
9441 DEFSYM (Qgnustep, "gnustep");
9443 #ifdef NS_IMPL_COCOA
9444 Fprovide (Qcocoa, Qnil);
9447 Fprovide (Qgnustep, Qnil);