| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_input_bubble.h" | 5 #include "chrome/browser/speech/speech_input_bubble.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_window.h" | 11 #include "chrome/browser/browser_window.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents_view.h" | 13 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/views/info_bubble.h" | 15 #include "chrome/browser/views/info_bubble.h" |
| 16 #include "chrome/common/notification_observer.h" | |
| 17 #include "chrome/common/notification_registrar.h" | |
| 18 #include "chrome/common/notification_source.h" | |
| 19 #include "chrome/common/notification_type.h" | |
| 20 #include "gfx/canvas.h" | 16 #include "gfx/canvas.h" |
| 21 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 23 #include "views/controls/button/native_button.h" | 19 #include "views/controls/button/native_button.h" |
| 24 #include "views/controls/image_view.h" | 20 #include "views/controls/image_view.h" |
| 25 #include "views/controls/label.h" | 21 #include "views/controls/label.h" |
| 26 #include "views/standard_layout.h" | 22 #include "views/standard_layout.h" |
| 27 #include "views/view.h" | 23 #include "views/view.h" |
| 28 | 24 |
| 29 namespace { | 25 namespace { |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 185 |
| 190 height = cancel_->GetPreferredSize().height(); | 186 height = cancel_->GetPreferredSize().height(); |
| 191 int width = cancel_->GetPreferredSize().width(); | 187 int width = cancel_->GetPreferredSize().width(); |
| 192 cancel_->SetBounds(x + (available_width - width) / 2, y, width, height); | 188 cancel_->SetBounds(x + (available_width - width) / 2, y, width, height); |
| 193 } | 189 } |
| 194 } | 190 } |
| 195 | 191 |
| 196 // Implementation of SpeechInputBubble. | 192 // Implementation of SpeechInputBubble. |
| 197 class SpeechInputBubbleImpl | 193 class SpeechInputBubbleImpl |
| 198 : public SpeechInputBubbleBase, | 194 : public SpeechInputBubbleBase, |
| 199 public InfoBubbleDelegate, | 195 public InfoBubbleDelegate { |
| 200 public NotificationObserver { | |
| 201 public: | 196 public: |
| 202 SpeechInputBubbleImpl(TabContents* tab_contents, | 197 SpeechInputBubbleImpl(TabContents* tab_contents, |
| 203 Delegate* delegate, | 198 Delegate* delegate, |
| 204 const gfx::Rect& element_rect); | 199 const gfx::Rect& element_rect); |
| 205 virtual ~SpeechInputBubbleImpl(); | 200 virtual ~SpeechInputBubbleImpl(); |
| 206 | 201 |
| 207 // SpeechInputBubble methods. | 202 // SpeechInputBubble methods. |
| 208 virtual void Show(); | 203 virtual void Show(); |
| 209 virtual void Hide(); | 204 virtual void Hide(); |
| 210 | 205 |
| 211 // SpeechInputBubbleBase methods. | 206 // SpeechInputBubbleBase methods. |
| 212 virtual void UpdateLayout(); | 207 virtual void UpdateLayout(); |
| 213 virtual void SetImage(const SkBitmap& image); | 208 virtual void SetImage(const SkBitmap& image); |
| 214 | 209 |
| 215 // Returns the screen rectangle to use as the info bubble's target. | 210 // Returns the screen rectangle to use as the info bubble's target. |
| 216 // |element_rect| is the html element's bounds in page coordinates. | 211 // |element_rect| is the html element's bounds in page coordinates. |
| 217 gfx::Rect GetInfoBubbleTarget(const gfx::Rect& element_rect); | 212 gfx::Rect GetInfoBubbleTarget(const gfx::Rect& element_rect); |
| 218 | 213 |
| 219 // NotificationObserver implementation. | |
| 220 virtual void Observe(NotificationType type, | |
| 221 const NotificationSource& source, | |
| 222 const NotificationDetails& details); | |
| 223 | |
| 224 // InfoBubbleDelegate | 214 // InfoBubbleDelegate |
| 225 virtual void InfoBubbleClosing(InfoBubble* info_bubble, | 215 virtual void InfoBubbleClosing(InfoBubble* info_bubble, |
| 226 bool closed_by_escape); | 216 bool closed_by_escape); |
| 227 virtual bool CloseOnEscape(); | 217 virtual bool CloseOnEscape(); |
| 228 virtual bool FadeInOnShow(); | 218 virtual bool FadeInOnShow(); |
| 229 | 219 |
| 230 private: | 220 private: |
| 231 Delegate* delegate_; | 221 Delegate* delegate_; |
| 232 InfoBubble* info_bubble_; | 222 InfoBubble* info_bubble_; |
| 233 TabContents* tab_contents_; | |
| 234 ContentView* bubble_content_; | 223 ContentView* bubble_content_; |
| 235 NotificationRegistrar registrar_; | |
| 236 gfx::Rect element_rect_; | 224 gfx::Rect element_rect_; |
| 237 | 225 |
| 238 // Set to true if the object is being destroyed normally instead of the | 226 // Set to true if the object is being destroyed normally instead of the |
| 239 // user clicking outside the window causing it to close automatically. | 227 // user clicking outside the window causing it to close automatically. |
| 240 bool did_invoke_close_; | 228 bool did_invoke_close_; |
| 241 | 229 |
| 242 DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleImpl); | 230 DISALLOW_COPY_AND_ASSIGN(SpeechInputBubbleImpl); |
| 243 }; | 231 }; |
| 244 | 232 |
| 245 SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents, | 233 SpeechInputBubbleImpl::SpeechInputBubbleImpl(TabContents* tab_contents, |
| 246 Delegate* delegate, | 234 Delegate* delegate, |
| 247 const gfx::Rect& element_rect) | 235 const gfx::Rect& element_rect) |
| 248 : delegate_(delegate), | 236 : SpeechInputBubbleBase(tab_contents), |
| 237 delegate_(delegate), |
| 249 info_bubble_(NULL), | 238 info_bubble_(NULL), |
| 250 tab_contents_(tab_contents), | |
| 251 bubble_content_(NULL), | 239 bubble_content_(NULL), |
| 252 element_rect_(element_rect), | 240 element_rect_(element_rect), |
| 253 did_invoke_close_(false) { | 241 did_invoke_close_(false) { |
| 254 } | 242 } |
| 255 | 243 |
| 256 SpeechInputBubbleImpl::~SpeechInputBubbleImpl() { | 244 SpeechInputBubbleImpl::~SpeechInputBubbleImpl() { |
| 257 did_invoke_close_ = true; | 245 did_invoke_close_ = true; |
| 258 Hide(); | 246 Hide(); |
| 259 } | 247 } |
| 260 | 248 |
| 261 gfx::Rect SpeechInputBubbleImpl::GetInfoBubbleTarget( | 249 gfx::Rect SpeechInputBubbleImpl::GetInfoBubbleTarget( |
| 262 const gfx::Rect& element_rect) { | 250 const gfx::Rect& element_rect) { |
| 263 gfx::Rect container_rect; | 251 gfx::Rect container_rect; |
| 264 tab_contents_->GetContainerBounds(&container_rect); | 252 tab_contents()->GetContainerBounds(&container_rect); |
| 265 return gfx::Rect( | 253 return gfx::Rect( |
| 266 container_rect.x() + element_rect.x() + kBubbleTargetOffsetX, | 254 container_rect.x() + element_rect.x() + kBubbleTargetOffsetX, |
| 267 container_rect.y() + element_rect.y() + element_rect.height(), 1, 1); | 255 container_rect.y() + element_rect.y() + element_rect.height(), 1, 1); |
| 268 } | 256 } |
| 269 | 257 |
| 270 void SpeechInputBubbleImpl::Observe(NotificationType type, | |
| 271 const NotificationSource& source, | |
| 272 const NotificationDetails& details) { | |
| 273 if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | |
| 274 delegate_->InfoBubbleButtonClicked(SpeechInputBubble::BUTTON_CANCEL); | |
| 275 } else { | |
| 276 NOTREACHED() << "Unknown notification"; | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 void SpeechInputBubbleImpl::InfoBubbleClosing(InfoBubble* info_bubble, | 258 void SpeechInputBubbleImpl::InfoBubbleClosing(InfoBubble* info_bubble, |
| 281 bool closed_by_escape) { | 259 bool closed_by_escape) { |
| 282 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
| 283 Source<TabContents>(tab_contents_)); | |
| 284 info_bubble_ = NULL; | 260 info_bubble_ = NULL; |
| 285 bubble_content_ = NULL; | 261 bubble_content_ = NULL; |
| 286 if (!did_invoke_close_) | 262 if (!did_invoke_close_) |
| 287 delegate_->InfoBubbleFocusChanged(); | 263 delegate_->InfoBubbleFocusChanged(); |
| 288 } | 264 } |
| 289 | 265 |
| 290 bool SpeechInputBubbleImpl::CloseOnEscape() { | 266 bool SpeechInputBubbleImpl::CloseOnEscape() { |
| 291 return false; | 267 return false; |
| 292 } | 268 } |
| 293 | 269 |
| 294 bool SpeechInputBubbleImpl::FadeInOnShow() { | 270 bool SpeechInputBubbleImpl::FadeInOnShow() { |
| 295 return false; | 271 return false; |
| 296 } | 272 } |
| 297 | 273 |
| 298 void SpeechInputBubbleImpl::Show() { | 274 void SpeechInputBubbleImpl::Show() { |
| 299 if (info_bubble_) | 275 if (info_bubble_) |
| 300 return; // nothing to do, already visible. | 276 return; // nothing to do, already visible. |
| 301 | 277 |
| 302 bubble_content_ = new ContentView(delegate_); | 278 bubble_content_ = new ContentView(delegate_); |
| 303 UpdateLayout(); | 279 UpdateLayout(); |
| 304 | 280 |
| 305 views::Widget* parent = views::Widget::GetWidgetFromNativeWindow( | 281 views::Widget* parent = views::Widget::GetWidgetFromNativeWindow( |
| 306 tab_contents_->view()->GetTopLevelNativeWindow()); | 282 tab_contents()->view()->GetTopLevelNativeWindow()); |
| 307 info_bubble_ = InfoBubble::Show(parent, | 283 info_bubble_ = InfoBubble::Show(parent, |
| 308 GetInfoBubbleTarget(element_rect_), | 284 GetInfoBubbleTarget(element_rect_), |
| 309 BubbleBorder::TOP_LEFT, bubble_content_, | 285 BubbleBorder::TOP_LEFT, bubble_content_, |
| 310 this); | 286 this); |
| 311 | 287 |
| 312 // We don't want fade outs when closing because it makes speech recognition | 288 // We don't want fade outs when closing because it makes speech recognition |
| 313 // appear slower than it is. Also setting it to false allows |Close| to | 289 // appear slower than it is. Also setting it to false allows |Close| to |
| 314 // destroy the bubble immediately instead of waiting for the fade animation | 290 // destroy the bubble immediately instead of waiting for the fade animation |
| 315 // to end so the caller can manage this object's life cycle like a normal | 291 // to end so the caller can manage this object's life cycle like a normal |
| 316 // stack based or member variable object. | 292 // stack based or member variable object. |
| 317 info_bubble_->set_fade_away_on_close(false); | 293 info_bubble_->set_fade_away_on_close(false); |
| 318 | |
| 319 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
| 320 Source<TabContents>(tab_contents_)); | |
| 321 } | 294 } |
| 322 | 295 |
| 323 void SpeechInputBubbleImpl::Hide() { | 296 void SpeechInputBubbleImpl::Hide() { |
| 324 if (info_bubble_) | 297 if (info_bubble_) |
| 325 info_bubble_->Close(); | 298 info_bubble_->Close(); |
| 326 } | 299 } |
| 327 | 300 |
| 328 void SpeechInputBubbleImpl::UpdateLayout() { | 301 void SpeechInputBubbleImpl::UpdateLayout() { |
| 329 if (bubble_content_) | 302 if (bubble_content_) |
| 330 bubble_content_->UpdateLayout(display_mode(), message_text()); | 303 bubble_content_->UpdateLayout(display_mode(), message_text()); |
| 331 if (info_bubble_) // Will be null on first call. | 304 if (info_bubble_) // Will be null on first call. |
| 332 info_bubble_->SizeToContents(); | 305 info_bubble_->SizeToContents(); |
| 333 } | 306 } |
| 334 | 307 |
| 335 void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) { | 308 void SpeechInputBubbleImpl::SetImage(const SkBitmap& image) { |
| 336 if (bubble_content_) | 309 if (bubble_content_) |
| 337 bubble_content_->SetImage(image); | 310 bubble_content_->SetImage(image); |
| 338 } | 311 } |
| 339 | 312 |
| 340 } // namespace | 313 } // namespace |
| 341 | 314 |
| 342 SpeechInputBubble* SpeechInputBubble::CreateNativeBubble( | 315 SpeechInputBubble* SpeechInputBubble::CreateNativeBubble( |
| 343 TabContents* tab_contents, | 316 TabContents* tab_contents, |
| 344 SpeechInputBubble::Delegate* delegate, | 317 SpeechInputBubble::Delegate* delegate, |
| 345 const gfx::Rect& element_rect) { | 318 const gfx::Rect& element_rect) { |
| 346 return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect); | 319 return new SpeechInputBubbleImpl(tab_contents, delegate, element_rect); |
| 347 } | 320 } |
| OLD | NEW |