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