Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / location_bar / zoom_decoration.mm
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 #import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
6
7 #include "base/strings/string16.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
12 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
13 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
14 #include "chrome/browser/ui/zoom/zoom_controller.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
17
18 ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
19     : owner_(owner),
20       bubble_(nil) {
21 }
22
23 ZoomDecoration::~ZoomDecoration() {
24   [bubble_ closeWithoutAnimation];
25 }
26
27 bool ZoomDecoration::UpdateIfNecessary(ZoomController* zoom_controller) {
28   if (!ShouldShowDecoration()) {
29     if (!IsVisible() && !bubble_)
30       return false;
31
32     HideUI();
33     return true;
34   }
35
36   base::string16 zoom_percent =
37       base::IntToString16(zoom_controller->GetZoomPercent());
38   NSString* zoom_string =
39       l10n_util::GetNSStringFWithFixup(IDS_TOOLTIP_ZOOM, zoom_percent);
40
41   if (IsVisible() && [tooltip_ isEqualToString:zoom_string])
42     return false;
43
44   ShowAndUpdateUI(zoom_controller, zoom_string);
45   return true;
46 }
47
48 void ZoomDecoration::ShowBubble(BOOL auto_close) {
49   if (bubble_)
50     return;
51
52   content::WebContents* web_contents = owner_->GetWebContents();
53   if (!web_contents)
54     return;
55
56   // Get the frame of the decoration.
57   AutocompleteTextField* field = owner_->GetAutocompleteTextField();
58   const NSRect frame =
59       [[field cell] frameForDecoration:this inFrame:[field bounds]];
60
61   // Find point for bubble's arrow in screen coordinates.
62   NSPoint anchor = GetBubblePointInFrame(frame);
63   anchor = [field convertPoint:anchor toView:nil];
64   anchor = [[field window] convertBaseToScreen:anchor];
65
66   bubble_ = [[ZoomBubbleController alloc] initWithParentWindow:[field window]
67                                                       delegate:this];
68   [bubble_ showAnchoredAt:anchor autoClose:auto_close];
69 }
70
71 void ZoomDecoration::CloseBubble() {
72   [bubble_ close];
73 }
74
75 void ZoomDecoration::HideUI() {
76   [bubble_ close];
77   SetVisible(false);
78 }
79
80 void ZoomDecoration::ShowAndUpdateUI(ZoomController* zoom_controller,
81                                      NSString* tooltip_string) {
82   SetImage(OmniboxViewMac::ImageForResource(
83       zoom_controller->GetResourceForZoomLevel()));
84
85   tooltip_.reset([tooltip_string retain]);
86
87   SetVisible(true);
88   [bubble_ onZoomChanged];
89 }
90
91 NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
92   return NSMakePoint(NSMaxX(frame), NSMaxY(frame));
93 }
94
95 bool ZoomDecoration::IsAtDefaultZoom() const {
96   content::WebContents* web_contents = owner_->GetWebContents();
97   if (!web_contents)
98     return false;
99
100   ZoomController* zoomController =
101       ZoomController::FromWebContents(web_contents);
102   return zoomController && zoomController->IsAtDefaultZoom();
103 }
104
105 bool ZoomDecoration::ShouldShowDecoration() const {
106   return owner_->GetWebContents() != NULL &&
107       !owner_->GetToolbarModel()->input_in_progress() &&
108       (bubble_ || !IsAtDefaultZoom());
109 }
110
111 bool ZoomDecoration::AcceptsMousePress() {
112   return true;
113 }
114
115 bool ZoomDecoration::OnMousePressed(NSRect frame, NSPoint location) {
116   if (bubble_)
117     CloseBubble();
118   else
119     ShowBubble(NO);
120   return true;
121 }
122
123 NSString* ZoomDecoration::GetToolTip() {
124   return tooltip_.get();
125 }
126
127 content::WebContents* ZoomDecoration::GetWebContents() {
128   return owner_->GetWebContents();
129 }
130
131 void ZoomDecoration::OnClose() {
132   bubble_ = nil;
133
134   // If the page is at default zoom then hiding the zoom decoration
135   // was suppressed while the bubble was open. Now that the bubble is
136   // closed the decoration can be hidden.
137   if (IsAtDefaultZoom() && IsVisible()) {
138     SetVisible(false);
139     owner_->OnDecorationsChanged();
140   }
141 }