Skip to content

Commit

Permalink
[w32g] Improve ShowFileDialog()
Browse files Browse the repository at this point in the history
  • Loading branch information
starg2 committed Dec 11, 2023
1 parent f73072f commit 4623172
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
40 changes: 21 additions & 19 deletions interface/w32g_utl.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
#include "rtsyn.h"

#include <wchar.h>
#define CINTERFACE
#define COBJMACROS
#include <shobjidl.h>

///r
Expand Down Expand Up @@ -1550,7 +1552,7 @@ void BitBltRect(HDC dst, HDC src, const RECT *rc)
}

BOOL ShowFileDialog(
int mode,
FILEDIALOG_MODE mode,
HWND hParentWindow,
LPCWSTR pTitle,
char *pResult,
Expand All @@ -1572,16 +1574,16 @@ BOOL ShowFileDialog(
return FALSE;

if (guid)
pFileDialog->lpVtbl->SetClientGuid(pFileDialog, guid);
IFileDialog_SetClientGuid(pFileDialog, guid);

if (pTitle)
pFileDialog->lpVtbl->SetTitle(pFileDialog, pTitle);
IFileDialog_SetTitle(pFileDialog, pTitle);

if (filterCount > 0 && pFilters)
pFileDialog->lpVtbl->SetFileTypes(pFileDialog, filterCount, pFilters);
IFileDialog_SetFileTypes(pFileDialog, filterCount, pFilters);

FILEOPENDIALOGOPTIONS options;
pFileDialog->lpVtbl->GetOptions(pFileDialog, &options);
IFileDialog_GetOptions(pFileDialog, &options);
options |= FOS_NOCHANGEDIR | FOS_FORCEFILESYSTEM;

switch (mode) {
Expand All @@ -1597,29 +1599,29 @@ BOOL ShowFileDialog(
break;
}

pFileDialog->lpVtbl->SetOptions(pFileDialog, options);
IFileDialog_SetOptions(pFileDialog, options);

hr = pFileDialog->lpVtbl->Show(pFileDialog, hParentWindow);
hr = IFileDialog_Show(pFileDialog, hParentWindow);

if (hr == S_OK) {
if (mode == FILEDIALOG_OPEN_MULTIPLE_FILES) {
IFileOpenDialog *pFileOpenDialog;

if (SUCCEEDED(pFileDialog->lpVtbl->QueryInterface(pFileDialog, &IID_IFileOpenDialog, (void**)&pFileOpenDialog))) {
if (SUCCEEDED(IFileDialog_QueryInterface(pFileDialog, &IID_IFileOpenDialog, (void**)&pFileOpenDialog))) {
IShellItemArray *pShellItemArray;

if (SUCCEEDED(pFileOpenDialog->lpVtbl->GetResults(pFileOpenDialog, &pShellItemArray))) {
if (SUCCEEDED(IFileOpenDialog_GetResults(pFileOpenDialog, &pShellItemArray))) {
size_t pos = 0;
DWORD count = 0;
pShellItemArray->lpVtbl->GetCount(pShellItemArray, &count);
IShellItemArray_GetCount(pShellItemArray, &count);

for (DWORD i = 0; i < count; i++) {
IShellItem *pShellItem;

if (SUCCEEDED(pShellItemArray->lpVtbl->GetItemAt(pShellItemArray, i, &pShellItem))) {
if (SUCCEEDED(IShellItemArray_GetItemAt(pShellItemArray, i, &pShellItem))) {
wchar_t *pBuffer;

if (SUCCEEDED(pShellItem->lpVtbl->GetDisplayName(pShellItem, i == 0 ? SIGDN_FILESYSPATH : SIGDN_PARENTRELATIVEPARSING, &pBuffer))) {
if (SUCCEEDED(IShellItem_GetDisplayName(pShellItem, i == 0 ? SIGDN_FILESYSPATH : SIGDN_PARENTRELATIVEPARSING, &pBuffer))) {
int len = wcslen(pBuffer);

if (i == 0) {
Expand All @@ -1644,24 +1646,24 @@ BOOL ShowFileDialog(
CoTaskMemFree(pBuffer);
}

pShellItem->lpVtbl->Release(pShellItem);
IShellItem_Release(pShellItem);
}
}

pResult[pos] = '\0';
pResult[pos + 1] = '\0';
pShellItemArray->lpVtbl->Release(pShellItemArray);
IShellItemArray_Release(pShellItemArray);
}

pFileOpenDialog->lpVtbl->Release(pFileOpenDialog);
IFileOpenDialog_Release(pFileOpenDialog);
}
} else {
IShellItem* pShellItem;

if (SUCCEEDED(pFileDialog->lpVtbl->GetResult(pFileDialog, &pShellItem))) {
if (SUCCEEDED(IFileDialog_GetResult(pFileDialog, &pShellItem))) {
wchar_t* pBuffer;

if (SUCCEEDED(pShellItem->lpVtbl->GetDisplayName(pShellItem, SIGDN_FILESYSPATH, &pBuffer))) {
if (SUCCEEDED(IShellItem_GetDisplayName(pShellItem, SIGDN_FILESYSPATH, &pBuffer))) {
int c = WideCharToMultiByte(
CP_ACP,
0,
Expand All @@ -1678,12 +1680,12 @@ BOOL ShowFileDialog(
CoTaskMemFree(pBuffer);
}

pShellItem->lpVtbl->Release(pShellItem);
IShellItem_Release(pShellItem);
}
}
}

pFileDialog->lpVtbl->Release(pFileDialog);
IFileDialog_Release(pFileDialog);
return hr == S_OK;
}

Expand Down
6 changes: 3 additions & 3 deletions interface/w32g_utl.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,16 @@ extern int IniVersionCheck(void);
extern void BitBltRect(HDC dst, HDC src, const RECT *rc);

// mode option for ShowFileDialog()
enum {
typedef enum {
FILEDIALOG_OPEN_FILE,
FILEDIALOG_OPEN_MULTIPLE_FILES,
FILEDIALOG_OPEN_FOLDER,
FILEDIALOG_SAVE_FILE
};
} FILEDIALOG_MODE;

// pDirectory and pResult must point to an array of at least FILEPATH_MAX length!
extern BOOL ShowFileDialog(
int mode,
FILEDIALOG_MODE mode,
HWND hParentWindow,
LPCWSTR pTitle,
char *pResult,
Expand Down

0 comments on commit 4623172

Please sign in to comment.