1 /* NeXT/Open/GNUstep / MacOSX communication module.
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2013
4 Free Software Foundation, Inc.
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
68 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_5
75 int term_trace_num
= 0;
76 #define NSTRACE
(x
) fprintf (stderr
, "
%s:%d: [%d] " #x "\n", \
77 __FILE__
, __LINE__
, ++term_trace_num
)
82 extern NSString
*NSMenuDidBeginTrackingNotification
;
84 /* ==========================================================================
88 ========================================================================== */
90 /* Convert a symbol indexed with an NSxxx value to a value as defined
91 in keyboard.c (lispy_function_key). I hope this is a correct way
93 static unsigned convert_ns_to_X_keysym
[] =
95 NSHomeFunctionKey
, 0x50,
96 NSLeftArrowFunctionKey
, 0x51,
97 NSUpArrowFunctionKey
, 0x52,
98 NSRightArrowFunctionKey
, 0x53,
99 NSDownArrowFunctionKey
, 0x54,
100 NSPageUpFunctionKey
, 0x55,
101 NSPageDownFunctionKey
, 0x56,
102 NSEndFunctionKey
, 0x57,
103 NSBeginFunctionKey
, 0x58,
104 NSSelectFunctionKey
, 0x60,
105 NSPrintFunctionKey
, 0x61,
106 NSClearLineFunctionKey
, 0x0B,
107 NSExecuteFunctionKey
, 0x62,
108 NSInsertFunctionKey
, 0x63,
109 NSUndoFunctionKey
, 0x65,
110 NSRedoFunctionKey
, 0x66,
111 NSMenuFunctionKey
, 0x67,
112 NSFindFunctionKey
, 0x68,
113 NSHelpFunctionKey
, 0x6A,
114 NSBreakFunctionKey
, 0x6B,
116 NSF1FunctionKey
, 0xBE,
117 NSF2FunctionKey
, 0xBF,
118 NSF3FunctionKey
, 0xC0,
119 NSF4FunctionKey
, 0xC1,
120 NSF5FunctionKey
, 0xC2,
121 NSF6FunctionKey
, 0xC3,
122 NSF7FunctionKey
, 0xC4,
123 NSF8FunctionKey
, 0xC5,
124 NSF9FunctionKey
, 0xC6,
125 NSF10FunctionKey
, 0xC7,
126 NSF11FunctionKey
, 0xC8,
127 NSF12FunctionKey
, 0xC9,
128 NSF13FunctionKey
, 0xCA,
129 NSF14FunctionKey
, 0xCB,
130 NSF15FunctionKey
, 0xCC,
131 NSF16FunctionKey
, 0xCD,
132 NSF17FunctionKey
, 0xCE,
133 NSF18FunctionKey
, 0xCF,
134 NSF19FunctionKey
, 0xD0,
135 NSF20FunctionKey
, 0xD1,
136 NSF21FunctionKey
, 0xD2,
137 NSF22FunctionKey
, 0xD3,
138 NSF23FunctionKey
, 0xD4,
139 NSF24FunctionKey
, 0xD5,
141 NSBackspaceCharacter
, 0x08, /* 8: Not on some KBs. */
142 NSDeleteCharacter
, 0xFF, /* 127: Big 'delete' key upper right. */
143 NSDeleteFunctionKey
, 0x9F, /* 63272: Del forw key off main array. */
145 NSTabCharacter
, 0x09,
146 0x19, 0x09, /* left tab->regular since pass shift */
147 NSCarriageReturnCharacter
, 0x0D,
148 NSNewlineCharacter
, 0x0D,
149 NSEnterCharacter
, 0x8D,
151 0x41|NSNumericPadKeyMask
, 0xAE, /* KP_Decimal */
152 0x43|NSNumericPadKeyMask
, 0xAA, /* KP_Multiply */
153 0x45|NSNumericPadKeyMask
, 0xAB, /* KP_Add */
154 0x4B|NSNumericPadKeyMask
, 0xAF, /* KP_Divide */
155 0x4E|NSNumericPadKeyMask
, 0xAD, /* KP_Subtract */
156 0x51|NSNumericPadKeyMask
, 0xBD, /* KP_Equal */
157 0x52|NSNumericPadKeyMask
, 0xB0, /* KP_0 */
158 0x53|NSNumericPadKeyMask
, 0xB1, /* KP_1 */
159 0x54|NSNumericPadKeyMask
, 0xB2, /* KP_2 */
160 0x55|NSNumericPadKeyMask
, 0xB3, /* KP_3 */
161 0x56|NSNumericPadKeyMask
, 0xB4, /* KP_4 */
162 0x57|NSNumericPadKeyMask
, 0xB5, /* KP_5 */
163 0x58|NSNumericPadKeyMask
, 0xB6, /* KP_6 */
164 0x59|NSNumericPadKeyMask
, 0xB7, /* KP_7 */
165 0x5B|NSNumericPadKeyMask
, 0xB8, /* KP_8 */
166 0x5C|NSNumericPadKeyMask
, 0xB9, /* KP_9 */
168 0x1B, 0x1B /* escape */
171 static Lisp_Object Qmodifier_value
;
172 Lisp_Object Qalt
, Qcontrol
, Qhyper
, Qmeta
, Qsuper
;
173 extern Lisp_Object Qcursor_color
, Qcursor_type
, Qns
, Qleft
;
175 static Lisp_Object QUTF8_STRING
;
176 static Lisp_Object Qcocoa
, Qgnustep
;
178 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
179 the maximum font size to NOT antialias. On GNUstep there is currently
180 no way to control this behavior. */
181 float ns_antialias_threshold
;
183 NSArray
*ns_send_types
=0, *ns_return_types
=0, *ns_drag_types
=0;
184 NSString
*ns_app_name
= @"Emacs"
; /* default changed later */
186 /* Display variables */
187 struct ns_display_info
*x_display_list
; /* Chain of existing displays */
188 long context_menu_value
= 0;
191 static
struct frame
*ns_updating_frame
;
192 static NSView
*focus_view
= NULL;
193 static int ns_window_num
= 0;
194 #ifdef NS_IMPL_GNUSTEP
197 static BOOL gsaved
= NO
;
198 static BOOL ns_fake_keydown
= NO
;
200 static BOOL ns_menu_bar_is_hidden
= NO
;
202 /*static int debug_lock = 0; */
205 static BOOL send_appdefined
= YES
;
206 #define NO_APPDEFINED_DATA
(-8)
207 static int last_appdefined_event_data
= NO_APPDEFINED_DATA
;
208 static NSTimer
*timed_entry
= 0;
209 static NSTimer
*scroll_repeat_entry
= nil
;
210 static fd_set select_readfds
, select_writefds
;
211 enum
{ SELECT_HAVE_READ
= 1, SELECT_HAVE_WRITE
= 2, SELECT_HAVE_TMO
= 4 };
212 static int select_nfds
= 0, select_valid
= 0;
213 static
struct timespec select_timeout
= { 0, 0 };
214 static int selfds
[2] = { -1, -1 };
215 static pthread_mutex_t select_mutex
;
216 static int apploopnr
= 0;
217 static NSAutoreleasePool
*outerpool
;
218 static
struct input_event
*emacs_event
= NULL;
219 static
struct input_event
*q_event_ptr
= NULL;
220 static int n_emacs_events_pending
= 0;
221 static NSMutableArray
*ns_pending_files
, *ns_pending_service_names
,
222 *ns_pending_service_args
;
223 static BOOL ns_do_open_file
= NO
;
224 static BOOL ns_last_use_native_fullscreen
;
227 struct input_event
*q
;
235 * State for pending menu activation:
236 * MENU_NONE Normal state
237 * MENU_PENDING A menu has been clicked on, but has been canceled so we can
238 * run lisp to update the menu.
239 * MENU_OPENING Menu is up to date, and the click event is redone so the menu
243 #define MENU_PENDING
1
244 #define MENU_OPENING
2
245 static int menu_will_open_state
= MENU_NONE
;
247 /* Saved position for menu click. */
248 static CGPoint menu_mouse_point
;
251 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
252 #define NS_FUNCTION_KEY_MASK
0x800000
253 #define NSLeftControlKeyMask
(0x000001 | NSControlKeyMask
)
254 #define NSRightControlKeyMask
(0x002000 | NSControlKeyMask
)
255 #define NSLeftCommandKeyMask
(0x000008 | NSCommandKeyMask
)
256 #define NSRightCommandKeyMask
(0x000010 | NSCommandKeyMask
)
257 #define NSLeftAlternateKeyMask
(0x000020 | NSAlternateKeyMask
)
258 #define NSRightAlternateKeyMask
(0x000040 | NSAlternateKeyMask
)
259 #define EV_MODIFIERS
(e
) \
260 ((([e modifierFlags
] & NSHelpKeyMask
) ? \
261 hyper_modifier
: 0) \
262 |
(!EQ
(ns_right_alternate_modifier
, Qleft
) && \
263 (([e modifierFlags
] & NSRightAlternateKeyMask
) \
264 == NSRightAlternateKeyMask
) ? \
265 parse_solitary_modifier
(ns_right_alternate_modifier
) : 0) \
266 |
(([e modifierFlags
] & NSAlternateKeyMask
) ? \
267 parse_solitary_modifier
(ns_alternate_modifier
) : 0) \
268 |
(([e modifierFlags
] & NSShiftKeyMask
) ? \
269 shift_modifier
: 0) \
270 |
(!EQ
(ns_right_control_modifier
, Qleft
) && \
271 (([e modifierFlags
] & NSRightControlKeyMask
) \
272 == NSRightControlKeyMask
) ? \
273 parse_solitary_modifier
(ns_right_control_modifier
) : 0) \
274 |
(([e modifierFlags
] & NSControlKeyMask
) ? \
275 parse_solitary_modifier
(ns_control_modifier
) : 0) \
276 |
(([e modifierFlags
] & NS_FUNCTION_KEY_MASK
) ? \
277 parse_solitary_modifier
(ns_function_modifier
) : 0) \
278 |
(!EQ
(ns_right_command_modifier
, Qleft
) && \
279 (([e modifierFlags
] & NSRightCommandKeyMask
) \
280 == NSRightCommandKeyMask
) ? \
281 parse_solitary_modifier
(ns_right_command_modifier
) : 0) \
282 |
(([e modifierFlags
] & NSCommandKeyMask
) ? \
283 parse_solitary_modifier
(ns_command_modifier
):0))
285 #define EV_UDMODIFIERS
(e
) \
286 ((([e
type] == NSLeftMouseDown
) ? down_modifier
: 0) \
287 |
(([e
type] == NSRightMouseDown
) ? down_modifier
: 0) \
288 |
(([e
type] == NSOtherMouseDown
) ? down_modifier
: 0) \
289 |
(([e
type] == NSLeftMouseDragged
) ? down_modifier
: 0) \
290 |
(([e
type] == NSRightMouseDragged
) ? down_modifier
: 0) \
291 |
(([e
type] == NSOtherMouseDragged
) ? down_modifier
: 0) \
292 |
(([e
type] == NSLeftMouseUp
) ? up_modifier
: 0) \
293 |
(([e
type] == NSRightMouseUp
) ? up_modifier
: 0) \
294 |
(([e
type] == NSOtherMouseUp
) ? up_modifier
: 0))
296 #define EV_BUTTON
(e
) \
297 ((([e
type] == NSLeftMouseDown
) ||
([e
type] == NSLeftMouseUp
)) ?
0 : \
298 (([e
type] == NSRightMouseDown
) ||
([e
type] == NSRightMouseUp
)) ?
2 : \
299 [e buttonNumber
] - 1)
301 /* Convert the time field to a timestamp in milliseconds. */
302 #define EV_TIMESTAMP
(e
) ([e timestamp
] * 1000)
304 /* This is a piece of code which is common to all the event handling
305 methods. Maybe it should even be a function. */
306 #define EV_TRAILER
(e
) \
308 XSETFRAME
(emacs_event
->frame_or_window
, emacsframe
); \
312 #define EV_TRAILER2
(e
) \
314 if (e
) emacs_event
->timestamp
= EV_TIMESTAMP
(e
); \
317 n_emacs_events_pending
++; \
318 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
); \
321 hold_event
(emacs_event
); \
322 EVENT_INIT
(*emacs_event
); \
323 ns_send_appdefined
(-1); \
326 /* TODO: get rid of need for these forward declarations */
327 static void ns_condemn_scroll_bars
(struct frame
*f
);
328 static void ns_judge_scroll_bars
(struct frame
*f
);
329 void x_set_frame_alpha
(struct frame
*f
);
332 /* ==========================================================================
336 ========================================================================== */
339 hold_event
(struct input_event
*event
)
341 if (hold_event_q.nr
== hold_event_q.cap
)
343 if (hold_event_q.cap
== 0) hold_event_q.cap
= 10;
344 else hold_event_q.cap
*= 2;
346 xrealloc
(hold_event_q.q
, hold_event_q.cap
* sizeof
*hold_event_q.q
);
349 hold_event_q.q
[hold_event_q.nr
++] = *event
;
350 /* Make sure ns_read_socket is called, i.e. we have input. */
352 send_appdefined
= YES
;
356 append2
(Lisp_Object list
, Lisp_Object item
)
357 /* --------------------------------------------------------------------------
358 Utility to append to a list
359 -------------------------------------------------------------------------- */
361 Lisp_Object array
[2];
363 array
[1] = list1
(item
);
364 return Fnconc
(2, &array
[0]);
369 ns_etc_directory
(void
)
370 /* If running as a self-contained app bundle, return as a string the
371 filename of the etc directory, if present; else nil. */
373 NSBundle
*bundle
= [NSBundle mainBundle
];
374 NSString
*resourceDir
= [bundle resourcePath
];
375 NSString
*resourcePath
;
376 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
379 resourcePath
= [resourceDir stringByAppendingPathComponent
: @"etc"
];
380 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
382 if (isDir
) return [resourcePath UTF8String
];
390 /* If running as a self-contained app bundle, return as a path string
391 the filenames of the libexec and bin directories, ie libexec:bin.
392 Otherwise, return nil.
393 Normally, Emacs does not add its own bin/ directory to the PATH.
394 However, a self-contained NS build has a different layout, with
395 bin/ and libexec/ subdirectories in the directory that contains
397 We put libexec first, because init_callproc_1 uses the first
398 element to initialize exec-directory. An alternative would be
399 for init_callproc to check for invocation-directory/libexec.
402 NSBundle
*bundle
= [NSBundle mainBundle
];
403 NSString
*resourceDir
= [bundle resourcePath
];
404 NSString
*binDir
= [bundle bundlePath
];
405 NSString
*resourcePath
, *resourcePaths
;
407 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
408 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
410 NSEnumerator
*pathEnum
;
413 range
= [resourceDir rangeOfString
: @"Contents"
];
414 if (range.location
!= NSNotFound
)
416 binDir
= [binDir stringByAppendingPathComponent
: @"Contents"
];
418 binDir
= [binDir stringByAppendingPathComponent
: @"MacOS"
];
422 paths
= [binDir stringsByAppendingPaths
:
423 [NSArray arrayWithObjects
: @"libexec"
, @"bin"
, nil
]];
424 pathEnum
= [paths objectEnumerator
];
427 while ((resourcePath
= [pathEnum nextObject
]))
429 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
432 if ([resourcePaths
length] > 0)
434 = [resourcePaths stringByAppendingString
: pathSeparator
];
436 = [resourcePaths stringByAppendingString
: resourcePath
];
439 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
447 /* If running as a self-contained app bundle, return as a path string
448 the filenames of the site-lisp and lisp directories.
449 Ie, site-lisp:lisp. Otherwise, return nil. */
451 NSBundle
*bundle
= [NSBundle mainBundle
];
452 NSString
*resourceDir
= [bundle resourcePath
];
453 NSString
*resourcePath
, *resourcePaths
;
454 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
455 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
457 NSArray
*paths
= [resourceDir stringsByAppendingPaths
:
458 [NSArray arrayWithObjects
:
459 @"site
-lisp"
, @"lisp"
, nil
]];
460 NSEnumerator
*pathEnum
= [paths objectEnumerator
];
463 /* Hack to skip site-lisp. */
464 if (no_site_lisp
) resourcePath
= [pathEnum nextObject
];
466 while ((resourcePath
= [pathEnum nextObject
]))
468 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
471 if ([resourcePaths
length] > 0)
473 = [resourcePaths stringByAppendingString
: pathSeparator
];
475 = [resourcePaths stringByAppendingString
: resourcePath
];
478 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
484 ns_timeout
(int usecs
)
485 /* --------------------------------------------------------------------------
486 Blocking timer utility used by ns_ring_bell
487 -------------------------------------------------------------------------- */
489 struct timespec wakeup
= timespec_add
(current_timespec
(),
490 make_timespec
(0, usecs
* 1000));
492 /* Keep waiting until past the time wakeup. */
495 struct timespec timeout
, now
= current_timespec
();
496 if (timespec_cmp
(wakeup
, now
) <= 0)
498 timeout
= timespec_sub
(wakeup
, now
);
500 /* Try to wait that long--but we might wake up sooner. */
501 pselect
(0, NULL, NULL, NULL, &timeout
, NULL);
507 ns_release_object
(void
*obj
)
508 /* --------------------------------------------------------------------------
509 Release an object (callable from C)
510 -------------------------------------------------------------------------- */
517 ns_retain_object
(void
*obj
)
518 /* --------------------------------------------------------------------------
519 Retain an object (callable from C)
520 -------------------------------------------------------------------------- */
527 ns_alloc_autorelease_pool
(void
)
528 /* --------------------------------------------------------------------------
529 Allocate a pool for temporary objects (callable from C)
530 -------------------------------------------------------------------------- */
532 return [[NSAutoreleasePool alloc
] init
];
537 ns_release_autorelease_pool
(void
*pool
)
538 /* --------------------------------------------------------------------------
539 Free a pool and temporary objects it refers to (callable from C)
540 -------------------------------------------------------------------------- */
542 ns_release_object
(pool
);
547 /* ==========================================================================
549 Focus (clipping) and screen update
551 ========================================================================== */
554 // Window constraining
555 // -------------------
557 // To ensure that the windows are not placed under the
menu bar, they
558 // are typically moved by the call
-back constrainFrameRect. However
,
559 // by overriding it
, it
's possible to inhibit this, leaving the window
560 // in it's original position.
562 // It
's possible to hide the menu bar. However, technically, it's only
563 // possible to hide it when the application is active. To ensure that
564 // this work properly
, the
menu bar and window constraining are
565 // deferred until the application becomes active.
567 // Even though it
's not possible to manually move a window above the
568 // top of the screen, it is allowed if it's done programmatically
,
569 // when the
menu is
hidden. This allows the editable area to cover the
570 // full screen height.
575 // Use the following extra files
:
578 // ;; Hide
menu and place frame slightly above the top of the screen.
579 // (setq ns
-auto
-hide
-menu-bar t
)
580 // (set-frame
-position
(selected
-frame
) 0 -20)
584 // emacs
-Q
-l init.el
586 // Result
: No
menu bar, and the
title bar should be above the screen.
592 // Result
: Menu bar visible
, frame placed immediately below the
menu.
596 ns_constrain_all_frames
(void
)
598 Lisp_Object tail
, frame
;
600 FOR_EACH_FRAME
(tail
, frame
)
602 struct frame
*f
= XFRAME
(frame
);
605 NSView
*view = FRAME_NS_VIEW
(f
);
606 /* This no-op will trigger the default window placing
607 * constraint system. */
608 f
->output_data.ns
->dont_constrain
= 0;
609 [[view window
] setFrameOrigin
:[[view window
] frame
].origin
];
615 /* True, if the menu bar should be hidden. */
618 ns_menu_bar_should_be_hidden
(void
)
620 return !NILP
(ns_auto_hide_menu_bar
)
621 && [NSApp respondsToSelector
:@selector
(setPresentationOptions
:)];
625 /* Show or hide the menu bar, based on user setting. */
628 ns_update_auto_hide_menu_bar
(void
)
631 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
634 NSTRACE
(ns_update_auto_hide_menu_bar
);
636 if (NSApp
!= nil
&& [NSApp isActive
])
638 // Note
, "setPresentationOptions" triggers an
error unless the
639 // application is active.
640 BOOL menu_bar_should_be_hidden
= ns_menu_bar_should_be_hidden
();
642 if (menu_bar_should_be_hidden
!= ns_menu_bar_is_hidden
)
644 NSApplicationPresentationOptions options
645 = NSApplicationPresentationDefault
;
647 if (menu_bar_should_be_hidden
)
648 options |
= NSApplicationPresentationAutoHideMenuBar
649 | NSApplicationPresentationAutoHideDock
;
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 EmacsView
*view = FRAME_NS_VIEW
(f
);
676 NSTRACE
(ns_update_begin
);
678 ns_update_auto_hide_menu_bar
();
681 if ([view isFullscreen
] && [view fsIsNative
])
683 // Fix reappearing tool
bar in fullscreen
for OSX
10.7
684 BOOL tbar_visible
= FRAME_EXTERNAL_TOOL_BAR
(f
) ? YES
: NO
;
685 NSToolbar
*toolbar
= [FRAME_NS_VIEW
(f
) toolbar
];
686 if (! tbar_visible
!= ! [toolbar isVisible
])
687 [toolbar setVisible
: tbar_visible
];
691 ns_updating_frame
= f
;
694 /* drawRect may have been called for say the minibuffer, and then clip path
695 is for the minibuffer. But the display engine may draw more because
696 we have set the frame as garbaged. So reset clip path to the whole
701 NSRect r
= [view frame
];
702 NSRect cr
= [[view window
] frame
];
703 /* If a large frame size is set, r may be larger than the window frame
704 before constrained. In that case don't change the clip path, as we
705 will clear in to the tool bar and title bar. */
707 + FRAME_NS_TITLEBAR_HEIGHT
(f
)
708 + FRAME_TOOLBAR_HEIGHT
(f
) <= cr.
size.height
)
710 bp
= [[NSBezierPath bezierPathWithRect
: r
] retain
];
717 #ifdef NS_IMPL_GNUSTEP
718 uRect
= NSMakeRect
(0, 0, 0, 0);
724 ns_update_window_begin
(struct window
*w
)
725 /* --------------------------------------------------------------------------
726 Prepare for a grouped sequence of drawing calls
727 external (RIF) call; for one window, called after update_begin
728 -------------------------------------------------------------------------- */
730 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
731 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
733 NSTRACE
(ns_update_window_begin
);
734 w
->output_cursor
= w
->cursor
;
738 if (f
== hlinfo
->mouse_face_mouse_frame
)
740 /* Don't do highlighting for mouse motion during the update. */
741 hlinfo
->mouse_face_defer
= 1;
743 /* If the frame needs to be redrawn,
744 simply forget about any prior mouse highlighting. */
745 if (FRAME_GARBAGED_P
(f
))
746 hlinfo
->mouse_face_window
= Qnil
;
748 /* (further code for mouse faces ifdef'd out in other terms elided) */
756 ns_update_window_end
(struct window
*w
, bool cursor_on_p
,
757 bool mouse_face_overwritten_p
)
758 /* --------------------------------------------------------------------------
759 Finished a grouped sequence of drawing calls
760 external (RIF) call; for one window called before update_end
761 -------------------------------------------------------------------------- */
763 /* note: this fn is nearly identical in all terms */
764 if (!w
->pseudo_window_p
)
769 display_and_set_cursor
(w
, 1,
770 w
->output_cursor.hpos
, w
->output_cursor.vpos
,
771 w
->output_cursor.x
, w
->output_cursor.y
);
773 if (draw_window_fringes
(w
, 1))
775 if (WINDOW_RIGHT_DIVIDER_WIDTH
(w
))
776 x_draw_right_divider
(w
);
778 x_draw_vertical_border
(w
);
784 /* If a row with mouse-face was overwritten, arrange for
785 frame_up_to_date to redisplay the mouse highlight. */
786 if (mouse_face_overwritten_p
)
787 reset_mouse_highlight
(MOUSE_HL_INFO
(XFRAME
(w
->frame
)));
789 NSTRACE
(update_window_end
);
794 ns_update_end
(struct frame
*f
)
795 /* --------------------------------------------------------------------------
796 Finished a grouped sequence of drawing calls
797 external (RIF) call; for whole frame, called after update_window_end
798 -------------------------------------------------------------------------- */
800 EmacsView
*view = FRAME_NS_VIEW
(f
);
802 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
803 MOUSE_HL_INFO
(f
)->mouse_face_defer
= 0;
808 [[view window
] flushWindow
];
811 ns_updating_frame
= NULL;
812 NSTRACE
(ns_update_end
);
816 ns_focus
(struct frame
*f
, NSRect
*r
, int n
)
817 /* --------------------------------------------------------------------------
818 Internal: Focus on given frame. During small local updates this is used to
819 draw, however during large updates, ns_update_begin and ns_update_end are
820 called to wrap the whole thing, in which case these calls are stubbed out.
821 Except, on GNUstep, we accumulate the rectangle being drawn into, because
822 the back end won't do this automatically, and will just end up flushing
824 -------------------------------------------------------------------------- */
826 // NSTRACE
(ns_focus
);
828 fprintf (stderr, "focus: %d", c++);
829 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
830 fprintf (stderr, "\n"); */
832 if (f
!= ns_updating_frame
)
834 NSView
*view = FRAME_NS_VIEW
(f
);
835 if (view != focus_view
)
837 if (focus_view
!= NULL)
839 [focus_view unlockFocus
];
840 [[focus_view window
] flushWindow
];
847 /*if (view) debug_lock++; */
854 [[NSGraphicsContext currentContext
] saveGraphicsState
];
856 NSRectClipList
(r
, 2);
865 ns_unfocus
(struct frame
*f
)
866 /* --------------------------------------------------------------------------
867 Internal: Remove focus on given frame
868 -------------------------------------------------------------------------- */
870 // NSTRACE
(ns_unfocus
);
874 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
878 if (f
!= ns_updating_frame
)
880 if (focus_view
!= NULL)
882 [focus_view unlockFocus
];
883 [[focus_view window
] flushWindow
];
892 ns_clip_to_row
(struct window
*w
, struct glyph_row
*row
,
893 enum glyph_row_area area
, BOOL gc
)
894 /* --------------------------------------------------------------------------
895 Internal (but parallels other terms): Focus drawing on given row
896 -------------------------------------------------------------------------- */
898 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
900 int window_x
, window_y
, window_width
;
902 window_box
(w
, area
, &window_x
, &window_y
, &window_width
, 0);
904 clip_rect.origin.x
= window_x
;
905 clip_rect.origin.y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, row
->y
));
906 clip_rect.origin.y
= max (clip_rect.origin.y
, window_y
);
907 clip_rect.
size.width
= window_width
;
908 clip_rect.
size.height
= row
->visible_height
;
910 ns_focus
(f
, &clip_rect
, 1);
915 ns_ring_bell
(struct frame
*f
)
916 /* --------------------------------------------------------------------------
918 -------------------------------------------------------------------------- */
920 NSTRACE
(ns_ring_bell
);
923 NSAutoreleasePool
*pool
;
924 struct frame
*frame
= SELECTED_FRAME
();
928 pool
= [[NSAutoreleasePool alloc
] init
];
930 view = FRAME_NS_VIEW
(frame
);
934 NSPoint dim
= NSMakePoint
(128, 128);
937 r.origin.x
+= (r.
size.width
- dim.x
) / 2;
938 r.origin.y
+= (r.
size.height
- dim.y
) / 2;
939 r.
size.width
= dim.x
;
940 r.
size.height
= dim.y
;
941 surr
= NSInsetRect
(r
, -2, -2);
942 ns_focus
(frame
, &surr
, 1);
943 [[view window
] cacheImageInRect
: [view convertRect
: surr toView
:nil
]];
944 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
945 (FRAME_DEFAULT_FACE
(frame
)), frame
) set];
947 [[view window
] flushWindow
];
949 [[view window
] restoreCachedImage
];
950 [[view window
] flushWindow
];
962 /* ==========================================================================
964 Frame / window manager related functions
966 ========================================================================== */
970 ns_raise_frame
(struct frame
*f
)
971 /* --------------------------------------------------------------------------
972 Bring window to foreground and make it active
973 -------------------------------------------------------------------------- */
976 check_window_system
(f
);
977 view = FRAME_NS_VIEW
(f
);
979 if (FRAME_VISIBLE_P
(f
))
980 [[view window
] makeKeyAndOrderFront
: NSApp
];
986 ns_lower_frame
(struct frame
*f
)
987 /* --------------------------------------------------------------------------
989 -------------------------------------------------------------------------- */
992 check_window_system
(f
);
993 view = FRAME_NS_VIEW
(f
);
995 [[view window
] orderBack
: NSApp
];
1001 ns_frame_raise_lower
(struct frame
*f
, int raise
)
1002 /* --------------------------------------------------------------------------
1004 -------------------------------------------------------------------------- */
1006 NSTRACE
(ns_frame_raise_lower
);
1016 ns_frame_rehighlight
(struct frame
*frame
)
1017 /* --------------------------------------------------------------------------
1018 External (hook): called on things like window switching within frame
1019 -------------------------------------------------------------------------- */
1021 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(frame
);
1022 struct frame
*old_highlight
= dpyinfo
->x_highlight_frame
;
1024 NSTRACE
(ns_frame_rehighlight
);
1025 if (dpyinfo
->x_focus_frame
)
1027 dpyinfo
->x_highlight_frame
1028 = (FRAMEP
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1029 ? XFRAME
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1030 : dpyinfo
->x_focus_frame
);
1031 if (!FRAME_LIVE_P
(dpyinfo
->x_highlight_frame
))
1033 fset_focus_frame
(dpyinfo
->x_focus_frame
, Qnil
);
1034 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
;
1038 dpyinfo
->x_highlight_frame
= 0;
1040 if (dpyinfo
->x_highlight_frame
&&
1041 dpyinfo
->x_highlight_frame
!= old_highlight
)
1045 x_update_cursor
(old_highlight
, 1);
1046 x_set_frame_alpha
(old_highlight
);
1048 if (dpyinfo
->x_highlight_frame
)
1050 x_update_cursor
(dpyinfo
->x_highlight_frame
, 1);
1051 x_set_frame_alpha
(dpyinfo
->x_highlight_frame
);
1058 x_make_frame_visible
(struct frame
*f
)
1059 /* --------------------------------------------------------------------------
1060 External: Show the window (X11 semantics)
1061 -------------------------------------------------------------------------- */
1063 NSTRACE
(x_make_frame_visible
);
1064 /* XXX: at some points in past this was not needed, as the only place that
1065 called this (frame.c:Fraise_frame ()) also called raise_lower;
1066 if this ends up the case again, comment this out again. */
1067 if (!FRAME_VISIBLE_P
(f
))
1069 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1071 SET_FRAME_VISIBLE
(f
, 1);
1074 /* Making a new frame from a fullscreen frame will make the new frame
1075 fullscreen also. So skip handleFS as this will print an error. */
1076 if ([view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
1077 && [view isFullscreen
])
1080 if (f
->want_fullscreen
!= FULLSCREEN_NONE
)
1091 x_make_frame_invisible
(struct frame
*f
)
1092 /* --------------------------------------------------------------------------
1093 External: Hide the window (X11 semantics)
1094 -------------------------------------------------------------------------- */
1097 NSTRACE
(x_make_frame_invisible
);
1098 check_window_system
(f
);
1099 view = FRAME_NS_VIEW
(f
);
1100 [[view window
] orderOut
: NSApp
];
1101 SET_FRAME_VISIBLE
(f
, 0);
1102 SET_FRAME_ICONIFIED
(f
, 0);
1107 x_iconify_frame
(struct frame
*f
)
1108 /* --------------------------------------------------------------------------
1109 External: Iconify window
1110 -------------------------------------------------------------------------- */
1113 struct ns_display_info
*dpyinfo
;
1115 NSTRACE
(x_iconify_frame
);
1116 check_window_system
(f
);
1117 view = FRAME_NS_VIEW
(f
);
1118 dpyinfo
= FRAME_DISPLAY_INFO
(f
);
1120 if (dpyinfo
->x_highlight_frame
== f
)
1121 dpyinfo
->x_highlight_frame
= 0;
1123 if ([[view window
] windowNumber
] <= 0)
1125 /* the window is still deferred. Make it very small, bring it
1126 on screen and order it out. */
1127 NSRect s
= { { 100, 100}, {0, 0} };
1129 t
= [[view window
] frame
];
1130 [[view window
] setFrame
: s display
: NO
];
1131 [[view window
] orderBack
: NSApp
];
1132 [[view window
] orderOut
: NSApp
];
1133 [[view window
] setFrame
: t display
: NO
];
1135 [[view window
] miniaturize
: NSApp
];
1138 /* Free X resources of frame F. */
1141 x_free_frame_resources
(struct frame
*f
)
1144 struct ns_display_info
*dpyinfo
;
1145 Mouse_HLInfo
*hlinfo
;
1147 NSTRACE
(x_free_frame_resources
);
1148 check_window_system
(f
);
1149 view = FRAME_NS_VIEW
(f
);
1150 dpyinfo
= FRAME_DISPLAY_INFO
(f
);
1151 hlinfo
= MOUSE_HL_INFO
(f
);
1153 [(EmacsView
*)view setWindowClosing
: YES
]; /* may not have been informed */
1157 free_frame_menubar
(f
);
1159 if (FRAME_FACE_CACHE
(f
))
1160 free_frame_faces
(f
);
1162 if (f
== dpyinfo
->x_focus_frame
)
1163 dpyinfo
->x_focus_frame
= 0;
1164 if (f
== dpyinfo
->x_highlight_frame
)
1165 dpyinfo
->x_highlight_frame
= 0;
1166 if (f
== hlinfo
->mouse_face_mouse_frame
)
1167 reset_mouse_highlight
(hlinfo
);
1169 if (f
->output_data.ns
->miniimage
!= nil
)
1170 [f
->output_data.ns
->miniimage release
];
1172 [[view window
] close];
1175 xfree
(f
->output_data.ns
);
1181 x_destroy_window
(struct frame
*f
)
1182 /* --------------------------------------------------------------------------
1183 External: Delete the window
1184 -------------------------------------------------------------------------- */
1186 NSTRACE
(x_destroy_window
);
1187 check_window_system
(f
);
1188 x_free_frame_resources
(f
);
1194 x_set_offset
(struct frame
*f
, int xoff
, int yoff
, int change_grav
)
1195 /* --------------------------------------------------------------------------
1196 External: Position the window
1197 -------------------------------------------------------------------------- */
1199 NSView
*view = FRAME_NS_VIEW
(f
);
1200 NSArray
*screens
= [NSScreen screens
];
1201 NSScreen
*fscreen
= [screens objectAtIndex
: 0];
1202 NSScreen
*screen
= [[view window
] screen
];
1204 NSTRACE
(x_set_offset
);
1211 if (view != nil
&& screen
&& fscreen
)
1213 f
->left_pos
= f
->size_hint_flags
& XNegative
1214 ?
[screen visibleFrame
].
size.width
+ f
->left_pos
- FRAME_PIXEL_WIDTH
(f
)
1216 /* We use visibleFrame here to take menu bar into account.
1217 Ideally we should also adjust left/top with visibleFrame.origin. */
1219 f
->top_pos
= f
->size_hint_flags
& YNegative
1220 ?
([screen visibleFrame
].
size.height
+ f
->top_pos
1221 - FRAME_PIXEL_HEIGHT
(f
) - FRAME_NS_TITLEBAR_HEIGHT
(f
)
1222 - FRAME_TOOLBAR_HEIGHT
(f
))
1224 #ifdef NS_IMPL_GNUSTEP
1225 if (f
->left_pos
< 100)
1226 f
->left_pos
= 100; /* don't overlap menu */
1228 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1230 f
->output_data.ns
->dont_constrain
= 0;
1231 [[view window
] setFrameTopLeftPoint
:
1232 NSMakePoint
(SCREENMAXBOUND
(f
->left_pos
),
1233 SCREENMAXBOUND
([fscreen frame
].
size.height
1234 - NS_TOP_POS
(f
)))];
1235 f
->size_hint_flags
&= ~
(XNegative|YNegative
);
1243 x_set_window_size
(struct frame
*f
,
1248 /* --------------------------------------------------------------------------
1249 Adjust window pixel size based on given character grid size
1250 Impl is a bit more complex than other terms, need to do some
1252 -------------------------------------------------------------------------- */
1254 EmacsView
*view = FRAME_NS_VIEW
(f
);
1255 NSWindow
*window
= [view window
];
1256 NSRect wr
= [window frame
];
1257 int tb
= FRAME_EXTERNAL_TOOL_BAR
(f
);
1258 int pixelwidth
, pixelheight
;
1261 NSTRACE
(x_set_window_size
);
1266 /*fprintf (stderr, "\tsetWindowSize: %d x %d, pixelwise %d, font size %d x %d\n", width, height, pixelwise, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f));*/
1270 check_frame_size
(f
, &width
, &height
, pixelwise
);
1272 f
->scroll_bar_actual_width
= NS_SCROLL_BAR_WIDTH
(f
);
1273 compute_fringe_widths
(f
, 0);
1277 pixelwidth
= FRAME_TEXT_TO_PIXEL_WIDTH
(f
, width
);
1278 pixelheight
= FRAME_TEXT_TO_PIXEL_HEIGHT
(f
, height
);
1279 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(f
, pixelwidth
);
1280 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(f
, pixelheight
);
1284 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, width
);
1285 pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, height
);
1290 /* If we have a toolbar, take its height into account. */
1291 if (tb
&& ! [view isFullscreen
])
1293 /* NOTE: previously this would generate wrong result if toolbar not
1294 yet displayed and fixing toolbar_height=32 helped, but
1295 now (200903) seems no longer needed */
1296 FRAME_TOOLBAR_HEIGHT
(f
) =
1297 NSHeight
([window frameRectForContentRect
: NSMakeRect
(0, 0, 0, 0)])
1298 - FRAME_NS_TITLEBAR_HEIGHT
(f
);
1299 #ifdef NS_IMPL_GNUSTEP
1300 FRAME_TOOLBAR_HEIGHT
(f
) -= 3;
1304 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
1306 wr.
size.width
= pixelwidth
+ f
->border_width
;
1307 wr.
size.height
= pixelheight
;
1308 if (! [view isFullscreen
])
1309 wr.
size.height
+= FRAME_NS_TITLEBAR_HEIGHT
(f
)
1310 + FRAME_TOOLBAR_HEIGHT
(f
);
1312 /* Do not try to constrain to this screen. We may have multiple
1313 screens, and want Emacs to span those. Constraining to screen
1314 prevents that, and that is not nice to the user. */
1315 if (f
->output_data.ns
->zooming
)
1316 f
->output_data.ns
->zooming
= 0;
1318 wr.origin.y
+= FRAME_PIXEL_HEIGHT
(f
) - pixelheight
;
1320 [view setRows
: rows andColumns
: cols
];
1321 [window setFrame
: wr display
: YES
];
1323 fprintf (stderr
, "
\tx_set_window_size
%d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight);
1325 /* This is a trick to compensate for Emacs' managing the scrollbar area
1326 as a fixed number of standard character columns. Instead of leaving
1327 blank space for the extra, we chopped it off above. Now for
1328 left-hand scrollbars, we shift all rendering to the left by the
1329 difference between the real width and Emacs' imagined one. For
1330 right-hand bars, don't worry about it since the extra is never used.
1331 (Obviously doesn't work for vertically split windows tho..) */
1333 NSPoint origin
= FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT
(f
)
1334 ? NSMakePoint
(FRAME_SCROLL_BAR_COLS
(f
) * FRAME_COLUMN_WIDTH
(f
)
1335 - NS_SCROLL_BAR_WIDTH
(f
), 0)
1336 : NSMakePoint
(0, 0);
1337 [view setFrame
: NSMakeRect
(0, 0, pixelwidth
, pixelheight
)];
1338 [view setBoundsOrigin
: origin
];
1341 change_frame_size
(f
, width
, height
, 0, 1, 0, pixelwise
);
1342 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1344 mark_window_cursors_off
(XWINDOW
(f
->root_window
));
1345 cancel_mouse_face
(f
);
1352 ns_fullscreen_hook
(struct frame
*f
)
1354 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1356 if (!FRAME_VISIBLE_P
(f
))
1359 if (! [view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
)
1361 /* Old style fs don't initiate correctly if created from
1362 init/default-frame alist, so use a timer (not nice...).
1364 [NSTimer scheduledTimerWithTimeInterval
: 0.5 target
: view
1365 selector
: @selector
(handleFS
)
1366 userInfo
: nil repeats
: NO
];
1375 /* ==========================================================================
1379 ========================================================================== */
1383 ns_lookup_indexed_color
(unsigned
long idx
, struct frame
*f
)
1385 struct ns_color_table
*color_table
= FRAME_DISPLAY_INFO
(f
)->color_table
;
1386 if (idx
< 1 || idx
>= color_table
->avail
)
1388 return color_table
->colors
[idx
];
1393 ns_index_color
(NSColor
*color
, struct frame
*f
)
1395 struct ns_color_table
*color_table
= FRAME_DISPLAY_INFO
(f
)->color_table
;
1399 if (!color_table
->colors
)
1401 color_table
->size = NS_COLOR_CAPACITY
;
1402 color_table
->avail
= 1; /* skip idx=0 as marker */
1403 color_table
->colors
= xmalloc
(color_table
->size * sizeof
(NSColor
*));
1404 color_table
->colors
[0] = nil
;
1405 color_table
->empty_indices
= [[NSMutableSet alloc
] init
];
1408 /* Do we already have this color? */
1409 for (i
= 1; i
< color_table
->avail
; i
++)
1410 if (color_table
->colors
[i
] && [color_table
->colors
[i
] isEqual: color
])
1413 if ([color_table
->empty_indices count
] > 0)
1415 NSNumber
*index
= [color_table
->empty_indices anyObject
];
1416 [color_table
->empty_indices removeObject
: index
];
1417 idx
= [index unsignedLongValue
];
1421 if (color_table
->avail
== color_table
->size)
1422 color_table
->colors
=
1423 xpalloc
(color_table
->colors
, &color_table
->size, 1,
1424 min (ULONG_MAX
, PTRDIFF_MAX
), sizeof
*color_table
->colors
);
1425 idx
= color_table
->avail
++;
1428 color_table
->colors
[idx
] = color
;
1430 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1436 ns_free_indexed_color
(unsigned
long idx
, struct frame
*f
)
1438 struct ns_color_table
*color_table
;
1445 color_table
= FRAME_DISPLAY_INFO
(f
)->color_table
;
1447 if (idx
<= 0 || idx
>= color_table
->size) {
1448 message1
("ns_free_indexed_color
: Color index out of range.
\n"
);
1452 index
= [NSNumber numberWithUnsignedInt
: idx
];
1453 if ([color_table
->empty_indices containsObject
: index
]) {
1454 message1
("ns_free_indexed_color
: attempt to free already freed color.
\n"
);
1458 color
= color_table
->colors
[idx
];
1460 color_table
->colors
[idx
] = nil
;
1461 [color_table
->empty_indices addObject
: index
];
1462 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1467 ns_get_color
(const
char *name
, NSColor
**col
)
1468 /* --------------------------------------------------------------------------
1470 -------------------------------------------------------------------------- */
1471 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
1472 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1473 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1476 static
char hex
[20];
1478 float r
= -1.0, g
, b
;
1479 NSString
*nsname
= [NSString stringWithUTF8String
: name
];
1481 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1484 if ([nsname isEqualToString
: @"ns_selection_bg_color"
])
1486 #ifdef NS_IMPL_COCOA
1487 NSString
*defname
= [[NSUserDefaults standardUserDefaults
]
1488 stringForKey
: @"AppleHighlightColor"
];
1493 if ((new
= [NSColor selectedTextBackgroundColor
]) != nil
)
1495 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1500 nsname
= NS_SELECTION_BG_COLOR_DEFAULT
;
1502 name
= [nsname UTF8String
];
1504 else if ([nsname isEqualToString
: @"ns_selection_fg_color"
])
1506 /* NOTE: OSX applications normally don't set foreground selection, but
1507 text may be unreadable if we don't.
1509 if ((new
= [NSColor selectedTextColor
]) != nil
)
1511 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1516 nsname
= NS_SELECTION_FG_COLOR_DEFAULT
;
1517 name
= [nsname UTF8String
];
1520 /* First, check for some sort of numeric specification. */
1523 if (name
[0] == '0' || name
[0] == '1' || name
[0] == '.') /* RGB decimal */
1525 NSScanner
*scanner
= [NSScanner scannerWithString
: nsname
];
1526 [scanner scanFloat
: &r
];
1527 [scanner scanFloat
: &g
];
1528 [scanner scanFloat
: &b
];
1530 else if (!strncmp
(name
, "rgb
:"
, 4)) /* A newer X11 format -- rgb:r/g/b */
1531 scaling
= (snprintf
(hex
, sizeof hex
, "
%s", name + 4) - 2) / 3;
1532 else if (name
[0] == '#') /* An old X11 format; convert to newer */
1534 int len
= (strlen
(name
) - 1);
1535 int start
= (len
% 3 == 0) ? 1 : len / 4 + 1;
1537 scaling
= strlen
(name
+start
) / 3;
1538 for (i
= 0; i
< 3; i
++)
1539 sprintf (hex
+ i
* (scaling
+ 1), "
%.*s/", scaling,
1540 name
+ start
+ i
* scaling
);
1541 hex
[3 * (scaling
+ 1) - 1] = '\0';
1547 float fscale
= scaling
== 4 ?
65535.0 : (scaling
== 2 ?
255.0 : 15.0);
1548 if (sscanf (hex
, "
%x/%x/%x", &rr, &gg, &bb))
1558 *col
= [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: 1.0];
1563 /* Otherwise, color is expected to be from a list */
1565 NSEnumerator
*lenum
, *cenum
;
1569 #ifdef NS_IMPL_GNUSTEP
1570 /* XXX: who is wrong, the requestor or the implementation? */
1571 if ([nsname compare
: @"Highlight" options
: NSCaseInsensitiveSearch
]
1573 nsname
= @"highlightColor"
;
1576 lenum
= [[NSColorList availableColorLists
] objectEnumerator
];
1577 while ( (clist
= [lenum nextObject
]) && new
== nil
)
1579 cenum
= [[clist allKeys
] objectEnumerator
];
1580 while ( (name
= [cenum nextObject
]) && new
== nil
)
1582 if ([name compare
: nsname
1583 options
: NSCaseInsensitiveSearch
] == NSOrderedSame
)
1584 new
= [clist colorWithKey
: name
];
1590 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1597 ns_lisp_to_color
(Lisp_Object color
, NSColor
**col
)
1598 /* --------------------------------------------------------------------------
1599 Convert a Lisp string object to a NS color
1600 -------------------------------------------------------------------------- */
1602 NSTRACE
(ns_lisp_to_color
);
1603 if (STRINGP
(color
))
1604 return ns_get_color
(SSDATA
(color
), col
);
1605 else if (SYMBOLP
(color
))
1606 return ns_get_color
(SSDATA
(SYMBOL_NAME
(color
)), col
);
1612 ns_color_to_lisp
(NSColor
*col
)
1613 /* --------------------------------------------------------------------------
1614 Convert a color to a lisp string with the RGB equivalent
1615 -------------------------------------------------------------------------- */
1617 EmacsCGFloat red
, green
, blue
, alpha
, gray;
1620 NSTRACE
(ns_color_to_lisp
);
1623 if ([[col colorSpaceName
] isEqualToString
: NSNamedColorSpace
])
1625 if ((str
=[[col colorNameComponent
] UTF8String
]))
1628 return build_string
((char *)str
);
1631 [[col colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
]
1632 getRed
: &red green
: &green blue
: &blue alpha
: &alpha
];
1633 if (red
==green
&& red
==blue
)
1635 [[col colorUsingColorSpaceName
: NSCalibratedWhiteColorSpace
]
1636 getWhite
: &gray alpha
: &alpha
];
1637 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1638 lrint
(gray * 0xff), lrint
(gray * 0xff), lrint
(gray * 0xff));
1640 return build_string
(buf
);
1643 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1644 lrint
(red
*0xff), lrint
(green
*0xff), lrint
(blue
*0xff));
1647 return build_string
(buf
);
1652 ns_query_color
(void
*col
, XColor
*color_def
, int setPixel
)
1653 /* --------------------------------------------------------------------------
1654 Get ARGB values out of NSColor col and put them into color_def.
1655 If setPixel, set the pixel to a concatenated version.
1656 and set color_def pixel to the resulting index.
1657 -------------------------------------------------------------------------- */
1659 EmacsCGFloat r
, g
, b
, a
;
1661 [((NSColor
*)col
) getRed
: &r green
: &g blue
: &b alpha
: &a
];
1662 color_def
->red
= r
* 65535;
1663 color_def
->green
= g
* 65535;
1664 color_def
->blue
= b
* 65535;
1666 if (setPixel
== YES
)
1668 = ARGB_TO_ULONG
((int
)(a
*255),
1669 (int
)(r
*255), (int
)(g
*255), (int
)(b
*255));
1674 ns_defined_color
(struct frame
*f
,
1679 /* --------------------------------------------------------------------------
1680 Return true if named color found, and set color_def rgb accordingly.
1681 If makeIndex and alloc are nonzero put the color in the color_table,
1682 and set color_def pixel to the resulting index.
1683 If makeIndex is zero, set color_def pixel to ARGB.
1684 Return false if not found
1685 -------------------------------------------------------------------------- */
1688 NSTRACE
(ns_defined_color
);
1691 if (ns_get_color
(name
, &col
) != 0) /* Color not found */
1696 if (makeIndex
&& alloc
)
1697 color_def
->pixel
= ns_index_color
(col
, f
);
1698 ns_query_color
(col
, color_def
, !makeIndex
);
1705 x_set_frame_alpha
(struct frame
*f
)
1706 /* --------------------------------------------------------------------------
1707 change the entire-frame transparency
1708 -------------------------------------------------------------------------- */
1710 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(f
);
1712 double alpha_min
= 1.0;
1714 if (dpyinfo
->x_highlight_frame
== f
)
1715 alpha
= f
->alpha
[0];
1717 alpha
= f
->alpha
[1];
1719 if (FLOATP
(Vframe_alpha_lower_limit
))
1720 alpha_min
= XFLOAT_DATA
(Vframe_alpha_lower_limit
);
1721 else if (INTEGERP
(Vframe_alpha_lower_limit
))
1722 alpha_min
= (XINT
(Vframe_alpha_lower_limit
)) / 100.0;
1726 else if (1.0 < alpha
)
1728 else if (0.0 <= alpha
&& alpha
< alpha_min
&& alpha_min
<= 1.0)
1731 #ifdef NS_IMPL_COCOA
1733 EmacsView
*view = FRAME_NS_VIEW
(f
);
1734 [[view window
] setAlphaValue
: alpha
];
1740 /* ==========================================================================
1744 ========================================================================== */
1748 x_set_mouse_pixel_position
(struct frame
*f
, int pix_x
, int pix_y
)
1749 /* --------------------------------------------------------------------------
1750 Programmatically reposition mouse pointer in pixel coordinates
1751 -------------------------------------------------------------------------- */
1753 NSTRACE
(x_set_mouse_pixel_position
);
1756 /* FIXME: this does not work, and what about GNUstep? */
1757 #ifdef NS_IMPL_COCOA
1758 [FRAME_NS_VIEW
(f
) lockFocus
];
1759 PSsetmouse
((float
)pix_x
, (float
)pix_y
);
1760 [FRAME_NS_VIEW
(f
) unlockFocus
];
1767 x_set_mouse_position
(struct frame
*f
, int h
, int v
)
1768 /* --------------------------------------------------------------------------
1769 Programmatically reposition mouse pointer in character coordinates
1770 -------------------------------------------------------------------------- */
1774 pix_x
= FRAME_COL_TO_PIXEL_X
(f
, h
) + FRAME_COLUMN_WIDTH
(f
) / 2;
1775 pix_y
= FRAME_LINE_TO_PIXEL_Y
(f
, v
) + FRAME_LINE_HEIGHT
(f
) / 2;
1777 if (pix_x
< 0) pix_x
= 0;
1778 if (pix_x
> FRAME_PIXEL_WIDTH
(f
)) pix_x
= FRAME_PIXEL_WIDTH
(f
);
1780 if (pix_y
< 0) pix_y
= 0;
1781 if (pix_y
> FRAME_PIXEL_HEIGHT
(f
)) pix_y
= FRAME_PIXEL_HEIGHT
(f
);
1783 x_set_mouse_pixel_position
(f
, pix_x
, pix_y
);
1788 note_mouse_movement
(struct frame
*frame
, CGFloat x
, CGFloat y
)
1789 /* ------------------------------------------------------------------------
1790 Called by EmacsView on mouseMovement events. Passes on
1791 to emacs mainstream code if we moved off of a rect of interest
1792 known as last_mouse_glyph.
1793 ------------------------------------------------------------------------ */
1795 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(frame
);
1798 // NSTRACE
(note_mouse_movement
);
1800 dpyinfo
->last_mouse_motion_frame
= frame
;
1801 r
= &dpyinfo
->last_mouse_glyph
;
1803 /* Note, this doesn't get called for enter/leave, since we don't have a
1804 position. Those are taken care of in the corresponding NSView methods. */
1806 /* has movement gone beyond last rect we were tracking? */
1807 if (x
< r
->origin.x || x
>= r
->origin.x
+ r
->size.width
1808 || y
< r
->origin.y || y
>= r
->origin.y
+ r
->size.height
)
1810 ns_update_begin
(frame
);
1811 frame
->mouse_moved
= 1;
1812 note_mouse_highlight
(frame
, x
, y
);
1813 remember_mouse_glyph
(frame
, x
, y
, r
);
1814 ns_update_end
(frame
);
1823 ns_mouse_position
(struct frame
**fp
, int insist
, Lisp_Object
*bar_window
,
1824 enum scroll_bar_part
*part
, Lisp_Object
*x
, Lisp_Object
*y
,
1826 /* --------------------------------------------------------------------------
1827 External (hook): inform emacs about mouse position and hit parts.
1828 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1829 x & y should be position in the scrollbar (the whole bar, not the handle)
1830 and length of scrollbar respectively
1831 -------------------------------------------------------------------------- */
1835 Lisp_Object frame
, tail
;
1837 struct ns_display_info
*dpyinfo
;
1839 NSTRACE
(ns_mouse_position
);
1843 fprintf (stderr
, "Warning
: ns_mouse_position
() called with
null *fp.
\n"
);
1847 dpyinfo
= FRAME_DISPLAY_INFO
(*fp
);
1851 if (dpyinfo
->last_mouse_scroll_bar
!= nil
&& insist
== 0)
1853 /* TODO: we do not use this path at the moment because drag events will
1854 go directly to the EmacsScroller. Leaving code in for now. */
1855 [dpyinfo
->last_mouse_scroll_bar
1856 getMouseMotionPart
: (int
*)part window
: bar_window x
: x y
: y
];
1858 *time
= dpyinfo
->last_mouse_movement_time
;
1859 dpyinfo
->last_mouse_scroll_bar
= nil
;
1863 /* Clear the mouse-moved flag for every frame on this display. */
1864 FOR_EACH_FRAME
(tail
, frame
)
1865 if (FRAME_NS_P
(XFRAME
(frame
))
1866 && FRAME_NS_DISPLAY
(XFRAME
(frame
)) == FRAME_NS_DISPLAY
(*fp
))
1867 XFRAME
(frame
)->mouse_moved
= 0;
1869 dpyinfo
->last_mouse_scroll_bar
= nil
;
1870 if (dpyinfo
->last_mouse_frame
1871 && FRAME_LIVE_P
(dpyinfo
->last_mouse_frame
))
1872 f
= dpyinfo
->last_mouse_frame
;
1874 f
= dpyinfo
->x_focus_frame ? dpyinfo
->x_focus_frame
1875 : SELECTED_FRAME
();
1877 if (f
&& FRAME_NS_P
(f
))
1879 view = FRAME_NS_VIEW
(*fp
);
1881 position
= [[view window
] mouseLocationOutsideOfEventStream
];
1882 position
= [view convertPoint
: position fromView
: nil
];
1883 remember_mouse_glyph
(f
, position.x
, position.y
,
1884 &dpyinfo
->last_mouse_glyph
);
1885 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1887 if (bar_window
) *bar_window
= Qnil
;
1888 if (part
) *part
= 0; /*scroll_bar_handle; */
1890 if (x
) XSETINT
(*x
, lrint
(position.x
));
1891 if (y
) XSETINT
(*y
, lrint
(position.y
));
1893 *time
= dpyinfo
->last_mouse_movement_time
;
1903 ns_frame_up_to_date
(struct frame
*f
)
1904 /* --------------------------------------------------------------------------
1905 External (hook): Fix up mouse highlighting right after a full update.
1906 Can't use FRAME_MOUSE_UPDATE due to ns_frame_begin and ns_frame_end calls.
1907 -------------------------------------------------------------------------- */
1909 NSTRACE
(ns_frame_up_to_date
);
1913 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
1914 if (f
== hlinfo
->mouse_face_mouse_frame
)
1918 note_mouse_highlight
(hlinfo
->mouse_face_mouse_frame
,
1919 hlinfo
->mouse_face_mouse_x
,
1920 hlinfo
->mouse_face_mouse_y
);
1929 ns_define_frame_cursor
(struct frame
*f
, Cursor cursor
)
1930 /* --------------------------------------------------------------------------
1931 External (RIF): set frame mouse pointer type.
1932 -------------------------------------------------------------------------- */
1934 NSTRACE
(ns_define_frame_cursor
);
1935 if (FRAME_POINTER_TYPE
(f
) != cursor
)
1937 EmacsView
*view = FRAME_NS_VIEW
(f
);
1938 FRAME_POINTER_TYPE
(f
) = cursor
;
1939 [[view window
] invalidateCursorRectsForView
: view];
1940 /* Redisplay assumes this function also draws the changed frame
1941 cursor, but this function doesn't, so do it explicitly. */
1942 x_update_cursor
(f
, 1);
1948 /* ==========================================================================
1952 ========================================================================== */
1956 ns_convert_key
(unsigned code
)
1957 /* --------------------------------------------------------------------------
1958 Internal call used by NSView-keyDown.
1959 -------------------------------------------------------------------------- */
1961 const unsigned last_keysym
= (sizeof
(convert_ns_to_X_keysym
)
1962 / sizeof
(convert_ns_to_X_keysym
[0]));
1964 /* An array would be faster, but less easy to read. */
1965 for (keysym
= 0; keysym
< last_keysym
; keysym
+= 2)
1966 if (code
== convert_ns_to_X_keysym
[keysym
])
1967 return 0xFF00 | convert_ns_to_X_keysym
[keysym
+1];
1969 /* if decide to use keyCode and Carbon table, use this line:
1970 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1975 x_get_keysym_name
(int keysym
)
1976 /* --------------------------------------------------------------------------
1977 Called by keyboard.c. Not sure if the return val is important, except
1979 -------------------------------------------------------------------------- */
1981 static
char value
[16];
1982 NSTRACE
(x_get_keysym_name
);
1983 sprintf (value
, "
%d", keysym);
1989 /* ==========================================================================
1991 Block drawing operations
1993 ========================================================================== */
1997 ns_redraw_scroll_bars
(struct frame
*f
)
2001 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
2002 NSTRACE
(ns_redraw_scroll_bars
);
2003 for (i
=[subviews count
]-1; i
>= 0; i
--)
2005 view = [subviews objectAtIndex
: i
];
2006 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
2013 ns_clear_frame
(struct frame
*f
)
2014 /* --------------------------------------------------------------------------
2015 External (hook): Erase the entire frame
2016 -------------------------------------------------------------------------- */
2018 NSView
*view = FRAME_NS_VIEW
(f
);
2021 NSTRACE
(ns_clear_frame
);
2023 /* comes on initial frame because we have
2024 after-make-frame-functions = select-frame */
2025 if (!FRAME_DEFAULT_FACE
(f
))
2028 mark_window_cursors_off
(XWINDOW
(FRAME_ROOT_WINDOW
(f
)));
2033 ns_focus
(f
, &r
, 1);
2034 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(FRAME_DEFAULT_FACE
(f
)), f
) set];
2038 /* as of 2006/11 or so this is now needed */
2039 ns_redraw_scroll_bars
(f
);
2045 ns_clear_frame_area
(struct frame
*f
, int x
, int y
, int width
, int height
)
2046 /* --------------------------------------------------------------------------
2047 External (RIF): Clear section of frame
2048 -------------------------------------------------------------------------- */
2050 NSRect r
= NSMakeRect
(x
, y
, width
, height
);
2051 NSView
*view = FRAME_NS_VIEW
(f
);
2052 struct face
*face
= FRAME_DEFAULT_FACE
(f
);
2057 NSTRACE
(ns_clear_frame_area
);
2059 r
= NSIntersectionRect
(r
, [view frame
]);
2060 ns_focus
(f
, &r
, 1);
2061 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), f
) set];
2071 ns_scroll_run
(struct window
*w
, struct run
*run
)
2072 /* --------------------------------------------------------------------------
2073 External (RIF): Insert or delete n lines at line vpos
2074 -------------------------------------------------------------------------- */
2076 struct frame
*f
= XFRAME
(w
->frame
);
2077 int x
, y
, width
, height
, from_y
, to_y
, bottom_y
;
2079 NSTRACE
(ns_scroll_run
);
2081 /* begin copy from other terms */
2082 /* Get frame-relative bounding box of the text display area of W,
2083 without mode lines. Include in this box the left and right
2085 window_box
(w
, ANY_AREA
, &x
, &y
, &width
, &height
);
2087 from_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->current_y
);
2088 to_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->desired_y
);
2089 bottom_y
= y
+ height
;
2093 /* Scrolling up. Make sure we don't copy part of the mode
2094 line at the bottom. */
2095 if (from_y
+ run
->height
> bottom_y
)
2096 height
= bottom_y
- from_y
;
2098 height
= run
->height
;
2102 /* Scrolling down. Make sure we don't copy over the mode line.
2104 if (to_y
+ run
->height
> bottom_y
)
2105 height
= bottom_y
- to_y
;
2107 height
= run
->height
;
2109 /* end copy from other terms */
2119 NSRect srcRect
= NSMakeRect
(x
, from_y
, width
, height
);
2120 NSRect dstRect
= NSMakeRect
(x
, to_y
, width
, height
);
2121 NSPoint dstOrigin
= NSMakePoint
(x
, to_y
);
2123 ns_focus
(f
, &dstRect
, 1);
2124 NSCopyBits
(0, srcRect
, dstOrigin
);
2133 ns_after_update_window_line
(struct window
*w
, struct glyph_row
*desired_row
)
2134 /* --------------------------------------------------------------------------
2135 External (RIF): preparatory to fringe update after text was updated
2136 -------------------------------------------------------------------------- */
2141 NSTRACE
(ns_after_update_window_line
);
2143 /* begin copy from other terms */
2146 if (!desired_row
->mode_line_p
&& !w
->pseudo_window_p
)
2147 desired_row
->redraw_fringe_bitmaps_p
= 1;
2149 /* When a window has disappeared, make sure that no rest of
2150 full-width rows stays visible in the internal border. */
2151 if (windows_or_buffers_changed
2152 && desired_row
->full_width_p
2153 && (f
= XFRAME
(w
->frame
),
2154 width
= FRAME_INTERNAL_BORDER_WIDTH
(f
),
2156 && (height
= desired_row
->visible_height
,
2159 int y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, desired_row
->y
));
2162 ns_clear_frame_area
(f
, 0, y
, width
, height
);
2163 ns_clear_frame_area
(f
,
2164 FRAME_PIXEL_WIDTH
(f
) - width
,
2172 ns_shift_glyphs_for_insert
(struct frame
*f
,
2173 int x
, int y
, int width
, int height
,
2175 /* --------------------------------------------------------------------------
2176 External (RIF): copy an area horizontally, don't worry about clearing src
2177 -------------------------------------------------------------------------- */
2179 NSRect srcRect
= NSMakeRect
(x
, y
, width
, height
);
2180 NSRect dstRect
= NSMakeRect
(x
+shift_by
, y
, width
, height
);
2181 NSPoint dstOrigin
= dstRect.origin
;
2183 NSTRACE
(ns_shift_glyphs_for_insert
);
2185 ns_focus
(f
, &dstRect
, 1);
2186 NSCopyBits
(0, srcRect
, dstOrigin
);
2192 /* ==========================================================================
2194 Character encoding and metrics
2196 ========================================================================== */
2200 ns_compute_glyph_string_overhangs
(struct glyph_string
*s
)
2201 /* --------------------------------------------------------------------------
2202 External (RIF); compute left/right overhang of whole string and set in s
2203 -------------------------------------------------------------------------- */
2205 struct font
*font
= s
->font
;
2209 struct font_metrics metrics
;
2210 unsigned int codes
[2];
2211 codes
[0] = *(s
->char2b
);
2212 codes
[1] = *(s
->char2b
+ s
->nchars
- 1);
2214 font
->driver
->text_extents
(font
, codes
, 2, &metrics
);
2215 s
->left_overhang
= -metrics.lbearing
;
2217 = metrics.rbearing
> metrics.width
2218 ? metrics.rbearing
- metrics.width
: 0;
2222 s
->left_overhang
= 0;
2223 if (EQ
(font
->driver
->type, Qns
))
2224 s
->right_overhang
= ((struct nsfont_info
*)font
)->ital ?
2225 FONT_HEIGHT
(font
) * 0.2 : 0;
2227 s
->right_overhang
= 0;
2233 /* ==========================================================================
2235 Fringe and cursor drawing
2237 ========================================================================== */
2240 extern int max_used_fringe_bitmap
;
2242 ns_draw_fringe_bitmap
(struct window
*w
, struct glyph_row
*row
,
2243 struct draw_fringe_bitmap_params
*p
)
2244 /* --------------------------------------------------------------------------
2245 External (RIF); fringe-related
2246 -------------------------------------------------------------------------- */
2248 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2249 struct face
*face
= p
->face
;
2250 static EmacsImage
**bimgs
= NULL;
2251 static int nBimgs
= 0;
2253 /* grow bimgs if needed */
2254 if (nBimgs
< max_used_fringe_bitmap
)
2256 bimgs
= xrealloc
(bimgs
, max_used_fringe_bitmap
* sizeof
*bimgs
);
2257 memset
(bimgs
+ nBimgs
, 0,
2258 (max_used_fringe_bitmap
- nBimgs
) * sizeof
*bimgs
);
2259 nBimgs
= max_used_fringe_bitmap
;
2262 /* Must clip because of partially visible lines. */
2263 ns_clip_to_row
(w
, row
, ANY_AREA
, YES
);
2267 int bx
= p
->bx
, by
= p
->by
, nx
= p
->nx
, ny
= p
->ny
;
2269 /* If the fringe is adjacent to the left (right) scroll bar of a
2270 leftmost (rightmost, respectively) window, then extend its
2271 background to the gap between the fringe and the bar. */
2272 if ((WINDOW_LEFTMOST_P
(w
)
2273 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT
(w
))
2274 ||
(WINDOW_RIGHTMOST_P
(w
)
2275 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT
(w
)))
2277 int sb_width
= WINDOW_CONFIG_SCROLL_BAR_WIDTH
(w
);
2281 int bar_area_x
= WINDOW_SCROLL_BAR_AREA_X
(w
);
2282 int bar_area_width
= (WINDOW_CONFIG_SCROLL_BAR_COLS
(w
)
2283 * FRAME_COLUMN_WIDTH
(f
));
2287 /* Bitmap fills the fringe. */
2288 if (bar_area_x
+ bar_area_width
== p
->x
)
2289 bx
= bar_area_x
+ sb_width
;
2290 else if (p
->x
+ p
->wd
== bar_area_x
)
2294 int header_line_height
= WINDOW_HEADER_LINE_HEIGHT
(w
);
2296 nx
= bar_area_width
- sb_width
;
2297 by
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (header_line_height
,
2299 ny
= row
->visible_height
;
2304 if (bar_area_x
+ bar_area_width
== bx
)
2306 bx
= bar_area_x
+ sb_width
;
2307 nx
+= bar_area_width
- sb_width
;
2309 else if (bx
+ nx
== bar_area_x
)
2310 nx
+= bar_area_width
- sb_width
;
2315 if (bx
>= 0 && nx
> 0)
2317 NSRect r
= NSMakeRect
(bx
, by
, nx
, ny
);
2319 [ns_lookup_indexed_color
(face
->background
, f
) set];
2326 NSRect r
= NSMakeRect
(p
->x
, p
->y
, p
->wd
, p
->h
);
2327 EmacsImage
*img
= bimgs
[p
->which - 1];
2331 unsigned short
*bits
= p
->bits
+ p
->dh
;
2334 unsigned
char *cbits
= xmalloc
(len
);
2336 for (i
= 0; i
< len
; i
++)
2337 cbits
[i
] = ~
(bits
[i
] & 0xff);
2338 img
= [[EmacsImage alloc
] initFromXBM
: cbits width
: 8 height
: p
->h
2340 bimgs
[p
->which - 1] = img
;
2345 /* Since we composite the bitmap instead of just blitting it, we need
2346 to erase the whole background. */
2347 [ns_lookup_indexed_color
(face
->background
, f
) set];
2349 [img setXBMColor
: ns_lookup_indexed_color
(face
->foreground
, f
)];
2350 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
2352 fromRect
: NSZeroRect
2353 operation
: NSCompositeSourceOver
2359 NSPoint pt
= r.origin
;
2361 [img compositeToPoint
: pt operation
: NSCompositeSourceOver
];
2370 ns_draw_window_cursor
(struct window
*w
, struct glyph_row
*glyph_row
,
2371 int x
, int y
, enum text_cursor_kinds cursor_type
,
2372 int cursor_width
, bool on_p
, bool active_p
)
2373 /* --------------------------------------------------------------------------
2374 External call (RIF): draw cursor.
2375 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
2376 -------------------------------------------------------------------------- */
2379 int fx
, fy
, h
, cursor_height
;
2380 struct frame
*f
= WINDOW_XFRAME
(w
);
2381 struct glyph
*phys_cursor_glyph
;
2382 struct glyph
*cursor_glyph
;
2384 NSColor
*hollow_color
= FRAME_BACKGROUND_COLOR
(f
);
2386 /* If cursor is out of bounds, don't draw garbage. This can happen
2387 in mini-buffer windows when switching between echo area glyphs
2390 NSTRACE
(dumpcursor
);
2395 w
->phys_cursor_type
= cursor_type
;
2396 w
->phys_cursor_on_p
= on_p
;
2398 if (cursor_type
== NO_CURSOR
)
2400 w
->phys_cursor_width
= 0;
2404 if ((phys_cursor_glyph
= get_phys_cursor_glyph
(w
)) == NULL)
2406 if (glyph_row
->exact_window_width_line_p
2407 && w
->phys_cursor.hpos
>= glyph_row
->used
[TEXT_AREA
])
2409 glyph_row
->cursor_in_fringe_p
= 1;
2410 draw_fringe_bitmap
(w
, glyph_row
, 0);
2415 /* We draw the cursor (with NSRectFill), then draw the glyph on top
2416 (other terminals do it the other way round). We must set
2417 w->phys_cursor_width to the cursor width. For bar cursors, that
2418 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
2419 get_phys_cursor_geometry
(w
, glyph_row
, phys_cursor_glyph
, &fx
, &fy
, &h
);
2421 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
2422 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
2423 if (cursor_type
== BAR_CURSOR
)
2425 if (cursor_width
< 1)
2426 cursor_width
= max (FRAME_CURSOR_WIDTH
(f
), 1);
2427 w
->phys_cursor_width
= cursor_width
;
2429 /* If we have an HBAR, "cursor_width" MAY specify height. */
2430 else if (cursor_type
== HBAR_CURSOR
)
2432 cursor_height
= (cursor_width
< 1) ? lrint
(0.25 * h
) : cursor_width
;
2433 fy
+= h
- cursor_height
;
2437 r.origin.x
= fx
, r.origin.y
= fy
;
2439 r.
size.width
= w
->phys_cursor_width
;
2441 /* TODO: only needed in rare cases with last-resort font in HELLO..
2442 should we do this more efficiently? */
2443 ns_clip_to_row
(w
, glyph_row
, ANY_AREA
, NO
); /* do ns_focus(f, &r, 1); if remove */
2446 face
= FACE_FROM_ID
(f
, phys_cursor_glyph
->face_id
);
2447 if (face
&& NS_FACE_BACKGROUND
(face
)
2448 == ns_index_color
(FRAME_CURSOR_COLOR
(f
), f
))
2450 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), f
) set];
2451 hollow_color
= FRAME_CURSOR_COLOR
(f
);
2454 [FRAME_CURSOR_COLOR
(f
) set];
2456 #ifdef NS_IMPL_COCOA
2457 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2458 atomic. Cleaner ways of doing this should be investigated.
2459 One way would be to set a global variable DRAWING_CURSOR
2460 when making the call to draw_phys..(), don't focus in that
2461 case, then move the ns_unfocus() here after that call. */
2462 NSDisableScreenUpdates
();
2465 switch (cursor_type
)
2469 case FILLED_BOX_CURSOR
:
2472 case HOLLOW_BOX_CURSOR
:
2475 NSRectFill
(NSInsetRect
(r
, 1, 1));
2476 [FRAME_CURSOR_COLOR
(f
) set];
2483 /* If the character under cursor is R2L, draw the bar cursor
2484 on the right of its glyph, rather than on the left. */
2485 cursor_glyph
= get_phys_cursor_glyph
(w
);
2486 if ((cursor_glyph
->resolved_level
& 1) != 0)
2487 s.origin.x
+= cursor_glyph
->pixel_width
- s.
size.width
;
2494 /* draw the character under the cursor */
2495 if (cursor_type
!= NO_CURSOR
)
2496 draw_phys_cursor_glyph
(w
, glyph_row
, DRAW_CURSOR
);
2498 #ifdef NS_IMPL_COCOA
2499 NSEnableScreenUpdates
();
2506 ns_draw_vertical_window_border
(struct window
*w
, int x
, int y0
, int y1
)
2507 /* --------------------------------------------------------------------------
2508 External (RIF): Draw a vertical line.
2509 -------------------------------------------------------------------------- */
2511 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2513 NSRect r
= NSMakeRect
(x
, y0
, 1, y1
-y0
);
2515 NSTRACE
(ns_draw_vertical_window_border
);
2517 face
= FACE_FROM_ID
(f
, VERTICAL_BORDER_FACE_ID
);
2519 [ns_lookup_indexed_color
(face
->foreground
, f
) set];
2521 ns_focus
(f
, &r
, 1);
2528 ns_draw_window_divider
(struct window
*w
, int x0
, int x1
, int y0
, int y1
)
2529 /* --------------------------------------------------------------------------
2530 External (RIF): Draw a window divider.
2531 -------------------------------------------------------------------------- */
2533 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2535 NSRect r
= NSMakeRect
(x0
, y0
, x1
-x0
, y1
-y0
);
2537 NSTRACE
(ns_draw_window_divider
);
2539 face
= FACE_FROM_ID
(f
, WINDOW_DIVIDER_FACE_ID
);
2541 [ns_lookup_indexed_color
(face
->foreground
, f
) set];
2543 ns_focus
(f
, &r
, 1);
2550 show_hourglass
(struct atimer
*timer
)
2552 if (hourglass_shown_p
)
2557 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2559 hourglass_shown_p
= 1;
2565 hide_hourglass
(void
)
2567 if (!hourglass_shown_p
)
2572 /* TODO: remove NSProgressIndicator from all frames */
2574 hourglass_shown_p
= 0;
2580 /* ==========================================================================
2582 Glyph drawing operations
2584 ========================================================================== */
2587 ns_get_glyph_string_clip_rect
(struct glyph_string
*s
, NativeRectangle
*nr
)
2588 /* --------------------------------------------------------------------------
2589 Wrapper utility to account for internal border width on full-width lines,
2590 and allow top full-width rows to hit the frame top. nr should be pointer
2591 to two successive NSRects. Number of rects actually used is returned.
2592 -------------------------------------------------------------------------- */
2594 int n
= get_glyph_string_clip_rects
(s
, nr
, 2);
2598 /* --------------------------------------------------------------------
2599 Draw a wavy line under glyph string s. The wave fills wave_height
2606 wave_height = 3 | * * * *
2607 --------------------------------------------------------------------- */
2610 ns_draw_underwave
(struct glyph_string
*s
, EmacsCGFloat width
, EmacsCGFloat x
)
2612 int wave_height
= 3, wave_length
= 2;
2613 int y
, dx
, dy
, odd
, xmax
;
2618 dy
= wave_height
- 1;
2619 y
= s
->ybase
- wave_height
+ 3;
2622 /* Find and set clipping rectangle */
2623 waveClip
= NSMakeRect
(x
, y
, width
, wave_height
);
2624 [[NSGraphicsContext currentContext
] saveGraphicsState
];
2625 NSRectClip
(waveClip
);
2627 /* Draw the waves */
2628 a.x
= x
- ((int
)(x
) % dx) + (EmacsCGFloat) 0.5;
2630 odd
= (int
)(a.x
/dx
) % 2;
2631 a.y
= b.y
= y
+ 0.5;
2640 [NSBezierPath strokeLineFromPoint
:a toPoint
:b
];
2641 a.x
= b.x
, a.y
= b.y
;
2642 b.x
+= dx
, b.y
= y
+ 0.5 + odd
*dy
;
2646 /* Restore previous clipping rectangle(s) */
2647 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
2653 ns_draw_text_decoration
(struct glyph_string
*s
, struct face
*face
,
2654 NSColor
*defaultCol
, CGFloat width
, CGFloat x
)
2655 /* --------------------------------------------------------------------------
2656 Draw underline, overline, and strike-through on glyph string s.
2657 -------------------------------------------------------------------------- */
2659 if (s
->for_overlaps
)
2663 if (face
->underline_p
)
2665 if (s
->face
->underline_type
== FACE_UNDER_WAVE
)
2667 if (face
->underline_defaulted_p
)
2670 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2672 ns_draw_underwave
(s
, width
, x
);
2674 else if (s
->face
->underline_type
== FACE_UNDER_LINE
)
2678 unsigned
long thickness
, position
;
2680 /* If the prev was underlined, match its appearance. */
2681 if (s
->prev
&& s
->prev
->face
->underline_p
2682 && s
->prev
->face
->underline_type
== FACE_UNDER_LINE
2683 && s
->prev
->underline_thickness
> 0)
2685 thickness
= s
->prev
->underline_thickness
;
2686 position
= s
->prev
->underline_position
;
2691 unsigned
long descent
;
2694 descent
= s
->y
+ s
->height
- s
->ybase
;
2696 /* Use underline thickness of font, defaulting to 1. */
2697 thickness
= (font
&& font
->underline_thickness
> 0)
2698 ? font
->underline_thickness
: 1;
2700 /* Determine the offset of underlining from the baseline. */
2701 if (x_underline_at_descent_line
)
2702 position
= descent
- thickness
;
2703 else if (x_use_underline_position_properties
2704 && font
&& font
->underline_position
>= 0)
2705 position
= font
->underline_position
;
2707 position
= lround
(font
->descent
/ 2);
2709 position
= underline_minimum_offset
;
2711 position
= max (position
, underline_minimum_offset
);
2713 /* Ensure underlining is not cropped. */
2714 if (descent
<= position
)
2716 position
= descent
- 1;
2719 else if (descent
< position
+ thickness
)
2723 s
->underline_thickness
= thickness
;
2724 s
->underline_position
= position
;
2726 r
= NSMakeRect
(x
, s
->ybase
+ position
, width
, thickness
);
2728 if (face
->underline_defaulted_p
)
2731 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2735 /* Do overline. We follow other terms in using a thickness of 1
2736 and ignoring overline_margin. */
2737 if (face
->overline_p
)
2740 r
= NSMakeRect
(x
, s
->y
, width
, 1);
2742 if (face
->overline_color_defaulted_p
)
2745 [ns_lookup_indexed_color
(face
->overline_color
, s
->f
) set];
2749 /* Do strike-through. We follow other terms for thickness and
2750 vertical position.*/
2751 if (face
->strike_through_p
)
2756 dy
= lrint
((s
->height
- 1) / 2);
2757 r
= NSMakeRect
(x
, s
->y
+ dy
, width
, 1);
2759 if (face
->strike_through_color_defaulted_p
)
2762 [ns_lookup_indexed_color
(face
->strike_through_color
, s
->f
) set];
2768 ns_draw_box
(NSRect r
, CGFloat thickness
, NSColor
*col
,
2769 char left_p
, char right_p
)
2770 /* --------------------------------------------------------------------------
2771 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2772 Note we can't just use an NSDrawRect command, because of the possibility
2773 of some sides not being drawn, and because the rect will be filled.
2774 -------------------------------------------------------------------------- */
2780 s.
size.height
= thickness
;
2782 s.origin.y
+= r.
size.height
- thickness
;
2785 s.
size.height
= r.
size.height
;
2786 s.origin.y
= r.origin.y
;
2788 /* left, right (optional) */
2789 s.
size.width
= thickness
;
2794 s.origin.x
+= r.
size.width
- thickness
;
2801 ns_draw_relief
(NSRect r
, int thickness
, char raised_p
,
2802 char top_p
, char bottom_p
, char left_p
, char right_p
,
2803 struct glyph_string
*s
)
2804 /* --------------------------------------------------------------------------
2805 Draw a relief rect inside r, optionally leaving some sides open.
2806 Note we can't just use an NSDrawBezel command, because of the possibility
2807 of some sides not being drawn, and because the rect will be filled.
2808 -------------------------------------------------------------------------- */
2810 static NSColor
*baseCol
= nil
, *lightCol
= nil
, *darkCol
= nil
;
2811 NSColor
*newBaseCol
= nil
;
2814 NSTRACE
(ns_draw_relief
);
2818 if (s
->face
->use_box_color_for_shadows_p
)
2820 newBaseCol
= ns_lookup_indexed_color
(s
->face
->box_color
, s
->f
);
2822 /* else if (s->first_glyph->type == IMAGE_GLYPH
2824 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2826 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2830 newBaseCol
= ns_lookup_indexed_color
(s
->face
->background
, s
->f
);
2833 if (newBaseCol
== nil
)
2834 newBaseCol
= [NSColor grayColor
];
2836 if (newBaseCol
!= baseCol
) /* TODO: better check */
2839 baseCol
= [newBaseCol retain
];
2841 lightCol
= [[baseCol highlightWithLevel
: 0.2] retain
];
2843 darkCol
= [[baseCol shadowWithLevel
: 0.3] retain
];
2846 [(raised_p ? lightCol
: darkCol
) set];
2848 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2851 sr.
size.height
= thickness
;
2852 if (top_p
) NSRectFill
(sr
);
2855 sr.
size.height
= r.
size.height
;
2856 sr.
size.width
= thickness
;
2857 if (left_p
) NSRectFill
(sr
);
2859 [(raised_p ? darkCol
: lightCol
) set];
2862 sr.
size.width
= r.
size.width
;
2863 sr.
size.height
= thickness
;
2864 sr.origin.y
+= r.
size.height
- thickness
;
2865 if (bottom_p
) NSRectFill
(sr
);
2868 sr.
size.height
= r.
size.height
;
2869 sr.origin.y
= r.origin.y
;
2870 sr.
size.width
= thickness
;
2871 sr.origin.x
+= r.
size.width
- thickness
;
2872 if (right_p
) NSRectFill
(sr
);
2877 ns_dumpglyphs_box_or_relief
(struct glyph_string
*s
)
2878 /* --------------------------------------------------------------------------
2879 Function modeled after x_draw_glyph_string_box ().
2880 Sets up parameters for drawing.
2881 -------------------------------------------------------------------------- */
2883 int right_x
, last_x
;
2884 char left_p
, right_p
;
2885 struct glyph
*last_glyph
;
2890 if (s
->hl
== DRAW_MOUSE_FACE
)
2892 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2894 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2899 thickness
= face
->box_line_width
;
2901 NSTRACE
(ns_dumpglyphs_box_or_relief
);
2903 last_x
= ((s
->row
->full_width_p
&& !s
->w
->pseudo_window_p
)
2904 ? WINDOW_RIGHT_EDGE_X
(s
->w
)
2905 : window_box_right
(s
->w
, s
->area
));
2906 last_glyph
= (s
->cmp || s
->img
2907 ? s
->first_glyph
: s
->first_glyph
+ s
->nchars
-1);
2909 right_x
= ((s
->row
->full_width_p
&& s
->extends_to_end_of_line_p
2910 ? last_x
- 1 : min (last_x
, s
->x
+ s
->background_width
) - 1));
2912 left_p
= (s
->first_glyph
->left_box_line_p
2913 ||
(s
->hl
== DRAW_MOUSE_FACE
2914 && (s
->prev
== NULL || s
->prev
->hl
!= s
->hl
)));
2915 right_p
= (last_glyph
->right_box_line_p
2916 ||
(s
->hl
== DRAW_MOUSE_FACE
2917 && (s
->next
== NULL || s
->next
->hl
!= s
->hl
)));
2919 r
= NSMakeRect
(s
->x
, s
->y
, right_x
- s
->x
+ 1, s
->height
);
2921 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
2922 if (s
->face
->box
== FACE_SIMPLE_BOX
&& s
->face
->box_color
)
2924 ns_draw_box
(r
, abs (thickness
),
2925 ns_lookup_indexed_color
(face
->box_color
, s
->f
),
2930 ns_draw_relief
(r
, abs (thickness
), s
->face
->box
== FACE_RAISED_BOX
,
2931 1, 1, left_p
, right_p
, s
);
2937 ns_maybe_dumpglyphs_background
(struct glyph_string
*s
, char force_p
)
2938 /* --------------------------------------------------------------------------
2939 Modeled after x_draw_glyph_string_background, which draws BG in
2940 certain cases. Others are left to the text rendering routine.
2941 -------------------------------------------------------------------------- */
2943 NSTRACE
(ns_maybe_dumpglyphs_background
);
2945 if (!s
->background_filled_p
/* || s->hl == DRAW_MOUSE_FACE*/)
2947 int box_line_width
= max (s
->face
->box_line_width
, 0);
2948 if (FONT_HEIGHT
(s
->font
) < s
->height
- 2 * box_line_width
2949 || s
->font_not_found_p || s
->extends_to_end_of_line_p || force_p
)
2952 if (s
->hl
== DRAW_MOUSE_FACE
)
2954 face
= FACE_FROM_ID
(s
->f
,
2955 MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2957 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2960 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2962 [(NS_FACE_BACKGROUND
(face
) != 0
2963 ? ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
)
2964 : FRAME_BACKGROUND_COLOR
(s
->f
)) set];
2967 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(s
->f
);
2968 [[dpyinfo
->bitmaps
[face
->stipple
-1].img stippleMask
] set];
2971 if (s
->hl
!= DRAW_CURSOR
)
2973 NSRect r
= NSMakeRect
(s
->x
, s
->y
+ box_line_width
,
2974 s
->background_width
,
2975 s
->height
-2*box_line_width
);
2979 s
->background_filled_p
= 1;
2986 ns_dumpglyphs_image
(struct glyph_string
*s
, NSRect r
)
2987 /* --------------------------------------------------------------------------
2988 Renders an image and associated borders.
2989 -------------------------------------------------------------------------- */
2991 EmacsImage
*img
= s
->img
->pixmap
;
2992 int box_line_vwidth
= max (s
->face
->box_line_width
, 0);
2993 int x
= s
->x
, y
= s
->ybase
- image_ascent
(s
->img
, s
->face
, &s
->slice);
2994 int bg_x
, bg_y
, bg_height
;
3001 NSTRACE
(ns_dumpglyphs_image
);
3003 if (s
->face
->box
!= FACE_NO_BOX
3004 && s
->first_glyph
->left_box_line_p
&& s
->slice.x
== 0)
3005 x
+= abs (s
->face
->box_line_width
);
3008 bg_y
= s
->slice.y
== 0 ? s
->y
: s
->y
+ box_line_vwidth
;
3009 bg_height
= s
->height
;
3010 /* other terms have this, but was causing problems w/tabbar mode */
3011 /* - 2 * box_line_vwidth; */
3013 if (s
->slice.x
== 0) x
+= s
->img
->hmargin
;
3014 if (s
->slice.y
== 0) y
+= s
->img
->vmargin
;
3016 /* Draw BG: if we need larger area than image itself cleared, do that,
3017 otherwise, since we composite the image under NS (instead of mucking
3018 with its background color), we must clear just the image area. */
3019 if (s
->hl
== DRAW_MOUSE_FACE
)
3021 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
3023 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
3026 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
3028 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
) set];
3030 if (bg_height
> s
->slice.height || s
->img
->hmargin || s
->img
->vmargin
3031 || s
->img
->mask || s
->img
->pixmap
== 0 || s
->width
!= s
->background_width
)
3033 br
= NSMakeRect
(bg_x
, bg_y
, s
->background_width
, bg_height
);
3034 s
->background_filled_p
= 1;
3038 br
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
3043 /* Draw the image.. do we need to draw placeholder if img ==nil? */
3046 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
3047 NSRect dr
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
3048 NSRect ir
= NSMakeRect
(s
->slice.x
, s
->slice.y
,
3049 s
->slice.width
, s
->slice.height
);
3052 operation
: NSCompositeSourceOver
3057 [img compositeToPoint
: NSMakePoint
(x
, y
+ s
->slice.height
)
3058 operation
: NSCompositeSourceOver
];
3062 if (s
->hl
== DRAW_CURSOR
)
3064 [FRAME_CURSOR_COLOR
(s
->f
) set];
3065 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3066 tdCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3068 /* Currently on NS img->mask is always 0. Since
3069 get_window_cursor_type specifies a hollow box cursor when on
3070 a non-masked image we never reach this clause. But we put it
3071 in in anticipation of better support for image masks on
3073 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3077 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3080 /* Draw underline, overline, strike-through. */
3081 ns_draw_text_decoration
(s
, face
, tdCol
, br.
size.width
, br.origin.x
);
3083 /* Draw relief, if requested */
3084 if (s
->img
->relief || s
->hl
==DRAW_IMAGE_RAISED || s
->hl
==DRAW_IMAGE_SUNKEN
)
3086 if (s
->hl
== DRAW_IMAGE_SUNKEN || s
->hl
== DRAW_IMAGE_RAISED
)
3088 th
= tool_bar_button_relief
>= 0 ?
3089 tool_bar_button_relief
: DEFAULT_TOOL_BAR_BUTTON_RELIEF
;
3090 raised_p
= (s
->hl
== DRAW_IMAGE_RAISED
);
3094 th
= abs (s
->img
->relief
);
3095 raised_p
= (s
->img
->relief
> 0);
3098 r.origin.x
= x
- th
;
3099 r.origin.y
= y
- th
;
3100 r.
size.width
= s
->slice.width
+ 2*th
-1;
3101 r.
size.height
= s
->slice.height
+ 2*th
-1;
3102 ns_draw_relief
(r
, th
, raised_p
,
3104 s
->slice.y
+ s
->slice.height
== s
->img
->height
,
3106 s
->slice.x
+ s
->slice.width
== s
->img
->width
, s
);
3109 /* If there is no mask, the background won't be seen,
3110 so draw a rectangle on the image for the cursor.
3111 Do this for all images, getting transparency right is not reliable. */
3112 if (s
->hl
== DRAW_CURSOR
)
3114 int thickness
= abs (s
->img
->relief
);
3115 if (thickness
== 0) thickness
= 1;
3116 ns_draw_box
(br
, thickness
, FRAME_CURSOR_COLOR
(s
->f
), 1, 1);
3122 ns_dumpglyphs_stretch
(struct glyph_string
*s
)
3127 NSColor
*fgCol
, *bgCol
;
3129 if (!s
->background_filled_p
)
3131 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3132 *r
= NSMakeRect
(s
->x
, s
->y
, s
->background_width
, s
->height
);
3134 ns_focus
(s
->f
, r
, n
);
3136 if (s
->hl
== DRAW_MOUSE_FACE
)
3138 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
3140 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
3143 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
3145 bgCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3146 fgCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3148 for (i
= 0; i
< n
; ++i
)
3150 if (!s
->row
->full_width_p
)
3152 int overrun
, leftoverrun
;
3154 /* truncate to avoid overwriting fringe and/or scrollbar */
3155 overrun
= max (0, (s
->x
+ s
->background_width
)
3156 - (WINDOW_BOX_RIGHT_EDGE_X
(s
->w
)
3157 - WINDOW_RIGHT_FRINGE_WIDTH
(s
->w
)));
3158 r
[i
].
size.width
-= overrun
;
3160 /* truncate to avoid overwriting to left of the window box */
3161 leftoverrun
= (WINDOW_BOX_LEFT_EDGE_X
(s
->w
)
3162 + WINDOW_LEFT_FRINGE_WIDTH
(s
->w
)) - s
->x
;
3164 if (leftoverrun
> 0)
3166 r
[i
].origin.x
+= leftoverrun
;
3167 r
[i
].
size.width
-= leftoverrun
;
3170 /* XXX: Try to work between problem where a stretch glyph on
3171 a partially-visible bottom row will clear part of the
3172 modeline, and another where list-buffers headers and similar
3173 rows erroneously have visible_height set to 0. Not sure
3174 where this is coming from as other terms seem not to show. */
3175 r
[i
].
size.height
= min (s
->height
, s
->row
->visible_height
);
3180 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3181 overwriting cursor (usually when cursor on a tab) */
3182 if (s
->hl
== DRAW_CURSOR
)
3187 width
= s
->w
->phys_cursor_width
;
3188 r
[i
].
size.width
-= width
;
3189 r
[i
].origin.x
+= width
;
3193 /* Draw overlining, etc. on the cursor. */
3194 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3195 ns_draw_text_decoration
(s
, face
, bgCol
, width
, x
);
3197 ns_draw_text_decoration
(s
, face
, fgCol
, width
, x
);
3204 /* Draw overlining, etc. on the stretch glyph (or the part
3205 of the stretch glyph after the cursor). */
3206 ns_draw_text_decoration
(s
, face
, fgCol
, r
[i
].
size.width
,
3210 s
->background_filled_p
= 1;
3216 ns_draw_glyph_string
(struct glyph_string
*s
)
3217 /* --------------------------------------------------------------------------
3218 External (RIF): Main draw-text call.
3219 -------------------------------------------------------------------------- */
3221 /* TODO (optimize): focus for box and contents draw */
3224 char box_drawn_p
= 0;
3225 struct font
*font
= s
->face
->font
;
3226 if (! font
) font
= FRAME_FONT
(s
->f
);
3228 NSTRACE
(ns_draw_glyph_string
);
3230 if (s
->next
&& s
->right_overhang
&& !s
->for_overlaps
/*&&s->hl!=DRAW_CURSOR*/)
3233 struct glyph_string
*next
;
3235 for (width
= 0, next
= s
->next
;
3236 next
&& width
< s
->right_overhang
;
3237 width
+= next
->width
, next
= next
->next
)
3238 if (next
->first_glyph
->type != IMAGE_GLYPH
)
3240 if (next
->first_glyph
->type != STRETCH_GLYPH
)
3242 n
= ns_get_glyph_string_clip_rect
(s
->next
, r
);
3243 ns_focus
(s
->f
, r
, n
);
3244 ns_maybe_dumpglyphs_background
(s
->next
, 1);
3249 ns_dumpglyphs_stretch
(s
->next
);
3251 next
->num_clips
= 0;
3255 if (!s
->for_overlaps
&& s
->face
->box
!= FACE_NO_BOX
3256 && (s
->first_glyph
->type == CHAR_GLYPH
3257 || s
->first_glyph
->type == COMPOSITE_GLYPH
))
3259 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3260 ns_focus
(s
->f
, r
, n
);
3261 ns_maybe_dumpglyphs_background
(s
, 1);
3262 ns_dumpglyphs_box_or_relief
(s
);
3267 switch (s
->first_glyph
->type)
3271 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3272 ns_focus
(s
->f
, r
, n
);
3273 ns_dumpglyphs_image
(s
, r
[0]);
3278 ns_dumpglyphs_stretch
(s
);
3282 case COMPOSITE_GLYPH
:
3283 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3284 ns_focus
(s
->f
, r
, n
);
3286 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3287 && ! s
->first_glyph
->u.cmp.automatic
))
3288 s
->background_filled_p
= 1;
3290 ns_maybe_dumpglyphs_background
3291 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3293 flags
= s
->hl
== DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR
:
3294 (s
->hl
== DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE
:
3295 (s
->for_overlaps ? NS_DUMPGLYPH_FOREGROUND
:
3296 NS_DUMPGLYPH_NORMAL
));
3298 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3300 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3301 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3302 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3306 (s
, 0, s
->nchars
, s
->x
, s
->y
,
3307 (flags
== NS_DUMPGLYPH_NORMAL
&& !s
->background_filled_p
)
3308 || flags
== NS_DUMPGLYPH_MOUSEFACE
);
3311 NSColor
*col
= (NS_FACE_FOREGROUND
(s
->face
) != 0
3312 ? ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(s
->face
),
3314 : FRAME_FOREGROUND_COLOR
(s
->f
));
3317 /* Draw underline, overline, strike-through. */
3318 ns_draw_text_decoration
(s
, s
->face
, col
, s
->width
, s
->x
);
3321 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3323 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3324 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3325 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3331 case GLYPHLESS_GLYPH
:
3332 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3333 ns_focus
(s
->f
, r
, n
);
3335 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3336 && ! s
->first_glyph
->u.cmp.automatic
))
3337 s
->background_filled_p
= 1;
3339 ns_maybe_dumpglyphs_background
3340 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3342 /* Not yet implemented. */
3351 /* Draw box if not done already. */
3352 if (!s
->for_overlaps
&& !box_drawn_p
&& s
->face
->box
!= FACE_NO_BOX
)
3354 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3355 ns_focus
(s
->f
, r
, n
);
3356 ns_dumpglyphs_box_or_relief
(s
);
3365 /* ==========================================================================
3369 ========================================================================== */
3373 ns_send_appdefined
(int value
)
3374 /* --------------------------------------------------------------------------
3375 Internal: post an appdefined event which EmacsApp-sendEvent will
3376 recognize and take as a command to halt the event loop.
3377 -------------------------------------------------------------------------- */
3379 /*NSTRACE (ns_send_appdefined); */
3381 #ifdef NS_IMPL_GNUSTEP
3382 // GNUStep needs postEvent to happen on the main thread.
3383 if (! [[NSThread currentThread
] isMainThread
])
3385 EmacsApp
*app
= (EmacsApp
*)NSApp
;
3386 app
->nextappdefined
= value
;
3387 [app performSelectorOnMainThread
:@selector
(sendFromMainThread
:)
3394 /* Only post this event if we haven't already posted one. This will end
3395 the [NXApp run] main loop after having processed all events queued at
3397 if (send_appdefined
)
3401 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3402 send_appdefined
= NO
;
3404 /* Don't need wakeup timer any more */
3407 [timed_entry invalidate
];
3408 [timed_entry release
];
3412 nxev
= [NSEvent otherEventWithType
: NSApplicationDefined
3413 location
: NSMakePoint
(0, 0)
3416 windowNumber
: [[NSApp mainWindow
] windowNumber
]
3417 context
: [NSApp context
]
3422 /* Post an application defined event on the event queue. When this is
3423 received the [NXApp run] will return, thus having processed all
3424 events which are currently queued. */
3425 [NSApp postEvent
: nxev atStart
: NO
];
3429 #ifdef HAVE_NATIVE_FS
3433 Lisp_Object frame
, tail
;
3435 if (ns_last_use_native_fullscreen
== ns_use_native_fullscreen
)
3438 ns_last_use_native_fullscreen
= ns_use_native_fullscreen
;
3440 FOR_EACH_FRAME
(tail
, frame
)
3442 struct frame
*f
= XFRAME
(frame
);
3445 EmacsView
*view = FRAME_NS_VIEW
(f
);
3446 [view updateCollectionBehaviour
];
3452 /* GNUStep and OSX <= 10.4 does not have cancelTracking. */
3453 #
if defined
(NS_IMPL_COCOA
) && \
3454 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_5
3455 /* Check if menu open should be cancelled or continued as normal. */
3457 ns_check_menu_open
(NSMenu
*menu)
3459 /* Click in menu bar? */
3460 NSArray
*a
= [[NSApp mainMenu
] itemArray
];
3464 if (menu == nil
) // Menu tracking ended.
3466 if (menu_will_open_state
== MENU_OPENING
)
3467 menu_will_open_state
= MENU_NONE
;
3471 for (i
= 0; ! found
&& i
< [a count
]; i
++)
3472 found
= menu == [[a objectAtIndex
:i
] submenu
];
3475 if (menu_will_open_state
== MENU_NONE
&& emacs_event
)
3477 NSEvent
*theEvent
= [NSApp currentEvent
];
3478 struct frame
*emacsframe
= SELECTED_FRAME
();
3480 [menu cancelTracking
];
3481 menu_will_open_state
= MENU_PENDING
;
3482 emacs_event
->kind
= MENU_BAR_ACTIVATE_EVENT
;
3483 EV_TRAILER
(theEvent
);
3485 CGEventRef ourEvent
= CGEventCreate
(NULL);
3486 menu_mouse_point
= CGEventGetLocation
(ourEvent
);
3487 CFRelease
(ourEvent
);
3489 else if (menu_will_open_state
== MENU_OPENING
)
3491 menu_will_open_state
= MENU_NONE
;
3496 /* Redo saved menu click if state is MENU_PENDING. */
3498 ns_check_pending_open_menu
()
3500 if (menu_will_open_state
== MENU_PENDING
)
3502 CGEventSourceRef source
3503 = CGEventSourceCreate
(kCGEventSourceStateHIDSystemState
);
3505 CGEventRef event
= CGEventCreateMouseEvent
(source
,
3506 kCGEventLeftMouseDown
,
3508 kCGMouseButtonLeft
);
3509 CGEventSetType
(event
, kCGEventLeftMouseDown
);
3510 CGEventPost
(kCGHIDEventTap
, event
);
3514 menu_will_open_state
= MENU_OPENING
;
3517 #endif
/* NS_IMPL_COCOA) && >= MAC_OS_X_VERSION_10_5 */
3520 ns_read_socket
(struct terminal *terminal, struct input_event
*hold_quit
)
3521 /* --------------------------------------------------------------------------
3522 External (hook): Post an event to ourself and keep reading events until
3523 we read it back again. In effect process all events which were waiting.
3524 From 21+ we have to manage the event buffer ourselves.
3525 -------------------------------------------------------------------------- */
3527 struct input_event ev
;
3530 /* NSTRACE (ns_read_socket); */
3532 #ifdef HAVE_NATIVE_FS
3536 if ([NSApp modalWindow
] != nil
)
3539 if (hold_event_q.nr
> 0)
3542 for (i
= 0; i
< hold_event_q.nr
; ++i
)
3543 kbd_buffer_store_event_hold
(&hold_event_q.q
[i
], hold_quit
);
3544 hold_event_q.nr
= 0;
3549 n_emacs_events_pending
= 0;
3552 q_event_ptr
= hold_quit
;
3554 /* we manage autorelease pools by allocate/reallocate each time around
3555 the loop; strict nesting is occasionally violated but seems not to
3556 matter.. earlier methods using full nesting caused major memory leaks */
3557 [outerpool release
];
3558 outerpool
= [[NSAutoreleasePool alloc
] init
];
3560 /* If have pending open-file requests, attend to the next one of those. */
3561 if (ns_pending_files
&& [ns_pending_files count
] != 0
3562 && [(EmacsApp
*)NSApp openFile
: [ns_pending_files objectAtIndex
: 0]])
3564 [ns_pending_files removeObjectAtIndex
: 0];
3566 /* Deal with pending service requests. */
3567 else if (ns_pending_service_names
&& [ns_pending_service_names count
] != 0
3569 NSApp fulfillService
: [ns_pending_service_names objectAtIndex
: 0]
3570 withArg
: [ns_pending_service_args objectAtIndex
: 0]])
3572 [ns_pending_service_names removeObjectAtIndex
: 0];
3573 [ns_pending_service_args removeObjectAtIndex
: 0];
3577 /* Run and wait for events. We must always send one NX_APPDEFINED event
3578 to ourself, otherwise [NXApp run] will never exit. */
3579 send_appdefined
= YES
;
3580 ns_send_appdefined
(-1);
3582 if (++apploopnr
!= 1)
3590 nevents
= n_emacs_events_pending
;
3591 n_emacs_events_pending
= 0;
3592 emacs_event
= q_event_ptr
= NULL;
3600 ns_select
(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
3601 fd_set
*exceptfds
, struct timespec const
*timeout
,
3602 sigset_t const
*sigmask
)
3603 /* --------------------------------------------------------------------------
3604 Replacement for select, checking for events
3605 -------------------------------------------------------------------------- */
3609 struct input_event event
;
3612 /* NSTRACE (ns_select); */
3614 #ifdef HAVE_NATIVE_FS
3618 if (hold_event_q.nr
> 0)
3620 /* We already have events pending. */
3626 for (k
= 0; k
< nfds
+1; k
++)
3628 if (readfds
&& FD_ISSET
(k
, readfds
)) ++nr
;
3629 if (writefds
&& FD_ISSET
(k
, writefds
)) ++nr
;
3633 ||
(timeout
&& timeout
->tv_sec
== 0 && timeout
->tv_nsec
== 0))
3634 return pselect
(nfds
, readfds
, writefds
, exceptfds
, timeout
, sigmask
);
3636 [outerpool release
];
3637 outerpool
= [[NSAutoreleasePool alloc
] init
];
3640 send_appdefined
= YES
;
3643 pthread_mutex_lock
(&select_mutex
);
3648 select_readfds
= *readfds
;
3649 select_valid
+= SELECT_HAVE_READ
;
3653 select_writefds
= *writefds
;
3654 select_valid
+= SELECT_HAVE_WRITE
;
3659 select_timeout
= *timeout
;
3660 select_valid
+= SELECT_HAVE_TMO
;
3663 pthread_mutex_unlock
(&select_mutex
);
3665 /* Inform fd_handler that select should be called */
3667 emacs_write_sig
(selfds
[1], &c
, 1);
3669 else if (nr
== 0 && timeout
)
3671 /* No file descriptor, just a timeout, no need to wake fd_handler */
3672 double time
= timespectod
(*timeout
);
3673 timed_entry
= [[NSTimer scheduledTimerWithTimeInterval
: time
3676 @selector
(timeout_handler
:)
3681 else /* No timeout and no file descriptors, can this happen? */
3683 /* Send appdefined so we exit from the loop */
3684 ns_send_appdefined
(-1);
3689 emacs_event
= &event
;
3690 if (++apploopnr
!= 1)
3697 if (nr
> 0 && readfds
)
3700 emacs_write_sig
(selfds
[1], &c
, 1);
3704 t
= last_appdefined_event_data
;
3706 if (t
!= NO_APPDEFINED_DATA
)
3708 last_appdefined_event_data
= NO_APPDEFINED_DATA
;
3712 /* The NX_APPDEFINED event we received was a timeout. */
3717 /* The NX_APPDEFINED event we received was the result of
3718 at least one real input event arriving. */
3724 /* Received back from select () in fd_handler; copy the results */
3725 pthread_mutex_lock
(&select_mutex
);
3726 if (readfds
) *readfds
= select_readfds
;
3727 if (writefds
) *writefds
= select_writefds
;
3728 pthread_mutex_unlock
(&select_mutex
);
3743 /* ==========================================================================
3747 ========================================================================== */
3751 ns_set_vertical_scroll_bar
(struct window
*window
,
3752 int portion
, int whole
, int position
)
3753 /* --------------------------------------------------------------------------
3754 External (hook): Update or add scrollbar
3755 -------------------------------------------------------------------------- */
3759 struct frame
*f
= XFRAME
(WINDOW_FRAME
(window
));
3760 EmacsView
*view = FRAME_NS_VIEW
(f
);
3761 int window_y
, window_height
;
3762 int top
, left
, height
, width
, sb_width
, sb_left
;
3764 BOOL fringe_extended_p
;
3766 /* optimization; display engine sends WAY too many of these.. */
3767 if (!NILP
(window
->vertical_scroll_bar
))
3769 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3770 if ([bar checkSamePosition
: position portion
: portion whole
: whole
])
3772 if (view->scrollbarsNeedingUpdate
== 0)
3774 if (!windows_or_buffers_changed
)
3778 view->scrollbarsNeedingUpdate
--;
3782 NSTRACE
(ns_set_vertical_scroll_bar
);
3784 /* Get dimensions. */
3785 window_box
(window
, ANY_AREA
, 0, &window_y
, 0, &window_height
);
3787 height
= window_height
;
3788 width
= WINDOW_CONFIG_SCROLL_BAR_COLS
(window
) * FRAME_COLUMN_WIDTH
(f
);
3789 left
= WINDOW_SCROLL_BAR_AREA_X
(window
);
3791 /* allow for displaying a skinnier scrollbar than char area allotted */
3792 sb_width
= (WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) > 0) ?
3793 WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) : width
;
3796 r
= NSMakeRect
(sb_left
, top
, sb_width
, height
);
3797 /* the parent view is flipped, so we need to flip y value */
3799 r.origin.y
= (v.
size.height
- r.
size.height
- r.origin.y
);
3801 fringe_extended_p
= WINDOW_FRINGE_EXTENDED_P
(window
);
3803 XSETWINDOW
(win
, window
);
3806 /* we want at least 5 lines to display a scrollbar */
3807 if (WINDOW_TOTAL_LINES
(window
) < 5)
3809 if (!NILP
(window
->vertical_scroll_bar
))
3811 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3812 [bar removeFromSuperview
];
3813 wset_vertical_scroll_bar
(window
, Qnil
);
3815 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3820 if (NILP
(window
->vertical_scroll_bar
))
3822 if (width
> 0 && height
> 0)
3824 if (fringe_extended_p
)
3825 ns_clear_frame_area
(f
, sb_left
, top
, sb_width
, height
);
3827 ns_clear_frame_area
(f
, left
, top
, width
, height
);
3830 bar = [[EmacsScroller alloc
] initFrame
: r window
: win
];
3831 wset_vertical_scroll_bar
(window
, make_save_ptr
(bar));
3836 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3837 oldRect
= [bar frame
];
3838 r.
size.width
= oldRect.
size.width
;
3839 if (FRAME_LIVE_P
(f
) && !NSEqualRects
(oldRect
, r
))
3841 if (oldRect.origin.x
!= r.origin.x
)
3842 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3847 [bar setPosition
: position portion
: portion whole
: whole
];
3853 ns_condemn_scroll_bars
(struct frame
*f
)
3854 /* --------------------------------------------------------------------------
3855 External (hook): arrange for all frame's scrollbars to be removed
3856 at next call to judge_scroll_bars, except for those redeemed.
3857 -------------------------------------------------------------------------- */
3861 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
3863 NSTRACE
(ns_condemn_scroll_bars
);
3865 for (i
=[subviews count
]-1; i
>= 0; i
--)
3867 view = [subviews objectAtIndex
: i
];
3868 if ([view isKindOfClass
: [EmacsScroller
class]])
3875 ns_redeem_scroll_bar
(struct window
*window
)
3876 /* --------------------------------------------------------------------------
3877 External (hook): arrange to spare this window's scrollbar
3878 at next call to judge_scroll_bars.
3879 -------------------------------------------------------------------------- */
3882 NSTRACE
(ns_redeem_scroll_bar
);
3883 if (!NILP
(window
->vertical_scroll_bar
))
3885 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3892 ns_judge_scroll_bars
(struct frame
*f
)
3893 /* --------------------------------------------------------------------------
3894 External (hook): destroy all scrollbars on frame that weren't
3895 redeemed after call to condemn_scroll_bars.
3896 -------------------------------------------------------------------------- */
3900 EmacsView
*eview
= FRAME_NS_VIEW
(f
);
3901 NSArray
*subviews
= [[eview superview
] subviews
];
3904 NSTRACE
(ns_judge_scroll_bars
);
3905 for (i
= [subviews count
]-1; i
>= 0; --i
)
3907 view = [subviews objectAtIndex
: i
];
3908 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
3914 [eview updateFrameSize
: NO
];
3917 /* ==========================================================================
3921 ========================================================================== */
3924 x_display_pixel_height
(struct ns_display_info
*dpyinfo
)
3926 NSArray
*screens
= [NSScreen screens
];
3927 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3932 while ((screen
= [enumerator nextObject
]) != nil
)
3933 frame
= NSUnionRect
(frame
, [screen frame
]);
3935 return NSHeight
(frame
);
3939 x_display_pixel_width
(struct ns_display_info
*dpyinfo
)
3941 NSArray
*screens
= [NSScreen screens
];
3942 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3947 while ((screen
= [enumerator nextObject
]) != nil
)
3948 frame
= NSUnionRect
(frame
, [screen frame
]);
3950 return NSWidth
(frame
);
3954 static Lisp_Object ns_string_to_lispmod
(const
char *s
)
3955 /* --------------------------------------------------------------------------
3956 Convert modifier name to lisp symbol
3957 -------------------------------------------------------------------------- */
3959 if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qmeta
)), s
, 10))
3961 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qsuper
)), s
, 10))
3963 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qcontrol
)), s
, 10))
3965 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qalt
)), s
, 10))
3967 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qhyper
)), s
, 10))
3969 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qnone
)), s
, 10))
3977 ns_default
(const
char *parameter
, Lisp_Object
*result
,
3978 Lisp_Object yesval
, Lisp_Object noval
,
3979 BOOL is_float
, BOOL is_modstring
)
3980 /* --------------------------------------------------------------------------
3981 Check a parameter value in user's preferences
3982 -------------------------------------------------------------------------- */
3984 const
char *value
= ns_get_defaults_value
(parameter
);
3990 if (c_strcasecmp
(value
, "YES"
) == 0)
3992 else if (c_strcasecmp
(value
, "NO"
) == 0)
3994 else if (is_float
&& (f
= strtod
(value
, &pos
), pos
!= value
))
3995 *result
= make_float
(f
);
3996 else if (is_modstring
&& value
)
3997 *result
= ns_string_to_lispmod
(value
);
3998 else fprintf (stderr
,
3999 "Bad value
for default
\"%s\": \"%s\"\n", parameter, value);
4005 ns_initialize_display_info
(struct ns_display_info
*dpyinfo
)
4006 /* --------------------------------------------------------------------------
4007 Initialize global info and storage for display.
4008 -------------------------------------------------------------------------- */
4010 NSScreen
*screen
= [NSScreen mainScreen
];
4011 NSWindowDepth depth
= [screen depth
];
4013 dpyinfo
->resx
= 72.27; /* used 75.0, but this makes pt == pixel, expected */
4014 dpyinfo
->resy
= 72.27;
4015 dpyinfo
->color_p
= ![NSDeviceWhiteColorSpace isEqualToString
:
4016 NSColorSpaceFromDepth
(depth
)]
4017 && ![NSCalibratedWhiteColorSpace isEqualToString
:
4018 NSColorSpaceFromDepth
(depth
)];
4019 dpyinfo
->n_planes
= NSBitsPerPixelFromDepth
(depth
);
4020 dpyinfo
->color_table
= xmalloc
(sizeof
*dpyinfo
->color_table
);
4021 dpyinfo
->color_table
->colors
= NULL;
4022 dpyinfo
->root_window
= 42; /* a placeholder.. */
4023 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
= NULL;
4024 dpyinfo
->n_fonts
= 0;
4025 dpyinfo
->smallest_font_height
= 1;
4026 dpyinfo
->smallest_char_width
= 1;
4028 reset_mouse_highlight
(&dpyinfo
->mouse_highlight
);
4032 /* This and next define (many of the) public functions in this file. */
4033 /* x_... are generic versions in xdisp.c that we, and other terms, get away
4034 with using despite presence in the "system dependent" redisplay
4035 interface. In addition, many of the ns_ methods have code that is
4036 shared with all terms, indicating need for further refactoring. */
4037 extern frame_parm_handler ns_frame_parm_handlers
[];
4038 static
struct redisplay_interface ns_redisplay_interface
=
4040 ns_frame_parm_handlers
,
4044 x_clear_end_of_line
,
4046 ns_after_update_window_line
,
4047 ns_update_window_begin
,
4048 ns_update_window_end
,
4049 0, /* flush_display */
4050 x_clear_window_mouse_face
,
4051 x_get_glyph_overhangs
,
4052 x_fix_overlapping_area
,
4053 ns_draw_fringe_bitmap
,
4054 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
4055 0, /* destroy_fringe_bitmap */
4056 ns_compute_glyph_string_overhangs
,
4057 ns_draw_glyph_string
,
4058 ns_define_frame_cursor
,
4059 ns_clear_frame_area
,
4060 ns_draw_window_cursor
,
4061 ns_draw_vertical_window_border
,
4062 ns_draw_window_divider
,
4063 ns_shift_glyphs_for_insert
4068 ns_delete_display
(struct ns_display_info
*dpyinfo
)
4074 /* This function is called when the last frame on a display is deleted. */
4076 ns_delete_terminal
(struct terminal *terminal)
4078 struct ns_display_info
*dpyinfo
= terminal->display_info.ns
;
4080 /* Protect against recursive calls. delete_frame in
4081 delete_terminal calls us back when it deletes our last frame. */
4082 if (!terminal->name
)
4087 x_destroy_all_bitmaps
(dpyinfo
);
4088 ns_delete_display
(dpyinfo
);
4093 static
struct terminal *
4094 ns_create_terminal
(struct ns_display_info
*dpyinfo
)
4095 /* --------------------------------------------------------------------------
4096 Set up use of NS before we make the first connection.
4097 -------------------------------------------------------------------------- */
4099 struct terminal *terminal;
4101 NSTRACE
(ns_create_terminal
);
4103 terminal = create_terminal
();
4105 terminal->type = output_ns
;
4106 terminal->display_info.ns
= dpyinfo
;
4107 dpyinfo
->terminal = terminal;
4109 terminal->rif
= &ns_redisplay_interface
;
4111 terminal->clear_frame_hook
= ns_clear_frame
;
4112 terminal->ins_del_lines_hook
= 0; /* XXX vestigial? */
4113 terminal->delete_glyphs_hook
= 0; /* XXX vestigial? */
4114 terminal->ring_bell_hook
= ns_ring_bell
;
4115 terminal->reset_terminal_modes_hook
= NULL;
4116 terminal->set_terminal_modes_hook
= NULL;
4117 terminal->update_begin_hook
= ns_update_begin
;
4118 terminal->update_end_hook
= ns_update_end
;
4119 terminal->set_terminal_window_hook
= NULL; /* XXX vestigial? */
4120 terminal->read_socket_hook
= ns_read_socket
;
4121 terminal->frame_up_to_date_hook
= ns_frame_up_to_date
;
4122 terminal->mouse_position_hook
= ns_mouse_position
;
4123 terminal->frame_rehighlight_hook
= ns_frame_rehighlight
;
4124 terminal->frame_raise_lower_hook
= ns_frame_raise_lower
;
4126 terminal->fullscreen_hook
= ns_fullscreen_hook
;
4128 terminal->set_vertical_scroll_bar_hook
= ns_set_vertical_scroll_bar
;
4129 terminal->condemn_scroll_bars_hook
= ns_condemn_scroll_bars
;
4130 terminal->redeem_scroll_bar_hook
= ns_redeem_scroll_bar
;
4131 terminal->judge_scroll_bars_hook
= ns_judge_scroll_bars
;
4133 terminal->delete_frame_hook
= x_destroy_window
;
4134 terminal->delete_terminal_hook
= ns_delete_terminal
;
4140 struct ns_display_info
*
4141 ns_term_init
(Lisp_Object display_name
)
4142 /* --------------------------------------------------------------------------
4143 Start the Application and get things rolling.
4144 -------------------------------------------------------------------------- */
4146 struct terminal *terminal;
4147 struct ns_display_info
*dpyinfo
;
4148 static int ns_initialized
= 0;
4151 if (ns_initialized
) return x_display_list
;
4154 NSTRACE
(ns_term_init
);
4156 [outerpool release
];
4157 outerpool
= [[NSAutoreleasePool alloc
] init
];
4159 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
4160 /*GSDebugAllocationActive (YES); */
4164 Fset_input_interrupt_mode
(Qnil
);
4166 if (selfds
[0] == -1)
4168 if (emacs_pipe
(selfds
) != 0)
4170 fprintf (stderr
, "Failed to create pipe
: %s\n",
4171 emacs_strerror
(errno
));
4175 fcntl
(selfds
[0], F_SETFL
, O_NONBLOCK|fcntl
(selfds
[0], F_GETFL
));
4176 FD_ZERO
(&select_readfds
);
4177 FD_ZERO
(&select_writefds
);
4178 pthread_mutex_init
(&select_mutex
, NULL);
4181 ns_pending_files
= [[NSMutableArray alloc
] init
];
4182 ns_pending_service_names
= [[NSMutableArray alloc
] init
];
4183 ns_pending_service_args
= [[NSMutableArray alloc
] init
];
4185 /* Start app and create the main menu, window, view.
4186 Needs to be here because ns_initialize_display_info () uses AppKit classes.
4187 The view will then ask the NSApp to stop and return to Emacs. */
4188 [EmacsApp sharedApplication
];
4191 [NSApp setDelegate
: NSApp
];
4193 /* Start the select thread. */
4194 [NSThread detachNewThreadSelector
:@selector
(fd_handler
:)
4198 /* debugging: log all notifications */
4199 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
4200 selector: @selector (logNotification:)
4201 name: nil object: nil]; */
4203 dpyinfo
= xzalloc
(sizeof
*dpyinfo
);
4205 ns_initialize_display_info
(dpyinfo
);
4206 terminal = ns_create_terminal
(dpyinfo
);
4208 terminal->kboard
= allocate_kboard
(Qns
);
4209 /* Don't let the initial kboard remain current longer than necessary.
4210 That would cause problems if a file loaded on startup tries to
4211 prompt in the mini-buffer. */
4212 if (current_kboard
== initial_kboard
)
4213 current_kboard
= terminal->kboard
;
4214 terminal->kboard
->reference_count
++;
4216 dpyinfo
->next
= x_display_list
;
4217 x_display_list
= dpyinfo
;
4219 dpyinfo
->name_list_element
= Fcons
(display_name
, Qnil
);
4221 terminal->name
= xstrdup
(SSDATA
(display_name
));
4225 if (!inhibit_x_resources
)
4227 ns_default
("GSFontAntiAlias"
, &ns_antialias_text
,
4230 /* this is a standard variable */
4231 ns_default
("AppleAntiAliasingThreshold"
, &tmp
,
4232 make_float
(10.0), make_float
(6.0), YES
, NO
);
4233 ns_antialias_threshold
= NILP
(tmp
) ?
10.0 : XFLOATINT
(tmp
);
4237 NSColorList
*cl
= [NSColorList colorListNamed
: @"Emacs"
];
4241 Lisp_Object color_file
, color_map
, color
;
4245 color_file
= Fexpand_file_name
(build_string
("rgb.txt"
),
4246 Fsymbol_value
(intern
("data
-directory"
)));
4248 color_map
= Fx_load_color_file
(color_file
);
4249 if (NILP
(color_map
))
4250 fatal
("Could not read
%s.\n", SDATA (color_file));
4252 cl
= [[NSColorList alloc
] initWithName
: @"Emacs"
];
4253 for ( ; CONSP
(color_map
); color_map
= XCDR
(color_map
))
4255 color
= XCAR
(color_map
);
4256 name
= SSDATA
(XCAR
(color
));
4257 c
= XINT
(XCDR
(color
));
4259 [NSColor colorWithCalibratedRed
: RED_FROM_ULONG
(c
) / 255.0
4260 green
: GREEN_FROM_ULONG
(c
) / 255.0
4261 blue
: BLUE_FROM_ULONG
(c
) / 255.0
4263 forKey
: [NSString stringWithUTF8String
: name
]];
4265 [cl writeToFile
: nil
];
4270 #ifdef NS_IMPL_GNUSTEP
4271 Vwindow_system_version
= build_string
(gnustep_base_version
);
4273 /*PSnextrelease (128, c); */
4274 char c
[DBL_BUFSIZE_BOUND
];
4275 int len
= dtoastr
(c
, sizeof c
, 0, 0, NSAppKitVersionNumber
);
4276 Vwindow_system_version
= make_unibyte_string
(c
, len
);
4280 delete_keyboard_wait_descriptor
(0);
4282 ns_app_name
= [[NSProcessInfo processInfo
] processName
];
4284 /* Set up OS X app menu */
4285 #ifdef NS_IMPL_COCOA
4289 /* set up the application menu */
4290 svcsMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Services"
];
4291 [svcsMenu setAutoenablesItems
: NO
];
4292 appMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Emacs"
];
4293 [appMenu setAutoenablesItems
: NO
];
4294 mainMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4295 dockMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4297 [appMenu insertItemWithTitle
: @"About Emacs"
4298 action
: @selector
(orderFrontStandardAboutPanel
:)
4301 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 1];
4302 [appMenu insertItemWithTitle
: @"Preferences..."
4303 action
: @selector
(showPreferencesWindow
:)
4306 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 3];
4307 item
= [appMenu insertItemWithTitle
: @"Services"
4308 action
: @selector
(menuDown
:)
4311 [appMenu setSubmenu
: svcsMenu forItem
: item
];
4312 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 5];
4313 [appMenu insertItemWithTitle
: @"Hide Emacs"
4314 action
: @selector
(hide
:)
4317 item
= [appMenu insertItemWithTitle
: @"Hide Others"
4318 action
: @selector
(hideOtherApplications
:)
4321 [item setKeyEquivalentModifierMask
: NSCommandKeyMask | NSAlternateKeyMask
];
4322 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 8];
4323 [appMenu insertItemWithTitle
: @"
Quit Emacs"
4324 action
: @selector
(terminate
:)
4328 item
= [mainMenu insertItemWithTitle
: ns_app_name
4329 action
: @selector
(menuDown
:)
4332 [mainMenu setSubmenu
: appMenu forItem
: item
];
4333 [dockMenu insertItemWithTitle
: @"New Frame"
4334 action
: @selector
(newFrame
:)
4338 [NSApp setMainMenu
: mainMenu
];
4339 [NSApp setAppleMenu
: appMenu
];
4340 [NSApp setServicesMenu
: svcsMenu
];
4341 /* Needed at least on Cocoa, to get dock menu to show windows */
4342 [NSApp setWindowsMenu
: [[NSMenu alloc
] init
]];
4344 [[NSNotificationCenter defaultCenter
]
4345 addObserver
: mainMenu
4346 selector
: @selector
(trackingNotification
:)
4347 name
: NSMenuDidBeginTrackingNotification object
: mainMenu
];
4348 [[NSNotificationCenter defaultCenter
]
4349 addObserver
: mainMenu
4350 selector
: @selector
(trackingNotification
:)
4351 name
: NSMenuDidEndTrackingNotification object
: mainMenu
];
4353 #endif
/* MAC OS X menu setup */
4355 /* Register our external input/output types, used for determining
4356 applicable services and also drag/drop eligibility. */
4357 ns_send_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
] retain
];
4358 ns_return_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
]
4360 ns_drag_types
= [[NSArray arrayWithObjects
:
4362 NSTabularTextPboardType
,
4363 NSFilenamesPboardType
,
4366 NSFontPboardType
, nil
] retain
];
4368 /* If fullscreen is in init/default-frame-alist, focus isn't set
4369 right for fullscreen windows, so set this. */
4370 [NSApp activateIgnoringOtherApps
:YES
];
4373 ns_do_open_file
= YES
;
4375 #ifdef NS_IMPL_GNUSTEP
4376 /* GNUstep steals SIGCHLD for use in NSTask, but we don't use NSTask.
4377 We must re-catch it so subprocess works. */
4378 catch_child_signal
();
4385 ns_term_shutdown
(int sig
)
4387 [[NSUserDefaults standardUserDefaults
] synchronize
];
4389 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4390 if (STRINGP
(Vauto_save_list_file_name
))
4391 unlink
(SSDATA
(Vauto_save_list_file_name
));
4393 if (sig
== 0 || sig
== SIGTERM
)
4395 [NSApp terminate
: NSApp
];
4397 else // force a stack
trace to happen
4404 /* ==========================================================================
4406 EmacsApp implementation
4408 ========================================================================== */
4411 @implementation EmacsApp
4413 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_9
4416 if (self
= [super init
])
4417 self
->isFirst
= YES
;
4424 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
4426 if (isFirst
) [self finishLaunching
];
4429 shouldKeepRunning
= YES
;
4433 pool
= [[NSAutoreleasePool alloc
] init
];
4436 [self nextEventMatchingMask
:NSAnyEventMask
4437 untilDate
:[NSDate distantFuture
]
4438 inMode
:NSDefaultRunLoopMode
4440 [self sendEvent
:event
];
4441 [self updateWindows
];
4442 } while (shouldKeepRunning
);
4447 - (void
)stop
: (id
)sender
4449 shouldKeepRunning
= NO
;
4450 // Stop possible
dialog also. Noop
if no
dialog present.
4451 // The file
dialog still leaks
7k
- 10k on
10.9 though.
4452 [super stop
:sender
];
4456 - (void
)logNotification
: (NSNotification
*)notification
4458 const
char *name
= [[notification name
] UTF8String
];
4459 if (!strstr
(name
, "Update"
) && !strstr
(name
, "NSMenu"
)
4460 && !strstr
(name
, "WindowNumber"
))
4461 NSLog
(@"notification
: '%@'"
, [notification name
]);
4465 - (void
)sendEvent
: (NSEvent
*)theEvent
4466 /* --------------------------------------------------------------------------
4467 Called when NSApp is running for each event received. Used to stop
4468 the loop when we choose, since there's no way to just run one iteration.
4469 -------------------------------------------------------------------------- */
4471 int
type = [theEvent
type];
4472 NSWindow
*window
= [theEvent window
];
4474 /* NSTRACE (sendEvent); */
4475 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4477 #ifdef NS_IMPL_GNUSTEP
4478 // Keyboard events aren
't propagated to file dialogs for some reason.
4479 if ([NSApp modalWindow] != nil &&
4480 (type == NSKeyDown || type == NSKeyUp || type == NSFlagsChanged))
4482 [[NSApp modalWindow] sendEvent: theEvent];
4487 if (type == NSApplicationDefined)
4489 switch ([theEvent data2])
4491 #ifdef NS_IMPL_COCOA
4492 case NSAPP_DATA2_RUNASSCRIPT:
4497 case NSAPP_DATA2_RUNFILEDIALOG:
4498 ns_run_file_dialog ();
4504 if (type == NSCursorUpdate && window == nil)
4506 fprintf (stderr, "Dropping external cursor update event.\n");
4510 if (type == NSApplicationDefined)
4512 /* Events posted by ns_send_appdefined interrupt the run loop here.
4513 But, if a modal window is up, an appdefined can still come through,
4514 (e.g., from a makeKeyWindow event) but stopping self also stops the
4515 modal loop. Just defer it until later. */
4516 if ([NSApp modalWindow] == nil)
4518 last_appdefined_event_data = [theEvent data1];
4523 send_appdefined = YES;
4528 #ifdef NS_IMPL_COCOA
4529 /* If no dialog and none of our frames have focus and it is a move, skip it.
4530 It is a mouse move in an auxiliary menu, i.e. on the top right on OSX,
4531 such as Wifi, sound, date or similar.
4532 This prevents "spooky" highlighting in the frame under the menu. */
4533 if (type == NSMouseMoved && [NSApp modalWindow] == nil)
4535 struct ns_display_info *di;
4536 BOOL has_focus = NO;
4537 for (di = x_display_list; ! has_focus && di; di = di->next)
4538 has_focus = di->x_focus_frame != 0;
4544 [super sendEvent: theEvent];
4548 - (void)showPreferencesWindow: (id)sender
4550 struct frame *emacsframe = SELECTED_FRAME ();
4551 NSEvent *theEvent = [NSApp currentEvent];
4555 emacs_event->kind = NS_NONKEY_EVENT;
4556 emacs_event->code = KEY_NS_SHOW_PREFS;
4557 emacs_event->modifiers = 0;
4558 EV_TRAILER (theEvent);
4562 - (void)newFrame: (id)sender
4564 struct frame *emacsframe = SELECTED_FRAME ();
4565 NSEvent *theEvent = [NSApp currentEvent];
4569 emacs_event->kind = NS_NONKEY_EVENT;
4570 emacs_event->code = KEY_NS_NEW_FRAME;
4571 emacs_event->modifiers = 0;
4572 EV_TRAILER (theEvent);
4576 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4577 - (BOOL) openFile: (NSString *)fileName
4579 struct frame *emacsframe = SELECTED_FRAME ();
4580 NSEvent *theEvent = [NSApp currentEvent];
4585 emacs_event->kind = NS_NONKEY_EVENT;
4586 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
4587 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
4588 ns_input_line = Qnil; /* can be start or cons start,end */
4589 emacs_event->modifiers =0;
4590 EV_TRAILER (theEvent);
4596 /* **************************************************************************
4598 EmacsApp delegate implementation
4600 ************************************************************************** */
4602 - (void)applicationDidFinishLaunching: (NSNotification *)notification
4603 /* --------------------------------------------------------------------------
4604 When application is loaded, terminate event loop in ns_term_init
4605 -------------------------------------------------------------------------- */
4607 NSTRACE (applicationDidFinishLaunching);
4608 [NSApp setServicesProvider: NSApp];
4609 ns_send_appdefined (-2);
4613 /* Termination sequences:
4616 MenuBar | File | Exit:
4617 Select Quit from App menubar:
4619 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4622 Select Quit from Dock menu:
4625 Cancel -> Nothing else
4629 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4634 - (void) terminate: (id)sender
4636 struct frame *emacsframe = SELECTED_FRAME ();
4641 emacs_event->kind = NS_NONKEY_EVENT;
4642 emacs_event->code = KEY_NS_POWER_OFF;
4643 emacs_event->arg = Qt; /* mark as non-key event */
4644 EV_TRAILER ((id)nil);
4648 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
4652 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
4653 return NSTerminateNow;
4655 ret = NSRunAlertPanel(ns_app_name,
4656 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
4657 @"Save Buffers and Exit", @"Cancel", nil);
4659 if (ret == NSAlertDefaultReturn)
4660 return NSTerminateNow;
4661 else if (ret == NSAlertAlternateReturn)
4662 return NSTerminateCancel;
4663 return NSTerminateNow; /* just in case */
4667 not_in_argv (NSString *arg)
4670 const char *a = [arg UTF8String];
4671 for (k = 1; k < initial_argc; ++k)
4672 if (strcmp (a, initial_argv[k]) == 0) return 0;
4676 /* Notification from the Workspace to open a file */
4677 - (BOOL)application: sender openFile: (NSString *)file
4679 if (ns_do_open_file || not_in_argv (file))
4680 [ns_pending_files addObject: file];
4685 /* Open a file as a temporary file */
4686 - (BOOL)application: sender openTempFile: (NSString *)file
4688 if (ns_do_open_file || not_in_argv (file))
4689 [ns_pending_files addObject: file];
4694 /* Notification from the Workspace to open a file noninteractively (?) */
4695 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
4697 if (ns_do_open_file || not_in_argv (file))
4698 [ns_pending_files addObject: file];
4702 /* Notification from the Workspace to open multiple files */
4703 - (void)application: sender openFiles: (NSArray *)fileList
4705 NSEnumerator *files = [fileList objectEnumerator];
4707 /* Don't open files from the command
line unconditionally
,
4708 Cocoa parses the command
line wrong
, --option value tries to open value
4709 if --option is the last option. */
4710 while ((file
= [files nextObject
]) != nil
)
4711 if (ns_do_open_file || not_in_argv
(file
))
4712 [ns_pending_files addObject
: file
];
4714 [self replyToOpenOrPrint
: NSApplicationDelegateReplySuccess
];
4719 /* Handle dock menu requests. */
4720 - (NSMenu
*)applicationDockMenu
: (NSApplication
*) sender
4726 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4727 - (void
)applicationWillBecomeActive
: (NSNotification
*)notification
4729 //ns_app_active
=YES
;
4731 - (void
)applicationDidBecomeActive
: (NSNotification
*)notification
4733 NSTRACE
(applicationDidBecomeActive
);
4735 //ns_app_active
=YES
;
4737 ns_update_auto_hide_menu_bar
();
4738 // No constraining takes place when the application is not active.
4739 ns_constrain_all_frames
();
4741 - (void
)applicationDidResignActive
: (NSNotification
*)notification
4744 ns_send_appdefined
(-1);
4749 /* ==========================================================================
4751 EmacsApp aux handlers for managing event loop
4753 ========================================================================== */
4756 - (void
)timeout_handler
: (NSTimer
*)timedEntry
4757 /* --------------------------------------------------------------------------
4758 The timeout specified to ns_select has passed.
4759 -------------------------------------------------------------------------- */
4761 /*NSTRACE (timeout_handler); */
4762 ns_send_appdefined
(-2);
4765 #ifdef NS_IMPL_GNUSTEP
4766 - (void
)sendFromMainThread
:(id
)unused
4768 ns_send_appdefined
(nextappdefined
);
4772 - (void
)fd_handler
:(id
)unused
4773 /* --------------------------------------------------------------------------
4774 Check data waiting on file descriptors and terminate if so
4775 -------------------------------------------------------------------------- */
4778 int waiting
= 1, nfds
;
4781 fd_set readfds
, writefds
, *wfds
;
4782 struct timespec timeout
, *tmo
;
4783 NSAutoreleasePool
*pool
= nil
;
4785 /* NSTRACE (fd_handler); */
4790 pool
= [[NSAutoreleasePool alloc
] init
];
4796 FD_SET
(selfds
[0], &fds
);
4797 result
= select
(selfds
[0]+1, &fds
, NULL, NULL, NULL);
4798 if (result
> 0 && read
(selfds
[0], &c
, 1) == 1 && c
== 'g')
4803 pthread_mutex_lock
(&select_mutex
);
4806 if (select_valid
& SELECT_HAVE_READ
)
4807 readfds
= select_readfds
;
4811 if (select_valid
& SELECT_HAVE_WRITE
)
4813 writefds
= select_writefds
;
4818 if (select_valid
& SELECT_HAVE_TMO
)
4820 timeout
= select_timeout
;
4826 pthread_mutex_unlock
(&select_mutex
);
4828 FD_SET
(selfds
[0], &readfds
);
4829 if (selfds
[0] >= nfds
) nfds
= selfds
[0]+1;
4831 result
= pselect
(nfds
, &readfds
, wfds
, NULL, tmo
, NULL);
4834 ns_send_appdefined
(-2);
4835 else if (result
> 0)
4837 if (FD_ISSET
(selfds
[0], &readfds
))
4839 if (read
(selfds
[0], &c
, 1) == 1 && c
== 's')
4844 pthread_mutex_lock
(&select_mutex
);
4845 if (select_valid
& SELECT_HAVE_READ
)
4846 select_readfds
= readfds
;
4847 if (select_valid
& SELECT_HAVE_WRITE
)
4848 select_writefds
= writefds
;
4849 if (select_valid
& SELECT_HAVE_TMO
)
4850 select_timeout
= timeout
;
4851 pthread_mutex_unlock
(&select_mutex
);
4853 ns_send_appdefined
(result
);
4863 /* ==========================================================================
4867 ========================================================================== */
4869 /* called from system: queue for next pass through event loop */
4870 - (void
)requestService
: (NSPasteboard
*)pboard
4871 userData
: (NSString
*)userData
4872 error: (NSString
**)error
4874 [ns_pending_service_names addObject
: userData
];
4875 [ns_pending_service_args addObject
: [NSString stringWithUTF8String
:
4876 SSDATA
(ns_string_from_pasteboard
(pboard
))]];
4880 /* called from ns_read_socket to clear queue */
4881 - (BOOL
)fulfillService
: (NSString
*)name withArg
: (NSString
*)arg
4883 struct frame
*emacsframe
= SELECTED_FRAME
();
4884 NSEvent
*theEvent
= [NSApp currentEvent
];
4889 emacs_event
->kind
= NS_NONKEY_EVENT
;
4890 emacs_event
->code
= KEY_NS_SPI_SERVICE_CALL
;
4891 ns_input_spi_name
= build_string
([name UTF8String
]);
4892 ns_input_spi_arg
= build_string
([arg UTF8String
]);
4893 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
4894 EV_TRAILER
(theEvent
);
4904 /* ==========================================================================
4906 EmacsView implementation
4908 ========================================================================== */
4911 @implementation EmacsView
4913 /* needed to inform when window closed from LISP */
4914 - (void
) setWindowClosing
: (BOOL
)closing
4916 windowClosing
= closing
;
4922 NSTRACE
(EmacsView_dealloc
);
4924 if (fs_state
== FULLSCREEN_BOTH
)
4925 [nonfs_window release
];
4930 /* called on font panel selection */
4931 - (void
)changeFont
: (id
)sender
4933 NSEvent
*e
= [[self window
] currentEvent
];
4934 struct face
*face
= FRAME_DEFAULT_FACE
(emacsframe
);
4935 struct font
*font
= face
->font
;
4940 NSTRACE
(changeFont
);
4945 if (EQ
(font
->driver
->type, Qns
))
4946 nsfont
= ((struct nsfont_info
*)font
)->nsfont
;
4947 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_5
4949 nsfont
= (NSFont
*) macfont_get_nsctfont
(font
);
4952 if ((newFont
= [sender convertFont
: nsfont
]))
4954 SET_FRAME_GARBAGED
(emacsframe
); /* now needed as of 2008/10 */
4956 emacs_event
->kind
= NS_NONKEY_EVENT
;
4957 emacs_event
->modifiers
= 0;
4958 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
4960 size = [newFont pointSize
];
4961 ns_input_fontsize
= make_number
(lrint
(size));
4962 ns_input_font
= build_string
([[newFont familyName
] UTF8String
]);
4968 - (BOOL
)acceptsFirstResponder
4970 NSTRACE
(acceptsFirstResponder
);
4975 - (void
)resetCursorRects
4977 NSRect visible
= [self visibleRect
];
4978 NSCursor
*currentCursor
= FRAME_POINTER_TYPE
(emacsframe
);
4979 NSTRACE
(resetCursorRects
);
4981 if (currentCursor
== nil
)
4982 currentCursor
= [NSCursor arrowCursor
];
4984 if (!NSIsEmptyRect
(visible
))
4985 [self addCursorRect
: visible cursor
: currentCursor
];
4986 [currentCursor setOnMouseEntered
: YES
];
4991 /*****************************************************************************/
4992 /* Keyboard handling. */
4995 - (void
)keyDown
: (NSEvent
*)theEvent
4997 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
4999 unsigned fnKeysym
= 0;
5000 static NSMutableArray
*nsEvArray
;
5001 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
5002 static BOOL firstTime
= YES
;
5005 unsigned int flags
= [theEvent modifierFlags
];
5009 /* Rhapsody and OS X give up and down events for the arrow keys */
5010 if (ns_fake_keydown
== YES
)
5011 ns_fake_keydown
= NO
;
5012 else if ([theEvent
type] != NSKeyDown
)
5018 if (![[self window
] isKeyWindow
]
5019 && [[theEvent window
] isKindOfClass
: [EmacsWindow
class]]
5020 /* we must avoid an infinite loop here. */
5021 && (EmacsView
*)[[theEvent window
] delegate
] != self
)
5023 /* XXX: There is an occasional condition in which, when Emacs display
5024 updates a different frame from the current one, and temporarily
5025 selects it, then processes some interrupt-driven input
5026 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
5027 for some reason that window has its first responder set to the NSView
5028 most recently updated (I guess), which is not the correct one. */
5029 [(EmacsView
*)[[theEvent window
] delegate
] keyDown
: theEvent
];
5033 if (nsEvArray
== nil
)
5034 nsEvArray
= [[NSMutableArray alloc
] initWithCapacity
: 1];
5036 [NSCursor setHiddenUntilMouseMoves
: YES
];
5038 if (hlinfo
->mouse_face_hidden
&& INTEGERP
(Vmouse_highlight
))
5040 clear_mouse_face
(hlinfo
);
5041 hlinfo
->mouse_face_hidden
= 1;
5044 if (!processingCompose
)
5046 /* When using screen sharing, no left or right information is sent,
5047 so use Left key in those cases. */
5048 int is_left_key
, is_right_key
;
5050 code
= ([[theEvent charactersIgnoringModifiers
] length] == 0) ?
5051 0 : [[theEvent charactersIgnoringModifiers
] characterAtIndex
: 0];
5053 /* (Carbon way: [theEvent keyCode]) */
5055 /* is it a "function key"? */
5056 fnKeysym
= (code
< 0x00ff && (flags
&NSNumericPadKeyMask
))
5057 ? ns_convert_key
([theEvent keyCode
] | NSNumericPadKeyMask
)
5058 : ns_convert_key
(code
);
5062 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
5063 because Emacs treats Delete and KP-Delete same (in simple.el). */
5064 if ((fnKeysym
== 0xFFFF && [theEvent keyCode
] == 0x33)
5065 #ifdef NS_IMPL_GNUSTEP
5066 /* GNUstep uses incompatible keycodes, even for those that are
5067 supposed to be hardware independent. Just check for delete.
5068 Keypad delete does not have keysym 0xFFFF.
5069 See http://savannah.gnu.org/bugs/?25395
5071 ||
(fnKeysym
== 0xFFFF && code
== 127)
5074 code
= 0xFF08; /* backspace */
5079 /* are there modifiers? */
5080 emacs_event
->modifiers
= 0;
5082 if (flags
& NSHelpKeyMask
)
5083 emacs_event
->modifiers |
= hyper_modifier
;
5085 if (flags
& NSShiftKeyMask
)
5086 emacs_event
->modifiers |
= shift_modifier
;
5088 is_right_key
= (flags
& NSRightCommandKeyMask
) == NSRightCommandKeyMask
;
5089 is_left_key
= (flags
& NSLeftCommandKeyMask
) == NSLeftCommandKeyMask
5090 ||
(! is_right_key
&& (flags
& NSCommandKeyMask
) == NSCommandKeyMask
);
5093 emacs_event
->modifiers |
= parse_solitary_modifier
5094 (EQ
(ns_right_command_modifier
, Qleft
)
5095 ? ns_command_modifier
5096 : ns_right_command_modifier
);
5100 emacs_event
->modifiers |
= parse_solitary_modifier
5101 (ns_command_modifier
);
5103 /* if super (default), take input manager's word so things like
5104 dvorak / qwerty layout work */
5105 if (EQ
(ns_command_modifier
, Qsuper
)
5107 && [[theEvent characters
] length] != 0)
5109 /* XXX: the code we get will be unshifted, so if we have
5110 a shift modifier, must convert ourselves */
5111 if (!(flags
& NSShiftKeyMask
))
5112 code
= [[theEvent characters
] characterAtIndex
: 0];
5114 /* this is ugly and also requires linking w/Carbon framework
5115 (for LMGetKbdType) so for now leave this rare (?) case
5116 undealt with.. in future look into CGEvent methods */
5119 long smv
= GetScriptManagerVariable
(smKeyScript
);
5120 Handle uchrHandle
= GetResource
5121 ('uchr', GetScriptVariable
(smv
, smScriptKeys
));
5123 UCKeyTranslate
((UCKeyboardLayout
*)*uchrHandle
,
5124 [[theEvent characters
] characterAtIndex
: 0],
5125 kUCKeyActionDisplay
,
5126 (flags
& ~NSCommandKeyMask
) >> 8,
5127 LMGetKbdType
(), kUCKeyTranslateNoDeadKeysMask
,
5128 &dummy
, 1, &dummy
, &code
);
5135 is_right_key
= (flags
& NSRightControlKeyMask
) == NSRightControlKeyMask
;
5136 is_left_key
= (flags
& NSLeftControlKeyMask
) == NSLeftControlKeyMask
5137 ||
(! is_right_key
&& (flags
& NSControlKeyMask
) == NSControlKeyMask
);
5140 emacs_event
->modifiers |
= parse_solitary_modifier
5141 (EQ
(ns_right_control_modifier
, Qleft
)
5142 ? ns_control_modifier
5143 : ns_right_control_modifier
);
5146 emacs_event
->modifiers |
= parse_solitary_modifier
5147 (ns_control_modifier
);
5149 if (flags
& NS_FUNCTION_KEY_MASK
&& !fnKeysym
)
5150 emacs_event
->modifiers |
=
5151 parse_solitary_modifier
(ns_function_modifier
);
5153 left_is_none
= NILP
(ns_alternate_modifier
)
5154 || EQ
(ns_alternate_modifier
, Qnone
);
5156 is_right_key
= (flags
& NSRightAlternateKeyMask
)
5157 == NSRightAlternateKeyMask
;
5158 is_left_key
= (flags
& NSLeftAlternateKeyMask
) == NSLeftAlternateKeyMask
5160 && (flags
& NSAlternateKeyMask
) == NSAlternateKeyMask
);
5164 if ((NILP
(ns_right_alternate_modifier
)
5165 || EQ
(ns_right_alternate_modifier
, Qnone
)
5166 ||
(EQ
(ns_right_alternate_modifier
, Qleft
) && left_is_none
))
5168 { /* accept pre-interp alt comb */
5169 if ([[theEvent characters
] length] > 0)
5170 code
= [[theEvent characters
] characterAtIndex
: 0];
5171 /*HACK: clear lone shift modifier to stop next if from firing */
5172 if (emacs_event
->modifiers
== shift_modifier
)
5173 emacs_event
->modifiers
= 0;
5176 emacs_event
->modifiers |
= parse_solitary_modifier
5177 (EQ
(ns_right_alternate_modifier
, Qleft
)
5178 ? ns_alternate_modifier
5179 : ns_right_alternate_modifier
);
5182 if (is_left_key
) /* default = meta */
5184 if (left_is_none
&& !fnKeysym
)
5185 { /* accept pre-interp alt comb */
5186 if ([[theEvent characters
] length] > 0)
5187 code
= [[theEvent characters
] characterAtIndex
: 0];
5188 /*HACK: clear lone shift modifier to stop next if from firing */
5189 if (emacs_event
->modifiers
== shift_modifier
)
5190 emacs_event
->modifiers
= 0;
5193 emacs_event
->modifiers |
=
5194 parse_solitary_modifier
(ns_alternate_modifier
);
5198 fprintf (stderr
, "keyDown
: code
=%x\tfnKey =%x\tflags = %x\tmods = %x\n",
5199 code
, fnKeysym
, flags
, emacs_event
->modifiers
);
5201 /* if it was a function key or had modifiers, pass it directly to emacs */
5202 if (fnKeysym ||
(emacs_event
->modifiers
5203 && (emacs_event
->modifiers
!= shift_modifier
)
5204 && [[theEvent charactersIgnoringModifiers
] length] > 0))
5205 /*[[theEvent characters] length] */
5207 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5209 code |
= (1<<28)|
(3<<16);
5210 else if (code
== 0x7f)
5211 code |
= (1<<28)|
(3<<16);
5213 emacs_event
->kind
= code
> 0xFF
5214 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5216 emacs_event
->code
= code
;
5217 EV_TRAILER
(theEvent
);
5218 processingCompose
= NO
;
5224 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
5225 /* if we get here we should send the key for input manager processing */
5226 /* Disable warning, there is nothing a user can do about it anyway, and
5227 it does not seem to matter. */
5229 if (firstTime
&& [[NSInputManager currentInputManager
]
5230 wantsToDelayTextChangeNotifications
] == NO
)
5232 "Emacs
: WARNING
: TextInput mgr wants marked
text to be permanent
!\n"
);
5236 if (NS_KEYLOG
&& !processingCompose
)
5237 fprintf (stderr
, "keyDown
: Begin compose sequence.
\n"
);
5239 processingCompose
= YES
;
5240 [nsEvArray addObject
: theEvent
];
5241 [self interpretKeyEvents
: nsEvArray
];
5242 [nsEvArray removeObject
: theEvent
];
5246 #ifdef NS_IMPL_COCOA
5247 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
5248 decided not to send key-down for.
5249 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
5250 This only applies on Tiger and earlier.
5251 If it matches one of these, send it on to keyDown. */
5252 -(void
)keyUp
: (NSEvent
*)theEvent
5254 int flags
= [theEvent modifierFlags
];
5255 int code
= [theEvent keyCode
];
5256 if (floor (NSAppKitVersionNumber
) <= 824 /*NSAppKitVersionNumber10_4*/ &&
5257 code
== 0x30 && (flags
& NSControlKeyMask
) && !(flags
& NSCommandKeyMask
))
5260 fprintf (stderr
, "keyUp
: passed test"
);
5261 ns_fake_keydown
= YES
;
5262 [self keyDown
: theEvent
];
5268 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
5271 /* <NSTextInput>: called when done composing;
5272 NOTE: also called when we delete over working text, followed immed.
5273 by doCommandBySelector: deleteBackward: */
5274 - (void
)insertText
: (id
)aString
5277 int len
= [(NSString
*)aString
length];
5281 NSLog
(@"insertText
'%@'\tlen
= %d", aString, len);
5282 processingCompose
= NO
;
5287 /* first, clear any working text */
5288 if (workingText
!= nil
)
5289 [self deleteWorkingText
];
5291 /* now insert the string as keystrokes */
5292 for (i
=0; i
<len
; i
++)
5294 code
= [aString characterAtIndex
: i
];
5295 /* TODO: still need this? */
5297 code
= '~'; /* 0x7E */
5298 if (code
!= 32) /* Space */
5299 emacs_event
->modifiers
= 0;
5301 = code
> 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5302 emacs_event
->code
= code
;
5303 EV_TRAILER
((id
)nil
);
5308 /* <NSTextInput>: inserts display of composing characters */
5309 - (void
)setMarkedText
: (id
)aString selectedRange
: (NSRange
)selRange
5311 NSString
*str
= [aString respondsToSelector
: @selector
(string
)] ?
5312 [aString string
] : aString
;
5314 NSLog
(@"setMarkedText
'%@' len
=%lu range %lu from %lu",
5315 str
, (unsigned
long)[str
length],
5316 (unsigned
long)selRange.
length,
5317 (unsigned
long)selRange.location
);
5319 if (workingText
!= nil
)
5320 [self deleteWorkingText
];
5321 if ([str
length] == 0)
5327 processingCompose
= YES
;
5328 workingText
= [str copy
];
5329 ns_working_text
= build_string
([workingText UTF8String
]);
5331 emacs_event
->kind
= NS_TEXT_EVENT
;
5332 emacs_event
->code
= KEY_NS_PUT_WORKING_TEXT
;
5333 EV_TRAILER
((id
)nil
);
5337 /* delete display of composing characters [not in <NSTextInput>] */
5338 - (void
)deleteWorkingText
5340 if (workingText
== nil
)
5343 NSLog
(@"deleteWorkingText len
=%lu\n", (unsigned long)[workingText length]);
5344 [workingText release
];
5346 processingCompose
= NO
;
5351 emacs_event
->kind
= NS_TEXT_EVENT
;
5352 emacs_event
->code
= KEY_NS_UNPUT_WORKING_TEXT
;
5353 EV_TRAILER
((id
)nil
);
5357 - (BOOL
)hasMarkedText
5359 return workingText
!= nil
;
5363 - (NSRange
)markedRange
5365 NSRange rng
= workingText
!= nil
5366 ? NSMakeRange
(0, [workingText
length]) : NSMakeRange
(NSNotFound
, 0);
5368 NSLog
(@"markedRange request"
);
5376 NSLog
(@"unmark
(accept
) text"
);
5377 [self deleteWorkingText
];
5378 processingCompose
= NO
;
5382 /* used to position char selection windows, etc. */
5383 - (NSRect
)firstRectForCharacterRange
: (NSRange
)theRange
5387 struct window
*win
= XWINDOW
(FRAME_SELECTED_WINDOW
(emacsframe
));
5389 NSLog
(@"firstRectForCharRange request"
);
5391 rect.
size.width
= theRange.
length * FRAME_COLUMN_WIDTH
(emacsframe
);
5392 rect.
size.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5393 pt.x
= WINDOW_TEXT_TO_FRAME_PIXEL_X
(win
, win
->phys_cursor.x
);
5394 pt.y
= WINDOW_TO_FRAME_PIXEL_Y
(win
, win
->phys_cursor.y
5395 +FRAME_LINE_HEIGHT
(emacsframe
));
5397 pt
= [self convertPoint
: pt toView
: nil
];
5398 pt
= [[self window
] convertBaseToScreen
: pt
];
5404 - (NSInteger
)conversationIdentifier
5406 return (NSInteger
)self
;
5410 - (void
)doCommandBySelector
: (SEL
)aSelector
5413 NSLog
(@"doCommandBySelector
: %@", NSStringFromSelector (aSelector));
5415 processingCompose
= NO
;
5416 if (aSelector
== @selector
(deleteBackward
:))
5418 /* happens when user backspaces over an ongoing composition:
5419 throw a 'delete' into the event queue */
5422 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5423 emacs_event
->code
= 0xFF08;
5424 EV_TRAILER
((id
)nil
);
5428 - (NSArray
*)validAttributesForMarkedText
5430 static NSArray
*arr
= nil
;
5431 if (arr
== nil
) arr
= [NSArray new
];
5432 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
5436 - (NSRange
)selectedRange
5439 NSLog
(@"selectedRange request"
);
5440 return NSMakeRange
(NSNotFound
, 0);
5443 #
if defined
(NS_IMPL_COCOA
) || GNUSTEP_GUI_MAJOR_VERSION
> 0 || \
5444 GNUSTEP_GUI_MINOR_VERSION
> 22
5445 - (NSUInteger
)characterIndexForPoint
: (NSPoint
)thePoint
5447 - (unsigned int
)characterIndexForPoint
: (NSPoint
)thePoint
5451 NSLog
(@"characterIndexForPoint request"
);
5455 - (NSAttributedString
*)attributedSubstringFromRange
: (NSRange
)theRange
5457 static NSAttributedString
*str
= nil
;
5458 if (str
== nil
) str
= [NSAttributedString new
];
5460 NSLog
(@"attributedSubstringFromRange request"
);
5464 /* End <NSTextInput> impl. */
5465 /*****************************************************************************/
5468 /* This is what happens when the user presses a mouse button. */
5469 - (void
)mouseDown
: (NSEvent
*)theEvent
5471 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(emacsframe
);
5472 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
5474 NSTRACE
(mouseDown
);
5476 [self deleteWorkingText
];
5481 dpyinfo
->last_mouse_frame
= emacsframe
;
5482 /* appears to be needed to prevent spurious movement events generated on
5484 emacsframe
->mouse_moved
= 0;
5486 if ([theEvent
type] == NSScrollWheel
)
5488 CGFloat delta
= [theEvent deltaY
];
5489 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
5492 emacs_event
->kind
= WHEEL_EVENT
;
5493 emacs_event
->code
= 0;
5494 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
) |
5495 ((delta
> 0) ? up_modifier
: down_modifier
);
5499 emacs_event
->kind
= MOUSE_CLICK_EVENT
;
5500 emacs_event
->code
= EV_BUTTON
(theEvent
);
5501 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
)
5502 | EV_UDMODIFIERS
(theEvent
);
5504 XSETINT
(emacs_event
->x
, lrint
(p.x
));
5505 XSETINT
(emacs_event
->y
, lrint
(p.y
));
5506 EV_TRAILER
(theEvent
);
5510 - (void
)rightMouseDown
: (NSEvent
*)theEvent
5512 NSTRACE
(rightMouseDown
);
5513 [self mouseDown
: theEvent
];
5517 - (void
)otherMouseDown
: (NSEvent
*)theEvent
5519 NSTRACE
(otherMouseDown
);
5520 [self mouseDown
: theEvent
];
5524 - (void
)mouseUp
: (NSEvent
*)theEvent
5527 [self mouseDown
: theEvent
];
5531 - (void
)rightMouseUp
: (NSEvent
*)theEvent
5533 NSTRACE
(rightMouseUp
);
5534 [self mouseDown
: theEvent
];
5538 - (void
)otherMouseUp
: (NSEvent
*)theEvent
5540 NSTRACE
(otherMouseUp
);
5541 [self mouseDown
: theEvent
];
5545 - (void
) scrollWheel
: (NSEvent
*)theEvent
5547 NSTRACE
(scrollWheel
);
5548 [self mouseDown
: theEvent
];
5552 /* Tell emacs the mouse has moved. */
5553 - (void
)mouseMoved
: (NSEvent
*)e
5555 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
5556 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(emacsframe
);
5560 // NSTRACE
(mouseMoved
);
5562 dpyinfo
->last_mouse_movement_time
= EV_TIMESTAMP
(e
);
5563 pt
= [self convertPoint
: [e locationInWindow
] fromView
: nil
];
5564 dpyinfo
->last_mouse_motion_x
= pt.x
;
5565 dpyinfo
->last_mouse_motion_y
= pt.y
;
5567 /* update any mouse face */
5568 if (hlinfo
->mouse_face_hidden
)
5570 hlinfo
->mouse_face_hidden
= 0;
5571 clear_mouse_face
(hlinfo
);
5574 /* tooltip handling */
5575 previous_help_echo_string
= help_echo_string
;
5576 help_echo_string
= Qnil
;
5578 if (!NILP
(Vmouse_autoselect_window
))
5580 NSTRACE
(mouse_autoselect_window
);
5581 static Lisp_Object last_mouse_window
;
5583 = window_from_coordinates
(emacsframe
, pt.x
, pt.y
, 0, 0);
5585 if (WINDOWP
(window
)
5586 && !EQ
(window
, last_mouse_window
)
5587 && !EQ
(window
, selected_window
)
5588 && (focus_follows_mouse
5589 ||
(EQ
(XWINDOW
(window
)->frame
,
5590 XWINDOW
(selected_window
)->frame
))))
5592 NSTRACE
(in_window
);
5593 emacs_event
->kind
= SELECT_WINDOW_EVENT
;
5594 emacs_event
->frame_or_window
= window
;
5597 /* Remember the last window where we saw the mouse. */
5598 last_mouse_window
= window
;
5601 if (!note_mouse_movement
(emacsframe
, pt.x
, pt.y
))
5602 help_echo_string
= previous_help_echo_string
;
5604 XSETFRAME
(frame
, emacsframe
);
5605 if (!NILP
(help_echo_string
) ||
!NILP
(previous_help_echo_string
))
5607 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
5608 (note_mouse_highlight), which is called through the
5609 note_mouse_movement () call above */
5610 gen_help_event
(help_echo_string
, frame
, help_echo_window
,
5611 help_echo_object
, help_echo_pos
);
5615 help_echo_string
= Qnil
;
5616 gen_help_event
(Qnil
, frame
, Qnil
, Qnil
, 0);
5619 if (emacsframe
->mouse_moved
&& send_appdefined
)
5620 ns_send_appdefined
(-1);
5624 - (void
)mouseDragged
: (NSEvent
*)e
5626 NSTRACE
(mouseDragged
);
5627 [self mouseMoved
: e
];
5631 - (void
)rightMouseDragged
: (NSEvent
*)e
5633 NSTRACE
(rightMouseDragged
);
5634 [self mouseMoved
: e
];
5638 - (void
)otherMouseDragged
: (NSEvent
*)e
5640 NSTRACE
(otherMouseDragged
);
5641 [self mouseMoved
: e
];
5645 - (BOOL
)windowShouldClose
: (id
)sender
5647 NSEvent
*e
=[[self window
] currentEvent
];
5649 NSTRACE
(windowShouldClose
);
5650 windowClosing
= YES
;
5653 emacs_event
->kind
= DELETE_WINDOW_EVENT
;
5654 emacs_event
->modifiers
= 0;
5655 emacs_event
->code
= 0;
5657 /* Don't close this window, let this be done from lisp code. */
5661 - (void
) updateFrameSize
: (BOOL
) delay
;
5663 NSWindow
*window
= [self window
];
5664 NSRect wr
= [window frame
];
5666 int oldc
= cols
, oldr
= rows
;
5667 int oldw
= FRAME_PIXEL_WIDTH
(emacsframe
),
5668 oldh
= FRAME_PIXEL_HEIGHT
(emacsframe
);
5671 if (! [self isFullscreen
])
5673 extra
= FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5674 + FRAME_TOOLBAR_HEIGHT
(emacsframe
);
5677 neww
= (int
)wr.
size.width
- emacsframe
->border_width
;
5678 newh
= (int
)wr.
size.height
- extra
;
5680 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
, neww
);
5681 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
, newh
);
5683 if (cols
< MINWIDTH
)
5686 if (rows
< MINHEIGHT
)
5689 if (oldr
!= rows || oldc
!= cols || neww
!= oldw || newh
!= oldh
)
5691 NSView
*view = FRAME_NS_VIEW
(emacsframe
);
5692 NSWindow
*win
= [view window
];
5693 NSSize sz
= [win resizeIncrements
];
5695 change_frame_size
(emacsframe
,
5696 FRAME_PIXEL_TO_TEXT_WIDTH
(emacsframe
, neww
),
5697 FRAME_PIXEL_TO_TEXT_HEIGHT
(emacsframe
, newh
),
5699 SET_FRAME_GARBAGED
(emacsframe
);
5700 cancel_mouse_face
(emacsframe
);
5702 // Did resize increments change because of a font change?
5703 if (sz.width
!= FRAME_COLUMN_WIDTH
(emacsframe
) ||
5704 sz.height
!= FRAME_LINE_HEIGHT
(emacsframe
))
5706 sz.width
= FRAME_COLUMN_WIDTH
(emacsframe
);
5707 sz.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5708 [win setResizeIncrements
: sz
];
5711 [view setFrame
: NSMakeRect
(0, 0, neww
, newh
)];
5712 [self windowDidMove
:nil
]; // Update top
/left.
5716 - (NSSize
)windowWillResize
: (NSWindow
*)sender toSize
: (NSSize
)frameSize
5717 /* normalize frame to gridded text size */
5721 NSTRACE
(windowWillResize
);
5722 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5724 if (fs_state
== FULLSCREEN_MAXIMIZED
5725 && (maximized_width
!= (int
)frameSize.width
5726 || maximized_height
!= (int
)frameSize.height
))
5727 [self setFSValue
: FULLSCREEN_NONE
];
5728 else if (fs_state
== FULLSCREEN_WIDTH
5729 && maximized_width
!= (int
)frameSize.width
)
5730 [self setFSValue
: FULLSCREEN_NONE
];
5731 else if (fs_state
== FULLSCREEN_HEIGHT
5732 && maximized_height
!= (int
)frameSize.height
)
5733 [self setFSValue
: FULLSCREEN_NONE
];
5734 if (fs_state
== FULLSCREEN_NONE
)
5735 maximized_width
= maximized_height
= -1;
5737 if (! [self isFullscreen
])
5739 extra
= FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5740 + FRAME_TOOLBAR_HEIGHT
(emacsframe
);
5743 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
, frameSize.width
);
5744 if (cols
< MINWIDTH
)
5747 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
,
5748 frameSize.height
- extra
);
5749 if (rows
< MINHEIGHT
)
5751 #ifdef NS_IMPL_COCOA
5753 /* this sets window title to have size in it; the wm does this under GS */
5754 NSRect r
= [[self window
] frame
];
5755 if (r.
size.height
== frameSize.height
&& r.
size.width
== frameSize.width
)
5763 else if (fs_state
== FULLSCREEN_NONE
&& ! maximizing_resize
)
5766 NSWindow
*window
= [self window
];
5769 char *t
= strdup
([[[self window
] title] UTF8String
]);
5770 char *pos
= strstr
(t
, " — "
);
5775 size_title
= xmalloc
(strlen
(old_title
) + 40);
5776 esprintf
(size_title
, "
%s — (%d x %d)", old_title, cols, rows);
5777 [window setTitle
: [NSString stringWithUTF8String
: size_title
]];
5782 #endif
/* NS_IMPL_COCOA */
5783 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5789 - (void
)windowDidResize
: (NSNotification
*)notification
5791 if (! [self fsIsNative
])
5793 NSWindow
*theWindow
= [notification object
];
5794 /* We can get notification on the non-FS window when in
5796 if ([self window
] != theWindow
) return;
5799 #ifdef NS_IMPL_GNUSTEP
5800 NSWindow
*theWindow
= [notification object
];
5802 /* In GNUstep, at least currently, it's possible to get a didResize
5803 without getting a willResize.. therefore we need to act as if we got
5804 the willResize now */
5805 NSSize sz
= [theWindow frame
].
size;
5806 sz
= [self windowWillResize
: theWindow toSize
: sz
];
5807 #endif
/* NS_IMPL_GNUSTEP */
5809 NSTRACE
(windowDidResize
);
5810 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5812 if (cols
> 0 && rows
> 0)
5814 [self updateFrameSize
: YES
];
5817 ns_send_appdefined
(-1);
5820 #ifdef NS_IMPL_COCOA
5821 - (void
)viewDidEndLiveResize
5823 [super viewDidEndLiveResize
];
5826 [[self window
] setTitle
: [NSString stringWithUTF8String
: old_title
]];
5830 maximizing_resize
= NO
;
5832 #endif
/* NS_IMPL_COCOA */
5835 - (void
)windowDidBecomeKey
: (NSNotification
*)notification
5836 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5838 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(emacsframe
);
5839 struct frame
*old_focus
= dpyinfo
->x_focus_frame
;
5841 NSTRACE
(windowDidBecomeKey
);
5843 if (emacsframe
!= old_focus
)
5844 dpyinfo
->x_focus_frame
= emacsframe
;
5846 ns_frame_rehighlight
(emacsframe
);
5850 emacs_event
->kind
= FOCUS_IN_EVENT
;
5851 EV_TRAILER
((id
)nil
);
5856 - (void
)windowDidResignKey
: (NSNotification
*)notification
5857 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5859 struct ns_display_info
*dpyinfo
= FRAME_DISPLAY_INFO
(emacsframe
);
5860 BOOL is_focus_frame
= dpyinfo
->x_focus_frame
== emacsframe
;
5861 NSTRACE
(windowDidResignKey
);
5864 dpyinfo
->x_focus_frame
= 0;
5866 ns_frame_rehighlight
(emacsframe
);
5868 /* FIXME: for some reason needed on second and subsequent clicks away
5869 from sole-frame Emacs to get hollow box to show */
5870 if (!windowClosing
&& [[self window
] isVisible
] == YES
)
5872 x_update_cursor
(emacsframe
, 1);
5873 x_set_frame_alpha
(emacsframe
);
5876 if (emacs_event
&& is_focus_frame
)
5878 [self deleteWorkingText
];
5879 emacs_event
->kind
= FOCUS_OUT_EVENT
;
5880 EV_TRAILER
((id
)nil
);
5885 - (void
)windowWillMiniaturize
: sender
5887 NSTRACE
(windowWillMiniaturize
);
5903 - initFrameFromEmacs
: (struct frame
*)f
5912 NSTRACE
(initFrameFromEmacs
);
5915 processingCompose
= NO
;
5916 scrollbarsNeedingUpdate
= 0;
5917 fs_state
= FULLSCREEN_NONE
;
5918 fs_before_fs
= next_maximized
= -1;
5919 #ifdef HAVE_NATIVE_FS
5920 fs_is_native
= ns_use_native_fullscreen
;
5924 maximized_width
= maximized_height
= -1;
5927 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5929 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
5930 r
= NSMakeRect
(0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, f
->text_cols
),
5931 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, f
->text_lines
));
5932 [self initWithFrame
: r
];
5933 [self setAutoresizingMask
: NSViewWidthSizable | NSViewHeightSizable
];
5935 FRAME_NS_VIEW
(f
) = self
;
5937 #ifdef NS_IMPL_COCOA
5939 maximizing_resize
= NO
;
5942 win
= [[EmacsWindow alloc
]
5943 initWithContentRect
: r
5944 styleMask
: (NSResizableWindowMask |
5945 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_7
5946 NSTitledWindowMask |
5948 NSMiniaturizableWindowMask |
5949 NSClosableWindowMask
)
5950 backing
: NSBackingStoreBuffered
5953 #ifdef HAVE_NATIVE_FS
5954 [win setCollectionBehavior
:NSWindowCollectionBehaviorFullScreenPrimary
];
5958 bwidth
= f
->border_width
= wr.
size.width
- r.
size.width
;
5959 tibar_height
= FRAME_NS_TITLEBAR_HEIGHT
(f
) = wr.
size.height
- r.
size.height
;
5961 [win setAcceptsMouseMovedEvents
: YES
];
5962 [win setDelegate
: self
];
5963 [win useOptimizedDrawing
: YES
];
5965 sz.width
= FRAME_COLUMN_WIDTH
(f
);
5966 sz.height
= FRAME_LINE_HEIGHT
(f
);
5967 [win setResizeIncrements
: sz
];
5969 [[win contentView
] addSubview
: self
];
5972 [self registerForDraggedTypes
: ns_drag_types
];
5975 name
= [NSString stringWithUTF8String
:
5976 NILP
(tem
) ? "Emacs"
: SSDATA
(tem
)];
5977 [win setTitle
: name
];
5979 /* toolbar support */
5980 toolbar
= [[EmacsToolbar alloc
] initForView
: self withIdentifier
:
5981 [NSString stringWithFormat
: @"Emacs Frame
%d",
5983 [win setToolbar
: toolbar
];
5984 [toolbar setVisible
: NO
];
5985 #ifdef NS_IMPL_COCOA
5987 NSButton
*toggleButton
;
5988 toggleButton
= [win standardWindowButton
: NSWindowToolbarButton
];
5989 [toggleButton setTarget
: self
];
5990 [toggleButton setAction
: @selector
(toggleToolbar
: )];
5993 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
5997 [win setMiniwindowTitle
:
5998 [NSString stringWithUTF8String
: SSDATA
(tem
)]];
6001 NSScreen
*screen
= [win screen
];
6004 [win setFrameTopLeftPoint
: NSMakePoint
6005 (IN_BOUND
(-SCREENMAX
, f
->left_pos
, SCREENMAX
),
6006 IN_BOUND
(-SCREENMAX
,
6007 [screen frame
].
size.height
- NS_TOP_POS
(f
), SCREENMAX
))];
6010 [win makeFirstResponder
: self
];
6012 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
6013 (FRAME_DEFAULT_FACE
(emacsframe
)), emacsframe
);
6014 [win setBackgroundColor
: col
];
6015 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
6016 [win setOpaque
: NO
];
6018 [self allocateGState
];
6020 [NSApp registerServicesMenuSendTypes
: ns_send_types
6028 - (void
)windowDidMove
: sender
6030 NSWindow
*win
= [self window
];
6031 NSRect r
= [win frame
];
6032 NSArray
*screens
= [NSScreen screens
];
6033 NSScreen
*screen
= [screens objectAtIndex
: 0];
6035 NSTRACE
(windowDidMove
);
6037 if (!emacsframe
->output_data.ns
)
6041 emacsframe
->left_pos
= r.origin.x
;
6042 emacsframe
->top_pos
=
6043 [screen frame
].
size.height
- (r.origin.y
+ r.
size.height
);
6048 /* Called AFTER method below, but before our windowWillResize call there leads
6049 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
6050 location so set_window_size moves the frame. */
6051 - (BOOL
)windowShouldZoom
: (NSWindow
*)sender toFrame
: (NSRect
)newFrame
6053 emacsframe
->output_data.ns
->zooming
= 1;
6058 /* Override to do something slightly nonstandard, but nice. First click on
6059 zoom button will zoom vertically. Second will zoom completely. Third
6060 returns to original. */
6061 - (NSRect
)windowWillUseStandardFrame
:(NSWindow
*)sender
6062 defaultFrame
:(NSRect
)defaultFrame
6064 NSRect result
= [sender frame
];
6066 NSTRACE
(windowWillUseStandardFrame
);
6068 if (fs_before_fs
!= -1) /* Entering fullscreen */
6070 result
= defaultFrame
;
6072 else if (next_maximized
== FULLSCREEN_HEIGHT
6073 ||
(next_maximized
== -1
6074 && abs (defaultFrame.
size.height
- result.
size.height
)
6075 > FRAME_LINE_HEIGHT
(emacsframe
)))
6078 ns_userRect
= result
;
6079 maximized_height
= result.
size.height
= defaultFrame.
size.height
;
6080 maximized_width
= -1;
6081 result.origin.y
= defaultFrame.origin.y
;
6082 [self setFSValue
: FULLSCREEN_HEIGHT
];
6083 #ifdef NS_IMPL_COCOA
6084 maximizing_resize
= YES
;
6087 else if (next_maximized
== FULLSCREEN_WIDTH
)
6089 ns_userRect
= result
;
6090 maximized_width
= result.
size.width
= defaultFrame.
size.width
;
6091 maximized_height
= -1;
6092 result.origin.x
= defaultFrame.origin.x
;
6093 [self setFSValue
: FULLSCREEN_WIDTH
];
6095 else if (next_maximized
== FULLSCREEN_MAXIMIZED
6096 ||
(next_maximized
== -1
6097 && abs (defaultFrame.
size.width
- result.
size.width
)
6098 > FRAME_COLUMN_WIDTH
(emacsframe
)))
6100 result
= defaultFrame
; /* second click */
6101 maximized_width
= result.
size.width
;
6102 maximized_height
= result.
size.height
;
6103 [self setFSValue
: FULLSCREEN_MAXIMIZED
];
6104 #ifdef NS_IMPL_COCOA
6105 maximizing_resize
= YES
;
6111 result
= ns_userRect.
size.height ? ns_userRect
: result
;
6112 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
6113 #ifdef NS_IMPL_COCOA
6114 maximizing_resize
= fs_state
!= FULLSCREEN_NONE
;
6116 [self setFSValue
: FULLSCREEN_NONE
];
6117 maximized_width
= maximized_height
= -1;
6120 if (fs_before_fs
== -1) next_maximized
= -1;
6121 [self windowWillResize
: sender toSize
: result.
size];
6126 - (void
)windowDidDeminiaturize
: sender
6128 NSTRACE
(windowDidDeminiaturize
);
6129 if (!emacsframe
->output_data.ns
)
6132 SET_FRAME_ICONIFIED
(emacsframe
, 0);
6133 SET_FRAME_VISIBLE
(emacsframe
, 1);
6134 windows_or_buffers_changed
= 63;
6138 emacs_event
->kind
= DEICONIFY_EVENT
;
6139 EV_TRAILER
((id
)nil
);
6144 - (void
)windowDidExpose
: sender
6146 NSTRACE
(windowDidExpose
);
6147 if (!emacsframe
->output_data.ns
)
6150 SET_FRAME_VISIBLE
(emacsframe
, 1);
6151 SET_FRAME_GARBAGED
(emacsframe
);
6153 if (send_appdefined
)
6154 ns_send_appdefined
(-1);
6158 - (void
)windowDidMiniaturize
: sender
6160 NSTRACE
(windowDidMiniaturize
);
6161 if (!emacsframe
->output_data.ns
)
6164 SET_FRAME_ICONIFIED
(emacsframe
, 1);
6165 SET_FRAME_VISIBLE
(emacsframe
, 0);
6169 emacs_event
->kind
= ICONIFY_EVENT
;
6170 EV_TRAILER
((id
)nil
);
6174 #ifdef HAVE_NATIVE_FS
6175 - (NSApplicationPresentationOptions
)window
:(NSWindow
*)window
6176 willUseFullScreenPresentationOptions
:
6177 (NSApplicationPresentationOptions
)proposedOptions
6179 return proposedOptions|NSApplicationPresentationAutoHideToolbar
;
6183 - (void
)windowWillEnterFullScreen
:(NSNotification
*)notification
6185 fs_before_fs
= fs_state
;
6188 - (void
)windowDidEnterFullScreen
:(NSNotification
*)notification
6190 [self setFSValue
: FULLSCREEN_BOTH
];
6191 if (! [self fsIsNative
])
6193 [self windowDidBecomeKey
:notification
];
6194 [nonfs_window orderOut
:self
];
6198 BOOL tbar_visible
= FRAME_EXTERNAL_TOOL_BAR
(emacsframe
) ? YES
: NO
;
6199 #ifdef NS_IMPL_COCOA
6200 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_7
6201 unsigned val
= (unsigned
)[NSApp presentationOptions
];
6203 // OSX
10.7 bug
fix, the
menu won
't appear without this.
6204 // val is non-zero on other OSX versions.
6207 NSApplicationPresentationOptions options
6208 = NSApplicationPresentationAutoHideDock
6209 | NSApplicationPresentationAutoHideMenuBar
6210 | NSApplicationPresentationFullScreen
6211 | NSApplicationPresentationAutoHideToolbar;
6213 [NSApp setPresentationOptions: options];
6217 [toolbar setVisible:tbar_visible];
6221 - (void)windowWillExitFullScreen:(NSNotification *)notification
6223 if (next_maximized != -1)
6224 fs_before_fs = next_maximized;
6227 - (void)windowDidExitFullScreen:(NSNotification *)notification
6229 [self setFSValue: fs_before_fs];
6231 #ifdef HAVE_NATIVE_FS
6232 [self updateCollectionBehaviour];
6234 if (FRAME_EXTERNAL_TOOL_BAR (emacsframe))
6236 [toolbar setVisible:YES];
6237 update_frame_tool_bar (emacsframe);
6238 [self updateFrameSize:YES];
6239 [[self window] display];
6242 [toolbar setVisible:NO];
6244 if (next_maximized != -1)
6245 [[self window] performZoom:self];
6250 return fs_is_native;
6253 - (BOOL)isFullscreen
6255 if (! fs_is_native) return nonfs_window != nil;
6256 #ifdef HAVE_NATIVE_FS
6257 return ([[self window] styleMask] & NSFullScreenWindowMask) != 0;
6263 #ifdef HAVE_NATIVE_FS
6264 - (void)updateCollectionBehaviour
6266 if (! [self isFullscreen])
6268 NSWindow *win = [self window];
6269 NSWindowCollectionBehavior b = [win collectionBehavior];
6270 if (ns_use_native_fullscreen)
6271 b |= NSWindowCollectionBehaviorFullScreenPrimary;
6273 b &= ~NSWindowCollectionBehaviorFullScreenPrimary;
6275 [win setCollectionBehavior: b];
6276 fs_is_native = ns_use_native_fullscreen;
6281 - (void)toggleFullScreen: (id)sender
6292 #ifdef HAVE_NATIVE_FS
6293 [[self window] toggleFullScreen:sender];
6299 onFirstScreen = [[w screen] isEqual:[[NSScreen screens] objectAtIndex:0]];
6302 col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
6303 (FRAME_DEFAULT_FACE (f)),
6306 sz.width = FRAME_COLUMN_WIDTH (f);
6307 sz.height = FRAME_LINE_HEIGHT (f);
6309 if (fs_state != FULLSCREEN_BOTH)
6311 /* Hide dock and menubar if we are on the primary screen. */
6314 #if defined (NS_IMPL_COCOA) && \
6315 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
6316 NSApplicationPresentationOptions options
6317 = NSApplicationPresentationAutoHideDock
6318 | NSApplicationPresentationAutoHideMenuBar;
6320 [NSApp setPresentationOptions: options];
6322 [NSMenu setMenuBarVisible:NO];
6326 fw = [[EmacsFSWindow alloc]
6327 initWithContentRect:[w contentRectForFrameRect:wr]
6328 styleMask:NSBorderlessWindowMask
6329 backing:NSBackingStoreBuffered
6333 [fw setContentView:[w contentView]];
6334 [fw setTitle:[w title]];
6335 [fw setDelegate:self];
6336 [fw setAcceptsMouseMovedEvents: YES];
6337 [fw useOptimizedDrawing: YES];
6338 [fw setResizeIncrements: sz];
6339 [fw setBackgroundColor: col];
6340 if ([col alphaComponent] != (EmacsCGFloat) 1.0)
6343 f->border_width = 0;
6344 FRAME_NS_TITLEBAR_HEIGHT (f) = 0;
6345 tobar_height = FRAME_TOOLBAR_HEIGHT (f);
6346 FRAME_TOOLBAR_HEIGHT (f) = 0;
6350 [self windowWillEnterFullScreen:nil];
6351 [fw makeKeyAndOrderFront:NSApp];
6352 [fw makeFirstResponder:self];
6354 r = [fw frameRectForContentRect:[[fw screen] frame]];
6355 [fw setFrame: r display:YES animate:YES];
6356 [self windowDidEnterFullScreen:nil];
6367 #if defined (NS_IMPL_COCOA) && \
6368 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
6369 [NSApp setPresentationOptions: NSApplicationPresentationDefault];
6371 [NSMenu setMenuBarVisible:YES];
6375 [w setContentView:[fw contentView]];
6376 [w setResizeIncrements: sz];
6377 [w setBackgroundColor: col];
6378 if ([col alphaComponent] != (EmacsCGFloat) 1.0)
6381 f->border_width = bwidth;
6382 FRAME_NS_TITLEBAR_HEIGHT (f) = tibar_height;
6383 if (FRAME_EXTERNAL_TOOL_BAR (f))
6384 FRAME_TOOLBAR_HEIGHT (f) = tobar_height;
6386 [self windowWillExitFullScreen:nil];
6387 [fw setFrame: [w frame] display:YES animate:YES];
6389 [w makeKeyAndOrderFront:NSApp];
6390 [self windowDidExitFullScreen:nil];
6391 [self updateFrameSize:YES];
6397 if (fs_state != emacsframe->want_fullscreen)
6399 if (fs_state == FULLSCREEN_BOTH)
6401 [self toggleFullScreen:self];
6404 switch (emacsframe->want_fullscreen)
6406 case FULLSCREEN_BOTH:
6407 [self toggleFullScreen:self];
6409 case FULLSCREEN_WIDTH:
6410 next_maximized = FULLSCREEN_WIDTH;
6411 if (fs_state != FULLSCREEN_BOTH)
6412 [[self window] performZoom:self];
6414 case FULLSCREEN_HEIGHT:
6415 next_maximized = FULLSCREEN_HEIGHT;
6416 if (fs_state != FULLSCREEN_BOTH)
6417 [[self window] performZoom:self];
6419 case FULLSCREEN_MAXIMIZED:
6420 next_maximized = FULLSCREEN_MAXIMIZED;
6421 if (fs_state != FULLSCREEN_BOTH)
6422 [[self window] performZoom:self];
6424 case FULLSCREEN_NONE:
6425 if (fs_state != FULLSCREEN_BOTH)
6427 next_maximized = FULLSCREEN_NONE;
6428 [[self window] performZoom:self];
6433 emacsframe->want_fullscreen = FULLSCREEN_NONE;
6438 - (void) setFSValue: (int)value
6440 Lisp_Object lval = Qnil;
6443 case FULLSCREEN_BOTH:
6446 case FULLSCREEN_WIDTH:
6449 case FULLSCREEN_HEIGHT:
6452 case FULLSCREEN_MAXIMIZED:
6456 store_frame_param (emacsframe, Qfullscreen, lval);
6460 - (void)mouseEntered: (NSEvent *)theEvent
6462 NSTRACE (mouseEntered);
6464 FRAME_DISPLAY_INFO (emacsframe)->last_mouse_movement_time
6465 = EV_TIMESTAMP (theEvent);
6469 - (void)mouseExited: (NSEvent *)theEvent
6471 Mouse_HLInfo *hlinfo = emacsframe ? MOUSE_HL_INFO (emacsframe) : NULL;
6473 NSTRACE (mouseExited);
6478 FRAME_DISPLAY_INFO (emacsframe)->last_mouse_movement_time
6479 = EV_TIMESTAMP (theEvent);
6481 if (emacsframe == hlinfo->mouse_face_mouse_frame)
6483 clear_mouse_face (hlinfo);
6484 hlinfo->mouse_face_mouse_frame = 0;
6492 if (context_menu_value == -1)
6493 context_menu_value = [sender tag];
6496 NSInteger tag = [sender tag];
6497 find_and_call_menu_selection (emacsframe, emacsframe->menu_bar_items_used,
6498 emacsframe->menu_bar_vector,
6502 ns_send_appdefined (-1);
6507 - (EmacsToolbar *)toolbar
6513 /* this gets called on toolbar button click */
6514 - toolbarClicked: (id)item
6517 int idx = [item tag] * TOOL_BAR_ITEM_NSLOTS;
6519 NSTRACE (toolbarClicked);
6524 /* send first event (for some reason two needed) */
6525 theEvent = [[self window] currentEvent];
6526 emacs_event->kind = TOOL_BAR_EVENT;
6527 XSETFRAME (emacs_event->arg, emacsframe);
6528 EV_TRAILER (theEvent);
6530 emacs_event->kind = TOOL_BAR_EVENT;
6531 /* XSETINT (emacs_event->code, 0); */
6532 emacs_event->arg = AREF (emacsframe->tool_bar_items,
6533 idx + TOOL_BAR_ITEM_KEY);
6534 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6535 EV_TRAILER (theEvent);
6540 - toggleToolbar: (id)sender
6545 emacs_event->kind = NS_NONKEY_EVENT;
6546 emacs_event->code = KEY_NS_TOGGLE_TOOLBAR;
6547 EV_TRAILER ((id)nil);
6552 - (void)drawRect: (NSRect)rect
6554 int x = NSMinX (rect), y = NSMinY (rect);
6555 int width = NSWidth (rect), height = NSHeight (rect);
6559 if (!emacsframe || !emacsframe->output_data.ns)
6562 ns_clear_frame_area (emacsframe, x, y, width, height);
6563 expose_frame (emacsframe, x, y, width, height);
6566 drawRect: may be called (at least in OS X 10.5) for invisible
6567 views as well for some reason. Thus, do not infer visibility
6570 emacsframe->async_visible = 1;
6571 emacsframe->async_iconified = 0;
6576 /* NSDraggingDestination protocol methods. Actually this is not really a
6577 protocol, but a category of Object. O well... */
6579 -(NSUInteger) draggingEntered: (id <NSDraggingInfo>) sender
6581 NSTRACE (draggingEntered);
6582 return NSDragOperationGeneric;
6586 -(BOOL)prepareForDragOperation: (id <NSDraggingInfo>) sender
6592 -(BOOL)performDragOperation: (id <NSDraggingInfo>) sender
6597 NSEvent *theEvent = [[self window] currentEvent];
6600 NSTRACE (performDragOperation);
6605 position = [self convertPoint: [sender draggingLocation] fromView: nil];
6606 x = lrint (position.x); y = lrint (position.y);
6608 pb = [sender draggingPasteboard];
6609 type = [pb availableTypeFromArray: ns_drag_types];
6614 else if ([type isEqualToString: NSFilenamesPboardType])
6617 NSEnumerator *fenum;
6620 if (!(files = [pb propertyListForType: type]))
6623 fenum = [files objectEnumerator];
6624 while ( (file = [fenum nextObject]) )
6626 emacs_event->kind = NS_NONKEY_EVENT;
6627 emacs_event->code = KEY_NS_DRAG_FILE;
6628 XSETINT (emacs_event->x, x);
6629 XSETINT (emacs_event->y, y);
6630 ns_input_file = append2 (ns_input_file,
6631 build_string ([file UTF8String]));
6632 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6633 EV_TRAILER (theEvent);
6637 else if ([type isEqualToString: NSURLPboardType])
6642 if (!(fileURL = [NSURL URLFromPasteboard: pb]) ||
6643 [fileURL isFileURL] == NO)
6646 file = [fileURL path];
6647 emacs_event->kind = NS_NONKEY_EVENT;
6648 emacs_event->code = KEY_NS_DRAG_FILE;
6649 XSETINT (emacs_event->x, x);
6650 XSETINT (emacs_event->y, y);
6651 ns_input_file = append2 (ns_input_file, build_string ([file UTF8String]));
6652 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6653 EV_TRAILER (theEvent);
6656 else if ([type isEqualToString: NSStringPboardType]
6657 || [type isEqualToString: NSTabularTextPboardType])
6661 if (! (data = [pb stringForType: type]))
6664 emacs_event->kind = NS_NONKEY_EVENT;
6665 emacs_event->code = KEY_NS_DRAG_TEXT;
6666 XSETINT (emacs_event->x, x);
6667 XSETINT (emacs_event->y, y);
6668 ns_input_text = build_string ([data UTF8String]);
6669 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6670 EV_TRAILER (theEvent);
6673 else if ([type isEqualToString: NSColorPboardType])
6675 NSColor *c = [NSColor colorFromPasteboard: pb];
6676 emacs_event->kind = NS_NONKEY_EVENT;
6677 emacs_event->code = KEY_NS_DRAG_COLOR;
6678 XSETINT (emacs_event->x, x);
6679 XSETINT (emacs_event->y, y);
6680 ns_input_color = ns_color_to_lisp (c);
6681 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6682 EV_TRAILER (theEvent);
6685 else if ([type isEqualToString: NSFontPboardType])
6687 /* impl based on GNUstep NSTextView.m */
6688 NSData *data = [pb dataForType: NSFontPboardType];
6689 NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: data];
6690 NSFont *font = [dict objectForKey: NSFontAttributeName];
6696 emacs_event->kind = NS_NONKEY_EVENT;
6697 emacs_event->code = KEY_NS_CHANGE_FONT;
6698 XSETINT (emacs_event->x, x);
6699 XSETINT (emacs_event->y, y);
6700 ns_input_font = build_string ([[font fontName] UTF8String]);
6701 snprintf (fontSize, 10, "%f", [font pointSize]);
6702 ns_input_fontsize = build_string (fontSize);
6703 emacs_event->modifiers = EV_MODIFIERS (theEvent);
6704 EV_TRAILER (theEvent);
6709 error ("Invalid data type in dragging pasteboard.");
6715 - (id) validRequestorForSendType: (NSString *)typeSent
6716 returnType: (NSString *)typeReturned
6718 NSTRACE (validRequestorForSendType);
6719 if (typeSent != nil && [ns_send_types indexOfObject: typeSent] != NSNotFound
6720 && typeReturned == nil)
6722 if (! NILP (ns_get_local_selection (QPRIMARY, QUTF8_STRING)))
6726 return [super validRequestorForSendType: typeSent
6727 returnType: typeReturned];
6731 /* The next two methods are part of NSServicesRequests informal protocol,
6732 supposedly called when a services menu item is chosen from this app.
6733 But this should not happen because we override the services menu with our
6734 own entries which call ns-perform-service.
6735 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
6736 So let's at least stub them out until further investigation can be done. */
6738 - (BOOL
) readSelectionFromPasteboard
: (NSPasteboard
*)pb
6740 /* we could call ns_string_from_pasteboard(pboard) here but then it should
6741 be written into the buffer in place of the existing selection..
6742 ordinary service calls go through functions defined in ns-win.el */
6746 - (BOOL
) writeSelectionToPasteboard
: (NSPasteboard
*)pb types
: (NSArray
*)types
6748 NSArray
*typesDeclared
;
6751 /* We only support NSStringPboardType */
6752 if ([types containsObject
:NSStringPboardType
] == NO
) {
6756 val
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6757 if (CONSP
(val
) && SYMBOLP
(XCAR
(val
)))
6760 if (CONSP
(val
) && NILP
(XCDR
(val
)))
6763 if (! STRINGP
(val
))
6766 typesDeclared
= [NSArray arrayWithObject
:NSStringPboardType
];
6767 [pb declareTypes
:typesDeclared owner
:nil
];
6768 ns_string_to_pasteboard
(pb
, val
);
6773 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
6774 (gives a miniaturized version of the window); currently we use the latter for
6775 frames whose active buffer doesn't correspond to any file
6776 (e.g., '*scratch*') */
6777 - setMiniwindowImage
: (BOOL
) setMini
6779 id
image = [[self window
] miniwindowImage
];
6780 NSTRACE
(setMiniwindowImage
);
6782 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
6783 about "AppleDockIconEnabled" notwithstanding, however the set message
6784 below has its effect nonetheless. */
6785 if (image != emacsframe
->output_data.ns
->miniimage
)
6787 if (image && [image isKindOfClass
: [EmacsImage
class]])
6789 [[self window
] setMiniwindowImage
:
6790 setMini ? emacsframe
->output_data.ns
->miniimage
: nil
];
6797 - (void
) setRows
: (int
) r andColumns
: (int
) c
6803 @
end /* EmacsView */
6807 /* ==========================================================================
6809 EmacsWindow implementation
6811 ========================================================================== */
6813 @implementation EmacsWindow
6815 #ifdef NS_IMPL_COCOA
6816 - (id
)accessibilityAttributeValue
:(NSString
*)attribute
6818 Lisp_Object str
= Qnil
;
6819 struct frame
*f
= SELECTED_FRAME
();
6820 struct buffer
*curbuf
= XBUFFER
(XWINDOW
(f
->selected_window
)->contents
);
6822 if ([attribute isEqualToString
:NSAccessibilityRoleAttribute
])
6823 return NSAccessibilityTextFieldRole
;
6825 if ([attribute isEqualToString
:NSAccessibilitySelectedTextAttribute
]
6826 && curbuf
&& ! NILP
(BVAR
(curbuf
, mark_active
)))
6828 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6830 else if (curbuf
&& [attribute isEqualToString
:NSAccessibilityValueAttribute
])
6832 if (! NILP
(BVAR
(curbuf
, mark_active
)))
6833 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6837 ptrdiff_t start_byte
= BUF_BEGV_BYTE
(curbuf
);
6838 ptrdiff_t byte_range
= BUF_ZV_BYTE
(curbuf
) - start_byte
;
6839 ptrdiff_t range
= BUF_ZV
(curbuf
) - BUF_BEGV
(curbuf
);
6841 if (! NILP
(BVAR
(curbuf
, enable_multibyte_characters
)))
6842 str
= make_uninit_multibyte_string
(range
, byte_range
);
6844 str
= make_uninit_string
(range
);
6845 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
6846 Is this a problem? */
6847 memcpy
(SDATA
(str
), BYTE_POS_ADDR
(start_byte
), byte_range
);
6854 if (CONSP
(str
) && SYMBOLP
(XCAR
(str
)))
6857 if (CONSP
(str
) && NILP
(XCDR
(str
)))
6862 const
char *utfStr
= SSDATA
(str
);
6863 NSString
*nsStr
= [NSString stringWithUTF8String
: utfStr
];
6868 return [super accessibilityAttributeValue
:attribute
];
6870 #endif
/* NS_IMPL_COCOA */
6872 /* If we have multiple monitors, one above the other, we don't want to
6873 restrict the height to just one monitor. So we override this. */
6874 - (NSRect
)constrainFrameRect
:(NSRect
)frameRect toScreen
:(NSScreen
*)screen
6876 /* When making the frame visible for the first time or if there is just
6877 one screen, we want to constrain. Other times not. */
6878 NSUInteger nr_screens
= [[NSScreen screens
] count
];
6879 struct frame
*f
= ((EmacsView
*)[self delegate
])->emacsframe
;
6880 NSTRACE
(constrainFrameRect
);
6882 if (nr_screens
== 1)
6884 NSRect r
= [super constrainFrameRect
:frameRect toScreen
:screen
];
6888 if (f
->output_data.ns
->dont_constrain
6889 || ns_menu_bar_should_be_hidden
())
6892 f
->output_data.ns
->dont_constrain
= 1;
6893 return [super constrainFrameRect
:frameRect toScreen
:screen
];
6896 @
end /* EmacsWindow */
6899 @implementation EmacsFSWindow
6901 - (BOOL
)canBecomeKeyWindow
6906 - (BOOL
)canBecomeMainWindow
6913 /* ==========================================================================
6915 EmacsScroller implementation
6917 ========================================================================== */
6920 @implementation EmacsScroller
6922 /* for repeat button push */
6923 #define SCROLL_BAR_FIRST_DELAY
0.5
6924 #define SCROLL_BAR_CONTINUOUS_DELAY
(1.0 / 15)
6926 + (CGFloat
) scrollerWidth
6928 /* TODO: if we want to allow variable widths, this is the place to do it,
6929 however neither GNUstep nor Cocoa support it very well */
6930 return [NSScroller scrollerWidth
];
6934 - initFrame
: (NSRect
)r window
: (Lisp_Object
)nwin
6936 NSTRACE
(EmacsScroller_initFrame
);
6938 r.
size.width
= [EmacsScroller scrollerWidth
];
6939 [super initWithFrame
: r
/*NSMakeRect (0, 0, 0, 0)*/];
6940 [self setContinuous
: YES
];
6941 [self setEnabled
: YES
];
6943 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
6944 locked against the top and bottom edges, and right edge on OS X, where
6945 scrollers are on right. */
6946 #ifdef NS_IMPL_GNUSTEP
6947 [self setAutoresizingMask
: NSViewMaxXMargin | NSViewHeightSizable
];
6949 [self setAutoresizingMask
: NSViewMinXMargin | NSViewHeightSizable
];
6954 pixel_height
= NSHeight
(r
);
6955 if (pixel_height
== 0) pixel_height
= 1;
6956 min_portion
= 20 / pixel_height
;
6958 frame
= XFRAME
(XWINDOW
(win
)->frame
);
6959 if (FRAME_LIVE_P
(frame
))
6962 EmacsView
*view = FRAME_NS_VIEW
(frame
);
6963 NSView
*sview
= [[view window
] contentView
];
6964 NSArray
*subs
= [sview subviews
];
6966 /* disable optimization stopping redraw of other scrollbars */
6967 view->scrollbarsNeedingUpdate
= 0;
6968 for (i
=[subs count
]-1; i
>= 0; i
--)
6969 if ([[subs objectAtIndex
: i
] isKindOfClass
: [EmacsScroller
class]])
6970 view->scrollbarsNeedingUpdate
++;
6971 [sview addSubview
: self
];
6974 /* [self setFrame: r]; */
6980 - (void
)setFrame
: (NSRect
)newRect
6982 NSTRACE
(EmacsScroller_setFrame
);
6983 /* block_input (); */
6984 pixel_height
= NSHeight
(newRect
);
6985 if (pixel_height
== 0) pixel_height
= 1;
6986 min_portion
= 20 / pixel_height
;
6987 [super setFrame
: newRect
];
6989 /* unblock_input (); */
6995 NSTRACE
(EmacsScroller_dealloc
);
6997 wset_vertical_scroll_bar
(XWINDOW
(win
), Qnil
);
7025 /* ensure other scrollbar updates after deletion */
7026 view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
7028 view->scrollbarsNeedingUpdate
++;
7029 [self removeFromSuperview
];
7037 - (void
)resetCursorRects
7039 NSRect visible
= [self visibleRect
];
7040 NSTRACE
(resetCursorRects
);
7042 if (!NSIsEmptyRect
(visible
))
7043 [self addCursorRect
: visible cursor
: [NSCursor arrowCursor
]];
7044 [[NSCursor arrowCursor
] setOnMouseEntered
: YES
];
7048 - (int
) checkSamePosition
: (int
) position portion
: (int
) portion
7051 return em_position
==position
&& em_portion
==portion
&& em_whole
==whole
7052 && portion
!= whole
; /* needed for resize empty buf */
7056 - setPosition
: (int
)position portion
: (int
)portion whole
: (int
)whole
7058 NSTRACE
(setPosition
);
7060 em_position
= position
;
7061 em_portion
= portion
;
7064 if (portion
>= whole
)
7066 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
7067 [self setKnobProportion
: 1.0];
7068 [self setDoubleValue
: 1.0];
7070 [self setFloatValue
: 0.0 knobProportion
: 1.0];
7077 portion
= max ((float
)whole
*min_portion
/pixel_height
, portion
);
7078 pos
= (float
)position
/ (whole
- portion
);
7079 por
= (CGFloat
)portion
/whole
;
7080 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
7081 [self setKnobProportion
: por
];
7082 [self setDoubleValue
: pos
];
7084 [self setFloatValue
: pos knobProportion
: por
];
7088 /* Events may come here even if the event loop is not running.
7089 If we don't enter the event loop, the scroll bar will not update.
7090 So send SIGIO to ourselves. */
7091 if (apploopnr
== 0) raise
(SIGIO
);
7096 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
7097 drag events will go directly to the EmacsScroller. Leaving in for now. */
7098 -(void
)getMouseMotionPart
: (int
*)part window
: (Lisp_Object
*)window
7099 x
: (Lisp_Object
*)x y
: ( Lisp_Object
*)y
7101 *part
= last_hit_part
;
7103 XSETINT
(*y
, pixel_height
);
7104 if ([self floatValue
] > 0.999F
)
7105 XSETINT
(*x
, pixel_height
);
7107 XSETINT
(*x
, pixel_height
* [self floatValue
]);
7111 /* set up emacs_event */
7112 - (void
) sendScrollEventAtLoc
: (float
)loc fromEvent
: (NSEvent
*)e
7117 emacs_event
->part
= last_hit_part
;
7118 emacs_event
->code
= 0;
7119 emacs_event
->modifiers
= EV_MODIFIERS
(e
) | down_modifier
;
7120 emacs_event
->frame_or_window
= win
;
7121 emacs_event
->timestamp
= EV_TIMESTAMP
(e
);
7122 emacs_event
->kind
= SCROLL_BAR_CLICK_EVENT
;
7123 emacs_event
->arg
= Qnil
;
7124 XSETINT
(emacs_event
->x
, loc
* pixel_height
);
7125 XSETINT
(emacs_event
->y
, pixel_height
-20);
7129 n_emacs_events_pending
++;
7130 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
);
7133 hold_event
(emacs_event
);
7134 EVENT_INIT
(*emacs_event
);
7135 ns_send_appdefined
(-1);
7139 /* called manually thru timer to implement repeated button action w/hold-down */
7140 - repeatScroll
: (NSTimer
*)scrollEntry
7142 NSEvent
*e
= [[self window
] currentEvent
];
7143 NSPoint p
= [[self window
] mouseLocationOutsideOfEventStream
];
7144 BOOL inKnob
= [self testPart
: p
] == NSScrollerKnob
;
7146 /* clear timer if need be */
7147 if (inKnob ||
[scroll_repeat_entry timeInterval
] == SCROLL_BAR_FIRST_DELAY
)
7149 [scroll_repeat_entry invalidate
];
7150 [scroll_repeat_entry release
];
7151 scroll_repeat_entry
= nil
;
7157 = [[NSTimer scheduledTimerWithTimeInterval
:
7158 SCROLL_BAR_CONTINUOUS_DELAY
7160 selector
: @selector
(repeatScroll
:)
7166 [self sendScrollEventAtLoc
: 0 fromEvent
: e
];
7171 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
7172 mouseDragged events without going into a modal loop. */
7173 - (void
)mouseDown
: (NSEvent
*)e
7176 /* hitPart is only updated AFTER event is passed on */
7177 NSScrollerPart part
= [self testPart
: [e locationInWindow
]];
7178 CGFloat inc
= 0.0, loc
, kloc
, pos
;
7181 NSTRACE
(EmacsScroller_mouseDown
);
7185 case NSScrollerDecrementPage
:
7186 last_hit_part
= scroll_bar_above_handle
; inc
= -1.0; break;
7187 case NSScrollerIncrementPage
:
7188 last_hit_part
= scroll_bar_below_handle
; inc
= 1.0; break;
7189 case NSScrollerDecrementLine
:
7190 last_hit_part
= scroll_bar_up_arrow
; inc
= -0.1; break;
7191 case NSScrollerIncrementLine
:
7192 last_hit_part
= scroll_bar_down_arrow
; inc
= 0.1; break;
7193 case NSScrollerKnob
:
7194 last_hit_part
= scroll_bar_handle
; break;
7195 case NSScrollerKnobSlot
: /* GNUstep-only */
7196 last_hit_part
= scroll_bar_move_ratio
; break;
7197 default
: /* NSScrollerNoPart? */
7198 fprintf (stderr
, "EmacsScoller
-mouseDown
: unexpected part
%ld\n",
7205 pos
= 0; /* ignored */
7207 /* set a timer to repeat, as we can't let superclass do this modally */
7209 = [[NSTimer scheduledTimerWithTimeInterval
: SCROLL_BAR_FIRST_DELAY
7211 selector
: @selector
(repeatScroll
:)
7218 /* handle, or on GNUstep possibly slot */
7219 NSEvent
*fake_event
;
7221 /* compute float loc in slot and mouse offset on knob */
7222 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7224 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7230 else if (loc
>= NSHeight
(sr
))
7232 loc
= NSHeight
(sr
);
7240 kr
= [self convertRect
: [self rectForPart
: NSScrollerKnob
]
7242 kloc
= NSHeight
(kr
) - ([e locationInWindow
].y
- NSMinY
(kr
));
7244 last_mouse_offset
= kloc
;
7246 /* if knob, tell emacs a location offset by knob pos
7247 (to indicate top of handle) */
7248 if (part
== NSScrollerKnob
)
7249 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7251 /* else this is a slot click on GNUstep: go straight there */
7252 pos
= loc
/ NSHeight
(sr
);
7254 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
7255 fake_event
= [NSEvent mouseEventWithType
: NSLeftMouseUp
7256 location
: [e locationInWindow
]
7257 modifierFlags
: [e modifierFlags
]
7258 timestamp
: [e timestamp
]
7259 windowNumber
: [e windowNumber
]
7260 context
: [e context
]
7261 eventNumber
: [e eventNumber
]
7262 clickCount
: [e clickCount
]
7263 pressure
: [e pressure
]];
7264 [super mouseUp
: fake_event
];
7267 if (part
!= NSScrollerKnob
)
7268 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7272 /* Called as we manually track scroller drags, rather than superclass. */
7273 - (void
)mouseDragged
: (NSEvent
*)e
7278 NSTRACE
(EmacsScroller_mouseDragged
);
7280 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7282 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7288 else if (loc
>= NSHeight
(sr
) + last_mouse_offset
)
7290 loc
= NSHeight
(sr
) + last_mouse_offset
;
7293 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7294 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7298 - (void
)mouseUp
: (NSEvent
*)e
7300 if (scroll_repeat_entry
)
7302 [scroll_repeat_entry invalidate
];
7303 [scroll_repeat_entry release
];
7304 scroll_repeat_entry
= nil
;
7310 /* treat scrollwheel events in the bar as though they were in the main window */
7311 - (void
) scrollWheel
: (NSEvent
*)theEvent
7313 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
7314 [view mouseDown
: theEvent
];
7317 @
end /* EmacsScroller */
7320 #ifdef NS_IMPL_GNUSTEP
7321 /* Dummy class to get rid of startup warnings. */
7322 @implementation EmacsDocument
7328 /* ==========================================================================
7330 Font-related functions; these used to be in nsfaces.m
7332 ========================================================================== */
7336 x_new_font
(struct frame
*f
, Lisp_Object font_object
, int fontset
)
7338 struct font
*font
= XFONT_OBJECT
(font_object
);
7339 EmacsView
*view = FRAME_NS_VIEW
(f
);
7342 fontset
= fontset_from_font
(font_object
);
7343 FRAME_FONTSET
(f
) = fontset
;
7345 if (FRAME_FONT
(f
) == font
)
7346 /* This font is already set in frame F. There's nothing more to
7350 FRAME_FONT
(f
) = font
;
7352 FRAME_BASELINE_OFFSET
(f
) = font
->baseline_offset
;
7353 FRAME_COLUMN_WIDTH
(f
) = font
->average_width
;
7354 FRAME_LINE_HEIGHT
(f
) = font
->height
;
7356 compute_fringe_widths
(f
, 1);
7358 /* Compute the scroll bar width in character columns. */
7359 if (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) > 0)
7361 int wid
= FRAME_COLUMN_WIDTH
(f
);
7362 FRAME_CONFIG_SCROLL_BAR_COLS
(f
)
7363 = (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) + wid
- 1) / wid
;
7367 int wid
= FRAME_COLUMN_WIDTH
(f
);
7368 FRAME_CONFIG_SCROLL_BAR_COLS
(f
) = (14 + wid
- 1) / wid
;
7371 /* Now make the frame display the given font. */
7372 if (FRAME_NS_WINDOW
(f
) != 0 && ! [view isFullscreen
])
7373 x_set_window_size
(f
, 0, FRAME_COLS
(f
) * FRAME_COLUMN_WIDTH
(f
),
7374 FRAME_LINES
(f
) * FRAME_LINE_HEIGHT
(f
), 1);
7380 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
7381 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
7385 ns_xlfd_to_fontname
(const
char *xlfd
)
7386 /* --------------------------------------------------------------------------
7387 Convert an X font name (XLFD) to an NS font name.
7388 Only family is used.
7389 The string returned is temporarily allocated.
7390 -------------------------------------------------------------------------- */
7392 char *name
= xmalloc
(180);
7396 if (!strncmp
(xlfd
, "
--"
, 2))
7397 sscanf (xlfd
, "
--%*[^-]-%[^-]179-", name);
7399 sscanf (xlfd
, "
-%*[^-]-%[^-]179-", name);
7401 /* stopgap for malformed XLFD input */
7402 if (strlen
(name
) == 0)
7403 strcpy
(name
, "Monaco"
);
7405 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
7406 also uppercase after '-' or ' ' */
7407 name
[0] = c_toupper
(name
[0]);
7408 for (len
=strlen
(name
), i
=0; i
<len
; i
++)
7414 name
[i
+1] = c_toupper
(name
[i
+1]);
7416 else if (name
[i
] == '_')
7420 name
[i
+1] = c_toupper
(name
[i
+1]);
7423 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
7424 ret
= [[NSString stringWithUTF8String
: name
] UTF8String
];
7431 syms_of_nsterm
(void
)
7433 NSTRACE
(syms_of_nsterm
);
7435 ns_antialias_threshold
= 10.0;
7437 /* from 23+ we need to tell emacs what modifiers there are.. */
7438 DEFSYM
(Qmodifier_value
, "modifier
-value"
);
7439 DEFSYM
(Qalt
, "alt"
);
7440 DEFSYM
(Qhyper
, "hyper"
);
7441 DEFSYM
(Qmeta
, "
meta"
);
7442 DEFSYM
(Qsuper
, "super"
);
7443 DEFSYM
(Qcontrol
, "control"
);
7444 DEFSYM
(QUTF8_STRING
, "UTF8_STRING"
);
7446 Fput
(Qalt
, Qmodifier_value
, make_number
(alt_modifier
));
7447 Fput
(Qhyper
, Qmodifier_value
, make_number
(hyper_modifier
));
7448 Fput
(Qmeta
, Qmodifier_value
, make_number
(meta_modifier
));
7449 Fput
(Qsuper
, Qmodifier_value
, make_number
(super_modifier
));
7450 Fput
(Qcontrol
, Qmodifier_value
, make_number
(ctrl_modifier
));
7452 DEFVAR_LISP
("ns
-input-file"
, ns_input_file
,
7453 "The file specified in the last NS event."
);
7454 ns_input_file
=Qnil
;
7456 DEFVAR_LISP
("ns
-input-text"
, ns_input_text
,
7457 "The data received in the last NS
text drag event."
);
7458 ns_input_text
=Qnil
;
7460 DEFVAR_LISP
("ns
-working
-text"
, ns_working_text
,
7461 "String
for visualizing working composition sequence."
);
7462 ns_working_text
=Qnil
;
7464 DEFVAR_LISP
("ns
-input-font"
, ns_input_font
,
7465 "The font specified in the last NS event."
);
7466 ns_input_font
=Qnil
;
7468 DEFVAR_LISP
("ns
-input-fontsize"
, ns_input_fontsize
,
7469 "The fontsize specified in the last NS event."
);
7470 ns_input_fontsize
=Qnil
;
7472 DEFVAR_LISP
("ns
-input-line"
, ns_input_line
,
7473 "The
line specified in the last NS event."
);
7474 ns_input_line
=Qnil
;
7476 DEFVAR_LISP
("ns
-input-color"
, ns_input_color
,
7477 "The color specified in the last NS event."
);
7478 ns_input_color
=Qnil
;
7480 DEFVAR_LISP
("ns
-input-spi
-name"
, ns_input_spi_name
,
7481 "The service name specified in the last NS event."
);
7482 ns_input_spi_name
=Qnil
;
7484 DEFVAR_LISP
("ns
-input-spi
-arg"
, ns_input_spi_arg
,
7485 "The service argument specified in the last NS event."
);
7486 ns_input_spi_arg
=Qnil
;
7488 DEFVAR_LISP
("ns
-alternate
-modifier"
, ns_alternate_modifier
,
7489 "This variable describes the behavior of the alternate or option key.
\n\
7490 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7491 Set to none means that the alternate
/ option key is not interpreted by Emacs
\n\
7492 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7493 ns_alternate_modifier
= Qmeta
;
7495 DEFVAR_LISP
("ns
-right
-alternate
-modifier"
, ns_right_alternate_modifier
,
7496 "This variable describes the behavior of the right alternate or option key.
\n\
7497 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7498 Set to left means be the same key as `ns
-alternate
-modifier
'.\n\
7499 Set to none means that the alternate / option key is not interpreted by Emacs\n\
7500 at all, allowing it to be used at a lower level for accented character entry.");
7501 ns_right_alternate_modifier = Qleft;
7503 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
7504 "This variable describes the behavior of the command key.\n\
7505 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
7506 ns_command_modifier = Qsuper;
7508 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
7509 "This variable describes the behavior of the right command key.\n\
7510 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7511 Set to left means be the same key as `ns-command-modifier'.
\n\
7512 Set to none means that the command
/ option key is not interpreted by Emacs
\n\
7513 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7514 ns_right_command_modifier
= Qleft
;
7516 DEFVAR_LISP
("ns
-control
-modifier"
, ns_control_modifier
,
7517 "This variable describes the behavior of the control key.
\n\
7518 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key."
);
7519 ns_control_modifier
= Qcontrol
;
7521 DEFVAR_LISP
("ns
-right
-control
-modifier"
, ns_right_control_modifier
,
7522 "This variable describes the behavior of the right control key.
\n\
7523 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7524 Set to left means be the same key as `ns
-control
-modifier
'.\n\
7525 Set to none means that the control / option key is not interpreted by Emacs\n\
7526 at all, allowing it to be used at a lower level for accented character entry.");
7527 ns_right_control_modifier = Qleft;
7529 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
7530 "This variable describes the behavior of the function key (on laptops).\n\
7531 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7532 Set to none means that the function key is not interpreted by Emacs at all,\n\
7533 allowing it to be used at a lower level for accented character entry.");
7534 ns_function_modifier = Qnone;
7536 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
7537 "Non-nil (the default) means to render text antialiased.");
7538 ns_antialias_text = Qt;
7540 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
7541 "Whether to confirm application quit using dialog.");
7542 ns_confirm_quit = Qnil;
7544 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
7545 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
7546 Only works on OSX 10.6 or later. */);
7547 ns_auto_hide_menu_bar = Qnil;
7549 DEFVAR_BOOL ("ns-use-native-fullscreen", ns_use_native_fullscreen,
7550 doc: /*Non-nil means to use native fullscreen on OSX >= 10.7.
7551 Nil means use fullscreen the old (< 10.7) way. The old way works better with
7552 multiple monitors, but lacks tool bar. This variable is ignored on OSX < 10.7.
7553 Default is t for OSX >= 10.7, nil otherwise. */);
7554 #ifdef HAVE_NATIVE_FS
7555 ns_use_native_fullscreen = YES;
7557 ns_use_native_fullscreen = NO;
7559 ns_last_use_native_fullscreen = ns_use_native_fullscreen;
7561 /* TODO: move to common code */
7562 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7563 doc: /* Which toolkit scroll bars Emacs uses, if any.
7564 A value of nil means Emacs doesn't use toolkit scroll bars.
7565 With the X Window system
, the value is a symbol describing the
7566 X toolkit. Possible values are
: gtk
, motif
, xaw
, or xaw3d.
7567 With MS Windows or Nextstep
, the value is t. */
);
7568 Vx_toolkit_scroll_bars
= Qt
;
7570 DEFVAR_BOOL
("x
-use
-underline
-position
-properties"
,
7571 x_use_underline_position_properties
,
7572 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
7573 A value of nil means ignore them. If you encounter fonts with bogus
7574 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7575 to 4.1, set this to nil. */);
7576 x_use_underline_position_properties
= 0;
7578 DEFVAR_BOOL
("x
-underline
-at
-descent
-line"
,
7579 x_underline_at_descent_line
,
7580 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7581 A value of nil means to draw the underline according to the value of the
7582 variable `x-use-underline-position-properties', which is usually at the
7583 baseline level. The default value is nil. */);
7584 x_underline_at_descent_line
= 0;
7586 /* Tell Emacs about this window system. */
7587 Fprovide
(Qns
, Qnil
);
7589 DEFSYM
(Qcocoa
, "cocoa"
);
7590 DEFSYM
(Qgnustep
, "gnustep"
);
7593 #ifdef NS_IMPL_COCOA
7594 Fprovide
(Qcocoa
, Qnil
);
7595 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= 1050
7599 Fprovide
(Qgnustep
, Qnil
);