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 "remoting/host/disconnect_window.h" | 5 #include "remoting/host/disconnect_window.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 | 44 |
45 private: | 45 private: |
46 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam, | 46 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam, |
47 LPARAM lParam); | 47 LPARAM lParam); |
48 | 48 |
49 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); | 49 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
50 | 50 |
51 void ShutdownHost(); | 51 void ShutdownHost(); |
52 void EndDialog(); | 52 void EndDialog(); |
53 void SetStrings(const UiStrings& strings, const std::string& username); | 53 void SetStrings(const UiStrings& strings, const std::string& username); |
54 void SetDialogPosition(); | |
54 | 55 |
55 DisconnectCallback disconnect_callback_; | 56 DisconnectCallback disconnect_callback_; |
56 HWND hwnd_; | 57 HWND hwnd_; |
57 bool has_hotkey_; | 58 bool has_hotkey_; |
58 base::win::ScopedGDIObject<HPEN> border_pen_; | 59 base::win::ScopedGDIObject<HPEN> border_pen_; |
59 | 60 |
60 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); | 61 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); |
61 }; | 62 }; |
62 | 63 |
63 DisconnectWindowWin::DisconnectWindowWin() | 64 DisconnectWindowWin::DisconnectWindowWin() |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 ShutdownHost(); | 103 ShutdownHost(); |
103 return TRUE; | 104 return TRUE; |
104 } | 105 } |
105 return FALSE; | 106 return FALSE; |
106 | 107 |
107 // Ensure we don't try to use the HWND anymore. | 108 // Ensure we don't try to use the HWND anymore. |
108 case WM_DESTROY: | 109 case WM_DESTROY: |
109 hwnd_ = NULL; | 110 hwnd_ = NULL; |
110 return TRUE; | 111 return TRUE; |
111 | 112 |
113 // Ensure the dialog stays visible if the work area dimensions change. | |
114 case WM_SETTINGCHANGE: | |
115 if (wParam == SPI_SETWORKAREA) | |
116 SetDialogPosition(); | |
117 return FALSE; | |
alexeypa (please no reviews)
2012/08/08 22:05:08
Return TRUE if the message was processed, FALSE ot
Wez
2012/08/09 21:58:08
We'd only do that if we wanted to suppress any add
Wez
2012/08/09 21:58:08
Done.
| |
118 | |
119 // Ensure the dialog stays visible if the display dimensions change. | |
120 case WM_DISPLAYCHANGE: | |
121 SetDialogPosition(); | |
122 return FALSE; | |
alexeypa (please no reviews)
2012/08/08 22:05:08
return TRUE;
Wez
2012/08/09 21:58:08
Same here.
Wez
2012/08/09 21:58:08
Done.
| |
123 | |
112 // Handle the disconnect hot-key. | 124 // Handle the disconnect hot-key. |
113 case WM_HOTKEY: | 125 case WM_HOTKEY: |
114 EndDialog(); | 126 EndDialog(); |
115 ShutdownHost(); | 127 ShutdownHost(); |
116 return TRUE; | 128 return TRUE; |
117 | 129 |
118 // Let the window be draggable by its client area by responding | 130 // Let the window be draggable by its client area by responding |
119 // that the entire window is the title bar. | 131 // that the entire window is the title bar. |
120 case WM_NCHITTEST: | 132 case WM_NCHITTEST: |
121 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); | 133 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); |
122 return TRUE; | 134 return TRUE; |
123 | 135 |
124 case WM_PAINT: | 136 case WM_PAINT: { |
137 PAINTSTRUCT ps; | |
138 HDC hdc = BeginPaint(hwnd_, &ps); | |
alexeypa (please no reviews)
2012/08/08 22:05:08
BeginPaint can return NULL.
Wez
2012/08/09 21:58:08
Technically yes, but unless your system is horribl
alexeypa (please no reviews)
2012/08/10 17:28:01
The default assumption a person reading the code m
Wez
2012/08/10 20:06:17
I think this specific code should be pretty safe,
alexeypa (please no reviews)
2012/08/10 20:30:03
OK, thanks!
| |
139 RECT rect; | |
140 GetClientRect(hwnd_, &rect); | |
alexeypa (please no reviews)
2012/08/08 22:05:08
GetClientRect can fail.
| |
125 { | 141 { |
126 PAINTSTRUCT ps; | 142 base::win::ScopedSelectObject border(hdc, border_pen_); |
127 HDC hdc = BeginPaint(hwnd_, &ps); | 143 base::win::ScopedSelectObject brush(hdc, GetStockObject(NULL_BRUSH)); |
alexeypa (please no reviews)
2012/08/08 22:05:08
GetStockObject can fail.
| |
128 RECT rect; | 144 RoundRect(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1, |
alexeypa (please no reviews)
2012/08/08 22:05:08
RoundRect can fail.
| |
129 GetClientRect(hwnd_, &rect); | 145 kWindowBorderRadius, kWindowBorderRadius); |
130 { | |
131 base::win::ScopedSelectObject border(hdc, border_pen_); | |
132 base::win::ScopedSelectObject brush(hdc, GetStockObject(NULL_BRUSH)); | |
133 RoundRect(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1, | |
134 kWindowBorderRadius, kWindowBorderRadius); | |
135 } | |
136 EndPaint(hwnd_, &ps); | |
137 return TRUE; | |
138 } | 146 } |
147 EndPaint(hwnd_, &ps); | |
148 return TRUE; | |
alexeypa (please no reviews)
2012/08/08 22:05:08
The application should return TRUE it processes WM
Wez
2012/08/09 21:58:08
Yes, it is doing.
alexeypa (please no reviews)
2012/08/10 17:28:01
:-) I'm not sure what my comment was doing there.
| |
149 } | |
139 } | 150 } |
140 return FALSE; | 151 return FALSE; |
141 } | 152 } |
142 | 153 |
143 void DisconnectWindowWin::Show(ChromotingHost* host, | 154 void DisconnectWindowWin::Show(ChromotingHost* host, |
144 const DisconnectCallback& disconnect_callback, | 155 const DisconnectCallback& disconnect_callback, |
145 const std::string& username) { | 156 const std::string& username) { |
146 CHECK(!hwnd_); | 157 CHECK(!hwnd_); |
147 disconnect_callback_ = disconnect_callback; | 158 disconnect_callback_ = disconnect_callback; |
148 | 159 |
(...skipping 26 matching lines...) Expand all Loading... | |
175 (DLGPROC)DialogProc, (LPARAM)this); | 186 (DLGPROC)DialogProc, (LPARAM)this); |
176 CHECK(hwnd_); | 187 CHECK(hwnd_); |
177 | 188 |
178 // Set up handler for Ctrl-Alt-Esc shortcut. | 189 // Set up handler for Ctrl-Alt-Esc shortcut. |
179 if (!has_hotkey_ && RegisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID, | 190 if (!has_hotkey_ && RegisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID, |
180 MOD_ALT | MOD_CONTROL, VK_ESCAPE)) { | 191 MOD_ALT | MOD_CONTROL, VK_ESCAPE)) { |
181 has_hotkey_ = true; | 192 has_hotkey_ = true; |
182 } | 193 } |
183 | 194 |
184 SetStrings(host->ui_strings(), username); | 195 SetStrings(host->ui_strings(), username); |
185 | 196 SetDialogPosition(); |
186 // Try to center the window above the task-bar. If that fails, use the | |
187 // primary monitor. If that fails (very unlikely), use the default position. | |
188 HWND taskbar = FindWindow(L"Shell_TrayWnd", NULL); | |
189 HMONITOR monitor = MonitorFromWindow(taskbar, MONITOR_DEFAULTTOPRIMARY); | |
190 MONITORINFO monitor_info = {sizeof(monitor_info)}; | |
191 RECT window_rect; | |
192 if (GetMonitorInfo(monitor, &monitor_info) && | |
193 GetWindowRect(hwnd_, &window_rect)) { | |
194 int window_width = window_rect.right - window_rect.left; | |
195 int window_height = window_rect.bottom - window_rect.top; | |
196 int top = monitor_info.rcWork.bottom - window_height; | |
197 int left = (monitor_info.rcWork.right + monitor_info.rcWork.left - | |
198 window_width) / 2; | |
199 SetWindowPos(hwnd_, NULL, left, top, 0, 0, SWP_NOSIZE | SWP_NOZORDER); | |
200 } | |
201 ShowWindow(hwnd_, SW_SHOW); | 197 ShowWindow(hwnd_, SW_SHOW); |
202 } | 198 } |
203 | 199 |
204 void DisconnectWindowWin::ShutdownHost() { | 200 void DisconnectWindowWin::ShutdownHost() { |
205 CHECK(!disconnect_callback_.is_null()); | 201 CHECK(!disconnect_callback_.is_null()); |
206 disconnect_callback_.Run(); | 202 disconnect_callback_.Run(); |
207 } | 203 } |
208 | 204 |
209 static int GetControlTextWidth(HWND control) { | 205 static int GetControlTextWidth(HWND control) { |
210 RECT rect = {0, 0, 0, 0}; | 206 RECT rect = {0, 0, 0, 0}; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 GetWindowRect(hwnd_, &window_rect); | 262 GetWindowRect(hwnd_, &window_rect); |
267 int width = (window_rect.right - window_rect.left) + | 263 int width = (window_rect.right - window_rect.left) + |
268 button_width_delta + label_width_delta; | 264 button_width_delta + label_width_delta; |
269 int height = window_rect.bottom - window_rect.top; | 265 int height = window_rect.bottom - window_rect.top; |
270 SetWindowPos(hwnd_, NULL, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER); | 266 SetWindowPos(hwnd_, NULL, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER); |
271 HRGN rgn = CreateRoundRectRgn(0, 0, width, height, kWindowBorderRadius, | 267 HRGN rgn = CreateRoundRectRgn(0, 0, width, height, kWindowBorderRadius, |
272 kWindowBorderRadius); | 268 kWindowBorderRadius); |
273 SetWindowRgn(hwnd_, rgn, TRUE); | 269 SetWindowRgn(hwnd_, rgn, TRUE); |
274 } | 270 } |
275 | 271 |
272 void DisconnectWindowWin::SetDialogPosition() { | |
273 // Try to center the window above the task-bar. If that fails, use the | |
274 // primary monitor. If that fails (very unlikely), use the default position. | |
275 HWND taskbar = FindWindow(L"Shell_TrayWnd", NULL); | |
alexeypa (please no reviews)
2012/08/08 22:05:08
nit: Define a constant for the window name.
alexeypa (please no reviews)
2012/08/08 22:05:08
All the API functions can fail.
alexeypa (please no reviews)
2012/08/08 22:05:08
It would be nice if the window could be correctly
Wez
2012/08/09 21:58:08
That's what this code does; see the comment and pa
Wez
2012/08/09 21:58:08
If FindWindow() fails then MonitorFromWindow() wil
Wez
2012/08/09 21:58:08
Done.
| |
276 HMONITOR monitor = MonitorFromWindow(taskbar, MONITOR_DEFAULTTOPRIMARY); | |
277 MONITORINFO monitor_info = {sizeof(monitor_info)}; | |
278 RECT window_rect; | |
279 if (GetMonitorInfo(monitor, &monitor_info) && | |
280 GetWindowRect(hwnd_, &window_rect)) { | |
281 int window_width = window_rect.right - window_rect.left; | |
282 int window_height = window_rect.bottom - window_rect.top; | |
283 int top = monitor_info.rcWork.bottom - window_height; | |
284 int left = (monitor_info.rcWork.right + monitor_info.rcWork.left - | |
285 window_width) / 2; | |
286 SetWindowPos(hwnd_, NULL, left, top, 0, 0, SWP_NOSIZE | SWP_NOZORDER); | |
287 } | |
288 } | |
289 | |
276 void DisconnectWindowWin::Hide() { | 290 void DisconnectWindowWin::Hide() { |
277 EndDialog(); | 291 EndDialog(); |
278 } | 292 } |
279 | 293 |
280 void DisconnectWindowWin::EndDialog() { | 294 void DisconnectWindowWin::EndDialog() { |
281 if (has_hotkey_) { | 295 if (has_hotkey_) { |
282 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); | 296 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); |
283 has_hotkey_ = false; | 297 has_hotkey_ = false; |
284 } | 298 } |
285 | 299 |
286 if (hwnd_) { | 300 if (hwnd_) { |
287 ::DestroyWindow(hwnd_); | 301 ::DestroyWindow(hwnd_); |
288 hwnd_ = NULL; | 302 hwnd_ = NULL; |
289 } | 303 } |
290 } | 304 } |
291 | 305 |
292 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { | 306 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { |
293 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); | 307 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); |
294 } | 308 } |
295 | 309 |
296 } // namespace remoting | 310 } // namespace remoting |
OLD | NEW |