| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 } // namespace anonymous | 35 } // namespace anonymous |
| 36 | 36 |
| 37 namespace remoting { | 37 namespace remoting { |
| 38 | 38 |
| 39 class DisconnectWindowWin : public DisconnectWindow { | 39 class DisconnectWindowWin : public DisconnectWindow { |
| 40 public: | 40 public: |
| 41 DisconnectWindowWin(); | 41 DisconnectWindowWin(); |
| 42 virtual ~DisconnectWindowWin(); | 42 virtual ~DisconnectWindowWin(); |
| 43 | 43 |
| 44 // DisconnectWindow interface. |
| 44 virtual bool Show(const UiStrings& ui_strings, | 45 virtual bool Show(const UiStrings& ui_strings, |
| 45 const DisconnectCallback& disconnect_callback, | 46 const base::Closure& disconnect_callback, |
| 46 const std::string& username) OVERRIDE; | 47 const std::string& username) OVERRIDE; |
| 47 virtual void Hide() OVERRIDE; | 48 virtual void Hide() OVERRIDE; |
| 48 | 49 |
| 49 private: | 50 private: |
| 50 static BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wparam, | 51 static BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wparam, |
| 51 LPARAM lparam); | 52 LPARAM lparam); |
| 52 | 53 |
| 53 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); | 54 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 54 | 55 |
| 55 // Creates the dialog window and registers the disconnect hot key. | 56 // Creates the dialog window and registers the disconnect hot key. |
| 56 bool BeginDialog(const UiStrings& ui_strings, | 57 bool BeginDialog(const UiStrings& ui_strings, |
| 57 const std::string& username); | 58 const std::string& username); |
| 58 | 59 |
| 59 // Unregisters the disconnect hot key and closed the dialog. | 60 // Closes the dialog, unregisters the hot key and invokes the disconnect |
| 61 // callback, if set. |
| 60 void EndDialog(); | 62 void EndDialog(); |
| 61 | 63 |
| 62 // Invokes the disconnect callback passed to Show(). | |
| 63 void InvokeDiconnectCallback(); | |
| 64 | |
| 65 // Trys to position the dialog window above the taskbar. | 64 // Trys to position the dialog window above the taskbar. |
| 66 void SetDialogPosition(); | 65 void SetDialogPosition(); |
| 67 | 66 |
| 68 // Applies localization string and resizes the dialog. | 67 // Applies localization string and resizes the dialog. |
| 69 bool SetStrings(const UiStrings& strings, const string16& username); | 68 bool SetStrings(const UiStrings& strings, const string16& username); |
| 70 | 69 |
| 71 DisconnectCallback disconnect_callback_; | 70 base::Closure disconnect_callback_; |
| 72 HWND hwnd_; | 71 HWND hwnd_; |
| 73 bool has_hotkey_; | 72 bool has_hotkey_; |
| 74 base::win::ScopedGDIObject<HPEN> border_pen_; | 73 base::win::ScopedGDIObject<HPEN> border_pen_; |
| 75 | 74 |
| 76 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); | 75 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); |
| 77 }; | 76 }; |
| 78 | 77 |
| 79 static int GetControlTextWidth(HWND control) { | 78 static int GetControlTextWidth(HWND control) { |
| 80 RECT rect = {0, 0, 0, 0}; | 79 RECT rect = {0, 0, 0, 0}; |
| 81 WCHAR text[256]; | 80 WCHAR text[256]; |
| 82 int result = GetWindowText(control, text, arraysize(text)); | 81 int result = GetWindowText(control, text, arraysize(text)); |
| 83 if (result) { | 82 if (result) { |
| 84 base::win::ScopedGetDC dc(control); | 83 base::win::ScopedGetDC dc(control); |
| 85 base::win::ScopedSelectObject font( | 84 base::win::ScopedSelectObject font( |
| 86 dc, (HFONT)SendMessage(control, WM_GETFONT, 0, 0)); | 85 dc, (HFONT)SendMessage(control, WM_GETFONT, 0, 0)); |
| 87 DrawText(dc, text, -1, &rect, DT_CALCRECT | DT_SINGLELINE); | 86 DrawText(dc, text, -1, &rect, DT_CALCRECT | DT_SINGLELINE); |
| 88 } | 87 } |
| 89 return rect.right; | 88 return rect.right; |
| 90 } | 89 } |
| 91 | 90 |
| 92 DisconnectWindowWin::DisconnectWindowWin() | 91 DisconnectWindowWin::DisconnectWindowWin() |
| 93 : hwnd_(NULL), | 92 : hwnd_(NULL), |
| 94 has_hotkey_(false), | 93 has_hotkey_(false), |
| 95 border_pen_(CreatePen(PS_SOLID, 5, | 94 border_pen_(CreatePen(PS_SOLID, 5, |
| 96 RGB(0.13 * 255, 0.69 * 255, 0.11 * 255))) { | 95 RGB(0.13 * 255, 0.69 * 255, 0.11 * 255))) { |
| 97 } | 96 } |
| 98 | 97 |
| 99 DisconnectWindowWin::~DisconnectWindowWin() { | 98 DisconnectWindowWin::~DisconnectWindowWin() { |
| 100 EndDialog(); | 99 Hide(); |
| 101 } | 100 } |
| 102 | 101 |
| 103 bool DisconnectWindowWin::Show(const UiStrings& ui_strings, | 102 bool DisconnectWindowWin::Show(const UiStrings& ui_strings, |
| 104 const DisconnectCallback& disconnect_callback, | 103 const base::Closure& disconnect_callback, |
| 105 const std::string& username) { | 104 const std::string& username) { |
| 106 DCHECK(disconnect_callback_.is_null()); | 105 DCHECK(disconnect_callback_.is_null()); |
| 107 DCHECK(!disconnect_callback.is_null()); | 106 DCHECK(!disconnect_callback.is_null()); |
| 108 | 107 |
| 109 disconnect_callback_ = disconnect_callback; | 108 disconnect_callback_ = disconnect_callback; |
| 110 | 109 |
| 111 if (BeginDialog(ui_strings, username)) { | 110 if (BeginDialog(ui_strings, username)) { |
| 112 return true; | 111 return true; |
| 113 } else { | 112 } else { |
| 114 EndDialog(); | 113 Hide(); |
| 115 return false; | 114 return false; |
| 116 } | 115 } |
| 117 } | 116 } |
| 118 | 117 |
| 119 void DisconnectWindowWin::Hide() { | 118 void DisconnectWindowWin::Hide() { |
| 119 // Clear the |disconnect_callback_| so it won't be invoked by EndDialog(). |
| 120 disconnect_callback_.Reset(); |
| 120 EndDialog(); | 121 EndDialog(); |
| 121 } | 122 } |
| 122 | 123 |
| 123 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT message, | 124 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT message, |
| 124 WPARAM wparam, LPARAM lparam) { | 125 WPARAM wparam, LPARAM lparam) { |
| 125 LONG_PTR self = NULL; | 126 LONG_PTR self = NULL; |
| 126 if (message == WM_INITDIALOG) { | 127 if (message == WM_INITDIALOG) { |
| 127 self = lparam; | 128 self = lparam; |
| 128 | 129 |
| 129 // Store |this| to the window's user data. | 130 // Store |this| to the window's user data. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 147 switch (message) { | 148 switch (message) { |
| 148 // Ignore close messages. | 149 // Ignore close messages. |
| 149 case WM_CLOSE: | 150 case WM_CLOSE: |
| 150 return TRUE; | 151 return TRUE; |
| 151 | 152 |
| 152 // Handle the Disconnect button. | 153 // Handle the Disconnect button. |
| 153 case WM_COMMAND: | 154 case WM_COMMAND: |
| 154 switch (LOWORD(wparam)) { | 155 switch (LOWORD(wparam)) { |
| 155 case IDC_DISCONNECT: | 156 case IDC_DISCONNECT: |
| 156 EndDialog(); | 157 EndDialog(); |
| 157 InvokeDiconnectCallback(); | |
| 158 return TRUE; | 158 return TRUE; |
| 159 } | 159 } |
| 160 return FALSE; | 160 return FALSE; |
| 161 | 161 |
| 162 // Ensure we don't try to use the HWND anymore. | 162 // Ensure we don't try to use the HWND anymore. |
| 163 case WM_DESTROY: | 163 case WM_DESTROY: |
| 164 hwnd_ = NULL; | 164 hwnd_ = NULL; |
| 165 |
| 166 // Ensure that the disconnect callback is invoked even if somehow our |
| 167 // window gets destroyed. |
| 168 EndDialog(); |
| 169 |
| 165 return TRUE; | 170 return TRUE; |
| 166 | 171 |
| 167 // Ensure the dialog stays visible if the work area dimensions change. | 172 // Ensure the dialog stays visible if the work area dimensions change. |
| 168 case WM_SETTINGCHANGE: | 173 case WM_SETTINGCHANGE: |
| 169 if (wparam == SPI_SETWORKAREA) | 174 if (wparam == SPI_SETWORKAREA) |
| 170 SetDialogPosition(); | 175 SetDialogPosition(); |
| 171 return TRUE; | 176 return TRUE; |
| 172 | 177 |
| 173 // Ensure the dialog stays visible if the display dimensions change. | 178 // Ensure the dialog stays visible if the display dimensions change. |
| 174 case WM_DISPLAYCHANGE: | 179 case WM_DISPLAYCHANGE: |
| 175 SetDialogPosition(); | 180 SetDialogPosition(); |
| 176 return TRUE; | 181 return TRUE; |
| 177 | 182 |
| 178 // Handle the disconnect hot-key. | 183 // Handle the disconnect hot-key. |
| 179 case WM_HOTKEY: | 184 case WM_HOTKEY: |
| 180 EndDialog(); | 185 EndDialog(); |
| 181 InvokeDiconnectCallback(); | |
| 182 return TRUE; | 186 return TRUE; |
| 183 | 187 |
| 184 // Let the window be draggable by its client area by responding | 188 // Let the window be draggable by its client area by responding |
| 185 // that the entire window is the title bar. | 189 // that the entire window is the title bar. |
| 186 case WM_NCHITTEST: | 190 case WM_NCHITTEST: |
| 187 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); | 191 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); |
| 188 return TRUE; | 192 return TRUE; |
| 189 | 193 |
| 190 case WM_PAINT: { | 194 case WM_PAINT: { |
| 191 PAINTSTRUCT ps; | 195 PAINTSTRUCT ps; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 263 } |
| 260 | 264 |
| 261 void DisconnectWindowWin::EndDialog() { | 265 void DisconnectWindowWin::EndDialog() { |
| 262 if (has_hotkey_) { | 266 if (has_hotkey_) { |
| 263 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); | 267 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); |
| 264 has_hotkey_ = false; | 268 has_hotkey_ = false; |
| 265 } | 269 } |
| 266 | 270 |
| 267 if (hwnd_) { | 271 if (hwnd_) { |
| 268 ::DestroyWindow(hwnd_); | 272 ::DestroyWindow(hwnd_); |
| 269 hwnd_ = NULL; | 273 DCHECK(hwnd_ == NULL); |
| 270 } | 274 } |
| 271 | 275 |
| 272 disconnect_callback_.Reset(); | 276 if (!disconnect_callback_.is_null()) { |
| 273 } | 277 disconnect_callback_.Run(); |
| 274 | 278 disconnect_callback_.Reset(); |
| 275 void DisconnectWindowWin::InvokeDiconnectCallback() { | 279 } |
| 276 disconnect_callback_.Run(); | |
| 277 } | 280 } |
| 278 | 281 |
| 279 void DisconnectWindowWin::SetDialogPosition() { | 282 void DisconnectWindowWin::SetDialogPosition() { |
| 280 // Try to center the window above the task-bar. If that fails, use the | 283 // Try to center the window above the task-bar. If that fails, use the |
| 281 // primary monitor. If that fails (very unlikely), use the default position. | 284 // primary monitor. If that fails (very unlikely), use the default position. |
| 282 HWND taskbar = FindWindow(kShellTrayWindowName, NULL); | 285 HWND taskbar = FindWindow(kShellTrayWindowName, NULL); |
| 283 HMONITOR monitor = MonitorFromWindow(taskbar, MONITOR_DEFAULTTOPRIMARY); | 286 HMONITOR monitor = MonitorFromWindow(taskbar, MONITOR_DEFAULTTOPRIMARY); |
| 284 MONITORINFO monitor_info = {sizeof(monitor_info)}; | 287 MONITORINFO monitor_info = {sizeof(monitor_info)}; |
| 285 RECT window_rect; | 288 RECT window_rect; |
| 286 if (GetMonitorInfo(monitor, &monitor_info) && | 289 if (GetMonitorInfo(monitor, &monitor_info) && |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return false; | 384 return false; |
| 382 | 385 |
| 383 return true; | 386 return true; |
| 384 } | 387 } |
| 385 | 388 |
| 386 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { | 389 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { |
| 387 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); | 390 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); |
| 388 } | 391 } |
| 389 | 392 |
| 390 } // namespace remoting | 393 } // namespace remoting |
| OLD | NEW |