PageRenderTime 36ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PluginPDFViewMenu.mm

http://firefox-mac-pdf.googlecode.com/
Objective C++ | 190 lines | 114 code | 33 blank | 43 comment | 21 complexity | 7c278c4b112fd4dde798f509efa5ed3d MD5 | raw file
  1. /*
  2. * Copyright (C) 2005, 2006, 2007 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. #import "PluginInstance.h"
  33. // This is the implementation of the menu.
  34. // This includes source code from the WebPDFView.mm in the WebKit project
  35. static void _applicationInfoForMIMEType(NSString *type, NSString **name, NSImage **image)
  36. {
  37. NSURL *appURL = nil;
  38. OSStatus error = LSCopyApplicationForMIMEType((CFStringRef)type, kLSRolesAll, (CFURLRef *)&appURL);
  39. if (error != noErr)
  40. return;
  41. NSString *appPath = [appURL path];
  42. CFRelease (appURL);
  43. *image = [[NSWorkspace sharedWorkspace] iconForFile:appPath];
  44. [*image setSize:NSMakeSize(16.f,16.f)];
  45. NSString *appName = [[NSFileManager defaultManager] displayNameAtPath:appPath];
  46. *name = appName;
  47. }
  48. @implementation PluginPDFView (PluginPDFViewMenu)
  49. - (BOOL)acceptsFirstResponder {
  50. return YES;
  51. }
  52. - (BOOL)becomeFirstResponder
  53. {
  54. // This works together with setNextKeyView to splice our PDFSubview into
  55. // the key loop similar to the way NSScrollView does this.
  56. NSWindow *window = [self window];
  57. id newFirstResponder = nil;
  58. if ([window keyViewSelectionDirection] == NSSelectingPrevious) {
  59. NSView *previousValidKeyView = [self previousValidKeyView];
  60. if ((previousValidKeyView != self) && (previousValidKeyView != pdfView))
  61. newFirstResponder = previousValidKeyView;
  62. } else {
  63. NSView *PDFDocumentView = [pdfView documentView];
  64. if ([PDFDocumentView acceptsFirstResponder])
  65. newFirstResponder = PDFDocumentView;
  66. }
  67. if (!newFirstResponder)
  68. return NO;
  69. if (![window makeFirstResponder:newFirstResponder])
  70. return NO;
  71. //[[dataSource webFrame] _clearSelectionInOtherFrames];
  72. return YES;
  73. }
  74. - (NSMenuItem*) menuItemOpenWithFinder
  75. {
  76. NSString *appName = nil;
  77. NSImage *appIcon = nil;
  78. _applicationInfoForMIMEType(@"application/pdf", &appName, &appIcon);
  79. if (!appName)
  80. appName = @"Finder";
  81. NSBundle* bundle = [NSBundle bundleForClass:[self class]];
  82. NSString* openStr = NSLocalizedStringFromTableInBundle(@"Open with %@", nil, bundle, @"Open PDF with application");
  83. // To match the PDFKit style, we'll add Open with Preview even when there's no document yet to view, and
  84. // disable it using validateUserInterfaceItem.
  85. NSString *title = [NSString stringWithFormat:openStr, appName];
  86. NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:@selector(openWithFinder:) keyEquivalent:@""];
  87. if (appIcon) {
  88. [item setImage:appIcon];
  89. }
  90. return [item autorelease];
  91. }
  92. - (int) menuInsertIndex:(NSMenu*)menu
  93. {
  94. NSSet* priorActions = [[NSSet alloc] initWithObjects:
  95. NSStringFromSelector(@selector(_searchInSpotlight:)),
  96. NSStringFromSelector(@selector(_searchInGoogle:)),
  97. NSStringFromSelector(@selector(_searchInDictionary:)),
  98. NSStringFromSelector(@selector(copy:)),
  99. nil];
  100. int length = [[menu itemArray] count];
  101. for (int i = 0; i < length; i++) {
  102. NSString* action = NSStringFromSelector([[menu itemAtIndex:i] action]);
  103. if (action != nil && ![priorActions containsObject:action]) {
  104. return i;
  105. }
  106. }
  107. return -1;
  108. }
  109. - (NSMenu *)menuForEvent:(NSEvent*)theEvent
  110. {
  111. NSMenu* menu = [pdfView menuForEvent:theEvent];
  112. int insertIndex = [self menuInsertIndex:menu];
  113. NSBundle* bundle = [NSBundle bundleForClass:[self class]];
  114. NSString* printStr = NSLocalizedStringFromTableInBundle(@"Print File...", nil, bundle, @"Print PDF file");
  115. NSString* saveStr = NSLocalizedStringFromTableInBundle(@"Save File As...", nil, bundle, @"Save PDF file");
  116. // Add the Open with Preview/Finder item
  117. [menu insertItem:[NSMenuItem separatorItem] atIndex:insertIndex];
  118. [menu insertItem:[self menuItemOpenWithFinder] atIndex:insertIndex];
  119. [menu insertItem:[NSMenuItem separatorItem] atIndex:insertIndex];
  120. [menu insertItemWithTitle:printStr action:@selector(doPrint:) keyEquivalent:@"" atIndex:insertIndex];
  121. [menu insertItemWithTitle:saveStr action:@selector(saveAs:) keyEquivalent:@"" atIndex:insertIndex];
  122. // Swizzle the search in google
  123. NSEnumerator *e = [[menu itemArray] objectEnumerator];
  124. NSMenuItem *item;
  125. for (int i = 0; (item = [e nextObject]) != nil; i++) {
  126. NSString *actionString = NSStringFromSelector([item action]);
  127. if ([actionString isEqualToString:NSStringFromSelector(@selector(_searchInGoogle:))]) {
  128. [item setAction:@selector(googleInFirefox:)];
  129. break;
  130. }
  131. }
  132. return menu;
  133. }
  134. // Code courtesy of Chris Wegg
  135. // TODO: possibly implement with gecko API
  136. - (void)googleInFirefox:(id)sender {
  137. //Get selection, URL encode it, add it to the google search string, and ask the OS to open that URL.
  138. //Should open a new firefox tab provided its the default browser.
  139. PDFSelection *selection = [pdfView currentSelection];
  140. NSString *escapedselection=(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
  141. (CFStringRef) [selection string], NULL, NULL, kCFStringEncodingUTF8);
  142. NSString *searchurl=[NSString stringWithFormat:@"http://www.google.com/search?q=%@",escapedselection];
  143. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:searchurl]];
  144. }
  145. - (void)doPrint:(id)sender
  146. {
  147. [plugin print];
  148. }
  149. - (void)saveAs:(id)sender
  150. {
  151. [plugin save];
  152. }
  153. - (void)openWithFinder:(id)sender
  154. {
  155. [plugin openWithFinder];
  156. }
  157. @end