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" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/win/scoped_gdi_object.h" | 13 #include "base/win/scoped_gdi_object.h" |
14 #include "base/win/scoped_hdc.h" | 14 #include "base/win/scoped_hdc.h" |
15 #include "base/win/scoped_select_object.h" | 15 #include "base/win/scoped_select_object.h" |
16 #include "remoting/host/chromoting_host.h" | 16 #include "remoting/host/chromoting_host.h" |
17 // TODO(wez): The DisconnectWindow isn't plugin-specific, so shouldn't have | 17 #include "remoting/host/host_ui_resource.h" |
18 // a dependency on the plugin's resource header. | |
19 #include "remoting/host/plugin/host_plugin_resource.h" | |
20 #include "remoting/host/ui_strings.h" | 18 #include "remoting/host/ui_strings.h" |
21 | 19 |
22 // TODO(garykac): Lots of duplicated code in this file and | 20 // TODO(garykac): Lots of duplicated code in this file and |
23 // continue_window_win.cc. If we need to expand this then we should | 21 // continue_window_win.cc. If we need to expand this then we should |
24 // create a class with the shared code. | 22 // create a class with the shared code. |
25 | 23 |
26 // HMODULE from DllMain/WinMain. This is needed to find our dialog resource. | 24 // HMODULE from DllMain/WinMain. This is needed to find our dialog resource. |
27 // This is defined in: | 25 // This is defined in: |
28 // Plugin: host_plugin.cc | 26 // Plugin: host_plugin.cc |
29 // SimpleHost: simple_host_process.cc | 27 // SimpleHost: simple_host_process.cc |
30 extern HMODULE g_hModule; | 28 extern HMODULE g_hModule; |
31 | 29 |
32 const int DISCONNECT_HOTKEY_ID = 1000; | 30 const int DISCONNECT_HOTKEY_ID = 1000; |
33 const int kWindowBorderRadius = 14; | 31 const int kWindowBorderRadius = 14; |
34 | 32 |
35 namespace remoting { | 33 namespace remoting { |
36 | 34 |
37 class DisconnectWindowWin : public DisconnectWindow { | 35 class DisconnectWindowWin : public DisconnectWindow { |
38 public: | 36 public: |
39 DisconnectWindowWin(); | 37 DisconnectWindowWin(); |
40 virtual ~DisconnectWindowWin(); | 38 virtual ~DisconnectWindowWin(); |
41 | 39 |
42 virtual void Show(remoting::ChromotingHost* host, | 40 virtual void Show(ChromotingHost* host, |
| 41 const DisconnectCallback& disconnect_callback, |
43 const std::string& username) OVERRIDE; | 42 const std::string& username) OVERRIDE; |
44 virtual void Hide() OVERRIDE; | 43 virtual void Hide() OVERRIDE; |
45 | 44 |
46 private: | 45 private: |
47 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam, | 46 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam, |
48 LPARAM lParam); | 47 LPARAM lParam); |
49 | 48 |
50 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); | 49 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
51 | 50 |
52 void ShutdownHost(); | 51 void ShutdownHost(); |
53 void EndDialog(int result); | 52 void EndDialog(); |
54 void SetStrings(const UiStrings& strings, const std::string& username); | 53 void SetStrings(const UiStrings& strings, const std::string& username); |
55 | 54 |
56 remoting::ChromotingHost* host_; | 55 DisconnectCallback disconnect_callback_; |
57 HWND hwnd_; | 56 HWND hwnd_; |
58 bool has_hotkey_; | 57 bool has_hotkey_; |
59 base::win::ScopedGDIObject<HPEN> border_pen_; | 58 base::win::ScopedGDIObject<HPEN> border_pen_; |
60 | 59 |
61 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); | 60 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); |
62 }; | 61 }; |
63 | 62 |
64 DisconnectWindowWin::DisconnectWindowWin() | 63 DisconnectWindowWin::DisconnectWindowWin() |
65 : host_(NULL), | 64 : hwnd_(NULL), |
66 hwnd_(NULL), | |
67 has_hotkey_(false), | 65 has_hotkey_(false), |
68 border_pen_(CreatePen(PS_SOLID, 5, | 66 border_pen_(CreatePen(PS_SOLID, 5, |
69 RGB(0.13 * 255, 0.69 * 255, 0.11 * 255))) { | 67 RGB(0.13 * 255, 0.69 * 255, 0.11 * 255))) { |
70 } | 68 } |
71 | 69 |
72 DisconnectWindowWin::~DisconnectWindowWin() { | 70 DisconnectWindowWin::~DisconnectWindowWin() { |
73 EndDialog(0); | 71 EndDialog(); |
74 } | 72 } |
75 | 73 |
76 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT msg, | 74 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT msg, |
77 WPARAM wParam, LPARAM lParam) { | 75 WPARAM wParam, LPARAM lParam) { |
78 DisconnectWindowWin* win = NULL; | 76 DisconnectWindowWin* win = NULL; |
79 if (msg == WM_INITDIALOG) { | 77 if (msg == WM_INITDIALOG) { |
80 win = reinterpret_cast<DisconnectWindowWin*>(lParam); | 78 win = reinterpret_cast<DisconnectWindowWin*>(lParam); |
81 CHECK(win); | 79 CHECK(win); |
82 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)win); | 80 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)win); |
83 } else { | 81 } else { |
84 LONG_PTR lp = GetWindowLongPtr(hwnd, DWLP_USER); | 82 LONG_PTR lp = GetWindowLongPtr(hwnd, DWLP_USER); |
85 win = reinterpret_cast<DisconnectWindowWin*>(lp); | 83 win = reinterpret_cast<DisconnectWindowWin*>(lp); |
86 } | 84 } |
87 if (win == NULL) | 85 if (win == NULL) |
88 return FALSE; | 86 return FALSE; |
89 return win->OnDialogMessage(hwnd, msg, wParam, lParam); | 87 return win->OnDialogMessage(hwnd, msg, wParam, lParam); |
90 } | 88 } |
91 | 89 |
92 BOOL DisconnectWindowWin::OnDialogMessage(HWND hwnd, UINT msg, | 90 BOOL DisconnectWindowWin::OnDialogMessage(HWND hwnd, UINT msg, |
93 WPARAM wParam, LPARAM lParam) { | 91 WPARAM wParam, LPARAM lParam) { |
94 switch (msg) { | 92 switch (msg) { |
95 // Ignore close messages. | 93 // Ignore close messages. |
96 case WM_CLOSE: | 94 case WM_CLOSE: |
97 return TRUE; | 95 return TRUE; |
98 | 96 |
99 // Handle the Disconnect button. | 97 // Handle the Disconnect button. |
100 case WM_COMMAND: | 98 case WM_COMMAND: |
101 switch (LOWORD(wParam)) { | 99 switch (LOWORD(wParam)) { |
102 case IDC_DISCONNECT: | 100 case IDC_DISCONNECT: |
| 101 EndDialog(); |
103 ShutdownHost(); | 102 ShutdownHost(); |
104 EndDialog(LOWORD(wParam)); | |
105 return TRUE; | 103 return TRUE; |
106 } | 104 } |
107 return FALSE; | 105 return FALSE; |
108 | 106 |
109 // Ensure we don't try to use the HWND anymore. | 107 // Ensure we don't try to use the HWND anymore. |
110 case WM_DESTROY: | 108 case WM_DESTROY: |
111 hwnd_ = NULL; | 109 hwnd_ = NULL; |
112 return TRUE; | 110 return TRUE; |
113 | 111 |
114 // Handle the disconnect hot-key. | 112 // Handle the disconnect hot-key. |
115 case WM_HOTKEY: | 113 case WM_HOTKEY: |
| 114 EndDialog(); |
116 ShutdownHost(); | 115 ShutdownHost(); |
117 EndDialog(0); | |
118 return TRUE; | 116 return TRUE; |
119 | 117 |
120 // Let the window be draggable by its client area by responding | 118 // Let the window be draggable by its client area by responding |
121 // that the entire window is the title bar. | 119 // that the entire window is the title bar. |
122 case WM_NCHITTEST: | 120 case WM_NCHITTEST: |
123 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); | 121 SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION); |
124 return TRUE; | 122 return TRUE; |
125 | 123 |
126 case WM_PAINT: | 124 case WM_PAINT: |
127 { | 125 { |
128 PAINTSTRUCT ps; | 126 PAINTSTRUCT ps; |
129 HDC hdc = BeginPaint(hwnd_, &ps); | 127 HDC hdc = BeginPaint(hwnd_, &ps); |
130 RECT rect; | 128 RECT rect; |
131 GetClientRect(hwnd_, &rect); | 129 GetClientRect(hwnd_, &rect); |
132 { | 130 { |
133 base::win::ScopedSelectObject border(hdc, border_pen_); | 131 base::win::ScopedSelectObject border(hdc, border_pen_); |
134 base::win::ScopedSelectObject brush(hdc, GetStockObject(NULL_BRUSH)); | 132 base::win::ScopedSelectObject brush(hdc, GetStockObject(NULL_BRUSH)); |
135 RoundRect(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1, | 133 RoundRect(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1, |
136 kWindowBorderRadius, kWindowBorderRadius); | 134 kWindowBorderRadius, kWindowBorderRadius); |
137 } | 135 } |
138 EndPaint(hwnd_, &ps); | 136 EndPaint(hwnd_, &ps); |
139 return TRUE; | 137 return TRUE; |
140 } | 138 } |
141 } | 139 } |
142 return FALSE; | 140 return FALSE; |
143 } | 141 } |
144 | 142 |
145 void DisconnectWindowWin::Show(ChromotingHost* host, | 143 void DisconnectWindowWin::Show(ChromotingHost* host, |
| 144 const DisconnectCallback& disconnect_callback, |
146 const std::string& username) { | 145 const std::string& username) { |
147 CHECK(!hwnd_); | 146 CHECK(!hwnd_); |
148 host_ = host; | 147 disconnect_callback_ = disconnect_callback; |
149 | 148 |
150 // Load the dialog resource so that we can modify the RTL flags if necessary. | 149 // Load the dialog resource so that we can modify the RTL flags if necessary. |
151 // This is taken from chrome/default_plugin/install_dialog.cc | 150 // This is taken from chrome/default_plugin/install_dialog.cc |
152 HRSRC dialog_resource = | 151 HRSRC dialog_resource = |
153 FindResource(g_hModule, MAKEINTRESOURCE(IDD_DISCONNECT), RT_DIALOG); | 152 FindResource(g_hModule, MAKEINTRESOURCE(IDD_DISCONNECT), RT_DIALOG); |
154 CHECK(dialog_resource); | 153 CHECK(dialog_resource); |
155 HGLOBAL dialog_template = LoadResource(g_hModule, dialog_resource); | 154 HGLOBAL dialog_template = LoadResource(g_hModule, dialog_resource); |
156 CHECK(dialog_template); | 155 CHECK(dialog_template); |
157 DLGTEMPLATE* dialog_pointer = | 156 DLGTEMPLATE* dialog_pointer = |
158 reinterpret_cast<DLGTEMPLATE*>(LockResource(dialog_template)); | 157 reinterpret_cast<DLGTEMPLATE*>(LockResource(dialog_template)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 int window_height = window_rect.bottom - window_rect.top; | 195 int window_height = window_rect.bottom - window_rect.top; |
197 int top = monitor_info.rcWork.bottom - window_height; | 196 int top = monitor_info.rcWork.bottom - window_height; |
198 int left = (monitor_info.rcWork.right + monitor_info.rcWork.left - | 197 int left = (monitor_info.rcWork.right + monitor_info.rcWork.left - |
199 window_width) / 2; | 198 window_width) / 2; |
200 SetWindowPos(hwnd_, NULL, left, top, 0, 0, SWP_NOSIZE | SWP_NOZORDER); | 199 SetWindowPos(hwnd_, NULL, left, top, 0, 0, SWP_NOSIZE | SWP_NOZORDER); |
201 } | 200 } |
202 ShowWindow(hwnd_, SW_SHOW); | 201 ShowWindow(hwnd_, SW_SHOW); |
203 } | 202 } |
204 | 203 |
205 void DisconnectWindowWin::ShutdownHost() { | 204 void DisconnectWindowWin::ShutdownHost() { |
206 CHECK(host_); | 205 CHECK(!disconnect_callback_.is_null()); |
207 host_->Shutdown(base::Closure()); | 206 disconnect_callback_.Run(); |
208 } | 207 } |
209 | 208 |
210 static int GetControlTextWidth(HWND control) { | 209 static int GetControlTextWidth(HWND control) { |
211 RECT rect = {0, 0, 0, 0}; | 210 RECT rect = {0, 0, 0, 0}; |
212 WCHAR text[256]; | 211 WCHAR text[256]; |
213 int result = GetWindowText(control, text, arraysize(text)); | 212 int result = GetWindowText(control, text, arraysize(text)); |
214 if (result) { | 213 if (result) { |
215 base::win::ScopedGetDC dc(control); | 214 base::win::ScopedGetDC dc(control); |
216 base::win::ScopedSelectObject font( | 215 base::win::ScopedSelectObject font( |
217 dc, (HFONT)SendMessage(control, WM_GETFONT, 0, 0)); | 216 dc, (HFONT)SendMessage(control, WM_GETFONT, 0, 0)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 int width = (window_rect.right - window_rect.left) + | 267 int width = (window_rect.right - window_rect.left) + |
269 button_width_delta + label_width_delta; | 268 button_width_delta + label_width_delta; |
270 int height = window_rect.bottom - window_rect.top; | 269 int height = window_rect.bottom - window_rect.top; |
271 SetWindowPos(hwnd_, NULL, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER); | 270 SetWindowPos(hwnd_, NULL, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER); |
272 HRGN rgn = CreateRoundRectRgn(0, 0, width, height, kWindowBorderRadius, | 271 HRGN rgn = CreateRoundRectRgn(0, 0, width, height, kWindowBorderRadius, |
273 kWindowBorderRadius); | 272 kWindowBorderRadius); |
274 SetWindowRgn(hwnd_, rgn, TRUE); | 273 SetWindowRgn(hwnd_, rgn, TRUE); |
275 } | 274 } |
276 | 275 |
277 void DisconnectWindowWin::Hide() { | 276 void DisconnectWindowWin::Hide() { |
278 EndDialog(0); | 277 EndDialog(); |
279 } | 278 } |
280 | 279 |
281 void DisconnectWindowWin::EndDialog(int result) { | 280 void DisconnectWindowWin::EndDialog() { |
282 if (has_hotkey_) { | 281 if (has_hotkey_) { |
283 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); | 282 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID); |
284 has_hotkey_ = false; | 283 has_hotkey_ = false; |
285 } | 284 } |
286 | 285 |
287 if (hwnd_) { | 286 if (hwnd_) { |
288 ::EndDialog(hwnd_, result); | 287 ::DestroyWindow(hwnd_); |
289 hwnd_ = NULL; | 288 hwnd_ = NULL; |
290 } | 289 } |
291 } | 290 } |
292 | 291 |
293 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { | 292 scoped_ptr<DisconnectWindow> DisconnectWindow::Create() { |
294 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); | 293 return scoped_ptr<DisconnectWindow>(new DisconnectWindowWin()); |
295 } | 294 } |
296 | 295 |
297 } // namespace remoting | 296 } // namespace remoting |
OLD | NEW |