Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1075)

Side by Side Diff: chrome/browser/speech/speech_input_bubble_gtk.cc

Issue 9568002: Renamed speech input implementation from to speech_recognition_*. The namespace has been renamed fr… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebased from master. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/speech/speech_input_bubble.h"
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/gtk/browser_toolbar_gtk.h"
11 #include "chrome/browser/ui/gtk/browser_window_gtk.h"
12 #include "chrome/browser/ui/gtk/bubble/bubble_gtk.h"
13 #include "chrome/browser/ui/gtk/gtk_chrome_link_button.h"
14 #include "chrome/browser/ui/gtk/gtk_util.h"
15 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h"
16 #include "chrome/browser/ui/gtk/theme_service_gtk.h"
17 #include "content/public/browser/resource_context.h"
18 #include "content/public/browser/speech_input_manager.h"
19 #include "content/public/browser/web_contents.h"
20 #include "grit/generated_resources.h"
21 #include "grit/theme_resources.h"
22 #include "ui/base/gtk/gtk_hig_constants.h"
23 #include "ui/base/gtk/owned_widget_gtk.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/gtk_util.h"
27 #include "ui/gfx/rect.h"
28
29 using content::WebContents;
30
31 namespace {
32
33 const int kBubbleControlVerticalSpacing = 5;
34 const int kBubbleControlHorizontalSpacing = 20;
35 const int kIconHorizontalPadding = 10;
36 const int kButtonBarHorizontalSpacing = 10;
37
38 // Use black for text labels since the bubble has white background.
39 const GdkColor& kLabelTextColor = ui::kGdkBlack;
40
41 // Implementation of SpeechInputBubble for GTK. This shows a speech input bubble
42 // on screen.
43 class SpeechInputBubbleGtk : public SpeechInputBubbleBase,
44 public BubbleDelegateGtk {
45 public:
46 SpeechInputBubbleGtk(WebContents* web_contents,
47 Delegate* delegate,
48 const gfx::Rect& element_rect);
49 ~SpeechInputBubbleGtk();
50
51 private:
52 // SpeechInputBubbleBase:
53 virtual void Show() OVERRIDE;
54 virtual void Hide() OVERRIDE;
55 virtual void UpdateLayout() OVERRIDE;
56 virtual void UpdateImage() OVERRIDE;
57
58 // BubbleDelegateGtk:
59 virtual void BubbleClosing(BubbleGtk* bubble, bool closed_by_escape) OVERRIDE;
60
61 CHROMEGTK_CALLBACK_0(SpeechInputBubbleGtk, void, OnCancelClicked);
62 CHROMEGTK_CALLBACK_0(SpeechInputBubbleGtk, void, OnTryAgainClicked);
63 CHROMEGTK_CALLBACK_0(SpeechInputBubbleGtk, void, OnMicSettingsClicked);
64
65 Delegate* delegate_;
66 BubbleGtk* bubble_;
67 gfx::Rect element_rect_;
68 bool did_invoke_close_;
69
70 GtkWidget* label_;
71 GtkWidget* cancel_button_;
72 GtkWidget* try_again_button_;
73 GtkWidget* icon_;
74 GtkWidget* icon_container_;
75 GtkWidget* mic_settings_;
76
77 DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleGtk);
78 };
79
80 SpeechInputBubbleGtk::SpeechInputBubbleGtk(WebContents* web_contents,
81 Delegate* delegate,
82 const gfx::Rect& element_rect)
83 : SpeechInputBubbleBase(web_contents),
84 delegate_(delegate),
85 bubble_(NULL),
86 element_rect_(element_rect),
87 did_invoke_close_(false),
88 label_(NULL),
89 cancel_button_(NULL),
90 try_again_button_(NULL),
91 icon_(NULL),
92 icon_container_(NULL),
93 mic_settings_(NULL) {
94 }
95
96 SpeechInputBubbleGtk::~SpeechInputBubbleGtk() {
97 // The |Close| call below invokes our |BubbleClosing| method. Since we were
98 // destroyed by the caller we don't need to call them back, hence set this
99 // flag here.
100 did_invoke_close_ = true;
101 Hide();
102 }
103
104 void SpeechInputBubbleGtk::OnCancelClicked(GtkWidget* widget) {
105 delegate_->InfoBubbleButtonClicked(BUTTON_CANCEL);
106 }
107
108 void SpeechInputBubbleGtk::OnTryAgainClicked(GtkWidget* widget) {
109 delegate_->InfoBubbleButtonClicked(BUTTON_TRY_AGAIN);
110 }
111
112 void SpeechInputBubbleGtk::OnMicSettingsClicked(GtkWidget* widget) {
113 content::SpeechInputManager::GetInstance()->ShowAudioInputSettings();
114 Hide();
115 }
116
117 void SpeechInputBubbleGtk::Show() {
118 if (bubble_)
119 return; // Nothing further to do since the bubble is already visible.
120
121 // We use a vbox to arrange the controls (label, image, button bar) vertically
122 // and the button bar is a hbox holding the 2 buttons (try again and cancel).
123 // To get horizontal space around them we place this vbox with padding in a
124 // GtkAlignment below.
125 GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
126
127 // The icon with a some padding on the left and right.
128 icon_container_ = gtk_alignment_new(0, 0, 0, 0);
129 icon_ = gtk_image_new();
130 gtk_container_add(GTK_CONTAINER(icon_container_), icon_);
131 gtk_box_pack_start(GTK_BOX(vbox), icon_container_, FALSE, FALSE,
132 kBubbleControlVerticalSpacing);
133
134 label_ = gtk_label_new(NULL);
135 gtk_util::SetLabelColor(label_, &kLabelTextColor);
136 gtk_box_pack_start(GTK_BOX(vbox), label_, FALSE, FALSE,
137 kBubbleControlVerticalSpacing);
138
139 Profile* profile = Profile::FromBrowserContext(
140 web_contents()->GetBrowserContext());
141
142 // TODO(tommi): The audio_manager property can only be accessed from the
143 // IO thread, so we can't call CanShowAudioInputSettings directly here if
144 // we can show the input settings. For now, we always show the link (like
145 // we do on other platforms).
146 if (true) {
147 mic_settings_ = gtk_chrome_link_button_new(
148 l10n_util::GetStringUTF8(IDS_SPEECH_INPUT_MIC_SETTINGS).c_str());
149 gtk_box_pack_start(GTK_BOX(vbox), mic_settings_, FALSE, FALSE,
150 kBubbleControlVerticalSpacing);
151 g_signal_connect(mic_settings_, "clicked",
152 G_CALLBACK(&OnMicSettingsClickedThunk), this);
153 }
154
155 GtkWidget* button_bar = gtk_hbox_new(FALSE, kButtonBarHorizontalSpacing);
156 gtk_box_pack_start(GTK_BOX(vbox), button_bar, FALSE, FALSE,
157 kBubbleControlVerticalSpacing);
158
159 cancel_button_ = gtk_button_new_with_label(
160 l10n_util::GetStringUTF8(IDS_CANCEL).c_str());
161 gtk_box_pack_start(GTK_BOX(button_bar), cancel_button_, TRUE, FALSE, 0);
162 g_signal_connect(cancel_button_, "clicked",
163 G_CALLBACK(&OnCancelClickedThunk), this);
164
165 try_again_button_ = gtk_button_new_with_label(
166 l10n_util::GetStringUTF8(IDS_SPEECH_INPUT_TRY_AGAIN).c_str());
167 gtk_box_pack_start(GTK_BOX(button_bar), try_again_button_, TRUE, FALSE, 0);
168 g_signal_connect(try_again_button_, "clicked",
169 G_CALLBACK(&OnTryAgainClickedThunk), this);
170
171 GtkWidget* content = gtk_alignment_new(0, 0, 0, 0);
172 gtk_alignment_set_padding(GTK_ALIGNMENT(content),
173 kBubbleControlVerticalSpacing, kBubbleControlVerticalSpacing,
174 kBubbleControlHorizontalSpacing, kBubbleControlHorizontalSpacing);
175 gtk_container_add(GTK_CONTAINER(content), vbox);
176
177 ThemeServiceGtk* theme_provider = ThemeServiceGtk::GetFrom(profile);
178 GtkWidget* reference_widget = web_contents()->GetNativeView();
179 gfx::Rect container_rect;
180 web_contents()->GetContainerBounds(&container_rect);
181 gfx::Rect target_rect(element_rect_.right() - kBubbleTargetOffsetX,
182 element_rect_.bottom(), 1, 1);
183
184 if (target_rect.x() < 0 || target_rect.y() < 0 ||
185 target_rect.x() > container_rect.width() ||
186 target_rect.y() > container_rect.height()) {
187 // Target is not in screen view, so point to wrench.
188 Browser* browser =
189 Browser::GetOrCreateTabbedBrowser(profile);
190 BrowserWindowGtk* browser_window =
191 BrowserWindowGtk::GetBrowserWindowForNativeWindow(
192 browser->window()->GetNativeHandle());
193 reference_widget = browser_window->GetToolbar()->GetLocationBarView()
194 ->location_icon_widget();
195 target_rect = gtk_util::WidgetBounds(reference_widget);
196 }
197 bubble_ = BubbleGtk::Show(reference_widget,
198 &target_rect,
199 content,
200 BubbleGtk::ARROW_LOCATION_TOP_LEFT,
201 false, // match_system_theme
202 true, // grab_input
203 theme_provider,
204 this);
205
206 UpdateLayout();
207 }
208
209 void SpeechInputBubbleGtk::Hide() {
210 if (bubble_)
211 bubble_->Close();
212 }
213
214 void SpeechInputBubbleGtk::UpdateLayout() {
215 if (!bubble_)
216 return;
217
218 if (display_mode() == DISPLAY_MODE_MESSAGE) {
219 // Message text and the Try Again + Cancel buttons are visible, hide the
220 // icon.
221 gtk_label_set_text(GTK_LABEL(label_),
222 UTF16ToUTF8(message_text()).c_str());
223 gtk_widget_show(label_);
224 gtk_widget_show(try_again_button_);
225 if (mic_settings_)
226 gtk_widget_show(mic_settings_);
227 gtk_widget_hide(icon_);
228 } else {
229 // Heading text, icon and cancel button are visible, hide the Try Again
230 // button.
231 gtk_label_set_text(GTK_LABEL(label_),
232 l10n_util::GetStringUTF8(IDS_SPEECH_INPUT_BUBBLE_HEADING).c_str());
233 if (display_mode() == DISPLAY_MODE_RECORDING) {
234 gtk_widget_show(label_);
235 } else {
236 gtk_widget_hide(label_);
237 }
238 UpdateImage();
239 gtk_widget_show(icon_);
240 gtk_widget_hide(try_again_button_);
241 if (mic_settings_)
242 gtk_widget_hide(mic_settings_);
243 if (display_mode() == DISPLAY_MODE_WARM_UP) {
244 gtk_widget_hide(cancel_button_);
245
246 // The text label and cancel button are hidden in this mode, but we want
247 // the popup to appear the same size as it would once recording starts,
248 // so as to reduce UI jank when recording starts. So we calculate the
249 // difference in size between the two sets of controls and add that as
250 // padding around the icon here.
251 GtkRequisition cancel_size;
252 gtk_widget_get_child_requisition(cancel_button_, &cancel_size);
253 GtkRequisition label_size;
254 gtk_widget_get_child_requisition(label_, &label_size);
255 SkBitmap* volume = ResourceBundle::GetSharedInstance().GetBitmapNamed(
256 IDR_SPEECH_INPUT_MIC_EMPTY);
257 int desired_width = std::max(volume->width(), cancel_size.width) +
258 kIconHorizontalPadding * 2;
259 int desired_height = volume->height() + label_size.height +
260 cancel_size.height +
261 kBubbleControlVerticalSpacing * 2;
262 int diff_width = desired_width - icon_image().width();
263 int diff_height = desired_height - icon_image().height();
264 gtk_alignment_set_padding(GTK_ALIGNMENT(icon_container_),
265 diff_height / 2, diff_height - diff_height / 2,
266 diff_width / 2, diff_width - diff_width / 2);
267 } else {
268 // Reset the padding done above.
269 gtk_alignment_set_padding(GTK_ALIGNMENT(icon_container_), 0, 0,
270 kIconHorizontalPadding, kIconHorizontalPadding);
271 gtk_widget_show(cancel_button_);
272 }
273 }
274 }
275
276 void SpeechInputBubbleGtk::UpdateImage() {
277 SkBitmap image = icon_image();
278 if (image.isNull() || !bubble_)
279 return;
280
281 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&image);
282 gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf);
283 g_object_unref(pixbuf);
284 }
285
286 void SpeechInputBubbleGtk::BubbleClosing(BubbleGtk* bubble,
287 bool closed_by_escape) {
288 bubble_ = NULL;
289 if (!did_invoke_close_)
290 delegate_->InfoBubbleFocusChanged();
291 }
292
293 } // namespace
294
295 SpeechInputBubble* SpeechInputBubble::CreateNativeBubble(
296 WebContents* web_contents,
297 Delegate* delegate,
298 const gfx::Rect& element_rect) {
299 return new SpeechInputBubbleGtk(web_contents, delegate, element_rect);
300 }
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_input_bubble_controller_unittest.cc ('k') | chrome/browser/speech/speech_input_bubble_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698