blob: 826781d67a0f14558ddc1e4b5191e2b3d3e2d88e [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * 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
19#include <vncviewer/OptionsDialog.h>
20#include <vncviewer/CConn.h>
21#include <vncviewer/resource.h>
22#include <rfb_win32/Registry.h>
23#include <rfb_win32/MsgBox.h>
24#include <rfb_win32/OSVersion.h>
25#include <rfb/encodings.h>
26#include <rfb/CConnection.h>
27#include <commdlg.h>
28#include <rfb/LogWriter.h>
29
30using namespace rfb;
31using namespace rfb::win32;
32
33static LogWriter vlog("Options");
34
35
36struct OptionsInfo {
37 CConn* view;
38 CConnOptions options;
39};
40
41
42OptionsDialog rfb::win32::OptionsDialog::global;
43
44
45class ViewerOptions : public PropSheet {
46public:
47 ViewerOptions(OptionsInfo& info_, std::list<PropSheetPage*> pages)
48 : PropSheet(GetModuleHandle(0),
49 info_.view ? _T("VNC Viewer Options") : _T("VNC Viewer Defaults"), pages),
50 info(info_), changed(false) {
51 }
52 ~ViewerOptions() {
53 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) {
87 case encodingTight: setItemChecked(IDC_ENCODING_TIGHT, true); break;
88 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 }
92 setItemChecked(IDC_CUSTOM_COMPRESSLEVEL, dlg->options.customCompressLevel);
93 setItemInt(IDC_COMPRESSLEVEL, dlg->options.compressLevel);
94 setItemChecked(IDC_ALLOW_JPEG, !dlg->options.noJpeg);
95 setItemInt(IDC_QUALITYLEVEL, dlg->options.qualityLevel);
96 onCommand(IDC_ENCODING_AUTO, 0 /* ? */); // Force enableItem status to refresh
97 onCommand(IDC_CUSTOM_COMPRESSLEVEL, 0 /* ? */); // Force enableItem status to refresh
98 onCommand(IDC_ALLOW_JPEG, 0 /* ? */); // Force enableItem status to refresh
99 }
100 virtual bool onOk() {
101 dlg->options.autoSelect = isItemChecked(IDC_ENCODING_AUTO);
102 dlg->options.fullColour = isItemChecked(IDC_FORMAT_FULLCOLOUR);
103 dlg->options.customCompressLevel = isItemChecked(IDC_CUSTOM_COMPRESSLEVEL);
104 dlg->options.compressLevel = getItemInt(IDC_COMPRESSLEVEL);
105 dlg->options.noJpeg = !isItemChecked(IDC_ALLOW_JPEG);
106 dlg->options.qualityLevel = getItemInt(IDC_QUALITYLEVEL);
107 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;
113 dlg->options.preferredEncoding = encodingTight;
114 if (isItemChecked(IDC_ENCODING_ZRLE))
115 dlg->options.preferredEncoding = encodingZRLE;
116 if (isItemChecked(IDC_ENCODING_HEXTILE))
117 dlg->options.preferredEncoding = encodingHextile;
118 if (isItemChecked(IDC_ENCODING_RAW))
119 dlg->options.preferredEncoding = encodingRaw;
120 ((ViewerOptions*)propSheet)->setChanged();
121 return true;
122 }
123 virtual bool onCommand(int id, int cmd) {
124 if (id == IDC_ENCODING_AUTO) {
125 bool ok = !isItemChecked(IDC_ENCODING_AUTO);
126 enableItem(IDC_ENCODING_TIGHT, ok);
127 enableItem(IDC_ENCODING_ZRLE, ok);
128 enableItem(IDC_ENCODING_HEXTILE, ok);
129 enableItem(IDC_ENCODING_RAW, ok);
130 enableItem(IDC_FORMAT_FULLCOLOUR, ok);
131 enableItem(IDC_FORMAT_MEDIUMCOLOUR, ok);
132 enableItem(IDC_FORMAT_LOWCOLOUR, ok);
133 enableItem(IDC_FORMAT_VERYLOWCOLOUR, ok);
134 return true;
135 }
136 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 }
144 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);
164 setItemChecked(IDC_AUTO_RECONNECT, dlg->options.autoReconnect);
165 setItemChecked(IDC_SHOW_TOOLBAR, dlg->options.showToolbar);
george8257160b02006-09-11 04:11:51 +0000166 char scale_values[10][20] = {
167 "10","25","50","75","90","100","125","150","200","Auto"
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +0000168 };
169 HWND hScaleCombo = GetDlgItem(handle, IDC_COMBO_SCALE);
george8257160b02006-09-11 04:11:51 +0000170 for (int i = 0; i <= 9; i++) {
Constantin Kaplinsky1ae2eb02006-05-26 05:24:24 +0000171 SendMessage(hScaleCombo, CB_INSERTSTRING,
172 (WPARAM)i, (LPARAM)(int FAR*)scale_values[i]);
173 }
george8204a77712006-05-29 14:18:14 +0000174 if (dlg->options.autoScaling) {
175 SetDlgItemText(handle, IDC_COMBO_SCALE, "Auto");
176 } else {
177 SetDlgItemInt(handle, IDC_COMBO_SCALE, dlg->options.scale, FALSE);
178 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000179 }
180 virtual bool onOk() {
181 dlg->options.shared = isItemChecked(IDC_CONN_SHARED);
182 dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN);
183 dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR);
184 dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE);
185 dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3);
186 dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL);
187 dlg->options.autoReconnect = isItemChecked(IDC_AUTO_RECONNECT);
188 dlg->options.showToolbar = isItemChecked(IDC_SHOW_TOOLBAR);
george8204a77712006-05-29 14:18:14 +0000189 int s = GetDlgItemInt(handle, IDC_COMBO_SCALE, NULL, FALSE);
190 if (s > 0) {
191 dlg->options.scale = s;
192 dlg->options.autoScaling = false;
george8204a77712006-05-29 14:18:14 +0000193 } else {
194 char scaleStr[20];
195 GetDlgItemText(handle, IDC_COMBO_SCALE, scaleStr, 20);
196 if (strcmp(scaleStr, "Auto") == 0) {
197 dlg->options.autoScaling = true;
george8204a77712006-05-29 14:18:14 +0000198 }
199 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000200 ((ViewerOptions*)propSheet)->setChanged();
201 return true;
202 }
george82a9a33872007-04-30 11:37:36 +0000203 virtual bool onCommand(int id, int cmd) {
204 if (id == IDC_COMBO_SCALE) {
205 if (cmd == CBN_SELENDOK || cmd == CBN_EDITCHANGE) {
206 char scaleStr[20];
207 if (cmd == CBN_SELENDOK) {
208 HWND handleComboScale = GetDlgItem(handle, IDC_COMBO_SCALE);
209 int index = SendMessage(handleComboScale, CB_GETCURSEL, 0, 0);
210 SendMessage(handleComboScale, CB_GETLBTEXT, (WPARAM)index, (LPARAM)scaleStr);
211 } else {
212 GetDlgItemText(handle, IDC_COMBO_SCALE, scaleStr, 20);
213 }
george82a9a33872007-04-30 11:37:36 +0000214 return true;
215 }
216 }
217 return false;
218 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000219protected:
220 OptionsInfo* dlg;
221};
222
223class InputsPage : public PropSheetPage {
224public:
225 InputsPage(OptionsInfo* dlg_)
226 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_INPUTS)), dlg(dlg_) {
227 }
228 virtual void initDialog() {
229 setItemChecked(IDC_SEND_POINTER, dlg->options.sendPtrEvents);
230 setItemChecked(IDC_SEND_KEYS, dlg->options.sendKeyEvents);
231 setItemChecked(IDC_CLIENT_CUTTEXT, dlg->options.clientCutText);
232 setItemChecked(IDC_SERVER_CUTTEXT, dlg->options.serverCutText);
233 setItemChecked(IDC_DISABLE_WINKEYS, dlg->options.disableWinKeys && !osVersion.isPlatformWindows);
234 enableItem(IDC_DISABLE_WINKEYS, !osVersion.isPlatformWindows);
235 setItemChecked(IDC_EMULATE3, dlg->options.emulate3);
236 setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0);
237
238 // Populate the Menu Key tab
239 HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY);
240 SendMessage(menuKey, CB_RESETCONTENT, 0, 0);
241 SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none"));
242 if (!dlg->options.menuKey)
243 SendMessage(menuKey, CB_SETCURSEL, 0, 0);
244 for (int i=0; i<12; i++) {
245 TCHAR buf[4];
246 _stprintf(buf, _T("F%d"), i+1);
247 int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf);
248 if (i == (dlg->options.menuKey - VK_F1))
249 SendMessage(menuKey, CB_SETCURSEL, index, 0);
250 }
251 }
252 virtual bool onOk() {
253 dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER);
254 dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS);
255 dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT);
256 dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT);
257 dlg->options.disableWinKeys = isItemChecked(IDC_DISABLE_WINKEYS);
258 dlg->options.emulate3 = isItemChecked(IDC_EMULATE3);
259 dlg->options.pointerEventInterval =
260 isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0;
261
262 HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY);
263 int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0);
264 TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1);
265 SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf);
266 if (_tcscmp(keyName.buf, _T("none")) == 0)
267 dlg->options.setMenuKey("");
268 else
269 dlg->options.setMenuKey(CStr(keyName.buf));
270
271 ((ViewerOptions*)propSheet)->setChanged();
272 return true;
273 }
274protected:
275 OptionsInfo* dlg;
276};
277
278class DefaultsPage : public PropSheetPage {
279public:
280 DefaultsPage(OptionsInfo* dlg_)
281 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) {
282 }
283 virtual void initDialog() {
284 enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf);
285 enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf);
286 }
287 virtual bool onCommand(int id, int cmd) {
288 switch (id) {
289 case IDC_LOAD_DEFAULTS:
290 dlg->options = CConnOptions();
291 break;
292 case IDC_SAVE_DEFAULTS:
293 propSheet->commitPages();
294 dlg->options.writeDefaults();
295 break;
296 case IDC_LOAD_CONFIG:
297 dlg->options.readFromFile(dlg->options.configFileName.buf);
298 break;
299 case IDC_SAVE_CONFIG:
300 propSheet->commitPages();
301 dlg->options.writeToFile(dlg->options.configFileName.buf);
302 MsgBox(handle, _T("Options saved successfully"),
303 MB_OK | MB_ICONINFORMATION);
304 return 0;
305 case IDC_SAVE_CONFIG_AS:
306 propSheet->commitPages();
307 // Get a filename to save to
308 TCHAR newFilename[4096];
309 TCHAR currentDir[4096];
310 if (dlg->options.configFileName.buf)
311 _tcscpy(newFilename, TStr(dlg->options.configFileName.buf));
312 else
313 newFilename[0] = 0;
314 OPENFILENAME ofn;
315 memset(&ofn, 0, sizeof(ofn));
316#ifdef OPENFILENAME_SIZE_VERSION_400
317 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
318#else
319 ofn.lStructSize = sizeof(ofn);
320#endif
321 ofn.hwndOwner = handle;
322 ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000");
323 ofn.lpstrFile = newFilename;
324 currentDir[0] = 0;
325 GetCurrentDirectory(4096, currentDir);
326 ofn.lpstrInitialDir = currentDir;
327 ofn.nMaxFile = 4096;
328 ofn.lpstrDefExt = _T(".vnc");
329 ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
330 if (!GetSaveFileName(&ofn)) {
331 if (CommDlgExtendedError())
332 throw rdr::Exception("GetSaveFileName failed");
333 return 0;
334 }
335
336 // Save the Options
337 dlg->options.writeToFile(CStr(newFilename));
338 MsgBox(handle, _T("Options saved successfully"),
339 MB_OK | MB_ICONINFORMATION);
340 return 0;
341 };
342 propSheet->reInitPages();
343 return true;
344 }
345protected:
346 OptionsInfo* dlg;
347};
348
349
350OptionsDialog::OptionsDialog() : visible(false) {
351}
352
353bool OptionsDialog::showDialog(CConn* view, bool capture) {
354 if (visible) return false;
355 visible = true;
356
357 // Grab the current properties
358 OptionsInfo info;
359 if (view)
360 info.options = view->getOptions();
361 info.view = view;
362
363 // Build a list of pages to display
364 std::list<PropSheetPage*> pages;
365 FormatPage formatPage(&info); pages.push_back(&formatPage);
366 InputsPage inputsPage(&info); pages.push_back(&inputsPage);
367 MiscPage miscPage(&info); pages.push_back(&miscPage);
368 DefaultsPage defPage(&info); if (view) pages.push_back(&defPage);
369
370 // Show the property sheet
371 ViewerOptions dialog(info, pages);
372 dialog.showPropSheet(view && view->getWindow() ? view->getWindow()->getHandle() : 0,
373 false, false, capture);
374
375 visible = false;
376 return dialog.changed;
377}