1 // Copyright (c) 2012 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.
5 #include "content/renderer/external_popup_menu.h"
7 #include "content/common/view_messages.h"
8 #include "content/renderer/menu_item_builder.h"
9 #include "content/renderer/render_view_impl.h"
10 #include "third_party/WebKit/public/platform/WebRect.h"
11 #include "third_party/WebKit/public/web/WebExternalPopupMenuClient.h"
15 ExternalPopupMenu::ExternalPopupMenu(
16 RenderViewImpl
* render_view
,
17 const blink::WebPopupMenuInfo
& popup_menu_info
,
18 blink::WebExternalPopupMenuClient
* popup_menu_client
)
19 : render_view_(render_view
),
20 popup_menu_info_(popup_menu_info
),
21 popup_menu_client_(popup_menu_client
),
22 origin_scale_for_emulation_(0) {
25 void ExternalPopupMenu::SetOriginScaleAndOffsetForEmulation(
26 float scale
, const gfx::Point
& offset
) {
27 origin_scale_for_emulation_
= scale
;
28 origin_offset_for_emulation_
= offset
;
31 void ExternalPopupMenu::show(const blink::WebRect
& bounds
) {
32 blink::WebRect rect
= bounds
;
33 if (origin_scale_for_emulation_
) {
34 rect
.x
*= origin_scale_for_emulation_
;
35 rect
.y
*= origin_scale_for_emulation_
;
37 rect
.x
+= origin_offset_for_emulation_
.x();
38 rect
.y
+= origin_offset_for_emulation_
.y();
40 ViewHostMsg_ShowPopup_Params popup_params
;
41 popup_params
.bounds
= rect
;
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
;
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
]));
49 popup_params
.right_aligned
= popup_menu_info_
.rightAligned
;
50 popup_params
.allow_multiple_selection
=
51 popup_menu_info_
.allowMultipleSelection
;
53 new ViewHostMsg_ShowPopup(render_view_
->routing_id(), popup_params
));
56 void ExternalPopupMenu::close() {
57 render_view_
->Send(new ViewHostMsg_HidePopup(render_view_
->routing_id()));
58 render_view_
->DidHideExternalPopupMenu();
59 // |this| was deleted.
62 #if defined(OS_MACOSX)
63 void ExternalPopupMenu::DidSelectItem(int index
) {
64 if (!popup_menu_client_
)
67 popup_menu_client_
->didCancel();
69 popup_menu_client_
->didAcceptIndex(index
);
73 #if defined(OS_ANDROID)
74 void ExternalPopupMenu::DidSelectItems(bool canceled
,
75 const std::vector
<int>& indices
) {
76 if (!popup_menu_client_
)
79 popup_menu_client_
->didCancel();
81 popup_menu_client_
->didAcceptIndices(indices
);
85 } // namespace content