1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/speech/speech_input_bubble.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/browser/ui/views/info_bubble.h"
#include "gfx/canvas.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "views/controls/button/native_button.h"
#include "views/controls/image_view.h"
#include "views/controls/label.h"
#include "views/standard_layout.h"
#include "views/view.h"
namespace {
const int kBubbleHorizMargin = 6;
const int kBubbleVertMargin = 0;
// This is the content view which is placed inside a SpeechInputBubble.
class ContentView
: public views::View,
public views::ButtonListener {
public:
explicit ContentView(SpeechInputBubbleDelegate* delegate);
void UpdateLayout(SpeechInputBubbleBase::DisplayMode mode,
const string16& message_text);
void SetImage(const SkBitmap& image);
// views::ButtonListener methods.
virtual void ButtonPressed(views::Button* source, const views::Event& event);
// views::View overrides.
virtual gfx::Size GetPreferredSize();
virtual void Layout();
private:
SpeechInputBubbleDelegate* delegate_;
views::ImageView* icon_;
views::Label* heading_;
views::Label* message_;
views::NativeButton* try_again_;
views::NativeButton* cancel_;
DISALLOW_COPY_AND_ASSIGN(ContentView);
};
ContentView::ContentView(SpeechInputBubbleDelegate* delegate)
: delegate_(delegate) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
const gfx::Font& font = rb.GetFont(ResourceBundle::MediumFont);
heading_ = new views::Label(
UTF16ToWide(l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_BUBBLE_HEADING)));
heading_->SetFont(font);
heading_->SetHorizontalAlignment(views::Label::ALIGN_CENTER);
AddChildView(heading_);
message_ = new views::Label();
message_->SetFont(font);
message_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
message_->SetMultiLine(true);
AddChildView(message_);
icon_ = new views::ImageView();
icon_->SetImage(*ResourceBundle::GetSharedInstance().GetBitmapNamed(
IDR_SPEECH_INPUT_MIC_EMPTY));
icon_->SetHorizontalAlignment(views::ImageView::CENTER);
AddChildView(icon_);
cancel_ = new views::NativeButton(
this,
UTF16ToWide(l10n_util::GetStringUTF16(IDS_CANCEL)));
AddChildView(cancel_);
try_again_ = new views::NativeButton(
this,
UTF16ToWide(l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_TRY_AGAIN)));
AddChildView(try_again_);
}
void ContentView::UpdateLayout(SpeechInputBubbleBase::DisplayMode mode,
const string16& message_text) {
bool is_message = (mode == SpeechInputBubbleBase::DISPLAY_MODE_MESSAGE);
heading_->SetVisible(!is_message);
icon_->SetVisible(!is_message);
message_->SetVisible(is_message);
try_again_->SetVisible(is_message);
if (mode == SpeechInputBubbleBase::DISPLAY_MODE_MESSAGE) {
message_->SetText(UTF16ToWideHack(message_text));
} else if (mode == SpeechInputBubbleBase::DISPLAY_MODE_RECORDING) {
heading_->SetText(UTF16ToWide(
l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_BUBBLE_HEADING)));
icon_->SetImage(*ResourceBundle::GetSharedInstance().GetBitmapNamed(
IDR_SPEECH_INPUT_MIC_EMPTY));
} else {
heading_->SetText(UTF16ToWide(
l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_BUBBLE_WORKING)));
}
}
void ContentView::SetImage(const SkBitmap& image) {
icon_->SetImage(image);
}
void ContentView::ButtonPressed(views::Button* source,
const views::Event& event) {
if (source == cancel_) {
delegate_->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_CANCEL);
} else if (source == try_again_) {
delegate_->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_TRY_AGAIN);
} else {
NOTREACHED() << "Unknown button";
}
}
gfx::Size ContentView::GetPreferredSize() {
int width = heading_->GetPreferredSize().width();
int control_width = cancel_->GetPreferredSize().width() +
try_again_->GetPreferredSize().width() +
kRelatedButtonHSpacing;
if (control_width > width)
width = control_width;
control_width = icon_->GetPreferredSize().width();
if (control_width > width)
width = control_width;
int height = cancel_->GetPreferredSize().height();
if (message_->IsVisible()) {
height += message_->GetHeightForWidth(width) +
kLabelToControlVerticalSpacing;
} else {
height += heading_->GetPreferredSize().height() +
icon_->GetImage().height();
}
width += kBubbleHorizMargin * 2;
height += kBubbleVertMargin * 2;
return gfx::Size(width, height);
}
void ContentView::Layout() {
int x = kBubbleHorizMargin;
int y = kBubbleVertMargin;
int available_width = width() - kBubbleHorizMargin * 2;
int available_height = height() - kBubbleVertMargin * 2;
if (message_->IsVisible()) {
DCHECK(try_again_->IsVisible());
int height = try_again_->GetPreferredSize().height();
int try_again_width = try_again_->GetPreferredSize().width();
int cancel_width = cancel_->GetPreferredSize().width();
y += available_height - height;
x += (available_width - cancel_width - try_again_width -
kRelatedButtonHSpacing) / 2;
try_again_->SetBounds(x, y, try_again_width, height);
cancel_->SetBounds(x + try_again_width + kRelatedButtonHSpacing, y,
cancel_width, height);
height = message_->GetHeightForWidth(available_width);
if (height > y - kBubbleVertMargin)
height = y - kBubbleVertMargin;
message_->SetBounds(kBubbleHorizMargin, kBubbleVertMargin,
available_width, height);
} else {
DCHECK(heading_->IsVisible());
DCHECK(icon_->IsVisible());
int height = heading_->GetPreferredSize().height();
heading_->SetBounds(x, y, available_width, height);
y += height;
height = icon_->GetImage().height();
icon_->SetBounds(x, y, available_width, height);
y += height;
height = cancel_->GetPreferredSize().height();
int width = cancel_->GetPreferredSize().width();
cancel_->SetBounds(x + (available_width - width) / 2, y, width, height);
}
}
// Implementation of SpeechInputBubble.
class SpeechInputBubbleImpl
: public SpeechInputBubbleBase,
public InfoBubbleDelegate {
public:
SpeechInputBubbleImpl(TabContents* tab_contents,
Delegate* delegate,
const gfx::Rect& element_rect);
virtual ~SpeechInputBubbleImpl();
// SpeechInputBubble methods.
virtual void Show();
virtual void Hide();
// SpeechInputBubbleBase methods.
virtual void UpdateLayout();
virtual void SetImage(const SkBitmap& image);
// Returns the screen rectangle to use as the info bubble's target.
// |element_rect| is the html element's bounds in page coordinates.
gfx::Rect GetInfoBubbleTarget(const gfx::Rect& element_rect);
// InfoBubbleDelegate
virtual void InfoBubbleClosing(InfoBubble* info_bubble,
bool closed_by_escape);
virtual bool CloseOnEscape();
virtual bool FadeInOnShow();
private:
Delegate* delegate_;
InfoBubble* info_bubble_;
ContentView* bubble_content_;
gfx::Rect element_rect_;
// Set to true if the object is being destroyed normally instead of the
// user clicking outside the window causing it to close automatically.
bool did_invoke_close_;
DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleImpl);
};
SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents,
Delegate* delegate,
const gfx::Rect& element_rect)
: SpeechInputBubbleBase(tab_contents),
delegate_(delegate),
info_bubble_(NULL),
bubble_content_(NULL),
element_rect_(element_rect),
did_invoke_close_(false) {
}
SpeechInputBubbleImpl::~SpeechInputBubbleImpl() {
did_invoke_close_ = true;
Hide();
}
gfx::Rect SpeechInputBubbleImpl::GetInfoBubbleTarget(
const gfx::Rect& element_rect) {
gfx::Rect container_rect;
tab_contents()->GetContainerBounds(&container_rect);
return gfx::Rect(
container_rect.x() + element_rect.x() + kBubbleTargetOffsetX,
container_rect.y() + element_rect.y() + element_rect.height(), 1, 1);
}
void SpeechInputBubbleImpl::InfoBubbleClosing(InfoBubble* info_bubble,
bool closed_by_escape) {
info_bubble_ = NULL;
bubble_content_ = NULL;
if (!did_invoke_close_)
delegate_->InfoBubbleFocusChanged();
}
bool SpeechInputBubbleImpl::CloseOnEscape() {
return false;
}
bool SpeechInputBubbleImpl::FadeInOnShow() {
return false;
}
void SpeechInputBubbleImpl::Show() {
if (info_bubble_)
return; // nothing to do, already visible.
bubble_content_ = new ContentView(delegate_);
UpdateLayout();
views::Widget* parent = views::Widget::GetWidgetFromNativeWindow(
tab_contents()->view()->GetTopLevelNativeWindow());
info_bubble_ = InfoBubble::Show(parent,
GetInfoBubbleTarget(element_rect_),
BubbleBorder::TOP_LEFT, bubble_content_,
this);
// We don't want fade outs when closing because it makes speech recognition
// appear slower than it is. Also setting it to false allows |Close| to
// destroy the bubble immediately instead of waiting for the fade animation
// to end so the caller can manage this object's life cycle like a normal
// stack based or member variable object.
info_bubble_->set_fade_away_on_close(false);
}
void SpeechInputBubbleImpl::Hide() {
if (info_bubble_)
info_bubble_->Close();
}
void SpeechInputBubbleImpl::UpdateLayout() {
if (bubble_content_)
bubble_content_->UpdateLayout(display_mode(), message_text());
if (info_bubble_) // Will be null on first call.
info_bubble_->SizeToContents();
}
void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) {
if (bubble_content_)
bubble_content_->SetImage(image);
}
} // namespace
SpeechInputBubble* SpeechInputBubble::CreateNativeBubble(
TabContents* tab_contents,
SpeechInputBubble::Delegate* delegate,
const gfx::Rect& element_rect) {
return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect);
}
|