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