Chromium Code Reviews| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 // Finally, make a default one. | 54 // Finally, make a default one. |
| 55 NativeWidget* CreateNativeWidget(NativeWidget* native_widget, | 55 NativeWidget* CreateNativeWidget(NativeWidget* native_widget, |
| 56 internal::NativeWidgetDelegate* delegate) { | 56 internal::NativeWidgetDelegate* delegate) { |
| 57 if (!native_widget) { | 57 if (!native_widget) { |
| 58 native_widget = | 58 native_widget = |
| 59 internal::NativeWidgetPrivate::CreateNativeWidget(delegate); | 59 internal::NativeWidgetPrivate::CreateNativeWidget(delegate); |
| 60 } | 60 } |
| 61 return native_widget; | 61 return native_widget; |
| 62 } | 62 } |
| 63 | 63 |
| 64 class PostMousePressedProcessor : public WidgetObserver { | |
|
sky
2013/03/21 22:17:48
How about declaring this as an inner class (you ca
varunjain
2013/03/21 22:47:14
Done.
| |
| 65 public: | |
| 66 explicit PostMousePressedProcessor(views::Widget* owner) : owner_(owner) { | |
| 67 owner_->AddObserver(this); | |
| 68 } | |
| 69 | |
| 70 virtual ~PostMousePressedProcessor() { | |
| 71 if (owner_) { | |
| 72 owner_->RemoveObserver(this); | |
| 73 owner_ = NULL; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void DoPostMousePressedProcessing() { | |
| 78 // Make sure we're still visible before we attempt capture as the mouse | |
| 79 // press processing may have made the window hide (as happens with menus). | |
| 80 if (!owner_ || !owner_->IsVisible()) | |
| 81 return; | |
| 82 | |
| 83 owner_->set_is_mouse_button_pressed(true); | |
| 84 | |
| 85 // OnNativeWidgetDestroying also notifies all | |
| 86 // WidgetObservers::OnWidgetDestroying. So we can be sure that if |owner_| | |
| 87 // is non-NULL, |owner_->native_widget_| will also be non-NULL. | |
| 88 if (!owner_->native_widget_private()->HasCapture()) | |
| 89 owner_->native_widget_private()->SetCapture(); | |
| 90 } | |
| 91 | |
| 92 // Overridden from WidgetObserver. | |
| 93 virtual void OnWidgetDestroying(Widget* widget) OVERRIDE { | |
| 94 if (owner_) { | |
|
sky
2013/03/21 22:17:48
Consolidate this and 71-74.
varunjain
2013/03/21 22:47:14
Done.
| |
| 95 owner_->RemoveObserver(this); | |
| 96 owner_ = NULL; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 views::Widget* owner_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(PostMousePressedProcessor); | |
| 104 }; | |
| 105 | |
| 64 } // namespace | 106 } // namespace |
| 65 | 107 |
| 66 // This class is used to keep track of the event a Widget is processing, and | 108 // This class is used to keep track of the event a Widget is processing, and |
| 67 // restore any previously active event afterwards. | 109 // restore any previously active event afterwards. |
| 68 class ScopedEvent { | 110 class ScopedEvent { |
| 69 public: | 111 public: |
| 70 ScopedEvent(Widget* widget, const ui::Event& event) | 112 ScopedEvent(Widget* widget, const ui::Event& event) |
| 71 : widget_(widget), | 113 : widget_(widget), |
| 72 event_(&event) { | 114 event_(&event) { |
| 73 widget->event_stack_.push(this); | 115 widget->event_stack_.push(this); |
| (...skipping 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1136 void Widget::OnKeyEvent(ui::KeyEvent* event) { | 1178 void Widget::OnKeyEvent(ui::KeyEvent* event) { |
| 1137 ScopedEvent scoped(this, *event); | 1179 ScopedEvent scoped(this, *event); |
| 1138 static_cast<internal::RootView*>(GetRootView())-> | 1180 static_cast<internal::RootView*>(GetRootView())-> |
| 1139 DispatchKeyEvent(event); | 1181 DispatchKeyEvent(event); |
| 1140 } | 1182 } |
| 1141 | 1183 |
| 1142 void Widget::OnMouseEvent(ui::MouseEvent* event) { | 1184 void Widget::OnMouseEvent(ui::MouseEvent* event) { |
| 1143 ScopedEvent scoped(this, *event); | 1185 ScopedEvent scoped(this, *event); |
| 1144 View* root_view = GetRootView(); | 1186 View* root_view = GetRootView(); |
| 1145 switch (event->type()) { | 1187 switch (event->type()) { |
| 1146 case ui::ET_MOUSE_PRESSED: | 1188 case ui::ET_MOUSE_PRESSED: { |
| 1147 last_mouse_event_was_move_ = false; | 1189 last_mouse_event_was_move_ = false; |
| 1148 // Make sure we're still visible before we attempt capture as the mouse | 1190 |
| 1149 // press processing may have made the window hide (as happens with menus). | 1191 // We may get deleted by the time we return from OnMousePressed. So we |
| 1150 if (root_view && root_view->OnMousePressed(*event) && IsVisible()) { | 1192 // use an observer to do capture after OnMousePressed in a safe way. |
| 1151 is_mouse_button_pressed_ = true; | 1193 PostMousePressedProcessor post_mouse_pressed_processor(this); |
| 1152 if (!native_widget_->HasCapture()) | 1194 if (root_view && root_view->OnMousePressed(*event)) { |
| 1153 native_widget_->SetCapture(); | 1195 post_mouse_pressed_processor.DoPostMousePressedProcessing(); |
| 1154 event->SetHandled(); | 1196 event->SetHandled(); |
| 1155 } | 1197 } |
| 1156 return; | 1198 return; |
| 1199 } | |
| 1157 case ui::ET_MOUSE_RELEASED: | 1200 case ui::ET_MOUSE_RELEASED: |
| 1158 last_mouse_event_was_move_ = false; | 1201 last_mouse_event_was_move_ = false; |
| 1159 is_mouse_button_pressed_ = false; | 1202 is_mouse_button_pressed_ = false; |
| 1160 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1203 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
| 1161 if (native_widget_->HasCapture() && | 1204 if (native_widget_->HasCapture() && |
| 1162 ShouldReleaseCaptureOnMouseReleased()) { | 1205 ShouldReleaseCaptureOnMouseReleased()) { |
| 1163 native_widget_->ReleaseCapture(); | 1206 native_widget_->ReleaseCapture(); |
| 1164 } | 1207 } |
| 1165 if (root_view) | 1208 if (root_view) |
| 1166 root_view->OnMouseReleased(*event); | 1209 root_view->OnMouseReleased(*event); |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1421 | 1464 |
| 1422 //////////////////////////////////////////////////////////////////////////////// | 1465 //////////////////////////////////////////////////////////////////////////////// |
| 1423 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1466 // internal::NativeWidgetPrivate, NativeWidget implementation: |
| 1424 | 1467 |
| 1425 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1468 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
| 1426 return this; | 1469 return this; |
| 1427 } | 1470 } |
| 1428 | 1471 |
| 1429 } // namespace internal | 1472 } // namespace internal |
| 1430 } // namespace views | 1473 } // namespace views |
| OLD | NEW |