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 "ui/views/widget/widget.h" | 5 #include "ui/views/widget/widget.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.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" |
(...skipping 1133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1144 void Widget::OnKeyEvent(ui::KeyEvent* event) { | 1144 void Widget::OnKeyEvent(ui::KeyEvent* event) { |
1145 ScopedEvent scoped(this, *event); | 1145 ScopedEvent scoped(this, *event); |
1146 static_cast<internal::RootView*>(GetRootView())-> | 1146 static_cast<internal::RootView*>(GetRootView())-> |
1147 DispatchKeyEvent(event); | 1147 DispatchKeyEvent(event); |
1148 } | 1148 } |
1149 | 1149 |
1150 void Widget::OnMouseEvent(ui::MouseEvent* event) { | 1150 void Widget::OnMouseEvent(ui::MouseEvent* event) { |
1151 ScopedEvent scoped(this, *event); | 1151 ScopedEvent scoped(this, *event); |
1152 View* root_view = GetRootView(); | 1152 View* root_view = GetRootView(); |
1153 switch (event->type()) { | 1153 switch (event->type()) { |
1154 case ui::ET_MOUSE_PRESSED: | 1154 case ui::ET_MOUSE_PRESSED: { |
1155 last_mouse_event_was_move_ = false; | 1155 last_mouse_event_was_move_ = false; |
| 1156 |
| 1157 // We may get deleted by the time we return from OnMousePressed. So we |
| 1158 // use an observer to make sure we are still alive. |
| 1159 WidgetDeletionObserver widget_deletion_observer(this); |
1156 // Make sure we're still visible before we attempt capture as the mouse | 1160 // Make sure we're still visible before we attempt capture as the mouse |
1157 // press processing may have made the window hide (as happens with menus). | 1161 // press processing may have made the window hide (as happens with menus). |
1158 if (root_view && root_view->OnMousePressed(*event) && IsVisible()) { | 1162 if (root_view && root_view->OnMousePressed(*event) && |
| 1163 widget_deletion_observer.IsWidgetAlive() && IsVisible()) { |
1159 is_mouse_button_pressed_ = true; | 1164 is_mouse_button_pressed_ = true; |
1160 if (!native_widget_->HasCapture()) | 1165 if (!native_widget_->HasCapture()) |
1161 native_widget_->SetCapture(); | 1166 native_widget_->SetCapture(); |
1162 event->SetHandled(); | 1167 event->SetHandled(); |
1163 } | 1168 } |
1164 return; | 1169 return; |
| 1170 } |
1165 case ui::ET_MOUSE_RELEASED: | 1171 case ui::ET_MOUSE_RELEASED: |
1166 last_mouse_event_was_move_ = false; | 1172 last_mouse_event_was_move_ = false; |
1167 is_mouse_button_pressed_ = false; | 1173 is_mouse_button_pressed_ = false; |
1168 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1174 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
1169 if (native_widget_->HasCapture() && | 1175 if (native_widget_->HasCapture() && |
1170 ShouldReleaseCaptureOnMouseReleased()) { | 1176 ShouldReleaseCaptureOnMouseReleased()) { |
1171 native_widget_->ReleaseCapture(); | 1177 native_widget_->ReleaseCapture(); |
1172 } | 1178 } |
1173 if (root_view) | 1179 if (root_view) |
1174 root_view->OnMouseReleased(*event); | 1180 root_view->OnMouseReleased(*event); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 } | 1424 } |
1419 return false; | 1425 return false; |
1420 } | 1426 } |
1421 | 1427 |
1422 void Widget::ReplaceInputMethod(InputMethod* input_method) { | 1428 void Widget::ReplaceInputMethod(InputMethod* input_method) { |
1423 input_method_.reset(input_method); | 1429 input_method_.reset(input_method); |
1424 input_method->SetDelegate(native_widget_->GetInputMethodDelegate()); | 1430 input_method->SetDelegate(native_widget_->GetInputMethodDelegate()); |
1425 input_method->Init(this); | 1431 input_method->Init(this); |
1426 } | 1432 } |
1427 | 1433 |
| 1434 //////////////////////////////////////////////////////////////////////////////// |
| 1435 // WidgetDeletionObserver: |
| 1436 |
| 1437 WidgetDeletionObserver::WidgetDeletionObserver(Widget* widget) |
| 1438 : widget_(widget) { |
| 1439 if (widget_) |
| 1440 widget_->AddObserver(this); |
| 1441 } |
| 1442 |
| 1443 WidgetDeletionObserver::~WidgetDeletionObserver() { |
| 1444 CleanupWidget(); |
| 1445 } |
| 1446 |
| 1447 bool WidgetDeletionObserver::IsWidgetAlive() { |
| 1448 return widget_; |
| 1449 } |
| 1450 |
| 1451 void WidgetDeletionObserver::OnWidgetDestroying(Widget* widget) { |
| 1452 CleanupWidget(); |
| 1453 } |
| 1454 |
| 1455 void WidgetDeletionObserver::CleanupWidget() { |
| 1456 if (widget_) { |
| 1457 widget_->RemoveObserver(this); |
| 1458 widget_ = NULL; |
| 1459 } |
| 1460 } |
| 1461 |
1428 namespace internal { | 1462 namespace internal { |
1429 | 1463 |
1430 //////////////////////////////////////////////////////////////////////////////// | 1464 //////////////////////////////////////////////////////////////////////////////// |
1431 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1465 // internal::NativeWidgetPrivate, NativeWidget implementation: |
1432 | 1466 |
1433 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1467 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
1434 return this; | 1468 return this; |
1435 } | 1469 } |
1436 | 1470 |
1437 } // namespace internal | 1471 } // namespace internal |
1438 } // namespace views | 1472 } // namespace views |
OLD | NEW |