2011-04-09 18:54:59 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2010 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/external_popup_menu_mac.h"
|
|
|
|
#include "libcef/browser_impl.h"
|
2011-04-09 18:54:59 +02:00
|
|
|
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebExternalPopupMenuClient.h"
|
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenuInfo.h"
|
2011-12-21 21:02:39 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
|
2011-04-09 18:54:59 +02:00
|
|
|
#include "webkit/glue/webmenurunner_mac.h"
|
|
|
|
|
|
|
|
ExternalPopupMenu::ExternalPopupMenu(
|
|
|
|
BrowserWebViewDelegate* delegate,
|
|
|
|
const WebKit::WebPopupMenuInfo& popup_menu_info,
|
|
|
|
WebKit::WebExternalPopupMenuClient* popup_menu_client)
|
|
|
|
: delegate_(delegate),
|
|
|
|
popup_menu_info_(popup_menu_info),
|
|
|
|
popup_menu_client_(popup_menu_client) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalPopupMenu::show(const WebKit::WebRect& bounds) {
|
|
|
|
// Display a HTML select menu.
|
|
|
|
|
|
|
|
std::vector<WebMenuItem> items;
|
|
|
|
for (size_t i = 0; i < popup_menu_info_.items.size(); ++i)
|
|
|
|
items.push_back(popup_menu_info_.items[i]);
|
|
|
|
|
|
|
|
double font_size = popup_menu_info_.itemFontSize;
|
|
|
|
int selected_index = popup_menu_info_.selectedIndex;
|
|
|
|
bool right_aligned = popup_menu_info_.rightAligned;
|
|
|
|
|
|
|
|
CefBrowserImpl* browser = delegate_->GetBrowser();
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
NSView* view = nil;
|
|
|
|
NSRect view_rect;
|
|
|
|
|
|
|
|
if (!browser->IsWindowRenderingDisabled()) {
|
|
|
|
view = browser->UIT_GetWebViewWndHandle();
|
|
|
|
view_rect = [view bounds];
|
|
|
|
} else {
|
|
|
|
view = browser->UIT_GetMainWndHandle();
|
|
|
|
if (view != nil) {
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
// Retrieve the view rect.
|
|
|
|
CefRect rect;
|
|
|
|
CefRefPtr<CefRenderHandler> render_handler = client->GetRenderHandler();
|
|
|
|
if (render_handler.get() &&
|
|
|
|
render_handler->GetViewRect(browser, rect)) {
|
|
|
|
view_rect = NSMakeRect(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == nil || view_rect.size.width == 0 || view_rect.size.height == 0) {
|
|
|
|
popup_menu_client_->didCancel();
|
|
|
|
delegate_->ClosePopupMenu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-09 18:54:59 +02:00
|
|
|
// Set up the menu position.
|
|
|
|
int y_offset = bounds.y + bounds.height;
|
|
|
|
NSRect position = NSMakeRect(bounds.x, view_rect.size.height - y_offset,
|
|
|
|
bounds.width, bounds.height);
|
|
|
|
|
|
|
|
// Display the menu.
|
|
|
|
scoped_nsobject<WebMenuRunner> menu_runner;
|
|
|
|
menu_runner.reset([[WebMenuRunner alloc] initWithItems:items
|
|
|
|
fontSize:font_size
|
|
|
|
rightAligned:right_aligned]);
|
|
|
|
|
2012-05-16 18:56:38 +02:00
|
|
|
[menu_runner runMenuInView:view
|
2011-04-09 18:54:59 +02:00
|
|
|
withBounds:position
|
|
|
|
initialIndex:selected_index];
|
|
|
|
|
|
|
|
if ([menu_runner menuItemWasChosen])
|
|
|
|
popup_menu_client_->didAcceptIndex([menu_runner indexOfSelectedItem]);
|
|
|
|
else
|
|
|
|
popup_menu_client_->didCancel();
|
|
|
|
|
|
|
|
delegate_->ClosePopupMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalPopupMenu::close() {
|
|
|
|
popup_menu_client_ = NULL;
|
|
|
|
delegate_ = NULL;
|
|
|
|
}
|