- add sources.
[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/browser/zoom_bubble_controller.h"
11 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
14 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
15 #include "chrome/browser/ui/zoom/zoom_controller.h"
16 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util_mac.h"
18
19 ZoomDecoration::ZoomDecoration(LocationBarViewMac* owner)
20     : owner_(owner),
21       bubble_(nil) {
22 }
23
24 ZoomDecoration::~ZoomDecoration() {
25   [bubble_ closeWithoutAnimation];
26 }
27
28 void ZoomDecoration::Update(ZoomController* zoom_controller) {
29   if (!ShouldShowDecoration()) {
30     [bubble_ close];
31     SetVisible(false);
32     return;
33   }
34
35   SetImage(OmniboxViewMac::ImageForResource(
36       zoom_controller->GetResourceForZoomLevel()));
37
38   string16 zoom_percent = base::IntToString16(zoom_controller->zoom_percent());
39   NSString* zoom_string =
40       l10n_util::GetNSStringFWithFixup(IDS_TOOLTIP_ZOOM, zoom_percent);
41   tooltip_.reset([zoom_string retain]);
42
43   SetVisible(true);
44
45   [bubble_ onZoomChanged];
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   AutocompleteTextFieldCell* cell = [field cell];
59   const NSRect frame = [cell frameForDecoration:this
60                                         inFrame:[field bounds]];
61
62   // Find point for bubble's arrow in screen coordinates.
63   NSPoint anchor = GetBubblePointInFrame(frame);
64   anchor = [field convertPoint:anchor toView:nil];
65   anchor = [[field window] convertBaseToScreen:anchor];
66
67   if (!bubble_) {
68     void(^observer)(ZoomBubbleController*) = ^(ZoomBubbleController*) {
69         bubble_ = nil;
70         // If the page is at default zoom then hiding the zoom decoration was
71         // suppressed while the bubble was open. Now that the bubble is closed
72         // the decoration can be hidden.
73         if (IsAtDefaultZoom() && IsVisible()) {
74           SetVisible(false);
75           owner_->OnDecorationsChanged();
76         }
77     };
78     bubble_ =
79         [[ZoomBubbleController alloc] initWithParentWindow:[field window]
80                                              closeObserver:observer];
81   }
82
83   [bubble_ showForWebContents:web_contents
84                    anchoredAt:anchor
85                     autoClose:auto_close];
86 }
87
88 void ZoomDecoration::CloseBubble() {
89   [bubble_ close];
90 }
91
92 NSPoint ZoomDecoration::GetBubblePointInFrame(NSRect frame) {
93   return NSMakePoint(NSMaxX(frame), NSMaxY(frame));
94 }
95
96 bool ZoomDecoration::IsAtDefaultZoom() const {
97   content::WebContents* web_contents = owner_->GetWebContents();
98   if (!web_contents)
99     return false;
100   ZoomController* zoomController =
101       ZoomController::FromWebContents(web_contents);
102   return zoomController && zoomController->IsAtDefaultZoom();
103 }
104
105 bool ZoomDecoration::ShouldShowDecoration() const {
106   return !owner_->GetToolbarModel()->input_in_progress() &&
107       (bubble_ || !IsAtDefaultZoom());
108 }
109
110 bool ZoomDecoration::AcceptsMousePress() {
111   return true;
112 }
113
114 bool ZoomDecoration::OnMousePressed(NSRect frame) {
115   if (bubble_)
116     CloseBubble();
117   else
118     ShowBubble(NO);
119   return true;
120 }
121
122 NSString* ZoomDecoration::GetToolTip() {
123   return tooltip_.get();
124 }