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#import <AppKit/AppKit.h>
6
7#include "base/logging.h"
8#include "build/build_config.h"
9#include "content/child/npapi/plugin_instance.h"
10
11// When C++ exceptions are disabled, the C++ library defines |try| and
12// |catch| so as to allow exception-expecting C++ code to build properly when
13// language support for exceptions is not present.  These macros interfere
14// with the use of |@try| and |@catch| in Objective-C files such as this one.
15// Undefine these macros here, after everything has been #included, since
16// there will be no C++ uses and only Objective-C uses from this point on.
17#undef try
18#undef catch
19
20namespace content {
21
22NPError PluginInstance::PopUpContextMenu(NPMenu* menu) {
23  if (!currently_handled_event_)
24    return NPERR_GENERIC_ERROR;
25
26  CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID());
27  NSPoint screen_point = NSMakePoint(
28      plugin_origin_.x() + currently_handled_event_->data.mouse.pluginX,
29      plugin_origin_.y() + currently_handled_event_->data.mouse.pluginY);
30  // Plugin offsets are upper-left based, so flip vertically for Cocoa.
31  screen_point.y = main_display_bounds.size.height - screen_point.y;
32
33  NSMenu* nsmenu = reinterpret_cast<NSMenu*>(menu);
34  NPError return_val = NPERR_NO_ERROR;
35  @try {
36    [nsmenu popUpMenuPositioningItem:nil atLocation:screen_point inView:nil];
37  }
38  @catch (NSException* e) {
39    NSLog(@"Caught exception while handling PopUpContextMenu: %@", e);
40    return_val = NPERR_GENERIC_ERROR;
41  }
42
43  return return_val;
44}
45
46ScopedCurrentPluginEvent::ScopedCurrentPluginEvent(PluginInstance* instance,
47                                                   NPCocoaEvent* event)
48    : instance_(instance) {
49  instance_->set_currently_handled_event(event);
50}
51
52ScopedCurrentPluginEvent::~ScopedCurrentPluginEvent() {
53  instance_->set_currently_handled_event(NULL);
54}
55
56}  // namespace content
57