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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 if (cancel_->visible()) { | 321 if (cancel_->visible()) { |
321 control_height = cancel_->GetPreferredSize().height(); | 322 control_height = cancel_->GetPreferredSize().height(); |
322 int width = cancel_->GetPreferredSize().width(); | 323 int width = cancel_->GetPreferredSize().width(); |
323 cancel_->SetBounds(x + (available_width - width) / 2, y, width, | 324 cancel_->SetBounds(x + (available_width - width) / 2, y, width, |
324 control_height); | 325 control_height); |
325 } | 326 } |
326 } | 327 } |
327 } | 328 } |
328 | 329 |
329 // Implementation of SpeechRecognitionBubble. | 330 // Implementation of SpeechRecognitionBubble. |
330 class SpeechRecognitionBubbleImpl : public SpeechRecognitionBubbleBase { | 331 class SpeechRecognitionBubbleImpl |
| 332 : public SpeechRecognitionBubbleBase, |
| 333 public views::WidgetObserver { |
331 public: | 334 public: |
332 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, | 335 SpeechRecognitionBubbleImpl(int render_process_id, int render_view_id, |
333 Delegate* delegate, | 336 Delegate* delegate, |
334 const gfx::Rect& element_rect); | 337 const gfx::Rect& element_rect); |
335 virtual ~SpeechRecognitionBubbleImpl(); | 338 virtual ~SpeechRecognitionBubbleImpl(); |
336 | 339 |
337 // SpeechRecognitionBubble methods. | 340 // SpeechRecognitionBubble methods. |
338 virtual void Show() OVERRIDE; | 341 virtual void Show() OVERRIDE; |
339 virtual void Hide() OVERRIDE; | 342 virtual void Hide() OVERRIDE; |
340 | 343 |
341 // SpeechRecognitionBubbleBase methods. | 344 // SpeechRecognitionBubbleBase methods. |
342 virtual void UpdateLayout() OVERRIDE; | 345 virtual void UpdateLayout() OVERRIDE; |
343 virtual void UpdateImage() OVERRIDE; | 346 virtual void UpdateImage() OVERRIDE; |
344 | 347 |
| 348 // views::WidgetObserver methods. |
| 349 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; |
| 350 |
345 private: | 351 private: |
346 Delegate* delegate_; | 352 Delegate* delegate_; |
347 SpeechRecognitionBubbleView* bubble_; | 353 SpeechRecognitionBubbleView* bubble_; |
348 gfx::Rect element_rect_; | 354 gfx::Rect element_rect_; |
349 | 355 |
350 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); | 356 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionBubbleImpl); |
351 }; | 357 }; |
352 | 358 |
353 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( | 359 SpeechRecognitionBubbleImpl::SpeechRecognitionBubbleImpl( |
354 int render_process_id, int render_view_id, Delegate* delegate, | 360 int render_process_id, int render_view_id, Delegate* delegate, |
355 const gfx::Rect& element_rect) | 361 const gfx::Rect& element_rect) |
356 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), | 362 : SpeechRecognitionBubbleBase(render_process_id, render_view_id), |
357 delegate_(delegate), | 363 delegate_(delegate), |
358 bubble_(NULL), | 364 bubble_(NULL), |
359 element_rect_(element_rect) { | 365 element_rect_(element_rect) { |
360 } | 366 } |
361 | 367 |
362 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { | 368 SpeechRecognitionBubbleImpl::~SpeechRecognitionBubbleImpl() { |
363 if (bubble_) { | 369 if (bubble_) { |
| 370 bubble_->GetWidget()->RemoveObserver(this); |
364 bubble_->set_notify_delegate_on_activation_change(false); | 371 bubble_->set_notify_delegate_on_activation_change(false); |
365 bubble_->GetWidget()->Close(); | 372 bubble_->GetWidget()->Close(); |
366 } | 373 } |
367 } | 374 } |
368 | 375 |
| 376 void SpeechRecognitionBubbleImpl::OnWidgetDestroying(views::Widget* widget) { |
| 377 bubble_ = NULL; |
| 378 } |
| 379 |
369 void SpeechRecognitionBubbleImpl::Show() { | 380 void SpeechRecognitionBubbleImpl::Show() { |
370 WebContents* web_contents = GetWebContents(); | 381 WebContents* web_contents = GetWebContents(); |
371 if (!web_contents) | 382 if (!web_contents) |
372 return; | 383 return; |
373 | 384 |
374 if (!bubble_) { | 385 if (!bubble_) { |
375 views::View* icon = NULL; | 386 views::View* icon = NULL; |
376 | 387 |
377 // Anchor to the location bar, in case |element_rect| is offscreen. | 388 // Anchor to the location bar, in case |element_rect| is offscreen. |
378 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 389 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
379 if (browser) { | 390 if (browser) { |
380 BrowserView* browser_view = | 391 BrowserView* browser_view = |
381 BrowserView::GetBrowserViewForBrowser(browser); | 392 BrowserView::GetBrowserViewForBrowser(browser); |
382 icon = browser_view->GetLocationBarView() ? | 393 icon = browser_view->GetLocationBarView() ? |
383 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; | 394 browser_view->GetLocationBarView()->GetLocationBarAnchor() : NULL; |
384 } | 395 } |
385 | 396 |
386 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, | 397 bubble_ = new SpeechRecognitionBubbleView(delegate_, icon, element_rect_, |
387 web_contents); | 398 web_contents); |
388 | 399 |
389 if (!icon) { | 400 if (!icon) { |
390 // We dont't have an icon to attach to. Manually specify the web contents | 401 // We dont't have an icon to attach to. Manually specify the web contents |
391 // window as the parent. | 402 // window as the parent. |
392 bubble_->set_parent_window( | 403 bubble_->set_parent_window( |
393 web_contents->GetView()->GetTopLevelNativeWindow()); | 404 web_contents->GetView()->GetTopLevelNativeWindow()); |
394 } | 405 } |
395 | 406 |
396 views::BubbleDelegateView::CreateBubble(bubble_); | 407 views::BubbleDelegateView::CreateBubble(bubble_); |
397 UpdateLayout(); | 408 UpdateLayout(); |
398 } | 409 bubble_->GetWidget()->AddObserver(this); |
| 410 } |
399 bubble_->GetWidget()->Show(); | 411 bubble_->GetWidget()->Show(); |
400 } | 412 } |
401 | 413 |
402 void SpeechRecognitionBubbleImpl::Hide() { | 414 void SpeechRecognitionBubbleImpl::Hide() { |
403 if (bubble_) | 415 if (bubble_) |
404 bubble_->GetWidget()->Hide(); | 416 bubble_->GetWidget()->Hide(); |
405 } | 417 } |
406 | 418 |
407 void SpeechRecognitionBubbleImpl::UpdateLayout() { | 419 void SpeechRecognitionBubbleImpl::UpdateLayout() { |
408 if (bubble_ && GetWebContents()) | 420 if (bubble_ && GetWebContents()) |
409 bubble_->UpdateLayout(display_mode(), message_text(), icon_image()); | 421 bubble_->UpdateLayout(display_mode(), message_text(), icon_image()); |
410 } | 422 } |
411 | 423 |
412 void SpeechRecognitionBubbleImpl::UpdateImage() { | 424 void SpeechRecognitionBubbleImpl::UpdateImage() { |
413 if (bubble_ && GetWebContents()) | 425 if (bubble_ && GetWebContents()) |
414 bubble_->SetImage(icon_image()); | 426 bubble_->SetImage(icon_image()); |
415 } | 427 } |
416 | 428 |
417 } // namespace | 429 } // namespace |
418 | 430 |
419 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( | 431 SpeechRecognitionBubble* SpeechRecognitionBubble::CreateNativeBubble( |
420 int render_process_id, | 432 int render_process_id, |
421 int render_view_id, | 433 int render_view_id, |
422 SpeechRecognitionBubble::Delegate* delegate, | 434 SpeechRecognitionBubble::Delegate* delegate, |
423 const gfx::Rect& element_rect) { | 435 const gfx::Rect& element_rect) { |
424 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, | 436 return new SpeechRecognitionBubbleImpl(render_process_id, render_view_id, |
425 delegate, element_rect); | 437 delegate, element_rect); |
426 } | 438 } |
OLD | NEW |