1 /* NeXT/Open/GNUstep / MacOSX communication module.
3 Copyright (C) 1989, 1993-1994, 2005-2006, 2008-2013 Free Software
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 Originally by Carl Edman
23 Updated by Christian Limpach (chris@nice.ch)
24 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
25 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
26 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
29 /* This should be the first include, as it may set up #defines affecting
30 interpretation of even the system includes. */
36 #include
<sys
/types.h
>
42 #include
<c
-strcase.h
>
46 #include "blockinput.h"
47 #include "sysselect.h"
50 #include "character.h"
52 #include "composite.h"
55 #include "termhooks.h"
63 #ifdef NS_IMPL_GNUSTEP
69 int term_trace_num
= 0;
70 #define NSTRACE
(x
) fprintf (stderr
, "
%s:%d: [%d] " #x "\n", \
71 __FILE__
, __LINE__
, ++term_trace_num
)
76 extern NSString
*NSMenuDidBeginTrackingNotification
;
78 /* ==========================================================================
82 ========================================================================== */
84 /* Convert a symbol indexed with an NSxxx value to a value as defined
85 in keyboard.c (lispy_function_key). I hope this is a correct way
87 static unsigned convert_ns_to_X_keysym
[] =
89 NSHomeFunctionKey
, 0x50,
90 NSLeftArrowFunctionKey
, 0x51,
91 NSUpArrowFunctionKey
, 0x52,
92 NSRightArrowFunctionKey
, 0x53,
93 NSDownArrowFunctionKey
, 0x54,
94 NSPageUpFunctionKey
, 0x55,
95 NSPageDownFunctionKey
, 0x56,
96 NSEndFunctionKey
, 0x57,
97 NSBeginFunctionKey
, 0x58,
98 NSSelectFunctionKey
, 0x60,
99 NSPrintFunctionKey
, 0x61,
100 NSClearLineFunctionKey
, 0x0B,
101 NSExecuteFunctionKey
, 0x62,
102 NSInsertFunctionKey
, 0x63,
103 NSUndoFunctionKey
, 0x65,
104 NSRedoFunctionKey
, 0x66,
105 NSMenuFunctionKey
, 0x67,
106 NSFindFunctionKey
, 0x68,
107 NSHelpFunctionKey
, 0x6A,
108 NSBreakFunctionKey
, 0x6B,
110 NSF1FunctionKey
, 0xBE,
111 NSF2FunctionKey
, 0xBF,
112 NSF3FunctionKey
, 0xC0,
113 NSF4FunctionKey
, 0xC1,
114 NSF5FunctionKey
, 0xC2,
115 NSF6FunctionKey
, 0xC3,
116 NSF7FunctionKey
, 0xC4,
117 NSF8FunctionKey
, 0xC5,
118 NSF9FunctionKey
, 0xC6,
119 NSF10FunctionKey
, 0xC7,
120 NSF11FunctionKey
, 0xC8,
121 NSF12FunctionKey
, 0xC9,
122 NSF13FunctionKey
, 0xCA,
123 NSF14FunctionKey
, 0xCB,
124 NSF15FunctionKey
, 0xCC,
125 NSF16FunctionKey
, 0xCD,
126 NSF17FunctionKey
, 0xCE,
127 NSF18FunctionKey
, 0xCF,
128 NSF19FunctionKey
, 0xD0,
129 NSF20FunctionKey
, 0xD1,
130 NSF21FunctionKey
, 0xD2,
131 NSF22FunctionKey
, 0xD3,
132 NSF23FunctionKey
, 0xD4,
133 NSF24FunctionKey
, 0xD5,
135 NSBackspaceCharacter
, 0x08, /* 8: Not on some KBs. */
136 NSDeleteCharacter
, 0xFF, /* 127: Big 'delete' key upper right. */
137 NSDeleteFunctionKey
, 0x9F, /* 63272: Del forw key off main array. */
139 NSTabCharacter
, 0x09,
140 0x19, 0x09, /* left tab->regular since pass shift */
141 NSCarriageReturnCharacter
, 0x0D,
142 NSNewlineCharacter
, 0x0D,
143 NSEnterCharacter
, 0x8D,
145 0x41|NSNumericPadKeyMask
, 0xAE, /* KP_Decimal */
146 0x43|NSNumericPadKeyMask
, 0xAA, /* KP_Multiply */
147 0x45|NSNumericPadKeyMask
, 0xAB, /* KP_Add */
148 0x4B|NSNumericPadKeyMask
, 0xAF, /* KP_Divide */
149 0x4E|NSNumericPadKeyMask
, 0xAD, /* KP_Subtract */
150 0x51|NSNumericPadKeyMask
, 0xBD, /* KP_Equal */
151 0x52|NSNumericPadKeyMask
, 0xB0, /* KP_0 */
152 0x53|NSNumericPadKeyMask
, 0xB1, /* KP_1 */
153 0x54|NSNumericPadKeyMask
, 0xB2, /* KP_2 */
154 0x55|NSNumericPadKeyMask
, 0xB3, /* KP_3 */
155 0x56|NSNumericPadKeyMask
, 0xB4, /* KP_4 */
156 0x57|NSNumericPadKeyMask
, 0xB5, /* KP_5 */
157 0x58|NSNumericPadKeyMask
, 0xB6, /* KP_6 */
158 0x59|NSNumericPadKeyMask
, 0xB7, /* KP_7 */
159 0x5B|NSNumericPadKeyMask
, 0xB8, /* KP_8 */
160 0x5C|NSNumericPadKeyMask
, 0xB9, /* KP_9 */
162 0x1B, 0x1B /* escape */
165 static Lisp_Object Qmodifier_value
;
166 Lisp_Object Qalt
, Qcontrol
, Qhyper
, Qmeta
, Qsuper
;
167 extern Lisp_Object Qcursor_color
, Qcursor_type
, Qns
, Qleft
;
169 static Lisp_Object QUTF8_STRING
;
171 /* On OS X picks up the default NSGlobalDomain AppleAntiAliasingThreshold,
172 the maximum font size to NOT antialias. On GNUstep there is currently
173 no way to control this behavior. */
174 float ns_antialias_threshold
;
176 /* Used to pick up AppleHighlightColor on OS X */
177 NSString
*ns_selection_color
;
179 NSArray
*ns_send_types
=0, *ns_return_types
=0, *ns_drag_types
=0;
180 NSString
*ns_app_name
= @"Emacs"
; /* default changed later */
182 /* Display variables */
183 struct ns_display_info
*x_display_list
; /* Chain of existing displays */
184 Lisp_Object ns_display_name_list
;
185 long context_menu_value
= 0;
188 NSPoint last_mouse_motion_position
;
189 static NSRect last_mouse_glyph
;
190 static Time last_mouse_movement_time
= 0;
191 static Lisp_Object last_mouse_motion_frame
;
192 static EmacsScroller
*last_mouse_scroll_bar
= nil
;
193 static
struct frame
*ns_updating_frame
;
194 static NSView
*focus_view
= NULL;
195 static int ns_window_num
= 0;
196 #ifdef NS_IMPL_GNUSTEP
199 static BOOL gsaved
= NO
;
200 static BOOL ns_fake_keydown
= NO
;
201 int ns_tmp_flags
; /* FIXME */
202 struct nsfont_info
*ns_tmp_font
; /* FIXME */
204 static BOOL ns_menu_bar_is_hidden
= NO
;
206 /*static int debug_lock = 0; */
209 static BOOL send_appdefined
= YES
;
210 #define NO_APPDEFINED_DATA
(-8)
211 static int last_appdefined_event_data
= NO_APPDEFINED_DATA
;
212 static NSTimer
*timed_entry
= 0;
213 static NSTimer
*scroll_repeat_entry
= nil
;
214 static fd_set select_readfds
, select_writefds
;
215 enum
{ SELECT_HAVE_READ
= 1, SELECT_HAVE_WRITE
= 2, SELECT_HAVE_TMO
= 4 };
216 static int select_nfds
= 0, select_valid
= 0;
217 static EMACS_TIME select_timeout
= { 0, 0 };
218 static int selfds
[2] = { -1, -1 };
219 static pthread_mutex_t select_mutex
;
220 static int apploopnr
= 0;
221 static NSAutoreleasePool
*outerpool
;
222 static
struct input_event
*emacs_event
= NULL;
223 static
struct input_event
*q_event_ptr
= NULL;
224 static int n_emacs_events_pending
= 0;
225 static NSMutableArray
*ns_pending_files
, *ns_pending_service_names
,
226 *ns_pending_service_args
;
227 static BOOL ns_do_open_file
= NO
;
228 static BOOL ns_last_use_native_fullscreen
;
231 struct input_event
*q
;
239 * State for pending menu activation:
240 * MENU_NONE Normal state
241 * MENU_PENDING A menu has been clicked on, but has been canceled so we can
242 * run lisp to update the menu.
243 * MENU_OPENING Menu is up to date, and the click event is redone so the menu
247 #define MENU_PENDING
1
248 #define MENU_OPENING
2
249 static int menu_will_open_state
= MENU_NONE
;
251 /* Saved position for menu click. */
252 static CGPoint menu_mouse_point
;
254 /* Title for the menu to open. */
255 static
char *menu_pending_title
= 0;
258 /* Convert modifiers in a NeXTstep event to emacs style modifiers. */
259 #define NS_FUNCTION_KEY_MASK
0x800000
260 #define NSLeftControlKeyMask
(0x000001 | NSControlKeyMask
)
261 #define NSRightControlKeyMask
(0x002000 | NSControlKeyMask
)
262 #define NSLeftCommandKeyMask
(0x000008 | NSCommandKeyMask
)
263 #define NSRightCommandKeyMask
(0x000010 | NSCommandKeyMask
)
264 #define NSLeftAlternateKeyMask
(0x000020 | NSAlternateKeyMask
)
265 #define NSRightAlternateKeyMask
(0x000040 | NSAlternateKeyMask
)
266 #define EV_MODIFIERS
(e
) \
267 ((([e modifierFlags
] & NSHelpKeyMask
) ? \
268 hyper_modifier
: 0) \
269 |
(!EQ
(ns_right_alternate_modifier
, Qleft
) && \
270 (([e modifierFlags
] & NSRightAlternateKeyMask
) \
271 == NSRightAlternateKeyMask
) ? \
272 parse_solitary_modifier
(ns_right_alternate_modifier
) : 0) \
273 |
(([e modifierFlags
] & NSAlternateKeyMask
) ? \
274 parse_solitary_modifier
(ns_alternate_modifier
) : 0) \
275 |
(([e modifierFlags
] & NSShiftKeyMask
) ? \
276 shift_modifier
: 0) \
277 |
(!EQ
(ns_right_control_modifier
, Qleft
) && \
278 (([e modifierFlags
] & NSRightControlKeyMask
) \
279 == NSRightControlKeyMask
) ? \
280 parse_solitary_modifier
(ns_right_control_modifier
) : 0) \
281 |
(([e modifierFlags
] & NSControlKeyMask
) ? \
282 parse_solitary_modifier
(ns_control_modifier
) : 0) \
283 |
(([e modifierFlags
] & NS_FUNCTION_KEY_MASK
) ? \
284 parse_solitary_modifier
(ns_function_modifier
) : 0) \
285 |
(!EQ
(ns_right_command_modifier
, Qleft
) && \
286 (([e modifierFlags
] & NSRightCommandKeyMask
) \
287 == NSRightCommandKeyMask
) ? \
288 parse_solitary_modifier
(ns_right_command_modifier
) : 0) \
289 |
(([e modifierFlags
] & NSCommandKeyMask
) ? \
290 parse_solitary_modifier
(ns_command_modifier
):0))
292 #define EV_UDMODIFIERS
(e
) \
293 ((([e
type] == NSLeftMouseDown
) ? down_modifier
: 0) \
294 |
(([e
type] == NSRightMouseDown
) ? down_modifier
: 0) \
295 |
(([e
type] == NSOtherMouseDown
) ? down_modifier
: 0) \
296 |
(([e
type] == NSLeftMouseDragged
) ? down_modifier
: 0) \
297 |
(([e
type] == NSRightMouseDragged
) ? down_modifier
: 0) \
298 |
(([e
type] == NSOtherMouseDragged
) ? down_modifier
: 0) \
299 |
(([e
type] == NSLeftMouseUp
) ? up_modifier
: 0) \
300 |
(([e
type] == NSRightMouseUp
) ? up_modifier
: 0) \
301 |
(([e
type] == NSOtherMouseUp
) ? up_modifier
: 0))
303 #define EV_BUTTON
(e
) \
304 ((([e
type] == NSLeftMouseDown
) ||
([e
type] == NSLeftMouseUp
)) ?
0 : \
305 (([e
type] == NSRightMouseDown
) ||
([e
type] == NSRightMouseUp
)) ?
2 : \
306 [e buttonNumber
] - 1)
308 /* Convert the time field to a timestamp in milliseconds. */
309 #define EV_TIMESTAMP
(e
) ([e timestamp
] * 1000)
311 /* This is a piece of code which is common to all the event handling
312 methods. Maybe it should even be a function. */
313 #define EV_TRAILER
(e
) \
315 XSETFRAME
(emacs_event
->frame_or_window
, emacsframe
); \
316 if (e
) emacs_event
->timestamp
= EV_TIMESTAMP
(e
); \
319 n_emacs_events_pending
++; \
320 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
); \
323 hold_event
(emacs_event
); \
324 EVENT_INIT
(*emacs_event
); \
325 ns_send_appdefined
(-1); \
328 /* TODO: get rid of need for these forward declarations */
329 static void ns_condemn_scroll_bars
(struct frame
*f
);
330 static void ns_judge_scroll_bars
(struct frame
*f
);
331 void x_set_frame_alpha
(struct frame
*f
);
334 /* ==========================================================================
338 ========================================================================== */
341 hold_event
(struct input_event
*event
)
343 if (hold_event_q.nr
== hold_event_q.cap
)
345 if (hold_event_q.cap
== 0) hold_event_q.cap
= 10;
346 else hold_event_q.cap
*= 2;
348 xrealloc
(hold_event_q.q
, hold_event_q.cap
* sizeof
*hold_event_q.q
);
351 hold_event_q.q
[hold_event_q.nr
++] = *event
;
352 /* Make sure ns_read_socket is called, i.e. we have input. */
354 send_appdefined
= YES
;
358 append2
(Lisp_Object list
, Lisp_Object item
)
359 /* --------------------------------------------------------------------------
360 Utility to append to a list
361 -------------------------------------------------------------------------- */
363 Lisp_Object array
[2];
365 array
[1] = list1
(item
);
366 return Fnconc
(2, &array
[0]);
371 ns_etc_directory
(void
)
372 /* If running as a self-contained app bundle, return as a string the
373 filename of the etc directory, if present; else nil. */
375 NSBundle
*bundle
= [NSBundle mainBundle
];
376 NSString
*resourceDir
= [bundle resourcePath
];
377 NSString
*resourcePath
;
378 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
381 resourcePath
= [resourceDir stringByAppendingPathComponent
: @"etc"
];
382 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
384 if (isDir
) return [resourcePath UTF8String
];
392 /* If running as a self-contained app bundle, return as a path string
393 the filenames of the libexec and bin directories, ie libexec:bin.
394 Otherwise, return nil.
395 Normally, Emacs does not add its own bin/ directory to the PATH.
396 However, a self-contained NS build has a different layout, with
397 bin/ and libexec/ subdirectories in the directory that contains
399 We put libexec first, because init_callproc_1 uses the first
400 element to initialize exec-directory. An alternative would be
401 for init_callproc to check for invocation-directory/libexec.
404 NSBundle
*bundle
= [NSBundle mainBundle
];
405 NSString
*resourceDir
= [bundle resourcePath
];
406 NSString
*binDir
= [bundle bundlePath
];
407 NSString
*resourcePath
, *resourcePaths
;
409 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
410 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
412 NSEnumerator
*pathEnum
;
415 range
= [resourceDir rangeOfString
: @"Contents"
];
416 if (range.location
!= NSNotFound
)
418 binDir
= [binDir stringByAppendingPathComponent
: @"Contents"
];
420 binDir
= [binDir stringByAppendingPathComponent
: @"MacOS"
];
424 paths
= [binDir stringsByAppendingPaths
:
425 [NSArray arrayWithObjects
: @"libexec"
, @"bin"
, nil
]];
426 pathEnum
= [paths objectEnumerator
];
429 while ((resourcePath
= [pathEnum nextObject
]))
431 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
434 if ([resourcePaths
length] > 0)
436 = [resourcePaths stringByAppendingString
: pathSeparator
];
438 = [resourcePaths stringByAppendingString
: resourcePath
];
441 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
449 /* If running as a self-contained app bundle, return as a path string
450 the filenames of the site-lisp, lisp and leim directories.
451 Ie, site-lisp:lisp:leim. Otherwise, return nil. */
453 NSBundle
*bundle
= [NSBundle mainBundle
];
454 NSString
*resourceDir
= [bundle resourcePath
];
455 NSString
*resourcePath
, *resourcePaths
;
456 NSString
*pathSeparator
= [NSString stringWithFormat
: @"
%c", SEPCHAR];
457 NSFileManager
*fileManager
= [NSFileManager defaultManager
];
459 NSArray
*paths
= [resourceDir stringsByAppendingPaths
:
460 [NSArray arrayWithObjects
:
461 @"site
-lisp"
, @"lisp"
, @"leim"
, nil
]];
462 NSEnumerator
*pathEnum
= [paths objectEnumerator
];
465 /* Hack to skip site-lisp. */
466 if (no_site_lisp
) resourcePath
= [pathEnum nextObject
];
468 while ((resourcePath
= [pathEnum nextObject
]))
470 if ([fileManager fileExistsAtPath
: resourcePath isDirectory
: &isDir
])
473 if ([resourcePaths
length] > 0)
475 = [resourcePaths stringByAppendingString
: pathSeparator
];
477 = [resourcePaths stringByAppendingString
: resourcePath
];
480 if ([resourcePaths
length] > 0) return [resourcePaths UTF8String
];
486 ns_timeout
(int usecs
)
487 /* --------------------------------------------------------------------------
488 Blocking timer utility used by ns_ring_bell
489 -------------------------------------------------------------------------- */
491 EMACS_TIME wakeup
= add_emacs_time
(current_emacs_time
(),
492 make_emacs_time
(0, usecs
* 1000));
494 /* Keep waiting until past the time wakeup. */
497 EMACS_TIME timeout
, now
= current_emacs_time
();
498 if (EMACS_TIME_LE
(wakeup
, now
))
500 timeout
= sub_emacs_time
(wakeup
, now
);
502 /* Try to wait that long--but we might wake up sooner. */
503 pselect
(0, NULL, NULL, NULL, &timeout
, NULL);
509 ns_release_object
(void
*obj
)
510 /* --------------------------------------------------------------------------
511 Release an object (callable from C)
512 -------------------------------------------------------------------------- */
519 ns_retain_object
(void
*obj
)
520 /* --------------------------------------------------------------------------
521 Retain an object (callable from C)
522 -------------------------------------------------------------------------- */
529 ns_alloc_autorelease_pool
(void
)
530 /* --------------------------------------------------------------------------
531 Allocate a pool for temporary objects (callable from C)
532 -------------------------------------------------------------------------- */
534 return [[NSAutoreleasePool alloc
] init
];
539 ns_release_autorelease_pool
(void
*pool
)
540 /* --------------------------------------------------------------------------
541 Free a pool and temporary objects it refers to (callable from C)
542 -------------------------------------------------------------------------- */
544 ns_release_object
(pool
);
549 /* ==========================================================================
551 Focus (clipping) and screen update
553 ========================================================================== */
556 // Window constraining
557 // -------------------
559 // To ensure that the windows are not placed under the
menu bar, they
560 // are typically moved by the call
-back constrainFrameRect. However
,
561 // by overriding it
, it
's possible to inhibit this, leaving the window
562 // in it's original position.
564 // It
's possible to hide the menu bar. However, technically, it's only
565 // possible to hide it when the application is active. To ensure that
566 // this work properly
, the
menu bar and window constraining are
567 // deferred until the application becomes active.
569 // Even though it
's not possible to manually move a window above the
570 // top of the screen, it is allowed if it's done programmatically
,
571 // when the
menu is
hidden. This allows the editable area to cover the
572 // full screen height.
577 // Use the following extra files
:
580 // ;; Hide
menu and place frame slightly above the top of the screen.
581 // (setq ns
-auto
-hide
-menu-bar t
)
582 // (set-frame
-position
(selected
-frame
) 0 -20)
586 // emacs
-Q
-l init.el
588 // Result
: No
menu bar, and the
title bar should be above the screen.
594 // Result
: Menu bar visible
, frame placed immediately below the
menu.
598 ns_constrain_all_frames
(void
)
600 Lisp_Object tail
, frame
;
602 FOR_EACH_FRAME
(tail
, frame
)
604 struct frame
*f
= XFRAME
(frame
);
607 NSView
*view = FRAME_NS_VIEW
(f
);
608 /* This no-op will trigger the default window placing
609 * constraint system. */
610 f
->output_data.ns
->dont_constrain
= 0;
611 [[view window
] setFrameOrigin
:[[view window
] frame
].origin
];
617 /* True, if the menu bar should be hidden. */
620 ns_menu_bar_should_be_hidden
(void
)
622 return !NILP
(ns_auto_hide_menu_bar
)
623 && [NSApp respondsToSelector
:@selector
(setPresentationOptions
:)];
627 /* Show or hide the menu bar, based on user setting. */
630 ns_update_auto_hide_menu_bar
(void
)
633 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
636 NSTRACE
(ns_update_auto_hide_menu_bar
);
640 && [NSApp respondsToSelector
:@selector
(setPresentationOptions
:)])
642 // Note
, "setPresentationOptions" triggers an
error unless the
643 // application is active.
644 BOOL menu_bar_should_be_hidden
= ns_menu_bar_should_be_hidden
();
646 if (menu_bar_should_be_hidden
!= ns_menu_bar_is_hidden
)
648 NSApplicationPresentationOptions options
649 = NSApplicationPresentationAutoHideDock
;
651 if (menu_bar_should_be_hidden
)
652 options |
= NSApplicationPresentationAutoHideMenuBar
;
654 [NSApp setPresentationOptions
: options
];
656 ns_menu_bar_is_hidden
= menu_bar_should_be_hidden
;
658 if (!ns_menu_bar_is_hidden
)
660 ns_constrain_all_frames
();
672 ns_update_begin
(struct frame
*f
)
673 /* --------------------------------------------------------------------------
674 Prepare for a grouped sequence of drawing calls
675 external (RIF) call; whole frame, called before update_window_begin
676 -------------------------------------------------------------------------- */
678 NSView
*view = FRAME_NS_VIEW
(f
);
679 NSTRACE
(ns_update_begin
);
681 ns_update_auto_hide_menu_bar
();
683 ns_updating_frame
= f
;
686 /* drawRect may have been called for say the minibuffer, and then clip path
687 is for the minibuffer. But the display engine may draw more because
688 we have set the frame as garbaged. So reset clip path to the whole
693 NSRect r
= [view frame
];
694 NSRect cr
= [[view window
] frame
];
695 /* If a large frame size is set, r may be larger than the window frame
696 before constrained. In that case don't change the clip path, as we
697 will clear in to the tool bar and title bar. */
699 + FRAME_NS_TITLEBAR_HEIGHT
(f
)
700 + FRAME_TOOLBAR_HEIGHT
(f
) <= cr.
size.height
)
702 bp
= [[NSBezierPath bezierPathWithRect
: r
] retain
];
709 #ifdef NS_IMPL_GNUSTEP
710 uRect
= NSMakeRect
(0, 0, 0, 0);
716 ns_update_window_begin
(struct window
*w
)
717 /* --------------------------------------------------------------------------
718 Prepare for a grouped sequence of drawing calls
719 external (RIF) call; for one window, called after update_begin
720 -------------------------------------------------------------------------- */
722 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
723 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
725 NSTRACE
(ns_update_window_begin
);
726 set_output_cursor
(&w
->cursor
);
730 if (f
== hlinfo
->mouse_face_mouse_frame
)
732 /* Don't do highlighting for mouse motion during the update. */
733 hlinfo
->mouse_face_defer
= 1;
735 /* If the frame needs to be redrawn,
736 simply forget about any prior mouse highlighting. */
737 if (FRAME_GARBAGED_P
(f
))
738 hlinfo
->mouse_face_window
= Qnil
;
740 /* (further code for mouse faces ifdef'd out in other terms elided) */
748 ns_update_window_end
(struct window
*w
, int cursor_on_p
,
749 int mouse_face_overwritten_p
)
750 /* --------------------------------------------------------------------------
751 Finished a grouped sequence of drawing calls
752 external (RIF) call; for one window called before update_end
753 -------------------------------------------------------------------------- */
755 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(XFRAME
(w
->frame
));
757 /* note: this fn is nearly identical in all terms */
758 if (!w
->pseudo_window_p
)
763 display_and_set_cursor
(w
, 1,
764 output_cursor.hpos
, output_cursor.vpos
,
765 output_cursor.x
, output_cursor.y
);
767 if (draw_window_fringes
(w
, 1))
768 x_draw_vertical_border
(w
);
773 /* If a row with mouse-face was overwritten, arrange for
774 frame_up_to_date to redisplay the mouse highlight. */
775 if (mouse_face_overwritten_p
)
777 hlinfo
->mouse_face_beg_row
= hlinfo
->mouse_face_beg_col
= -1;
778 hlinfo
->mouse_face_end_row
= hlinfo
->mouse_face_end_col
= -1;
779 hlinfo
->mouse_face_window
= Qnil
;
782 NSTRACE
(update_window_end
);
787 ns_update_end
(struct frame
*f
)
788 /* --------------------------------------------------------------------------
789 Finished a grouped sequence of drawing calls
790 external (RIF) call; for whole frame, called after update_window_end
791 -------------------------------------------------------------------------- */
793 EmacsView
*view = FRAME_NS_VIEW
(f
);
795 /* if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
796 MOUSE_HL_INFO
(f
)->mouse_face_defer
= 0;
801 [[view window
] flushWindow
];
804 ns_updating_frame
= NULL;
805 NSTRACE
(ns_update_end
);
810 ns_flush
(struct frame
*f
)
811 /* --------------------------------------------------------------------------
813 NS impl is no-op since currently we flush in ns_update_end and elsewhere
814 -------------------------------------------------------------------------- */
821 ns_focus
(struct frame
*f
, NSRect
*r
, int n
)
822 /* --------------------------------------------------------------------------
823 Internal: Focus on given frame. During small local updates this is used to
824 draw, however during large updates, ns_update_begin and ns_update_end are
825 called to wrap the whole thing, in which case these calls are stubbed out.
826 Except, on GNUstep, we accumulate the rectangle being drawn into, because
827 the back end won't do this automatically, and will just end up flushing
829 -------------------------------------------------------------------------- */
831 // NSTRACE
(ns_focus
);
833 fprintf (stderr, "focus: %d", c++);
834 if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
835 fprintf (stderr, "\n"); */
837 if (f
!= ns_updating_frame
)
839 NSView
*view = FRAME_NS_VIEW
(f
);
840 if (view != focus_view
)
842 if (focus_view
!= NULL)
844 [focus_view unlockFocus
];
845 [[focus_view window
] flushWindow
];
852 /*if (view) debug_lock++; */
859 [[NSGraphicsContext currentContext
] saveGraphicsState
];
861 NSRectClipList
(r
, 2);
870 ns_unfocus
(struct frame
*f
)
871 /* --------------------------------------------------------------------------
872 Internal: Remove focus on given frame
873 -------------------------------------------------------------------------- */
875 // NSTRACE
(ns_unfocus
);
879 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
883 if (f
!= ns_updating_frame
)
885 if (focus_view
!= NULL)
887 [focus_view unlockFocus
];
888 [[focus_view window
] flushWindow
];
897 ns_clip_to_row
(struct window
*w
, struct glyph_row
*row
, int area
, BOOL gc
)
898 /* --------------------------------------------------------------------------
899 Internal (but parallels other terms): Focus drawing on given row
900 -------------------------------------------------------------------------- */
902 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
904 int window_x
, window_y
, window_width
;
906 window_box
(w
, area
, &window_x
, &window_y
, &window_width
, 0);
908 clip_rect.origin.x
= window_x
;
909 clip_rect.origin.y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, row
->y
));
910 clip_rect.origin.y
= max (clip_rect.origin.y
, window_y
);
911 clip_rect.
size.width
= window_width
;
912 clip_rect.
size.height
= row
->visible_height
;
914 ns_focus
(f
, &clip_rect
, 1);
919 ns_ring_bell
(struct frame
*f
)
920 /* --------------------------------------------------------------------------
922 -------------------------------------------------------------------------- */
924 NSTRACE
(ns_ring_bell
);
927 NSAutoreleasePool
*pool
;
928 struct frame
*frame
= SELECTED_FRAME
();
932 pool
= [[NSAutoreleasePool alloc
] init
];
934 view = FRAME_NS_VIEW
(frame
);
938 NSPoint dim
= NSMakePoint
(128, 128);
941 r.origin.x
+= (r.
size.width
- dim.x
) / 2;
942 r.origin.y
+= (r.
size.height
- dim.y
) / 2;
943 r.
size.width
= dim.x
;
944 r.
size.height
= dim.y
;
945 surr
= NSInsetRect
(r
, -2, -2);
946 ns_focus
(frame
, &surr
, 1);
947 [[view window
] cacheImageInRect
: [view convertRect
: surr toView
:nil
]];
948 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
949 (FRAME_DEFAULT_FACE
(frame
)), frame
) set];
951 [[view window
] flushWindow
];
953 [[view window
] restoreCachedImage
];
954 [[view window
] flushWindow
];
968 ns_reset_terminal_modes
(struct terminal *terminal)
969 /* Externally called as hook */
971 NSTRACE
(ns_reset_terminal_modes
);
976 ns_set_terminal_modes
(struct terminal *terminal)
977 /* Externally called as hook */
979 NSTRACE
(ns_set_terminal_modes
);
984 /* ==========================================================================
986 Frame / window manager related functions
988 ========================================================================== */
992 ns_raise_frame
(struct frame
*f
)
993 /* --------------------------------------------------------------------------
994 Bring window to foreground and make it active
995 -------------------------------------------------------------------------- */
998 check_window_system
(f
);
999 view = FRAME_NS_VIEW
(f
);
1001 if (FRAME_VISIBLE_P
(f
))
1002 [[view window
] makeKeyAndOrderFront
: NSApp
];
1008 ns_lower_frame
(struct frame
*f
)
1009 /* --------------------------------------------------------------------------
1011 -------------------------------------------------------------------------- */
1014 check_window_system
(f
);
1015 view = FRAME_NS_VIEW
(f
);
1017 [[view window
] orderBack
: NSApp
];
1023 ns_frame_raise_lower
(struct frame
*f
, int raise
)
1024 /* --------------------------------------------------------------------------
1026 -------------------------------------------------------------------------- */
1028 NSTRACE
(ns_frame_raise_lower
);
1038 ns_frame_rehighlight
(struct frame
*frame
)
1039 /* --------------------------------------------------------------------------
1040 External (hook): called on things like window switching within frame
1041 -------------------------------------------------------------------------- */
1043 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(frame
);
1044 struct frame
*old_highlight
= dpyinfo
->x_highlight_frame
;
1046 NSTRACE
(ns_frame_rehighlight
);
1047 if (dpyinfo
->x_focus_frame
)
1049 dpyinfo
->x_highlight_frame
1050 = (FRAMEP
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1051 ? XFRAME
(FRAME_FOCUS_FRAME
(dpyinfo
->x_focus_frame
))
1052 : dpyinfo
->x_focus_frame
);
1053 if (!FRAME_LIVE_P
(dpyinfo
->x_highlight_frame
))
1055 fset_focus_frame
(dpyinfo
->x_focus_frame
, Qnil
);
1056 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
;
1060 dpyinfo
->x_highlight_frame
= 0;
1062 if (dpyinfo
->x_highlight_frame
&&
1063 dpyinfo
->x_highlight_frame
!= old_highlight
)
1067 x_update_cursor
(old_highlight
, 1);
1068 x_set_frame_alpha
(old_highlight
);
1070 if (dpyinfo
->x_highlight_frame
)
1072 x_update_cursor
(dpyinfo
->x_highlight_frame
, 1);
1073 x_set_frame_alpha
(dpyinfo
->x_highlight_frame
);
1080 x_make_frame_visible
(struct frame
*f
)
1081 /* --------------------------------------------------------------------------
1082 External: Show the window (X11 semantics)
1083 -------------------------------------------------------------------------- */
1085 NSTRACE
(x_make_frame_visible
);
1086 /* XXX: at some points in past this was not needed, as the only place that
1087 called this (frame.c:Fraise_frame ()) also called raise_lower;
1088 if this ends up the case again, comment this out again. */
1089 if (!FRAME_VISIBLE_P
(f
))
1091 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1093 SET_FRAME_VISIBLE
(f
, 1);
1096 /* Making a new frame from a fullscreen frame will make the new frame
1097 fullscreen also. So skip handleFS as this will print an error. */
1098 if ([view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
1099 && [view isFullscreen
])
1102 if (f
->want_fullscreen
!= FULLSCREEN_NONE
)
1113 x_make_frame_invisible
(struct frame
*f
)
1114 /* --------------------------------------------------------------------------
1115 External: Hide the window (X11 semantics)
1116 -------------------------------------------------------------------------- */
1119 NSTRACE
(x_make_frame_invisible
);
1120 check_window_system
(f
);
1121 view = FRAME_NS_VIEW
(f
);
1122 [[view window
] orderOut
: NSApp
];
1123 SET_FRAME_VISIBLE
(f
, 0);
1124 SET_FRAME_ICONIFIED
(f
, 0);
1129 x_iconify_frame
(struct frame
*f
)
1130 /* --------------------------------------------------------------------------
1131 External: Iconify window
1132 -------------------------------------------------------------------------- */
1135 struct ns_display_info
*dpyinfo
;
1137 NSTRACE
(x_iconify_frame
);
1138 check_window_system
(f
);
1139 view = FRAME_NS_VIEW
(f
);
1140 dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1142 if (dpyinfo
->x_highlight_frame
== f
)
1143 dpyinfo
->x_highlight_frame
= 0;
1145 if ([[view window
] windowNumber
] <= 0)
1147 /* the window is still deferred. Make it very small, bring it
1148 on screen and order it out. */
1149 NSRect s
= { { 100, 100}, {0, 0} };
1151 t
= [[view window
] frame
];
1152 [[view window
] setFrame
: s display
: NO
];
1153 [[view window
] orderBack
: NSApp
];
1154 [[view window
] orderOut
: NSApp
];
1155 [[view window
] setFrame
: t display
: NO
];
1157 [[view window
] miniaturize
: NSApp
];
1160 /* Free X resources of frame F. */
1163 x_free_frame_resources
(struct frame
*f
)
1166 struct ns_display_info
*dpyinfo
;
1167 Mouse_HLInfo
*hlinfo
;
1169 NSTRACE
(x_free_frame_resources
);
1170 check_window_system
(f
);
1171 view = FRAME_NS_VIEW
(f
);
1172 dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1173 hlinfo
= MOUSE_HL_INFO
(f
);
1175 [(EmacsView
*)view setWindowClosing
: YES
]; /* may not have been informed */
1179 free_frame_menubar
(f
);
1181 if (FRAME_FACE_CACHE
(f
))
1182 free_frame_faces
(f
);
1184 if (f
== dpyinfo
->x_focus_frame
)
1185 dpyinfo
->x_focus_frame
= 0;
1186 if (f
== dpyinfo
->x_highlight_frame
)
1187 dpyinfo
->x_highlight_frame
= 0;
1188 if (f
== hlinfo
->mouse_face_mouse_frame
)
1190 hlinfo
->mouse_face_beg_row
= hlinfo
->mouse_face_beg_col
= -1;
1191 hlinfo
->mouse_face_end_row
= hlinfo
->mouse_face_end_col
= -1;
1192 hlinfo
->mouse_face_window
= Qnil
;
1193 hlinfo
->mouse_face_mouse_frame
= 0;
1196 if (f
->output_data.ns
->miniimage
!= nil
)
1197 [f
->output_data.ns
->miniimage release
];
1199 [[view window
] close];
1202 xfree
(f
->output_data.ns
);
1208 x_destroy_window
(struct frame
*f
)
1209 /* --------------------------------------------------------------------------
1210 External: Delete the window
1211 -------------------------------------------------------------------------- */
1213 NSTRACE
(x_destroy_window
);
1214 check_window_system
(f
);
1215 x_free_frame_resources
(f
);
1221 x_set_offset
(struct frame
*f
, int xoff
, int yoff
, int change_grav
)
1222 /* --------------------------------------------------------------------------
1223 External: Position the window
1224 -------------------------------------------------------------------------- */
1226 NSView
*view = FRAME_NS_VIEW
(f
);
1227 NSArray
*screens
= [NSScreen screens
];
1228 NSScreen
*fscreen
= [screens objectAtIndex
: 0];
1229 NSScreen
*screen
= [[view window
] screen
];
1231 NSTRACE
(x_set_offset
);
1238 if (view != nil
&& screen
&& fscreen
)
1240 f
->left_pos
= f
->size_hint_flags
& XNegative
1241 ?
[screen visibleFrame
].
size.width
+ f
->left_pos
- FRAME_PIXEL_WIDTH
(f
)
1243 /* We use visibleFrame here to take menu bar into account.
1244 Ideally we should also adjust left/top with visibleFrame.origin. */
1246 f
->top_pos
= f
->size_hint_flags
& YNegative
1247 ?
([screen visibleFrame
].
size.height
+ f
->top_pos
1248 - FRAME_PIXEL_HEIGHT
(f
) - FRAME_NS_TITLEBAR_HEIGHT
(f
)
1249 - FRAME_TOOLBAR_HEIGHT
(f
))
1251 #ifdef NS_IMPL_GNUSTEP
1252 if (f
->left_pos
< 100)
1253 f
->left_pos
= 100; /* don't overlap menu */
1255 /* Constrain the setFrameTopLeftPoint so we don't move behind the
1257 f
->output_data.ns
->dont_constrain
= 0;
1258 [[view window
] setFrameTopLeftPoint
:
1259 NSMakePoint
(SCREENMAXBOUND
(f
->left_pos
),
1260 SCREENMAXBOUND
([fscreen frame
].
size.height
1261 - NS_TOP_POS
(f
)))];
1262 f
->size_hint_flags
&= ~
(XNegative|YNegative
);
1270 x_set_window_size
(struct frame
*f
, int change_grav
, int cols
, int rows
)
1271 /* --------------------------------------------------------------------------
1272 Adjust window pixel size based on given character grid size
1273 Impl is a bit more complex than other terms, need to do some
1275 -------------------------------------------------------------------------- */
1277 EmacsView
*view = FRAME_NS_VIEW
(f
);
1278 NSWindow
*window
= [view window
];
1279 NSRect wr
= [window frame
];
1280 int tb
= FRAME_EXTERNAL_TOOL_BAR
(f
);
1281 int pixelwidth
, pixelheight
;
1283 NSTRACE
(x_set_window_size
);
1288 /*fprintf (stderr, "\tsetWindowSize: %d x %d, font size %d x %d\n", cols, rows, FRAME_COLUMN_WIDTH (f), FRAME_LINE_HEIGHT (f)); */
1292 check_frame_size
(f
, &rows
, &cols
);
1294 f
->scroll_bar_actual_width
= NS_SCROLL_BAR_WIDTH
(f
);
1295 compute_fringe_widths
(f
, 0);
1297 pixelwidth
= FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, cols
);
1298 pixelheight
= FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, rows
);
1300 /* If we have a toolbar, take its height into account. */
1301 if (tb
&& ! [view isFullscreen
])
1303 /* NOTE: previously this would generate wrong result if toolbar not
1304 yet displayed and fixing toolbar_height=32 helped, but
1305 now (200903) seems no longer needed */
1306 FRAME_TOOLBAR_HEIGHT
(f
) =
1307 NSHeight
([window frameRectForContentRect
: NSMakeRect
(0, 0, 0, 0)])
1308 - FRAME_NS_TITLEBAR_HEIGHT
(f
);
1309 #ifdef NS_IMPL_GNUSTEP
1310 FRAME_TOOLBAR_HEIGHT
(f
) -= 3;
1314 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
1316 wr.
size.width
= pixelwidth
+ f
->border_width
;
1317 wr.
size.height
= pixelheight
;
1318 if (! [view isFullscreen
])
1319 wr.
size.height
+= FRAME_NS_TITLEBAR_HEIGHT
(f
)
1320 + FRAME_TOOLBAR_HEIGHT
(f
);
1322 /* Do not try to constrain to this screen. We may have multiple
1323 screens, and want Emacs to span those. Constraining to screen
1324 prevents that, and that is not nice to the user. */
1325 if (f
->output_data.ns
->zooming
)
1326 f
->output_data.ns
->zooming
= 0;
1328 wr.origin.y
+= FRAME_PIXEL_HEIGHT
(f
) - pixelheight
;
1330 [view setRows
: rows andColumns
: cols
];
1331 [window setFrame
: wr display
: YES
];
1333 /*fprintf (stderr, "\tx_set_window_size %d, %d\t%d, %d\n", cols, rows, pixelwidth, pixelheight); */
1335 /* This is a trick to compensate for Emacs' managing the scrollbar area
1336 as a fixed number of standard character columns. Instead of leaving
1337 blank space for the extra, we chopped it off above. Now for
1338 left-hand scrollbars, we shift all rendering to the left by the
1339 difference between the real width and Emacs' imagined one. For
1340 right-hand bars, don't worry about it since the extra is never used.
1341 (Obviously doesn't work for vertically split windows tho..) */
1343 NSPoint origin
= FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT
(f
)
1344 ? NSMakePoint
(FRAME_SCROLL_BAR_COLS
(f
) * FRAME_COLUMN_WIDTH
(f
)
1345 - NS_SCROLL_BAR_WIDTH
(f
), 0)
1346 : NSMakePoint
(0, 0);
1347 [view setFrame
: NSMakeRect
(0, 0, pixelwidth
, pixelheight
)];
1348 [view setBoundsOrigin
: origin
];
1351 change_frame_size
(f
, rows
, cols
, 0, 1, 0); /* pretend, delay, safe */
1352 FRAME_PIXEL_WIDTH
(f
) = pixelwidth
;
1353 FRAME_PIXEL_HEIGHT
(f
) = pixelheight
;
1354 /* SET_FRAME_GARBAGED (f); // this short-circuits expose call in drawRect */
1356 mark_window_cursors_off
(XWINDOW
(f
->root_window
));
1357 cancel_mouse_face
(f
);
1364 ns_fullscreen_hook
(struct frame
*f
)
1366 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(f
);
1368 if (!FRAME_VISIBLE_P
(f
))
1371 if (! [view fsIsNative
] && f
->want_fullscreen
== FULLSCREEN_BOTH
)
1373 /* Old style fs don't initiate correctly if created from
1374 init/default-frame alist, so use a timer (not nice...).
1376 [NSTimer scheduledTimerWithTimeInterval
: 0.5 target
: view
1377 selector
: @selector
(handleFS
)
1378 userInfo
: nil repeats
: NO
];
1387 /* ==========================================================================
1391 ========================================================================== */
1395 ns_lookup_indexed_color
(unsigned
long idx
, struct frame
*f
)
1397 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1398 if (idx
< 1 || idx
>= color_table
->avail
)
1400 return color_table
->colors
[idx
];
1405 ns_index_color
(NSColor
*color
, struct frame
*f
)
1407 struct ns_color_table
*color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1411 if (!color_table
->colors
)
1413 color_table
->size = NS_COLOR_CAPACITY
;
1414 color_table
->avail
= 1; /* skip idx=0 as marker */
1415 color_table
->colors
= xmalloc
(color_table
->size * sizeof
(NSColor
*));
1416 color_table
->colors
[0] = nil
;
1417 color_table
->empty_indices
= [[NSMutableSet alloc
] init
];
1420 /* do we already have this color ? */
1421 for (i
= 1; i
< color_table
->avail
; i
++)
1422 if (color_table
->colors
[i
] && [color_table
->colors
[i
] isEqual: color
])
1425 if ([color_table
->empty_indices count
] > 0)
1427 NSNumber
*index
= [color_table
->empty_indices anyObject
];
1428 [color_table
->empty_indices removeObject
: index
];
1429 idx
= [index unsignedLongValue
];
1433 if (color_table
->avail
== color_table
->size)
1434 color_table
->colors
=
1435 xpalloc
(color_table
->colors
, &color_table
->size, 1,
1436 min (ULONG_MAX
, PTRDIFF_MAX
), sizeof
*color_table
->colors
);
1437 idx
= color_table
->avail
++;
1440 color_table
->colors
[idx
] = color
;
1442 /*fprintf(stderr, "color_table: allocated %d\n",idx);*/
1448 ns_free_indexed_color
(unsigned
long idx
, struct frame
*f
)
1450 struct ns_color_table
*color_table
;
1457 color_table
= FRAME_NS_DISPLAY_INFO
(f
)->color_table
;
1459 if (idx
<= 0 || idx
>= color_table
->size) {
1460 message1
("ns_free_indexed_color
: Color index out of range.
\n"
);
1464 index
= [NSNumber numberWithUnsignedInt
: idx
];
1465 if ([color_table
->empty_indices containsObject
: index
]) {
1466 message1
("ns_free_indexed_color
: attempt to free already freed color.
\n"
);
1470 color
= color_table
->colors
[idx
];
1472 color_table
->colors
[idx
] = nil
;
1473 [color_table
->empty_indices addObject
: index
];
1474 /*fprintf(stderr, "color_table: FREED %d\n",idx);*/
1479 ns_get_color
(const
char *name
, NSColor
**col
)
1480 /* --------------------------------------------------------------------------
1482 -------------------------------------------------------------------------- */
1483 /* On *Step, we attempt to mimic the X11 platform here, down to installing an
1484 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1485 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1488 static
char hex
[20];
1490 float r
= -1.0, g
, b
;
1491 NSString
*nsname
= [NSString stringWithUTF8String
: name
];
1493 /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
1496 if ([nsname isEqualToString
: @"ns_selection_color"
])
1498 nsname
= ns_selection_color
;
1499 name
= [ns_selection_color UTF8String
];
1502 /* First, check for some sort of numeric specification. */
1505 if (name
[0] == '0' || name
[0] == '1' || name
[0] == '.') /* RGB decimal */
1507 NSScanner
*scanner
= [NSScanner scannerWithString
: nsname
];
1508 [scanner scanFloat
: &r
];
1509 [scanner scanFloat
: &g
];
1510 [scanner scanFloat
: &b
];
1512 else if (!strncmp
(name
, "rgb
:"
, 4)) /* A newer X11 format -- rgb:r/g/b */
1513 scaling
= (snprintf
(hex
, sizeof hex
, "
%s", name + 4) - 2) / 3;
1514 else if (name
[0] == '#') /* An old X11 format; convert to newer */
1516 int len
= (strlen
(name
) - 1);
1517 int start
= (len
% 3 == 0) ? 1 : len / 4 + 1;
1519 scaling
= strlen
(name
+start
) / 3;
1520 for (i
= 0; i
< 3; i
++)
1521 sprintf (hex
+ i
* (scaling
+ 1), "
%.*s/", scaling,
1522 name
+ start
+ i
* scaling
);
1523 hex
[3 * (scaling
+ 1) - 1] = '\0';
1529 float fscale
= scaling
== 4 ?
65535.0 : (scaling
== 2 ?
255.0 : 15.0);
1530 if (sscanf (hex
, "
%x/%x/%x", &rr, &gg, &bb))
1540 *col
= [NSColor colorWithCalibratedRed
: r green
: g blue
: b alpha
: 1.0];
1545 /* Otherwise, color is expected to be from a list */
1547 NSEnumerator
*lenum
, *cenum
;
1551 #ifdef NS_IMPL_GNUSTEP
1552 /* XXX: who is wrong, the requestor or the implementation? */
1553 if ([nsname compare
: @"Highlight" options
: NSCaseInsensitiveSearch
]
1555 nsname
= @"highlightColor"
;
1558 lenum
= [[NSColorList availableColorLists
] objectEnumerator
];
1559 while ( (clist
= [lenum nextObject
]) && new
== nil
)
1561 cenum
= [[clist allKeys
] objectEnumerator
];
1562 while ( (name
= [cenum nextObject
]) && new
== nil
)
1564 if ([name compare
: nsname
1565 options
: NSCaseInsensitiveSearch
] == NSOrderedSame
)
1566 new
= [clist colorWithKey
: name
];
1572 *col
= [new colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
];
1579 ns_lisp_to_color
(Lisp_Object color
, NSColor
**col
)
1580 /* --------------------------------------------------------------------------
1581 Convert a Lisp string object to a NS color
1582 -------------------------------------------------------------------------- */
1584 NSTRACE
(ns_lisp_to_color
);
1585 if (STRINGP
(color
))
1586 return ns_get_color
(SSDATA
(color
), col
);
1587 else if (SYMBOLP
(color
))
1588 return ns_get_color
(SSDATA
(SYMBOL_NAME
(color
)), col
);
1594 ns_color_to_lisp
(NSColor
*col
)
1595 /* --------------------------------------------------------------------------
1596 Convert a color to a lisp string with the RGB equivalent
1597 -------------------------------------------------------------------------- */
1599 EmacsCGFloat red
, green
, blue
, alpha
, gray;
1602 NSTRACE
(ns_color_to_lisp
);
1605 if ([[col colorSpaceName
] isEqualToString
: NSNamedColorSpace
])
1607 if ((str
=[[col colorNameComponent
] UTF8String
]))
1610 return build_string
((char *)str
);
1613 [[col colorUsingColorSpaceName
: NSCalibratedRGBColorSpace
]
1614 getRed
: &red green
: &green blue
: &blue alpha
: &alpha
];
1615 if (red
==green
&& red
==blue
)
1617 [[col colorUsingColorSpaceName
: NSCalibratedWhiteColorSpace
]
1618 getWhite
: &gray alpha
: &alpha
];
1619 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1620 lrint
(gray * 0xff), lrint
(gray * 0xff), lrint
(gray * 0xff));
1622 return build_string
(buf
);
1625 snprintf
(buf
, sizeof
(buf
), "#
%2.2lx%2.2lx%2.2lx",
1626 lrint
(red
*0xff), lrint
(green
*0xff), lrint
(blue
*0xff));
1629 return build_string
(buf
);
1634 ns_query_color
(void
*col
, XColor
*color_def
, int setPixel
)
1635 /* --------------------------------------------------------------------------
1636 Get ARGB values out of NSColor col and put them into color_def.
1637 If setPixel, set the pixel to a concatenated version.
1638 and set color_def pixel to the resulting index.
1639 -------------------------------------------------------------------------- */
1641 EmacsCGFloat r
, g
, b
, a
;
1643 [((NSColor
*)col
) getRed
: &r green
: &g blue
: &b alpha
: &a
];
1644 color_def
->red
= r
* 65535;
1645 color_def
->green
= g
* 65535;
1646 color_def
->blue
= b
* 65535;
1648 if (setPixel
== YES
)
1650 = ARGB_TO_ULONG
((int
)(a
*255),
1651 (int
)(r
*255), (int
)(g
*255), (int
)(b
*255));
1656 ns_defined_color
(struct frame
*f
,
1661 /* --------------------------------------------------------------------------
1662 Return true if named color found, and set color_def rgb accordingly.
1663 If makeIndex and alloc are nonzero put the color in the color_table,
1664 and set color_def pixel to the resulting index.
1665 If makeIndex is zero, set color_def pixel to ARGB.
1666 Return false if not found
1667 -------------------------------------------------------------------------- */
1670 NSTRACE
(ns_defined_color
);
1673 if (ns_get_color
(name
, &col
) != 0) /* Color not found */
1678 if (makeIndex
&& alloc
)
1679 color_def
->pixel
= ns_index_color
(col
, f
);
1680 ns_query_color
(col
, color_def
, !makeIndex
);
1687 x_set_frame_alpha
(struct frame
*f
)
1688 /* --------------------------------------------------------------------------
1689 change the entire-frame transparency
1690 -------------------------------------------------------------------------- */
1692 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(f
);
1694 double alpha_min
= 1.0;
1696 if (dpyinfo
->x_highlight_frame
== f
)
1697 alpha
= f
->alpha
[0];
1699 alpha
= f
->alpha
[1];
1701 if (FLOATP
(Vframe_alpha_lower_limit
))
1702 alpha_min
= XFLOAT_DATA
(Vframe_alpha_lower_limit
);
1703 else if (INTEGERP
(Vframe_alpha_lower_limit
))
1704 alpha_min
= (XINT
(Vframe_alpha_lower_limit
)) / 100.0;
1708 else if (1.0 < alpha
)
1710 else if (0.0 <= alpha
&& alpha
< alpha_min
&& alpha_min
<= 1.0)
1713 #ifdef NS_IMPL_COCOA
1715 EmacsView
*view = FRAME_NS_VIEW
(f
);
1716 [[view window
] setAlphaValue
: alpha
];
1722 /* ==========================================================================
1726 ========================================================================== */
1730 x_set_mouse_pixel_position
(struct frame
*f
, int pix_x
, int pix_y
)
1731 /* --------------------------------------------------------------------------
1732 Programmatically reposition mouse pointer in pixel coordinates
1733 -------------------------------------------------------------------------- */
1735 NSTRACE
(x_set_mouse_pixel_position
);
1738 /* FIXME: this does not work, and what about GNUstep? */
1739 #ifdef NS_IMPL_COCOA
1740 [FRAME_NS_VIEW
(f
) lockFocus
];
1741 PSsetmouse
((float
)pix_x
, (float
)pix_y
);
1742 [FRAME_NS_VIEW
(f
) unlockFocus
];
1749 x_set_mouse_position
(struct frame
*f
, int h
, int v
)
1750 /* --------------------------------------------------------------------------
1751 Programmatically reposition mouse pointer in character coordinates
1752 -------------------------------------------------------------------------- */
1756 pix_x
= FRAME_COL_TO_PIXEL_X
(f
, h
) + FRAME_COLUMN_WIDTH
(f
) / 2;
1757 pix_y
= FRAME_LINE_TO_PIXEL_Y
(f
, v
) + FRAME_LINE_HEIGHT
(f
) / 2;
1759 if (pix_x
< 0) pix_x
= 0;
1760 if (pix_x
> FRAME_PIXEL_WIDTH
(f
)) pix_x
= FRAME_PIXEL_WIDTH
(f
);
1762 if (pix_y
< 0) pix_y
= 0;
1763 if (pix_y
> FRAME_PIXEL_HEIGHT
(f
)) pix_y
= FRAME_PIXEL_HEIGHT
(f
);
1765 x_set_mouse_pixel_position
(f
, pix_x
, pix_y
);
1770 note_mouse_movement
(struct frame
*frame
, CGFloat x
, CGFloat y
)
1771 /* ------------------------------------------------------------------------
1772 Called by EmacsView on mouseMovement events. Passes on
1773 to emacs mainstream code if we moved off of a rect of interest
1774 known as last_mouse_glyph.
1775 ------------------------------------------------------------------------ */
1777 // NSTRACE
(note_mouse_movement
);
1779 XSETFRAME
(last_mouse_motion_frame
, frame
);
1781 /* Note, this doesn't get called for enter/leave, since we don't have a
1782 position. Those are taken care of in the corresponding NSView methods. */
1784 /* has movement gone beyond last rect we were tracking? */
1785 if (x
< last_mouse_glyph.origin.x ||
1786 x
>= (last_mouse_glyph.origin.x
+ last_mouse_glyph.
size.width
) ||
1787 y
< last_mouse_glyph.origin.y ||
1788 y
>= (last_mouse_glyph.origin.y
+ last_mouse_glyph.
size.height
))
1790 ns_update_begin
(frame
);
1791 frame
->mouse_moved
= 1;
1792 note_mouse_highlight
(frame
, x
, y
);
1793 remember_mouse_glyph
(frame
, x
, y
, &last_mouse_glyph
);
1794 ns_update_end
(frame
);
1803 ns_mouse_position
(struct frame
**fp
, int insist
, Lisp_Object
*bar_window
,
1804 enum scroll_bar_part
*part
, Lisp_Object
*x
, Lisp_Object
*y
,
1806 /* --------------------------------------------------------------------------
1807 External (hook): inform emacs about mouse position and hit parts.
1808 If a scrollbar is being dragged, set bar_window, part, x, y, time.
1809 x & y should be position in the scrollbar (the whole bar, not the handle)
1810 and length of scrollbar respectively
1811 -------------------------------------------------------------------------- */
1815 Lisp_Object frame
, tail
;
1817 struct ns_display_info
*dpyinfo
;
1819 NSTRACE
(ns_mouse_position
);
1823 fprintf (stderr
, "Warning
: ns_mouse_position
() called with
null *fp.
\n"
);
1827 dpyinfo
= FRAME_NS_DISPLAY_INFO
(*fp
);
1831 if (last_mouse_scroll_bar
!= nil
&& insist
== 0)
1833 /* TODO: we do not use this path at the moment because drag events will
1834 go directly to the EmacsScroller. Leaving code in for now. */
1835 [last_mouse_scroll_bar getMouseMotionPart
: (int
*)part window
: bar_window
1837 if (time
) *time
= last_mouse_movement_time
;
1838 last_mouse_scroll_bar
= nil
;
1842 /* Clear the mouse-moved flag for every frame on this display. */
1843 FOR_EACH_FRAME
(tail
, frame
)
1844 if (FRAME_NS_P
(XFRAME
(frame
))
1845 && FRAME_NS_DISPLAY
(XFRAME
(frame
)) == FRAME_NS_DISPLAY
(*fp
))
1846 XFRAME
(frame
)->mouse_moved
= 0;
1848 last_mouse_scroll_bar
= nil
;
1849 if (last_mouse_frame
&& FRAME_LIVE_P
(last_mouse_frame
))
1850 f
= last_mouse_frame
;
1852 f
= dpyinfo
->x_focus_frame ? dpyinfo
->x_focus_frame
1853 : SELECTED_FRAME
();
1855 if (f
&& FRAME_NS_P
(f
))
1857 view = FRAME_NS_VIEW
(*fp
);
1859 position
= [[view window
] mouseLocationOutsideOfEventStream
];
1860 position
= [view convertPoint
: position fromView
: nil
];
1861 remember_mouse_glyph
(f
, position.x
, position.y
, &last_mouse_glyph
);
1862 /*fprintf (stderr, "ns_mouse_position: %.0f, %.0f\n", position.x, position.y); */
1864 if (bar_window
) *bar_window
= Qnil
;
1865 if (part
) *part
= 0; /*scroll_bar_handle; */
1867 if (x
) XSETINT
(*x
, lrint
(position.x
));
1868 if (y
) XSETINT
(*y
, lrint
(position.y
));
1869 if (time
) *time
= last_mouse_movement_time
;
1879 ns_frame_up_to_date
(struct frame
*f
)
1880 /* --------------------------------------------------------------------------
1881 External (hook): Fix up mouse highlighting right after a full update.
1882 Can't use FRAME_MOUSE_UPDATE due to ns_frame_begin and ns_frame_end calls.
1883 -------------------------------------------------------------------------- */
1885 NSTRACE
(ns_frame_up_to_date
);
1889 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(f
);
1890 if (f
== hlinfo
->mouse_face_mouse_frame
)
1894 note_mouse_highlight
(hlinfo
->mouse_face_mouse_frame
,
1895 hlinfo
->mouse_face_mouse_x
,
1896 hlinfo
->mouse_face_mouse_y
);
1905 ns_define_frame_cursor
(struct frame
*f
, Cursor cursor
)
1906 /* --------------------------------------------------------------------------
1907 External (RIF): set frame mouse pointer type.
1908 -------------------------------------------------------------------------- */
1910 NSTRACE
(ns_define_frame_cursor
);
1911 if (FRAME_POINTER_TYPE
(f
) != cursor
)
1913 EmacsView
*view = FRAME_NS_VIEW
(f
);
1914 FRAME_POINTER_TYPE
(f
) = cursor
;
1915 [[view window
] invalidateCursorRectsForView
: view];
1916 /* Redisplay assumes this function also draws the changed frame
1917 cursor, but this function doesn't, so do it explicitly. */
1918 x_update_cursor
(f
, 1);
1924 /* ==========================================================================
1928 ========================================================================== */
1932 ns_convert_key
(unsigned code
)
1933 /* --------------------------------------------------------------------------
1934 Internal call used by NSView-keyDown.
1935 -------------------------------------------------------------------------- */
1937 const unsigned last_keysym
= (sizeof
(convert_ns_to_X_keysym
)
1938 / sizeof
(convert_ns_to_X_keysym
[0]));
1940 /* An array would be faster, but less easy to read. */
1941 for (keysym
= 0; keysym
< last_keysym
; keysym
+= 2)
1942 if (code
== convert_ns_to_X_keysym
[keysym
])
1943 return 0xFF00 | convert_ns_to_X_keysym
[keysym
+1];
1945 /* if decide to use keyCode and Carbon table, use this line:
1946 return code > 0xff ? 0 : 0xFF00 | ns_keycode_to_xkeysym_table[code]; */
1951 x_get_keysym_name
(int keysym
)
1952 /* --------------------------------------------------------------------------
1953 Called by keyboard.c. Not sure if the return val is important, except
1955 -------------------------------------------------------------------------- */
1957 static
char value
[16];
1958 NSTRACE
(x_get_keysym_name
);
1959 sprintf (value
, "
%d", keysym);
1965 /* ==========================================================================
1967 Block drawing operations
1969 ========================================================================== */
1973 ns_redraw_scroll_bars
(struct frame
*f
)
1977 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
1978 NSTRACE
(ns_redraw_scroll_bars
);
1979 for (i
=[subviews count
]-1; i
>= 0; i
--)
1981 view = [subviews objectAtIndex
: i
];
1982 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
1989 ns_clear_frame
(struct frame
*f
)
1990 /* --------------------------------------------------------------------------
1991 External (hook): Erase the entire frame
1992 -------------------------------------------------------------------------- */
1994 NSView
*view = FRAME_NS_VIEW
(f
);
1997 NSTRACE
(ns_clear_frame
);
1999 /* comes on initial frame because we have
2000 after-make-frame-functions = select-frame */
2001 if (!FRAME_DEFAULT_FACE
(f
))
2004 mark_window_cursors_off
(XWINDOW
(FRAME_ROOT_WINDOW
(f
)));
2006 output_cursor.hpos
= output_cursor.vpos
= 0;
2007 output_cursor.x
= -1;
2012 ns_focus
(f
, &r
, 1);
2013 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(FRAME_DEFAULT_FACE
(f
)), f
) set];
2017 /* as of 2006/11 or so this is now needed */
2018 ns_redraw_scroll_bars
(f
);
2024 ns_clear_frame_area
(struct frame
*f
, int x
, int y
, int width
, int height
)
2025 /* --------------------------------------------------------------------------
2026 External (RIF): Clear section of frame
2027 -------------------------------------------------------------------------- */
2029 NSRect r
= NSMakeRect
(x
, y
, width
, height
);
2030 NSView
*view = FRAME_NS_VIEW
(f
);
2031 struct face
*face
= FRAME_DEFAULT_FACE
(f
);
2036 NSTRACE
(ns_clear_frame_area
);
2038 r
= NSIntersectionRect
(r
, [view frame
]);
2039 ns_focus
(f
, &r
, 1);
2040 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), f
) set];
2050 ns_scroll_run
(struct window
*w
, struct run
*run
)
2051 /* --------------------------------------------------------------------------
2052 External (RIF): Insert or delete n lines at line vpos
2053 -------------------------------------------------------------------------- */
2055 struct frame
*f
= XFRAME
(w
->frame
);
2056 int x
, y
, width
, height
, from_y
, to_y
, bottom_y
;
2058 NSTRACE
(ns_scroll_run
);
2060 /* begin copy from other terms */
2061 /* Get frame-relative bounding box of the text display area of W,
2062 without mode lines. Include in this box the left and right
2064 window_box
(w
, -1, &x
, &y
, &width
, &height
);
2066 from_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->current_y
);
2067 to_y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, run
->desired_y
);
2068 bottom_y
= y
+ height
;
2072 /* Scrolling up. Make sure we don't copy part of the mode
2073 line at the bottom. */
2074 if (from_y
+ run
->height
> bottom_y
)
2075 height
= bottom_y
- from_y
;
2077 height
= run
->height
;
2081 /* Scrolling down. Make sure we don't copy over the mode line.
2083 if (to_y
+ run
->height
> bottom_y
)
2084 height
= bottom_y
- to_y
;
2086 height
= run
->height
;
2088 /* end copy from other terms */
2098 NSRect srcRect
= NSMakeRect
(x
, from_y
, width
, height
);
2099 NSRect dstRect
= NSMakeRect
(x
, to_y
, width
, height
);
2100 NSPoint dstOrigin
= NSMakePoint
(x
, to_y
);
2102 ns_focus
(f
, &dstRect
, 1);
2103 NSCopyBits
(0, srcRect
, dstOrigin
);
2112 ns_after_update_window_line
(struct window
*w
, struct glyph_row
*desired_row
)
2113 /* --------------------------------------------------------------------------
2114 External (RIF): preparatory to fringe update after text was updated
2115 -------------------------------------------------------------------------- */
2120 NSTRACE
(ns_after_update_window_line
);
2122 /* begin copy from other terms */
2125 if (!desired_row
->mode_line_p
&& !w
->pseudo_window_p
)
2126 desired_row
->redraw_fringe_bitmaps_p
= 1;
2128 /* When a window has disappeared, make sure that no rest of
2129 full-width rows stays visible in the internal border. */
2130 if (windows_or_buffers_changed
2131 && desired_row
->full_width_p
2132 && (f
= XFRAME
(w
->frame
),
2133 width
= FRAME_INTERNAL_BORDER_WIDTH
(f
),
2135 && (height
= desired_row
->visible_height
,
2138 int y
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (0, desired_row
->y
));
2141 ns_clear_frame_area
(f
, 0, y
, width
, height
);
2142 ns_clear_frame_area
(f
,
2143 FRAME_PIXEL_WIDTH
(f
) - width
,
2151 ns_shift_glyphs_for_insert
(struct frame
*f
,
2152 int x
, int y
, int width
, int height
,
2154 /* --------------------------------------------------------------------------
2155 External (RIF): copy an area horizontally, don't worry about clearing src
2156 -------------------------------------------------------------------------- */
2158 NSRect srcRect
= NSMakeRect
(x
, y
, width
, height
);
2159 NSRect dstRect
= NSMakeRect
(x
+shift_by
, y
, width
, height
);
2160 NSPoint dstOrigin
= dstRect.origin
;
2162 NSTRACE
(ns_shift_glyphs_for_insert
);
2164 ns_focus
(f
, &dstRect
, 1);
2165 NSCopyBits
(0, srcRect
, dstOrigin
);
2171 /* ==========================================================================
2173 Character encoding and metrics
2175 ========================================================================== */
2179 ns_compute_glyph_string_overhangs
(struct glyph_string
*s
)
2180 /* --------------------------------------------------------------------------
2181 External (RIF); compute left/right overhang of whole string and set in s
2182 -------------------------------------------------------------------------- */
2184 struct font
*font
= s
->font
;
2188 struct font_metrics metrics
;
2189 unsigned int codes
[2];
2190 codes
[0] = *(s
->char2b
);
2191 codes
[1] = *(s
->char2b
+ s
->nchars
- 1);
2193 font
->driver
->text_extents
(font
, codes
, 2, &metrics
);
2194 s
->left_overhang
= -metrics.lbearing
;
2196 = metrics.rbearing
> metrics.width
2197 ? metrics.rbearing
- metrics.width
: 0;
2201 s
->left_overhang
= 0;
2202 s
->right_overhang
= ((struct nsfont_info
*)font
)->ital ?
2203 FONT_HEIGHT
(font
) * 0.2 : 0;
2209 /* ==========================================================================
2211 Fringe and cursor drawing
2213 ========================================================================== */
2216 extern int max_used_fringe_bitmap
;
2218 ns_draw_fringe_bitmap
(struct window
*w
, struct glyph_row
*row
,
2219 struct draw_fringe_bitmap_params
*p
)
2220 /* --------------------------------------------------------------------------
2221 External (RIF); fringe-related
2222 -------------------------------------------------------------------------- */
2224 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2225 struct face
*face
= p
->face
;
2226 static EmacsImage
**bimgs
= NULL;
2227 static int nBimgs
= 0;
2229 /* grow bimgs if needed */
2230 if (nBimgs
< max_used_fringe_bitmap
)
2232 bimgs
= xrealloc
(bimgs
, max_used_fringe_bitmap
* sizeof
*bimgs
);
2233 memset
(bimgs
+ nBimgs
, 0,
2234 (max_used_fringe_bitmap
- nBimgs
) * sizeof
*bimgs
);
2235 nBimgs
= max_used_fringe_bitmap
;
2238 /* Must clip because of partially visible lines. */
2239 ns_clip_to_row
(w
, row
, -1, YES
);
2243 int bx
= p
->bx
, by
= p
->by
, nx
= p
->nx
, ny
= p
->ny
;
2245 /* If the fringe is adjacent to the left (right) scroll bar of a
2246 leftmost (rightmost, respectively) window, then extend its
2247 background to the gap between the fringe and the bar. */
2248 if ((WINDOW_LEFTMOST_P
(w
)
2249 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT
(w
))
2250 ||
(WINDOW_RIGHTMOST_P
(w
)
2251 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT
(w
)))
2253 int sb_width
= WINDOW_CONFIG_SCROLL_BAR_WIDTH
(w
);
2257 int bar_area_x
= WINDOW_SCROLL_BAR_AREA_X
(w
);
2258 int bar_area_width
= (WINDOW_CONFIG_SCROLL_BAR_COLS
(w
)
2259 * FRAME_COLUMN_WIDTH
(f
));
2263 /* Bitmap fills the fringe. */
2264 if (bar_area_x
+ bar_area_width
== p
->x
)
2265 bx
= bar_area_x
+ sb_width
;
2266 else if (p
->x
+ p
->wd
== bar_area_x
)
2270 int header_line_height
= WINDOW_HEADER_LINE_HEIGHT
(w
);
2272 nx
= bar_area_width
- sb_width
;
2273 by
= WINDOW_TO_FRAME_PIXEL_Y
(w
, max (header_line_height
,
2275 ny
= row
->visible_height
;
2280 if (bar_area_x
+ bar_area_width
== bx
)
2282 bx
= bar_area_x
+ sb_width
;
2283 nx
+= bar_area_width
- sb_width
;
2285 else if (bx
+ nx
== bar_area_x
)
2286 nx
+= bar_area_width
- sb_width
;
2291 if (bx
>= 0 && nx
> 0)
2293 NSRect r
= NSMakeRect
(bx
, by
, nx
, ny
);
2295 [ns_lookup_indexed_color
(face
->background
, f
) set];
2302 NSRect r
= NSMakeRect
(p
->x
, p
->y
, p
->wd
, p
->h
);
2303 EmacsImage
*img
= bimgs
[p
->which - 1];
2307 unsigned short
*bits
= p
->bits
+ p
->dh
;
2310 unsigned
char *cbits
= xmalloc
(len
);
2312 for (i
= 0; i
< len
; i
++)
2313 cbits
[i
] = ~
(bits
[i
] & 0xff);
2314 img
= [[EmacsImage alloc
] initFromXBM
: cbits width
: 8 height
: p
->h
2316 bimgs
[p
->which - 1] = img
;
2321 /* Since we composite the bitmap instead of just blitting it, we need
2322 to erase the whole background. */
2323 [ns_lookup_indexed_color
(face
->background
, f
) set];
2325 [img setXBMColor
: ns_lookup_indexed_color
(face
->foreground
, f
)];
2326 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
2328 fromRect
: NSZeroRect
2329 operation
: NSCompositeSourceOver
2335 NSPoint pt
= r.origin
;
2337 [img compositeToPoint
: pt operation
: NSCompositeSourceOver
];
2346 ns_draw_window_cursor
(struct window
*w
, struct glyph_row
*glyph_row
,
2347 int x
, int y
, int cursor_type
, int cursor_width
,
2348 int on_p
, int active_p
)
2349 /* --------------------------------------------------------------------------
2350 External call (RIF): draw cursor.
2351 Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
2352 -------------------------------------------------------------------------- */
2355 int fx
, fy
, h
, cursor_height
;
2356 struct frame
*f
= WINDOW_XFRAME
(w
);
2357 struct glyph
*phys_cursor_glyph
;
2358 struct glyph
*cursor_glyph
;
2360 NSColor
*hollow_color
= FRAME_BACKGROUND_COLOR
(f
);
2362 /* If cursor is out of bounds, don't draw garbage. This can happen
2363 in mini-buffer windows when switching between echo area glyphs
2366 NSTRACE
(dumpcursor
);
2371 w
->phys_cursor_type
= cursor_type
;
2372 w
->phys_cursor_on_p
= on_p
;
2374 if (cursor_type
== NO_CURSOR
)
2376 w
->phys_cursor_width
= 0;
2380 if ((phys_cursor_glyph
= get_phys_cursor_glyph
(w
)) == NULL)
2382 if (glyph_row
->exact_window_width_line_p
2383 && w
->phys_cursor.hpos
>= glyph_row
->used
[TEXT_AREA
])
2385 glyph_row
->cursor_in_fringe_p
= 1;
2386 draw_fringe_bitmap
(w
, glyph_row
, 0);
2391 /* We draw the cursor (with NSRectFill), then draw the glyph on top
2392 (other terminals do it the other way round). We must set
2393 w->phys_cursor_width to the cursor width. For bar cursors, that
2394 is CURSOR_WIDTH; for box cursors, it is the glyph width. */
2395 get_phys_cursor_geometry
(w
, glyph_row
, phys_cursor_glyph
, &fx
, &fy
, &h
);
2397 /* The above get_phys_cursor_geometry call set w->phys_cursor_width
2398 to the glyph width; replace with CURSOR_WIDTH for (V)BAR cursors. */
2399 if (cursor_type
== BAR_CURSOR
)
2401 if (cursor_width
< 1)
2402 cursor_width
= max (FRAME_CURSOR_WIDTH
(f
), 1);
2403 w
->phys_cursor_width
= cursor_width
;
2405 /* If we have an HBAR, "cursor_width" MAY specify height. */
2406 else if (cursor_type
== HBAR_CURSOR
)
2408 cursor_height
= (cursor_width
< 1) ? lrint
(0.25 * h
) : cursor_width
;
2409 fy
+= h
- cursor_height
;
2413 r.origin.x
= fx
, r.origin.y
= fy
;
2415 r.
size.width
= w
->phys_cursor_width
;
2417 /* TODO: only needed in rare cases with last-resort font in HELLO..
2418 should we do this more efficiently? */
2419 ns_clip_to_row
(w
, glyph_row
, -1, NO
); /* do ns_focus(f, &r, 1); if remove */
2422 face
= FACE_FROM_ID
(f
, phys_cursor_glyph
->face_id
);
2423 if (face
&& NS_FACE_BACKGROUND
(face
)
2424 == ns_index_color
(FRAME_CURSOR_COLOR
(f
), f
))
2426 [ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), f
) set];
2427 hollow_color
= FRAME_CURSOR_COLOR
(f
);
2430 [FRAME_CURSOR_COLOR
(f
) set];
2432 #ifdef NS_IMPL_COCOA
2433 /* TODO: This makes drawing of cursor plus that of phys_cursor_glyph
2434 atomic. Cleaner ways of doing this should be investigated.
2435 One way would be to set a global variable DRAWING_CURSOR
2436 when making the call to draw_phys..(), don't focus in that
2437 case, then move the ns_unfocus() here after that call. */
2438 NSDisableScreenUpdates
();
2441 switch (cursor_type
)
2445 case FILLED_BOX_CURSOR
:
2448 case HOLLOW_BOX_CURSOR
:
2451 NSRectFill
(NSInsetRect
(r
, 1, 1));
2452 [FRAME_CURSOR_COLOR
(f
) set];
2459 /* If the character under cursor is R2L, draw the bar cursor
2460 on the right of its glyph, rather than on the left. */
2461 cursor_glyph
= get_phys_cursor_glyph
(w
);
2462 if ((cursor_glyph
->resolved_level
& 1) != 0)
2463 s.origin.x
+= cursor_glyph
->pixel_width
- s.
size.width
;
2470 /* draw the character under the cursor */
2471 if (cursor_type
!= NO_CURSOR
)
2472 draw_phys_cursor_glyph
(w
, glyph_row
, DRAW_CURSOR
);
2474 #ifdef NS_IMPL_COCOA
2475 NSEnableScreenUpdates
();
2482 ns_draw_vertical_window_border
(struct window
*w
, int x
, int y0
, int y1
)
2483 /* --------------------------------------------------------------------------
2484 External (RIF): Draw a vertical line.
2485 -------------------------------------------------------------------------- */
2487 struct frame
*f
= XFRAME
(WINDOW_FRAME
(w
));
2489 NSRect r
= NSMakeRect
(x
, y0
, 1, y1
-y0
);
2491 NSTRACE
(ns_draw_vertical_window_border
);
2493 face
= FACE_FROM_ID
(f
, VERTICAL_BORDER_FACE_ID
);
2495 [ns_lookup_indexed_color
(face
->foreground
, f
) set];
2497 ns_focus
(f
, &r
, 1);
2504 show_hourglass
(struct atimer
*timer
)
2506 if (hourglass_shown_p
)
2511 /* TODO: add NSProgressIndicator to selected frame (see macfns.c) */
2513 hourglass_shown_p
= 1;
2519 hide_hourglass
(void
)
2521 if (!hourglass_shown_p
)
2526 /* TODO: remove NSProgressIndicator from all frames */
2528 hourglass_shown_p
= 0;
2534 /* ==========================================================================
2536 Glyph drawing operations
2538 ========================================================================== */
2541 ns_get_glyph_string_clip_rect
(struct glyph_string
*s
, NativeRectangle
*nr
)
2542 /* --------------------------------------------------------------------------
2543 Wrapper utility to account for internal border width on full-width lines,
2544 and allow top full-width rows to hit the frame top. nr should be pointer
2545 to two successive NSRects. Number of rects actually used is returned.
2546 -------------------------------------------------------------------------- */
2548 int n
= get_glyph_string_clip_rects
(s
, nr
, 2);
2552 /* --------------------------------------------------------------------
2553 Draw a wavy line under glyph string s. The wave fills wave_height
2560 wave_height = 3 | * * * *
2561 --------------------------------------------------------------------- */
2564 ns_draw_underwave
(struct glyph_string
*s
, EmacsCGFloat width
, EmacsCGFloat x
)
2566 int wave_height
= 3, wave_length
= 2;
2567 int y
, dx
, dy
, odd
, xmax
;
2572 dy
= wave_height
- 1;
2573 y
= s
->ybase
- wave_height
+ 3;
2576 /* Find and set clipping rectangle */
2577 waveClip
= NSMakeRect
(x
, y
, width
, wave_height
);
2578 [[NSGraphicsContext currentContext
] saveGraphicsState
];
2579 NSRectClip
(waveClip
);
2581 /* Draw the waves */
2582 a.x
= x
- ((int
)(x
) % dx) + (EmacsCGFloat) 0.5;
2584 odd
= (int
)(a.x
/dx
) % 2;
2585 a.y
= b.y
= y
+ 0.5;
2594 [NSBezierPath strokeLineFromPoint
:a toPoint
:b
];
2595 a.x
= b.x
, a.y
= b.y
;
2596 b.x
+= dx
, b.y
= y
+ 0.5 + odd
*dy
;
2600 /* Restore previous clipping rectangle(s) */
2601 [[NSGraphicsContext currentContext
] restoreGraphicsState
];
2607 ns_draw_text_decoration
(struct glyph_string
*s
, struct face
*face
,
2608 NSColor
*defaultCol
, CGFloat width
, CGFloat x
)
2609 /* --------------------------------------------------------------------------
2610 Draw underline, overline, and strike-through on glyph string s.
2611 -------------------------------------------------------------------------- */
2613 if (s
->for_overlaps
)
2617 if (face
->underline_p
)
2619 if (s
->face
->underline_type
== FACE_UNDER_WAVE
)
2621 if (face
->underline_defaulted_p
)
2624 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2626 ns_draw_underwave
(s
, width
, x
);
2628 else if (s
->face
->underline_type
== FACE_UNDER_LINE
)
2632 unsigned
long thickness
, position
;
2634 /* If the prev was underlined, match its appearance. */
2635 if (s
->prev
&& s
->prev
->face
->underline_p
2636 && s
->prev
->face
->underline_type
== FACE_UNDER_LINE
2637 && s
->prev
->underline_thickness
> 0)
2639 thickness
= s
->prev
->underline_thickness
;
2640 position
= s
->prev
->underline_position
;
2645 unsigned
long descent
;
2648 descent
= s
->y
+ s
->height
- s
->ybase
;
2650 /* Use underline thickness of font, defaulting to 1. */
2651 thickness
= (font
&& font
->underline_thickness
> 0)
2652 ? font
->underline_thickness
: 1;
2654 /* Determine the offset of underlining from the baseline. */
2655 if (x_underline_at_descent_line
)
2656 position
= descent
- thickness
;
2657 else if (x_use_underline_position_properties
2658 && font
&& font
->underline_position
>= 0)
2659 position
= font
->underline_position
;
2661 position
= lround
(font
->descent
/ 2);
2663 position
= underline_minimum_offset
;
2665 position
= max (position
, underline_minimum_offset
);
2667 /* Ensure underlining is not cropped. */
2668 if (descent
<= position
)
2670 position
= descent
- 1;
2673 else if (descent
< position
+ thickness
)
2677 s
->underline_thickness
= thickness
;
2678 s
->underline_position
= position
;
2680 r
= NSMakeRect
(x
, s
->ybase
+ position
, width
, thickness
);
2682 if (face
->underline_defaulted_p
)
2685 [ns_lookup_indexed_color
(face
->underline_color
, s
->f
) set];
2689 /* Do overline. We follow other terms in using a thickness of 1
2690 and ignoring overline_margin. */
2691 if (face
->overline_p
)
2694 r
= NSMakeRect
(x
, s
->y
, width
, 1);
2696 if (face
->overline_color_defaulted_p
)
2699 [ns_lookup_indexed_color
(face
->overline_color
, s
->f
) set];
2703 /* Do strike-through. We follow other terms for thickness and
2704 vertical position.*/
2705 if (face
->strike_through_p
)
2710 dy
= lrint
((s
->height
- 1) / 2);
2711 r
= NSMakeRect
(x
, s
->y
+ dy
, width
, 1);
2713 if (face
->strike_through_color_defaulted_p
)
2716 [ns_lookup_indexed_color
(face
->strike_through_color
, s
->f
) set];
2722 ns_draw_box
(NSRect r
, CGFloat thickness
, NSColor
*col
,
2723 char left_p
, char right_p
)
2724 /* --------------------------------------------------------------------------
2725 Draw an unfilled rect inside r, optionally leaving left and/or right open.
2726 Note we can't just use an NSDrawRect command, because of the possibility
2727 of some sides not being drawn, and because the rect will be filled.
2728 -------------------------------------------------------------------------- */
2734 s.
size.height
= thickness
;
2736 s.origin.y
+= r.
size.height
- thickness
;
2739 s.
size.height
= r.
size.height
;
2740 s.origin.y
= r.origin.y
;
2742 /* left, right (optional) */
2743 s.
size.width
= thickness
;
2748 s.origin.x
+= r.
size.width
- thickness
;
2755 ns_draw_relief
(NSRect r
, int thickness
, char raised_p
,
2756 char top_p
, char bottom_p
, char left_p
, char right_p
,
2757 struct glyph_string
*s
)
2758 /* --------------------------------------------------------------------------
2759 Draw a relief rect inside r, optionally leaving some sides open.
2760 Note we can't just use an NSDrawBezel command, because of the possibility
2761 of some sides not being drawn, and because the rect will be filled.
2762 -------------------------------------------------------------------------- */
2764 static NSColor
*baseCol
= nil
, *lightCol
= nil
, *darkCol
= nil
;
2765 NSColor
*newBaseCol
= nil
;
2768 NSTRACE
(ns_draw_relief
);
2772 if (s
->face
->use_box_color_for_shadows_p
)
2774 newBaseCol
= ns_lookup_indexed_color
(s
->face
->box_color
, s
->f
);
2776 /* else if (s->first_glyph->type == IMAGE_GLYPH
2778 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2780 newBaseCol = IMAGE_BACKGROUND (s->img, s->f, 0);
2784 newBaseCol
= ns_lookup_indexed_color
(s
->face
->background
, s
->f
);
2787 if (newBaseCol
== nil
)
2788 newBaseCol
= [NSColor grayColor
];
2790 if (newBaseCol
!= baseCol
) /* TODO: better check */
2793 baseCol
= [newBaseCol retain
];
2795 lightCol
= [[baseCol highlightWithLevel
: 0.2] retain
];
2797 darkCol
= [[baseCol shadowWithLevel
: 0.3] retain
];
2800 [(raised_p ? lightCol
: darkCol
) set];
2802 /* TODO: mitering. Using NSBezierPath doesn't work because of color switch. */
2805 sr.
size.height
= thickness
;
2806 if (top_p
) NSRectFill
(sr
);
2809 sr.
size.height
= r.
size.height
;
2810 sr.
size.width
= thickness
;
2811 if (left_p
) NSRectFill
(sr
);
2813 [(raised_p ? darkCol
: lightCol
) set];
2816 sr.
size.width
= r.
size.width
;
2817 sr.
size.height
= thickness
;
2818 sr.origin.y
+= r.
size.height
- thickness
;
2819 if (bottom_p
) NSRectFill
(sr
);
2822 sr.
size.height
= r.
size.height
;
2823 sr.origin.y
= r.origin.y
;
2824 sr.
size.width
= thickness
;
2825 sr.origin.x
+= r.
size.width
- thickness
;
2826 if (right_p
) NSRectFill
(sr
);
2831 ns_dumpglyphs_box_or_relief
(struct glyph_string
*s
)
2832 /* --------------------------------------------------------------------------
2833 Function modeled after x_draw_glyph_string_box ().
2834 Sets up parameters for drawing.
2835 -------------------------------------------------------------------------- */
2837 int right_x
, last_x
;
2838 char left_p
, right_p
;
2839 struct glyph
*last_glyph
;
2844 if (s
->hl
== DRAW_MOUSE_FACE
)
2846 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2848 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2853 thickness
= face
->box_line_width
;
2855 NSTRACE
(ns_dumpglyphs_box_or_relief
);
2857 last_x
= ((s
->row
->full_width_p
&& !s
->w
->pseudo_window_p
)
2858 ? WINDOW_RIGHT_EDGE_X
(s
->w
)
2859 : window_box_right
(s
->w
, s
->area
));
2860 last_glyph
= (s
->cmp || s
->img
2861 ? s
->first_glyph
: s
->first_glyph
+ s
->nchars
-1);
2863 right_x
= ((s
->row
->full_width_p
&& s
->extends_to_end_of_line_p
2864 ? last_x
- 1 : min (last_x
, s
->x
+ s
->background_width
) - 1));
2866 left_p
= (s
->first_glyph
->left_box_line_p
2867 ||
(s
->hl
== DRAW_MOUSE_FACE
2868 && (s
->prev
== NULL || s
->prev
->hl
!= s
->hl
)));
2869 right_p
= (last_glyph
->right_box_line_p
2870 ||
(s
->hl
== DRAW_MOUSE_FACE
2871 && (s
->next
== NULL || s
->next
->hl
!= s
->hl
)));
2873 r
= NSMakeRect
(s
->x
, s
->y
, right_x
- s
->x
+ 1, s
->height
);
2875 /* TODO: Sometimes box_color is 0 and this seems wrong; should investigate. */
2876 if (s
->face
->box
== FACE_SIMPLE_BOX
&& s
->face
->box_color
)
2878 ns_draw_box
(r
, abs (thickness
),
2879 ns_lookup_indexed_color
(face
->box_color
, s
->f
),
2884 ns_draw_relief
(r
, abs (thickness
), s
->face
->box
== FACE_RAISED_BOX
,
2885 1, 1, left_p
, right_p
, s
);
2891 ns_maybe_dumpglyphs_background
(struct glyph_string
*s
, char force_p
)
2892 /* --------------------------------------------------------------------------
2893 Modeled after x_draw_glyph_string_background, which draws BG in
2894 certain cases. Others are left to the text rendering routine.
2895 -------------------------------------------------------------------------- */
2897 NSTRACE
(ns_maybe_dumpglyphs_background
);
2899 if (!s
->background_filled_p
/* || s->hl == DRAW_MOUSE_FACE*/)
2901 int box_line_width
= max (s
->face
->box_line_width
, 0);
2902 if (FONT_HEIGHT
(s
->font
) < s
->height
- 2 * box_line_width
2903 || s
->font_not_found_p || s
->extends_to_end_of_line_p || force_p
)
2906 if (s
->hl
== DRAW_MOUSE_FACE
)
2908 face
= FACE_FROM_ID
(s
->f
,
2909 MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2911 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2914 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2916 [(NS_FACE_BACKGROUND
(face
) != 0
2917 ? ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
)
2918 : FRAME_BACKGROUND_COLOR
(s
->f
)) set];
2921 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(s
->f
);
2922 [[dpyinfo
->bitmaps
[face
->stipple
-1].img stippleMask
] set];
2925 if (s
->hl
!= DRAW_CURSOR
)
2927 NSRect r
= NSMakeRect
(s
->x
, s
->y
+ box_line_width
,
2928 s
->background_width
,
2929 s
->height
-2*box_line_width
);
2933 s
->background_filled_p
= 1;
2940 ns_dumpglyphs_image
(struct glyph_string
*s
, NSRect r
)
2941 /* --------------------------------------------------------------------------
2942 Renders an image and associated borders.
2943 -------------------------------------------------------------------------- */
2945 EmacsImage
*img
= s
->img
->pixmap
;
2946 int box_line_vwidth
= max (s
->face
->box_line_width
, 0);
2947 int x
= s
->x
, y
= s
->ybase
- image_ascent
(s
->img
, s
->face
, &s
->slice);
2948 int bg_x
, bg_y
, bg_height
;
2955 NSTRACE
(ns_dumpglyphs_image
);
2957 if (s
->face
->box
!= FACE_NO_BOX
2958 && s
->first_glyph
->left_box_line_p
&& s
->slice.x
== 0)
2959 x
+= abs (s
->face
->box_line_width
);
2962 bg_y
= s
->slice.y
== 0 ? s
->y
: s
->y
+ box_line_vwidth
;
2963 bg_height
= s
->height
;
2964 /* other terms have this, but was causing problems w/tabbar mode */
2965 /* - 2 * box_line_vwidth; */
2967 if (s
->slice.x
== 0) x
+= s
->img
->hmargin
;
2968 if (s
->slice.y
== 0) y
+= s
->img
->vmargin
;
2970 /* Draw BG: if we need larger area than image itself cleared, do that,
2971 otherwise, since we composite the image under NS (instead of mucking
2972 with its background color), we must clear just the image area. */
2973 if (s
->hl
== DRAW_MOUSE_FACE
)
2975 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
2977 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
2980 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
2982 [ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
) set];
2984 if (bg_height
> s
->slice.height || s
->img
->hmargin || s
->img
->vmargin
2985 || s
->img
->mask || s
->img
->pixmap
== 0 || s
->width
!= s
->background_width
)
2987 br
= NSMakeRect
(bg_x
, bg_y
, s
->background_width
, bg_height
);
2988 s
->background_filled_p
= 1;
2992 br
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
2997 /* Draw the image.. do we need to draw placeholder if img ==nil? */
3000 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
3001 NSRect dr
= NSMakeRect
(x
, y
, s
->slice.width
, s
->slice.height
);
3002 NSRect ir
= NSMakeRect
(s
->slice.x
, s
->slice.y
,
3003 s
->slice.width
, s
->slice.height
);
3006 operation
: NSCompositeSourceOver
3011 [img compositeToPoint
: NSMakePoint
(x
, y
+ s
->slice.height
)
3012 operation
: NSCompositeSourceOver
];
3016 if (s
->hl
== DRAW_CURSOR
)
3018 [FRAME_CURSOR_COLOR
(s
->f
) set];
3019 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3020 tdCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3022 /* Currently on NS img->mask is always 0. Since
3023 get_window_cursor_type specifies a hollow box cursor when on
3024 a non-masked image we never reach this clause. But we put it
3025 in in anticipation of better support for image masks on
3027 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3031 tdCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3034 /* Draw underline, overline, strike-through. */
3035 ns_draw_text_decoration
(s
, face
, tdCol
, br.
size.width
, br.origin.x
);
3037 /* Draw relief, if requested */
3038 if (s
->img
->relief || s
->hl
==DRAW_IMAGE_RAISED || s
->hl
==DRAW_IMAGE_SUNKEN
)
3040 if (s
->hl
== DRAW_IMAGE_SUNKEN || s
->hl
== DRAW_IMAGE_RAISED
)
3042 th
= tool_bar_button_relief
>= 0 ?
3043 tool_bar_button_relief
: DEFAULT_TOOL_BAR_BUTTON_RELIEF
;
3044 raised_p
= (s
->hl
== DRAW_IMAGE_RAISED
);
3048 th
= abs (s
->img
->relief
);
3049 raised_p
= (s
->img
->relief
> 0);
3052 r.origin.x
= x
- th
;
3053 r.origin.y
= y
- th
;
3054 r.
size.width
= s
->slice.width
+ 2*th
-1;
3055 r.
size.height
= s
->slice.height
+ 2*th
-1;
3056 ns_draw_relief
(r
, th
, raised_p
,
3058 s
->slice.y
+ s
->slice.height
== s
->img
->height
,
3060 s
->slice.x
+ s
->slice.width
== s
->img
->width
, s
);
3063 /* If there is no mask, the background won't be seen,
3064 so draw a rectangle on the image for the cursor.
3065 Do this for all images, getting transparency right is not reliable. */
3066 if (s
->hl
== DRAW_CURSOR
)
3068 int thickness
= abs (s
->img
->relief
);
3069 if (thickness
== 0) thickness
= 1;
3070 ns_draw_box
(br
, thickness
, FRAME_CURSOR_COLOR
(s
->f
), 1, 1);
3076 ns_dumpglyphs_stretch
(struct glyph_string
*s
)
3081 NSColor
*fgCol
, *bgCol
;
3083 if (!s
->background_filled_p
)
3085 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3086 *r
= NSMakeRect
(s
->x
, s
->y
, s
->background_width
, s
->height
);
3088 ns_focus
(s
->f
, r
, n
);
3090 if (s
->hl
== DRAW_MOUSE_FACE
)
3092 face
= FACE_FROM_ID
(s
->f
, MOUSE_HL_INFO
(s
->f
)->mouse_face_face_id
);
3094 face
= FACE_FROM_ID
(s
->f
, MOUSE_FACE_ID
);
3097 face
= FACE_FROM_ID
(s
->f
, s
->first_glyph
->face_id
);
3099 bgCol
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
(face
), s
->f
);
3100 fgCol
= ns_lookup_indexed_color
(NS_FACE_FOREGROUND
(face
), s
->f
);
3102 for (i
= 0; i
< n
; ++i
)
3104 if (!s
->row
->full_width_p
)
3106 int overrun
, leftoverrun
;
3108 /* truncate to avoid overwriting fringe and/or scrollbar */
3109 overrun
= max (0, (s
->x
+ s
->background_width
)
3110 - (WINDOW_BOX_RIGHT_EDGE_X
(s
->w
)
3111 - WINDOW_RIGHT_FRINGE_WIDTH
(s
->w
)));
3112 r
[i
].
size.width
-= overrun
;
3114 /* truncate to avoid overwriting to left of the window box */
3115 leftoverrun
= (WINDOW_BOX_LEFT_EDGE_X
(s
->w
)
3116 + WINDOW_LEFT_FRINGE_WIDTH
(s
->w
)) - s
->x
;
3118 if (leftoverrun
> 0)
3120 r
[i
].origin.x
+= leftoverrun
;
3121 r
[i
].
size.width
-= leftoverrun
;
3124 /* XXX: Try to work between problem where a stretch glyph on
3125 a partially-visible bottom row will clear part of the
3126 modeline, and another where list-buffers headers and similar
3127 rows erroneously have visible_height set to 0. Not sure
3128 where this is coming from as other terms seem not to show. */
3129 r
[i
].
size.height
= min (s
->height
, s
->row
->visible_height
);
3134 /* NOTE: under NS this is NOT used to draw cursors, but we must avoid
3135 overwriting cursor (usually when cursor on a tab) */
3136 if (s
->hl
== DRAW_CURSOR
)
3141 width
= s
->w
->phys_cursor_width
;
3142 r
[i
].
size.width
-= width
;
3143 r
[i
].origin.x
+= width
;
3147 /* Draw overlining, etc. on the cursor. */
3148 if (s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3149 ns_draw_text_decoration
(s
, face
, bgCol
, width
, x
);
3151 ns_draw_text_decoration
(s
, face
, fgCol
, width
, x
);
3158 /* Draw overlining, etc. on the stretch glyph (or the part
3159 of the stretch glyph after the cursor). */
3160 ns_draw_text_decoration
(s
, face
, fgCol
, r
[i
].
size.width
,
3164 s
->background_filled_p
= 1;
3170 ns_draw_glyph_string
(struct glyph_string
*s
)
3171 /* --------------------------------------------------------------------------
3172 External (RIF): Main draw-text call.
3173 -------------------------------------------------------------------------- */
3175 /* TODO (optimize): focus for box and contents draw */
3178 char box_drawn_p
= 0;
3180 NSTRACE
(ns_draw_glyph_string
);
3182 if (s
->next
&& s
->right_overhang
&& !s
->for_overlaps
/*&&s->hl!=DRAW_CURSOR*/)
3185 struct glyph_string
*next
;
3187 for (width
= 0, next
= s
->next
;
3188 next
&& width
< s
->right_overhang
;
3189 width
+= next
->width
, next
= next
->next
)
3190 if (next
->first_glyph
->type != IMAGE_GLYPH
)
3192 if (next
->first_glyph
->type != STRETCH_GLYPH
)
3194 n
= ns_get_glyph_string_clip_rect
(s
->next
, r
);
3195 ns_focus
(s
->f
, r
, n
);
3196 ns_maybe_dumpglyphs_background
(s
->next
, 1);
3201 ns_dumpglyphs_stretch
(s
->next
);
3203 next
->num_clips
= 0;
3207 if (!s
->for_overlaps
&& s
->face
->box
!= FACE_NO_BOX
3208 && (s
->first_glyph
->type == CHAR_GLYPH
3209 || s
->first_glyph
->type == COMPOSITE_GLYPH
))
3211 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3212 ns_focus
(s
->f
, r
, n
);
3213 ns_maybe_dumpglyphs_background
(s
, 1);
3214 ns_dumpglyphs_box_or_relief
(s
);
3219 switch (s
->first_glyph
->type)
3223 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3224 ns_focus
(s
->f
, r
, n
);
3225 ns_dumpglyphs_image
(s
, r
[0]);
3230 ns_dumpglyphs_stretch
(s
);
3234 case COMPOSITE_GLYPH
:
3235 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3236 ns_focus
(s
->f
, r
, n
);
3238 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3239 && ! s
->first_glyph
->u.cmp.automatic
))
3240 s
->background_filled_p
= 1;
3242 ns_maybe_dumpglyphs_background
3243 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3245 ns_tmp_flags
= s
->hl
== DRAW_CURSOR ? NS_DUMPGLYPH_CURSOR
:
3246 (s
->hl
== DRAW_MOUSE_FACE ? NS_DUMPGLYPH_MOUSEFACE
:
3247 (s
->for_overlaps ? NS_DUMPGLYPH_FOREGROUND
:
3248 NS_DUMPGLYPH_NORMAL
));
3249 ns_tmp_font
= (struct nsfont_info
*)s
->face
->font
;
3250 if (ns_tmp_font
== NULL)
3251 ns_tmp_font
= (struct nsfont_info
*)FRAME_FONT
(s
->f
);
3253 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3255 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3256 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3257 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3260 ns_tmp_font
->font.driver
->draw
3261 (s
, 0, s
->nchars
, s
->x
, s
->y
,
3262 (ns_tmp_flags
== NS_DUMPGLYPH_NORMAL
&& !s
->background_filled_p
)
3263 || ns_tmp_flags
== NS_DUMPGLYPH_MOUSEFACE
);
3265 if (s
->hl
== DRAW_CURSOR
&& s
->w
->phys_cursor_type
== FILLED_BOX_CURSOR
)
3267 unsigned
long tmp
= NS_FACE_BACKGROUND
(s
->face
);
3268 NS_FACE_BACKGROUND
(s
->face
) = NS_FACE_FOREGROUND
(s
->face
);
3269 NS_FACE_FOREGROUND
(s
->face
) = tmp
;
3275 case GLYPHLESS_GLYPH
:
3276 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3277 ns_focus
(s
->f
, r
, n
);
3279 if (s
->for_overlaps ||
(s
->cmp_from
> 0
3280 && ! s
->first_glyph
->u.cmp.automatic
))
3281 s
->background_filled_p
= 1;
3283 ns_maybe_dumpglyphs_background
3284 (s
, s
->first_glyph
->type == COMPOSITE_GLYPH
);
3286 /* Not yet implemented. */
3295 /* Draw box if not done already. */
3296 if (!s
->for_overlaps
&& !box_drawn_p
&& s
->face
->box
!= FACE_NO_BOX
)
3298 n
= ns_get_glyph_string_clip_rect
(s
, r
);
3299 ns_focus
(s
->f
, r
, n
);
3300 ns_dumpglyphs_box_or_relief
(s
);
3309 /* ==========================================================================
3313 ========================================================================== */
3317 ns_send_appdefined
(int value
)
3318 /* --------------------------------------------------------------------------
3319 Internal: post an appdefined event which EmacsApp-sendEvent will
3320 recognize and take as a command to halt the event loop.
3321 -------------------------------------------------------------------------- */
3323 /*NSTRACE (ns_send_appdefined); */
3325 #ifdef NS_IMPL_GNUSTEP
3326 // GNUStep needs postEvent to happen on the main thread.
3327 if (! [[NSThread currentThread
] isMainThread
])
3329 EmacsApp
*app
= (EmacsApp
*)NSApp
;
3330 app
->nextappdefined
= value
;
3331 [app performSelectorOnMainThread
:@selector
(sendFromMainThread
:)
3338 /* Only post this event if we haven't already posted one. This will end
3339 the [NXApp run] main loop after having processed all events queued at
3341 if (send_appdefined
)
3345 /* We only need one NX_APPDEFINED event to stop NXApp from running. */
3346 send_appdefined
= NO
;
3348 /* Don't need wakeup timer any more */
3351 [timed_entry invalidate
];
3352 [timed_entry release
];
3356 nxev
= [NSEvent otherEventWithType
: NSApplicationDefined
3357 location
: NSMakePoint
(0, 0)
3360 windowNumber
: [[NSApp mainWindow
] windowNumber
]
3361 context
: [NSApp context
]
3366 /* Post an application defined event on the event queue. When this is
3367 received the [NXApp run] will return, thus having processed all
3368 events which are currently queued. */
3369 [NSApp postEvent
: nxev atStart
: NO
];
3373 #ifdef HAVE_NATIVE_FS
3377 Lisp_Object frame
, tail
;
3379 if (ns_last_use_native_fullscreen
== ns_use_native_fullscreen
)
3382 ns_last_use_native_fullscreen
= ns_use_native_fullscreen
;
3384 /* Clear the mouse-moved flag for every frame on this display. */
3385 FOR_EACH_FRAME
(tail
, frame
)
3387 struct frame
*f
= XFRAME
(frame
);
3390 EmacsView
*view = FRAME_NS_VIEW
(f
);
3391 [view updateCollectionBehaviour
];
3397 /* GNUStep and OSX <= 10.4 does not have cancelTracking. */
3398 #
if defined
(NS_IMPL_COCOA
) && \
3399 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_5
3401 ns_get_pending_menu_title
()
3403 return menu_pending_title
;
3406 /* Check if menu open should be cancelled or continued as normal. */
3408 ns_check_menu_open
(NSMenu
*menu)
3410 /* Click in menu bar? */
3411 NSArray
*a
= [[NSApp mainMenu
] itemArray
];
3414 for (i
= 0; ! found
&& i
< [a count
]; i
++)
3415 found
= menu == [[a objectAtIndex
:i
] submenu
];
3418 if (menu_will_open_state
== MENU_NONE
&& emacs_event
)
3420 NSEvent
*theEvent
= [NSApp currentEvent
];
3421 struct frame
*emacsframe
= SELECTED_FRAME
();
3423 [menu cancelTracking
];
3424 menu_will_open_state
= MENU_PENDING
;
3425 emacs_event
->kind
= MENU_BAR_ACTIVATE_EVENT
;
3426 EV_TRAILER
(theEvent
);
3428 CGEventRef ourEvent
= CGEventCreate
(NULL);
3429 menu_mouse_point
= CGEventGetLocation
(ourEvent
);
3430 CFRelease
(ourEvent
);
3431 xfree
(menu_pending_title
);
3432 menu_pending_title
= xstrdup
([[menu title] UTF8String
]);
3434 else if (menu_will_open_state
== MENU_OPENING
)
3436 menu_will_open_state
= MENU_NONE
;
3441 /* Redo saved menu click if state is MENU_PENDING. */
3443 ns_check_pending_open_menu
()
3445 if (menu_will_open_state
== MENU_PENDING
)
3447 CGEventSourceRef source
3448 = CGEventSourceCreate
(kCGEventSourceStateHIDSystemState
);
3450 CGEventRef event
= CGEventCreateMouseEvent
(source
,
3451 kCGEventLeftMouseDown
,
3453 kCGMouseButtonLeft
);
3454 CGEventSetType
(event
, kCGEventLeftMouseDown
);
3455 CGEventPost
(kCGHIDEventTap
, event
);
3459 menu_will_open_state
= MENU_OPENING
;
3462 #endif
/* NS_IMPL_COCOA) && >= MAC_OS_X_VERSION_10_5 */
3465 ns_read_socket
(struct terminal *terminal, struct input_event
*hold_quit
)
3466 /* --------------------------------------------------------------------------
3467 External (hook): Post an event to ourself and keep reading events until
3468 we read it back again. In effect process all events which were waiting.
3469 From 21+ we have to manage the event buffer ourselves.
3470 -------------------------------------------------------------------------- */
3472 struct input_event ev
;
3475 /* NSTRACE (ns_read_socket); */
3477 #ifdef HAVE_NATIVE_FS
3481 if ([NSApp modalWindow
] != nil
)
3484 if (hold_event_q.nr
> 0)
3487 for (i
= 0; i
< hold_event_q.nr
; ++i
)
3488 kbd_buffer_store_event_hold
(&hold_event_q.q
[i
], hold_quit
);
3489 hold_event_q.nr
= 0;
3494 n_emacs_events_pending
= 0;
3497 q_event_ptr
= hold_quit
;
3499 /* we manage autorelease pools by allocate/reallocate each time around
3500 the loop; strict nesting is occasionally violated but seems not to
3501 matter.. earlier methods using full nesting caused major memory leaks */
3502 [outerpool release
];
3503 outerpool
= [[NSAutoreleasePool alloc
] init
];
3505 /* If have pending open-file requests, attend to the next one of those. */
3506 if (ns_pending_files
&& [ns_pending_files count
] != 0
3507 && [(EmacsApp
*)NSApp openFile
: [ns_pending_files objectAtIndex
: 0]])
3509 [ns_pending_files removeObjectAtIndex
: 0];
3511 /* Deal with pending service requests. */
3512 else if (ns_pending_service_names
&& [ns_pending_service_names count
] != 0
3514 NSApp fulfillService
: [ns_pending_service_names objectAtIndex
: 0]
3515 withArg
: [ns_pending_service_args objectAtIndex
: 0]])
3517 [ns_pending_service_names removeObjectAtIndex
: 0];
3518 [ns_pending_service_args removeObjectAtIndex
: 0];
3522 /* Run and wait for events. We must always send one NX_APPDEFINED event
3523 to ourself, otherwise [NXApp run] will never exit. */
3524 send_appdefined
= YES
;
3525 ns_send_appdefined
(-1);
3527 if (++apploopnr
!= 1)
3535 nevents
= n_emacs_events_pending
;
3536 n_emacs_events_pending
= 0;
3537 emacs_event
= q_event_ptr
= NULL;
3545 ns_select
(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
3546 fd_set
*exceptfds
, EMACS_TIME
*timeout
, sigset_t
*sigmask
)
3547 /* --------------------------------------------------------------------------
3548 Replacement for select, checking for events
3549 -------------------------------------------------------------------------- */
3553 struct input_event event
;
3556 /* NSTRACE (ns_select); */
3558 #ifdef HAVE_NATIVE_FS
3562 if (hold_event_q.nr
> 0)
3564 /* We already have events pending. */
3570 for (k
= 0; k
< nfds
+1; k
++)
3572 if (readfds
&& FD_ISSET
(k
, readfds
)) ++nr
;
3573 if (writefds
&& FD_ISSET
(k
, writefds
)) ++nr
;
3577 ||
(timeout
&& timeout
->tv_sec
== 0 && timeout
->tv_nsec
== 0))
3578 return pselect
(nfds
, readfds
, writefds
, exceptfds
, timeout
, sigmask
);
3580 [outerpool release
];
3581 outerpool
= [[NSAutoreleasePool alloc
] init
];
3584 send_appdefined
= YES
;
3587 pthread_mutex_lock
(&select_mutex
);
3592 select_readfds
= *readfds
;
3593 select_valid
+= SELECT_HAVE_READ
;
3597 select_writefds
= *writefds
;
3598 select_valid
+= SELECT_HAVE_WRITE
;
3603 select_timeout
= *timeout
;
3604 select_valid
+= SELECT_HAVE_TMO
;
3607 pthread_mutex_unlock
(&select_mutex
);
3609 /* Inform fd_handler that select should be called */
3611 emacs_write_sig
(selfds
[1], &c
, 1);
3613 else if (nr
== 0 && timeout
)
3615 /* No file descriptor, just a timeout, no need to wake fd_handler */
3616 double time
= EMACS_TIME_TO_DOUBLE
(*timeout
);
3617 timed_entry
= [[NSTimer scheduledTimerWithTimeInterval
: time
3620 @selector
(timeout_handler
:)
3625 else /* No timeout and no file descriptors, can this happen? */
3627 /* Send appdefined so we exit from the loop */
3628 ns_send_appdefined
(-1);
3633 emacs_event
= &event
;
3634 if (++apploopnr
!= 1)
3641 if (nr
> 0 && readfds
)
3644 emacs_write_sig
(selfds
[1], &c
, 1);
3648 t
= last_appdefined_event_data
;
3650 if (t
!= NO_APPDEFINED_DATA
)
3652 last_appdefined_event_data
= NO_APPDEFINED_DATA
;
3656 /* The NX_APPDEFINED event we received was a timeout. */
3661 /* The NX_APPDEFINED event we received was the result of
3662 at least one real input event arriving. */
3668 /* Received back from select () in fd_handler; copy the results */
3669 pthread_mutex_lock
(&select_mutex
);
3670 if (readfds
) *readfds
= select_readfds
;
3671 if (writefds
) *writefds
= select_writefds
;
3672 if (timeout
) *timeout
= select_timeout
;
3673 pthread_mutex_unlock
(&select_mutex
);
3688 /* ==========================================================================
3692 ========================================================================== */
3696 ns_set_vertical_scroll_bar
(struct window
*window
,
3697 int portion
, int whole
, int position
)
3698 /* --------------------------------------------------------------------------
3699 External (hook): Update or add scrollbar
3700 -------------------------------------------------------------------------- */
3704 struct frame
*f
= XFRAME
(WINDOW_FRAME
(window
));
3705 EmacsView
*view = FRAME_NS_VIEW
(f
);
3706 int window_y
, window_height
;
3707 int top
, left
, height
, width
, sb_width
, sb_left
;
3709 BOOL fringe_extended_p
;
3711 /* optimization; display engine sends WAY too many of these.. */
3712 if (!NILP
(window
->vertical_scroll_bar
))
3714 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3715 if ([bar checkSamePosition
: position portion
: portion whole
: whole
])
3717 if (view->scrollbarsNeedingUpdate
== 0)
3719 if (!windows_or_buffers_changed
)
3723 view->scrollbarsNeedingUpdate
--;
3727 NSTRACE
(ns_set_vertical_scroll_bar
);
3729 /* Get dimensions. */
3730 window_box
(window
, -1, 0, &window_y
, 0, &window_height
);
3732 height
= window_height
;
3733 width
= WINDOW_CONFIG_SCROLL_BAR_COLS
(window
) * FRAME_COLUMN_WIDTH
(f
);
3734 left
= WINDOW_SCROLL_BAR_AREA_X
(window
);
3736 /* allow for displaying a skinnier scrollbar than char area allotted */
3737 sb_width
= (WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) > 0) ?
3738 WINDOW_CONFIG_SCROLL_BAR_WIDTH
(window
) : width
;
3741 r
= NSMakeRect
(sb_left
, top
, sb_width
, height
);
3742 /* the parent view is flipped, so we need to flip y value */
3744 r.origin.y
= (v.
size.height
- r.
size.height
- r.origin.y
);
3746 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT
(window
))
3747 fringe_extended_p
= (WINDOW_LEFTMOST_P
(window
)
3748 && WINDOW_LEFT_FRINGE_WIDTH
(window
)
3749 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS
(window
)
3750 || WINDOW_LEFT_MARGIN_COLS
(window
) == 0));
3752 fringe_extended_p
= (WINDOW_RIGHTMOST_P
(window
)
3753 && WINDOW_RIGHT_FRINGE_WIDTH
(window
)
3754 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS
(window
)
3755 || WINDOW_RIGHT_MARGIN_COLS
(window
) == 0));
3757 XSETWINDOW
(win
, window
);
3760 /* we want at least 5 lines to display a scrollbar */
3761 if (WINDOW_TOTAL_LINES
(window
) < 5)
3763 if (!NILP
(window
->vertical_scroll_bar
))
3765 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3766 [bar removeFromSuperview
];
3767 wset_vertical_scroll_bar
(window
, Qnil
);
3769 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3774 if (NILP
(window
->vertical_scroll_bar
))
3776 if (width
> 0 && height
> 0)
3778 if (fringe_extended_p
)
3779 ns_clear_frame_area
(f
, sb_left
, top
, sb_width
, height
);
3781 ns_clear_frame_area
(f
, left
, top
, width
, height
);
3784 bar = [[EmacsScroller alloc
] initFrame
: r window
: win
];
3785 wset_vertical_scroll_bar
(window
, make_save_ptr
(bar));
3790 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3791 oldRect
= [bar frame
];
3792 r.
size.width
= oldRect.
size.width
;
3793 if (FRAME_LIVE_P
(f
) && !NSEqualRects
(oldRect
, r
))
3795 if (oldRect.origin.x
!= r.origin.x
)
3796 ns_clear_frame_area
(f
, sb_left
, top
, width
, height
);
3801 [bar setPosition
: position portion
: portion whole
: whole
];
3807 ns_condemn_scroll_bars
(struct frame
*f
)
3808 /* --------------------------------------------------------------------------
3809 External (hook): arrange for all frame's scrollbars to be removed
3810 at next call to judge_scroll_bars, except for those redeemed.
3811 -------------------------------------------------------------------------- */
3815 NSArray
*subviews
= [[FRAME_NS_VIEW
(f
) superview
] subviews
];
3817 NSTRACE
(ns_condemn_scroll_bars
);
3819 for (i
=[subviews count
]-1; i
>= 0; i
--)
3821 view = [subviews objectAtIndex
: i
];
3822 if ([view isKindOfClass
: [EmacsScroller
class]])
3829 ns_redeem_scroll_bar
(struct window
*window
)
3830 /* --------------------------------------------------------------------------
3831 External (hook): arrange to spare this window's scrollbar
3832 at next call to judge_scroll_bars.
3833 -------------------------------------------------------------------------- */
3836 NSTRACE
(ns_redeem_scroll_bar
);
3837 if (!NILP
(window
->vertical_scroll_bar
))
3839 bar = XNS_SCROLL_BAR
(window
->vertical_scroll_bar
);
3846 ns_judge_scroll_bars
(struct frame
*f
)
3847 /* --------------------------------------------------------------------------
3848 External (hook): destroy all scrollbars on frame that weren't
3849 redeemed after call to condemn_scroll_bars.
3850 -------------------------------------------------------------------------- */
3854 EmacsView
*eview
= FRAME_NS_VIEW
(f
);
3855 NSArray
*subviews
= [[eview superview
] subviews
];
3858 NSTRACE
(ns_judge_scroll_bars
);
3859 for (i
= [subviews count
]-1; i
>= 0; --i
)
3861 view = [subviews objectAtIndex
: i
];
3862 if (![view isKindOfClass
: [EmacsScroller
class]]) continue
;
3868 [eview updateFrameSize
: NO
];
3871 /* ==========================================================================
3875 ========================================================================== */
3878 x_display_pixel_height
(struct ns_display_info
*dpyinfo
)
3880 NSArray
*screens
= [NSScreen screens
];
3881 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3886 while ((screen
= [enumerator nextObject
]) != nil
)
3887 frame
= NSUnionRect
(frame
, [screen frame
]);
3889 return NSHeight
(frame
);
3893 x_display_pixel_width
(struct ns_display_info
*dpyinfo
)
3895 NSArray
*screens
= [NSScreen screens
];
3896 NSEnumerator
*enumerator
= [screens objectEnumerator
];
3901 while ((screen
= [enumerator nextObject
]) != nil
)
3902 frame
= NSUnionRect
(frame
, [screen frame
]);
3904 return NSWidth
(frame
);
3908 static Lisp_Object ns_string_to_lispmod
(const
char *s
)
3909 /* --------------------------------------------------------------------------
3910 Convert modifier name to lisp symbol
3911 -------------------------------------------------------------------------- */
3913 if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qmeta
)), s
, 10))
3915 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qsuper
)), s
, 10))
3917 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qcontrol
)), s
, 10))
3919 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qalt
)), s
, 10))
3921 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qhyper
)), s
, 10))
3923 else if (!strncmp
(SSDATA
(SYMBOL_NAME
(Qnone
)), s
, 10))
3931 ns_default
(const
char *parameter
, Lisp_Object
*result
,
3932 Lisp_Object yesval
, Lisp_Object noval
,
3933 BOOL is_float
, BOOL is_modstring
)
3934 /* --------------------------------------------------------------------------
3935 Check a parameter value in user's preferences
3936 -------------------------------------------------------------------------- */
3938 const
char *value
= ns_get_defaults_value
(parameter
);
3944 if (c_strcasecmp
(value
, "YES"
) == 0)
3946 else if (c_strcasecmp
(value
, "NO"
) == 0)
3948 else if (is_float
&& (f
= strtod
(value
, &pos
), pos
!= value
))
3949 *result
= make_float
(f
);
3950 else if (is_modstring
&& value
)
3951 *result
= ns_string_to_lispmod
(value
);
3952 else fprintf (stderr
,
3953 "Bad value
for default
\"%s\": \"%s\"\n", parameter, value);
3959 ns_initialize_display_info
(struct ns_display_info
*dpyinfo
)
3960 /* --------------------------------------------------------------------------
3961 Initialize global info and storage for display.
3962 -------------------------------------------------------------------------- */
3964 NSScreen
*screen
= [NSScreen mainScreen
];
3965 NSWindowDepth depth
= [screen depth
];
3966 Mouse_HLInfo
*hlinfo
= &dpyinfo
->mouse_highlight
;
3968 dpyinfo
->resx
= 72.27; /* used 75.0, but this makes pt == pixel, expected */
3969 dpyinfo
->resy
= 72.27;
3970 dpyinfo
->color_p
= ![NSDeviceWhiteColorSpace isEqualToString
:
3971 NSColorSpaceFromDepth
(depth
)]
3972 && ![NSCalibratedWhiteColorSpace isEqualToString
:
3973 NSColorSpaceFromDepth
(depth
)];
3974 dpyinfo
->n_planes
= NSBitsPerPixelFromDepth
(depth
);
3975 dpyinfo
->image_cache
= make_image_cache
();
3976 dpyinfo
->color_table
= xmalloc
(sizeof
*dpyinfo
->color_table
);
3977 dpyinfo
->color_table
->colors
= NULL;
3978 dpyinfo
->root_window
= 42; /* a placeholder.. */
3980 hlinfo
->mouse_face_mouse_frame
= NULL;
3981 hlinfo
->mouse_face_beg_row
= hlinfo
->mouse_face_beg_col
= -1;
3982 hlinfo
->mouse_face_end_row
= hlinfo
->mouse_face_end_col
= -1;
3983 hlinfo
->mouse_face_face_id
= DEFAULT_FACE_ID
;
3984 hlinfo
->mouse_face_window
= hlinfo
->mouse_face_overlay
= Qnil
;
3985 hlinfo
->mouse_face_hidden
= 0;
3987 hlinfo
->mouse_face_mouse_x
= hlinfo
->mouse_face_mouse_y
= 0;
3988 hlinfo
->mouse_face_defer
= 0;
3990 dpyinfo
->x_highlight_frame
= dpyinfo
->x_focus_frame
= NULL;
3992 dpyinfo
->n_fonts
= 0;
3993 dpyinfo
->smallest_font_height
= 1;
3994 dpyinfo
->smallest_char_width
= 1;
3998 /* This and next define (many of the) public functions in this file. */
3999 /* x_... are generic versions in xdisp.c that we, and other terms, get away
4000 with using despite presence in the "system dependent" redisplay
4001 interface. In addition, many of the ns_ methods have code that is
4002 shared with all terms, indicating need for further refactoring. */
4003 extern frame_parm_handler ns_frame_parm_handlers
[];
4004 static
struct redisplay_interface ns_redisplay_interface
=
4006 ns_frame_parm_handlers
,
4010 x_clear_end_of_line
,
4012 ns_after_update_window_line
,
4013 ns_update_window_begin
,
4014 ns_update_window_end
,
4017 0, /* flush_display_optional */
4018 x_clear_window_mouse_face
,
4019 x_get_glyph_overhangs
,
4020 x_fix_overlapping_area
,
4021 ns_draw_fringe_bitmap
,
4022 0, /* define_fringe_bitmap */ /* FIXME: simplify ns_draw_fringe_bitmap */
4023 0, /* destroy_fringe_bitmap */
4024 ns_compute_glyph_string_overhangs
,
4025 ns_draw_glyph_string
, /* interface to nsfont.m */
4026 ns_define_frame_cursor
,
4027 ns_clear_frame_area
,
4028 ns_draw_window_cursor
,
4029 ns_draw_vertical_window_border
,
4030 ns_shift_glyphs_for_insert
4035 ns_delete_display
(struct ns_display_info
*dpyinfo
)
4041 /* This function is called when the last frame on a display is deleted. */
4043 ns_delete_terminal
(struct terminal *terminal)
4045 struct ns_display_info
*dpyinfo
= terminal->display_info.ns
;
4047 /* Protect against recursive calls. delete_frame in
4048 delete_terminal calls us back when it deletes our last frame. */
4049 if (!terminal->name
)
4054 x_destroy_all_bitmaps
(dpyinfo
);
4055 ns_delete_display
(dpyinfo
);
4060 static
struct terminal *
4061 ns_create_terminal
(struct ns_display_info
*dpyinfo
)
4062 /* --------------------------------------------------------------------------
4063 Set up use of NS before we make the first connection.
4064 -------------------------------------------------------------------------- */
4066 struct terminal *terminal;
4068 NSTRACE
(ns_create_terminal
);
4070 terminal = create_terminal
();
4072 terminal->type = output_ns
;
4073 terminal->display_info.ns
= dpyinfo
;
4074 dpyinfo
->terminal = terminal;
4076 terminal->rif
= &ns_redisplay_interface
;
4078 terminal->clear_frame_hook
= ns_clear_frame
;
4079 terminal->ins_del_lines_hook
= 0; /* XXX vestigial? */
4080 terminal->delete_glyphs_hook
= 0; /* XXX vestigial? */
4081 terminal->ring_bell_hook
= ns_ring_bell
;
4082 terminal->reset_terminal_modes_hook
= ns_reset_terminal_modes
;
4083 terminal->set_terminal_modes_hook
= ns_set_terminal_modes
;
4084 terminal->update_begin_hook
= ns_update_begin
;
4085 terminal->update_end_hook
= ns_update_end
;
4086 terminal->set_terminal_window_hook
= NULL; /* XXX vestigial? */
4087 terminal->read_socket_hook
= ns_read_socket
;
4088 terminal->frame_up_to_date_hook
= ns_frame_up_to_date
;
4089 terminal->mouse_position_hook
= ns_mouse_position
;
4090 terminal->frame_rehighlight_hook
= ns_frame_rehighlight
;
4091 terminal->frame_raise_lower_hook
= ns_frame_raise_lower
;
4093 terminal->fullscreen_hook
= ns_fullscreen_hook
;
4095 terminal->set_vertical_scroll_bar_hook
= ns_set_vertical_scroll_bar
;
4096 terminal->condemn_scroll_bars_hook
= ns_condemn_scroll_bars
;
4097 terminal->redeem_scroll_bar_hook
= ns_redeem_scroll_bar
;
4098 terminal->judge_scroll_bars_hook
= ns_judge_scroll_bars
;
4100 terminal->delete_frame_hook
= x_destroy_window
;
4101 terminal->delete_terminal_hook
= ns_delete_terminal
;
4103 terminal->scroll_region_ok
= 1;
4104 terminal->char_ins_del_ok
= 1;
4105 terminal->line_ins_del_ok
= 1;
4106 terminal->fast_clear_end_of_line
= 1;
4107 terminal->memory_below_frame
= 0;
4113 struct ns_display_info
*
4114 ns_term_init
(Lisp_Object display_name
)
4115 /* --------------------------------------------------------------------------
4116 Start the Application and get things rolling.
4117 -------------------------------------------------------------------------- */
4119 struct terminal *terminal;
4120 struct ns_display_info
*dpyinfo
;
4121 static int ns_initialized
= 0;
4124 if (ns_initialized
) return x_display_list
;
4127 NSTRACE
(ns_term_init
);
4129 [outerpool release
];
4130 outerpool
= [[NSAutoreleasePool alloc
] init
];
4132 /* count object allocs (About, click icon); on OS X use ObjectAlloc tool */
4133 /*GSDebugAllocationActive (YES); */
4137 Fset_input_interrupt_mode
(Qnil
);
4139 if (selfds
[0] == -1)
4141 if (emacs_pipe
(selfds
) != 0)
4143 fprintf (stderr
, "Failed to create pipe
: %s\n",
4144 emacs_strerror
(errno
));
4148 fcntl
(selfds
[0], F_SETFL
, O_NONBLOCK|fcntl
(selfds
[0], F_GETFL
));
4149 FD_ZERO
(&select_readfds
);
4150 FD_ZERO
(&select_writefds
);
4151 pthread_mutex_init
(&select_mutex
, NULL);
4154 ns_pending_files
= [[NSMutableArray alloc
] init
];
4155 ns_pending_service_names
= [[NSMutableArray alloc
] init
];
4156 ns_pending_service_args
= [[NSMutableArray alloc
] init
];
4158 /* Start app and create the main menu, window, view.
4159 Needs to be here because ns_initialize_display_info () uses AppKit classes.
4160 The view will then ask the NSApp to stop and return to Emacs. */
4161 [EmacsApp sharedApplication
];
4164 [NSApp setDelegate
: NSApp
];
4166 /* Start the select thread. */
4167 [NSThread detachNewThreadSelector
:@selector
(fd_handler
:)
4171 /* debugging: log all notifications */
4172 /* [[NSNotificationCenter defaultCenter] addObserver: NSApp
4173 selector: @selector (logNotification:)
4174 name: nil object: nil]; */
4176 dpyinfo
= xzalloc
(sizeof
*dpyinfo
);
4178 ns_initialize_display_info
(dpyinfo
);
4179 terminal = ns_create_terminal
(dpyinfo
);
4181 terminal->kboard
= xmalloc
(sizeof
*terminal->kboard
);
4182 init_kboard
(terminal->kboard
);
4183 kset_window_system
(terminal->kboard
, Qns
);
4184 terminal->kboard
->next_kboard
= all_kboards
;
4185 all_kboards
= terminal->kboard
;
4186 /* Don't let the initial kboard remain current longer than necessary.
4187 That would cause problems if a file loaded on startup tries to
4188 prompt in the mini-buffer. */
4189 if (current_kboard
== initial_kboard
)
4190 current_kboard
= terminal->kboard
;
4191 terminal->kboard
->reference_count
++;
4193 dpyinfo
->next
= x_display_list
;
4194 x_display_list
= dpyinfo
;
4196 /* Put it on ns_display_name_list */
4197 ns_display_name_list
= Fcons
(Fcons
(display_name
, Qnil
),
4198 ns_display_name_list
);
4199 dpyinfo
->name_list_element
= XCAR
(ns_display_name_list
);
4201 terminal->name
= xstrdup
(SSDATA
(display_name
));
4205 if (!inhibit_x_resources
)
4207 ns_default
("GSFontAntiAlias"
, &ns_antialias_text
,
4210 /* this is a standard variable */
4211 ns_default
("AppleAntiAliasingThreshold"
, &tmp
,
4212 make_float
(10.0), make_float
(6.0), YES
, NO
);
4213 ns_antialias_threshold
= NILP
(tmp
) ?
10.0 : XFLOATINT
(tmp
);
4216 ns_selection_color
= [[NSUserDefaults standardUserDefaults
]
4217 stringForKey
: @"AppleHighlightColor"
];
4218 if (ns_selection_color
== nil
)
4219 ns_selection_color
= NS_SELECTION_COLOR_DEFAULT
;
4222 NSColorList
*cl
= [NSColorList colorListNamed
: @"Emacs"
];
4226 Lisp_Object color_file
, color_map
, color
;
4230 color_file
= Fexpand_file_name
(build_string
("rgb.txt"
),
4231 Fsymbol_value
(intern
("data
-directory"
)));
4233 color_map
= Fx_load_color_file
(color_file
);
4234 if (NILP
(color_map
))
4235 fatal
("Could not read
%s.\n", SDATA (color_file));
4237 cl
= [[NSColorList alloc
] initWithName
: @"Emacs"
];
4238 for ( ; CONSP
(color_map
); color_map
= XCDR
(color_map
))
4240 color
= XCAR
(color_map
);
4241 name
= SSDATA
(XCAR
(color
));
4242 c
= XINT
(XCDR
(color
));
4244 [NSColor colorWithCalibratedRed
: RED_FROM_ULONG
(c
) / 255.0
4245 green
: GREEN_FROM_ULONG
(c
) / 255.0
4246 blue
: BLUE_FROM_ULONG
(c
) / 255.0
4248 forKey
: [NSString stringWithUTF8String
: name
]];
4250 [cl writeToFile
: nil
];
4255 #ifdef NS_IMPL_GNUSTEP
4256 Vwindow_system_version
= build_string
(gnustep_base_version
);
4258 /*PSnextrelease (128, c); */
4259 char c
[DBL_BUFSIZE_BOUND
];
4260 int len
= dtoastr
(c
, sizeof c
, 0, 0, NSAppKitVersionNumber
);
4261 Vwindow_system_version
= make_unibyte_string
(c
, len
);
4265 delete_keyboard_wait_descriptor
(0);
4267 ns_app_name
= [[NSProcessInfo processInfo
] processName
];
4269 /* Set up OS X app menu */
4270 #ifdef NS_IMPL_COCOA
4274 /* set up the application menu */
4275 svcsMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Services"
];
4276 [svcsMenu setAutoenablesItems
: NO
];
4277 appMenu
= [[EmacsMenu alloc
] initWithTitle
: @"Emacs"
];
4278 [appMenu setAutoenablesItems
: NO
];
4279 mainMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4280 dockMenu
= [[EmacsMenu alloc
] initWithTitle
: @""
];
4282 [appMenu insertItemWithTitle
: @"About Emacs"
4283 action
: @selector
(orderFrontStandardAboutPanel
:)
4286 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 1];
4287 [appMenu insertItemWithTitle
: @"Preferences..."
4288 action
: @selector
(showPreferencesWindow
:)
4291 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 3];
4292 item
= [appMenu insertItemWithTitle
: @"Services"
4293 action
: @selector
(menuDown
:)
4296 [appMenu setSubmenu
: svcsMenu forItem
: item
];
4297 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 5];
4298 [appMenu insertItemWithTitle
: @"Hide Emacs"
4299 action
: @selector
(hide
:)
4302 item
= [appMenu insertItemWithTitle
: @"Hide Others"
4303 action
: @selector
(hideOtherApplications
:)
4306 [item setKeyEquivalentModifierMask
: NSCommandKeyMask | NSAlternateKeyMask
];
4307 [appMenu insertItem
: [NSMenuItem separatorItem
] atIndex
: 8];
4308 [appMenu insertItemWithTitle
: @"
Quit Emacs"
4309 action
: @selector
(terminate
:)
4313 item
= [mainMenu insertItemWithTitle
: ns_app_name
4314 action
: @selector
(menuDown
:)
4317 [mainMenu setSubmenu
: appMenu forItem
: item
];
4318 [dockMenu insertItemWithTitle
: @"New Frame"
4319 action
: @selector
(newFrame
:)
4323 [NSApp setMainMenu
: mainMenu
];
4324 [NSApp setAppleMenu
: appMenu
];
4325 [NSApp setServicesMenu
: svcsMenu
];
4326 /* Needed at least on Cocoa, to get dock menu to show windows */
4327 [NSApp setWindowsMenu
: [[NSMenu alloc
] init
]];
4329 [[NSNotificationCenter defaultCenter
]
4330 addObserver
: mainMenu
4331 selector
: @selector
(trackingNotification
:)
4332 name
: NSMenuDidBeginTrackingNotification object
: mainMenu
];
4333 [[NSNotificationCenter defaultCenter
]
4334 addObserver
: mainMenu
4335 selector
: @selector
(trackingNotification
:)
4336 name
: NSMenuDidEndTrackingNotification object
: mainMenu
];
4338 #endif
/* MAC OS X menu setup */
4340 /* Register our external input/output types, used for determining
4341 applicable services and also drag/drop eligibility. */
4342 ns_send_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
] retain
];
4343 ns_return_types
= [[NSArray arrayWithObjects
: NSStringPboardType
, nil
]
4345 ns_drag_types
= [[NSArray arrayWithObjects
:
4347 NSTabularTextPboardType
,
4348 NSFilenamesPboardType
,
4351 NSFontPboardType
, nil
] retain
];
4353 /* If fullscreen is in init/default-frame-alist, focus isn't set
4354 right for fullscreen windows, so set this. */
4355 [NSApp activateIgnoringOtherApps
:YES
];
4358 ns_do_open_file
= YES
;
4360 #ifdef NS_IMPL_GNUSTEP
4361 /* GNUstep steals SIGCHLD for use in NSTask, but we don't use NSTask.
4362 We must re-catch it so subprocess works. */
4363 catch_child_signal
();
4370 ns_term_shutdown
(int sig
)
4372 [[NSUserDefaults standardUserDefaults
] synchronize
];
4374 /* code not reached in emacs.c after this is called by shut_down_emacs: */
4375 if (STRINGP
(Vauto_save_list_file_name
))
4376 unlink
(SSDATA
(Vauto_save_list_file_name
));
4378 if (sig
== 0 || sig
== SIGTERM
)
4380 [NSApp terminate
: NSApp
];
4382 else // force a stack
trace to happen
4389 /* ==========================================================================
4391 EmacsApp implementation
4393 ========================================================================== */
4396 @implementation EmacsApp
4398 - (void
)logNotification
: (NSNotification
*)notification
4400 const
char *name
= [[notification name
] UTF8String
];
4401 if (!strstr
(name
, "Update"
) && !strstr
(name
, "NSMenu"
)
4402 && !strstr
(name
, "WindowNumber"
))
4403 NSLog
(@"notification
: '%@'"
, [notification name
]);
4407 - (void
)sendEvent
: (NSEvent
*)theEvent
4408 /* --------------------------------------------------------------------------
4409 Called when NSApp is running for each event received. Used to stop
4410 the loop when we choose, since there's no way to just run one iteration.
4411 -------------------------------------------------------------------------- */
4413 int
type = [theEvent
type];
4414 NSWindow
*window
= [theEvent window
];
4416 /* NSTRACE (sendEvent); */
4417 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
4419 #ifdef NS_IMPL_GNUSTEP
4420 // Keyboard events aren
't propagated to file dialogs for some reason.
4421 if ([NSApp modalWindow] != nil &&
4422 (type == NSKeyDown || type == NSKeyUp || type == NSFlagsChanged))
4424 [[NSApp modalWindow] sendEvent: theEvent];
4429 if (type == NSApplicationDefined)
4431 switch ([theEvent data2])
4433 #ifdef NS_IMPL_COCOA
4434 case NSAPP_DATA2_RUNASSCRIPT:
4439 case NSAPP_DATA2_RUNFILEDIALOG:
4440 ns_run_file_dialog ();
4446 if (type == NSCursorUpdate && window == nil)
4448 fprintf (stderr, "Dropping external cursor update event.\n");
4452 if (type == NSApplicationDefined)
4454 /* Events posted by ns_send_appdefined interrupt the run loop here.
4455 But, if a modal window is up, an appdefined can still come through,
4456 (e.g., from a makeKeyWindow event) but stopping self also stops the
4457 modal loop. Just defer it until later. */
4458 if ([NSApp modalWindow] == nil)
4460 last_appdefined_event_data = [theEvent data1];
4465 send_appdefined = YES;
4470 #ifdef NS_IMPL_COCOA
4471 /* If no dialog and none of our frames have focus and it is a move, skip it.
4472 It is a mouse move in an auxiliary menu, i.e. on the top right on OSX,
4473 such as Wifi, sound, date or similar.
4474 This prevents "spooky" highlighting in the frame under the menu. */
4475 if (type == NSMouseMoved && [NSApp modalWindow] == nil)
4477 struct ns_display_info *di;
4478 BOOL has_focus = NO;
4479 for (di = x_display_list; ! has_focus && di; di = di->next)
4480 has_focus = di->x_focus_frame != 0;
4486 [super sendEvent: theEvent];
4490 - (void)showPreferencesWindow: (id)sender
4492 struct frame *emacsframe = SELECTED_FRAME ();
4493 NSEvent *theEvent = [NSApp currentEvent];
4497 emacs_event->kind = NS_NONKEY_EVENT;
4498 emacs_event->code = KEY_NS_SHOW_PREFS;
4499 emacs_event->modifiers = 0;
4500 EV_TRAILER (theEvent);
4504 - (void)newFrame: (id)sender
4506 struct frame *emacsframe = SELECTED_FRAME ();
4507 NSEvent *theEvent = [NSApp currentEvent];
4511 emacs_event->kind = NS_NONKEY_EVENT;
4512 emacs_event->code = KEY_NS_NEW_FRAME;
4513 emacs_event->modifiers = 0;
4514 EV_TRAILER (theEvent);
4518 /* Open a file (used by below, after going into queue read by ns_read_socket) */
4519 - (BOOL) openFile: (NSString *)fileName
4521 struct frame *emacsframe = SELECTED_FRAME ();
4522 NSEvent *theEvent = [NSApp currentEvent];
4527 emacs_event->kind = NS_NONKEY_EVENT;
4528 emacs_event->code = KEY_NS_OPEN_FILE_LINE;
4529 ns_input_file = append2 (ns_input_file, build_string ([fileName UTF8String]));
4530 ns_input_line = Qnil; /* can be start or cons start,end */
4531 emacs_event->modifiers =0;
4532 EV_TRAILER (theEvent);
4538 /* **************************************************************************
4540 EmacsApp delegate implementation
4542 ************************************************************************** */
4544 - (void)applicationDidFinishLaunching: (NSNotification *)notification
4545 /* --------------------------------------------------------------------------
4546 When application is loaded, terminate event loop in ns_term_init
4547 -------------------------------------------------------------------------- */
4549 NSTRACE (applicationDidFinishLaunching);
4550 [NSApp setServicesProvider: NSApp];
4551 ns_send_appdefined (-2);
4555 /* Termination sequences:
4558 MenuBar | File | Exit:
4559 Select Quit from App menubar:
4561 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4564 Select Quit from Dock menu:
4567 Cancel -> Nothing else
4571 KEY_NS_POWER_OFF, (save-buffers-kill-emacs)
4576 - (void) terminate: (id)sender
4578 struct frame *emacsframe = SELECTED_FRAME ();
4583 emacs_event->kind = NS_NONKEY_EVENT;
4584 emacs_event->code = KEY_NS_POWER_OFF;
4585 emacs_event->arg = Qt; /* mark as non-key event */
4586 EV_TRAILER ((id)nil);
4590 - (NSApplicationTerminateReply)applicationShouldTerminate: (id)sender
4594 if (NILP (ns_confirm_quit)) // || ns_shutdown_properly --> TO DO
4595 return NSTerminateNow;
4597 ret = NSRunAlertPanel(ns_app_name,
4598 @"Exit requested. Would you like to Save Buffers and Exit, or Cancel the request?",
4599 @"Save Buffers and Exit", @"Cancel", nil);
4601 if (ret == NSAlertDefaultReturn)
4602 return NSTerminateNow;
4603 else if (ret == NSAlertAlternateReturn)
4604 return NSTerminateCancel;
4605 return NSTerminateNow; /* just in case */
4609 not_in_argv (NSString *arg)
4612 const char *a = [arg UTF8String];
4613 for (k = 1; k < initial_argc; ++k)
4614 if (strcmp (a, initial_argv[k]) == 0) return 0;
4618 /* Notification from the Workspace to open a file */
4619 - (BOOL)application: sender openFile: (NSString *)file
4621 if (ns_do_open_file || not_in_argv (file))
4622 [ns_pending_files addObject: file];
4627 /* Open a file as a temporary file */
4628 - (BOOL)application: sender openTempFile: (NSString *)file
4630 if (ns_do_open_file || not_in_argv (file))
4631 [ns_pending_files addObject: file];
4636 /* Notification from the Workspace to open a file noninteractively (?) */
4637 - (BOOL)application: sender openFileWithoutUI: (NSString *)file
4639 if (ns_do_open_file || not_in_argv (file))
4640 [ns_pending_files addObject: file];
4644 /* Notification from the Workspace to open multiple files */
4645 - (void)application: sender openFiles: (NSArray *)fileList
4647 NSEnumerator *files = [fileList objectEnumerator];
4649 /* Don't open files from the command
line unconditionally
,
4650 Cocoa parses the command
line wrong
, --option value tries to open value
4651 if --option is the last option. */
4652 while ((file
= [files nextObject
]) != nil
)
4653 if (ns_do_open_file || not_in_argv
(file
))
4654 [ns_pending_files addObject
: file
];
4656 [self replyToOpenOrPrint
: NSApplicationDelegateReplySuccess
];
4661 /* Handle dock menu requests. */
4662 - (NSMenu
*)applicationDockMenu
: (NSApplication
*) sender
4668 /* TODO: these may help w/IO switching btwn terminal and NSApp */
4669 - (void
)applicationWillBecomeActive
: (NSNotification
*)notification
4671 //ns_app_active
=YES
;
4673 - (void
)applicationDidBecomeActive
: (NSNotification
*)notification
4675 NSTRACE
(applicationDidBecomeActive
);
4677 //ns_app_active
=YES
;
4679 ns_update_auto_hide_menu_bar
();
4680 // No constraining takes place when the application is not active.
4681 ns_constrain_all_frames
();
4683 - (void
)applicationDidResignActive
: (NSNotification
*)notification
4686 ns_send_appdefined
(-1);
4691 /* ==========================================================================
4693 EmacsApp aux handlers for managing event loop
4695 ========================================================================== */
4698 - (void
)timeout_handler
: (NSTimer
*)timedEntry
4699 /* --------------------------------------------------------------------------
4700 The timeout specified to ns_select has passed.
4701 -------------------------------------------------------------------------- */
4703 /*NSTRACE (timeout_handler); */
4704 ns_send_appdefined
(-2);
4707 #ifdef NS_IMPL_GNUSTEP
4708 - (void
)sendFromMainThread
:(id
)unused
4710 ns_send_appdefined
(nextappdefined
);
4714 - (void
)fd_handler
:(id
)unused
4715 /* --------------------------------------------------------------------------
4716 Check data waiting on file descriptors and terminate if so
4717 -------------------------------------------------------------------------- */
4720 int waiting
= 1, nfds
;
4723 SELECT_TYPE readfds
, writefds
, *wfds
;
4724 EMACS_TIME timeout
, *tmo
;
4725 NSAutoreleasePool
*pool
= nil
;
4727 /* NSTRACE (fd_handler); */
4732 pool
= [[NSAutoreleasePool alloc
] init
];
4738 FD_SET
(selfds
[0], &fds
);
4739 result
= select
(selfds
[0]+1, &fds
, NULL, NULL, NULL);
4740 if (result
> 0 && read
(selfds
[0], &c
, 1) == 1 && c
== 'g')
4745 pthread_mutex_lock
(&select_mutex
);
4748 if (select_valid
& SELECT_HAVE_READ
)
4749 readfds
= select_readfds
;
4753 if (select_valid
& SELECT_HAVE_WRITE
)
4755 writefds
= select_writefds
;
4760 if (select_valid
& SELECT_HAVE_TMO
)
4762 timeout
= select_timeout
;
4768 pthread_mutex_unlock
(&select_mutex
);
4770 FD_SET
(selfds
[0], &readfds
);
4771 if (selfds
[0] >= nfds
) nfds
= selfds
[0]+1;
4773 result
= pselect
(nfds
, &readfds
, wfds
, NULL, tmo
, NULL);
4776 ns_send_appdefined
(-2);
4777 else if (result
> 0)
4779 if (FD_ISSET
(selfds
[0], &readfds
))
4781 if (read
(selfds
[0], &c
, 1) == 1 && c
== 's')
4786 pthread_mutex_lock
(&select_mutex
);
4787 if (select_valid
& SELECT_HAVE_READ
)
4788 select_readfds
= readfds
;
4789 if (select_valid
& SELECT_HAVE_WRITE
)
4790 select_writefds
= writefds
;
4791 if (select_valid
& SELECT_HAVE_TMO
)
4792 select_timeout
= timeout
;
4793 pthread_mutex_unlock
(&select_mutex
);
4795 ns_send_appdefined
(result
);
4805 /* ==========================================================================
4809 ========================================================================== */
4811 /* called from system: queue for next pass through event loop */
4812 - (void
)requestService
: (NSPasteboard
*)pboard
4813 userData
: (NSString
*)userData
4814 error: (NSString
**)error
4816 [ns_pending_service_names addObject
: userData
];
4817 [ns_pending_service_args addObject
: [NSString stringWithUTF8String
:
4818 SSDATA
(ns_string_from_pasteboard
(pboard
))]];
4822 /* called from ns_read_socket to clear queue */
4823 - (BOOL
)fulfillService
: (NSString
*)name withArg
: (NSString
*)arg
4825 struct frame
*emacsframe
= SELECTED_FRAME
();
4826 NSEvent
*theEvent
= [NSApp currentEvent
];
4831 emacs_event
->kind
= NS_NONKEY_EVENT
;
4832 emacs_event
->code
= KEY_NS_SPI_SERVICE_CALL
;
4833 ns_input_spi_name
= build_string
([name UTF8String
]);
4834 ns_input_spi_arg
= build_string
([arg UTF8String
]);
4835 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
4836 EV_TRAILER
(theEvent
);
4846 /* ==========================================================================
4848 EmacsView implementation
4850 ========================================================================== */
4853 @implementation EmacsView
4855 /* needed to inform when window closed from LISP */
4856 - (void
) setWindowClosing
: (BOOL
)closing
4858 windowClosing
= closing
;
4864 NSTRACE
(EmacsView_dealloc
);
4866 if (fs_state
== FULLSCREEN_BOTH
)
4867 [nonfs_window release
];
4872 /* called on font panel selection */
4873 - (void
)changeFont
: (id
)sender
4875 NSEvent
*e
=[[self window
] currentEvent
];
4876 struct face
*face
=FRAME_DEFAULT_FACE
(emacsframe
);
4880 NSTRACE
(changeFont
);
4884 if ((newFont
= [sender convertFont
:
4885 ((struct nsfont_info
*)face
->font
)->nsfont
]))
4887 SET_FRAME_GARBAGED
(emacsframe
); /* now needed as of 2008/10 */
4889 emacs_event
->kind
= NS_NONKEY_EVENT
;
4890 emacs_event
->modifiers
= 0;
4891 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
4893 size = [newFont pointSize
];
4894 ns_input_fontsize
= make_number
(lrint
(size));
4895 ns_input_font
= build_string
([[newFont familyName
] UTF8String
]);
4901 - (BOOL
)acceptsFirstResponder
4903 NSTRACE
(acceptsFirstResponder
);
4908 - (void
)resetCursorRects
4910 NSRect visible
= [self visibleRect
];
4911 NSCursor
*currentCursor
= FRAME_POINTER_TYPE
(emacsframe
);
4912 NSTRACE
(resetCursorRects
);
4914 if (currentCursor
== nil
)
4915 currentCursor
= [NSCursor arrowCursor
];
4917 if (!NSIsEmptyRect
(visible
))
4918 [self addCursorRect
: visible cursor
: currentCursor
];
4919 [currentCursor setOnMouseEntered
: YES
];
4924 /*****************************************************************************/
4925 /* Keyboard handling. */
4928 - (void
)keyDown
: (NSEvent
*)theEvent
4930 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
4932 unsigned fnKeysym
= 0;
4933 static NSMutableArray
*nsEvArray
;
4934 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
4935 static BOOL firstTime
= YES
;
4938 unsigned int flags
= [theEvent modifierFlags
];
4942 /* Rhapsody and OS X give up and down events for the arrow keys */
4943 if (ns_fake_keydown
== YES
)
4944 ns_fake_keydown
= NO
;
4945 else if ([theEvent
type] != NSKeyDown
)
4951 if (![[self window
] isKeyWindow
]
4952 && [[theEvent window
] isKindOfClass
: [EmacsWindow
class]]
4953 /* we must avoid an infinite loop here. */
4954 && (EmacsView
*)[[theEvent window
] delegate
] != self
)
4956 /* XXX: There is an occasional condition in which, when Emacs display
4957 updates a different frame from the current one, and temporarily
4958 selects it, then processes some interrupt-driven input
4959 (dispnew.c:3878), OS will send the event to the correct NSWindow, but
4960 for some reason that window has its first responder set to the NSView
4961 most recently updated (I guess), which is not the correct one. */
4962 [(EmacsView
*)[[theEvent window
] delegate
] keyDown
: theEvent
];
4966 if (nsEvArray
== nil
)
4967 nsEvArray
= [[NSMutableArray alloc
] initWithCapacity
: 1];
4969 [NSCursor setHiddenUntilMouseMoves
: YES
];
4971 if (hlinfo
->mouse_face_hidden
&& INTEGERP
(Vmouse_highlight
))
4973 clear_mouse_face
(hlinfo
);
4974 hlinfo
->mouse_face_hidden
= 1;
4977 if (!processingCompose
)
4979 /* When using screen sharing, no left or right information is sent,
4980 so use Left key in those cases. */
4981 int is_left_key
, is_right_key
;
4983 code
= ([[theEvent charactersIgnoringModifiers
] length] == 0) ?
4984 0 : [[theEvent charactersIgnoringModifiers
] characterAtIndex
: 0];
4986 /* (Carbon way: [theEvent keyCode]) */
4988 /* is it a "function key"? */
4989 fnKeysym
= (code
< 0x00ff && (flags
&NSNumericPadKeyMask
))
4990 ? ns_convert_key
([theEvent keyCode
] | NSNumericPadKeyMask
)
4991 : ns_convert_key
(code
);
4995 /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
4996 because Emacs treats Delete and KP-Delete same (in simple.el). */
4997 if ((fnKeysym
== 0xFFFF && [theEvent keyCode
] == 0x33)
4998 #ifdef NS_IMPL_GNUSTEP
4999 /* GNUstep uses incompatible keycodes, even for those that are
5000 supposed to be hardware independent. Just check for delete.
5001 Keypad delete does not have keysym 0xFFFF.
5002 See http://savannah.gnu.org/bugs/?25395
5004 ||
(fnKeysym
== 0xFFFF && code
== 127)
5007 code
= 0xFF08; /* backspace */
5012 /* are there modifiers? */
5013 emacs_event
->modifiers
= 0;
5015 if (flags
& NSHelpKeyMask
)
5016 emacs_event
->modifiers |
= hyper_modifier
;
5018 if (flags
& NSShiftKeyMask
)
5019 emacs_event
->modifiers |
= shift_modifier
;
5021 is_right_key
= (flags
& NSRightCommandKeyMask
) == NSRightCommandKeyMask
;
5022 is_left_key
= (flags
& NSLeftCommandKeyMask
) == NSLeftCommandKeyMask
5023 ||
(! is_right_key
&& (flags
& NSCommandKeyMask
) == NSCommandKeyMask
);
5026 emacs_event
->modifiers |
= parse_solitary_modifier
5027 (EQ
(ns_right_command_modifier
, Qleft
)
5028 ? ns_command_modifier
5029 : ns_right_command_modifier
);
5033 emacs_event
->modifiers |
= parse_solitary_modifier
5034 (ns_command_modifier
);
5036 /* if super (default), take input manager's word so things like
5037 dvorak / qwerty layout work */
5038 if (EQ
(ns_command_modifier
, Qsuper
)
5040 && [[theEvent characters
] length] != 0)
5042 /* XXX: the code we get will be unshifted, so if we have
5043 a shift modifier, must convert ourselves */
5044 if (!(flags
& NSShiftKeyMask
))
5045 code
= [[theEvent characters
] characterAtIndex
: 0];
5047 /* this is ugly and also requires linking w/Carbon framework
5048 (for LMGetKbdType) so for now leave this rare (?) case
5049 undealt with.. in future look into CGEvent methods */
5052 long smv
= GetScriptManagerVariable
(smKeyScript
);
5053 Handle uchrHandle
= GetResource
5054 ('uchr', GetScriptVariable
(smv
, smScriptKeys
));
5056 UCKeyTranslate
((UCKeyboardLayout
*)*uchrHandle
,
5057 [[theEvent characters
] characterAtIndex
: 0],
5058 kUCKeyActionDisplay
,
5059 (flags
& ~NSCommandKeyMask
) >> 8,
5060 LMGetKbdType
(), kUCKeyTranslateNoDeadKeysMask
,
5061 &dummy
, 1, &dummy
, &code
);
5068 is_right_key
= (flags
& NSRightControlKeyMask
) == NSRightControlKeyMask
;
5069 is_left_key
= (flags
& NSLeftControlKeyMask
) == NSLeftControlKeyMask
5070 ||
(! is_right_key
&& (flags
& NSControlKeyMask
) == NSControlKeyMask
);
5073 emacs_event
->modifiers |
= parse_solitary_modifier
5074 (EQ
(ns_right_control_modifier
, Qleft
)
5075 ? ns_control_modifier
5076 : ns_right_control_modifier
);
5079 emacs_event
->modifiers |
= parse_solitary_modifier
5080 (ns_control_modifier
);
5082 if (flags
& NS_FUNCTION_KEY_MASK
&& !fnKeysym
)
5083 emacs_event
->modifiers |
=
5084 parse_solitary_modifier
(ns_function_modifier
);
5086 left_is_none
= NILP
(ns_alternate_modifier
)
5087 || EQ
(ns_alternate_modifier
, Qnone
);
5089 is_right_key
= (flags
& NSRightAlternateKeyMask
)
5090 == NSRightAlternateKeyMask
;
5091 is_left_key
= (flags
& NSLeftAlternateKeyMask
) == NSLeftAlternateKeyMask
5093 && (flags
& NSAlternateKeyMask
) == NSAlternateKeyMask
);
5097 if ((NILP
(ns_right_alternate_modifier
)
5098 || EQ
(ns_right_alternate_modifier
, Qnone
)
5099 ||
(EQ
(ns_right_alternate_modifier
, Qleft
) && left_is_none
))
5101 { /* accept pre-interp alt comb */
5102 if ([[theEvent characters
] length] > 0)
5103 code
= [[theEvent characters
] characterAtIndex
: 0];
5104 /*HACK: clear lone shift modifier to stop next if from firing */
5105 if (emacs_event
->modifiers
== shift_modifier
)
5106 emacs_event
->modifiers
= 0;
5109 emacs_event
->modifiers |
= parse_solitary_modifier
5110 (EQ
(ns_right_alternate_modifier
, Qleft
)
5111 ? ns_alternate_modifier
5112 : ns_right_alternate_modifier
);
5115 if (is_left_key
) /* default = meta */
5117 if (left_is_none
&& !fnKeysym
)
5118 { /* accept pre-interp alt comb */
5119 if ([[theEvent characters
] length] > 0)
5120 code
= [[theEvent characters
] characterAtIndex
: 0];
5121 /*HACK: clear lone shift modifier to stop next if from firing */
5122 if (emacs_event
->modifiers
== shift_modifier
)
5123 emacs_event
->modifiers
= 0;
5126 emacs_event
->modifiers |
=
5127 parse_solitary_modifier
(ns_alternate_modifier
);
5131 fprintf (stderr
, "keyDown
: code
=%x\tfnKey =%x\tflags = %x\tmods = %x\n",
5132 code
, fnKeysym
, flags
, emacs_event
->modifiers
);
5134 /* if it was a function key or had modifiers, pass it directly to emacs */
5135 if (fnKeysym ||
(emacs_event
->modifiers
5136 && (emacs_event
->modifiers
!= shift_modifier
)
5137 && [[theEvent charactersIgnoringModifiers
] length] > 0))
5138 /*[[theEvent characters] length] */
5140 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5142 code |
= (1<<28)|
(3<<16);
5143 else if (code
== 0x7f)
5144 code |
= (1<<28)|
(3<<16);
5146 emacs_event
->kind
= code
> 0xFF
5147 ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5149 emacs_event
->code
= code
;
5150 EV_TRAILER
(theEvent
);
5151 processingCompose
= NO
;
5157 #
if !defined
(NS_IMPL_COCOA
) || MAC_OS_X_VERSION_MAX_ALLOWED
< MAC_OS_X_VERSION_10_6
5158 /* if we get here we should send the key for input manager processing */
5159 /* Disable warning, there is nothing a user can do about it anyway, and
5160 it does not seem to matter. */
5162 if (firstTime
&& [[NSInputManager currentInputManager
]
5163 wantsToDelayTextChangeNotifications
] == NO
)
5165 "Emacs
: WARNING
: TextInput mgr wants marked
text to be permanent
!\n"
);
5169 if (NS_KEYLOG
&& !processingCompose
)
5170 fprintf (stderr
, "keyDown
: Begin compose sequence.
\n"
);
5172 processingCompose
= YES
;
5173 [nsEvArray addObject
: theEvent
];
5174 [self interpretKeyEvents
: nsEvArray
];
5175 [nsEvArray removeObject
: theEvent
];
5179 #ifdef NS_IMPL_COCOA
5180 /* Needed to pick up Ctrl-tab and possibly other events that OS X has
5181 decided not to send key-down for.
5182 See http://osdir.com/ml/editors.vim.mac/2007-10/msg00141.html
5183 This only applies on Tiger and earlier.
5184 If it matches one of these, send it on to keyDown. */
5185 -(void
)keyUp
: (NSEvent
*)theEvent
5187 int flags
= [theEvent modifierFlags
];
5188 int code
= [theEvent keyCode
];
5189 if (floor (NSAppKitVersionNumber
) <= 824 /*NSAppKitVersionNumber10_4*/ &&
5190 code
== 0x30 && (flags
& NSControlKeyMask
) && !(flags
& NSCommandKeyMask
))
5193 fprintf (stderr
, "keyUp
: passed test"
);
5194 ns_fake_keydown
= YES
;
5195 [self keyDown
: theEvent
];
5201 /* <NSTextInput> implementation (called through super interpretKeyEvents:]). */
5204 /* <NSTextInput>: called when done composing;
5205 NOTE: also called when we delete over working text, followed immed.
5206 by doCommandBySelector: deleteBackward: */
5207 - (void
)insertText
: (id
)aString
5210 int len
= [(NSString
*)aString
length];
5214 NSLog
(@"insertText
'%@'\tlen
= %d", aString, len);
5215 processingCompose
= NO
;
5220 /* first, clear any working text */
5221 if (workingText
!= nil
)
5222 [self deleteWorkingText
];
5224 /* now insert the string as keystrokes */
5225 for (i
=0; i
<len
; i
++)
5227 code
= [aString characterAtIndex
: i
];
5228 /* TODO: still need this? */
5230 code
= '~'; /* 0x7E */
5231 if (code
!= 32) /* Space */
5232 emacs_event
->modifiers
= 0;
5234 = code
> 0xFF ? MULTIBYTE_CHAR_KEYSTROKE_EVENT
: ASCII_KEYSTROKE_EVENT
;
5235 emacs_event
->code
= code
;
5236 EV_TRAILER
((id
)nil
);
5241 /* <NSTextInput>: inserts display of composing characters */
5242 - (void
)setMarkedText
: (id
)aString selectedRange
: (NSRange
)selRange
5244 NSString
*str
= [aString respondsToSelector
: @selector
(string
)] ?
5245 [aString string
] : aString
;
5247 NSLog
(@"setMarkedText
'%@' len
=%d range %d from %d", str, [str length],
5248 selRange.
length, selRange.location
);
5250 if (workingText
!= nil
)
5251 [self deleteWorkingText
];
5252 if ([str
length] == 0)
5258 processingCompose
= YES
;
5259 workingText
= [str copy
];
5260 ns_working_text
= build_string
([workingText UTF8String
]);
5262 emacs_event
->kind
= NS_TEXT_EVENT
;
5263 emacs_event
->code
= KEY_NS_PUT_WORKING_TEXT
;
5264 EV_TRAILER
((id
)nil
);
5268 /* delete display of composing characters [not in <NSTextInput>] */
5269 - (void
)deleteWorkingText
5271 if (workingText
== nil
)
5274 NSLog
(@"deleteWorkingText len
=%d\n", [workingText length]);
5275 [workingText release
];
5277 processingCompose
= NO
;
5282 emacs_event
->kind
= NS_TEXT_EVENT
;
5283 emacs_event
->code
= KEY_NS_UNPUT_WORKING_TEXT
;
5284 EV_TRAILER
((id
)nil
);
5288 - (BOOL
)hasMarkedText
5290 return workingText
!= nil
;
5294 - (NSRange
)markedRange
5296 NSRange rng
= workingText
!= nil
5297 ? NSMakeRange
(0, [workingText
length]) : NSMakeRange
(NSNotFound
, 0);
5299 NSLog
(@"markedRange request"
);
5307 NSLog
(@"unmark
(accept
) text"
);
5308 [self deleteWorkingText
];
5309 processingCompose
= NO
;
5313 /* used to position char selection windows, etc. */
5314 - (NSRect
)firstRectForCharacterRange
: (NSRange
)theRange
5318 struct window
*win
= XWINDOW
(FRAME_SELECTED_WINDOW
(emacsframe
));
5320 NSLog
(@"firstRectForCharRange request"
);
5322 rect.
size.width
= theRange.
length * FRAME_COLUMN_WIDTH
(emacsframe
);
5323 rect.
size.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5324 pt.x
= WINDOW_TEXT_TO_FRAME_PIXEL_X
(win
, win
->phys_cursor.x
);
5325 pt.y
= WINDOW_TO_FRAME_PIXEL_Y
(win
, win
->phys_cursor.y
5326 +FRAME_LINE_HEIGHT
(emacsframe
));
5328 pt
= [self convertPoint
: pt toView
: nil
];
5329 pt
= [[self window
] convertBaseToScreen
: pt
];
5335 - (NSInteger
)conversationIdentifier
5337 return (NSInteger
)self
;
5341 - (void
)doCommandBySelector
: (SEL
)aSelector
5344 NSLog
(@"doCommandBySelector
: %@", NSStringFromSelector (aSelector));
5346 processingCompose
= NO
;
5347 if (aSelector
== @selector
(deleteBackward
:))
5349 /* happens when user backspaces over an ongoing composition:
5350 throw a 'delete' into the event queue */
5353 emacs_event
->kind
= NON_ASCII_KEYSTROKE_EVENT
;
5354 emacs_event
->code
= 0xFF08;
5355 EV_TRAILER
((id
)nil
);
5359 - (NSArray
*)validAttributesForMarkedText
5361 static NSArray
*arr
= nil
;
5362 if (arr
== nil
) arr
= [NSArray new
];
5363 /* [[NSArray arrayWithObject: NSUnderlineStyleAttributeName] retain]; */
5367 - (NSRange
)selectedRange
5370 NSLog
(@"selectedRange request"
);
5371 return NSMakeRange
(NSNotFound
, 0);
5374 #
if defined
(NS_IMPL_COCOA
) || GNUSTEP_GUI_MAJOR_VERSION
> 0 || \
5375 GNUSTEP_GUI_MINOR_VERSION
> 22
5376 - (NSUInteger
)characterIndexForPoint
: (NSPoint
)thePoint
5378 - (unsigned int
)characterIndexForPoint
: (NSPoint
)thePoint
5382 NSLog
(@"characterIndexForPoint request"
);
5386 - (NSAttributedString
*)attributedSubstringFromRange
: (NSRange
)theRange
5388 static NSAttributedString
*str
= nil
;
5389 if (str
== nil
) str
= [NSAttributedString new
];
5391 NSLog
(@"attributedSubstringFromRange request"
);
5395 /* End <NSTextInput> impl. */
5396 /*****************************************************************************/
5399 /* This is what happens when the user presses a mouse button. */
5400 - (void
)mouseDown
: (NSEvent
*)theEvent
5402 NSPoint p
= [self convertPoint
: [theEvent locationInWindow
] fromView
: nil
];
5404 NSTRACE
(mouseDown
);
5406 [self deleteWorkingText
];
5411 last_mouse_frame
= emacsframe
;
5412 /* appears to be needed to prevent spurious movement events generated on
5414 last_mouse_frame
->mouse_moved
= 0;
5416 if ([theEvent
type] == NSScrollWheel
)
5418 CGFloat delta
= [theEvent deltaY
];
5419 /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
5422 emacs_event
->kind
= WHEEL_EVENT
;
5423 emacs_event
->code
= 0;
5424 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
) |
5425 ((delta
> 0) ? up_modifier
: down_modifier
);
5429 emacs_event
->kind
= MOUSE_CLICK_EVENT
;
5430 emacs_event
->code
= EV_BUTTON
(theEvent
);
5431 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
)
5432 | EV_UDMODIFIERS
(theEvent
);
5434 XSETINT
(emacs_event
->x
, lrint
(p.x
));
5435 XSETINT
(emacs_event
->y
, lrint
(p.y
));
5436 EV_TRAILER
(theEvent
);
5440 - (void
)rightMouseDown
: (NSEvent
*)theEvent
5442 NSTRACE
(rightMouseDown
);
5443 [self mouseDown
: theEvent
];
5447 - (void
)otherMouseDown
: (NSEvent
*)theEvent
5449 NSTRACE
(otherMouseDown
);
5450 [self mouseDown
: theEvent
];
5454 - (void
)mouseUp
: (NSEvent
*)theEvent
5457 [self mouseDown
: theEvent
];
5461 - (void
)rightMouseUp
: (NSEvent
*)theEvent
5463 NSTRACE
(rightMouseUp
);
5464 [self mouseDown
: theEvent
];
5468 - (void
)otherMouseUp
: (NSEvent
*)theEvent
5470 NSTRACE
(otherMouseUp
);
5471 [self mouseDown
: theEvent
];
5475 - (void
) scrollWheel
: (NSEvent
*)theEvent
5477 NSTRACE
(scrollWheel
);
5478 [self mouseDown
: theEvent
];
5482 /* Tell emacs the mouse has moved. */
5483 - (void
)mouseMoved
: (NSEvent
*)e
5485 Mouse_HLInfo
*hlinfo
= MOUSE_HL_INFO
(emacsframe
);
5488 // NSTRACE
(mouseMoved
);
5490 last_mouse_movement_time
= EV_TIMESTAMP
(e
);
5491 last_mouse_motion_position
5492 = [self convertPoint
: [e locationInWindow
] fromView
: nil
];
5494 /* update any mouse face */
5495 if (hlinfo
->mouse_face_hidden
)
5497 hlinfo
->mouse_face_hidden
= 0;
5498 clear_mouse_face
(hlinfo
);
5501 /* tooltip handling */
5502 previous_help_echo_string
= help_echo_string
;
5503 help_echo_string
= Qnil
;
5505 if (!note_mouse_movement
(emacsframe
, last_mouse_motion_position.x
,
5506 last_mouse_motion_position.y
))
5507 help_echo_string
= previous_help_echo_string
;
5509 XSETFRAME
(frame
, emacsframe
);
5510 if (!NILP
(help_echo_string
) ||
!NILP
(previous_help_echo_string
))
5512 /* NOTE: help_echo_{window,pos,object} are set in xdisp.c
5513 (note_mouse_highlight), which is called through the
5514 note_mouse_movement () call above */
5515 gen_help_event
(help_echo_string
, frame
, help_echo_window
,
5516 help_echo_object
, help_echo_pos
);
5520 help_echo_string
= Qnil
;
5521 gen_help_event
(Qnil
, frame
, Qnil
, Qnil
, 0);
5524 if (emacsframe
->mouse_moved
&& send_appdefined
)
5525 ns_send_appdefined
(-1);
5529 - (void
)mouseDragged
: (NSEvent
*)e
5531 NSTRACE
(mouseDragged
);
5532 [self mouseMoved
: e
];
5536 - (void
)rightMouseDragged
: (NSEvent
*)e
5538 NSTRACE
(rightMouseDragged
);
5539 [self mouseMoved
: e
];
5543 - (void
)otherMouseDragged
: (NSEvent
*)e
5545 NSTRACE
(otherMouseDragged
);
5546 [self mouseMoved
: e
];
5550 - (BOOL
)windowShouldClose
: (id
)sender
5552 NSEvent
*e
=[[self window
] currentEvent
];
5554 NSTRACE
(windowShouldClose
);
5555 windowClosing
= YES
;
5558 emacs_event
->kind
= DELETE_WINDOW_EVENT
;
5559 emacs_event
->modifiers
= 0;
5560 emacs_event
->code
= 0;
5562 /* Don't close this window, let this be done from lisp code. */
5566 - (void
) updateFrameSize
: (BOOL
) delay
;
5568 NSWindow
*window
= [self window
];
5569 NSRect wr
= [window frame
];
5572 #ifdef NS_IMPL_GNUSTEP
5576 int oldc
= cols
, oldr
= rows
;
5577 int oldw
= FRAME_PIXEL_WIDTH
(emacsframe
),
5578 oldh
= FRAME_PIXEL_HEIGHT
(emacsframe
);
5581 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
, wr.
size.width
+ gsextra
);
5583 if (cols
< MINWIDTH
)
5586 if (! [self isFullscreen
])
5588 extra
= FRAME_NS_TITLEBAR_HEIGHT
(emacsframe
)
5589 + FRAME_TOOLBAR_HEIGHT
(emacsframe
) - gsextra
;
5592 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
, wr.
size.height
- extra
);
5594 if (rows
< MINHEIGHT
)
5597 neww
= (int
)wr.
size.width
- emacsframe
->border_width
;
5598 newh
= (int
)wr.
size.height
- extra
;
5600 if (oldr
!= rows || oldc
!= cols || neww
!= oldw || newh
!= oldh
)
5602 NSView
*view = FRAME_NS_VIEW
(emacsframe
);
5603 NSWindow
*win
= [view window
];
5604 NSSize sz
= [win resizeIncrements
];
5606 FRAME_PIXEL_WIDTH
(emacsframe
) = neww
;
5607 FRAME_PIXEL_HEIGHT
(emacsframe
) = newh
;
5608 change_frame_size
(emacsframe
, rows
, cols
, 0, delay
, 0);
5609 SET_FRAME_GARBAGED
(emacsframe
);
5610 cancel_mouse_face
(emacsframe
);
5612 // Did resize increments change because of a font change?
5613 if (sz.width
!= FRAME_COLUMN_WIDTH
(emacsframe
) ||
5614 sz.height
!= FRAME_LINE_HEIGHT
(emacsframe
))
5616 sz.width
= FRAME_COLUMN_WIDTH
(emacsframe
);
5617 sz.height
= FRAME_LINE_HEIGHT
(emacsframe
);
5618 [win setResizeIncrements
: sz
];
5621 [view setFrame
: NSMakeRect
(0, 0, neww
, newh
)];
5622 [self windowDidMove
:nil
]; // Update top
/left.
5626 - (NSSize
)windowWillResize
: (NSWindow
*)sender toSize
: (NSSize
)frameSize
5627 /* normalize frame to gridded text size */
5631 #ifdef NS_IMPL_GNUSTEP
5635 NSTRACE
(windowWillResize
);
5636 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
5638 if (fs_state
== FULLSCREEN_MAXIMIZED
5639 && (maximized_width
!= (int
)frameSize.width
5640 || maximized_height
!= (int
)frameSize.height
))
5641 [self setFSValue
: FULLSCREEN_NONE
];
5642 else if (fs_state
== FULLSCREEN_WIDTH
5643 && maximized_width
!= (int
)frameSize.width
)
5644 [self setFSValue
: FULLSCREEN_NONE
];
5645 else if (fs_state
== FULLSCREEN_HEIGHT
5646 && maximized_height
!= (int
)frameSize.height
)
5647 [self setFSValue
: FULLSCREEN_NONE
];
5648 if (fs_state
== FULLSCREEN_NONE
)
5649 maximized_width
= maximized_height
= -1;
5651 cols
= FRAME_PIXEL_WIDTH_TO_TEXT_COLS
(emacsframe
,
5652 frameSize.width
+ gsextra
);
5653 if (cols
< MINWIDTH
)
5656 rows
= FRAME_PIXEL_HEIGHT_TO_TEXT_LINES
(emacsframe
,
5657 frameSize.height
- extra
);
5658 if (rows
< MINHEIGHT
)
5660 #ifdef NS_IMPL_COCOA
5662 /* this sets window title to have size in it; the wm does this under GS */
5663 NSRect r
= [[self window
] frame
];
5664 if (r.
size.height
== frameSize.height
&& r.
size.width
== frameSize.width
)
5672 else if (fs_state
== FULLSCREEN_NONE
&& ! maximizing_resize
)
5675 NSWindow
*window
= [self window
];
5678 char *t
= strdup
([[[self window
] title] UTF8String
]);
5679 char *pos
= strstr
(t
, " — "
);
5684 size_title
= xmalloc
(strlen
(old_title
) + 40);
5685 esprintf
(size_title
, "
%s — (%d x %d)", old_title, cols, rows);
5686 [window setTitle
: [NSString stringWithUTF8String
: size_title
]];
5691 #endif
/* NS_IMPL_COCOA */
5692 /*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
5698 - (void
)windowDidResize
: (NSNotification
*)notification
5700 if (! [self fsIsNative
])
5702 NSWindow
*theWindow
= [notification object
];
5703 /* We can get notification on the non-FS window when in
5705 if ([self window
] != theWindow
) return;
5708 #ifdef NS_IMPL_GNUSTEP
5709 NSWindow
*theWindow
= [notification object
];
5711 /* In GNUstep, at least currently, it's possible to get a didResize
5712 without getting a willResize.. therefore we need to act as if we got
5713 the willResize now */
5714 NSSize sz
= [theWindow frame
].
size;
5715 sz
= [self windowWillResize
: theWindow toSize
: sz
];
5716 #endif
/* NS_IMPL_GNUSTEP */
5718 NSTRACE
(windowDidResize
);
5719 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
5721 if (cols
> 0 && rows
> 0)
5723 [self updateFrameSize
: YES
];
5726 ns_send_appdefined
(-1);
5729 #ifdef NS_IMPL_COCOA
5730 - (void
)viewDidEndLiveResize
5732 [super viewDidEndLiveResize
];
5735 [[self window
] setTitle
: [NSString stringWithUTF8String
: old_title
]];
5739 maximizing_resize
= NO
;
5741 #endif
/* NS_IMPL_COCOA */
5744 - (void
)windowDidBecomeKey
: (NSNotification
*)notification
5745 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5747 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5748 struct frame
*old_focus
= dpyinfo
->x_focus_frame
;
5750 NSTRACE
(windowDidBecomeKey
);
5752 if (emacsframe
!= old_focus
)
5753 dpyinfo
->x_focus_frame
= emacsframe
;
5755 ns_frame_rehighlight
(emacsframe
);
5759 emacs_event
->kind
= FOCUS_IN_EVENT
;
5760 EV_TRAILER
((id
)nil
);
5765 - (void
)windowDidResignKey
: (NSNotification
*)notification
5766 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
5768 struct ns_display_info
*dpyinfo
= FRAME_NS_DISPLAY_INFO
(emacsframe
);
5769 BOOL is_focus_frame
= dpyinfo
->x_focus_frame
== emacsframe
;
5770 NSTRACE
(windowDidResignKey
);
5773 dpyinfo
->x_focus_frame
= 0;
5775 ns_frame_rehighlight
(emacsframe
);
5777 /* FIXME: for some reason needed on second and subsequent clicks away
5778 from sole-frame Emacs to get hollow box to show */
5779 if (!windowClosing
&& [[self window
] isVisible
] == YES
)
5781 x_update_cursor
(emacsframe
, 1);
5782 x_set_frame_alpha
(emacsframe
);
5785 if (emacs_event
&& is_focus_frame
)
5787 [self deleteWorkingText
];
5788 emacs_event
->kind
= FOCUS_OUT_EVENT
;
5789 EV_TRAILER
((id
)nil
);
5794 - (void
)windowWillMiniaturize
: sender
5796 NSTRACE
(windowWillMiniaturize
);
5812 - initFrameFromEmacs
: (struct frame
*)f
5821 NSTRACE
(initFrameFromEmacs
);
5824 processingCompose
= NO
;
5825 scrollbarsNeedingUpdate
= 0;
5826 fs_state
= FULLSCREEN_NONE
;
5827 fs_before_fs
= next_maximized
= -1;
5828 #ifdef HAVE_NATIVE_FS
5829 fs_is_native
= ns_use_native_fullscreen
;
5833 maximized_width
= maximized_height
= -1;
5836 /*fprintf (stderr,"init with %d, %d\n",f->text_cols, f->text_lines); */
5838 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
5839 r
= NSMakeRect
(0, 0, FRAME_TEXT_COLS_TO_PIXEL_WIDTH
(f
, f
->text_cols
),
5840 FRAME_TEXT_LINES_TO_PIXEL_HEIGHT
(f
, f
->text_lines
));
5841 [self initWithFrame
: r
];
5842 [self setAutoresizingMask
: NSViewWidthSizable | NSViewHeightSizable
];
5844 FRAME_NS_VIEW
(f
) = self
;
5846 #ifdef NS_IMPL_COCOA
5848 maximizing_resize
= NO
;
5851 win
= [[EmacsWindow alloc
]
5852 initWithContentRect
: r
5853 styleMask
: (NSResizableWindowMask |
5854 #
if MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_7
5855 NSTitledWindowMask |
5857 NSMiniaturizableWindowMask |
5858 NSClosableWindowMask
)
5859 backing
: NSBackingStoreBuffered
5862 #ifdef HAVE_NATIVE_FS
5863 [win setCollectionBehavior
:NSWindowCollectionBehaviorFullScreenPrimary
];
5867 bwidth
= f
->border_width
= wr.
size.width
- r.
size.width
;
5868 tibar_height
= FRAME_NS_TITLEBAR_HEIGHT
(f
) = wr.
size.height
- r.
size.height
;
5870 [win setAcceptsMouseMovedEvents
: YES
];
5871 [win setDelegate
: self
];
5872 [win useOptimizedDrawing
: YES
];
5874 sz.width
= FRAME_COLUMN_WIDTH
(f
);
5875 sz.height
= FRAME_LINE_HEIGHT
(f
);
5876 [win setResizeIncrements
: sz
];
5878 [[win contentView
] addSubview
: self
];
5881 [self registerForDraggedTypes
: ns_drag_types
];
5884 name
= [NSString stringWithUTF8String
:
5885 NILP
(tem
) ? "Emacs"
: SSDATA
(tem
)];
5886 [win setTitle
: name
];
5888 /* toolbar support */
5889 toolbar
= [[EmacsToolbar alloc
] initForView
: self withIdentifier
:
5890 [NSString stringWithFormat
: @"Emacs Frame
%d",
5892 [win setToolbar
: toolbar
];
5893 [toolbar setVisible
: NO
];
5894 #ifdef NS_IMPL_COCOA
5896 NSButton
*toggleButton
;
5897 toggleButton
= [win standardWindowButton
: NSWindowToolbarButton
];
5898 [toggleButton setTarget
: self
];
5899 [toggleButton setAction
: @selector
(toggleToolbar
: )];
5902 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
5906 [win setMiniwindowTitle
:
5907 [NSString stringWithUTF8String
: SSDATA
(tem
)]];
5910 NSScreen
*screen
= [win screen
];
5913 [win setFrameTopLeftPoint
: NSMakePoint
5914 (IN_BOUND
(-SCREENMAX
, f
->left_pos
, SCREENMAX
),
5915 IN_BOUND
(-SCREENMAX
,
5916 [screen frame
].
size.height
- NS_TOP_POS
(f
), SCREENMAX
))];
5919 [win makeFirstResponder
: self
];
5921 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
5922 (FRAME_DEFAULT_FACE
(emacsframe
)), emacsframe
);
5923 [win setBackgroundColor
: col
];
5924 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
5925 [win setOpaque
: NO
];
5927 [self allocateGState
];
5929 [NSApp registerServicesMenuSendTypes
: ns_send_types
5937 - (void
)windowDidMove
: sender
5939 NSWindow
*win
= [self window
];
5940 NSRect r
= [win frame
];
5941 NSArray
*screens
= [NSScreen screens
];
5942 NSScreen
*screen
= [screens objectAtIndex
: 0];
5944 NSTRACE
(windowDidMove
);
5946 if (!emacsframe
->output_data.ns
)
5950 emacsframe
->left_pos
= r.origin.x
;
5951 emacsframe
->top_pos
=
5952 [screen frame
].
size.height
- (r.origin.y
+ r.
size.height
);
5957 /* Called AFTER method below, but before our windowWillResize call there leads
5958 to windowDidResize -> x_set_window_size. Update emacs' notion of frame
5959 location so set_window_size moves the frame. */
5960 - (BOOL
)windowShouldZoom
: (NSWindow
*)sender toFrame
: (NSRect
)newFrame
5962 emacsframe
->output_data.ns
->zooming
= 1;
5967 /* Override to do something slightly nonstandard, but nice. First click on
5968 zoom button will zoom vertically. Second will zoom completely. Third
5969 returns to original. */
5970 - (NSRect
)windowWillUseStandardFrame
:(NSWindow
*)sender
5971 defaultFrame
:(NSRect
)defaultFrame
5973 NSRect result
= [sender frame
];
5975 NSTRACE
(windowWillUseStandardFrame
);
5977 if (fs_before_fs
!= -1) /* Entering fullscreen */
5979 result
= defaultFrame
;
5981 else if (next_maximized
== FULLSCREEN_HEIGHT
5982 ||
(next_maximized
== -1
5983 && abs (defaultFrame.
size.height
- result.
size.height
)
5984 > FRAME_LINE_HEIGHT
(emacsframe
)))
5987 ns_userRect
= result
;
5988 maximized_height
= result.
size.height
= defaultFrame.
size.height
;
5989 maximized_width
= -1;
5990 result.origin.y
= defaultFrame.origin.y
;
5991 [self setFSValue
: FULLSCREEN_HEIGHT
];
5992 #ifdef NS_IMPL_COCOA
5993 maximizing_resize
= YES
;
5996 else if (next_maximized
== FULLSCREEN_WIDTH
)
5998 ns_userRect
= result
;
5999 maximized_width
= result.
size.width
= defaultFrame.
size.width
;
6000 maximized_height
= -1;
6001 result.origin.x
= defaultFrame.origin.x
;
6002 [self setFSValue
: FULLSCREEN_WIDTH
];
6004 else if (next_maximized
== FULLSCREEN_MAXIMIZED
6005 ||
(next_maximized
== -1
6006 && abs (defaultFrame.
size.width
- result.
size.width
)
6007 > FRAME_COLUMN_WIDTH
(emacsframe
)))
6009 result
= defaultFrame
; /* second click */
6010 maximized_width
= result.
size.width
;
6011 maximized_height
= result.
size.height
;
6012 [self setFSValue
: FULLSCREEN_MAXIMIZED
];
6013 #ifdef NS_IMPL_COCOA
6014 maximizing_resize
= YES
;
6020 result
= ns_userRect.
size.height ? ns_userRect
: result
;
6021 ns_userRect
= NSMakeRect
(0, 0, 0, 0);
6022 #ifdef NS_IMPL_COCOA
6023 maximizing_resize
= fs_state
!= FULLSCREEN_NONE
;
6025 [self setFSValue
: FULLSCREEN_NONE
];
6026 maximized_width
= maximized_height
= -1;
6029 if (fs_before_fs
== -1) next_maximized
= -1;
6030 [self windowWillResize
: sender toSize
: result.
size];
6035 - (void
)windowDidDeminiaturize
: sender
6037 NSTRACE
(windowDidDeminiaturize
);
6038 if (!emacsframe
->output_data.ns
)
6041 SET_FRAME_ICONIFIED
(emacsframe
, 0);
6042 SET_FRAME_VISIBLE
(emacsframe
, 1);
6043 windows_or_buffers_changed
++;
6047 emacs_event
->kind
= DEICONIFY_EVENT
;
6048 EV_TRAILER
((id
)nil
);
6053 - (void
)windowDidExpose
: sender
6055 NSTRACE
(windowDidExpose
);
6056 if (!emacsframe
->output_data.ns
)
6059 SET_FRAME_VISIBLE
(emacsframe
, 1);
6060 SET_FRAME_GARBAGED
(emacsframe
);
6062 if (send_appdefined
)
6063 ns_send_appdefined
(-1);
6067 - (void
)windowDidMiniaturize
: sender
6069 NSTRACE
(windowDidMiniaturize
);
6070 if (!emacsframe
->output_data.ns
)
6073 SET_FRAME_ICONIFIED
(emacsframe
, 1);
6074 SET_FRAME_VISIBLE
(emacsframe
, 0);
6078 emacs_event
->kind
= ICONIFY_EVENT
;
6079 EV_TRAILER
((id
)nil
);
6083 #ifdef HAVE_NATIVE_FS
6084 - (NSApplicationPresentationOptions
)window
:(NSWindow
*)window
6085 willUseFullScreenPresentationOptions
:
6086 (NSApplicationPresentationOptions
)proposedOptions
6088 return proposedOptions|NSApplicationPresentationAutoHideToolbar
;
6092 - (void
)windowWillEnterFullScreen
:(NSNotification
*)notification
6094 fs_before_fs
= fs_state
;
6097 - (void
)windowDidEnterFullScreen
:(NSNotification
*)notification
6099 [self setFSValue
: FULLSCREEN_BOTH
];
6100 if (! [self fsIsNative
])
6102 [self windowDidBecomeKey
:notification
];
6103 [nonfs_window orderOut
:self
];
6105 else if (! FRAME_EXTERNAL_TOOL_BAR
(emacsframe
))
6106 [toolbar setVisible
:NO
];
6109 - (void
)windowWillExitFullScreen
:(NSNotification
*)notification
6111 if (next_maximized
!= -1)
6112 fs_before_fs
= next_maximized
;
6115 - (void
)windowDidExitFullScreen
:(NSNotification
*)notification
6117 [self setFSValue
: fs_before_fs
];
6119 #ifdef NS_IMPL_COCOA
6120 [self updateCollectionBehaviour
];
6122 if (FRAME_EXTERNAL_TOOL_BAR
(emacsframe
))
6124 [toolbar setVisible
:YES
];
6125 update_frame_tool_bar
(emacsframe
);
6126 [self updateFrameSize
:YES
];
6127 [[self window
] display
];
6130 [toolbar setVisible
:NO
];
6132 if (next_maximized
!= -1)
6133 [[self window
] performZoom
:self
];
6138 return fs_is_native
;
6141 - (BOOL
)isFullscreen
6143 if (! fs_is_native
) return nonfs_window
!= nil
;
6144 #ifdef HAVE_NATIVE_FS
6145 return ([[self window
] styleMask
] & NSFullScreenWindowMask
) != 0;
6151 #ifdef HAVE_NATIVE_FS
6152 - (void
)updateCollectionBehaviour
6154 if (! [self isFullscreen
])
6156 NSWindow
*win
= [self window
];
6157 NSWindowCollectionBehavior b
= [win collectionBehavior
];
6158 if (ns_use_native_fullscreen
)
6159 b |
= NSWindowCollectionBehaviorFullScreenPrimary
;
6161 b
&= ~NSWindowCollectionBehaviorFullScreenPrimary
;
6163 [win setCollectionBehavior
: b
];
6164 fs_is_native
= ns_use_native_fullscreen
;
6169 - (void
)toggleFullScreen
: (id
)sender
6180 #ifdef NS_IMPL_COCOA
6181 [[self window
] toggleFullScreen
:sender
];
6187 onFirstScreen
= [[w screen
] isEqual:[[NSScreen screens
] objectAtIndex
:0]];
6190 col
= ns_lookup_indexed_color
(NS_FACE_BACKGROUND
6191 (FRAME_DEFAULT_FACE
(f
)),
6194 sz.width
= FRAME_COLUMN_WIDTH
(f
);
6195 sz.height
= FRAME_LINE_HEIGHT
(f
);
6197 if (fs_state
!= FULLSCREEN_BOTH
)
6199 /* Hide dock and menubar if we are on the primary screen. */
6202 #
if defined
(NS_IMPL_COCOA
) && \
6203 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
6204 NSApplicationPresentationOptions options
6205 = NSApplicationPresentationAutoHideDock
6206 | NSApplicationPresentationAutoHideMenuBar
;
6208 [NSApp setPresentationOptions
: options
];
6210 [NSMenu setMenuBarVisible
:NO
];
6214 fw
= [[EmacsFSWindow alloc
]
6215 initWithContentRect
:[w contentRectForFrameRect
:wr
]
6216 styleMask
:NSBorderlessWindowMask
6217 backing
:NSBackingStoreBuffered
6221 [fw setContentView
:[w contentView
]];
6222 [fw setTitle
:[w
title]];
6223 [fw setDelegate
:self
];
6224 [fw setAcceptsMouseMovedEvents
: YES
];
6225 [fw useOptimizedDrawing
: YES
];
6226 [fw setResizeIncrements
: sz
];
6227 [fw setBackgroundColor
: col
];
6228 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
6231 f
->border_width
= 0;
6232 FRAME_NS_TITLEBAR_HEIGHT
(f
) = 0;
6233 tobar_height
= FRAME_TOOLBAR_HEIGHT
(f
);
6234 FRAME_TOOLBAR_HEIGHT
(f
) = 0;
6238 [self windowWillEnterFullScreen
:nil
];
6239 [fw makeKeyAndOrderFront
:NSApp
];
6240 [fw makeFirstResponder
:self
];
6242 r
= [fw frameRectForContentRect
:[[fw screen
] frame
]];
6243 [fw setFrame
: r display
:YES animate
:YES
];
6244 [self windowDidEnterFullScreen
:nil
];
6255 #
if defined
(NS_IMPL_COCOA
) && \
6256 MAC_OS_X_VERSION_MAX_ALLOWED
>= MAC_OS_X_VERSION_10_6
6257 [NSApp setPresentationOptions
: NSApplicationPresentationDefault
];
6259 [NSMenu setMenuBarVisible
:YES
];
6263 [w setContentView
:[fw contentView
]];
6264 [w setResizeIncrements
: sz
];
6265 [w setBackgroundColor
: col
];
6266 if ([col alphaComponent
] != (EmacsCGFloat
) 1.0)
6269 f
->border_width
= bwidth
;
6270 FRAME_NS_TITLEBAR_HEIGHT
(f
) = tibar_height
;
6271 if (FRAME_EXTERNAL_TOOL_BAR
(f
))
6272 FRAME_TOOLBAR_HEIGHT
(f
) = tobar_height
;
6274 [self windowWillExitFullScreen
:nil
];
6275 [fw setFrame
: [w frame
] display
:YES animate
:YES
];
6277 [w makeKeyAndOrderFront
:NSApp
];
6278 [self windowDidExitFullScreen
:nil
];
6279 [self updateFrameSize
:YES
];
6285 if (fs_state
!= emacsframe
->want_fullscreen
)
6287 if (fs_state
== FULLSCREEN_BOTH
)
6289 [self toggleFullScreen
:self
];
6292 switch (emacsframe
->want_fullscreen
)
6294 case FULLSCREEN_BOTH
:
6295 [self toggleFullScreen
:self
];
6297 case FULLSCREEN_WIDTH
:
6298 next_maximized
= FULLSCREEN_WIDTH
;
6299 if (fs_state
!= FULLSCREEN_BOTH
)
6300 [[self window
] performZoom
:self
];
6302 case FULLSCREEN_HEIGHT
:
6303 next_maximized
= FULLSCREEN_HEIGHT
;
6304 if (fs_state
!= FULLSCREEN_BOTH
)
6305 [[self window
] performZoom
:self
];
6307 case FULLSCREEN_MAXIMIZED
:
6308 next_maximized
= FULLSCREEN_MAXIMIZED
;
6309 if (fs_state
!= FULLSCREEN_BOTH
)
6310 [[self window
] performZoom
:self
];
6312 case FULLSCREEN_NONE
:
6313 if (fs_state
!= FULLSCREEN_BOTH
)
6315 next_maximized
= FULLSCREEN_NONE
;
6316 [[self window
] performZoom
:self
];
6321 emacsframe
->want_fullscreen
= FULLSCREEN_NONE
;
6326 - (void
) setFSValue
: (int
)value
6328 Lisp_Object lval
= Qnil
;
6331 case FULLSCREEN_BOTH
:
6334 case FULLSCREEN_WIDTH
:
6337 case FULLSCREEN_HEIGHT
:
6340 case FULLSCREEN_MAXIMIZED
:
6344 store_frame_param
(emacsframe
, Qfullscreen
, lval
);
6348 - (void
)mouseEntered
: (NSEvent
*)theEvent
6350 NSTRACE
(mouseEntered
);
6351 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
6355 - (void
)mouseExited
: (NSEvent
*)theEvent
6357 Mouse_HLInfo
*hlinfo
= emacsframe ? MOUSE_HL_INFO
(emacsframe
) : NULL;
6359 NSTRACE
(mouseExited
);
6364 last_mouse_movement_time
= EV_TIMESTAMP
(theEvent
);
6366 if (emacsframe
== hlinfo
->mouse_face_mouse_frame
)
6368 clear_mouse_face
(hlinfo
);
6369 hlinfo
->mouse_face_mouse_frame
= 0;
6377 if (context_menu_value
== -1)
6378 context_menu_value
= [sender tag
];
6381 NSInteger tag
= [sender tag
];
6382 find_and_call_menu_selection
(emacsframe
, emacsframe
->menu_bar_items_used
,
6383 emacsframe
->menu_bar_vector
,
6387 ns_send_appdefined
(-1);
6392 - (EmacsToolbar
*)toolbar
6398 /* this gets called on toolbar button click */
6399 - toolbarClicked
: (id
)item
6402 int idx
= [item tag
] * TOOL_BAR_ITEM_NSLOTS
;
6404 NSTRACE
(toolbarClicked
);
6409 /* send first event (for some reason two needed) */
6410 theEvent
= [[self window
] currentEvent
];
6411 emacs_event
->kind
= TOOL_BAR_EVENT
;
6412 XSETFRAME
(emacs_event
->arg
, emacsframe
);
6413 EV_TRAILER
(theEvent
);
6415 emacs_event
->kind
= TOOL_BAR_EVENT
;
6416 /* XSETINT (emacs_event->code, 0); */
6417 emacs_event
->arg
= AREF
(emacsframe
->tool_bar_items
,
6418 idx
+ TOOL_BAR_ITEM_KEY
);
6419 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6420 EV_TRAILER
(theEvent
);
6425 - toggleToolbar
: (id
)sender
6430 emacs_event
->kind
= NS_NONKEY_EVENT
;
6431 emacs_event
->code
= KEY_NS_TOGGLE_TOOLBAR
;
6432 EV_TRAILER
((id
)nil
);
6437 - (void
)drawRect
: (NSRect
)rect
6439 int x
= NSMinX
(rect
), y
= NSMinY
(rect
);
6440 int width
= NSWidth
(rect
), height
= NSHeight
(rect
);
6444 if (!emacsframe ||
!emacsframe
->output_data.ns
)
6447 ns_clear_frame_area
(emacsframe
, x
, y
, width
, height
);
6448 expose_frame
(emacsframe
, x
, y
, width
, height
);
6451 drawRect: may be called (at least in OS X 10.5) for invisible
6452 views as well for some reason. Thus, do not infer visibility
6455 emacsframe->async_visible = 1;
6456 emacsframe->async_iconified = 0;
6461 /* NSDraggingDestination protocol methods. Actually this is not really a
6462 protocol, but a category of Object. O well... */
6464 -(NSUInteger
) draggingEntered
: (id
<NSDraggingInfo
>) sender
6466 NSTRACE
(draggingEntered
);
6467 return NSDragOperationGeneric
;
6471 -(BOOL
)prepareForDragOperation
: (id
<NSDraggingInfo
>) sender
6477 -(BOOL
)performDragOperation
: (id
<NSDraggingInfo
>) sender
6482 NSEvent
*theEvent
= [[self window
] currentEvent
];
6485 NSTRACE
(performDragOperation
);
6490 position
= [self convertPoint
: [sender draggingLocation
] fromView
: nil
];
6491 x
= lrint
(position.x
); y
= lrint
(position.y
);
6493 pb
= [sender draggingPasteboard
];
6494 type = [pb availableTypeFromArray
: ns_drag_types
];
6499 else if ([type isEqualToString
: NSFilenamesPboardType
])
6502 NSEnumerator
*fenum
;
6505 if (!(files
= [pb propertyListForType
: type]))
6508 fenum
= [files objectEnumerator
];
6509 while ( (file
= [fenum nextObject
]) )
6511 emacs_event
->kind
= NS_NONKEY_EVENT
;
6512 emacs_event
->code
= KEY_NS_DRAG_FILE
;
6513 XSETINT
(emacs_event
->x
, x
);
6514 XSETINT
(emacs_event
->y
, y
);
6515 ns_input_file
= append2
(ns_input_file
,
6516 build_string
([file UTF8String
]));
6517 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6518 EV_TRAILER
(theEvent
);
6522 else if ([type isEqualToString
: NSURLPboardType
])
6527 if (!(fileURL
= [NSURL URLFromPasteboard
: pb
]) ||
6528 [fileURL isFileURL
] == NO
)
6531 file
= [fileURL
path];
6532 emacs_event
->kind
= NS_NONKEY_EVENT
;
6533 emacs_event
->code
= KEY_NS_DRAG_FILE
;
6534 XSETINT
(emacs_event
->x
, x
);
6535 XSETINT
(emacs_event
->y
, y
);
6536 ns_input_file
= append2
(ns_input_file
, build_string
([file UTF8String
]));
6537 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6538 EV_TRAILER
(theEvent
);
6541 else if ([type isEqualToString
: NSStringPboardType
]
6542 ||
[type isEqualToString
: NSTabularTextPboardType
])
6546 if (! (data
= [pb stringForType
: type]))
6549 emacs_event
->kind
= NS_NONKEY_EVENT
;
6550 emacs_event
->code
= KEY_NS_DRAG_TEXT
;
6551 XSETINT
(emacs_event
->x
, x
);
6552 XSETINT
(emacs_event
->y
, y
);
6553 ns_input_text
= build_string
([data UTF8String
]);
6554 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6555 EV_TRAILER
(theEvent
);
6558 else if ([type isEqualToString
: NSColorPboardType
])
6560 NSColor
*c
= [NSColor colorFromPasteboard
: pb
];
6561 emacs_event
->kind
= NS_NONKEY_EVENT
;
6562 emacs_event
->code
= KEY_NS_DRAG_COLOR
;
6563 XSETINT
(emacs_event
->x
, x
);
6564 XSETINT
(emacs_event
->y
, y
);
6565 ns_input_color
= ns_color_to_lisp
(c
);
6566 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6567 EV_TRAILER
(theEvent
);
6570 else if ([type isEqualToString
: NSFontPboardType
])
6572 /* impl based on GNUstep NSTextView.m */
6573 NSData
*data
= [pb dataForType
: NSFontPboardType
];
6574 NSDictionary
*dict
= [NSUnarchiver unarchiveObjectWithData
: data
];
6575 NSFont
*font
= [dict objectForKey
: NSFontAttributeName
];
6581 emacs_event
->kind
= NS_NONKEY_EVENT
;
6582 emacs_event
->code
= KEY_NS_CHANGE_FONT
;
6583 XSETINT
(emacs_event
->x
, x
);
6584 XSETINT
(emacs_event
->y
, y
);
6585 ns_input_font
= build_string
([[font fontName
] UTF8String
]);
6586 snprintf
(fontSize
, 10, "
%f", [font pointSize]);
6587 ns_input_fontsize
= build_string
(fontSize
);
6588 emacs_event
->modifiers
= EV_MODIFIERS
(theEvent
);
6589 EV_TRAILER
(theEvent
);
6594 error ("Invalid data
type in dragging pasteboard."
);
6600 - (id
) validRequestorForSendType
: (NSString
*)typeSent
6601 returnType
: (NSString
*)typeReturned
6603 NSTRACE
(validRequestorForSendType
);
6604 if (typeSent
!= nil
&& [ns_send_types indexOfObject
: typeSent
] != NSNotFound
6605 && typeReturned
== nil
)
6607 if (! NILP
(ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
)))
6611 return [super validRequestorForSendType
: typeSent
6612 returnType
: typeReturned
];
6616 /* The next two methods are part of NSServicesRequests informal protocol,
6617 supposedly called when a services menu item is chosen from this app.
6618 But this should not happen because we override the services menu with our
6619 own entries which call ns-perform-service.
6620 Nonetheless, it appeared to happen (under strange circumstances): bug#1435.
6621 So let's at least stub them out until further investigation can be done. */
6623 - (BOOL
) readSelectionFromPasteboard
: (NSPasteboard
*)pb
6625 /* we could call ns_string_from_pasteboard(pboard) here but then it should
6626 be written into the buffer in place of the existing selection..
6627 ordinary service calls go through functions defined in ns-win.el */
6631 - (BOOL
) writeSelectionToPasteboard
: (NSPasteboard
*)pb types
: (NSArray
*)types
6633 NSArray
*typesDeclared
;
6636 /* We only support NSStringPboardType */
6637 if ([types containsObject
:NSStringPboardType
] == NO
) {
6641 val
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6642 if (CONSP
(val
) && SYMBOLP
(XCAR
(val
)))
6645 if (CONSP
(val
) && NILP
(XCDR
(val
)))
6648 if (! STRINGP
(val
))
6651 typesDeclared
= [NSArray arrayWithObject
:NSStringPboardType
];
6652 [pb declareTypes
:typesDeclared owner
:nil
];
6653 ns_string_to_pasteboard
(pb
, val
);
6658 /* setMini =YES means set from internal (gives a finder icon), NO means set nil
6659 (gives a miniaturized version of the window); currently we use the latter for
6660 frames whose active buffer doesn't correspond to any file
6661 (e.g., '*scratch*') */
6662 - setMiniwindowImage
: (BOOL
) setMini
6664 id
image = [[self window
] miniwindowImage
];
6665 NSTRACE
(setMiniwindowImage
);
6667 /* NOTE: under Cocoa miniwindowImage always returns nil, documentation
6668 about "AppleDockIconEnabled" notwithstanding, however the set message
6669 below has its effect nonetheless. */
6670 if (image != emacsframe
->output_data.ns
->miniimage
)
6672 if (image && [image isKindOfClass
: [EmacsImage
class]])
6674 [[self window
] setMiniwindowImage
:
6675 setMini ? emacsframe
->output_data.ns
->miniimage
: nil
];
6682 - (void
) setRows
: (int
) r andColumns
: (int
) c
6688 @
end /* EmacsView */
6692 /* ==========================================================================
6694 EmacsWindow implementation
6696 ========================================================================== */
6698 @implementation EmacsWindow
6700 #ifdef NS_IMPL_COCOA
6701 - (id
)accessibilityAttributeValue
:(NSString
*)attribute
6703 Lisp_Object str
= Qnil
;
6704 struct frame
*f
= SELECTED_FRAME
();
6705 struct buffer
*curbuf
= XBUFFER
(XWINDOW
(f
->selected_window
)->contents
);
6707 if ([attribute isEqualToString
:NSAccessibilityRoleAttribute
])
6708 return NSAccessibilityTextFieldRole
;
6710 if ([attribute isEqualToString
:NSAccessibilitySelectedTextAttribute
]
6711 && curbuf
&& ! NILP
(BVAR
(curbuf
, mark_active
)))
6713 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6715 else if (curbuf
&& [attribute isEqualToString
:NSAccessibilityValueAttribute
])
6717 if (! NILP
(BVAR
(curbuf
, mark_active
)))
6718 str
= ns_get_local_selection
(QPRIMARY
, QUTF8_STRING
);
6722 ptrdiff_t start_byte
= BUF_BEGV_BYTE
(curbuf
);
6723 ptrdiff_t byte_range
= BUF_ZV_BYTE
(curbuf
) - start_byte
;
6724 ptrdiff_t range
= BUF_ZV
(curbuf
) - BUF_BEGV
(curbuf
);
6726 if (! NILP
(BVAR
(curbuf
, enable_multibyte_characters
)))
6727 str
= make_uninit_multibyte_string
(range
, byte_range
);
6729 str
= make_uninit_string
(range
);
6730 /* To check: This returns emacs-utf-8, which is a superset of utf-8.
6731 Is this a problem? */
6732 memcpy
(SDATA
(str
), BYTE_POS_ADDR
(start_byte
), byte_range
);
6739 if (CONSP
(str
) && SYMBOLP
(XCAR
(str
)))
6742 if (CONSP
(str
) && NILP
(XCDR
(str
)))
6747 const
char *utfStr
= SSDATA
(str
);
6748 NSString
*nsStr
= [NSString stringWithUTF8String
: utfStr
];
6753 return [super accessibilityAttributeValue
:attribute
];
6755 #endif
/* NS_IMPL_COCOA */
6757 /* If we have multiple monitors, one above the other, we don't want to
6758 restrict the height to just one monitor. So we override this. */
6759 - (NSRect
)constrainFrameRect
:(NSRect
)frameRect toScreen
:(NSScreen
*)screen
6761 /* When making the frame visible for the first time or if there is just
6762 one screen, we want to constrain. Other times not. */
6763 NSUInteger nr_screens
= [[NSScreen screens
] count
];
6764 struct frame
*f
= ((EmacsView
*)[self delegate
])->emacsframe
;
6765 NSTRACE
(constrainFrameRect
);
6767 if (nr_screens
== 1)
6769 NSRect r
= [super constrainFrameRect
:frameRect toScreen
:screen
];
6773 if (f
->output_data.ns
->dont_constrain
6774 || ns_menu_bar_should_be_hidden
())
6777 f
->output_data.ns
->dont_constrain
= 1;
6778 return [super constrainFrameRect
:frameRect toScreen
:screen
];
6781 @
end /* EmacsWindow */
6784 @implementation EmacsFSWindow
6786 - (BOOL
)canBecomeKeyWindow
6791 - (BOOL
)canBecomeMainWindow
6798 /* ==========================================================================
6800 EmacsScroller implementation
6802 ========================================================================== */
6805 @implementation EmacsScroller
6807 /* for repeat button push */
6808 #define SCROLL_BAR_FIRST_DELAY
0.5
6809 #define SCROLL_BAR_CONTINUOUS_DELAY
(1.0 / 15)
6811 + (CGFloat
) scrollerWidth
6813 /* TODO: if we want to allow variable widths, this is the place to do it,
6814 however neither GNUstep nor Cocoa support it very well */
6815 return [NSScroller scrollerWidth
];
6819 - initFrame
: (NSRect
)r window
: (Lisp_Object
)nwin
6821 NSTRACE
(EmacsScroller_initFrame
);
6823 r.
size.width
= [EmacsScroller scrollerWidth
];
6824 [super initWithFrame
: r
/*NSMakeRect (0, 0, 0, 0)*/];
6825 [self setContinuous
: YES
];
6826 [self setEnabled
: YES
];
6828 /* Ensure auto resizing of scrollbars occurs within the emacs frame's view
6829 locked against the top and bottom edges, and right edge on OS X, where
6830 scrollers are on right. */
6831 #ifdef NS_IMPL_GNUSTEP
6832 [self setAutoresizingMask
: NSViewMaxXMargin | NSViewHeightSizable
];
6834 [self setAutoresizingMask
: NSViewMinXMargin | NSViewHeightSizable
];
6839 pixel_height
= NSHeight
(r
);
6840 if (pixel_height
== 0) pixel_height
= 1;
6841 min_portion
= 20 / pixel_height
;
6843 frame
= XFRAME
(XWINDOW
(win
)->frame
);
6844 if (FRAME_LIVE_P
(frame
))
6847 EmacsView
*view = FRAME_NS_VIEW
(frame
);
6848 NSView
*sview
= [[view window
] contentView
];
6849 NSArray
*subs
= [sview subviews
];
6851 /* disable optimization stopping redraw of other scrollbars */
6852 view->scrollbarsNeedingUpdate
= 0;
6853 for (i
=[subs count
]-1; i
>= 0; i
--)
6854 if ([[subs objectAtIndex
: i
] isKindOfClass
: [EmacsScroller
class]])
6855 view->scrollbarsNeedingUpdate
++;
6856 [sview addSubview
: self
];
6859 /* [self setFrame: r]; */
6865 - (void
)setFrame
: (NSRect
)newRect
6867 NSTRACE
(EmacsScroller_setFrame
);
6868 /* block_input (); */
6869 pixel_height
= NSHeight
(newRect
);
6870 if (pixel_height
== 0) pixel_height
= 1;
6871 min_portion
= 20 / pixel_height
;
6872 [super setFrame
: newRect
];
6874 /* unblock_input (); */
6880 NSTRACE
(EmacsScroller_dealloc
);
6882 wset_vertical_scroll_bar
(XWINDOW
(win
), Qnil
);
6910 /* ensure other scrollbar updates after deletion */
6911 view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
6913 view->scrollbarsNeedingUpdate
++;
6914 [self removeFromSuperview
];
6922 - (void
)resetCursorRects
6924 NSRect visible
= [self visibleRect
];
6925 NSTRACE
(resetCursorRects
);
6927 if (!NSIsEmptyRect
(visible
))
6928 [self addCursorRect
: visible cursor
: [NSCursor arrowCursor
]];
6929 [[NSCursor arrowCursor
] setOnMouseEntered
: YES
];
6933 - (int
) checkSamePosition
: (int
) position portion
: (int
) portion
6936 return em_position
==position
&& em_portion
==portion
&& em_whole
==whole
6937 && portion
!= whole
; /* needed for resize empty buf */
6941 - setPosition
: (int
)position portion
: (int
)portion whole
: (int
)whole
6943 NSTRACE
(setPosition
);
6945 em_position
= position
;
6946 em_portion
= portion
;
6949 if (portion
>= whole
)
6951 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
6952 [self setKnobProportion
: 1.0];
6953 [self setDoubleValue
: 1.0];
6955 [self setFloatValue
: 0.0 knobProportion
: 1.0];
6962 portion
= max ((float
)whole
*min_portion
/pixel_height
, portion
);
6963 pos
= (float
)position
/ (whole
- portion
);
6964 por
= (CGFloat
)portion
/whole
;
6965 #
if defined
(NS_IMPL_COCOA
) && MAC_OS_X_VERSION_MAX_ALLOWED
> MAC_OS_X_VERSION_10_5
6966 [self setKnobProportion
: por
];
6967 [self setDoubleValue
: pos
];
6969 [self setFloatValue
: pos knobProportion
: por
];
6973 /* Events may come here even if the event loop is not running.
6974 If we don't enter the event loop, the scroll bar will not update.
6975 So send SIGIO to ourselves. */
6976 if (apploopnr
== 0) raise
(SIGIO
);
6981 /* FIXME: unused at moment (see ns_mouse_position) at the moment because
6982 drag events will go directly to the EmacsScroller. Leaving in for now. */
6983 -(void
)getMouseMotionPart
: (int
*)part window
: (Lisp_Object
*)window
6984 x
: (Lisp_Object
*)x y
: ( Lisp_Object
*)y
6986 *part
= last_hit_part
;
6988 XSETINT
(*y
, pixel_height
);
6989 if ([self floatValue
] > 0.999F
)
6990 XSETINT
(*x
, pixel_height
);
6992 XSETINT
(*x
, pixel_height
* [self floatValue
]);
6996 /* set up emacs_event */
6997 - (void
) sendScrollEventAtLoc
: (float
)loc fromEvent
: (NSEvent
*)e
7002 emacs_event
->part
= last_hit_part
;
7003 emacs_event
->code
= 0;
7004 emacs_event
->modifiers
= EV_MODIFIERS
(e
) | down_modifier
;
7005 emacs_event
->frame_or_window
= win
;
7006 emacs_event
->timestamp
= EV_TIMESTAMP
(e
);
7007 emacs_event
->kind
= SCROLL_BAR_CLICK_EVENT
;
7008 emacs_event
->arg
= Qnil
;
7009 XSETINT
(emacs_event
->x
, loc
* pixel_height
);
7010 XSETINT
(emacs_event
->y
, pixel_height
-20);
7014 n_emacs_events_pending
++;
7015 kbd_buffer_store_event_hold
(emacs_event
, q_event_ptr
);
7018 hold_event
(emacs_event
);
7019 EVENT_INIT
(*emacs_event
);
7020 ns_send_appdefined
(-1);
7024 /* called manually thru timer to implement repeated button action w/hold-down */
7025 - repeatScroll
: (NSTimer
*)scrollEntry
7027 NSEvent
*e
= [[self window
] currentEvent
];
7028 NSPoint p
= [[self window
] mouseLocationOutsideOfEventStream
];
7029 BOOL inKnob
= [self testPart
: p
] == NSScrollerKnob
;
7031 /* clear timer if need be */
7032 if (inKnob ||
[scroll_repeat_entry timeInterval
] == SCROLL_BAR_FIRST_DELAY
)
7034 [scroll_repeat_entry invalidate
];
7035 [scroll_repeat_entry release
];
7036 scroll_repeat_entry
= nil
;
7042 = [[NSTimer scheduledTimerWithTimeInterval
:
7043 SCROLL_BAR_CONTINUOUS_DELAY
7045 selector
: @selector
(repeatScroll
:)
7051 [self sendScrollEventAtLoc
: 0 fromEvent
: e
];
7056 /* Asynchronous mouse tracking for scroller. This allows us to dispatch
7057 mouseDragged events without going into a modal loop. */
7058 - (void
)mouseDown
: (NSEvent
*)e
7061 /* hitPart is only updated AFTER event is passed on */
7062 NSScrollerPart part
= [self testPart
: [e locationInWindow
]];
7063 CGFloat inc
= 0.0, loc
, kloc
, pos
;
7066 NSTRACE
(EmacsScroller_mouseDown
);
7070 case NSScrollerDecrementPage
:
7071 last_hit_part
= scroll_bar_above_handle
; inc
= -1.0; break;
7072 case NSScrollerIncrementPage
:
7073 last_hit_part
= scroll_bar_below_handle
; inc
= 1.0; break;
7074 case NSScrollerDecrementLine
:
7075 last_hit_part
= scroll_bar_up_arrow
; inc
= -0.1; break;
7076 case NSScrollerIncrementLine
:
7077 last_hit_part
= scroll_bar_down_arrow
; inc
= 0.1; break;
7078 case NSScrollerKnob
:
7079 last_hit_part
= scroll_bar_handle
; break;
7080 case NSScrollerKnobSlot
: /* GNUstep-only */
7081 last_hit_part
= scroll_bar_move_ratio
; break;
7082 default
: /* NSScrollerNoPart? */
7083 fprintf (stderr
, "EmacsScoller
-mouseDown
: unexpected part
%ld\n",
7090 pos
= 0; /* ignored */
7092 /* set a timer to repeat, as we can't let superclass do this modally */
7094 = [[NSTimer scheduledTimerWithTimeInterval
: SCROLL_BAR_FIRST_DELAY
7096 selector
: @selector
(repeatScroll
:)
7103 /* handle, or on GNUstep possibly slot */
7104 NSEvent
*fake_event
;
7106 /* compute float loc in slot and mouse offset on knob */
7107 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7109 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7115 else if (loc
>= NSHeight
(sr
))
7117 loc
= NSHeight
(sr
);
7125 kr
= [self convertRect
: [self rectForPart
: NSScrollerKnob
]
7127 kloc
= NSHeight
(kr
) - ([e locationInWindow
].y
- NSMinY
(kr
));
7129 last_mouse_offset
= kloc
;
7131 /* if knob, tell emacs a location offset by knob pos
7132 (to indicate top of handle) */
7133 if (part
== NSScrollerKnob
)
7134 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7136 /* else this is a slot click on GNUstep: go straight there */
7137 pos
= loc
/ NSHeight
(sr
);
7139 /* send a fake mouse-up to super to preempt modal -trackKnob: mode */
7140 fake_event
= [NSEvent mouseEventWithType
: NSLeftMouseUp
7141 location
: [e locationInWindow
]
7142 modifierFlags
: [e modifierFlags
]
7143 timestamp
: [e timestamp
]
7144 windowNumber
: [e windowNumber
]
7145 context
: [e context
]
7146 eventNumber
: [e eventNumber
]
7147 clickCount
: [e clickCount
]
7148 pressure
: [e pressure
]];
7149 [super mouseUp
: fake_event
];
7152 if (part
!= NSScrollerKnob
)
7153 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7157 /* Called as we manually track scroller drags, rather than superclass. */
7158 - (void
)mouseDragged
: (NSEvent
*)e
7163 NSTRACE
(EmacsScroller_mouseDragged
);
7165 sr
= [self convertRect
: [self rectForPart
: NSScrollerKnobSlot
]
7167 loc
= NSHeight
(sr
) - ([e locationInWindow
].y
- NSMinY
(sr
));
7173 else if (loc
>= NSHeight
(sr
) + last_mouse_offset
)
7175 loc
= NSHeight
(sr
) + last_mouse_offset
;
7178 pos
= (loc
- last_mouse_offset
) / NSHeight
(sr
);
7179 [self sendScrollEventAtLoc
: pos fromEvent
: e
];
7183 - (void
)mouseUp
: (NSEvent
*)e
7185 if (scroll_repeat_entry
)
7187 [scroll_repeat_entry invalidate
];
7188 [scroll_repeat_entry release
];
7189 scroll_repeat_entry
= nil
;
7195 /* treat scrollwheel events in the bar as though they were in the main window */
7196 - (void
) scrollWheel
: (NSEvent
*)theEvent
7198 EmacsView
*view = (EmacsView
*)FRAME_NS_VIEW
(frame
);
7199 [view mouseDown
: theEvent
];
7202 @
end /* EmacsScroller */
7205 #ifdef NS_IMPL_GNUSTEP
7206 /* Dummy class to get rid of startup warnings. */
7207 @implementation EmacsDocument
7213 /* ==========================================================================
7215 Font-related functions; these used to be in nsfaces.m
7217 ========================================================================== */
7221 x_new_font
(struct frame
*f
, Lisp_Object font_object
, int fontset
)
7223 struct font
*font
= XFONT_OBJECT
(font_object
);
7226 fontset
= fontset_from_font
(font_object
);
7227 FRAME_FONTSET
(f
) = fontset
;
7229 if (FRAME_FONT
(f
) == font
)
7230 /* This font is already set in frame F. There's nothing more to
7234 FRAME_FONT
(f
) = font
;
7236 FRAME_BASELINE_OFFSET
(f
) = font
->baseline_offset
;
7237 FRAME_COLUMN_WIDTH
(f
) = font
->average_width
;
7238 FRAME_LINE_HEIGHT
(f
) = font
->height
;
7240 compute_fringe_widths
(f
, 1);
7242 /* Compute the scroll bar width in character columns. */
7243 if (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) > 0)
7245 int wid
= FRAME_COLUMN_WIDTH
(f
);
7246 FRAME_CONFIG_SCROLL_BAR_COLS
(f
)
7247 = (FRAME_CONFIG_SCROLL_BAR_WIDTH
(f
) + wid
- 1) / wid
;
7251 int wid
= FRAME_COLUMN_WIDTH
(f
);
7252 FRAME_CONFIG_SCROLL_BAR_COLS
(f
) = (14 + wid
- 1) / wid
;
7255 /* Now make the frame display the given font. */
7256 if (FRAME_NS_WINDOW
(f
) != 0)
7257 x_set_window_size
(f
, 0, FRAME_COLS
(f
), FRAME_LINES
(f
));
7263 /* XLFD: -foundry-family-weight-slant-swidth-adstyle-pxlsz-ptSz-resx-resy-spc-avgWidth-rgstry-encoding */
7264 /* Note: ns_font_to_xlfd and ns_fontname_to_xlfd no longer needed, removed
7268 ns_xlfd_to_fontname
(const
char *xlfd
)
7269 /* --------------------------------------------------------------------------
7270 Convert an X font name (XLFD) to an NS font name.
7271 Only family is used.
7272 The string returned is temporarily allocated.
7273 -------------------------------------------------------------------------- */
7275 char *name
= xmalloc
(180);
7279 if (!strncmp
(xlfd
, "
--"
, 2))
7280 sscanf (xlfd
, "
--%*[^-]-%[^-]179-", name);
7282 sscanf (xlfd
, "
-%*[^-]-%[^-]179-", name);
7284 /* stopgap for malformed XLFD input */
7285 if (strlen
(name
) == 0)
7286 strcpy
(name
, "Monaco"
);
7288 /* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
7289 also uppercase after '-' or ' ' */
7290 name
[0] = c_toupper
(name
[0]);
7291 for (len
=strlen
(name
), i
=0; i
<len
; i
++)
7297 name
[i
+1] = c_toupper
(name
[i
+1]);
7299 else if (name
[i
] == '_')
7303 name
[i
+1] = c_toupper
(name
[i
+1]);
7306 /*fprintf (stderr, "converted '%s' to '%s'\n",xlfd,name); */
7307 ret
= [[NSString stringWithUTF8String
: name
] UTF8String
];
7314 syms_of_nsterm
(void
)
7316 NSTRACE
(syms_of_nsterm
);
7318 ns_antialias_threshold
= 10.0;
7320 /* from 23+ we need to tell emacs what modifiers there are.. */
7321 DEFSYM
(Qmodifier_value
, "modifier
-value"
);
7322 DEFSYM
(Qalt
, "alt"
);
7323 DEFSYM
(Qhyper
, "hyper"
);
7324 DEFSYM
(Qmeta
, "
meta"
);
7325 DEFSYM
(Qsuper
, "super"
);
7326 DEFSYM
(Qcontrol
, "control"
);
7327 DEFSYM
(QUTF8_STRING
, "UTF8_STRING"
);
7329 Fput
(Qalt
, Qmodifier_value
, make_number
(alt_modifier
));
7330 Fput
(Qhyper
, Qmodifier_value
, make_number
(hyper_modifier
));
7331 Fput
(Qmeta
, Qmodifier_value
, make_number
(meta_modifier
));
7332 Fput
(Qsuper
, Qmodifier_value
, make_number
(super_modifier
));
7333 Fput
(Qcontrol
, Qmodifier_value
, make_number
(ctrl_modifier
));
7335 DEFVAR_LISP
("ns
-input-file"
, ns_input_file
,
7336 "The file specified in the last NS event."
);
7337 ns_input_file
=Qnil
;
7339 DEFVAR_LISP
("ns
-input-text"
, ns_input_text
,
7340 "The data received in the last NS
text drag event."
);
7341 ns_input_text
=Qnil
;
7343 DEFVAR_LISP
("ns
-working
-text"
, ns_working_text
,
7344 "String
for visualizing working composition sequence."
);
7345 ns_working_text
=Qnil
;
7347 DEFVAR_LISP
("ns
-input-font"
, ns_input_font
,
7348 "The font specified in the last NS event."
);
7349 ns_input_font
=Qnil
;
7351 DEFVAR_LISP
("ns
-input-fontsize"
, ns_input_fontsize
,
7352 "The fontsize specified in the last NS event."
);
7353 ns_input_fontsize
=Qnil
;
7355 DEFVAR_LISP
("ns
-input-line"
, ns_input_line
,
7356 "The
line specified in the last NS event."
);
7357 ns_input_line
=Qnil
;
7359 DEFVAR_LISP
("ns
-input-color"
, ns_input_color
,
7360 "The color specified in the last NS event."
);
7361 ns_input_color
=Qnil
;
7363 DEFVAR_LISP
("ns
-input-spi
-name"
, ns_input_spi_name
,
7364 "The service name specified in the last NS event."
);
7365 ns_input_spi_name
=Qnil
;
7367 DEFVAR_LISP
("ns
-input-spi
-arg"
, ns_input_spi_arg
,
7368 "The service argument specified in the last NS event."
);
7369 ns_input_spi_arg
=Qnil
;
7371 DEFVAR_LISP
("ns
-alternate
-modifier"
, ns_alternate_modifier
,
7372 "This variable describes the behavior of the alternate or option key.
\n\
7373 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7374 Set to none means that the alternate
/ option key is not interpreted by Emacs
\n\
7375 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7376 ns_alternate_modifier
= Qmeta
;
7378 DEFVAR_LISP
("ns
-right
-alternate
-modifier"
, ns_right_alternate_modifier
,
7379 "This variable describes the behavior of the right alternate or option key.
\n\
7380 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7381 Set to left means be the same key as `ns
-alternate
-modifier
'.\n\
7382 Set to none means that the alternate / option key is not interpreted by Emacs\n\
7383 at all, allowing it to be used at a lower level for accented character entry.");
7384 ns_right_alternate_modifier = Qleft;
7386 DEFVAR_LISP ("ns-command-modifier", ns_command_modifier,
7387 "This variable describes the behavior of the command key.\n\
7388 Set to control, meta, alt, super, or hyper means it is taken to be that key.");
7389 ns_command_modifier = Qsuper;
7391 DEFVAR_LISP ("ns-right-command-modifier", ns_right_command_modifier,
7392 "This variable describes the behavior of the right command key.\n\
7393 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7394 Set to left means be the same key as `ns-command-modifier'.
\n\
7395 Set to none means that the command
/ option key is not interpreted by Emacs
\n\
7396 at
all, allowing it to be used at a
lower level
for accented character entry."
);
7397 ns_right_command_modifier
= Qleft
;
7399 DEFVAR_LISP
("ns
-control
-modifier"
, ns_control_modifier
,
7400 "This variable describes the behavior of the control key.
\n\
7401 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key."
);
7402 ns_control_modifier
= Qcontrol
;
7404 DEFVAR_LISP
("ns
-right
-control
-modifier"
, ns_right_control_modifier
,
7405 "This variable describes the behavior of the right control key.
\n\
7406 Set to control
, meta, alt
, super
, or hyper means it is taken to be that key.
\n\
7407 Set to left means be the same key as `ns
-control
-modifier
'.\n\
7408 Set to none means that the control / option key is not interpreted by Emacs\n\
7409 at all, allowing it to be used at a lower level for accented character entry.");
7410 ns_right_control_modifier = Qleft;
7412 DEFVAR_LISP ("ns-function-modifier", ns_function_modifier,
7413 "This variable describes the behavior of the function key (on laptops).\n\
7414 Set to control, meta, alt, super, or hyper means it is taken to be that key.\n\
7415 Set to none means that the function key is not interpreted by Emacs at all,\n\
7416 allowing it to be used at a lower level for accented character entry.");
7417 ns_function_modifier = Qnone;
7419 DEFVAR_LISP ("ns-antialias-text", ns_antialias_text,
7420 "Non-nil (the default) means to render text antialiased.");
7421 ns_antialias_text = Qt;
7423 DEFVAR_LISP ("ns-confirm-quit", ns_confirm_quit,
7424 "Whether to confirm application quit using dialog.");
7425 ns_confirm_quit = Qnil;
7427 staticpro (&ns_display_name_list);
7428 ns_display_name_list = Qnil;
7430 staticpro (&last_mouse_motion_frame);
7431 last_mouse_motion_frame = Qnil;
7433 DEFVAR_LISP ("ns-auto-hide-menu-bar", ns_auto_hide_menu_bar,
7434 doc: /* Non-nil means that the menu bar is hidden, but appears when the mouse is near.
7435 Only works on OSX 10.6 or later. */);
7436 ns_auto_hide_menu_bar = Qnil;
7438 DEFVAR_BOOL ("ns-use-native-fullscreen", ns_use_native_fullscreen,
7439 doc: /*Non-nil means to use native fullscreen on OSX >= 10.7.
7440 Nil means use fullscreen the old (< 10.7) way. The old way works better with
7441 multiple monitors, but lacks tool bar. This variable is ignored on OSX < 10.7.
7442 Default is t for OSX >= 10.7, nil otherwise. */);
7443 #ifdef HAVE_NATIVE_FS
7444 ns_use_native_fullscreen = YES;
7446 ns_use_native_fullscreen = NO;
7448 ns_last_use_native_fullscreen = ns_use_native_fullscreen;
7450 /* TODO: move to common code */
7451 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
7452 doc: /* Which toolkit scroll bars Emacs uses, if any.
7453 A value of nil means Emacs doesn't use toolkit scroll bars.
7454 With the X Window system
, the value is a symbol describing the
7455 X toolkit. Possible values are
: gtk
, motif
, xaw
, or xaw3d.
7456 With MS Windows or Nextstep
, the value is t. */
);
7457 Vx_toolkit_scroll_bars
= Qt
;
7459 DEFVAR_BOOL
("x
-use
-underline
-position
-properties"
,
7460 x_use_underline_position_properties
,
7461 doc: /*Non-nil means make use of UNDERLINE_POSITION font properties.
7462 A value of nil means ignore them. If you encounter fonts with bogus
7463 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
7464 to 4.1, set this to nil. */);
7465 x_use_underline_position_properties
= 0;
7467 DEFVAR_BOOL
("x
-underline
-at
-descent
-line"
,
7468 x_underline_at_descent_line
,
7469 doc: /* Non-nil means to draw the underline at the same place as the descent line.
7470 A value of nil means to draw the underline according to the value of the
7471 variable `x-use-underline-position-properties', which is usually at the
7472 baseline level. The default value is nil. */);
7473 x_underline_at_descent_line
= 0;
7475 /* Tell emacs about this window system. */
7476 Fprovide
(intern
("ns"
), Qnil
);