OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "build/build_config.h" | |
6 | |
7 #import <AppKit/AppKit.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "webkit/glue/plugins/plugin_instance.h" | |
11 | |
12 // When C++ exceptions are disabled, the C++ library defines |try| and | |
13 // |catch| so as to allow exception-expecting C++ code to build properly when | |
14 // language support for exceptions is not present. These macros interfere | |
15 // with the use of |@try| and |@catch| in Objective-C files such as this one. | |
16 // Undefine these macros here, after everything has been #included, since | |
17 // there will be no C++ uses and only Objective-C uses from this point on. | |
18 #undef try | |
19 #undef catch | |
20 | |
21 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 | |
22 @interface NSMenu (SnowLeopardMenuPopUpDeclaration) | |
23 - (BOOL)popUpMenuPositioningItem:(NSMenuItem*)item | |
24 atLocation:(NSPoint)location | |
25 inView:(NSView*)view; | |
26 @end | |
27 #endif | |
28 | |
29 namespace { | |
30 | |
31 // Returns an autoreleased NSEvent constructed from the given np_event, | |
32 // targeting the given window. | |
33 NSEvent* NSEventForNPCocoaEvent(NPCocoaEvent* np_event, NSWindow* window) { | |
34 bool mouse_down = 1; | |
35 switch (np_event->type) { | |
36 case NPCocoaEventMouseDown: | |
37 mouse_down = 1; | |
38 break; | |
39 case NPCocoaEventMouseUp: | |
40 mouse_down = 0; | |
41 break; | |
42 default: | |
43 // If plugins start bringing up context menus for things other than | |
44 // clicks, this will need more plumbing; for now just log it and proceed | |
45 // as if it were a mouse down. | |
46 NOTREACHED(); | |
47 } | |
48 NSEventType event_type = NSLeftMouseDown; | |
49 switch (np_event->data.mouse.buttonNumber) { | |
50 case 0: | |
51 event_type = mouse_down ? NSLeftMouseDown : NSLeftMouseUp; | |
52 break; | |
53 case 1: | |
54 event_type = mouse_down ? NSRightMouseDown : NSRightMouseUp; | |
55 break; | |
56 default: | |
57 event_type = mouse_down ? NSOtherMouseDown : NSOtherMouseUp; | |
58 break; | |
59 } | |
60 | |
61 NSInteger click_count = np_event->data.mouse.clickCount; | |
62 NSInteger modifiers = np_event->data.mouse.modifierFlags; | |
63 // NPCocoaEvent doesn't have a timestamp, so just use the current time. | |
64 NSEvent* event = | |
65 [NSEvent mouseEventWithType:event_type | |
66 location:NSMakePoint(0, 0) | |
67 modifierFlags:modifiers | |
68 timestamp:[[NSApp currentEvent] timestamp] | |
69 windowNumber:[window windowNumber] | |
70 context:[NSGraphicsContext currentContext] | |
71 eventNumber:0 | |
72 clickCount:click_count | |
73 pressure:1.0]; | |
74 return event; | |
75 } | |
76 | |
77 } // namespace | |
78 | |
79 namespace NPAPI { | |
80 | |
81 NPError PluginInstance::PopUpContextMenu(NPMenu* menu) { | |
82 if (!currently_handled_event_) | |
83 return NPERR_GENERIC_ERROR; | |
84 | |
85 CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID()); | |
86 NSPoint screen_point = NSMakePoint( | |
87 plugin_origin_.x() + currently_handled_event_->data.mouse.pluginX, | |
88 plugin_origin_.y() + currently_handled_event_->data.mouse.pluginY); | |
89 // Plugin offsets are upper-left based, so flip vertically for Cocoa. | |
90 screen_point.y = main_display_bounds.size.height - screen_point.y; | |
91 | |
92 NSMenu* nsmenu = reinterpret_cast<NSMenu*>(menu); | |
93 NPError return_val = NPERR_NO_ERROR; | |
94 NSWindow* window = nil; | |
95 @try { | |
96 if ([nsmenu respondsToSelector: | |
97 @selector(popUpMenuPositioningItem:atLocation:inView:)]) { | |
98 [nsmenu popUpMenuPositioningItem:nil atLocation:screen_point inView:nil]; | |
99 } else { | |
100 NSRect dummy_window_rect = NSMakeRect(screen_point.x, screen_point.y, | |
101 1, 1); | |
102 window = [[NSWindow alloc] initWithContentRect:dummy_window_rect | |
103 styleMask:NSBorderlessWindowMask | |
104 backing:NSBackingStoreNonretained | |
105 defer:YES]; | |
106 [window setTitle:@"PopupMenuDummy"]; // Lets interposing identify it. | |
107 [window setAlphaValue:0]; | |
108 [window makeKeyAndOrderFront:nil]; | |
109 [NSMenu popUpContextMenu:nsmenu | |
110 withEvent:NSEventForNPCocoaEvent(currently_handled_event_, | |
111 window) | |
112 forView:[window contentView]]; | |
113 } | |
114 } | |
115 @catch (NSException* e) { | |
116 NSLog(@"Caught exception while handling PopUpContextMenu: %@", e); | |
117 return_val = NPERR_GENERIC_ERROR; | |
118 } | |
119 | |
120 if (window) { | |
121 @try { | |
122 [window orderOut:nil]; | |
123 [window release]; | |
124 } | |
125 @catch (NSException* e) { | |
126 NSLog(@"Caught exception while cleaning up in PopUpContextMenu: %@", e); | |
127 } | |
128 } | |
129 | |
130 return return_val; | |
131 } | |
132 | |
133 } // namespace NPAPI | |
OLD | NEW |