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/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), | 161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), |
162 is_secondary_widget_(true), | 162 is_secondary_widget_(true), |
163 frame_type_(FRAME_TYPE_DEFAULT), | 163 frame_type_(FRAME_TYPE_DEFAULT), |
164 disable_inactive_rendering_(false), | 164 disable_inactive_rendering_(false), |
165 widget_closed_(false), | 165 widget_closed_(false), |
166 saved_show_state_(ui::SHOW_STATE_DEFAULT), | 166 saved_show_state_(ui::SHOW_STATE_DEFAULT), |
167 focus_on_creation_(true), | 167 focus_on_creation_(true), |
168 is_top_level_(false), | 168 is_top_level_(false), |
169 native_widget_initialized_(false), | 169 native_widget_initialized_(false), |
170 native_widget_destroyed_(false), | 170 native_widget_destroyed_(false), |
| 171 is_mouse_button_pressed_(false), |
171 ignore_capture_loss_(false), | 172 ignore_capture_loss_(false), |
172 last_mouse_event_was_move_(false), | 173 last_mouse_event_was_move_(false), |
173 auto_release_capture_(true), | 174 auto_release_capture_(true), |
174 root_layers_dirty_(false), | 175 root_layers_dirty_(false), |
175 movement_disabled_(false), | 176 movement_disabled_(false), |
176 observer_manager_(this) { | 177 observer_manager_(this) { |
177 } | 178 } |
178 | 179 |
179 Widget::~Widget() { | 180 Widget::~Widget() { |
180 DestroyRootView(); | 181 DestroyRootView(); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 | 344 |
344 widget_delegate_ = params.delegate ? | 345 widget_delegate_ = params.delegate ? |
345 params.delegate : new DefaultWidgetDelegate(this); | 346 params.delegate : new DefaultWidgetDelegate(this); |
346 widget_delegate_->set_can_activate(can_activate); | 347 widget_delegate_->set_can_activate(can_activate); |
347 | 348 |
348 ownership_ = params.ownership; | 349 ownership_ = params.ownership; |
349 native_widget_ = CreateNativeWidget(params.native_widget, this)-> | 350 native_widget_ = CreateNativeWidget(params.native_widget, this)-> |
350 AsNativeWidgetPrivate(); | 351 AsNativeWidgetPrivate(); |
351 root_view_.reset(CreateRootView()); | 352 root_view_.reset(CreateRootView()); |
352 default_theme_provider_.reset(new ui::DefaultThemeProvider); | 353 default_theme_provider_.reset(new ui::DefaultThemeProvider); |
| 354 if (params.type == InitParams::TYPE_MENU) { |
| 355 is_mouse_button_pressed_ = |
| 356 internal::NativeWidgetPrivate::IsMouseButtonDown(); |
| 357 } |
353 native_widget_->InitNativeWidget(params); | 358 native_widget_->InitNativeWidget(params); |
354 if (RequiresNonClientView(params.type)) { | 359 if (RequiresNonClientView(params.type)) { |
355 non_client_view_ = new NonClientView; | 360 non_client_view_ = new NonClientView; |
356 non_client_view_->SetFrameView(CreateNonClientFrameView()); | 361 non_client_view_->SetFrameView(CreateNonClientFrameView()); |
357 // Create the ClientView, add it to the NonClientView and add the | 362 // Create the ClientView, add it to the NonClientView and add the |
358 // NonClientView to the RootView. This will cause everything to be parented. | 363 // NonClientView to the RootView. This will cause everything to be parented. |
359 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); | 364 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); |
360 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView()); | 365 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView()); |
361 SetContentsView(non_client_view_); | 366 SetContentsView(non_client_view_); |
362 // Initialize the window's title before setting the window's initial bounds; | 367 // Initialize the window's title before setting the window's initial bounds; |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 | 941 |
937 void Widget::SetCapture(View* view) { | 942 void Widget::SetCapture(View* view) { |
938 if (!native_widget_->HasCapture()) { | 943 if (!native_widget_->HasCapture()) { |
939 native_widget_->SetCapture(); | 944 native_widget_->SetCapture(); |
940 | 945 |
941 // Early return if setting capture was unsuccessful. | 946 // Early return if setting capture was unsuccessful. |
942 if (!native_widget_->HasCapture()) | 947 if (!native_widget_->HasCapture()) |
943 return; | 948 return; |
944 } | 949 } |
945 | 950 |
| 951 if (internal::NativeWidgetPrivate::IsMouseButtonDown()) |
| 952 is_mouse_button_pressed_ = true; |
946 root_view_->SetMouseHandler(view); | 953 root_view_->SetMouseHandler(view); |
947 } | 954 } |
948 | 955 |
949 void Widget::ReleaseCapture() { | 956 void Widget::ReleaseCapture() { |
950 if (native_widget_->HasCapture()) | 957 if (native_widget_->HasCapture()) |
951 native_widget_->ReleaseCapture(); | 958 native_widget_->ReleaseCapture(); |
952 } | 959 } |
953 | 960 |
954 bool Widget::HasCapture() { | 961 bool Widget::HasCapture() { |
955 return native_widget_->HasCapture(); | 962 return native_widget_->HasCapture(); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1176 } | 1183 } |
1177 | 1184 |
1178 void Widget::OnKeyEvent(ui::KeyEvent* event) { | 1185 void Widget::OnKeyEvent(ui::KeyEvent* event) { |
1179 SendEventToProcessor(event); | 1186 SendEventToProcessor(event); |
1180 } | 1187 } |
1181 | 1188 |
1182 // TODO(tdanderson): We should not be calling the OnMouse*() functions on | 1189 // TODO(tdanderson): We should not be calling the OnMouse*() functions on |
1183 // RootView from anywhere in Widget. Use | 1190 // RootView from anywhere in Widget. Use |
1184 // SendEventToProcessor() instead. See crbug.com/348087. | 1191 // SendEventToProcessor() instead. See crbug.com/348087. |
1185 void Widget::OnMouseEvent(ui::MouseEvent* event) { | 1192 void Widget::OnMouseEvent(ui::MouseEvent* event) { |
1186 if (event->type() != ui::ET_MOUSE_MOVED) | |
1187 last_mouse_event_was_move_ = false; | |
1188 | |
1189 View* root_view = GetRootView(); | 1193 View* root_view = GetRootView(); |
1190 switch (event->type()) { | 1194 switch (event->type()) { |
1191 case ui::ET_MOUSE_PRESSED: { | 1195 case ui::ET_MOUSE_PRESSED: { |
| 1196 last_mouse_event_was_move_ = false; |
| 1197 |
1192 // We may get deleted by the time we return from OnMousePressed. So we | 1198 // We may get deleted by the time we return from OnMousePressed. So we |
1193 // use an observer to make sure we are still alive. | 1199 // use an observer to make sure we are still alive. |
1194 WidgetDeletionObserver widget_deletion_observer(this); | 1200 WidgetDeletionObserver widget_deletion_observer(this); |
1195 | 1201 |
1196 // Make sure we're still visible before we attempt capture as the mouse | 1202 // Make sure we're still visible before we attempt capture as the mouse |
1197 // press processing may have made the window hide (as happens with menus). | 1203 // press processing may have made the window hide (as happens with menus). |
1198 | 1204 |
1199 // It is possible for a View to show a context menu on mouse-press. Since | 1205 // It is possible for a View to show a context menu on mouse-press. Since |
1200 // the menu does a capture and starts a nested message-loop, the release | 1206 // the menu does a capture and starts a nested message-loop, the release |
1201 // would go to the menu. The next click (i.e. both mouse-press and release | 1207 // would go to the menu. The next click (i.e. both mouse-press and release |
1202 // events) also go to the menu. The menu (and the nested message-loop) | 1208 // events) also go to the menu. The menu (and the nested message-loop) |
1203 // gets closed after this second release event. The code then resumes from | 1209 // gets closed after this second release event. The code then resumes from |
1204 // here. So make sure that the mouse-button is still down before doing a | 1210 // here. So make sure that the mouse-button is still down before doing a |
1205 // capture. | 1211 // capture. |
1206 if (root_view && root_view->OnMousePressed(*event) && | 1212 if (root_view && root_view->OnMousePressed(*event) && |
1207 widget_deletion_observer.IsWidgetAlive() && IsVisible() && | 1213 widget_deletion_observer.IsWidgetAlive() && IsVisible() && |
1208 internal::NativeWidgetPrivate::IsMouseButtonDown()) { | 1214 internal::NativeWidgetPrivate::IsMouseButtonDown()) { |
| 1215 is_mouse_button_pressed_ = true; |
1209 if (!native_widget_->HasCapture()) | 1216 if (!native_widget_->HasCapture()) |
1210 native_widget_->SetCapture(); | 1217 native_widget_->SetCapture(); |
1211 event->SetHandled(); | 1218 event->SetHandled(); |
1212 } | 1219 } |
1213 return; | 1220 return; |
1214 } | 1221 } |
1215 | 1222 |
1216 case ui::ET_MOUSE_RELEASED: | 1223 case ui::ET_MOUSE_RELEASED: |
| 1224 last_mouse_event_was_move_ = false; |
| 1225 is_mouse_button_pressed_ = false; |
1217 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1226 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
1218 if (auto_release_capture_ && native_widget_->HasCapture()) { | 1227 if (auto_release_capture_ && native_widget_->HasCapture()) { |
1219 base::AutoReset<bool> resetter(&ignore_capture_loss_, true); | 1228 base::AutoReset<bool> resetter(&ignore_capture_loss_, true); |
1220 native_widget_->ReleaseCapture(); | 1229 native_widget_->ReleaseCapture(); |
1221 } | 1230 } |
1222 if (root_view) | 1231 if (root_view) |
1223 root_view->OnMouseReleased(*event); | 1232 root_view->OnMouseReleased(*event); |
1224 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) | 1233 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) |
1225 event->SetHandled(); | 1234 event->SetHandled(); |
1226 return; | 1235 return; |
1227 | 1236 |
1228 case ui::ET_MOUSE_MOVED: | 1237 case ui::ET_MOUSE_MOVED: |
1229 if (!last_mouse_event_was_move_ || | 1238 case ui::ET_MOUSE_DRAGGED: |
1230 last_mouse_event_position_ != event->location()) { | 1239 if (native_widget_->HasCapture() && is_mouse_button_pressed_) { |
| 1240 last_mouse_event_was_move_ = false; |
| 1241 if (root_view) |
| 1242 root_view->OnMouseDragged(*event); |
| 1243 } else if (!last_mouse_event_was_move_ || |
| 1244 last_mouse_event_position_ != event->location()) { |
1231 last_mouse_event_position_ = event->location(); | 1245 last_mouse_event_position_ = event->location(); |
1232 last_mouse_event_was_move_ = true; | 1246 last_mouse_event_was_move_ = true; |
1233 if (root_view) | 1247 if (root_view) |
1234 root_view->OnMouseMoved(*event); | 1248 root_view->OnMouseMoved(*event); |
1235 } | 1249 } |
1236 return; | 1250 return; |
1237 | 1251 |
1238 case ui::ET_MOUSE_DRAGGED: | |
1239 if (root_view) | |
1240 root_view->OnMouseDragged(*event); | |
1241 return; | |
1242 | |
1243 case ui::ET_MOUSE_EXITED: | 1252 case ui::ET_MOUSE_EXITED: |
| 1253 last_mouse_event_was_move_ = false; |
1244 if (root_view) | 1254 if (root_view) |
1245 root_view->OnMouseExited(*event); | 1255 root_view->OnMouseExited(*event); |
1246 return; | 1256 return; |
1247 | 1257 |
1248 case ui::ET_MOUSEWHEEL: | 1258 case ui::ET_MOUSEWHEEL: |
1249 if (root_view && root_view->OnMouseWheel( | 1259 if (root_view && root_view->OnMouseWheel( |
1250 static_cast<const ui::MouseWheelEvent&>(*event))) | 1260 static_cast<const ui::MouseWheelEvent&>(*event))) |
1251 event->SetHandled(); | 1261 event->SetHandled(); |
1252 return; | 1262 return; |
1253 | 1263 |
1254 default: | 1264 default: |
1255 return; | 1265 return; |
1256 } | 1266 } |
1257 } | 1267 } |
1258 | 1268 |
1259 void Widget::OnMouseCaptureLost() { | 1269 void Widget::OnMouseCaptureLost() { |
1260 if (ignore_capture_loss_) | 1270 if (ignore_capture_loss_) |
1261 return; | 1271 return; |
1262 | 1272 |
1263 View* root_view = GetRootView(); | 1273 View* root_view = GetRootView(); |
1264 if (root_view) | 1274 if (root_view) |
1265 root_view->OnMouseCaptureLost(); | 1275 root_view->OnMouseCaptureLost(); |
| 1276 is_mouse_button_pressed_ = false; |
1266 } | 1277 } |
1267 | 1278 |
1268 void Widget::OnScrollEvent(ui::ScrollEvent* event) { | 1279 void Widget::OnScrollEvent(ui::ScrollEvent* event) { |
1269 ui::ScrollEvent event_copy(*event); | 1280 ui::ScrollEvent event_copy(*event); |
1270 SendEventToProcessor(&event_copy); | 1281 SendEventToProcessor(&event_copy); |
1271 | 1282 |
1272 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events. | 1283 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events. |
1273 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) { | 1284 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) { |
1274 ui::MouseWheelEvent wheel(*event); | 1285 ui::MouseWheelEvent wheel(*event); |
1275 OnMouseEvent(&wheel); | 1286 OnMouseEvent(&wheel); |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1510 | 1521 |
1511 //////////////////////////////////////////////////////////////////////////////// | 1522 //////////////////////////////////////////////////////////////////////////////// |
1512 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1523 // internal::NativeWidgetPrivate, NativeWidget implementation: |
1513 | 1524 |
1514 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1525 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
1515 return this; | 1526 return this; |
1516 } | 1527 } |
1517 | 1528 |
1518 } // namespace internal | 1529 } // namespace internal |
1519 } // namespace views | 1530 } // namespace views |
OLD | NEW |