2 * windlg.c - dialogs for PuTTY(tel), including the configuration dialog.
23 #define TVINSERTSTRUCT TV_INSERTSTRUCT
24 #define TVITEM TV_ITEM
29 * These are the various bits of data required to handle the
30 * portable-dialog stuff in the config box. Having them at file
31 * scope in here isn't too bad a place to put them; if we were ever
32 * to need more than one config box per process we could always
33 * shift them to a per-config-box structure stored in GWL_USERDATA.
35 static struct controlbox *ctrlbox;
37 * ctrls_base holds the OK and Cancel buttons: the controls which
38 * are present in all dialog panels. ctrls_panel holds the ones
39 * which change from panel to panel.
41 static struct winctrls ctrls_base, ctrls_panel;
42 static struct dlgparam dp;
44 static char **events = NULL;
45 static int nevents = 0, negsize = 0;
47 extern Conf *conf; /* defined in window.c */
49 #define PRINTER_DISABLED_STRING "None (printing disabled)"
51 void force_normal(HWND hwnd)
53 static int recurse = 0;
61 wp.length = sizeof(wp);
62 if (GetWindowPlacement(hwnd, &wp) && wp.showCmd == SW_SHOWMAXIMIZED) {
63 wp.showCmd = SW_SHOWNORMAL;
64 SetWindowPlacement(hwnd, &wp);
69 static int CALLBACK LogProc(HWND hwnd, UINT msg,
70 WPARAM wParam, LPARAM lParam)
77 char *str = dupprintf("%s Event Log", appname);
78 SetWindowText(hwnd, str);
82 static int tabs[4] = { 78, 108 };
83 SendDlgItemMessage(hwnd, IDN_LIST, LB_SETTABSTOPS, 2,
86 for (i = 0; i < nevents; i++)
87 SendDlgItemMessage(hwnd, IDN_LIST, LB_ADDSTRING,
88 0, (LPARAM) events[i]);
91 switch (LOWORD(wParam)) {
95 SetActiveWindow(GetParent(hwnd));
99 if (HIWORD(wParam) == BN_CLICKED ||
100 HIWORD(wParam) == BN_DOUBLECLICKED) {
103 selcount = SendDlgItemMessage(hwnd, IDN_LIST,
104 LB_GETSELCOUNT, 0, 0);
105 if (selcount == 0) { /* don't even try to copy zero items */
110 selitems = snewn(selcount, int);
112 int count = SendDlgItemMessage(hwnd, IDN_LIST,
119 static unsigned char sel_nl[] = SEL_NL;
121 if (count == 0) { /* can't copy zero stuff */
127 for (i = 0; i < count; i++)
129 strlen(events[selitems[i]]) + sizeof(sel_nl);
131 clipdata = snewn(size, char);
134 for (i = 0; i < count; i++) {
135 char *q = events[selitems[i]];
136 int qlen = strlen(q);
139 memcpy(p, sel_nl, sizeof(sel_nl));
142 write_aclip(NULL, clipdata, size, TRUE);
147 for (i = 0; i < nevents; i++)
148 SendDlgItemMessage(hwnd, IDN_LIST, LB_SETSEL,
157 SetActiveWindow(GetParent(hwnd));
164 static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
165 WPARAM wParam, LPARAM lParam)
170 char *str = dupprintf("%s Licence", appname);
171 SetWindowText(hwnd, str);
176 switch (LOWORD(wParam)) {
190 static int CALLBACK AboutProc(HWND hwnd, UINT msg,
191 WPARAM wParam, LPARAM lParam)
197 str = dupprintf("About %s", appname);
198 SetWindowText(hwnd, str);
200 SetDlgItemText(hwnd, IDA_TEXT1, appname);
201 SetDlgItemText(hwnd, IDA_VERSION, ver);
204 switch (LOWORD(wParam)) {
207 EndDialog(hwnd, TRUE);
210 EnableWindow(hwnd, 0);
211 DialogBox(hinst, MAKEINTRESOURCE(IDD_LICENCEBOX),
213 EnableWindow(hwnd, 1);
214 SetActiveWindow(hwnd);
218 /* Load web browser */
219 ShellExecute(hwnd, "open",
220 "http://www.chiark.greenend.org.uk/~sgtatham/putty/",
221 0, 0, SW_SHOWDEFAULT);
226 EndDialog(hwnd, TRUE);
232 static int SaneDialogBox(HINSTANCE hinst,
235 DLGPROC lpDialogFunc)
244 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
245 wc.lpfnWndProc = DefDlgProc;
247 wc.cbWndExtra = DLGWINDOWEXTRA + 2*sizeof(LONG_PTR);
248 wc.hInstance = hinst;
250 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
251 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
252 wc.lpszMenuName = NULL;
253 wc.lpszClassName = "PuTTYConfigBox";
256 hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc);
258 SetWindowLongPtr(hwnd, BOXFLAGS, 0); /* flags */
259 SetWindowLongPtr(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */
261 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
262 flags=GetWindowLongPtr(hwnd, BOXFLAGS);
263 if (!(flags & DF_END) && !IsDialogMessage(hwnd, &msg))
264 DispatchMessage(&msg);
270 PostQuitMessage(msg.wParam); /* We got a WM_QUIT, pass it on */
272 ret=GetWindowLongPtr(hwnd, BOXRESULT);
277 static void SaneEndDialog(HWND hwnd, int ret)
279 SetWindowLongPtr(hwnd, BOXRESULT, ret);
280 SetWindowLongPtr(hwnd, BOXFLAGS, DF_END);
284 * Null dialog procedure.
286 static int CALLBACK NullDlgProc(HWND hwnd, UINT msg,
287 WPARAM wParam, LPARAM lParam)
293 IDCX_ABOUT = IDC_ABOUT,
297 IDCX_PANELBASE = IDCX_STDBASE + 32
300 struct treeview_faff {
305 static HTREEITEM treeview_insert(struct treeview_faff *faff,
306 int level, char *text, char *path)
311 ins.hParent = (level > 0 ? faff->lastat[level - 1] : TVI_ROOT);
312 ins.hInsertAfter = faff->lastat[level];
313 #if _WIN32_IE >= 0x0400 && defined NONAMELESSUNION
314 #define INSITEM DUMMYUNIONNAME.item
318 ins.INSITEM.mask = TVIF_TEXT | TVIF_PARAM;
319 ins.INSITEM.pszText = text;
320 ins.INSITEM.cchTextMax = strlen(text)+1;
321 ins.INSITEM.lParam = (LPARAM)path;
322 newitem = TreeView_InsertItem(faff->treeview, &ins);
324 TreeView_Expand(faff->treeview, faff->lastat[level - 1],
325 (level > 1 ? TVE_COLLAPSE : TVE_EXPAND));
326 faff->lastat[level] = newitem;
327 for (i = level + 1; i < 4; i++)
328 faff->lastat[i] = NULL;
333 * Create the panelfuls of controls in the configuration box.
335 static void create_controls(HWND hwnd, char *path)
344 * Here we must create the basic standard controls.
346 ctlposinit(&cp, hwnd, 3, 3, 235);
348 base_id = IDCX_STDBASE;
351 * Otherwise, we're creating the controls for a particular
354 ctlposinit(&cp, hwnd, 100, 3, 13);
356 base_id = IDCX_PANELBASE;
359 for (index=-1; (index = ctrl_find_path(ctrlbox, path, index)) >= 0 ;) {
360 struct controlset *s = ctrlbox->ctrlsets[index];
361 winctrl_layout(&dp, wc, &cp, s, &base_id);
366 * This function is the configuration box.
367 * (Being a dialog procedure, in general it returns 0 if the default
368 * dialog processing should be performed, and 1 if it should not.)
370 static int CALLBACK GenericMainDlgProc(HWND hwnd, UINT msg,
371 WPARAM wParam, LPARAM lParam)
374 struct treeview_faff tvfaff;
380 create_controls(hwnd, ""); /* Open and Cancel buttons etc */
381 SetWindowText(hwnd, dp.wintitle);
382 SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
384 SetWindowLongPtr(hwnd, GWL_EXSTYLE,
385 GetWindowLongPtr(hwnd, GWL_EXSTYLE) |
388 HWND item = GetDlgItem(hwnd, IDC_HELPBTN);
392 SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,
393 (LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(IDI_CFGICON)));
397 { /* centre the window */
400 hw = GetDesktopWindow();
401 if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
403 (rs.right + rs.left + rd.left - rd.right) / 2,
404 (rs.bottom + rs.top + rd.top - rd.bottom) / 2,
405 rd.right - rd.left, rd.bottom - rd.top, TRUE);
409 * Create the tree view.
417 r.right = r.left + 95;
419 r.bottom = r.top + 10;
420 MapDialogRect(hwnd, &r);
421 tvstatic = CreateWindowEx(0, "STATIC", "Cate&gory:",
422 WS_CHILD | WS_VISIBLE,
424 r.right - r.left, r.bottom - r.top,
425 hwnd, (HMENU) IDCX_TVSTATIC, hinst,
427 font = SendMessage(hwnd, WM_GETFONT, 0, 0);
428 SendMessage(tvstatic, WM_SETFONT, font, MAKELPARAM(TRUE, 0));
431 r.right = r.left + 95;
433 r.bottom = r.top + 219;
434 MapDialogRect(hwnd, &r);
435 treeview = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
436 WS_CHILD | WS_VISIBLE |
437 WS_TABSTOP | TVS_HASLINES |
438 TVS_DISABLEDRAGDROP | TVS_HASBUTTONS
440 TVS_SHOWSELALWAYS, r.left, r.top,
441 r.right - r.left, r.bottom - r.top,
442 hwnd, (HMENU) IDCX_TREEVIEW, hinst,
444 font = SendMessage(hwnd, WM_GETFONT, 0, 0);
445 SendMessage(treeview, WM_SETFONT, font, MAKELPARAM(TRUE, 0));
446 tvfaff.treeview = treeview;
447 memset(tvfaff.lastat, 0, sizeof(tvfaff.lastat));
451 * Set up the tree view contents.
454 HTREEITEM hfirst = NULL;
457 char *firstpath = NULL;
459 for (i = 0; i < ctrlbox->nctrlsets; i++) {
460 struct controlset *s = ctrlbox->ctrlsets[i];
467 j = path ? ctrl_path_compare(s->pathname, path) : 0;
469 continue; /* same path, nothing to add to tree */
472 * We expect never to find an implicit path
473 * component. For example, we expect never to see
474 * A/B/C followed by A/D/E, because that would
475 * _implicitly_ create A/D. All our path prefixes
476 * are expected to contain actual controls and be
477 * selectable in the treeview; so we would expect
478 * to see A/D _explicitly_ before encountering
481 assert(j == ctrl_path_elements(s->pathname) - 1);
483 c = strrchr(s->pathname, '/');
489 item = treeview_insert(&tvfaff, j, c, s->pathname);
492 firstpath = s->pathname;
499 * Put the treeview selection on to the first panel in the
502 TreeView_SelectItem(treeview, hfirst);
505 * And create the actual control set for that panel, to
506 * match the initial treeview selection.
508 create_controls(hwnd, firstpath);
509 dlg_refresh(NULL, &dp); /* and set up control values */
513 * Set focus into the first available control.
519 for (i = 0; (c = winctrl_findbyindex(&ctrls_panel, i)) != NULL;
522 dlg_set_focus(c->ctrl, &dp);
529 * Now we've finished creating our initial set of controls,
530 * it's safe to actually show the window without risking setup
533 ShowWindow(hwnd, SW_SHOWNORMAL);
536 * Set the flag that activates a couple of the other message
537 * handlers below, which were disabled until now to avoid
538 * spurious firing during the above setup procedure.
540 SetWindowLongPtr(hwnd, GWLP_USERDATA, 1);
544 * Button release should trigger WM_OK if there was a
545 * previous double click on the session list.
549 SaneEndDialog(hwnd, dp.endresult ? 1 : 0);
552 if (LOWORD(wParam) == IDCX_TREEVIEW &&
553 ((LPNMHDR) lParam)->code == TVN_SELCHANGED) {
555 * Selection-change events on the treeview cause us to do
556 * a flurry of control deletion and creation - but only
557 * after WM_INITDIALOG has finished. The initial
558 * selection-change event(s) during treeview setup are
565 if (GetWindowLongPtr(hwnd, GWLP_USERDATA) != 1)
568 i = TreeView_GetSelection(((LPNMHDR) lParam)->hwndFrom);
570 SendMessage (hwnd, WM_SETREDRAW, FALSE, 0);
573 item.pszText = buffer;
574 item.cchTextMax = sizeof(buffer);
575 item.mask = TVIF_TEXT | TVIF_PARAM;
576 TreeView_GetItem(((LPNMHDR) lParam)->hwndFrom, &item);
578 /* Destroy all controls in the currently visible panel. */
583 while ((c = winctrl_findbyindex(&ctrls_panel, 0)) != NULL) {
584 for (k = 0; k < c->num_ids; k++) {
585 item = GetDlgItem(hwnd, c->base_id + k);
589 winctrl_rem_shortcuts(&dp, c);
590 winctrl_remove(&ctrls_panel, c);
595 create_controls(hwnd, (char *)item.lParam);
597 dlg_refresh(NULL, &dp); /* set up control values */
599 SendMessage (hwnd, WM_SETREDRAW, TRUE, 0);
600 InvalidateRect (hwnd, NULL, TRUE);
602 SetFocus(((LPNMHDR) lParam)->hwndFrom); /* ensure focus stays */
608 default: /* also handle drag list msg here */
610 * Only process WM_COMMAND once the dialog is fully formed.
612 if (GetWindowLongPtr(hwnd, GWLP_USERDATA) == 1) {
613 ret = winctrl_handle_command(&dp, msg, wParam, lParam);
614 if (dp.ended && GetCapture() != hwnd)
615 SaneEndDialog(hwnd, dp.endresult ? 1 : 0);
620 if (!winctrl_context_help(&dp, hwnd,
621 ((LPHELPINFO)lParam)->iCtrlId))
626 SaneEndDialog(hwnd, 0);
629 /* Grrr Explorer will maximize Dialogs! */
631 if (wParam == SIZE_MAXIMIZED)
639 void modal_about_box(HWND hwnd)
641 EnableWindow(hwnd, 0);
642 DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, AboutProc);
643 EnableWindow(hwnd, 1);
644 SetActiveWindow(hwnd);
647 void show_help(HWND hwnd)
649 launch_help(hwnd, NULL);
652 void defuse_showwindow(void)
655 * Work around the fact that the app's first call to ShowWindow
656 * will ignore the default in favour of the shell-provided
661 hwnd = CreateDialog(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX),
663 ShowWindow(hwnd, SW_HIDE);
664 SetActiveWindow(hwnd);
673 ctrlbox = ctrl_new_box();
674 setup_config_box(ctrlbox, FALSE, 0, 0);
675 win_setup_config_box(ctrlbox, &dp.hwnd, has_help(), FALSE, 0);
677 winctrl_init(&ctrls_base);
678 winctrl_init(&ctrls_panel);
679 dp_add_tree(&dp, &ctrls_base);
680 dp_add_tree(&dp, &ctrls_panel);
681 dp.wintitle = dupprintf("%s Configuration", appname);
682 dp.errtitle = dupprintf("%s Error", appname);
684 dlg_auto_set_fixed_pitch_flag(&dp);
685 dp.shortcuts['g'] = TRUE; /* the treeview: `Cate&gory' */
688 SaneDialogBox(hinst, MAKEINTRESOURCE(IDD_MAINBOX), NULL,
691 ctrl_free_box(ctrlbox);
692 winctrl_cleanup(&ctrls_panel);
693 winctrl_cleanup(&ctrls_base);
699 int do_reconfig(HWND hwnd, int protcfginfo)
704 backup_conf = conf_copy(conf);
706 ctrlbox = ctrl_new_box();
707 protocol = conf_get_int(conf, CONF_protocol);
708 setup_config_box(ctrlbox, TRUE, protocol, protcfginfo);
709 win_setup_config_box(ctrlbox, &dp.hwnd, has_help(), TRUE, protocol);
711 winctrl_init(&ctrls_base);
712 winctrl_init(&ctrls_panel);
713 dp_add_tree(&dp, &ctrls_base);
714 dp_add_tree(&dp, &ctrls_panel);
715 dp.wintitle = dupprintf("%s Reconfiguration", appname);
716 dp.errtitle = dupprintf("%s Error", appname);
718 dlg_auto_set_fixed_pitch_flag(&dp);
719 dp.shortcuts['g'] = TRUE; /* the treeview: `Cate&gory' */
721 ret = SaneDialogBox(hinst, MAKEINTRESOURCE(IDD_MAINBOX), NULL,
724 ctrl_free_box(ctrlbox);
725 winctrl_cleanup(&ctrls_base);
726 winctrl_cleanup(&ctrls_panel);
730 conf_copy_into(conf, backup_conf);
732 conf_free(backup_conf);
737 void logevent(void *frontend, const char *string)
742 log_eventlog(logctx, string);
744 if (nevents >= negsize) {
746 events = sresize(events, negsize, char *);
750 strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm);
752 events[nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
753 strcpy(events[nevents], timebuf);
754 strcat(events[nevents], string);
757 SendDlgItemMessage(logbox, IDN_LIST, LB_ADDSTRING,
758 0, (LPARAM) events[nevents]);
759 count = SendDlgItemMessage(logbox, IDN_LIST, LB_GETCOUNT, 0, 0);
760 SendDlgItemMessage(logbox, IDN_LIST, LB_SETTOPINDEX, count - 1, 0);
765 void showeventlog(HWND hwnd)
768 logbox = CreateDialog(hinst, MAKEINTRESOURCE(IDD_LOGBOX),
770 ShowWindow(logbox, SW_SHOWNORMAL);
772 SetActiveWindow(logbox);
775 void showabout(HWND hwnd)
777 DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, AboutProc);
780 int verify_ssh_host_key(void *frontend, char *host, int port,
781 const char *keytype, char *keystr, char *fingerprint,
782 void (*callback)(void *ctx, int result), void *ctx)
786 static const char absentmsg[] =
787 "The server's host key is not cached in the registry. You\n"
788 "have no guarantee that the server is the computer you\n"
790 "The server's %s key fingerprint is:\n"
792 "If you trust this host, hit Yes to add the key to\n"
793 "%s's cache and carry on connecting.\n"
794 "If you want to carry on connecting just once, without\n"
795 "adding the key to the cache, hit No.\n"
796 "If you do not trust this host, hit Cancel to abandon the\n"
799 static const char wrongmsg[] =
800 "WARNING - POTENTIAL SECURITY BREACH!\n"
802 "The server's host key does not match the one %s has\n"
803 "cached in the registry. This means that either the\n"
804 "server administrator has changed the host key, or you\n"
805 "have actually connected to another computer pretending\n"
806 "to be the server.\n"
807 "The new %s key fingerprint is:\n"
809 "If you were expecting this change and trust the new key,\n"
810 "hit Yes to update %s's cache and continue connecting.\n"
811 "If you want to carry on connecting but without updating\n"
812 "the cache, hit No.\n"
813 "If you want to abandon the connection completely, hit\n"
814 "Cancel. Hitting Cancel is the ONLY guaranteed safe\n" "choice.\n";
816 static const char mbtitle[] = "%s Security Alert";
819 * Verify the key against the registry.
821 ret = verify_host_key(host, port, keytype, keystr);
823 if (ret == 0) /* success - key matched OK */
825 else if (ret == 2) { /* key was different */
827 char *text = dupprintf(wrongmsg, appname, keytype, fingerprint,
829 char *caption = dupprintf(mbtitle, appname);
830 mbret = message_box(text, caption,
831 MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3,
832 HELPCTXID(errors_hostkey_changed));
833 assert(mbret==IDYES || mbret==IDNO || mbret==IDCANCEL);
836 if (mbret == IDYES) {
837 store_host_key(host, port, keytype, keystr);
839 } else if (mbret == IDNO)
841 } else if (ret == 1) { /* key was absent */
843 char *text = dupprintf(absentmsg, keytype, fingerprint, appname);
844 char *caption = dupprintf(mbtitle, appname);
845 mbret = message_box(text, caption,
846 MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3,
847 HELPCTXID(errors_hostkey_absent));
848 assert(mbret==IDYES || mbret==IDNO || mbret==IDCANCEL);
851 if (mbret == IDYES) {
852 store_host_key(host, port, keytype, keystr);
854 } else if (mbret == IDNO)
857 return 0; /* abandon the connection */
861 * Ask whether the selected algorithm is acceptable (since it was
862 * below the configured 'warn' threshold).
864 int askalg(void *frontend, const char *algtype, const char *algname,
865 void (*callback)(void *ctx, int result), void *ctx)
867 static const char mbtitle[] = "%s Security Alert";
868 static const char msg[] =
869 "The first %s supported by the server\n"
870 "is %.64s, which is below the configured\n"
871 "warning threshold.\n"
872 "Do you want to continue with this connection?\n";
873 char *message, *title;
876 message = dupprintf(msg, algtype, algname);
877 title = dupprintf(mbtitle, appname);
878 mbret = MessageBox(NULL, message, title,
879 MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2);
880 socket_reselect_all();
890 * Ask whether to wipe a session log file before writing to it.
891 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
893 int askappend(void *frontend, Filename *filename,
894 void (*callback)(void *ctx, int result), void *ctx)
896 static const char msgtemplate[] =
897 "The session log file \"%.*s\" already exists.\n"
898 "You can overwrite it with a new session log,\n"
899 "append your session log to the end of it,\n"
900 "or disable session logging for this session.\n"
901 "Hit Yes to wipe the file, No to append to it,\n"
902 "or Cancel to disable logging.";
907 message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
908 mbtitle = dupprintf("%s Log to File", appname);
910 mbret = MessageBox(NULL, message, mbtitle,
911 MB_ICONQUESTION | MB_YESNOCANCEL | MB_DEFBUTTON3);
913 socket_reselect_all();
920 else if (mbret == IDNO)
927 * Warn about the obsolescent key file format.
929 * Uniquely among these functions, this one does _not_ expect a
930 * frontend handle. This means that if PuTTY is ported to a
931 * platform which requires frontend handles, this function will be
932 * an anomaly. Fortunately, the problem it addresses will not have
933 * been present on that platform, so it can plausibly be
934 * implemented as an empty function.
936 void old_keyfile_warning(void)
938 static const char mbtitle[] = "%s Key File Warning";
939 static const char message[] =
940 "You are loading an SSH-2 private key which has an\n"
941 "old version of the file format. This means your key\n"
942 "file is not fully tamperproof. Future versions of\n"
943 "%s may stop supporting this private key format,\n"
944 "so we recommend you convert your key to the new\n"
947 "You can perform this conversion by loading the key\n"
948 "into PuTTYgen and then saving it again.";
951 msg = dupprintf(message, appname);
952 title = dupprintf(mbtitle, appname);
954 MessageBox(NULL, msg, title, MB_OK);
956 socket_reselect_all();