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