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