PageRenderTime 53ms CodeModel.GetById 30ms app.highlight 1ms RepoModel.GetById 19ms app.codeStats 0ms

/src/PluginPDFViewPDFKitBundle.mm

http://firefox-mac-pdf.googlecode.com/
Objective C++ | 145 lines | 78 code | 21 blank | 46 comment | 12 complexity | a610f38c1c77ef1d978bc003ebfb3457 MD5 | raw file
  1/*
  2 * Copyright (C) 2005, 2006, 2007, 2008 Apple 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/*
 30 * Modified by Sam Gross <colesbury@gmail.com> for use with Firefox PDF Plugin for Mac OS X.
 31 */
 32#import "PluginPDFView.h"
 33
 34extern "C" NSString *_NSPathForSystemFramework(NSString *framework);
 35
 36/*
 37 This is a category for PDFView to prevent it from handling keystrokes by itself.
 38 That means no scrolling etc., if we want that we'll have to implement it by hand.
 39 But it's necessary to have Firefox keybindings (and those implemented by other extensions)
 40 handled correctly.
 41 */
 42@interface PDFView (NoFocus)
 43- (void)keyDown:(NSEvent*)theEvent;
 44- (BOOL)performKeyEquivalent:(NSEvent*)theEvent;
 45@end
 46
 47@implementation PDFView (NoFocus)
 48
 49/*
 50 We forward all keyDown and performKeyEquivalent events down to the PluginPDFView.
 51 That superview-chain will be quite fragile when the view layout changes, it
 52 should be closely monitored.
 53 */
 54- (void)keyDown:(NSEvent*)theEvent
 55{
 56  [[[[self superview] superview] superview] keyDown:theEvent];
 57}
 58- (BOOL)performKeyEquivalent:(NSEvent*)theEvent
 59{
 60  return [[[[self superview] superview] superview] performKeyEquivalent:theEvent];
 61}
 62
 63@end
 64
 65
 66@interface PluginPDFView (FileInternal)
 67+ (Class)_PDFPreviewViewClass;
 68+ (Class)_PDFViewClass;
 69@end
 70
 71@implementation PluginPDFView (PDFKitBundle)
 72
 73+ (NSBundle *)PDFKitBundle
 74{
 75    static NSBundle *PDFKitBundle = nil;
 76    if (PDFKitBundle == nil) {
 77        NSString *PDFKitPath = [_NSPathForSystemFramework(@"Quartz.framework") stringByAppendingString:@"/Frameworks/PDFKit.framework"];
 78        if (PDFKitPath == nil) {
 79            NSLog(@"Couldn't find PDFKit.framework");
 80            return nil;
 81        }
 82        PDFKitBundle = [NSBundle bundleWithPath:PDFKitPath];
 83        if (![PDFKitBundle load]) {
 84            NSLog(@"Couldn't load PDFKit.framework");
 85        }
 86    }
 87    return PDFKitBundle;
 88}
 89
 90+ (Class)_PDFPreviewViewClass
 91{
 92    static Class PDFPreviewViewClass = nil;
 93    static BOOL checkedForPDFPreviewViewClass = NO;
 94    
 95    if (!checkedForPDFPreviewViewClass) {
 96        checkedForPDFPreviewViewClass = YES;
 97        PDFPreviewViewClass = [[PluginPDFView PDFKitBundle] classNamed:@"PDFPreviewView"];
 98    }
 99    
100    // This class might not be available; callers need to deal with a nil return here.
101    return PDFPreviewViewClass;
102}
103
104+ (Class)_PDFViewClass
105{
106    static Class PDFViewClass = nil;
107    if (PDFViewClass == nil) {
108        PDFViewClass = [[PluginPDFView PDFKitBundle] classNamed:@"PDFView"];
109        if (!PDFViewClass)
110            NSLog(@"Couldn't find PDFView class in PDFKit.framework");
111    }
112    return PDFViewClass;
113}
114
115// see initWithFrame:(NSRect)frame in WebPDFView.mm
116- (void)initPDFViewWithFrame:(NSRect)frame
117{
118   Class previewViewClass = [[self class] _PDFPreviewViewClass];
119
120   NSView *previewView = nil;
121   if (previewViewClass) {
122      previewView = [[previewViewClass alloc] initWithFrame:frame];
123   }
124
125   NSView *topLevelPDFKitView = nil;
126   if (previewView) {
127      // We'll retain the PDFSubview here so that it is equally retained in all
128      // code paths. That way we don't need to worry about conditionally releasing
129      // it later.
130      pdfView = [[previewView performSelector:@selector(pdfView)] retain];
131      topLevelPDFKitView = previewView;
132   } else {
133      pdfView = [[[[self class] _PDFViewClass] alloc] initWithFrame:frame];
134      topLevelPDFKitView = pdfView;
135   }
136
137   [topLevelPDFKitView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
138   [self addSubview:topLevelPDFKitView];
139
140   [pdfView setDelegate:plugin];
141   
142   [previewView release];
143}
144
145@end