1 /* NeXT/Open/GNUstep / MacOSX communication module.
2 Copyright (C) 1989, 1993, 1994, 2005, 2006, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 Originally by Carl Edman
22 Updated by Christian Limpach (chris@nice.ch)
23 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
24 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
25 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
28 /* This should be the first include, as it may set up #defines affecting
29 interpretation of even the system includes. */
33 #include
<sys
/types.h
>
38 #include "blockinput.h"
39 #include "sysselect.h"
42 #include "character.h"
44 #include "composite.h"
47 #include "termhooks.h"
58 int term_trace_num
= 0;
59 #define NSTRACE
(x
) fprintf (stderr
, "
%s:%d: [%d] " #x "\n", \
60 __FILE__
, __LINE__
, ++term_trace_num
)
66 /* ==========================================================================
70 ========================================================================== */
72 /* Convert a symbol indexed with an NSxxx value to a value as defined
73 in keyboard.c (lispy_function_key). I hope this is a correct way
75 static unsigned convert_ns_to_X_keysym
[] =
77 NSHomeFunctionKey
, 0x50,
78 NSLeftArrowFunctionKey
, 0x51,
79 NSUpArrowFunctionKey
, 0x52,
80 NSRightArrowFunctionKey
, 0x53,
81 NSDownArrowFunctionKey
, 0x54,
82 NSPageUpFunctionKey
, 0x55,
83 NSPageDownFunctionKey
, 0x56,
84 NSEndFunctionKey
, 0x57,
85 NSBeginFunctionKey
, 0x58,
86 NSSelectFunctionKey
, 0x60,
87 NSPrintFunctionKey
, 0x61,
88 NSExecuteFunctionKey
, 0x62,
89 NSInsertFunctionKey
, 0x63,
90 NSUndoFunctionKey
, 0x65,
91 NSRedoFunctionKey
, 0x66,
92 NSMenuFunctionKey
, 0x67,
93 NSFindFunctionKey
, 0x68,
94 NSHelpFunctionKey
, 0x6A,
95 NSBreakFunctionKey
, 0x6B,
97 NSF1FunctionKey
, 0xBE,
98 NSF2FunctionKey
, 0xBF,
99 NSF3FunctionKey
, 0xC0,
100 NSF4FunctionKey
, 0xC1,
101 NSF5FunctionKey
, 0xC2,
102 NSF6FunctionKey
, 0xC3,
103 NSF7FunctionKey
, 0xC4,
104 NSF8FunctionKey
, 0xC5,
105 NSF9FunctionKey
, 0xC6,
106 NSF10FunctionKey
, 0xC7,
107 NSF11FunctionKey
, 0xC8,
108 NSF12FunctionKey
, 0xC9,
109 NSF13FunctionKey
, 0xCA,
110 NSF14FunctionKey
, 0xCB,
111 NSF15FunctionKey
, 0xCC,
113 NSBackspaceCharacter
, 0x08, /* 8: Not on some KBs. */
114 NSDeleteCharacter
, 0xFF, /* 127: Big 'delete' key upper right. */
115 NSDeleteFunctionKey
, 0x9F, /* 63272: Del forw key off main array. */
117 NSTabCharacter
, 0x09,
118 0x19, 0x09, /* left tab->regular since pass shift */
119 NSCarriageReturnCharacter
, 0x0D,
120 NSNewlineCharacter
, 0x0D,
121 NSEnterCharacter
, 0x8D,
123 0x1B, 0x1B /* escape */
127 /* Lisp communications */
128 Lisp_Object ns_input_file
, ns_input_font
, ns_input_fontsize
, ns_input_line
;
129 Lisp_Object ns_input_color
, ns_input_text
, ns_working_text
;
130 Lisp_Object ns_input_spi_name
, ns_input_spi_arg
;
131 Lisp_Object Vx_toolkit_scroll_bars
;
132 static Lisp_Object Qmodifier_value
;
133 /* TODO: unsure why these defined in term files, anyway we need in keymap.c */
134 Lisp_Object Qalt
, Qcontrol
, Qhyper
, Qmeta
, Qsuper
;
135 extern Lisp_Object Qcursor_color
, Qcursor_type
, Qns
;
138 EmacsPrefsController
*prefsController
;
140 /* Preferences equivalent to those set by X resources under X are managed
141 through the OpenStep defaults system. These pertain to behavior of the
142 graphical interface components. The one difference from X is that the
143 values below are SET when the user chooses save-options. This makes
144 things easier for users, but sometimes violates expectations when some
145 user-set options appear when running under -q/Q. Therefore we depart
146 from X behavior and refuse to read defaults when started under these
149 /* Set in emacs.c. */
152 /* Specifies which emacs modifier should be generated when NS receives
153 the Alternate modifer. May be Qnone or any of the modifier lisp symbols. */
154 Lisp_Object ns_alternate_modifier
;
156 /* Specifies which emacs modifier should be generated when NS receives
157 the Command modifer. May be any of the modifier lisp symbols. */
158 Lisp_Object ns_command_modifier
;
160 /* Specifies which emacs modifier should be generated when NS receives
161 the Control modifer. May be any of the modifier lisp symbols. */
162 Lisp_Object ns_control_modifier
;
164 /* Specifies which emacs modifier should be generated when NS receives
165 the Function modifer (laptops). May be any of the modifier lisp symbols. */
166 Lisp_Object ns_function_modifier
;
168 /* A floating point value specifying vertical stretch (positive) or shrink
169 (negative) of text line spacing. Zero means default spacing.
170 YES indicates 0.5, NO indicates 0.0. */
171 Lisp_Object ns_expand_space
;
173 /* Control via default 'GSFontAntiAlias' on OS X and GNUstep. */
174 Lisp_Object ns_antialias_text
;
176 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
177 the maximum font size to NOT antialias. On GNUstep there is currently
178 no way to control this behavior. */
179 float ns_antialias_threshold
;
181 /* Controls use of an undocumented CG function to do Quickdraw-style font
182 smoothing (less heavy) instead of regular Quartz smoothing. */
183 Lisp_Object ns_use_qd_smoothing
;
185 /* Used to pick up AppleHighlightColor on OS X */
186 Lisp_Object ns_use_system_highlight_color
;
187 NSString
*ns_selection_color
;
189 /* Confirm on exit. */
190 Lisp_Object ns_confirm_quit
;
192 NSArray
*ns_send_types
=0, *ns_return_types
=0, *ns_drag_types
=0;
194 /* Display variables */
195 struct ns_display_info
*x_display_list
; /* Chain of existing displays */
196 Lisp_Object ns_display_name_list
;
197 long context_menu_value
= 0;
200 NSPoint last_mouse_motion_position
;
201 static NSRect last_mouse_glyph
;
202 static unsigned
long last_mouse_movement_time
= 0;
203 static Lisp_Object last_mouse_motion_frame
;
204 static EmacsScroller
*last_mouse_scroll_bar
= nil
;
205 static
struct frame
*ns_updating_frame
;
206 static NSView
*focus_view
= NULL;
207 static int ns_window_num
=0;
209 static BOOL gsaved
= NO
;
210 BOOL ns_in_resize
= NO
;
211 static BOOL ns_fake_keydown
= NO
;
212 int ns_tmp_flags
; /* FIXME */
213 struct nsfont_info
*ns_tmp_font
; /* FIXME */
214 /*static int debug_lock = 0; */
217 /* This undocumented Quartz function controls how fonts are anti-aliased.
218 (Found from code in Mac wxWindows impl, discovered by running `nm' on
220 Mode 0 is normal anti-aliasing, mode 1 is no anti-aliasing, and mode 2 is
221 4-bit pixel-aligned anti-aliasing (the old QuickDraw standard). */
222 extern void CGContextSetFontRenderingMode
(CGContextRef cg
, int v
);
227 static BOOL send_appdefined
= YES
;
228 static NSEvent
*last_appdefined_event
= 0;
229 static NSTimer
*timed_entry
= 0;
230 static NSTimer
*fd_entry
= nil
;
231 static NSTimer
*scroll_repeat_entry
= nil
;
232 static fd_set select_readfds
, t_readfds
;
233 static
struct timeval select_timeout
;
234 static int select_nfds
;
235 static NSAutoreleasePool
*outerpool
;
236 static BOOL ns_shutdown_properly
= NO
;
237 static
struct input_event
*emacs_event
= NULL;
238 static
struct input_event
*q_event_ptr
= NULL;
239 static int n_emacs_events_pending
= 0;
240 static NSMutableArray
*ns_pending_files
, *ns_pending_service_names
,
241 *ns_pending_service_args
;
242 static BOOL inNsSelect
= 0;
244 /* Convert modifiers in a NeXTSTEP event to emacs style modifiers. */
245 #define NS_FUNCTION_KEY_MASK
0x800000
246 #define EV_MODIFIERS
(e
) \
247 ((([e modifierFlags
] & NSHelpKeyMask
) ? \
248 hyper_modifier
: 0) \
249 |
(([e modifierFlags
] & NSAlternateKeyMask
) ? \
250 parse_solitary_modifier
(ns_alternate_modifier
) : 0) \
251 |
(([e modifierFlags
] & NSShiftKeyMask
) ? \
252 shift_modifier
: 0) \
253 |
(([e modifierFlags
] & NSControlKeyMask
) ? \
254 parse_solitary_modifier
(ns_control_modifier
) : 0) \
255 |
(([e modifierFlags
] & NS_FUNCTION_KEY_MASK
) ? \
256 parse_solitary_modifier
(ns_function_modifier
) : 0) \
257 |
(([e modifierFlags
] & NSCommandKeyMask
) ? \
258 parse_solitary_modifier
(ns_command_modifier
):0))
260 #define EV_UDMODIFIERS
(e
) \
261 ((([e
type] == NSLeftMouseDown
) ? down_modifier
: 0) \
262 |
(([e
type] == NSRightMouseDown
) ? down_modifier
: 0) \
263 |
(([e
type] == NSOtherMouseDown
) ? down_modifier
: 0) \
264 |
(([e
type] == NSLeftMouseDragged
) ? down_modifier
: 0) \
265 |
(([e
type] == NSRightMouseDragged
) ? down_modifier
: 0) \
266 |
(([e
type] == NSOtherMouseDragged
) ? down_modifier
: 0) \
267 |
(([e
type] == NSLeftMouseUp
) ? up_modifier
: 0) \
268 |
(([e
type] == NSRightMouseUp
) ? up_modifier
: 0) \
269 |
(([e
type] == NSOtherMouseUp
) ? up_modifier
: 0))
271 #define EV_BUTTON
(e
) \
272 ((([e
type] == NSLeftMouseDown
) ||
([e
type] == NSLeftMouseUp
)) ?
0 : \
273 (([e
type] == NSRightMouseDown
) ||
([e
type] == NSRightMouseUp
)) ?
2 : \
274 [e buttonNumber
] - 1)
276 /* Convert the time field to a timestamp in milliseconds. */
277 #ifdef NS_IMPL_GNUSTEP
278 /* Apple says timestamp is in seconds, but GNUstep seems to be returning msec */
279 #define EV_TIMESTAMP
(e
) ([e timestamp
])
281 #define EV_TIMESTAMP
(e
) ([e timestamp
] * 1000)
282 #endif
/* not gnustep */
284 /* This is a piece of code which is common to all the event handling
285 methods. Maybe it should even be a function. */
286 #define EV_TRAILER
(e
) \
288 XSETFRAME
(emacs_event
->frame_or_window
, emacsframe
); \
289 if (e
) emacs_event
->timestamp
= EV_TIMESTAMP
(e
); \
290 n_emacs_events_pending
++; \
291 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
); \
292 EVENT_INIT
(*emacs_event
); \
293 ns_send_appdefined
(-1); \
296 void x_set_cursor_type
(struct frame
*, Lisp_Object
, Lisp_Object
);
298 /* TODO: get rid of need for these forward declarations */
299 static void ns_condemn_scroll_bars
(struct frame
*f
);
300 static void ns_judge_scroll_bars
(struct frame
*f
);
301 void x_set_frame_alpha
(struct frame
*f
);
303 /* unused variables needed for compatibility reasons */
304 int x_use_underline_position_properties
, x_underline_at_descent_line
;
305 /* FIXME: figure out what to do with underline_minimum_offset. */
308 /* ==========================================================================
312 ========================================================================== */
316 append2
(Lisp_Object list
, Lisp_Object item
)
317 /* --------------------------------------------------------------------------
318 Utility to append to a list
319 -------------------------------------------------------------------------- */
321 Lisp_Object array
[2];
323 array
[1] = Fcons
(item
, Qnil
);
324 return Fnconc
(2, &array
[0]);
330 /* --------------------------------------------------------------------------
331 Used to allow emacs to find its resources under Emacs.app
332 Called from emacs.c at startup.
333 -------------------------------------------------------------------------- */
335 NSBundle
*bundle
= [NSBundle mainBundle
];
336 NSString
*binDir
= [bundle bundlePath
], *resourceDir
= [bundle resourcePath
];
337 NSString
*resourcePath
, *resourcePaths
;
339 BOOL onWindows
= NO
; /* how do I determine this? */
340 NSString
*pathSeparator
= onWindows ? @"
;"
: @"
:"
;
341 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
343 /*NSLog (@"ns_init_paths: '%@'\n%@\n", [[NSBundle mainBundle] bundlePath], [[NSBundle mainBundle] resourcePath]); */
345 /* get bindir from base */
346 range
= [resourceDir rangeOfString
: @"Contents"
];
347 if (range.location
!= NSNotFound
)
349 binDir
= [binDir stringByAppendingPathComponent
: @"Contents"
];
351 binDir
= [binDir stringByAppendingPathComponent
: @"MacOS"
];
355 /* the following based on Andrew Choi's init_mac_osx_environment () */
356 if (!getenv ("EMACSLOADPATH"
))
358 NSArray
*paths
= [resourceDir stringsByAppendingPaths
:
359 [NSArray arrayWithObjects
:
360 @"site
-lisp"
, @"lisp"
, @"leim"
, nil
]];
361 NSEnumerator
*pathEnum
= [paths objectEnumerator
];
363 while (resourcePath
= [pathEnum nextObject
])
365 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
368 if ([resourcePaths
length] > 0)
370 = [resourcePaths stringByAppendingString
: pathSeparator
];
372 = [resourcePaths stringByAppendingString
: resourcePath
];
375 if ([resourcePaths
length] > 0)
376 setenv
("EMACSLOADPATH"
, [resourcePaths UTF8String
], 1);
377 /*NSLog (@"loadPath: '%s'\n", resourcePaths); */
380 if (!getenv ("EMACSPATH"
))
382 NSArray
*paths
= [binDir stringsByAppendingPaths
:
383 [NSArray arrayWithObjects
: @"bin"
,
385 NSEnumerator
*pathEnum
= [paths objectEnumerator
];
387 while (resourcePath
= [pathEnum nextObject
])
389 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
392 if ([resourcePaths
length] > 0)
394 = [resourcePaths stringByAppendingString
: pathSeparator
];
396 = [resourcePaths stringByAppendingString
: resourcePath
];
399 if ([resourcePaths
length] > 0)
400 setenv
("EMACSPATH"
, [resourcePaths UTF8String
], 1);
403 resourcePath
= [resourceDir stringByAppendingPathComponent
: @"etc"
];
404 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
408 if (!getenv ("EMACSDATA"
))
409 setenv
("EMACSDATA"
, [resourcePath UTF8String
], 1);
410 if (!getenv ("EMACSDOC"
))
411 setenv
("EMACSDOC"
, [resourcePath UTF8String
], 1);
415 if (!getenv ("INFOPATH"
))
417 resourcePath
= [resourceDir stringByAppendingPathComponent
: @"
info"
];
418 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
420 setenv
("INFOPATH"
, [resourcePath UTF8String
], 1);
426 timeval_subtract
(struct timeval
*result
, struct timeval x
, struct timeval y
)
427 /* --------------------------------------------------------------------------
428 Subtract the `struct timeval' values X and Y, storing the result in RESULT.
429 Return 1 if the difference is negative, otherwise 0.
430 -------------------------------------------------------------------------- */
432 /* Perform the carry for the later subtraction by updating y.
433 This is safer because on some systems
434 the tv_sec member is unsigned. */
435 if (x.tv_usec
< y.tv_usec
)
437 int nsec
= (y.tv_usec
- x.tv_usec
) / 1000000 + 1;
438 y.tv_usec
-= 1000000 * nsec
;
441 if (x.tv_usec
- y.tv_usec
> 1000000)
443 int nsec
= (y.tv_usec
- x.tv_usec
) / 1000000;
444 y.tv_usec
+= 1000000 * nsec
;
448 /* Compute the time remaining to wait. tv_usec is certainly positive. */
449 result
->tv_sec
= x.tv_sec
- y.tv_sec
;
450 result
->tv_usec
= x.tv_usec
- y.tv_usec
;
452 /* Return indication of whether the result should be considered negative. */
453 return x.tv_sec
< y.tv_sec
;
457 ns_timeout
(int usecs
)
458 /* --------------------------------------------------------------------------
459 Blocking timer utility used by ns_ring_bell
460 -------------------------------------------------------------------------- */
462 struct timeval wakeup
;
464 EMACS_GET_TIME
(wakeup
);
466 /* Compute time to wait until, propagating carry from usecs. */
467 wakeup.tv_usec
+= usecs
;
468 wakeup.tv_sec
+= (wakeup.tv_usec
/ 1000000);
469 wakeup.tv_usec
%= 1000000;
471 /* Keep waiting until past the time wakeup. */
474 struct timeval timeout
;
476 EMACS_GET_TIME
(timeout
);
478 /* In effect, timeout = wakeup - timeout.
479 Break if result would be negative. */
480 if (timeval_subtract
(&timeout
, wakeup
, timeout
))
483 /* Try to wait that long--but we might wake up sooner. */
484 select
(0, NULL, NULL, NULL, &timeout
);
490 ns_release_object
(void
*obj
)
491 /* --------------------------------------------------------------------------
492 Release an object (callable from C)
493 -------------------------------------------------------------------------- */
500 ns_retain_object
(void
*obj
)
501 /* --------------------------------------------------------------------------
502 Retain an object (callable from C)
503 -------------------------------------------------------------------------- */
510 ns_alloc_autorelease_pool
()
511 /* --------------------------------------------------------------------------
512 Allocate a pool for temporary objects (callable from C)
513 -------------------------------------------------------------------------- */
515 return [[NSAutoreleasePool alloc
] init
];
520 ns_release_autorelease_pool
(void
*pool
)
521 /* --------------------------------------------------------------------------
522 Free a pool and temporary objects it refers to (callable from C)
523 -------------------------------------------------------------------------- */
525 ns_release_object
(pool
);
530 /* ==========================================================================
532 Focus (clipping) and screen update
534 ========================================================================== */
537 ns_resize_handle_rect
(NSWindow
*window
)
539 NSRect r
= [window frame
];
540 r.origin.x
= r.
size.width
- RESIZE_HANDLE_SIZE
;
542 r.
size.width
= r.
size.height
= RESIZE_HANDLE_SIZE
;
548 ns_update_begin
(struct frame
*f
)
549 /* --------------------------------------------------------------------------
550 Prepare for a grouped sequence of drawing calls
551 external (RIF) call; whole frame, called before update_window_begin
552 -------------------------------------------------------------------------- */
554 NSView
*view = FRAME_NS_VIEW
(f
);
555 NSTRACE
(ns_update_begin
);
557 ns_updating_frame
= f
;
560 #ifdef NS_IMPL_GNUSTEP
561 uRect
= NSMakeRect
(0, 0, 0, 0);
567 ns_update_window_begin
(struct window
*w
)
568 /* --------------------------------------------------------------------------
569 Prepare for a grouped sequence of drawing calls
570 external (RIF) call; for one window, called after update_begin
571 -------------------------------------------------------------------------- */
573 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
574 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
575 NSTRACE
(ns_update_window_begin
);
578 set_output_cursor
(&w
->cursor
);
582 if (f
== dpyinfo
->mouse_face_mouse_frame
)
584 /* Don't do highlighting for mouse motion during the update. */
585 dpyinfo
->mouse_face_defer
= 1;
587 /* If the frame needs to be redrawn,
588 simply forget about any prior mouse highlighting. */
589 if (FRAME_GARBAGED_P
(f
))
590 dpyinfo
->mouse_face_window
= Qnil
;
592 /* (further code for mouse faces ifdef'd out in other terms elided) */
600 ns_update_window_end
(struct window
*w
, int cursor_on_p
,
601 int mouse_face_overwritten_p
)
602 /* --------------------------------------------------------------------------
603 Finished a grouped sequence of drawing calls
604 external (RIF) call; for one window called before update_end
605 -------------------------------------------------------------------------- */
607 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(XFRAME
(w
->frame
));
609 /* note: this fn is nearly identical in all terms */
610 if (!w
->pseudo_window_p
)
615 display_and_set_cursor
(w
, 1,
616 output_cursor.hpos
, output_cursor.vpos
,
617 output_cursor.x
, output_cursor.y
);
619 if (draw_window_fringes
(w
, 1))
620 x_draw_vertical_border
(w
);
625 /* If a row with mouse-face was overwritten, arrange for
626 frame_up_to_date to redisplay the mouse highlight. */
627 if (mouse_face_overwritten_p
)
629 dpyinfo
->mouse_face_beg_row
= dpyinfo
->mouse_face_beg_col
= -1;
630 dpyinfo
->mouse_face_end_row
= dpyinfo
->mouse_face_end_col
= -1;
631 dpyinfo
->mouse_face_window
= Qnil
;
634 updated_window
= NULL;
635 NSTRACE
(update_window_end
);
640 ns_update_end
(struct frame
*f
)
641 /* --------------------------------------------------------------------------
642 Finished a grouped sequence of drawing calls
643 external (RIF) call; for whole frame, called after update_window_end
644 -------------------------------------------------------------------------- */
646 NSView
*view = FRAME_NS_VIEW
(f
);
648 /* if (f == FRAME_NS_DISPLAY_INFO (f)->mouse_face_mouse_frame) */
649 FRAME_NS_DISPLAY_INFO
(f
)->mouse_face_defer
= 0;
653 #ifdef NS_IMPL_GNUSTEP
654 /* trigger flush only in the rectangle we tracked as being drawn */
655 [view unlockFocusNeedsFlush
: NO
];
656 /*fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", uRect.origin.x, uRect.origin.y, uRect.size.width, uRect.size.height); */
657 [view lockFocusInRect
: uRect
];
661 [[view window
] flushWindow
];
664 ns_updating_frame
= NULL;
665 NSTRACE
(ns_update_end
);
670 ns_flush
(struct frame
*f
)
671 /* --------------------------------------------------------------------------
673 NS impl is no-op since currently we flush in ns_update_end and elsewhere
674 -------------------------------------------------------------------------- */
681 ns_focus
(struct frame
*f
, NSRect
*r
, int n
)
682 /* --------------------------------------------------------------------------
683 Internal: Focus on given frame. During small local updates this is used to
684 draw, however during large updates, ns_update_begin and ns_update_end are
685 called to wrap the whole thing, in which case these calls are stubbed out.
686 Except, on GNUstep, we accumulate the rectangle being drawn into, because
687 the back end won't do this automatically, and will just end up flushing
689 -------------------------------------------------------------------------- */
691 // NSTRACE
(ns_focus
);
692 #ifdef NS_IMPL_GNUSTEP
695 u
= NSUnionRect
(r
[0], r
[1]);
700 fprintf (stderr, "focus: %d", c++);
701 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
702 fprintf (stderr, "\n"); */
704 if (f
!= ns_updating_frame
)
706 NSView
*view = FRAME_NS_VIEW
(f
);
707 if (view != focus_view
)
709 if (focus_view
!= NULL)
711 [focus_view unlockFocus
];
712 [[focus_view window
] flushWindow
];
717 #ifdef NS_IMPL_GNUSTEP
718 r ?
[view lockFocusInRect
: u
] : [view lockFocus
];
723 /*if (view) debug_lock++; */
725 #ifdef NS_IMPL_GNUSTEP
728 /* more than one rect being drawn into */
731 [view unlockFocus
]; /* add prev rect to redraw list */
732 [view lockFocusInRect
: u
]; /* focus for draw in new rect */
737 #ifdef NS_IMPL_GNUSTEP
740 /* in batch mode, but in GNUstep must still track rectangles explicitly */
741 uRect
= (r ? NSUnionRect
(uRect
, u
) : [FRAME_NS_VIEW
(f
) visibleRect
]);
748 [[NSGraphicsContext currentContext
] saveGraphicsState
];
750 NSRectClipList
(r
, 2);
759 ns_unfocus
(struct frame
*f
)
760 /* --------------------------------------------------------------------------
761 Internal: Remove focus on given frame
762 -------------------------------------------------------------------------- */
764 // NSTRACE
(ns_unfocus
);
768 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
772 if (f
!= ns_updating_frame
)
774 if (focus_view
!= NULL)
776 [focus_view unlockFocus
];
777 [[focus_view window
] flushWindow
];
786 ns_clip_to_row
(struct window
*w
, struct glyph_row
*row
, int area
, BOOL gc
)
787 /* --------------------------------------------------------------------------
788 Internal (but parallels other terms): Focus drawing on given row
789 -------------------------------------------------------------------------- */
791 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
793 int window_x
, window_y
, window_width
;
795 window_box
(w
, area
, &window_x
, &window_y
, &window_width
, 0);
797 clip_rect.origin.x
= window_x
- FRAME_INTERNAL_BORDER_WIDTH
(f
);
798 clip_rect.origin.y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, row
->y
));
799 clip_rect.origin.y
= max (clip_rect.origin.y
, window_y
);
800 clip_rect.
size.width
= window_width
+ 2 * FRAME_INTERNAL_BORDER_WIDTH
(f
);
801 clip_rect.
size.height
= row
->visible_height
;
803 /* allow a full-height row at the top when requested
804 (used to draw fringe all the way through internal border area) */
805 if (gc
&& clip_rect.origin.y
< 5)
807 clip_rect.origin.y
-= FRAME_INTERNAL_BORDER_WIDTH
(f
);
808 clip_rect.
size.height
+= FRAME_INTERNAL_BORDER_WIDTH
(f
);
811 /* likewise at bottom */
813 FRAME_PIXEL_HEIGHT
(f
) - (clip_rect.origin.y
+ clip_rect.
size.height
) < 5)
814 clip_rect.
size.height
+= FRAME_INTERNAL_BORDER_WIDTH
(f
);
816 ns_focus
(f
, &clip_rect
, 1);
822 /* --------------------------------------------------------------------------
824 -------------------------------------------------------------------------- */
826 NSTRACE
(ns_ring_bell
);
829 NSAutoreleasePool
*pool
;
830 struct frame
*frame
= SELECTED_FRAME
();
834 pool
= [[NSAutoreleasePool alloc
] init
];
836 view = FRAME_NS_VIEW
(frame
);
840 NSPoint dim
= NSMakePoint
(128, 128);
843 r.origin.x
+= (r.
size.width
- dim.x
) / 2;
844 r.origin.y
+= (r.
size.height
- dim.y
) / 2;
845 r.
size.width
= dim.x
;
846 r.
size.height
= dim.y
;
847 /* XXX: cacheImageInRect under GNUstep does not account for
848 offset in x_set_window_size, so overestimate (4 fine on Cocoa) */
849 surr
= NSInsetRect
(r
, -10, -10);
850 ns_focus
(frame
, &surr
, 1);
851 [[view window
] cacheImageInRect
: surr
];
852 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
853 (FRAME_DEFAULT_FACE
(frame
)), frame
) set];
855 [[view window
] flushWindow
];
857 [[view window
] restoreCachedImage
];
858 [[view window
] flushWindow
];
872 ns_reset_terminal_modes
(struct terminal *terminal)
873 /* Externally called as hook */
875 NSTRACE
(ns_reset_terminal_modes
);
879 ns_set_terminal_modes
(struct terminal *terminal)
880 /* Externally called as hook */
882 NSTRACE
(ns_set_terminal_modes
);
887 /* ==========================================================================
889 Frame / window manager related functions
891 ========================================================================== */
895 ns_raise_frame
(struct frame
*f
)
896 /* --------------------------------------------------------------------------
897 Bring window to foreground and make it active
898 -------------------------------------------------------------------------- */
900 NSView
*view = FRAME_NS_VIEW
(f
);
903 [[view window
] makeKeyAndOrderFront
: NSApp
];
909 ns_lower_frame
(struct frame
*f
)
910 /* --------------------------------------------------------------------------
912 -------------------------------------------------------------------------- */
914 NSView
*view = FRAME_NS_VIEW
(f
);
917 [[view window
] orderBack
: NSApp
];
923 ns_frame_raise_lower
(struct frame
*f
, int raise
)
924 /* --------------------------------------------------------------------------
926 -------------------------------------------------------------------------- */
928 NSTRACE
(ns_frame_raise_lower
);
938 ns_frame_rehighlight
(struct frame
*frame
)
939 /* --------------------------------------------------------------------------
940 External (hook): called on things like window switching within frame
941 -------------------------------------------------------------------------- */
943 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(frame
);
944 struct frame
*old_highlight
= dpyinfo
->x_highlight_frame
;
946 NSTRACE
(ns_frame_rehighlight
);
947 if (dpyinfo
->x_focus_frame
)
949 dpyinfo
->x_highlight_frame
950 = (FRAMEP
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
951 ? XFRAME
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
952 : dpyinfo
->x_focus_frame
);
953 if (!FRAME_LIVE_P
(dpyinfo
->x_highlight_frame
))
955 FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
) = Qnil
;
956 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
;
960 dpyinfo
->x_highlight_frame
= 0;
962 if (dpyinfo
->x_highlight_frame
&&
963 dpyinfo
->x_highlight_frame
!= old_highlight
)
967 x_update_cursor
(old_highlight
, 1);
968 x_set_frame_alpha
(old_highlight
);
970 if (dpyinfo
->x_highlight_frame
)
972 x_update_cursor
(dpyinfo
->x_highlight_frame
, 1);
973 x_set_frame_alpha
(dpyinfo
->x_highlight_frame
);
980 x_make_frame_visible
(struct frame
*f
)
981 /* --------------------------------------------------------------------------
982 External: Show the window (X11 semantics)
983 -------------------------------------------------------------------------- */
985 NSTRACE
(x_make_frame_visible
);
986 /* XXX: at some points in past this was not needed, as the only place that
987 called this (frame.c:Fraise_frame ()) also called raise_lower;
988 if this ends up the case again, comment this out again. */
989 if (!FRAME_VISIBLE_P
(f
))
995 x_make_frame_invisible
(struct frame
*f
)
996 /* --------------------------------------------------------------------------
997 External: Hide the window (X11 semantics)
998 -------------------------------------------------------------------------- */
1000 NSView
* view = FRAME_NS_VIEW
(f
);
1001 NSTRACE
(x_make_frame_invisible
);
1003 [[view window
] orderOut
: NSApp
];
1008 x_iconify_frame
(struct frame
*f
)
1009 /* --------------------------------------------------------------------------
1010 External: Iconify window
1011 -------------------------------------------------------------------------- */
1013 NSView
* view = FRAME_NS_VIEW
(f
);
1014 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1015 NSTRACE
(x_iconify_frame
);
1018 if (dpyinfo
->x_highlight_frame
== f
)
1019 dpyinfo
->x_highlight_frame
= 0;
1021 if ([[view window
] windowNumber
] <= 0)
1023 /* the window is still deferred. Make it very small, bring it
1024 on screen and order it out. */
1025 NSRect s
= { { 100, 100}, {0, 0} };
1027 t
= [[view window
] frame
];
1028 [[view window
] setFrame
: s display
: NO
];
1029 [[view window
] orderBack
: NSApp
];
1030 [[view window
] orderOut
: NSApp
];
1031 [[view window
] setFrame
: t display
: NO
];
1033 [[view window
] miniaturize
: NSApp
];
1038 x_destroy_window
(struct frame
*f
)
1039 /* --------------------------------------------------------------------------
1040 External: Delete the window
1041 -------------------------------------------------------------------------- */
1043 NSView
*view = FRAME_NS_VIEW
(f
);
1044 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1045 NSTRACE
(x_destroy_window
);
1048 [(EmacsView
*)view setWindowClosing
: YES
]; /* may not have been informed */
1052 free_frame_menubar
(f
);
1054 if (FRAME_FACE_CACHE
(f
))
1055 free_frame_faces
(f
);
1057 if (f
== dpyinfo
->x_focus_frame
)
1058 dpyinfo
->x_focus_frame
= 0;
1059 if (f
== dpyinfo
->x_highlight_frame
)
1060 dpyinfo
->x_highlight_frame
= 0;
1061 if (f
== dpyinfo
->mouse_face_mouse_frame
)
1063 dpyinfo
->mouse_face_beg_row
= dpyinfo
->mouse_face_beg_col
= -1;
1064 dpyinfo
->mouse_face_end_row
= dpyinfo
->mouse_face_end_col
= -1;
1065 dpyinfo
->mouse_face_window
= Qnil
;
1066 dpyinfo
->mouse_face_deferred_gc
= 0;
1067 dpyinfo
->mouse_face_mouse_frame
= 0;
1070 xfree
(f
->output_data.ns
);
1072 [[view window
] close];
1081 x_set_offset
(struct frame
*f
, int xoff
, int yoff
, int change_grav
)
1082 /* --------------------------------------------------------------------------
1083 External: Position the window
1084 -------------------------------------------------------------------------- */
1087 NSView
*view = FRAME_NS_VIEW
(f
);
1089 NSTRACE
(x_set_offset
);
1095 #ifdef NS_IMPL_GNUSTEP
1097 f
->left_pos
= 100; /* don't overlap menu */
1099 if (view != nil
&& (screen
= [[view window
] screen
]))
1100 [[view window
] setFrameTopLeftPoint
:
1101 NSMakePoint
(SCREENMAXBOUND
(f
->left_pos
),
1102 SCREENMAXBOUND
([screen frame
].
size.height
1103 - NS_TOP_POS
(f
)))];
1109 x_set_window_size
(struct frame
*f
, int change_grav
, int cols
, int rows
)
1110 /* --------------------------------------------------------------------------
1111 Adjust window pixel size based on given character grid size
1112 Impl is a bit more complex than other terms, need to do some
1113 internal clipping and also pay attention to screen constraints.
1114 -------------------------------------------------------------------------- */
1116 EmacsView
*view = FRAME_NS_VIEW
(f
);
1117 EmacsToolbar
*toolbar
= [view toolbar
];
1118 NSWindow
*window
= [view window
];
1119 NSScreen
*screen
= [window screen
];
1120 NSRect wr
= [window frame
];
1121 int tb
= FRAME_EXTERNAL_TOOL_BAR
(f
);
1122 int pixelwidth
, pixelheight
;
1123 static int oldRows
, oldCols
, oldFontWidth
, oldFontHeight
;
1125 static
struct frame
*oldF
;
1127 NSTRACE
(x_set_window_size
);
1131 && rows
== oldRows
&& cols
== oldCols
1132 && oldFontWidth
== FRAME_COLUMN_WIDTH
(f
)
1133 && oldFontHeight
== FRAME_LINE_HEIGHT
(f
)
1137 /*fprintf (stderr, "\tsetWindowSize: %d x %d, font size %d x %d\n", cols, rows, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f)); */
1141 check_frame_size
(f
, &rows
, &cols
);
1145 oldFontWidth
= FRAME_COLUMN_WIDTH
(f
);
1146 oldFontHeight
= FRAME_LINE_HEIGHT
(f
);
1149 f
->scroll_bar_actual_width
= NS_SCROLL_BAR_WIDTH
(f
);
1150 compute_fringe_widths
(f
, 0);
1152 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, cols
);
1153 pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, rows
);
1155 /* If we have a change in toolbar display, calculate height */
1157 /* XXX: GNUstep has not yet implemented the first method below, added
1158 in Panther, however the second is incorrect under Cocoa. */
1159 #ifdef NS_IMPL_GNUSTEP
1160 FRAME_NS_TOOLBAR_HEIGHT
(f
)
1161 = NSHeight
([NSWindow frameRectForContentRect
: NSMakeRect
(0, 0, 0, 0)
1162 styleMask
: [window styleMask
]])
1163 - FRAME_NS_TITLEBAR_HEIGHT
(f
);
1165 FRAME_NS_TOOLBAR_HEIGHT
(f
) = 32;
1166 /* actually get wrong result here if toolbar not yet displayed
1167 NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)])
1168 - FRAME_NS_TITLEBAR_HEIGHT (f); */
1171 FRAME_NS_TOOLBAR_HEIGHT
(f
) = 0;
1173 wr.
size.width
= pixelwidth
+ f
->border_width
;
1174 wr.
size.height
= pixelheight
+ FRAME_NS_TITLEBAR_HEIGHT
(f
)
1175 + FRAME_NS_TOOLBAR_HEIGHT
(f
);
1177 /* constrain to screen if we can */
1180 NSSize sz
= [screen visibleFrame
].
size;
1181 NSSize ez
= { wr.
size.width
- sz.width
, wr.
size.height
- sz.height
};
1184 int cr
= ez.width
/ FRAME_COLUMN_WIDTH
(f
) + 1;
1187 wr.
size.width
-= cr
* FRAME_COLUMN_WIDTH
(f
);
1188 pixelwidth
-= cr
* FRAME_COLUMN_WIDTH
(f
);
1192 int rr
= ez.height
/ FRAME_LINE_HEIGHT
(f
) + 1;
1195 wr.
size.height
-= rr
* FRAME_LINE_HEIGHT
(f
);
1196 pixelheight
-= rr
* FRAME_LINE_HEIGHT
(f
);
1198 wr.origin.x
= f
->left_pos
;
1199 wr.origin.y
= [screen frame
].
size.height
- NS_TOP_POS
(f
)
1203 [view setRows
: rows andColumns
: cols
];
1204 [window setFrame
: wr display
: YES
];
1206 /*fprintf (stderr, "\tx_set_window_size %d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight); */
1208 /* This is a trick to compensate for Emacs' managing the scrollbar area
1209 as a fixed number of standard character columns. Instead of leaving
1210 blank space for the extra, we chopped it off above. Now for
1211 left-hand scrollbars, we shift all rendering to the left by the
1212 difference between the real width and Emacs' imagined one. For
1213 right-hand bars, don't worry about it since the extra is never used.
1214 (Obviously doesn't work for vertically split windows tho..) */
1215 NSPoint origin
= FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT
(f
)
1216 ? NSMakePoint
(FRAME_SCROLL_BAR_COLS
(f
) * FRAME_COLUMN_WIDTH
(f
)
1217 - NS_SCROLL_BAR_WIDTH
(f
), 0)
1218 : NSMakePoint
(0, 0);
1219 [view setFrame
: NSMakeRect
(0, 0, pixelwidth
, pixelheight
)];
1220 [view setBoundsOrigin
: origin
];
1222 change_frame_size
(f
, rows
, cols
, 0, 1, 0); /* pretend, delay, safe */
1223 FRAME_PIXEL_WIDTH
(f
) = pixelwidth
;
1224 FRAME_PIXEL_HEIGHT
(f
) = pixelheight
;
1225 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1227 mark_window_cursors_off
(XWINDOW
(f
->root_window
));
1228 cancel_mouse_face
(f
);
1235 /* ==========================================================================
1239 ========================================================================== */
1243 ns_lookup_indexed_color
(unsigned
long idx
, struct frame
*f
)
1245 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1246 return color_table
->colors
[idx
];
1251 ns_index_color
(NSColor
*color
, struct frame
*f
)
1253 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1257 if (!color_table
->colors
)
1259 color_table
->size = NS_COLOR_CAPACITY
;
1260 color_table
->avail
= 1; /* skip idx=0 as marker */
1262 = (NSColor
**)xmalloc
(color_table
->size * sizeof
(NSColor
*));
1263 color_table
->empty_indices
= [[NSMutableSet alloc
] init
];
1266 /* do we already have this color ? */
1269 for (i
= 1; i
< color_table
->avail
; i
++)
1271 if (color_table
->colors
[i
] && [color_table
->colors
[i
] isEqual: color
])
1273 [color_table
->colors
[i
] retain
];
1279 if ([color_table
->empty_indices count
] > 0)
1281 index
= [color_table
->empty_indices anyObject
];
1282 [color_table
->empty_indices removeObject
: index
];
1283 idx
= [index unsignedIntValue
];
1287 if (color_table
->avail
== color_table
->size)
1289 color_table
->size += NS_COLOR_CAPACITY
;
1291 = (NSColor
**)xrealloc
(color_table
->colors
,
1292 color_table
->size * sizeof
(NSColor
*));
1294 idx
= color_table
->avail
++;
1297 color_table
->colors
[idx
] = color
;
1299 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1305 ns_free_indexed_color
(unsigned
long idx
, struct frame
*f
)
1307 struct ns_color_table
*color_table
;
1314 color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1316 if (idx
<= 0 || idx
>= color_table
->size) {
1317 message1
("ns_free_indexed_color
: Color index out of range.
\n"
);
1321 index
= [NSNumber numberWithUnsignedInt
: idx
];
1322 if ([color_table
->empty_indices containsObject
: index
]) {
1323 message1
("ns_free_indexed_color
: attempt to free already freed color.
\n"
);
1327 color
= color_table
->colors
[idx
];
1329 color_table
->colors
[idx
] = nil
;
1330 [color_table
->empty_indices addObject
: [NSNumber numberWithUnsignedInt
: idx
]];
1331 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1336 ns_get_color
(const
char *name
, NSColor
**col
)
1337 /* --------------------------------------------------------------------------
1339 /* --------------------------------------------------------------------------
1340 /* On *Step, we recognize several color formats, in addition to a catalog
1341 of colors found in the file Emacs.clr. Color formats include:
1342 - #rrggbb or RGBrrggbb where rr, gg, bb specify red, green and blue in hex
1343 - ARGBaarrggbb is similar, with aa being the alpha channel (FF = opaque)
1344 - HSVhhssvv and AHSVaahhssvv (or HSB/AHSB) are similar for hue, saturation,
1346 - CMYKccmmyykk is similar for cyan, magenta, yellow, black. */
1348 NSColor
* new
= nil
;
1349 const
char *hex
= NULL;
1350 enum
{ rgb
, argb
, hsv, ahsv
, cmyk
, gray } color_space
;
1351 NSString
*nsname
= [NSString stringWithUTF8String
: name
];
1353 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1356 if ([nsname isEqualToString
: @"ns_selection_color"
])
1358 nsname
= ns_selection_color
;
1359 name
= [ns_selection_color UTF8String
];
1362 if (name
[0] == '0' || name
[0] == '1' || name
[0] == '.')
1365 NSScanner
*scanner
= [NSScanner scannerWithString
: nsname
];
1367 [scanner scanFloat
: &r
];
1368 [scanner scanFloat
: &g
];
1369 [scanner scanFloat
: &b
];
1370 *col
= [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: 1.0];
1375 /* FIXME: emacs seems to downcase everything before passing it here,
1376 which we can work around, except for GRAY, since gray##, where ## is
1377 decimal between 0 and 99, is also an X11 colorname. */
1378 if (name
[0] == '#') /* X11 format */
1383 else if (!memcmp
(name
, "RGB"
, 3) ||
!memcmp
(name
, "rgb"
, 3))
1388 else if (!memcmp
(name
, "ARGB"
, 4) ||
!memcmp
(name
, "argb"
, 4))
1393 else if (!memcmp
(name
, "
HSV"
, 3) ||
!memcmp
(name
, "
hsv"
, 3) ||
1394 !memcmp
(name
, "HSB"
, 3) ||
!memcmp
(name
, "hsb"
, 3))
1399 else if (!memcmp
(name
, "AHSV"
, 4) ||
!memcmp
(name
, "ahsv"
, 4) ||
1400 !memcmp
(name
, "AHSB"
, 4) ||
!memcmp
(name
, "ahsb"
, 4))
1405 else if (!memcmp
(name
, "CMYK"
, 4) ||
!memcmp
(name
, "cmyk"
, 4))
1410 else if (!memcmp
(name
, "
GRAY"
, 4) /*|| !memcmp (name, "gray", 4)*/)
1416 /* Direct colors (hex values) */
1419 unsigned
long long color
= 0;
1420 if (sscanf (hex
, "
%x", &color))
1422 float f1
, f2
, f3
, f4
;
1423 /* Assume it's either 1 byte or 2 per channel... */
1424 if (strlen
(hex
) > 8) {
1425 f1
= ((color
>> 48) & 0xffff) / 65535.0;
1426 f2
= ((color
>> 32) & 0xffff) / 65535.0;
1427 f3
= ((color
>> 16) & 0xffff) / 65535.0;
1428 f4
= ((color
) & 0xffff) / 65535.0;
1430 f1
= ((color
>> 24) & 0xff) / 255.0;
1431 f2
= ((color
>> 16) & 0xff) / 255.0;
1432 f3
= ((color
>> 8) & 0xff) / 255.0;
1433 f4
= ((color
) & 0xff) / 255.0;
1436 switch (color_space
)
1439 *col
= [NSColor colorWithCalibratedRed
: f2
1445 *col
= [NSColor colorWithCalibratedRed
: f2
1451 *col
= [NSColor colorWithCalibratedHue
: f2
1457 *col
= [NSColor colorWithCalibratedHue
: f2
1463 *col
= [NSColor colorWithCalibratedWhite
: f3 alpha
: f4
];
1466 *col
= [NSColor colorWithDeviceCyan
: f1
1473 *col
= [*col colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1479 /* Otherwise, color is expected to be from a list */
1481 NSEnumerator
*lenum
, *cenum
;
1485 #ifdef NS_IMPL_GNUSTEP
1486 /* XXX: who is wrong, the requestor or the implementation? */
1487 if ([nsname compare
: @"Highlight" options
: NSCaseInsensitiveSearch
]
1489 nsname
= @"highlightColor"
;
1492 lenum
= [[NSColorList availableColorLists
] objectEnumerator
];
1493 while ( (clist
= [lenum nextObject
]) && new
== nil
)
1495 cenum
= [[clist allKeys
] objectEnumerator
];
1496 while ( (name
= [cenum nextObject
]) && new
== nil
)
1498 if ([name compare
: nsname
1499 options
: NSCaseInsensitiveSearch
] == NSOrderedSame
)
1500 new
= [clist colorWithKey
: name
];
1506 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1508 NSLog (@"Failed to find color '%@'", nsname); */
1515 ns_get_color_default
(const
char *name
, NSColor
*dflt
)
1516 /* --------------------------------------------------------------------------
1517 Parse a color or use a default value
1518 -------------------------------------------------------------------------- */
1522 if (ns_get_color
(name
, &col
))
1530 ns_lisp_to_color
(Lisp_Object color
, NSColor
**col
)
1531 /* --------------------------------------------------------------------------
1532 Convert a Lisp string object to a NS color
1533 -------------------------------------------------------------------------- */
1535 NSTRACE
(ns_lisp_to_color
);
1536 if (STRINGP
(color
))
1537 return ns_get_color
(SDATA
(color
), col
);
1538 else if (SYMBOLP
(color
))
1539 return ns_get_color
(SDATA
(SYMBOL_NAME
(color
)), col
);
1545 ns_color_to_lisp
(NSColor
*col
)
1546 /* --------------------------------------------------------------------------
1547 Convert a color to a lisp string with the RGB equivalent
1548 -------------------------------------------------------------------------- */
1550 float red
, green
, blue
, alpha
, gray;
1553 NSTRACE
(ns_color_to_lisp
);
1556 if ([[col colorSpaceName
] isEqualToString
: NSNamedColorSpace
])
1558 if ((str
=[[col colorNameComponent
] UTF8String
]))
1561 return build_string
((char *)str
);
1564 [[col colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
]
1565 getRed
: &red green
: &green blue
: &blue alpha
: &alpha
];
1566 if (red
==green
&& red
==blue
)
1568 [[col colorUsingColorSpaceName
: NSCalibratedWhiteColorSpace
]
1569 getWhite
: &gray alpha
: &alpha
];
1570 snprintf
(buf
, sizeof
(buf
), "
GRAY%02.2lx%02.2lx",
1571 lrint
(gray * 0xff), lrint
(alpha
* 0xff));
1573 return build_string
(buf
);
1576 snprintf
(buf
, sizeof
(buf
), "ARGB
%02.2lx%02.2lx%02.2lx%02.2lx",
1578 lrint
(red
*0xff), lrint
(green
*0xff), lrint
(blue
*0xff));
1581 return build_string
(buf
);
1586 ns_query_color
(void
*col
, XColor
*color_def
, int setPixel
)
1587 /* --------------------------------------------------------------------------
1588 Get ARGB values out of NSColor col and put them into color_def.
1589 If setPixel, set the pixel to a concatenated version.
1590 and set color_def pixel to the resulting index.
1591 -------------------------------------------------------------------------- */
1595 [((NSColor
*)col
) getRed
: &r green
: &g blue
: &b alpha
: &a
];
1596 color_def
->red
= r
* 65535;
1597 color_def
->green
= g
* 65535;
1598 color_def
->blue
= b
* 65535;
1600 if (setPixel
== YES
)
1602 = ARGB_TO_ULONG
((int
)(a
*255),
1603 (int
)(r
*255), (int
)(g
*255), (int
)(b
*255));
1608 ns_defined_color
(struct frame
*f
, char *name
, XColor
*color_def
, int alloc
,
1610 /* --------------------------------------------------------------------------
1611 Return 1 if named color found, and set color_def rgb accordingly.
1612 If makeIndex and alloc are nonzero put the color in the color_table,
1613 and set color_def pixel to the resulting index.
1614 If makeIndex is zero, set color_def pixel to ARGB.
1615 Return 0 if not found
1616 -------------------------------------------------------------------------- */
1619 int notFound
= ns_get_color
(name
, &temp
);
1621 NSTRACE
(ns_defined_color
);
1626 if (makeIndex
&& alloc
)
1627 color_def
->pixel
= ns_index_color
(temp
, f
); /* [temp retain]; */
1629 ns_query_color
(temp
, color_def
, !makeIndex
);
1636 ns_get_rgb_color
(struct frame
*f
, float r
, float g
, float b
, float a
)
1637 /* --------------------------------------------------------------------------
1638 return an autoreleased RGB color
1639 -------------------------------------------------------------------------- */
1641 /*static int c = 1; fprintf (stderr, "color request %d\n", c++); */
1642 if (r
< 0.0) r
= 0.0;
1643 else if (r
> 1.0) r
= 1.0;
1644 if (g
< 0.0) g
= 0.0;
1645 else if (g
> 1.0) g
= 1.0;
1646 if (b
< 0.0) b
= 0.0;
1647 else if (b
> 1.0) b
= 1.0;
1648 if (a
< 0.0) a
= 0.0;
1649 else if (a
> 1.0) a
= 1.0;
1650 return (unsigned
long) ns_index_color
(
1651 [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: a
], f
);
1656 x_set_frame_alpha
(struct frame
*f
)
1657 /* --------------------------------------------------------------------------
1658 change the entire-frame transparency
1659 -------------------------------------------------------------------------- */
1661 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1662 EmacsView
*view = FRAME_NS_VIEW
(f
);
1664 double alpha_min
= 1.0;
1666 if (dpyinfo
->x_highlight_frame
== f
)
1667 alpha
= f
->alpha
[0];
1669 alpha
= f
->alpha
[1];
1671 if (FLOATP
(Vframe_alpha_lower_limit
))
1672 alpha_min
= XFLOAT_DATA
(Vframe_alpha_lower_limit
);
1673 else if (INTEGERP
(Vframe_alpha_lower_limit
))
1674 alpha_min
= (XINT
(Vframe_alpha_lower_limit
)) / 100.0;
1678 else if (1.0 < alpha
)
1680 else if (0.0 <= alpha
&& alpha
< alpha_min
&& alpha_min
<= 1.0)
1683 #ifdef NS_IMPL_COCOA
1684 [[view window
] setAlphaValue
: alpha
];
1689 /* ==========================================================================
1693 ========================================================================== */
1697 x_set_mouse_pixel_position
(struct frame
*f
, int pix_x
, int pix_y
)
1698 /* --------------------------------------------------------------------------
1699 Programmatically reposition mouse pointer in pixel coordinates
1700 -------------------------------------------------------------------------- */
1702 NSTRACE
(x_set_mouse_pixel_position
);
1705 /* FIXME: this does not work, and what about GNUstep? */
1706 #ifdef NS_IMPL_COCOA
1707 [FRAME_NS_VIEW
(f
) lockFocus
];
1708 PSsetmouse
((float
)pix_x
, (float
)pix_y
);
1709 [FRAME_NS_VIEW
(f
) unlockFocus
];
1716 x_set_mouse_position
(struct frame
*f
, int h
, int v
)
1717 /* --------------------------------------------------------------------------
1718 Programmatically reposition mouse pointer in character coordinates
1719 -------------------------------------------------------------------------- */
1723 pix_x
= FRAME_COL_TO_PIXEL_X
(f
, h
) + FRAME_COLUMN_WIDTH
(f
) / 2;
1724 pix_y
= FRAME_LINE_TO_PIXEL_Y
(f
, v
) + FRAME_LINE_HEIGHT
(f
) / 2;
1726 if (pix_x
< 0) pix_x
= 0;
1727 if (pix_x
> FRAME_PIXEL_WIDTH
(f
)) pix_x
= FRAME_PIXEL_WIDTH
(f
);
1729 if (pix_y
< 0) pix_y
= 0;
1730 if (pix_y
> FRAME_PIXEL_HEIGHT
(f
)) pix_y
= FRAME_PIXEL_HEIGHT
(f
);
1732 x_set_mouse_pixel_position
(f
, pix_x
, pix_y
);
1737 note_mouse_movement
(struct frame
*frame
, float x
, float y
)
1738 /* ------------------------------------------------------------------------
1739 Called by EmacsView on mouseMovement events. Passes on
1740 to emacs mainstream code if we moved off of a rect of interest
1741 known as last_mouse_glyph.
1742 ------------------------------------------------------------------------ */
1744 // NSTRACE
(note_mouse_movement
);
1746 XSETFRAME
(last_mouse_motion_frame
, frame
);
1748 /* Note, this doesn't get called for enter/leave, since we don't have a
1749 position. Those are taken care of in the corresponding NSView methods. */
1751 /* has movement gone beyond last rect we were tracking? */
1752 if (x
< last_mouse_glyph.origin.x ||
1753 x
>= (last_mouse_glyph.origin.x
+ last_mouse_glyph.
size.width
) ||
1754 y
< last_mouse_glyph.origin.y ||
1755 y
>= (last_mouse_glyph.origin.y
+ last_mouse_glyph.
size.height
))
1757 frame
->mouse_moved
= 1;
1758 note_mouse_highlight
(frame
, x
, y
);
1759 remember_mouse_glyph
(frame
, x
, y
, &last_mouse_glyph
);
1768 ns_mouse_position
(struct frame
**fp
, int insist
, Lisp_Object
*bar_window
,
1769 enum scroll_bar_part
*part
, Lisp_Object
*x
, Lisp_Object
*y
,
1770 unsigned
long *time
)
1771 /* --------------------------------------------------------------------------
1772 External (hook): inform emacs about mouse position and hit parts.
1773 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1774 x & y should be position in the scrollbar (the whole bar, not the handle)
1775 and length of scrollbar respectively
1776 -------------------------------------------------------------------------- */
1781 Lisp_Object frame
, tail
;
1783 struct ns_display_info
*dpyinfo
;
1785 NSTRACE
(ns_mouse_position
);
1789 fprintf (stderr
, "Warning
: ns_mouse_position
() called with
null *fp.
\n"
);
1793 dpyinfo
= FRAME_NS_DISPLAY_INFO
(*fp
);
1797 if (last_mouse_scroll_bar
!= nil
&& insist
== 0)
1799 /* TODO: we do not use this path at the moment because drag events will
1800 go directly to the EmacsScroller. Leaving code in for now. */
1801 [last_mouse_scroll_bar getMouseMotionPart
: (int
*)part window
: bar_window
1803 if (time
) *time
= last_mouse_movement_time
;
1804 last_mouse_scroll_bar
= nil
;
1808 /* Clear the mouse-moved flag for every frame on this display. */
1809 FOR_EACH_FRAME
(tail
, frame
)
1810 if (FRAME_NS_P
(XFRAME
(frame
))
1811 && FRAME_NS_DISPLAY
(XFRAME
(frame
)) == FRAME_NS_DISPLAY
(*fp
))
1812 XFRAME
(frame
)->mouse_moved
= 0;
1814 last_mouse_scroll_bar
= nil
;
1815 if (last_mouse_frame
&& FRAME_LIVE_P
(last_mouse_frame
))
1816 f
= last_mouse_frame
;
1818 f
= dpyinfo
->x_focus_frame ? dpyinfo
->x_focus_frame
1819 : SELECTED_FRAME
();
1821 if (f
&& f
->output_data.ns
) /* TODO: 2nd check no longer needed? */
1823 view = FRAME_NS_VIEW
(*fp
);
1825 position
= [[view window
] mouseLocationOutsideOfEventStream
];
1826 position
= [view convertPoint
: position fromView
: nil
];
1827 remember_mouse_glyph
(f
, position.x
, position.y
, &last_mouse_glyph
);
1828 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1830 if (bar_window
) *bar_window
= Qnil
;
1831 if (part
) *part
= 0; /*scroll_bar_handle; */
1833 if (x
) XSETINT
(*x
, lrint
(position.x
));
1834 if (y
) XSETINT
(*y
, lrint
(position.y
));
1835 if (time
) *time
= last_mouse_movement_time
;
1845 ns_frame_up_to_date
(struct frame
*f
)
1846 /* --------------------------------------------------------------------------
1847 External (hook): Fix up mouse highlighting right after a full update.
1848 Some highlighting was deferred if GC was happening during
1849 note_mouse_highlight (), while other highlighting was deferred for update.
1850 -------------------------------------------------------------------------- */
1852 NSTRACE
(ns_frame_up_to_date
);
1856 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1857 if ((dpyinfo
->mouse_face_deferred_gc||f
==dpyinfo
->mouse_face_mouse_frame
)
1858 /*&& dpyinfo->mouse_face_mouse_frame*/)
1861 if (dpyinfo
->mouse_face_mouse_frame
)
1862 note_mouse_highlight
(dpyinfo
->mouse_face_mouse_frame
,
1863 dpyinfo
->mouse_face_mouse_x
,
1864 dpyinfo
->mouse_face_mouse_y
);
1865 dpyinfo
->mouse_face_deferred_gc
= 0;
1873 ns_define_frame_cursor
(struct frame
*f
, Cursor cursor
)
1874 /* --------------------------------------------------------------------------
1875 External (RIF): set frame mouse pointer type.
1876 -------------------------------------------------------------------------- */
1878 NSTRACE
(ns_define_frame_cursor
);
1879 if (FRAME_POINTER_TYPE
(f
) != cursor
)
1881 EmacsView
*view = FRAME_NS_VIEW
(f
);
1882 FRAME_POINTER_TYPE
(f
) = cursor
;
1883 [[view window
] invalidateCursorRectsForView
: view];
1889 /* ==========================================================================
1893 ========================================================================== */
1897 ns_convert_key
(unsigned code
)
1898 /* --------------------------------------------------------------------------
1899 Internal call used by NSView-keyDown.
1900 -------------------------------------------------------------------------- */
1902 const unsigned last_keysym
= (sizeof
(convert_ns_to_X_keysym
)
1903 / sizeof
(convert_ns_to_X_keysym
[0]));
1905 /* An array would be faster, but less easy to read. */
1906 for (keysym
= 0; keysym
< last_keysym
; keysym
+= 2)
1907 if (code
== convert_ns_to_X_keysym
[keysym
])
1908 return 0xFF00 | convert_ns_to_X_keysym
[keysym
+1];
1910 /* if decide to use keyCode and Carbon table, use this line:
1911 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1916 x_get_keysym_name
(int keysym
)
1917 /* --------------------------------------------------------------------------
1918 Called by keyboard.c. Not sure if the return val is important, except
1920 -------------------------------------------------------------------------- */
1922 static
char value
[16];
1923 NSTRACE
(x_get_keysym_name
);
1924 sprintf (value
, "
%d", keysym);
1930 /* ==========================================================================
1932 Block drawing operations
1934 ========================================================================== */
1938 ns_redraw_scroll_bars
(struct frame
*f
)
1942 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
1943 NSTRACE
(ns_judge_scroll_bars
);
1944 for (i
=[subviews count
]-1; i
>= 0; i
--)
1946 view = [subviews objectAtIndex
: i
];
1947 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
1954 ns_clear_frame
(struct frame
*f
)
1955 /* --------------------------------------------------------------------------
1956 External (hook): Erase the entire frame
1957 -------------------------------------------------------------------------- */
1959 NSView
*view = FRAME_NS_VIEW
(f
);
1962 NSTRACE
(ns_clear_frame
);
1966 /* comes on initial frame because we have
1967 after-make-frame-functions = select-frame */
1968 if (!FRAME_DEFAULT_FACE
(f
))
1971 mark_window_cursors_off
(XWINDOW
(FRAME_ROOT_WINDOW
(f
)));
1973 output_cursor.hpos
= output_cursor.vpos
= 0;
1974 output_cursor.x
= -1;
1979 ns_focus
(f
, &r
, 1);
1980 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(FRAME_DEFAULT_FACE
(f
)), f
) set];
1984 #ifdef NS_IMPL_COCOA
1985 [[view window
] display
]; /* redraw resize handle */
1988 /* as of 2006/11 or so this is now needed */
1989 ns_redraw_scroll_bars
(f
);
1995 ns_clear_frame_area
(struct frame
*f
, int x
, int y
, int width
, int height
)
1996 /* --------------------------------------------------------------------------
1997 External (RIF): Clear section of frame
1998 -------------------------------------------------------------------------- */
2000 NSRect r
= NSMakeRect
(x
, y
, width
, height
);
2001 NSView
*view = FRAME_NS_VIEW
(f
);
2002 struct face
*face
= FRAME_DEFAULT_FACE
(f
);
2007 NSTRACE
(ns_clear_frame_area
);
2009 r
= NSIntersectionRect
(r
, [view frame
]);
2010 ns_focus
(f
, &r
, 1);
2011 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), f
) set];
2013 #ifdef NS_IMPL_COCOA
2015 /* clip out the resize handle */
2016 NSWindow
*window
= [FRAME_NS_VIEW
(f
) window
];
2018 = [view convertRect
: ns_resize_handle_rect
(window
) fromView
: nil
];
2020 ir
= NSIntersectionRect
(r
, ir
);
2021 if (NSIsEmptyRect
(ir
))
2027 #ifdef NS_IMPL_COCOA
2031 NSRect r1
= r
, r2
= r
; /* upper and lower non-intersecting */
2032 r1.
size.height
-= ir.
size.height
;
2033 r2.origin.y
+= r1.
size.height
;
2034 r2.
size.width
-= ir.
size.width
;
2035 r2.
size.height
= ir.
size.height
;
2048 ns_scroll_run
(struct window
*w
, struct run
*run
)
2049 /* --------------------------------------------------------------------------
2050 External (RIF): Insert or delete n lines at line vpos
2051 -------------------------------------------------------------------------- */
2053 struct frame
*f
= XFRAME
(w
->frame
);
2054 int x
, y
, width
, height
, from_y
, to_y
, bottom_y
;
2056 NSTRACE
(ns_scroll_run
);
2058 /* begin copy from other terms */
2059 /* Get frame-relative bounding box of the text display area of W,
2060 without mode lines. Include in this box the left and right
2062 window_box
(w
, -1, &x
, &y
, &width
, &height
);
2064 from_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->current_y
);
2065 to_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->desired_y
);
2066 bottom_y
= y
+ height
;
2070 /* Scrolling up. Make sure we don't copy part of the mode
2071 line at the bottom. */
2072 if (from_y
+ run
->height
> bottom_y
)
2073 height
= bottom_y
- from_y
;
2075 height
= run
->height
;
2079 /* Scolling down. Make sure we don't copy over the mode line.
2081 if (to_y
+ run
->height
> bottom_y
)
2082 height
= bottom_y
- to_y
;
2084 height
= run
->height
;
2086 /* end copy from other terms */
2097 NSRect srcRect
= NSMakeRect
(x
, from_y
, width
, height
);
2098 NSRect dstRect
= NSMakeRect
(x
, to_y
, width
, height
);
2099 NSPoint dstOrigin
= NSMakePoint
(x
, to_y
);
2101 ns_focus
(f
, &dstRect
, 1);
2102 NSCopyBits
(0, srcRect
, dstOrigin
);
2111 ns_after_update_window_line
(struct glyph_row
*desired_row
)
2112 /* --------------------------------------------------------------------------
2113 External (RIF): preparatory to fringe update after text was updated
2114 -------------------------------------------------------------------------- */
2116 struct window
*w
= updated_window
;
2120 NSTRACE
(ns_after_update_window_line
);
2122 /* begin copy from other terms */
2125 if (!desired_row
->mode_line_p
&& !w
->pseudo_window_p
)
2126 desired_row
->redraw_fringe_bitmaps_p
= 1;
2128 /* When a window has disappeared, make sure that no rest of
2129 full-width rows stays visible in the internal border.
2130 Under NS this is drawn inside the fringes. */
2131 if (windows_or_buffers_changed
2132 && (f
= XFRAME
(w
->frame
),
2133 width
= FRAME_INTERNAL_BORDER_WIDTH
(f
),
2135 && (height
= desired_row
->visible_height
,
2138 int y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, desired_row
->y
));
2140 /* Internal border is drawn below the tool bar. */
2141 if (WINDOWP
(f
->tool_bar_window
)
2142 && w
== XWINDOW
(f
->tool_bar_window
))
2144 /* end copy from other terms */
2147 if (!desired_row
->full_width_p
)
2149 int x1
= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH
(w
)
2150 + WINDOW_LEFT_FRINGE_WIDTH
(w
);
2151 int x2
= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH
(w
)
2152 + FRAME_PIXEL_WIDTH
(f
) - NS_SCROLL_BAR_WIDTH
(f
)
2153 - WINDOW_RIGHT_FRINGE_WIDTH
(w
)
2154 - FRAME_INTERNAL_BORDER_WIDTH
(f
);
2155 ns_clear_frame_area
(f
, x1
, y
, width
, height
);
2156 ns_clear_frame_area
(f
, x2
, y
, width
, height
);
2164 ns_shift_glyphs_for_insert
(struct frame
*f
,
2165 int x
, int y
, int width
, int height
,
2167 /* --------------------------------------------------------------------------
2168 External (RIF): copy an area horizontally, don't worry about clearing src
2169 -------------------------------------------------------------------------- */
2171 NSRect srcRect
= NSMakeRect
(x
, y
, width
, height
);
2172 NSRect dstRect
= NSMakeRect
(x
+shift_by
, y
, width
, height
);
2173 NSPoint dstOrigin
= dstRect.origin
;
2175 NSTRACE
(ns_shift_glyphs_for_insert
);
2177 ns_focus
(f
, &dstRect
, 1);
2178 NSCopyBits
(0, srcRect
, dstOrigin
);
2184 /* ==========================================================================
2186 Character encoding and metrics
2188 ========================================================================== */
2192 ns_compute_glyph_string_overhangs
(struct glyph_string
*s
)
2193 /* --------------------------------------------------------------------------
2194 External (RIF); compute left/right overhang of whole string and set in s
2195 -------------------------------------------------------------------------- */
2197 struct face
*face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2198 struct font
*font
= s
->font
; /*face->font; */
2202 struct font_metrics metrics
;
2203 unsigned int codes
[2];
2204 codes
[0] = *(s
->char2b
);
2205 codes
[1] = *(s
->char2b
+ s
->nchars
- 1);
2207 font
->driver
->text_extents
(font
, codes
, 2, &metrics
);
2208 s
->left_overhang
= -metrics.lbearing
;
2210 = metrics.rbearing
> metrics.width
2211 ? metrics.rbearing
- metrics.width
: 0;
2215 s
->left_overhang
= 0;
2216 s
->right_overhang
= ((struct nsfont_info
*)font
)->ital ?
2217 FONT_HEIGHT
(font
) * 0.2 : 0;
2223 /* ==========================================================================
2225 Fringe and cursor drawing
2227 ========================================================================== */
2230 extern int max_used_fringe_bitmap
;
2232 ns_draw_fringe_bitmap
(struct window
*w
, struct glyph_row
*row
,
2233 struct draw_fringe_bitmap_params
*p
)
2234 /* --------------------------------------------------------------------------
2235 External (RIF); fringe-related
2236 -------------------------------------------------------------------------- */
2238 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2239 struct face
*face
= p
->face
;
2241 static EmacsImage
**bimgs
= NULL;
2242 static int nBimgs
= 0;
2243 /* NS-specific: move internal border inside fringe */
2244 int x
= p
->bx
< 0 ? p
->x
: p
->bx
;
2245 int wd
= p
->bx
< 0 ? p
->wd
: p
->nx
;
2246 BOOL fringeOnVeryLeft
2247 = x
- WINDOW_LEFT_SCROLL_BAR_COLS
(w
) * WINDOW_FRAME_COLUMN_WIDTH
(w
)
2248 - FRAME_INTERNAL_BORDER_WIDTH
(f
) < 10;
2249 BOOL fringeOnVeryRight
2250 = FRAME_PIXEL_WIDTH
(f
) - x
- wd
- FRAME_INTERNAL_BORDER_WIDTH
(f
)
2251 - WINDOW_RIGHT_SCROLL_BAR_COLS
(w
) * WINDOW_FRAME_COLUMN_WIDTH
(w
) < 10;
2252 int xAdjust
= FRAME_INTERNAL_BORDER_WIDTH
(f
) *
2253 (fringeOnVeryLeft ?
-1 : (fringeOnVeryRight ?
1 : 0));
2255 /* grow bimgs if needed */
2256 if (nBimgs
< max_used_fringe_bitmap
)
2258 EmacsImage
**newBimgs
2259 = xmalloc
(max_used_fringe_bitmap
* sizeof
(EmacsImage
*));
2260 bzero
(newBimgs
, max_used_fringe_bitmap
* sizeof
(EmacsImage
*));
2264 bcopy
(bimgs
, newBimgs
, nBimgs
* sizeof
(EmacsImage
*));
2269 nBimgs
= max_used_fringe_bitmap
;
2272 /* Must clip because of partially visible lines. */
2273 rowY
= WINDOW_TO_FRAME_PIXEL_Y
(w
, row
->y
);
2276 /* Adjust position of "bottom aligned" bitmap on partially
2277 visible last row. */
2279 int oldVH
= row
->visible_height
;
2280 row
->visible_height
= p
->h
;
2281 row
->y
-= rowY
- p
->y
;
2282 ns_clip_to_row
(w
, row
, -1, NO
);
2284 row
->visible_height
= oldVH
;
2287 ns_clip_to_row
(w
, row
, -1, YES
);
2289 if (p
->bx
>= 0 && !p
->overlay_p
)
2291 int yAdjust
= rowY
- FRAME_INTERNAL_BORDER_WIDTH
(f
) < 5 ?
2292 -FRAME_INTERNAL_BORDER_WIDTH
(f
) : 0;
2293 int yIncr
= FRAME_PIXEL_HEIGHT
(f
) - (p
->by
+yAdjust
+ p
->ny
) < 5 ?
2294 FRAME_INTERNAL_BORDER_WIDTH
(f
) : 0;
2296 yIncr
+= FRAME_INTERNAL_BORDER_WIDTH
(f
);
2297 NSRect r
= NSMakeRect
(p
->bx
+xAdjust
, p
->by
+yAdjust
, p
->nx
, p
->ny
+yIncr
);
2299 [ns_lookup_indexed_color
(face
->background
, f
) set];
2305 NSRect r
= NSMakeRect
(p
->x
+xAdjust
, p
->y
, p
->wd
, p
->h
);
2306 NSPoint pt
= r.origin
;
2307 EmacsImage
*img
= bimgs
[p
->which - 1];
2311 unsigned short
*bits
= p
->bits
+ p
->dh
;
2312 int len
= 8 * p
->h
/8;
2314 unsigned
char *cbits
= xmalloc
(len
);
2316 for (i
=0; i
<len
; i
++)
2317 cbits
[i
] = ~
(bits
[i
] & 0xff);
2318 img
= [[EmacsImage alloc
] initFromXBM
: cbits width
: 8 height
: p
->h
2320 bimgs
[p
->which - 1] = img
;
2325 /* Since we composite the bitmap instead of just blitting it, we need
2326 to erase the whole background. */
2327 [ns_lookup_indexed_color
(face
->background
, f
) set];
2330 [img setXBMColor
: ns_lookup_indexed_color
(face
->foreground
, f
)];
2331 [img compositeToPoint
: pt operation
: NSCompositeSourceOver
];
2338 ns_draw_window_cursor
(struct window
*w
, struct glyph_row
*glyph_row
,
2339 int x
, int y
, int cursor_type
, int cursor_width
,
2340 int on_p
, int active_p
)
2341 /* --------------------------------------------------------------------------
2342 External call (RIF): draw cursor
2343 (modeled after x_draw_window_cursor
2344 FIXME: cursor_width is effectively bogus -- it sometimes gets set
2345 in xdisp.c set_frame_cursor_types, sometimes left uninitialized;
2346 DON'T USE IT (no other terms do)
2347 -------------------------------------------------------------------------- */
2351 struct frame
*f
= WINDOW_XFRAME
(w
);
2352 struct glyph
*phys_cursor_glyph
;
2353 int overspill
, cursorToDraw
;
2355 NSTRACE
(dumpcursor
);
2356 //fprintf(stderr
, "drawcursor
(%d,%d) activep = %d\tonp = %d\tc_type = %d\twidth = %d\n",x,y, active_p,on_p,cursor_type,cursor_width);
2361 w
->phys_cursor_type
= cursor_type
;
2362 w
->phys_cursor_on_p
= on_p
;
2364 if (cursor_type
== NO_CURSOR
)
2366 w
->phys_cursor_width
= 0;
2370 if ((phys_cursor_glyph
= get_phys_cursor_glyph
(w
)) == NULL)
2372 if (glyph_row
->exact_window_width_line_p
2373 && w
->phys_cursor.hpos
>= glyph_row
->used
[TEXT_AREA
])
2375 glyph_row
->cursor_in_fringe_p
= 1;
2376 draw_fringe_bitmap
(w
, glyph_row
, 0);
2381 get_phys_cursor_geometry
(w
, glyph_row
, phys_cursor_glyph
, &fx
, &fy
, &h
);
2383 r.origin.x
= fx
, r.origin.y
= fy
;
2385 r.
size.width
= w
->phys_cursor_width
;
2387 /* FIXME: if we overwrite the internal border area, it does not get erased;
2388 fix by truncating cursor, but better would be to erase properly */
2389 overspill
= r.origin.x
+ r.
size.width
-
2390 WINDOW_TEXT_TO_FRAME_PIXEL_X
(w
, WINDOW_BOX_RIGHT_EDGE_X
(w
)
2391 - WINDOW_TOTAL_FRINGE_WIDTH
(w
) - FRAME_INTERNAL_BORDER_WIDTH
(f
));
2393 r.
size.width
-= overspill
;
2395 /* TODO: only needed in rare cases with last-resort font in HELLO..
2396 should we do this more efficiently? */
2397 ns_clip_to_row
(w
, glyph_row
, -1, NO
); /* do ns_focus(f, &r, 1); if remove */
2398 [FRAME_CURSOR_COLOR
(f
) set];
2400 #ifdef NS_IMPL_COCOA
2401 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2402 atomic. Cleaner ways of doing this should be investigated.
2403 One way would be to set a global variable DRAWING_CURSOR
2404 when making the call to draw_phys..(), don't focus in that
2405 case, then move the ns_unfocus() here after that call. */
2406 NSDisableScreenUpdates
();
2409 cursorToDraw
= active_p ? cursor_type
: HOLLOW_BOX_CURSOR
;
2410 switch (cursorToDraw
)
2414 case FILLED_BOX_CURSOR
:
2417 case HOLLOW_BOX_CURSOR
:
2419 [FRAME_BACKGROUND_COLOR
(f
) set];
2420 NSRectFill
(NSInsetRect
(r
, 1, 1));
2421 [FRAME_CURSOR_COLOR
(f
) set];
2425 s.origin.y
+= lrint
(0.75 * s.
size.height
);
2426 s.
size.width
= min (FRAME_COLUMN_WIDTH
(f
), s.
size.width
);
2427 s.
size.height
= lrint
(s.
size.height
* 0.25);
2432 s.
size.width
= min (cursor_width
, 2); //FIXME
(see above
)
2438 /* draw the character under the cursor */
2439 if (cursorToDraw
!= NO_CURSOR
)
2440 draw_phys_cursor_glyph
(w
, glyph_row
, DRAW_CURSOR
);
2442 #ifdef NS_IMPL_COCOA
2443 NSEnableScreenUpdates
();
2450 ns_draw_vertical_window_border
(struct window
*w
, int x
, int y0
, int y1
)
2451 /* --------------------------------------------------------------------------
2452 External (RIF): Draw a vertical line.
2453 -------------------------------------------------------------------------- */
2455 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2457 NSRect r
= NSMakeRect
(x
, y0
, 2, y1
-y0
);
2459 NSTRACE
(ns_draw_vertical_window_border
);
2461 face
= FACE_FROM_ID
(f
, VERTICAL_BORDER_FACE_ID
);
2463 [ns_lookup_indexed_color
(face
->foreground
, f
) set];
2465 ns_focus
(f
, &r
, 1);
2466 NSDrawGroove
(r
, r
);
2472 show_hourglass
(struct atimer
*timer
)
2474 if (hourglass_shown_p
)
2479 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2481 hourglass_shown_p
= 1;
2489 if (!hourglass_shown_p
)
2494 /* TODO: remove NSProgressIndicator from all frames */
2496 hourglass_shown_p
= 0;
2502 /* ==========================================================================
2504 Glyph drawing operations
2506 ========================================================================== */
2509 static inline NSRect
2510 ns_fix_rect_ibw
(NSRect r
, int fibw
, int frame_pixel_width
)
2511 /* --------------------------------------------------------------------------
2512 Under NS we draw internal borders inside fringes, and want full-width
2513 rendering to go all the way to edge. This function makes that correction.
2514 -------------------------------------------------------------------------- */
2516 if (r.origin.y
<= fibw
+1)
2518 r.
size.height
+= r.origin.y
;
2521 if (r.origin.x
<= fibw
+1)
2523 r.
size.width
+= r.origin.x
;
2526 if (frame_pixel_width
- (r.origin.x
+r.
size.width
) <= fibw
+1)
2527 r.
size.width
+= fibw
;
2534 ns_get_glyph_string_clip_rect
(struct glyph_string
*s
, NativeRectangle
*nr
)
2535 /* --------------------------------------------------------------------------
2536 Wrapper utility to account for internal border width on full-width lines,
2537 and allow top full-width rows to hit the frame top. nr should be pointer
2538 to two successive NSRects. Number of rects actually used is returned.
2539 -------------------------------------------------------------------------- */
2541 int n
= get_glyph_string_clip_rects
(s
, nr
, 2);
2542 if (s
->row
->full_width_p
)
2544 *nr
= ns_fix_rect_ibw
(*nr
, FRAME_INTERNAL_BORDER_WIDTH
(s
->f
),
2545 FRAME_PIXEL_WIDTH
(s
->f
));
2547 *nr
= ns_fix_rect_ibw
(*(nr
+1), FRAME_INTERNAL_BORDER_WIDTH
(s
->f
),
2548 FRAME_PIXEL_WIDTH
(s
->f
));
2555 ns_draw_box
(NSRect r
, float thickness
, NSColor
*col
, char left_p
, char right_p
)
2556 /* --------------------------------------------------------------------------
2557 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2558 Note we can't just use an NSDrawRect command, because of the possibility
2559 of some sides not being drawn, and because the rect will be filled.
2560 -------------------------------------------------------------------------- */
2566 s.
size.height
= thickness
;
2568 s.origin.y
+= r.
size.height
- thickness
;
2571 s.
size.height
= r.
size.height
;
2572 s.origin.y
= r.origin.y
;
2574 /* left, right (optional) */
2575 s.
size.width
= thickness
;
2580 s.origin.x
+= r.
size.width
- thickness
;
2587 ns_draw_relief
(NSRect r
, int thickness
, char raised_p
,
2588 char top_p
, char bottom_p
, char left_p
, char right_p
,
2589 struct glyph_string
*s
)
2590 /* --------------------------------------------------------------------------
2591 Draw a relief rect inside r, optionally leaving some sides open.
2592 Note we can't just use an NSDrawBezel command, because of the possibility
2593 of some sides not being drawn, and because the rect will be filled.
2594 -------------------------------------------------------------------------- */
2596 static NSColor
*baseCol
= nil
, *lightCol
= nil
, *darkCol
= nil
;
2597 NSColor
*newBaseCol
= nil
;
2600 NSTRACE
(ns_draw_relief
);
2604 if (s
->face
->use_box_color_for_shadows_p
)
2606 newBaseCol
= ns_lookup_indexed_color
(s
->face
->box_color
, s
->f
);
2608 /* else if (s->first_glyph->type == IMAGE_GLYPH
2610 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2612 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2616 newBaseCol
= ns_lookup_indexed_color
(s
->face
->background
, s
->f
);
2619 if (newBaseCol
== nil
)
2620 newBaseCol
= [NSColor grayColor
];
2622 if (newBaseCol
!= baseCol
) /* TODO: better check */
2625 baseCol
= [newBaseCol retain
];
2627 lightCol
= [[baseCol highlightWithLevel
: 0.2] retain
];
2629 darkCol
= [[baseCol shadowWithLevel
: 0.3] retain
];
2632 [(raised_p ? lightCol
: darkCol
) set];
2634 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2637 sr.
size.height
= thickness
;
2638 if (top_p
) NSRectFill
(sr
);
2641 sr.
size.height
= r.
size.height
;
2642 sr.
size.width
= thickness
;
2643 if (left_p
) NSRectFill
(sr
);
2645 [(raised_p ? darkCol
: lightCol
) set];
2648 sr.
size.width
= r.
size.width
;
2649 sr.
size.height
= thickness
;
2650 sr.origin.y
+= r.
size.height
- thickness
;
2651 if (bottom_p
) NSRectFill
(sr
);
2654 sr.
size.height
= r.
size.height
;
2655 sr.origin.y
= r.origin.y
;
2656 sr.
size.width
= thickness
;
2657 sr.origin.x
+= r.
size.width
- thickness
;
2658 if (right_p
) NSRectFill
(sr
);
2663 ns_dumpglyphs_box_or_relief
(struct glyph_string
*s
)
2664 /* --------------------------------------------------------------------------
2665 Function modeled after x_draw_glyph_string_box ().
2666 Sets up parameters for drawing.
2667 -------------------------------------------------------------------------- */
2669 int right_x
, last_x
;
2670 char left_p
, right_p
;
2671 struct glyph
*last_glyph
;
2676 if (s
->hl
== DRAW_MOUSE_FACE
)
2679 (s
->f
, FRAME_NS_DISPLAY_INFO
(s
->f
)->mouse_face_face_id
);
2681 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2686 thickness
= face
->box_line_width
;
2688 NSTRACE
(ns_dumpglyphs_box_or_relief
);
2690 last_x
= ((s
->row
->full_width_p
&& !s
->w
->pseudo_window_p
)
2691 ? WINDOW_RIGHT_EDGE_X
(s
->w
)
2692 : window_box_right
(s
->w
, s
->area
));
2693 last_glyph
= (s
->cmp || s
->img
2694 ? s
->first_glyph
: s
->first_glyph
+ s
->nchars
-1);
2696 right_x
= ((s
->row
->full_width_p
&& s
->extends_to_end_of_line_p
2697 ? last_x
- 1 : min (last_x
, s
->x
+ s
->background_width
) - 1));
2699 left_p
= (s
->first_glyph
->left_box_line_p
2700 ||
(s
->hl
== DRAW_MOUSE_FACE
2701 && (s
->prev
== NULL || s
->prev
->hl
!= s
->hl
)));
2702 right_p
= (last_glyph
->right_box_line_p
2703 ||
(s
->hl
== DRAW_MOUSE_FACE
2704 && (s
->next
== NULL || s
->next
->hl
!= s
->hl
)));
2706 r
= NSMakeRect
(s
->x
, s
->y
, right_x
- s
->x
+ 1, s
->height
);
2708 /* expand full-width row over internal borders */
2709 if (s
->row
->full_width_p
)
2710 r
= ns_fix_rect_ibw
(r
, FRAME_INTERNAL_BORDER_WIDTH
(s
->f
),
2711 FRAME_PIXEL_WIDTH
(s
->f
));
2713 if (s
->face
->box
== FACE_SIMPLE_BOX
)
2715 xassert
(s
->face
->box_color
!= nil
);
2716 ns_draw_box
(r
, abs (thickness
),
2717 ns_lookup_indexed_color
(face
->box_color
, s
->f
),
2722 ns_draw_relief
(r
, abs (thickness
), s
->face
->box
== FACE_RAISED_BOX
,
2723 1, 1, left_p
, right_p
, s
);
2729 ns_maybe_dumpglyphs_background
(struct glyph_string
*s
, char force_p
)
2730 /* --------------------------------------------------------------------------
2731 Modeled after x_draw_glyph_string_background, which draws BG in
2732 certain cases. Others are left to the text rendering routine.
2733 -------------------------------------------------------------------------- */
2735 NSTRACE
(ns_maybe_dumpglyphs_background
);
2737 if (!s
->background_filled_p
/* || s->hl == DRAW_MOUSE_FACE*/)
2739 int box_line_width
= max (s
->face
->box_line_width
, 0);
2740 if (FONT_HEIGHT
(s
->font
) < s
->height
- 2 * box_line_width
2741 || s
->font_not_found_p || s
->extends_to_end_of_line_p || force_p
)
2744 if (s
->hl
== DRAW_MOUSE_FACE
)
2747 (s
->f
, FRAME_NS_DISPLAY_INFO
(s
->f
)->mouse_face_face_id
);
2749 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2752 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2754 [(NS_FACE_BACKGROUND
(face
) != 0
2755 ? ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
)
2756 : FRAME_BACKGROUND_COLOR
(s
->f
)) set];
2759 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(s
->f
);
2760 [[dpyinfo
->bitmaps
[face
->stipple
-1].img stippleMask
] set];
2763 if (s
->hl
!= DRAW_CURSOR
)
2765 NSRect r
= NSMakeRect
(s
->x
, s
->y
+ box_line_width
,
2766 s
->background_width
,
2767 s
->height
-2*box_line_width
);
2769 /* expand full-width row over internal borders */
2770 if (s
->row
->full_width_p
)
2772 int fibw
= FRAME_INTERNAL_BORDER_WIDTH
(s
->f
);
2773 if (r.origin.y
<= fibw
+1 + box_line_width
)
2775 r.
size.height
+= r.origin.y
;
2778 if (r.origin.x
<= fibw
+1)
2780 r.
size.width
+= 2*r.origin.x
;
2783 if (FRAME_PIXEL_WIDTH
(s
->f
) - (r.origin.x
+ r.
size.width
)
2785 r.
size.width
+= fibw
;
2791 s
->background_filled_p
= 1;
2798 ns_dumpglyphs_image
(struct glyph_string
*s
, NSRect r
)
2799 /* --------------------------------------------------------------------------
2800 Renders an image and associated borders.
2801 -------------------------------------------------------------------------- */
2803 EmacsImage
*img
= s
->img
->pixmap
;
2804 int box_line_vwidth
= max (s
->face
->box_line_width
, 0);
2805 int x
= s
->x
, y
= s
->ybase
- image_ascent
(s
->img
, s
->face
, &s
->slice);
2806 int bg_x
, bg_y
, bg_height
;
2811 NSTRACE
(ns_dumpglyphs_image
);
2813 if (s
->face
->box
!= FACE_NO_BOX
2814 && s
->first_glyph
->left_box_line_p
&& s
->slice.x
== 0)
2815 x
+= abs (s
->face
->box_line_width
);
2818 bg_y
= s
->slice.y
== 0 ? s
->y
: s
->y
+ box_line_vwidth
;
2819 bg_height
= s
->height
;
2820 /* other terms have this, but was causing problems w/tabbar mode */
2821 /* - 2 * box_line_vwidth; */
2823 if (s
->slice.x
== 0) x
+= s
->img
->hmargin
;
2824 if (s
->slice.y
== 0) y
+= s
->img
->vmargin
;
2826 /* Draw BG: if we need larger area than image itself cleared, do that,
2827 otherwise, since we composite the image under NS (instead of mucking
2828 with its background color), we must clear just the image area. */
2829 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
2830 (FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
)), s
->f
) set];
2832 if (bg_height
> s
->slice.height || s
->img
->hmargin || s
->img
->vmargin
2833 || s
->img
->mask || s
->img
->pixmap
== 0 || s
->width
!= s
->background_width
)
2835 br
= NSMakeRect
(bg_x
, bg_y
, s
->background_width
, bg_height
);
2836 s
->background_filled_p
= 1;
2840 br
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
2843 /* expand full-width row over internal borders */
2844 if (s
->row
->full_width_p
)
2846 int fibw
= FRAME_INTERNAL_BORDER_WIDTH
(s
->f
);
2847 if (br.origin.y
<= fibw
+1 + box_line_vwidth
)
2849 br.
size.height
+= br.origin.y
;
2852 if (br.origin.x
<= fibw
+1 + box_line_vwidth
)
2854 br.
size.width
+= br.origin.x
;
2857 if (FRAME_PIXEL_WIDTH
(s
->f
) - (br.origin.x
+ br.
size.width
) <= fibw
+1)
2858 br.
size.width
+= fibw
;
2863 /* Draw the image.. do we need to draw placeholder if img ==nil? */
2865 [img compositeToPoint
: NSMakePoint
(x
, y
+ s
->slice.height
)
2866 operation
: NSCompositeSourceOver
];
2868 /* Draw relief, if requested */
2869 if (s
->img
->relief || s
->hl
==DRAW_IMAGE_RAISED || s
->hl
==DRAW_IMAGE_SUNKEN
)
2871 if (s
->hl
== DRAW_IMAGE_SUNKEN || s
->hl
== DRAW_IMAGE_RAISED
)
2873 th
= tool_bar_button_relief
>= 0 ?
2874 tool_bar_button_relief
: DEFAULT_TOOL_BAR_BUTTON_RELIEF
;
2875 raised_p
= (s
->hl
== DRAW_IMAGE_RAISED
);
2879 th
= abs (s
->img
->relief
);
2880 raised_p
= (s
->img
->relief
> 0);
2883 r.origin.x
= x
- th
;
2884 r.origin.y
= y
- th
;
2885 r.
size.width
= s
->slice.width
+ 2*th
-1;
2886 r.
size.height
= s
->slice.height
+ 2*th
-1;
2887 ns_draw_relief
(r
, th
, raised_p
,
2889 s
->slice.y
+ s
->slice.height
== s
->img
->height
,
2891 s
->slice.x
+ s
->slice.width
== s
->img
->width
, s
);
2897 ns_dumpglyphs_stretch
(struct glyph_string
*s
)
2902 if (!s
->background_filled_p
)
2904 n
= ns_get_glyph_string_clip_rect
(s
, r
);
2905 *r
= NSMakeRect
(s
->x
, s
->y
, s
->background_width
, s
->height
);
2909 if (!s
->row
->full_width_p
)
2911 /* truncate to avoid overwriting fringe and/or scrollbar */
2912 int overrun
= max (0, (s
->x
+ s
->background_width
)
2913 - (WINDOW_BOX_RIGHT_EDGE_X
(s
->w
)
2914 - WINDOW_RIGHT_FRINGE_WIDTH
(s
->w
)));
2915 r
[i
].
size.width
-= overrun
;
2917 /* XXX: Try to work between problem where a stretch glyph on
2918 a partially-visible bottom row will clear part of the
2919 modeline, and another where list-buffers headers and similar
2920 rows erroneously have visible_height set to 0. Not sure
2921 where this is coming from as other terms seem not to show. */
2922 r
[i
].
size.height
= min (s
->height
, s
->row
->visible_height
);
2925 /* expand full-width rows over internal borders */
2928 r
[i
] = ns_fix_rect_ibw
(r
[i
], FRAME_INTERNAL_BORDER_WIDTH
(s
->f
),
2929 FRAME_PIXEL_WIDTH
(s
->f
));
2932 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
2933 overwriting cursor (usually when cursor on a tab) */
2934 if (s
->hl
== DRAW_CURSOR
)
2936 r
[i
].origin.x
+= s
->width
;
2937 r
[i
].
size.width
-= s
->width
;
2941 ns_focus
(s
->f
, r
, n
);
2942 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
2943 (FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
)), s
->f
) set];
2947 s
->background_filled_p
= 1;
2953 ns_draw_glyph_string
(struct glyph_string
*s
)
2954 /* --------------------------------------------------------------------------
2955 External (RIF): Main draw-text call.
2956 -------------------------------------------------------------------------- */
2958 /* TODO (optimize): focus for box and contents draw */
2961 char box_drawn_p
= 0;
2963 NSTRACE
(ns_draw_glyph_string
);
2965 if (s
->next
&& s
->right_overhang
&& !s
->for_overlaps
/*&&s->hl!=DRAW_CURSOR*/)
2968 struct glyph_string
*next
;
2970 for (width
= 0, next
= s
->next
;
2971 next
&& width
< s
->right_overhang
;
2972 width
+= next
->width
, next
= next
->next
)
2973 if (next
->first_glyph
->type != IMAGE_GLYPH
)
2975 if (next
->first_glyph
->type != STRETCH_GLYPH
)
2977 n
= ns_get_glyph_string_clip_rect
(s
->next
, r
);
2978 ns_focus
(s
->f
, r
, n
);
2979 ns_maybe_dumpglyphs_background
(s
->next
, 1);
2984 ns_dumpglyphs_stretch
(s
->next
);
2986 next
->num_clips
= 0;
2990 if (!s
->for_overlaps
&& s
->face
->box
!= FACE_NO_BOX
2991 && (s
->first_glyph
->type == CHAR_GLYPH
2992 || s
->first_glyph
->type == COMPOSITE_GLYPH
))
2994 n
= ns_get_glyph_string_clip_rect
(s
, r
);
2995 ns_focus
(s
->f
, r
, n
);
2996 ns_maybe_dumpglyphs_background
(s
, 1);
2997 ns_dumpglyphs_box_or_relief
(s
);
3002 switch (s
->first_glyph
->type)
3006 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3007 ns_focus
(s
->f
, r
, n
);
3008 ns_dumpglyphs_image
(s
, r
[0]);
3013 ns_dumpglyphs_stretch
(s
);
3017 case COMPOSITE_GLYPH
:
3018 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3019 ns_focus
(s
->f
, r
, n
);
3021 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3022 && ! s
->first_glyph
->u.cmp.automatic
))
3023 s
->background_filled_p
= 1;
3025 ns_maybe_dumpglyphs_background
3026 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3028 ns_tmp_flags
= s
->hl
== DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR
:
3029 (s
->hl
== DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE
:
3030 (s
->for_overlaps ? NS_DUMPGLYPH_FOREGROUND
:
3031 NS_DUMPGLYPH_NORMAL
));
3032 ns_tmp_font
= (struct nsfont_info
*)s
->face
->font
;
3033 if (ns_tmp_font
== NULL)
3034 ns_tmp_font
= (struct nsfont_info
*)FRAME_FONT
(s
->f
);
3036 ns_tmp_font
->font.driver
->draw
3037 (s
, 0, s
->nchars
, s
->x
, s
->y
,
3038 (ns_tmp_flags
== NS_DUMPGLYPH_NORMAL
&& !s
->background_filled_p
)
3039 || ns_tmp_flags
== NS_DUMPGLYPH_MOUSEFACE
);
3048 /* Draw box if not done already. */
3049 if (!s
->for_overlaps
&& !box_drawn_p
&& s
->face
->box
!= FACE_NO_BOX
)
3051 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3052 ns_focus
(s
->f
, r
, n
);
3053 ns_dumpglyphs_box_or_relief
(s
);
3062 /* ==========================================================================
3066 ========================================================================== */
3070 ns_send_appdefined
(int value
)
3071 /* --------------------------------------------------------------------------
3072 Internal: post an appdefined event which EmacsApp-sendEvent will
3073 recognize and take as a command to halt the event loop.
3074 -------------------------------------------------------------------------- */
3076 /*NSTRACE (ns_send_appdefined); */
3078 /* Only post this event if we haven't already posted one. This will end
3079 the [NXApp run] main loop after having processed all events queued at
3081 if (send_appdefined
)
3085 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3086 send_appdefined
= NO
;
3088 /* Don't need wakeup timer any more */
3091 [timed_entry invalidate
];
3092 [timed_entry release
];
3096 /* Ditto for file descriptor poller */
3099 [fd_entry invalidate
];
3104 nxev
= [NSEvent otherEventWithType
: NSApplicationDefined
3105 location
: NSMakePoint
(0, 0)
3108 windowNumber
: [[NSApp mainWindow
] windowNumber
]
3109 context
: [NSApp context
]
3114 /* Post an application defined event on the event queue. When this is
3115 received the [NXApp run] will return, thus having processed all
3116 events which are currently queued. */
3117 [NSApp postEvent
: nxev atStart
: NO
];
3123 ns_read_socket
(struct terminal *terminal, int expected
,
3124 struct input_event
*hold_quit
)
3125 /* --------------------------------------------------------------------------
3126 External (hook): Post an event to ourself and keep reading events until
3127 we read it back again. In effect process all events which were waiting.
3128 From 21+ we have to manage the event buffer ourselves.
3129 -------------------------------------------------------------------------- */
3131 struct input_event ev
;
3133 static NSDate
*lastCheck
= nil
;
3135 /* NSTRACE (ns_read_socket); */
3137 if (interrupt_input_blocked
)
3139 interrupt_input_pending
= 1;
3141 pending_signals
= 1;
3146 interrupt_input_pending
= 0;
3148 pending_signals
= pending_atimers
;
3152 #ifdef COCOA_EXPERIMENTAL_CTRL_G
3153 /* causes Feval to abort; should probably set this in calling code when
3154 it IS actually called from signal handler (which is only the case
3155 under NS if SYNC_INPUT is off) */
3159 n_emacs_events_pending
= 0;
3162 q_event_ptr
= hold_quit
;
3164 /* we manage autorelease pools by allocate/reallocate each time around
3165 the loop; strict nesting is occasionally violated but seems not to
3166 matter.. earlier methods using full nesting caused major memory leaks */
3167 [outerpool release
];
3168 outerpool
= [[NSAutoreleasePool alloc
] init
];
3170 /* If have pending open-file requests, attend to the next one of those. */
3171 if (ns_pending_files
&& [ns_pending_files count
] != 0
3172 && [(EmacsApp
*)NSApp openFile
: [ns_pending_files objectAtIndex
: 0]])
3174 [ns_pending_files removeObjectAtIndex
: 0];
3176 /* Deal with pending service requests. */
3177 else if (ns_pending_service_names
&& [ns_pending_service_names count
] != 0
3179 NSApp fulfillService
: [ns_pending_service_names objectAtIndex
: 0]
3180 withArg
: [ns_pending_service_args objectAtIndex
: 0]])
3182 [ns_pending_service_names removeObjectAtIndex
: 0];
3183 [ns_pending_service_args removeObjectAtIndex
: 0];
3187 /* Run and wait for events. We must always send one NX_APPDEFINED event
3188 to ourself, otherwise [NXApp run] will never exit. */
3189 send_appdefined
= YES
;
3191 /* If called via ns_select, this is called once with expected=1,
3192 because we expect either the timeout or file descriptor activity.
3193 In this case the first event through will either be real input or
3194 one of these. read_avail_input() then calls once more with expected=0
3195 and in that case we need to return quickly if there is nothing.
3196 If we're being called outside of that, it's also OK to return quickly
3197 after one iteration through the event loop, since other terms do
3198 this and emacs expects it. */
3199 if (!(inNsSelect
&& expected
)) // (!inNsSelect ||
!expected
)
3201 /* Post an application defined event on the event queue. When this is
3202 received the [NXApp run] will return, thus having processed all
3203 events which are currently queued, if any. */
3204 ns_send_appdefined
(-1);
3210 nevents
= n_emacs_events_pending
;
3211 n_emacs_events_pending
= 0;
3212 emacs_event
= q_event_ptr
= NULL;
3214 #ifdef COCOA_EXPERIMENTAL_CTRL_G
3224 ns_select
(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
3225 fd_set
*exceptfds
, struct timeval
*timeout
)
3226 /* --------------------------------------------------------------------------
3227 Replacement for select, checking for events
3228 -------------------------------------------------------------------------- */
3233 /* NSTRACE (ns_select); */
3235 if (NSApp
== nil
/* || ([NSApp isActive] == NO &&
3236 [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil
3237 inMode:NSDefaultRunLoopMode dequeue:NO] == nil) */)
3238 return select
(nfds
, readfds
, writefds
, exceptfds
, timeout
);
3240 /* Save file descriptor set, which gets overwritten in calls to select ()
3241 Note, this is called from process.c, and only readfds is ever set */
3244 memcpy
(&select_readfds
, readfds
, sizeof
(fd_set
));
3250 /* Try an initial select for pending data on input files */
3251 select_timeout.tv_sec
= select_timeout.tv_usec
= 0;
3252 result
= select
(nfds
, readfds
, writefds
, exceptfds
, &select_timeout
);
3256 /* if (!timeout || timed_entry || fd_entry)
3257 fprintf (stderr, "assertion failed: timeout null or timed_entry/fd_entry non-null in ns_select\n"); */
3259 /* set a timeout and run the main AppKit event loop while continuing
3260 to monitor the files */
3261 time
= ((double) timeout
->tv_sec
) + ((double) timeout
->tv_usec
)/1000000.0;
3262 timed_entry
= [[NSTimer scheduledTimerWithTimeInterval
: time
3264 selector
: @selector
(timeout_handler
:)
3266 repeats
: YES
] /* for safe removal */
3269 /* set a periodic task to try the select () again */
3270 fd_entry
= [[NSTimer scheduledTimerWithTimeInterval
: 0.1
3272 selector
: @selector
(fd_handler
:)
3277 /* Let Application dispatch events until it receives an event of the type
3278 NX_APPDEFINED, which should only be sent by timeout_handler.
3279 We tell read_avail_input() that input is "expected" because we do expect
3280 either the timeout or fd handler to fire, and if they don't, the original
3281 call from process.c that got us here expects us to wait until some input
3285 ev
= last_appdefined_event
;
3291 if ([ev
type] != NSApplicationDefined
)
3295 last_appdefined_event
= 0;
3299 /* The NX_APPDEFINED event we received was a timeout. */
3304 /* The NX_APPDEFINED event we received was the result of
3305 at least one real input event arriving. */
3311 /* Received back from select () in fd_handler; copy the results */
3313 memcpy
(readfds
, &select_readfds
, sizeof
(fd_set
));
3317 /* never reached, shut compiler up */
3323 /* ==========================================================================
3327 ========================================================================== */
3331 ns_set_vertical_scroll_bar
(struct window
*window
,
3332 int portion
, int whole
, int position
)
3333 /* --------------------------------------------------------------------------
3334 External (hook): Update or add scrollbar
3335 -------------------------------------------------------------------------- */
3339 struct frame
*f
= XFRAME
(WINDOW_FRAME
(window
));
3340 EmacsView
*view = FRAME_NS_VIEW
(f
);
3341 int window_y
, window_height
;
3342 BOOL barOnVeryLeft
, barOnVeryRight
;
3343 int top
, left
, height
, width
, sb_width
, sb_left
;
3345 static int count
= 0;
3347 /* optimization; display engine sends WAY too many of these.. */
3348 if (!NILP
(window
->vertical_scroll_bar
))
3350 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3351 if ([bar checkSamePosition
: position portion
: portion whole
: whole
])
3353 if (view->scrollbarsNeedingUpdate
== 0)
3355 if (!windows_or_buffers_changed
)
3359 view->scrollbarsNeedingUpdate
--;
3363 NSTRACE
(ns_set_vertical_scroll_bar
);
3365 /* Get dimensions. */
3366 window_box
(window
, -1, 0, &window_y
, 0, &window_height
);
3368 height
= window_height
;
3369 width
= WINDOW_CONFIG_SCROLL_BAR_COLS
(window
) * FRAME_COLUMN_WIDTH
(f
);
3370 left
= WINDOW_SCROLL_BAR_AREA_X
(window
);
3372 if (top
< 5) /* top scrollbar adjustment */
3374 top
-= FRAME_INTERNAL_BORDER_WIDTH
(f
);
3375 height
+= FRAME_INTERNAL_BORDER_WIDTH
(f
);
3378 /* allow for displaying a skinnier scrollbar than char area allotted */
3379 sb_width
= (WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) > 0) ?
3380 WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) : width
;
3382 barOnVeryLeft
= left
< 5;
3383 barOnVeryRight
= FRAME_PIXEL_WIDTH
(f
) - left
- width
< 5;
3384 sb_left
= left
+ FRAME_INTERNAL_BORDER_WIDTH
(f
)
3385 * (barOnVeryLeft ?
-1 : (barOnVeryRight ?
1 : 0));
3387 r
= NSMakeRect
(sb_left
, top
, sb_width
, height
);
3388 /* the parent view is flipped, so we need to flip y value */
3390 r.origin.y
= (v.
size.height
- r.
size.height
- r.origin.y
);
3392 XSETWINDOW
(win
, window
);
3395 /* we want at least 5 lines to display a scrollbar */
3396 if (WINDOW_TOTAL_LINES
(window
) < 5)
3398 if (!NILP
(window
->vertical_scroll_bar
))
3400 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3401 [bar removeFromSuperview
];
3402 window
->vertical_scroll_bar
= Qnil
;
3404 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3409 if (NILP
(window
->vertical_scroll_bar
))
3411 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3412 bar = [[EmacsScroller alloc
] initFrame
: r window
: win
];
3413 window
->vertical_scroll_bar
= make_save_value
(bar, 0);
3418 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3419 oldRect
= [bar frame
];
3420 r.
size.width
= oldRect.
size.width
;
3421 if (FRAME_LIVE_P
(f
) && !NSEqualRects
(oldRect
, r
))
3423 if (oldRect.origin.x
!= r.origin.x
)
3424 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3429 [bar setPosition
: position portion
: portion whole
: whole
];
3435 ns_condemn_scroll_bars
(struct frame
*f
)
3436 /* --------------------------------------------------------------------------
3437 External (hook): arrange for all frame's scrollbars to be removed
3438 at next call to judge_scroll_bars, except for those redeemed.
3439 -------------------------------------------------------------------------- */
3443 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
3445 NSTRACE
(ns_condemn_scroll_bars
);
3447 for (i
=[subviews count
]-1; i
>= 0; i
--)
3449 view = [subviews objectAtIndex
: i
];
3450 if ([view isKindOfClass
: [EmacsScroller
class]])
3457 ns_redeem_scroll_bar
(struct window
*window
)
3458 /* --------------------------------------------------------------------------
3459 External (hook): arrange to spare this window's scrollbar
3460 at next call to judge_scroll_bars.
3461 -------------------------------------------------------------------------- */
3464 NSTRACE
(ns_redeem_scroll_bar
);
3465 if (!NILP
(window
->vertical_scroll_bar
))
3467 bar =XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3474 ns_judge_scroll_bars
(struct frame
*f
)
3475 /* --------------------------------------------------------------------------
3476 External (hook): destroy all scrollbars on frame that weren't
3477 redeemed after call to condemn_scroll_bars.
3478 -------------------------------------------------------------------------- */
3482 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
3483 NSTRACE
(ns_judge_scroll_bars
);
3484 for (i
=[subviews count
]-1; i
>= 0; i
--)
3486 view = [subviews objectAtIndex
: i
];
3487 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
3494 x_wm_set_icon_position
(struct frame
*f
, int icon_x
, int icon_y
)
3496 /* XXX irrelevant under NS */
3501 /* ==========================================================================
3505 ========================================================================== */
3508 x_display_pixel_height
(dpyinfo
)
3509 struct ns_display_info
*dpyinfo
;
3511 NSScreen
*screen
= [NSScreen mainScreen
];
3512 return [screen frame
].
size.height
;
3516 x_display_pixel_width
(dpyinfo
)
3517 struct ns_display_info
*dpyinfo
;
3519 NSScreen
*screen
= [NSScreen mainScreen
];
3520 return [screen frame
].
size.width
;
3524 static Lisp_Object ns_string_to_lispmod
(const
char *s
)
3525 /* --------------------------------------------------------------------------
3526 Convert modifier name to lisp symbol
3527 -------------------------------------------------------------------------- */
3529 if (!strncmp
(SDATA
(SYMBOL_NAME
(Qmeta
)), s
, 10))
3531 else if (!strncmp
(SDATA
(SYMBOL_NAME
(Qsuper
)), s
, 10))
3533 else if (!strncmp
(SDATA
(SYMBOL_NAME
(Qcontrol
)), s
, 10))
3535 else if (!strncmp
(SDATA
(SYMBOL_NAME
(Qalt
)), s
, 10))
3537 else if (!strncmp
(SDATA
(SYMBOL_NAME
(Qhyper
)), s
, 10))
3539 else if (!strncmp
(SDATA
(SYMBOL_NAME
(Qnone
)), s
, 10))
3546 static Lisp_Object ns_mod_to_lisp
(int m
)
3547 /* --------------------------------------------------------------------------
3548 Convert modifier code (see lisp.h) to lisp symbol
3549 -------------------------------------------------------------------------- */
3553 else if (m
== CHAR_SUPER
)
3555 else if (m
== CHAR_CTL
)
3557 else if (m
== CHAR_ALT
)
3559 else if (m
== CHAR_HYPER
)
3561 else /* if (m == 0) */
3567 ns_set_default_prefs
()
3568 /* --------------------------------------------------------------------------
3569 Initialize preference variables to defaults
3570 -------------------------------------------------------------------------- */
3572 ns_alternate_modifier
= Qmeta
;
3573 ns_command_modifier
= Qsuper
;
3574 ns_control_modifier
= Qcontrol
;
3575 ns_function_modifier
= Qnone
;
3576 ns_expand_space
= make_float
(0.0);
3577 ns_antialias_text
= Qt
;
3578 ns_antialias_threshold
= 10.0; /* not exposed to lisp side */
3579 ns_use_qd_smoothing
= Qnil
;
3580 ns_use_system_highlight_color
= Qt
;
3581 ns_confirm_quit
= Qnil
;
3586 ns_default
(const
char *parameter
, Lisp_Object
*result
,
3587 Lisp_Object yesval
, Lisp_Object noval
,
3588 BOOL is_float
, BOOL is_modstring
)
3589 /* --------------------------------------------------------------------------
3590 Check a parameter value in user's preferences
3591 -------------------------------------------------------------------------- */
3595 if ( (value
=[[[NSUserDefaults standardUserDefaults
]
3596 stringForKey
: [NSString stringWithUTF8String
: parameter
]]
3601 if (strcasecmp
(value
, "YES"
) == 0)
3603 else if (strcasecmp
(value
, "NO"
) == 0)
3605 else if (is_float
&& (f
= strtod
(value
, &pos
), pos
!= value
))
3606 *result
= make_float
(f
);
3607 else if (is_modstring
&& value
)
3608 *result
= ns_string_to_lispmod
(value
);
3609 else fprintf (stderr
,
3610 "Bad value
for default
\"%s\": \"%s\"\n", parameter, value);
3616 ns_initialize_display_info
(struct ns_display_info
*dpyinfo
)
3617 /* --------------------------------------------------------------------------
3618 Initialize global info and storage for display.
3619 -------------------------------------------------------------------------- */
3621 NSScreen
*screen
= [NSScreen mainScreen
];
3622 NSWindowDepth depth
= [screen depth
];
3624 dpyinfo
->resx
= 72.27; /* used 75.0, but this makes pt == pixel, expected */
3625 dpyinfo
->resy
= 72.27;
3626 dpyinfo
->color_p
= ![NSDeviceWhiteColorSpace isEqualToString
:
3627 NSColorSpaceFromDepth
(depth
)]
3628 && ![NSCalibratedWhiteColorSpace isEqualToString
:
3629 NSColorSpaceFromDepth
(depth
)];
3630 dpyinfo
->n_planes
= NSBitsPerPixelFromDepth
(depth
);
3631 dpyinfo
->image_cache
= make_image_cache
();
3632 dpyinfo
->color_table
3633 = (struct ns_color_table
*)xmalloc
(sizeof
(struct ns_color_table
));
3634 dpyinfo
->color_table
->colors
= NULL;
3635 dpyinfo
->root_window
= 42; /* a placeholder.. */
3637 dpyinfo
->mouse_face_mouse_frame
= NULL;
3638 dpyinfo
->mouse_face_deferred_gc
= 0;
3639 dpyinfo
->mouse_face_beg_row
= dpyinfo
->mouse_face_beg_col
= -1;
3640 dpyinfo
->mouse_face_end_row
= dpyinfo
->mouse_face_end_col
= -1;
3641 dpyinfo
->mouse_face_face_id
= DEFAULT_FACE_ID
;
3642 dpyinfo
->mouse_face_window
= dpyinfo
->mouse_face_overlay
= Qnil
;
3643 dpyinfo
->mouse_face_hidden
= 0;
3645 dpyinfo
->mouse_face_mouse_x
= dpyinfo
->mouse_face_mouse_y
= 0;
3646 dpyinfo
->mouse_face_defer
= 0;
3648 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
= NULL;
3650 dpyinfo
->n_fonts
= 0;
3651 dpyinfo
->smallest_font_height
= 1;
3652 dpyinfo
->smallest_char_width
= 1;
3656 /* This and next define (many of the) public functions in this file. */
3657 /* x_... are generic versions in xdisp.c that we, and other terms, get away
3658 with using despite presence in the "system dependent" redisplay
3659 interface. In addition, many of the ns_ methods have code that is
3660 shared with all terms, indicating need for further refactoring. */
3661 extern frame_parm_handler ns_frame_parm_handlers
[];
3662 static
struct redisplay_interface ns_redisplay_interface
=
3664 ns_frame_parm_handlers
,
3668 x_clear_end_of_line
,
3670 ns_after_update_window_line
,
3671 ns_update_window_begin
,
3672 ns_update_window_end
,
3675 0, /* flush_display_optional */
3676 x_clear_window_mouse_face
,
3677 x_get_glyph_overhangs
,
3678 x_fix_overlapping_area
,
3679 ns_draw_fringe_bitmap
,
3680 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
3681 0, /* destroy_fringe_bitmap */
3682 ns_compute_glyph_string_overhangs
,
3683 ns_draw_glyph_string
, /* interface to nsfont.m */
3684 ns_define_frame_cursor
,
3685 ns_clear_frame_area
,
3686 ns_draw_window_cursor
,
3687 ns_draw_vertical_window_border
,
3688 ns_shift_glyphs_for_insert
3693 ns_delete_display
(struct ns_display_info
*dpyinfo
)
3699 /* This function is called when the last frame on a display is deleted. */
3701 ns_delete_terminal
(struct terminal *terminal)
3703 struct ns_display_info
*dpyinfo
= terminal->display_info.ns
;
3706 /* Protect against recursive calls. delete_frame in
3707 delete_terminal calls us back when it deletes our last frame. */
3708 if (!terminal->name
)
3713 x_destroy_all_bitmaps
(dpyinfo
);
3714 ns_delete_display
(dpyinfo
);
3719 static
struct terminal *
3720 ns_create_terminal
(struct ns_display_info
*dpyinfo
)
3721 /* --------------------------------------------------------------------------
3722 Set up use of NS before we make the first connection.
3723 -------------------------------------------------------------------------- */
3725 struct terminal *terminal;
3727 NSTRACE
(ns_create_terminal
);
3729 terminal = create_terminal
();
3731 terminal->type = output_ns
;
3732 terminal->display_info.ns
= dpyinfo
;
3733 dpyinfo
->terminal = terminal;
3735 terminal->rif
= &ns_redisplay_interface
;
3737 terminal->clear_frame_hook
= ns_clear_frame
;
3738 terminal->ins_del_lines_hook
= 0; /* XXX vestigial? */
3739 terminal->delete_glyphs_hook
= 0; /* XXX vestigial? */
3740 terminal->ring_bell_hook
= ns_ring_bell
;
3741 terminal->reset_terminal_modes_hook
= ns_reset_terminal_modes
;
3742 terminal->set_terminal_modes_hook
= ns_set_terminal_modes
;
3743 terminal->update_begin_hook
= ns_update_begin
;
3744 terminal->update_end_hook
= ns_update_end
;
3745 terminal->set_terminal_window_hook
= NULL; /* XXX vestigial? */
3746 terminal->read_socket_hook
= ns_read_socket
;
3747 terminal->frame_up_to_date_hook
= ns_frame_up_to_date
;
3748 terminal->mouse_position_hook
= ns_mouse_position
;
3749 terminal->frame_rehighlight_hook
= ns_frame_rehighlight
;
3750 terminal->frame_raise_lower_hook
= ns_frame_raise_lower
;
3752 terminal->fullscreen_hook
= 0; /* see XTfullscreen_hook */
3754 terminal->set_vertical_scroll_bar_hook
= ns_set_vertical_scroll_bar
;
3755 terminal->condemn_scroll_bars_hook
= ns_condemn_scroll_bars
;
3756 terminal->redeem_scroll_bar_hook
= ns_redeem_scroll_bar
;
3757 terminal->judge_scroll_bars_hook
= ns_judge_scroll_bars
;
3759 terminal->delete_frame_hook
= x_destroy_window
;
3760 terminal->delete_terminal_hook
= ns_delete_terminal
;
3762 terminal->scroll_region_ok
= 1;
3763 terminal->char_ins_del_ok
= 1;
3764 terminal->line_ins_del_ok
= 1;
3765 terminal->fast_clear_end_of_line
= 1;
3766 terminal->memory_below_frame
= 0;
3772 struct ns_display_info
*
3773 ns_term_init
(Lisp_Object display_name
)
3774 /* --------------------------------------------------------------------------
3775 Start the Application and get things rolling.
3776 -------------------------------------------------------------------------- */
3778 struct terminal *terminal;
3779 struct ns_display_info
*dpyinfo
;
3780 static int ns_initialized
= 0;
3783 NSTRACE
(ns_term_init
);
3785 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
3786 /*GSDebugAllocationActive (YES); */
3788 handling_signal
= 0;
3790 if (!ns_initialized
)
3793 Fset_input_interrupt_mode
(Qnil
);
3797 ns_pending_files
= [[NSMutableArray alloc
] init
];
3798 ns_pending_service_names
= [[NSMutableArray alloc
] init
];
3799 ns_pending_service_args
= [[NSMutableArray alloc
] init
];
3801 /* Start app and create the main menu, window, view.
3802 Needs to be here because ns_initialize_display_info () uses AppKit classes.
3803 The view will then ask the NSApp to stop and return to Emacs. */
3804 [EmacsApp sharedApplication
];
3807 [NSApp setDelegate
: NSApp
];
3809 /* debugging: log all notifications */
3810 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
3811 selector: @selector (logNotification:)
3812 name: nil object: nil]; */
3814 dpyinfo
= (struct ns_display_info
*)xmalloc
(sizeof
(struct ns_display_info
));
3815 bzero
(dpyinfo
, sizeof
(struct ns_display_info
));
3817 ns_initialize_display_info
(dpyinfo
);
3818 terminal = ns_create_terminal
(dpyinfo
);
3820 terminal->kboard
= (KBOARD
*) xmalloc
(sizeof
(KBOARD
));
3821 init_kboard
(terminal->kboard
);
3822 terminal->kboard
->Vwindow_system
= Qns
;
3823 terminal->kboard
->next_kboard
= all_kboards
;
3824 all_kboards
= terminal->kboard
;
3825 /* Don't let the initial kboard remain current longer than necessary.
3826 That would cause problems if a file loaded on startup tries to
3827 prompt in the mini-buffer. */
3828 if (current_kboard
== initial_kboard
)
3829 current_kboard
= terminal->kboard
;
3830 terminal->kboard
->reference_count
++;
3832 dpyinfo
->next
= x_display_list
;
3833 x_display_list
= dpyinfo
;
3835 /* Put it on ns_display_name_list */
3836 ns_display_name_list
= Fcons
(Fcons
(display_name
, Qnil
),
3837 ns_display_name_list
);
3838 dpyinfo
->name_list_element
= XCAR
(ns_display_name_list
);
3840 /* Set the name of the terminal. */
3841 terminal->name
= (char *) xmalloc
(SBYTES
(display_name
) + 1);
3842 strncpy
(terminal->name
, SDATA
(display_name
), SBYTES
(display_name
));
3843 terminal->name
[SBYTES
(display_name
)] = 0;
3847 /* Read various user defaults. */
3848 ns_set_default_prefs
();
3849 if (!ns_no_defaults
)
3851 ns_default
("AlternateModifier"
, &ns_alternate_modifier
,
3852 Qnil
, Qnil
, NO
, YES
);
3853 if (NILP
(ns_alternate_modifier
))
3854 ns_alternate_modifier
= Qmeta
;
3855 ns_default
("CommandModifier"
, &ns_command_modifier
,
3856 Qnil
, Qnil
, NO
, YES
);
3857 if (NILP
(ns_command_modifier
))
3858 ns_command_modifier
= Qsuper
;
3859 ns_default
("ControlModifier"
, &ns_control_modifier
,
3860 Qnil
, Qnil
, NO
, YES
);
3861 if (NILP
(ns_control_modifier
))
3862 ns_control_modifier
= Qcontrol
;
3863 ns_default
("FunctionModifier"
, &ns_function_modifier
,
3864 Qnil
, Qnil
, NO
, YES
);
3865 if (NILP
(ns_function_modifier
))
3866 ns_function_modifier
= Qnone
;
3867 ns_default
("ExpandSpace"
, &ns_expand_space
,
3868 make_float
(0.5), make_float
(0.0), YES
, NO
);
3869 ns_default
("GSFontAntiAlias"
, &ns_antialias_text
,
3872 ns_default
("AppleAntiAliasingThreshold"
, &tmp
,
3873 make_float
(10.0), make_float
(6.0), YES
, NO
);
3874 ns_antialias_threshold
= NILP
(tmp
) ?
10.0 : XFLOATINT
(tmp
);
3875 ns_default
("UseQuickdrawSmoothing"
, &ns_use_qd_smoothing
,
3877 ns_default
("UseSystemHighlightColor"
, &ns_use_system_highlight_color
,
3879 ns_default
("ConfirmQuit"
, &ns_confirm_quit
,
3883 if (EQ
(ns_use_system_highlight_color
, Qt
))
3885 ns_selection_color
= [[NSUserDefaults standardUserDefaults
]
3886 stringForKey
: @"AppleHighlightColor"
];
3887 if (ns_selection_color
== nil
)
3888 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
3891 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
3894 NSColorList
*cl
= [NSColorList colorListNamed
: @"Emacs"
];
3898 Lisp_Object color_file
, color_map
, color
;
3903 color_file
= Fexpand_file_name
(build_string
("rgb.txt"
),
3904 Fsymbol_value
(intern
("data
-directory"
)));
3905 if (NILP
(Ffile_readable_p
(color_file
)))
3906 fatal
("Could not
find %s.\n", SDATA (color_file));
3908 color_map
= Fx_load_color_file
(color_file
);
3909 if (NILP
(color_map
))
3910 fatal
("Could not read
%s.\n", SDATA (color_file));
3912 cl
= [[NSColorList alloc
] initWithName
: @"Emacs"
];
3913 for ( ; CONSP
(color_map
); color_map
= XCDR
(color_map
))
3915 color
= XCAR
(color_map
);
3916 name
= SDATA
(XCAR
(color
));
3917 c
= XINT
(XCDR
(color
));
3919 [NSColor colorWithCalibratedRed
: RED_FROM_ULONG
(c
) / 255.0
3920 green
: GREEN_FROM_ULONG
(c
) / 255.0
3921 blue
: BLUE_FROM_ULONG
(c
) / 255.0
3923 forKey
: [NSString stringWithUTF8String
: name
]];
3925 [cl writeToFile
: nil
];
3931 #ifdef NS_IMPL_GNUSTEP
3932 strncpy
(c
, gnustep_base_version
, sizeof
(c
));
3934 /*PSnextrelease (128, c); */
3935 snprintf
(c
, sizeof
(c
), "
%g", NSAppKitVersionNumber);
3937 Vwindow_system_version
= build_string
(c
);
3940 delete_keyboard_wait_descriptor
(0);
3942 /* Set up OS X app menu */
3943 #ifdef NS_IMPL_COCOA
3947 /* set up the application menu */
3948 svcsMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Services"
];
3949 [svcsMenu setAutoenablesItems
: NO
];
3950 appMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Emacs"
];
3951 [appMenu setAutoenablesItems
: NO
];
3952 mainMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
3953 dockMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
3955 [appMenu insertItemWithTitle
: @"About Emacs"
3956 action
: @selector
(orderFrontStandardAboutPanel
:)
3959 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 1];
3960 [appMenu insertItemWithTitle
: @"Preferences..."
3961 action
: @selector
(showPreferencesWindow
:)
3964 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 3];
3965 item
= [appMenu insertItemWithTitle
: @"Services"
3966 action
: @selector
(menuDown
:)
3969 [appMenu setSubmenu
: svcsMenu forItem
: item
];
3970 /* [svcsMenu setSupercell: item]; */
3971 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 5];
3972 [appMenu insertItemWithTitle
: @"Hide Emacs"
3973 action
: @selector
(hide
:)
3976 item
= [appMenu insertItemWithTitle
: @"Hide Others"
3977 action
: @selector
(hideOtherApplications
:)
3980 [item setKeyEquivalentModifierMask
: NSCommandKeyMask | NSAlternateKeyMask
];
3981 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 8];
3982 [appMenu insertItemWithTitle
: @"
Quit Emacs"
3983 action
: @selector
(terminate
:)
3987 item
= [mainMenu insertItemWithTitle
: @"Emacs"
3988 action
: @selector
(menuDown
:)
3991 [mainMenu setSubmenu
: appMenu forItem
: item
];
3992 [dockMenu insertItemWithTitle
: @"New Frame"
3993 action
: @selector
(newFrame
:)
3997 [NSApp setMainMenu
: mainMenu
];
3998 [NSApp setAppleMenu
: appMenu
];
3999 [NSApp setServicesMenu
: svcsMenu
];
4000 /* Needed at least on Cocoa, to get dock menu to show windows */
4001 [NSApp setWindowsMenu
: [[NSMenu alloc
] init
]];
4003 #endif
/* MAC OS X menu setup */
4011 extern Lisp_Object Vauto_save_list_file_name
;
4013 ns_term_shutdown
(int sig
)
4015 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4016 if (STRINGP
(Vauto_save_list_file_name
))
4017 unlink
(SDATA
(Vauto_save_list_file_name
));
4019 if (sig
== 0 || sig
== SIGTERM
)
4021 ns_shutdown_properly
= YES
;
4022 [NSApp terminate
: NSApp
];
4024 else // force a stack
trace to happen
4031 /* ==========================================================================
4033 EmacsApp implementation
4035 ========================================================================== */
4038 @implementation EmacsApp
4040 - (void
)logNotification
: (NSNotification
*)notification
4042 const
char *name
= [[notification name
] UTF8String
];
4043 if (!strstr
(name
, "Update"
) && !strstr
(name
, "NSMenu"
)
4044 && !strstr
(name
, "WindowNumber"
))
4045 NSLog
(@"notification
: '%@'"
, [notification name
]);
4049 - (void
)sendEvent
: (NSEvent
*)theEvent
4050 /* --------------------------------------------------------------------------
4051 Events posted by ns_send_appdefined interrupt the run loop here
4052 -------------------------------------------------------------------------- */
4054 int
type = [theEvent
type];
4055 NSWindow
*window
= [theEvent window
];
4056 /* NSTRACE (sendEvent); */
4057 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4059 if (type == NSCursorUpdate
&& window
== nil
)
4061 fprintf (stderr
, "Dropping external cursor update event.
\n"
);
4065 #ifdef NS_IMPL_COCOA
4066 /* pass mouse down in resize handle and subsequent drags directly to
4067 EmacsWindow so we can generate continuous redisplays */
4070 if (type == NSLeftMouseDragged
)
4072 [window mouseDragged
: theEvent
];
4075 else if (type == NSLeftMouseUp
)
4077 [window mouseUp
: theEvent
];
4081 else if (type == NSLeftMouseDown
)
4083 NSRect r
= ns_resize_handle_rect
(window
);
4084 if (NSPointInRect
([theEvent locationInWindow
], r
))
4087 [window mouseDown
: theEvent
];
4093 if (type == NSApplicationDefined
)
4095 last_appdefined_event
= theEvent
;
4099 [super sendEvent
: theEvent
];
4103 - (void
)showPreferencesWindow
: (id
)sender
4105 if (prefsController
== nil
)
4106 prefsController
= [[EmacsPrefsController alloc
] init
];
4107 [prefsController showForFrame
: SELECTED_FRAME
()];
4111 - (void
)newFrame
: (id
)sender
4113 struct frame
*emacsframe
= SELECTED_FRAME
();
4114 NSEvent
*theEvent
= [NSApp currentEvent
];
4118 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4119 emacs_event
->code
= KEY_NS_NEW_FRAME
;
4120 emacs_event
->modifiers
= 0;
4121 EV_TRAILER
(theEvent
);
4125 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4126 - (BOOL
) openFile
: (NSString
*)fileName
4128 struct frame
*emacsframe
= SELECTED_FRAME
();
4129 NSEvent
*theEvent
= [NSApp currentEvent
];
4134 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4135 emacs_event
->code
= KEY_NS_OPEN_FILE_LINE
;
4136 ns_input_file
= append2
(ns_input_file
, build_string
([fileName UTF8String
]));
4137 ns_input_line
= Qnil
; /* can be start or cons start,end */
4138 emacs_event
->modifiers
=0;
4139 EV_TRAILER
(theEvent
);
4145 /* **************************************************************************
4147 EmacsApp delegate implementation
4149 ************************************************************************** */
4151 - (void
)applicationDidFinishLaunching
: (NSNotification
*)notification
4152 /* --------------------------------------------------------------------------
4153 When application is loaded, terminate event loop in ns_term_init
4154 -------------------------------------------------------------------------- */
4156 NSTRACE
(applicationDidFinishLaunching
);
4157 [NSApp setServicesProvider
: NSApp
];
4158 ns_send_appdefined
(-2);
4162 /* Termination sequences (ns_shutdown_properly):
4165 MenuBar | File | Exit:
4167 received -terminate: 1
4168 received -appShouldTerminate: 1
4170 Select Quit from App menubar:
4171 received -terminate: 0
4173 received -terminate: 1
4174 received -appShouldTerminate: 1
4176 Select Quit from Dock menu:
4178 received -appShouldTerminate: 0
4179 Cancel -> Nothing else
4181 received -terminate: 0
4183 received -terminate: 1
4184 received -appShouldTerminate: 1
4187 - (void
) terminate
: (id
)sender
4189 if (ns_shutdown_properly
)
4190 [super terminate
: sender
];
4193 struct frame
*emacsframe
= SELECTED_FRAME
();
4198 ns_shutdown_properly
= YES
;
4199 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4200 emacs_event
->code
= KEY_NS_POWER_OFF
;
4201 EV_TRAILER
((id
)nil
);
4206 - (NSApplicationTerminateReply
)applicationShouldTerminate
: (id
)sender
4210 if (ns_shutdown_properly || NILP
(ns_confirm_quit
))
4211 return NSTerminateNow
;
4213 /* XXX: This while() loop is needed because if the user switches to another
4214 application while the panel is up, it is taken down w/a return value
4215 of -1000, and the event queue gets messed up. In this case resend
4216 the appdefined and put up the window again. */
4218 ret
= NSRunAlertPanel
([[NSProcessInfo processInfo
] processName
],
4219 [NSString stringWithUTF8String
:"Exit requested. Would you like to
Save Buffers and Exit
, or Cancel the request?"
],
4220 @"
Save Buffers and Exit"
, @"Cancel"
, nil
);
4222 if (ret
== NSAlertDefaultReturn
)
4224 send_appdefined
= YES
;
4225 ns_send_appdefined
(-1);
4226 return NSTerminateNow
;
4228 else if (ret
== NSAlertAlternateReturn
)
4230 send_appdefined
= YES
;
4231 ns_send_appdefined
(-1);
4232 return NSTerminateCancel
;
4238 /* Notification from the Workspace to open a file */
4239 - (BOOL
)application
: sender openFile
: (NSString
*)file
4241 [ns_pending_files addObject
: file
];
4246 /* Open a file as a temporary file */
4247 - (BOOL
)application
: sender openTempFile
: (NSString
*)file
4249 [ns_pending_files addObject
: file
];
4254 /* Notification from the Workspace to open a file noninteractively (?) */
4255 - (BOOL
)application
: sender openFileWithoutUI
: (NSString
*)file
4257 [ns_pending_files addObject
: file
];
4262 /* Notification from the Workspace to open multiple files */
4263 - (void
)application
: sender openFiles
: (NSArray
*)fileList
4265 NSEnumerator
*files
= [fileList objectEnumerator
];
4267 while ((file
= [files nextObject
]) != nil
)
4268 [ns_pending_files addObject
: file
];
4270 /* TODO: when GNUstep implements this (and we require that version of
4271 GNUstep), remove. */
4272 #ifndef NS_IMPL_GNUSTEP
4273 [self replyToOpenOrPrint
: NSApplicationDelegateReplySuccess
];
4274 #endif
/* !NS_IMPL_GNUSTEP */
4279 /* Handle dock menu requests. */
4280 - (NSMenu
*)applicationDockMenu
: (NSApplication
*) sender
4286 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4287 - (void
)applicationWillBecomeActive
: (NSNotification
*)notification
4289 //ns_app_active
=YES
;
4291 - (void
)applicationDidBecomeActive
: (NSNotification
*)notification
4293 //ns_app_active
=YES
;
4295 - (void
)applicationDidResignActive
: (NSNotification
*)notification
4298 ns_send_appdefined
(-1);
4303 /* ==========================================================================
4305 EmacsApp aux handlers for managing event loop
4307 ========================================================================== */
4310 - (void
)timeout_handler
: (NSTimer
*)timedEntry
4311 /* --------------------------------------------------------------------------
4312 The timeout specified to ns_select has passed.
4313 -------------------------------------------------------------------------- */
4315 /*NSTRACE (timeout_handler); */
4316 ns_send_appdefined
(-2);
4319 extern void update_window_cursor
(struct window
*w
, int on
);
4321 - (void
)fd_handler
: (NSTimer
*) fdEntry
4322 /* --------------------------------------------------------------------------
4323 Check data waiting on file descriptors and terminate if so
4324 -------------------------------------------------------------------------- */
4327 /* NSTRACE (fd_handler); */
4329 if (select_nfds
== 0)
4332 memcpy
(&t_readfds
, &select_readfds
, sizeof
(fd_set
));
4334 select_timeout.tv_sec
= select_timeout.tv_usec
= 0;
4335 result
= select
(select_nfds
, &t_readfds
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
4339 memcpy
(&select_readfds
, &t_readfds
, sizeof
(fd_set
));
4340 ns_send_appdefined
(result
);
4346 /* ==========================================================================
4350 ========================================================================== */
4352 /* called from system: queue for next pass through event loop */
4353 - (void
)requestService
: (NSPasteboard
*)pboard
4354 userData
: (NSString
*)userData
4355 error: (NSString
**)error
4357 [ns_pending_service_names addObject
: userData
];
4358 [ns_pending_service_args addObject
: [NSString stringWithUTF8String
:
4359 SDATA
(ns_string_from_pasteboard
(pboard
))]];
4363 /* called from ns_read_socket to clear queue */
4364 - (BOOL
)fulfillService
: (NSString
*)name withArg
: (NSString
*)arg
4366 struct frame
*emacsframe
= SELECTED_FRAME
();
4367 NSEvent
*theEvent
= [NSApp currentEvent
];
4372 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4373 emacs_event
->code
= KEY_NS_SPI_SERVICE_CALL
;
4374 ns_input_spi_name
= build_string
([name UTF8String
]);
4375 ns_input_spi_arg
= build_string
([arg UTF8String
]);
4376 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
4377 EV_TRAILER
(theEvent
);
4387 /* ==========================================================================
4389 EmacsView implementation
4391 ========================================================================== */
4394 @implementation EmacsView
4396 /* needed to inform when window closed from LISP */
4397 - (void
) setWindowClosing
: (BOOL
)closing
4399 windowClosing
= closing
;
4405 NSTRACE
(EmacsView_dealloc
);
4411 /* called on font panel selection */
4412 - (void
)changeFont
: (id
)sender
4414 NSEvent
*e
=[[self window
] currentEvent
];
4415 struct face
*face
=FRAME_DEFAULT_FACE
(emacsframe
);
4419 NSTRACE
(changeFont
);
4423 if (newFont
= [sender convertFont
:
4424 ((struct nsfont_info
*)face
->font
)->nsfont
])
4426 SET_FRAME_GARBAGED
(emacsframe
); /* now needed as of 2008/10 */
4428 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4429 emacs_event
->modifiers
= 0;
4430 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
4432 size = [newFont pointSize
];
4433 ns_input_fontsize
= make_number
(lrint
(size));
4434 ns_input_font
= build_string
([[newFont familyName
] UTF8String
]);
4440 - (BOOL
)acceptsFirstResponder
4442 NSTRACE
(acceptsFirstResponder
);
4447 - (void
)resetCursorRects
4449 NSRect visible
= [self visibleRect
];
4450 NSCursor
*currentCursor
= FRAME_POINTER_TYPE
(emacsframe
);
4451 NSTRACE
(resetCursorRects
);
4453 if (currentCursor
== nil
)
4454 currentCursor
= [NSCursor arrowCursor
];
4456 if (!NSIsEmptyRect
(visible
))
4457 [self addCursorRect
: visible cursor
: currentCursor
];
4458 [currentCursor setOnMouseEntered
: YES
];
4463 /*****************************************************************************/
4464 /* Keyboard handling. */
4467 - (void
)keyDown
: (NSEvent
*)theEvent
4469 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
4471 unsigned fnKeysym
= 0;
4473 static NSMutableArray
*nsEvArray
;
4474 static BOOL firstTime
= YES
;
4478 /* Rhapsody and OS X give up and down events for the arrow keys */
4479 if (ns_fake_keydown
== YES
)
4480 ns_fake_keydown
= NO
;
4481 else if ([theEvent
type] != NSKeyDown
)
4487 if (![[self window
] isKeyWindow
])
4489 /* XXX: Using NO_SOCK_SIGIO like Carbon causes a condition in which,
4490 when Emacs display updates a different frame from the current one,
4491 and temporarily selects it, then processes some interrupt-driven
4492 input (dispnew.c:3878), OS will send the event to the correct NSWindow,
4493 but for some reason that window has its first responder set to the
4494 NSView most recently updated (I guess), which is not the correct one.
4495 UPDATE: After multi-TTY merge this happens even w/o NO_SOCK_SIGIO */
4496 if ([[theEvent window
] isKindOfClass
: [EmacsWindow
class]])
4497 [(EmacsView
*)[[theEvent window
] delegate
] keyDown
: theEvent
];
4501 if (nsEvArray
== nil
)
4502 nsEvArray
= [[NSMutableArray alloc
] initWithCapacity
: 1];
4504 [NSCursor setHiddenUntilMouseMoves
: YES
];
4506 if (dpyinfo
->mouse_face_hidden
&& INTEGERP
(Vmouse_highlight
))
4508 clear_mouse_face
(dpyinfo
);
4509 dpyinfo
->mouse_face_hidden
= 1;
4512 if (!processingCompose
)
4514 code
= ([[theEvent charactersIgnoringModifiers
] length] == 0) ?
4515 0 : [[theEvent charactersIgnoringModifiers
] characterAtIndex
: 0];
4516 /* (Carbon way: [theEvent keyCode]) */
4518 /* is it a "function key"? */
4519 fnKeysym
= ns_convert_key
(code
);
4522 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
4523 because Emacs treats Delete and KP-Delete same (in simple.el). */
4524 if (fnKeysym
== 0xFFFF && [theEvent keyCode
] == 0x33)
4525 code
= 0xFF08; /* backspace */
4530 /* are there modifiers? */
4531 emacs_event
->modifiers
= 0;
4532 flags
= [theEvent modifierFlags
];
4534 if (flags
& NSHelpKeyMask
)
4535 emacs_event
->modifiers |
= hyper_modifier
;
4537 if (flags
& NSShiftKeyMask
)
4538 emacs_event
->modifiers |
= shift_modifier
;
4540 if (flags
& NSCommandKeyMask
)
4542 emacs_event
->modifiers |
= parse_solitary_modifier
(ns_command_modifier
);
4543 /* if super (default), take input manager's word so things like
4544 dvorak / qwerty layout work */
4545 if (EQ
(ns_command_modifier
, Qsuper
)
4547 && [[theEvent characters
] length] != 0)
4549 /* XXX: the code we get will be unshifted, so if we have
4550 a shift modifier, must convert ourselves */
4551 if (!(flags
& NSShiftKeyMask
))
4552 code
= [[theEvent characters
] characterAtIndex
: 0];
4554 /* this is ugly and also requires linking w/Carbon framework
4555 (for LMGetKbdType) so for now leave this rare (?) case
4556 undealt with.. in future look into CGEvent methods */
4559 long smv
= GetScriptManagerVariable
(smKeyScript
);
4560 Handle uchrHandle
= GetResource
4561 ('uchr', GetScriptVariable
(smv
, smScriptKeys
));
4563 UCKeyTranslate
((UCKeyboardLayout
*)*uchrHandle
,
4564 [[theEvent characters
] characterAtIndex
: 0],
4565 kUCKeyActionDisplay
,
4566 (flags
& ~NSCommandKeyMask
) >> 8,
4567 LMGetKbdType
(), kUCKeyTranslateNoDeadKeysMask
,
4568 &dummy
, 1, &dummy
, &code
);
4575 if (flags
& NSControlKeyMask
)
4576 emacs_event
->modifiers |
=
4577 parse_solitary_modifier
(ns_control_modifier
);
4579 if (flags
& NS_FUNCTION_KEY_MASK
&& !fnKeysym
)
4580 emacs_event
->modifiers |
=
4581 parse_solitary_modifier
(ns_function_modifier
);
4583 if (flags
& NSAlternateKeyMask
) /* default = meta */
4585 if ((NILP
(ns_alternate_modifier
) || EQ
(ns_alternate_modifier
, Qnone
))
4587 { /* accept pre-interp alt comb */
4588 if ([[theEvent characters
] length] > 0)
4589 code
= [[theEvent characters
] characterAtIndex
: 0];
4590 /*HACK: clear lone shift modifier to stop next if from firing */
4591 if (emacs_event
->modifiers
== shift_modifier
)
4592 emacs_event
->modifiers
= 0;
4595 emacs_event
->modifiers |
=
4596 parse_solitary_modifier
(ns_alternate_modifier
);
4600 fprintf (stderr
, "keyDown
: code
=%x\tfnKey =%x\tflags = %x\tmods = %x\n",
4601 code
, fnKeysym
, flags
, emacs_event
->modifiers
);
4603 /* if it was a function key or had modifiers, pass it directly to emacs */
4604 if (fnKeysym ||
(emacs_event
->modifiers
4605 && [[theEvent charactersIgnoringModifiers
] length] > 0))
4606 /*[[theEvent characters] length] */
4608 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4610 code |
= (1<<28)|
(3<<16);
4611 else if (code
== 0x7f)
4612 code |
= (1<<28)|
(3<<16);
4614 emacs_event
->kind
= code
> 0xFF
4615 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
4617 emacs_event
->code
= code
;
4618 EV_TRAILER
(theEvent
);
4623 /* if we get here we should send the key for input manager processing */
4624 if (firstTime
&& [[NSInputManager currentInputManager
]
4625 wantsToDelayTextChangeNotifications
] == NO
)
4627 "Emacs
: WARNING
: TextInput mgr wants marked
text to be permanent
!\n"
);
4630 if (NS_KEYLOG
&& !processingCompose
)
4631 fprintf (stderr
, "keyDown
: Begin compose sequence.
\n"
);
4633 processingCompose
= YES
;
4634 [nsEvArray addObject
: theEvent
];
4635 [self interpretKeyEvents
: nsEvArray
];
4636 [nsEvArray removeObject
: theEvent
];
4640 #ifdef NS_IMPL_COCOA
4641 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
4642 decided not to send key-down for.
4643 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
4644 If it matches one of these, send it on to keyDown. */
4645 -(void
)keyUp
: (NSEvent
*)theEvent
4647 int flags
= [theEvent modifierFlags
];
4648 int code
= [theEvent keyCode
];
4649 if (code
== 0x30 && (flags
& NSControlKeyMask
) && !(flags
& NSCommandKeyMask
))
4652 fprintf (stderr
, "keyUp
: passed test"
);
4653 ns_fake_keydown
= YES
;
4654 [self keyDown
: theEvent
];
4660 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
4663 /* <NSTextInput>: called when done composing;
4664 NOTE: also called when we delete over working text, followed immed.
4665 by doCommandBySelector: deleteBackward: */
4666 - (void
)insertText
: (id
)aString
4669 int len
= [(NSString
*)aString
length];
4673 NSLog
(@"insertText
'%@'\tlen
= %d", aString, len);
4674 processingCompose
= NO
;
4679 /* first, clear any working text */
4680 if (workingText
!= nil
)
4681 [self deleteWorkingText
];
4683 /* now insert the string as keystrokes */
4684 for (i
=0; i
<len
; i
++)
4686 code
= [aString characterAtIndex
: i
];
4687 /* TODO: still need this? */
4689 code
= '~'; /* 0x7E */
4690 emacs_event
->modifiers
= 0;
4692 = code
> 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
4693 emacs_event
->code
= code
;
4694 EV_TRAILER
((id
)nil
);
4699 /* <NSTextInput>: inserts display of composing characters */
4700 - (void
)setMarkedText
: (id
)aString selectedRange
: (NSRange
)selRange
4702 NSString
*str
= [aString respondsToSelector
: @selector
(string
)] ?
4703 [aString string
] : aString
;
4705 NSLog
(@"setMarkedText
'%@' len
=%d range %d from %d", str, [str length],
4706 selRange.
length, selRange.location
);
4708 if (workingText
!= nil
)
4709 [self deleteWorkingText
];
4710 if ([str
length] == 0)
4716 processingCompose
= YES
;
4717 workingText
= [str copy
];
4718 ns_working_text
= build_string
([workingText UTF8String
]);
4720 emacs_event
->kind
= NS_TEXT_EVENT
;
4721 emacs_event
->code
= KEY_NS_PUT_WORKING_TEXT
;
4722 EV_TRAILER
((id
)nil
);
4726 /* delete display of composing characters [not in <NSTextInput>] */
4727 - (void
)deleteWorkingText
4729 if (workingText
== nil
)
4732 NSLog
(@"deleteWorkingText len
=%d\n", [workingText length]);
4733 [workingText release
];
4735 processingCompose
= NO
;
4740 emacs_event
->kind
= NS_TEXT_EVENT
;
4741 emacs_event
->code
= KEY_NS_UNPUT_WORKING_TEXT
;
4742 EV_TRAILER
((id
)nil
);
4746 - (BOOL
)hasMarkedText
4748 return workingText
!= nil
;
4752 - (NSRange
)markedRange
4754 NSRange rng
= workingText
!= nil
4755 ? NSMakeRange
(0, [workingText
length]) : NSMakeRange
(NSNotFound
, 0);
4757 NSLog
(@"markedRange request"
);
4765 NSLog
(@"unmark
(accept
) text"
);
4766 [self deleteWorkingText
];
4767 processingCompose
= NO
;
4771 /* used to position char selection windows, etc. */
4772 - (NSRect
)firstRectForCharacterRange
: (NSRange
)theRange
4776 struct window
*win
= XWINDOW
(FRAME_SELECTED_WINDOW
(emacsframe
));
4778 NSLog
(@"firstRectForCharRange request"
);
4780 rect.
size.width
= theRange.
length * FRAME_COLUMN_WIDTH
(emacsframe
);
4781 rect.
size.height
= FRAME_LINE_HEIGHT
(emacsframe
);
4782 pt.x
= WINDOW_TEXT_TO_FRAME_PIXEL_X
(win
, win
->phys_cursor.x
);
4783 pt.y
= WINDOW_TO_FRAME_PIXEL_Y
(win
, win
->phys_cursor.y
4784 +FRAME_LINE_HEIGHT
(emacsframe
));
4786 pt
= [self convertPoint
: pt toView
: nil
];
4787 pt
= [[self window
] convertBaseToScreen
: pt
];
4793 - (NSInteger
)conversationIdentifier
4795 return (NSInteger
)self
;
4799 - (void
)doCommandBySelector
: (SEL
)aSelector
4802 NSLog
(@"doCommandBySelector
: %@", NSStringFromSelector (aSelector));
4804 if (aSelector
== @selector
(deleteBackward
:))
4806 /* happens when user backspaces over an ongoing composition:
4807 throw a 'delete' into the event queue */
4810 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
4811 emacs_event
->code
= 0xFF08;
4812 EV_TRAILER
((id
)nil
);
4816 - (NSArray
*)validAttributesForMarkedText
4818 static NSArray
*arr
= nil
;
4819 if (arr
== nil
) arr
= [NSArray new
];
4820 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
4824 - (NSRange
)selectedRange
4827 NSLog
(@"selectedRange request"
);
4828 return NSMakeRange
(NSNotFound
, 0);
4831 - (unsigned int
)characterIndexForPoint
: (NSPoint
)thePoint
4834 NSLog
(@"characterIndexForPoint request"
);
4838 - (NSAttributedString
*)attributedSubstringFromRange
: (NSRange
)theRange
4840 static NSAttributedString
*str
= nil
;
4841 if (str
== nil
) str
= [NSAttributedString new
];
4843 NSLog
(@"attributedSubstringFromRange request"
);
4847 /* End <NSTextInput> impl. */
4848 /*****************************************************************************/
4851 /* This is what happens when the user presses a mouse button. */
4852 - (void
)mouseDown
: (NSEvent
*)theEvent
4854 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
4857 NSTRACE
(mouseDown
);
4859 [self deleteWorkingText
];
4864 last_mouse_frame
= emacsframe
;
4865 /* appears to be needed to prevent spurious movement events generated on
4867 last_mouse_frame
->mouse_moved
= 0;
4869 if ([theEvent
type] == NSScrollWheel
)
4871 float delta
= [theEvent deltaY
];
4872 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
4875 emacs_event
->kind
= WHEEL_EVENT
;
4876 emacs_event
->code
= 0;
4877 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
) |
4878 ((delta
> 0) ? up_modifier
: down_modifier
);
4882 emacs_event
->kind
= MOUSE_CLICK_EVENT
;
4883 emacs_event
->code
= EV_BUTTON
(theEvent
);
4884 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
)
4885 | EV_UDMODIFIERS
(theEvent
);
4887 XSETINT
(emacs_event
->x
, lrint
(p.x
));
4888 XSETINT
(emacs_event
->y
, lrint
(p.y
));
4889 EV_TRAILER
(theEvent
);
4893 - (void
)rightMouseDown
: (NSEvent
*)theEvent
4895 NSTRACE
(rightMouseDown
);
4896 [self mouseDown
: theEvent
];
4900 - (void
)otherMouseDown
: (NSEvent
*)theEvent
4902 NSTRACE
(otherMouseDown
);
4903 [self mouseDown
: theEvent
];
4907 - (void
)mouseUp
: (NSEvent
*)theEvent
4910 [self mouseDown
: theEvent
];
4914 - (void
)rightMouseUp
: (NSEvent
*)theEvent
4916 NSTRACE
(rightMouseUp
);
4917 [self mouseDown
: theEvent
];
4921 - (void
)otherMouseUp
: (NSEvent
*)theEvent
4923 NSTRACE
(otherMouseUp
);
4924 [self mouseDown
: theEvent
];
4928 - (void
) scrollWheel
: (NSEvent
*)theEvent
4930 NSTRACE
(scrollWheel
);
4931 [self mouseDown
: theEvent
];
4935 /* Tell emacs the mouse has moved. */
4936 - (void
)mouseMoved
: (NSEvent
*)e
4938 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
4941 // NSTRACE
(mouseMoved
);
4943 last_mouse_movement_time
= EV_TIMESTAMP
(e
);
4944 last_mouse_motion_position
4945 = [self convertPoint
: [e locationInWindow
] fromView
: nil
];
4947 /* update any mouse face */
4948 if (dpyinfo
->mouse_face_hidden
)
4950 dpyinfo
->mouse_face_hidden
= 0;
4951 clear_mouse_face
(dpyinfo
);
4954 /* tooltip handling */
4955 previous_help_echo_string
= help_echo_string
;
4956 help_echo_string
= Qnil
;
4958 if (!note_mouse_movement
(emacsframe
, last_mouse_motion_position.x
,
4959 last_mouse_motion_position.y
))
4960 help_echo_string
= previous_help_echo_string
;
4962 XSETFRAME
(frame
, emacsframe
);
4963 if (!NILP
(help_echo_string
) ||
!NILP
(previous_help_echo_string
))
4965 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
4966 (note_mouse_highlight), which is called through the
4967 note_mouse_movement () call above */
4968 gen_help_event
(help_echo_string
, frame
, help_echo_window
,
4969 help_echo_object
, help_echo_pos
);
4973 help_echo_string
= Qnil
;
4974 gen_help_event
(Qnil
, frame
, Qnil
, Qnil
, 0);
4977 if (emacsframe
->mouse_moved
&& send_appdefined
)
4978 ns_send_appdefined
(-1);
4982 - (void
)mouseDragged
: (NSEvent
*)e
4984 NSTRACE
(mouseDragged
);
4985 [self mouseMoved
: e
];
4989 - (void
)rightMouseDragged
: (NSEvent
*)e
4991 NSTRACE
(rightMouseDragged
);
4992 [self mouseMoved
: e
];
4996 - (void
)otherMouseDragged
: (NSEvent
*)e
4998 NSTRACE
(otherMouseDragged
);
4999 [self mouseMoved
: e
];
5003 - (BOOL
)windowShouldClose
: (id
)sender
5005 NSEvent
*e
=[[self window
] currentEvent
];
5007 NSTRACE
(windowShouldClose
);
5008 windowClosing
= YES
;
5011 emacs_event
->kind
= DELETE_WINDOW_EVENT
;
5012 emacs_event
->modifiers
= 0;
5013 emacs_event
->code
= 0;
5015 /* Don't close this window, let this be done from lisp code. */
5020 - (NSSize
)windowWillResize
: (NSWindow
*)sender toSize
: (NSSize
)frameSize
5021 /* normalize frame to gridded text size */
5023 NSTRACE
(windowWillResize
);
5024 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5026 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
,
5027 #ifdef NS_IMPL_GNUSTEP
5028 frameSize.width
+ 3);
5032 if (cols
< MINWIDTH
)
5034 frameSize.width
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(emacsframe
, cols
);
5036 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
, frameSize.height
5037 #ifdef NS_IMPL_GNUSTEP
5038 - FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
) + 3
5039 - FRAME_NS_TOOLBAR_HEIGHT
(emacsframe
));
5041 - FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5042 - FRAME_NS_TOOLBAR_HEIGHT
(emacsframe
));
5044 if (rows
< MINHEIGHT
)
5046 frameSize.height
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(emacsframe
, rows
)
5047 + FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5048 + FRAME_NS_TOOLBAR_HEIGHT
(emacsframe
);
5049 #ifdef NS_IMPL_COCOA
5051 /* this sets window title to have size in it; the wm does this under GS */
5052 NSRect r
= [[self window
] frame
];
5053 if (r.
size.height
== frameSize.height
&& r.
size.width
== frameSize.width
)
5064 NSWindow
*window
= [self window
];
5067 const
char *t
= [[[self window
] title] UTF8String
];
5068 char *pos
= strstr
(t
, " — "
);
5071 old_title
= (char *) xmalloc
(strlen
(t
) + 1);
5072 strcpy
(old_title
, t
);
5074 size_title
= xmalloc
(strlen
(old_title
) + 40);
5075 sprintf (size_title
, "
%s — (%d x %d)", old_title, cols, rows);
5076 [window setTitle
: [NSString stringWithUTF8String
: size_title
]];
5081 #endif
/* NS_IMPL_COCOA */
5082 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5088 - (void
)windowDidResize
: (NSNotification
*)notification
5090 NSWindow
*theWindow
= [notification object
];
5092 #ifdef NS_IMPL_GNUSTEP
5093 /* in GNUstep, at least currently, it's possible to get a didResize
5094 without getting a willResize.. therefore we need to act as if we got
5095 the willResize now */
5096 NSSize sz
= [theWindow frame
].
size;
5097 sz
= [self windowWillResize
: theWindow toSize
: sz
];
5098 #endif
/* NS_IMPL_GNUSTEP */
5100 NSTRACE
(windowDidResize
);
5101 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5103 #ifdef NS_IMPL_COCOA
5109 #endif
/* NS_IMPL_COCOA */
5111 if (cols
> 0 && rows
> 0)
5112 x_set_window_size
(emacsframe
, 0, cols
, rows
);
5114 ns_send_appdefined
(-1);
5116 /* The following line causes a crash on GNUstep. Adrian Robert
5117 says he doesn't remember why he added this line, but removing it
5118 doesn't seem to cause problems on OSX, either. */
5125 - (void
)windowDidBecomeKey
: (NSNotification
*)notification
5126 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5128 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5129 struct frame
*old_focus
= dpyinfo
->x_focus_frame
;
5131 NSTRACE
(windowDidBecomeKey
);
5133 if (emacsframe
!= old_focus
)
5134 dpyinfo
->x_focus_frame
= emacsframe
;
5136 ns_frame_rehighlight
(emacsframe
);
5140 emacs_event
->kind
= FOCUS_IN_EVENT
;
5141 EV_TRAILER
((id
)nil
);
5146 - (void
)windowDidResignKey
: (NSNotification
*)notification
5147 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5149 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5150 NSTRACE
(windowDidResignKey
);
5152 if (dpyinfo
->x_focus_frame
== emacsframe
)
5153 dpyinfo
->x_focus_frame
= 0;
5155 ns_frame_rehighlight
(emacsframe
);
5157 /* FIXME: for some reason needed on second and subsequent clicks away
5158 from sole-frame Emacs to get hollow box to show */
5159 if (!windowClosing
&& [[self window
] isVisible
] == YES
)
5161 x_update_cursor
(emacsframe
, 1);
5162 x_set_frame_alpha
(emacsframe
);
5167 [self deleteWorkingText
];
5168 emacs_event
->kind
= FOCUS_IN_EVENT
;
5169 EV_TRAILER
((id
)nil
);
5174 - (void
)windowWillMiniaturize
: sender
5176 NSTRACE
(windowWillMiniaturize
);
5192 - initFrameFromEmacs
: (struct frame
*)f
5197 NSButton
*toggleButton
;
5198 int vbextra
= NS_SCROLL_BAR_WIDTH
(f
);
5203 NSTRACE
(initFrameFromEmacs
);
5206 processingCompose
= NO
;
5207 scrollbarsNeedingUpdate
= 0;
5209 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5211 r
= NSMakeRect
(0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, f
->text_cols
),
5212 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, f
->text_lines
));
5213 [self initWithFrame
: r
];
5215 FRAME_NS_VIEW
(f
) = self
;
5219 win
= [[EmacsWindow alloc
]
5220 initWithContentRect
: r
5221 styleMask
: (NSResizableWindowMask |
5222 NSMiniaturizableWindowMask |
5223 NSClosableWindowMask
)
5224 backing
: NSBackingStoreBuffered
5228 f
->border_width
= wr.
size.width
- r.
size.width
;
5229 FRAME_NS_TITLEBAR_HEIGHT
(f
) = wr.
size.height
- r.
size.height
;
5231 [win setAcceptsMouseMovedEvents
: YES
];
5232 [win setDelegate
: self
];
5233 [win useOptimizedDrawing
: YES
];
5235 sz.width
= FRAME_COLUMN_WIDTH
(f
);
5236 sz.height
= FRAME_LINE_HEIGHT
(f
);
5237 [win setResizeIncrements
: sz
];
5239 [[win contentView
] addSubview
: self
];
5242 [self registerForDraggedTypes
: ns_drag_types
];
5245 name
= [NSString stringWithUTF8String
:
5246 NILP
(tem
) ?
(unsigned
char *)"Emacs"
: SDATA
(tem
)];
5247 [win setTitle
: name
];
5249 /* toolbar support */
5250 toolbar
= [[EmacsToolbar alloc
] initForView
: self withIdentifier
:
5251 [NSString stringWithFormat
: @"Emacs Frame
%d",
5253 [win setToolbar
: toolbar
];
5254 [toolbar setVisible
: NO
];
5255 #ifdef NS_IMPL_COCOA
5256 toggleButton
= [win standardWindowButton
: NSWindowToolbarButton
];
5257 [toggleButton setTarget
: self
];
5258 [toggleButton setAction
: @selector
(toggleToolbar
: )];
5260 FRAME_NS_TOOLBAR_HEIGHT
(f
) = 0;
5264 [win setMiniwindowTitle
:
5265 [NSString stringWithUTF8String
: SDATA
(tem
)]];
5268 NSScreen
*screen
= [win screen
];
5271 [win setFrameTopLeftPoint
: NSMakePoint
5272 (IN_BOUND
(-SCREENMAX
, f
->left_pos
, SCREENMAX
),
5273 IN_BOUND
(-SCREENMAX
,
5274 [screen frame
].
size.height
- NS_TOP_POS
(f
), SCREENMAX
))];
5277 [win makeFirstResponder
: self
];
5279 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
5280 (FRAME_DEFAULT_FACE
(emacsframe
)), emacsframe
);
5281 [win setBackgroundColor
: col
];
5282 if ([col alphaComponent
] != 1.0)
5283 [win setOpaque
: NO
];
5285 [self allocateGState
];
5292 - (void
)windowDidMove
: sender
5294 NSWindow
*win
= [self window
];
5295 NSRect r
= [win frame
];
5296 NSScreen
*screen
= [win screen
];
5297 NSRect sr
= [screen frame
];
5299 NSTRACE
(windowDidMove
);
5301 if (!emacsframe
->output_data.ns
)
5305 emacsframe
->left_pos
= r.origin.x
; /* - sr.origin.x; */
5306 emacsframe
->top_pos
= sr.
size.height
-
5307 (r.origin.y
+ r.
size.height
); /* + sr.origin.y; */
5311 #ifdef NS_IMPL_COCOA
5312 /* if we don't do this manually, the window will resize but not move */
5313 - (BOOL
)windowShouldZoom
: (NSWindow
*)sender toFrame
: (NSRect
)newFrame
5315 [[self window
] setFrame
: newFrame display
: NO
];
5320 /* Implement this to control size of frame on zoom.
5321 - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender
5322 defaultFrame:(NSRect)defaultFrame; */
5325 - (void
)windowDidDeminiaturize
: sender
5327 NSTRACE
(windowDidDeminiaturize
);
5328 if (!emacsframe
->output_data.ns
)
5330 emacsframe
->async_visible
= 1;
5331 emacsframe
->async_iconified
= 0;
5332 windows_or_buffers_changed
++;
5336 emacs_event
->kind
= ICONIFY_EVENT
;
5337 EV_TRAILER
((id
)nil
);
5342 - (void
)windowDidExpose
: sender
5344 NSTRACE
(windowDidExpose
);
5345 if (!emacsframe
->output_data.ns
)
5347 emacsframe
->async_visible
= 1;
5348 SET_FRAME_GARBAGED
(emacsframe
);
5350 if (send_appdefined
)
5351 ns_send_appdefined
(-1);
5355 - (void
)windowDidMiniaturize
: sender
5357 NSTRACE
(windowDidMiniaturize
);
5358 if (!emacsframe
->output_data.ns
)
5361 emacsframe
->async_iconified
= 1;
5365 emacs_event
->kind
= ICONIFY_EVENT
;
5366 EV_TRAILER
((id
)nil
);
5371 - (void
)mouseEntered
: (NSEvent
*)theEvent
5373 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
5374 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5375 NSTRACE
(mouseEntered
);
5377 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
5381 - (void
)mouseExited
: (NSEvent
*)theEvent
5383 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
5385 struct ns_display_info
*dpyinfo
5386 = emacsframe ? FRAME_NS_DISPLAY_INFO
(emacsframe
) : NULL;
5388 NSTRACE
(mouseExited
);
5390 if (dpyinfo ||
!emacsframe
)
5393 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
5395 if (emacsframe
== dpyinfo
->mouse_face_mouse_frame
)
5397 clear_mouse_face
(dpyinfo
);
5398 dpyinfo
->mouse_face_mouse_frame
= 0;
5406 if (context_menu_value
== -1)
5407 context_menu_value
= [sender tag
];
5409 find_and_call_menu_selection
(emacsframe
, emacsframe
->menu_bar_items_used
,
5410 emacsframe
->menu_bar_vector
,
5411 (void
*)[sender tag
]);
5412 ns_send_appdefined
(-1);
5417 - (EmacsToolbar
*)toolbar
5423 /* this gets called on toolbar button click */
5424 - toolbarClicked
: (id
)item
5427 int idx
= [item tag
] * TOOL_BAR_ITEM_NSLOTS
;
5429 NSTRACE
(toolbarClicked
);
5434 /* send first event (for some reason two needed) */
5435 theEvent
= [[self window
] currentEvent
];
5436 emacs_event
->kind
= TOOL_BAR_EVENT
;
5437 XSETFRAME
(emacs_event
->arg
, emacsframe
);
5438 EV_TRAILER
(theEvent
);
5440 emacs_event
->kind
= TOOL_BAR_EVENT
;
5441 /* XSETINT (emacs_event->code, 0); */
5442 emacs_event
->arg
= AREF
(emacsframe
->tool_bar_items
,
5443 idx
+ TOOL_BAR_ITEM_KEY
);
5444 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5445 EV_TRAILER
(theEvent
);
5450 - toggleToolbar
: (id
)sender
5455 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5456 emacs_event
->code
= KEY_NS_TOGGLE_TOOLBAR
;
5457 EV_TRAILER
((id
)nil
);
5462 - (void
)drawRect
: (NSRect
)rect
5464 int x
= NSMinX
(rect
), y
= NSMinY
(rect
);
5465 int width
= NSWidth
(rect
), height
= NSHeight
(rect
);
5469 if (!emacsframe ||
!emacsframe
->output_data.ns
)
5473 ns_clear_frame_area
(emacsframe
, x
, y
, width
, height
);
5474 expose_frame
(emacsframe
, x
, y
, width
, height
);
5476 emacsframe
->async_visible
= 1;
5477 emacsframe
->async_iconified
= 0;
5479 /* SET_FRAME_GARBAGED (emacsframe);
5480 ns_send_appdefined (-1); */
5484 /* NSDraggingDestination protocol methods. Actually this is not really a
5485 protocol, but a category of Object. O well... */
5487 -(unsigned int
) draggingEntered
: (id
<NSDraggingInfo
>) sender
5489 NSTRACE
(draggingEntered
);
5490 return NSDragOperationGeneric
;
5494 -(BOOL
)prepareForDragOperation
: (id
<NSDraggingInfo
>) sender
5500 -(BOOL
)performDragOperation
: (id
<NSDraggingInfo
>) sender
5505 NSEvent
*theEvent
= [[self window
] currentEvent
];
5508 NSTRACE
(performDragOperation
);
5513 position
= [self convertPoint
: [sender draggingLocation
] fromView
: nil
];
5514 x
= lrint
(position.x
); y
= lrint
(position.y
);
5516 pb
= [sender draggingPasteboard
];
5517 type = [pb availableTypeFromArray
: ns_drag_types
];
5522 else if ([type isEqualToString
: NSFilenamesPboardType
])
5525 NSEnumerator
*fenum
;
5528 if (!(files
= [pb propertyListForType
: type]))
5531 fenum
= [files objectEnumerator
];
5532 while ( (file
= [fenum nextObject
]) )
5534 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5535 emacs_event
->code
= KEY_NS_DRAG_FILE
;
5536 XSETINT
(emacs_event
->x
, x
);
5537 XSETINT
(emacs_event
->y
, y
);
5538 ns_input_file
= append2
(ns_input_file
,
5539 build_string
([file UTF8String
]));
5540 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5541 EV_TRAILER
(theEvent
);
5545 else if ([type isEqualToString
: NSURLPboardType
])
5550 if (!(fileURL
= [NSURL URLFromPasteboard
: pb
]) ||
5551 [fileURL isFileURL
] == NO
)
5554 file
= [fileURL
path];
5555 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5556 emacs_event
->code
= KEY_NS_DRAG_FILE
;
5557 XSETINT
(emacs_event
->x
, x
);
5558 XSETINT
(emacs_event
->y
, y
);
5559 ns_input_file
= append2
(ns_input_file
, build_string
([file UTF8String
]));
5560 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5561 EV_TRAILER
(theEvent
);
5564 else if ([type isEqualToString
: NSStringPboardType
]
5565 ||
[type isEqualToString
: NSTabularTextPboardType
])
5569 if (! (data
= [pb stringForType
: type]))
5572 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5573 emacs_event
->code
= KEY_NS_DRAG_TEXT
;
5574 XSETINT
(emacs_event
->x
, x
);
5575 XSETINT
(emacs_event
->y
, y
);
5576 ns_input_text
= build_string
([data UTF8String
]);
5577 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5578 EV_TRAILER
(theEvent
);
5581 else if ([type isEqualToString
: NSColorPboardType
])
5583 NSColor
*c
= [NSColor colorFromPasteboard
: pb
];
5584 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5585 emacs_event
->code
= KEY_NS_DRAG_COLOR
;
5586 XSETINT
(emacs_event
->x
, x
);
5587 XSETINT
(emacs_event
->y
, y
);
5588 ns_input_color
= ns_color_to_lisp
(c
);
5589 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5590 EV_TRAILER
(theEvent
);
5593 else if ([type isEqualToString
: NSFontPboardType
])
5595 /* impl based on GNUstep NSTextView.m */
5596 NSData
*data
= [pb dataForType
: NSFontPboardType
];
5597 NSDictionary
*dict
= [NSUnarchiver unarchiveObjectWithData
: data
];
5598 NSFont
*font
= [dict objectForKey
: NSFontAttributeName
];
5604 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5605 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
5606 XSETINT
(emacs_event
->x
, x
);
5607 XSETINT
(emacs_event
->y
, y
);
5608 ns_input_font
= build_string
([[font fontName
] UTF8String
]);
5609 snprintf
(fontSize
, 10, "
%f", [font pointSize]);
5610 ns_input_fontsize
= build_string
(fontSize
);
5611 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
5612 EV_TRAILER
(theEvent
);
5617 error ("Invalid data
type in dragging pasteboard."
);
5623 - validRequestorForSendType
: (NSString
*)typeSent
5624 returnType
: (NSString
*)typeReturned
5626 NSTRACE
(validRequestorForSendType
);
5627 if ([ns_send_types indexOfObjectIdenticalTo
: typeSent
] != NSNotFound
&&
5628 [ns_return_types indexOfObjectIdenticalTo
: typeSent
] != NSNotFound
)
5631 return [super validRequestorForSendType
: typeSent
5632 returnType
: typeReturned
];
5636 /* The next two methods are part of NSServicesRequests informal protocol,
5637 supposedly called when a services menu item is chosen from this app.
5638 But this should not happen because we override the services menu with our
5639 own entries which call ns-perform-service.
5640 Nonetheless, it appeared to happen here (under strange circumstances):
5641 http://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=1435
5642 So let's at least stub them out until further investigation can be done. */
5644 - (BOOL
) readSelectionFromPasteboard
: (NSPasteboard
*)pb
5646 /* we could call ns_string_from_pasteboard(pboard) here but then it should
5647 be written into the buffer in place of the existing selection..
5648 ordinary service calls go through functions defined in ns-win.el */
5652 - (BOOL
) writeSelectionToPasteboard
: (NSPasteboard
*)pb types
: (NSArray
*)types
5654 /* supposed to write for as many of types as we are able */
5659 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
5660 (gives a miniaturized version of the window); currently we use the latter for
5661 frames whose active buffer doesn't correspond to any file
5662 (e.g., '*scratch*') */
5663 - setMiniwindowImage
: (BOOL
) setMini
5665 id
image = [[self window
] miniwindowImage
];
5666 NSTRACE
(setMiniwindowImage
);
5668 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
5669 about "AppleDockIconEnabled" notwithstanding, however the set message
5670 below has its effect nonetheless. */
5671 if (image != emacsframe
->output_data.ns
->miniimage
)
5673 if (image && [image isKindOfClass
: [EmacsImage
class]])
5675 [[self window
] setMiniwindowImage
:
5676 setMini ? emacsframe
->output_data.ns
->miniimage
: nil
];
5683 - (void
) setRows
: (int
) r andColumns
: (int
) c
5689 @
end /* EmacsView */
5693 /* ==========================================================================
5695 EmacsWindow implementation
5697 ========================================================================== */
5699 @implementation EmacsWindow
5701 /* called only on resize clicks by special case in EmacsApp-sendEvent */
5702 - (void
)mouseDown
: (NSEvent
*)theEvent
5706 NSSize
size = [[theEvent window
] frame
].
size;
5707 grabOffset
= [theEvent locationInWindow
];
5708 grabOffset.x
= size.width
- grabOffset.x
;
5711 [super mouseDown
: theEvent
];
5716 - (void
)mouseUp
: (NSEvent
*)theEvent
5720 struct frame
*f
= ((EmacsView
*)[self delegate
])->emacsframe
;
5722 ns_set_name_as_filename
(f
);
5724 ns_send_appdefined
(-1);
5727 [super mouseUp
: theEvent
];
5731 /* send resize events */
5732 - (void
)mouseDragged
: (NSEvent
*)theEvent
5736 NSPoint p
= [theEvent locationInWindow
];
5737 NSSize
size, vettedSize
, origSize
= [self frame
].
size;
5739 size.width
= p.x
+ grabOffset.x
;
5740 size.height
= origSize.height
- p.y
+ grabOffset.y
;
5742 if (size.width
== origSize.width
&& size.height
== origSize.height
)
5745 vettedSize
= [[self delegate
] windowWillResize
: self toSize
: size];
5746 if (vettedSize.width
!= size.width || vettedSize.height
!= size.height
)
5748 [[NSNotificationCenter defaultCenter
]
5749 postNotificationName
: NSWindowDidResizeNotification
5754 [super mouseDragged
: theEvent
];
5757 @
end /* EmacsWindow */
5760 /* ==========================================================================
5762 EmacsScroller implementation
5764 ========================================================================== */
5767 @implementation EmacsScroller
5769 /* for repeat button push */
5770 #define SCROLL_BAR_FIRST_DELAY
0.5
5771 #define SCROLL_BAR_CONTINUOUS_DELAY
(1.0 / 15)
5773 + (float
) scrollerWidth
5775 /* TODO: if we want to allow variable widths, this is the place to do it,
5776 however neither GNUstep nor Cocoa support it very well */
5777 return [NSScroller scrollerWidth
];
5781 - initFrame
: (NSRect
)r window
: (Lisp_Object
)nwin
5783 NSTRACE
(EmacsScroller_initFrame
);
5785 r.
size.width
= [EmacsScroller scrollerWidth
];
5786 [super initWithFrame
: r
/*NSMakeRect (0, 0, 0, 0)*/];
5787 [self setContinuous
: YES
];
5788 [self setEnabled
: YES
];
5790 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
5791 locked against the right, top and bottom edges. */
5792 [self setAutoresizingMask
: NSViewMinXMargin | NSViewHeightSizable
];
5796 pixel_height
= NSHeight
(r
);
5797 min_portion
= 20 / pixel_height
;
5799 frame
= XFRAME
(XWINDOW
(win
)->frame
);
5800 if (FRAME_LIVE_P
(frame
))
5803 EmacsView
*view = FRAME_NS_VIEW
(frame
);
5804 NSView
*sview
= [[view window
] contentView
];
5805 NSArray
*subs
= [sview subviews
];
5807 /* disable optimization stopping redraw of other scrollbars */
5808 view->scrollbarsNeedingUpdate
= 0;
5809 for (i
=[subs count
]-1; i
>= 0; i
--)
5810 if ([[subs objectAtIndex
: i
] isKindOfClass
: [EmacsScroller
class]])
5811 view->scrollbarsNeedingUpdate
++;
5812 [sview addSubview
: self
];
5815 /* [self setFrame: r]; */
5821 - (void
)setFrame
: (NSRect
)newRect
5823 NSTRACE
(EmacsScroller_setFrame
);
5825 pixel_height
= NSHeight
(newRect
);
5826 min_portion
= 20 / pixel_height
;
5827 [super setFrame
: newRect
];
5829 /* UNBLOCK_INPUT; */
5835 NSTRACE
(EmacsScroller_dealloc
);
5837 XWINDOW
(win
)->vertical_scroll_bar
= Qnil
;
5864 /* ensure other scrollbar updates after deletion */
5865 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
5867 view->scrollbarsNeedingUpdate
++;
5868 [self removeFromSuperview
];
5876 - (void
)resetCursorRects
5878 NSRect visible
= [self visibleRect
];
5879 NSTRACE
(resetCursorRects
);
5881 if (!NSIsEmptyRect
(visible
))
5882 [self addCursorRect
: visible cursor
: [NSCursor arrowCursor
]];
5883 [[NSCursor arrowCursor
] setOnMouseEntered
: YES
];
5887 - (int
) checkSamePosition
: (int
) position portion
: (int
) portion
5890 return em_position
==position
&& em_portion
==portion
&& em_whole
==whole
5891 && portion
!= whole
; /* needed for resize empty buf */
5895 - setPosition
: (int
)position portion
: (int
)portion whole
: (int
)whole
5897 NSTRACE
(setPosition
);
5899 em_position
= position
;
5900 em_portion
= portion
;
5903 if (portion
>= whole
)
5904 [self setFloatValue
: 0.0 knobProportion
: 1.0];
5908 portion
= max ((float
)whole
*min_portion
/pixel_height
, portion
);
5909 pos
= (float
)position
/ (whole
- portion
);
5910 por
= (float
)portion
/whole
;
5911 [self setFloatValue
: pos knobProportion
: por
];
5913 #ifdef NS_IMPL_GNUSTEP
5919 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
5920 drag events will go directly to the EmacsScroller. Leaving in for now. */
5921 -(void
)getMouseMotionPart
: (int
*)part window
: (Lisp_Object
*)window
5922 x
: (Lisp_Object
*)x y
: ( Lisp_Object
*)y
5924 *part
= last_hit_part
;
5926 XSETINT
(*y
, pixel_height
);
5927 if ([self floatValue
] > 0.999)
5928 XSETINT
(*x
, pixel_height
);
5930 XSETINT
(*x
, pixel_height
* [self floatValue
]);
5934 /* set up emacs_event */
5935 - (void
) sendScrollEventAtLoc
: (float
)loc fromEvent
: (NSEvent
*)e
5940 emacs_event
->part
= last_hit_part
;
5941 emacs_event
->code
= 0;
5942 emacs_event
->modifiers
= EV_MODIFIERS
(e
) | down_modifier
;
5943 emacs_event
->frame_or_window
= win
;
5944 emacs_event
->timestamp
= EV_TIMESTAMP
(e
);
5945 emacs_event
->kind
= SCROLL_BAR_CLICK_EVENT
;
5946 emacs_event
->arg
= Qnil
;
5947 XSETINT
(emacs_event
->x
, loc
* pixel_height
);
5948 XSETINT
(emacs_event
->y
, pixel_height
-20);
5950 n_emacs_events_pending
++;
5951 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
);
5952 EVENT_INIT
(*emacs_event
);
5953 ns_send_appdefined
(-1);
5957 /* called manually thru timer to implement repeated button action w/hold-down */
5958 - repeatScroll
: (NSTimer
*)scrollEntry
5960 NSEvent
*e
= [[self window
] currentEvent
];
5961 NSPoint p
= [[self window
] mouseLocationOutsideOfEventStream
];
5962 BOOL inKnob
= [self testPart
: p
] == NSScrollerKnob
;
5964 /* clear timer if need be */
5965 if (inKnob ||
[scroll_repeat_entry timeInterval
] == SCROLL_BAR_FIRST_DELAY
)
5967 [scroll_repeat_entry invalidate
];
5968 [scroll_repeat_entry release
];
5969 scroll_repeat_entry
= nil
;
5975 = [[NSTimer scheduledTimerWithTimeInterval
:
5976 SCROLL_BAR_CONTINUOUS_DELAY
5978 selector
: @selector
(repeatScroll
:)
5984 [self sendScrollEventAtLoc
: 0 fromEvent
: e
];
5989 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
5990 mouseDragged events without going into a modal loop. */
5991 - (void
)mouseDown
: (NSEvent
*)e
5994 /* hitPart is only updated AFTER event is passed on */
5995 NSScrollerPart part
= [self testPart
: [e locationInWindow
]];
5996 double inc
= 0.0, loc
, kloc
, pos
;
5999 NSTRACE
(EmacsScroller_mouseDown
);
6003 case NSScrollerDecrementPage
:
6004 last_hit_part
= scroll_bar_above_handle
; inc
= -1.0; break;
6005 case NSScrollerIncrementPage
:
6006 last_hit_part
= scroll_bar_below_handle
; inc
= 1.0; break;
6007 case NSScrollerDecrementLine
:
6008 last_hit_part
= scroll_bar_up_arrow
; inc
= -0.1; break;
6009 case NSScrollerIncrementLine
:
6010 last_hit_part
= scroll_bar_down_arrow
; inc
= 0.1; break;
6011 case NSScrollerKnob
:
6012 last_hit_part
= scroll_bar_handle
; break;
6013 case NSScrollerKnobSlot
: /* GNUstep-only */
6014 last_hit_part
= scroll_bar_move_ratio
; break;
6015 default
: /* NSScrollerNoPart? */
6016 fprintf (stderr
, "EmacsScoller
-mouseDown
: unexpected part
%d\n", part);
6022 pos
= 0; /* ignored */
6024 /* set a timer to repeat, as we can't let superclass do this modally */
6026 = [[NSTimer scheduledTimerWithTimeInterval
: 0.5
6028 selector
: @selector
(repeatScroll
:)
6035 /* handle, or on GNUstep possibly slot */
6036 NSEvent
*fake_event
;
6038 /* compute float loc in slot and mouse offset on knob */
6039 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
6041 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
6047 else if (loc
>= NSHeight
(sr
))
6049 loc
= NSHeight
(sr
);
6057 kr
= [self convertRect
: [self rectForPart
: NSScrollerKnob
]
6059 kloc
= NSHeight
(kr
) - ([e locationInWindow
].y
- NSMinY
(kr
));
6061 last_mouse_offset
= kloc
;
6063 /* if knob, tell emacs a location offset by knob pos
6064 (to indicate top of handle) */
6065 if (part
== NSScrollerKnob
)
6066 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
6068 /* else this is a slot click on GNUstep: go straight there */
6069 pos
= loc
/ NSHeight
(sr
);
6071 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
6072 fake_event
= [NSEvent mouseEventWithType
: NSLeftMouseUp
6073 location
: [e locationInWindow
]
6074 modifierFlags
: [e modifierFlags
]
6075 timestamp
: [e timestamp
]
6076 windowNumber
: [e windowNumber
]
6077 context
: [e context
]
6078 eventNumber
: [e eventNumber
]
6079 clickCount
: [e clickCount
]
6080 pressure
: [e pressure
]];
6081 [super mouseUp
: fake_event
];
6084 if (part
!= NSScrollerKnob
)
6085 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
6089 /* Called as we manually track scroller drags, rather than superclass. */
6090 - (void
)mouseDragged
: (NSEvent
*)e
6096 NSTRACE
(EmacsScroller_mouseDragged
);
6098 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
6100 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
6107 else if (loc
>= NSHeight
(sr
) + last_mouse_offset
)
6109 loc
= NSHeight
(sr
) + last_mouse_offset
;
6113 pos
= /*(edge ? loc :*/ (loc
- last_mouse_offset
) / NSHeight
(sr
);
6114 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
6118 - (void
)mouseUp
: (NSEvent
*)e
6120 if (scroll_repeat_entry
)
6122 [scroll_repeat_entry invalidate
];
6123 [scroll_repeat_entry release
];
6124 scroll_repeat_entry
= nil
;
6130 /* treat scrollwheel events in the bar as though they were in the main window */
6131 - (void
) scrollWheel
: (NSEvent
*)theEvent
6133 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
6134 [view mouseDown
: theEvent
];
6137 @
end /* EmacsScroller */
6141 /* ==========================================================================
6143 EmacsPrefsController implementation
6145 ========================================================================== */
6148 @implementation EmacsPrefsController
6150 /* in Tiger+, can just do [popup selectItemWithTag: tag]; */
6151 static void selectItemWithTag
(NSPopUpButton
*popup
, int tag
)
6153 NSEnumerator
*items
= [[popup itemArray
] objectEnumerator
];
6155 while (item
= [items nextObject
])
6157 if ([item tag
] == tag
)
6159 [popup selectItem
: item
];
6167 [NSBundle loadNibNamed
: @"preferences" owner
: self
];
6172 - (void
) showForFrame
: (struct frame
*)f
6175 [self setPanelFromValues
];
6176 [prefsWindow makeKeyAndOrderFront
: self
];
6177 [prefsWindow display
];
6181 /* If you change this, change setPanelFromDefaultValues too. */
6182 - (void
) setPanelFromValues
6185 = ns_lisp_to_cursor_type
(get_frame_param
(frame
, Qcursor_type
));
6186 prevExpandSpace
= XFLOATINT
(ns_expand_space
);
6188 #ifdef NS_IMPL_COCOA
6189 prevUseHighlightColor
= ns_use_system_highlight_color
;
6192 [expandSpaceSlider setFloatValue
: prevExpandSpace
];
6193 [cursorTypeMatrix selectCellWithTag
: (cursorType
== FILLED_BOX_CURSOR ?
1 :
6194 (cursorType
== BAR_CURSOR ?
2 :
6195 (cursorType
== HBAR_CURSOR ?
3 : 4)))];
6196 selectItemWithTag
(alternateModMenu
,
6197 parse_solitary_modifier
(ns_alternate_modifier
));
6198 selectItemWithTag
(commandModMenu
,
6199 parse_solitary_modifier
(ns_command_modifier
));
6200 #ifdef NS_IMPL_COCOA
6201 selectItemWithTag
(controlModMenu
,
6202 parse_solitary_modifier
(ns_control_modifier
));
6203 selectItemWithTag
(functionModMenu
,
6204 parse_solitary_modifier
(ns_function_modifier
));
6205 [smoothFontsCheck setState
: (NILP
(ns_antialias_text
) ? NO
: YES
)];
6206 [useQuickdrawCheck setState
: (NILP
(ns_use_qd_smoothing
) ? NO
: YES
)];
6207 [useSysHiliteCheck setState
: (NILP
(prevUseHighlightColor
) ? NO
: YES
)];
6208 [confirmQuitCheck setState
: (NILP
(ns_confirm_quit
) ? NO
: YES
)];
6213 /* This and ns_set_default_prefs should be changed together. */
6214 - (void
) setPanelFromDefaultValues
6216 [expandSpaceSlider setFloatValue
: 0.0];
6217 [cursorTypeMatrix selectCellWithTag
: 1]; /* filled box */
6218 selectItemWithTag
(alternateModMenu
, meta_modifier
);
6219 selectItemWithTag
(commandModMenu
, super_modifier
);
6220 #ifdef NS_IMPL_COCOA
6221 selectItemWithTag
(controlModMenu
, ctrl_modifier
);
6222 selectItemWithTag
(functionModMenu
, 0); /* none */
6223 [smoothFontsCheck setState
: YES
];
6224 [useQuickdrawCheck setState
: NO
];
6225 [useSysHiliteCheck setState
: YES
];
6230 - (void
) setValuesFromPanel
6232 int altTag
= [[alternateModMenu selectedItem
] tag
];
6233 int cmdTag
= [[commandModMenu selectedItem
] tag
];
6234 #ifdef NS_IMPL_COCOA
6235 int ctrlTag
= [[controlModMenu selectedItem
] tag
];
6236 int fnTag
= [[functionModMenu selectedItem
] tag
];
6238 float expandSpace
= [expandSpaceSlider floatValue
];
6239 int cursorTag
= [[cursorTypeMatrix selectedCell
] tag
];
6240 Lisp_Object cursor_type
= ns_cursor_type_to_lisp
6241 ( cursorTag
== 1 ? FILLED_BOX_CURSOR
6242 : cursorTag
== 2 ? BAR_CURSOR
6243 : cursorTag
== 3 ? HBAR_CURSOR
: HOLLOW_BOX_CURSOR
);
6245 if (expandSpace
!= prevExpandSpace
)
6247 ns_expand_space
= make_float
(expandSpace
);
6248 /* TODO: more needed: store needed metrics in nsfont_info, update
6249 frame default font max_bounds and fontp, recompute faces */
6250 /* FRAME_LINE_HEIGHT (frame) *= (expandSpace / prevExpandSpace);
6251 x_set_window_size (frame, 0, frame->text_cols, frame->text_lines); */
6252 prevExpandSpace
= expandSpace
;
6255 store_frame_param
(frame
, Qcursor_type
, cursor_type
);
6256 x_set_cursor_type
(frame
, cursor_type
, Qnil
); /* FIXME: do only if changed */
6258 ns_alternate_modifier
= ns_mod_to_lisp
(altTag
);
6259 ns_command_modifier
= ns_mod_to_lisp
(cmdTag
);
6260 #ifdef NS_IMPL_COCOA
6261 ns_control_modifier
= ns_mod_to_lisp
(ctrlTag
);
6262 ns_function_modifier
= ns_mod_to_lisp
(fnTag
);
6263 ns_antialias_text
= [smoothFontsCheck state
] ? Qt
: Qnil
;
6264 ns_use_qd_smoothing
= [useQuickdrawCheck state
] ? Qt
: Qnil
;
6265 ns_use_system_highlight_color
= [useSysHiliteCheck state
] ? Qt
: Qnil
;
6266 ns_confirm_quit
= [confirmQuitCheck state
] ? Qt
: Qnil
;
6267 if (! EQ
(ns_use_system_highlight_color
, prevUseHighlightColor
))
6269 prevUseHighlightColor
= ns_use_system_highlight_color
;
6270 if (EQ
(ns_use_system_highlight_color
, Qt
))
6272 ns_selection_color
= [[NSUserDefaults standardUserDefaults
]
6273 stringForKey
: @"AppleHighlightColor"
];
6274 if (ns_selection_color
== nil
)
6275 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
6278 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
6280 #endif
/* NS_IMPL_COCOA */
6281 Fcall_interactively
(intern
("ns
-save-preferences"
), Qnil
, Qnil
);
6286 - (IBAction
)cancel
: (id
)sender
6288 [prefsWindow
close];
6292 - (IBAction
)ok
: (id
)sender
6294 [self setValuesFromPanel
];
6295 [prefsWindow
close];
6299 - (IBAction
)resetToDefaults
: (id
)sender
6301 [self setPanelFromDefaultValues
];
6305 - (IBAction
)runHelp
: (id
)sender
6307 struct frame
*emacsframe
= frame
;
6310 ns_raise_frame
(frame
);
6311 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
6312 emacs_event
->code
= KEY_NS_INFO_PREFS
;
6313 EV_TRAILER
((id
)nil
);
6317 - (IBAction
)setColors
: (id
)sender
6319 Lisp_Object lispFrame
;
6320 XSETFRAME
(lispFrame
, frame
);
6321 ns_raise_frame
(frame
);
6322 Fns_popup_color_panel
(lispFrame
);
6326 - (IBAction
)setDefaultFont
: (id
)sender
6328 Lisp_Object lispFrame
;
6329 XSETFRAME
(lispFrame
, frame
);
6330 ns_raise_frame
(frame
);
6331 Fns_popup_font_panel
(lispFrame
);
6334 @
end /* EmacsPrefsController */
6339 /* ==========================================================================
6341 Font-related functions; these used to be in nsfaces.m
6343 ========================================================================== */
6347 x_new_font
(struct frame
*f
, Lisp_Object font_object
, int fontset
)
6349 struct font
*font
= XFONT_OBJECT
(font_object
);
6352 fontset
= fontset_from_font
(font_object
);
6353 FRAME_FONTSET
(f
) = fontset
;
6355 if (FRAME_FONT
(f
) == font
)
6356 /* This font is already set in frame F. There's nothing more to
6360 FRAME_FONT
(f
) = font
;
6362 FRAME_BASELINE_OFFSET
(f
) = font
->baseline_offset
;
6363 FRAME_COLUMN_WIDTH
(f
) = font
->average_width
;
6364 FRAME_SPACE_WIDTH
(f
) = font
->space_width
;
6365 FRAME_LINE_HEIGHT
(f
) = font
->height
;
6367 compute_fringe_widths
(f
, 1);
6369 /* Compute the scroll bar width in character columns. */
6370 if (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) > 0)
6372 int wid
= FRAME_COLUMN_WIDTH
(f
);
6373 FRAME_CONFIG_SCROLL_BAR_COLS
(f
)
6374 = (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) + wid
- 1) / wid
;
6378 int wid
= FRAME_COLUMN_WIDTH
(f
);
6379 FRAME_CONFIG_SCROLL_BAR_COLS
(f
) = (14 + wid
- 1) / wid
;
6382 /* Now make the frame display the given font. */
6383 if (FRAME_NS_WINDOW
(f
) != 0)
6384 x_set_window_size
(f
, 0, FRAME_COLS
(f
), FRAME_LINES
(f
));
6390 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
6391 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
6395 ns_xlfd_to_fontname
(const
char *xlfd
)
6396 /* --------------------------------------------------------------------------
6397 Convert an X font name (XLFD) to an NS font name.
6398 Only family is used.
6399 The string returned is temporarily allocated.
6400 -------------------------------------------------------------------------- */
6402 char *name
= xmalloc
(180);
6406 if (!strncmp
(xlfd
, "
--"
, 2))
6407 sscanf (xlfd
, "
--%*[^-]-%[^-]179-", name);
6409 sscanf (xlfd
, "
-%*[^-]-%[^-]179-", name);
6411 /* stopgap for malformed XLFD input */
6412 if (strlen
(name
) == 0)
6413 strcpy
(name
, "Monaco"
);
6415 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
6416 also uppercase after '-' or ' ' */
6417 name
[0] = toupper
(name
[0]);
6418 for (len
=strlen
(name
), i
=0; i
<len
; i
++)
6424 name
[i
+1] = toupper
(name
[i
+1]);
6426 else if (name
[i
] == '_')
6430 name
[i
+1] = toupper
(name
[i
+1]);
6433 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
6434 ret
= [[NSString stringWithUTF8String
: name
] UTF8String
];
6443 NSTRACE
(syms_of_nsterm
);
6444 DEFVAR_LISP
("ns
-input-file"
, &ns_input_file
,
6445 "The file specified in the last NS event."
);
6446 ns_input_file
=Qnil
;
6448 DEFVAR_LISP
("ns
-input-text"
, &ns_input_text
,
6449 "The data received in the last NS
text drag event."
);
6450 ns_input_text
=Qnil
;
6452 DEFVAR_LISP
("ns
-working
-text"
, &ns_working_text
,
6453 "String
for visualizing working composition sequence."
);
6454 ns_working_text
=Qnil
;
6456 DEFVAR_LISP
("ns
-input-font"
, &ns_input_font
,
6457 "The font specified in the last NS event."
);
6458 ns_input_font
=Qnil
;
6460 DEFVAR_LISP
("ns
-input-fontsize"
, &ns_input_fontsize
,
6461 "The fontsize specified in the last NS event."
);
6462 ns_input_fontsize
=Qnil
;
6464 DEFVAR_LISP
("ns
-input-line"
, &ns_input_line
,
6465 "The
line specified in the last NS event."
);
6466 ns_input_line
=Qnil
;
6468 DEFVAR_LISP
("ns
-input-color"
, &ns_input_color
,
6469 "The color specified in the last NS event."
);
6470 ns_input_color
=Qnil
;
6472 DEFVAR_LISP
("ns
-input-spi
-name"
, &ns_input_spi_name
,
6473 "The service name specified in the last NS event."
);
6474 ns_input_spi_name
=Qnil
;
6476 DEFVAR_LISP
("ns
-input-spi
-arg"
, &ns_input_spi_arg
,
6477 "The service argument specified in the last NS event."
);
6478 ns_input_spi_arg
=Qnil
;
6480 DEFVAR_LISP
("ns
-alternate
-modifier"
, &ns_alternate_modifier
,
6481 "This variable describes the behavior of the alternate or option key.
\n\
6482 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
6483 Set to none means that the alternate
/ option key is not interpreted by Emacs
\n\
6484 at
all, allowing it to be used at a
lower level
for accented character entry."
);
6486 DEFVAR_LISP
("ns
-command
-modifier"
, &ns_command_modifier
,
6487 "This variable describes the behavior of the command key.
\n\
6488 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key."
);
6490 DEFVAR_LISP
("ns
-control
-modifier"
, &ns_control_modifier
,
6491 "This variable describes the behavior of the control key.
\n\
6492 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key."
);
6494 DEFVAR_LISP
("ns
-function-modifier"
, &ns_function_modifier
,
6495 "This variable describes the behavior of the
function key
(on laptops
).
\n\
6496 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
6497 Set to none means that the
function key is not interpreted by Emacs at
all,\n\
6498 allowing it to be used at a
lower level
for accented character entry."
);
6500 DEFVAR_LISP
("ns
-expand
-space"
, &ns_expand_space
,
6501 "Amount by
which spacing between lines is expanded
(positive
)\n\
6502 or shrunk
(negative
). Zero
(the default
) means standard
line height.
\n\
6503 (This variable should only be read
, never
set.
)"
);
6505 DEFVAR_LISP
("ns
-antialias
-text"
, &ns_antialias_text
,
6506 "Non
-nil
(the default
) means to render
text antialiased. Only has an effect on OS X Panther and above."
);
6508 DEFVAR_LISP
("ns
-use
-qd
-smoothing"
, &ns_use_qd_smoothing
,
6509 "Whether to render
text using QuickDraw
(less heavy
) antialiasing. Only has an effect on OS X Panther and above. Default is nil
(use Quartz smoothing
)."
);
6511 DEFVAR_LISP
("ns
-use
-system
-highlight
-color"
,
6512 &ns_use_system_highlight_color
,
6513 "Whether to use the system default
(on OS X only
) for the highlight color. Nil means to use standard emacs
(prior to
version 21) 'grey'."
);
6515 DEFVAR_LISP
("ns
-confirm
-quit"
, &ns_confirm_quit
,
6516 "Whether to confirm application
quit using
dialog."
);
6518 staticpro
(&ns_display_name_list
);
6519 ns_display_name_list
= Qnil
;
6521 staticpro
(&last_mouse_motion_frame
);
6522 last_mouse_motion_frame
= Qnil
;
6524 /* from 23+ we need to tell emacs what modifiers there are.. */
6525 Qmodifier_value
= intern
("modifier
-value"
);
6526 Qalt
= intern
("alt"
);
6527 Fput
(Qalt
, Qmodifier_value
, make_number
(alt_modifier
));
6528 Qhyper
= intern
("hyper"
);
6529 Fput
(Qhyper
, Qmodifier_value
, make_number
(hyper_modifier
));
6530 Qmeta
= intern
("
meta"
);
6531 Fput
(Qmeta
, Qmodifier_value
, make_number
(meta_modifier
));
6532 Qsuper
= intern
("super"
);
6533 Fput
(Qsuper
, Qmodifier_value
, make_number
(super_modifier
));
6534 Qcontrol
= intern
("control"
);
6535 Fput
(Qcontrol
, Qmodifier_value
, make_number
(ctrl_modifier
));
6537 /* TODO: move to common code */
6538 DEFVAR_LISP
("x
-toolkit
-scroll
-bars"
, &Vx_toolkit_scroll_bars
,
6539 doc: /* If not nil, Emacs uses toolkit scroll bars. */);
6540 #ifdef USE_TOOLKIT_SCROLL_BARS
6541 Vx_toolkit_scroll_bars
= Qt
;
6543 Vx_toolkit_scroll_bars
= Qnil
;
6546 /* these are unsupported but we need the declarations to avoid whining
6547 messages from cus-start.el */
6548 DEFVAR_BOOL
("x
-use
-underline
-position
-properties"
,
6549 &x_use_underline_position_properties
,
6550 doc: /* NOT SUPPORTED UNDER NS.
6551 *Non-nil means make use of UNDERLINE_POSITION font properties.
6552 A value of nil means ignore them. If you encounter fonts with bogus
6553 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
6554 to 4.1, set this to nil.
6556 NOTE: Not supported on Mac yet. */);
6557 x_use_underline_position_properties
= 0;
6559 DEFVAR_BOOL
("x
-underline
-at
-descent
-line"
,
6560 &x_underline_at_descent_line
,
6561 doc: /* NOT SUPPORTED UNDER NS.
6562 *Non-nil means to draw the underline at the same place as the descent line.
6563 A value of nil means to draw the underline according to the value of the
6564 variable `x-use-underline-position-properties', which is usually at the
6565 baseline level. The default value is nil. */);
6566 x_underline_at_descent_line
= 0;
6568 /* Tell emacs about this window system. */
6569 Fprovide
(intern
("ns"
), Qnil
);
6573 // arch
-tag
: 6eaa8f7d
-a69b
-4e1c
-b43d
-ab31defbe0d2