10.9.8 Adding a SaveAsDlg Dialog Box Procedure

You must supply a dialog box procedure for the SaveAs dialog box. This procedure retrieves a filename from the edit control and copies the name to the global variable FileName. The dialog box procedure should look like this:

int FAR PASCAL SaveAsDlg(hDlg, message, wParam, lParam)
HWND hDlg;
UINT message;
WPARAM wParam;
LPARAM lParam;
{
    char TempName[128];

    switch (message) {
        case WM_INITDIALOG:
            if (!FileName[0])
                fSaveEnabled = FALSE;

            else {
                fSaveEnabled = TRUE;
                DlgDirList(hDlg, DefPath, NULL, IDC_PATH, 0x4010);
                SetDlgItemText(hDlg, IDC_EDIT, FileName);
                SendDlgItemMessage(hDlg, IDC_EDIT, EM_SETSEL, 0,
                    MAKELONG(0, 0x7fff));
            }

            EnableWindow(GetDlgItem(hDlg, IDOK), fSaveEnabled);
            SetFocus(GetDlgItem(hDlg, IDC_EDIT));
            return FALSE;        /* FALSE since focus changed */




        case WM_COMMAND:
            switch (wParam) {
                case IDC_EDIT:
                    if (HIWORD(lParam) == EN_CHANGE && !fSaveEnabled)
                        EnableWindow(GetDlgItem(hDlg, IDOK),
                            fSaveEnabled = TRUE);
                    return TRUE;

                case IDOK:
                    GetDlgItemText(hDlg, IDC_EDIT, TempName, 128);
                    if (CheckFileName(hDlg, FileName, TempName)) {
                        SeparateFile(hDlg, (LPSTR) str,
                            (LPSTR) DefSpec, (LPSTR) FileName);
                        if (str[0]) lstrcpy(DefPath, str);
                        EndDialog(hDlg, IDOK);
                    }
                    return TRUE;

                case IDCANCEL:
                    EndDialog(hDlg, IDCANCEL);
                    return TRUE;
            }
            break;
    }
    return FALSE;
}

The WM_INITDIALOG case enables or disables the Save button. The button should be disabled if there is no current filename. The EnableWindow function, along with the fSaveEnabled variable, enables or disables the button. If there is a current filename, it should be the proposed name. The SetDlgItemText function copies the filename to the edit control, and the SendDlgItemMessage function selects the entire name for editing. The DlgDirList function sets the IDC_PATH control to the current directory. Since there is no list box to fill, no list box identifier is given.

The WM_COMMAND case processes notification messages from the controls in the dialog box. When the function receives the EN_CHANGE notification from the edit control IDC_EDIT, it uses the EnableWindow function to enable the Save button, if it is not already enabled.

When the function receives a notification from the Save button, it uses the GetDlgItemText function to retrieve the filename in the edit control, then checks the validity of the filename by using the locally defined function CheckFileName. This function ensures that the filename contains no path separators or wildcards. It then determines whether the file already exists; if the file does exist, CheckFileName uses the MessageBox function to ask the user whether the file should be overwritten. Finally, the dialog box procedure uses the SeparateFile function to copy the filename to the DefSpec and DefPath variables.