blob: 93e033aa82d1d7edc09e823f28790e826413a694 [file] [log] [blame]
Constantin Kaplinskyac306682006-05-16 08:48:31 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000019#include <vncviewer/OptionsDialog.h>
Constantin Kaplinskyac306682006-05-16 08:48:31 +000020#include <vncviewer/CConn.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000021#include <vncviewer/resource.h>
22#include <rfb_win32/Registry.h>
Constantin Kaplinskyac306682006-05-16 08:48:31 +000023#include <rfb_win32/MsgBox.h>
24#include <rfb_win32/OSVersion.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000025#include <rfb/encodings.h>
26#include <rfb/CConnection.h>
Constantin Kaplinskyac306682006-05-16 08:48:31 +000027#include <commdlg.h>
28#include <rfb/LogWriter.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000029
30using namespace rfb;
31using namespace rfb::win32;
32
33static LogWriter vlog("Options");
34
35
36struct OptionsInfo {
Constantin Kaplinskyac306682006-05-16 08:48:31 +000037 CConn* view;
38 CConnOptions options;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000039};
40
41
42OptionsDialog rfb::win32::OptionsDialog::global;
43
44
Constantin Kaplinskyac306682006-05-16 08:48:31 +000045class ViewerOptions : public PropSheet {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000046public:
Constantin Kaplinskyac306682006-05-16 08:48:31 +000047 ViewerOptions(OptionsInfo& info_, std::list<PropSheetPage*> pages)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000048 : PropSheet(GetModuleHandle(0),
49 info_.view ? _T("VNC Viewer Options") : _T("VNC Viewer Defaults"), pages),
50 info(info_), changed(false) {
51 }
Constantin Kaplinskyac306682006-05-16 08:48:31 +000052 ~ViewerOptions() {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000053 if (changed) {
54 if (info.view)
55 // Apply the settings to the supplied session object
56 info.view->applyOptions(info.options);
57 else {
58 // Commit the settings to the user's registry area
59 info.options.writeDefaults();
60 }
61 }
62 }
63
64 void setChanged() {changed = true;}
65
66 bool changed;
67 OptionsInfo& info;
68};
69
70
71class FormatPage : public PropSheetPage {
72public:
73 FormatPage(OptionsInfo* dlg_)
74 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_FORMAT)), dlg(dlg_) {
75 }
76 virtual void initDialog() {
77 setItemChecked(IDC_ENCODING_AUTO, dlg->options.autoSelect);
78 setItemChecked(IDC_FORMAT_FULLCOLOUR, dlg->options.fullColour);
79 if (!dlg->options.fullColour) {
80 switch (dlg->options.lowColourLevel) {
81 case 0: setItemChecked(IDC_FORMAT_VERYLOWCOLOUR, true); break;
82 case 1: setItemChecked(IDC_FORMAT_LOWCOLOUR, true); break;
83 case 2: setItemChecked(IDC_FORMAT_MEDIUMCOLOUR, true); break;
84 }
85 }
86 switch (dlg->options.preferredEncoding) {
Peter Åstranda2cdd2b2004-12-19 16:14:47 +000087 case encodingTight: setItemChecked(IDC_ENCODING_TIGHT, true); break;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000088 case encodingZRLE: setItemChecked(IDC_ENCODING_ZRLE, true); break;
89 case encodingHextile: setItemChecked(IDC_ENCODING_HEXTILE, true); break;
90 case encodingRaw: setItemChecked(IDC_ENCODING_RAW, true); break;
91 }
Peter Åstrand7489c2f2004-12-29 11:07:20 +000092 setItemChecked(IDC_CUSTOM_COMPRESSLEVEL, dlg->options.customCompressLevel);
Peter Åstrand365427a2004-12-29 10:59:03 +000093 setItemInt(IDC_COMPRESSLEVEL, dlg->options.compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +000094 setItemChecked(IDC_ALLOW_JPEG, !dlg->options.noJpeg);
Peter Åstrand142e84d2004-12-28 15:27:28 +000095 setItemInt(IDC_QUALITYLEVEL, dlg->options.qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000096 onCommand(IDC_ENCODING_AUTO, 0 /* ? */); // Force enableItem status to refresh
Peter Åstrand81396de2004-12-29 11:14:34 +000097 onCommand(IDC_CUSTOM_COMPRESSLEVEL, 0 /* ? */); // Force enableItem status to refresh
98 onCommand(IDC_ALLOW_JPEG, 0 /* ? */); // Force enableItem status to refresh
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000099 }
100 virtual bool onOk() {
101 dlg->options.autoSelect = isItemChecked(IDC_ENCODING_AUTO);
102 dlg->options.fullColour = isItemChecked(IDC_FORMAT_FULLCOLOUR);
Peter Åstrand7489c2f2004-12-29 11:07:20 +0000103 dlg->options.customCompressLevel = isItemChecked(IDC_CUSTOM_COMPRESSLEVEL);
Peter Åstrand365427a2004-12-29 10:59:03 +0000104 dlg->options.compressLevel = getItemInt(IDC_COMPRESSLEVEL);
Peter Åstrand0b870262004-12-28 15:55:46 +0000105 dlg->options.noJpeg = !isItemChecked(IDC_ALLOW_JPEG);
Peter Åstrand142e84d2004-12-28 15:27:28 +0000106 dlg->options.qualityLevel = getItemInt(IDC_QUALITYLEVEL);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000107 if (isItemChecked(IDC_FORMAT_VERYLOWCOLOUR))
108 dlg->options.lowColourLevel = 0;
109 if (isItemChecked(IDC_FORMAT_LOWCOLOUR))
110 dlg->options.lowColourLevel = 1;
111 if (isItemChecked(IDC_FORMAT_MEDIUMCOLOUR))
112 dlg->options.lowColourLevel = 2;
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000113 dlg->options.preferredEncoding = encodingTight;
114 if (isItemChecked(IDC_ENCODING_ZRLE))
115 dlg->options.preferredEncoding = encodingZRLE;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000116 if (isItemChecked(IDC_ENCODING_HEXTILE))
117 dlg->options.preferredEncoding = encodingHextile;
118 if (isItemChecked(IDC_ENCODING_RAW))
119 dlg->options.preferredEncoding = encodingRaw;
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000120 ((ViewerOptions*)propSheet)->setChanged();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000121 return true;
122 }
123 virtual bool onCommand(int id, int cmd) {
124 if (id == IDC_ENCODING_AUTO) {
125 bool ok = !isItemChecked(IDC_ENCODING_AUTO);
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000126 enableItem(IDC_ENCODING_TIGHT, ok);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000127 enableItem(IDC_ENCODING_ZRLE, ok);
128 enableItem(IDC_ENCODING_HEXTILE, ok);
129 enableItem(IDC_ENCODING_RAW, ok);
Peter Åstrand1e6d8982004-12-28 15:07:38 +0000130 enableItem(IDC_FORMAT_FULLCOLOUR, ok);
131 enableItem(IDC_FORMAT_MEDIUMCOLOUR, ok);
132 enableItem(IDC_FORMAT_LOWCOLOUR, ok);
133 enableItem(IDC_FORMAT_VERYLOWCOLOUR, ok);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000134 return true;
135 }
Peter Åstrand81396de2004-12-29 11:14:34 +0000136 if (id == IDC_CUSTOM_COMPRESSLEVEL) {
137 enableItem(IDC_COMPRESSLEVEL, isItemChecked(IDC_CUSTOM_COMPRESSLEVEL));
138 return true;
139 }
140 if (id == IDC_ALLOW_JPEG) {
141 enableItem(IDC_QUALITYLEVEL, isItemChecked(IDC_ALLOW_JPEG));
142 return true;
143 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000144 return false;
145 }
146protected:
147 OptionsInfo* dlg;
148};
149
150class MiscPage : public PropSheetPage {
151public:
152 MiscPage(OptionsInfo* dlg_)
153 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_MISC)), dlg(dlg_) {
154 }
155 virtual void initDialog() {
156 setItemChecked(IDC_CONN_SHARED, dlg->options.shared);
157 enableItem(IDC_CONN_SHARED, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
158 setItemChecked(IDC_FULL_SCREEN, dlg->options.fullScreen);
159 setItemChecked(IDC_LOCAL_CURSOR, dlg->options.useLocalCursor);
160 setItemChecked(IDC_DESKTOP_RESIZE, dlg->options.useDesktopResize);
161 enableItem(IDC_PROTOCOL_3_3, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
162 setItemChecked(IDC_PROTOCOL_3_3, dlg->options.protocol3_3);
163 setItemChecked(IDC_ACCEPT_BELL, dlg->options.acceptBell);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000164 setItemChecked(IDC_AUTO_RECONNECT, dlg->options.autoReconnect);
george82564dd132005-12-01 15:48:39 +0000165 setItemChecked(IDC_SHOW_TOOLBAR, dlg->options.showToolbar);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000166 }
167 virtual bool onOk() {
168 dlg->options.shared = isItemChecked(IDC_CONN_SHARED);
169 dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN);
170 dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR);
171 dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE);
172 dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3);
173 dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000174 dlg->options.autoReconnect = isItemChecked(IDC_AUTO_RECONNECT);
george82564dd132005-12-01 15:48:39 +0000175 dlg->options.showToolbar = isItemChecked(IDC_SHOW_TOOLBAR);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000176 ((ViewerOptions*)propSheet)->setChanged();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000177 return true;
178 }
179protected:
180 OptionsInfo* dlg;
181};
182
183class InputsPage : public PropSheetPage {
184public:
185 InputsPage(OptionsInfo* dlg_)
186 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_INPUTS)), dlg(dlg_) {
187 }
188 virtual void initDialog() {
189 setItemChecked(IDC_SEND_POINTER, dlg->options.sendPtrEvents);
190 setItemChecked(IDC_SEND_KEYS, dlg->options.sendKeyEvents);
Peter Åstrand57dcc452005-01-28 14:52:50 +0000191 setItemChecked(IDC_SEND_SYSKEYS, dlg->options.sendSysKeys);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000192 setItemChecked(IDC_CLIENT_CUTTEXT, dlg->options.clientCutText);
193 setItemChecked(IDC_SERVER_CUTTEXT, dlg->options.serverCutText);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000194 setItemChecked(IDC_DISABLE_WINKEYS, dlg->options.disableWinKeys && !osVersion.isPlatformWindows);
195 enableItem(IDC_DISABLE_WINKEYS, !osVersion.isPlatformWindows);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000196 setItemChecked(IDC_EMULATE3, dlg->options.emulate3);
197 setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0);
198
199 // Populate the Menu Key tab
200 HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY);
201 SendMessage(menuKey, CB_RESETCONTENT, 0, 0);
202 SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none"));
203 if (!dlg->options.menuKey)
204 SendMessage(menuKey, CB_SETCURSEL, 0, 0);
205 for (int i=0; i<12; i++) {
206 TCHAR buf[4];
207 _stprintf(buf, _T("F%d"), i+1);
208 int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf);
209 if (i == (dlg->options.menuKey - VK_F1))
210 SendMessage(menuKey, CB_SETCURSEL, index, 0);
211 }
212 }
213 virtual bool onOk() {
214 dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER);
215 dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS);
Peter Åstrand57dcc452005-01-28 14:52:50 +0000216 dlg->options.sendSysKeys = isItemChecked(IDC_SEND_SYSKEYS);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000217 dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT);
218 dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000219 dlg->options.disableWinKeys = isItemChecked(IDC_DISABLE_WINKEYS);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000220 dlg->options.emulate3 = isItemChecked(IDC_EMULATE3);
221 dlg->options.pointerEventInterval =
222 isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0;
223
224 HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY);
225 int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0);
226 TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1);
227 SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf);
228 if (_tcscmp(keyName.buf, _T("none")) == 0)
229 dlg->options.setMenuKey("");
230 else
231 dlg->options.setMenuKey(CStr(keyName.buf));
232
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000233 ((ViewerOptions*)propSheet)->setChanged();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000234 return true;
235 }
236protected:
237 OptionsInfo* dlg;
238};
239
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000240class DefaultsPage : public PropSheetPage {
241public:
242 DefaultsPage(OptionsInfo* dlg_)
243 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) {
244 }
245 virtual void initDialog() {
246 enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf);
247 enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf);
248 }
249 virtual bool onCommand(int id, int cmd) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000250 switch (id) {
251 case IDC_LOAD_DEFAULTS:
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000252 dlg->options = CConnOptions();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000253 break;
254 case IDC_SAVE_DEFAULTS:
255 propSheet->commitPages();
256 dlg->options.writeDefaults();
257 break;
258 case IDC_LOAD_CONFIG:
259 dlg->options.readFromFile(dlg->options.configFileName.buf);
260 break;
261 case IDC_SAVE_CONFIG:
262 propSheet->commitPages();
263 dlg->options.writeToFile(dlg->options.configFileName.buf);
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000264 MsgBox(handle, _T("Options saved successfully"),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000265 MB_OK | MB_ICONINFORMATION);
266 return 0;
267 case IDC_SAVE_CONFIG_AS:
268 propSheet->commitPages();
269 // Get a filename to save to
270 TCHAR newFilename[4096];
271 TCHAR currentDir[4096];
272 if (dlg->options.configFileName.buf)
273 _tcscpy(newFilename, TStr(dlg->options.configFileName.buf));
274 else
275 newFilename[0] = 0;
276 OPENFILENAME ofn;
277 memset(&ofn, 0, sizeof(ofn));
278#ifdef OPENFILENAME_SIZE_VERSION_400
279 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
280#else
281 ofn.lStructSize = sizeof(ofn);
282#endif
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000283 ofn.hwndOwner = handle;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000284 ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000");
285 ofn.lpstrFile = newFilename;
286 currentDir[0] = 0;
287 GetCurrentDirectory(4096, currentDir);
288 ofn.lpstrInitialDir = currentDir;
289 ofn.nMaxFile = 4096;
290 ofn.lpstrDefExt = _T(".vnc");
291 ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
292 if (!GetSaveFileName(&ofn)) {
293 if (CommDlgExtendedError())
294 throw rdr::Exception("GetSaveFileName failed");
295 return 0;
296 }
297
298 // Save the Options
299 dlg->options.writeToFile(CStr(newFilename));
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000300 MsgBox(handle, _T("Options saved successfully"),
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000301 MB_OK | MB_ICONINFORMATION);
302 return 0;
303 };
304 propSheet->reInitPages();
305 return true;
306 }
307protected:
308 OptionsInfo* dlg;
309};
310
311
312OptionsDialog::OptionsDialog() : visible(false) {
313}
314
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000315bool OptionsDialog::showDialog(CConn* view, bool capture) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000316 if (visible) return false;
317 visible = true;
318
319 // Grab the current properties
320 OptionsInfo info;
321 if (view)
322 info.options = view->getOptions();
323 info.view = view;
324
325 // Build a list of pages to display
326 std::list<PropSheetPage*> pages;
327 FormatPage formatPage(&info); pages.push_back(&formatPage);
328 InputsPage inputsPage(&info); pages.push_back(&inputsPage);
329 MiscPage miscPage(&info); pages.push_back(&miscPage);
330 DefaultsPage defPage(&info); if (view) pages.push_back(&defPage);
331
332 // Show the property sheet
Constantin Kaplinskyac306682006-05-16 08:48:31 +0000333 ViewerOptions dialog(info, pages);
334 dialog.showPropSheet(view && view->getWindow() ? view->getWindow()->getHandle() : 0,
335 false, false, capture);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000336
337 visible = false;
338 return dialog.changed;
339}