| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/window/window.h" | 5 #include "views/window/window.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "ui/base/l10n/l10n_font_util.h" | 9 #include "ui/base/l10n/l10n_font_util.h" |
| 11 #include "ui/base/resource/resource_bundle.h" | 10 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/gfx/font.h" | 11 #include "ui/gfx/font.h" |
| 13 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
| 14 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
| 15 #include "views/widget/widget.h" | 14 #include "views/widget/widget.h" |
| 16 #include "views/widget/native_widget.h" | 15 #include "views/widget/native_widget.h" |
| 17 #include "views/window/native_window.h" | 16 #include "views/window/native_window.h" |
| 18 #include "views/window/window_delegate.h" | 17 #include "views/window/window_delegate.h" |
| 19 | 18 |
| 20 namespace views { | 19 namespace views { |
| 21 | 20 |
| 22 //////////////////////////////////////////////////////////////////////////////// | 21 //////////////////////////////////////////////////////////////////////////////// |
| 23 // Window, public: | 22 // Window, public: |
| 24 | 23 |
| 25 Window::InitParams::InitParams(WindowDelegate* window_delegate) | 24 Window::InitParams::InitParams(WindowDelegate* window_delegate) |
| 26 : window_delegate(window_delegate), | 25 : window_delegate(window_delegate), |
| 27 parent_window(NULL), | 26 parent_window(NULL), |
| 28 native_window(NULL), | 27 native_window(NULL), |
| 29 widget_init_params(Widget::InitParams::TYPE_WINDOW) { | 28 widget_init_params(Widget::InitParams::TYPE_WINDOW) { |
| 30 } | 29 } |
| 31 | 30 |
| 32 Window::Window() | 31 Window::Window() |
| 33 : native_window_(NULL), | 32 : native_window_(NULL), |
| 34 saved_maximized_state_(false), | 33 saved_maximized_state_(false), |
| 35 minimum_size_(100, 100), | 34 minimum_size_(100, 100) { |
| 36 disable_inactive_rendering_(false), | |
| 37 window_closed_(false) { | |
| 38 } | 35 } |
| 39 | 36 |
| 40 Window::~Window() { | 37 Window::~Window() { |
| 41 } | 38 } |
| 42 | 39 |
| 43 // static | 40 // static |
| 44 Window* Window::CreateChromeWindow(gfx::NativeWindow parent, | 41 Window* Window::CreateChromeWindow(gfx::NativeWindow parent, |
| 45 const gfx::Rect& bounds, | 42 const gfx::Rect& bounds, |
| 46 WindowDelegate* window_delegate) { | 43 WindowDelegate* window_delegate) { |
| 47 Window* window = new Window; | 44 Window* window = new Window; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 InitParams modified_params = params; | 78 InitParams modified_params = params; |
| 82 modified_params.widget_init_params.delegate = params.window_delegate; | 79 modified_params.widget_init_params.delegate = params.window_delegate; |
| 83 DCHECK(!modified_params.widget_init_params.delegate->window_); | 80 DCHECK(!modified_params.widget_init_params.delegate->window_); |
| 84 modified_params.widget_init_params.delegate->window_ = this; | 81 modified_params.widget_init_params.delegate->window_ = this; |
| 85 modified_params.widget_init_params.native_widget = | 82 modified_params.widget_init_params.native_widget = |
| 86 native_window_->AsNativeWidget(); | 83 native_window_->AsNativeWidget(); |
| 87 Init(modified_params.widget_init_params); | 84 Init(modified_params.widget_init_params); |
| 88 OnNativeWindowCreated(modified_params.widget_init_params.bounds); | 85 OnNativeWindowCreated(modified_params.widget_init_params.bounds); |
| 89 } | 86 } |
| 90 | 87 |
| 91 gfx::Rect Window::GetBounds() const { | |
| 92 return GetWindowScreenBounds(); | |
| 93 } | |
| 94 | |
| 95 gfx::Rect Window::GetNormalBounds() const { | |
| 96 return native_window_->GetRestoredBounds(); | |
| 97 } | |
| 98 | |
| 99 void Window::ShowInactive() { | |
| 100 native_window_->ShowNativeWindow(NativeWindow::SHOW_INACTIVE); | |
| 101 } | |
| 102 | |
| 103 void Window::DisableInactiveRendering() { | |
| 104 disable_inactive_rendering_ = true; | |
| 105 non_client_view()->DisableInactiveRendering(disable_inactive_rendering_); | |
| 106 } | |
| 107 | |
| 108 void Window::EnableClose(bool enable) { | |
| 109 non_client_view()->EnableClose(enable); | |
| 110 native_window_->EnableClose(enable); | |
| 111 } | |
| 112 | |
| 113 void Window::UpdateWindowTitle() { | |
| 114 // If the non-client view is rendering its own title, it'll need to relayout | |
| 115 // now. | |
| 116 non_client_view()->Layout(); | |
| 117 | |
| 118 // Update the native frame's text. We do this regardless of whether or not | |
| 119 // the native frame is being used, since this also updates the taskbar, etc. | |
| 120 string16 window_title; | |
| 121 if (native_window_->AsNativeWidget()->IsScreenReaderActive()) { | |
| 122 window_title = WideToUTF16(widget_delegate()->GetAccessibleWindowTitle()); | |
| 123 } else { | |
| 124 window_title = WideToUTF16(widget_delegate()->GetWindowTitle()); | |
| 125 } | |
| 126 base::i18n::AdjustStringForLocaleDirection(&window_title); | |
| 127 native_window_->AsNativeWidget()->SetWindowTitle(UTF16ToWide(window_title)); | |
| 128 } | |
| 129 | |
| 130 void Window::UpdateWindowIcon() { | |
| 131 non_client_view()->UpdateWindowIcon(); | |
| 132 native_window_->AsNativeWidget()->SetWindowIcons( | |
| 133 widget_delegate()->GetWindowIcon(), | |
| 134 widget_delegate()->GetWindowAppIcon()); | |
| 135 } | |
| 136 | |
| 137 //////////////////////////////////////////////////////////////////////////////// | 88 //////////////////////////////////////////////////////////////////////////////// |
| 138 // Window, Widget overrides: | 89 // Window, Widget overrides: |
| 139 | 90 |
| 140 void Window::Show() { | 91 void Window::Show() { |
| 141 native_window_->ShowNativeWindow( | 92 native_window_->AsNativeWidget()->ShowNativeWidget( |
| 142 saved_maximized_state_ ? NativeWindow::SHOW_MAXIMIZED | 93 saved_maximized_state_ ? NativeWidget::SHOW_MAXIMIZED |
| 143 : NativeWindow::SHOW_RESTORED); | 94 : NativeWidget::SHOW_RESTORED); |
| 144 // |saved_maximized_state_| only applies the first time the window is shown. | 95 // |saved_maximized_state_| only applies the first time the window is shown. |
| 145 // If we don't reset the value the window will be shown maximized every time | 96 // If we don't reset the value the window will be shown maximized every time |
| 146 // it is subsequently shown after being hidden. | 97 // it is subsequently shown after being hidden. |
| 147 saved_maximized_state_ = false; | 98 saved_maximized_state_ = false; |
| 148 } | 99 } |
| 149 | 100 |
| 150 void Window::Close() { | 101 Window* Window::AsWindow() { |
| 151 if (window_closed_) { | 102 return this; |
| 152 // It appears we can hit this code path if you close a modal dialog then | 103 } |
| 153 // close the last browser before the destructor is hit, which triggers | |
| 154 // invoking Close again. | |
| 155 return; | |
| 156 } | |
| 157 | 104 |
| 158 if (non_client_view()->CanClose()) { | 105 const Window* Window::AsWindow() const { |
| 159 SaveWindowPosition(); | 106 return this; |
| 160 Widget::Close(); | |
| 161 window_closed_ = true; | |
| 162 } | |
| 163 } | 107 } |
| 164 | 108 |
| 165 //////////////////////////////////////////////////////////////////////////////// | 109 //////////////////////////////////////////////////////////////////////////////// |
| 166 // Window, internal::NativeWindowDelegate implementation: | 110 // Window, internal::NativeWindowDelegate implementation: |
| 167 | 111 |
| 168 bool Window::CanActivate() const { | |
| 169 return widget_delegate()->CanActivate(); | |
| 170 } | |
| 171 | |
| 172 bool Window::IsInactiveRenderingDisabled() const { | |
| 173 return disable_inactive_rendering_; | |
| 174 } | |
| 175 | |
| 176 void Window::EnableInactiveRendering() { | |
| 177 disable_inactive_rendering_ = false; | |
| 178 non_client_view()->DisableInactiveRendering(false); | |
| 179 } | |
| 180 | |
| 181 bool Window::IsModal() const { | 112 bool Window::IsModal() const { |
| 182 return widget_delegate()->IsModal(); | 113 return widget_delegate()->IsModal(); |
| 183 } | 114 } |
| 184 | 115 |
| 185 bool Window::IsDialogBox() const { | 116 bool Window::IsDialogBox() const { |
| 186 return !!widget_delegate()->AsDialogDelegate(); | 117 return !!widget_delegate()->AsDialogDelegate(); |
| 187 } | 118 } |
| 188 | 119 |
| 189 gfx::Size Window::GetMinimumSize() { | |
| 190 return non_client_view()->GetMinimumSize(); | |
| 191 } | |
| 192 | |
| 193 int Window::GetNonClientComponent(const gfx::Point& point) { | |
| 194 return non_client_view()->NonClientHitTest(point); | |
| 195 } | |
| 196 | |
| 197 bool Window::ExecuteCommand(int command_id) { | |
| 198 return widget_delegate()->ExecuteWindowsCommand(command_id); | |
| 199 } | |
| 200 | |
| 201 void Window::OnNativeWindowCreated(const gfx::Rect& bounds) { | 120 void Window::OnNativeWindowCreated(const gfx::Rect& bounds) { |
| 202 if (widget_delegate()->IsModal()) | 121 if (widget_delegate()->IsModal()) |
| 203 native_window_->BecomeModal(); | 122 native_window_->BecomeModal(); |
| 204 | 123 |
| 205 // Create the ClientView, add it to the NonClientView and add the | |
| 206 // NonClientView to the RootView. This will cause everything to be parented. | |
| 207 non_client_view()->set_client_view( | |
| 208 widget_delegate()->CreateClientView(this)); | |
| 209 SetContentsView(non_client_view()); | |
| 210 | |
| 211 UpdateWindowTitle(); | 124 UpdateWindowTitle(); |
| 212 native_window_->AsNativeWidget()->SetAccessibleRole( | |
| 213 widget_delegate()->GetAccessibleWindowRole()); | |
| 214 native_window_->AsNativeWidget()->SetAccessibleState( | |
| 215 widget_delegate()->GetAccessibleWindowState()); | |
| 216 | |
| 217 SetInitialBounds(bounds); | 125 SetInitialBounds(bounds); |
| 218 } | 126 } |
| 219 | 127 |
| 220 void Window::OnNativeWindowActivationChanged(bool active) { | |
| 221 if (!active) | |
| 222 SaveWindowPosition(); | |
| 223 widget_delegate()->OnWindowActivationChanged(active); | |
| 224 } | |
| 225 | |
| 226 void Window::OnNativeWindowBeginUserBoundsChange() { | |
| 227 widget_delegate()->OnWindowBeginUserBoundsChange(); | |
| 228 } | |
| 229 | |
| 230 void Window::OnNativeWindowEndUserBoundsChange() { | |
| 231 widget_delegate()->OnWindowEndUserBoundsChange(); | |
| 232 } | |
| 233 | |
| 234 void Window::OnNativeWindowDestroying() { | |
| 235 non_client_view()->WindowClosing(); | |
| 236 widget_delegate()->WindowClosing(); | |
| 237 } | |
| 238 | |
| 239 void Window::OnNativeWindowBoundsChanged() { | |
| 240 SaveWindowPosition(); | |
| 241 } | |
| 242 | |
| 243 Window* Window::AsWindow() { | |
| 244 return this; | |
| 245 } | |
| 246 | |
| 247 internal::NativeWidgetDelegate* Window::AsNativeWidgetDelegate() { | 128 internal::NativeWidgetDelegate* Window::AsNativeWidgetDelegate() { |
| 248 return this; | 129 return this; |
| 249 } | 130 } |
| 250 | 131 |
| 251 //////////////////////////////////////////////////////////////////////////////// | 132 //////////////////////////////////////////////////////////////////////////////// |
| 252 // Window, private: | 133 // Window, private: |
| 253 | 134 |
| 254 void Window::SetInitialBounds(const gfx::Rect& bounds) { | 135 void Window::SetInitialBounds(const gfx::Rect& bounds) { |
| 255 // First we obtain the window's saved show-style and store it. We need to do | 136 // First we obtain the window's saved show-style and store it. We need to do |
| 256 // this here, rather than in Show() because by the time Show() is called, | 137 // this here, rather than in Show() because by the time Show() is called, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 // center over its parent. | 173 // center over its parent. |
| 293 native_window_->AsNativeWidget()->CenterWindow( | 174 native_window_->AsNativeWidget()->CenterWindow( |
| 294 non_client_view()->GetPreferredSize()); | 175 non_client_view()->GetPreferredSize()); |
| 295 } else { | 176 } else { |
| 296 // Use the supplied initial bounds. | 177 // Use the supplied initial bounds. |
| 297 SetBoundsConstrained(bounds, NULL); | 178 SetBoundsConstrained(bounds, NULL); |
| 298 } | 179 } |
| 299 } | 180 } |
| 300 } | 181 } |
| 301 | 182 |
| 302 void Window::SaveWindowPosition() { | |
| 303 // The window delegate does the actual saving for us. It seems like (judging | |
| 304 // by go/crash) that in some circumstances we can end up here after | |
| 305 // WM_DESTROY, at which point the window delegate is likely gone. So just | |
| 306 // bail. | |
| 307 if (!widget_delegate()) | |
| 308 return; | |
| 309 | |
| 310 bool maximized; | |
| 311 gfx::Rect bounds; | |
| 312 native_window_->AsNativeWidget()->GetWindowBoundsAndMaximizedState( | |
| 313 &bounds, | |
| 314 &maximized); | |
| 315 widget_delegate()->SaveWindowPlacement(bounds, maximized); | |
| 316 } | |
| 317 | |
| 318 } // namespace views | 183 } // namespace views |
| OLD | NEW |