summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/wndutil.cpp
blob: 5d832e5038f50fd806b1d1d91bb0bcc33351c2ea (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"


// Exit macros
#define WnduExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_WNDUTIL, x, e, s, __VA_ARGS__)
#define WnduExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_WNDUTIL, p, x, e, s, __VA_ARGS__)
#define WnduExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_WNDUTIL, p, x, s, __VA_ARGS__)
#define WnduExitOnNullDebugTrace(p, x, e, s, ...)  ExitOnNullDebugTraceSource(DUTIL_SOURCE_WNDUTIL, p, x, e, s, __VA_ARGS__)
#define WnduExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_WNDUTIL, p, x, s, __VA_ARGS__)
#define WnduExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_WNDUTIL, e, x, s, __VA_ARGS__)
#define WnduExitOnOptionalXmlQueryFailure(x, b, s, ...) ExitOnOptionalXmlQueryFailureSource(DUTIL_SOURCE_WNDUTIL, x, b, s, __VA_ARGS__)
#define WnduExitOnRequiredXmlQueryFailure(x, s, ...) ExitOnRequiredXmlQueryFailureSource(DUTIL_SOURCE_WNDUTIL, x, s, __VA_ARGS__)
#define WnduExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_WNDUTIL, g, x, s, __VA_ARGS__)

struct MEMBUFFER_FOR_RICHEDIT
{
    BYTE* rgbData;
    DWORD cbData;

    DWORD iData;
};

const DWORD GROW_WINDOW_TEXT = 250;


// prototypes
static DWORD CALLBACK RichEditStreamFromFileHandleCallback(
    __in DWORD_PTR dwCookie,
    __in_bcount(cb) LPBYTE pbBuff,
    __in LONG cb,
    __in LONG *pcb
    );
static DWORD CALLBACK RichEditStreamFromMemoryCallback(
    __in DWORD_PTR dwCookie,
    __in_bcount(cb) LPBYTE pbBuff,
    __in LONG cb,
    __in LONG *pcb
    );


DAPI_(HRESULT) WnduLoadRichEditFromFile(
    __in HWND hWnd,
    __in_z LPCWSTR wzFileName,
    __in HMODULE hModule
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczFile = NULL;
    HANDLE hFile = INVALID_HANDLE_VALUE;

    hr = PathRelativeToModule(&sczFile, wzFileName, hModule);
    WnduExitOnFailure(hr, "Failed to read resource data.");

    hFile = ::CreateFileW(sczFile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (INVALID_HANDLE_VALUE == hFile)
    {
        WnduExitWithLastError(hr, "Failed to open RTF file.");
    }
    else
    {
        LONGLONG llRtfSize;
        hr = FileSizeByHandle(hFile, &llRtfSize);
        if (SUCCEEDED(hr))
        {
            ::SendMessageW(hWnd, EM_EXLIMITTEXT, 0, static_cast<LPARAM>(llRtfSize));
        }

        EDITSTREAM es = { };
        es.pfnCallback = RichEditStreamFromFileHandleCallback;
        es.dwCookie = reinterpret_cast<DWORD_PTR>(hFile);

        ::SendMessageW(hWnd, EM_STREAMIN, SF_RTF, reinterpret_cast<LPARAM>(&es));
        hr = es.dwError;
        WnduExitOnFailure(hr, "Failed to update RTF stream.");
    }

LExit:
    ReleaseStr(sczFile);
    ReleaseFile(hFile);

    return hr;
}

DAPI_(HRESULT) WnduLoadRichEditFromResource(
    __in HWND hWnd,
    __in_z LPCSTR szResourceName,
    __in HMODULE hModule
    )
{
    HRESULT hr = S_OK;
    MEMBUFFER_FOR_RICHEDIT buffer = { };
    EDITSTREAM es = { };

    hr = ResReadData(hModule, szResourceName, reinterpret_cast<LPVOID*>(&buffer.rgbData), &buffer.cbData);
    WnduExitOnFailure(hr, "Failed to read resource data.");

    es.pfnCallback = RichEditStreamFromMemoryCallback;
    es.dwCookie = reinterpret_cast<DWORD_PTR>(&buffer);

    ::SendMessageW(hWnd, EM_STREAMIN, SF_RTF, reinterpret_cast<LPARAM>(&es));
    hr = es.dwError;
    WnduExitOnFailure(hr, "Failed to update RTF stream.");

LExit:
    return hr;
}


DAPI_(HRESULT) WnduGetControlText(
    __in HWND hWnd,
    __inout_z LPWSTR* psczText
    )
{
    HRESULT hr = S_OK;
    SIZE_T cbSize = 0;
    DWORD cchText = 0;
    DWORD cchTextRead = 0;

    // Ensure the string has room for at least one character.
    hr = StrMaxLength(*psczText, &cbSize);
    WnduExitOnFailure(hr, "Failed to get text buffer length.");

    cchText = (DWORD)min(DWORD_MAX, cbSize);

    if (!cchText)
    {
        cchText = GROW_WINDOW_TEXT;

        hr = StrAlloc(psczText, cchText);
        WnduExitOnFailure(hr, "Failed to grow text buffer.");
    }

    // Read (and keep growing buffer) until we finally read less than there
    // is room in the buffer.
    for (;;)
    {
        cchTextRead = ::GetWindowTextW(hWnd, *psczText, cchText);
        if (cchTextRead + 1 < cchText)
        {
            break;
        }
        else
        {
            cchText = cchTextRead + GROW_WINDOW_TEXT;

            hr = StrAlloc(psczText, cchText);
            WnduExitOnFailure(hr, "Failed to grow text buffer again.");
        }
    }

LExit:
    return hr;
}


DAPI_(HRESULT) WnduShowOpenFileDialog(
    __in_opt HWND hwndParent,
    __in BOOL fForcePathExists,
    __in BOOL fForceFileExists,
    __in_opt LPCWSTR wzTitle,
    __in COMDLG_FILTERSPEC* rgFilters,
    __in DWORD cFilters,
    __in DWORD dwDefaultFilter,
    __in_opt LPCWSTR wzDefaultPath,
    __inout LPWSTR* psczPath
    )
{
    HRESULT hr = S_OK;
    IFileOpenDialog* pFileOpenDialog = NULL;
    FILEOPENDIALOGOPTIONS fos = 0;
    FILEOPENDIALOGOPTIONS additionalFlags = FOS_NOCHANGEDIR;
    IShellItem* psi = NULL;
    LPWSTR sczDefaultDirectory = NULL;
    LPWSTR sczPath = NULL;

    hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpenDialog));
    WnduExitOnFailure(hr, "Failed to create FileOpenDialog object.");

    hr = pFileOpenDialog->SetTitle(wzTitle);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog title.");

    hr = pFileOpenDialog->GetOptions(&fos);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog options.");

    if (fForcePathExists)
    {
        additionalFlags |= FOS_PATHMUSTEXIST;
    }

    if (fForceFileExists)
    {
        additionalFlags |= FOS_FILEMUSTEXIST;
    }

    hr = pFileOpenDialog->SetOptions(fos | additionalFlags);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog options.");

    hr = pFileOpenDialog->SetFileTypes(cFilters, rgFilters);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog file types.");

    hr = pFileOpenDialog->SetFileTypeIndex(dwDefaultFilter);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog default file type.");

    if (wzDefaultPath && *wzDefaultPath)
    {
        hr = PathGetDirectory(wzDefaultPath, &sczDefaultDirectory);
        if (SUCCEEDED(hr))
        {
            hr = ::SHCreateItemFromParsingName(sczDefaultDirectory, NULL, IID_PPV_ARGS(&psi));
            if (SUCCEEDED(hr))
            {
                hr = pFileOpenDialog->SetFolder(psi);
                WnduExitOnFailure(hr, "Failed to set FileOpenDialog folder.");
            }

            ReleaseNullObject(psi);
        }

        hr = pFileOpenDialog->SetFileName(wzDefaultPath);
        WnduExitOnFailure(hr, "Failed to set FileOpenDialog file name.");
    }

    hr = pFileOpenDialog->Show(hwndParent);
    if (HRESULT_FROM_WIN32(ERROR_CANCELLED) == hr)
    {
        ExitFunction();
    }
    WnduExitOnFailure(hr, "Failed to show FileOpenDialog for file.");

    hr = pFileOpenDialog->GetResult(&psi);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog result.");

    hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &sczPath);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog result path.");

    hr = StrAllocString(psczPath, sczPath, 0);
    WnduExitOnFailure(hr, "Failed to copy FileOpenDialog result path.");

LExit:
    if (sczPath)
    {
        ::CoTaskMemFree(sczPath);
    }

    ReleaseStr(sczDefaultDirectory);
    ReleaseObject(psi);
    ReleaseObject(pFileOpenDialog);

    return hr;
}


DAPI_(HRESULT) WnduShowOpenFolderDialog(
    __in_opt HWND hwndParent,
    __in BOOL fForceFileSystem,
    __in_opt LPCWSTR wzTitle,
    __inout LPWSTR* psczPath
    )
{
    HRESULT hr = S_OK;
    IFileOpenDialog* pFileOpenDialog = NULL;
    FILEOPENDIALOGOPTIONS fos = 0;
    FILEOPENDIALOGOPTIONS additionalFlags = FOS_PICKFOLDERS | FOS_NOCHANGEDIR;
    IShellItem* psi = NULL;
    LPWSTR sczPath = NULL;

    hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpenDialog));
    WnduExitOnFailure(hr, "Failed to create FileOpenDialog object.");

    hr = pFileOpenDialog->SetTitle(wzTitle);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog title.");

    hr = pFileOpenDialog->GetOptions(&fos);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog options.");

    if (fForceFileSystem)
    {
        additionalFlags |= FOS_FORCEFILESYSTEM;
    }

    hr = pFileOpenDialog->SetOptions(fos | additionalFlags);
    WnduExitOnFailure(hr, "Failed to set FileOpenDialog options.");

    hr = pFileOpenDialog->Show(hwndParent);
    if (HRESULT_FROM_WIN32(ERROR_CANCELLED) == hr)
    {
        ExitFunction();
    }
    WnduExitOnFailure(hr, "Failed to show FileOpenDialog for folder.");

    hr = pFileOpenDialog->GetResult(&psi);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog result.");

    hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &sczPath);
    WnduExitOnFailure(hr, "Failed to get FileOpenDialog result path.");

    hr = StrAllocString(psczPath, sczPath, 0);
    WnduExitOnFailure(hr, "Failed to copy FileOpenDialog result path.");

LExit:
    if (sczPath)
    {
        ::CoTaskMemFree(sczPath);
    }

    ReleaseObject(psi);
    ReleaseObject(pFileOpenDialog);

    return hr;
}


static DWORD CALLBACK RichEditStreamFromFileHandleCallback(
    __in DWORD_PTR dwCookie,
    __in_bcount(cb) LPBYTE pbBuff,
    __in LONG cb,
    __in LONG* pcb
    )
{
    HRESULT hr = S_OK;
    HANDLE hFile = reinterpret_cast<HANDLE>(dwCookie);

    if (!::ReadFile(hFile, pbBuff, cb, reinterpret_cast<DWORD*>(pcb), NULL))
    {
        WnduExitWithLastError(hr, "Failed to read file");
    }

LExit:
    return hr;
}


static DWORD CALLBACK RichEditStreamFromMemoryCallback(
    __in DWORD_PTR dwCookie,
    __in_bcount(cb) LPBYTE pbBuff,
    __in LONG cb,
    __in LONG* pcb
    )
{
    HRESULT hr = S_OK;
    MEMBUFFER_FOR_RICHEDIT* pBuffer = reinterpret_cast<MEMBUFFER_FOR_RICHEDIT*>(dwCookie);
    DWORD cbCopy = 0;

    if (pBuffer->iData < pBuffer->cbData)
    {
        cbCopy = min(static_cast<DWORD>(cb), pBuffer->cbData - pBuffer->iData);
        memcpy(pbBuff, pBuffer->rgbData + pBuffer->iData, cbCopy);

        pBuffer->iData += cbCopy;
        Assert(pBuffer->iData <= pBuffer->cbData);
    }

    *pcb = cbCopy;
    return hr;
}