summaryrefslogtreecommitdiffstats
path: root/private/tapi/dev/cpl/insdisk.c
blob: bd04cda14674a94ed364711e62e02934fc94e2d7 (plain) (blame)
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
/* BUGBUG define Ansi/Oem ness of the file parameters */

#include  <windows.h>
#include  <windowsx.h>
#include  <commdlg.h>
#include  <dlgs.h>
#include  "resource.h"
#include  "tapicpl.h"
#include  "util.h"
#include  "string.h"
#include  "insdisk.h"
#include  "help.h"

//#include "priv.h"


typedef struct {
    LPCSTR lpszDiskName;
    LPSTR lpszPath;
    LPCSTR lpszFile;
    LPCSTR lpszOtherFiles;
    HICON hIcon;
    UINT wFlags;
} INSERTDISK, FAR *LPINSERTDISK;

UINT wHelpMsg;
UINT wBrowseDoneMsg;
char szInstSection[] = "Install Locations";
char szDriveA[] = "A:\\";
char szShellHelp[] = "ShellHelp";
char szFILEOKSTRING[] = FILEOKSTRING;

BOOL NEAR PASCAL DoBrowse(HWND hDlg, LPCSTR lpszFile, LPCSTR lpszOtherFiles);
BOOL NEAR PASCAL VerifyFileExists(HWND hwnd, LPCSTR lpPath, LPCSTR lpFile);
BOOL EXPORT  InsertDiskDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);


LPSTR NEAR PASCAL PtrToNullToNull(LPCSTR lpsz)
{
    if (lpsz && (*lpsz == 0))
        return NULL;
    else
        return (LPSTR)lpsz;
}


// in:
//      lpszDiskName
//          NULL, or the name of the disk being looked for
//          "Windows Install Disk #3"
//      lpszFile
//          NULL, or specific file being looked for or "key file"
//      lpszPath
//          if initalized default path to present to the user
//      hIcon
//          NULL, or disk type icon to display
//      wFlags
//          ID_DISKISTEXT
//
// out:
//      lpszPath
//          qualified path to location where key file is
//          found
//
// returns:
//      -1      error, failed to create dialog
//      IDOK
//      IDCANCEL

int WINAPI InsertDisk(HWND hwnd, LPCSTR lpszDiskName,
    LPCSTR lpszFile, LPCSTR lpszOtherFiles, LPSTR lpszPath, HICON hIcon, UINT wFlags)
{
    INSERTDISK id;
    extern CPL  gCPL;       // app global

    id.lpszDiskName = PtrToNullToNull(lpszDiskName);
    id.lpszFile = PtrToNullToNull(lpszFile);
    id.lpszPath = lpszPath;
    id.lpszOtherFiles = lpszOtherFiles;
    id.hIcon = hIcon;
    id.wFlags = wFlags;

    return DialogBoxParam(gCPL.hCplInst, MAKEINTRESOURCE(IDD_INSERT_DISK), hwnd, (DLGPROC)InsertDiskDlg, (LONG)(LPINSERTDISK)&id);

}

void NEAR PASCAL SetInsertDiskText(HWND hDlg, LPINSERTDISK lpid)
{
    char szBuf[300], szTemp[200];
    char szTemp2[2];
    LPCSTR lpszFile;
    UINT wFlags;
    extern CPL  gCPL;       // app global

    wFlags = lpid->wFlags;
    lpszFile = lpid->lpszFile;

    if (lpid->lpszDiskName && lpszFile) {
        LoadString(gCPL.hCplInst, IDS_DISKFILEMSG, szTemp, sizeof(szTemp));

        //
        // In case the localizer wants the order of the strings reversed,
        // we examine this string.  '0' is normal, anything else
        // will be swapped.
        //

        LoadString(gCPL.hCplInst, IDS_DISKFILEMSGSWAP, szTemp2, sizeof(szTemp2));

        if ( szTemp2[0] == '0' )
           wsprintf(szBuf, szTemp, lpid->lpszDiskName, lpszFile);
        else
           wsprintf(szBuf, szTemp, lpszFile, lpid->lpszDiskName);

    } else if (lpid->lpszDiskName) {
        LoadString(gCPL.hCplInst, IDS_DISKMSG, szTemp, sizeof(szTemp));
        wsprintf(szBuf, szTemp, lpid->lpszDiskName);
    } else if (lpszFile) {
        LoadString(gCPL.hCplInst, IDS_FILEMSG, szTemp, sizeof(szTemp));
        wsprintf(szBuf, szTemp, lpid->lpszFile);
    }

    SetDlgItemText(hDlg, IDD_TEXT, szBuf);

    // and set the app icon if needed
    if (lpid->hIcon)
        SendDlgItemMessage(hDlg, IDD_ICON, STM_SETICON, (WPARAM)lpid->hIcon, 0L);
}


BOOL EXPORT  InsertDiskDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    LPINSERTDISK lpid;
    HANDLE hMRU;
    char szTemp[CPL_MAX_PATH];
    extern CPL  gCPL;       // app global

    lpid = (LPINSERTDISK)GetWindowLong(hDlg, DWL_USER);

    switch (message) {
    case WM_INITDIALOG:
        SetWindowLong(hDlg, DWL_USER, lParam);
        lpid = (LPINSERTDISK)lParam;

        SetInsertDiskText(hDlg, lpid);

        SendDlgItemMessage(hDlg, IDD_PATH, EM_LIMITTEXT, CPL_MAX_PATH - 20, 0L);
        if (*lpid->lpszPath)
           lstrcpyn(szTemp, lpid->lpszPath, sizeof(szTemp));
        else
           lstrcpy(szTemp, szDriveA);

        LpszPathRemoveBackslash(szTemp);
        SetDlgItemText(hDlg, IDD_PATH, szTemp);

        SendDlgItemMessage(hDlg, IDD_PATH, EM_SETSEL, 0, 0x7FFF0000);

        wBrowseDoneMsg = RegisterWindowMessage(szFILEOKSTRING);

        return TRUE;

    case WM_COMMAND:
        switch (wParam) {
        case IDOK:
            GetDlgItemText(hDlg, IDD_PATH, szTemp, sizeof(szTemp));
            PathRemoveBlanks(szTemp);
            LpszPathAddBackslash(szTemp);

            lstrcpy(lpid->lpszPath, szTemp);

            // we only validate if there didn't specify random other
            // files (wild cards)

            if (!lpid->lpszOtherFiles) {
                lstrcat(szTemp, lpid->lpszFile);
                if (!VerifyFileExists(hDlg, szTemp, lpid->lpszFile))
                    break;
            }

            // fall through...

        case IDCANCEL:
            EndDialog(hDlg, wParam);
            return(TRUE);

        case IDD_BROWSE:
            if (DoBrowse(hDlg, lpid->lpszFile, lpid->lpszOtherFiles))
                SendMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, IDOK), MAKELONG(TRUE, 0));
            else
                SendMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, IDD_PATH), MAKELONG(TRUE, 0));
            break;

        case IDHELP:
            goto DoHelp;
        }
        break;

    default:
        if (message == gCPL.uHelpMsg) {
DoHelp:
//            Help( hDlg, CPL_HLP_INSERT_DISK );
            // CPHelp(hDlg);
            return TRUE;
        } else
            return FALSE;

    }
    return (FALSE);                           /* Didn't process a message    */
}

void NEAR PASCAL CopyWindowText(HWND hwnd, HWND hwnd2)
{
    char szBuf[255];

    GetWindowText(hwnd, szBuf, sizeof(szBuf));
    SetWindowText(hwnd2, szBuf);
}


BOOL NEAR PASCAL VerifyFileExists(HWND hwnd, LPCSTR lpPath, LPCSTR lpFile)
{
    int fh;
    OFSTRUCT of;

    fh = OpenFile(lpPath, &of, OF_EXIST);

    // BUGBUG, we probably need to try again with share modes
    // use OpenSharedFile() from shell.dll

    if (fh == -1) {
        IMessageBox(hwnd, IDS_FILENOTINDIR, 0, MB_OK | MB_ICONINFORMATION, (LPSTR)lpFile);
        return FALSE;   // file does not exists
    }
    return TRUE;        // it is there
}

// Hooks into common dialog to show only directories
BOOL EXPORT  BrowseHookProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
    HWND hwndT;
    extern CPL  gCPL;       // app global

    switch (wMsg) {
    case WM_INITDIALOG:
          #define lpOFN ((LPOPENFILENAME)lParam)
          // CopyWindowText(GetDlgItem(lpOFN->hwndOwner, IDD_TEXT), GetDlgItem(hDlg, ctlLast + 1));
//          CopyWindowText(lpOFN->hwndOwner, hDlg);

          // hide the file we are looking for in an extra control
          SetDlgItemText(hDlg, ctlLast + 3, lpOFN->lpstrFile);
          goto PostMyMessage;

    case WM_COMMAND:
        switch (wParam) {
            case lst2:
            case cmb2:
            case IDOK:
PostMyMessage:
                PostMessage(hDlg, WM_COMMAND, ctlLast+2, 0L);
                break;

            case pshHelp:
                goto DoHelp;

            case ctlLast + 2:
                hwndT = GetDlgItem(hDlg, lst1);
                if (SendMessage(hwndT, LB_GETCOUNT, 0, 0L)) {
                    SendMessage(hwndT, LB_SETCURSEL, 0, 0L);
                    SendMessage(hDlg, WM_COMMAND, lst1, MAKELONG(hwndT, LBN_SELCHANGE));
                    break;
                }
                // poke the file we are looking for back into the edit field
                // this is needed so that commdlg will send us the
                // wBrowseDoneMsg

                CopyWindowText(GetDlgItem(hDlg, ctlLast + 3), GetDlgItem(hDlg, edt1));

                break;
        }
        break;

    default:
        if (wMsg == gCPL.uHelpMsg) {
DoHelp:
//			Help( hDlg, CPL_HLP_BROWSE );
            // CPHelp(hDlg);
            return TRUE;
        } else if (wMsg == wBrowseDoneMsg) {

            if (!VerifyFileExists(hDlg, lpOFN->lpstrFile, lpOFN->lpstrFile + lpOFN->nFileOffset))
                return TRUE;

        }
    }

    return FALSE;  // commdlg, do your thing
}


// hDlg is the prompt dialog
// lpszFile is the file we are looking for

BOOL NEAR PASCAL DoBrowse(HWND hDlg, LPCSTR lpszFile, LPCSTR lpszOtherFiles)
{
    OPENFILENAME ofn;
    char szPath[CPL_MAX_PATH], szInitDir[CPL_MAX_PATH];
    char szFilter[30];
    int temp;
    LPSTR lpTemp, lpFilters;
    extern CPL  gCPL;       // app global

    // build a filter that contains wild cards so the
    // commdlg will fill the file list & edit control for us.
    // this is based on the file passed in and any other
    // file specs the caller wants to use.

    szFilter[0] = 'a';      // dummy file type string (user doesn't see this)
    szFilter[1] = '\0';
    lpFilters = szFilter + 2;

    lstrcpyn(lpFilters, lpszFile, sizeof(szFilter) - 2);
    // make sure the extension is ".*"
    lpTemp = _fstrchr(lpFilters, '.');
    if (lpTemp) {
        lstrcpy(lpTemp, ".*");
    }
    if (lpszOtherFiles) {
        lpTemp = lpFilters + lstrlen(lpFilters);
        *lpTemp++ = ';';
        lstrcpy(lpTemp, lpszOtherFiles);
    }

    lstrcpyn(szPath, lpszFile, sizeof(szPath));

    GetDlgItemText(hDlg, IDD_PATH, szInitDir, sizeof(szInitDir));
    PathRemoveBlanks(szInitDir);

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hDlg;
    ofn.hInstance = gCPL.hCplInst;
    ofn.lpstrFilter = szFilter;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter = 0;
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = szPath;
    ofn.nMaxFile = sizeof(szPath);
    ofn.lpstrInitialDir = szInitDir;
    ofn.lpstrTitle = NULL;
    ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLEHOOK |
                OFN_ENABLETEMPLATE |
                OFN_SHOWHELP | OFN_NOCHANGEDIR;
    ofn.lCustData = MAKELONG(hDlg, 0);
    ofn.lpfnHook = BrowseHookProc;
    ofn.lpTemplateName  = (LPSTR)MAKEINTRESOURCE(IDD_BROWSE);
    ofn.nFileOffset = 0;
    ofn.nFileExtension = 0;
    ofn.lpstrDefExt = NULL;
    ofn.lpstrFileTitle = NULL;

    temp = GetOpenFileName(&ofn);

    UpdateWindow(hDlg); // force buttons to repaint

    if (temp) {
        // remove file part, put the result in the path field
        szPath[ofn.nFileOffset - 1] = '\0';
        SetDlgItemText(hDlg, IDD_PATH, szPath);
        return TRUE;
    }
    return FALSE;
}