blob: 93e7f0c7d3220c16ee925bf5c17401e669aa14b7 [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 }
96 onCommand(IDC_ENCODING_AUTO, 0 /* ? */); // Force enableItem status to refresh
97 }
98 virtual bool onOk() {
99 dlg->options.autoSelect = isItemChecked(IDC_ENCODING_AUTO);
100 dlg->options.fullColour = isItemChecked(IDC_FORMAT_FULLCOLOUR);
101 if (isItemChecked(IDC_FORMAT_VERYLOWCOLOUR))
102 dlg->options.lowColourLevel = 0;
103 if (isItemChecked(IDC_FORMAT_LOWCOLOUR))
104 dlg->options.lowColourLevel = 1;
105 if (isItemChecked(IDC_FORMAT_MEDIUMCOLOUR))
106 dlg->options.lowColourLevel = 2;
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000107 dlg->options.preferredEncoding = encodingTight;
108 if (isItemChecked(IDC_ENCODING_ZRLE))
109 dlg->options.preferredEncoding = encodingZRLE;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000110 if (isItemChecked(IDC_ENCODING_HEXTILE))
111 dlg->options.preferredEncoding = encodingHextile;
112 if (isItemChecked(IDC_ENCODING_RAW))
113 dlg->options.preferredEncoding = encodingRaw;
114 ((VNCviewerOptions*)propSheet)->setChanged();
115 return true;
116 }
117 virtual bool onCommand(int id, int cmd) {
118 if (id == IDC_ENCODING_AUTO) {
119 bool ok = !isItemChecked(IDC_ENCODING_AUTO);
Peter Åstranda2cdd2b2004-12-19 16:14:47 +0000120 enableItem(IDC_ENCODING_TIGHT, ok);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000121 enableItem(IDC_ENCODING_ZRLE, ok);
122 enableItem(IDC_ENCODING_HEXTILE, ok);
123 enableItem(IDC_ENCODING_RAW, ok);
124 return true;
125 }
126 return false;
127 }
128protected:
129 OptionsInfo* dlg;
130};
131
132class MiscPage : public PropSheetPage {
133public:
134 MiscPage(OptionsInfo* dlg_)
135 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_MISC)), dlg(dlg_) {
136 }
137 virtual void initDialog() {
138 setItemChecked(IDC_CONN_SHARED, dlg->options.shared);
139 enableItem(IDC_CONN_SHARED, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
140 setItemChecked(IDC_FULL_SCREEN, dlg->options.fullScreen);
141 setItemChecked(IDC_LOCAL_CURSOR, dlg->options.useLocalCursor);
142 setItemChecked(IDC_DESKTOP_RESIZE, dlg->options.useDesktopResize);
143 enableItem(IDC_PROTOCOL_3_3, (!dlg->view) || (dlg->view->state() != CConnection::RFBSTATE_NORMAL));
144 setItemChecked(IDC_PROTOCOL_3_3, dlg->options.protocol3_3);
145 setItemChecked(IDC_ACCEPT_BELL, dlg->options.acceptBell);
146 }
147 virtual bool onOk() {
148 dlg->options.shared = isItemChecked(IDC_CONN_SHARED);
149 dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN);
150 dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR);
151 dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE);
152 dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3);
153 dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL);
154 ((VNCviewerOptions*)propSheet)->setChanged();
155 return true;
156 }
157protected:
158 OptionsInfo* dlg;
159};
160
161class InputsPage : public PropSheetPage {
162public:
163 InputsPage(OptionsInfo* dlg_)
164 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_INPUTS)), dlg(dlg_) {
165 }
166 virtual void initDialog() {
167 setItemChecked(IDC_SEND_POINTER, dlg->options.sendPtrEvents);
168 setItemChecked(IDC_SEND_KEYS, dlg->options.sendKeyEvents);
169 setItemChecked(IDC_CLIENT_CUTTEXT, dlg->options.clientCutText);
170 setItemChecked(IDC_SERVER_CUTTEXT, dlg->options.serverCutText);
171 setItemChecked(IDC_EMULATE3, dlg->options.emulate3);
172 setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0);
173
174 // Populate the Menu Key tab
175 HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY);
176 SendMessage(menuKey, CB_RESETCONTENT, 0, 0);
177 SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none"));
178 if (!dlg->options.menuKey)
179 SendMessage(menuKey, CB_SETCURSEL, 0, 0);
180 for (int i=0; i<12; i++) {
181 TCHAR buf[4];
182 _stprintf(buf, _T("F%d"), i+1);
183 int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf);
184 if (i == (dlg->options.menuKey - VK_F1))
185 SendMessage(menuKey, CB_SETCURSEL, index, 0);
186 }
187 }
188 virtual bool onOk() {
189 dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER);
190 dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS);
191 dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT);
192 dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT);
193 dlg->options.emulate3 = isItemChecked(IDC_EMULATE3);
194 dlg->options.pointerEventInterval =
195 isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0;
196
197 HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY);
198 int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0);
199 TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1);
200 SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf);
201 if (_tcscmp(keyName.buf, _T("none")) == 0)
202 dlg->options.setMenuKey("");
203 else
204 dlg->options.setMenuKey(CStr(keyName.buf));
205
206 ((VNCviewerOptions*)propSheet)->setChanged();
207 return true;
208 }
209protected:
210 OptionsInfo* dlg;
211};
212
213
214class DefaultsPage : public PropSheetPage {
215public:
216 DefaultsPage(OptionsInfo* dlg_)
217 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) {
218 }
219 virtual void initDialog() {
220 enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf);
221 enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf);
222 }
223 virtual bool onCommand(int id, int cmd) {
224 HWND hwnd = dlg->view ? dlg->view->getHandle() : 0;
225 switch (id) {
226 case IDC_LOAD_DEFAULTS:
227 dlg->options = CViewOptions();
228 break;
229 case IDC_SAVE_DEFAULTS:
230 propSheet->commitPages();
231 dlg->options.writeDefaults();
232 break;
233 case IDC_LOAD_CONFIG:
234 dlg->options.readFromFile(dlg->options.configFileName.buf);
235 break;
236 case IDC_SAVE_CONFIG:
237 propSheet->commitPages();
238 dlg->options.writeToFile(dlg->options.configFileName.buf);
239 MsgBox(hwnd, _T("Options saved successfully"),
240 MB_OK | MB_ICONINFORMATION);
241 return 0;
242 case IDC_SAVE_CONFIG_AS:
243 propSheet->commitPages();
244 // Get a filename to save to
245 TCHAR newFilename[4096];
246 TCHAR currentDir[4096];
247 if (dlg->options.configFileName.buf)
248 _tcscpy(newFilename, TStr(dlg->options.configFileName.buf));
249 else
250 newFilename[0] = 0;
251 OPENFILENAME ofn;
252 memset(&ofn, 0, sizeof(ofn));
253#ifdef OPENFILENAME_SIZE_VERSION_400
254 ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
255#else
256 ofn.lStructSize = sizeof(ofn);
257#endif
258 ofn.hwndOwner = hwnd;
259 ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000");
260 ofn.lpstrFile = newFilename;
261 currentDir[0] = 0;
262 GetCurrentDirectory(4096, currentDir);
263 ofn.lpstrInitialDir = currentDir;
264 ofn.nMaxFile = 4096;
265 ofn.lpstrDefExt = _T(".vnc");
266 ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
267 if (!GetSaveFileName(&ofn)) {
268 if (CommDlgExtendedError())
269 throw rdr::Exception("GetSaveFileName failed");
270 return 0;
271 }
272
273 // Save the Options
274 dlg->options.writeToFile(CStr(newFilename));
275 MsgBox(hwnd, _T("Options saved successfully"),
276 MB_OK | MB_ICONINFORMATION);
277 return 0;
278 };
279 propSheet->reInitPages();
280 return true;
281 }
282protected:
283 OptionsInfo* dlg;
284};
285
286
287OptionsDialog::OptionsDialog() : visible(false) {
288}
289
290bool OptionsDialog::showDialog(CView* view, bool capture) {
291 if (visible) return false;
292 visible = true;
293
294 // Grab the current properties
295 OptionsInfo info;
296 if (view)
297 info.options = view->getOptions();
298 info.view = view;
299
300 // Build a list of pages to display
301 std::list<PropSheetPage*> pages;
302 FormatPage formatPage(&info); pages.push_back(&formatPage);
303 InputsPage inputsPage(&info); pages.push_back(&inputsPage);
304 MiscPage miscPage(&info); pages.push_back(&miscPage);
305 DefaultsPage defPage(&info); if (view) pages.push_back(&defPage);
306
307 // Show the property sheet
308 VNCviewerOptions dialog(info, pages);
309 dialog.showPropSheet(view ? view->getHandle() : 0, false, false, capture);
310
311 visible = false;
312 return dialog.changed;
313}