EndDialog(hwnd, dp.endresult ? 1 : 0);
SaneEndDialog(hwnd, dp.endresult ? 1 : 0);
static int CALLBACK GenericMainDlgProc(HWND hwnd, UINT msg,				       WPARAM wParam, LPARAM lParam){    HWND hw, treeview;    struct treeview_faff tvfaff;    int ret;    switch (msg) {      case WM_INITDIALOG:	dp.hwnd = hwnd;	create_controls(hwnd, "");     /* Open and Cancel buttons etc */	SetWindowText(hwnd, dp.wintitle);	SetWindowLong(hwnd, GWL_USERDATA, 0);        if (help_path)            SetWindowLong(hwnd, GWL_EXSTYLE,                          GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_CONTEXTHELP);        else {            HWND item = GetDlgItem(hwnd, IDC_HELPBTN);            if (item)                DestroyWindow(item);        }        requested_help = FALSE;	SendMessage(hwnd, WM_SETICON, (WPARAM) ICON_BIG,		    (LPARAM) LoadIcon(hinst, MAKEINTRESOURCE(IDI_CFGICON)));	/*	 * Centre the window.	 */	{			       /* centre the window */	    RECT rs, rd;	    hw = GetDesktopWindow();	    if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))		MoveWindow(hwnd,			   (rs.right + rs.left + rd.left - rd.right) / 2,			   (rs.bottom + rs.top + rd.top - rd.bottom) / 2,			   rd.right - rd.left, rd.bottom - rd.top, TRUE);	}	/*	 * Create the tree view.	 */	{	    RECT r;	    WPARAM font;	    HWND tvstatic;	    r.left = 3;	    r.right = r.left + 75;	    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 + 75;	    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;	    for (i = 0; i < ctrlbox->nctrlsets; i++) {		struct controlset *s = 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;		path = s->pathname;	    }	    /*	     * Put the treeview selection on to the Session panel.	     * This should also cause creation of the relevant	     * controls.	     */	    TreeView_SelectItem(treeview, hfirst);	}	/*	 * Set focus into the first available control.	 */	{	    int i;	    struct winctrl *c;	    for (i = 0; (c = winctrl_findbyindex(&ctrls_panel, i)) != NULL;		 i++) {		if (c->ctrl) {		    dlg_set_focus(c->ctrl, &dp);		    break;		}	    }	}	SetWindowLong(hwnd, GWL_USERDATA, 1);	return 0;      case WM_LBUTTONUP:	/*	 * Button release should trigger WM_OK if there was a	 * previous double click on the session list.	 */	ReleaseCapture();	if (dp.ended)	    EndDialog(hwnd, dp.endresult ? 1 : 0);	break;      case WM_NOTIFY:	if (LOWORD(wParam) == IDCX_TREEVIEW &&	    ((LPNMHDR) lParam)->code == TVN_SELCHANGED) {	    HTREEITEM i =		TreeView_GetSelection(((LPNMHDR) lParam)->hwndFrom);	    TVITEM item;	    char buffer[64];  	    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(&ctrls_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(&dp, c);		    winctrl_remove(&ctrls_panel, c);		    sfree(c->data);		    sfree(c);		}	    }	    create_controls(hwnd, (char *)item.lParam);	    dlg_refresh(NULL, &dp);    /* set up control values */ 	    SendMessage (hwnd, WM_SETREDRAW, TRUE, 0); 	    InvalidateRect (hwnd, NULL, TRUE);	    SetFocus(((LPNMHDR) lParam)->hwndFrom);	/* ensure focus stays */	    return 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.	 */	if (GetWindowLong(hwnd, GWL_USERDATA) == 1) {	    ret = winctrl_handle_command(&dp, msg, wParam, lParam);	    if (dp.ended && GetCapture() != hwnd)		EndDialog(hwnd, dp.endresult ? 1 : 0);	} else	    ret = 0;	return ret;      case WM_HELP:        if (help_path) {	    if (winctrl_context_help(&dp, hwnd,				     ((LPHELPINFO)lParam)->iCtrlId))                requested_help = TRUE;	    else                MessageBeep(0);        }        break;      case WM_CLOSE:        if (requested_help) {            WinHelp(hwnd, help_path, HELP_QUIT, 0);            requested_help = FALSE;        }	EndDialog(hwnd, 0);	return 0;	/* Grrr Explorer will maximize Dialogs! */      case WM_SIZE:	if (wParam == SIZE_MAXIMIZED)	    force_normal(hwnd);	return 0;    }    return 0;}