2
* Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14
* its contributors may be used to endorse or promote products derived
15
* from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
// I don't think this class belongs in WebKit. Lets move it out.
31
// Things that I've never bothered working out:
32
// For non-sheet windows, handle Carbon WindowMove events so as to do the same things as -[NSWindow _windowMoved].
33
// Check to see how this stuff deals with various screen size change scenarious.
34
// M.P. Warning - 9/17/01
36
// There are some invariants I'm maintaining for objects of this class which have been successfully initialized but not deallocated. These all make it easier to not override every single method of NSWindow.
37
// _auxiliaryStorage->auxWFlags.hasShadow will always be false if the Carbon window has a kWindowNoShadowAttribute, and vice versa.
38
// _auxiliaryStorage->_auxWFlags.minimized will always reflect the window's Carbon collapsed state.
39
// _borderView will always point to an NSCarbonWindowFrame.
40
// _contentView will always point to an NSCarbonWindowContentView;
41
// _frame will always reflect the window's Carbon kWindowStructureRgn bounds.
42
// _styleMask will always have _NSCarbonWindowMask set, and will have NSClosableWindowMask, NSMiniaturizableWindowMask, NSResizableWindowMask, and/or NSTitledWindowMask set as appropriate.
43
// _wflags.oneShot and _wflags.delayedOneShot will always be false.
44
// _wFlags.visible will always reflect the window's Carbon visibility.
45
// _windowNum will always be greater than zero, and valid.
46
// The instance variables involved are ones that came to my attention during the initial writing of this class; I haven't methodically gone through NSWindow's ivar list or anything like that. M.P. Notice - 10/10/00
48
// Things that have to be worked on if NSCarbonWindows are ever used for something other than dialogs and sheets:
49
// Clicking on an NSCarbonWindow while a Cocoa app-modal dialog is shown does not beep, as it should [old bug, maybe fixed now].
50
// Handling of mouse clicks or key presses for any window control (close, miniaturize, zoom) might not be all there.
51
// Handling of miniaturization of Carbon windows via title bar double-click might not be all there.
52
// The background on NSCarbonWindowTester's sample window (not sample dialog or sample sheet) might be wrong.
53
// The controls on NSCarbonWindowTester's sample window look inactive when the window is inactive, but have first-click behavior.
54
// M.P. Warning - 12/14/00
56
// Some things would have to be made public if someone wanted to subclass this so as to support more menu item commands. M.P. Warning - 9/19/00
60
#import "CarbonWindowAdapter.h"
62
#import "CarbonWindowFrame.h"
63
#import "CarbonWindowContentView.h"
64
#import "HIViewAdapter.h"
66
#import <WebKitSystemInterface.h>
68
#import <AppKit/AppKit.h>
69
//#import <CoreGraphics/CGSWindow.h>
70
#import <HIToolbox/CarbonEvents.h>
71
#import <HIToolbox/Controls.h>
72
#import <HIToolbox/HIView.h>
75
#import <WebCore/WebCoreObjCExtras.h>
76
#import <WebCore/RunLoop.h>
77
#import <runtime/InitializeThreading.h>
78
#import <wtf/MainThread.h>
80
#import "WebKitLogging.h"
81
#import "WebNSObjectExtras.h"
82
#import "WebTypesInternal.h"
84
@interface NSWindow(HIWebFrameView)
85
- (id)_initContent:(const NSRect *)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag contentView:aView;
86
- (void)_oldPlaceWindow:(NSRect)frameRect;
87
- (void)_windowMovedToRect:(NSRect)actualFrame;
88
- (void)_setWindowNumber:(NSInteger)nativeWindow;
89
- (NSGraphicsContext *)_threadContext;
90
- (void)_setFrame:(NSRect)newWindowFrameRect;
91
- (void)_setVisible:(BOOL)flag;
94
@interface NSApplication(HIWebFrameView)
95
- (void)setIsActive:(BOOL)aFlag;
96
- (id)_setMouseActivationInProgress:(BOOL)flag;
97
- (BOOL)_handleKeyEquivalent:(NSEvent*)theEvent;
100
@interface NSInputContext
101
- (BOOL)processInputKeyBindings:(NSEvent *)event;
104
// Forward declarations.
105
static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData);
107
@implementation CarbonWindowAdapter
110
// Return an appropriate window frame class.
111
+ (Class)frameViewClassForStyleMask:(unsigned int)style {
113
// There's only one appropriate window style, and only one appropriate window frame class.
114
assert(style & WKCarbonWindowMask());
115
return [CarbonWindowFrame class];
120
// Overriding of the parent class' designated initializer, just for safety's sake.
121
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)style backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
123
// Do the standard Cocoa thing.
124
self = [super initWithContentRect:contentRect styleMask:style backing:bufferingType defer:flag];
125
if (self==nil) return nil;
129
_windowRefIsOwned = NO;
130
_eventHandler = NULL;
137
// Given a reference to a Carbon window that is to be encapsulated, an indicator of whether or not this object should take responsibility for disposing of the Carbon window, and an indicator of whether to disable Carbon window ordering, initialize. This is the class' designated initializer.
138
- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned disableOrdering:(BOOL)inDisableOrdering carbon:(BOOL)inCarbon {
140
NSBackingStoreType backingStoreType;
141
CarbonWindowContentView *carbonWindowContentView;
142
NSWindow *windowAsProperty;
144
UInt32 windowFeatures;
145
WindowAttributes windowAttributes;
146
unsigned int styleMask;
148
WindowModality windowModality;
149
ControlRef contentView;
152
// It's very weak to have to put this before the invocation of [super initWithContentRect:...], but -setContentView: is invoked from within that initializer. It turns out that the common admonition about not calling virtual functions from within C++ constructors makes sense in Objective-C too. M.P. Notice - 10/10/00
153
_windowRef = inWindowRef;
154
//_auxiliaryStorage->_windowRef = inWindowRef;
155
_windowRefIsOwned = inWindowRefIsOwned;
158
// Find out the window's CoreGraphics window reference.
159
nativeWindow = WKGetNativeWindowFromWindowRef(inWindowRef);
161
// Find out the window's Carbon window attributes.
162
GetWindowAttributes(inWindowRef, &windowAttributes);
164
// Find out the window's Carbon window features.
165
GetWindowFeatures(inWindowRef, &windowFeatures);
167
// Figure out the window's backing store type.
168
// At one time, this had code stolen from CreatePlatformWindow in HIToolbox/Windows/Platform/CGSPlatform.c
169
// But now the non-retained window class is a Carbon secret that's not even in
170
// WindowsPriv.h; maybe we'll have to revisit this if someone needs to use WebKit
171
// in a non-retained window.
172
backingStoreType = NSBackingStoreRetained;
174
// Figure out the window's style mask.
175
styleMask = WKCarbonWindowMask();
176
if (windowAttributes & kWindowCloseBoxAttribute) styleMask |= NSClosableWindowMask;
177
if (windowAttributes & kWindowResizableAttribute) styleMask |= NSResizableWindowMask;
178
if (windowFeatures & kWindowCanCollapse) styleMask |= NSMiniaturizableWindowMask;
179
if (windowFeatures & kWindowHasTitleBar) styleMask |= NSTitledWindowMask;
181
osStatus = GetWindowModality(_windowRef, &windowModality, NULL);
182
if (osStatus != noErr) {
183
NSLog(@"Couldn't get window modality: error=%ld", osStatus);
187
// Create one of our special content views.
188
carbonWindowContentView = [[[CarbonWindowContentView alloc] init] autorelease];
190
// Do some standard Cocoa initialization. The defer argument's value is YES because we don't want -[NSWindow _commonAwake] to get called. It doesn't appear that any relevant NSWindow code checks _wFlags.deferred, so we should be able to get away with the lie.
191
self = (CarbonWindowAdapter*)[super _initContent:NULL styleMask:styleMask backing:backingStoreType defer:YES contentView:carbonWindowContentView];
192
if (!self) return nil;
193
assert(_contentView);
195
// Record accurately whether or not this window has a shadow, in case someone asks.
196
// _auxiliaryStorage->_auxWFlags.hasShadow = (windowAttributes & kWindowNoShadowAttribute) ? NO : YES;
198
// Record the window number.
199
[self _setWindowNumber:(NSInteger)nativeWindow];
201
// Set up from the frame rectangle.
202
// We didn't even really try to get it right at _initContent:... time, because it's more trouble that it's worth to write a real +[NSCarbonWindow frameRectForContentRect:styleMask:]. M.P. Notice - 10/10/00
203
[self reconcileToCarbonWindowBounds];
205
// Install an event handler for the Carbon window events in which we're interested.
206
const EventTypeSpec kEvents[] = {
207
{ kEventClassWindow, kEventWindowActivated },
208
{ kEventClassWindow, kEventWindowDeactivated },
209
{ kEventClassWindow, kEventWindowBoundsChanged },
210
{ kEventClassWindow, kEventWindowShown },
211
{ kEventClassWindow, kEventWindowHidden }
214
const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };
216
osStatus = InstallEventHandler( GetWindowEventTarget(_windowRef), NSCarbonWindowHandleEvent, GetEventTypeCount( kEvents ), kEvents, (void*)self, &_eventHandler);
217
if (osStatus!=noErr) {
222
osStatus = InstallEventHandler( GetControlEventTarget( HIViewGetRoot( _windowRef ) ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
223
if (osStatus!=noErr) {
228
HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowContentID, &contentView );
229
osStatus = InstallEventHandler( GetControlEventTarget( contentView ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
230
if (osStatus!=noErr) {
235
// Put a pointer to this Cocoa NSWindow in a Carbon window property tag.
236
// Right now, this is just used by NSViewCarbonControl. M.P. Notice - 10/9/00
237
windowAsProperty = self;
238
osStatus = SetWindowProperty(_windowRef, WKCarbonWindowPropertyCreator(), WKCarbonWindowPropertyTag(), sizeof(NSWindow *), &windowAsProperty);
239
if (osStatus!=noErr) {
244
// Ignore the Carbon window activation/deactivation events that Carbon sends to its windows at app activation/deactivation. We'll send such events when we think it's appropriate.
245
_passingCarbonWindowActivationEvents = NO;
247
// Be sure to sync up visibility
248
[self _setVisible:(BOOL)IsWindowVisible( _windowRef )];
255
- (void)setViewsNeedDisplay:(BOOL)wellDoThey {
256
// Make sure we can flush anything that needs it.
258
// We need to sync the context here. I was hoping I didn't need to do this,
259
// but apparently when scrolling, the AppKit view system draws directly.
260
// When this occurs, I cannot intercept it to make it draw in my HIView
261
// context. What ends up happening is that it draws, but nothing ever
264
if ( [self windowNumber] != -1 )
266
CGContextRef cgContext = (CGContextRef)[[self _threadContext] graphicsPort];
267
CGContextSynchronize( cgContext );
273
JSC::initializeThreading();
274
WTF::initializeMainThreadToProcessMainThread();
275
WebCore::RunLoop::initializeMainRunLoop();
276
WebCoreObjCFinalizeOnMainThread(self);
279
// Given a reference to a Carbon window that is to be encapsulated, and an indicator of whether or not this object should take responsibility for disposing of the Carbon window, initialize.
280
- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned {
281
// for now, set disableOrdering to YES because that is what we've been doing and is therefore lower risk. However, I think it would be correct to set it to NO.
282
return [self initWithCarbonWindowRef:inWindowRef takingOwnership:inWindowRefIsOwned disableOrdering:YES carbon:NO];
288
if (WebCoreObjCScheduleDeallocateOnMainThread([CarbonWindowAdapter class], self))
291
// Clean up, if necessary.
292
// if we didn't remove the event handler at dealloc time, we would risk getting sent events after the window has been deallocated. See 2702179. M.P. Notice - 6/1/01
293
if (_eventHandler) RemoveEventHandler(_eventHandler);
295
// Do the standard Cocoa thing.
300
ASSERT_MAIN_THREAD();
301
if (_eventHandler) RemoveEventHandler(_eventHandler);
305
- (WindowRef)windowRef {
312
// should always be YES, but check in order to avoid initialization or deallocation surprises
313
- (BOOL)_hasWindowRef {
314
return (_windowRef != NULL);
317
// an NSCarbonWindow does not manage the windowRef. The windowRef manages the NSCarbonWindow
318
- (BOOL)_managesWindowRef {
322
- (void)_removeWindowRef {
325
if (_eventHandler) RemoveEventHandler(_eventHandler);
327
_eventHandler = NULL;
330
- (WindowClass)_carbonWindowClass {
331
WindowClass windowClass = kDocumentWindowClass;
334
if ([self _hasWindowRef]) {
335
osStatus = GetWindowClass([self windowRef], &windowClass);
336
if (osStatus != noErr) {
337
NSLog(@"Couldn't get window class: error=%ld", osStatus);
343
// Update this window's frame and content frame rectangles to match the Carbon window's structure bounds and content bounds rectangles. Return yes if the update was really necessary, no otherwise.
344
- (BOOL)reconcileToCarbonWindowBounds {
347
NSRect newContentFrameRect;
348
NSRect newWindowFrameRect;
349
NSRect oldContentFrameRect;
350
Rect windowContentBoundsRect;
351
Rect windowStructureBoundsRect;
353
// Initialize for safe returning.
354
BOOL reconciliationWasNecessary = NO;
356
// Precondition check.
357
assert(_contentView);
359
// Get the Carbon window's bounds, which are expressed in global screen coordinates, with (0,0) at the top-left of the main screen.
360
osStatus = GetWindowBounds(_windowRef, kWindowStructureRgn, &windowStructureBoundsRect);
361
if (osStatus!=noErr) NSLog(@"A Carbon window's structure bounds couldn't be gotten.");
362
osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
363
if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
365
// Set the frame rectangle of the border view and this window from the Carbon window's structure region bounds.
366
newWindowFrameRect.origin.x = windowStructureBoundsRect.left;
367
newWindowFrameRect.origin.y = NSMaxY([(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame]) - windowStructureBoundsRect.bottom;
368
newWindowFrameRect.size.width = windowStructureBoundsRect.right - windowStructureBoundsRect.left;
369
newWindowFrameRect.size.height = windowStructureBoundsRect.bottom - windowStructureBoundsRect.top;
370
if (!NSEqualRects(newWindowFrameRect, _frame)) {
371
[self _setFrame:newWindowFrameRect];
372
[_borderView setFrameSize:newWindowFrameRect.size];
373
reconciliationWasNecessary = YES;
376
// Set the content view's frame rect from the Carbon window's content region bounds.
377
newContentFrameRect.origin.x = windowContentBoundsRect.left - windowStructureBoundsRect.left;
378
newContentFrameRect.origin.y = windowStructureBoundsRect.bottom - windowContentBoundsRect.bottom;
379
newContentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
380
newContentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
381
oldContentFrameRect = [(NSView *)_contentView frame];
382
if (!NSEqualRects(newContentFrameRect, oldContentFrameRect)) {
383
[(NSView *)_contentView setFrame:newContentFrameRect];
384
reconciliationWasNecessary = YES;
388
return reconciliationWasNecessary;
393
// Handle an event just like an NSWindow would.
394
- (void)sendSuperEvent:(NSEvent *)inEvent {
396
// Filter out a few events that just result in complaints in the log.
397
// Ignore some unknown event that gets sent when NSTextViews in printing accessory views are focused. M.P. Notice - 12/7/00
398
BOOL ignoreEvent = NO;
399
NSEventType eventType = [inEvent type];
400
if (eventType==NSSystemDefined) {
401
short eventSubtype = [inEvent subtype];
402
if (eventSubtype==7) {
405
} else if (eventType == NSKeyDown) {
406
// Handle command-space as [NSApp sendEvent:] does.
407
if ([NSInputContext processInputKeyBindings:inEvent]) {
413
if (!ignoreEvent) [super sendEvent:inEvent];
416
- (void)relinquishFocus
418
NSResponder* firstResponder;
420
// Carbon thinks that a control has the keyboard focus,
421
// or we wouldn't be being asked to relinquish focus.
423
firstResponder = [self firstResponder];
424
if ([firstResponder isKindOfClass:[NSView class]] ){
425
// Make the window the first responder, so that no view is the key view.
426
[self makeFirstResponder:self];
430
- (BOOL)makeFirstResponder:(NSResponder *)aResponder
432
// Let NSWindow focus the appropriate NSView.
433
if (![super makeFirstResponder:aResponder])
436
// Now, if the view we're focusing is in a HIWebView, find the
437
// corresponding HIWebView for the NSView, and tell carbon to
438
// clear any focused control.
439
HIViewRef viewRef = 0;
440
NSResponder *firstResponder = [self firstResponder];
441
if ([firstResponder isKindOfClass:[NSView class]]) {
442
NSView *view = (NSView *)firstResponder;
444
viewRef = [HIViewAdapter getHIViewForNSView:view];
447
view = [view superview];
452
GetKeyboardFocus (_windowRef, &focus);
453
if (focus != viewRef) {
454
SetKeyboardFocus (_windowRef, viewRef, kControlIndicatorPart );
460
// There's no override of _addCursorRect:cursor:forView:, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
463
// There's no override of _autoResizeState, despite the fact that NSWindow's operates on _windowNum, because it looks like it might work on Carbon windows as is.
466
// Disappointingly, -_blockHeartBeat: is not immediately invoked to turn off heartbeating. Heartbeating is turned off by setting the gDefaultButtonPaused global variable, and then this method is invoked later, if that global is set (at heartbeating time I guess). Something has to change if we want to hook this up in Carbon windows. M.P. Warning - 9/17/01
468
// Do the right thing for a Carbon window.
469
- (void)_blockHeartBeat:(BOOL)flag {
471
ControlRef defaultButton;
474
// Do the standard Cocoa thing.
475
[super _blockHeartBeat:flag];
477
// If there's a default Carbon button in this Carbon window, make it stop pulsing, the Carbon way.
478
// This is inspired by HIToolbox/Controls/Definitions/ButtonCDEF.c's ButtonEventHandler(). M.P. Notice - 12/5/00
479
osStatus = GetWindowDefaultButton(_windowRef, &defaultButton);
480
if (osStatus==noErr && defaultButton) {
481
Boolean anotherButtonIsTracking = flag ? TRUE : FALSE;
482
osStatus = SetControlData(defaultButton, kControlNoPart, kControlPushButtonAnotherButtonTrackingTag, sizeof(Boolean), &anotherButtonIsTracking);
483
if (osStatus==noErr) DrawOneControl(defaultButton);
484
else NSLog(@"Some data couldn't be set in a Carbon control.");
491
// Do the right thing for a Carbon window.
492
- (void)_cancelKey:(id)sender {
494
// Most of the time the handling of the cancel key will be done by Carbon, but this method will be invoked if an NSCarbonWindow is wrapping a Carbon window that contains an NSViewCarbonControl, and the escape key or whatever is pressed with an NSTextView focused. Just do what Carbon would do.
495
ControlRef cancelButton;
496
GetWindowCancelButton(_windowRef, &cancelButton);
498
if (IsControlActive(cancelButton)) {
499
HIViewSimulateClick(cancelButton, kControlButtonPart, 0, NULL);
507
// Do the right thing for a Carbon window.
508
- (void)_commonAwake {
510
// Complain, because this should never be called. We insist that -[NSCarbonWindow initWithCarbonWindowRef] is the only valid initializer for instances of this class, and that there's no such thing as a one-shot NSCarbonWindow.
511
NSLog(@"-[NSCarbonWindow _commonAwake] is not implemented.");
516
// There's no override of _commonInitFrame:styleMask:backing:defer:, despite the fact that NSWindow's modifies quite a few instance variables, because it gets called in a harmless way if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
519
// Do the right thing for a Carbon window.
520
- (id)_destroyRealWindow:(BOOL)orderingOut {
522
// Complain, because this should never be called. We don't support one-shot NSCarbonWindows.
523
NSLog(@"-[NSCarbonWindow _destroyRealWindow:] is not implemented.");
529
// There's no override of _discardCursorRectsForView, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
532
// There's no override of _forceFlushWindowToScreen, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
535
// There's no override of _getPositionFromServer, despite the fact that NSWindow's operates on _windowNum, because it's only called from -[NSApplication _activateWindows], which is hopefully about to become obsolete any second now.
538
// There's no override of _globalWindowNum, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
541
// There's no override of _initContent:styleMask:backing:defer:contentView:, despite the fact that NSWindow's modifies _auxiliaryStorage->_auxWFlags.hasShadow, because it will never get called if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
544
// There's no override of _initContent:styleMask:backing:defer:counterpart:, despite the fact that NSWindow's modifies _auxiliaryStorage->_auxWFlags.hasShadow, because it will never get called if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
547
// Do what NSWindow would do, but then sychronize the Carbon window structures.
548
- (void)_oldPlaceWindow:(NSRect)frameRect {
552
// Do the standard Cocoa thing.
553
[super _oldPlaceWindow:frameRect];
555
// Tell Carbon to update its various regions.
556
// Despite its name, this function should be called early and often, even if the window isn't visible yet. 2702648. M.P. Notice - 7/24/01
557
osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
558
if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
563
// There's no override of _orderOutAndCalcKeyWithCounter:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
566
// There's no override of _realHeartBeatThreadContext, despite the fact that NSWindows's invokes [self windowNumber], because it looks like it might not do anything that will effect a Carbon window.
569
// There's no override of _registerWithDockIfNeeded, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
572
// There's no override of _removeCursorRect:cursor:forView:, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
575
// There's no override of _setAvoidsActivation:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
578
// There's no override of _setFrame:, despite the fact that NSWindow's modifies _frame, because it looks like it might work on Carbon windows as is. The synchronization of the Carbon window bounds rect to the Cocoa frame rect is done in the overrides of _oldPlaceWindow: and _windowMovedToRect:.
581
// There's no override of _setFrameCommon:display:stashSize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
584
// There's no override of _setWindowNumber:, despite the fact that NSWindow's modifies _windowNum and invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
587
// Do what NSWindow would do, but for a Carbon window.
588
// This function is mostly cut-and-pasted from -[NSWindow _termWindowIfOwner]. M.P. Notice - 8/7/00
589
- (void)_termWindowIfOwner {
590
[self _setWindowNumber:-1];
591
_wFlags.isTerminating = YES;
592
if (_windowRef && _windowRefIsOwned) DisposeWindow(_windowRef);
593
// KW - need to clear window shadow state so it gets reset correctly when new window created
594
// if ([_borderView respondsToSelector:@selector(setShadowState:)]) {
595
// [_borderView setShadowState:kFrameShadowNone];
597
_wFlags.isTerminating = NO;
601
// There's no override of _threadContext, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might not do anything that will effect a Carbon window.
604
// There's no override of _windowMoved:, despite the fact that NSWindow's operates on _windowNum, because it looks like it might work on Carbon windows as is.
607
// Do what NSWindow would do, but then sychronize the Carbon window structures.
608
- (void)_windowMovedToRect:(NSRect)actualFrame {
612
// Do the standard Cocoa thing.
613
[super _windowMovedToRect:actualFrame];
615
// Let Carbon know that the window has been moved, unless this method is being called "early."
616
if (_wFlags.visible) {
617
osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
618
if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
623
- (NSRect)constrainFrameRect:(NSRect)actualFrame toScreen:(NSScreen *)screen {
624
// let Carbon decide window size and position
628
- (void)selectKeyViewFollowingView:(NSView *)aView {
629
HIViewRef view = NULL;
631
view = [HIViewAdapter getHIViewForNSView:aView];
635
HIViewRef contentView;
637
GetRootControl( GetControlOwner( view ), &contentView );
638
HIViewAdvanceFocus( contentView, 0 );
642
[super selectKeyViewFollowingView:aView];
646
- (void)selectKeyViewPrecedingView:(NSView *)aView {
647
HIViewRef view = NULL;
649
view = [HIViewAdapter getHIViewForNSView:aView];
653
HIViewRef contentView;
655
GetRootControl( GetControlOwner( view ), &contentView );
656
HIViewAdvanceFocus( contentView, shiftKey );
660
[super selectKeyViewPrecedingView:aView];
664
- (void)makeKeyWindow {
665
[NSApp _setMouseActivationInProgress:NO];
666
[NSApp setIsActive:YES];
667
[super makeKeyWindow];
672
// Do the right thing for a Carbon window.
673
- (BOOL)canBecomeKeyWindow {
678
// Do the right thing for a Carbon window.
679
- (BOOL)canBecomeMainWindow {
681
WindowClass windowClass;
682
// By default, Carbon windows cannot become the main window.
683
// What about when the default isn't right? Requiring subclassing seems harsh. M.P. Warning - 9/17/01
684
// KW - modify this to allow document windows to become main
685
// This is primarily to get the right look, so that you don't have two windows that both look active - one Cocoa document and one Carbon document
686
osStatus = GetWindowClass(_windowRef, &windowClass);
687
return (osStatus == noErr && windowClass == kDocumentWindowClass);
692
// There's no override of deminiaturize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
695
// There's no override of disableCursorRects, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
699
// There's no override of enableCursorRects, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
702
// Do the right thing for a Carbon window.
703
- (void)encodeWithCoder:(NSCoder *)coder {
705
// Actually, this will probably never be implemented. M.P. Notice - 8/2/00
706
NSLog(@"-[NSCarbonWindow encodeWithCoder:] is not implemented.");
711
// There's no override of frame, despite the fact that NSWindow's returns _frame, because _frame is one of the instance variables whose value we're keeping synchronized with the Carbon window.
714
// Do the right thing for a Carbon window.
715
- (id)initWithCoder:(NSCoder *)coder {
717
// Actually, this will probably never be implemented. M.P. Notice - 8/2/00
718
NSLog(@"-[NSCarbonWindow initWithCoder:] is not implemented.");
725
// There's no override of level, despite the fact that NSWindow's returns _level, because _level is one of the instance variables whose value we're keeping synchronized with the Carbon window.
728
// There's no override of miniaturize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
731
// There's no override of resizeToScreenWithEvent:, despite the fact that NSWindow's operates on _windowNum.
732
// It looks like it's only called when an _NSForceResizeEventType event is passed into -[NSWindow sendEvent:], and I can't find any instances of that happening.
735
// Do the right thing for a Carbon Window.
736
- (void)sendEvent:(NSEvent *)theEvent {
738
// Not all events are handled in the same manner.
739
NSEventType eventType = [theEvent type];
740
if (eventType==NSAppKitDefined) {
742
// Handle the event the Cocoa way. Carbon won't understand it anyway.
743
[super sendEvent:theEvent];
749
// There's no override of setAcceptsMouseMovedEvents:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
752
// There's no override of setBackingType:, despite the fact that NSWindow's invokes [self windowNumber], because it's apparently not expected to do anything anyway, judging from the current implementation of PSsetwindowtype().
755
// Do what NSWindow would do, but for a Carbon window.
756
- (void)setContentView:(NSView *)aView {
758
NSRect contentFrameRect;
760
Rect windowContentBoundsRect;
762
// Precondition check.
764
assert([_borderView isKindOfClass:[CarbonWindowFrame class]]);
769
assert([aView isKindOfClass:[CarbonWindowContentView class]]);
771
// Find out the window's Carbon window structure region (content) bounds.
772
osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
773
if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
774
contentFrameRect.origin = NSZeroPoint;
775
contentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
776
contentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
778
// If the content view is still in some other view hierarchy, pry it free.
779
[_contentView removeFromSuperview];
780
assert(![_contentView superview]);
782
// Record the content view, and size it to this window's content frame.
783
_contentView = aView;
784
[(NSView *)_contentView setFrame:contentFrameRect];
786
// Make the content view a subview of the border view.
787
[_borderView addSubview:_contentView];
789
// Tell the content view it's new place in the responder chain.
790
[_contentView setNextResponder:self];
795
// There's no override of setDepthLimit:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
798
- (BOOL)worksWhenModal {
799
WindowClass windowClass = [self _carbonWindowClass];
800
return (windowClass == kFloatingWindowClass || windowClass == kUtilityWindowClass);
803
- (void)_setModalWindowLevel {
807
- (id)_clearModalWindowLevel {
811
// There's no override of setLevel:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
812
// I thought at first that there should be a mapping between Cocoa level and Carbon window class, but experiments convince me that such is not the case. M.P. Notice - 9/18/00
815
// There's no override of windowNumber, despite the fact that NSWindow's returns _windowNum, because _windowNum is one of the instance variables whose value we're keeping synchronized with the Carbon window.
818
- (UInt32)carbonHICommandIDFromActionSelector:(SEL)inActionSelector {
820
// Initialize with the default return value.
821
UInt32 hiCommandID = 0;
823
// Pretty simple, if tedious.
824
if (inActionSelector==@selector(clear:)) hiCommandID = kHICommandClear;
825
else if (inActionSelector==@selector(copy:)) hiCommandID = kHICommandCopy;
826
else if (inActionSelector==@selector(cut:)) hiCommandID = kHICommandCut;
827
else if (inActionSelector==@selector(paste:)) hiCommandID = kHICommandPaste;
828
else if (inActionSelector==@selector(redo:)) hiCommandID = kHICommandRedo;
829
else if (inActionSelector==@selector(selectAll:)) hiCommandID = kHICommandSelectAll;
830
else if (inActionSelector==@selector(undo:)) hiCommandID = kHICommandUndo;
838
- (void)sendCarbonProcessHICommandEvent:(UInt32)inHICommandID {
840
EventTargetRef eventTargetRef;
844
// Initialize for safe error handling.
845
EventRef eventRef = NULL;
847
// Create a Process Command event. Don't mention anything about the menu item, because we don't want the Carbon Event handler fiddling with it.
848
hiCommand.attributes = 0;
849
hiCommand.commandID = inHICommandID;
850
hiCommand.menu.menuRef = 0;
851
hiCommand.menu.menuItemIndex = 0;
852
osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandProcess, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
853
if (osStatus!=noErr) {
854
NSLog(@"CreateEvent() returned %i.", (int)osStatus);
857
osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
858
if (osStatus!=noErr) {
859
NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
863
// Send a Carbon event to whatever has the Carbon user focus.
864
eventTargetRef = GetUserFocusEventTarget();
865
osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
866
if (osStatus!=noErr) {
867
NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
874
if (eventRef) ReleaseEvent(eventRef);
879
- (Boolean)sendCarbonUpdateHICommandStatusEvent:(UInt32)inHICommandID withMenuRef:(MenuRef)inMenuRef andMenuItemIndex:(UInt16)inMenuItemIndex {
881
EventTargetRef eventTargetRef;
885
// Initialize for safe error handling and flag returning.
886
Boolean eventWasHandled = FALSE;
887
EventRef eventRef = NULL;
889
// Create a Process Command event. Don't mention anything about the menu item, because we don't want the Carbon Event handler fiddling with it.
890
hiCommand.attributes = kHICommandFromMenu;
891
hiCommand.commandID = inHICommandID;
892
hiCommand.menu.menuRef = inMenuRef;
893
hiCommand.menu.menuItemIndex = inMenuItemIndex;
894
osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandUpdateStatus, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
895
if (osStatus!=noErr) {
896
NSLog(@"CreateEvent() returned %i.", (int)osStatus);
899
osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
900
if (osStatus!=noErr) {
901
NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
905
// Send a Carbon event to whatever has the Carbon user focus.
906
eventTargetRef = GetUserFocusEventTarget();
907
osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
908
if (osStatus==noErr) {
909
eventWasHandled = TRUE;
910
} else if (osStatus!=eventNotHandledErr) {
911
NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
918
if (eventRef) ReleaseEvent(eventRef);
921
return eventWasHandled;
925
- (void)_handleRootBoundsChanged
927
HIViewRef root = HIViewGetRoot( _windowRef );
930
HIViewGetFrame( root, &frame );
931
[_borderView setFrameSize:*(NSSize*)&frame.size];
934
- (void)_handleContentBoundsChanged
936
HIViewRef root, contentView;
937
HIRect rootBounds, contentFrame;
938
NSRect oldContentFrameRect;
940
root = HIViewGetRoot( _windowRef );
941
HIViewFindByID( root, kHIViewWindowContentID, &contentView );
942
HIViewGetFrame( contentView, &contentFrame );
943
HIViewGetBounds( root, &rootBounds );
945
// Set the content view's frame rect from the Carbon window's content region bounds.
946
contentFrame.origin.y = rootBounds.size.height - CGRectGetMaxY( contentFrame );
948
oldContentFrameRect = [(NSView *)_contentView frame];
949
if ( !NSEqualRects( *(NSRect*)&contentFrame, oldContentFrameRect ) ) {
950
[(NSView *)_contentView setFrame:*(NSRect*)&contentFrame];
954
- (OSStatus)_handleCarbonEvent:(EventRef)inEvent callRef:(EventHandlerCallRef)inCallRef {
955
OSStatus result = eventNotHandledErr;
957
switch ( GetEventClass( inEvent ) )
959
case kEventClassControl:
963
check( GetEventKind( inEvent ) == kEventControlBoundsChanged );
965
GetEventParameter( inEvent, kEventParamDirectObject, typeControlRef, NULL,
966
sizeof( ControlRef ), NULL, &control );
968
if ( control == HIViewGetRoot( _windowRef ) )
969
[self _handleRootBoundsChanged];
971
[self _handleContentBoundsChanged];
975
case kEventClassWindow:
976
switch ( GetEventKind( inEvent ) )
978
case kEventWindowShown:
979
[self _setVisible:YES];
982
case kEventWindowHidden:
983
[self _setVisible:NO];
986
case kEventWindowActivated:
987
[self makeKeyWindow];
990
case kEventWindowDeactivated:
991
[self resignKeyWindow];
994
case kEventWindowBoundsChanged:
995
[self reconcileToCarbonWindowBounds];
1004
// Handle various events that Carbon is sending to our window.
1005
static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData) {
1007
// default action is to send event to next handler. We modify osStatus as necessary where we don't want this behavior
1008
OSStatus osStatus = eventNotHandledErr;
1010
// We do different things for different event types.
1011
CarbonWindowAdapter *carbonWindow = (CarbonWindowAdapter *)inUserData;
1013
osStatus = [carbonWindow _handleCarbonEvent: inEventRef callRef: inEventHandlerCallRef];
1015
// Done. If we want to propagate the event, we return eventNotHandledErr to send it to the next handler
1020
// [3364117] We need to make sure this does not fall through to the AppKit implementation! bad things happen.
1021
- (void)_reallyDoOrderWindow:(NSWindowOrderingMode)place relativeTo:(int)otherWin findKey:(BOOL)doKeyCalc forCounter:(BOOL)isACounter force:(BOOL)doForce isModal:(BOOL)isModal {
1024
- (NSRect) _growBoxRect
1026
WindowAttributes attrs;
1027
NSRect retRect = NSZeroRect;
1029
GetWindowAttributes( _windowRef, &attrs );
1031
if ( attrs & kWindowResizableAttribute )
1033
HIRect bounds, rect;
1036
HIViewGetBounds( HIViewGetRoot( _windowRef ), &bounds );
1037
HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowGrowBoxID, &view );
1038
HIViewGetFrame( view, &rect );
1040
rect.origin.y = bounds.size.height - CGRectGetMaxY( rect ) - 1;
1043
retRect = *(NSRect*)▭
1049
@end // implementation CarbonWindowAdapter