⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carbonwindowadapter.mm

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 MM
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1.  Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer.  * 2.  Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution.  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of *     its contributors may be used to endorse or promote products derived *     from this software without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */// I don't think this class belongs in WebKit. Lets move it out.// Things that I've never bothered working out:// For non-sheet windows, handle Carbon WindowMove events so as to do the same things as -[NSWindow _windowMoved].// Check to see how this stuff deals with various screen size change scenarious.// M.P. Warning - 9/17/01// 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.// _auxiliaryStorage->auxWFlags.hasShadow will always be false if the Carbon window has a kWindowNoShadowAttribute, and vice versa.// _auxiliaryStorage->_auxWFlags.minimized will always reflect the window's Carbon collapsed state.// _borderView will always point to an NSCarbonWindowFrame.// _contentView will always point to an NSCarbonWindowContentView;// _frame will always reflect the window's Carbon kWindowStructureRgn bounds.// _styleMask will always have _NSCarbonWindowMask set, and will have NSClosableWindowMask, NSMiniaturizableWindowMask, NSResizableWindowMask, and/or NSTitledWindowMask set as appropriate.// _wflags.oneShot and _wflags.delayedOneShot will always be false.// _wFlags.visible will always reflect the window's Carbon visibility.// _windowNum will always be greater than zero, and valid.// 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// Things that have to be worked on if NSCarbonWindows are ever used for something other than dialogs and sheets:// Clicking on an NSCarbonWindow while a Cocoa app-modal dialog is shown does not beep, as it should [old bug, maybe fixed now].// Handling of mouse clicks or key presses for any window control (close, miniaturize, zoom) might not be all there.// Handling of miniaturization of Carbon windows via title bar double-click might not be all there.// The background on NSCarbonWindowTester's sample window (not sample dialog or sample sheet) might be wrong.// The controls on NSCarbonWindowTester's sample window look inactive when the window is inactive, but have first-click behavior.// M.P. Warning - 12/14/00// 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#ifndef __LP64__#import "CarbonWindowAdapter.h"#import "CarbonWindowFrame.h"#import "CarbonWindowContentView.h"#import "HIViewAdapter.h"#import <WebKitSystemInterface.h>#import <AppKit/AppKit.h>//#import <CoreGraphics/CGSWindow.h>#import <HIToolbox/CarbonEvents.h>#import <HIToolbox/Controls.h>#import <HIToolbox/HIView.h>#import <assert.h>#import <WebCore/WebCoreObjCExtras.h>#import <runtime/InitializeThreading.h>#import "WebKitLogging.h"#import "WebNSObjectExtras.h"#import "WebTypesInternal.h"@interface NSWindow(HIWebFrameView)- _initContent:(const NSRect *)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag contentView:aView;- (void)_oldPlaceWindow:(NSRect)frameRect;- (void)_windowMovedToRect:(NSRect)actualFrame;- (void)_setWindowNumber:(NSInteger)nativeWindow;- (NSGraphicsContext *)_threadContext;- (void)_setFrame:(NSRect)newWindowFrameRect;- (void)_setVisible:(BOOL)flag;@end@interface NSApplication(HIWebFrameView)- (void)setIsActive:(BOOL)aFlag;- (id)_setMouseActivationInProgress:(BOOL)flag;- (BOOL)_handleKeyEquivalent:(NSEvent*)theEvent;@end@interface NSInputContext- (BOOL)processInputKeyBindings:(NSEvent *)event;@end// Forward declarations.static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData);@implementation CarbonWindowAdapter// Return an appropriate window frame class.+ (Class)frameViewClassForStyleMask:(unsigned int)style {    // There's only one appropriate window style, and only one appropriate window frame class.    assert(style & WKCarbonWindowMask());    return [CarbonWindowFrame class];}// Overriding of the parent class' designated initializer, just for safety's sake.- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)style backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {    // Do the standard Cocoa thing.    self = [super initWithContentRect:contentRect styleMask:style backing:bufferingType defer:flag];    if (self==nil) return nil;    // Simple.    _windowRef = NULL;    _windowRefIsOwned = NO;    _eventHandler = NULL;        // Done.    return self;}// 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.- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned disableOrdering:(BOOL)inDisableOrdering carbon:(BOOL)inCarbon {    NSBackingStoreType backingStoreType;    CarbonWindowContentView *carbonWindowContentView;    NSWindow *windowAsProperty;    OSStatus osStatus;    UInt32 windowFeatures;    WindowAttributes windowAttributes;    unsigned int styleMask;    void *nativeWindow;    WindowModality windowModality;	ControlRef		contentView;	    // Simple.    // 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    _windowRef = inWindowRef;    //_auxiliaryStorage->_windowRef = inWindowRef;    _windowRefIsOwned = inWindowRefIsOwned;	_carbon = inCarbon;    // Find out the window's CoreGraphics window reference.    nativeWindow = WKGetNativeWindowFromWindowRef(inWindowRef);    // Find out the window's Carbon window attributes.    GetWindowAttributes(inWindowRef, &windowAttributes);    // Find out the window's Carbon window features.    GetWindowFeatures(inWindowRef, &windowFeatures);    // Figure out the window's backing store type.    // At one time, this had code stolen from CreatePlatformWindow in HIToolbox/Windows/Platform/CGSPlatform.c	// But now the non-retained window class is a Carbon secret that's not even in	// WindowsPriv.h; maybe we'll have to revisit this if someone needs to use WebKit	// in a non-retained window.    backingStoreType = NSBackingStoreRetained;    // Figure out the window's style mask.    styleMask = WKCarbonWindowMask();    if (windowAttributes & kWindowCloseBoxAttribute) styleMask |= NSClosableWindowMask;    if (windowAttributes & kWindowResizableAttribute) styleMask |= NSResizableWindowMask;    if (windowFeatures & kWindowCanCollapse) styleMask |= NSMiniaturizableWindowMask;    if (windowFeatures & kWindowHasTitleBar) styleMask |= NSTitledWindowMask;    osStatus = GetWindowModality(_windowRef, &windowModality, NULL);    if (osStatus != noErr) {        NSLog(@"Couldn't get window modality: error=%d", osStatus);        return nil;    }        // Create one of our special content views.    carbonWindowContentView = [[[CarbonWindowContentView alloc] init] autorelease];    // 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.    self = (CarbonWindowAdapter*)[super _initContent:NULL styleMask:styleMask backing:backingStoreType defer:YES contentView:carbonWindowContentView];    if (!self) return nil;    assert(_contentView);    // Record accurately whether or not this window has a shadow, in case someone asks. //   _auxiliaryStorage->_auxWFlags.hasShadow = (windowAttributes & kWindowNoShadowAttribute) ? NO : YES;    // Record the window number.    [self _setWindowNumber:(NSInteger)nativeWindow];    // Set up from the frame rectangle.    // 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    [self reconcileToCarbonWindowBounds];    // Install an event handler for the Carbon window events in which we're interested.    const EventTypeSpec kEvents[] = {            { kEventClassWindow, kEventWindowActivated },            { kEventClassWindow, kEventWindowDeactivated },            { kEventClassWindow, kEventWindowBoundsChanged },            { kEventClassWindow, kEventWindowShown },            { kEventClassWindow, kEventWindowHidden }    };        const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };        osStatus = InstallEventHandler( GetWindowEventTarget(_windowRef), NSCarbonWindowHandleEvent, GetEventTypeCount( kEvents ), kEvents, (void*)self, &_eventHandler);    if (osStatus!=noErr) {            [self release];            return nil;    }    osStatus = InstallEventHandler( GetControlEventTarget( HIViewGetRoot( _windowRef ) ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);    if (osStatus!=noErr) {            [self release];            return nil;    }    HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowContentID, &contentView );    osStatus = InstallEventHandler( GetControlEventTarget( contentView ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);    if (osStatus!=noErr) {            [self release];            return nil;    }	    // Put a pointer to this Cocoa NSWindow in a Carbon window property tag.    // Right now, this is just used by NSViewCarbonControl.  M.P. Notice - 10/9/00    windowAsProperty = self;    osStatus = SetWindowProperty(_windowRef, WKCarbonWindowPropertyCreator(), WKCarbonWindowPropertyTag(), sizeof(NSWindow *), &windowAsProperty);    if (osStatus!=noErr) {        [self release];        return nil;    }    // 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.    _passingCarbonWindowActivationEvents = NO;    // Be sure to sync up visibility    [self _setVisible:(BOOL)IsWindowVisible( _windowRef )];    // Done.    return self;}- (void)setViewsNeedDisplay:(BOOL)wellDoThey {	// Make sure we can flush anything that needs it.	// We need to sync the context here. I was hoping I didn't need to do this,	// but apparently when scrolling, the AppKit view system draws directly.	// When this occurs, I cannot intercept it to make it draw in my HIView	// context. What ends up happening is that it draws, but nothing ever	// flushes it.	if ( [self windowNumber] != -1 )	{		CGContextRef cgContext = (CGContextRef)[[self _threadContext] graphicsPort];		CGContextSynchronize( cgContext );	}}+ (void)initialize{    JSC::initializeThreading();#ifndef BUILDING_ON_TIGER    WebCoreObjCFinalizeOnMainThread(self);#endif}// 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.- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned {    // 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.    return [self initWithCarbonWindowRef:inWindowRef takingOwnership:inWindowRefIsOwned disableOrdering:YES carbon:NO];}// Clean up.- (void)dealloc {    if (WebCoreObjCScheduleDeallocateOnMainThread([CarbonWindowAdapter class], self))        return;    // Clean up, if necessary.    // 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    if (_eventHandler) RemoveEventHandler(_eventHandler);    // Do the standard Cocoa thing.    [super dealloc];}- (void)finalize {    ASSERT_MAIN_THREAD();    if (_eventHandler) RemoveEventHandler(_eventHandler);    [super finalize];}- (WindowRef)windowRef {    // Simple.    return _windowRef;}// should always be YES, but check in order to avoid initialization or deallocation surprises- (BOOL)_hasWindowRef {    return (_windowRef != NULL);}// an NSCarbonWindow does not manage the windowRef.  The windowRef manages the NSCarbonWindow- (BOOL)_managesWindowRef {    return NO;}- (void)_removeWindowRef {    _windowRef = NULL;	    if (_eventHandler) RemoveEventHandler(_eventHandler);		_eventHandler = NULL;}- (WindowClass)_carbonWindowClass {    WindowClass windowClass = kDocumentWindowClass;    OSStatus osStatus;        if ([self _hasWindowRef]) {        osStatus = GetWindowClass([self windowRef], &windowClass);        if (osStatus != noErr) {            NSLog(@"Couldn't get window class: error=%d", osStatus);        }    }    return windowClass; }// 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.- (BOOL)reconcileToCarbonWindowBounds {    OSStatus osStatus;    NSRect newContentFrameRect;    NSRect newWindowFrameRect;    NSRect oldContentFrameRect;    Rect windowContentBoundsRect;    Rect windowStructureBoundsRect;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -