15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/location_bar/location_icon_view.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
8ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/views/website_settings/website_settings_popup_view.h"
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocationIconView::LocationIconView(LocationBarView* location_bar)
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : suppress_mouse_released_action_(false),
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      page_info_helper_(this, location_bar) {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON));
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LocationIconView::~LocationIconView() {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) {
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (event.IsOnlyMiddleMouseButton() &&
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION)) {
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::string16 text;
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ui::Clipboard::GetForCurrentThread()->ReadText(
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ui::CLIPBOARD_TYPE_SELECTION, &text);
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    text = OmniboxView::SanitizeTextForPaste(text);
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OmniboxEditModel* model =
300de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)        page_info_helper_.location_bar()->GetOmniboxView()->model();
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (model->CanPasteAndGo(text))
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      model->PasteAndGo(text);
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  suppress_mouse_released_action_ = WebsiteSettingsPopupView::IsPopupShowing();
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LocationIconView::OnMouseReleased(const ui::MouseEvent& event) {
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (event.IsOnlyMiddleMouseButton())
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If this is the second click on this view then the bubble was showing on
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // the mouse pressed event and is hidden now. Prevent the bubble from
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // reshowing by doing nothing here.
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (suppress_mouse_released_action_) {
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    suppress_mouse_released_action_ = false;
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnClickOrTap(event);
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
540de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) {
550de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  page_info_helper_.location_bar()->GetOmniboxView()->CloseOmniboxPopup();
560de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  return false;
570de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)}
580de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LocationIconView::OnGestureEvent(ui::GestureEvent* event) {
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (event->type() != ui::ET_GESTURE_TAP)
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  OnClickOrTap(*event);
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  event->SetHandled();
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid LocationIconView::OnClickOrTap(const ui::LocatedEvent& event) {
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Do not show page info if the user has been editing the location bar or the
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // location bar is at the NTP.
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (page_info_helper_.location_bar()->GetOmniboxView()->IsEditingOrEmpty())
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  page_info_helper_.ProcessEvent(event);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LocationIconView::ShowTooltip(bool show) {
76ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  SetTooltipText(show ?
77ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch      l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON) : base::string16());
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
79