1 /* NeXT/Open/GNUstep / MacOSX communication module.
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2013 Free Software
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
29 /* This should be the first include, as it may set up #defines affecting
30 interpretation of even the system includes. */
36 #include
<sys
/types.h
>
42 #include
<c
-strcase.h
>
46 #include "blockinput.h"
47 #include "sysselect.h"
50 #include "character.h"
52 #include "composite.h"
55 #include "termhooks.h"
63 #ifdef NS_IMPL_GNUSTEP
69 int term_trace_num
= 0;
70 #define NSTRACE
(x
) fprintf (stderr
, "
%s:%d: [%d] " #x "\n", \
71 __FILE__
, __LINE__
, ++term_trace_num
)
76 extern NSString
*NSMenuDidBeginTrackingNotification
;
78 /* ==========================================================================
82 ========================================================================== */
84 /* Convert a symbol indexed with an NSxxx value to a value as defined
85 in keyboard.c (lispy_function_key). I hope this is a correct way
87 static unsigned convert_ns_to_X_keysym
[] =
89 NSHomeFunctionKey
, 0x50,
90 NSLeftArrowFunctionKey
, 0x51,
91 NSUpArrowFunctionKey
, 0x52,
92 NSRightArrowFunctionKey
, 0x53,
93 NSDownArrowFunctionKey
, 0x54,
94 NSPageUpFunctionKey
, 0x55,
95 NSPageDownFunctionKey
, 0x56,
96 NSEndFunctionKey
, 0x57,
97 NSBeginFunctionKey
, 0x58,
98 NSSelectFunctionKey
, 0x60,
99 NSPrintFunctionKey
, 0x61,
100 NSClearLineFunctionKey
, 0x0B,
101 NSExecuteFunctionKey
, 0x62,
102 NSInsertFunctionKey
, 0x63,
103 NSUndoFunctionKey
, 0x65,
104 NSRedoFunctionKey
, 0x66,
105 NSMenuFunctionKey
, 0x67,
106 NSFindFunctionKey
, 0x68,
107 NSHelpFunctionKey
, 0x6A,
108 NSBreakFunctionKey
, 0x6B,
110 NSF1FunctionKey
, 0xBE,
111 NSF2FunctionKey
, 0xBF,
112 NSF3FunctionKey
, 0xC0,
113 NSF4FunctionKey
, 0xC1,
114 NSF5FunctionKey
, 0xC2,
115 NSF6FunctionKey
, 0xC3,
116 NSF7FunctionKey
, 0xC4,
117 NSF8FunctionKey
, 0xC5,
118 NSF9FunctionKey
, 0xC6,
119 NSF10FunctionKey
, 0xC7,
120 NSF11FunctionKey
, 0xC8,
121 NSF12FunctionKey
, 0xC9,
122 NSF13FunctionKey
, 0xCA,
123 NSF14FunctionKey
, 0xCB,
124 NSF15FunctionKey
, 0xCC,
125 NSF16FunctionKey
, 0xCD,
126 NSF17FunctionKey
, 0xCE,
127 NSF18FunctionKey
, 0xCF,
128 NSF19FunctionKey
, 0xD0,
129 NSF20FunctionKey
, 0xD1,
130 NSF21FunctionKey
, 0xD2,
131 NSF22FunctionKey
, 0xD3,
132 NSF23FunctionKey
, 0xD4,
133 NSF24FunctionKey
, 0xD5,
135 NSBackspaceCharacter
, 0x08, /* 8: Not on some KBs. */
136 NSDeleteCharacter
, 0xFF, /* 127: Big 'delete' key upper right. */
137 NSDeleteFunctionKey
, 0x9F, /* 63272: Del forw key off main array. */
139 NSTabCharacter
, 0x09,
140 0x19, 0x09, /* left tab->regular since pass shift */
141 NSCarriageReturnCharacter
, 0x0D,
142 NSNewlineCharacter
, 0x0D,
143 NSEnterCharacter
, 0x8D,
145 0x41|NSNumericPadKeyMask
, 0xAE, /* KP_Decimal */
146 0x43|NSNumericPadKeyMask
, 0xAA, /* KP_Multiply */
147 0x45|NSNumericPadKeyMask
, 0xAB, /* KP_Add */
148 0x4B|NSNumericPadKeyMask
, 0xAF, /* KP_Divide */
149 0x4E|NSNumericPadKeyMask
, 0xAD, /* KP_Subtract */
150 0x51|NSNumericPadKeyMask
, 0xBD, /* KP_Equal */
151 0x52|NSNumericPadKeyMask
, 0xB0, /* KP_0 */
152 0x53|NSNumericPadKeyMask
, 0xB1, /* KP_1 */
153 0x54|NSNumericPadKeyMask
, 0xB2, /* KP_2 */
154 0x55|NSNumericPadKeyMask
, 0xB3, /* KP_3 */
155 0x56|NSNumericPadKeyMask
, 0xB4, /* KP_4 */
156 0x57|NSNumericPadKeyMask
, 0xB5, /* KP_5 */
157 0x58|NSNumericPadKeyMask
, 0xB6, /* KP_6 */
158 0x59|NSNumericPadKeyMask
, 0xB7, /* KP_7 */
159 0x5B|NSNumericPadKeyMask
, 0xB8, /* KP_8 */
160 0x5C|NSNumericPadKeyMask
, 0xB9, /* KP_9 */
162 0x1B, 0x1B /* escape */
165 static Lisp_Object Qmodifier_value
;
166 Lisp_Object Qalt
, Qcontrol
, Qhyper
, Qmeta
, Qsuper
;
167 extern Lisp_Object Qcursor_color
, Qcursor_type
, Qns
, Qleft
;
169 static Lisp_Object QUTF8_STRING
;
171 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
172 the maximum font size to NOT antialias. On GNUstep there is currently
173 no way to control this behavior. */
174 float ns_antialias_threshold
;
176 /* Used to pick up AppleHighlightColor on OS X */
177 NSString
*ns_selection_color
;
179 NSArray
*ns_send_types
=0, *ns_return_types
=0, *ns_drag_types
=0;
180 NSString
*ns_app_name
= @"Emacs"
; /* default changed later */
182 /* Display variables */
183 struct ns_display_info
*x_display_list
; /* Chain of existing displays */
184 Lisp_Object ns_display_name_list
;
185 long context_menu_value
= 0;
188 NSPoint last_mouse_motion_position
;
189 static NSRect last_mouse_glyph
;
190 static Time last_mouse_movement_time
= 0;
191 static Lisp_Object last_mouse_motion_frame
;
192 static EmacsScroller
*last_mouse_scroll_bar
= nil
;
193 static
struct frame
*ns_updating_frame
;
194 static NSView
*focus_view
= NULL;
195 static int ns_window_num
= 0;
196 #ifdef NS_IMPL_GNUSTEP
199 static BOOL gsaved
= NO
;
200 static BOOL ns_fake_keydown
= NO
;
201 int ns_tmp_flags
; /* FIXME */
202 struct nsfont_info
*ns_tmp_font
; /* FIXME */
204 static BOOL ns_menu_bar_is_hidden
= NO
;
206 /*static int debug_lock = 0; */
209 static BOOL send_appdefined
= YES
;
210 #define NO_APPDEFINED_DATA
(-8)
211 static int last_appdefined_event_data
= NO_APPDEFINED_DATA
;
212 static NSTimer
*timed_entry
= 0;
213 static NSTimer
*scroll_repeat_entry
= nil
;
214 static fd_set select_readfds
, select_writefds
;
215 enum
{ SELECT_HAVE_READ
= 1, SELECT_HAVE_WRITE
= 2, SELECT_HAVE_TMO
= 4 };
216 static int select_nfds
= 0, select_valid
= 0;
217 static
struct timespec select_timeout
= { 0, 0 };
218 static int selfds
[2] = { -1, -1 };
219 static pthread_mutex_t select_mutex
;
220 static int apploopnr
= 0;
221 static NSAutoreleasePool
*outerpool
;
222 static
struct input_event
*emacs_event
= NULL;
223 static
struct input_event
*q_event_ptr
= NULL;
224 static int n_emacs_events_pending
= 0;
225 static NSMutableArray
*ns_pending_files
, *ns_pending_service_names
,
226 *ns_pending_service_args
;
227 static BOOL ns_do_open_file
= NO
;
228 static BOOL ns_last_use_native_fullscreen
;
231 struct input_event
*q
;
239 * State for pending menu activation:
240 * MENU_NONE Normal state
241 * MENU_PENDING A menu has been clicked on, but has been canceled so we can
242 * run lisp to update the menu.
243 * MENU_OPENING Menu is up to date, and the click event is redone so the menu
247 #define MENU_PENDING
1
248 #define MENU_OPENING
2
249 static int menu_will_open_state
= MENU_NONE
;
251 /* Saved position for menu click. */
252 static CGPoint menu_mouse_point
;
255 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
256 #define NS_FUNCTION_KEY_MASK
0x800000
257 #define NSLeftControlKeyMask
(0x000001 | NSControlKeyMask
)
258 #define NSRightControlKeyMask
(0x002000 | NSControlKeyMask
)
259 #define NSLeftCommandKeyMask
(0x000008 | NSCommandKeyMask
)
260 #define NSRightCommandKeyMask
(0x000010 | NSCommandKeyMask
)
261 #define NSLeftAlternateKeyMask
(0x000020 | NSAlternateKeyMask
)
262 #define NSRightAlternateKeyMask
(0x000040 | NSAlternateKeyMask
)
263 #define EV_MODIFIERS
(e
) \
264 ((([e modifierFlags
] & NSHelpKeyMask
) ? \
265 hyper_modifier
: 0) \
266 |
(!EQ
(ns_right_alternate_modifier
, Qleft
) && \
267 (([e modifierFlags
] & NSRightAlternateKeyMask
) \
268 == NSRightAlternateKeyMask
) ? \
269 parse_solitary_modifier
(ns_right_alternate_modifier
) : 0) \
270 |
(([e modifierFlags
] & NSAlternateKeyMask
) ? \
271 parse_solitary_modifier
(ns_alternate_modifier
) : 0) \
272 |
(([e modifierFlags
] & NSShiftKeyMask
) ? \
273 shift_modifier
: 0) \
274 |
(!EQ
(ns_right_control_modifier
, Qleft
) && \
275 (([e modifierFlags
] & NSRightControlKeyMask
) \
276 == NSRightControlKeyMask
) ? \
277 parse_solitary_modifier
(ns_right_control_modifier
) : 0) \
278 |
(([e modifierFlags
] & NSControlKeyMask
) ? \
279 parse_solitary_modifier
(ns_control_modifier
) : 0) \
280 |
(([e modifierFlags
] & NS_FUNCTION_KEY_MASK
) ? \
281 parse_solitary_modifier
(ns_function_modifier
) : 0) \
282 |
(!EQ
(ns_right_command_modifier
, Qleft
) && \
283 (([e modifierFlags
] & NSRightCommandKeyMask
) \
284 == NSRightCommandKeyMask
) ? \
285 parse_solitary_modifier
(ns_right_command_modifier
) : 0) \
286 |
(([e modifierFlags
] & NSCommandKeyMask
) ? \
287 parse_solitary_modifier
(ns_command_modifier
):0))
289 #define EV_UDMODIFIERS
(e
) \
290 ((([e
type] == NSLeftMouseDown
) ? down_modifier
: 0) \
291 |
(([e
type] == NSRightMouseDown
) ? down_modifier
: 0) \
292 |
(([e
type] == NSOtherMouseDown
) ? down_modifier
: 0) \
293 |
(([e
type] == NSLeftMouseDragged
) ? down_modifier
: 0) \
294 |
(([e
type] == NSRightMouseDragged
) ? down_modifier
: 0) \
295 |
(([e
type] == NSOtherMouseDragged
) ? down_modifier
: 0) \
296 |
(([e
type] == NSLeftMouseUp
) ? up_modifier
: 0) \
297 |
(([e
type] == NSRightMouseUp
) ? up_modifier
: 0) \
298 |
(([e
type] == NSOtherMouseUp
) ? up_modifier
: 0))
300 #define EV_BUTTON
(e
) \
301 ((([e
type] == NSLeftMouseDown
) ||
([e
type] == NSLeftMouseUp
)) ?
0 : \
302 (([e
type] == NSRightMouseDown
) ||
([e
type] == NSRightMouseUp
)) ?
2 : \
303 [e buttonNumber
] - 1)
305 /* Convert the time field to a timestamp in milliseconds. */
306 #define EV_TIMESTAMP
(e
) ([e timestamp
] * 1000)
308 /* This is a piece of code which is common to all the event handling
309 methods. Maybe it should even be a function. */
310 #define EV_TRAILER
(e
) \
312 XSETFRAME
(emacs_event
->frame_or_window
, emacsframe
); \
313 if (e
) emacs_event
->timestamp
= EV_TIMESTAMP
(e
); \
316 n_emacs_events_pending
++; \
317 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
); \
320 hold_event
(emacs_event
); \
321 EVENT_INIT
(*emacs_event
); \
322 ns_send_appdefined
(-1); \
325 /* TODO: get rid of need for these forward declarations */
326 static void ns_condemn_scroll_bars
(struct frame
*f
);
327 static void ns_judge_scroll_bars
(struct frame
*f
);
328 void x_set_frame_alpha
(struct frame
*f
);
331 /* ==========================================================================
335 ========================================================================== */
338 hold_event
(struct input_event
*event
)
340 if (hold_event_q.nr
== hold_event_q.cap
)
342 if (hold_event_q.cap
== 0) hold_event_q.cap
= 10;
343 else hold_event_q.cap
*= 2;
345 xrealloc
(hold_event_q.q
, hold_event_q.cap
* sizeof
*hold_event_q.q
);
348 hold_event_q.q
[hold_event_q.nr
++] = *event
;
349 /* Make sure ns_read_socket is called, i.e. we have input. */
351 send_appdefined
= YES
;
355 append2
(Lisp_Object list
, Lisp_Object item
)
356 /* --------------------------------------------------------------------------
357 Utility to append to a list
358 -------------------------------------------------------------------------- */
360 Lisp_Object array
[2];
362 array
[1] = list1
(item
);
363 return Fnconc
(2, &array
[0]);
368 ns_etc_directory
(void
)
369 /* If running as a self-contained app bundle, return as a string the
370 filename of the etc directory, if present; else nil. */
372 NSBundle
*bundle
= [NSBundle mainBundle
];
373 NSString
*resourceDir
= [bundle resourcePath
];
374 NSString
*resourcePath
;
375 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
378 resourcePath
= [resourceDir stringByAppendingPathComponent
: @"etc"
];
379 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
381 if (isDir
) return [resourcePath UTF8String
];
389 /* If running as a self-contained app bundle, return as a path string
390 the filenames of the libexec and bin directories, ie libexec:bin.
391 Otherwise, return nil.
392 Normally, Emacs does not add its own bin/ directory to the PATH.
393 However, a self-contained NS build has a different layout, with
394 bin/ and libexec/ subdirectories in the directory that contains
396 We put libexec first, because init_callproc_1 uses the first
397 element to initialize exec-directory. An alternative would be
398 for init_callproc to check for invocation-directory/libexec.
401 NSBundle
*bundle
= [NSBundle mainBundle
];
402 NSString
*resourceDir
= [bundle resourcePath
];
403 NSString
*binDir
= [bundle bundlePath
];
404 NSString
*resourcePath
, *resourcePaths
;
406 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
407 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
409 NSEnumerator
*pathEnum
;
412 range
= [resourceDir rangeOfString
: @"Contents"
];
413 if (range.location
!= NSNotFound
)
415 binDir
= [binDir stringByAppendingPathComponent
: @"Contents"
];
417 binDir
= [binDir stringByAppendingPathComponent
: @"MacOS"
];
421 paths
= [binDir stringsByAppendingPaths
:
422 [NSArray arrayWithObjects
: @"libexec"
, @"bin"
, nil
]];
423 pathEnum
= [paths objectEnumerator
];
426 while ((resourcePath
= [pathEnum nextObject
]))
428 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
431 if ([resourcePaths
length] > 0)
433 = [resourcePaths stringByAppendingString
: pathSeparator
];
435 = [resourcePaths stringByAppendingString
: resourcePath
];
438 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
446 /* If running as a self-contained app bundle, return as a path string
447 the filenames of the site-lisp, lisp and leim directories.
448 Ie, site-lisp:lisp:leim. Otherwise, return nil. */
450 NSBundle
*bundle
= [NSBundle mainBundle
];
451 NSString
*resourceDir
= [bundle resourcePath
];
452 NSString
*resourcePath
, *resourcePaths
;
453 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
454 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
456 NSArray
*paths
= [resourceDir stringsByAppendingPaths
:
457 [NSArray arrayWithObjects
:
458 @"site
-lisp"
, @"lisp"
, @"leim"
, nil
]];
459 NSEnumerator
*pathEnum
= [paths objectEnumerator
];
462 /* Hack to skip site-lisp. */
463 if (no_site_lisp
) resourcePath
= [pathEnum nextObject
];
465 while ((resourcePath
= [pathEnum nextObject
]))
467 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
470 if ([resourcePaths
length] > 0)
472 = [resourcePaths stringByAppendingString
: pathSeparator
];
474 = [resourcePaths stringByAppendingString
: resourcePath
];
477 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
483 ns_timeout
(int usecs
)
484 /* --------------------------------------------------------------------------
485 Blocking timer utility used by ns_ring_bell
486 -------------------------------------------------------------------------- */
488 struct timespec wakeup
= timespec_add
(current_timespec
(),
489 make_timespec
(0, usecs
* 1000));
491 /* Keep waiting until past the time wakeup. */
494 struct timespec timeout
, now
= current_timespec
();
495 if (timespec_cmp
(wakeup
, now
) <= 0)
497 timeout
= timespec_sub
(wakeup
, now
);
499 /* Try to wait that long--but we might wake up sooner. */
500 pselect
(0, NULL, NULL, NULL, &timeout
, NULL);
506 ns_release_object
(void
*obj
)
507 /* --------------------------------------------------------------------------
508 Release an object (callable from C)
509 -------------------------------------------------------------------------- */
516 ns_retain_object
(void
*obj
)
517 /* --------------------------------------------------------------------------
518 Retain an object (callable from C)
519 -------------------------------------------------------------------------- */
526 ns_alloc_autorelease_pool
(void
)
527 /* --------------------------------------------------------------------------
528 Allocate a pool for temporary objects (callable from C)
529 -------------------------------------------------------------------------- */
531 return [[NSAutoreleasePool alloc
] init
];
536 ns_release_autorelease_pool
(void
*pool
)
537 /* --------------------------------------------------------------------------
538 Free a pool and temporary objects it refers to (callable from C)
539 -------------------------------------------------------------------------- */
541 ns_release_object
(pool
);
546 /* ==========================================================================
548 Focus (clipping) and screen update
550 ========================================================================== */
553 // Window constraining
554 // -------------------
556 // To ensure that the windows are not placed under the
menu bar, they
557 // are typically moved by the call
-back constrainFrameRect. However
,
558 // by overriding it
, it
's possible to inhibit this, leaving the window
559 // in it's original position.
561 // It
's possible to hide the menu bar. However, technically, it's only
562 // possible to hide it when the application is active. To ensure that
563 // this work properly
, the
menu bar and window constraining are
564 // deferred until the application becomes active.
566 // Even though it
's not possible to manually move a window above the
567 // top of the screen, it is allowed if it's done programmatically
,
568 // when the
menu is
hidden. This allows the editable area to cover the
569 // full screen height.
574 // Use the following extra files
:
577 // ;; Hide
menu and place frame slightly above the top of the screen.
578 // (setq ns
-auto
-hide
-menu-bar t
)
579 // (set-frame
-position
(selected
-frame
) 0 -20)
583 // emacs
-Q
-l init.el
585 // Result
: No
menu bar, and the
title bar should be above the screen.
591 // Result
: Menu bar visible
, frame placed immediately below the
menu.
595 ns_constrain_all_frames
(void
)
597 Lisp_Object tail
, frame
;
599 FOR_EACH_FRAME
(tail
, frame
)
601 struct frame
*f
= XFRAME
(frame
);
604 NSView
*view = FRAME_NS_VIEW
(f
);
605 /* This no-op will trigger the default window placing
606 * constraint system. */
607 f
->output_data.ns
->dont_constrain
= 0;
608 [[view window
] setFrameOrigin
:[[view window
] frame
].origin
];
614 /* True, if the menu bar should be hidden. */
617 ns_menu_bar_should_be_hidden
(void
)
619 return !NILP
(ns_auto_hide_menu_bar
)
620 && [NSApp respondsToSelector
:@selector
(setPresentationOptions
:)];
624 /* Show or hide the menu bar, based on user setting. */
627 ns_update_auto_hide_menu_bar
(void
)
630 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
633 NSTRACE
(ns_update_auto_hide_menu_bar
);
637 && [NSApp respondsToSelector
:@selector
(setPresentationOptions
:)])
639 // Note
, "setPresentationOptions" triggers an
error unless the
640 // application is active.
641 BOOL menu_bar_should_be_hidden
= ns_menu_bar_should_be_hidden
();
643 if (menu_bar_should_be_hidden
!= ns_menu_bar_is_hidden
)
645 NSApplicationPresentationOptions options
646 = NSApplicationPresentationAutoHideDock
;
648 if (menu_bar_should_be_hidden
)
649 options |
= NSApplicationPresentationAutoHideMenuBar
;
651 [NSApp setPresentationOptions
: options
];
653 ns_menu_bar_is_hidden
= menu_bar_should_be_hidden
;
655 if (!ns_menu_bar_is_hidden
)
657 ns_constrain_all_frames
();
669 ns_update_begin
(struct frame
*f
)
670 /* --------------------------------------------------------------------------
671 Prepare for a grouped sequence of drawing calls
672 external (RIF) call; whole frame, called before update_window_begin
673 -------------------------------------------------------------------------- */
675 NSView
*view = FRAME_NS_VIEW
(f
);
676 NSTRACE
(ns_update_begin
);
678 ns_update_auto_hide_menu_bar
();
680 ns_updating_frame
= f
;
683 /* drawRect may have been called for say the minibuffer, and then clip path
684 is for the minibuffer. But the display engine may draw more because
685 we have set the frame as garbaged. So reset clip path to the whole
690 NSRect r
= [view frame
];
691 NSRect cr
= [[view window
] frame
];
692 /* If a large frame size is set, r may be larger than the window frame
693 before constrained. In that case don't change the clip path, as we
694 will clear in to the tool bar and title bar. */
696 + FRAME_NS_TITLEBAR_HEIGHT
(f
)
697 + FRAME_TOOLBAR_HEIGHT
(f
) <= cr.
size.height
)
699 bp
= [[NSBezierPath bezierPathWithRect
: r
] retain
];
706 #ifdef NS_IMPL_GNUSTEP
707 uRect
= NSMakeRect
(0, 0, 0, 0);
713 ns_update_window_begin
(struct window
*w
)
714 /* --------------------------------------------------------------------------
715 Prepare for a grouped sequence of drawing calls
716 external (RIF) call; for one window, called after update_begin
717 -------------------------------------------------------------------------- */
719 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
720 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
722 NSTRACE
(ns_update_window_begin
);
723 set_output_cursor
(&w
->cursor
);
727 if (f
== hlinfo
->mouse_face_mouse_frame
)
729 /* Don't do highlighting for mouse motion during the update. */
730 hlinfo
->mouse_face_defer
= 1;
732 /* If the frame needs to be redrawn,
733 simply forget about any prior mouse highlighting. */
734 if (FRAME_GARBAGED_P
(f
))
735 hlinfo
->mouse_face_window
= Qnil
;
737 /* (further code for mouse faces ifdef'd out in other terms elided) */
745 ns_update_window_end
(struct window
*w
, bool cursor_on_p
,
746 bool mouse_face_overwritten_p
)
747 /* --------------------------------------------------------------------------
748 Finished a grouped sequence of drawing calls
749 external (RIF) call; for one window called before update_end
750 -------------------------------------------------------------------------- */
752 /* note: this fn is nearly identical in all terms */
753 if (!w
->pseudo_window_p
)
758 display_and_set_cursor
(w
, 1,
759 output_cursor.hpos
, output_cursor.vpos
,
760 output_cursor.x
, output_cursor.y
);
762 if (draw_window_fringes
(w
, 1))
763 x_draw_vertical_border
(w
);
768 /* If a row with mouse-face was overwritten, arrange for
769 frame_up_to_date to redisplay the mouse highlight. */
770 if (mouse_face_overwritten_p
)
771 reset_mouse_highlight
(MOUSE_HL_INFO
(XFRAME
(w
->frame
)));
773 NSTRACE
(update_window_end
);
778 ns_update_end
(struct frame
*f
)
779 /* --------------------------------------------------------------------------
780 Finished a grouped sequence of drawing calls
781 external (RIF) call; for whole frame, called after update_window_end
782 -------------------------------------------------------------------------- */
784 EmacsView
*view = FRAME_NS_VIEW
(f
);
786 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
787 MOUSE_HL_INFO
(f
)->mouse_face_defer
= 0;
792 [[view window
] flushWindow
];
795 ns_updating_frame
= NULL;
796 NSTRACE
(ns_update_end
);
801 ns_flush
(struct frame
*f
)
802 /* --------------------------------------------------------------------------
804 NS impl is no-op since currently we flush in ns_update_end and elsewhere
805 -------------------------------------------------------------------------- */
812 ns_focus
(struct frame
*f
, NSRect
*r
, int n
)
813 /* --------------------------------------------------------------------------
814 Internal: Focus on given frame. During small local updates this is used to
815 draw, however during large updates, ns_update_begin and ns_update_end are
816 called to wrap the whole thing, in which case these calls are stubbed out.
817 Except, on GNUstep, we accumulate the rectangle being drawn into, because
818 the back end won't do this automatically, and will just end up flushing
820 -------------------------------------------------------------------------- */
822 // NSTRACE
(ns_focus
);
824 fprintf (stderr, "focus: %d", c++);
825 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
826 fprintf (stderr, "\n"); */
828 if (f
!= ns_updating_frame
)
830 NSView
*view = FRAME_NS_VIEW
(f
);
831 if (view != focus_view
)
833 if (focus_view
!= NULL)
835 [focus_view unlockFocus
];
836 [[focus_view window
] flushWindow
];
843 /*if (view) debug_lock++; */
850 [[NSGraphicsContext currentContext
] saveGraphicsState
];
852 NSRectClipList
(r
, 2);
861 ns_unfocus
(struct frame
*f
)
862 /* --------------------------------------------------------------------------
863 Internal: Remove focus on given frame
864 -------------------------------------------------------------------------- */
866 // NSTRACE
(ns_unfocus
);
870 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
874 if (f
!= ns_updating_frame
)
876 if (focus_view
!= NULL)
878 [focus_view unlockFocus
];
879 [[focus_view window
] flushWindow
];
888 ns_clip_to_row
(struct window
*w
, struct glyph_row
*row
, int area
, BOOL gc
)
889 /* --------------------------------------------------------------------------
890 Internal (but parallels other terms): Focus drawing on given row
891 -------------------------------------------------------------------------- */
893 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
895 int window_x
, window_y
, window_width
;
897 window_box
(w
, area
, &window_x
, &window_y
, &window_width
, 0);
899 clip_rect.origin.x
= window_x
;
900 clip_rect.origin.y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, row
->y
));
901 clip_rect.origin.y
= max (clip_rect.origin.y
, window_y
);
902 clip_rect.
size.width
= window_width
;
903 clip_rect.
size.height
= row
->visible_height
;
905 ns_focus
(f
, &clip_rect
, 1);
910 ns_ring_bell
(struct frame
*f
)
911 /* --------------------------------------------------------------------------
913 -------------------------------------------------------------------------- */
915 NSTRACE
(ns_ring_bell
);
918 NSAutoreleasePool
*pool
;
919 struct frame
*frame
= SELECTED_FRAME
();
923 pool
= [[NSAutoreleasePool alloc
] init
];
925 view = FRAME_NS_VIEW
(frame
);
929 NSPoint dim
= NSMakePoint
(128, 128);
932 r.origin.x
+= (r.
size.width
- dim.x
) / 2;
933 r.origin.y
+= (r.
size.height
- dim.y
) / 2;
934 r.
size.width
= dim.x
;
935 r.
size.height
= dim.y
;
936 surr
= NSInsetRect
(r
, -2, -2);
937 ns_focus
(frame
, &surr
, 1);
938 [[view window
] cacheImageInRect
: [view convertRect
: surr toView
:nil
]];
939 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
940 (FRAME_DEFAULT_FACE
(frame
)), frame
) set];
942 [[view window
] flushWindow
];
944 [[view window
] restoreCachedImage
];
945 [[view window
] flushWindow
];
959 ns_reset_terminal_modes
(struct terminal *terminal)
960 /* Externally called as hook */
962 NSTRACE
(ns_reset_terminal_modes
);
967 ns_set_terminal_modes
(struct terminal *terminal)
968 /* Externally called as hook */
970 NSTRACE
(ns_set_terminal_modes
);
975 /* ==========================================================================
977 Frame / window manager related functions
979 ========================================================================== */
983 ns_raise_frame
(struct frame
*f
)
984 /* --------------------------------------------------------------------------
985 Bring window to foreground and make it active
986 -------------------------------------------------------------------------- */
989 check_window_system
(f
);
990 view = FRAME_NS_VIEW
(f
);
992 if (FRAME_VISIBLE_P
(f
))
993 [[view window
] makeKeyAndOrderFront
: NSApp
];
999 ns_lower_frame
(struct frame
*f
)
1000 /* --------------------------------------------------------------------------
1002 -------------------------------------------------------------------------- */
1005 check_window_system
(f
);
1006 view = FRAME_NS_VIEW
(f
);
1008 [[view window
] orderBack
: NSApp
];
1014 ns_frame_raise_lower
(struct frame
*f
, int raise
)
1015 /* --------------------------------------------------------------------------
1017 -------------------------------------------------------------------------- */
1019 NSTRACE
(ns_frame_raise_lower
);
1029 ns_frame_rehighlight
(struct frame
*frame
)
1030 /* --------------------------------------------------------------------------
1031 External (hook): called on things like window switching within frame
1032 -------------------------------------------------------------------------- */
1034 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(frame
);
1035 struct frame
*old_highlight
= dpyinfo
->x_highlight_frame
;
1037 NSTRACE
(ns_frame_rehighlight
);
1038 if (dpyinfo
->x_focus_frame
)
1040 dpyinfo
->x_highlight_frame
1041 = (FRAMEP
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1042 ? XFRAME
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1043 : dpyinfo
->x_focus_frame
);
1044 if (!FRAME_LIVE_P
(dpyinfo
->x_highlight_frame
))
1046 fset_focus_frame
(dpyinfo
->x_focus_frame
, Qnil
);
1047 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
;
1051 dpyinfo
->x_highlight_frame
= 0;
1053 if (dpyinfo
->x_highlight_frame
&&
1054 dpyinfo
->x_highlight_frame
!= old_highlight
)
1058 x_update_cursor
(old_highlight
, 1);
1059 x_set_frame_alpha
(old_highlight
);
1061 if (dpyinfo
->x_highlight_frame
)
1063 x_update_cursor
(dpyinfo
->x_highlight_frame
, 1);
1064 x_set_frame_alpha
(dpyinfo
->x_highlight_frame
);
1071 x_make_frame_visible
(struct frame
*f
)
1072 /* --------------------------------------------------------------------------
1073 External: Show the window (X11 semantics)
1074 -------------------------------------------------------------------------- */
1076 NSTRACE
(x_make_frame_visible
);
1077 /* XXX: at some points in past this was not needed, as the only place that
1078 called this (frame.c:Fraise_frame ()) also called raise_lower;
1079 if this ends up the case again, comment this out again. */
1080 if (!FRAME_VISIBLE_P
(f
))
1082 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1084 SET_FRAME_VISIBLE
(f
, 1);
1087 /* Making a new frame from a fullscreen frame will make the new frame
1088 fullscreen also. So skip handleFS as this will print an error. */
1089 if ([view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
1090 && [view isFullscreen
])
1093 if (f
->want_fullscreen
!= FULLSCREEN_NONE
)
1104 x_make_frame_invisible
(struct frame
*f
)
1105 /* --------------------------------------------------------------------------
1106 External: Hide the window (X11 semantics)
1107 -------------------------------------------------------------------------- */
1110 NSTRACE
(x_make_frame_invisible
);
1111 check_window_system
(f
);
1112 view = FRAME_NS_VIEW
(f
);
1113 [[view window
] orderOut
: NSApp
];
1114 SET_FRAME_VISIBLE
(f
, 0);
1115 SET_FRAME_ICONIFIED
(f
, 0);
1120 x_iconify_frame
(struct frame
*f
)
1121 /* --------------------------------------------------------------------------
1122 External: Iconify window
1123 -------------------------------------------------------------------------- */
1126 struct ns_display_info
*dpyinfo
;
1128 NSTRACE
(x_iconify_frame
);
1129 check_window_system
(f
);
1130 view = FRAME_NS_VIEW
(f
);
1131 dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1133 if (dpyinfo
->x_highlight_frame
== f
)
1134 dpyinfo
->x_highlight_frame
= 0;
1136 if ([[view window
] windowNumber
] <= 0)
1138 /* the window is still deferred. Make it very small, bring it
1139 on screen and order it out. */
1140 NSRect s
= { { 100, 100}, {0, 0} };
1142 t
= [[view window
] frame
];
1143 [[view window
] setFrame
: s display
: NO
];
1144 [[view window
] orderBack
: NSApp
];
1145 [[view window
] orderOut
: NSApp
];
1146 [[view window
] setFrame
: t display
: NO
];
1148 [[view window
] miniaturize
: NSApp
];
1151 /* Free X resources of frame F. */
1154 x_free_frame_resources
(struct frame
*f
)
1157 struct ns_display_info
*dpyinfo
;
1158 Mouse_HLInfo
*hlinfo
;
1160 NSTRACE
(x_free_frame_resources
);
1161 check_window_system
(f
);
1162 view = FRAME_NS_VIEW
(f
);
1163 dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1164 hlinfo
= MOUSE_HL_INFO
(f
);
1166 [(EmacsView
*)view setWindowClosing
: YES
]; /* may not have been informed */
1170 free_frame_menubar
(f
);
1172 if (FRAME_FACE_CACHE
(f
))
1173 free_frame_faces
(f
);
1175 if (f
== dpyinfo
->x_focus_frame
)
1176 dpyinfo
->x_focus_frame
= 0;
1177 if (f
== dpyinfo
->x_highlight_frame
)
1178 dpyinfo
->x_highlight_frame
= 0;
1179 if (f
== hlinfo
->mouse_face_mouse_frame
)
1180 reset_mouse_highlight
(hlinfo
);
1182 if (f
->output_data.ns
->miniimage
!= nil
)
1183 [f
->output_data.ns
->miniimage release
];
1185 [[view window
] close];
1188 xfree
(f
->output_data.ns
);
1194 x_destroy_window
(struct frame
*f
)
1195 /* --------------------------------------------------------------------------
1196 External: Delete the window
1197 -------------------------------------------------------------------------- */
1199 NSTRACE
(x_destroy_window
);
1200 check_window_system
(f
);
1201 x_free_frame_resources
(f
);
1207 x_set_offset
(struct frame
*f
, int xoff
, int yoff
, int change_grav
)
1208 /* --------------------------------------------------------------------------
1209 External: Position the window
1210 -------------------------------------------------------------------------- */
1212 NSView
*view = FRAME_NS_VIEW
(f
);
1213 NSArray
*screens
= [NSScreen screens
];
1214 NSScreen
*fscreen
= [screens objectAtIndex
: 0];
1215 NSScreen
*screen
= [[view window
] screen
];
1217 NSTRACE
(x_set_offset
);
1224 if (view != nil
&& screen
&& fscreen
)
1226 f
->left_pos
= f
->size_hint_flags
& XNegative
1227 ?
[screen visibleFrame
].
size.width
+ f
->left_pos
- FRAME_PIXEL_WIDTH
(f
)
1229 /* We use visibleFrame here to take menu bar into account.
1230 Ideally we should also adjust left/top with visibleFrame.origin. */
1232 f
->top_pos
= f
->size_hint_flags
& YNegative
1233 ?
([screen visibleFrame
].
size.height
+ f
->top_pos
1234 - FRAME_PIXEL_HEIGHT
(f
) - FRAME_NS_TITLEBAR_HEIGHT
(f
)
1235 - FRAME_TOOLBAR_HEIGHT
(f
))
1237 #ifdef NS_IMPL_GNUSTEP
1238 if (f
->left_pos
< 100)
1239 f
->left_pos
= 100; /* don't overlap menu */
1241 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1243 f
->output_data.ns
->dont_constrain
= 0;
1244 [[view window
] setFrameTopLeftPoint
:
1245 NSMakePoint
(SCREENMAXBOUND
(f
->left_pos
),
1246 SCREENMAXBOUND
([fscreen frame
].
size.height
1247 - NS_TOP_POS
(f
)))];
1248 f
->size_hint_flags
&= ~
(XNegative|YNegative
);
1256 x_set_window_size
(struct frame
*f
, int change_grav
, int cols
, int rows
)
1257 /* --------------------------------------------------------------------------
1258 Adjust window pixel size based on given character grid size
1259 Impl is a bit more complex than other terms, need to do some
1261 -------------------------------------------------------------------------- */
1263 EmacsView
*view = FRAME_NS_VIEW
(f
);
1264 NSWindow
*window
= [view window
];
1265 NSRect wr
= [window frame
];
1266 int tb
= FRAME_EXTERNAL_TOOL_BAR
(f
);
1267 int pixelwidth
, pixelheight
;
1269 NSTRACE
(x_set_window_size
);
1274 /*fprintf (stderr, "\tsetWindowSize: %d x %d, font size %d x %d\n", cols, rows, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f)); */
1278 check_frame_size
(f
, &rows
, &cols
);
1280 f
->scroll_bar_actual_width
= NS_SCROLL_BAR_WIDTH
(f
);
1281 compute_fringe_widths
(f
, 0);
1283 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, cols
);
1284 pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, rows
);
1286 /* If we have a toolbar, take its height into account. */
1287 if (tb
&& ! [view isFullscreen
])
1289 /* NOTE: previously this would generate wrong result if toolbar not
1290 yet displayed and fixing toolbar_height=32 helped, but
1291 now (200903) seems no longer needed */
1292 FRAME_TOOLBAR_HEIGHT
(f
) =
1293 NSHeight
([window frameRectForContentRect
: NSMakeRect
(0, 0, 0, 0)])
1294 - FRAME_NS_TITLEBAR_HEIGHT
(f
);
1295 #ifdef NS_IMPL_GNUSTEP
1296 FRAME_TOOLBAR_HEIGHT
(f
) -= 3;
1300 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
1302 wr.
size.width
= pixelwidth
+ f
->border_width
;
1303 wr.
size.height
= pixelheight
;
1304 if (! [view isFullscreen
])
1305 wr.
size.height
+= FRAME_NS_TITLEBAR_HEIGHT
(f
)
1306 + FRAME_TOOLBAR_HEIGHT
(f
);
1308 /* Do not try to constrain to this screen. We may have multiple
1309 screens, and want Emacs to span those. Constraining to screen
1310 prevents that, and that is not nice to the user. */
1311 if (f
->output_data.ns
->zooming
)
1312 f
->output_data.ns
->zooming
= 0;
1314 wr.origin.y
+= FRAME_PIXEL_HEIGHT
(f
) - pixelheight
;
1316 [view setRows
: rows andColumns
: cols
];
1317 [window setFrame
: wr display
: YES
];
1319 /*fprintf (stderr, "\tx_set_window_size %d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight); */
1321 /* This is a trick to compensate for Emacs' managing the scrollbar area
1322 as a fixed number of standard character columns. Instead of leaving
1323 blank space for the extra, we chopped it off above. Now for
1324 left-hand scrollbars, we shift all rendering to the left by the
1325 difference between the real width and Emacs' imagined one. For
1326 right-hand bars, don't worry about it since the extra is never used.
1327 (Obviously doesn't work for vertically split windows tho..) */
1329 NSPoint origin
= FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT
(f
)
1330 ? NSMakePoint
(FRAME_SCROLL_BAR_COLS
(f
) * FRAME_COLUMN_WIDTH
(f
)
1331 - NS_SCROLL_BAR_WIDTH
(f
), 0)
1332 : NSMakePoint
(0, 0);
1333 [view setFrame
: NSMakeRect
(0, 0, pixelwidth
, pixelheight
)];
1334 [view setBoundsOrigin
: origin
];
1337 change_frame_size
(f
, rows
, cols
, 0, 1, 0); /* pretend, delay, safe */
1338 FRAME_PIXEL_WIDTH
(f
) = pixelwidth
;
1339 FRAME_PIXEL_HEIGHT
(f
) = pixelheight
;
1340 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1342 mark_window_cursors_off
(XWINDOW
(f
->root_window
));
1343 cancel_mouse_face
(f
);
1350 ns_fullscreen_hook
(struct frame
*f
)
1352 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1354 if (!FRAME_VISIBLE_P
(f
))
1357 if (! [view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
)
1359 /* Old style fs don't initiate correctly if created from
1360 init/default-frame alist, so use a timer (not nice...).
1362 [NSTimer scheduledTimerWithTimeInterval
: 0.5 target
: view
1363 selector
: @selector
(handleFS
)
1364 userInfo
: nil repeats
: NO
];
1373 /* ==========================================================================
1377 ========================================================================== */
1381 ns_lookup_indexed_color
(unsigned
long idx
, struct frame
*f
)
1383 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1384 if (idx
< 1 || idx
>= color_table
->avail
)
1386 return color_table
->colors
[idx
];
1391 ns_index_color
(NSColor
*color
, struct frame
*f
)
1393 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1397 if (!color_table
->colors
)
1399 color_table
->size = NS_COLOR_CAPACITY
;
1400 color_table
->avail
= 1; /* skip idx=0 as marker */
1401 color_table
->colors
= xmalloc
(color_table
->size * sizeof
(NSColor
*));
1402 color_table
->colors
[0] = nil
;
1403 color_table
->empty_indices
= [[NSMutableSet alloc
] init
];
1406 /* do we already have this color ? */
1407 for (i
= 1; i
< color_table
->avail
; i
++)
1408 if (color_table
->colors
[i
] && [color_table
->colors
[i
] isEqual: color
])
1411 if ([color_table
->empty_indices count
] > 0)
1413 NSNumber
*index
= [color_table
->empty_indices anyObject
];
1414 [color_table
->empty_indices removeObject
: index
];
1415 idx
= [index unsignedLongValue
];
1419 if (color_table
->avail
== color_table
->size)
1420 color_table
->colors
=
1421 xpalloc
(color_table
->colors
, &color_table
->size, 1,
1422 min (ULONG_MAX
, PTRDIFF_MAX
), sizeof
*color_table
->colors
);
1423 idx
= color_table
->avail
++;
1426 color_table
->colors
[idx
] = color
;
1428 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1434 ns_free_indexed_color
(unsigned
long idx
, struct frame
*f
)
1436 struct ns_color_table
*color_table
;
1443 color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1445 if (idx
<= 0 || idx
>= color_table
->size) {
1446 message1
("ns_free_indexed_color
: Color index out of range.
\n"
);
1450 index
= [NSNumber numberWithUnsignedInt
: idx
];
1451 if ([color_table
->empty_indices containsObject
: index
]) {
1452 message1
("ns_free_indexed_color
: attempt to free already freed color.
\n"
);
1456 color
= color_table
->colors
[idx
];
1458 color_table
->colors
[idx
] = nil
;
1459 [color_table
->empty_indices addObject
: index
];
1460 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1465 ns_get_color
(const
char *name
, NSColor
**col
)
1466 /* --------------------------------------------------------------------------
1468 -------------------------------------------------------------------------- */
1469 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
1470 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1471 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1474 static
char hex
[20];
1476 float r
= -1.0, g
, b
;
1477 NSString
*nsname
= [NSString stringWithUTF8String
: name
];
1479 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1482 if ([nsname isEqualToString
: @"ns_selection_color"
])
1484 nsname
= ns_selection_color
;
1485 name
= [ns_selection_color UTF8String
];
1488 /* First, check for some sort of numeric specification. */
1491 if (name
[0] == '0' || name
[0] == '1' || name
[0] == '.') /* RGB decimal */
1493 NSScanner
*scanner
= [NSScanner scannerWithString
: nsname
];
1494 [scanner scanFloat
: &r
];
1495 [scanner scanFloat
: &g
];
1496 [scanner scanFloat
: &b
];
1498 else if (!strncmp
(name
, "rgb
:"
, 4)) /* A newer X11 format -- rgb:r/g/b */
1499 scaling
= (snprintf
(hex
, sizeof hex
, "
%s", name + 4) - 2) / 3;
1500 else if (name
[0] == '#') /* An old X11 format; convert to newer */
1502 int len
= (strlen
(name
) - 1);
1503 int start
= (len
% 3 == 0) ? 1 : len / 4 + 1;
1505 scaling
= strlen
(name
+start
) / 3;
1506 for (i
= 0; i
< 3; i
++)
1507 sprintf (hex
+ i
* (scaling
+ 1), "
%.*s/", scaling,
1508 name
+ start
+ i
* scaling
);
1509 hex
[3 * (scaling
+ 1) - 1] = '\0';
1515 float fscale
= scaling
== 4 ?
65535.0 : (scaling
== 2 ?
255.0 : 15.0);
1516 if (sscanf (hex
, "
%x/%x/%x", &rr, &gg, &bb))
1526 *col
= [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: 1.0];
1531 /* Otherwise, color is expected to be from a list */
1533 NSEnumerator
*lenum
, *cenum
;
1537 #ifdef NS_IMPL_GNUSTEP
1538 /* XXX: who is wrong, the requestor or the implementation? */
1539 if ([nsname compare
: @"Highlight" options
: NSCaseInsensitiveSearch
]
1541 nsname
= @"highlightColor"
;
1544 lenum
= [[NSColorList availableColorLists
] objectEnumerator
];
1545 while ( (clist
= [lenum nextObject
]) && new
== nil
)
1547 cenum
= [[clist allKeys
] objectEnumerator
];
1548 while ( (name
= [cenum nextObject
]) && new
== nil
)
1550 if ([name compare
: nsname
1551 options
: NSCaseInsensitiveSearch
] == NSOrderedSame
)
1552 new
= [clist colorWithKey
: name
];
1558 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1565 ns_lisp_to_color
(Lisp_Object color
, NSColor
**col
)
1566 /* --------------------------------------------------------------------------
1567 Convert a Lisp string object to a NS color
1568 -------------------------------------------------------------------------- */
1570 NSTRACE
(ns_lisp_to_color
);
1571 if (STRINGP
(color
))
1572 return ns_get_color
(SSDATA
(color
), col
);
1573 else if (SYMBOLP
(color
))
1574 return ns_get_color
(SSDATA
(SYMBOL_NAME
(color
)), col
);
1580 ns_color_to_lisp
(NSColor
*col
)
1581 /* --------------------------------------------------------------------------
1582 Convert a color to a lisp string with the RGB equivalent
1583 -------------------------------------------------------------------------- */
1585 EmacsCGFloat red
, green
, blue
, alpha
, gray;
1588 NSTRACE
(ns_color_to_lisp
);
1591 if ([[col colorSpaceName
] isEqualToString
: NSNamedColorSpace
])
1593 if ((str
=[[col colorNameComponent
] UTF8String
]))
1596 return build_string
((char *)str
);
1599 [[col colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
]
1600 getRed
: &red green
: &green blue
: &blue alpha
: &alpha
];
1601 if (red
==green
&& red
==blue
)
1603 [[col colorUsingColorSpaceName
: NSCalibratedWhiteColorSpace
]
1604 getWhite
: &gray alpha
: &alpha
];
1605 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1606 lrint
(gray * 0xff), lrint
(gray * 0xff), lrint
(gray * 0xff));
1608 return build_string
(buf
);
1611 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1612 lrint
(red
*0xff), lrint
(green
*0xff), lrint
(blue
*0xff));
1615 return build_string
(buf
);
1620 ns_query_color
(void
*col
, XColor
*color_def
, int setPixel
)
1621 /* --------------------------------------------------------------------------
1622 Get ARGB values out of NSColor col and put them into color_def.
1623 If setPixel, set the pixel to a concatenated version.
1624 and set color_def pixel to the resulting index.
1625 -------------------------------------------------------------------------- */
1627 EmacsCGFloat r
, g
, b
, a
;
1629 [((NSColor
*)col
) getRed
: &r green
: &g blue
: &b alpha
: &a
];
1630 color_def
->red
= r
* 65535;
1631 color_def
->green
= g
* 65535;
1632 color_def
->blue
= b
* 65535;
1634 if (setPixel
== YES
)
1636 = ARGB_TO_ULONG
((int
)(a
*255),
1637 (int
)(r
*255), (int
)(g
*255), (int
)(b
*255));
1642 ns_defined_color
(struct frame
*f
,
1647 /* --------------------------------------------------------------------------
1648 Return true if named color found, and set color_def rgb accordingly.
1649 If makeIndex and alloc are nonzero put the color in the color_table,
1650 and set color_def pixel to the resulting index.
1651 If makeIndex is zero, set color_def pixel to ARGB.
1652 Return false if not found
1653 -------------------------------------------------------------------------- */
1656 NSTRACE
(ns_defined_color
);
1659 if (ns_get_color
(name
, &col
) != 0) /* Color not found */
1664 if (makeIndex
&& alloc
)
1665 color_def
->pixel
= ns_index_color
(col
, f
);
1666 ns_query_color
(col
, color_def
, !makeIndex
);
1673 x_set_frame_alpha
(struct frame
*f
)
1674 /* --------------------------------------------------------------------------
1675 change the entire-frame transparency
1676 -------------------------------------------------------------------------- */
1678 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1680 double alpha_min
= 1.0;
1682 if (dpyinfo
->x_highlight_frame
== f
)
1683 alpha
= f
->alpha
[0];
1685 alpha
= f
->alpha
[1];
1687 if (FLOATP
(Vframe_alpha_lower_limit
))
1688 alpha_min
= XFLOAT_DATA
(Vframe_alpha_lower_limit
);
1689 else if (INTEGERP
(Vframe_alpha_lower_limit
))
1690 alpha_min
= (XINT
(Vframe_alpha_lower_limit
)) / 100.0;
1694 else if (1.0 < alpha
)
1696 else if (0.0 <= alpha
&& alpha
< alpha_min
&& alpha_min
<= 1.0)
1699 #ifdef NS_IMPL_COCOA
1701 EmacsView
*view = FRAME_NS_VIEW
(f
);
1702 [[view window
] setAlphaValue
: alpha
];
1708 /* ==========================================================================
1712 ========================================================================== */
1716 x_set_mouse_pixel_position
(struct frame
*f
, int pix_x
, int pix_y
)
1717 /* --------------------------------------------------------------------------
1718 Programmatically reposition mouse pointer in pixel coordinates
1719 -------------------------------------------------------------------------- */
1721 NSTRACE
(x_set_mouse_pixel_position
);
1724 /* FIXME: this does not work, and what about GNUstep? */
1725 #ifdef NS_IMPL_COCOA
1726 [FRAME_NS_VIEW
(f
) lockFocus
];
1727 PSsetmouse
((float
)pix_x
, (float
)pix_y
);
1728 [FRAME_NS_VIEW
(f
) unlockFocus
];
1735 x_set_mouse_position
(struct frame
*f
, int h
, int v
)
1736 /* --------------------------------------------------------------------------
1737 Programmatically reposition mouse pointer in character coordinates
1738 -------------------------------------------------------------------------- */
1742 pix_x
= FRAME_COL_TO_PIXEL_X
(f
, h
) + FRAME_COLUMN_WIDTH
(f
) / 2;
1743 pix_y
= FRAME_LINE_TO_PIXEL_Y
(f
, v
) + FRAME_LINE_HEIGHT
(f
) / 2;
1745 if (pix_x
< 0) pix_x
= 0;
1746 if (pix_x
> FRAME_PIXEL_WIDTH
(f
)) pix_x
= FRAME_PIXEL_WIDTH
(f
);
1748 if (pix_y
< 0) pix_y
= 0;
1749 if (pix_y
> FRAME_PIXEL_HEIGHT
(f
)) pix_y
= FRAME_PIXEL_HEIGHT
(f
);
1751 x_set_mouse_pixel_position
(f
, pix_x
, pix_y
);
1756 note_mouse_movement
(struct frame
*frame
, CGFloat x
, CGFloat y
)
1757 /* ------------------------------------------------------------------------
1758 Called by EmacsView on mouseMovement events. Passes on
1759 to emacs mainstream code if we moved off of a rect of interest
1760 known as last_mouse_glyph.
1761 ------------------------------------------------------------------------ */
1763 // NSTRACE
(note_mouse_movement
);
1765 XSETFRAME
(last_mouse_motion_frame
, frame
);
1767 /* Note, this doesn't get called for enter/leave, since we don't have a
1768 position. Those are taken care of in the corresponding NSView methods. */
1770 /* has movement gone beyond last rect we were tracking? */
1771 if (x
< last_mouse_glyph.origin.x ||
1772 x
>= (last_mouse_glyph.origin.x
+ last_mouse_glyph.
size.width
) ||
1773 y
< last_mouse_glyph.origin.y ||
1774 y
>= (last_mouse_glyph.origin.y
+ last_mouse_glyph.
size.height
))
1776 ns_update_begin
(frame
);
1777 frame
->mouse_moved
= 1;
1778 note_mouse_highlight
(frame
, x
, y
);
1779 remember_mouse_glyph
(frame
, x
, y
, &last_mouse_glyph
);
1780 ns_update_end
(frame
);
1789 ns_mouse_position
(struct frame
**fp
, int insist
, Lisp_Object
*bar_window
,
1790 enum scroll_bar_part
*part
, Lisp_Object
*x
, Lisp_Object
*y
,
1792 /* --------------------------------------------------------------------------
1793 External (hook): inform emacs about mouse position and hit parts.
1794 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1795 x & y should be position in the scrollbar (the whole bar, not the handle)
1796 and length of scrollbar respectively
1797 -------------------------------------------------------------------------- */
1801 Lisp_Object frame
, tail
;
1803 struct ns_display_info
*dpyinfo
;
1805 NSTRACE
(ns_mouse_position
);
1809 fprintf (stderr
, "Warning
: ns_mouse_position
() called with
null *fp.
\n"
);
1813 dpyinfo
= FRAME_NS_DISPLAY_INFO
(*fp
);
1817 if (last_mouse_scroll_bar
!= nil
&& insist
== 0)
1819 /* TODO: we do not use this path at the moment because drag events will
1820 go directly to the EmacsScroller. Leaving code in for now. */
1821 [last_mouse_scroll_bar getMouseMotionPart
: (int
*)part window
: bar_window
1823 if (time
) *time
= last_mouse_movement_time
;
1824 last_mouse_scroll_bar
= nil
;
1828 /* Clear the mouse-moved flag for every frame on this display. */
1829 FOR_EACH_FRAME
(tail
, frame
)
1830 if (FRAME_NS_P
(XFRAME
(frame
))
1831 && FRAME_NS_DISPLAY
(XFRAME
(frame
)) == FRAME_NS_DISPLAY
(*fp
))
1832 XFRAME
(frame
)->mouse_moved
= 0;
1834 last_mouse_scroll_bar
= nil
;
1835 if (last_mouse_frame
&& FRAME_LIVE_P
(last_mouse_frame
))
1836 f
= last_mouse_frame
;
1838 f
= dpyinfo
->x_focus_frame ? dpyinfo
->x_focus_frame
1839 : SELECTED_FRAME
();
1841 if (f
&& FRAME_NS_P
(f
))
1843 view = FRAME_NS_VIEW
(*fp
);
1845 position
= [[view window
] mouseLocationOutsideOfEventStream
];
1846 position
= [view convertPoint
: position fromView
: nil
];
1847 remember_mouse_glyph
(f
, position.x
, position.y
, &last_mouse_glyph
);
1848 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1850 if (bar_window
) *bar_window
= Qnil
;
1851 if (part
) *part
= 0; /*scroll_bar_handle; */
1853 if (x
) XSETINT
(*x
, lrint
(position.x
));
1854 if (y
) XSETINT
(*y
, lrint
(position.y
));
1855 if (time
) *time
= last_mouse_movement_time
;
1865 ns_frame_up_to_date
(struct frame
*f
)
1866 /* --------------------------------------------------------------------------
1867 External (hook): Fix up mouse highlighting right after a full update.
1868 Can't use FRAME_MOUSE_UPDATE due to ns_frame_begin and ns_frame_end calls.
1869 -------------------------------------------------------------------------- */
1871 NSTRACE
(ns_frame_up_to_date
);
1875 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
1876 if (f
== hlinfo
->mouse_face_mouse_frame
)
1880 note_mouse_highlight
(hlinfo
->mouse_face_mouse_frame
,
1881 hlinfo
->mouse_face_mouse_x
,
1882 hlinfo
->mouse_face_mouse_y
);
1891 ns_define_frame_cursor
(struct frame
*f
, Cursor cursor
)
1892 /* --------------------------------------------------------------------------
1893 External (RIF): set frame mouse pointer type.
1894 -------------------------------------------------------------------------- */
1896 NSTRACE
(ns_define_frame_cursor
);
1897 if (FRAME_POINTER_TYPE
(f
) != cursor
)
1899 EmacsView
*view = FRAME_NS_VIEW
(f
);
1900 FRAME_POINTER_TYPE
(f
) = cursor
;
1901 [[view window
] invalidateCursorRectsForView
: view];
1902 /* Redisplay assumes this function also draws the changed frame
1903 cursor, but this function doesn't, so do it explicitly. */
1904 x_update_cursor
(f
, 1);
1910 /* ==========================================================================
1914 ========================================================================== */
1918 ns_convert_key
(unsigned code
)
1919 /* --------------------------------------------------------------------------
1920 Internal call used by NSView-keyDown.
1921 -------------------------------------------------------------------------- */
1923 const unsigned last_keysym
= (sizeof
(convert_ns_to_X_keysym
)
1924 / sizeof
(convert_ns_to_X_keysym
[0]));
1926 /* An array would be faster, but less easy to read. */
1927 for (keysym
= 0; keysym
< last_keysym
; keysym
+= 2)
1928 if (code
== convert_ns_to_X_keysym
[keysym
])
1929 return 0xFF00 | convert_ns_to_X_keysym
[keysym
+1];
1931 /* if decide to use keyCode and Carbon table, use this line:
1932 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1937 x_get_keysym_name
(int keysym
)
1938 /* --------------------------------------------------------------------------
1939 Called by keyboard.c. Not sure if the return val is important, except
1941 -------------------------------------------------------------------------- */
1943 static
char value
[16];
1944 NSTRACE
(x_get_keysym_name
);
1945 sprintf (value
, "
%d", keysym);
1951 /* ==========================================================================
1953 Block drawing operations
1955 ========================================================================== */
1959 ns_redraw_scroll_bars
(struct frame
*f
)
1963 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
1964 NSTRACE
(ns_redraw_scroll_bars
);
1965 for (i
=[subviews count
]-1; i
>= 0; i
--)
1967 view = [subviews objectAtIndex
: i
];
1968 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
1975 ns_clear_frame
(struct frame
*f
)
1976 /* --------------------------------------------------------------------------
1977 External (hook): Erase the entire frame
1978 -------------------------------------------------------------------------- */
1980 NSView
*view = FRAME_NS_VIEW
(f
);
1983 NSTRACE
(ns_clear_frame
);
1985 /* comes on initial frame because we have
1986 after-make-frame-functions = select-frame */
1987 if (!FRAME_DEFAULT_FACE
(f
))
1990 mark_window_cursors_off
(XWINDOW
(FRAME_ROOT_WINDOW
(f
)));
1992 output_cursor.hpos
= output_cursor.vpos
= 0;
1993 output_cursor.x
= -1;
1998 ns_focus
(f
, &r
, 1);
1999 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(FRAME_DEFAULT_FACE
(f
)), f
) set];
2003 /* as of 2006/11 or so this is now needed */
2004 ns_redraw_scroll_bars
(f
);
2010 ns_clear_frame_area
(struct frame
*f
, int x
, int y
, int width
, int height
)
2011 /* --------------------------------------------------------------------------
2012 External (RIF): Clear section of frame
2013 -------------------------------------------------------------------------- */
2015 NSRect r
= NSMakeRect
(x
, y
, width
, height
);
2016 NSView
*view = FRAME_NS_VIEW
(f
);
2017 struct face
*face
= FRAME_DEFAULT_FACE
(f
);
2022 NSTRACE
(ns_clear_frame_area
);
2024 r
= NSIntersectionRect
(r
, [view frame
]);
2025 ns_focus
(f
, &r
, 1);
2026 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), f
) set];
2036 ns_scroll_run
(struct window
*w
, struct run
*run
)
2037 /* --------------------------------------------------------------------------
2038 External (RIF): Insert or delete n lines at line vpos
2039 -------------------------------------------------------------------------- */
2041 struct frame
*f
= XFRAME
(w
->frame
);
2042 int x
, y
, width
, height
, from_y
, to_y
, bottom_y
;
2044 NSTRACE
(ns_scroll_run
);
2046 /* begin copy from other terms */
2047 /* Get frame-relative bounding box of the text display area of W,
2048 without mode lines. Include in this box the left and right
2050 window_box
(w
, -1, &x
, &y
, &width
, &height
);
2052 from_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->current_y
);
2053 to_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->desired_y
);
2054 bottom_y
= y
+ height
;
2058 /* Scrolling up. Make sure we don't copy part of the mode
2059 line at the bottom. */
2060 if (from_y
+ run
->height
> bottom_y
)
2061 height
= bottom_y
- from_y
;
2063 height
= run
->height
;
2067 /* Scrolling down. Make sure we don't copy over the mode line.
2069 if (to_y
+ run
->height
> bottom_y
)
2070 height
= bottom_y
- to_y
;
2072 height
= run
->height
;
2074 /* end copy from other terms */
2084 NSRect srcRect
= NSMakeRect
(x
, from_y
, width
, height
);
2085 NSRect dstRect
= NSMakeRect
(x
, to_y
, width
, height
);
2086 NSPoint dstOrigin
= NSMakePoint
(x
, to_y
);
2088 ns_focus
(f
, &dstRect
, 1);
2089 NSCopyBits
(0, srcRect
, dstOrigin
);
2098 ns_after_update_window_line
(struct window
*w
, struct glyph_row
*desired_row
)
2099 /* --------------------------------------------------------------------------
2100 External (RIF): preparatory to fringe update after text was updated
2101 -------------------------------------------------------------------------- */
2106 NSTRACE
(ns_after_update_window_line
);
2108 /* begin copy from other terms */
2111 if (!desired_row
->mode_line_p
&& !w
->pseudo_window_p
)
2112 desired_row
->redraw_fringe_bitmaps_p
= 1;
2114 /* When a window has disappeared, make sure that no rest of
2115 full-width rows stays visible in the internal border. */
2116 if (windows_or_buffers_changed
2117 && desired_row
->full_width_p
2118 && (f
= XFRAME
(w
->frame
),
2119 width
= FRAME_INTERNAL_BORDER_WIDTH
(f
),
2121 && (height
= desired_row
->visible_height
,
2124 int y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, desired_row
->y
));
2127 ns_clear_frame_area
(f
, 0, y
, width
, height
);
2128 ns_clear_frame_area
(f
,
2129 FRAME_PIXEL_WIDTH
(f
) - width
,
2137 ns_shift_glyphs_for_insert
(struct frame
*f
,
2138 int x
, int y
, int width
, int height
,
2140 /* --------------------------------------------------------------------------
2141 External (RIF): copy an area horizontally, don't worry about clearing src
2142 -------------------------------------------------------------------------- */
2144 NSRect srcRect
= NSMakeRect
(x
, y
, width
, height
);
2145 NSRect dstRect
= NSMakeRect
(x
+shift_by
, y
, width
, height
);
2146 NSPoint dstOrigin
= dstRect.origin
;
2148 NSTRACE
(ns_shift_glyphs_for_insert
);
2150 ns_focus
(f
, &dstRect
, 1);
2151 NSCopyBits
(0, srcRect
, dstOrigin
);
2157 /* ==========================================================================
2159 Character encoding and metrics
2161 ========================================================================== */
2165 ns_compute_glyph_string_overhangs
(struct glyph_string
*s
)
2166 /* --------------------------------------------------------------------------
2167 External (RIF); compute left/right overhang of whole string and set in s
2168 -------------------------------------------------------------------------- */
2170 struct font
*font
= s
->font
;
2174 struct font_metrics metrics
;
2175 unsigned int codes
[2];
2176 codes
[0] = *(s
->char2b
);
2177 codes
[1] = *(s
->char2b
+ s
->nchars
- 1);
2179 font
->driver
->text_extents
(font
, codes
, 2, &metrics
);
2180 s
->left_overhang
= -metrics.lbearing
;
2182 = metrics.rbearing
> metrics.width
2183 ? metrics.rbearing
- metrics.width
: 0;
2187 s
->left_overhang
= 0;
2188 s
->right_overhang
= ((struct nsfont_info
*)font
)->ital ?
2189 FONT_HEIGHT
(font
) * 0.2 : 0;
2195 /* ==========================================================================
2197 Fringe and cursor drawing
2199 ========================================================================== */
2202 extern int max_used_fringe_bitmap
;
2204 ns_draw_fringe_bitmap
(struct window
*w
, struct glyph_row
*row
,
2205 struct draw_fringe_bitmap_params
*p
)
2206 /* --------------------------------------------------------------------------
2207 External (RIF); fringe-related
2208 -------------------------------------------------------------------------- */
2210 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2211 struct face
*face
= p
->face
;
2212 static EmacsImage
**bimgs
= NULL;
2213 static int nBimgs
= 0;
2215 /* grow bimgs if needed */
2216 if (nBimgs
< max_used_fringe_bitmap
)
2218 bimgs
= xrealloc
(bimgs
, max_used_fringe_bitmap
* sizeof
*bimgs
);
2219 memset
(bimgs
+ nBimgs
, 0,
2220 (max_used_fringe_bitmap
- nBimgs
) * sizeof
*bimgs
);
2221 nBimgs
= max_used_fringe_bitmap
;
2224 /* Must clip because of partially visible lines. */
2225 ns_clip_to_row
(w
, row
, -1, YES
);
2229 int bx
= p
->bx
, by
= p
->by
, nx
= p
->nx
, ny
= p
->ny
;
2231 /* If the fringe is adjacent to the left (right) scroll bar of a
2232 leftmost (rightmost, respectively) window, then extend its
2233 background to the gap between the fringe and the bar. */
2234 if ((WINDOW_LEFTMOST_P
(w
)
2235 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT
(w
))
2236 ||
(WINDOW_RIGHTMOST_P
(w
)
2237 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT
(w
)))
2239 int sb_width
= WINDOW_CONFIG_SCROLL_BAR_WIDTH
(w
);
2243 int bar_area_x
= WINDOW_SCROLL_BAR_AREA_X
(w
);
2244 int bar_area_width
= (WINDOW_CONFIG_SCROLL_BAR_COLS
(w
)
2245 * FRAME_COLUMN_WIDTH
(f
));
2249 /* Bitmap fills the fringe. */
2250 if (bar_area_x
+ bar_area_width
== p
->x
)
2251 bx
= bar_area_x
+ sb_width
;
2252 else if (p
->x
+ p
->wd
== bar_area_x
)
2256 int header_line_height
= WINDOW_HEADER_LINE_HEIGHT
(w
);
2258 nx
= bar_area_width
- sb_width
;
2259 by
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (header_line_height
,
2261 ny
= row
->visible_height
;
2266 if (bar_area_x
+ bar_area_width
== bx
)
2268 bx
= bar_area_x
+ sb_width
;
2269 nx
+= bar_area_width
- sb_width
;
2271 else if (bx
+ nx
== bar_area_x
)
2272 nx
+= bar_area_width
- sb_width
;
2277 if (bx
>= 0 && nx
> 0)
2279 NSRect r
= NSMakeRect
(bx
, by
, nx
, ny
);
2281 [ns_lookup_indexed_color
(face
->background
, f
) set];
2288 NSRect r
= NSMakeRect
(p
->x
, p
->y
, p
->wd
, p
->h
);
2289 EmacsImage
*img
= bimgs
[p
->which - 1];
2293 unsigned short
*bits
= p
->bits
+ p
->dh
;
2296 unsigned
char *cbits
= xmalloc
(len
);
2298 for (i
= 0; i
< len
; i
++)
2299 cbits
[i
] = ~
(bits
[i
] & 0xff);
2300 img
= [[EmacsImage alloc
] initFromXBM
: cbits width
: 8 height
: p
->h
2302 bimgs
[p
->which - 1] = img
;
2307 /* Since we composite the bitmap instead of just blitting it, we need
2308 to erase the whole background. */
2309 [ns_lookup_indexed_color
(face
->background
, f
) set];
2311 [img setXBMColor
: ns_lookup_indexed_color
(face
->foreground
, f
)];
2312 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
2314 fromRect
: NSZeroRect
2315 operation
: NSCompositeSourceOver
2321 NSPoint pt
= r.origin
;
2323 [img compositeToPoint
: pt operation
: NSCompositeSourceOver
];
2332 ns_draw_window_cursor
(struct window
*w
, struct glyph_row
*glyph_row
,
2333 int x
, int y
, enum text_cursor_kinds cursor_type
,
2334 int cursor_width
, bool on_p
, bool active_p
)
2335 /* --------------------------------------------------------------------------
2336 External call (RIF): draw cursor.
2337 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
2338 -------------------------------------------------------------------------- */
2341 int fx
, fy
, h
, cursor_height
;
2342 struct frame
*f
= WINDOW_XFRAME
(w
);
2343 struct glyph
*phys_cursor_glyph
;
2344 struct glyph
*cursor_glyph
;
2346 NSColor
*hollow_color
= FRAME_BACKGROUND_COLOR
(f
);
2348 /* If cursor is out of bounds, don't draw garbage. This can happen
2349 in mini-buffer windows when switching between echo area glyphs
2352 NSTRACE
(dumpcursor
);
2357 w
->phys_cursor_type
= cursor_type
;
2358 w
->phys_cursor_on_p
= on_p
;
2360 if (cursor_type
== NO_CURSOR
)
2362 w
->phys_cursor_width
= 0;
2366 if ((phys_cursor_glyph
= get_phys_cursor_glyph
(w
)) == NULL)
2368 if (glyph_row
->exact_window_width_line_p
2369 && w
->phys_cursor.hpos
>= glyph_row
->used
[TEXT_AREA
])
2371 glyph_row
->cursor_in_fringe_p
= 1;
2372 draw_fringe_bitmap
(w
, glyph_row
, 0);
2377 /* We draw the cursor (with NSRectFill), then draw the glyph on top
2378 (other terminals do it the other way round). We must set
2379 w->phys_cursor_width to the cursor width. For bar cursors, that
2380 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
2381 get_phys_cursor_geometry
(w
, glyph_row
, phys_cursor_glyph
, &fx
, &fy
, &h
);
2383 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
2384 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
2385 if (cursor_type
== BAR_CURSOR
)
2387 if (cursor_width
< 1)
2388 cursor_width
= max (FRAME_CURSOR_WIDTH
(f
), 1);
2389 w
->phys_cursor_width
= cursor_width
;
2391 /* If we have an HBAR, "cursor_width" MAY specify height. */
2392 else if (cursor_type
== HBAR_CURSOR
)
2394 cursor_height
= (cursor_width
< 1) ? lrint
(0.25 * h
) : cursor_width
;
2395 fy
+= h
- cursor_height
;
2399 r.origin.x
= fx
, r.origin.y
= fy
;
2401 r.
size.width
= w
->phys_cursor_width
;
2403 /* TODO: only needed in rare cases with last-resort font in HELLO..
2404 should we do this more efficiently? */
2405 ns_clip_to_row
(w
, glyph_row
, -1, NO
); /* do ns_focus(f, &r, 1); if remove */
2408 face
= FACE_FROM_ID
(f
, phys_cursor_glyph
->face_id
);
2409 if (face
&& NS_FACE_BACKGROUND
(face
)
2410 == ns_index_color
(FRAME_CURSOR_COLOR
(f
), f
))
2412 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), f
) set];
2413 hollow_color
= FRAME_CURSOR_COLOR
(f
);
2416 [FRAME_CURSOR_COLOR
(f
) set];
2418 #ifdef NS_IMPL_COCOA
2419 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2420 atomic. Cleaner ways of doing this should be investigated.
2421 One way would be to set a global variable DRAWING_CURSOR
2422 when making the call to draw_phys..(), don't focus in that
2423 case, then move the ns_unfocus() here after that call. */
2424 NSDisableScreenUpdates
();
2427 switch (cursor_type
)
2431 case FILLED_BOX_CURSOR
:
2434 case HOLLOW_BOX_CURSOR
:
2437 NSRectFill
(NSInsetRect
(r
, 1, 1));
2438 [FRAME_CURSOR_COLOR
(f
) set];
2445 /* If the character under cursor is R2L, draw the bar cursor
2446 on the right of its glyph, rather than on the left. */
2447 cursor_glyph
= get_phys_cursor_glyph
(w
);
2448 if ((cursor_glyph
->resolved_level
& 1) != 0)
2449 s.origin.x
+= cursor_glyph
->pixel_width
- s.
size.width
;
2456 /* draw the character under the cursor */
2457 if (cursor_type
!= NO_CURSOR
)
2458 draw_phys_cursor_glyph
(w
, glyph_row
, DRAW_CURSOR
);
2460 #ifdef NS_IMPL_COCOA
2461 NSEnableScreenUpdates
();
2468 ns_draw_vertical_window_border
(struct window
*w
, int x
, int y0
, int y1
)
2469 /* --------------------------------------------------------------------------
2470 External (RIF): Draw a vertical line.
2471 -------------------------------------------------------------------------- */
2473 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2475 NSRect r
= NSMakeRect
(x
, y0
, 1, y1
-y0
);
2477 NSTRACE
(ns_draw_vertical_window_border
);
2479 face
= FACE_FROM_ID
(f
, VERTICAL_BORDER_FACE_ID
);
2481 [ns_lookup_indexed_color
(face
->foreground
, f
) set];
2483 ns_focus
(f
, &r
, 1);
2490 show_hourglass
(struct atimer
*timer
)
2492 if (hourglass_shown_p
)
2497 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2499 hourglass_shown_p
= 1;
2505 hide_hourglass
(void
)
2507 if (!hourglass_shown_p
)
2512 /* TODO: remove NSProgressIndicator from all frames */
2514 hourglass_shown_p
= 0;
2520 /* ==========================================================================
2522 Glyph drawing operations
2524 ========================================================================== */
2527 ns_get_glyph_string_clip_rect
(struct glyph_string
*s
, NativeRectangle
*nr
)
2528 /* --------------------------------------------------------------------------
2529 Wrapper utility to account for internal border width on full-width lines,
2530 and allow top full-width rows to hit the frame top. nr should be pointer
2531 to two successive NSRects. Number of rects actually used is returned.
2532 -------------------------------------------------------------------------- */
2534 int n
= get_glyph_string_clip_rects
(s
, nr
, 2);
2538 /* --------------------------------------------------------------------
2539 Draw a wavy line under glyph string s. The wave fills wave_height
2546 wave_height = 3 | * * * *
2547 --------------------------------------------------------------------- */
2550 ns_draw_underwave
(struct glyph_string
*s
, EmacsCGFloat width
, EmacsCGFloat x
)
2552 int wave_height
= 3, wave_length
= 2;
2553 int y
, dx
, dy
, odd
, xmax
;
2558 dy
= wave_height
- 1;
2559 y
= s
->ybase
- wave_height
+ 3;
2562 /* Find and set clipping rectangle */
2563 waveClip
= NSMakeRect
(x
, y
, width
, wave_height
);
2564 [[NSGraphicsContext currentContext
] saveGraphicsState
];
2565 NSRectClip
(waveClip
);
2567 /* Draw the waves */
2568 a.x
= x
- ((int
)(x
) % dx) + (EmacsCGFloat) 0.5;
2570 odd
= (int
)(a.x
/dx
) % 2;
2571 a.y
= b.y
= y
+ 0.5;
2580 [NSBezierPath strokeLineFromPoint
:a toPoint
:b
];
2581 a.x
= b.x
, a.y
= b.y
;
2582 b.x
+= dx
, b.y
= y
+ 0.5 + odd
*dy
;
2586 /* Restore previous clipping rectangle(s) */
2587 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
2593 ns_draw_text_decoration
(struct glyph_string
*s
, struct face
*face
,
2594 NSColor
*defaultCol
, CGFloat width
, CGFloat x
)
2595 /* --------------------------------------------------------------------------
2596 Draw underline, overline, and strike-through on glyph string s.
2597 -------------------------------------------------------------------------- */
2599 if (s
->for_overlaps
)
2603 if (face
->underline_p
)
2605 if (s
->face
->underline_type
== FACE_UNDER_WAVE
)
2607 if (face
->underline_defaulted_p
)
2610 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2612 ns_draw_underwave
(s
, width
, x
);
2614 else if (s
->face
->underline_type
== FACE_UNDER_LINE
)
2618 unsigned
long thickness
, position
;
2620 /* If the prev was underlined, match its appearance. */
2621 if (s
->prev
&& s
->prev
->face
->underline_p
2622 && s
->prev
->face
->underline_type
== FACE_UNDER_LINE
2623 && s
->prev
->underline_thickness
> 0)
2625 thickness
= s
->prev
->underline_thickness
;
2626 position
= s
->prev
->underline_position
;
2631 unsigned
long descent
;
2634 descent
= s
->y
+ s
->height
- s
->ybase
;
2636 /* Use underline thickness of font, defaulting to 1. */
2637 thickness
= (font
&& font
->underline_thickness
> 0)
2638 ? font
->underline_thickness
: 1;
2640 /* Determine the offset of underlining from the baseline. */
2641 if (x_underline_at_descent_line
)
2642 position
= descent
- thickness
;
2643 else if (x_use_underline_position_properties
2644 && font
&& font
->underline_position
>= 0)
2645 position
= font
->underline_position
;
2647 position
= lround
(font
->descent
/ 2);
2649 position
= underline_minimum_offset
;
2651 position
= max (position
, underline_minimum_offset
);
2653 /* Ensure underlining is not cropped. */
2654 if (descent
<= position
)
2656 position
= descent
- 1;
2659 else if (descent
< position
+ thickness
)
2663 s
->underline_thickness
= thickness
;
2664 s
->underline_position
= position
;
2666 r
= NSMakeRect
(x
, s
->ybase
+ position
, width
, thickness
);
2668 if (face
->underline_defaulted_p
)
2671 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2675 /* Do overline. We follow other terms in using a thickness of 1
2676 and ignoring overline_margin. */
2677 if (face
->overline_p
)
2680 r
= NSMakeRect
(x
, s
->y
, width
, 1);
2682 if (face
->overline_color_defaulted_p
)
2685 [ns_lookup_indexed_color
(face
->overline_color
, s
->f
) set];
2689 /* Do strike-through. We follow other terms for thickness and
2690 vertical position.*/
2691 if (face
->strike_through_p
)
2696 dy
= lrint
((s
->height
- 1) / 2);
2697 r
= NSMakeRect
(x
, s
->y
+ dy
, width
, 1);
2699 if (face
->strike_through_color_defaulted_p
)
2702 [ns_lookup_indexed_color
(face
->strike_through_color
, s
->f
) set];
2708 ns_draw_box
(NSRect r
, CGFloat thickness
, NSColor
*col
,
2709 char left_p
, char right_p
)
2710 /* --------------------------------------------------------------------------
2711 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2712 Note we can't just use an NSDrawRect command, because of the possibility
2713 of some sides not being drawn, and because the rect will be filled.
2714 -------------------------------------------------------------------------- */
2720 s.
size.height
= thickness
;
2722 s.origin.y
+= r.
size.height
- thickness
;
2725 s.
size.height
= r.
size.height
;
2726 s.origin.y
= r.origin.y
;
2728 /* left, right (optional) */
2729 s.
size.width
= thickness
;
2734 s.origin.x
+= r.
size.width
- thickness
;
2741 ns_draw_relief
(NSRect r
, int thickness
, char raised_p
,
2742 char top_p
, char bottom_p
, char left_p
, char right_p
,
2743 struct glyph_string
*s
)
2744 /* --------------------------------------------------------------------------
2745 Draw a relief rect inside r, optionally leaving some sides open.
2746 Note we can't just use an NSDrawBezel command, because of the possibility
2747 of some sides not being drawn, and because the rect will be filled.
2748 -------------------------------------------------------------------------- */
2750 static NSColor
*baseCol
= nil
, *lightCol
= nil
, *darkCol
= nil
;
2751 NSColor
*newBaseCol
= nil
;
2754 NSTRACE
(ns_draw_relief
);
2758 if (s
->face
->use_box_color_for_shadows_p
)
2760 newBaseCol
= ns_lookup_indexed_color
(s
->face
->box_color
, s
->f
);
2762 /* else if (s->first_glyph->type == IMAGE_GLYPH
2764 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2766 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2770 newBaseCol
= ns_lookup_indexed_color
(s
->face
->background
, s
->f
);
2773 if (newBaseCol
== nil
)
2774 newBaseCol
= [NSColor grayColor
];
2776 if (newBaseCol
!= baseCol
) /* TODO: better check */
2779 baseCol
= [newBaseCol retain
];
2781 lightCol
= [[baseCol highlightWithLevel
: 0.2] retain
];
2783 darkCol
= [[baseCol shadowWithLevel
: 0.3] retain
];
2786 [(raised_p ? lightCol
: darkCol
) set];
2788 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2791 sr.
size.height
= thickness
;
2792 if (top_p
) NSRectFill
(sr
);
2795 sr.
size.height
= r.
size.height
;
2796 sr.
size.width
= thickness
;
2797 if (left_p
) NSRectFill
(sr
);
2799 [(raised_p ? darkCol
: lightCol
) set];
2802 sr.
size.width
= r.
size.width
;
2803 sr.
size.height
= thickness
;
2804 sr.origin.y
+= r.
size.height
- thickness
;
2805 if (bottom_p
) NSRectFill
(sr
);
2808 sr.
size.height
= r.
size.height
;
2809 sr.origin.y
= r.origin.y
;
2810 sr.
size.width
= thickness
;
2811 sr.origin.x
+= r.
size.width
- thickness
;
2812 if (right_p
) NSRectFill
(sr
);
2817 ns_dumpglyphs_box_or_relief
(struct glyph_string
*s
)
2818 /* --------------------------------------------------------------------------
2819 Function modeled after x_draw_glyph_string_box ().
2820 Sets up parameters for drawing.
2821 -------------------------------------------------------------------------- */
2823 int right_x
, last_x
;
2824 char left_p
, right_p
;
2825 struct glyph
*last_glyph
;
2830 if (s
->hl
== DRAW_MOUSE_FACE
)
2832 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2834 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2839 thickness
= face
->box_line_width
;
2841 NSTRACE
(ns_dumpglyphs_box_or_relief
);
2843 last_x
= ((s
->row
->full_width_p
&& !s
->w
->pseudo_window_p
)
2844 ? WINDOW_RIGHT_EDGE_X
(s
->w
)
2845 : window_box_right
(s
->w
, s
->area
));
2846 last_glyph
= (s
->cmp || s
->img
2847 ? s
->first_glyph
: s
->first_glyph
+ s
->nchars
-1);
2849 right_x
= ((s
->row
->full_width_p
&& s
->extends_to_end_of_line_p
2850 ? last_x
- 1 : min (last_x
, s
->x
+ s
->background_width
) - 1));
2852 left_p
= (s
->first_glyph
->left_box_line_p
2853 ||
(s
->hl
== DRAW_MOUSE_FACE
2854 && (s
->prev
== NULL || s
->prev
->hl
!= s
->hl
)));
2855 right_p
= (last_glyph
->right_box_line_p
2856 ||
(s
->hl
== DRAW_MOUSE_FACE
2857 && (s
->next
== NULL || s
->next
->hl
!= s
->hl
)));
2859 r
= NSMakeRect
(s
->x
, s
->y
, right_x
- s
->x
+ 1, s
->height
);
2861 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
2862 if (s
->face
->box
== FACE_SIMPLE_BOX
&& s
->face
->box_color
)
2864 ns_draw_box
(r
, abs (thickness
),
2865 ns_lookup_indexed_color
(face
->box_color
, s
->f
),
2870 ns_draw_relief
(r
, abs (thickness
), s
->face
->box
== FACE_RAISED_BOX
,
2871 1, 1, left_p
, right_p
, s
);
2877 ns_maybe_dumpglyphs_background
(struct glyph_string
*s
, char force_p
)
2878 /* --------------------------------------------------------------------------
2879 Modeled after x_draw_glyph_string_background, which draws BG in
2880 certain cases. Others are left to the text rendering routine.
2881 -------------------------------------------------------------------------- */
2883 NSTRACE
(ns_maybe_dumpglyphs_background
);
2885 if (!s
->background_filled_p
/* || s->hl == DRAW_MOUSE_FACE*/)
2887 int box_line_width
= max (s
->face
->box_line_width
, 0);
2888 if (FONT_HEIGHT
(s
->font
) < s
->height
- 2 * box_line_width
2889 || s
->font_not_found_p || s
->extends_to_end_of_line_p || force_p
)
2892 if (s
->hl
== DRAW_MOUSE_FACE
)
2894 face
= FACE_FROM_ID
(s
->f
,
2895 MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2897 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2900 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2902 [(NS_FACE_BACKGROUND
(face
) != 0
2903 ? ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
)
2904 : FRAME_BACKGROUND_COLOR
(s
->f
)) set];
2907 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(s
->f
);
2908 [[dpyinfo
->bitmaps
[face
->stipple
-1].img stippleMask
] set];
2911 if (s
->hl
!= DRAW_CURSOR
)
2913 NSRect r
= NSMakeRect
(s
->x
, s
->y
+ box_line_width
,
2914 s
->background_width
,
2915 s
->height
-2*box_line_width
);
2919 s
->background_filled_p
= 1;
2926 ns_dumpglyphs_image
(struct glyph_string
*s
, NSRect r
)
2927 /* --------------------------------------------------------------------------
2928 Renders an image and associated borders.
2929 -------------------------------------------------------------------------- */
2931 EmacsImage
*img
= s
->img
->pixmap
;
2932 int box_line_vwidth
= max (s
->face
->box_line_width
, 0);
2933 int x
= s
->x
, y
= s
->ybase
- image_ascent
(s
->img
, s
->face
, &s
->slice);
2934 int bg_x
, bg_y
, bg_height
;
2941 NSTRACE
(ns_dumpglyphs_image
);
2943 if (s
->face
->box
!= FACE_NO_BOX
2944 && s
->first_glyph
->left_box_line_p
&& s
->slice.x
== 0)
2945 x
+= abs (s
->face
->box_line_width
);
2948 bg_y
= s
->slice.y
== 0 ? s
->y
: s
->y
+ box_line_vwidth
;
2949 bg_height
= s
->height
;
2950 /* other terms have this, but was causing problems w/tabbar mode */
2951 /* - 2 * box_line_vwidth; */
2953 if (s
->slice.x
== 0) x
+= s
->img
->hmargin
;
2954 if (s
->slice.y
== 0) y
+= s
->img
->vmargin
;
2956 /* Draw BG: if we need larger area than image itself cleared, do that,
2957 otherwise, since we composite the image under NS (instead of mucking
2958 with its background color), we must clear just the image area. */
2959 if (s
->hl
== DRAW_MOUSE_FACE
)
2961 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2963 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2966 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2968 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
) set];
2970 if (bg_height
> s
->slice.height || s
->img
->hmargin || s
->img
->vmargin
2971 || s
->img
->mask || s
->img
->pixmap
== 0 || s
->width
!= s
->background_width
)
2973 br
= NSMakeRect
(bg_x
, bg_y
, s
->background_width
, bg_height
);
2974 s
->background_filled_p
= 1;
2978 br
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
2983 /* Draw the image.. do we need to draw placeholder if img ==nil? */
2986 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
2987 NSRect dr
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
2988 NSRect ir
= NSMakeRect
(s
->slice.x
, s
->slice.y
,
2989 s
->slice.width
, s
->slice.height
);
2992 operation
: NSCompositeSourceOver
2997 [img compositeToPoint
: NSMakePoint
(x
, y
+ s
->slice.height
)
2998 operation
: NSCompositeSourceOver
];
3002 if (s
->hl
== DRAW_CURSOR
)
3004 [FRAME_CURSOR_COLOR
(s
->f
) set];
3005 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3006 tdCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3008 /* Currently on NS img->mask is always 0. Since
3009 get_window_cursor_type specifies a hollow box cursor when on
3010 a non-masked image we never reach this clause. But we put it
3011 in in anticipation of better support for image masks on
3013 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3017 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3020 /* Draw underline, overline, strike-through. */
3021 ns_draw_text_decoration
(s
, face
, tdCol
, br.
size.width
, br.origin.x
);
3023 /* Draw relief, if requested */
3024 if (s
->img
->relief || s
->hl
==DRAW_IMAGE_RAISED || s
->hl
==DRAW_IMAGE_SUNKEN
)
3026 if (s
->hl
== DRAW_IMAGE_SUNKEN || s
->hl
== DRAW_IMAGE_RAISED
)
3028 th
= tool_bar_button_relief
>= 0 ?
3029 tool_bar_button_relief
: DEFAULT_TOOL_BAR_BUTTON_RELIEF
;
3030 raised_p
= (s
->hl
== DRAW_IMAGE_RAISED
);
3034 th
= abs (s
->img
->relief
);
3035 raised_p
= (s
->img
->relief
> 0);
3038 r.origin.x
= x
- th
;
3039 r.origin.y
= y
- th
;
3040 r.
size.width
= s
->slice.width
+ 2*th
-1;
3041 r.
size.height
= s
->slice.height
+ 2*th
-1;
3042 ns_draw_relief
(r
, th
, raised_p
,
3044 s
->slice.y
+ s
->slice.height
== s
->img
->height
,
3046 s
->slice.x
+ s
->slice.width
== s
->img
->width
, s
);
3049 /* If there is no mask, the background won't be seen,
3050 so draw a rectangle on the image for the cursor.
3051 Do this for all images, getting transparency right is not reliable. */
3052 if (s
->hl
== DRAW_CURSOR
)
3054 int thickness
= abs (s
->img
->relief
);
3055 if (thickness
== 0) thickness
= 1;
3056 ns_draw_box
(br
, thickness
, FRAME_CURSOR_COLOR
(s
->f
), 1, 1);
3062 ns_dumpglyphs_stretch
(struct glyph_string
*s
)
3067 NSColor
*fgCol
, *bgCol
;
3069 if (!s
->background_filled_p
)
3071 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3072 *r
= NSMakeRect
(s
->x
, s
->y
, s
->background_width
, s
->height
);
3074 ns_focus
(s
->f
, r
, n
);
3076 if (s
->hl
== DRAW_MOUSE_FACE
)
3078 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
3080 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
3083 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
3085 bgCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3086 fgCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3088 for (i
= 0; i
< n
; ++i
)
3090 if (!s
->row
->full_width_p
)
3092 int overrun
, leftoverrun
;
3094 /* truncate to avoid overwriting fringe and/or scrollbar */
3095 overrun
= max (0, (s
->x
+ s
->background_width
)
3096 - (WINDOW_BOX_RIGHT_EDGE_X
(s
->w
)
3097 - WINDOW_RIGHT_FRINGE_WIDTH
(s
->w
)));
3098 r
[i
].
size.width
-= overrun
;
3100 /* truncate to avoid overwriting to left of the window box */
3101 leftoverrun
= (WINDOW_BOX_LEFT_EDGE_X
(s
->w
)
3102 + WINDOW_LEFT_FRINGE_WIDTH
(s
->w
)) - s
->x
;
3104 if (leftoverrun
> 0)
3106 r
[i
].origin.x
+= leftoverrun
;
3107 r
[i
].
size.width
-= leftoverrun
;
3110 /* XXX: Try to work between problem where a stretch glyph on
3111 a partially-visible bottom row will clear part of the
3112 modeline, and another where list-buffers headers and similar
3113 rows erroneously have visible_height set to 0. Not sure
3114 where this is coming from as other terms seem not to show. */
3115 r
[i
].
size.height
= min (s
->height
, s
->row
->visible_height
);
3120 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3121 overwriting cursor (usually when cursor on a tab) */
3122 if (s
->hl
== DRAW_CURSOR
)
3127 width
= s
->w
->phys_cursor_width
;
3128 r
[i
].
size.width
-= width
;
3129 r
[i
].origin.x
+= width
;
3133 /* Draw overlining, etc. on the cursor. */
3134 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3135 ns_draw_text_decoration
(s
, face
, bgCol
, width
, x
);
3137 ns_draw_text_decoration
(s
, face
, fgCol
, width
, x
);
3144 /* Draw overlining, etc. on the stretch glyph (or the part
3145 of the stretch glyph after the cursor). */
3146 ns_draw_text_decoration
(s
, face
, fgCol
, r
[i
].
size.width
,
3150 s
->background_filled_p
= 1;
3156 ns_draw_glyph_string
(struct glyph_string
*s
)
3157 /* --------------------------------------------------------------------------
3158 External (RIF): Main draw-text call.
3159 -------------------------------------------------------------------------- */
3161 /* TODO (optimize): focus for box and contents draw */
3164 char box_drawn_p
= 0;
3166 NSTRACE
(ns_draw_glyph_string
);
3168 if (s
->next
&& s
->right_overhang
&& !s
->for_overlaps
/*&&s->hl!=DRAW_CURSOR*/)
3171 struct glyph_string
*next
;
3173 for (width
= 0, next
= s
->next
;
3174 next
&& width
< s
->right_overhang
;
3175 width
+= next
->width
, next
= next
->next
)
3176 if (next
->first_glyph
->type != IMAGE_GLYPH
)
3178 if (next
->first_glyph
->type != STRETCH_GLYPH
)
3180 n
= ns_get_glyph_string_clip_rect
(s
->next
, r
);
3181 ns_focus
(s
->f
, r
, n
);
3182 ns_maybe_dumpglyphs_background
(s
->next
, 1);
3187 ns_dumpglyphs_stretch
(s
->next
);
3189 next
->num_clips
= 0;
3193 if (!s
->for_overlaps
&& s
->face
->box
!= FACE_NO_BOX
3194 && (s
->first_glyph
->type == CHAR_GLYPH
3195 || s
->first_glyph
->type == COMPOSITE_GLYPH
))
3197 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3198 ns_focus
(s
->f
, r
, n
);
3199 ns_maybe_dumpglyphs_background
(s
, 1);
3200 ns_dumpglyphs_box_or_relief
(s
);
3205 switch (s
->first_glyph
->type)
3209 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3210 ns_focus
(s
->f
, r
, n
);
3211 ns_dumpglyphs_image
(s
, r
[0]);
3216 ns_dumpglyphs_stretch
(s
);
3220 case COMPOSITE_GLYPH
:
3221 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3222 ns_focus
(s
->f
, r
, n
);
3224 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3225 && ! s
->first_glyph
->u.cmp.automatic
))
3226 s
->background_filled_p
= 1;
3228 ns_maybe_dumpglyphs_background
3229 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3231 ns_tmp_flags
= s
->hl
== DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR
:
3232 (s
->hl
== DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE
:
3233 (s
->for_overlaps ? NS_DUMPGLYPH_FOREGROUND
:
3234 NS_DUMPGLYPH_NORMAL
));
3235 ns_tmp_font
= (struct nsfont_info
*)s
->face
->font
;
3236 if (ns_tmp_font
== NULL)
3237 ns_tmp_font
= (struct nsfont_info
*)FRAME_FONT
(s
->f
);
3239 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3241 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3242 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3243 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3246 ns_tmp_font
->font.driver
->draw
3247 (s
, 0, s
->nchars
, s
->x
, s
->y
,
3248 (ns_tmp_flags
== NS_DUMPGLYPH_NORMAL
&& !s
->background_filled_p
)
3249 || ns_tmp_flags
== NS_DUMPGLYPH_MOUSEFACE
);
3251 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3253 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3254 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3255 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3261 case GLYPHLESS_GLYPH
:
3262 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3263 ns_focus
(s
->f
, r
, n
);
3265 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3266 && ! s
->first_glyph
->u.cmp.automatic
))
3267 s
->background_filled_p
= 1;
3269 ns_maybe_dumpglyphs_background
3270 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3272 /* Not yet implemented. */
3281 /* Draw box if not done already. */
3282 if (!s
->for_overlaps
&& !box_drawn_p
&& s
->face
->box
!= FACE_NO_BOX
)
3284 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3285 ns_focus
(s
->f
, r
, n
);
3286 ns_dumpglyphs_box_or_relief
(s
);
3295 /* ==========================================================================
3299 ========================================================================== */
3303 ns_send_appdefined
(int value
)
3304 /* --------------------------------------------------------------------------
3305 Internal: post an appdefined event which EmacsApp-sendEvent will
3306 recognize and take as a command to halt the event loop.
3307 -------------------------------------------------------------------------- */
3309 /*NSTRACE (ns_send_appdefined); */
3311 #ifdef NS_IMPL_GNUSTEP
3312 // GNUStep needs postEvent to happen on the main thread.
3313 if (! [[NSThread currentThread
] isMainThread
])
3315 EmacsApp
*app
= (EmacsApp
*)NSApp
;
3316 app
->nextappdefined
= value
;
3317 [app performSelectorOnMainThread
:@selector
(sendFromMainThread
:)
3324 /* Only post this event if we haven't already posted one. This will end
3325 the [NXApp run] main loop after having processed all events queued at
3327 if (send_appdefined
)
3331 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3332 send_appdefined
= NO
;
3334 /* Don't need wakeup timer any more */
3337 [timed_entry invalidate
];
3338 [timed_entry release
];
3342 nxev
= [NSEvent otherEventWithType
: NSApplicationDefined
3343 location
: NSMakePoint
(0, 0)
3346 windowNumber
: [[NSApp mainWindow
] windowNumber
]
3347 context
: [NSApp context
]
3352 /* Post an application defined event on the event queue. When this is
3353 received the [NXApp run] will return, thus having processed all
3354 events which are currently queued. */
3355 [NSApp postEvent
: nxev atStart
: NO
];
3359 #ifdef HAVE_NATIVE_FS
3363 Lisp_Object frame
, tail
;
3365 if (ns_last_use_native_fullscreen
== ns_use_native_fullscreen
)
3368 ns_last_use_native_fullscreen
= ns_use_native_fullscreen
;
3370 /* Clear the mouse-moved flag for every frame on this display. */
3371 FOR_EACH_FRAME
(tail
, frame
)
3373 struct frame
*f
= XFRAME
(frame
);
3376 EmacsView
*view = FRAME_NS_VIEW
(f
);
3377 [view updateCollectionBehaviour
];
3383 /* GNUStep and OSX <= 10.4 does not have cancelTracking. */
3384 #
if defined
(NS_IMPL_COCOA
) && \
3385 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_5
3386 /* Check if menu open should be cancelled or continued as normal. */
3388 ns_check_menu_open
(NSMenu
*menu)
3390 /* Click in menu bar? */
3391 NSArray
*a
= [[NSApp mainMenu
] itemArray
];
3395 if (menu == nil
) // Menu tracking ended.
3397 if (menu_will_open_state
== MENU_OPENING
)
3398 menu_will_open_state
= MENU_NONE
;
3402 for (i
= 0; ! found
&& i
< [a count
]; i
++)
3403 found
= menu == [[a objectAtIndex
:i
] submenu
];
3406 if (menu_will_open_state
== MENU_NONE
&& emacs_event
)
3408 NSEvent
*theEvent
= [NSApp currentEvent
];
3409 struct frame
*emacsframe
= SELECTED_FRAME
();
3411 [menu cancelTracking
];
3412 menu_will_open_state
= MENU_PENDING
;
3413 emacs_event
->kind
= MENU_BAR_ACTIVATE_EVENT
;
3414 EV_TRAILER
(theEvent
);
3416 CGEventRef ourEvent
= CGEventCreate
(NULL);
3417 menu_mouse_point
= CGEventGetLocation
(ourEvent
);
3418 CFRelease
(ourEvent
);
3420 else if (menu_will_open_state
== MENU_OPENING
)
3422 menu_will_open_state
= MENU_NONE
;
3427 /* Redo saved menu click if state is MENU_PENDING. */
3429 ns_check_pending_open_menu
()
3431 if (menu_will_open_state
== MENU_PENDING
)
3433 CGEventSourceRef source
3434 = CGEventSourceCreate
(kCGEventSourceStateHIDSystemState
);
3436 CGEventRef event
= CGEventCreateMouseEvent
(source
,
3437 kCGEventLeftMouseDown
,
3439 kCGMouseButtonLeft
);
3440 CGEventSetType
(event
, kCGEventLeftMouseDown
);
3441 CGEventPost
(kCGHIDEventTap
, event
);
3445 menu_will_open_state
= MENU_OPENING
;
3448 #endif
/* NS_IMPL_COCOA) && >= MAC_OS_X_VERSION_10_5 */
3451 ns_read_socket
(struct terminal *terminal, struct input_event
*hold_quit
)
3452 /* --------------------------------------------------------------------------
3453 External (hook): Post an event to ourself and keep reading events until
3454 we read it back again. In effect process all events which were waiting.
3455 From 21+ we have to manage the event buffer ourselves.
3456 -------------------------------------------------------------------------- */
3458 struct input_event ev
;
3461 /* NSTRACE (ns_read_socket); */
3463 #ifdef HAVE_NATIVE_FS
3467 if ([NSApp modalWindow
] != nil
)
3470 if (hold_event_q.nr
> 0)
3473 for (i
= 0; i
< hold_event_q.nr
; ++i
)
3474 kbd_buffer_store_event_hold
(&hold_event_q.q
[i
], hold_quit
);
3475 hold_event_q.nr
= 0;
3480 n_emacs_events_pending
= 0;
3483 q_event_ptr
= hold_quit
;
3485 /* we manage autorelease pools by allocate/reallocate each time around
3486 the loop; strict nesting is occasionally violated but seems not to
3487 matter.. earlier methods using full nesting caused major memory leaks */
3488 [outerpool release
];
3489 outerpool
= [[NSAutoreleasePool alloc
] init
];
3491 /* If have pending open-file requests, attend to the next one of those. */
3492 if (ns_pending_files
&& [ns_pending_files count
] != 0
3493 && [(EmacsApp
*)NSApp openFile
: [ns_pending_files objectAtIndex
: 0]])
3495 [ns_pending_files removeObjectAtIndex
: 0];
3497 /* Deal with pending service requests. */
3498 else if (ns_pending_service_names
&& [ns_pending_service_names count
] != 0
3500 NSApp fulfillService
: [ns_pending_service_names objectAtIndex
: 0]
3501 withArg
: [ns_pending_service_args objectAtIndex
: 0]])
3503 [ns_pending_service_names removeObjectAtIndex
: 0];
3504 [ns_pending_service_args removeObjectAtIndex
: 0];
3508 /* Run and wait for events. We must always send one NX_APPDEFINED event
3509 to ourself, otherwise [NXApp run] will never exit. */
3510 send_appdefined
= YES
;
3511 ns_send_appdefined
(-1);
3513 if (++apploopnr
!= 1)
3521 nevents
= n_emacs_events_pending
;
3522 n_emacs_events_pending
= 0;
3523 emacs_event
= q_event_ptr
= NULL;
3531 ns_select
(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
3532 fd_set
*exceptfds
, struct timespec const
*timeout
,
3533 sigset_t const
*sigmask
)
3534 /* --------------------------------------------------------------------------
3535 Replacement for select, checking for events
3536 -------------------------------------------------------------------------- */
3540 struct input_event event
;
3543 /* NSTRACE (ns_select); */
3545 #ifdef HAVE_NATIVE_FS
3549 if (hold_event_q.nr
> 0)
3551 /* We already have events pending. */
3557 for (k
= 0; k
< nfds
+1; k
++)
3559 if (readfds
&& FD_ISSET
(k
, readfds
)) ++nr
;
3560 if (writefds
&& FD_ISSET
(k
, writefds
)) ++nr
;
3564 ||
(timeout
&& timeout
->tv_sec
== 0 && timeout
->tv_nsec
== 0))
3565 return pselect
(nfds
, readfds
, writefds
, exceptfds
, timeout
, sigmask
);
3567 [outerpool release
];
3568 outerpool
= [[NSAutoreleasePool alloc
] init
];
3571 send_appdefined
= YES
;
3574 pthread_mutex_lock
(&select_mutex
);
3579 select_readfds
= *readfds
;
3580 select_valid
+= SELECT_HAVE_READ
;
3584 select_writefds
= *writefds
;
3585 select_valid
+= SELECT_HAVE_WRITE
;
3590 select_timeout
= *timeout
;
3591 select_valid
+= SELECT_HAVE_TMO
;
3594 pthread_mutex_unlock
(&select_mutex
);
3596 /* Inform fd_handler that select should be called */
3598 emacs_write_sig
(selfds
[1], &c
, 1);
3600 else if (nr
== 0 && timeout
)
3602 /* No file descriptor, just a timeout, no need to wake fd_handler */
3603 double time
= timespectod
(*timeout
);
3604 timed_entry
= [[NSTimer scheduledTimerWithTimeInterval
: time
3607 @selector
(timeout_handler
:)
3612 else /* No timeout and no file descriptors, can this happen? */
3614 /* Send appdefined so we exit from the loop */
3615 ns_send_appdefined
(-1);
3620 emacs_event
= &event
;
3621 if (++apploopnr
!= 1)
3628 if (nr
> 0 && readfds
)
3631 emacs_write_sig
(selfds
[1], &c
, 1);
3635 t
= last_appdefined_event_data
;
3637 if (t
!= NO_APPDEFINED_DATA
)
3639 last_appdefined_event_data
= NO_APPDEFINED_DATA
;
3643 /* The NX_APPDEFINED event we received was a timeout. */
3648 /* The NX_APPDEFINED event we received was the result of
3649 at least one real input event arriving. */
3655 /* Received back from select () in fd_handler; copy the results */
3656 pthread_mutex_lock
(&select_mutex
);
3657 if (readfds
) *readfds
= select_readfds
;
3658 if (writefds
) *writefds
= select_writefds
;
3659 pthread_mutex_unlock
(&select_mutex
);
3674 /* ==========================================================================
3678 ========================================================================== */
3682 ns_set_vertical_scroll_bar
(struct window
*window
,
3683 int portion
, int whole
, int position
)
3684 /* --------------------------------------------------------------------------
3685 External (hook): Update or add scrollbar
3686 -------------------------------------------------------------------------- */
3690 struct frame
*f
= XFRAME
(WINDOW_FRAME
(window
));
3691 EmacsView
*view = FRAME_NS_VIEW
(f
);
3692 int window_y
, window_height
;
3693 int top
, left
, height
, width
, sb_width
, sb_left
;
3695 BOOL fringe_extended_p
;
3697 /* optimization; display engine sends WAY too many of these.. */
3698 if (!NILP
(window
->vertical_scroll_bar
))
3700 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3701 if ([bar checkSamePosition
: position portion
: portion whole
: whole
])
3703 if (view->scrollbarsNeedingUpdate
== 0)
3705 if (!windows_or_buffers_changed
)
3709 view->scrollbarsNeedingUpdate
--;
3713 NSTRACE
(ns_set_vertical_scroll_bar
);
3715 /* Get dimensions. */
3716 window_box
(window
, -1, 0, &window_y
, 0, &window_height
);
3718 height
= window_height
;
3719 width
= WINDOW_CONFIG_SCROLL_BAR_COLS
(window
) * FRAME_COLUMN_WIDTH
(f
);
3720 left
= WINDOW_SCROLL_BAR_AREA_X
(window
);
3722 /* allow for displaying a skinnier scrollbar than char area allotted */
3723 sb_width
= (WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) > 0) ?
3724 WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) : width
;
3727 r
= NSMakeRect
(sb_left
, top
, sb_width
, height
);
3728 /* the parent view is flipped, so we need to flip y value */
3730 r.origin.y
= (v.
size.height
- r.
size.height
- r.origin.y
);
3732 fringe_extended_p
= WINDOW_FRINGE_EXTENDED_P
(window
);
3734 XSETWINDOW
(win
, window
);
3737 /* we want at least 5 lines to display a scrollbar */
3738 if (WINDOW_TOTAL_LINES
(window
) < 5)
3740 if (!NILP
(window
->vertical_scroll_bar
))
3742 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3743 [bar removeFromSuperview
];
3744 wset_vertical_scroll_bar
(window
, Qnil
);
3746 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3751 if (NILP
(window
->vertical_scroll_bar
))
3753 if (width
> 0 && height
> 0)
3755 if (fringe_extended_p
)
3756 ns_clear_frame_area
(f
, sb_left
, top
, sb_width
, height
);
3758 ns_clear_frame_area
(f
, left
, top
, width
, height
);
3761 bar = [[EmacsScroller alloc
] initFrame
: r window
: win
];
3762 wset_vertical_scroll_bar
(window
, make_save_ptr
(bar));
3767 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3768 oldRect
= [bar frame
];
3769 r.
size.width
= oldRect.
size.width
;
3770 if (FRAME_LIVE_P
(f
) && !NSEqualRects
(oldRect
, r
))
3772 if (oldRect.origin.x
!= r.origin.x
)
3773 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3778 [bar setPosition
: position portion
: portion whole
: whole
];
3784 ns_condemn_scroll_bars
(struct frame
*f
)
3785 /* --------------------------------------------------------------------------
3786 External (hook): arrange for all frame's scrollbars to be removed
3787 at next call to judge_scroll_bars, except for those redeemed.
3788 -------------------------------------------------------------------------- */
3792 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
3794 NSTRACE
(ns_condemn_scroll_bars
);
3796 for (i
=[subviews count
]-1; i
>= 0; i
--)
3798 view = [subviews objectAtIndex
: i
];
3799 if ([view isKindOfClass
: [EmacsScroller
class]])
3806 ns_redeem_scroll_bar
(struct window
*window
)
3807 /* --------------------------------------------------------------------------
3808 External (hook): arrange to spare this window's scrollbar
3809 at next call to judge_scroll_bars.
3810 -------------------------------------------------------------------------- */
3813 NSTRACE
(ns_redeem_scroll_bar
);
3814 if (!NILP
(window
->vertical_scroll_bar
))
3816 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3823 ns_judge_scroll_bars
(struct frame
*f
)
3824 /* --------------------------------------------------------------------------
3825 External (hook): destroy all scrollbars on frame that weren't
3826 redeemed after call to condemn_scroll_bars.
3827 -------------------------------------------------------------------------- */
3831 EmacsView
*eview
= FRAME_NS_VIEW
(f
);
3832 NSArray
*subviews
= [[eview superview
] subviews
];
3835 NSTRACE
(ns_judge_scroll_bars
);
3836 for (i
= [subviews count
]-1; i
>= 0; --i
)
3838 view = [subviews objectAtIndex
: i
];
3839 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
3845 [eview updateFrameSize
: NO
];
3848 /* ==========================================================================
3852 ========================================================================== */
3855 x_display_pixel_height
(struct ns_display_info
*dpyinfo
)
3857 NSArray
*screens
= [NSScreen screens
];
3858 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3863 while ((screen
= [enumerator nextObject
]) != nil
)
3864 frame
= NSUnionRect
(frame
, [screen frame
]);
3866 return NSHeight
(frame
);
3870 x_display_pixel_width
(struct ns_display_info
*dpyinfo
)
3872 NSArray
*screens
= [NSScreen screens
];
3873 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3878 while ((screen
= [enumerator nextObject
]) != nil
)
3879 frame
= NSUnionRect
(frame
, [screen frame
]);
3881 return NSWidth
(frame
);
3885 static Lisp_Object ns_string_to_lispmod
(const
char *s
)
3886 /* --------------------------------------------------------------------------
3887 Convert modifier name to lisp symbol
3888 -------------------------------------------------------------------------- */
3890 if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qmeta
)), s
, 10))
3892 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qsuper
)), s
, 10))
3894 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qcontrol
)), s
, 10))
3896 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qalt
)), s
, 10))
3898 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qhyper
)), s
, 10))
3900 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qnone
)), s
, 10))
3908 ns_default
(const
char *parameter
, Lisp_Object
*result
,
3909 Lisp_Object yesval
, Lisp_Object noval
,
3910 BOOL is_float
, BOOL is_modstring
)
3911 /* --------------------------------------------------------------------------
3912 Check a parameter value in user's preferences
3913 -------------------------------------------------------------------------- */
3915 const
char *value
= ns_get_defaults_value
(parameter
);
3921 if (c_strcasecmp
(value
, "YES"
) == 0)
3923 else if (c_strcasecmp
(value
, "NO"
) == 0)
3925 else if (is_float
&& (f
= strtod
(value
, &pos
), pos
!= value
))
3926 *result
= make_float
(f
);
3927 else if (is_modstring
&& value
)
3928 *result
= ns_string_to_lispmod
(value
);
3929 else fprintf (stderr
,
3930 "Bad value
for default
\"%s\": \"%s\"\n", parameter, value);
3936 ns_initialize_display_info
(struct ns_display_info
*dpyinfo
)
3937 /* --------------------------------------------------------------------------
3938 Initialize global info and storage for display.
3939 -------------------------------------------------------------------------- */
3941 NSScreen
*screen
= [NSScreen mainScreen
];
3942 NSWindowDepth depth
= [screen depth
];
3944 dpyinfo
->resx
= 72.27; /* used 75.0, but this makes pt == pixel, expected */
3945 dpyinfo
->resy
= 72.27;
3946 dpyinfo
->color_p
= ![NSDeviceWhiteColorSpace isEqualToString
:
3947 NSColorSpaceFromDepth
(depth
)]
3948 && ![NSCalibratedWhiteColorSpace isEqualToString
:
3949 NSColorSpaceFromDepth
(depth
)];
3950 dpyinfo
->n_planes
= NSBitsPerPixelFromDepth
(depth
);
3951 dpyinfo
->image_cache
= make_image_cache
();
3952 dpyinfo
->color_table
= xmalloc
(sizeof
*dpyinfo
->color_table
);
3953 dpyinfo
->color_table
->colors
= NULL;
3954 dpyinfo
->root_window
= 42; /* a placeholder.. */
3955 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
= NULL;
3956 dpyinfo
->n_fonts
= 0;
3957 dpyinfo
->smallest_font_height
= 1;
3958 dpyinfo
->smallest_char_width
= 1;
3960 reset_mouse_highlight
(&dpyinfo
->mouse_highlight
);
3964 /* This and next define (many of the) public functions in this file. */
3965 /* x_... are generic versions in xdisp.c that we, and other terms, get away
3966 with using despite presence in the "system dependent" redisplay
3967 interface. In addition, many of the ns_ methods have code that is
3968 shared with all terms, indicating need for further refactoring. */
3969 extern frame_parm_handler ns_frame_parm_handlers
[];
3970 static
struct redisplay_interface ns_redisplay_interface
=
3972 ns_frame_parm_handlers
,
3976 x_clear_end_of_line
,
3978 ns_after_update_window_line
,
3979 ns_update_window_begin
,
3980 ns_update_window_end
,
3983 0, /* flush_display_optional */
3984 x_clear_window_mouse_face
,
3985 x_get_glyph_overhangs
,
3986 x_fix_overlapping_area
,
3987 ns_draw_fringe_bitmap
,
3988 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
3989 0, /* destroy_fringe_bitmap */
3990 ns_compute_glyph_string_overhangs
,
3991 ns_draw_glyph_string
, /* interface to nsfont.m */
3992 ns_define_frame_cursor
,
3993 ns_clear_frame_area
,
3994 ns_draw_window_cursor
,
3995 ns_draw_vertical_window_border
,
3996 ns_shift_glyphs_for_insert
4001 ns_delete_display
(struct ns_display_info
*dpyinfo
)
4007 /* This function is called when the last frame on a display is deleted. */
4009 ns_delete_terminal
(struct terminal *terminal)
4011 struct ns_display_info
*dpyinfo
= terminal->display_info.ns
;
4013 /* Protect against recursive calls. delete_frame in
4014 delete_terminal calls us back when it deletes our last frame. */
4015 if (!terminal->name
)
4020 x_destroy_all_bitmaps
(dpyinfo
);
4021 ns_delete_display
(dpyinfo
);
4026 static
struct terminal *
4027 ns_create_terminal
(struct ns_display_info
*dpyinfo
)
4028 /* --------------------------------------------------------------------------
4029 Set up use of NS before we make the first connection.
4030 -------------------------------------------------------------------------- */
4032 struct terminal *terminal;
4034 NSTRACE
(ns_create_terminal
);
4036 terminal = create_terminal
();
4038 terminal->type = output_ns
;
4039 terminal->display_info.ns
= dpyinfo
;
4040 dpyinfo
->terminal = terminal;
4042 terminal->rif
= &ns_redisplay_interface
;
4044 terminal->clear_frame_hook
= ns_clear_frame
;
4045 terminal->ins_del_lines_hook
= 0; /* XXX vestigial? */
4046 terminal->delete_glyphs_hook
= 0; /* XXX vestigial? */
4047 terminal->ring_bell_hook
= ns_ring_bell
;
4048 terminal->reset_terminal_modes_hook
= ns_reset_terminal_modes
;
4049 terminal->set_terminal_modes_hook
= ns_set_terminal_modes
;
4050 terminal->update_begin_hook
= ns_update_begin
;
4051 terminal->update_end_hook
= ns_update_end
;
4052 terminal->set_terminal_window_hook
= NULL; /* XXX vestigial? */
4053 terminal->read_socket_hook
= ns_read_socket
;
4054 terminal->frame_up_to_date_hook
= ns_frame_up_to_date
;
4055 terminal->mouse_position_hook
= ns_mouse_position
;
4056 terminal->frame_rehighlight_hook
= ns_frame_rehighlight
;
4057 terminal->frame_raise_lower_hook
= ns_frame_raise_lower
;
4059 terminal->fullscreen_hook
= ns_fullscreen_hook
;
4061 terminal->set_vertical_scroll_bar_hook
= ns_set_vertical_scroll_bar
;
4062 terminal->condemn_scroll_bars_hook
= ns_condemn_scroll_bars
;
4063 terminal->redeem_scroll_bar_hook
= ns_redeem_scroll_bar
;
4064 terminal->judge_scroll_bars_hook
= ns_judge_scroll_bars
;
4066 terminal->delete_frame_hook
= x_destroy_window
;
4067 terminal->delete_terminal_hook
= ns_delete_terminal
;
4069 terminal->scroll_region_ok
= 1;
4070 terminal->char_ins_del_ok
= 1;
4071 terminal->line_ins_del_ok
= 1;
4072 terminal->fast_clear_end_of_line
= 1;
4073 terminal->memory_below_frame
= 0;
4079 struct ns_display_info
*
4080 ns_term_init
(Lisp_Object display_name
)
4081 /* --------------------------------------------------------------------------
4082 Start the Application and get things rolling.
4083 -------------------------------------------------------------------------- */
4085 struct terminal *terminal;
4086 struct ns_display_info
*dpyinfo
;
4087 static int ns_initialized
= 0;
4090 if (ns_initialized
) return x_display_list
;
4093 NSTRACE
(ns_term_init
);
4095 [outerpool release
];
4096 outerpool
= [[NSAutoreleasePool alloc
] init
];
4098 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
4099 /*GSDebugAllocationActive (YES); */
4103 Fset_input_interrupt_mode
(Qnil
);
4105 if (selfds
[0] == -1)
4107 if (emacs_pipe
(selfds
) != 0)
4109 fprintf (stderr
, "Failed to create pipe
: %s\n",
4110 emacs_strerror
(errno
));
4114 fcntl
(selfds
[0], F_SETFL
, O_NONBLOCK|fcntl
(selfds
[0], F_GETFL
));
4115 FD_ZERO
(&select_readfds
);
4116 FD_ZERO
(&select_writefds
);
4117 pthread_mutex_init
(&select_mutex
, NULL);
4120 ns_pending_files
= [[NSMutableArray alloc
] init
];
4121 ns_pending_service_names
= [[NSMutableArray alloc
] init
];
4122 ns_pending_service_args
= [[NSMutableArray alloc
] init
];
4124 /* Start app and create the main menu, window, view.
4125 Needs to be here because ns_initialize_display_info () uses AppKit classes.
4126 The view will then ask the NSApp to stop and return to Emacs. */
4127 [EmacsApp sharedApplication
];
4130 [NSApp setDelegate
: NSApp
];
4132 /* Start the select thread. */
4133 [NSThread detachNewThreadSelector
:@selector
(fd_handler
:)
4137 /* debugging: log all notifications */
4138 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
4139 selector: @selector (logNotification:)
4140 name: nil object: nil]; */
4142 dpyinfo
= xzalloc
(sizeof
*dpyinfo
);
4144 ns_initialize_display_info
(dpyinfo
);
4145 terminal = ns_create_terminal
(dpyinfo
);
4147 terminal->kboard
= xmalloc
(sizeof
*terminal->kboard
);
4148 init_kboard
(terminal->kboard
);
4149 kset_window_system
(terminal->kboard
, Qns
);
4150 terminal->kboard
->next_kboard
= all_kboards
;
4151 all_kboards
= terminal->kboard
;
4152 /* Don't let the initial kboard remain current longer than necessary.
4153 That would cause problems if a file loaded on startup tries to
4154 prompt in the mini-buffer. */
4155 if (current_kboard
== initial_kboard
)
4156 current_kboard
= terminal->kboard
;
4157 terminal->kboard
->reference_count
++;
4159 dpyinfo
->next
= x_display_list
;
4160 x_display_list
= dpyinfo
;
4162 /* Put it on ns_display_name_list */
4163 ns_display_name_list
= Fcons
(Fcons
(display_name
, Qnil
),
4164 ns_display_name_list
);
4165 dpyinfo
->name_list_element
= XCAR
(ns_display_name_list
);
4167 terminal->name
= xstrdup
(SSDATA
(display_name
));
4171 if (!inhibit_x_resources
)
4173 ns_default
("GSFontAntiAlias"
, &ns_antialias_text
,
4176 /* this is a standard variable */
4177 ns_default
("AppleAntiAliasingThreshold"
, &tmp
,
4178 make_float
(10.0), make_float
(6.0), YES
, NO
);
4179 ns_antialias_threshold
= NILP
(tmp
) ?
10.0 : XFLOATINT
(tmp
);
4182 ns_selection_color
= [[NSUserDefaults standardUserDefaults
]
4183 stringForKey
: @"AppleHighlightColor"
];
4184 if (ns_selection_color
== nil
)
4185 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
4188 NSColorList
*cl
= [NSColorList colorListNamed
: @"Emacs"
];
4192 Lisp_Object color_file
, color_map
, color
;
4196 color_file
= Fexpand_file_name
(build_string
("rgb.txt"
),
4197 Fsymbol_value
(intern
("data
-directory"
)));
4199 color_map
= Fx_load_color_file
(color_file
);
4200 if (NILP
(color_map
))
4201 fatal
("Could not read
%s.\n", SDATA (color_file));
4203 cl
= [[NSColorList alloc
] initWithName
: @"Emacs"
];
4204 for ( ; CONSP
(color_map
); color_map
= XCDR
(color_map
))
4206 color
= XCAR
(color_map
);
4207 name
= SSDATA
(XCAR
(color
));
4208 c
= XINT
(XCDR
(color
));
4210 [NSColor colorWithCalibratedRed
: RED_FROM_ULONG
(c
) / 255.0
4211 green
: GREEN_FROM_ULONG
(c
) / 255.0
4212 blue
: BLUE_FROM_ULONG
(c
) / 255.0
4214 forKey
: [NSString stringWithUTF8String
: name
]];
4216 [cl writeToFile
: nil
];
4221 #ifdef NS_IMPL_GNUSTEP
4222 Vwindow_system_version
= build_string
(gnustep_base_version
);
4224 /*PSnextrelease (128, c); */
4225 char c
[DBL_BUFSIZE_BOUND
];
4226 int len
= dtoastr
(c
, sizeof c
, 0, 0, NSAppKitVersionNumber
);
4227 Vwindow_system_version
= make_unibyte_string
(c
, len
);
4231 delete_keyboard_wait_descriptor
(0);
4233 ns_app_name
= [[NSProcessInfo processInfo
] processName
];
4235 /* Set up OS X app menu */
4236 #ifdef NS_IMPL_COCOA
4240 /* set up the application menu */
4241 svcsMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Services"
];
4242 [svcsMenu setAutoenablesItems
: NO
];
4243 appMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Emacs"
];
4244 [appMenu setAutoenablesItems
: NO
];
4245 mainMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4246 dockMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4248 [appMenu insertItemWithTitle
: @"About Emacs"
4249 action
: @selector
(orderFrontStandardAboutPanel
:)
4252 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 1];
4253 [appMenu insertItemWithTitle
: @"Preferences..."
4254 action
: @selector
(showPreferencesWindow
:)
4257 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 3];
4258 item
= [appMenu insertItemWithTitle
: @"Services"
4259 action
: @selector
(menuDown
:)
4262 [appMenu setSubmenu
: svcsMenu forItem
: item
];
4263 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 5];
4264 [appMenu insertItemWithTitle
: @"Hide Emacs"
4265 action
: @selector
(hide
:)
4268 item
= [appMenu insertItemWithTitle
: @"Hide Others"
4269 action
: @selector
(hideOtherApplications
:)
4272 [item setKeyEquivalentModifierMask
: NSCommandKeyMask | NSAlternateKeyMask
];
4273 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 8];
4274 [appMenu insertItemWithTitle
: @"
Quit Emacs"
4275 action
: @selector
(terminate
:)
4279 item
= [mainMenu insertItemWithTitle
: ns_app_name
4280 action
: @selector
(menuDown
:)
4283 [mainMenu setSubmenu
: appMenu forItem
: item
];
4284 [dockMenu insertItemWithTitle
: @"New Frame"
4285 action
: @selector
(newFrame
:)
4289 [NSApp setMainMenu
: mainMenu
];
4290 [NSApp setAppleMenu
: appMenu
];
4291 [NSApp setServicesMenu
: svcsMenu
];
4292 /* Needed at least on Cocoa, to get dock menu to show windows */
4293 [NSApp setWindowsMenu
: [[NSMenu alloc
] init
]];
4295 [[NSNotificationCenter defaultCenter
]
4296 addObserver
: mainMenu
4297 selector
: @selector
(trackingNotification
:)
4298 name
: NSMenuDidBeginTrackingNotification object
: mainMenu
];
4299 [[NSNotificationCenter defaultCenter
]
4300 addObserver
: mainMenu
4301 selector
: @selector
(trackingNotification
:)
4302 name
: NSMenuDidEndTrackingNotification object
: mainMenu
];
4304 #endif
/* MAC OS X menu setup */
4306 /* Register our external input/output types, used for determining
4307 applicable services and also drag/drop eligibility. */
4308 ns_send_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
] retain
];
4309 ns_return_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
]
4311 ns_drag_types
= [[NSArray arrayWithObjects
:
4313 NSTabularTextPboardType
,
4314 NSFilenamesPboardType
,
4317 NSFontPboardType
, nil
] retain
];
4319 /* If fullscreen is in init/default-frame-alist, focus isn't set
4320 right for fullscreen windows, so set this. */
4321 [NSApp activateIgnoringOtherApps
:YES
];
4324 ns_do_open_file
= YES
;
4326 #ifdef NS_IMPL_GNUSTEP
4327 /* GNUstep steals SIGCHLD for use in NSTask, but we don't use NSTask.
4328 We must re-catch it so subprocess works. */
4329 catch_child_signal
();
4336 ns_term_shutdown
(int sig
)
4338 [[NSUserDefaults standardUserDefaults
] synchronize
];
4340 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4341 if (STRINGP
(Vauto_save_list_file_name
))
4342 unlink
(SSDATA
(Vauto_save_list_file_name
));
4344 if (sig
== 0 || sig
== SIGTERM
)
4346 [NSApp terminate
: NSApp
];
4348 else // force a stack
trace to happen
4355 /* ==========================================================================
4357 EmacsApp implementation
4359 ========================================================================== */
4362 @implementation EmacsApp
4364 - (void
)logNotification
: (NSNotification
*)notification
4366 const
char *name
= [[notification name
] UTF8String
];
4367 if (!strstr
(name
, "Update"
) && !strstr
(name
, "NSMenu"
)
4368 && !strstr
(name
, "WindowNumber"
))
4369 NSLog
(@"notification
: '%@'"
, [notification name
]);
4373 - (void
)sendEvent
: (NSEvent
*)theEvent
4374 /* --------------------------------------------------------------------------
4375 Called when NSApp is running for each event received. Used to stop
4376 the loop when we choose, since there's no way to just run one iteration.
4377 -------------------------------------------------------------------------- */
4379 int
type = [theEvent
type];
4380 NSWindow
*window
= [theEvent window
];
4382 /* NSTRACE (sendEvent); */
4383 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4385 #ifdef NS_IMPL_GNUSTEP
4386 // Keyboard events aren
't propagated to file dialogs for some reason.
4387 if ([NSApp modalWindow] != nil &&
4388 (type == NSKeyDown || type == NSKeyUp || type == NSFlagsChanged))
4390 [[NSApp modalWindow] sendEvent: theEvent];
4395 if (type == NSApplicationDefined)
4397 switch ([theEvent data2])
4399 #ifdef NS_IMPL_COCOA
4400 case NSAPP_DATA2_RUNASSCRIPT:
4405 case NSAPP_DATA2_RUNFILEDIALOG:
4406 ns_run_file_dialog ();
4412 if (type == NSCursorUpdate && window == nil)
4414 fprintf (stderr, "Dropping external cursor update event.\n");
4418 if (type == NSApplicationDefined)
4420 /* Events posted by ns_send_appdefined interrupt the run loop here.
4421 But, if a modal window is up, an appdefined can still come through,
4422 (e.g., from a makeKeyWindow event) but stopping self also stops the
4423 modal loop. Just defer it until later. */
4424 if ([NSApp modalWindow] == nil)
4426 last_appdefined_event_data = [theEvent data1];
4431 send_appdefined = YES;
4436 #ifdef NS_IMPL_COCOA
4437 /* If no dialog and none of our frames have focus and it is a move, skip it.
4438 It is a mouse move in an auxiliary menu, i.e. on the top right on OSX,
4439 such as Wifi, sound, date or similar.
4440 This prevents "spooky" highlighting in the frame under the menu. */
4441 if (type == NSMouseMoved && [NSApp modalWindow] == nil)
4443 struct ns_display_info *di;
4444 BOOL has_focus = NO;
4445 for (di = x_display_list; ! has_focus && di; di = di->next)
4446 has_focus = di->x_focus_frame != 0;
4452 [super sendEvent: theEvent];
4456 - (void)showPreferencesWindow: (id)sender
4458 struct frame *emacsframe = SELECTED_FRAME ();
4459 NSEvent *theEvent = [NSApp currentEvent];
4463 emacs_event->kind = NS_NONKEY_EVENT;
4464 emacs_event->code = KEY_NS_SHOW_PREFS;
4465 emacs_event->modifiers = 0;
4466 EV_TRAILER (theEvent);
4470 - (void)newFrame: (id)sender
4472 struct frame *emacsframe = SELECTED_FRAME ();
4473 NSEvent *theEvent = [NSApp currentEvent];
4477 emacs_event->kind = NS_NONKEY_EVENT;
4478 emacs_event->code = KEY_NS_NEW_FRAME;
4479 emacs_event->modifiers = 0;
4480 EV_TRAILER (theEvent);
4484 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4485 - (BOOL) openFile: (NSString *)fileName
4487 struct frame *emacsframe = SELECTED_FRAME ();
4488 NSEvent *theEvent = [NSApp currentEvent];
4493 emacs_event->kind = NS_NONKEY_EVENT;
4494 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
4495 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
4496 ns_input_line = Qnil; /* can be start or cons start,end */
4497 emacs_event->modifiers =0;
4498 EV_TRAILER (theEvent);
4504 /* **************************************************************************
4506 EmacsApp delegate implementation
4508 ************************************************************************** */
4510 - (void)applicationDidFinishLaunching: (NSNotification *)notification
4511 /* --------------------------------------------------------------------------
4512 When application is loaded, terminate event loop in ns_term_init
4513 -------------------------------------------------------------------------- */
4515 NSTRACE (applicationDidFinishLaunching);
4516 [NSApp setServicesProvider: NSApp];
4517 ns_send_appdefined (-2);
4521 /* Termination sequences:
4524 MenuBar | File | Exit:
4525 Select Quit from App menubar:
4527 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4530 Select Quit from Dock menu:
4533 Cancel -> Nothing else
4537 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4542 - (void) terminate: (id)sender
4544 struct frame *emacsframe = SELECTED_FRAME ();
4549 emacs_event->kind = NS_NONKEY_EVENT;
4550 emacs_event->code = KEY_NS_POWER_OFF;
4551 emacs_event->arg = Qt; /* mark as non-key event */
4552 EV_TRAILER ((id)nil);
4556 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
4560 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
4561 return NSTerminateNow;
4563 ret = NSRunAlertPanel(ns_app_name,
4564 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
4565 @"Save Buffers and Exit", @"Cancel", nil);
4567 if (ret == NSAlertDefaultReturn)
4568 return NSTerminateNow;
4569 else if (ret == NSAlertAlternateReturn)
4570 return NSTerminateCancel;
4571 return NSTerminateNow; /* just in case */
4575 not_in_argv (NSString *arg)
4578 const char *a = [arg UTF8String];
4579 for (k = 1; k < initial_argc; ++k)
4580 if (strcmp (a, initial_argv[k]) == 0) return 0;
4584 /* Notification from the Workspace to open a file */
4585 - (BOOL)application: sender openFile: (NSString *)file
4587 if (ns_do_open_file || not_in_argv (file))
4588 [ns_pending_files addObject: file];
4593 /* Open a file as a temporary file */
4594 - (BOOL)application: sender openTempFile: (NSString *)file
4596 if (ns_do_open_file || not_in_argv (file))
4597 [ns_pending_files addObject: file];
4602 /* Notification from the Workspace to open a file noninteractively (?) */
4603 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
4605 if (ns_do_open_file || not_in_argv (file))
4606 [ns_pending_files addObject: file];
4610 /* Notification from the Workspace to open multiple files */
4611 - (void)application: sender openFiles: (NSArray *)fileList
4613 NSEnumerator *files = [fileList objectEnumerator];
4615 /* Don't open files from the command
line unconditionally
,
4616 Cocoa parses the command
line wrong
, --option value tries to open value
4617 if --option is the last option. */
4618 while ((file
= [files nextObject
]) != nil
)
4619 if (ns_do_open_file || not_in_argv
(file
))
4620 [ns_pending_files addObject
: file
];
4622 [self replyToOpenOrPrint
: NSApplicationDelegateReplySuccess
];
4627 /* Handle dock menu requests. */
4628 - (NSMenu
*)applicationDockMenu
: (NSApplication
*) sender
4634 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4635 - (void
)applicationWillBecomeActive
: (NSNotification
*)notification
4637 //ns_app_active
=YES
;
4639 - (void
)applicationDidBecomeActive
: (NSNotification
*)notification
4641 NSTRACE
(applicationDidBecomeActive
);
4643 //ns_app_active
=YES
;
4645 ns_update_auto_hide_menu_bar
();
4646 // No constraining takes place when the application is not active.
4647 ns_constrain_all_frames
();
4649 - (void
)applicationDidResignActive
: (NSNotification
*)notification
4652 ns_send_appdefined
(-1);
4657 /* ==========================================================================
4659 EmacsApp aux handlers for managing event loop
4661 ========================================================================== */
4664 - (void
)timeout_handler
: (NSTimer
*)timedEntry
4665 /* --------------------------------------------------------------------------
4666 The timeout specified to ns_select has passed.
4667 -------------------------------------------------------------------------- */
4669 /*NSTRACE (timeout_handler); */
4670 ns_send_appdefined
(-2);
4673 #ifdef NS_IMPL_GNUSTEP
4674 - (void
)sendFromMainThread
:(id
)unused
4676 ns_send_appdefined
(nextappdefined
);
4680 - (void
)fd_handler
:(id
)unused
4681 /* --------------------------------------------------------------------------
4682 Check data waiting on file descriptors and terminate if so
4683 -------------------------------------------------------------------------- */
4686 int waiting
= 1, nfds
;
4689 SELECT_TYPE readfds
, writefds
, *wfds
;
4690 struct timespec timeout
, *tmo
;
4691 NSAutoreleasePool
*pool
= nil
;
4693 /* NSTRACE (fd_handler); */
4698 pool
= [[NSAutoreleasePool alloc
] init
];
4704 FD_SET
(selfds
[0], &fds
);
4705 result
= select
(selfds
[0]+1, &fds
, NULL, NULL, NULL);
4706 if (result
> 0 && read
(selfds
[0], &c
, 1) == 1 && c
== 'g')
4711 pthread_mutex_lock
(&select_mutex
);
4714 if (select_valid
& SELECT_HAVE_READ
)
4715 readfds
= select_readfds
;
4719 if (select_valid
& SELECT_HAVE_WRITE
)
4721 writefds
= select_writefds
;
4726 if (select_valid
& SELECT_HAVE_TMO
)
4728 timeout
= select_timeout
;
4734 pthread_mutex_unlock
(&select_mutex
);
4736 FD_SET
(selfds
[0], &readfds
);
4737 if (selfds
[0] >= nfds
) nfds
= selfds
[0]+1;
4739 result
= pselect
(nfds
, &readfds
, wfds
, NULL, tmo
, NULL);
4742 ns_send_appdefined
(-2);
4743 else if (result
> 0)
4745 if (FD_ISSET
(selfds
[0], &readfds
))
4747 if (read
(selfds
[0], &c
, 1) == 1 && c
== 's')
4752 pthread_mutex_lock
(&select_mutex
);
4753 if (select_valid
& SELECT_HAVE_READ
)
4754 select_readfds
= readfds
;
4755 if (select_valid
& SELECT_HAVE_WRITE
)
4756 select_writefds
= writefds
;
4757 if (select_valid
& SELECT_HAVE_TMO
)
4758 select_timeout
= timeout
;
4759 pthread_mutex_unlock
(&select_mutex
);
4761 ns_send_appdefined
(result
);
4771 /* ==========================================================================
4775 ========================================================================== */
4777 /* called from system: queue for next pass through event loop */
4778 - (void
)requestService
: (NSPasteboard
*)pboard
4779 userData
: (NSString
*)userData
4780 error: (NSString
**)error
4782 [ns_pending_service_names addObject
: userData
];
4783 [ns_pending_service_args addObject
: [NSString stringWithUTF8String
:
4784 SSDATA
(ns_string_from_pasteboard
(pboard
))]];
4788 /* called from ns_read_socket to clear queue */
4789 - (BOOL
)fulfillService
: (NSString
*)name withArg
: (NSString
*)arg
4791 struct frame
*emacsframe
= SELECTED_FRAME
();
4792 NSEvent
*theEvent
= [NSApp currentEvent
];
4797 emacs_event
->kind
= NS_NONKEY_EVENT
;
4798 emacs_event
->code
= KEY_NS_SPI_SERVICE_CALL
;
4799 ns_input_spi_name
= build_string
([name UTF8String
]);
4800 ns_input_spi_arg
= build_string
([arg UTF8String
]);
4801 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
4802 EV_TRAILER
(theEvent
);
4812 /* ==========================================================================
4814 EmacsView implementation
4816 ========================================================================== */
4819 @implementation EmacsView
4821 /* needed to inform when window closed from LISP */
4822 - (void
) setWindowClosing
: (BOOL
)closing
4824 windowClosing
= closing
;
4830 NSTRACE
(EmacsView_dealloc
);
4832 if (fs_state
== FULLSCREEN_BOTH
)
4833 [nonfs_window release
];
4838 /* called on font panel selection */
4839 - (void
)changeFont
: (id
)sender
4841 NSEvent
*e
=[[self window
] currentEvent
];
4842 struct face
*face
=FRAME_DEFAULT_FACE
(emacsframe
);
4846 NSTRACE
(changeFont
);
4850 if ((newFont
= [sender convertFont
:
4851 ((struct nsfont_info
*)face
->font
)->nsfont
]))
4853 SET_FRAME_GARBAGED
(emacsframe
); /* now needed as of 2008/10 */
4855 emacs_event
->kind
= NS_NONKEY_EVENT
;
4856 emacs_event
->modifiers
= 0;
4857 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
4859 size = [newFont pointSize
];
4860 ns_input_fontsize
= make_number
(lrint
(size));
4861 ns_input_font
= build_string
([[newFont familyName
] UTF8String
]);
4867 - (BOOL
)acceptsFirstResponder
4869 NSTRACE
(acceptsFirstResponder
);
4874 - (void
)resetCursorRects
4876 NSRect visible
= [self visibleRect
];
4877 NSCursor
*currentCursor
= FRAME_POINTER_TYPE
(emacsframe
);
4878 NSTRACE
(resetCursorRects
);
4880 if (currentCursor
== nil
)
4881 currentCursor
= [NSCursor arrowCursor
];
4883 if (!NSIsEmptyRect
(visible
))
4884 [self addCursorRect
: visible cursor
: currentCursor
];
4885 [currentCursor setOnMouseEntered
: YES
];
4890 /*****************************************************************************/
4891 /* Keyboard handling. */
4894 - (void
)keyDown
: (NSEvent
*)theEvent
4896 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
4898 unsigned fnKeysym
= 0;
4899 static NSMutableArray
*nsEvArray
;
4900 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
4901 static BOOL firstTime
= YES
;
4904 unsigned int flags
= [theEvent modifierFlags
];
4908 /* Rhapsody and OS X give up and down events for the arrow keys */
4909 if (ns_fake_keydown
== YES
)
4910 ns_fake_keydown
= NO
;
4911 else if ([theEvent
type] != NSKeyDown
)
4917 if (![[self window
] isKeyWindow
]
4918 && [[theEvent window
] isKindOfClass
: [EmacsWindow
class]]
4919 /* we must avoid an infinite loop here. */
4920 && (EmacsView
*)[[theEvent window
] delegate
] != self
)
4922 /* XXX: There is an occasional condition in which, when Emacs display
4923 updates a different frame from the current one, and temporarily
4924 selects it, then processes some interrupt-driven input
4925 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
4926 for some reason that window has its first responder set to the NSView
4927 most recently updated (I guess), which is not the correct one. */
4928 [(EmacsView
*)[[theEvent window
] delegate
] keyDown
: theEvent
];
4932 if (nsEvArray
== nil
)
4933 nsEvArray
= [[NSMutableArray alloc
] initWithCapacity
: 1];
4935 [NSCursor setHiddenUntilMouseMoves
: YES
];
4937 if (hlinfo
->mouse_face_hidden
&& INTEGERP
(Vmouse_highlight
))
4939 clear_mouse_face
(hlinfo
);
4940 hlinfo
->mouse_face_hidden
= 1;
4943 if (!processingCompose
)
4945 /* When using screen sharing, no left or right information is sent,
4946 so use Left key in those cases. */
4947 int is_left_key
, is_right_key
;
4949 code
= ([[theEvent charactersIgnoringModifiers
] length] == 0) ?
4950 0 : [[theEvent charactersIgnoringModifiers
] characterAtIndex
: 0];
4952 /* (Carbon way: [theEvent keyCode]) */
4954 /* is it a "function key"? */
4955 fnKeysym
= (code
< 0x00ff && (flags
&NSNumericPadKeyMask
))
4956 ? ns_convert_key
([theEvent keyCode
] | NSNumericPadKeyMask
)
4957 : ns_convert_key
(code
);
4961 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
4962 because Emacs treats Delete and KP-Delete same (in simple.el). */
4963 if ((fnKeysym
== 0xFFFF && [theEvent keyCode
] == 0x33)
4964 #ifdef NS_IMPL_GNUSTEP
4965 /* GNUstep uses incompatible keycodes, even for those that are
4966 supposed to be hardware independent. Just check for delete.
4967 Keypad delete does not have keysym 0xFFFF.
4968 See http://savannah.gnu.org/bugs/?25395
4970 ||
(fnKeysym
== 0xFFFF && code
== 127)
4973 code
= 0xFF08; /* backspace */
4978 /* are there modifiers? */
4979 emacs_event
->modifiers
= 0;
4981 if (flags
& NSHelpKeyMask
)
4982 emacs_event
->modifiers |
= hyper_modifier
;
4984 if (flags
& NSShiftKeyMask
)
4985 emacs_event
->modifiers |
= shift_modifier
;
4987 is_right_key
= (flags
& NSRightCommandKeyMask
) == NSRightCommandKeyMask
;
4988 is_left_key
= (flags
& NSLeftCommandKeyMask
) == NSLeftCommandKeyMask
4989 ||
(! is_right_key
&& (flags
& NSCommandKeyMask
) == NSCommandKeyMask
);
4992 emacs_event
->modifiers |
= parse_solitary_modifier
4993 (EQ
(ns_right_command_modifier
, Qleft
)
4994 ? ns_command_modifier
4995 : ns_right_command_modifier
);
4999 emacs_event
->modifiers |
= parse_solitary_modifier
5000 (ns_command_modifier
);
5002 /* if super (default), take input manager's word so things like
5003 dvorak / qwerty layout work */
5004 if (EQ
(ns_command_modifier
, Qsuper
)
5006 && [[theEvent characters
] length] != 0)
5008 /* XXX: the code we get will be unshifted, so if we have
5009 a shift modifier, must convert ourselves */
5010 if (!(flags
& NSShiftKeyMask
))
5011 code
= [[theEvent characters
] characterAtIndex
: 0];
5013 /* this is ugly and also requires linking w/Carbon framework
5014 (for LMGetKbdType) so for now leave this rare (?) case
5015 undealt with.. in future look into CGEvent methods */
5018 long smv
= GetScriptManagerVariable
(smKeyScript
);
5019 Handle uchrHandle
= GetResource
5020 ('uchr', GetScriptVariable
(smv
, smScriptKeys
));
5022 UCKeyTranslate
((UCKeyboardLayout
*)*uchrHandle
,
5023 [[theEvent characters
] characterAtIndex
: 0],
5024 kUCKeyActionDisplay
,
5025 (flags
& ~NSCommandKeyMask
) >> 8,
5026 LMGetKbdType
(), kUCKeyTranslateNoDeadKeysMask
,
5027 &dummy
, 1, &dummy
, &code
);
5034 is_right_key
= (flags
& NSRightControlKeyMask
) == NSRightControlKeyMask
;
5035 is_left_key
= (flags
& NSLeftControlKeyMask
) == NSLeftControlKeyMask
5036 ||
(! is_right_key
&& (flags
& NSControlKeyMask
) == NSControlKeyMask
);
5039 emacs_event
->modifiers |
= parse_solitary_modifier
5040 (EQ
(ns_right_control_modifier
, Qleft
)
5041 ? ns_control_modifier
5042 : ns_right_control_modifier
);
5045 emacs_event
->modifiers |
= parse_solitary_modifier
5046 (ns_control_modifier
);
5048 if (flags
& NS_FUNCTION_KEY_MASK
&& !fnKeysym
)
5049 emacs_event
->modifiers |
=
5050 parse_solitary_modifier
(ns_function_modifier
);
5052 left_is_none
= NILP
(ns_alternate_modifier
)
5053 || EQ
(ns_alternate_modifier
, Qnone
);
5055 is_right_key
= (flags
& NSRightAlternateKeyMask
)
5056 == NSRightAlternateKeyMask
;
5057 is_left_key
= (flags
& NSLeftAlternateKeyMask
) == NSLeftAlternateKeyMask
5059 && (flags
& NSAlternateKeyMask
) == NSAlternateKeyMask
);
5063 if ((NILP
(ns_right_alternate_modifier
)
5064 || EQ
(ns_right_alternate_modifier
, Qnone
)
5065 ||
(EQ
(ns_right_alternate_modifier
, Qleft
) && left_is_none
))
5067 { /* accept pre-interp alt comb */
5068 if ([[theEvent characters
] length] > 0)
5069 code
= [[theEvent characters
] characterAtIndex
: 0];
5070 /*HACK: clear lone shift modifier to stop next if from firing */
5071 if (emacs_event
->modifiers
== shift_modifier
)
5072 emacs_event
->modifiers
= 0;
5075 emacs_event
->modifiers |
= parse_solitary_modifier
5076 (EQ
(ns_right_alternate_modifier
, Qleft
)
5077 ? ns_alternate_modifier
5078 : ns_right_alternate_modifier
);
5081 if (is_left_key
) /* default = meta */
5083 if (left_is_none
&& !fnKeysym
)
5084 { /* accept pre-interp alt comb */
5085 if ([[theEvent characters
] length] > 0)
5086 code
= [[theEvent characters
] characterAtIndex
: 0];
5087 /*HACK: clear lone shift modifier to stop next if from firing */
5088 if (emacs_event
->modifiers
== shift_modifier
)
5089 emacs_event
->modifiers
= 0;
5092 emacs_event
->modifiers |
=
5093 parse_solitary_modifier
(ns_alternate_modifier
);
5097 fprintf (stderr
, "keyDown
: code
=%x\tfnKey =%x\tflags = %x\tmods = %x\n",
5098 code
, fnKeysym
, flags
, emacs_event
->modifiers
);
5100 /* if it was a function key or had modifiers, pass it directly to emacs */
5101 if (fnKeysym ||
(emacs_event
->modifiers
5102 && (emacs_event
->modifiers
!= shift_modifier
)
5103 && [[theEvent charactersIgnoringModifiers
] length] > 0))
5104 /*[[theEvent characters] length] */
5106 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5108 code |
= (1<<28)|
(3<<16);
5109 else if (code
== 0x7f)
5110 code |
= (1<<28)|
(3<<16);
5112 emacs_event
->kind
= code
> 0xFF
5113 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5115 emacs_event
->code
= code
;
5116 EV_TRAILER
(theEvent
);
5117 processingCompose
= NO
;
5123 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
5124 /* if we get here we should send the key for input manager processing */
5125 /* Disable warning, there is nothing a user can do about it anyway, and
5126 it does not seem to matter. */
5128 if (firstTime
&& [[NSInputManager currentInputManager
]
5129 wantsToDelayTextChangeNotifications
] == NO
)
5131 "Emacs
: WARNING
: TextInput mgr wants marked
text to be permanent
!\n"
);
5135 if (NS_KEYLOG
&& !processingCompose
)
5136 fprintf (stderr
, "keyDown
: Begin compose sequence.
\n"
);
5138 processingCompose
= YES
;
5139 [nsEvArray addObject
: theEvent
];
5140 [self interpretKeyEvents
: nsEvArray
];
5141 [nsEvArray removeObject
: theEvent
];
5145 #ifdef NS_IMPL_COCOA
5146 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
5147 decided not to send key-down for.
5148 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
5149 This only applies on Tiger and earlier.
5150 If it matches one of these, send it on to keyDown. */
5151 -(void
)keyUp
: (NSEvent
*)theEvent
5153 int flags
= [theEvent modifierFlags
];
5154 int code
= [theEvent keyCode
];
5155 if (floor (NSAppKitVersionNumber
) <= 824 /*NSAppKitVersionNumber10_4*/ &&
5156 code
== 0x30 && (flags
& NSControlKeyMask
) && !(flags
& NSCommandKeyMask
))
5159 fprintf (stderr
, "keyUp
: passed test"
);
5160 ns_fake_keydown
= YES
;
5161 [self keyDown
: theEvent
];
5167 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
5170 /* <NSTextInput>: called when done composing;
5171 NOTE: also called when we delete over working text, followed immed.
5172 by doCommandBySelector: deleteBackward: */
5173 - (void
)insertText
: (id
)aString
5176 int len
= [(NSString
*)aString
length];
5180 NSLog
(@"insertText
'%@'\tlen
= %d", aString, len);
5181 processingCompose
= NO
;
5186 /* first, clear any working text */
5187 if (workingText
!= nil
)
5188 [self deleteWorkingText
];
5190 /* now insert the string as keystrokes */
5191 for (i
=0; i
<len
; i
++)
5193 code
= [aString characterAtIndex
: i
];
5194 /* TODO: still need this? */
5196 code
= '~'; /* 0x7E */
5197 if (code
!= 32) /* Space */
5198 emacs_event
->modifiers
= 0;
5200 = code
> 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5201 emacs_event
->code
= code
;
5202 EV_TRAILER
((id
)nil
);
5207 /* <NSTextInput>: inserts display of composing characters */
5208 - (void
)setMarkedText
: (id
)aString selectedRange
: (NSRange
)selRange
5210 NSString
*str
= [aString respondsToSelector
: @selector
(string
)] ?
5211 [aString string
] : aString
;
5213 NSLog
(@"setMarkedText
'%@' len
=%d range %d from %d", str, [str length],
5214 selRange.
length, selRange.location
);
5216 if (workingText
!= nil
)
5217 [self deleteWorkingText
];
5218 if ([str
length] == 0)
5224 processingCompose
= YES
;
5225 workingText
= [str copy
];
5226 ns_working_text
= build_string
([workingText UTF8String
]);
5228 emacs_event
->kind
= NS_TEXT_EVENT
;
5229 emacs_event
->code
= KEY_NS_PUT_WORKING_TEXT
;
5230 EV_TRAILER
((id
)nil
);
5234 /* delete display of composing characters [not in <NSTextInput>] */
5235 - (void
)deleteWorkingText
5237 if (workingText
== nil
)
5240 NSLog
(@"deleteWorkingText len
=%d\n", [workingText length]);
5241 [workingText release
];
5243 processingCompose
= NO
;
5248 emacs_event
->kind
= NS_TEXT_EVENT
;
5249 emacs_event
->code
= KEY_NS_UNPUT_WORKING_TEXT
;
5250 EV_TRAILER
((id
)nil
);
5254 - (BOOL
)hasMarkedText
5256 return workingText
!= nil
;
5260 - (NSRange
)markedRange
5262 NSRange rng
= workingText
!= nil
5263 ? NSMakeRange
(0, [workingText
length]) : NSMakeRange
(NSNotFound
, 0);
5265 NSLog
(@"markedRange request"
);
5273 NSLog
(@"unmark
(accept
) text"
);
5274 [self deleteWorkingText
];
5275 processingCompose
= NO
;
5279 /* used to position char selection windows, etc. */
5280 - (NSRect
)firstRectForCharacterRange
: (NSRange
)theRange
5284 struct window
*win
= XWINDOW
(FRAME_SELECTED_WINDOW
(emacsframe
));
5286 NSLog
(@"firstRectForCharRange request"
);
5288 rect.
size.width
= theRange.
length * FRAME_COLUMN_WIDTH
(emacsframe
);
5289 rect.
size.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5290 pt.x
= WINDOW_TEXT_TO_FRAME_PIXEL_X
(win
, win
->phys_cursor.x
);
5291 pt.y
= WINDOW_TO_FRAME_PIXEL_Y
(win
, win
->phys_cursor.y
5292 +FRAME_LINE_HEIGHT
(emacsframe
));
5294 pt
= [self convertPoint
: pt toView
: nil
];
5295 pt
= [[self window
] convertBaseToScreen
: pt
];
5301 - (NSInteger
)conversationIdentifier
5303 return (NSInteger
)self
;
5307 - (void
)doCommandBySelector
: (SEL
)aSelector
5310 NSLog
(@"doCommandBySelector
: %@", NSStringFromSelector (aSelector));
5312 processingCompose
= NO
;
5313 if (aSelector
== @selector
(deleteBackward
:))
5315 /* happens when user backspaces over an ongoing composition:
5316 throw a 'delete' into the event queue */
5319 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5320 emacs_event
->code
= 0xFF08;
5321 EV_TRAILER
((id
)nil
);
5325 - (NSArray
*)validAttributesForMarkedText
5327 static NSArray
*arr
= nil
;
5328 if (arr
== nil
) arr
= [NSArray new
];
5329 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
5333 - (NSRange
)selectedRange
5336 NSLog
(@"selectedRange request"
);
5337 return NSMakeRange
(NSNotFound
, 0);
5340 #
if defined
(NS_IMPL_COCOA
) || GNUSTEP_GUI_MAJOR_VERSION
> 0 || \
5341 GNUSTEP_GUI_MINOR_VERSION
> 22
5342 - (NSUInteger
)characterIndexForPoint
: (NSPoint
)thePoint
5344 - (unsigned int
)characterIndexForPoint
: (NSPoint
)thePoint
5348 NSLog
(@"characterIndexForPoint request"
);
5352 - (NSAttributedString
*)attributedSubstringFromRange
: (NSRange
)theRange
5354 static NSAttributedString
*str
= nil
;
5355 if (str
== nil
) str
= [NSAttributedString new
];
5357 NSLog
(@"attributedSubstringFromRange request"
);
5361 /* End <NSTextInput> impl. */
5362 /*****************************************************************************/
5365 /* This is what happens when the user presses a mouse button. */
5366 - (void
)mouseDown
: (NSEvent
*)theEvent
5368 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
5370 NSTRACE
(mouseDown
);
5372 [self deleteWorkingText
];
5377 last_mouse_frame
= emacsframe
;
5378 /* appears to be needed to prevent spurious movement events generated on
5380 last_mouse_frame
->mouse_moved
= 0;
5382 if ([theEvent
type] == NSScrollWheel
)
5384 CGFloat delta
= [theEvent deltaY
];
5385 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
5388 emacs_event
->kind
= WHEEL_EVENT
;
5389 emacs_event
->code
= 0;
5390 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
) |
5391 ((delta
> 0) ? up_modifier
: down_modifier
);
5395 emacs_event
->kind
= MOUSE_CLICK_EVENT
;
5396 emacs_event
->code
= EV_BUTTON
(theEvent
);
5397 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
)
5398 | EV_UDMODIFIERS
(theEvent
);
5400 XSETINT
(emacs_event
->x
, lrint
(p.x
));
5401 XSETINT
(emacs_event
->y
, lrint
(p.y
));
5402 EV_TRAILER
(theEvent
);
5406 - (void
)rightMouseDown
: (NSEvent
*)theEvent
5408 NSTRACE
(rightMouseDown
);
5409 [self mouseDown
: theEvent
];
5413 - (void
)otherMouseDown
: (NSEvent
*)theEvent
5415 NSTRACE
(otherMouseDown
);
5416 [self mouseDown
: theEvent
];
5420 - (void
)mouseUp
: (NSEvent
*)theEvent
5423 [self mouseDown
: theEvent
];
5427 - (void
)rightMouseUp
: (NSEvent
*)theEvent
5429 NSTRACE
(rightMouseUp
);
5430 [self mouseDown
: theEvent
];
5434 - (void
)otherMouseUp
: (NSEvent
*)theEvent
5436 NSTRACE
(otherMouseUp
);
5437 [self mouseDown
: theEvent
];
5441 - (void
) scrollWheel
: (NSEvent
*)theEvent
5443 NSTRACE
(scrollWheel
);
5444 [self mouseDown
: theEvent
];
5448 /* Tell emacs the mouse has moved. */
5449 - (void
)mouseMoved
: (NSEvent
*)e
5451 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
5454 // NSTRACE
(mouseMoved
);
5456 last_mouse_movement_time
= EV_TIMESTAMP
(e
);
5457 last_mouse_motion_position
5458 = [self convertPoint
: [e locationInWindow
] fromView
: nil
];
5460 /* update any mouse face */
5461 if (hlinfo
->mouse_face_hidden
)
5463 hlinfo
->mouse_face_hidden
= 0;
5464 clear_mouse_face
(hlinfo
);
5467 /* tooltip handling */
5468 previous_help_echo_string
= help_echo_string
;
5469 help_echo_string
= Qnil
;
5471 if (!note_mouse_movement
(emacsframe
, last_mouse_motion_position.x
,
5472 last_mouse_motion_position.y
))
5473 help_echo_string
= previous_help_echo_string
;
5475 XSETFRAME
(frame
, emacsframe
);
5476 if (!NILP
(help_echo_string
) ||
!NILP
(previous_help_echo_string
))
5478 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
5479 (note_mouse_highlight), which is called through the
5480 note_mouse_movement () call above */
5481 gen_help_event
(help_echo_string
, frame
, help_echo_window
,
5482 help_echo_object
, help_echo_pos
);
5486 help_echo_string
= Qnil
;
5487 gen_help_event
(Qnil
, frame
, Qnil
, Qnil
, 0);
5490 if (emacsframe
->mouse_moved
&& send_appdefined
)
5491 ns_send_appdefined
(-1);
5495 - (void
)mouseDragged
: (NSEvent
*)e
5497 NSTRACE
(mouseDragged
);
5498 [self mouseMoved
: e
];
5502 - (void
)rightMouseDragged
: (NSEvent
*)e
5504 NSTRACE
(rightMouseDragged
);
5505 [self mouseMoved
: e
];
5509 - (void
)otherMouseDragged
: (NSEvent
*)e
5511 NSTRACE
(otherMouseDragged
);
5512 [self mouseMoved
: e
];
5516 - (BOOL
)windowShouldClose
: (id
)sender
5518 NSEvent
*e
=[[self window
] currentEvent
];
5520 NSTRACE
(windowShouldClose
);
5521 windowClosing
= YES
;
5524 emacs_event
->kind
= DELETE_WINDOW_EVENT
;
5525 emacs_event
->modifiers
= 0;
5526 emacs_event
->code
= 0;
5528 /* Don't close this window, let this be done from lisp code. */
5532 - (void
) updateFrameSize
: (BOOL
) delay
;
5534 NSWindow
*window
= [self window
];
5535 NSRect wr
= [window frame
];
5538 #ifdef NS_IMPL_GNUSTEP
5542 int oldc
= cols
, oldr
= rows
;
5543 int oldw
= FRAME_PIXEL_WIDTH
(emacsframe
),
5544 oldh
= FRAME_PIXEL_HEIGHT
(emacsframe
);
5547 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
, wr.
size.width
+ gsextra
);
5549 if (cols
< MINWIDTH
)
5552 if (! [self isFullscreen
])
5554 extra
= FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5555 + FRAME_TOOLBAR_HEIGHT
(emacsframe
) - gsextra
;
5558 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
, wr.
size.height
- extra
);
5560 if (rows
< MINHEIGHT
)
5563 neww
= (int
)wr.
size.width
- emacsframe
->border_width
;
5564 newh
= (int
)wr.
size.height
- extra
;
5566 if (oldr
!= rows || oldc
!= cols || neww
!= oldw || newh
!= oldh
)
5568 NSView
*view = FRAME_NS_VIEW
(emacsframe
);
5569 NSWindow
*win
= [view window
];
5570 NSSize sz
= [win resizeIncrements
];
5572 FRAME_PIXEL_WIDTH
(emacsframe
) = neww
;
5573 FRAME_PIXEL_HEIGHT
(emacsframe
) = newh
;
5574 change_frame_size
(emacsframe
, rows
, cols
, 0, delay
, 0);
5575 SET_FRAME_GARBAGED
(emacsframe
);
5576 cancel_mouse_face
(emacsframe
);
5578 // Did resize increments change because of a font change?
5579 if (sz.width
!= FRAME_COLUMN_WIDTH
(emacsframe
) ||
5580 sz.height
!= FRAME_LINE_HEIGHT
(emacsframe
))
5582 sz.width
= FRAME_COLUMN_WIDTH
(emacsframe
);
5583 sz.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5584 [win setResizeIncrements
: sz
];
5587 [view setFrame
: NSMakeRect
(0, 0, neww
, newh
)];
5588 [self windowDidMove
:nil
]; // Update top
/left.
5592 - (NSSize
)windowWillResize
: (NSWindow
*)sender toSize
: (NSSize
)frameSize
5593 /* normalize frame to gridded text size */
5597 #ifdef NS_IMPL_GNUSTEP
5601 NSTRACE
(windowWillResize
);
5602 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5604 if (fs_state
== FULLSCREEN_MAXIMIZED
5605 && (maximized_width
!= (int
)frameSize.width
5606 || maximized_height
!= (int
)frameSize.height
))
5607 [self setFSValue
: FULLSCREEN_NONE
];
5608 else if (fs_state
== FULLSCREEN_WIDTH
5609 && maximized_width
!= (int
)frameSize.width
)
5610 [self setFSValue
: FULLSCREEN_NONE
];
5611 else if (fs_state
== FULLSCREEN_HEIGHT
5612 && maximized_height
!= (int
)frameSize.height
)
5613 [self setFSValue
: FULLSCREEN_NONE
];
5614 if (fs_state
== FULLSCREEN_NONE
)
5615 maximized_width
= maximized_height
= -1;
5617 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
,
5618 frameSize.width
+ gsextra
);
5619 if (cols
< MINWIDTH
)
5622 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
,
5623 frameSize.height
- extra
);
5624 if (rows
< MINHEIGHT
)
5626 #ifdef NS_IMPL_COCOA
5628 /* this sets window title to have size in it; the wm does this under GS */
5629 NSRect r
= [[self window
] frame
];
5630 if (r.
size.height
== frameSize.height
&& r.
size.width
== frameSize.width
)
5638 else if (fs_state
== FULLSCREEN_NONE
&& ! maximizing_resize
)
5641 NSWindow
*window
= [self window
];
5644 char *t
= strdup
([[[self window
] title] UTF8String
]);
5645 char *pos
= strstr
(t
, " — "
);
5650 size_title
= xmalloc
(strlen
(old_title
) + 40);
5651 esprintf
(size_title
, "
%s — (%d x %d)", old_title, cols, rows);
5652 [window setTitle
: [NSString stringWithUTF8String
: size_title
]];
5657 #endif
/* NS_IMPL_COCOA */
5658 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5664 - (void
)windowDidResize
: (NSNotification
*)notification
5666 if (! [self fsIsNative
])
5668 NSWindow
*theWindow
= [notification object
];
5669 /* We can get notification on the non-FS window when in
5671 if ([self window
] != theWindow
) return;
5674 #ifdef NS_IMPL_GNUSTEP
5675 NSWindow
*theWindow
= [notification object
];
5677 /* In GNUstep, at least currently, it's possible to get a didResize
5678 without getting a willResize.. therefore we need to act as if we got
5679 the willResize now */
5680 NSSize sz
= [theWindow frame
].
size;
5681 sz
= [self windowWillResize
: theWindow toSize
: sz
];
5682 #endif
/* NS_IMPL_GNUSTEP */
5684 NSTRACE
(windowDidResize
);
5685 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5687 if (cols
> 0 && rows
> 0)
5689 [self updateFrameSize
: YES
];
5692 ns_send_appdefined
(-1);
5695 #ifdef NS_IMPL_COCOA
5696 - (void
)viewDidEndLiveResize
5698 [super viewDidEndLiveResize
];
5701 [[self window
] setTitle
: [NSString stringWithUTF8String
: old_title
]];
5705 maximizing_resize
= NO
;
5707 #endif
/* NS_IMPL_COCOA */
5710 - (void
)windowDidBecomeKey
: (NSNotification
*)notification
5711 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5713 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5714 struct frame
*old_focus
= dpyinfo
->x_focus_frame
;
5716 NSTRACE
(windowDidBecomeKey
);
5718 if (emacsframe
!= old_focus
)
5719 dpyinfo
->x_focus_frame
= emacsframe
;
5721 ns_frame_rehighlight
(emacsframe
);
5725 emacs_event
->kind
= FOCUS_IN_EVENT
;
5726 EV_TRAILER
((id
)nil
);
5731 - (void
)windowDidResignKey
: (NSNotification
*)notification
5732 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5734 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5735 BOOL is_focus_frame
= dpyinfo
->x_focus_frame
== emacsframe
;
5736 NSTRACE
(windowDidResignKey
);
5739 dpyinfo
->x_focus_frame
= 0;
5741 ns_frame_rehighlight
(emacsframe
);
5743 /* FIXME: for some reason needed on second and subsequent clicks away
5744 from sole-frame Emacs to get hollow box to show */
5745 if (!windowClosing
&& [[self window
] isVisible
] == YES
)
5747 x_update_cursor
(emacsframe
, 1);
5748 x_set_frame_alpha
(emacsframe
);
5751 if (emacs_event
&& is_focus_frame
)
5753 [self deleteWorkingText
];
5754 emacs_event
->kind
= FOCUS_OUT_EVENT
;
5755 EV_TRAILER
((id
)nil
);
5760 - (void
)windowWillMiniaturize
: sender
5762 NSTRACE
(windowWillMiniaturize
);
5778 - initFrameFromEmacs
: (struct frame
*)f
5787 NSTRACE
(initFrameFromEmacs
);
5790 processingCompose
= NO
;
5791 scrollbarsNeedingUpdate
= 0;
5792 fs_state
= FULLSCREEN_NONE
;
5793 fs_before_fs
= next_maximized
= -1;
5794 #ifdef HAVE_NATIVE_FS
5795 fs_is_native
= ns_use_native_fullscreen
;
5799 maximized_width
= maximized_height
= -1;
5802 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5804 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
5805 r
= NSMakeRect
(0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, f
->text_cols
),
5806 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, f
->text_lines
));
5807 [self initWithFrame
: r
];
5808 [self setAutoresizingMask
: NSViewWidthSizable | NSViewHeightSizable
];
5810 FRAME_NS_VIEW
(f
) = self
;
5812 #ifdef NS_IMPL_COCOA
5814 maximizing_resize
= NO
;
5817 win
= [[EmacsWindow alloc
]
5818 initWithContentRect
: r
5819 styleMask
: (NSResizableWindowMask |
5820 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_7
5821 NSTitledWindowMask |
5823 NSMiniaturizableWindowMask |
5824 NSClosableWindowMask
)
5825 backing
: NSBackingStoreBuffered
5828 #ifdef HAVE_NATIVE_FS
5829 [win setCollectionBehavior
:NSWindowCollectionBehaviorFullScreenPrimary
];
5833 bwidth
= f
->border_width
= wr.
size.width
- r.
size.width
;
5834 tibar_height
= FRAME_NS_TITLEBAR_HEIGHT
(f
) = wr.
size.height
- r.
size.height
;
5836 [win setAcceptsMouseMovedEvents
: YES
];
5837 [win setDelegate
: self
];
5838 [win useOptimizedDrawing
: YES
];
5840 sz.width
= FRAME_COLUMN_WIDTH
(f
);
5841 sz.height
= FRAME_LINE_HEIGHT
(f
);
5842 [win setResizeIncrements
: sz
];
5844 [[win contentView
] addSubview
: self
];
5847 [self registerForDraggedTypes
: ns_drag_types
];
5850 name
= [NSString stringWithUTF8String
:
5851 NILP
(tem
) ? "Emacs"
: SSDATA
(tem
)];
5852 [win setTitle
: name
];
5854 /* toolbar support */
5855 toolbar
= [[EmacsToolbar alloc
] initForView
: self withIdentifier
:
5856 [NSString stringWithFormat
: @"Emacs Frame
%d",
5858 [win setToolbar
: toolbar
];
5859 [toolbar setVisible
: NO
];
5860 #ifdef NS_IMPL_COCOA
5862 NSButton
*toggleButton
;
5863 toggleButton
= [win standardWindowButton
: NSWindowToolbarButton
];
5864 [toggleButton setTarget
: self
];
5865 [toggleButton setAction
: @selector
(toggleToolbar
: )];
5868 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
5872 [win setMiniwindowTitle
:
5873 [NSString stringWithUTF8String
: SSDATA
(tem
)]];
5876 NSScreen
*screen
= [win screen
];
5879 [win setFrameTopLeftPoint
: NSMakePoint
5880 (IN_BOUND
(-SCREENMAX
, f
->left_pos
, SCREENMAX
),
5881 IN_BOUND
(-SCREENMAX
,
5882 [screen frame
].
size.height
- NS_TOP_POS
(f
), SCREENMAX
))];
5885 [win makeFirstResponder
: self
];
5887 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
5888 (FRAME_DEFAULT_FACE
(emacsframe
)), emacsframe
);
5889 [win setBackgroundColor
: col
];
5890 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
5891 [win setOpaque
: NO
];
5893 [self allocateGState
];
5895 [NSApp registerServicesMenuSendTypes
: ns_send_types
5903 - (void
)windowDidMove
: sender
5905 NSWindow
*win
= [self window
];
5906 NSRect r
= [win frame
];
5907 NSArray
*screens
= [NSScreen screens
];
5908 NSScreen
*screen
= [screens objectAtIndex
: 0];
5910 NSTRACE
(windowDidMove
);
5912 if (!emacsframe
->output_data.ns
)
5916 emacsframe
->left_pos
= r.origin.x
;
5917 emacsframe
->top_pos
=
5918 [screen frame
].
size.height
- (r.origin.y
+ r.
size.height
);
5923 /* Called AFTER method below, but before our windowWillResize call there leads
5924 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
5925 location so set_window_size moves the frame. */
5926 - (BOOL
)windowShouldZoom
: (NSWindow
*)sender toFrame
: (NSRect
)newFrame
5928 emacsframe
->output_data.ns
->zooming
= 1;
5933 /* Override to do something slightly nonstandard, but nice. First click on
5934 zoom button will zoom vertically. Second will zoom completely. Third
5935 returns to original. */
5936 - (NSRect
)windowWillUseStandardFrame
:(NSWindow
*)sender
5937 defaultFrame
:(NSRect
)defaultFrame
5939 NSRect result
= [sender frame
];
5941 NSTRACE
(windowWillUseStandardFrame
);
5943 if (fs_before_fs
!= -1) /* Entering fullscreen */
5945 result
= defaultFrame
;
5947 else if (next_maximized
== FULLSCREEN_HEIGHT
5948 ||
(next_maximized
== -1
5949 && abs (defaultFrame.
size.height
- result.
size.height
)
5950 > FRAME_LINE_HEIGHT
(emacsframe
)))
5953 ns_userRect
= result
;
5954 maximized_height
= result.
size.height
= defaultFrame.
size.height
;
5955 maximized_width
= -1;
5956 result.origin.y
= defaultFrame.origin.y
;
5957 [self setFSValue
: FULLSCREEN_HEIGHT
];
5958 #ifdef NS_IMPL_COCOA
5959 maximizing_resize
= YES
;
5962 else if (next_maximized
== FULLSCREEN_WIDTH
)
5964 ns_userRect
= result
;
5965 maximized_width
= result.
size.width
= defaultFrame.
size.width
;
5966 maximized_height
= -1;
5967 result.origin.x
= defaultFrame.origin.x
;
5968 [self setFSValue
: FULLSCREEN_WIDTH
];
5970 else if (next_maximized
== FULLSCREEN_MAXIMIZED
5971 ||
(next_maximized
== -1
5972 && abs (defaultFrame.
size.width
- result.
size.width
)
5973 > FRAME_COLUMN_WIDTH
(emacsframe
)))
5975 result
= defaultFrame
; /* second click */
5976 maximized_width
= result.
size.width
;
5977 maximized_height
= result.
size.height
;
5978 [self setFSValue
: FULLSCREEN_MAXIMIZED
];
5979 #ifdef NS_IMPL_COCOA
5980 maximizing_resize
= YES
;
5986 result
= ns_userRect.
size.height ? ns_userRect
: result
;
5987 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
5988 #ifdef NS_IMPL_COCOA
5989 maximizing_resize
= fs_state
!= FULLSCREEN_NONE
;
5991 [self setFSValue
: FULLSCREEN_NONE
];
5992 maximized_width
= maximized_height
= -1;
5995 if (fs_before_fs
== -1) next_maximized
= -1;
5996 [self windowWillResize
: sender toSize
: result.
size];
6001 - (void
)windowDidDeminiaturize
: sender
6003 NSTRACE
(windowDidDeminiaturize
);
6004 if (!emacsframe
->output_data.ns
)
6007 SET_FRAME_ICONIFIED
(emacsframe
, 0);
6008 SET_FRAME_VISIBLE
(emacsframe
, 1);
6009 windows_or_buffers_changed
++;
6013 emacs_event
->kind
= DEICONIFY_EVENT
;
6014 EV_TRAILER
((id
)nil
);
6019 - (void
)windowDidExpose
: sender
6021 NSTRACE
(windowDidExpose
);
6022 if (!emacsframe
->output_data.ns
)
6025 SET_FRAME_VISIBLE
(emacsframe
, 1);
6026 SET_FRAME_GARBAGED
(emacsframe
);
6028 if (send_appdefined
)
6029 ns_send_appdefined
(-1);
6033 - (void
)windowDidMiniaturize
: sender
6035 NSTRACE
(windowDidMiniaturize
);
6036 if (!emacsframe
->output_data.ns
)
6039 SET_FRAME_ICONIFIED
(emacsframe
, 1);
6040 SET_FRAME_VISIBLE
(emacsframe
, 0);
6044 emacs_event
->kind
= ICONIFY_EVENT
;
6045 EV_TRAILER
((id
)nil
);
6049 #ifdef HAVE_NATIVE_FS
6050 - (NSApplicationPresentationOptions
)window
:(NSWindow
*)window
6051 willUseFullScreenPresentationOptions
:
6052 (NSApplicationPresentationOptions
)proposedOptions
6054 return proposedOptions|NSApplicationPresentationAutoHideToolbar
;
6058 - (void
)windowWillEnterFullScreen
:(NSNotification
*)notification
6060 fs_before_fs
= fs_state
;
6063 - (void
)windowDidEnterFullScreen
:(NSNotification
*)notification
6065 [self setFSValue
: FULLSCREEN_BOTH
];
6066 if (! [self fsIsNative
])
6068 [self windowDidBecomeKey
:notification
];
6069 [nonfs_window orderOut
:self
];
6071 else if (! FRAME_EXTERNAL_TOOL_BAR
(emacsframe
))
6072 [toolbar setVisible
:NO
];
6075 - (void
)windowWillExitFullScreen
:(NSNotification
*)notification
6077 if (next_maximized
!= -1)
6078 fs_before_fs
= next_maximized
;
6081 - (void
)windowDidExitFullScreen
:(NSNotification
*)notification
6083 [self setFSValue
: fs_before_fs
];
6085 #ifdef NS_IMPL_COCOA
6086 [self updateCollectionBehaviour
];
6088 if (FRAME_EXTERNAL_TOOL_BAR
(emacsframe
))
6090 [toolbar setVisible
:YES
];
6091 update_frame_tool_bar
(emacsframe
);
6092 [self updateFrameSize
:YES
];
6093 [[self window
] display
];
6096 [toolbar setVisible
:NO
];
6098 if (next_maximized
!= -1)
6099 [[self window
] performZoom
:self
];
6104 return fs_is_native
;
6107 - (BOOL
)isFullscreen
6109 if (! fs_is_native
) return nonfs_window
!= nil
;
6110 #ifdef HAVE_NATIVE_FS
6111 return ([[self window
] styleMask
] & NSFullScreenWindowMask
) != 0;
6117 #ifdef HAVE_NATIVE_FS
6118 - (void
)updateCollectionBehaviour
6120 if (! [self isFullscreen
])
6122 NSWindow
*win
= [self window
];
6123 NSWindowCollectionBehavior b
= [win collectionBehavior
];
6124 if (ns_use_native_fullscreen
)
6125 b |
= NSWindowCollectionBehaviorFullScreenPrimary
;
6127 b
&= ~NSWindowCollectionBehaviorFullScreenPrimary
;
6129 [win setCollectionBehavior
: b
];
6130 fs_is_native
= ns_use_native_fullscreen
;
6135 - (void
)toggleFullScreen
: (id
)sender
6146 #ifdef NS_IMPL_COCOA
6147 [[self window
] toggleFullScreen
:sender
];
6153 onFirstScreen
= [[w screen
] isEqual:[[NSScreen screens
] objectAtIndex
:0]];
6156 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
6157 (FRAME_DEFAULT_FACE
(f
)),
6160 sz.width
= FRAME_COLUMN_WIDTH
(f
);
6161 sz.height
= FRAME_LINE_HEIGHT
(f
);
6163 if (fs_state
!= FULLSCREEN_BOTH
)
6165 /* Hide dock and menubar if we are on the primary screen. */
6168 #
if defined
(NS_IMPL_COCOA
) && \
6169 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
6170 NSApplicationPresentationOptions options
6171 = NSApplicationPresentationAutoHideDock
6172 | NSApplicationPresentationAutoHideMenuBar
;
6174 [NSApp setPresentationOptions
: options
];
6176 [NSMenu setMenuBarVisible
:NO
];
6180 fw
= [[EmacsFSWindow alloc
]
6181 initWithContentRect
:[w contentRectForFrameRect
:wr
]
6182 styleMask
:NSBorderlessWindowMask
6183 backing
:NSBackingStoreBuffered
6187 [fw setContentView
:[w contentView
]];
6188 [fw setTitle
:[w
title]];
6189 [fw setDelegate
:self
];
6190 [fw setAcceptsMouseMovedEvents
: YES
];
6191 [fw useOptimizedDrawing
: YES
];
6192 [fw setResizeIncrements
: sz
];
6193 [fw setBackgroundColor
: col
];
6194 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
6197 f
->border_width
= 0;
6198 FRAME_NS_TITLEBAR_HEIGHT
(f
) = 0;
6199 tobar_height
= FRAME_TOOLBAR_HEIGHT
(f
);
6200 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
6204 [self windowWillEnterFullScreen
:nil
];
6205 [fw makeKeyAndOrderFront
:NSApp
];
6206 [fw makeFirstResponder
:self
];
6208 r
= [fw frameRectForContentRect
:[[fw screen
] frame
]];
6209 [fw setFrame
: r display
:YES animate
:YES
];
6210 [self windowDidEnterFullScreen
:nil
];
6221 #
if defined
(NS_IMPL_COCOA
) && \
6222 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
6223 [NSApp setPresentationOptions
: NSApplicationPresentationDefault
];
6225 [NSMenu setMenuBarVisible
:YES
];
6229 [w setContentView
:[fw contentView
]];
6230 [w setResizeIncrements
: sz
];
6231 [w setBackgroundColor
: col
];
6232 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
6235 f
->border_width
= bwidth
;
6236 FRAME_NS_TITLEBAR_HEIGHT
(f
) = tibar_height
;
6237 if (FRAME_EXTERNAL_TOOL_BAR
(f
))
6238 FRAME_TOOLBAR_HEIGHT
(f
) = tobar_height
;
6240 [self windowWillExitFullScreen
:nil
];
6241 [fw setFrame
: [w frame
] display
:YES animate
:YES
];
6243 [w makeKeyAndOrderFront
:NSApp
];
6244 [self windowDidExitFullScreen
:nil
];
6245 [self updateFrameSize
:YES
];
6251 if (fs_state
!= emacsframe
->want_fullscreen
)
6253 if (fs_state
== FULLSCREEN_BOTH
)
6255 [self toggleFullScreen
:self
];
6258 switch (emacsframe
->want_fullscreen
)
6260 case FULLSCREEN_BOTH
:
6261 [self toggleFullScreen
:self
];
6263 case FULLSCREEN_WIDTH
:
6264 next_maximized
= FULLSCREEN_WIDTH
;
6265 if (fs_state
!= FULLSCREEN_BOTH
)
6266 [[self window
] performZoom
:self
];
6268 case FULLSCREEN_HEIGHT
:
6269 next_maximized
= FULLSCREEN_HEIGHT
;
6270 if (fs_state
!= FULLSCREEN_BOTH
)
6271 [[self window
] performZoom
:self
];
6273 case FULLSCREEN_MAXIMIZED
:
6274 next_maximized
= FULLSCREEN_MAXIMIZED
;
6275 if (fs_state
!= FULLSCREEN_BOTH
)
6276 [[self window
] performZoom
:self
];
6278 case FULLSCREEN_NONE
:
6279 if (fs_state
!= FULLSCREEN_BOTH
)
6281 next_maximized
= FULLSCREEN_NONE
;
6282 [[self window
] performZoom
:self
];
6287 emacsframe
->want_fullscreen
= FULLSCREEN_NONE
;
6292 - (void
) setFSValue
: (int
)value
6294 Lisp_Object lval
= Qnil
;
6297 case FULLSCREEN_BOTH
:
6300 case FULLSCREEN_WIDTH
:
6303 case FULLSCREEN_HEIGHT
:
6306 case FULLSCREEN_MAXIMIZED
:
6310 store_frame_param
(emacsframe
, Qfullscreen
, lval
);
6314 - (void
)mouseEntered
: (NSEvent
*)theEvent
6316 NSTRACE
(mouseEntered
);
6317 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
6321 - (void
)mouseExited
: (NSEvent
*)theEvent
6323 Mouse_HLInfo
*hlinfo
= emacsframe ? MOUSE_HL_INFO
(emacsframe
) : NULL;
6325 NSTRACE
(mouseExited
);
6330 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
6332 if (emacsframe
== hlinfo
->mouse_face_mouse_frame
)
6334 clear_mouse_face
(hlinfo
);
6335 hlinfo
->mouse_face_mouse_frame
= 0;
6343 if (context_menu_value
== -1)
6344 context_menu_value
= [sender tag
];
6347 NSInteger tag
= [sender tag
];
6348 find_and_call_menu_selection
(emacsframe
, emacsframe
->menu_bar_items_used
,
6349 emacsframe
->menu_bar_vector
,
6353 ns_send_appdefined
(-1);
6358 - (EmacsToolbar
*)toolbar
6364 /* this gets called on toolbar button click */
6365 - toolbarClicked
: (id
)item
6368 int idx
= [item tag
] * TOOL_BAR_ITEM_NSLOTS
;
6370 NSTRACE
(toolbarClicked
);
6375 /* send first event (for some reason two needed) */
6376 theEvent
= [[self window
] currentEvent
];
6377 emacs_event
->kind
= TOOL_BAR_EVENT
;
6378 XSETFRAME
(emacs_event
->arg
, emacsframe
);
6379 EV_TRAILER
(theEvent
);
6381 emacs_event
->kind
= TOOL_BAR_EVENT
;
6382 /* XSETINT (emacs_event->code, 0); */
6383 emacs_event
->arg
= AREF
(emacsframe
->tool_bar_items
,
6384 idx
+ TOOL_BAR_ITEM_KEY
);
6385 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6386 EV_TRAILER
(theEvent
);
6391 - toggleToolbar
: (id
)sender
6396 emacs_event
->kind
= NS_NONKEY_EVENT
;
6397 emacs_event
->code
= KEY_NS_TOGGLE_TOOLBAR
;
6398 EV_TRAILER
((id
)nil
);
6403 - (void
)drawRect
: (NSRect
)rect
6405 int x
= NSMinX
(rect
), y
= NSMinY
(rect
);
6406 int width
= NSWidth
(rect
), height
= NSHeight
(rect
);
6410 if (!emacsframe ||
!emacsframe
->output_data.ns
)
6413 ns_clear_frame_area
(emacsframe
, x
, y
, width
, height
);
6414 expose_frame
(emacsframe
, x
, y
, width
, height
);
6417 drawRect: may be called (at least in OS X 10.5) for invisible
6418 views as well for some reason. Thus, do not infer visibility
6421 emacsframe->async_visible = 1;
6422 emacsframe->async_iconified = 0;
6427 /* NSDraggingDestination protocol methods. Actually this is not really a
6428 protocol, but a category of Object. O well... */
6430 -(NSUInteger
) draggingEntered
: (id
<NSDraggingInfo
>) sender
6432 NSTRACE
(draggingEntered
);
6433 return NSDragOperationGeneric
;
6437 -(BOOL
)prepareForDragOperation
: (id
<NSDraggingInfo
>) sender
6443 -(BOOL
)performDragOperation
: (id
<NSDraggingInfo
>) sender
6448 NSEvent
*theEvent
= [[self window
] currentEvent
];
6451 NSTRACE
(performDragOperation
);
6456 position
= [self convertPoint
: [sender draggingLocation
] fromView
: nil
];
6457 x
= lrint
(position.x
); y
= lrint
(position.y
);
6459 pb
= [sender draggingPasteboard
];
6460 type = [pb availableTypeFromArray
: ns_drag_types
];
6465 else if ([type isEqualToString
: NSFilenamesPboardType
])
6468 NSEnumerator
*fenum
;
6471 if (!(files
= [pb propertyListForType
: type]))
6474 fenum
= [files objectEnumerator
];
6475 while ( (file
= [fenum nextObject
]) )
6477 emacs_event
->kind
= NS_NONKEY_EVENT
;
6478 emacs_event
->code
= KEY_NS_DRAG_FILE
;
6479 XSETINT
(emacs_event
->x
, x
);
6480 XSETINT
(emacs_event
->y
, y
);
6481 ns_input_file
= append2
(ns_input_file
,
6482 build_string
([file UTF8String
]));
6483 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6484 EV_TRAILER
(theEvent
);
6488 else if ([type isEqualToString
: NSURLPboardType
])
6493 if (!(fileURL
= [NSURL URLFromPasteboard
: pb
]) ||
6494 [fileURL isFileURL
] == NO
)
6497 file
= [fileURL
path];
6498 emacs_event
->kind
= NS_NONKEY_EVENT
;
6499 emacs_event
->code
= KEY_NS_DRAG_FILE
;
6500 XSETINT
(emacs_event
->x
, x
);
6501 XSETINT
(emacs_event
->y
, y
);
6502 ns_input_file
= append2
(ns_input_file
, build_string
([file UTF8String
]));
6503 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6504 EV_TRAILER
(theEvent
);
6507 else if ([type isEqualToString
: NSStringPboardType
]
6508 ||
[type isEqualToString
: NSTabularTextPboardType
])
6512 if (! (data
= [pb stringForType
: type]))
6515 emacs_event
->kind
= NS_NONKEY_EVENT
;
6516 emacs_event
->code
= KEY_NS_DRAG_TEXT
;
6517 XSETINT
(emacs_event
->x
, x
);
6518 XSETINT
(emacs_event
->y
, y
);
6519 ns_input_text
= build_string
([data UTF8String
]);
6520 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6521 EV_TRAILER
(theEvent
);
6524 else if ([type isEqualToString
: NSColorPboardType
])
6526 NSColor
*c
= [NSColor colorFromPasteboard
: pb
];
6527 emacs_event
->kind
= NS_NONKEY_EVENT
;
6528 emacs_event
->code
= KEY_NS_DRAG_COLOR
;
6529 XSETINT
(emacs_event
->x
, x
);
6530 XSETINT
(emacs_event
->y
, y
);
6531 ns_input_color
= ns_color_to_lisp
(c
);
6532 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6533 EV_TRAILER
(theEvent
);
6536 else if ([type isEqualToString
: NSFontPboardType
])
6538 /* impl based on GNUstep NSTextView.m */
6539 NSData
*data
= [pb dataForType
: NSFontPboardType
];
6540 NSDictionary
*dict
= [NSUnarchiver unarchiveObjectWithData
: data
];
6541 NSFont
*font
= [dict objectForKey
: NSFontAttributeName
];
6547 emacs_event
->kind
= NS_NONKEY_EVENT
;
6548 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
6549 XSETINT
(emacs_event
->x
, x
);
6550 XSETINT
(emacs_event
->y
, y
);
6551 ns_input_font
= build_string
([[font fontName
] UTF8String
]);
6552 snprintf
(fontSize
, 10, "
%f", [font pointSize]);
6553 ns_input_fontsize
= build_string
(fontSize
);
6554 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6555 EV_TRAILER
(theEvent
);
6560 error ("Invalid data
type in dragging pasteboard."
);
6566 - (id
) validRequestorForSendType
: (NSString
*)typeSent
6567 returnType
: (NSString
*)typeReturned
6569 NSTRACE
(validRequestorForSendType
);
6570 if (typeSent
!= nil
&& [ns_send_types indexOfObject
: typeSent
] != NSNotFound
6571 && typeReturned
== nil
)
6573 if (! NILP
(ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
)))
6577 return [super validRequestorForSendType
: typeSent
6578 returnType
: typeReturned
];
6582 /* The next two methods are part of NSServicesRequests informal protocol,
6583 supposedly called when a services menu item is chosen from this app.
6584 But this should not happen because we override the services menu with our
6585 own entries which call ns-perform-service.
6586 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
6587 So let's at least stub them out until further investigation can be done. */
6589 - (BOOL
) readSelectionFromPasteboard
: (NSPasteboard
*)pb
6591 /* we could call ns_string_from_pasteboard(pboard) here but then it should
6592 be written into the buffer in place of the existing selection..
6593 ordinary service calls go through functions defined in ns-win.el */
6597 - (BOOL
) writeSelectionToPasteboard
: (NSPasteboard
*)pb types
: (NSArray
*)types
6599 NSArray
*typesDeclared
;
6602 /* We only support NSStringPboardType */
6603 if ([types containsObject
:NSStringPboardType
] == NO
) {
6607 val
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6608 if (CONSP
(val
) && SYMBOLP
(XCAR
(val
)))
6611 if (CONSP
(val
) && NILP
(XCDR
(val
)))
6614 if (! STRINGP
(val
))
6617 typesDeclared
= [NSArray arrayWithObject
:NSStringPboardType
];
6618 [pb declareTypes
:typesDeclared owner
:nil
];
6619 ns_string_to_pasteboard
(pb
, val
);
6624 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
6625 (gives a miniaturized version of the window); currently we use the latter for
6626 frames whose active buffer doesn't correspond to any file
6627 (e.g., '*scratch*') */
6628 - setMiniwindowImage
: (BOOL
) setMini
6630 id
image = [[self window
] miniwindowImage
];
6631 NSTRACE
(setMiniwindowImage
);
6633 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
6634 about "AppleDockIconEnabled" notwithstanding, however the set message
6635 below has its effect nonetheless. */
6636 if (image != emacsframe
->output_data.ns
->miniimage
)
6638 if (image && [image isKindOfClass
: [EmacsImage
class]])
6640 [[self window
] setMiniwindowImage
:
6641 setMini ? emacsframe
->output_data.ns
->miniimage
: nil
];
6648 - (void
) setRows
: (int
) r andColumns
: (int
) c
6654 @
end /* EmacsView */
6658 /* ==========================================================================
6660 EmacsWindow implementation
6662 ========================================================================== */
6664 @implementation EmacsWindow
6666 #ifdef NS_IMPL_COCOA
6667 - (id
)accessibilityAttributeValue
:(NSString
*)attribute
6669 Lisp_Object str
= Qnil
;
6670 struct frame
*f
= SELECTED_FRAME
();
6671 struct buffer
*curbuf
= XBUFFER
(XWINDOW
(f
->selected_window
)->contents
);
6673 if ([attribute isEqualToString
:NSAccessibilityRoleAttribute
])
6674 return NSAccessibilityTextFieldRole
;
6676 if ([attribute isEqualToString
:NSAccessibilitySelectedTextAttribute
]
6677 && curbuf
&& ! NILP
(BVAR
(curbuf
, mark_active
)))
6679 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6681 else if (curbuf
&& [attribute isEqualToString
:NSAccessibilityValueAttribute
])
6683 if (! NILP
(BVAR
(curbuf
, mark_active
)))
6684 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6688 ptrdiff_t start_byte
= BUF_BEGV_BYTE
(curbuf
);
6689 ptrdiff_t byte_range
= BUF_ZV_BYTE
(curbuf
) - start_byte
;
6690 ptrdiff_t range
= BUF_ZV
(curbuf
) - BUF_BEGV
(curbuf
);
6692 if (! NILP
(BVAR
(curbuf
, enable_multibyte_characters
)))
6693 str
= make_uninit_multibyte_string
(range
, byte_range
);
6695 str
= make_uninit_string
(range
);
6696 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
6697 Is this a problem? */
6698 memcpy
(SDATA
(str
), BYTE_POS_ADDR
(start_byte
), byte_range
);
6705 if (CONSP
(str
) && SYMBOLP
(XCAR
(str
)))
6708 if (CONSP
(str
) && NILP
(XCDR
(str
)))
6713 const
char *utfStr
= SSDATA
(str
);
6714 NSString
*nsStr
= [NSString stringWithUTF8String
: utfStr
];
6719 return [super accessibilityAttributeValue
:attribute
];
6721 #endif
/* NS_IMPL_COCOA */
6723 /* If we have multiple monitors, one above the other, we don't want to
6724 restrict the height to just one monitor. So we override this. */
6725 - (NSRect
)constrainFrameRect
:(NSRect
)frameRect toScreen
:(NSScreen
*)screen
6727 /* When making the frame visible for the first time or if there is just
6728 one screen, we want to constrain. Other times not. */
6729 NSUInteger nr_screens
= [[NSScreen screens
] count
];
6730 struct frame
*f
= ((EmacsView
*)[self delegate
])->emacsframe
;
6731 NSTRACE
(constrainFrameRect
);
6733 if (nr_screens
== 1)
6735 NSRect r
= [super constrainFrameRect
:frameRect toScreen
:screen
];
6739 if (f
->output_data.ns
->dont_constrain
6740 || ns_menu_bar_should_be_hidden
())
6743 f
->output_data.ns
->dont_constrain
= 1;
6744 return [super constrainFrameRect
:frameRect toScreen
:screen
];
6747 @
end /* EmacsWindow */
6750 @implementation EmacsFSWindow
6752 - (BOOL
)canBecomeKeyWindow
6757 - (BOOL
)canBecomeMainWindow
6764 /* ==========================================================================
6766 EmacsScroller implementation
6768 ========================================================================== */
6771 @implementation EmacsScroller
6773 /* for repeat button push */
6774 #define SCROLL_BAR_FIRST_DELAY
0.5
6775 #define SCROLL_BAR_CONTINUOUS_DELAY
(1.0 / 15)
6777 + (CGFloat
) scrollerWidth
6779 /* TODO: if we want to allow variable widths, this is the place to do it,
6780 however neither GNUstep nor Cocoa support it very well */
6781 return [NSScroller scrollerWidth
];
6785 - initFrame
: (NSRect
)r window
: (Lisp_Object
)nwin
6787 NSTRACE
(EmacsScroller_initFrame
);
6789 r.
size.width
= [EmacsScroller scrollerWidth
];
6790 [super initWithFrame
: r
/*NSMakeRect (0, 0, 0, 0)*/];
6791 [self setContinuous
: YES
];
6792 [self setEnabled
: YES
];
6794 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
6795 locked against the top and bottom edges, and right edge on OS X, where
6796 scrollers are on right. */
6797 #ifdef NS_IMPL_GNUSTEP
6798 [self setAutoresizingMask
: NSViewMaxXMargin | NSViewHeightSizable
];
6800 [self setAutoresizingMask
: NSViewMinXMargin | NSViewHeightSizable
];
6805 pixel_height
= NSHeight
(r
);
6806 if (pixel_height
== 0) pixel_height
= 1;
6807 min_portion
= 20 / pixel_height
;
6809 frame
= XFRAME
(XWINDOW
(win
)->frame
);
6810 if (FRAME_LIVE_P
(frame
))
6813 EmacsView
*view = FRAME_NS_VIEW
(frame
);
6814 NSView
*sview
= [[view window
] contentView
];
6815 NSArray
*subs
= [sview subviews
];
6817 /* disable optimization stopping redraw of other scrollbars */
6818 view->scrollbarsNeedingUpdate
= 0;
6819 for (i
=[subs count
]-1; i
>= 0; i
--)
6820 if ([[subs objectAtIndex
: i
] isKindOfClass
: [EmacsScroller
class]])
6821 view->scrollbarsNeedingUpdate
++;
6822 [sview addSubview
: self
];
6825 /* [self setFrame: r]; */
6831 - (void
)setFrame
: (NSRect
)newRect
6833 NSTRACE
(EmacsScroller_setFrame
);
6834 /* block_input (); */
6835 pixel_height
= NSHeight
(newRect
);
6836 if (pixel_height
== 0) pixel_height
= 1;
6837 min_portion
= 20 / pixel_height
;
6838 [super setFrame
: newRect
];
6840 /* unblock_input (); */
6846 NSTRACE
(EmacsScroller_dealloc
);
6848 wset_vertical_scroll_bar
(XWINDOW
(win
), Qnil
);
6876 /* ensure other scrollbar updates after deletion */
6877 view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
6879 view->scrollbarsNeedingUpdate
++;
6880 [self removeFromSuperview
];
6888 - (void
)resetCursorRects
6890 NSRect visible
= [self visibleRect
];
6891 NSTRACE
(resetCursorRects
);
6893 if (!NSIsEmptyRect
(visible
))
6894 [self addCursorRect
: visible cursor
: [NSCursor arrowCursor
]];
6895 [[NSCursor arrowCursor
] setOnMouseEntered
: YES
];
6899 - (int
) checkSamePosition
: (int
) position portion
: (int
) portion
6902 return em_position
==position
&& em_portion
==portion
&& em_whole
==whole
6903 && portion
!= whole
; /* needed for resize empty buf */
6907 - setPosition
: (int
)position portion
: (int
)portion whole
: (int
)whole
6909 NSTRACE
(setPosition
);
6911 em_position
= position
;
6912 em_portion
= portion
;
6915 if (portion
>= whole
)
6917 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
6918 [self setKnobProportion
: 1.0];
6919 [self setDoubleValue
: 1.0];
6921 [self setFloatValue
: 0.0 knobProportion
: 1.0];
6928 portion
= max ((float
)whole
*min_portion
/pixel_height
, portion
);
6929 pos
= (float
)position
/ (whole
- portion
);
6930 por
= (CGFloat
)portion
/whole
;
6931 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
6932 [self setKnobProportion
: por
];
6933 [self setDoubleValue
: pos
];
6935 [self setFloatValue
: pos knobProportion
: por
];
6939 /* Events may come here even if the event loop is not running.
6940 If we don't enter the event loop, the scroll bar will not update.
6941 So send SIGIO to ourselves. */
6942 if (apploopnr
== 0) raise
(SIGIO
);
6947 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
6948 drag events will go directly to the EmacsScroller. Leaving in for now. */
6949 -(void
)getMouseMotionPart
: (int
*)part window
: (Lisp_Object
*)window
6950 x
: (Lisp_Object
*)x y
: ( Lisp_Object
*)y
6952 *part
= last_hit_part
;
6954 XSETINT
(*y
, pixel_height
);
6955 if ([self floatValue
] > 0.999F
)
6956 XSETINT
(*x
, pixel_height
);
6958 XSETINT
(*x
, pixel_height
* [self floatValue
]);
6962 /* set up emacs_event */
6963 - (void
) sendScrollEventAtLoc
: (float
)loc fromEvent
: (NSEvent
*)e
6968 emacs_event
->part
= last_hit_part
;
6969 emacs_event
->code
= 0;
6970 emacs_event
->modifiers
= EV_MODIFIERS
(e
) | down_modifier
;
6971 emacs_event
->frame_or_window
= win
;
6972 emacs_event
->timestamp
= EV_TIMESTAMP
(e
);
6973 emacs_event
->kind
= SCROLL_BAR_CLICK_EVENT
;
6974 emacs_event
->arg
= Qnil
;
6975 XSETINT
(emacs_event
->x
, loc
* pixel_height
);
6976 XSETINT
(emacs_event
->y
, pixel_height
-20);
6980 n_emacs_events_pending
++;
6981 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
);
6984 hold_event
(emacs_event
);
6985 EVENT_INIT
(*emacs_event
);
6986 ns_send_appdefined
(-1);
6990 /* called manually thru timer to implement repeated button action w/hold-down */
6991 - repeatScroll
: (NSTimer
*)scrollEntry
6993 NSEvent
*e
= [[self window
] currentEvent
];
6994 NSPoint p
= [[self window
] mouseLocationOutsideOfEventStream
];
6995 BOOL inKnob
= [self testPart
: p
] == NSScrollerKnob
;
6997 /* clear timer if need be */
6998 if (inKnob ||
[scroll_repeat_entry timeInterval
] == SCROLL_BAR_FIRST_DELAY
)
7000 [scroll_repeat_entry invalidate
];
7001 [scroll_repeat_entry release
];
7002 scroll_repeat_entry
= nil
;
7008 = [[NSTimer scheduledTimerWithTimeInterval
:
7009 SCROLL_BAR_CONTINUOUS_DELAY
7011 selector
: @selector
(repeatScroll
:)
7017 [self sendScrollEventAtLoc
: 0 fromEvent
: e
];
7022 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
7023 mouseDragged events without going into a modal loop. */
7024 - (void
)mouseDown
: (NSEvent
*)e
7027 /* hitPart is only updated AFTER event is passed on */
7028 NSScrollerPart part
= [self testPart
: [e locationInWindow
]];
7029 CGFloat inc
= 0.0, loc
, kloc
, pos
;
7032 NSTRACE
(EmacsScroller_mouseDown
);
7036 case NSScrollerDecrementPage
:
7037 last_hit_part
= scroll_bar_above_handle
; inc
= -1.0; break;
7038 case NSScrollerIncrementPage
:
7039 last_hit_part
= scroll_bar_below_handle
; inc
= 1.0; break;
7040 case NSScrollerDecrementLine
:
7041 last_hit_part
= scroll_bar_up_arrow
; inc
= -0.1; break;
7042 case NSScrollerIncrementLine
:
7043 last_hit_part
= scroll_bar_down_arrow
; inc
= 0.1; break;
7044 case NSScrollerKnob
:
7045 last_hit_part
= scroll_bar_handle
; break;
7046 case NSScrollerKnobSlot
: /* GNUstep-only */
7047 last_hit_part
= scroll_bar_move_ratio
; break;
7048 default
: /* NSScrollerNoPart? */
7049 fprintf (stderr
, "EmacsScoller
-mouseDown
: unexpected part
%ld\n",
7056 pos
= 0; /* ignored */
7058 /* set a timer to repeat, as we can't let superclass do this modally */
7060 = [[NSTimer scheduledTimerWithTimeInterval
: SCROLL_BAR_FIRST_DELAY
7062 selector
: @selector
(repeatScroll
:)
7069 /* handle, or on GNUstep possibly slot */
7070 NSEvent
*fake_event
;
7072 /* compute float loc in slot and mouse offset on knob */
7073 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7075 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7081 else if (loc
>= NSHeight
(sr
))
7083 loc
= NSHeight
(sr
);
7091 kr
= [self convertRect
: [self rectForPart
: NSScrollerKnob
]
7093 kloc
= NSHeight
(kr
) - ([e locationInWindow
].y
- NSMinY
(kr
));
7095 last_mouse_offset
= kloc
;
7097 /* if knob, tell emacs a location offset by knob pos
7098 (to indicate top of handle) */
7099 if (part
== NSScrollerKnob
)
7100 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7102 /* else this is a slot click on GNUstep: go straight there */
7103 pos
= loc
/ NSHeight
(sr
);
7105 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
7106 fake_event
= [NSEvent mouseEventWithType
: NSLeftMouseUp
7107 location
: [e locationInWindow
]
7108 modifierFlags
: [e modifierFlags
]
7109 timestamp
: [e timestamp
]
7110 windowNumber
: [e windowNumber
]
7111 context
: [e context
]
7112 eventNumber
: [e eventNumber
]
7113 clickCount
: [e clickCount
]
7114 pressure
: [e pressure
]];
7115 [super mouseUp
: fake_event
];
7118 if (part
!= NSScrollerKnob
)
7119 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7123 /* Called as we manually track scroller drags, rather than superclass. */
7124 - (void
)mouseDragged
: (NSEvent
*)e
7129 NSTRACE
(EmacsScroller_mouseDragged
);
7131 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7133 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7139 else if (loc
>= NSHeight
(sr
) + last_mouse_offset
)
7141 loc
= NSHeight
(sr
) + last_mouse_offset
;
7144 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7145 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7149 - (void
)mouseUp
: (NSEvent
*)e
7151 if (scroll_repeat_entry
)
7153 [scroll_repeat_entry invalidate
];
7154 [scroll_repeat_entry release
];
7155 scroll_repeat_entry
= nil
;
7161 /* treat scrollwheel events in the bar as though they were in the main window */
7162 - (void
) scrollWheel
: (NSEvent
*)theEvent
7164 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
7165 [view mouseDown
: theEvent
];
7168 @
end /* EmacsScroller */
7171 #ifdef NS_IMPL_GNUSTEP
7172 /* Dummy class to get rid of startup warnings. */
7173 @implementation EmacsDocument
7179 /* ==========================================================================
7181 Font-related functions; these used to be in nsfaces.m
7183 ========================================================================== */
7187 x_new_font
(struct frame
*f
, Lisp_Object font_object
, int fontset
)
7189 struct font
*font
= XFONT_OBJECT
(font_object
);
7192 fontset
= fontset_from_font
(font_object
);
7193 FRAME_FONTSET
(f
) = fontset
;
7195 if (FRAME_FONT
(f
) == font
)
7196 /* This font is already set in frame F. There's nothing more to
7200 FRAME_FONT
(f
) = font
;
7202 FRAME_BASELINE_OFFSET
(f
) = font
->baseline_offset
;
7203 FRAME_COLUMN_WIDTH
(f
) = font
->average_width
;
7204 FRAME_LINE_HEIGHT
(f
) = font
->height
;
7206 compute_fringe_widths
(f
, 1);
7208 /* Compute the scroll bar width in character columns. */
7209 if (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) > 0)
7211 int wid
= FRAME_COLUMN_WIDTH
(f
);
7212 FRAME_CONFIG_SCROLL_BAR_COLS
(f
)
7213 = (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) + wid
- 1) / wid
;
7217 int wid
= FRAME_COLUMN_WIDTH
(f
);
7218 FRAME_CONFIG_SCROLL_BAR_COLS
(f
) = (14 + wid
- 1) / wid
;
7221 /* Now make the frame display the given font. */
7222 if (FRAME_NS_WINDOW
(f
) != 0)
7223 x_set_window_size
(f
, 0, FRAME_COLS
(f
), FRAME_LINES
(f
));
7229 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
7230 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
7234 ns_xlfd_to_fontname
(const
char *xlfd
)
7235 /* --------------------------------------------------------------------------
7236 Convert an X font name (XLFD) to an NS font name.
7237 Only family is used.
7238 The string returned is temporarily allocated.
7239 -------------------------------------------------------------------------- */
7241 char *name
= xmalloc
(180);
7245 if (!strncmp
(xlfd
, "
--"
, 2))
7246 sscanf (xlfd
, "
--%*[^-]-%[^-]179-", name);
7248 sscanf (xlfd
, "
-%*[^-]-%[^-]179-", name);
7250 /* stopgap for malformed XLFD input */
7251 if (strlen
(name
) == 0)
7252 strcpy
(name
, "Monaco"
);
7254 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
7255 also uppercase after '-' or ' ' */
7256 name
[0] = c_toupper
(name
[0]);
7257 for (len
=strlen
(name
), i
=0; i
<len
; i
++)
7263 name
[i
+1] = c_toupper
(name
[i
+1]);
7265 else if (name
[i
] == '_')
7269 name
[i
+1] = c_toupper
(name
[i
+1]);
7272 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
7273 ret
= [[NSString stringWithUTF8String
: name
] UTF8String
];
7280 syms_of_nsterm
(void
)
7282 NSTRACE
(syms_of_nsterm
);
7284 ns_antialias_threshold
= 10.0;
7286 /* from 23+ we need to tell emacs what modifiers there are.. */
7287 DEFSYM
(Qmodifier_value
, "modifier
-value"
);
7288 DEFSYM
(Qalt
, "alt"
);
7289 DEFSYM
(Qhyper
, "hyper"
);
7290 DEFSYM
(Qmeta
, "
meta"
);
7291 DEFSYM
(Qsuper
, "super"
);
7292 DEFSYM
(Qcontrol
, "control"
);
7293 DEFSYM
(QUTF8_STRING
, "UTF8_STRING"
);
7295 Fput
(Qalt
, Qmodifier_value
, make_number
(alt_modifier
));
7296 Fput
(Qhyper
, Qmodifier_value
, make_number
(hyper_modifier
));
7297 Fput
(Qmeta
, Qmodifier_value
, make_number
(meta_modifier
));
7298 Fput
(Qsuper
, Qmodifier_value
, make_number
(super_modifier
));
7299 Fput
(Qcontrol
, Qmodifier_value
, make_number
(ctrl_modifier
));
7301 DEFVAR_LISP
("ns
-input-file"
, ns_input_file
,
7302 "The file specified in the last NS event."
);
7303 ns_input_file
=Qnil
;
7305 DEFVAR_LISP
("ns
-input-text"
, ns_input_text
,
7306 "The data received in the last NS
text drag event."
);
7307 ns_input_text
=Qnil
;
7309 DEFVAR_LISP
("ns
-working
-text"
, ns_working_text
,
7310 "String
for visualizing working composition sequence."
);
7311 ns_working_text
=Qnil
;
7313 DEFVAR_LISP
("ns
-input-font"
, ns_input_font
,
7314 "The font specified in the last NS event."
);
7315 ns_input_font
=Qnil
;
7317 DEFVAR_LISP
("ns
-input-fontsize"
, ns_input_fontsize
,
7318 "The fontsize specified in the last NS event."
);
7319 ns_input_fontsize
=Qnil
;
7321 DEFVAR_LISP
("ns
-input-line"
, ns_input_line
,
7322 "The
line specified in the last NS event."
);
7323 ns_input_line
=Qnil
;
7325 DEFVAR_LISP
("ns
-input-color"
, ns_input_color
,
7326 "The color specified in the last NS event."
);
7327 ns_input_color
=Qnil
;
7329 DEFVAR_LISP
("ns
-input-spi
-name"
, ns_input_spi_name
,
7330 "The service name specified in the last NS event."
);
7331 ns_input_spi_name
=Qnil
;
7333 DEFVAR_LISP
("ns
-input-spi
-arg"
, ns_input_spi_arg
,
7334 "The service argument specified in the last NS event."
);
7335 ns_input_spi_arg
=Qnil
;
7337 DEFVAR_LISP
("ns
-alternate
-modifier"
, ns_alternate_modifier
,
7338 "This variable describes the behavior of the alternate or option key.
\n\
7339 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7340 Set to none means that the alternate
/ option key is not interpreted by Emacs
\n\
7341 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7342 ns_alternate_modifier
= Qmeta
;
7344 DEFVAR_LISP
("ns
-right
-alternate
-modifier"
, ns_right_alternate_modifier
,
7345 "This variable describes the behavior of the right alternate or option key.
\n\
7346 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7347 Set to left means be the same key as `ns
-alternate
-modifier
'.\n\
7348 Set to none means that the alternate / option key is not interpreted by Emacs\n\
7349 at all, allowing it to be used at a lower level for accented character entry.");
7350 ns_right_alternate_modifier = Qleft;
7352 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
7353 "This variable describes the behavior of the command key.\n\
7354 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
7355 ns_command_modifier = Qsuper;
7357 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
7358 "This variable describes the behavior of the right command key.\n\
7359 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7360 Set to left means be the same key as `ns-command-modifier'.
\n\
7361 Set to none means that the command
/ option key is not interpreted by Emacs
\n\
7362 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7363 ns_right_command_modifier
= Qleft
;
7365 DEFVAR_LISP
("ns
-control
-modifier"
, ns_control_modifier
,
7366 "This variable describes the behavior of the control key.
\n\
7367 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key."
);
7368 ns_control_modifier
= Qcontrol
;
7370 DEFVAR_LISP
("ns
-right
-control
-modifier"
, ns_right_control_modifier
,
7371 "This variable describes the behavior of the right control key.
\n\
7372 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7373 Set to left means be the same key as `ns
-control
-modifier
'.\n\
7374 Set to none means that the control / option key is not interpreted by Emacs\n\
7375 at all, allowing it to be used at a lower level for accented character entry.");
7376 ns_right_control_modifier = Qleft;
7378 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
7379 "This variable describes the behavior of the function key (on laptops).\n\
7380 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7381 Set to none means that the function key is not interpreted by Emacs at all,\n\
7382 allowing it to be used at a lower level for accented character entry.");
7383 ns_function_modifier = Qnone;
7385 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
7386 "Non-nil (the default) means to render text antialiased.");
7387 ns_antialias_text = Qt;
7389 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
7390 "Whether to confirm application quit using dialog.");
7391 ns_confirm_quit = Qnil;
7393 staticpro (&ns_display_name_list);
7394 ns_display_name_list = Qnil;
7396 staticpro (&last_mouse_motion_frame);
7397 last_mouse_motion_frame = Qnil;
7399 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
7400 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
7401 Only works on OSX 10.6 or later. */);
7402 ns_auto_hide_menu_bar = Qnil;
7404 DEFVAR_BOOL ("ns-use-native-fullscreen", ns_use_native_fullscreen,
7405 doc: /*Non-nil means to use native fullscreen on OSX >= 10.7.
7406 Nil means use fullscreen the old (< 10.7) way. The old way works better with
7407 multiple monitors, but lacks tool bar. This variable is ignored on OSX < 10.7.
7408 Default is t for OSX >= 10.7, nil otherwise. */);
7409 #ifdef HAVE_NATIVE_FS
7410 ns_use_native_fullscreen = YES;
7412 ns_use_native_fullscreen = NO;
7414 ns_last_use_native_fullscreen = ns_use_native_fullscreen;
7416 /* TODO: move to common code */
7417 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7418 doc: /* Which toolkit scroll bars Emacs uses, if any.
7419 A value of nil means Emacs doesn't use toolkit scroll bars.
7420 With the X Window system
, the value is a symbol describing the
7421 X toolkit. Possible values are
: gtk
, motif
, xaw
, or xaw3d.
7422 With MS Windows or Nextstep
, the value is t. */
);
7423 Vx_toolkit_scroll_bars
= Qt
;
7425 DEFVAR_BOOL
("x
-use
-underline
-position
-properties"
,
7426 x_use_underline_position_properties
,
7427 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
7428 A value of nil means ignore them. If you encounter fonts with bogus
7429 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7430 to 4.1, set this to nil. */);
7431 x_use_underline_position_properties
= 0;
7433 DEFVAR_BOOL
("x
-underline
-at
-descent
-line"
,
7434 x_underline_at_descent_line
,
7435 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7436 A value of nil means to draw the underline according to the value of the
7437 variable `x-use-underline-position-properties', which is usually at the
7438 baseline level. The default value is nil. */);
7439 x_underline_at_descent_line
= 0;
7441 /* Tell emacs about this window system. */
7442 Fprovide
(intern
("ns"
), Qnil
);