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/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" 12 13 namespace content { 14 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) { 23 } 24 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 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 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])); 48 } 49 popup_params.right_aligned = popup_menu_info_.rightAligned; 50 popup_params.allow_multiple_selection = 51 popup_menu_info_.allowMultipleSelection; 52 render_view_->Send( 53 new ViewHostMsg_ShowPopup(render_view_->routing_id(), popup_params)); 54 } 55 56 void ExternalPopupMenu::close() { 57 popup_menu_client_ = NULL; 58 render_view_ = NULL; 59 } 60 61 #if defined(OS_MACOSX) 62 void ExternalPopupMenu::DidSelectItem(int index) { 63 if (!popup_menu_client_) 64 return; 65 if (index == -1) 66 popup_menu_client_->didCancel(); 67 else 68 popup_menu_client_->didAcceptIndex(index); 69 } 70 #endif 71 72 #if defined(OS_ANDROID) 73 void ExternalPopupMenu::DidSelectItems(bool canceled, 74 const std::vector<int>& indices) { 75 if (!popup_menu_client_) 76 return; 77 if (canceled) 78 popup_menu_client_->didCancel(); 79 else 80 popup_menu_client_->didAcceptIndices(indices); 81 } 82 #endif 83 84 } // namespace content 85