blob: ae3f08a6cac622448a51dc3bfe5dbee8b973df88 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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#define WIN32_LEAN_AND_MEAN
20#if (_WIN32_WINNT < 0x0400)
21#define _WIN32_WINNT 0x0400
22#endif
23#include <windows.h>
24#include <commdlg.h>
25
26#include <vncviewer/OptionsDialog.h>
27#include <vncviewer/CView.h>
28#include <vncviewer/resource.h>
29#include <rfb_win32/Registry.h>
30#include <rfb/LogWriter.h>
31#include <rfb/encodings.h>
32#include <rfb/CConnection.h>
33
34using namespace rfb;
35using namespace rfb::win32;
36
37static LogWriter vlog("Options");
38
39
40struct OptionsInfo {
41 CView* view;
42 CViewOptions options;
43};
44
45
46OptionsDialog rfb::win32::OptionsDialog::global;
47
48
49class VNCviewerOptions : public PropSheet {
50public:
51 VNCviewerOptions(OptionsInfo& info_, std::list<PropSheetPage*> pages)
52 : PropSheet(GetModuleHandle(0),
53 info_.view ? _T("VNC Viewer Options") : _T("VNC Viewer Defaults"), pages),
54 info(info_), changed(false) {
55 }
56 ~VNCviewerOptions() {
57 if (changed) {
58 if (info.view)
59 // Apply the settings to the supplied session object
60 info.view->applyOptions(info.options);
61 else {
62 // Commit the settings to the user's registry area
63 info.options.writeDefaults();
64 }
65 }
66 }
67
68 void setChanged() {changed = true;}
69
70 bool changed;
71 OptionsInfo& info;
72};
73
74
75class FormatPage : public PropSheetPage {
76public:
77 FormatPage(OptionsInfo* dlg_)
78 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_FORMAT)), dlg(dlg_) {
79 }
80 virtual void initDialog() {
81 setItemChecked(IDC_ENCODING_AUTO, dlg->options.autoSelect);
82 setItemChecked(IDC_FORMAT_FULLCOLOUR, dlg->options.fullColour);
83 if (!dlg->options.fullColour) {
84 switch (dlg->options.lowColourLevel) {
85 case 0: setItemChecked(IDC_FORMAT_VERYLOWCOLOUR, true); break;
86 case 1: setItemChecked(IDC_FORMAT_LOWCOLOUR, true); break;
87 case 2: setItemChecked(IDC_FORMAT_MEDIUMCOLOUR, true); break;
88 }
89 }
90 switch (dlg->options.preferredEncoding) {
Peter Åstranda2cdd2b2004-12-19 16:14:47 +000091 case encodingTight: setItemChecked(IDC_ENCODING_TIGHT, true); break;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000092 case encodingZRLE: setItemChecked(IDC_ENCODING_ZRLE, true); break;
93 case encodingHextile: setItemChecked(IDC_ENCODING_HEXTILE, true); break;
94 case encodingRaw: setItemChecked(IDC_ENCODING_RAW, true); break;
95 }
Peter Åstrand365427a2004-12-29 10:59:03 +000096 setItemChecked(IDC_ALLOW_COMPRESSLEVEL, dlg->options.customCompressLevel);
97 setItemInt(IDC_COMPRESSLEVEL, dlg->options.compressLevel);
Peter Åstrand0b870262004-12-28 15:55:46 +000098 setItemChecked(IDC_ALLOW_JPEG, !dlg->options.noJpeg);
Peter Åstrand142e84d2004-12-28 15:27:28 +000099 setItemInt(IDC_QUALITYLEVEL, dlg->options.qualityLevel);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000100 onCommand(IDC_ENCODING_AUTO, 0 /* ? */); // Force enableItem status to refresh
101 }
102 virtual bool onOk() {
103 dlg->options.autoSelect = isItemChecked(IDC_ENCODING_AUTO);
104 dlg->options.fullColour = isItemChecked(IDC_FORMAT_FULLCOLOUR);
Peter Åstrand365427a2004-12-29 10:59:03 +0000105 dlg->options.customCompressLevel = isItemChecked(IDC_ALLOW_COMPRESSLEVEL);
106 dlg->options.compressLevel = getItemInt(IDC_COMPRESSLEVEL);
Peter Åstrand0b870262004-12-28 15:55:46 +0000107 dlg->options.noJpeg = !isItemChecked(IDC_ALLOW_JPEG);
Peter Åstrand142e84d2004-12-28 15:27:28 +0000108 dlg->options.qualityLevel = getItemInt(IDC_QUALITYLEVEL);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000109 if (isItemChecked(IDC_FORMAT_VERYLOWCOLOUR))
110 dlg->options.lowColourLevel = 0;
111 if (isItemChecked(IDC_FORMAT_LOWCOLOUR))
112 dlg->options.lowColourLevel = 1;
113 if (isItemChecked(IDC_FORMAT_MEDIUMCOLOUR))
114 dlg->options.lowColourLevel = 2;
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000115 dlg->options.preferredEncoding = encodingTight;
116 if (isItemChecked(IDC_ENCODING_ZRLE))
117 dlg->options.preferredEncoding = encodingZRLE;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000118 if (isItemChecked(IDC_ENCODING_HEXTILE))
119 dlg->options.preferredEncoding = encodingHextile;
120 if (isItemChecked(IDC_ENCODING_RAW))
121 dlg->options.preferredEncoding = encodingRaw;
122 ((VNCviewerOptions*)propSheet)->setChanged();
123 return true;
124 }
125 virtual bool onCommand(int id, int cmd) {
126 if (id == IDC_ENCODING_AUTO) {
127 bool ok = !isItemChecked(IDC_ENCODING_AUTO);
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000128 enableItem(IDC_ENCODING_TIGHT, ok);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000129 enableItem(IDC_ENCODING_ZRLE, ok);
130 enableItem(IDC_ENCODING_HEXTILE, ok);
131 enableItem(IDC_ENCODING_RAW, ok);
Peter Åstrand1e6d8982004-12-28 15:07:38 +0000132 enableItem(IDC_FORMAT_FULLCOLOUR, ok);
133 enableItem(IDC_FORMAT_MEDIUMCOLOUR, ok);
134 enableItem(IDC_FORMAT_LOWCOLOUR, ok);
135 enableItem(IDC_FORMAT_VERYLOWCOLOUR, ok);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000136 return true;
137 }
138 return false;
139 }
140protected:
141 OptionsInfo* dlg;
142};
143
144class MiscPage : public PropSheetPage {
145public:
146 MiscPage(OptionsInfo* dlg_)
147 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_MISC)), dlg(dlg_) {
148 }
149 virtual void initDialog() {
150 setItemChecked(IDC_CONN_SHARED, dlg->options.shared);
151 enableItem(IDC_CONN_SHARED, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
152 setItemChecked(IDC_FULL_SCREEN, dlg->options.fullScreen);
153 setItemChecked(IDC_LOCAL_CURSOR, dlg->options.useLocalCursor);
154 setItemChecked(IDC_DESKTOP_RESIZE, dlg->options.useDesktopResize);
155 enableItem(IDC_PROTOCOL_3_3, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
156 setItemChecked(IDC_PROTOCOL_3_3, dlg->options.protocol3_3);
157 setItemChecked(IDC_ACCEPT_BELL, dlg->options.acceptBell);
158 }
159 virtual bool onOk() {
160 dlg->options.shared = isItemChecked(IDC_CONN_SHARED);
161 dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN);
162 dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR);
163 dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE);
164 dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3);
165 dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL);
166 ((VNCviewerOptions*)propSheet)->setChanged();
167 return true;
168 }
169protected:
170 OptionsInfo* dlg;
171};
172
173class InputsPage : public PropSheetPage {
174public:
175 InputsPage(OptionsInfo* dlg_)
176 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_INPUTS)), dlg(dlg_) {
177 }
178 virtual void initDialog() {
179 setItemChecked(IDC_SEND_POINTER, dlg->options.sendPtrEvents);
180 setItemChecked(IDC_SEND_KEYS, dlg->options.sendKeyEvents);
181 setItemChecked(IDC_CLIENT_CUTTEXT, dlg->options.clientCutText);
182 setItemChecked(IDC_SERVER_CUTTEXT, dlg->options.serverCutText);
183 setItemChecked(IDC_EMULATE3, dlg->options.emulate3);
184 setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0);
185
186 // Populate the Menu Key tab
187 HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY);
188 SendMessage(menuKey, CB_RESETCONTENT, 0, 0);
189 SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none"));
190 if (!dlg->options.menuKey)
191 SendMessage(menuKey, CB_SETCURSEL, 0, 0);
192 for (int i=0; i<12; i++) {
193 TCHAR buf[4];
194 _stprintf(buf, _T("F%d"), i+1);
195 int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf);
196 if (i == (dlg->options.menuKey - VK_F1))
197 SendMessage(menuKey, CB_SETCURSEL, index, 0);
198 }
199 }
200 virtual bool onOk() {
201 dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER);
202 dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS);
203 dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT);
204 dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT);
205 dlg->options.emulate3 = isItemChecked(IDC_EMULATE3);
206 dlg->options.pointerEventInterval =
207 isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0;
208
209 HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY);
210 int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0);
211 TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1);
212 SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf);
213 if (_tcscmp(keyName.buf, _T("none")) == 0)
214 dlg->options.setMenuKey("");
215 else
216 dlg->options.setMenuKey(CStr(keyName.buf));
217
218 ((VNCviewerOptions*)propSheet)->setChanged();
219 return true;
220 }
221protected:
222 OptionsInfo* dlg;
223};
224
225
226class DefaultsPage : public PropSheetPage {
227public:
228 DefaultsPage(OptionsInfo* dlg_)
229 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) {
230 }
231 virtual void initDialog() {
232 enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf);
233 enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf);
234 }
235 virtual bool onCommand(int id, int cmd) {
236 HWND hwnd = dlg->view ? dlg->view->getHandle() : 0;
237 switch (id) {
238 case IDC_LOAD_DEFAULTS:
239 dlg->options = CViewOptions();
240 break;
241 case IDC_SAVE_DEFAULTS:
242 propSheet->commitPages();
243 dlg->options.writeDefaults();
244 break;
245 case IDC_LOAD_CONFIG:
246 dlg->options.readFromFile(dlg->options.configFileName.buf);
247 break;
248 case IDC_SAVE_CONFIG:
249 propSheet->commitPages();
250 dlg->options.writeToFile(dlg->options.configFileName.buf);
251 MsgBox(hwnd, _T("Options saved successfully"),
252 MB_OK | MB_ICONINFORMATION);
253 return 0;
254 case IDC_SAVE_CONFIG_AS:
255 propSheet->commitPages();
256 // Get a filename to save to
257 TCHAR newFilename[4096];
258 TCHAR currentDir[4096];
259 if (dlg->options.configFileName.buf)
260 _tcscpy(newFilename, TStr(dlg->options.configFileName.buf));
261 else
262 newFilename[0] = 0;
263 OPENFILENAME ofn;
264 memset(&ofn, 0, sizeof(ofn));
265#ifdef OPENFILENAME_SIZE_VERSION_400
266 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
267#else
268 ofn.lStructSize = sizeof(ofn);
269#endif
270 ofn.hwndOwner = hwnd;
271 ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000");
272 ofn.lpstrFile = newFilename;
273 currentDir[0] = 0;
274 GetCurrentDirectory(4096, currentDir);
275 ofn.lpstrInitialDir = currentDir;
276 ofn.nMaxFile = 4096;
277 ofn.lpstrDefExt = _T(".vnc");
278 ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
279 if (!GetSaveFileName(&ofn)) {
280 if (CommDlgExtendedError())
281 throw rdr::Exception("GetSaveFileName failed");
282 return 0;
283 }
284
285 // Save the Options
286 dlg->options.writeToFile(CStr(newFilename));
287 MsgBox(hwnd, _T("Options saved successfully"),
288 MB_OK | MB_ICONINFORMATION);
289 return 0;
290 };
291 propSheet->reInitPages();
292 return true;
293 }
294protected:
295 OptionsInfo* dlg;
296};
297
298
299OptionsDialog::OptionsDialog() : visible(false) {
300}
301
302bool OptionsDialog::showDialog(CView* view, bool capture) {
303 if (visible) return false;
304 visible = true;
305
306 // Grab the current properties
307 OptionsInfo info;
308 if (view)
309 info.options = view->getOptions();
310 info.view = view;
311
312 // Build a list of pages to display
313 std::list<PropSheetPage*> pages;
314 FormatPage formatPage(&info); pages.push_back(&formatPage);
315 InputsPage inputsPage(&info); pages.push_back(&inputsPage);
316 MiscPage miscPage(&info); pages.push_back(&miscPage);
317 DefaultsPage defPage(&info); if (view) pages.push_back(&defPage);
318
319 // Show the property sheet
320 VNCviewerOptions dialog(info, pages);
321 dialog.showPropSheet(view ? view->getHandle() : 0, false, false, capture);
322
323 visible = false;
324 return dialog.changed;
325}