webengine/osswebengine/WebKit/Carbon/CarbonWindowAdapter.m
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  *
       
     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. 
       
    16  *
       
    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.
       
    27  */
       
    28 
       
    29 // I don't think this class belongs in WebKit. Lets move it out.
       
    30 
       
    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
       
    35 
       
    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
       
    47 
       
    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
       
    55 
       
    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
       
    57 
       
    58 #ifndef __LP64__
       
    59 
       
    60 #import "CarbonWindowAdapter.h"
       
    61 
       
    62 #import "CarbonWindowFrame.h"
       
    63 #import "CarbonWindowContentView.h"
       
    64 #import "HIViewAdapter.h"
       
    65 
       
    66 #import <WebKitSystemInterface.h>
       
    67 
       
    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>
       
    73 #import <assert.h>
       
    74 
       
    75 #import <WebCore/WebCoreObjCExtras.h>
       
    76 
       
    77 #import "WebKitLogging.h"
       
    78 #import "WebNSObjectExtras.h"
       
    79 #import "WebTypesInternal.h"
       
    80 
       
    81 @interface NSWindow(HIWebFrameView)
       
    82 - _initContent:(const NSRect *)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag contentView:aView;
       
    83 - (void)_oldPlaceWindow:(NSRect)frameRect;
       
    84 - (void)_windowMovedToRect:(NSRect)actualFrame;
       
    85 - (void)_setWindowNumber:(NSInteger)nativeWindow;
       
    86 - (NSGraphicsContext *)_threadContext;
       
    87 - (void)_setFrame:(NSRect)newWindowFrameRect;
       
    88 - (void)_setVisible:(BOOL)flag;
       
    89 @end
       
    90 
       
    91 @interface NSApplication(HIWebFrameView)
       
    92 - (void)setIsActive:(BOOL)aFlag;
       
    93 - (id)_setMouseActivationInProgress:(BOOL)flag;
       
    94 - (BOOL)_handleKeyEquivalent:(NSEvent*)theEvent;
       
    95 @end
       
    96 
       
    97 @interface NSInputContext
       
    98 - (BOOL)processInputKeyBindings:(NSEvent *)event;
       
    99 @end
       
   100 
       
   101 // Forward declarations.
       
   102 static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData);
       
   103 
       
   104 @implementation CarbonWindowAdapter
       
   105 
       
   106 
       
   107 // Return an appropriate window frame class.
       
   108 + (Class)frameViewClassForStyleMask:(unsigned int)style {
       
   109 
       
   110     // There's only one appropriate window style, and only one appropriate window frame class.
       
   111     assert(style & WKCarbonWindowMask());
       
   112     return [CarbonWindowFrame class];
       
   113 
       
   114 }
       
   115 
       
   116 
       
   117 // Overriding of the parent class' designated initializer, just for safety's sake.
       
   118 - (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)style backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
       
   119 
       
   120     // Do the standard Cocoa thing.
       
   121     self = [super initWithContentRect:contentRect styleMask:style backing:bufferingType defer:flag];
       
   122     if (self==nil) return nil;
       
   123 
       
   124     // Simple.
       
   125     _windowRef = NULL;
       
   126     _windowRefIsOwned = NO;
       
   127     _eventHandler = NULL;
       
   128     
       
   129     // Done.
       
   130     return self;
       
   131 
       
   132 }
       
   133 
       
   134 // 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.
       
   135 - (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned disableOrdering:(BOOL)inDisableOrdering carbon:(BOOL)inCarbon {
       
   136 
       
   137     NSBackingStoreType backingStoreType;
       
   138     CarbonWindowContentView *carbonWindowContentView;
       
   139     NSWindow *windowAsProperty;
       
   140     OSStatus osStatus;
       
   141     UInt32 windowFeatures;
       
   142     WindowAttributes windowAttributes;
       
   143     unsigned int styleMask;
       
   144     void *nativeWindow;
       
   145     WindowModality windowModality;
       
   146 	ControlRef		contentView;
       
   147 	
       
   148     // Simple.
       
   149     // 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
       
   150     _windowRef = inWindowRef;
       
   151     //_auxiliaryStorage->_windowRef = inWindowRef;
       
   152     _windowRefIsOwned = inWindowRefIsOwned;
       
   153 	_carbon = inCarbon;
       
   154 
       
   155     // Find out the window's CoreGraphics window reference.
       
   156     nativeWindow = WKGetNativeWindowFromWindowRef(inWindowRef);
       
   157 
       
   158     // Find out the window's Carbon window attributes.
       
   159     GetWindowAttributes(inWindowRef, &windowAttributes);
       
   160 
       
   161     // Find out the window's Carbon window features.
       
   162     GetWindowFeatures(inWindowRef, &windowFeatures);
       
   163 
       
   164     // Figure out the window's backing store type.
       
   165     // At one time, this had code stolen from CreatePlatformWindow in HIToolbox/Windows/Platform/CGSPlatform.c
       
   166 	// But now the non-retained window class is a Carbon secret that's not even in
       
   167 	// WindowsPriv.h; maybe we'll have to revisit this if someone needs to use WebKit
       
   168 	// in a non-retained window.
       
   169     backingStoreType = NSBackingStoreRetained;
       
   170 
       
   171     // Figure out the window's style mask.
       
   172     styleMask = WKCarbonWindowMask();
       
   173     if (windowAttributes & kWindowCloseBoxAttribute) styleMask |= NSClosableWindowMask;
       
   174     if (windowAttributes & kWindowResizableAttribute) styleMask |= NSResizableWindowMask;
       
   175     if (windowFeatures & kWindowCanCollapse) styleMask |= NSMiniaturizableWindowMask;
       
   176     if (windowFeatures & kWindowHasTitleBar) styleMask |= NSTitledWindowMask;
       
   177 
       
   178     osStatus = GetWindowModality(_windowRef, &windowModality, NULL);
       
   179     if (osStatus != noErr) {
       
   180         NSLog(@"Couldn't get window modality: error=%d", osStatus);
       
   181         return nil;
       
   182     }
       
   183     
       
   184     // Create one of our special content views.
       
   185     carbonWindowContentView = [[[CarbonWindowContentView alloc] init] autorelease];
       
   186 
       
   187     // 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.
       
   188     self = (CarbonWindowAdapter*)[super _initContent:NULL styleMask:styleMask backing:backingStoreType defer:YES contentView:carbonWindowContentView];
       
   189     if (!self) return nil;
       
   190     assert(_contentView);
       
   191 
       
   192     // Record accurately whether or not this window has a shadow, in case someone asks.
       
   193  //   _auxiliaryStorage->_auxWFlags.hasShadow = (windowAttributes & kWindowNoShadowAttribute) ? NO : YES;
       
   194 
       
   195     // Record the window number.
       
   196     [self _setWindowNumber:(NSInteger)nativeWindow];
       
   197 
       
   198     // Set up from the frame rectangle.
       
   199     // 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
       
   200     [self reconcileToCarbonWindowBounds];
       
   201 
       
   202     // Install an event handler for the Carbon window events in which we're interested.
       
   203     const EventTypeSpec kEvents[] = {
       
   204             { kEventClassWindow, kEventWindowActivated },
       
   205             { kEventClassWindow, kEventWindowDeactivated },
       
   206             { kEventClassWindow, kEventWindowBoundsChanged },
       
   207             { kEventClassWindow, kEventWindowShown },
       
   208             { kEventClassWindow, kEventWindowHidden }
       
   209     };
       
   210     
       
   211     const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };
       
   212     
       
   213     osStatus = InstallEventHandler( GetWindowEventTarget(_windowRef), NSCarbonWindowHandleEvent, GetEventTypeCount( kEvents ), kEvents, (void*)self, &_eventHandler);
       
   214     if (osStatus!=noErr) {
       
   215             [self release];
       
   216             return nil;
       
   217     }
       
   218 
       
   219     osStatus = InstallEventHandler( GetControlEventTarget( HIViewGetRoot( _windowRef ) ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
       
   220     if (osStatus!=noErr) {
       
   221             [self release];
       
   222             return nil;
       
   223     }
       
   224 
       
   225     HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowContentID, &contentView );
       
   226     osStatus = InstallEventHandler( GetControlEventTarget( contentView ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
       
   227     if (osStatus!=noErr) {
       
   228             [self release];
       
   229             return nil;
       
   230     }
       
   231 	
       
   232     // Put a pointer to this Cocoa NSWindow in a Carbon window property tag.
       
   233     // Right now, this is just used by NSViewCarbonControl.  M.P. Notice - 10/9/00
       
   234     windowAsProperty = self;
       
   235     osStatus = SetWindowProperty(_windowRef, WKCarbonWindowPropertyCreator(), WKCarbonWindowPropertyTag(), sizeof(NSWindow *), &windowAsProperty);
       
   236     if (osStatus!=noErr) {
       
   237         [self release];
       
   238         return nil;
       
   239     }
       
   240 
       
   241     // 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.
       
   242     _passingCarbonWindowActivationEvents = NO;
       
   243 
       
   244     // Be sure to sync up visibility
       
   245     [self _setVisible:(BOOL)IsWindowVisible( _windowRef )];
       
   246 
       
   247     // Done.
       
   248     return self;
       
   249 
       
   250 }
       
   251 
       
   252 - (void)setViewsNeedDisplay:(BOOL)wellDoThey {
       
   253 	// Make sure we can flush anything that needs it.
       
   254 
       
   255 	// We need to sync the context here. I was hoping I didn't need to do this,
       
   256 	// but apparently when scrolling, the AppKit view system draws directly.
       
   257 	// When this occurs, I cannot intercept it to make it draw in my HIView
       
   258 	// context. What ends up happening is that it draws, but nothing ever
       
   259 	// flushes it.
       
   260 
       
   261 	if ( [self windowNumber] != -1 )
       
   262 	{
       
   263 		CGContextRef cgContext = (CGContextRef)[[self _threadContext] graphicsPort];
       
   264 		CGContextSynchronize( cgContext );
       
   265 	}
       
   266 }
       
   267 
       
   268 #ifndef BUILDING_ON_TIGER
       
   269 + (void)initialize
       
   270 {
       
   271     WebCoreObjCFinalizeOnMainThread(self);
       
   272 }
       
   273 #endif
       
   274 
       
   275 // 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.
       
   276 - (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned {
       
   277     // 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.
       
   278     return [self initWithCarbonWindowRef:inWindowRef takingOwnership:inWindowRefIsOwned disableOrdering:YES carbon:NO];
       
   279 }
       
   280 
       
   281 
       
   282 // Clean up.
       
   283 - (void)dealloc {
       
   284 
       
   285     // Clean up, if necessary.
       
   286     // 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
       
   287     if (_eventHandler) RemoveEventHandler(_eventHandler);
       
   288 
       
   289     // Do the standard Cocoa thing.
       
   290     [super dealloc];
       
   291 
       
   292 }
       
   293 
       
   294 - (void)finalize {
       
   295     ASSERT_MAIN_THREAD();
       
   296     if (_eventHandler) RemoveEventHandler(_eventHandler);
       
   297     [super finalize];
       
   298 }
       
   299 
       
   300 - (WindowRef)windowRef {
       
   301 
       
   302     // Simple.
       
   303     return _windowRef;
       
   304 
       
   305 }
       
   306 
       
   307 // should always be YES, but check in order to avoid initialization or deallocation surprises
       
   308 - (BOOL)_hasWindowRef {
       
   309     return (_windowRef != NULL);
       
   310 }
       
   311 
       
   312 // an NSCarbonWindow does not manage the windowRef.  The windowRef manages the NSCarbonWindow
       
   313 - (BOOL)_managesWindowRef {
       
   314     return NO;
       
   315 }
       
   316 
       
   317 - (void)_removeWindowRef {
       
   318     _windowRef = NULL;
       
   319 	
       
   320     if (_eventHandler) RemoveEventHandler(_eventHandler);
       
   321 	
       
   322 	_eventHandler = NULL;
       
   323 }
       
   324 
       
   325 - (WindowClass)_carbonWindowClass {
       
   326     WindowClass windowClass = kDocumentWindowClass;
       
   327     OSStatus osStatus;
       
   328     
       
   329     if ([self _hasWindowRef]) {
       
   330         osStatus = GetWindowClass([self windowRef], &windowClass);
       
   331         if (osStatus != noErr) {
       
   332             NSLog(@"Couldn't get window class: error=%d", osStatus);
       
   333         }
       
   334     }
       
   335     return windowClass; 
       
   336 }
       
   337 
       
   338 // 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.
       
   339 - (BOOL)reconcileToCarbonWindowBounds {
       
   340 
       
   341     OSStatus osStatus;
       
   342     NSRect newContentFrameRect;
       
   343     NSRect newWindowFrameRect;
       
   344     NSRect oldContentFrameRect;
       
   345     Rect windowContentBoundsRect;
       
   346     Rect windowStructureBoundsRect;
       
   347 
       
   348     // Initialize for safe returning.
       
   349     BOOL reconciliationWasNecessary = NO;
       
   350 
       
   351     // Precondition check.
       
   352     assert(_contentView);
       
   353 
       
   354     // Get the Carbon window's bounds, which are expressed in global screen coordinates, with (0,0) at the top-left of the main screen.
       
   355     osStatus = GetWindowBounds(_windowRef, kWindowStructureRgn, &windowStructureBoundsRect);
       
   356     if (osStatus!=noErr) NSLog(@"A Carbon window's structure bounds couldn't be gotten.");
       
   357     osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
       
   358     if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
       
   359 
       
   360     // Set the frame rectangle of the border view and this window from the Carbon window's structure region bounds.
       
   361     newWindowFrameRect.origin.x = windowStructureBoundsRect.left;
       
   362     newWindowFrameRect.origin.y = NSMaxY([[[NSScreen screens] objectAtIndex:0] frame]) - windowStructureBoundsRect.bottom;
       
   363     newWindowFrameRect.size.width = windowStructureBoundsRect.right - windowStructureBoundsRect.left;
       
   364     newWindowFrameRect.size.height = windowStructureBoundsRect.bottom - windowStructureBoundsRect.top;
       
   365     if (!NSEqualRects(newWindowFrameRect, _frame)) {
       
   366         [self _setFrame:newWindowFrameRect];
       
   367         [_borderView setFrameSize:newWindowFrameRect.size];
       
   368         reconciliationWasNecessary = YES;
       
   369     }
       
   370 
       
   371     // Set the content view's frame rect from the Carbon window's content region bounds.
       
   372     newContentFrameRect.origin.x = windowContentBoundsRect.left - windowStructureBoundsRect.left;
       
   373     newContentFrameRect.origin.y = windowStructureBoundsRect.bottom - windowContentBoundsRect.bottom;
       
   374     newContentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
       
   375     newContentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
       
   376     oldContentFrameRect = [_contentView frame];
       
   377     if (!NSEqualRects(newContentFrameRect, oldContentFrameRect)) {
       
   378         [_contentView setFrame:newContentFrameRect];
       
   379         reconciliationWasNecessary = YES;
       
   380     }
       
   381 
       
   382     // Done.
       
   383     return reconciliationWasNecessary;
       
   384 
       
   385 }
       
   386 
       
   387 
       
   388 // Handle an event just like an NSWindow would.
       
   389 - (void)sendSuperEvent:(NSEvent *)inEvent {
       
   390 
       
   391     // Filter out a few events that just result in complaints in the log.
       
   392     // Ignore some unknown event that gets sent when NSTextViews in printing accessory views are focused.  M.P. Notice - 12/7/00
       
   393     BOOL ignoreEvent = NO;
       
   394     NSEventType eventType = [inEvent type];
       
   395     if (eventType==NSSystemDefined) {
       
   396         short eventSubtype = [inEvent subtype];
       
   397         if (eventSubtype==7) {
       
   398             ignoreEvent = YES;
       
   399         }
       
   400     } else if (eventType == NSKeyDown) {
       
   401         // Handle command-space as [NSApp sendEvent:] does.
       
   402         if ([NSInputContext processInputKeyBindings:inEvent]) {
       
   403             return;
       
   404         }
       
   405     }
       
   406     
       
   407     // Simple.
       
   408     if (!ignoreEvent) [super sendEvent:inEvent];
       
   409 }
       
   410 
       
   411 - (void)relinquishFocus
       
   412 {
       
   413     NSResponder*  firstResponder;
       
   414 
       
   415     // Carbon thinks that a control has the keyboard focus,
       
   416     // or we wouldn't be being asked to relinquish focus.
       
   417 
       
   418 	firstResponder = [self firstResponder];
       
   419 	if ([firstResponder isKindOfClass:[NSView class]] ){
       
   420 		// Make the window the first responder, so that no view is the key view.
       
   421         [self makeFirstResponder:self];
       
   422     }
       
   423 }
       
   424 
       
   425 - (BOOL)makeFirstResponder:(NSResponder *)aResponder
       
   426 {
       
   427     // Let NSWindow focus the appropriate NSView.
       
   428     if (![super makeFirstResponder:aResponder])
       
   429         return NO;
       
   430 
       
   431     // Now, if the view we're focusing is in a HIWebView, find the
       
   432     // corresponding HIWebView for the NSView, and tell carbon to
       
   433     // clear any focused control.
       
   434     HIViewRef viewRef = 0;
       
   435     NSResponder *firstResponder = [self firstResponder];    
       
   436     if ([firstResponder isKindOfClass:[NSView class]]) {
       
   437         NSView *view = (NSView *)firstResponder;
       
   438         while (view) {
       
   439             viewRef = [HIViewAdapter getHIViewForNSView:view];
       
   440             if (viewRef)
       
   441                 break;
       
   442             view = [view superview];
       
   443         }
       
   444     }
       
   445 
       
   446     HIViewRef focus;
       
   447     GetKeyboardFocus (_windowRef, &focus);
       
   448     if (focus != viewRef) {
       
   449         SetKeyboardFocus (_windowRef, viewRef, kControlIndicatorPart );
       
   450     }
       
   451 
       
   452     return YES;
       
   453 }
       
   454 
       
   455 // 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.
       
   456 
       
   457 
       
   458 // 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.
       
   459 
       
   460 
       
   461 // 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
       
   462 /*
       
   463 // Do the right thing for a Carbon window.
       
   464 - (void)_blockHeartBeat:(BOOL)flag {
       
   465 
       
   466     ControlRef defaultButton;
       
   467     OSStatus osStatus;
       
   468 
       
   469     // Do the standard Cocoa thing.
       
   470     [super _blockHeartBeat:flag];
       
   471 
       
   472     // If there's a default Carbon button in this Carbon window, make it stop pulsing, the Carbon way.
       
   473     // This is inspired by HIToolbox/Controls/Definitions/ButtonCDEF.c's ButtonEventHandler().  M.P. Notice - 12/5/00
       
   474     osStatus = GetWindowDefaultButton(_windowRef, &defaultButton);
       
   475     if (osStatus==noErr && defaultButton) {
       
   476         Boolean anotherButtonIsTracking = flag ? TRUE : FALSE;
       
   477         osStatus = SetControlData(defaultButton, kControlNoPart, kControlPushButtonAnotherButtonTrackingTag, sizeof(Boolean), &anotherButtonIsTracking);
       
   478         if (osStatus==noErr) DrawOneControl(defaultButton);
       
   479         else NSLog(@"Some data couldn't be set in a Carbon control.");
       
   480     }
       
   481 
       
   482 }
       
   483 */
       
   484 
       
   485 
       
   486 // Do the right thing for a Carbon window.
       
   487 - (void)_cancelKey:(id)sender {
       
   488 
       
   489     // 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.
       
   490     ControlRef cancelButton;
       
   491     GetWindowCancelButton(_windowRef, &cancelButton);
       
   492     if (cancelButton) {
       
   493         if (IsControlActive(cancelButton)) {
       
   494             HIViewSimulateClick(cancelButton, kControlButtonPart, 0, NULL);
       
   495         }
       
   496     }
       
   497 
       
   498 }
       
   499 
       
   500 
       
   501 
       
   502 // Do the right thing for a Carbon window.
       
   503 - (void)_commonAwake {
       
   504 
       
   505     // 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.
       
   506     NSLog(@"-[NSCarbonWindow _commonAwake] is not implemented.");
       
   507 
       
   508 }
       
   509 
       
   510 
       
   511 // 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:].
       
   512 
       
   513 
       
   514 // Do the right thing for a Carbon window.
       
   515 - _destroyRealWindow:(BOOL)orderingOut {
       
   516 
       
   517     // Complain, because this should never be called.  We don't support one-shot NSCarbonWindows.
       
   518     NSLog(@"-[NSCarbonWindow _destroyRealWindow:] is not implemented.");
       
   519     return self;
       
   520     
       
   521 }
       
   522 
       
   523 
       
   524 // 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.
       
   525 
       
   526 
       
   527 // 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.
       
   528 
       
   529 
       
   530 // 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.
       
   531 
       
   532 
       
   533 // 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.
       
   534 
       
   535 
       
   536 // 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:].
       
   537 
       
   538 
       
   539 // 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:].
       
   540 
       
   541 
       
   542 // Do what NSWindow would do, but then sychronize the Carbon window structures.
       
   543 - (void)_oldPlaceWindow:(NSRect)frameRect {
       
   544 
       
   545     OSStatus osStatus;
       
   546 
       
   547     // Do the standard Cocoa thing.
       
   548     [super _oldPlaceWindow:frameRect];
       
   549 
       
   550     // Tell Carbon to update its various regions.
       
   551     // 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
       
   552     osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
       
   553     if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
       
   554 
       
   555 }
       
   556 
       
   557 
       
   558 // 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.
       
   559 
       
   560 
       
   561 // 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.
       
   562 
       
   563 
       
   564 // 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.
       
   565 
       
   566 
       
   567 // 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.
       
   568 
       
   569 
       
   570 // 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.
       
   571 
       
   572 
       
   573 // 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:.
       
   574 
       
   575 
       
   576 // 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.
       
   577 
       
   578 
       
   579 // 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.
       
   580 
       
   581 
       
   582 // Do what NSWindow would do, but for a Carbon window.
       
   583 // This function is mostly cut-and-pasted from -[NSWindow _termWindowIfOwner].  M.P. Notice - 8/7/00
       
   584 - (void)_termWindowIfOwner {
       
   585     [self _setWindowNumber:-1];
       
   586     _wFlags.isTerminating = YES;
       
   587     if (_windowRef && _windowRefIsOwned) DisposeWindow(_windowRef);
       
   588     // KW - need to clear window shadow state so it gets reset correctly when new window created
       
   589 //    if ([_borderView respondsToSelector:@selector(setShadowState:)]) {
       
   590 //        [_borderView setShadowState:kFrameShadowNone];
       
   591 //    }
       
   592     _wFlags.isTerminating = NO;
       
   593 }
       
   594 
       
   595 
       
   596 // 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.
       
   597 
       
   598 
       
   599 // 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.
       
   600 
       
   601 
       
   602 // Do what NSWindow would do, but then sychronize the Carbon window structures.
       
   603 - (void)_windowMovedToRect:(NSRect)actualFrame {
       
   604 
       
   605     OSStatus osStatus;
       
   606 
       
   607     // Do the standard Cocoa thing.
       
   608     [super _windowMovedToRect:actualFrame];
       
   609 
       
   610     // Let Carbon know that the window has been moved, unless this method is being called "early."
       
   611     if (_wFlags.visible) {
       
   612         osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
       
   613         if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
       
   614     }
       
   615 
       
   616 }
       
   617 
       
   618 - (NSRect)constrainFrameRect:(NSRect)actualFrame toScreen:(NSScreen *)screen {
       
   619     // let Carbon decide window size and position
       
   620     return actualFrame;
       
   621 }
       
   622 
       
   623 - (void)selectKeyViewFollowingView:(NSView *)aView {
       
   624 	HIViewRef	view = NULL;
       
   625 	
       
   626 	view = [HIViewAdapter getHIViewForNSView:aView];
       
   627 	
       
   628 	if ( view )
       
   629 	{	
       
   630 		HIViewRef	contentView;
       
   631 		
       
   632 		GetRootControl( GetControlOwner( view ), &contentView );
       
   633 		HIViewAdvanceFocus( contentView, 0 );
       
   634 	}
       
   635 	else
       
   636 	{
       
   637 		[super selectKeyViewFollowingView:aView];
       
   638 	}
       
   639 }
       
   640 
       
   641 - (void)selectKeyViewPrecedingView:(NSView *)aView {
       
   642 	HIViewRef	view = NULL;
       
   643 	
       
   644 	view = [HIViewAdapter getHIViewForNSView:aView];
       
   645 	
       
   646 	if ( view )
       
   647 	{	
       
   648 		HIViewRef	contentView;
       
   649 		
       
   650 		GetRootControl( GetControlOwner( view ), &contentView );
       
   651 		HIViewAdvanceFocus( contentView, shiftKey );
       
   652 	}
       
   653 	else
       
   654 	{
       
   655 		[super selectKeyViewPrecedingView:aView];
       
   656 	}
       
   657 }
       
   658 
       
   659 - (void)makeKeyWindow {
       
   660 	[NSApp _setMouseActivationInProgress:NO];
       
   661 	[NSApp setIsActive:YES];
       
   662 	[super makeKeyWindow];
       
   663 	WKShowKeyAndMain();
       
   664 }
       
   665 
       
   666 
       
   667 // Do the right thing for a Carbon window.
       
   668 - (BOOL)canBecomeKeyWindow {
       
   669 
       
   670     return YES;
       
   671 }
       
   672 
       
   673 // Do the right thing for a Carbon window.
       
   674 - (BOOL)canBecomeMainWindow {
       
   675     OSStatus osStatus;
       
   676     WindowClass windowClass;
       
   677     // By default, Carbon windows cannot become the main window.
       
   678     // What about when the default isn't right?  Requiring subclassing seems harsh.  M.P. Warning - 9/17/01
       
   679     // KW -  modify this to allow document windows to become main
       
   680     // 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
       
   681     osStatus = GetWindowClass(_windowRef, &windowClass);
       
   682     return (osStatus == noErr && windowClass == kDocumentWindowClass);
       
   683     
       
   684 }
       
   685 
       
   686 
       
   687 // 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.
       
   688 
       
   689 
       
   690 // 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.
       
   691 
       
   692 
       
   693 
       
   694 // 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.
       
   695 
       
   696 
       
   697 // Do the right thing for a Carbon window.
       
   698 - (void)encodeWithCoder:(NSCoder *)coder {
       
   699 
       
   700     // Actually, this will probably never be implemented.  M.P. Notice - 8/2/00
       
   701     NSLog(@"-[NSCarbonWindow encodeWithCoder:] is not implemented.");
       
   702 
       
   703 }
       
   704 
       
   705 
       
   706 // 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.
       
   707 
       
   708 
       
   709 // Do the right thing for a Carbon window.
       
   710 - (id)initWithCoder:(NSCoder *)coder {
       
   711 
       
   712     // Actually, this will probably never be implemented.  M.P. Notice - 8/2/00
       
   713     NSLog(@"-[NSCarbonWindow initWithCoder:] is not implemented.");
       
   714     [self release];
       
   715     return nil;
       
   716 
       
   717 }
       
   718 
       
   719 
       
   720 // 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.
       
   721 
       
   722 
       
   723 // 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.
       
   724 
       
   725 
       
   726 // There's no override of resizeToScreenWithEvent:, despite the fact that NSWindow's operates on _windowNum.
       
   727 // 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.
       
   728 
       
   729 /*
       
   730 // Do the right thing for a Carbon Window.
       
   731 - (void)sendEvent:(NSEvent *)theEvent {
       
   732 
       
   733     // Not all events are handled in the same manner.
       
   734     NSEventType eventType = [theEvent type];
       
   735     if (eventType==NSAppKitDefined) {
       
   736 
       
   737         // Handle the event the Cocoa way.  Carbon won't understand it anyway.
       
   738         [super sendEvent:theEvent];
       
   739 
       
   740     } 
       
   741 }
       
   742 */
       
   743 
       
   744 // 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.
       
   745 
       
   746 
       
   747 // 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().
       
   748 
       
   749 
       
   750 // Do what NSWindow would do, but for a Carbon window.
       
   751 - (void)setContentView:(NSView *)aView {
       
   752 
       
   753     NSRect contentFrameRect;
       
   754     OSStatus osStatus;
       
   755     Rect windowContentBoundsRect;
       
   756     
       
   757     // Precondition check.
       
   758     assert(_borderView);
       
   759     assert([_borderView isKindOfClass:[CarbonWindowFrame class]]);
       
   760     assert(_windowRef);
       
   761 
       
   762     // Parameter check.
       
   763     assert(aView);
       
   764     assert([aView isKindOfClass:[CarbonWindowContentView class]]);
       
   765 
       
   766     // Find out the window's Carbon window structure region (content) bounds.
       
   767     osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
       
   768     if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
       
   769     contentFrameRect.origin = NSZeroPoint;
       
   770     contentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
       
   771     contentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
       
   772 
       
   773     // If the content view is still in some other view hierarchy, pry it free.
       
   774     [_contentView removeFromSuperview];
       
   775     assert(![_contentView superview]);
       
   776 
       
   777     // Record the content view, and size it to this window's content frame.
       
   778     _contentView = aView;
       
   779     [_contentView setFrame:contentFrameRect];
       
   780 
       
   781     // Make the content view a subview of the border view.
       
   782     [_borderView addSubview:_contentView];
       
   783 
       
   784     // Tell the content view it's new place in the responder chain.
       
   785     [_contentView setNextResponder:self];
       
   786 
       
   787 }
       
   788 
       
   789 
       
   790 // 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.
       
   791 
       
   792 
       
   793 - (BOOL)worksWhenModal {
       
   794     WindowClass windowClass = [self _carbonWindowClass];
       
   795     return (windowClass == kFloatingWindowClass || windowClass == kUtilityWindowClass);
       
   796 }
       
   797 
       
   798 - (void)_setModalWindowLevel {
       
   799     return;
       
   800 }
       
   801 
       
   802 - _clearModalWindowLevel {
       
   803     return nil;
       
   804 }
       
   805 
       
   806 // 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.
       
   807 // 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
       
   808 
       
   809 
       
   810 // 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.
       
   811 
       
   812 
       
   813 - (UInt32)carbonHICommandIDFromActionSelector:(SEL)inActionSelector {
       
   814 
       
   815     // Initialize with the default return value.
       
   816     UInt32 hiCommandID = 0;
       
   817 
       
   818     // Pretty simple, if tedious.
       
   819     if (inActionSelector==@selector(clear:)) hiCommandID = kHICommandClear;
       
   820     else if (inActionSelector==@selector(copy:)) hiCommandID = kHICommandCopy;
       
   821     else if (inActionSelector==@selector(cut:)) hiCommandID = kHICommandCut;
       
   822     else if (inActionSelector==@selector(paste:)) hiCommandID = kHICommandPaste;
       
   823     else if (inActionSelector==@selector(redo:)) hiCommandID = kHICommandRedo;
       
   824     else if (inActionSelector==@selector(selectAll:)) hiCommandID = kHICommandSelectAll;
       
   825     else if (inActionSelector==@selector(undo:)) hiCommandID = kHICommandUndo;
       
   826 
       
   827     // Done.
       
   828     return hiCommandID;
       
   829 
       
   830 }
       
   831 
       
   832 
       
   833 - (void)sendCarbonProcessHICommandEvent:(UInt32)inHICommandID {
       
   834 
       
   835     EventTargetRef eventTargetRef;
       
   836     HICommand hiCommand;
       
   837     OSStatus osStatus;
       
   838 
       
   839     // Initialize for safe error handling.
       
   840     EventRef eventRef = NULL;
       
   841 
       
   842     // 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.
       
   843     hiCommand.attributes = 0;
       
   844     hiCommand.commandID = inHICommandID;
       
   845     hiCommand.menu.menuRef = 0;
       
   846     hiCommand.menu.menuItemIndex = 0;
       
   847     osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandProcess, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
       
   848     if (osStatus!=noErr) {
       
   849         NSLog(@"CreateEvent() returned %i.", (int)osStatus);
       
   850         goto CleanUp;  
       
   851     }
       
   852     osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
       
   853     if (osStatus!=noErr) {
       
   854         NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
       
   855         goto CleanUp;
       
   856     }
       
   857 
       
   858     // Send a Carbon event to whatever has the Carbon user focus.
       
   859     eventTargetRef = GetUserFocusEventTarget();
       
   860     osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
       
   861     if (osStatus!=noErr) {
       
   862         NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
       
   863         goto CleanUp;
       
   864     }
       
   865 
       
   866 CleanUp:
       
   867 
       
   868     // Clean up.
       
   869     if (eventRef) ReleaseEvent(eventRef);
       
   870 
       
   871 }
       
   872 
       
   873 
       
   874 - (Boolean)sendCarbonUpdateHICommandStatusEvent:(UInt32)inHICommandID withMenuRef:(MenuRef)inMenuRef andMenuItemIndex:(UInt16)inMenuItemIndex {
       
   875 
       
   876     EventTargetRef eventTargetRef;
       
   877     HICommand hiCommand;
       
   878     OSStatus osStatus;
       
   879 
       
   880     // Initialize for safe error handling and flag returning.
       
   881     Boolean eventWasHandled = FALSE;
       
   882     EventRef eventRef = NULL;
       
   883 
       
   884     // 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.
       
   885     hiCommand.attributes = kHICommandFromMenu;
       
   886     hiCommand.commandID = inHICommandID;
       
   887     hiCommand.menu.menuRef = inMenuRef;
       
   888     hiCommand.menu.menuItemIndex = inMenuItemIndex;
       
   889     osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandUpdateStatus, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
       
   890     if (osStatus!=noErr) {
       
   891         NSLog(@"CreateEvent() returned %i.", (int)osStatus);
       
   892         goto CleanUp;
       
   893     }
       
   894     osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
       
   895     if (osStatus!=noErr) {
       
   896         NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
       
   897         goto CleanUp;
       
   898     }
       
   899 
       
   900     // Send a Carbon event to whatever has the Carbon user focus.
       
   901     eventTargetRef = GetUserFocusEventTarget();
       
   902     osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
       
   903     if (osStatus==noErr) {
       
   904         eventWasHandled = TRUE;
       
   905     } else if (osStatus!=eventNotHandledErr) {
       
   906         NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
       
   907         goto CleanUp;
       
   908     }
       
   909 
       
   910 CleanUp:
       
   911 
       
   912     // Clean up.
       
   913     if (eventRef) ReleaseEvent(eventRef);
       
   914 
       
   915     // Done.
       
   916     return eventWasHandled;
       
   917 
       
   918 }
       
   919 
       
   920 - (void)_handleRootBoundsChanged
       
   921 {
       
   922 	HIViewRef	root = HIViewGetRoot( _windowRef ); 
       
   923 	HIRect		frame;
       
   924 
       
   925 	HIViewGetFrame( root, &frame );
       
   926 	[_borderView setFrameSize:*(NSSize*)&frame.size];
       
   927 }
       
   928 
       
   929 - (void)_handleContentBoundsChanged
       
   930 {
       
   931 	HIViewRef	root, contentView; 
       
   932 	HIRect		rootBounds, contentFrame;
       
   933 	NSRect		oldContentFrameRect;
       
   934 
       
   935 	root = HIViewGetRoot( _windowRef );
       
   936 	HIViewFindByID( root, kHIViewWindowContentID, &contentView );
       
   937 	HIViewGetFrame( contentView, &contentFrame );
       
   938 	HIViewGetBounds( root, &rootBounds );
       
   939 	
       
   940     // Set the content view's frame rect from the Carbon window's content region bounds.
       
   941     contentFrame.origin.y = rootBounds.size.height - CGRectGetMaxY( contentFrame );
       
   942 
       
   943     oldContentFrameRect = [_contentView frame];
       
   944     if ( !NSEqualRects( *(NSRect*)&contentFrame, oldContentFrameRect ) ) {
       
   945         [_contentView setFrame:*(NSRect*)&contentFrame];
       
   946     }
       
   947 }
       
   948 
       
   949 - (OSStatus)_handleCarbonEvent:(EventRef)inEvent callRef:(EventHandlerCallRef)inCallRef {
       
   950     OSStatus result = eventNotHandledErr;
       
   951     
       
   952     switch ( GetEventClass( inEvent ) )
       
   953     {
       
   954 		case kEventClassControl:
       
   955 			{
       
   956 				ControlRef		control;
       
   957 				
       
   958 				check( GetEventKind( inEvent ) == kEventControlBoundsChanged );
       
   959 				
       
   960 				GetEventParameter( inEvent, kEventParamDirectObject, typeControlRef, NULL,
       
   961 						sizeof( ControlRef ), NULL, &control );
       
   962 				
       
   963 				if ( control == HIViewGetRoot( _windowRef ) )
       
   964 					[self _handleRootBoundsChanged];
       
   965 				else
       
   966 					[self _handleContentBoundsChanged];
       
   967 			}
       
   968 			break;
       
   969 			
       
   970     	case kEventClassWindow:
       
   971     		switch ( GetEventKind( inEvent ) )
       
   972     		{
       
   973     			case kEventWindowShown:
       
   974 					[self _setVisible:YES];
       
   975     				break;
       
   976     			
       
   977     			case kEventWindowHidden:
       
   978 					[self _setVisible:NO];
       
   979     				break;
       
   980     			
       
   981     			case kEventWindowActivated:
       
   982 					[self makeKeyWindow];
       
   983 					break;
       
   984 				
       
   985     			case kEventWindowDeactivated:
       
   986 					[self resignKeyWindow];
       
   987 					break;
       
   988 				
       
   989 				case kEventWindowBoundsChanged:
       
   990 					[self reconcileToCarbonWindowBounds];
       
   991 					break;
       
   992 			}
       
   993     		break;
       
   994    	}
       
   995    	
       
   996     return result;
       
   997 }
       
   998 
       
   999 // Handle various events that Carbon is sending to our window.
       
  1000 static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData) {
       
  1001 
       
  1002     // default action is to send event to next handler.  We modify osStatus as necessary where we don't want this behavior
       
  1003     OSStatus osStatus = eventNotHandledErr;
       
  1004 
       
  1005     // We do different things for different event types.
       
  1006     CarbonWindowAdapter *carbonWindow = (CarbonWindowAdapter *)inUserData;
       
  1007 
       
  1008 	osStatus = [carbonWindow _handleCarbonEvent: inEventRef callRef: inEventHandlerCallRef];
       
  1009 	
       
  1010     // Done.  If we want to propagate the event, we return eventNotHandledErr to send it to the next handler
       
  1011     return osStatus;
       
  1012     
       
  1013 }
       
  1014 
       
  1015 // [3364117] We need to make sure this does not fall through to the AppKit implementation! bad things happen.
       
  1016 - (void)_reallyDoOrderWindow:(NSWindowOrderingMode)place relativeTo:(int)otherWin findKey:(BOOL)doKeyCalc forCounter:(BOOL)isACounter force:(BOOL)doForce isModal:(BOOL)isModal {
       
  1017 }
       
  1018 
       
  1019 - (NSRect) _growBoxRect
       
  1020 {
       
  1021       WindowAttributes                attrs;
       
  1022       NSRect                                  retRect = NSZeroRect;
       
  1023 
       
  1024       GetWindowAttributes( _windowRef, &attrs );
       
  1025 
       
  1026       if ( attrs & kWindowResizableAttribute )
       
  1027       {
       
  1028               HIRect          bounds, rect;
       
  1029               HIViewRef   view;
       
  1030 
       
  1031               HIViewGetBounds( HIViewGetRoot( _windowRef ), &bounds );
       
  1032               HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowGrowBoxID, &view );
       
  1033               HIViewGetFrame( view, &rect );
       
  1034 
       
  1035               rect.origin.y = bounds.size.height - CGRectGetMaxY( rect ) - 1;
       
  1036               rect.origin.x++;
       
  1037 
       
  1038               retRect = *(NSRect*)&rect;
       
  1039       }
       
  1040 
       
  1041       return retRect;
       
  1042 }
       
  1043 
       
  1044 @end // implementation CarbonWindowAdapter
       
  1045 
       
  1046 #endif