Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame^] | 1 | /* 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 | |
| 30 | using namespace rfb; |
| 31 | using namespace rfb::win32; |
| 32 | |
| 33 | static LogWriter vlog("Options"); |
| 34 | |
| 35 | |
| 36 | struct OptionsInfo { |
| 37 | CConn* view; |
| 38 | CConnOptions options; |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | OptionsDialog rfb::win32::OptionsDialog::global; |
| 43 | |
| 44 | |
| 45 | class ViewerOptions : public PropSheet { |
| 46 | public: |
| 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 | |
| 71 | class FormatPage : public PropSheetPage { |
| 72 | public: |
| 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 | } |
| 146 | protected: |
| 147 | OptionsInfo* dlg; |
| 148 | }; |
| 149 | |
| 150 | class MiscPage : public PropSheetPage { |
| 151 | public: |
| 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); |
| 166 | } |
| 167 | virtual bool onOk() { |
| 168 | dlg->options.shared = isItemChecked(IDC_CONN_SHARED); |
| 169 | dlg->options.fullScreen = isItemChecked(IDC_FULL_SCREEN); |
| 170 | dlg->options.useLocalCursor = isItemChecked(IDC_LOCAL_CURSOR); |
| 171 | dlg->options.useDesktopResize = isItemChecked(IDC_DESKTOP_RESIZE); |
| 172 | dlg->options.protocol3_3 = isItemChecked(IDC_PROTOCOL_3_3); |
| 173 | dlg->options.acceptBell = isItemChecked(IDC_ACCEPT_BELL); |
| 174 | dlg->options.autoReconnect = isItemChecked(IDC_AUTO_RECONNECT); |
| 175 | dlg->options.showToolbar = isItemChecked(IDC_SHOW_TOOLBAR); |
| 176 | ((ViewerOptions*)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_DISABLE_WINKEYS, dlg->options.disableWinKeys && !osVersion.isPlatformWindows); |
| 194 | enableItem(IDC_DISABLE_WINKEYS, !osVersion.isPlatformWindows); |
| 195 | setItemChecked(IDC_EMULATE3, dlg->options.emulate3); |
| 196 | setItemChecked(IDC_POINTER_INTERVAL, dlg->options.pointerEventInterval != 0); |
| 197 | |
| 198 | // Populate the Menu Key tab |
| 199 | HWND menuKey = GetDlgItem(handle, IDC_MENU_KEY); |
| 200 | SendMessage(menuKey, CB_RESETCONTENT, 0, 0); |
| 201 | SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)_T("none")); |
| 202 | if (!dlg->options.menuKey) |
| 203 | SendMessage(menuKey, CB_SETCURSEL, 0, 0); |
| 204 | for (int i=0; i<12; i++) { |
| 205 | TCHAR buf[4]; |
| 206 | _stprintf(buf, _T("F%d"), i+1); |
| 207 | int index = SendMessage(menuKey, CB_ADDSTRING, 0, (LPARAM)buf); |
| 208 | if (i == (dlg->options.menuKey - VK_F1)) |
| 209 | SendMessage(menuKey, CB_SETCURSEL, index, 0); |
| 210 | } |
| 211 | } |
| 212 | virtual bool onOk() { |
| 213 | dlg->options.sendPtrEvents = isItemChecked(IDC_SEND_POINTER); |
| 214 | dlg->options.sendKeyEvents = isItemChecked(IDC_SEND_KEYS); |
| 215 | dlg->options.clientCutText = isItemChecked(IDC_CLIENT_CUTTEXT); |
| 216 | dlg->options.serverCutText = isItemChecked(IDC_SERVER_CUTTEXT); |
| 217 | dlg->options.disableWinKeys = isItemChecked(IDC_DISABLE_WINKEYS); |
| 218 | dlg->options.emulate3 = isItemChecked(IDC_EMULATE3); |
| 219 | dlg->options.pointerEventInterval = |
| 220 | isItemChecked(IDC_POINTER_INTERVAL) ? 200 : 0; |
| 221 | |
| 222 | HWND mkHwnd = GetDlgItem(handle, IDC_MENU_KEY); |
| 223 | int index = SendMessage(mkHwnd, CB_GETCURSEL, 0, 0); |
| 224 | TCharArray keyName(SendMessage(mkHwnd, CB_GETLBTEXTLEN, index, 0)+1); |
| 225 | SendMessage(mkHwnd, CB_GETLBTEXT, index, (LPARAM)keyName.buf); |
| 226 | if (_tcscmp(keyName.buf, _T("none")) == 0) |
| 227 | dlg->options.setMenuKey(""); |
| 228 | else |
| 229 | dlg->options.setMenuKey(CStr(keyName.buf)); |
| 230 | |
| 231 | ((ViewerOptions*)propSheet)->setChanged(); |
| 232 | return true; |
| 233 | } |
| 234 | protected: |
| 235 | OptionsInfo* dlg; |
| 236 | }; |
| 237 | |
| 238 | class DefaultsPage : public PropSheetPage { |
| 239 | public: |
| 240 | DefaultsPage(OptionsInfo* dlg_) |
| 241 | : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTS)), dlg(dlg_) { |
| 242 | } |
| 243 | virtual void initDialog() { |
| 244 | enableItem(IDC_LOAD_CONFIG, dlg->options.configFileName.buf); |
| 245 | enableItem(IDC_SAVE_CONFIG, dlg->options.configFileName.buf); |
| 246 | } |
| 247 | virtual bool onCommand(int id, int cmd) { |
| 248 | switch (id) { |
| 249 | case IDC_LOAD_DEFAULTS: |
| 250 | dlg->options = CConnOptions(); |
| 251 | break; |
| 252 | case IDC_SAVE_DEFAULTS: |
| 253 | propSheet->commitPages(); |
| 254 | dlg->options.writeDefaults(); |
| 255 | break; |
| 256 | case IDC_LOAD_CONFIG: |
| 257 | dlg->options.readFromFile(dlg->options.configFileName.buf); |
| 258 | break; |
| 259 | case IDC_SAVE_CONFIG: |
| 260 | propSheet->commitPages(); |
| 261 | dlg->options.writeToFile(dlg->options.configFileName.buf); |
| 262 | MsgBox(handle, _T("Options saved successfully"), |
| 263 | MB_OK | MB_ICONINFORMATION); |
| 264 | return 0; |
| 265 | case IDC_SAVE_CONFIG_AS: |
| 266 | propSheet->commitPages(); |
| 267 | // Get a filename to save to |
| 268 | TCHAR newFilename[4096]; |
| 269 | TCHAR currentDir[4096]; |
| 270 | if (dlg->options.configFileName.buf) |
| 271 | _tcscpy(newFilename, TStr(dlg->options.configFileName.buf)); |
| 272 | else |
| 273 | newFilename[0] = 0; |
| 274 | OPENFILENAME ofn; |
| 275 | memset(&ofn, 0, sizeof(ofn)); |
| 276 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
| 277 | ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
| 278 | #else |
| 279 | ofn.lStructSize = sizeof(ofn); |
| 280 | #endif |
| 281 | ofn.hwndOwner = handle; |
| 282 | ofn.lpstrFilter = _T("VNC Connection Options\000*.vnc\000"); |
| 283 | ofn.lpstrFile = newFilename; |
| 284 | currentDir[0] = 0; |
| 285 | GetCurrentDirectory(4096, currentDir); |
| 286 | ofn.lpstrInitialDir = currentDir; |
| 287 | ofn.nMaxFile = 4096; |
| 288 | ofn.lpstrDefExt = _T(".vnc"); |
| 289 | ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; |
| 290 | if (!GetSaveFileName(&ofn)) { |
| 291 | if (CommDlgExtendedError()) |
| 292 | throw rdr::Exception("GetSaveFileName failed"); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | // Save the Options |
| 297 | dlg->options.writeToFile(CStr(newFilename)); |
| 298 | MsgBox(handle, _T("Options saved successfully"), |
| 299 | MB_OK | MB_ICONINFORMATION); |
| 300 | return 0; |
| 301 | }; |
| 302 | propSheet->reInitPages(); |
| 303 | return true; |
| 304 | } |
| 305 | protected: |
| 306 | OptionsInfo* dlg; |
| 307 | }; |
| 308 | |
| 309 | |
| 310 | OptionsDialog::OptionsDialog() : visible(false) { |
| 311 | } |
| 312 | |
| 313 | bool OptionsDialog::showDialog(CConn* view, bool capture) { |
| 314 | if (visible) return false; |
| 315 | visible = true; |
| 316 | |
| 317 | // Grab the current properties |
| 318 | OptionsInfo info; |
| 319 | if (view) |
| 320 | info.options = view->getOptions(); |
| 321 | info.view = view; |
| 322 | |
| 323 | // Build a list of pages to display |
| 324 | std::list<PropSheetPage*> pages; |
| 325 | FormatPage formatPage(&info); pages.push_back(&formatPage); |
| 326 | InputsPage inputsPage(&info); pages.push_back(&inputsPage); |
| 327 | MiscPage miscPage(&info); pages.push_back(&miscPage); |
| 328 | DefaultsPage defPage(&info); if (view) pages.push_back(&defPage); |
| 329 | |
| 330 | // Show the property sheet |
| 331 | ViewerOptions dialog(info, pages); |
| 332 | dialog.showPropSheet(view && view->getWindow() ? view->getWindow()->getHandle() : 0, |
| 333 | false, false, capture); |
| 334 | |
| 335 | visible = false; |
| 336 | return dialog.changed; |
| 337 | } |