VNCOptions.cpp 45.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
//  Copyright (C) 2002 Ultr@VNC Team Members. All Rights Reserved.
//
//  Copyright (C) 2000-2002 Const Kaplinsky. All Rights Reserved.
//
// Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
//
//  Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
//  This file is part of the VNC system.
//
//  The VNC system is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// If the source code for the VNC system is not available from the place 
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
// the authors on vnc@uk.research.att.com for information on obtaining it.


// VNCOptions.cpp: implementation of the VNCOptions class.

#include "stdhdrs.h"
#include "vncviewer.h"
#include "VNCOptions.h"
#include "Exception.h"
extern char sz_A2[64];
extern char sz_D1[64];
extern char sz_D2[64];
extern char sz_D3[64];
extern char sz_D4[64];
extern char sz_D5[64];
extern char sz_D6[64];
extern char sz_D7[64];
extern char sz_D8[64];
extern char sz_D9[64];
extern char sz_D10[64];
extern char sz_D11[64];
extern char sz_D12[64];
extern char sz_D13[64];
extern char sz_D14[64];
extern char sz_D15[64];
extern char sz_D16[64];
extern char sz_D17[64];
extern char sz_D18[64];
extern char sz_D19[64];
extern char sz_D20[64];
extern char sz_D21[64];
extern char sz_D22[64];
extern char sz_D23[64];
extern char sz_D24[64];
extern char sz_D25[64];
extern char sz_D26[64];
extern char sz_D27[64];
extern char sz_D28[64];

VNCOptions::VNCOptions()
{
  for (int i = 0; i <= LASTENCODING; i++)
    m_UseEnc[i] = false;

  m_UseEnc[rfbEncodingRaw] = true;
  m_UseEnc[rfbEncodingCopyRect] = true;
  m_UseEnc[rfbEncodingRRE] = true;
  m_UseEnc[rfbEncodingCoRRE] = true;
  m_UseEnc[rfbEncodingHextile] = true;
  m_UseEnc[rfbEncodingZlib] = true;
  m_UseEnc[rfbEncodingTight] = true;
  m_UseEnc[rfbEncodingZlibHex] = true;
  m_UseEnc[rfbEncodingZRLE] = true;
  m_UseEnc[rfbEncodingZYWRLE] = true;
  m_UseEnc[rfbEncodingUltra] = true;
	
  m_ViewOnly = false;
  m_FullScreen = false;
  autoDetect = true;
  m_Use8Bit = rfbPFFullColors; //false;
  m_ShowToolbar = true;
  m_fAutoScaling = false;
  m_NoStatus = false;
  m_NoHotKeys = false;

#ifndef UNDER_CE
  //m_PreferredEncoding = rfbEncodingHextile;
  m_PreferredEncoding = rfbEncodingZRLE;
#else
  // With WinCE2.0, CoRRE seems more efficient since it
  // reads the whole update in one socket call.
  m_PreferredEncoding = rfbEncodingCoRRE;
#endif
  m_JapKeyboard = false;
  m_SwapMouse = false;
  m_Emul3Buttons = true;
  m_Emul3Timeout = 100; // milliseconds
  m_Emul3Fuzz = 4;      // pixels away before emulation is cancelled
  m_Shared = true;
  m_DeiconifyOnBell = false;
  m_DisableClipboard = false;
  m_localCursor = DOTCURSOR; // NOCURSOR;
  m_scaling = false;
  m_fAutoScaling = false;
  m_scale_num = 100;
  m_scale_den = 100;

  
  // Modif sf@2002 - Server Scaling
  m_nServerScale = 1;

  m_reconnectcounter = 0;

  // Modif sf@2002 - Cache
  m_fEnableCache = false;
  // m_fAutoAdjust = false;

  m_host_options[0] = '\0';
  m_proxyhost[0] = '\0';
  m_port = -1;
  m_proxyport = -1;
	
  m_kbdname[0] = '\0';
  m_kbdSpecified = false;
	
  m_logLevel = 0;
  m_logToConsole = false;
  m_logToFile = false;
  m_logFilename[0] = '\0';
	
  m_delay=0;
  m_connectionSpecified = false;
  m_configSpecified = false;
  m_configFilename[0] = '\0';
  m_listening = false;
  m_listenPort = INCOMING_PORT_OFFSET;
  m_restricted = false;

  // Tight specific
  m_useCompressLevel = true;
  m_compressLevel = 6;		
  m_enableJpegCompression = true;
  m_jpegQualityLevel = 6;
  m_requestShapeUpdates = true;
  m_ignoreShapeUpdates = false;

  m_cmdlnUser[0] = '\0';		// act : add user option on command line
  m_clearPassword[0] = '\0';		// sf@2002
  m_quickoption = 1;				// sf@2002 - Auto Mode as default
  m_fUseDSMPlugin = false;
  m_fUseProxy = false;
  m_szDSMPluginFilename[0] = '\0';

  // sf@2003 - Auto Scaling
  m_saved_scale_num = 100;
  m_saved_scale_den = 100;
  m_saved_scaling = false;
  
  // sf@2007 - Autoreconnect
  m_autoReconnect = 3; // Default: 10s before reconnecting

  // Fix by Act : no user password command line after a rejected connection
  m_NoMoreCommandLineUserPassword = false;

  m_fExitCheck = true; //PGM @ Advantig

  
#ifdef UNDER_CE
  m_palmpc = false;
	
  // Check for PalmPC aspect 
  HDC temp_hdc = GetDC(NULL);
  int screen_width = GetDeviceCaps(temp_hdc, HORZRES);
  if (screen_width < 320)
  {
    m_palmpc = true;
  }
  ReleaseDC(NULL,temp_hdc);

  m_slowgdi = false;
#endif
}

VNCOptions& VNCOptions::operator=(VNCOptions& s)
{
  for (int i = rfbEncodingRaw; i<= LASTENCODING; i++)
    m_UseEnc[i] = s.m_UseEnc[i];
	
  m_ViewOnly			= s.m_ViewOnly;
  m_NoStatus			= s.m_NoStatus;
  m_FullScreen		= s.m_FullScreen;
  autoDetect = s.autoDetect;
  m_Use8Bit			= s.m_Use8Bit;
  m_PreferredEncoding = s.m_PreferredEncoding;
  m_SwapMouse			= s.m_SwapMouse;
  m_Emul3Buttons		= s.m_Emul3Buttons;
  m_Emul3Timeout		= s.m_Emul3Timeout;
  m_Emul3Fuzz			= s.m_Emul3Fuzz;      // pixels away before emulation is cancelled
  m_Shared			= s.m_Shared;
  m_DeiconifyOnBell	= s.m_DeiconifyOnBell;
  m_DisableClipboard  = s.m_DisableClipboard;
  m_scaling			= s.m_scaling;
  m_fAutoScaling    = s.m_fAutoScaling;
  m_scale_num			= s.m_scale_num;
  m_scale_den			= s.m_scale_den;
  m_localCursor		= s.m_localCursor;
  // Modif sf@2002
  m_nServerScale  	  = s.m_nServerScale;
  m_fEnableCache      = s.m_fEnableCache;
  m_quickoption       = s.m_quickoption;
  m_ShowToolbar       = s.m_ShowToolbar;
  m_fAutoScaling      = s.m_fAutoScaling;
  m_fUseDSMPlugin     = s.m_fUseDSMPlugin;
  m_NoHotKeys		  = s.m_NoHotKeys;
  
  // sf@2003 - Autoscaling
  m_saved_scale_num = s.m_saved_scale_num;
  m_saved_scale_den = s.m_saved_scale_den;
  m_saved_scaling   = s.m_saved_scaling;

  strcpy(m_szDSMPluginFilename, s.m_szDSMPluginFilename);
  
  strcpy(m_host_options, s.m_host_options);
  m_port				= s.m_port;

  strcpy(m_proxyhost, s.m_proxyhost);
  m_proxyport				= s.m_proxyport;
  m_fUseProxy	      = s.m_fUseProxy;
  
  strcpy(m_kbdname, s.m_kbdname);
  m_kbdSpecified		= s.m_kbdSpecified;
	
  m_logLevel			= s.m_logLevel;
  m_logToConsole		= s.m_logToConsole;
  m_logToFile			= s.m_logToFile;
  strcpy(m_logFilename, s.m_logFilename);

  m_delay				= s.m_delay;
  m_connectionSpecified = s.m_connectionSpecified;
  m_configSpecified   = s.m_configSpecified;
  strcpy(m_configFilename, s.m_configFilename);

  m_listening			= s.m_listening;
  m_listenPort			= s.m_listenPort;
  m_restricted			= s.m_restricted;

  // Tight specific
  m_useCompressLevel		= s.m_useCompressLevel;
  m_compressLevel			= s.m_compressLevel;
  m_enableJpegCompression	= s.m_enableJpegCompression;
  m_jpegQualityLevel		= s.m_jpegQualityLevel;
  m_requestShapeUpdates	    = s.m_requestShapeUpdates;
  m_ignoreShapeUpdates	    = s.m_ignoreShapeUpdates;
  
  // sf@2007 - Autoreconnect
  m_autoReconnect         = s.m_autoReconnect;
  m_JapKeyboard			  = s.m_JapKeyboard;

  m_fExitCheck    = s.m_fExitCheck; //PGM @ Advantig



#ifdef UNDER_CE
  m_palmpc			= s.m_palmpc;
  m_slowgdi			= s.m_slowgdi;
#endif
  return *this;
}

VNCOptions::~VNCOptions()
{
	
}

inline bool SwitchMatch(LPCTSTR arg, LPCTSTR swtch) {
  return (arg[0] == '-' || arg[0] == '/') &&
    (_tcsicmp(&arg[1], swtch) == 0);
}

static void ArgError(LPTSTR msg) {
  MessageBox(NULL,  msg, sz_D1,MB_OK | MB_TOPMOST | MB_ICONSTOP);
}

// Greatest common denominator, by Euclid
int gcd(int a, int b) {
  if (a < b) return gcd(b,a);
  if (b == 0) return a;
  return gcd(b, a % b);
}

void VNCOptions::FixScaling()
 {
  if (m_scale_num < 1 || m_scale_den < 1 || m_scale_num > 400 || m_scale_den > 100)
  {
    MessageBox(NULL,  sz_D2, 
               sz_D1,MB_OK | MB_TOPMOST | MB_ICONWARNING);
    m_scale_num = 1;
    m_scale_den = 1;	
    m_scaling = false;
  }
  int g = gcd(m_scale_num, m_scale_den);
  m_scale_num /= g;
  m_scale_den /= g;	
  
  // Modif sf@2002 - Server Scaling
  if (m_nServerScale < 1 || m_nServerScale > 9) m_nServerScale = 1;
}

void VNCOptions::SetFromCommandLine(LPTSTR szCmdLine) {
  // We assume no quoting here.
  // Copy the command line - we don't know what might happen to the original
  int cmdlinelen = _tcslen(szCmdLine);
  if (cmdlinelen == 0) return;
	
  TCHAR *cmd = new TCHAR[cmdlinelen + 1];
  _tcscpy(cmd, szCmdLine);
	
	// Count the number of spaces
	// This may be more than the number of arguments, but that doesn't matter.
  int nspaces = 0;
  TCHAR *p = cmd;
  TCHAR *pos = cmd;
  while ( ( pos = _tcschr(p, ' ') ) != NULL ) {
    nspaces ++;
    p = pos + 1;
  }
	
  // Create the array to hold pointers to each bit of string
  TCHAR **args = new LPTSTR[nspaces + 1];
	
  // replace spaces with nulls and
  // create an array of TCHAR*'s which points to start of each bit.
  pos = cmd;
  int i = 0;
  args[i] = cmd;
  bool inquote=false;
  for (pos = cmd; *pos != 0; pos++) {
    // Arguments are normally separated by spaces, unless there's quoting
    if ((*pos == ' ') && !inquote) {
      *pos = '\0';
      p = pos + 1;
      args[++i] = p;
    }
    if (*pos == '"') {  
      if (!inquote) {      // Are we starting a quoted argument?
        args[i] = ++pos; // It starts just after the quote
      } else {
        *pos = '\0';     // Finish a quoted argument?
      }
      inquote = !inquote;
    }
  }
  i++;

  bool hostGiven = false, portGiven = false;
  // take in order.
  for (int j = 0; j < i; j++) {
    if ( SwitchMatch(args[j], _T("help")) ||
         SwitchMatch(args[j], _T("?")) ||
         SwitchMatch(args[j], _T("h")))
	{
	  m_NoStatus = true;
      ShowUsage();
      exit(1);
    }
	else if ( SwitchMatch(args[j], _T("listen")))
	{
      m_listening = true;
      if (j+1 < i && args[j+1][0] >= '0' && args[j+1][0] <= '9') {
        if (_stscanf(args[j+1], _T("%d"), &m_listenPort) != 1) {
          ArgError(sz_D3);
          continue;
        }
        j++;
      }
    } else if ( SwitchMatch(args[j], _T("silentexit"))) { //PGM @ Advantig
      m_fExitCheck = false; //PGM @ Advantig
    } else if ( SwitchMatch(args[j], _T("restricted"))) {
      m_restricted = true;
    } else if ( SwitchMatch(args[j], _T("viewonly"))) {
      m_ViewOnly = true;
	} else if ( SwitchMatch(args[j], _T("nostatus"))) {
      m_NoStatus = true;
	} else if ( SwitchMatch(args[j], _T("nohotkeys"))) {
      m_NoHotKeys = true;
    } else if ( SwitchMatch(args[j], _T("notoolbar"))) {
      m_ShowToolbar = false;
    } else if ( SwitchMatch(args[j], _T("autoscaling"))) {
      m_fAutoScaling = true;
    } else if ( SwitchMatch(args[j], _T("fullscreen"))) {
      m_FullScreen = true;
    } else if ( SwitchMatch(args[j], _T("noauto"))) {
      autoDetect = false;
	  m_quickoption = 0;
    } else if ( SwitchMatch(args[j], _T("8bit"))) {
      m_Use8Bit = rfbPF256Colors; //true;
    } else if ( SwitchMatch(args[j], _T("256colors"))) {
      m_Use8Bit = rfbPFFullColors;
    } else if ( SwitchMatch(args[j], _T("64colors"))) {
      m_Use8Bit = rfbPF64Colors;
    } else if ( SwitchMatch(args[j], _T("8colors"))) {
      m_Use8Bit = rfbPF8Colors;
    } else if ( SwitchMatch(args[j], _T("8greycolors"))) {
      m_Use8Bit = rfbPF8GreyColors;
    } else if ( SwitchMatch(args[j], _T("4greycolors"))) {
      m_Use8Bit = rfbPF4GreyColors;
    } else if ( SwitchMatch(args[j], _T("2greycolors"))) {
      m_Use8Bit = rfbPF2GreyColors;
    } else if ( SwitchMatch(args[j], _T("shared"))) {
      m_Shared = true;
    } else if ( SwitchMatch(args[j], _T("swapmouse"))) {
      m_SwapMouse = true;
    } else if ( SwitchMatch(args[j], _T("nocursor"))) {
      m_localCursor = NOCURSOR;
    } else if ( SwitchMatch(args[j], _T("dotcursor"))) {
      m_localCursor = DOTCURSOR;
    } else if ( SwitchMatch(args[j], _T("normalcursor"))) {
      m_localCursor = NORMALCURSOR;
    } else if ( SwitchMatch(args[j], _T("belldeiconify") )) {
      m_DeiconifyOnBell = true;
    } else if ( SwitchMatch(args[j], _T("emulate3") )) {
      m_Emul3Buttons = true;
	} else if ( SwitchMatch(args[j], _T("JapKeyboard") )) {
      m_JapKeyboard = true;
    } else if ( SwitchMatch(args[j], _T("noemulate3") )) {
      m_Emul3Buttons = false;
	} else if ( SwitchMatch(args[j], _T("nocursorshape") )) {
			m_requestShapeUpdates = false;
	} else if ( SwitchMatch(args[j], _T("noremotecursor") )) {
			m_requestShapeUpdates = true;
			m_ignoreShapeUpdates = true;
    } else if ( SwitchMatch(args[j], _T("scale") )) {
      if (++j == i) {
        ArgError(sz_D4);
        continue;
      }
      int numscales = _stscanf(args[j], _T("%d/%d"), &m_scale_num, &m_scale_den);
      if (numscales < 1) {
        ArgError(sz_D5);
        continue;
      }
      if (numscales == 1) 
        m_scale_den = 1; // needed if you're overriding a previous setting

    } else if ( SwitchMatch(args[j], _T("emulate3timeout") )) {
      if (++j == i) {
        ArgError(sz_D6);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_Emul3Timeout) != 1) {
        ArgError(sz_D7);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("emulate3fuzz") )) {
      if (++j == i) {
        ArgError(sz_D8);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_Emul3Fuzz) != 1) {
        ArgError(sz_D9);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("disableclipboard") )) {
      m_DisableClipboard = true;
    }
#ifdef UNDER_CE
    // Manual setting of palm vs hpc aspect ratio for dialog boxes.
    else if ( SwitchMatch(args[j], _T("hpc") )) {
      m_palmpc = false;
    } else if ( SwitchMatch(args[j], _T("palm") )) {
      m_palmpc = true;
    } else if ( SwitchMatch(args[j], _T("slow") )) {
      m_slowgdi = true;
    } 
#endif
    else if ( SwitchMatch(args[j], _T("delay") )) {
      if (++j == i) {
        ArgError(sz_D10);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_delay) != 1) {
        ArgError(sz_D11);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("loglevel") )) {
      if (++j == i) {
        ArgError(sz_D12);
        continue;
      }
      if (_stscanf(args[j], _T("%d"), &m_logLevel) != 1) {
        ArgError(sz_D13);
        continue;
      }
			
    } else if ( SwitchMatch(args[j], _T("console") )) {
      m_logToConsole = true;
    } else if ( SwitchMatch(args[j], _T("logfile") )) {
      if (++j == i) {
        ArgError(sz_D14);
        continue;
      }
      if (_stscanf(args[j], _T("%s"), m_logFilename) != 1) {
        ArgError(sz_D15);
        continue;
      } else {
        m_logToFile = true;
      }
    } else if ( SwitchMatch(args[j], _T("config") )) {
      if (++j == i) {
        ArgError(sz_D16);
        continue;
      }
      // The GetPrivateProfile* stuff seems not to like some relative paths
      _fullpath(m_configFilename, args[j], _MAX_PATH);
      if (_access(m_configFilename, 04)) {
        ArgError(sz_D17);
        PostQuitMessage(1);
        continue;
      } else {
        Load(m_configFilename);
        m_configSpecified = true;
      }
    } else if ( SwitchMatch(args[j], _T("register") )) {
      Register();
      PostQuitMessage(0);
	
	}
	else if ( SwitchMatch(args[j], _T("encoding") )) {
			if (++j == i) {
				ArgError(sz_D18);
				continue;
			}
			int enc = -1;
			if (_tcsicmp(args[j], _T("raw")) == 0) {
				enc = rfbEncodingRaw;
			} else if (_tcsicmp(args[j], _T("rre")) == 0) {
				enc = rfbEncodingRRE;
			} else if (_tcsicmp(args[j], _T("corre")) == 0) {
				enc = rfbEncodingCoRRE;
			} else if (_tcsicmp(args[j], _T("hextile")) == 0) {
				enc = rfbEncodingHextile;
			} else if (_tcsicmp(args[j], _T("zlib")) == 0) {
				enc = rfbEncodingZlib;
			} else if (_tcsicmp(args[j], _T("zlibhex")) == 0) {
				enc = rfbEncodingZlibHex;
			} else if (_tcsicmp(args[j], _T("tight")) == 0) {
				enc = rfbEncodingTight;
			} else if (_tcsicmp(args[j], _T("ultra")) == 0) {
				enc = rfbEncodingUltra;
			} else {
				ArgError(sz_D19);
				continue;
			}
			if (enc != -1) {
				m_UseEnc[enc] = true;
				m_PreferredEncoding = enc;
			}
	}
	// Tight options
	else if ( SwitchMatch(args[j], _T("compresslevel") )) {
			if (++j == i) {
				ArgError(sz_D20);
				continue;
			}
			m_useCompressLevel = true;
			if (_stscanf(args[j], _T("%d"), &m_compressLevel) != 1) {
				ArgError(sz_D21);
				continue;
			}
		} else if ( SwitchMatch(args[j], _T("quality") )) {
			if (++j == i) {
				ArgError(sz_D22);
				continue;
			}
			m_enableJpegCompression = true;
			if (_stscanf(args[j], _T("%d"), &m_jpegQualityLevel) != 1) {
				ArgError(sz_D23);
				continue;
			}
	}
	// act : add user option on command line
	else if ( SwitchMatch(args[j], _T("user") ))
	{
			if (++j == i)
			{
				ArgError(sz_D24);
				continue;
			}
			strcpy(m_cmdlnUser, args[j]);
	} // act : add user option on command line
	// Modif sf@2002 : password in the command line
	else if ( SwitchMatch(args[j], _T("password") ))
	{
			if (++j == i)
			{
				ArgError(sz_D24);
				continue;
			}
			strcpy(m_clearPassword, args[j]);
	} // Modif sf@2002
	else if ( SwitchMatch(args[j], _T("serverscale") ))
	{
		if (++j == i)
		{
			ArgError(sz_D25);
			continue;
		}
		_stscanf(args[j], _T("%d"), &m_nServerScale);
		if (m_nServerScale < 1 || m_nServerScale > 9) m_nServerScale = 1;
	}
	// Modif sf@2002
	else if ( SwitchMatch(args[j], _T("quickoption") )) 
	{
		if (++j == i)
		{
			ArgError(sz_D26);
			continue;
		}
		_stscanf(args[j], _T("%d"), &m_quickoption);
	}
	// Modif sf@2002 - DSM Plugin 
	else if ( SwitchMatch(args[j], _T("dsmplugin") ))
	{
		if (++j == i)
		{
			ArgError(sz_D27);
			continue;
		}
		m_fUseDSMPlugin = true;
		strcpy(m_szDSMPluginFilename, args[j]);
	}
	else if ( SwitchMatch(args[j], _T("proxy") ))
	{
		if (++j == i)
		{
			ArgError(sz_D27); // sf@ - Todo: put correct message here
			continue;
		}
		TCHAR proxyhost[256];
		if (!ParseDisplay(args[j], proxyhost, 255, &m_proxyport)) {
			ShowUsage(sz_D28);
			PostQuitMessage(1);
		} else {
			m_fUseProxy = true;
			_tcscpy(m_proxyhost, proxyhost);
		}
	}
	else if (SwitchMatch(args[j], _T("autoreconnect"))) 
	{
        if (++j == i) {
            ArgError(_T("You must specify an autoreconnect delay (default is 10s)"));
            PostQuitMessage(1);
            continue;
        }
		_stscanf(args[j], _T("%d"), &m_autoReconnect);
	}
	else
	{
      TCHAR phost[256];
      if (!ParseDisplay(args[j], phost, 255, &m_port)) {
        ShowUsage(sz_D28);
        PostQuitMessage(1);
      } else {
        _tcscpy(m_host_options, phost);
        m_connectionSpecified = true;
      }
    }
  }       
	
  if (m_scale_num != 1 || m_scale_den != 1) 			
    m_scaling = true;

	// reduce scaling factors by greatest common denominator
  if (m_scaling) {
    FixScaling();
  }
  // tidy up
  delete [] cmd;
  delete [] args;
}



void saveInt(char *name, int value, char *fname) 
{
  char buf[4];
  sprintf(buf, "%d", value); 
  WritePrivateProfileString("options", name, buf, fname);
}

int readInt(char *name, int defval, char *fname)
{
  return GetPrivateProfileInt("options", name, defval, fname);
}

void VNCOptions::Save(char *fname)
{
  for (int i = rfbEncodingRaw; i<= LASTENCODING; i++) {
    char buf[128];
    sprintf(buf, "use_encoding_%d", i);
    saveInt(buf, m_UseEnc[i], fname);
  }
  saveInt("preferred_encoding",	m_PreferredEncoding,fname);
  saveInt("restricted",			m_restricted,		fname);
  saveInt("viewonly",				m_ViewOnly,			fname);
  saveInt("nostatus",				m_NoStatus,			fname);
  saveInt("nohotkeys",				m_NoHotKeys,		fname);
  saveInt("showtoolbar",			m_ShowToolbar,		fname);
  saveInt("AutoScaling",            m_fAutoScaling,     fname);
  saveInt("fullscreen",			m_FullScreen,		fname);
  saveInt("autoDetect", autoDetect, fname);
  saveInt("8bit",					m_Use8Bit,			fname);
  saveInt("shared",				m_Shared,			fname);
  saveInt("swapmouse",			m_SwapMouse,		fname);
  saveInt("belldeiconify",		m_DeiconifyOnBell,	fname);
  saveInt("emulate3",				m_Emul3Buttons,		fname);
  saveInt("JapKeyboard",				m_JapKeyboard,		fname);
  saveInt("emulate3timeout",		m_Emul3Timeout,		fname);
  saveInt("emulate3fuzz",			m_Emul3Fuzz,		fname);
  saveInt("disableclipboard",		m_DisableClipboard, fname);
  saveInt("localcursor",			m_localCursor,		fname);
  saveInt("Scaling",				m_scaling,		fname);
  saveInt("AutoScaling",			m_fAutoScaling,		fname);
  saveInt("scale_num",			m_scale_num,		fname);
  saveInt("scale_den",			m_scale_den,		fname);
  // Tight Specific
  saveInt("cursorshape",			m_requestShapeUpdates, fname);
  saveInt("noremotecursor",		m_ignoreShapeUpdates, fname);
  if (m_useCompressLevel) {
	saveInt("compresslevel",	m_compressLevel,	fname);
  }
  if (m_enableJpegCompression) {
	saveInt("quality",			m_jpegQualityLevel,	fname);
  }

  // Modif sf@2002
  saveInt("ServerScale",			m_nServerScale,		fname);
  saveInt("Reconnect",				m_reconnectcounter,		fname);
  saveInt("EnableCache",			m_fEnableCache,		fname);
  saveInt("QuickOption",			m_quickoption,	fname);
  saveInt("UseDSMPlugin",			m_fUseDSMPlugin,	fname);
  saveInt("UseProxy",				m_fUseProxy,	fname);

  WritePrivateProfileString("options", "DSMPlugin", m_szDSMPluginFilename, fname);
  //saveInt("AutoReconnect", m_autoReconnect,	fname);
 
  saveInt("ExitCheck",				m_fExitCheck,	fname); //PGM @ Advantig
}

void VNCOptions::Load(char *fname)
{
  for (int i = rfbEncodingRaw; i<= LASTENCODING; i++) {
    char buf[128];
    sprintf(buf, "use_encoding_%d", i);
    m_UseEnc[i] =   readInt(buf, m_UseEnc[i], fname) != 0;
  }
  m_PreferredEncoding =	readInt("preferred_encoding", m_PreferredEncoding,	fname);
  m_restricted =			readInt("restricted",		m_restricted,	fname) != 0 ;
  m_ViewOnly =			readInt("viewonly",			m_ViewOnly,		fname) != 0;
  m_NoStatus =			readInt("nostatus",			m_NoStatus,		fname) != 0;
  m_NoHotKeys =			readInt("nohotkeys",			m_NoHotKeys,	fname) != 0;
  m_ShowToolbar =			readInt("showtoolbar",			m_ShowToolbar,		fname) != 0;
  m_fAutoScaling =      readInt("AutoScaling",			m_fAutoScaling,		fname) != 0;
  m_FullScreen =			readInt("fullscreen",		m_FullScreen,	fname) != 0;
  autoDetect = readInt("autoDetect", autoDetect, fname) != 0;
  m_Use8Bit =				readInt("8bit",				m_Use8Bit,		fname) != 0;
  m_Shared =				readInt("shared",			m_Shared,		fname) != 0;
  m_SwapMouse =			readInt("swapmouse",		m_SwapMouse,	fname) != 0;
  m_DeiconifyOnBell =		readInt("belldeiconify",	m_DeiconifyOnBell, fname) != 0;
  m_Emul3Buttons =		readInt("emulate3",			m_Emul3Buttons, fname) != 0;
  m_JapKeyboard  =		readInt("JapKeyboard",			m_JapKeyboard, fname) != 0;
  m_Emul3Timeout =		readInt("emulate3timeout",	m_Emul3Timeout, fname);
  m_Emul3Fuzz =			readInt("emulate3fuzz",		m_Emul3Fuzz,    fname);
  m_DisableClipboard =	readInt("disableclipboard", m_DisableClipboard, fname) != 0;
  m_localCursor =			readInt("localcursor",		m_localCursor,	fname);
  m_scaling =			readInt("Scaling", m_scaling,  fname) != 0;
  m_fAutoScaling =		readInt("AutoScaling", m_fAutoScaling,  fname) != 0;
  m_scale_num =			readInt("scale_num",		m_scale_num,	fname);
  m_scale_den =			readInt("scale_den",		m_scale_den,	fname);
  // Tight specific
  m_requestShapeUpdates =	readInt("cursorshape",		m_requestShapeUpdates, fname) != 0;
  m_ignoreShapeUpdates =	readInt("noremotecursor",	m_ignoreShapeUpdates, fname) != 0;
  int level =				readInt("compresslevel",	-1,				fname);
  if (level != -1) {
	m_useCompressLevel = true;
	m_compressLevel = level;
  }
  level =					readInt("quality",			-1,				fname);
  if (level != -1) {
	m_enableJpegCompression = true;
	m_jpegQualityLevel = level;
  }
  // Modif sf@2002
  m_nServerScale =		readInt("ServerScale",		m_nServerScale,	fname);
  m_reconnectcounter = readInt("Reconnect",		m_reconnectcounter,	fname);
  m_fEnableCache =		readInt("EnableCache",		m_fEnableCache,	fname) != 0;
  m_quickoption  =		readInt("QuickOption",		m_quickoption, fname);
  m_fUseDSMPlugin =		readInt("UseDSMPlugin",		m_fUseDSMPlugin, fname) != 0;
  m_fUseProxy =			readInt("UseProxy",			m_fUseProxy, fname) != 0;
  GetPrivateProfileString("options", "DSMPlugin", "NoPlugin", m_szDSMPluginFilename, MAX_PATH, fname);

  //m_autoReconnect =		readInt("AutoReconnect",	m_autoReconnect, fname) != 0;
  
  m_fExitCheck =		readInt("ExitCheck", m_fExitCheck,  fname) != 0; //PGM @ Advantig

}

// Record the path to the VNC viewer and the type
// of the .vnc files in the registry
void VNCOptions::Register()
{
  char keybuf[_MAX_PATH * 2 + 20];
  HKEY hKey, hKey2;
  if ( RegCreateKey(HKEY_CLASSES_ROOT, ".vnc", &hKey)  == ERROR_SUCCESS ) {
    RegSetValue(hKey, NULL, REG_SZ, "VncViewer.Config", 0);
    RegCloseKey(hKey);
  } else {
    vnclog.Print(0, "Failed to register .vnc extension\n");
  }

  char filename[_MAX_PATH];
  if (GetModuleFileName(NULL, filename, _MAX_PATH) == 0) {
    vnclog.Print(0, "Error getting vncviewer filename\n");
    return;
  }
  vnclog.Print(2, "Viewer is %s\n", filename);

  if ( RegCreateKey(HKEY_CLASSES_ROOT, "VncViewer.Config", &hKey)  == ERROR_SUCCESS ) {
    RegSetValue(hKey, NULL, REG_SZ, "VNCviewer Config File", 0);
		
    if ( RegCreateKey(hKey, "DefaultIcon", &hKey2)  == ERROR_SUCCESS ) {
      sprintf(keybuf, "%s,0", filename);
      RegSetValue(hKey2, NULL, REG_SZ, keybuf, 0);
      RegCloseKey(hKey2);
    }
    if ( RegCreateKey(hKey, "Shell\\open\\command", &hKey2)  == ERROR_SUCCESS ) {
      sprintf(keybuf, "\"%s\" -config \"%%1\"", filename);
      RegSetValue(hKey2, NULL, REG_SZ, keybuf, 0);
      RegCloseKey(hKey2);
    }

    RegCloseKey(hKey);
  }

  if ( RegCreateKey(HKEY_LOCAL_MACHINE, 
                    "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\vncviewer.exe", 
                    &hKey)  == ERROR_SUCCESS ) {
    RegSetValue(hKey, NULL, REG_SZ, filename, 0);
    RegCloseKey(hKey);
  }
}

void VNCOptions::ShowUsage(LPTSTR info) {
  TCHAR msg[1024];
  TCHAR *tmpinf = _T("");
  if (info != NULL) 
    tmpinf = info;
  _stprintf(msg, 
#ifdef UNDER_CE
            _T("%s\n\rUsage includes:\n\r")
            _T("vncviewer [/8bit] [/swapmouse] [/shared] [/belldeiconify] \n\r")
            _T(" [/hpc | /palm] [/slow] [server:display] \n\r")
            _T("For full details see documentation."),
#else
            _T("%s\n\rUsage includes:\n\r"
               "  vncviewer [/8bit] [/swapmouse] [/shared] [/belldeiconify] \n\r"
               "      [/listen [portnum]] [/fullscreen] [/viewonly] [/notoolbar]\n\r"
               "      [/scale a/b] [/config configfile] [server:display] \n\r"
			   "      [/emulate3] [/quickoption n] [/serverscale n]\n\r"
			   "      [/silentexit] [/user msuser] [/password clearpassword]\n\r" // Added silentexit //PGM@ Advantig.com
			   "      [/nostatus] [/dsmplugin pluginfilename.dsm] [/autoscaling] \n\r"
			   "      [/autoreconnect delayInSeconds] \n\r"
			   "      [/nohotkeys] [/proxy proxyhost [portnum]] [/256colors] [/64colors]\r\n"
			   "      [/8colors] [/8greycolors] [/4greycolors] [/2greycolors]\r\n\r\n"
               "For full details see documentation."), 
#endif
            tmpinf);
  MessageBox(NULL,  msg, sz_A2, MB_OK | MB_ICONINFORMATION | MB_TOPMOST);
}

// The dialog box allows you to change the session-specific parameters
int VNCOptions::DoDialog(bool running)
{
  m_running = running;
  //desativa a instanciação janela de opções
  /*return DialogBoxParam(pApp->m_instance, DIALOG_MAKEINTRESOURCE(IDD_OPTIONDIALOG), 
                        NULL, (DLGPROC) OptDlgProc, (LONG) this);*/
  return NULL;
}

BOOL CALLBACK VNCOptions::OptDlgProc(  HWND hwnd,  UINT uMsg,  
                                       WPARAM wParam, LPARAM lParam ) {
  // This is a static method, so we don't know which instantiation we're 
  // dealing with. But we can get a pseudo-this from the parameter to 
  // WM_INITDIALOG, which we therafter store with the window and retrieve
  // as follows:
#ifndef _X64
  VNCOptions *_this = (VNCOptions *) GetWindowLong(hwnd, GWL_USERDATA);
#else
	VNCOptions *_this = (VNCOptions *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
#endif
	
  switch (uMsg) {
		
  case WM_INITDIALOG:
	  {
#ifndef _X64
		SetWindowLong(hwnd, GWL_USERDATA, lParam);
#else
		SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
#endif
		  _this = (VNCOptions *) lParam;
		  // Initialise the controls
		  
		  // Window always on top
		  RECT Rect;
		  GetWindowRect(hwnd, &Rect);
		  SetWindowPos(hwnd, 
			  HWND_TOPMOST,
			  Rect.left,
			  Rect.top,
			  Rect.right - Rect.left,
			  Rect.bottom - Rect.top,
			  SWP_SHOWWINDOW);
		  
		  HWND had = GetDlgItem(hwnd, IDC_AUTODETECT);
		  SendMessage(had, BM_SETCHECK, _this->autoDetect, 0);
		  int i = 0;	  
		  for (i = rfbEncodingRaw; i <= LASTENCODING; i++) {
			  HWND hPref = GetDlgItem(hwnd, IDC_RAWRADIO + (i-rfbEncodingRaw));
			  SendMessage(hPref, BM_SETCHECK, 
				  (i== _this->m_PreferredEncoding), 0);
			  EnableWindow(hPref, _this->m_UseEnc[i] && !_this->autoDetect);
		  }
		  
		  HWND hCopyRect = GetDlgItem(hwnd, ID_SESSION_SET_CRECT);
		  SendMessage(hCopyRect, BM_SETCHECK, _this->m_UseEnc[rfbEncodingCopyRect], 0);
		  EnableWindow(hCopyRect, !_this->autoDetect);
		  
		  HWND hSwap = GetDlgItem(hwnd, ID_SESSION_SWAPMOUSE);
		  SendMessage(hSwap, BM_SETCHECK, _this->m_SwapMouse, 0);
		  
		  // Tight 
		  HWND hAcl = GetDlgItem(hwnd, IDC_ALLOW_COMPRESSLEVEL);
		  EnableWindow(hAcl, !_this->autoDetect);
		  
		  HWND hCl = GetDlgItem(hwnd, IDC_COMPRESSLEVEL);
		  EnableWindow(hCl, !_this->autoDetect);
		  
		  HWND hAj = GetDlgItem(hwnd, IDC_ALLOW_JPEG);
		  EnableWindow(hAj, !_this->autoDetect);
		  
		  HWND hQl = GetDlgItem(hwnd, IDC_QUALITYLEVEL);
		  EnableWindow(hQl, !_this->autoDetect);
		  
		  HWND hDeiconify = GetDlgItem(hwnd, IDC_BELLDEICONIFY);
		  SendMessage(hDeiconify, BM_SETCHECK, _this->m_DeiconifyOnBell, 0);
		  
#ifndef UNDER_CE
		  HWND hDisableClip = GetDlgItem(hwnd, IDC_DISABLECLIPBOARD);
		  SendMessage(hDisableClip, BM_SETCHECK, _this->m_DisableClipboard, 0);
#endif			
		  

		  // sf@2005 - New Color depth choice
		  HWND hColorMode;
		  switch (_this->m_Use8Bit)
		  {
		  case rfbPFFullColors:
			  hColorMode = GetDlgItem(hwnd, IDC_FULLCOLORS_RADIO);
			  break;
		  case rfbPF256Colors:
			  hColorMode = GetDlgItem(hwnd, IDC_256COLORS_RADIO);
			  break;
		  case rfbPF64Colors:
			  hColorMode = GetDlgItem(hwnd, IDC_64COLORS_RADIO);
			  break;
		  case rfbPF8Colors:
			  hColorMode = GetDlgItem(hwnd, IDC_8COLORS_RADIO);
			  break;
		  case rfbPF8GreyColors:
			  hColorMode = GetDlgItem(hwnd, IDC_8GREYCOLORS_RADIO);
			  break;
		  case rfbPF4GreyColors:
			  hColorMode = GetDlgItem(hwnd, IDC_4GREYCOLORS_RADIO);
			  break;
		  case rfbPF2GreyColors:
			  hColorMode = GetDlgItem(hwnd, IDC_2GREYCOLORS_RADIO);
			  break;
		  }
		  SendMessage(hColorMode, BM_SETCHECK,	true, 0);

   	      // sf@2005 - New color depth choice
		  hColorMode = GetDlgItem(hwnd, IDC_FULLCOLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_256COLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_64COLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_8COLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_8GREYCOLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_4GREYCOLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);
		  hColorMode = GetDlgItem(hwnd, IDC_2GREYCOLORS_RADIO);
		  EnableWindow(hColorMode, !_this->autoDetect);


		  HWND hShared = GetDlgItem(hwnd, IDC_SHARED);
		  SendMessage(hShared, BM_SETCHECK, _this->m_Shared, 0);
		  EnableWindow(hShared, !_this->m_running);
		  
		  HWND hViewOnly = GetDlgItem(hwnd, IDC_VIEWONLY);
		  SendMessage(hViewOnly, BM_SETCHECK, _this->m_ViewOnly, 0);
		  
		  // Toolbar
		  HWND hShowToolbar = GetDlgItem(hwnd, IDC_SHOWTOOLBAR);
		  SendMessage(hShowToolbar, BM_SETCHECK, _this->m_ShowToolbar, 0);
		  
		  HWND hAutoScaling = GetDlgItem(hwnd, IDC_SCALING);
		  SendMessage(hAutoScaling, BM_SETCHECK, _this->m_fAutoScaling, 0);
		  
		  // SetDlgItemInt( hwnd, IDC_SCALE_NUM, _this->m_scale_num, FALSE);
		  // SetDlgItemInt( hwnd, IDC_SCALE_DEN, _this->m_scale_den, FALSE);
	
		  // Viewer Scaling combo box - Now using percentage
		  // The combo is still editable for customizable value
		  int Scales[13] = { 25, 50, 75, 80, 85, 90, 95, 100, 125, 150, 200, 300, 400};
		  HWND hViewerScale = GetDlgItem(hwnd, IDC_SCALE_CB);
		  char szPer[4];
		  for (i = 0; i <= 12; i++)
		  {
			  _itoa(Scales[i], szPer, 10);
			  SendMessage(hViewerScale, CB_INSERTSTRING, (WPARAM)i, (LPARAM)(int FAR*)szPer);
		  }
		  SetDlgItemInt(hwnd,
			            IDC_SCALE_CB,
						(( _this->m_scale_num * 100) / _this->m_scale_den),
						FALSE);
				  
		  // Modif sf@2002 - Server Scaling
		  SetDlgItemInt( hwnd, IDC_SERVER_SCALE, _this->m_nServerScale, FALSE);

		  if (_this->m_Shared)
		  SetDlgItemInt( hwnd, IDC_SERVER_RECON, _this->m_reconnectcounter, FALSE);
		  else SetDlgItemInt( hwnd, IDC_SERVER_RECON, 0, FALSE);
		  
		  // Modif sf@2002 - Cache 
		  HWND hCache = GetDlgItem(hwnd, ID_SESSION_SET_CACHE);
		  SendMessage(hCache, BM_SETCHECK, _this->m_fEnableCache, 0);
		  EnableWindow(hCache, !_this->autoDetect);
		  
		  
#ifndef UNDER_CE
		  HWND hFullScreen = GetDlgItem(hwnd, IDC_FULLSCREEN);
		  SendMessage(hFullScreen, BM_SETCHECK, _this->m_FullScreen, 0);
		  
		  HWND hEmulate = GetDlgItem(hwnd, IDC_EMULATECHECK);
		  SendMessage(hEmulate, BM_SETCHECK, _this->m_Emul3Buttons, 0);

		  HWND hJapkeyboard = GetDlgItem(hwnd, IDC_JAPKEYBOARD);
		  SendMessage(hJapkeyboard, BM_SETCHECK, _this->m_JapKeyboard, 0);
#endif

		  

		  // Tight Specific
		  HWND hAllowCompressLevel = GetDlgItem(hwnd, IDC_ALLOW_COMPRESSLEVEL);
		  SendMessage(hAllowCompressLevel, BM_SETCHECK, _this->m_useCompressLevel, 0);
		  
		  HWND hAllowJpeg = GetDlgItem(hwnd, IDC_ALLOW_JPEG);
		  SendMessage(hAllowJpeg, BM_SETCHECK, _this->m_enableJpegCompression, 0);
		  
		  SetDlgItemInt( hwnd, IDC_COMPRESSLEVEL, _this->m_compressLevel, FALSE);
		  SetDlgItemInt( hwnd, IDC_QUALITYLEVEL, _this->m_jpegQualityLevel, FALSE);
		  
		  
		  HWND hRemoteCursor;
		  if (_this->m_requestShapeUpdates && !_this->m_ignoreShapeUpdates) {
			  hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_ENABLE_RADIO);
		  } else if (_this->m_requestShapeUpdates) {
			  hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_IGNORE_RADIO);
		  } else {
			  hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_DISABLE_RADIO);
		  }
		  SendMessage(hRemoteCursor, BM_SETCHECK,	true, 0);
		  
		  CentreWindow(hwnd);
		  SetForegroundWindow(hwnd);
		  
		  HWND hExitCheck = GetDlgItem(hwnd, IDC_EXIT_CHECK); //PGM @ Advantig
		  SendMessage(hExitCheck, BM_SETCHECK, _this->m_fExitCheck, 0); //PGM @ Advantig
		  
		  return TRUE;
    }
	
  case WM_COMMAND:
	  switch (LOWORD(wParam))
	  {
	  case IDOK:
		  {
			  HWND had = GetDlgItem(hwnd, IDC_AUTODETECT);
			  _this->autoDetect =
				  (SendMessage(had, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  for (int i = rfbEncodingRaw; i <= LASTENCODING; i++) {
				  HWND hPref = GetDlgItem(hwnd, IDC_RAWRADIO+i-rfbEncodingRaw);
				  if (SendMessage(hPref, BM_GETCHECK, 0, 0) == BST_CHECKED)
					  _this->m_PreferredEncoding = i;
			  }

			// [v1.0.2-jp2 fix-->]
			  if (SendMessage(GetDlgItem(hwnd, IDC_ULTRA), BM_GETCHECK, 0, 0) == BST_CHECKED){
					SendMessage(GetDlgItem(hwnd, ID_SESSION_SET_CRECT), BM_SETCHECK, false, 0);
			  }
			// [<--v1.0.2-jp2 fix]
			  
			  HWND hCopyRect = GetDlgItem(hwnd, ID_SESSION_SET_CRECT);
			  _this->m_UseEnc[rfbEncodingCopyRect] =
				  (SendMessage(hCopyRect, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  // Modif sf@2002 - Cache - v1.1.0				
			  HWND hCache = GetDlgItem(hwnd, ID_SESSION_SET_CACHE);
			  _this->m_fEnableCache =
				  (SendMessage(hCache, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hSwap = GetDlgItem(hwnd, ID_SESSION_SWAPMOUSE);
			  _this->m_SwapMouse =
				  (SendMessage(hSwap, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hDeiconify = GetDlgItem(hwnd, IDC_BELLDEICONIFY);
			  _this->m_DeiconifyOnBell =
				  (SendMessage(hDeiconify, BM_GETCHECK, 0, 0) == BST_CHECKED);
#ifndef UNDER_CE				
			  HWND hDisableClip = GetDlgItem(hwnd, IDC_DISABLECLIPBOARD);
			  _this->m_DisableClipboard =
				  (SendMessage(hDisableClip, BM_GETCHECK, 0, 0) == BST_CHECKED);
#endif
			  
			  // sd@2005 - New Color depth choice
			  HWND hColorMode = GetDlgItem(hwnd, IDC_FULLCOLORS_RADIO);
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPFFullColors;
			  hColorMode = GetDlgItem(hwnd, IDC_256COLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF256Colors;
			  hColorMode = GetDlgItem(hwnd, IDC_64COLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF64Colors;
			  hColorMode = GetDlgItem(hwnd, IDC_8COLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF8Colors;
			  hColorMode = GetDlgItem(hwnd, IDC_8GREYCOLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF8GreyColors;
			  hColorMode = GetDlgItem(hwnd, IDC_4GREYCOLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF4GreyColors;
			  hColorMode = GetDlgItem(hwnd, IDC_2GREYCOLORS_RADIO);			  
			  if (SendMessage(hColorMode, BM_GETCHECK, 0, 0) == BST_CHECKED)
				  _this->m_Use8Bit = rfbPF2GreyColors;


			  HWND hShared = GetDlgItem(hwnd, IDC_SHARED);
			  _this->m_Shared =
				  (SendMessage(hShared, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hViewOnly = GetDlgItem(hwnd, IDC_VIEWONLY);
			  _this->m_ViewOnly = 
				  (SendMessage(hViewOnly, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hShowToolbar = GetDlgItem(hwnd, IDC_SHOWTOOLBAR);
			  _this->m_ShowToolbar = 
				  (SendMessage(hShowToolbar, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hAutoScaling = GetDlgItem(hwnd, IDC_SCALING);
			  _this->m_fAutoScaling = (SendMessage(hAutoScaling, BM_GETCHECK, 0, 0) == BST_CHECKED);
			 
			  HWND hViewerScaling = GetDlgItem(hwnd, IDC_SCALE_CB);
			  int nErr;
			  int nPer = GetDlgItemInt(hwnd, IDC_SCALE_CB, &nErr, FALSE);
			  if (nPer > 0)
			  {
				  _this->m_scale_num = nPer;
				  _this->m_scale_den = 100;
			  }
			  _this->m_scaling = !(_this->m_scale_num == 100);

			  if (_this->m_scaling || _this->m_fAutoScaling)
			  {
				  // _this->m_scale_num = GetDlgItemInt( hwnd, IDC_SCALE_NUM, NULL, TRUE);
				  // _this->m_scale_den = GetDlgItemInt( hwnd, IDC_SCALE_DEN, NULL, TRUE);
				  // Modif sf@2002 - Server Scaling
				  _this->m_nServerScale = GetDlgItemInt( hwnd, IDC_SERVER_SCALE, NULL, TRUE);
				  _this->FixScaling();
				  //if (_this->m_scale_num == 1 && _this->m_scale_den == 1)
				  // 	  _this->m_scaling = false;
			  }
			  else
			  {
				  _this->m_scale_num = 1;
				  _this->m_scale_den = 1;
				  // Modif sf@2002 - Server Scaling
				  _this->m_nServerScale = GetDlgItemInt( hwnd, IDC_SERVER_SCALE, NULL, TRUE);
				  if (_this->m_nServerScale < 1 || _this->m_nServerScale > 9) 
					  _this->m_nServerScale = 1;
			  }
			 if (_this->m_Shared)
			  _this->m_reconnectcounter = GetDlgItemInt( hwnd, IDC_SERVER_RECON, NULL, TRUE);
			 else _this->m_reconnectcounter=0;


			  
#ifndef UNDER_CE
			  HWND hFullScreen = GetDlgItem(hwnd, IDC_FULLSCREEN);
			  _this->m_FullScreen = 
				  (SendMessage(hFullScreen, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  HWND hEmulate = GetDlgItem(hwnd, IDC_EMULATECHECK);
			  _this->m_Emul3Buttons =
				  (SendMessage(hEmulate, BM_GETCHECK, 0, 0) == BST_CHECKED);

			  HWND hJapkeyboard = GetDlgItem(hwnd, IDC_JAPKEYBOARD);
			  _this->m_JapKeyboard =
				  (SendMessage(hJapkeyboard, BM_GETCHECK, 0, 0) == BST_CHECKED);
#endif
			  
			  // Tight Specific
			  HWND hAllowCompressLevel = GetDlgItem(hwnd, IDC_ALLOW_COMPRESSLEVEL);
			  _this->m_useCompressLevel = 
				  (SendMessage(hAllowCompressLevel, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  _this->m_compressLevel = GetDlgItemInt( hwnd, IDC_COMPRESSLEVEL, NULL, TRUE);
			  if ( _this->m_compressLevel < 0 ) { _this->m_compressLevel = 0; }
			  if ( _this->m_compressLevel > 9 ) { _this->m_compressLevel = 9; }
			  
			  HWND hAllowJpeg = GetDlgItem(hwnd, IDC_ALLOW_JPEG);
			  _this->m_enableJpegCompression = 
				  (SendMessage(hAllowJpeg, BM_GETCHECK, 0, 0) == BST_CHECKED);
			  
			  _this->m_jpegQualityLevel = GetDlgItemInt( hwnd, IDC_QUALITYLEVEL, NULL, TRUE);
			  if ( _this->m_jpegQualityLevel < 0 ) { _this->m_jpegQualityLevel = 0; }
			  if ( _this->m_jpegQualityLevel > 9 ) { _this->m_jpegQualityLevel = 9; }
			  
			  _this->m_requestShapeUpdates = false;
			  _this->m_ignoreShapeUpdates = false;
			  HWND hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_ENABLE_RADIO);
			  if (SendMessage(hRemoteCursor, BM_GETCHECK, 0, 0) == BST_CHECKED) {
				  _this->m_requestShapeUpdates = true;
			  } else {
				  hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_IGNORE_RADIO);
				  if (SendMessage(hRemoteCursor, BM_GETCHECK, 0, 0) == BST_CHECKED) {
					  _this->m_requestShapeUpdates = true;
					  _this->m_ignoreShapeUpdates = true;
				  }
			  }
		  
			  EndDialog(hwnd, TRUE);
			  
			  return TRUE;
      }
	  
    case IDCANCEL:
		EndDialog(hwnd, FALSE);
		return TRUE;
		
    case IDC_AUTODETECT:
		{
            bool ad = IsDlgButtonChecked(hwnd, IDC_AUTODETECT) ? true : false;
		for (int i = rfbEncodingRaw; i <= LASTENCODING; i++)
		{
			HWND hPref = GetDlgItem(hwnd, IDC_RAWRADIO + (i-rfbEncodingRaw));
			EnableWindow(hPref, _this->m_UseEnc[i] && !ad);
		}
		
		HWND hCopyRect = GetDlgItem(hwnd, ID_SESSION_SET_CRECT);
		EnableWindow(hCopyRect, !ad);

		// sf@2005 - New color depth choice
		HWND hColorMode = GetDlgItem(hwnd, IDC_FULLCOLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_256COLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_64COLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_8COLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_8GREYCOLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_4GREYCOLORS_RADIO);
		EnableWindow(hColorMode, !ad);
		hColorMode = GetDlgItem(hwnd, IDC_2GREYCOLORS_RADIO);
		EnableWindow(hColorMode, !ad);


		// sf@2002
		HWND hCache = GetDlgItem(hwnd, ID_SESSION_SET_CACHE);
		EnableWindow(hCache, !ad);
		
		HWND hAcl = GetDlgItem(hwnd, IDC_ALLOW_COMPRESSLEVEL);
		EnableWindow(hAcl, !ad);
		
		HWND hCl = GetDlgItem(hwnd, IDC_COMPRESSLEVEL);
		EnableWindow(hCl, !ad);
		
		HWND hAj = GetDlgItem(hwnd, IDC_ALLOW_JPEG);
		EnableWindow(hAj, !ad);
		
		HWND hQl = GetDlgItem(hwnd, IDC_QUALITYLEVEL);
		EnableWindow(hQl, !ad);
		}
		return TRUE;

	// If Xor Zlib is checked, check Cache encoding as well
	// (the user can still uncheck it if he wants)
    case IDC_ZLIBRADIO:
		{
            bool xor = IsDlgButtonChecked(hwnd, IDC_ZLIBRADIO) ? true : false;
		if (xor)
		{
	 	    HWND hCache = GetDlgItem(hwnd, ID_SESSION_SET_CACHE);
			SendMessage(hCache, BM_SETCHECK, true, 0);
		}
		return TRUE;
		}
	case IDC_ULTRA:
		{
		bool ultra=IsDlgButtonChecked(hwnd, IDC_ULTRA) ? true : false;
		if (ultra)
		{
	 	    HWND hCache = GetDlgItem(hwnd, ID_SESSION_SET_CACHE);
			SendMessage(hCache, BM_SETCHECK, false, 0);
			// [v1.0.2-jp2 fix-->]
			HWND hCopyRect = GetDlgItem(hwnd, ID_SESSION_SET_CRECT);
			SendMessage(hCopyRect, BM_SETCHECK, false, 0);
			// [<--v1.0.2-jp2 fix]
			HWND hRemoteCursor = GetDlgItem(hwnd, IDC_CSHAPE_DISABLE_RADIO);
			SendMessage(hRemoteCursor, BM_SETCHECK,	true, 0);
			HWND hRemoteCursor2 = GetDlgItem(hwnd, IDC_CSHAPE_ENABLE_RADIO);
			SendMessage(hRemoteCursor2, BM_SETCHECK,false, 0);
			HWND hRemoteCursor3 = GetDlgItem(hwnd, IDC_CSHAPE_IGNORE_RADIO);
			SendMessage(hRemoteCursor3, BM_SETCHECK,false, 0);
		}
		return TRUE;
		}

    }
    break;
	

  case WM_DESTROY:
	  EndDialog(hwnd, FALSE);
	  return TRUE;
  }
  return 0;
}