OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/speech/speech_recognition_bubble.h" | 5 #include "chrome/browser/speech/speech_recognition_bubble.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_finder.h" | 11 #include "chrome/browser/ui/browser_finder.h" |
12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
14 #include "content/public/browser/resource_context.h" | 14 #include "content/public/browser/resource_context.h" |
15 #include "content/public/browser/speech_recognition_manager.h" | 15 #include "content/public/browser/speech_recognition_manager.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_view.h" | 17 #include "content/public/browser/web_contents_view.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/views/bubble/bubble_delegate.h" | 22 #include "ui/views/bubble/bubble_delegate.h" |
23 #include "ui/views/controls/button/label_button.h" | 23 #include "ui/views/controls/button/label_button.h" |
24 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
25 #include "ui/views/controls/label.h" | 25 #include "ui/views/controls/label.h" |
26 #include "ui/views/controls/link.h" | 26 #include "ui/views/controls/link.h" |
27 #include "ui/views/controls/link_listener.h" | 27 #include "ui/views/controls/link_listener.h" |
28 #include "ui/views/layout/layout_constants.h" | 28 #include "ui/views/layout/layout_constants.h" |
| 29 #include "ui/views/widget/widget_observer.h" |
29 | 30 |
30 using content::WebContents; | 31 using content::WebContents; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 const int kBubbleHorizMargin = 6; | 35 const int kBubbleHorizMargin = 6; |
35 const int kBubbleVertMargin = 4; | 36 const int kBubbleVertMargin = 4; |
36 const int kBubbleHeadingVertMargin = 6; | 37 const int kBubbleHeadingVertMargin = 6; |
37 | 38 |
38 // This is the SpeechRecognitionBubble content and views bubble delegate. | 39 // This is the SpeechRecognitionBubble content and views bubble delegate. |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 if (cancel_->visible()) { | 304 if (cancel_->visible()) { |
304 control_height = cancel_->GetPreferredSize().height(); | 305 control_height = cancel_->GetPreferredSize().height(); |
305 int width = cancel_->GetPreferredSize().width(); | 306 int width = cancel_->GetPreferredSize().width(); |
306 cancel_->SetBounds(x + (available_width - width) / 2, y, width, | 307 cancel_->SetBounds(x + (available_width - width) / 2, y, width, |
307 control_height); | 308 control_height); |
308 } | 309 } |
309 } | 310 } |
310 } | 311 } |
311 | 312 |
312 // Implementation of SpeechRecognitionBubble. | 313 // Implementation of SpeechRecognitionBubble. |
313 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 314 class SpeechRecognitionBubbleImpl |
| 315 : public SpeechRecognitionBubbleBase, |
| 316 public views::WidgetObserver { |
314 public: | 317 public: |
315 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, | 318 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, |
316 Delegate* delegate, | 319 Delegate* delegate, |
317 const gfx::Rect& element_rect); | 320 const gfx::Rect& element_rect); |
318 virtual ~SpeechRecognitionBubbleImpl(); | 321 virtual ~SpeechRecognitionBubbleImpl(); |
319 | 322 |
320 // SpeechRecognitionBubble methods. | 323 // SpeechRecognitionBubble methods. |
321 virtual void Show() OVERRIDE; | 324 virtual void Show() OVERRIDE; |
322 virtual void Hide() OVERRIDE; | 325 virtual void Hide() OVERRIDE; |
323 | 326 |
324 // SpeechRecognitionBubbleBase methods. | 327 // SpeechRecognitionBubbleBase methods. |
325 virtual void UpdateLayout() OVERRIDE; | 328 virtual void UpdateLayout() OVERRIDE; |
326 virtual void UpdateImage() OVERRIDE; | 329 virtual void UpdateImage() OVERRIDE; |
327 | 330 |
| 331 // views::WidgetObserver methods. |
| 332 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; |
| 333 |
328 private: | 334 private: |
329 Delegate* delegate_; | 335 Delegate* delegate_; |
330 SpeechRecognitionBubbleView* bubble_; | 336 SpeechRecognitionBubbleView* bubble_; |
331 gfx::Rect element_rect_; | 337 gfx::Rect element_rect_; |
332 | 338 |
333 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); | 339 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); |
334 }; | 340 }; |
335 | 341 |
336 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 342 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
337 int render_process_id, int render_view_id, Delegate* delegate, | 343 int render_process_id, int render_view_id, Delegate* delegate, |
338 const gfx::Rect& element_rect) | 344 const gfx::Rect& element_rect) |
339 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), | 345 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
340 delegate_(delegate), | 346 delegate_(delegate), |
341 bubble_(NULL), | 347 bubble_(NULL), |
342 element_rect_(element_rect) { | 348 element_rect_(element_rect) { |
343 } | 349 } |
344 | 350 |
345 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 351 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
346 if (bubble_) { | 352 if (bubble_) { |
| 353 bubble_->GetWidget()->RemoveObserver(this); |
347 bubble_->set_notify_delegate_on_activation_change(false); | 354 bubble_->set_notify_delegate_on_activation_change(false); |
348 bubble_->GetWidget()->Close(); | 355 bubble_->GetWidget()->Close(); |
349 } | 356 } |
350 } | 357 } |
351 | 358 |
| 359 void SpeechRecognitionBubbleImpl::OnWidgetDestroying(views::Widget* widget) { |
| 360 bubble_ = NULL; |
| 361 } |
| 362 |
352 void SpeechRecognitionBubbleImpl::Show() { | 363 void SpeechRecognitionBubbleImpl::Show() { |
353 WebContents* web_contents = GetWebContents(); | 364 WebContents* web_contents = GetWebContents(); |
354 if (!web_contents) | 365 if (!web_contents) |
355 return; | 366 return; |
356 | 367 |
357 if (!bubble_) { | 368 if (!bubble_) { |
358 views::View* icon = NULL; | 369 views::View* icon = NULL; |
359 | 370 |
360 // Anchor to the location bar, in case |element_rect| is offscreen. | 371 // Anchor to the location bar, in case |element_rect| is offscreen. |
361 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 372 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
362 if (browser) { | 373 if (browser) { |
363 BrowserView* browser_view = | 374 BrowserView* browser_view = |
364 BrowserView::GetBrowserViewForBrowser(browser); | 375 BrowserView::GetBrowserViewForBrowser(browser); |
365 icon = browser_view->GetLocationBarView() ? | 376 icon = browser_view->GetLocationBarView() ? |
366 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; | 377 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; |
367 } | 378 } |
368 | 379 |
369 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, | 380 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, |
370 web_contents); | 381 web_contents); |
371 | 382 |
372 if (!icon) { | 383 if (!icon) { |
373 // We dont't have an icon to attach to. Manually specify the web contents | 384 // We dont't have an icon to attach to. Manually specify the web contents |
374 // window as the parent. | 385 // window as the parent. |
375 bubble_->set_parent_window( | 386 bubble_->set_parent_window( |
376 web_contents->GetView()->GetTopLevelNativeWindow()); | 387 web_contents->GetView()->GetTopLevelNativeWindow()); |
377 } | 388 } |
378 | 389 |
379 views::BubbleDelegateView::CreateBubble(bubble_); | 390 views::BubbleDelegateView::CreateBubble(bubble_); |
380 UpdateLayout(); | 391 UpdateLayout(); |
381 } | 392 bubble_->GetWidget()->AddObserver(this); |
| 393 } |
382 bubble_->GetWidget()->Show(); | 394 bubble_->GetWidget()->Show(); |
383 } | 395 } |
384 | 396 |
385 void SpeechRecognitionBubbleImpl::Hide() { | 397 void SpeechRecognitionBubbleImpl::Hide() { |
386 if (bubble_) | 398 if (bubble_) |
387 bubble_->GetWidget()->Hide(); | 399 bubble_->GetWidget()->Hide(); |
388 } | 400 } |
389 | 401 |
390 void SpeechRecognitionBubbleImpl::UpdateLayout() { | 402 void SpeechRecognitionBubbleImpl::UpdateLayout() { |
391 if (bubble_ && GetWebContents()) | 403 if (bubble_ && GetWebContents()) |
392 bubble_->UpdateLayout(display_mode(), message_text(), icon_image()); | 404 bubble_->UpdateLayout(display_mode(), message_text(), icon_image()); |
393 } | 405 } |
394 | 406 |
395 void SpeechRecognitionBubbleImpl::UpdateImage() { | 407 void SpeechRecognitionBubbleImpl::UpdateImage() { |
396 if (bubble_ && GetWebContents()) | 408 if (bubble_ && GetWebContents()) |
397 bubble_->SetImage(icon_image()); | 409 bubble_->SetImage(icon_image()); |
398 } | 410 } |
399 | 411 |
400 } // namespace | 412 } // namespace |
401 | 413 |
402 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 414 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
403 int render_process_id, | 415 int render_process_id, |
404 int render_view_id, | 416 int render_view_id, |
405 SpeechRecognitionBubble::Delegate* delegate, | 417 SpeechRecognitionBubble::Delegate* delegate, |
406 const gfx::Rect& element_rect) { | 418 const gfx::Rect& element_rect) { |
407 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, | 419 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, |
408 delegate, element_rect); | 420 delegate, element_rect); |
409 } | 421 } |
OLD | NEW |