• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/renderer/external_popup_menu.h"
6 
7 #include "content/common/frame_messages.h"
8 #include "content/renderer/menu_item_builder.h"
9 #include "content/renderer/render_frame_impl.h"
10 #include "third_party/WebKit/public/platform/WebRect.h"
11 #include "third_party/WebKit/public/web/WebExternalPopupMenuClient.h"
12 
13 namespace content {
14 
ExternalPopupMenu(RenderFrameImpl * render_frame,const blink::WebPopupMenuInfo & popup_menu_info,blink::WebExternalPopupMenuClient * popup_menu_client)15 ExternalPopupMenu::ExternalPopupMenu(
16     RenderFrameImpl* render_frame,
17     const blink::WebPopupMenuInfo& popup_menu_info,
18     blink::WebExternalPopupMenuClient* popup_menu_client)
19     : render_frame_(render_frame),
20       popup_menu_info_(popup_menu_info),
21       popup_menu_client_(popup_menu_client),
22       origin_scale_for_emulation_(0) {
23 }
24 
SetOriginScaleAndOffsetForEmulation(float scale,const gfx::Point & offset)25 void ExternalPopupMenu::SetOriginScaleAndOffsetForEmulation(
26     float scale, const gfx::Point& offset) {
27   origin_scale_for_emulation_ = scale;
28   origin_offset_for_emulation_ = offset;
29 }
30 
show(const blink::WebRect & bounds)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_;
36   }
37   rect.x += origin_offset_for_emulation_.x();
38   rect.y += origin_offset_for_emulation_.y();
39 
40   FrameHostMsg_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]));
48   }
49   popup_params.right_aligned = popup_menu_info_.rightAligned;
50   popup_params.allow_multiple_selection =
51       popup_menu_info_.allowMultipleSelection;
52   render_frame_->Send(
53       new FrameHostMsg_ShowPopup(render_frame_->GetRoutingID(), popup_params));
54 }
55 
close()56 void ExternalPopupMenu::close()  {
57   render_frame_->Send(
58       new FrameHostMsg_HidePopup(render_frame_->GetRoutingID()));
59   render_frame_->DidHideExternalPopupMenu();
60   // |this| was deleted.
61 }
62 
63 #if defined(OS_MACOSX)
DidSelectItem(int index)64 void ExternalPopupMenu::DidSelectItem(int index) {
65   if (!popup_menu_client_)
66     return;
67   if (index == -1)
68     popup_menu_client_->didCancel();
69   else
70     popup_menu_client_->didAcceptIndex(index);
71 }
72 #endif
73 
74 #if defined(OS_ANDROID)
DidSelectItems(bool canceled,const std::vector<int> & indices)75 void ExternalPopupMenu::DidSelectItems(bool canceled,
76                                        const std::vector<int>& indices) {
77   if (!popup_menu_client_)
78     return;
79   if (canceled)
80     popup_menu_client_->didCancel();
81   else
82     popup_menu_client_->didAcceptIndices(indices);
83 }
84 #endif
85 
86 }  // namespace content
87