| 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/browser/window_sizer.h" | 5 #include "chrome/browser/window_sizer.h" |
| 6 | 6 |
| 7 #include <atlbase.h> | 7 #include <atlbase.h> |
| 8 #include <atlapp.h> | 8 #include <atlapp.h> |
| 9 #include <atlmisc.h> | 9 #include <atlmisc.h> |
| 10 | 10 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 } | 81 } |
| 82 | 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); | 83 DISALLOW_COPY_AND_ASSIGN(DefaultMonitorInfoProvider); |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 /////////////////////////////////////////////////////////////////////////////// | 86 /////////////////////////////////////////////////////////////////////////////// |
| 87 // An implementation of WindowSizer::StateProvider that gets the last active | 87 // An implementation of WindowSizer::StateProvider that gets the last active |
| 88 // and persistent state from the browser window and the user's profile. | 88 // and persistent state from the browser window and the user's profile. |
| 89 class DefaultStateProvider : public WindowSizer::StateProvider { | 89 class DefaultStateProvider : public WindowSizer::StateProvider { |
| 90 public: | 90 public: |
| 91 explicit DefaultStateProvider(const std::wstring& app_name) | 91 explicit DefaultStateProvider(const std::wstring& app_name, Browser* browser) |
| 92 : app_name_(app_name) { | 92 : app_name_(app_name), |
| 93 browser_(browser) { |
| 93 } | 94 } |
| 94 | 95 |
| 95 // Overridden from WindowSizer::StateProvider: | 96 // Overridden from WindowSizer::StateProvider: |
| 96 virtual bool GetPersistentState(gfx::Rect* bounds, bool* maximized) const { | 97 virtual bool GetPersistentState(gfx::Rect* bounds, bool* maximized) const { |
| 97 DCHECK(bounds && maximized); | 98 DCHECK(bounds && maximized); |
| 98 | 99 |
| 99 std::wstring key(prefs::kBrowserWindowPlacement); | 100 std::wstring key(prefs::kBrowserWindowPlacement); |
| 100 if (!app_name_.empty()) { | 101 if (!app_name_.empty()) { |
| 101 key.append(L"_"); | 102 key.append(L"_"); |
| 102 key.append(app_name_); | 103 key.append(app_name_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 118 bounds->SetRect(left, top, std::max(0, right - left), | 119 bounds->SetRect(left, top, std::max(0, right - left), |
| 119 std::max(0, bottom - top)); | 120 std::max(0, bottom - top)); |
| 120 return has_prefs; | 121 return has_prefs; |
| 121 } | 122 } |
| 122 | 123 |
| 123 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { | 124 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { |
| 124 // Applications are always restored with the same position. | 125 // Applications are always restored with the same position. |
| 125 if (!app_name_.empty()) | 126 if (!app_name_.empty()) |
| 126 return false; | 127 return false; |
| 127 | 128 |
| 128 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); | 129 // If a reference browser is set, use its window. Otherwise find last |
| 129 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); | 130 // active. |
| 130 for (; it != end; ++it) { | 131 BrowserWindow* window = NULL; |
| 131 Browser* last_active = *it; | 132 if (browser_) { |
| 132 if (last_active && last_active->type() == Browser::TYPE_NORMAL) { | 133 window = browser_->window(); |
| 133 BrowserWindow* window = last_active->window(); | 134 DCHECK(window); |
| 134 DCHECK(window); | 135 } else { |
| 135 *bounds = window->GetNormalBounds(); | 136 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); |
| 136 return true; | 137 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); |
| 138 for (; (it != end); ++it) { |
| 139 Browser* last_active = *it; |
| 140 if (last_active && last_active->type() == Browser::TYPE_NORMAL) { |
| 141 window = last_active->window(); |
| 142 DCHECK(window); |
| 143 break; |
| 144 } |
| 137 } | 145 } |
| 138 } | 146 } |
| 139 | 147 |
| 148 if (window) { |
| 149 *bounds = window->GetNormalBounds(); |
| 150 return true; |
| 151 } |
| 152 |
| 140 return false; | 153 return false; |
| 141 } | 154 } |
| 142 | 155 |
| 143 private: | 156 private: |
| 144 std::wstring app_name_; | 157 std::wstring app_name_; |
| 145 | 158 |
| 159 // If set, is used as the reference browser for GetLastActiveWindowState. |
| 160 Browser* browser_; |
| 146 DISALLOW_EVIL_CONSTRUCTORS(DefaultStateProvider); | 161 DISALLOW_EVIL_CONSTRUCTORS(DefaultStateProvider); |
| 147 }; | 162 }; |
| 148 | 163 |
| 149 /////////////////////////////////////////////////////////////////////////////// | 164 /////////////////////////////////////////////////////////////////////////////// |
| 150 // WindowSizer, public: | 165 // WindowSizer, public: |
| 151 | 166 |
| 152 WindowSizer::WindowSizer( | 167 WindowSizer::WindowSizer( |
| 153 StateProvider* state_provider, | 168 StateProvider* state_provider, |
| 154 MonitorInfoProvider* monitor_info_provider) { | 169 MonitorInfoProvider* monitor_info_provider) { |
| 155 Init(state_provider, monitor_info_provider); | 170 Init(state_provider, monitor_info_provider); |
| 156 } | 171 } |
| 157 | 172 |
| 158 WindowSizer::~WindowSizer() { | 173 WindowSizer::~WindowSizer() { |
| 159 if (state_provider_) | 174 if (state_provider_) |
| 160 delete state_provider_; | 175 delete state_provider_; |
| 161 if (monitor_info_provider_) | 176 if (monitor_info_provider_) |
| 162 delete monitor_info_provider_; | 177 delete monitor_info_provider_; |
| 163 } | 178 } |
| 164 | 179 |
| 165 // static | 180 // static |
| 166 void WindowSizer::GetBrowserWindowBounds(const std::wstring& app_name, | 181 void WindowSizer::GetBrowserWindowBounds(const std::wstring& app_name, |
| 167 const gfx::Rect& specified_bounds, | 182 const gfx::Rect& specified_bounds, |
| 183 Browser* browser, |
| 168 gfx::Rect* window_bounds, | 184 gfx::Rect* window_bounds, |
| 169 bool* maximized) { | 185 bool* maximized) { |
| 170 const WindowSizer sizer(new DefaultStateProvider(app_name), | 186 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), |
| 171 new DefaultMonitorInfoProvider); | 187 new DefaultMonitorInfoProvider); |
| 172 sizer.DetermineWindowBounds(specified_bounds, window_bounds, maximized); | 188 sizer.DetermineWindowBounds(specified_bounds, window_bounds, maximized); |
| 173 } | 189 } |
| 174 | 190 |
| 175 gfx::Point WindowSizer::GetDefaultPopupOrigin(const gfx::Size& size) { | 191 gfx::Point WindowSizer::GetDefaultPopupOrigin(const gfx::Size& size) { |
| 176 RECT area; | 192 RECT area; |
| 177 SystemParametersInfo(SPI_GETWORKAREA, 0, &area, 0); | 193 SystemParametersInfo(SPI_GETWORKAREA, 0, &area, 0); |
| 178 gfx::Point corner(area.left, area.top); | 194 gfx::Point corner(area.left, area.top); |
| 179 | 195 |
| 180 if (Browser* b = BrowserList::GetLastActive()) { | 196 if (Browser* b = BrowserList::GetLastActive()) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 194 ); | 210 ); |
| 195 } | 211 } |
| 196 } | 212 } |
| 197 return corner; | 213 return corner; |
| 198 } | 214 } |
| 199 | 215 |
| 200 /////////////////////////////////////////////////////////////////////////////// | 216 /////////////////////////////////////////////////////////////////////////////// |
| 201 // WindowSizer, private: | 217 // WindowSizer, private: |
| 202 | 218 |
| 203 WindowSizer::WindowSizer(const std::wstring& app_name) { | 219 WindowSizer::WindowSizer(const std::wstring& app_name) { |
| 204 Init(new DefaultStateProvider(app_name), | 220 Init(new DefaultStateProvider(app_name, NULL), |
| 205 new DefaultMonitorInfoProvider); | 221 new DefaultMonitorInfoProvider); |
| 206 } | 222 } |
| 207 | 223 |
| 208 void WindowSizer::Init(StateProvider* state_provider, | 224 void WindowSizer::Init(StateProvider* state_provider, |
| 209 MonitorInfoProvider* monitor_info_provider) { | 225 MonitorInfoProvider* monitor_info_provider) { |
| 210 state_provider_ = state_provider; | 226 state_provider_ = state_provider; |
| 211 monitor_info_provider_ = monitor_info_provider; | 227 monitor_info_provider_ = monitor_info_provider; |
| 212 } | 228 } |
| 213 | 229 |
| 214 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, | 230 void WindowSizer::DetermineWindowBounds(const gfx::Rect& specified_bounds, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 // Now that we've tried to correct the x/y position to something reasonable, | 369 // Now that we've tried to correct the x/y position to something reasonable, |
| 354 // see if the window is still too tall or wide to fit, and resize it if need | 370 // see if the window is still too tall or wide to fit, and resize it if need |
| 355 // be. | 371 // be. |
| 356 if ((bottom_offscreen || top_offscreen) && | 372 if ((bottom_offscreen || top_offscreen) && |
| 357 bounds->bottom() > work_area.bottom()) | 373 bounds->bottom() > work_area.bottom()) |
| 358 bounds->set_height(work_area.height() - 2 * kWindowTilePixels); | 374 bounds->set_height(work_area.height() - 2 * kWindowTilePixels); |
| 359 if ((left_offscreen || right_offscreen) && | 375 if ((left_offscreen || right_offscreen) && |
| 360 bounds->right() > work_area.right()) | 376 bounds->right() > work_area.right()) |
| 361 bounds->set_width(work_area.width() - 2 * kWindowTilePixels); | 377 bounds->set_width(work_area.width() - 2 * kWindowTilePixels); |
| 362 } | 378 } |
| OLD | NEW |