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
|
/*
* dialog.c - dialogs for PuTTY(tel), including the configuration dialog.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include "putty.h"
#include "ssh.h"
#include "putty-rc.h"
#include "win-gui-seat.h"
#include "storage.h"
#include "dialog.h"
#include "licence.h"
#include <commctrl.h>
#include <commdlg.h>
#include <shellapi.h>
#ifdef MSVC4
#define TVINSERTSTRUCT TV_INSERTSTRUCT
#define TVITEM TV_ITEM
#define ICON_BIG 1
#endif
typedef struct PortableDialogStuff {
/*
* These are the various bits of data required to handle a dialog
* box that's been built up from the cross-platform dialog.c
* system.
*/
/* The 'controlbox' that was returned from the portable setup function */
struct controlbox *ctrlbox;
/* The dlgparam that's passed to all the runtime dlg_* functions.
* Declared as an array of 1 so it's convenient to pass it as a pointer. */
struct dlgparam dp[1];
/*
* Collections of instantiated controls. There can be more than
* one of these, because sometimes you want to destroy and
* recreate a subset of them - e.g. when switching panes in the
* main PuTTY config box, you delete and recreate _most_ of the
* controls, but not the OK and Cancel buttons at the bottom.
*/
size_t nctrltrees;
struct winctrls *ctrltrees;
/*
* Flag indicating whether the dialog box has been initialised.
* Used to suppresss spurious firing of message handlers during
* setup.
*/
bool initialised;
} PortableDialogStuff;
/*
* Initialise a PortableDialogStuff, before launching the dialog box.
*/
static PortableDialogStuff *pds_new(size_t nctrltrees)
{
PortableDialogStuff *pds = snew(PortableDialogStuff);
memset(pds, 0, sizeof(*pds));
pds->ctrlbox = ctrl_new_box();
dp_init(pds->dp);
pds->nctrltrees = nctrltrees;
pds->ctrltrees = snewn(pds->nctrltrees, struct winctrls);
for (size_t i = 0; i < pds->nctrltrees; i++) {
winctrl_init(&pds->ctrltrees[i]);
dp_add_tree(pds->dp, &pds->ctrltrees[i]);
}
pds->dp->errtitle = dupprintf("%s Error", appname);
pds->initialised = false;
return pds;
}
static void pds_free(PortableDialogStuff *pds)
{
ctrl_free_box(pds->ctrlbox);
dp_cleanup(pds->dp);
for (size_t i = 0; i < pds->nctrltrees; i++)
winctrl_cleanup(&pds->ctrltrees[i]);
sfree(pds->ctrltrees);
sfree(pds);
}
static INT_PTR pds_default_dlgproc(PortableDialogStuff *pds, HWND hwnd,
UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_LBUTTONUP:
/*
* Button release should trigger WM_OK if there was a
* previous double click on the host CA list.
*/
ReleaseCapture();
if (pds->dp->ended)
ShinyEndDialog(hwnd, pds->dp->endresult ? 1 : 0);
break;
case WM_COMMAND:
case WM_DRAWITEM:
default: { /* also handle drag list msg here */
/*
* Only process WM_COMMAND once the dialog is fully formed.
*/
int ret;
if (pds->initialised) {
ret = winctrl_handle_command(pds->dp, msg, wParam, lParam);
if (pds->dp->ended && GetCapture() != hwnd)
ShinyEndDialog(hwnd, pds->dp->endresult ? 1 : 0);
} else
ret = 0;
return ret;
}
case WM_HELP:
if (!winctrl_context_help(pds->dp,
hwnd, ((LPHELPINFO)lParam)->iCtrlId))
MessageBeep(0);
break;
case WM_CLOSE:
quit_help(hwnd);
ShinyEndDialog(hwnd, 0);
return 0;
/* Grrr Explorer will maximize Dialogs! */
case WM_SIZE:
if (wParam == SIZE_MAXIMIZED)
force_normal(hwnd);
return 0;
}
return 0;
}
static void pds_initdialog_start(PortableDialogStuff *pds, HWND hwnd)
{
pds->dp->hwnd = hwnd;
if (pds->dp->wintitle) /* apply override title, if provided */
SetWindowText(hwnd, pds->dp->wintitle);
/* The portable dialog system generally includes the ability to
* handle context help for particular controls. Enable the
* relevant window styles if we have a help file available. */
if (has_help()) {
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
SetWindowLongPtr(hwnd, GWL_EXSTYLE, style | WS_EX_CONTEXTHELP);
} else {
/* If not, and if the dialog template provided a top-level
* Help button, delete it */
HWND item = GetDlgItem(hwnd, IDC_HELPBTN);
if (item)
DestroyWindow(item);
}
}
/*
* Create the panelfuls of controls in the configuration box.
*/
static void pds_create_controls(
PortableDialogStuff *pds, size_t which_tree, int base_id,
int left, int right, int top, char *path)
{
struct ctlpos cp;
ctlposinit(&cp, pds->dp->hwnd, left, right, top);
for (int index = -1; (index = ctrl_find_path(
pds->ctrlbox, path, index)) >= 0 ;) {
struct controlset *s = pds->ctrlbox->ctrlsets[index];
winctrl_layout(pds->dp, &pds->ctrltrees[which_tree], &cp, s, &base_id);
}
}
static void pds_initdialog_finish(PortableDialogStuff *pds)
{
/*
* Set focus into the first available control in ctrltree #0,
* which the caller was expected to set up to be the one
* containing the dialog controls likely to be used first.
*/
struct winctrl *c;
for (int i = 0; (c = winctrl_findbyindex(&pds->ctrltrees[0], i)) != NULL;
i++) {
if (c->ctrl) {
dlg_set_focus(c->ctrl, pds->dp);
break;
}
}
/*
* Now we've finished creating our initial set of controls,
* it's safe to actually show the window without risking setup
* flicker.
*/
ShowWindow(pds->dp->hwnd, SW_SHOWNORMAL);
pds->initialised = true;
}
#define LOGEVENT_INITIAL_MAX 128
#define LOGEVENT_CIRCULAR_MAX 128
static char *events_initial[LOGEVENT_INITIAL_MAX];
static char *events_circular[LOGEVENT_CIRCULAR_MAX];
static int ninitial = 0, ncircular = 0, circular_first = 0;
#define PRINTER_DISABLED_STRING "None (printing disabled)"
void force_normal(HWND hwnd)
{
static bool recurse = false;
WINDOWPLACEMENT wp;
if (recurse)
return;
recurse = true;
wp.length = sizeof(wp);
if (GetWindowPlacement(hwnd, &wp) && wp.showCmd == SW_SHOWMAXIMIZED) {
wp.showCmd = SW_SHOWNORMAL;
SetWindowPlacement(hwnd, &wp);
}
recurse = false;
}
static char *getevent(int i)
{
if (i < ninitial)
return events_initial[i];
if ((i -= ninitial) < ncircular)
return events_circular[(circular_first + i) % LOGEVENT_CIRCULAR_MAX];
return NULL;
}
static HWND logbox;
HWND event_log_window(void) { return logbox; }
static INT_PTR CALLBACK LogProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
int i;
switch (msg) {
case WM_INITDIALOG: {
char *str = dupprintf("%s Event Log", appname);
SetWindowText(hwnd, str);
sfree(str);
static int tabs[4] = { 78, 108 };
SendDlgItemMessage(hwnd, IDN_LIST, LB_SETTABSTOPS, 2,
(LPARAM) tabs);
for (i = 0; i < ninitial; i++)
SendDlgItemMessage(hwnd, IDN_LIST, LB_ADDSTRING,
0, (LPARAM) events_initial[i]);
for (i = 0; i < ncircular; i++)
SendDlgItemMessage(hwnd, IDN_LIST, LB_ADDSTRING,
0, (LPARAM) events_circular[(circular_first + i) % LOGEVENT_CIRCULAR_MAX]);
return 1;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
logbox = NULL;
SetActiveWindow(GetParent(hwnd));
DestroyWindow(hwnd);
return 0;
case IDN_COPY:
if (HIWORD(wParam) == BN_CLICKED ||
HIWORD(wParam) == BN_DOUBLECLICKED) {
int selcount;
int *selitems;
selcount = SendDlgItemMessage(hwnd, IDN_LIST,
LB_GETSELCOUNT, 0, 0);
if (selcount == 0) { /* don't even try to copy zero items */
MessageBeep(0);
break;
}
selitems = snewn(selcount, int);
if (selitems) {
int count = SendDlgItemMessage(hwnd, IDN_LIST,
LB_GETSELITEMS,
selcount,
(LPARAM) selitems);
int i;
int size;
char *clipdata;
static unsigned char sel_nl[] = SEL_NL;
if (count == 0) { /* can't copy zero stuff */
MessageBeep(0);
break;
}
size = 0;
for (i = 0; i < count; i++)
size +=
strlen(getevent(selitems[i])) + sizeof(sel_nl);
clipdata = snewn(size, char);
if (clipdata) {
char *p = clipdata;
for (i = 0; i < count; i++) {
char *q = getevent(selitems[i]);
int qlen = strlen(q);
memcpy(p, q, qlen);
p += qlen;
memcpy(p, sel_nl, sizeof(sel_nl));
p += sizeof(sel_nl);
}
write_aclip(CLIP_SYSTEM, clipdata, size, true);
sfree(clipdata);
}
sfree(selitems);
for (i = 0; i < (ninitial + ncircular); i++)
SendDlgItemMessage(hwnd, IDN_LIST, LB_SETSEL,
false, i);
}
}
return 0;
}
return 0;
case WM_CLOSE:
logbox = NULL;
SetActiveWindow(GetParent(hwnd));
DestroyWindow(hwnd);
return 0;
}
return 0;
}
static INT_PTR CALLBACK LicenceProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG: {
char *str = dupprintf("%s Licence", appname);
SetWindowText(hwnd, str);
sfree(str);
SetDlgItemText(hwnd, IDA_TEXT, LICENCE_TEXT("\r\n\r\n"));
return 1;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 1);
return 0;
}
return 0;
case WM_CLOSE:
EndDialog(hwnd, 1);
return 0;
}
return 0;
}
static INT_PTR CALLBACK AboutProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
char *str;
switch (msg) {
case WM_INITDIALOG: {
str = dupprintf("About %s", appname);
SetWindowText(hwnd, str);
sfree(str);
char *buildinfo_text = buildinfo("\r\n");
char *text = dupprintf(
"%s\r\n\r\n%s\r\n\r\n%s\r\n\r\n%s",
appname, ver, buildinfo_text,
"\251 " SHORT_COPYRIGHT_DETAILS ". All rights reserved.");
sfree(buildinfo_text);
SetDlgItemText(hwnd, IDA_TEXT, text);
MakeDlgItemBorderless(hwnd, IDA_TEXT);
sfree(text);
return 1;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hwnd, true);
return 0;
case IDA_LICENCE:
EnableWindow(hwnd, 0);
DialogBox(hinst, MAKEINTRESOURCE(IDD_LICENCEBOX),
hwnd, LicenceProc);
EnableWindow(hwnd, 1);
SetActiveWindow(hwnd);
return 0;
case IDA_WEB:
/* Load web browser */
ShellExecute(hwnd, "open",
"https://www.chiark.greenend.org.uk/~sgtatham/putty/",
0, 0, SW_SHOWDEFAULT);
return 0;
}
return 0;
case WM_CLOSE:
EndDialog(hwnd, true);
return 0;
}
return 0;
}
/*
* Null dialog procedure.
*/
static INT_PTR CALLBACK NullDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
return 0;
}
enum {
IDCX_ABOUT = IDC_ABOUT,
IDCX_TVSTATIC,
IDCX_TREEVIEW,
IDCX_STDBASE,
IDCX_PANELBASE = IDCX_STDBASE + 32
};
struct treeview_faff {
HWND treeview;
HTREEITEM lastat[4];
};
static HTREEITEM treeview_insert(struct treeview_faff *faff,
int level, char *text, char *path)
{
TVINSERTSTRUCT ins;
int i;
HTREEITEM newitem;
ins.hParent = (level > 0 ? faff->lastat[level - 1] : TVI_ROOT);
ins.hInsertAfter = faff->lastat[level];
#if _WIN32_IE >= 0x0400 && defined NONAMELESSUNION
#define INSITEM DUMMYUNIONNAME.item
#else
#define INSITEM item
#endif
ins.INSITEM.mask = TVIF_TEXT | TVIF_PARAM;
ins.INSITEM.pszText = text;
ins.INSITEM.cchTextMax = strlen(text)+1;
ins.INSITEM.lParam = (LPARAM)path;
newitem = TreeView_InsertItem(faff->treeview, &ins);
if (level > 0)
TreeView_Expand(faff->treeview, faff->lastat[level - 1],
(level > 1 ? TVE_COLLAPSE : TVE_EXPAND));
faff->lastat[level] = newitem;
for (i = level + 1; i < 4; i++)
faff->lastat[i] = NULL;
return newitem;
}
const char *dialog_box_demo_screenshot_filename = NULL;
/* ctrltrees indices for the main dialog box */
enum {
TREE_PANEL, /* things we swap out every time treeview selects a new pane */
TREE_BASE, /* fixed things at the bottom like OK and Cancel buttons */
};
/*
* This function is the configuration box.
* (Being a dialog procedure, in general it returns 0 if the default
* dialog processing should be performed, and 1 if it should not.)
*/
static INT_PTR GenericMainDlgProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam, void *ctx)
{
PortableDialogStuff *pds = (PortableDialogStuff *)ctx;
const int DEMO_SCREENSHOT_TIMER_ID = 1230;
HWND treeview;
struct treeview_faff tvfaff;
switch (msg) {
case WM_INITDIALOG:
pds_initdialog_start(pds, hwnd);
pds_create_controls(pds, TREE_BASE, IDCX_STDBASE, 3, 3, 235, "");
SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
(LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(IDI_CFGICON)));
centre_window(hwnd);
/*
* Create the tree view.
*/
{
RECT r;
WPARAM font;
HWND tvstatic;
r.left = 3;
r.right = r.left + 95;
r.top = 3;
r.bottom = r.top + 10;
MapDialogRect(hwnd, &r);
tvstatic = CreateWindowEx(0, "STATIC", "Cate&gory:",
WS_CHILD | WS_VISIBLE,
r.left, r.top,
r.right - r.left, r.bottom - r.top,
hwnd, (HMENU) IDCX_TVSTATIC, hinst,
NULL);
font = SendMessage(hwnd, WM_GETFONT, 0, 0);
SendMessage(tvstatic, WM_SETFONT, font, MAKELPARAM(true, 0));
r.left = 3;
r.right = r.left + 95;
r.top = 13;
r.bottom = r.top + 219;
MapDialogRect(hwnd, &r);
treeview = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
WS_CHILD | WS_VISIBLE |
WS_TABSTOP | TVS_HASLINES |
TVS_DISABLEDRAGDROP | TVS_HASBUTTONS
| TVS_LINESATROOT |
TVS_SHOWSELALWAYS, r.left, r.top,
r.right - r.left, r.bottom - r.top,
hwnd, (HMENU) IDCX_TREEVIEW, hinst,
NULL);
font = SendMessage(hwnd, WM_GETFONT, 0, 0);
SendMessage(treeview, WM_SETFONT, font, MAKELPARAM(true, 0));
tvfaff.treeview = treeview;
memset(tvfaff.lastat, 0, sizeof(tvfaff.lastat));
}
/*
* Set up the tree view contents.
*/
{
HTREEITEM hfirst = NULL;
int i;
char *path = NULL;
char *firstpath = NULL;
for (i = 0; i < pds->ctrlbox->nctrlsets; i++) {
struct controlset *s = pds->ctrlbox->ctrlsets[i];
HTREEITEM item;
int j;
char *c;
if (!s->pathname[0])
continue;
j = path ? ctrl_path_compare(s->pathname, path) : 0;
if (j == INT_MAX)
continue; /* same path, nothing to add to tree */
/*
* We expect never to find an implicit path
* component. For example, we expect never to see
* A/B/C followed by A/D/E, because that would
* _implicitly_ create A/D. All our path prefixes
* are expected to contain actual controls and be
* selectable in the treeview; so we would expect
* to see A/D _explicitly_ before encountering
* A/D/E.
*/
assert(j == ctrl_path_elements(s->pathname) - 1);
c = strrchr(s->pathname, '/');
if (!c)
c = s->pathname;
else
c++;
item = treeview_insert(&tvfaff, j, c, s->pathname);
if (!hfirst) {
hfirst = item;
firstpath = s->pathname;
}
path = s->pathname;
}
/*
* Put the treeview selection on to the first panel in the
* ctrlbox.
*/
TreeView_SelectItem(treeview, hfirst);
/*
* And create the actual control set for that panel, to
* match the initial treeview selection.
*/
assert(firstpath); /* config.c must have given us _something_ */
pds_create_controls(pds, TREE_PANEL, IDCX_PANELBASE,
100, 3, 13, firstpath);
dlg_refresh(NULL, pds->dp); /* and set up control values */
}
if (dialog_box_demo_screenshot_filename)
SetTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID, TICKSPERSEC, NULL);
pds_initdialog_finish(pds);
return 0;
case WM_TIMER:
if (dialog_box_demo_screenshot_filename &&
(UINT_PTR)wParam == DEMO_SCREENSHOT_TIMER_ID) {
KillTimer(hwnd, DEMO_SCREENSHOT_TIMER_ID);
char *err = save_screenshot(
hwnd, dialog_box_demo_screenshot_filename);
if (err) {
MessageBox(hwnd, err, "Demo screenshot failure",
MB_OK | MB_ICONERROR);
sfree(err);
}
ShinyEndDialog(hwnd, 0);
}
return 0;
case WM_NOTIFY:
if (LOWORD(wParam) == IDCX_TREEVIEW &&
((LPNMHDR) lParam)->code == TVN_SELCHANGED) {
/*
* Selection-change events on the treeview cause us to do
* a flurry of control deletion and creation - but only
* after WM_INITDIALOG has finished. The initial
* selection-change event(s) during treeview setup are
* ignored.
*/
HTREEITEM i;
TVITEM item;
char buffer[64];
if (!pds->initialised)
return 0;
i = TreeView_GetSelection(((LPNMHDR) lParam)->hwndFrom);
SendMessage (hwnd, WM_SETREDRAW, false, 0);
item.hItem = i;
item.pszText = buffer;
item.cchTextMax = sizeof(buffer);
item.mask = TVIF_TEXT | TVIF_PARAM;
TreeView_GetItem(((LPNMHDR) lParam)->hwndFrom, &item);
{
/* Destroy all controls in the currently visible panel. */
int k;
HWND item;
struct winctrl *c;
while ((c = winctrl_findbyindex(
&pds->ctrltrees[TREE_PANEL], 0)) != NULL) {
for (k = 0; k < c->num_ids; k++) {
item = GetDlgItem(hwnd, c->base_id + k);
if (item)
DestroyWindow(item);
}
winctrl_rem_shortcuts(pds->dp, c);
winctrl_remove(&pds->ctrltrees[TREE_PANEL], c);
sfree(c->data);
sfree(c);
}
}
pds_create_controls(pds, TREE_PANEL, IDCX_PANELBASE,
100, 3, 13, (char *)item.lParam);
dlg_refresh(NULL, pds->dp); /* set up control values */
SendMessage (hwnd, WM_SETREDRAW, true, 0);
InvalidateRect (hwnd, NULL, true);
SetFocus(((LPNMHDR) lParam)->hwndFrom); /* ensure focus stays */
}
return 0;
default:
return pds_default_dlgproc(pds, hwnd, msg, wParam, lParam);
}
}
void modal_about_box(HWND hwnd)
{
EnableWindow(hwnd, 0);
DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, AboutProc);
EnableWindow(hwnd, 1);
SetActiveWindow(hwnd);
}
void show_help(HWND hwnd)
{
launch_help(hwnd, NULL);
}
void defuse_showwindow(void)
{
/*
* Work around the fact that the app's first call to ShowWindow
* will ignore the default in favour of the shell-provided
* setting.
*/
{
HWND hwnd;
hwnd = CreateDialog(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX),
NULL, NullDlgProc);
ShowWindow(hwnd, SW_HIDE);
SetActiveWindow(hwnd);
DestroyWindow(hwnd);
}
}
bool do_config(Conf *conf)
{
bool ret;
PortableDialogStuff *pds = pds_new(2);
setup_config_box(pds->ctrlbox, false, 0, 0);
win_setup_config_box(pds->ctrlbox, &pds->dp->hwnd, has_help(), false, 0);
pds->dp->wintitle = dupprintf("%s Configuration", appname);
pds->dp->data = conf;
dlg_auto_set_fixed_pitch_flag(pds->dp);
pds->dp->shortcuts['g'] = true; /* the treeview: `Cate&gory' */
ret = ShinyDialogBox(hinst, MAKEINTRESOURCE(IDD_MAINBOX), "PuTTYConfigBox",
NULL, GenericMainDlgProc, pds);
pds_free(pds);
return ret;
}
bool do_reconfig(HWND hwnd, Conf *conf, int protcfginfo)
{
Conf *backup_conf;
bool ret;
int protocol;
PortableDialogStuff *pds = pds_new(2);
backup_conf = conf_copy(conf);
protocol = conf_get_int(conf, CONF_protocol);
setup_config_box(pds->ctrlbox, true, protocol, protcfginfo);
win_setup_config_box(pds->ctrlbox, &pds->dp->hwnd, has_help(),
true, protocol);
pds->dp->wintitle = dupprintf("%s Reconfiguration", appname);
pds->dp->data = conf;
dlg_auto_set_fixed_pitch_flag(pds->dp);
pds->dp->shortcuts['g'] = true; /* the treeview: `Cate&gory' */
ret = ShinyDialogBox(hinst, MAKEINTRESOURCE(IDD_MAINBOX), "PuTTYConfigBox",
NULL, GenericMainDlgProc, pds);
pds_free(pds);
if (!ret)
conf_copy_into(conf, backup_conf);
conf_free(backup_conf);
return ret;
}
static void win_gui_eventlog(LogPolicy *lp, const char *string)
{
char timebuf[40];
char **location;
struct tm tm;
tm=ltime();
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
if (ninitial < LOGEVENT_INITIAL_MAX)
location = &events_initial[ninitial];
else
location = &events_circular[(circular_first + ncircular) % LOGEVENT_CIRCULAR_MAX];
if (*location)
sfree(*location);
*location = dupcat(timebuf, string);
if (logbox) {
int count;
SendDlgItemMessage(logbox, IDN_LIST, LB_ADDSTRING,
0, (LPARAM) *location);
count = SendDlgItemMessage(logbox, IDN_LIST, LB_GETCOUNT, 0, 0);
SendDlgItemMessage(logbox, IDN_LIST, LB_SETTOPINDEX, count - 1, 0);
}
if (ninitial < LOGEVENT_INITIAL_MAX) {
ninitial++;
} else if (ncircular < LOGEVENT_CIRCULAR_MAX) {
ncircular++;
} else if (ncircular == LOGEVENT_CIRCULAR_MAX) {
circular_first = (circular_first + 1) % LOGEVENT_CIRCULAR_MAX;
sfree(events_circular[circular_first]);
events_circular[circular_first] = dupstr("..");
}
}
static void win_gui_logging_error(LogPolicy *lp, const char *event)
{
WinGuiSeat *wgs = container_of(lp, WinGuiSeat, logpolicy);
/* Send 'can't open log file' errors to the terminal window.
* (Marked as stderr, although terminal.c won't care.) */
seat_stderr_pl(&wgs->seat, ptrlen_from_asciz(event));
seat_stderr_pl(&wgs->seat, PTRLEN_LITERAL("\r\n"));
}
void showeventlog(HWND hwnd)
{
if (!logbox) {
logbox = CreateDialog(hinst, MAKEINTRESOURCE(IDD_LOGBOX),
hwnd, LogProc);
ShowWindow(logbox, SW_SHOWNORMAL);
}
SetActiveWindow(logbox);
}
void showabout(HWND hwnd)
{
DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, AboutProc);
}
struct hostkey_dialog_ctx {
SeatDialogText *text;
bool has_title;
const char *helpctx;
};
static INT_PTR HostKeyMoreInfoProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam, void *vctx)
{
struct hostkey_dialog_ctx *ctx = (struct hostkey_dialog_ctx *)vctx;
switch (msg) {
case WM_INITDIALOG: {
int index = 100, y = 12;
WPARAM font = SendMessage(hwnd, WM_GETFONT, 0, 0);
const char *key = NULL;
for (SeatDialogTextItem *item = ctx->text->items,
*end = item + ctx->text->nitems; item < end; item++) {
switch (item->type) {
case SDT_MORE_INFO_KEY:
key = item->text;
break;
case SDT_MORE_INFO_VALUE_SHORT:
case SDT_MORE_INFO_VALUE_BLOB: {
RECT rk, rv;
DWORD editstyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP |
ES_AUTOHSCROLL | ES_READONLY;
if (item->type == SDT_MORE_INFO_VALUE_BLOB) {
rk.left = 12;
rk.right = 376;
rk.top = y;
rk.bottom = 8;
y += 10;
editstyle |= ES_MULTILINE;
rv.left = 12;
rv.right = 376;
rv.top = y;
rv.bottom = 64;
y += 68;
} else {
rk.left = 12;
rk.right = 80;
rk.top = y+2;
rk.bottom = 8;
rv.left = 100;
rv.right = 288;
rv.top = y;
rv.bottom = 12;
y += 16;
}
MapDialogRect(hwnd, &rk);
HWND ctl = CreateWindowEx(
0, "STATIC", key, WS_CHILD | WS_VISIBLE,
rk.left, rk.top, rk.right, rk.bottom,
hwnd, (HMENU)(ULONG_PTR)index++, hinst, NULL);
SendMessage(ctl, WM_SETFONT, font, MAKELPARAM(true, 0));
MapDialogRect(hwnd, &rv);
ctl = CreateWindowEx(
WS_EX_CLIENTEDGE, "EDIT", item->text, editstyle,
rv.left, rv.top, rv.right, rv.bottom,
hwnd, (HMENU)(ULONG_PTR)index++, hinst, NULL);
SendMessage(ctl, WM_SETFONT, font, MAKELPARAM(true, 0));
break;
}
default:
break;
}
}
/*
* Now resize the overall window, and move the Close button at
* the bottom.
*/
RECT r;
r.left = 176;
r.top = y + 10;
r.right = r.bottom = 0;
MapDialogRect(hwnd, &r);
HWND ctl = GetDlgItem(hwnd, IDOK);
SetWindowPos(ctl, NULL, r.left, r.top, 0, 0,
SWP_NOSIZE | SWP_NOREDRAW | SWP_NOZORDER);
r.left = r.top = r.right = 0;
r.bottom = 300;
MapDialogRect(hwnd, &r);
int oldheight = r.bottom;
r.left = r.top = r.right = 0;
r.bottom = y + 30;
MapDialogRect(hwnd, &r);
int newheight = r.bottom;
GetWindowRect(hwnd, &r);
SetWindowPos(hwnd, NULL, 0, 0, r.right - r.left,
r.bottom - r.top + newheight - oldheight,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
ShowWindow(hwnd, SW_SHOWNORMAL);
return 1;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
ShinyEndDialog(hwnd, 0);
return 0;
}
return 0;
case WM_CLOSE:
ShinyEndDialog(hwnd, 0);
return 0;
}
return 0;
}
static const char *process_seatdialogtext(
strbuf *dlg_text, const char **scary_heading, SeatDialogText *text)
{
const char *dlg_title = "";
for (SeatDialogTextItem *item = text->items,
*end = item + text->nitems; item < end; item++) {
switch (item->type) {
case SDT_PARA:
put_fmt(dlg_text, "%s\r\n\r\n", item->text);
break;
case SDT_DISPLAY:
put_fmt(dlg_text, "%s\r\n\r\n", item->text);
break;
case SDT_SCARY_HEADING:
assert(scary_heading != NULL && "only expect a scary heading if "
"the dialog has somewhere to put it");
*scary_heading = item->text;
break;
case SDT_TITLE:
dlg_title = item->text;
break;
default:
break;
}
}
/* Trim any trailing newlines */
while (strbuf_chomp(dlg_text, '\r') || strbuf_chomp(dlg_text, '\n'));
return dlg_title;
}
static INT_PTR HostKeyDialogProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam, void *vctx)
{
struct hostkey_dialog_ctx *ctx = (struct hostkey_dialog_ctx *)vctx;
switch (msg) {
case WM_INITDIALOG: {
strbuf *dlg_text = strbuf_new();
const char *scary_heading = NULL;
const char *dlg_title = process_seatdialogtext(
dlg_text, &scary_heading, ctx->text);
LPCTSTR iconid = IDI_QUESTION;
if (scary_heading) {
SetDlgItemText(hwnd, IDC_HK_TITLE, scary_heading);
iconid = IDI_WARNING;
}
SetDlgItemText(hwnd, IDC_HK_TEXT, dlg_text->s);
MakeDlgItemBorderless(hwnd, IDC_HK_TEXT);
strbuf_free(dlg_text);
SetWindowText(hwnd, dlg_title);
if (!ctx->has_title) {
HWND item = GetDlgItem(hwnd, IDC_HK_TITLE);
if (item)
DestroyWindow(item);
}
/*
* Find out how tall the text in the edit control really ended
* up (after line wrapping), and adjust the height of the
* whole box to match it.
*/
int height = SendDlgItemMessage(hwnd, IDC_HK_TEXT,
EM_GETLINECOUNT, 0, 0);
height *= 8; /* height of a text line, by definition of dialog units */
int edittop = ctx->has_title ? 40 : 20;
RECT r;
r.left = 40;
r.top = edittop;
r.right = 290;
r.bottom = height;
MapDialogRect(hwnd, &r);
SetWindowPos(GetDlgItem(hwnd, IDC_HK_TEXT), NULL,
r.left, r.top, r.right, r.bottom,
SWP_NOREDRAW | SWP_NOZORDER);
static const struct {
int id, x;
} buttons[] = {
{ IDCANCEL, 288 },
{ IDC_HK_ACCEPT, 168 },
{ IDC_HK_ONCE, 216 },
{ IDC_HK_MOREINFO, 60 },
{ IDHELP, 12 },
};
for (size_t i = 0; i < lenof(buttons); i++) {
HWND ctl = GetDlgItem(hwnd, buttons[i].id);
r.left = buttons[i].x;
r.top = edittop + height + 20;
r.right = r.bottom = 0;
MapDialogRect(hwnd, &r);
SetWindowPos(ctl, NULL, r.left, r.top, 0, 0,
SWP_NOSIZE | SWP_NOREDRAW | SWP_NOZORDER);
}
r.left = r.top = r.right = 0;
r.bottom = 240;
MapDialogRect(hwnd, &r);
int oldheight = r.bottom;
r.left = r.top = r.right = 0;
r.bottom = edittop + height + 40;
MapDialogRect(hwnd, &r);
int newheight = r.bottom;
GetWindowRect(hwnd, &r);
SetWindowPos(hwnd, NULL, 0, 0, r.right - r.left,
r.bottom - r.top + newheight - oldheight,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
HANDLE icon = LoadImage(
NULL, iconid, IMAGE_ICON,
GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
LR_SHARED);
SendDlgItemMessage(hwnd, IDC_HK_ICON, STM_SETICON, (WPARAM)icon, 0);
if (!has_help()) {
HWND item = GetDlgItem(hwnd, IDHELP);
if (item)
DestroyWindow(item);
}
ShowWindow(hwnd, SW_SHOWNORMAL);
return 1;
}
case WM_CTLCOLORSTATIC: {
HDC hdc = (HDC)wParam;
HWND control = (HWND)lParam;
if (GetWindowLongPtr(control, GWLP_ID) == IDC_HK_TITLE &&
ctx->has_title) {
SetBkMode(hdc, TRANSPARENT);
HFONT prev_font = (HFONT)SelectObject(
hdc, (HFONT)GetStockObject(SYSTEM_FONT));
LOGFONT lf;
if (GetObject(prev_font, sizeof(lf), &lf)) {
lf.lfWeight = FW_BOLD;
lf.lfHeight = lf.lfHeight * 3 / 2;
HFONT bold_font = CreateFontIndirect(&lf);
if (bold_font)
SelectObject(hdc, bold_font);
}
return (INT_PTR)GetSysColorBrush(COLOR_BTNFACE);
}
return 0;
}
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_HK_ACCEPT:
case IDC_HK_ONCE:
case IDCANCEL:
ShinyEndDialog(hwnd, LOWORD(wParam));
return 0;
case IDHELP: {
launch_help(hwnd, ctx->helpctx);
return 0;
}
case IDC_HK_MOREINFO: {
ShinyDialogBox(hinst, MAKEINTRESOURCE(IDD_HK_MOREINFO),
"PuTTYHostKeyMoreInfo", hwnd,
HostKeyMoreInfoProc, ctx);
}
}
return 0;
case WM_CLOSE:
ShinyEndDialog(hwnd, IDCANCEL);
return 0;
}
return 0;
}
const SeatDialogPromptDescriptions *win_seat_prompt_descriptions(Seat *seat)
{
static const SeatDialogPromptDescriptions descs = {
.hk_accept_action = "press \"Accept\"",
.hk_connect_once_action = "press \"Connect Once\"",
.hk_cancel_action = "press \"Cancel\"",
.hk_cancel_action_Participle = "Pressing \"Cancel\"",
.weak_accept_action = "press \"Yes\"",
.weak_cancel_action = "press \"No\"",
};
return &descs;
}
SeatPromptResult win_seat_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *cbctx)
{
WinGuiSeat *wgs = container_of(seat, WinGuiSeat, seat);
struct hostkey_dialog_ctx ctx[1];
ctx->text = text;
ctx->helpctx = helpctx;
int mbret = ShinyDialogBox(
hinst, MAKEINTRESOURCE(IDD_HOSTKEY), "PuTTYHostKeyDialog",
wgs->term_hwnd, HostKeyDialogProc, ctx);
assert(mbret==IDC_HK_ACCEPT || mbret==IDC_HK_ONCE || mbret==IDCANCEL);
if (mbret == IDC_HK_ACCEPT) {
store_host_key(host, port, keytype, keystr);
return SPR_OK;
} else if (mbret == IDC_HK_ONCE) {
return SPR_OK;
}
return SPR_USER_ABORT;
}
/*
* Ask whether the selected algorithm is acceptable (since it was
* below the configured 'warn' threshold).
*/
SeatPromptResult win_seat_confirm_weak_crypto_primitive(
Seat *seat, SeatDialogText *text,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
strbuf *dlg_text = strbuf_new();
const char *dlg_title = process_seatdialogtext(dlg_text, NULL, text);
int mbret = MessageBox(NULL, dlg_text->s, dlg_title,
MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2);
socket_reselect_all();
strbuf_free(dlg_text);
if (mbret == IDYES)
return SPR_OK;
else
return SPR_USER_ABORT;
}
SeatPromptResult win_seat_confirm_weak_cached_hostkey(
Seat *seat, SeatDialogText *text,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
strbuf *dlg_text = strbuf_new();
const char *dlg_title = process_seatdialogtext(dlg_text, NULL, text);
int mbret = MessageBox(NULL, dlg_text->s, dlg_title,
MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2);
socket_reselect_all();
strbuf_free(dlg_text);
if (mbret == IDYES)
return SPR_OK;
else
return SPR_USER_ABORT;
}
/*
* Ask whether to wipe a session log file before writing to it.
* Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
*/
static int win_gui_askappend(LogPolicy *lp, Filename *filename,
void (*callback)(void *ctx, int result),
void *ctx)
{
static const char msgtemplate[] =
"The session log file \"%.*s\" already exists.\n"
"You can overwrite it with a new session log,\n"
"append your session log to the end of it,\n"
"or disable session logging for this session.\n"
"Hit Yes to wipe the file, No to append to it,\n"
"or Cancel to disable logging.";
char *message;
char *mbtitle;
int mbret;
message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
mbtitle = dupprintf("%s Log to File", appname);
mbret = MessageBox(NULL, message, mbtitle,
MB_ICONQUESTION | MB_YESNOCANCEL | MB_DEFBUTTON3);
socket_reselect_all();
sfree(message);
sfree(mbtitle);
if (mbret == IDYES)
return 2;
else if (mbret == IDNO)
return 1;
else
return 0;
}
const LogPolicyVtable win_gui_logpolicy_vt = {
.eventlog = win_gui_eventlog,
.askappend = win_gui_askappend,
.logging_error = win_gui_logging_error,
.verbose = null_lp_verbose_yes,
};
/*
* Warn about the obsolescent key file format.
*
* Uniquely among these functions, this one does _not_ expect a
* frontend handle. This means that if PuTTY is ported to a
* platform which requires frontend handles, this function will be
* an anomaly. Fortunately, the problem it addresses will not have
* been present on that platform, so it can plausibly be
* implemented as an empty function.
*/
void old_keyfile_warning(void)
{
static const char mbtitle[] = "%s Key File Warning";
static const char message[] =
"You are loading an SSH-2 private key which has an\n"
"old version of the file format. This means your key\n"
"file is not fully tamperproof. Future versions of\n"
"%s may stop supporting this private key format,\n"
"so we recommend you convert your key to the new\n"
"format.\n"
"\n"
"You can perform this conversion by loading the key\n"
"into PuTTYgen and then saving it again.";
char *msg, *title;
msg = dupprintf(message, appname);
title = dupprintf(mbtitle, appname);
MessageBox(NULL, msg, title, MB_OK);
socket_reselect_all();
sfree(msg);
sfree(title);
}
static INT_PTR CAConfigProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
void *ctx)
{
PortableDialogStuff *pds = (PortableDialogStuff *)ctx;
switch (msg) {
case WM_INITDIALOG:
pds_initdialog_start(pds, hwnd);
SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
(LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(IDI_CFGICON)));
centre_window(hwnd);
pds_create_controls(pds, 0, IDCX_PANELBASE, 3, 3, 3, "Main");
pds_create_controls(pds, 0, IDCX_STDBASE, 3, 3, 243, "");
dlg_refresh(NULL, pds->dp); /* and set up control values */
pds_initdialog_finish(pds);
return 0;
default:
return pds_default_dlgproc(pds, hwnd, msg, wParam, lParam);
}
}
void show_ca_config_box(dlgparam *dp)
{
PortableDialogStuff *pds = pds_new(1);
setup_ca_config_box(pds->ctrlbox);
ShinyDialogBox(hinst, MAKEINTRESOURCE(IDD_CA_CONFIG), "PuTTYConfigBox",
dp ? dp->hwnd : NULL, CAConfigProc, pds);
pds_free(pds);
}
|