Home | History | Annotate | Download | only in renderer
      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   render_view_->Send(new ViewHostMsg_HidePopup(render_view_->routing_id()));
     58   render_view_->DidHideExternalPopupMenu();
     59   // |this| was deleted.
     60 }
     61 
     62 #if defined(OS_MACOSX)
     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 }
     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 
     85 }  // namespace content
     86