blob: c326645e3b5eaa929b4cfa5e35e37c96e5ec50fa [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 }
george82a9a33872007-04-30 11:37:36 +0000179 HWND hScaleFilterCombo = GetDlgItem(handle, IDC_COMBO_SCALE_FILTER);
180 SendMessage(hScaleFilterCombo, CB_RESETCONTENT, 0, 0);
181 ScaleFilters scaleFilters;
182 for (i = 0; i <= rfb::scaleFilterMaxNumber; i++) {
183 SendMessage(hScaleFilterCombo, CB_ADDSTRING, (WPARAM)0, (LPARAM)(int FAR*)scaleFilters[i].name);
184 }
185 SendMessage(hScaleFilterCombo, CB_SETCURSEL, (WPARAM)dlg->options.scaleFilter, (LPARAM)0);
186 if (dlg->options.scale == 100 && !dlg->options.autoScaling) enableItem(IDC_COMBO_SCALE_FILTER, 0);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000187 }
188 virtual bool onOk() {
189 dlg->options.shared = isItemChecked(IDC_CONN_SHARED);
190 dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN);
191 dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR);
192 dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE);
193 dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3);
194 dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL);
195 dlg->options.autoReconnect = isItemChecked(IDC_AUTO_RECONNECT);
196 dlg->options.showToolbar = isItemChecked(IDC_SHOW_TOOLBAR);
george8204a77712006-05-29 14:18:14 +0000197 int s = GetDlgItemInt(handle, IDC_COMBO_SCALE, NULL, FALSE);
198 if (s > 0) {
199 dlg->options.scale = s;
200 dlg->options.autoScaling = false;
george8204a77712006-05-29 14:18:14 +0000201 } else {
202 char scaleStr[20];
203 GetDlgItemText(handle, IDC_COMBO_SCALE, scaleStr, 20);
204 if (strcmp(scaleStr, "Auto") == 0) {
205 dlg->options.autoScaling = true;
george8204a77712006-05-29 14:18:14 +0000206 }
207 }
george82a9a33872007-04-30 11:37:36 +0000208 dlg->options.scaleFilter = SendMessage(GetDlgItem(handle, IDC_COMBO_SCALE_FILTER), CB_GETCURSEL, 0, 0);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000209 ((ViewerOptions*)propSheet)->setChanged();
210 return true;
211 }
george82a9a33872007-04-30 11:37:36 +0000212 virtual bool onCommand(int id, int cmd) {
213 if (id == IDC_COMBO_SCALE) {
214 if (cmd == CBN_SELENDOK || cmd == CBN_EDITCHANGE) {
215 char scaleStr[20];
216 if (cmd == CBN_SELENDOK) {
217 HWND handleComboScale = GetDlgItem(handle, IDC_COMBO_SCALE);
218 int index = SendMessage(handleComboScale, CB_GETCURSEL, 0, 0);
219 SendMessage(handleComboScale, CB_GETLBTEXT, (WPARAM)index, (LPARAM)scaleStr);
220 } else {
221 GetDlgItemText(handle, IDC_COMBO_SCALE, scaleStr, 20);
222 }
223 if (strcmp(scaleStr, "100") == 0) enableItem(IDC_COMBO_SCALE_FILTER, 0);
224 else enableItem(IDC_COMBO_SCALE_FILTER, 1);
225 return true;
226 }
227 }
228 return false;
229 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000230protected:
231 OptionsInfo* dlg;
232};
233
234class InputsPage : public PropSheetPage {
235public:
236 InputsPage(OptionsInfo* dlg_)
237 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_INPUTS)), dlg(dlg_) {
238 }
239 virtual void initDialog() {
240 setItemChecked(IDC_SEND_POINTER, dlg->options.sendPtrEvents);
241 setItemChecked(IDC_SEND_KEYS, dlg->options.sendKeyEvents);
242 setItemChecked(IDC_CLIENT_CUTTEXT, dlg->options.clientCutText);
243 setItemChecked(IDC_SERVER_CUTTEXT, dlg->options.serverCutText);
244 setItemChecked(IDC_DISABLE_WINKEYS, dlg->options.disableWinKeys && !osVersion.isPlatformWindows);
245 enableItem(IDC_DISABLE_WINKEYS, !osVersion.isPlatformWindows);
246 setItemChecked(IDC_EMULATE3, dlg->options.emulate3);
247 setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0);
248
249 // Populate the Menu Key tab
250 HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY);
251 SendMessage(menuKey, CB_RESETCONTENT, 0, 0);
252 SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none"));
253 if (!dlg->options.menuKey)
254 SendMessage(menuKey, CB_SETCURSEL, 0, 0);
255 for (int i=0; i<12; i++) {
256 TCHAR buf[4];
257 _stprintf(buf, _T("F%d"), i+1);
258 int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf);
259 if (i == (dlg->options.menuKey - VK_F1))
260 SendMessage(menuKey, CB_SETCURSEL, index, 0);
261 }
262 }
263 virtual bool onOk() {
264 dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER);
265 dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS);
266 dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT);
267 dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT);
268 dlg->options.disableWinKeys = isItemChecked(IDC_DISABLE_WINKEYS);
269 dlg->options.emulate3 = isItemChecked(IDC_EMULATE3);
270 dlg->options.pointerEventInterval =
271 isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0;
272
273 HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY);
274 int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0);
275 TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1);
276 SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf);
277 if (_tcscmp(keyName.buf, _T("none")) == 0)
278 dlg->options.setMenuKey("");
279 else
280 dlg->options.setMenuKey(CStr(keyName.buf));
281
282 ((ViewerOptions*)propSheet)->setChanged();
283 return true;
284 }
285protected:
286 OptionsInfo* dlg;
287};
288
289class DefaultsPage : public PropSheetPage {
290public:
291 DefaultsPage(OptionsInfo* dlg_)
292 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) {
293 }
294 virtual void initDialog() {
295 enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf);
296 enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf);
297 }
298 virtual bool onCommand(int id, int cmd) {
299 switch (id) {
300 case IDC_LOAD_DEFAULTS:
301 dlg->options = CConnOptions();
302 break;
303 case IDC_SAVE_DEFAULTS:
304 propSheet->commitPages();
305 dlg->options.writeDefaults();
306 break;
307 case IDC_LOAD_CONFIG:
308 dlg->options.readFromFile(dlg->options.configFileName.buf);
309 break;
310 case IDC_SAVE_CONFIG:
311 propSheet->commitPages();
312 dlg->options.writeToFile(dlg->options.configFileName.buf);
313 MsgBox(handle, _T("Options saved successfully"),
314 MB_OK | MB_ICONINFORMATION);
315 return 0;
316 case IDC_SAVE_CONFIG_AS:
317 propSheet->commitPages();
318 // Get a filename to save to
319 TCHAR newFilename[4096];
320 TCHAR currentDir[4096];
321 if (dlg->options.configFileName.buf)
322 _tcscpy(newFilename, TStr(dlg->options.configFileName.buf));
323 else
324 newFilename[0] = 0;
325 OPENFILENAME ofn;
326 memset(&ofn, 0, sizeof(ofn));
327#ifdef OPENFILENAME_SIZE_VERSION_400
328 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
329#else
330 ofn.lStructSize = sizeof(ofn);
331#endif
332 ofn.hwndOwner = handle;
333 ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000");
334 ofn.lpstrFile = newFilename;
335 currentDir[0] = 0;
336 GetCurrentDirectory(4096, currentDir);
337 ofn.lpstrInitialDir = currentDir;
338 ofn.nMaxFile = 4096;
339 ofn.lpstrDefExt = _T(".vnc");
340 ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
341 if (!GetSaveFileName(&ofn)) {
342 if (CommDlgExtendedError())
343 throw rdr::Exception("GetSaveFileName failed");
344 return 0;
345 }
346
347 // Save the Options
348 dlg->options.writeToFile(CStr(newFilename));
349 MsgBox(handle, _T("Options saved successfully"),
350 MB_OK | MB_ICONINFORMATION);
351 return 0;
352 };
353 propSheet->reInitPages();
354 return true;
355 }
356protected:
357 OptionsInfo* dlg;
358};
359
360
361OptionsDialog::OptionsDialog() : visible(false) {
362}
363
364bool OptionsDialog::showDialog(CConn* view, bool capture) {
365 if (visible) return false;
366 visible = true;
367
368 // Grab the current properties
369 OptionsInfo info;
370 if (view)
371 info.options = view->getOptions();
372 info.view = view;
373
374 // Build a list of pages to display
375 std::list<PropSheetPage*> pages;
376 FormatPage formatPage(&info); pages.push_back(&formatPage);
377 InputsPage inputsPage(&info); pages.push_back(&inputsPage);
378 MiscPage miscPage(&info); pages.push_back(&miscPage);
379 DefaultsPage defPage(&info); if (view) pages.push_back(&defPage);
380
381 // Show the property sheet
382 ViewerOptions dialog(info, pages);
383 dialog.showPropSheet(view && view->getWindow() ? view->getWindow()->getHandle() : 0,
384 false, false, capture);
385
386 visible = false;
387 return dialog.changed;
388}