blob: a1bb2475b73b01c0050aeb075aa27639f3302bee [file] [log] [blame]
[email protected]24d2b172012-05-26 00:54:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]caf706f2010-10-26 17:54:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]55722152011-03-22 01:33:535#include "content/renderer/external_popup_menu.h"
[email protected]caf706f2010-10-26 17:54:086
[email protected]55722152011-03-22 01:33:537#include "content/common/view_messages.h"
[email protected]54bf9952013-07-17 06:43:208#include "content/renderer/menu_item_builder.h"
[email protected]310ebd6302011-10-10 19:06:289#include "content/renderer/render_view_impl.h"
[email protected]5c30b5e02013-05-30 03:46:0810#include "third_party/WebKit/public/platform/WebRect.h"
[email protected]2255a9332013-06-17 05:12:3111#include "third_party/WebKit/public/web/WebExternalPopupMenuClient.h"
[email protected]caf706f2010-10-26 17:54:0812
[email protected]e9ff79c2012-10-19 21:31:2613namespace content {
14
[email protected]caf706f2010-10-26 17:54:0815ExternalPopupMenu::ExternalPopupMenu(
[email protected]310ebd6302011-10-10 19:06:2816 RenderViewImpl* render_view,
[email protected]180ef242013-11-07 06:50:4617 const blink::WebPopupMenuInfo& popup_menu_info,
18 blink::WebExternalPopupMenuClient* popup_menu_client)
[email protected]caf706f2010-10-26 17:54:0819 : render_view_(render_view),
20 popup_menu_info_(popup_menu_info),
[email protected]b2e4c70132013-10-03 02:07:5121 popup_menu_client_(popup_menu_client),
22 origin_scale_for_emulation_(0) {
23}
24
[email protected]9a2d7ee32013-12-05 12:15:4925void ExternalPopupMenu::SetOriginScaleAndOffsetForEmulation(
26 float scale, const gfx::Point& offset) {
[email protected]b2e4c70132013-10-03 02:07:5127 origin_scale_for_emulation_ = scale;
[email protected]9a2d7ee32013-12-05 12:15:4928 origin_offset_for_emulation_ = offset;
[email protected]caf706f2010-10-26 17:54:0829}
30
[email protected]180ef242013-11-07 06:50:4631void ExternalPopupMenu::show(const blink::WebRect& bounds) {
32 blink::WebRect rect = bounds;
[email protected]b2e4c70132013-10-03 02:07:5133 if (origin_scale_for_emulation_) {
34 rect.x *= origin_scale_for_emulation_;
35 rect.y *= origin_scale_for_emulation_;
36 }
[email protected]9a2d7ee32013-12-05 12:15:4937 rect.x += origin_offset_for_emulation_.x();
38 rect.y += origin_offset_for_emulation_.y();
[email protected]b2e4c70132013-10-03 02:07:5139
[email protected]caf706f2010-10-26 17:54:0840 ViewHostMsg_ShowPopup_Params popup_params;
[email protected]b2e4c70132013-10-03 02:07:5141 popup_params.bounds = rect;
[email protected]caf706f2010-10-26 17:54:0842 popup_params.item_height = popup_menu_info_.itemHeight;
43 popup_params.item_font_size = popup_menu_info_.itemFontSize;
44 popup_params.selected_item = popup_menu_info_.selectedIndex;
[email protected]54bf9952013-07-17 06:43:2045 for (size_t i = 0; i < popup_menu_info_.items.size(); ++i) {
46 popup_params.popup_items.push_back(
47 MenuItemBuilder::Build(popup_menu_info_.items[i]));
48 }
[email protected]caf706f2010-10-26 17:54:0849 popup_params.right_aligned = popup_menu_info_.rightAligned;
[email protected]24d2b172012-05-26 00:54:1250 popup_params.allow_multiple_selection =
51 popup_menu_info_.allowMultipleSelection;
[email protected]caf706f2010-10-26 17:54:0852 render_view_->Send(
53 new ViewHostMsg_ShowPopup(render_view_->routing_id(), popup_params));
54}
55
56void ExternalPopupMenu::close() {
[email protected]8ff26a8c2014-03-24 02:59:4057 render_view_->Send(new ViewHostMsg_HidePopup(render_view_->routing_id()));
58 render_view_->DidHideExternalPopupMenu();
59 // |this| was deleted.
[email protected]caf706f2010-10-26 17:54:0860}
61
[email protected]24d2b172012-05-26 00:54:1262#if defined(OS_MACOSX)
[email protected]caf706f2010-10-26 17:54:0863void ExternalPopupMenu::DidSelectItem(int index) {
64 if (!popup_menu_client_)
65 return;
66 if (index == -1)
67 popup_menu_client_->didCancel();
68 else
69 popup_menu_client_->didAcceptIndex(index);
70}
[email protected]24d2b172012-05-26 00:54:1271#endif
72
73#if defined(OS_ANDROID)
74void ExternalPopupMenu::DidSelectItems(bool canceled,
75 const std::vector<int>& indices) {
76 if (!popup_menu_client_)
77 return;
78 if (canceled)
79 popup_menu_client_->didCancel();
80 else
81 popup_menu_client_->didAcceptIndices(indices);
82}
83#endif
84
[email protected]e9ff79c2012-10-19 21:31:2685} // namespace content