Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / speech_recognition_bubble_cocoa.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 <Cocoa/Cocoa.h>
6
7 #include "chrome/browser/speech/speech_recognition_bubble.h"
8
9 #import "base/mac/scoped_nsobject.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
12 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
14 #import "chrome/browser/ui/cocoa/speech_recognition_window_controller.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_view.h"
17 #include "ui/gfx/image/image_skia_util_mac.h"
18
19 using content::WebContents;
20
21 namespace {
22
23 // A class to bridge between the speech recognition C++ code and the Objective-C
24 // bubble implementation. See chrome/browser/speech/speech_recognition_bubble.h
25 // for more information on how this gets used.
26 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase {
27  public:
28   SpeechRecognitionBubbleImpl(int render_process_id,
29                               int render_view_id,
30                               Delegate* delegate,
31                               const gfx::Rect& element_rect);
32   virtual ~SpeechRecognitionBubbleImpl();
33   virtual void Show() OVERRIDE;
34   virtual void Hide() OVERRIDE;
35   virtual void UpdateLayout() OVERRIDE;
36   virtual void UpdateImage() OVERRIDE;
37
38  private:
39   base::scoped_nsobject<SpeechRecognitionWindowController> window_;
40   Delegate* delegate_;
41   gfx::Rect element_rect_;
42 };
43
44 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl(
45     int render_process_id,
46     int render_view_id,
47     Delegate* delegate,
48     const gfx::Rect& element_rect)
49     : SpeechRecognitionBubbleBase(render_process_id, render_view_id),
50       delegate_(delegate),
51       element_rect_(element_rect) {
52 }
53
54 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() {
55   if (window_.get())
56     [window_.get() close];
57 }
58
59 void SpeechRecognitionBubbleImpl::UpdateImage() {
60   if (window_.get() && GetWebContents())
61     [window_.get() setImage:gfx::NSImageFromImageSkia(icon_image())];
62 }
63
64 void SpeechRecognitionBubbleImpl::Show() {
65   if (!GetWebContents())
66     return;
67
68   if (window_.get()) {
69     [window_.get() show];
70     return;
71   }
72
73   // Find the screen coordinates for the given tab and position the bubble's
74   // arrow anchor point inside that to point at the bottom-left of the html
75   // input element rect if the position is valid, otherwise point it towards
76   // the page icon in the omnibox.
77   gfx::NativeView view = GetWebContents()->GetView()->GetNativeView();
78   NSWindow* parent_window = [view window];
79   NSRect tab_bounds = [view bounds];
80   int anchor_x = tab_bounds.origin.x + element_rect_.x() +
81                  element_rect_.width() - kBubbleTargetOffsetX;
82   int anchor_y = tab_bounds.origin.y + tab_bounds.size.height -
83                  element_rect_.y() - element_rect_.height();
84
85   NSPoint anchor = NSMakePoint(anchor_x, anchor_y);
86   if (NSPointInRect(anchor, tab_bounds)) {
87     // Good, convert to window coordinates.
88     anchor = [view convertPoint:anchor toView:nil];
89   } else {
90     LocationBarViewMac* locationBar =
91         [[parent_window windowController] locationBarBridge];
92
93     if (locationBar) {
94       anchor = locationBar->GetPageInfoBubblePoint();
95     } else {
96       // This is very rare, but possible. Just use the top-left corner.
97       // See http://crbug.com/119237
98       anchor = NSMakePoint(NSMinX(tab_bounds), NSMaxY(tab_bounds));
99       anchor = [view convertPoint:anchor toView:nil];
100     }
101   }
102
103   anchor = [parent_window convertBaseToScreen:anchor];
104
105   window_.reset([[SpeechRecognitionWindowController alloc]
106       initWithParentWindow:parent_window
107                   delegate:delegate_
108                 anchoredAt:anchor]);
109
110   UpdateLayout();
111   [window_.get() show];
112 }
113
114 void SpeechRecognitionBubbleImpl::Hide() {
115   if (!window_.get())
116     return;
117
118   [window_.get() close];
119   window_.reset();
120 }
121
122 void SpeechRecognitionBubbleImpl::UpdateLayout() {
123   if (!window_.get() || !GetWebContents())
124     return;
125
126   [window_.get() updateLayout:display_mode()
127                   messageText:message_text()
128                     iconImage:gfx::NSImageFromImageSkia(icon_image())];
129 }
130
131 }  // namespace
132
133 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble(
134     int render_process_id,
135     int render_view_id,
136     Delegate* delegate,
137     const gfx::Rect& element_rect) {
138   return new SpeechRecognitionBubbleImpl(render_process_id,
139                                          render_view_id,
140                                          delegate,
141                                          element_rect);
142 }