PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 1ms RepoModel.GetById 59ms app.codeStats 0ms

/src/PluginPDFView.mm

http://firefox-mac-pdf.googlecode.com/
Objective C++ | 137 lines | 84 code | 16 blank | 37 comment | 3 complexity | 72286ad72cd557a1db75db841ce36c80 MD5 | raw file
  1/*
  2 * Copyright (c) 2008 Samuel Gross.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a copy
  5 * of this software and associated documentation files (the "Software"), to deal
  6 * in the Software without restriction, including without limitation the rights
  7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 * copies of the Software, and to permit persons to whom the Software is
  9 * furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 20 * THE SOFTWARE.
 21 */
 22#import "PluginPDFView.h"
 23#import "PluginInstance.h"
 24
 25static BOOL retValuePerformKeyEquivalent;
 26
 27@interface NSMenu (PDFAltMethod)
 28- (BOOL)altPerformKeyEquivalent:(NSEvent*)theEvent;
 29@end
 30
 31@implementation NSMenu (PDFAltMethod)
 32- (BOOL)altPerformKeyEquivalent:(NSEvent*)theEvent
 33{
 34  retValuePerformKeyEquivalent = [self altPerformKeyEquivalent:theEvent];
 35  return retValuePerformKeyEquivalent;
 36}
 37@end
 38
 39
 40@implementation PluginPDFView
 41
 42- (PDFView*)pdfView
 43{
 44  return pdfView;
 45}
 46
 47- (void)dealloc
 48{
 49  [pdfView release];
 50  [super dealloc];
 51}
 52
 53- (void)awakeFromNib
 54{
 55  [self initPDFViewWithFrame:[self frame]];
 56}
 57
 58- (NSView *)hitTest:(NSPoint)point
 59 {
 60  // We override hit test and invert the next responder loop so that
 61  // we can preview all mouse events. Our next responder is the view
 62  // that would have receieved the event. We make sure the pdfView has
 63  // a nil next responder to prevent an infinite loop.
 64  // This is a terrible HACK. There must be a better way...
 65  [self setNextResponder:[super hitTest:point]];
 66  [pdfView setNextResponder:nil];
 67  return self;
 68}
 69
 70- (void)mouseDown:(NSEvent*)theEvent
 71{
 72  NSResponder* firstResponder = [[[self window] firstResponder] retain];
 73  // pass mouse down event to parent view (to claim browser focus from other XUL elements)
 74  [[self superview] mouseDown:theEvent];
 75  // reclaim focus
 76  [[self window] makeFirstResponder:firstResponder];
 77  // process event
 78  [super mouseDown:theEvent];
 79  [firstResponder release];
 80  
 81  // used by SelectionController
 82  [[NSNotificationCenter defaultCenter] postNotificationName:@"mouseDown" object:self];  
 83}
 84
 85
 86- (BOOL)handleCommonKeyEvents:(NSEvent*)theEvent
 87{
 88  /*
 89   Here we have to redefine all key bindings, users expect to work in a PDFView.
 90   Probably still incomplete.
 91   */
 92  switch ([theEvent keyCode]) {
 93    case 0x31: // Space
 94    case 0x7C: // Right
 95      [pdfView scrollPageDown:nil];
 96      return YES;
 97    //case 0x33: // Backspace (most people will use this for "Go Back")
 98    case 0x7B: // Left
 99      [pdfView scrollPageUp:nil];
100      return YES;
101    case 0x7D: // Down
102      [pdfView scrollLineDown:nil];
103      return YES;
104    case 0x7E: // Up
105      [pdfView scrollLineUp:nil];
106      return YES;
107  }
108  return NO;
109}
110
111- (void)keyDown:(NSEvent*)theEvent
112{
113//  NSLog(@"keyDown: %d", [theEvent keyCode]);
114  if (![self handleCommonKeyEvents:theEvent]) {
115    [[[self superview] superview] keyDown:theEvent];
116  }
117}
118
119- (BOOL)performKeyEquivalent:(NSEvent*)theEvent
120{
121//  NSLog(@"PluginPDFView performKeyEquivalent: %d", [theEvent keyCode]);
122  switch ([theEvent keyCode])
123  {
124    case 24: // CMD+'='
125      [pdfView zoomIn:nil];
126      return YES;
127    case 27: // CMD+'-'
128      [pdfView zoomOut:nil];
129      return YES;
130    case 8: // CMD+'c'
131      [pdfView copy:nil];
132      return YES;
133  }
134  return [self handleCommonKeyEvents:theEvent];
135}
136
137@end