| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/views/window.h" | 5 #include "chrome/views/window.h" |
| 6 | 6 |
| 7 #include "base/win_util.h" | 7 #include "base/win_util.h" |
| 8 #include "chrome/app/chrome_dll_resource.h" | 8 #include "chrome/app/chrome_dll_resource.h" |
| 9 // TODO(beng): some day make this unfortunate dependency not exist. | 9 // TODO(beng): some day make this unfortunate dependency not exist. |
| 10 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 MONITORINFO mi; | 78 MONITORINFO mi; |
| 79 mi.cbSize = sizeof(mi); | 79 mi.cbSize = sizeof(mi); |
| 80 ::GetMonitorInfo(current_monitor, &mi); | 80 ::GetMonitorInfo(current_monitor, &mi); |
| 81 working_rect = mi.rcWork; | 81 working_rect = mi.rcWork; |
| 82 } | 82 } |
| 83 working_rect.Inset(kMonitorEdgePadding, kMonitorEdgePadding); | 83 working_rect.Inset(kMonitorEdgePadding, kMonitorEdgePadding); |
| 84 return working_rect.size(); | 84 return working_rect.size(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void Window::Show() { | 87 void Window::Show() { |
| 88 Show(SW_SHOW); | 88 int show_state = GetShowState(); |
| 89 bool maximized = false; |
| 90 if (window_delegate_->GetSavedMaximizedState(&maximized) && maximized) |
| 91 show_state = SW_SHOWMAXIMIZED; |
| 92 Show(show_state); |
| 89 } | 93 } |
| 90 | 94 |
| 91 void Window::Show(int show_style) { | 95 void Window::Show(int show_state) { |
| 92 ShowWindow(show_style); | 96 ShowWindow(show_state); |
| 93 SetInitialFocus(); | 97 SetInitialFocus(); |
| 94 } | 98 } |
| 95 | 99 |
| 100 int Window::GetShowState() const { |
| 101 return SW_SHOWNORMAL; |
| 102 } |
| 103 |
| 96 void Window::Activate() { | 104 void Window::Activate() { |
| 97 if (IsMinimized()) | 105 if (IsMinimized()) |
| 98 ::ShowWindow(GetHWND(), SW_RESTORE); | 106 ::ShowWindow(GetHWND(), SW_RESTORE); |
| 99 ::SetWindowPos(GetHWND(), HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); | 107 ::SetWindowPos(GetHWND(), HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); |
| 100 SetForegroundWindow(GetHWND()); | 108 SetForegroundWindow(GetHWND()); |
| 101 } | 109 } |
| 102 | 110 |
| 103 void Window::SetBounds(const gfx::Rect& bounds) { | 111 void Window::SetBounds(const gfx::Rect& bounds) { |
| 104 SetBounds(bounds, NULL); | 112 SetBounds(bounds, NULL); |
| 105 } | 113 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 DestroyIcon(old_icon); | 194 DestroyIcon(old_icon); |
| 187 } | 195 } |
| 188 } | 196 } |
| 189 | 197 |
| 190 void Window::ExecuteSystemMenuCommand(int command) { | 198 void Window::ExecuteSystemMenuCommand(int command) { |
| 191 if (command) | 199 if (command) |
| 192 SendMessage(GetHWND(), WM_SYSCOMMAND, command, 0); | 200 SendMessage(GetHWND(), WM_SYSCOMMAND, command, 0); |
| 193 } | 201 } |
| 194 | 202 |
| 195 // static | 203 // static |
| 196 bool Window::SaveWindowPositionToPrefService(PrefService* pref_service, | |
| 197 const std::wstring& entry, | |
| 198 const CRect& bounds, | |
| 199 bool maximized, | |
| 200 bool always_on_top) { | |
| 201 DCHECK(pref_service); | |
| 202 DictionaryValue* win_pref = pref_service->GetMutableDictionary(entry.c_str()); | |
| 203 DCHECK(win_pref); | |
| 204 | |
| 205 win_pref->SetInteger(L"left", bounds.left); | |
| 206 win_pref->SetInteger(L"top", bounds.top); | |
| 207 win_pref->SetInteger(L"right", bounds.right); | |
| 208 win_pref->SetInteger(L"bottom", bounds.bottom); | |
| 209 win_pref->SetBoolean(L"maximized", maximized); | |
| 210 win_pref->SetBoolean(L"always_on_top", always_on_top); | |
| 211 return true; | |
| 212 } | |
| 213 | |
| 214 // static | |
| 215 bool Window::RestoreWindowPositionFromPrefService(PrefService* pref_service, | |
| 216 const std::wstring& entry, | |
| 217 CRect* bounds, | |
| 218 bool* maximized, | |
| 219 bool* always_on_top) { | |
| 220 DCHECK(pref_service); | |
| 221 DCHECK(bounds); | |
| 222 DCHECK(maximized); | |
| 223 DCHECK(always_on_top); | |
| 224 | |
| 225 const DictionaryValue* dictionary = pref_service->GetDictionary(entry.c_str())
; | |
| 226 if (!dictionary) | |
| 227 return false; | |
| 228 | |
| 229 int left, top, right, bottom; | |
| 230 bool temp_maximized, temp_always_on_top; | |
| 231 if (!dictionary || !dictionary->GetInteger(L"left", &left) || | |
| 232 !dictionary->GetInteger(L"top", &top) || | |
| 233 !dictionary->GetInteger(L"right", &right) || | |
| 234 !dictionary->GetInteger(L"bottom", &bottom) || | |
| 235 !dictionary->GetBoolean(L"maximized", &temp_maximized) || | |
| 236 !dictionary->GetBoolean(L"always_on_top", &temp_always_on_top)) | |
| 237 return false; | |
| 238 | |
| 239 bounds->SetRect(left, top, right, bottom); | |
| 240 *maximized = temp_maximized; | |
| 241 *always_on_top = temp_always_on_top; | |
| 242 return true; | |
| 243 } | |
| 244 | |
| 245 // static | |
| 246 gfx::Size Window::GetLocalizedContentsSize(int col_resource_id, | 204 gfx::Size Window::GetLocalizedContentsSize(int col_resource_id, |
| 247 int row_resource_id) { | 205 int row_resource_id) { |
| 248 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 206 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 249 ChromeFont font = rb.GetFont(ResourceBundle::BaseFont); | 207 ChromeFont font = rb.GetFont(ResourceBundle::BaseFont); |
| 250 | 208 |
| 251 double chars = _wtof(l10n_util::GetString(col_resource_id).c_str()); | 209 double chars = _wtof(l10n_util::GetString(col_resource_id).c_str()); |
| 252 double lines = _wtof(l10n_util::GetString(row_resource_id).c_str()); | 210 double lines = _wtof(l10n_util::GetString(row_resource_id).c_str()); |
| 253 | 211 |
| 254 int width = font.GetExpectedTextWidth(static_cast<int>(chars)); | 212 int width = font.GetExpectedTextWidth(static_cast<int>(chars)); |
| 255 int height = static_cast<int>(font.height() * lines); | 213 int height = static_cast<int>(font.height() * lines); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 win_util::SetWindowUserData(GetHWND(), this); | 263 win_util::SetWindowUserData(GetHWND(), this); |
| 306 | 264 |
| 307 std::wstring window_title = window_delegate_->GetWindowTitle(); | 265 std::wstring window_title = window_delegate_->GetWindowTitle(); |
| 308 std::wstring localized_text; | 266 std::wstring localized_text; |
| 309 if (l10n_util::AdjustStringForLocaleDirection(window_title, &localized_text)) | 267 if (l10n_util::AdjustStringForLocaleDirection(window_title, &localized_text)) |
| 310 window_title.assign(localized_text); | 268 window_title.assign(localized_text); |
| 311 SetWindowText(GetHWND(), window_title.c_str()); | 269 SetWindowText(GetHWND(), window_title.c_str()); |
| 312 | 270 |
| 313 SetClientView(window_delegate_->CreateClientView(this)); | 271 SetClientView(window_delegate_->CreateClientView(this)); |
| 314 SetInitialBounds(bounds); | 272 SetInitialBounds(bounds); |
| 315 | 273 InitAlwaysOnTopState(); |
| 316 if (window_delegate_->HasAlwaysOnTopMenu()) | |
| 317 AddAlwaysOnTopSystemMenuItem(); | |
| 318 } | 274 } |
| 319 | 275 |
| 320 void Window::SetClientView(ClientView* client_view) { | 276 void Window::SetClientView(ClientView* client_view) { |
| 321 DCHECK(client_view && !client_view_ && GetHWND()); | 277 DCHECK(client_view && !client_view_ && GetHWND()); |
| 322 client_view_ = client_view; | 278 client_view_ = client_view; |
| 323 if (non_client_view_) { | 279 if (non_client_view_) { |
| 324 // This will trigger the ClientView to be added by the non-client view. | 280 // This will trigger the ClientView to be added by the non-client view. |
| 325 ContainerWin::SetContentsView(non_client_view_); | 281 ContainerWin::SetContentsView(non_client_view_); |
| 326 } else { | 282 } else { |
| 327 ContainerWin::SetContentsView(client_view_); | 283 ContainerWin::SetContentsView(client_view_); |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 v->RequestFocus(); | 462 v->RequestFocus(); |
| 507 } else { | 463 } else { |
| 508 // The window does not get keyboard messages unless we focus it, not sure | 464 // The window does not get keyboard messages unless we focus it, not sure |
| 509 // why. | 465 // why. |
| 510 SetFocus(GetHWND()); | 466 SetFocus(GetHWND()); |
| 511 } | 467 } |
| 512 } | 468 } |
| 513 | 469 |
| 514 void Window::SetInitialBounds(const gfx::Rect& create_bounds) { | 470 void Window::SetInitialBounds(const gfx::Rect& create_bounds) { |
| 515 // Restore the window's placement from the controller. | 471 // Restore the window's placement from the controller. |
| 516 CRect saved_bounds(create_bounds.ToRECT()); | 472 gfx::Rect saved_bounds(create_bounds.ToRECT()); |
| 517 bool maximized = false; | 473 if (window_delegate_->GetSavedWindowBounds(&saved_bounds)) { |
| 518 if (window_delegate_->RestoreWindowPosition(&saved_bounds, | |
| 519 &maximized, | |
| 520 &is_always_on_top_)) { | |
| 521 // Make sure the bounds are at least the minimum size. | 474 // Make sure the bounds are at least the minimum size. |
| 522 if (saved_bounds.Width() < minimum_size_.cx) { | 475 if (saved_bounds.width() < minimum_size_.cx) { |
| 523 saved_bounds.SetRect(saved_bounds.left, saved_bounds.top, | 476 saved_bounds.SetRect(saved_bounds.x(), saved_bounds.y(), |
| 524 saved_bounds.right + minimum_size_.cx - | 477 saved_bounds.right() + minimum_size_.cx - |
| 525 saved_bounds.Width(), | 478 saved_bounds.width(), |
| 526 saved_bounds.bottom); | 479 saved_bounds.bottom()); |
| 527 } | 480 } |
| 528 | 481 |
| 529 if (saved_bounds.Height() < minimum_size_.cy) { | 482 if (saved_bounds.height() < minimum_size_.cy) { |
| 530 saved_bounds.SetRect(saved_bounds.left, saved_bounds.top, | 483 saved_bounds.SetRect(saved_bounds.x(), saved_bounds.y(), |
| 531 saved_bounds.right, | 484 saved_bounds.right(), |
| 532 saved_bounds.bottom + minimum_size_.cy - | 485 saved_bounds.bottom() + minimum_size_.cy - |
| 533 saved_bounds.Height()); | 486 saved_bounds.height()); |
| 534 } | 487 } |
| 535 | 488 |
| 536 WINDOWPLACEMENT placement = {0}; | 489 // "Show state" (maximized, minimized, etc) is handled by Show(). |
| 537 placement.length = sizeof(WINDOWPLACEMENT); | 490 SetBounds(saved_bounds, NULL); |
| 538 placement.rcNormalPosition = saved_bounds; | |
| 539 if (maximized) | |
| 540 placement.showCmd = SW_SHOWMAXIMIZED; | |
| 541 ::SetWindowPlacement(GetHWND(), &placement); | |
| 542 | |
| 543 if (is_always_on_top_ != window_delegate_->IsAlwaysOnTop()) | |
| 544 AlwaysOnTopChanged(); | |
| 545 } else { | 491 } else { |
| 546 if (create_bounds.IsEmpty()) { | 492 if (create_bounds.IsEmpty()) { |
| 547 // No initial bounds supplied, so size the window to its content and | 493 // No initial bounds supplied, so size the window to its content and |
| 548 // center over its parent. | 494 // center over its parent. |
| 549 SizeWindowToDefault(); | 495 SizeWindowToDefault(); |
| 550 } else { | 496 } else { |
| 551 // Use the supplied initial bounds. | 497 // Use the supplied initial bounds. |
| 552 SetBounds(create_bounds); | 498 SetBounds(create_bounds); |
| 553 } | 499 } |
| 554 } | 500 } |
| 555 } | 501 } |
| 556 | 502 |
| 503 void Window::InitAlwaysOnTopState() { |
| 504 is_always_on_top_ = false; |
| 505 if (window_delegate_->GetSavedAlwaysOnTopState(&is_always_on_top_) && |
| 506 is_always_on_top_ != window_delegate_->IsAlwaysOnTop()) { |
| 507 AlwaysOnTopChanged(); |
| 508 } |
| 509 |
| 510 if (window_delegate_->HasAlwaysOnTopMenu()) |
| 511 AddAlwaysOnTopSystemMenuItem(); |
| 512 } |
| 513 |
| 557 void Window::AddAlwaysOnTopSystemMenuItem() { | 514 void Window::AddAlwaysOnTopSystemMenuItem() { |
| 558 // The Win32 API requires that we own the text. | 515 // The Win32 API requires that we own the text. |
| 559 always_on_top_menu_text_ = l10n_util::GetString(IDS_ALWAYS_ON_TOP); | 516 always_on_top_menu_text_ = l10n_util::GetString(IDS_ALWAYS_ON_TOP); |
| 560 | 517 |
| 561 // Let's insert a menu to the window. | 518 // Let's insert a menu to the window. |
| 562 HMENU system_menu = ::GetSystemMenu(GetHWND(), FALSE); | 519 HMENU system_menu = ::GetSystemMenu(GetHWND(), FALSE); |
| 563 int index = ::GetMenuItemCount(system_menu) - 1; | 520 int index = ::GetMenuItemCount(system_menu) - 1; |
| 564 if (index < 0) { | 521 if (index < 0) { |
| 565 // Paranoia check. | 522 // Paranoia check. |
| 566 NOTREACHED(); | 523 NOTREACHED(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 | 593 |
| 637 void Window::SaveWindowPosition() { | 594 void Window::SaveWindowPosition() { |
| 638 WINDOWPLACEMENT win_placement = { 0 }; | 595 WINDOWPLACEMENT win_placement = { 0 }; |
| 639 win_placement.length = sizeof(WINDOWPLACEMENT); | 596 win_placement.length = sizeof(WINDOWPLACEMENT); |
| 640 | 597 |
| 641 BOOL r = GetWindowPlacement(GetHWND(), &win_placement); | 598 BOOL r = GetWindowPlacement(GetHWND(), &win_placement); |
| 642 DCHECK(r); | 599 DCHECK(r); |
| 643 | 600 |
| 644 bool maximized = (win_placement.showCmd == SW_SHOWMAXIMIZED); | 601 bool maximized = (win_placement.showCmd == SW_SHOWMAXIMIZED); |
| 645 CRect window_bounds(win_placement.rcNormalPosition); | 602 CRect window_bounds(win_placement.rcNormalPosition); |
| 646 window_delegate_->SaveWindowPosition(window_bounds, | 603 window_delegate_->SaveWindowPlacement( |
| 647 maximized, | 604 gfx::Rect(win_placement.rcNormalPosition), maximized, is_always_on_top_); |
| 648 is_always_on_top_); | |
| 649 } | 605 } |
| 650 | 606 |
| 651 void Window::InitClass() { | 607 void Window::InitClass() { |
| 652 static bool initialized = false; | 608 static bool initialized = false; |
| 653 if (!initialized) { | 609 if (!initialized) { |
| 654 nwse_cursor_ = LoadCursor(NULL, IDC_SIZENWSE); | 610 nwse_cursor_ = LoadCursor(NULL, IDC_SIZENWSE); |
| 655 initialized = true; | 611 initialized = true; |
| 656 } | 612 } |
| 657 } | 613 } |
| 658 | 614 |
| 659 } // namespace views | 615 } // namespace views |
| 660 | 616 |
| OLD | NEW |