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/bind.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
10 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
11 #include "ui/base/hit_test.h" | 10 #include "ui/base/hit_test.h" |
12 #include "ui/base/l10n/l10n_font_util.h" | 11 #include "ui/base/l10n/l10n_font_util.h" |
13 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/compositor/compositor.h" | 13 #include "ui/compositor/compositor.h" |
15 #include "ui/compositor/layer.h" | 14 #include "ui/compositor/layer.h" |
16 #include "ui/gfx/screen.h" | 15 #include "ui/gfx/screen.h" |
17 #include "ui/views/focus/focus_manager.h" | 16 #include "ui/views/focus/focus_manager.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 100 |
102 Widget::InitParams::InitParams() | 101 Widget::InitParams::InitParams() |
103 : type(TYPE_WINDOW), | 102 : type(TYPE_WINDOW), |
104 delegate(NULL), | 103 delegate(NULL), |
105 child(false), | 104 child(false), |
106 transient(false), | 105 transient(false), |
107 transparent(ViewsDelegate::views_delegate && | 106 transparent(ViewsDelegate::views_delegate && |
108 ViewsDelegate::views_delegate->UseTransparentWindows()), | 107 ViewsDelegate::views_delegate->UseTransparentWindows()), |
109 accept_events(true), | 108 accept_events(true), |
110 can_activate(true), | 109 can_activate(true), |
111 close_on_deactivate(false), | |
112 keep_on_top(false), | 110 keep_on_top(false), |
113 ownership(NATIVE_WIDGET_OWNS_WIDGET), | 111 ownership(NATIVE_WIDGET_OWNS_WIDGET), |
114 mirror_origin_in_rtl(false), | 112 mirror_origin_in_rtl(false), |
115 has_dropshadow(false), | 113 has_dropshadow(false), |
116 remove_standard_frame(false), | 114 remove_standard_frame(false), |
117 show_state(ui::SHOW_STATE_DEFAULT), | 115 show_state(ui::SHOW_STATE_DEFAULT), |
118 double_buffer(false), | 116 double_buffer(false), |
119 parent(NULL), | 117 parent(NULL), |
120 parent_widget(NULL), | 118 parent_widget(NULL), |
121 native_widget(NULL), | 119 native_widget(NULL), |
122 top_level(false), | 120 top_level(false), |
123 layer_type(ui::LAYER_TEXTURED) { | 121 layer_type(ui::LAYER_TEXTURED) { |
124 } | 122 } |
125 | 123 |
126 Widget::InitParams::InitParams(Type type) | 124 Widget::InitParams::InitParams(Type type) |
127 : type(type), | 125 : type(type), |
128 delegate(NULL), | 126 delegate(NULL), |
129 child(type == TYPE_CONTROL), | 127 child(type == TYPE_CONTROL), |
130 transient(type == TYPE_BUBBLE || type == TYPE_POPUP || type == TYPE_MENU), | 128 transient(type == TYPE_BUBBLE || type == TYPE_POPUP || type == TYPE_MENU), |
131 transparent(type == TYPE_WINDOW && | 129 transparent(type == TYPE_WINDOW && |
132 ViewsDelegate::views_delegate && | 130 ViewsDelegate::views_delegate && |
133 ViewsDelegate::views_delegate->UseTransparentWindows()), | 131 ViewsDelegate::views_delegate->UseTransparentWindows()), |
134 accept_events(true), | 132 accept_events(true), |
135 can_activate(type != TYPE_POPUP && type != TYPE_MENU), | 133 can_activate(type != TYPE_POPUP && type != TYPE_MENU), |
136 close_on_deactivate(false), | |
137 keep_on_top(type == TYPE_MENU), | 134 keep_on_top(type == TYPE_MENU), |
138 ownership(NATIVE_WIDGET_OWNS_WIDGET), | 135 ownership(NATIVE_WIDGET_OWNS_WIDGET), |
139 mirror_origin_in_rtl(false), | 136 mirror_origin_in_rtl(false), |
140 has_dropshadow(false), | 137 has_dropshadow(false), |
141 remove_standard_frame(false), | 138 remove_standard_frame(false), |
142 show_state(ui::SHOW_STATE_DEFAULT), | 139 show_state(ui::SHOW_STATE_DEFAULT), |
143 double_buffer(false), | 140 double_buffer(false), |
144 parent(NULL), | 141 parent(NULL), |
145 parent_widget(NULL), | 142 parent_widget(NULL), |
146 native_widget(NULL), | 143 native_widget(NULL), |
147 top_level(false), | 144 top_level(false), |
148 layer_type(ui::LAYER_TEXTURED) { | 145 layer_type(ui::LAYER_TEXTURED) { |
149 } | 146 } |
150 | 147 |
151 gfx::NativeView Widget::InitParams::GetParent() const { | 148 gfx::NativeView Widget::InitParams::GetParent() const { |
152 return parent_widget ? parent_widget->GetNativeView() : parent; | 149 return parent_widget ? parent_widget->GetNativeView() : parent; |
153 } | 150 } |
154 | 151 |
155 //////////////////////////////////////////////////////////////////////////////// | 152 //////////////////////////////////////////////////////////////////////////////// |
156 // Widget, public: | 153 // Widget, public: |
157 | 154 |
158 Widget::Widget() | 155 Widget::Widget() |
159 : ALLOW_THIS_IN_INITIALIZER_LIST(set_capture_factory_(this)), | 156 : native_widget_(NULL), |
160 native_widget_(NULL), | |
161 widget_delegate_(NULL), | 157 widget_delegate_(NULL), |
162 non_client_view_(NULL), | 158 non_client_view_(NULL), |
163 dragged_view_(NULL), | 159 dragged_view_(NULL), |
164 event_stack_(), | 160 event_stack_(), |
165 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), | 161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), |
166 is_secondary_widget_(true), | 162 is_secondary_widget_(true), |
167 frame_type_(FRAME_TYPE_DEFAULT), | 163 frame_type_(FRAME_TYPE_DEFAULT), |
168 disable_inactive_rendering_(false), | 164 disable_inactive_rendering_(false), |
169 widget_closed_(false), | 165 widget_closed_(false), |
170 saved_show_state_(ui::SHOW_STATE_DEFAULT), | 166 saved_show_state_(ui::SHOW_STATE_DEFAULT), |
171 focus_on_creation_(true), | 167 focus_on_creation_(true), |
172 close_on_deactivate_(false), | |
173 is_top_level_(false), | 168 is_top_level_(false), |
174 native_widget_initialized_(false), | 169 native_widget_initialized_(false), |
175 native_widget_destroyed_(false), | 170 native_widget_destroyed_(false), |
176 is_mouse_button_pressed_(false), | 171 is_mouse_button_pressed_(false), |
177 last_mouse_event_was_move_(false) { | 172 last_mouse_event_was_move_(false) { |
178 } | 173 } |
179 | 174 |
180 Widget::~Widget() { | 175 Widget::~Widget() { |
181 while (!event_stack_.empty()) { | 176 while (!event_stack_.empty()) { |
182 event_stack_.top()->reset(); | 177 event_stack_.top()->reset(); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 SetInitialBounds(params.bounds); | 309 SetInitialBounds(params.bounds); |
315 if (params.show_state == ui::SHOW_STATE_MAXIMIZED) | 310 if (params.show_state == ui::SHOW_STATE_MAXIMIZED) |
316 Maximize(); | 311 Maximize(); |
317 else if (params.show_state == ui::SHOW_STATE_MINIMIZED) | 312 else if (params.show_state == ui::SHOW_STATE_MINIMIZED) |
318 Minimize(); | 313 Minimize(); |
319 UpdateWindowTitle(); | 314 UpdateWindowTitle(); |
320 } else if (params.delegate) { | 315 } else if (params.delegate) { |
321 SetContentsView(params.delegate->GetContentsView()); | 316 SetContentsView(params.delegate->GetContentsView()); |
322 SetInitialBoundsForFramelessWindow(params.bounds); | 317 SetInitialBoundsForFramelessWindow(params.bounds); |
323 } | 318 } |
324 close_on_deactivate_ = params.close_on_deactivate; | |
325 native_widget_initialized_ = true; | 319 native_widget_initialized_ = true; |
326 } | 320 } |
327 | 321 |
328 // Unconverted methods (see header) -------------------------------------------- | 322 // Unconverted methods (see header) -------------------------------------------- |
329 | 323 |
330 gfx::NativeView Widget::GetNativeView() const { | 324 gfx::NativeView Widget::GetNativeView() const { |
331 return native_widget_->GetNativeView(); | 325 return native_widget_->GetNativeView(); |
332 } | 326 } |
333 | 327 |
334 gfx::NativeWindow Widget::GetNativeWindow() const { | 328 gfx::NativeWindow Widget::GetNativeWindow() const { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 } else { | 504 } else { |
511 native_widget_->ShowWithWindowState(saved_show_state_); | 505 native_widget_->ShowWithWindowState(saved_show_state_); |
512 } | 506 } |
513 // |saved_show_state_| only applies the first time the window is shown. | 507 // |saved_show_state_| only applies the first time the window is shown. |
514 // If we don't reset the value the window may be shown maximized every time | 508 // If we don't reset the value the window may be shown maximized every time |
515 // it is subsequently shown after being hidden. | 509 // it is subsequently shown after being hidden. |
516 saved_show_state_ = ui::SHOW_STATE_NORMAL; | 510 saved_show_state_ = ui::SHOW_STATE_NORMAL; |
517 } else { | 511 } else { |
518 native_widget_->Show(); | 512 native_widget_->Show(); |
519 } | 513 } |
520 | |
521 if (CanActivate() && close_on_deactivate_) { | |
522 // Set mouse capture on timeout in case this is called from a | |
523 // mouse pressed handler. | |
524 MessageLoopForUI::current()->PostTask(FROM_HERE, base::Bind( | |
525 &Widget::SetMouseCapture, set_capture_factory_.GetWeakPtr(), | |
526 static_cast<View*>(NULL))); | |
527 } | |
528 } | 514 } |
529 | 515 |
530 void Widget::Hide() { | 516 void Widget::Hide() { |
531 native_widget_->Hide(); | 517 native_widget_->Hide(); |
532 } | 518 } |
533 | 519 |
534 void Widget::ShowInactive() { | 520 void Widget::ShowInactive() { |
535 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED, | 521 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED, |
536 // call SetBounds()with the restored bounds to set the correct size. This | 522 // call SetBounds()with the restored bounds to set the correct size. This |
537 // normally should not happen, but if it does we should avoid showing unsized | 523 // normally should not happen, but if it does we should avoid showing unsized |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 case ui::ET_MOUSE_PRESSED: | 1043 case ui::ET_MOUSE_PRESSED: |
1058 last_mouse_event_was_move_ = false; | 1044 last_mouse_event_was_move_ = false; |
1059 // Make sure we're still visible before we attempt capture as the mouse | 1045 // Make sure we're still visible before we attempt capture as the mouse |
1060 // press processing may have made the window hide (as happens with menus). | 1046 // press processing may have made the window hide (as happens with menus). |
1061 if (GetRootView()->OnMousePressed(event) && IsVisible()) { | 1047 if (GetRootView()->OnMousePressed(event) && IsVisible()) { |
1062 is_mouse_button_pressed_ = true; | 1048 is_mouse_button_pressed_ = true; |
1063 if (!native_widget_->HasCapture()) | 1049 if (!native_widget_->HasCapture()) |
1064 native_widget_->SetCapture(); | 1050 native_widget_->SetCapture(); |
1065 return true; | 1051 return true; |
1066 } | 1052 } |
1067 if (close_on_deactivate_ && !GetRootView()->HitTest(event.location())) | |
1068 Close(); | |
1069 return false; | 1053 return false; |
1070 case ui::ET_MOUSE_RELEASED: | 1054 case ui::ET_MOUSE_RELEASED: |
1071 last_mouse_event_was_move_ = false; | 1055 last_mouse_event_was_move_ = false; |
1072 is_mouse_button_pressed_ = false; | 1056 is_mouse_button_pressed_ = false; |
1073 // Release capture first, to avoid confusion if OnMouseReleased blocks. | 1057 // Release capture first, to avoid confusion if OnMouseReleased blocks. |
1074 if (native_widget_->HasCapture() && | 1058 if (native_widget_->HasCapture() && |
1075 ShouldReleaseCaptureOnMouseReleased()) { | 1059 ShouldReleaseCaptureOnMouseReleased()) { |
1076 native_widget_->ReleaseCapture(); | 1060 native_widget_->ReleaseCapture(); |
1077 } | 1061 } |
1078 GetRootView()->OnMouseReleased(event); | 1062 GetRootView()->OnMouseReleased(event); |
(...skipping 20 matching lines...) Expand all Loading... |
1099 default: | 1083 default: |
1100 return false; | 1084 return false; |
1101 } | 1085 } |
1102 return true; | 1086 return true; |
1103 } | 1087 } |
1104 | 1088 |
1105 void Widget::OnMouseCaptureLost() { | 1089 void Widget::OnMouseCaptureLost() { |
1106 if (is_mouse_button_pressed_) | 1090 if (is_mouse_button_pressed_) |
1107 GetRootView()->OnMouseCaptureLost(); | 1091 GetRootView()->OnMouseCaptureLost(); |
1108 is_mouse_button_pressed_ = false; | 1092 is_mouse_button_pressed_ = false; |
1109 | |
1110 // Without mouse capture, the widget doesn't process all events so can miss | |
1111 // the user clicking outside the root view's bounds. | |
1112 if (close_on_deactivate_) | |
1113 Close(); | |
1114 } | 1093 } |
1115 | 1094 |
1116 ui::TouchStatus Widget::OnTouchEvent(const TouchEvent& event) { | 1095 ui::TouchStatus Widget::OnTouchEvent(const TouchEvent& event) { |
1117 ScopedEvent scoped(this, event); | 1096 ScopedEvent scoped(this, event); |
1118 return GetRootView()->OnTouchEvent(event); | 1097 return GetRootView()->OnTouchEvent(event); |
1119 } | 1098 } |
1120 | 1099 |
1121 ui::GestureStatus Widget::OnGestureEvent(const GestureEvent& event) { | 1100 ui::GestureStatus Widget::OnGestureEvent(const GestureEvent& event) { |
1122 ScopedEvent scoped(this, event); | 1101 ScopedEvent scoped(this, event); |
1123 return GetRootView()->OnGestureEvent(event); | 1102 return GetRootView()->OnGestureEvent(event); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 non_client_view_ = NULL; | 1150 non_client_view_ = NULL; |
1172 root_view_.reset(); | 1151 root_view_.reset(); |
1173 // Input method has to be destroyed before focus manager. | 1152 // Input method has to be destroyed before focus manager. |
1174 input_method_.reset(); | 1153 input_method_.reset(); |
1175 } | 1154 } |
1176 | 1155 |
1177 //////////////////////////////////////////////////////////////////////////////// | 1156 //////////////////////////////////////////////////////////////////////////////// |
1178 // Widget, private: | 1157 // Widget, private: |
1179 | 1158 |
1180 bool Widget::ShouldReleaseCaptureOnMouseReleased() const { | 1159 bool Widget::ShouldReleaseCaptureOnMouseReleased() const { |
1181 return !close_on_deactivate_; | 1160 return true; |
1182 } | 1161 } |
1183 | 1162 |
1184 void Widget::SetInactiveRenderingDisabled(bool value) { | 1163 void Widget::SetInactiveRenderingDisabled(bool value) { |
1185 disable_inactive_rendering_ = value; | 1164 disable_inactive_rendering_ = value; |
1186 // We need to always notify the NonClientView so that it can trigger a paint. | 1165 // We need to always notify the NonClientView so that it can trigger a paint. |
1187 // TODO: what's really needed is a way to know when either the active state | 1166 // TODO: what's really needed is a way to know when either the active state |
1188 // changes or |disable_inactive_rendering_| changes. | 1167 // changes or |disable_inactive_rendering_| changes. |
1189 if (non_client_view_) | 1168 if (non_client_view_) |
1190 non_client_view_->SetInactiveRenderingDisabled(value); | 1169 non_client_view_->SetInactiveRenderingDisabled(value); |
1191 native_widget_->SetInactiveRenderingDisabled(value); | 1170 native_widget_->SetInactiveRenderingDisabled(value); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1282 | 1261 |
1283 //////////////////////////////////////////////////////////////////////////////// | 1262 //////////////////////////////////////////////////////////////////////////////// |
1284 // internal::NativeWidgetPrivate, NativeWidget implementation: | 1263 // internal::NativeWidgetPrivate, NativeWidget implementation: |
1285 | 1264 |
1286 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { | 1265 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() { |
1287 return this; | 1266 return this; |
1288 } | 1267 } |
1289 | 1268 |
1290 } // namespace internal | 1269 } // namespace internal |
1291 } // namespace views | 1270 } // namespace views |
OLD | NEW |