PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 1ms 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. static BOOL retValuePerformKeyEquivalent;
  25. @interface NSMenu (PDFAltMethod)
  26. - (BOOL)altPerformKeyEquivalent:(NSEvent*)theEvent;
  27. @end
  28. @implementation NSMenu (PDFAltMethod)
  29. - (BOOL)altPerformKeyEquivalent:(NSEvent*)theEvent
  30. {
  31. retValuePerformKeyEquivalent = [self altPerformKeyEquivalent:theEvent];
  32. return retValuePerformKeyEquivalent;
  33. }
  34. @end
  35. @implementation PluginPDFView
  36. - (PDFView*)pdfView
  37. {
  38. return pdfView;
  39. }
  40. - (void)dealloc
  41. {
  42. [pdfView release];
  43. [super dealloc];
  44. }
  45. - (void)awakeFromNib
  46. {
  47. [self initPDFViewWithFrame:[self frame]];
  48. }
  49. - (NSView *)hitTest:(NSPoint)point
  50. {
  51. // We override hit test and invert the next responder loop so that
  52. // we can preview all mouse events. Our next responder is the view
  53. // that would have receieved the event. We make sure the pdfView has
  54. // a nil next responder to prevent an infinite loop.
  55. // This is a terrible HACK. There must be a better way...
  56. [self setNextResponder:[super hitTest:point]];
  57. [pdfView setNextResponder:nil];
  58. return self;
  59. }
  60. - (void)mouseDown:(NSEvent*)theEvent
  61. {
  62. NSResponder* firstResponder = [[[self window] firstResponder] retain];
  63. // pass mouse down event to parent view (to claim browser focus from other XUL elements)
  64. [[self superview] mouseDown:theEvent];
  65. // reclaim focus
  66. [[self window] makeFirstResponder:firstResponder];
  67. // process event
  68. [super mouseDown:theEvent];
  69. [firstResponder release];
  70. // used by SelectionController
  71. [[NSNotificationCenter defaultCenter] postNotificationName:@"mouseDown" object:self];
  72. }
  73. - (BOOL)handleCommonKeyEvents:(NSEvent*)theEvent
  74. {
  75. /*
  76. Here we have to redefine all key bindings, users expect to work in a PDFView.
  77. Probably still incomplete.
  78. */
  79. switch ([theEvent keyCode]) {
  80. case 0x31: // Space
  81. case 0x7C: // Right
  82. [pdfView scrollPageDown:nil];
  83. return YES;
  84. //case 0x33: // Backspace (most people will use this for "Go Back")
  85. case 0x7B: // Left
  86. [pdfView scrollPageUp:nil];
  87. return YES;
  88. case 0x7D: // Down
  89. [pdfView scrollLineDown:nil];
  90. return YES;
  91. case 0x7E: // Up
  92. [pdfView scrollLineUp:nil];
  93. return YES;
  94. }
  95. return NO;
  96. }
  97. - (void)keyDown:(NSEvent*)theEvent
  98. {
  99. // NSLog(@"keyDown: %d", [theEvent keyCode]);
  100. if (![self handleCommonKeyEvents:theEvent]) {
  101. [[[self superview] superview] keyDown:theEvent];
  102. }
  103. }
  104. - (BOOL)performKeyEquivalent:(NSEvent*)theEvent
  105. {
  106. // NSLog(@"PluginPDFView performKeyEquivalent: %d", [theEvent keyCode]);
  107. switch ([theEvent keyCode])
  108. {
  109. case 24: // CMD+'='
  110. [pdfView zoomIn:nil];
  111. return YES;
  112. case 27: // CMD+'-'
  113. [pdfView zoomOut:nil];
  114. return YES;
  115. case 8: // CMD+'c'
  116. [pdfView copy:nil];
  117. return YES;
  118. }
  119. return [self handleCommonKeyEvents:theEvent];
  120. }
  121. @end