| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 return can_activate_; | 119 return can_activate_; |
| 120 } | 120 } |
| 121 | 121 |
| 122 private: | 122 private: |
| 123 Widget* widget_; | 123 Widget* widget_; |
| 124 bool can_activate_; | 124 bool can_activate_; |
| 125 | 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate); | 126 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate); |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 class Widget::PostMousePressedProcessor : public WidgetObserver { | |
| 130 public: | |
| 131 explicit PostMousePressedProcessor(views::Widget* owner) : owner_(owner) { | |
| 132 owner_->AddObserver(this); | |
| 133 } | |
| 134 | |
| 135 virtual ~PostMousePressedProcessor() { | |
| 136 CleanupOwner(); | |
| 137 } | |
| 138 | |
| 139 void DoPostMousePressedProcessing() { | |
| 140 // Make sure we're still visible before we attempt capture as the mouse | |
| 141 // press processing may have made the window hide (as happens with menus). | |
| 142 if (!owner_ || !owner_->IsVisible()) | |
| 143 return; | |
| 144 | |
| 145 owner_->is_mouse_button_pressed_ = true; | |
| 146 | |
| 147 // OnNativeWidgetDestroying also notifies all | |
| 148 // WidgetObservers::OnWidgetDestroying. So we can be sure that if |owner_| | |
| 149 // is non-NULL, |owner_->native_widget_| will also be non-NULL. | |
| 150 if (!owner_->native_widget_private()->HasCapture()) | |
| 151 owner_->native_widget_private()->SetCapture(); | |
| 152 } | |
| 153 | |
| 154 // Overridden from WidgetObserver. | |
| 155 virtual void OnWidgetDestroying(Widget* widget) OVERRIDE { | |
| 156 CleanupOwner(); | |
| 157 } | |
| 158 | |
| 159 private: | |
| 160 void CleanupOwner() { | |
| 161 if (owner_) { | |
| 162 owner_->RemoveObserver(this); | |
| 163 owner_ = NULL; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 views::Widget* owner_; | |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(PostMousePressedProcessor); | |
| 170 }; | |
| 171 | |
| 172 //////////////////////////////////////////////////////////////////////////////// | 129 //////////////////////////////////////////////////////////////////////////////// |
| 173 // Widget, InitParams: | 130 // Widget, InitParams: |
| 174 | 131 |
| 175 Widget::InitParams::InitParams() | 132 Widget::InitParams::InitParams() |
| 176 : type(TYPE_WINDOW), | 133 : type(TYPE_WINDOW), |
| 177 delegate(NULL), | 134 delegate(NULL), |
| 178 child(false), | 135 child(false), |
| 179 transient(false), | 136 transient(false), |
| 180 transparent(ViewsDelegate::views_delegate && | 137 transparent(ViewsDelegate::views_delegate && |
| 181 ViewsDelegate::views_delegate->UseTransparentWindows()), | 138 ViewsDelegate::views_delegate->UseTransparentWindows()), |
| (...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1187 void Widget::OnKeyEvent(ui::KeyEvent* event) { | 1144 void Widget::OnKeyEvent(ui::KeyEvent* event) { |
| 1188 ScopedEvent scoped(this, *event); | 1145 ScopedEvent scoped(this, *event); |
| 1189 static_cast<internal::RootView*>(GetRootView())-> | 1146 static_cast<internal::RootView*>(GetRootView())-> |
| 1190 DispatchKeyEvent(event); | 1147 DispatchKeyEvent(event); |
| 1191 } | 1148 } |
| 1192 | 1149 |
| 1193 void Widget::OnMouseEvent(ui::MouseEvent* event) { | 1150 void Widget::OnMouseEvent(ui::MouseEvent* event) { |
| 1194 ScopedEvent scoped(this, *event); | 1151 ScopedEvent scoped(this, *event); |
| 1195 View* root_view = GetRootView(); | 1152 View* root_view = GetRootView(); |
| 1196 switch (event->type()) { | 1153 switch (event->type()) { |
| 1197 case ui::ET_MOUSE_PRESSED: { | 1154 case ui::ET_MOUSE_PRESSED: |
| 1198 last_mouse_event_was_move_ = false; | 1155 last_mouse_event_was_move_ = false; |
| 1199 | 1156 // Make sure we're still visible before we attempt capture as the mouse |
| 1200 // We may get deleted by the time we return from OnMousePressed. So we | 1157 // press processing may have made the window hide (as happens with menus). |
| 1201 // use an observer to do capture after OnMousePressed in a safe way. | 1158 if (root_view && root_view->OnMousePressed(*event) && IsVisible()) { |
| 1202 PostMousePressedProcessor post_mouse_pressed_processor(this); | 1159 is_mouse_button_pressed_ = true; |
| 1203 if (root_view && root_view->OnMousePressed(*event)) { | 1160 if (!native_widget_->HasCapture()) |
| 1204 post_mouse_pressed_processor.DoPostMousePressedProcessing(); | 1161 native_widget_->SetCapture(); |
| 1205 event->SetHandled(); | 1162 event->SetHandled(); |
| 1206 } | 1163 } |
| 1207 return; | 1164 return; |
| 1208 } | |
| 1209 case ui::ET_MOUSE_RELEASED: | 1165 case ui::ET_MOUSE_RELEASED: |
| 1210 last_mouse_event_was_move_ = false; | 1166 last_mouse_event_was_move_ = false; |
| 1211 is_mouse_button_pressed_ = false; | 1167 is_mouse_button_pressed_ = false; |
| 1212 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1168 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
| 1213 if (native_widget_->HasCapture() && | 1169 if (native_widget_->HasCapture() && |
| 1214 ShouldReleaseCaptureOnMouseReleased()) { | 1170 ShouldReleaseCaptureOnMouseReleased()) { |
| 1215 native_widget_->ReleaseCapture(); | 1171 native_widget_->ReleaseCapture(); |
| 1216 } | 1172 } |
| 1217 if (root_view) | 1173 if (root_view) |
| 1218 root_view->OnMouseReleased(*event); | 1174 root_view->OnMouseReleased(*event); |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1473 | 1429 |
| 1474 //////////////////////////////////////////////////////////////////////////////// | 1430 //////////////////////////////////////////////////////////////////////////////// |
| 1475 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1431 // internal::NativeWidgetPrivate, NativeWidget implementation: |
| 1476 | 1432 |
| 1477 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1433 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
| 1478 return this; | 1434 return this; |
| 1479 } | 1435 } |
| 1480 | 1436 |
| 1481 } // namespace internal | 1437 } // namespace internal |
| 1482 } // namespace views | 1438 } // namespace views |
| OLD | NEW |