Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: remoting/host/disconnect_window_win.cc

Issue 7613009: Add support for Disconnect hot key in Windows. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "remoting/host/chromoting_host.h" 12 #include "remoting/host/chromoting_host.h"
13 // TODO(wez): The DisconnectWindow isn't plugin-specific, so shouldn't have 13 // TODO(wez): The DisconnectWindow isn't plugin-specific, so shouldn't have
14 // a dependency on the plugin's resource header. 14 // a dependency on the plugin's resource header.
15 #include "remoting/host/plugin/host_plugin_resource.h" 15 #include "remoting/host/plugin/host_plugin_resource.h"
16 16
17 // TODO(garykac): Lots of duplicated code in this file and 17 // TODO(garykac): Lots of duplicated code in this file and
18 // continue_window_win.cc. These global floating windows are temporary so 18 // continue_window_win.cc. These global floating windows are temporary so
19 // they should be deleted soon. If we need to expand this then we should 19 // they should be deleted soon. If we need to expand this then we should
20 // create a class with the shared code. 20 // create a class with the shared code.
21 21
22 // HMODULE from DllMain/WinMain. This is needed to find our dialog resource. 22 // HMODULE from DllMain/WinMain. This is needed to find our dialog resource.
23 // This is defined in: 23 // This is defined in:
24 // Plugin: host_plugin.cc 24 // Plugin: host_plugin.cc
25 // SimpleHost: simple_host_process.cc 25 // SimpleHost: simple_host_process.cc
26 extern HMODULE g_hModule; 26 extern HMODULE g_hModule;
27 27
28 const int DISCONNECT_HOTKEY_ID = 1000;
29
28 namespace remoting { 30 namespace remoting {
29 31
30 class DisconnectWindowWin : public DisconnectWindow { 32 class DisconnectWindowWin : public DisconnectWindow {
31 public: 33 public:
32 DisconnectWindowWin(); 34 DisconnectWindowWin();
33 virtual ~DisconnectWindowWin(); 35 virtual ~DisconnectWindowWin();
34 36
35 virtual void Show(remoting::ChromotingHost* host, 37 virtual void Show(remoting::ChromotingHost* host,
36 const std::string& username) OVERRIDE; 38 const std::string& username) OVERRIDE;
37 virtual void Hide() OVERRIDE; 39 virtual void Hide() OVERRIDE;
38 40
39 private: 41 private:
40 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam, 42 static BOOL CALLBACK DialogProc(HWND hwmd, UINT msg, WPARAM wParam,
41 LPARAM lParam); 43 LPARAM lParam);
42 44
43 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 45 BOOL OnDialogMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
44 46
45 void EndDialog(); 47 void ShutdownHost();
48 void SetDisconnectButtonText(HWND hwnd);
49 void EndDialog(int result);
50 void RemoveKeyHandler();
46 51
47 remoting::ChromotingHost* host_; 52 remoting::ChromotingHost* host_;
48 std::string username_; 53 std::string username_;
49 HWND hwnd_; 54 HWND hwnd_;
55 bool has_hotkey_;
50 56
51 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin); 57 DISALLOW_COPY_AND_ASSIGN(DisconnectWindowWin);
52 }; 58 };
53 59
54 DisconnectWindowWin::DisconnectWindowWin() 60 DisconnectWindowWin::DisconnectWindowWin()
55 : host_(NULL), 61 : host_(NULL),
56 username_(""), 62 username_(""),
57 hwnd_(NULL) { 63 hwnd_(NULL),
64 has_hotkey_(false) {
58 } 65 }
59 66
60 DisconnectWindowWin::~DisconnectWindowWin() { 67 DisconnectWindowWin::~DisconnectWindowWin() {
61 EndDialog(); 68 EndDialog(0);
62 } 69 }
63 70
64 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT msg, 71 BOOL CALLBACK DisconnectWindowWin::DialogProc(HWND hwnd, UINT msg,
65 WPARAM wParam, LPARAM lParam) { 72 WPARAM wParam, LPARAM lParam) {
66 DisconnectWindowWin* win = NULL; 73 DisconnectWindowWin* win = NULL;
67 if (msg == WM_INITDIALOG) { 74 if (msg == WM_INITDIALOG) {
68 win = reinterpret_cast<DisconnectWindowWin*>(lParam); 75 win = reinterpret_cast<DisconnectWindowWin*>(lParam);
69 CHECK(win); 76 CHECK(win);
70 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)win); 77 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)win);
71 } else { 78 } else {
72 LONG_PTR lp = GetWindowLongPtr(hwnd, DWLP_USER); 79 LONG_PTR lp = GetWindowLongPtr(hwnd, DWLP_USER);
73 win = reinterpret_cast<DisconnectWindowWin*>(lp); 80 win = reinterpret_cast<DisconnectWindowWin*>(lp);
74 } 81 }
75 if (win == NULL) 82 if (win == NULL)
76 return FALSE; 83 return FALSE;
77 return win->OnDialogMessage(hwnd, msg, wParam, lParam); 84 return win->OnDialogMessage(hwnd, msg, wParam, lParam);
78 } 85 }
79 86
80 BOOL DisconnectWindowWin::OnDialogMessage(HWND hwnd, UINT msg, 87 BOOL DisconnectWindowWin::OnDialogMessage(HWND hwnd, UINT msg,
81 WPARAM wParam, LPARAM lParam) { 88 WPARAM wParam, LPARAM lParam) {
82 switch (msg) { 89 switch (msg) {
83 case WM_INITDIALOG: 90 case WM_INITDIALOG:
84 { 91 {
85 // Update UI string placeholders with actual strings. 92 // Update UI string placeholders with actual strings.
86 std::wstring w_title = UTF8ToWide(kTitle); 93 std::wstring w_title = UTF8ToWide(kTitle);
87 SetWindowText(hwnd, w_title.c_str()); 94 SetWindowText(hwnd, w_title.c_str());
88 95
89 HWND hwndButton = GetDlgItem(hwnd, IDC_DISCONNECT); 96 SetDisconnectButtonText(hwnd);
90 CHECK(hwndButton);
91 std::wstring w_button = UTF8ToWide(kDisconnectButton);
92 w_button += UTF8ToWide(kDisconnectKeysWin);
93 SetWindowText(hwndButton, w_button.c_str());
94 97
95 HWND hwndSharingWith = GetDlgItem(hwnd, IDC_DISCONNECT_SHARINGWITH); 98 HWND hwndSharingWith = GetDlgItem(hwnd, IDC_DISCONNECT_SHARINGWITH);
96 CHECK(hwndSharingWith); 99 CHECK(hwndSharingWith);
97 std::wstring w_sharing = UTF8ToWide(kSharingWith); 100 std::wstring w_sharing = UTF8ToWide(kSharingWith);
98 SetWindowText(hwndSharingWith, w_sharing.c_str()); 101 SetWindowText(hwndSharingWith, w_sharing.c_str());
99 102
100 // Update username in dialog. 103 // Update username in dialog.
101 HWND hwndUsername = GetDlgItem(hwnd, IDC_DISCONNECT_USERNAME); 104 HWND hwndUsername = GetDlgItem(hwnd, IDC_DISCONNECT_USERNAME);
102 CHECK(hwndUsername); 105 CHECK(hwndUsername);
103 std::wstring w_username = UTF8ToWide(username_); 106 std::wstring w_username = UTF8ToWide(username_);
104 SetWindowText(hwndUsername, w_username.c_str()); 107 SetWindowText(hwndUsername, w_username.c_str());
105 } 108 }
106 return TRUE; 109 return TRUE;
110 case WM_HOTKEY:
111 {
112 ShutdownHost();
113 EndDialog(0);
114 }
115 return TRUE;
107 case WM_CLOSE: 116 case WM_CLOSE:
108 // Ignore close messages. 117 // Ignore close messages.
109 return TRUE; 118 return TRUE;
110 case WM_DESTROY: 119 case WM_DESTROY:
111 // Ensure we don't try to use the HWND anymore. 120 // Ensure we don't try to use the HWND anymore.
112 hwnd_ = NULL; 121 hwnd_ = NULL;
113 return TRUE; 122 return TRUE;
114 case WM_COMMAND: 123 case WM_COMMAND:
115 switch (LOWORD(wParam)) { 124 switch (LOWORD(wParam)) {
116 case IDC_DISCONNECT: 125 case IDC_DISCONNECT:
117 { 126 {
118 CHECK(host_); 127 ShutdownHost();
119 host_->Shutdown(NULL); 128 EndDialog(LOWORD(wParam));
120 ::EndDialog(hwnd, LOWORD(wParam));
121 hwnd_ = NULL;
122 } 129 }
123 return TRUE; 130 return TRUE;
124 } 131 }
125 } 132 }
126 return FALSE; 133 return FALSE;
127 } 134 }
128 135
129 void DisconnectWindowWin::Show(ChromotingHost* host, 136 void DisconnectWindowWin::Show(ChromotingHost* host,
130 const std::string& username) { 137 const std::string& username) {
131 host_ = host; 138 host_ = host;
132 username_ = username; 139 username_ = username;
133 140
134 CHECK(!hwnd_); 141 CHECK(!hwnd_);
135 hwnd_ = CreateDialogParam(g_hModule, MAKEINTRESOURCE(IDD_DISCONNECT), NULL, 142 hwnd_ = CreateDialogParam(g_hModule, MAKEINTRESOURCE(IDD_DISCONNECT), NULL,
136 (DLGPROC)DialogProc, (LPARAM)this); 143 (DLGPROC)DialogProc, (LPARAM)this);
137 if (!hwnd_) { 144 if (!hwnd_) {
138 LOG(ERROR) << "Unable to create Disconnect dialog for remoting."; 145 LOG(ERROR) << "Unable to create Disconnect dialog for remoting.";
139 return; 146 return;
140 } 147 }
141 148
142 ShowWindow(hwnd_, SW_SHOW); 149 ShowWindow(hwnd_, SW_SHOW);
150
151 // Set up handler for Ctrl-Alt-Esc shortcut.
152 if (!has_hotkey_) {
153 RegisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID, MOD_ALT | MOD_CONTROL,
154 VK_ESCAPE);
155 has_hotkey_ = true;
Jamie 2011/08/12 00:00:33 Surely this depends on the return value of Registe
garykac 2011/08/12 00:23:16 Surely I shouldn't be writing code today... :-(
156 }
157 SetDisconnectButtonText(hwnd_);
158 }
159
160 void DisconnectWindowWin::ShutdownHost() {
161 CHECK(host_);
162 host_->Shutdown(NULL);
163 }
164
165 void DisconnectWindowWin::SetDisconnectButtonText(HWND hwnd) {
166 HWND hwndButton = GetDlgItem(hwnd, IDC_DISCONNECT);
167 CHECK(hwndButton);
168 std::wstring w_button = UTF8ToWide(kDisconnectButton);
169 if (has_hotkey_)
170 w_button += UTF8ToWide(kDisconnectKeysWin);
171 SetWindowText(hwndButton, w_button.c_str());
143 } 172 }
144 173
145 void DisconnectWindowWin::Hide() { 174 void DisconnectWindowWin::Hide() {
146 EndDialog(); 175 EndDialog(0);
147 } 176 }
148 177
149 void DisconnectWindowWin::EndDialog() { 178 void DisconnectWindowWin::EndDialog(int result) {
179 RemoveKeyHandler();
150 if (hwnd_) { 180 if (hwnd_) {
151 ::EndDialog(hwnd_, 0); 181 ::EndDialog(hwnd_, result);
152 hwnd_ = NULL; 182 hwnd_ = NULL;
153 } 183 }
154 } 184 }
155 185
186 void DisconnectWindowWin::RemoveKeyHandler() {
187 UnregisterHotKey(hwnd_, DISCONNECT_HOTKEY_ID);
Jamie 2011/08/12 00:00:33 Should be conditional on has_hotkey_
garykac 2011/08/12 00:23:16 Done.
188 has_hotkey_ = false;
189 }
190
156 DisconnectWindow* DisconnectWindow::Create() { 191 DisconnectWindow* DisconnectWindow::Create() {
157 return new DisconnectWindowWin; 192 return new DisconnectWindowWin;
158 } 193 }
159 194
160 } // namespace remoting 195 } // namespace remoting
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698