[email protected] | 24d2b17 | 2012-05-26 00:54:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 5572215 | 2011-03-22 01:33:53 | [diff] [blame] | 5 | #include "content/renderer/external_popup_menu.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 6 | |
[email protected] | 5572215 | 2011-03-22 01:33:53 | [diff] [blame] | 7 | #include "content/common/view_messages.h" |
[email protected] | 54bf995 | 2013-07-17 06:43:20 | [diff] [blame] | 8 | #include "content/renderer/menu_item_builder.h" |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 9 | #include "content/renderer/render_view_impl.h" |
[email protected] | 5c30b5e0 | 2013-05-30 03:46:08 | [diff] [blame] | 10 | #include "third_party/WebKit/public/platform/WebRect.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 11 | #include "third_party/WebKit/public/web/WebExternalPopupMenuClient.h" |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 12 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 13 | namespace content { |
14 | |||||
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 15 | ExternalPopupMenu::ExternalPopupMenu( |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 16 | RenderViewImpl* render_view, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 17 | const blink::WebPopupMenuInfo& popup_menu_info, |
18 | blink::WebExternalPopupMenuClient* popup_menu_client) | ||||
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 19 | : render_view_(render_view), |
20 | popup_menu_info_(popup_menu_info), | ||||
[email protected] | b2e4c7013 | 2013-10-03 02:07:51 | [diff] [blame] | 21 | popup_menu_client_(popup_menu_client), |
22 | origin_scale_for_emulation_(0) { | ||||
23 | } | ||||
24 | |||||
[email protected] | 9a2d7ee3 | 2013-12-05 12:15:49 | [diff] [blame] | 25 | void ExternalPopupMenu::SetOriginScaleAndOffsetForEmulation( |
26 | float scale, const gfx::Point& offset) { | ||||
[email protected] | b2e4c7013 | 2013-10-03 02:07:51 | [diff] [blame] | 27 | origin_scale_for_emulation_ = scale; |
[email protected] | 9a2d7ee3 | 2013-12-05 12:15:49 | [diff] [blame] | 28 | origin_offset_for_emulation_ = offset; |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 29 | } |
30 | |||||
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 31 | void ExternalPopupMenu::show(const blink::WebRect& bounds) { |
32 | blink::WebRect rect = bounds; | ||||
[email protected] | b2e4c7013 | 2013-10-03 02:07:51 | [diff] [blame] | 33 | if (origin_scale_for_emulation_) { |
34 | rect.x *= origin_scale_for_emulation_; | ||||
35 | rect.y *= origin_scale_for_emulation_; | ||||
36 | } | ||||
[email protected] | 9a2d7ee3 | 2013-12-05 12:15:49 | [diff] [blame] | 37 | rect.x += origin_offset_for_emulation_.x(); |
38 | rect.y += origin_offset_for_emulation_.y(); | ||||
[email protected] | b2e4c7013 | 2013-10-03 02:07:51 | [diff] [blame] | 39 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 40 | ViewHostMsg_ShowPopup_Params popup_params; |
[email protected] | b2e4c7013 | 2013-10-03 02:07:51 | [diff] [blame] | 41 | popup_params.bounds = rect; |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 42 | 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] | 54bf995 | 2013-07-17 06:43:20 | [diff] [blame] | 45 | 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] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 49 | popup_params.right_aligned = popup_menu_info_.rightAligned; |
[email protected] | 24d2b17 | 2012-05-26 00:54:12 | [diff] [blame] | 50 | popup_params.allow_multiple_selection = |
51 | popup_menu_info_.allowMultipleSelection; | ||||
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 52 | render_view_->Send( |
53 | new ViewHostMsg_ShowPopup(render_view_->routing_id(), popup_params)); | ||||
54 | } | ||||
55 | |||||
56 | void ExternalPopupMenu::close() { | ||||
[email protected] | 8ff26a8c | 2014-03-24 02:59:40 | [diff] [blame] | 57 | render_view_->Send(new ViewHostMsg_HidePopup(render_view_->routing_id())); |
58 | render_view_->DidHideExternalPopupMenu(); | ||||
59 | // |this| was deleted. | ||||
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 60 | } |
61 | |||||
[email protected] | 24d2b17 | 2012-05-26 00:54:12 | [diff] [blame] | 62 | #if defined(OS_MACOSX) |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 63 | void 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] | 24d2b17 | 2012-05-26 00:54:12 | [diff] [blame] | 71 | #endif |
72 | |||||
73 | #if defined(OS_ANDROID) | ||||
74 | void 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] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 85 | } // namespace content |