📄 webframeview.mm
字号:
/* * Copyright (C) 2005, 2006, 2007, 2008 Apple 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. */#import "WebFrameView.h"#import "WebClipView.h"#import "WebDataSourcePrivate.h"#import "WebDocument.h"#import "WebDynamicScrollBarsViewInternal.h"#import "WebFrame.h"#import "WebFrameInternal.h"#import "WebFrameViewInternal.h"#import "WebFrameViewPrivate.h"#import "WebHistoryItemInternal.h"#import "WebHTMLViewPrivate.h"#import "WebKeyGenerator.h"#import "WebKitErrorsPrivate.h"#import "WebKitStatisticsPrivate.h"#import "WebKitVersionChecks.h"#import "WebNSDictionaryExtras.h"#import "WebNSObjectExtras.h"#import "WebNSPasteboardExtras.h"#import "WebNSViewExtras.h"#import "WebNSWindowExtras.h"#import "WebPDFView.h"#import "WebPreferenceKeysPrivate.h"#import "WebSystemInterface.h"#import "WebViewFactory.h"#import "WebViewInternal.h"#import "WebViewPrivate.h"#import <Foundation/NSURLRequest.h>#import <WebCore/DragController.h>#import <WebCore/EventHandler.h>#import <WebCore/Frame.h>#import <WebCore/FrameView.h>#import <WebCore/HistoryItem.h>#import <WebCore/Page.h>#import <WebCore/RenderPart.h>#import <WebCore/ThreadCheck.h>#import <WebCore/WebCoreFrameView.h>#import <WebCore/WebCoreView.h>#import <WebKitSystemInterface.h>#import <wtf/Assertions.h>using namespace WebCore;@interface NSWindow (WindowPrivate)- (BOOL)_needsToResetDragMargins;- (void)_setNeedsToResetDragMargins:(BOOL)s;@end@interface NSClipView (AppKitSecretsIKnow)- (BOOL)_scrollTo:(const NSPoint *)newOrigin animate:(BOOL)animate; // need the boolean result from this method@endenum { SpaceKey = 0x0020};@interface WebFrameView (WebFrameViewFileInternal) <WebCoreFrameView>- (float)_verticalKeyboardScrollDistance;@end@interface WebFrameViewPrivate : NSObject {@public WebFrame *webFrame; WebDynamicScrollBarsView *frameScrollView;}@end@implementation WebFrameViewPrivate- (void)dealloc{ [frameScrollView release]; [super dealloc];}@end@implementation WebFrameView (WebFrameViewFileInternal)- (float)_verticalKeyboardScrollDistance{ // Arrow keys scroll the same distance that clicking the scroll arrow does. return [[self _scrollView] verticalLineScroll];}- (Frame*)_web_frame{ return core(_private->webFrame);}@end@implementation WebFrameView (WebInternal)// Note that the WebVew is not retained.- (WebView *)_webView{ return [_private->webFrame webView];}- (void)_setDocumentView:(NSView <WebDocumentView> *)view{ WebDynamicScrollBarsView *sv = [self _scrollView]; core([self _webView])->dragController()->setDidInitiateDrag(false); [sv setSuppressLayout:YES]; // If the old view is the first responder, transfer first responder status to the new view as // a convenience and so that we don't leave the window pointing to a view that's no longer in it. NSWindow *window = [sv window]; NSResponder *firstResponder = [window firstResponder]; bool makeNewViewFirstResponder = [firstResponder isKindOfClass:[NSView class]] && [(NSView *)firstResponder isDescendantOf:[sv documentView]]; // Suppress the resetting of drag margins since we know we can't affect them. BOOL resetDragMargins = [window _needsToResetDragMargins]; [window _setNeedsToResetDragMargins:NO]; [sv setDocumentView:view]; [window _setNeedsToResetDragMargins:resetDragMargins]; if (makeNewViewFirstResponder) [window makeFirstResponder:view]; [sv setSuppressLayout:NO];}-(NSView <WebDocumentView> *)_makeDocumentViewForDataSource:(WebDataSource *)dataSource{ NSString* MIMEType = [dataSource _responseMIMEType]; if (!MIMEType) MIMEType = @"text/html"; Class viewClass = [[self class] _viewClassForMIMEType:MIMEType]; NSView <WebDocumentView> *documentView; if (viewClass) { // If the dataSource's representation has already been created, and it is also the // same class as the desired documentView, then use it as the documentView instead // of creating another one (Radar 4340787). id <WebDocumentRepresentation> dataSourceRepresentation = [dataSource representation]; if (dataSourceRepresentation && [dataSourceRepresentation class] == viewClass) documentView = (NSView <WebDocumentView> *)[dataSourceRepresentation retain]; else documentView = [[viewClass alloc] initWithFrame:[self bounds]]; } else documentView = nil; [self _setDocumentView:documentView]; [documentView release]; return documentView;}- (void)_setWebFrame:(WebFrame *)webFrame{ if (!webFrame) { NSView *docV = [self documentView]; if ([docV respondsToSelector:@selector(close)]) [docV performSelector:@selector(close)]; } // Not retained because the WebView owns the WebFrame, which owns the WebFrameView. _private->webFrame = webFrame; }- (WebDynamicScrollBarsView *)_scrollView{ // This can be called by [super dealloc] when cleaning up the key view loop, // after _private has been nilled out. if (_private == nil) return nil; return _private->frameScrollView;}- (float)_verticalPageScrollDistance{ float overlap = [self _verticalKeyboardScrollDistance]; float height = [[self _contentView] bounds].size.height; return (height < overlap) ? height / 2 : height - overlap;}static inline void addTypesFromClass(NSMutableDictionary *allTypes, Class objCClass, NSArray *supportTypes){ NSEnumerator *enumerator = [supportTypes objectEnumerator]; ASSERT(enumerator != nil); NSString *mime = nil; while ((mime = [enumerator nextObject]) != nil) { // Don't clobber previously-registered classes. if ([allTypes objectForKey:mime] == nil) [allTypes setObject:objCClass forKey:mime]; }}+ (NSMutableDictionary *)_viewTypesAllowImageTypeOmission:(BOOL)allowImageTypeOmission{ static NSMutableDictionary *viewTypes = nil; static BOOL addedImageTypes = NO; if (!viewTypes) { viewTypes = [[NSMutableDictionary alloc] init]; addTypesFromClass(viewTypes, [WebHTMLView class], [WebHTMLView supportedNonImageMIMETypes]); // Since this is a "secret default" we don't bother registering it. BOOL omitPDFSupport = [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitOmitPDFSupport"]; if (!omitPDFSupport) addTypesFromClass(viewTypes, [WebPDFView class], [WebPDFView supportedMIMETypes]); } if (!addedImageTypes && !allowImageTypeOmission) { addTypesFromClass(viewTypes, [WebHTMLView class], [WebHTMLView supportedImageMIMETypes]); addedImageTypes = YES; } return viewTypes;}+ (BOOL)_canShowMIMETypeAsHTML:(NSString *)MIMEType{ return [[[self _viewTypesAllowImageTypeOmission:YES] _webkit_objectForMIMEType:MIMEType] isSubclassOfClass:[WebHTMLView class]];}+ (Class)_viewClassForMIMEType:(NSString *)MIMEType{ Class viewClass; return [WebView _viewClass:&viewClass andRepresentationClass:nil forMIMEType:MIMEType] ? viewClass : nil;}- (void)_install{ ASSERT(_private->webFrame); ASSERT(_private->frameScrollView); Frame* frame = core(_private->webFrame); ASSERT(frame); ASSERT(frame->page()); // If this isn't the main frame, it must have an owner element set, or it // won't ever get installed in the view hierarchy. ASSERT(frame == frame->page()->mainFrame() || frame->ownerElement()); FrameView* view = frame->view(); view->setPlatformWidget(_private->frameScrollView); // FIXME: Frame tries to do this too. Is this code needed? if (RenderPart* owner = frame->ownerRenderer()) { owner->setWidget(view); // Now the render part owns the view, so we don't any more. } view->initScrollbars();}@end@implementation WebFrameView- initWithCoder:(NSCoder *)decoder{ // Older nibs containing WebViews will also contain WebFrameViews. We need to keep track of // their count also to match the decrement in -dealloc. ++WebFrameViewCount; return [super initWithCoder:decoder];}- initWithFrame:(NSRect)frame{ self = [super initWithFrame:frame]; if (!self) return nil; static bool didFirstTimeInitialization; if (!didFirstTimeInitialization) { didFirstTimeInitialization = true; InitWebCoreSystemInterface(); // Need to tell WebCore what function to call for the "History Item has Changed" notification. // Note: We also do this in WebHistoryItem's init method. WebCore::notifyHistoryItemChanged = WKNotifyHistoryItemChanged; [WebViewFactory createSharedFactory]; [WebKeyGenerator createSharedGenerator]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // CoreGraphics deferred updates are disabled if WebKitEnableCoalescedUpdatesPreferenceKey is NO // or has no value. For compatibility with Mac OS X 10.4.6, deferred updates are off by default. if (![defaults boolForKey:WebKitEnableDeferredUpdatesPreferenceKey]) WKDisableCGDeferredUpdates(); if (!WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_MAIN_THREAD_EXCEPTIONS)) setDefaultThreadViolationBehavior(LogOnFirstThreadViolation); } _private = [[WebFrameViewPrivate alloc] init]; WebDynamicScrollBarsView *scrollView = [[WebDynamicScrollBarsView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, frame.size.width, frame.size.height)]; _private->frameScrollView = scrollView; [scrollView setContentView:[[[WebClipView alloc] initWithFrame:[scrollView bounds]] autorelease]]; [scrollView setDrawsBackground:NO]; [scrollView setHasVerticalScroller:NO]; [scrollView setHasHorizontalScroller:NO]; [scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [scrollView setLineScroll:40.0f];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -