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 "chrome/browser/ui/views/location_bar/zoom_view.h"
6
7#include "chrome/browser/ui/toolbar/toolbar_model.h"
8#include "chrome/browser/ui/view_ids.h"
9#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
10#include "chrome/browser/ui/zoom/zoom_controller.h"
11#include "grit/generated_resources.h"
12#include "grit/theme_resources.h"
13#include "ui/base/accessibility/accessible_view_state.h"
14#include "ui/base/events/event.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/base/resource/resource_bundle.h"
17#include "ui/gfx/size.h"
18
19ZoomView::ZoomView(ToolbarModel* toolbar_model,
20                   LocationBarView::Delegate* location_bar_delegate)
21    : toolbar_model_(toolbar_model),
22      location_bar_delegate_(location_bar_delegate) {
23  set_accessibility_focusable(true);
24  Update(NULL);
25  LocationBarView::InitTouchableLocationBarChildView(this);
26}
27
28ZoomView::~ZoomView() {
29}
30
31void ZoomView::Update(ZoomController* zoom_controller) {
32  if (!zoom_controller || zoom_controller->IsAtDefaultZoom() ||
33      toolbar_model_->GetInputInProgress()) {
34    SetVisible(false);
35    ZoomBubbleView::CloseBubble();
36    return;
37  }
38
39  SetTooltipText(l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM,
40      zoom_controller->zoom_percent()));
41  SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
42      zoom_controller->GetResourceForZoomLevel()));
43  SetVisible(true);
44}
45
46void ZoomView::GetAccessibleState(ui::AccessibleViewState* state) {
47  state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_ZOOM);
48  state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
49}
50
51bool ZoomView::GetTooltipText(const gfx::Point& p, string16* tooltip) const {
52  // Don't show tooltip if the zoom bubble is displayed.
53  return !ZoomBubbleView::IsShowing() && ImageView::GetTooltipText(p, tooltip);
54}
55
56bool ZoomView::OnMousePressed(const ui::MouseEvent& event) {
57  // Do nothing until mouse is released.
58  return true;
59}
60
61void ZoomView::OnMouseReleased(const ui::MouseEvent& event) {
62  if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location()))
63    ActivateBubble();
64}
65
66bool ZoomView::OnKeyPressed(const ui::KeyEvent& event) {
67  if (event.key_code() != ui::VKEY_SPACE &&
68      event.key_code() != ui::VKEY_RETURN) {
69    return false;
70  }
71
72  ActivateBubble();
73  return true;
74}
75
76void ZoomView::OnGestureEvent(ui::GestureEvent* event) {
77  if (event->type() == ui::ET_GESTURE_TAP) {
78    ActivateBubble();
79    event->SetHandled();
80  }
81}
82
83void ZoomView::ActivateBubble() {
84  ZoomBubbleView::ShowBubble(location_bar_delegate_->GetWebContents(), false);
85}
86