Commit c23a91cb authored by Nikolay Sivov's avatar Nikolay Sivov Committed by Alexandre Julliard

comdlg32: Consolidate file dialog initialization to avoid duplication.

parent aef84332
...@@ -318,236 +318,162 @@ static BOOL GetFileName95(FileOpenDlgInfos *fodInfos) ...@@ -318,236 +318,162 @@ static BOOL GetFileName95(FileOpenDlgInfos *fodInfos)
return lRes; return lRes;
} }
/*********************************************************************** static WCHAR *heap_strdupAtoW(const char *str)
* GetFileDialog95A
*
* Call GetFileName95 with this structure and clean the memory.
*
* IN : The OPENFILENAMEA initialisation structure passed to
* GetOpenFileNameA win api function (see filedlg.c)
*/
static BOOL GetFileDialog95A(LPOPENFILENAMEA ofn,UINT iDlgType)
{ {
BOOL ret; WCHAR *ret;
FileOpenDlgInfos fodInfos; INT len;
LPSTR lpstrSavDir = NULL;
LPWSTR title = NULL;
LPWSTR defext = NULL;
LPWSTR filter = NULL;
LPWSTR customfilter = NULL;
INITCOMMONCONTROLSEX icc;
/* Initialize ComboBoxEx32 */
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_USEREX_CLASSES;
InitCommonControlsEx(&icc);
/* Initialize CommDlgExtendedError() */
COMDLG32_SetCommDlgExtendedError(0);
/* Initialize FileOpenDlgInfos structure */
ZeroMemory(&fodInfos, sizeof(FileOpenDlgInfos));
/* Pass in the original ofn */
fodInfos.ofnInfos = (LPOPENFILENAMEW)ofn;
/* save current directory */
if (ofn->Flags & OFN_NOCHANGEDIR)
{
lpstrSavDir = MemAlloc(MAX_PATH);
GetCurrentDirectoryA(MAX_PATH, lpstrSavDir);
}
fodInfos.unicode = FALSE; if (!str)
return NULL;
/* convert all the input strings to unicode */ len = MultiByteToWideChar(CP_ACP, 0, str, -1, 0, 0);
if(ofn->lpstrInitialDir) ret = MemAlloc(len * sizeof(WCHAR));
{ MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrInitialDir, -1, NULL, 0 );
fodInfos.initdir = MemAlloc((len+1)*sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrInitialDir, -1, fodInfos.initdir, len);
}
else
fodInfos.initdir = NULL;
if(ofn->lpstrFile) return ret;
{ }
fodInfos.filename = MemAlloc(ofn->nMaxFile*sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFile, -1, fodInfos.filename, ofn->nMaxFile);
}
else
fodInfos.filename = NULL;
if(ofn->lpstrDefExt) static void init_filedlg_infoW(OPENFILENAMEW *ofn, FileOpenDlgInfos *info)
{ {
DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrDefExt, -1, NULL, 0 ); INITCOMMONCONTROLSEX icc;
defext = MemAlloc((len+1)*sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrDefExt, -1, defext, len);
}
fodInfos.defext = defext;
if(ofn->lpstrTitle) /* Initialize ComboBoxEx32 */
{ icc.dwSize = sizeof(icc);
DWORD len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrTitle, -1, NULL, 0 ); icc.dwICC = ICC_USEREX_CLASSES;
title = MemAlloc((len+1)*sizeof(WCHAR)); InitCommonControlsEx(&icc);
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrTitle, -1, title, len);
}
fodInfos.title = title;
if (ofn->lpstrFilter) /* Initialize CommDlgExtendedError() */
{ COMDLG32_SetCommDlgExtendedError(0);
LPCSTR s;
int n, len;
/* filter is a list... title\0ext\0......\0\0 */
s = ofn->lpstrFilter;
while (*s) s = s+strlen(s)+1;
s++;
n = s - ofn->lpstrFilter;
len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFilter, n, NULL, 0 );
filter = MemAlloc(len*sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrFilter, n, filter, len );
}
fodInfos.filter = filter;
/* convert lpstrCustomFilter */ memset(info, 0, sizeof(*info));
if (ofn->lpstrCustomFilter)
{
LPCSTR s;
int n, len;
/* customfilter contains a pair of strings... title\0ext\0 */
s = ofn->lpstrCustomFilter;
if (*s) s = s+strlen(s)+1;
if (*s) s = s+strlen(s)+1;
n = s - ofn->lpstrCustomFilter;
len = MultiByteToWideChar( CP_ACP, 0, ofn->lpstrCustomFilter, n, NULL, 0 );
customfilter = MemAlloc(len*sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, ofn->lpstrCustomFilter, n, customfilter, len );
}
fodInfos.customfilter = customfilter;
/* Initialize the dialog property */ /* Pass in the original ofn */
fodInfos.DlgInfos.dwDlgProp = 0; info->ofnInfos = ofn;
fodInfos.DlgInfos.hwndCustomDlg = NULL;
switch(iDlgType) info->title = ofn->lpstrTitle;
{ info->defext = ofn->lpstrDefExt;
case OPEN_DIALOG : info->filter = ofn->lpstrFilter;
ret = GetFileName95(&fodInfos); info->customfilter = ofn->lpstrCustomFilter;
break;
case SAVE_DIALOG :
fodInfos.DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
ret = GetFileName95(&fodInfos);
break;
default :
ret = FALSE;
}
if (lpstrSavDir) if (ofn->lpstrFile)
{ {
SetCurrentDirectoryA(lpstrSavDir); info->filename = MemAlloc(ofn->nMaxFile * sizeof(WCHAR));
MemFree(lpstrSavDir); lstrcpynW(info->filename, ofn->lpstrFile, ofn->nMaxFile);
} }
MemFree(title);
MemFree(defext);
MemFree(filter);
MemFree(customfilter);
MemFree(fodInfos.initdir);
MemFree(fodInfos.filename);
TRACE("selected file: %s\n",ofn->lpstrFile); if (ofn->lpstrInitialDir)
{
DWORD len = ExpandEnvironmentStringsW(ofn->lpstrInitialDir, NULL, 0);
if (len)
{
info->initdir = MemAlloc(len * sizeof(WCHAR));
ExpandEnvironmentStringsW(ofn->lpstrInitialDir, info->initdir, len);
}
}
return ret; info->unicode = TRUE;
} }
/*********************************************************************** static void init_filedlg_infoA(OPENFILENAMEA *ofn, FileOpenDlgInfos *info)
* GetFileDialog95W
*
* Copy the OPENFILENAMEW structure in a FileOpenDlgInfos structure.
* Call GetFileName95 with this structure and clean the memory.
*
*/
static BOOL GetFileDialog95W(LPOPENFILENAMEW ofn,UINT iDlgType)
{ {
BOOL ret; OPENFILENAMEW ofnW;
FileOpenDlgInfos fodInfos;
LPWSTR lpstrSavDir = NULL;
INITCOMMONCONTROLSEX icc;
/* Initialize ComboBoxEx32 */ ofnW = *(OPENFILENAMEW *)ofn;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_USEREX_CLASSES;
InitCommonControlsEx(&icc);
/* Initialize CommDlgExtendedError() */ ofnW.lpstrInitialDir = heap_strdupAtoW(ofn->lpstrInitialDir);
COMDLG32_SetCommDlgExtendedError(0); ofnW.lpstrFile = heap_strdupAtoW(ofn->lpstrFile);
ofnW.lpstrDefExt = heap_strdupAtoW(ofn->lpstrDefExt);
ofnW.lpstrTitle = heap_strdupAtoW(ofn->lpstrTitle);
/* Initialize FileOpenDlgInfos structure */ if (ofn->lpstrFilter)
ZeroMemory(&fodInfos, sizeof(FileOpenDlgInfos)); {
int n, len;
LPCSTR s;
/* filter is a list... title\0ext\0......\0\0 */
s = ofn->lpstrFilter;
while (*s) s = s+strlen(s)+1;
s++;
n = s - ofn->lpstrFilter;
len = MultiByteToWideChar(CP_ACP, 0, ofn->lpstrFilter, n, NULL, 0);
ofnW.lpstrFilter = MemAlloc(len * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, ofn->lpstrFilter, n, (WCHAR *)ofnW.lpstrFilter, len);
}
/* Pass in the original ofn */ /* convert lpstrCustomFilter */
fodInfos.ofnInfos = ofn; if (ofn->lpstrCustomFilter)
{
int n, len;
LPCSTR s;
/* customfilter contains a pair of strings... title\0ext\0 */
s = ofn->lpstrCustomFilter;
if (*s) s = s+strlen(s)+1;
if (*s) s = s+strlen(s)+1;
n = s - ofn->lpstrCustomFilter;
len = MultiByteToWideChar(CP_ACP, 0, ofn->lpstrCustomFilter, n, NULL, 0);
ofnW.lpstrCustomFilter = MemAlloc(len * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, ofn->lpstrCustomFilter, n, ofnW.lpstrCustomFilter, len);
}
fodInfos.title = ofn->lpstrTitle; init_filedlg_infoW(&ofnW, info);
fodInfos.defext = ofn->lpstrDefExt;
fodInfos.filter = ofn->lpstrFilter;
fodInfos.customfilter = ofn->lpstrCustomFilter;
/* convert string arguments, save others */ /* fixup A-specific fields */
if(ofn->lpstrFile) info->ofnInfos = (OPENFILENAMEW *)ofn;
{ info->unicode = FALSE;
fodInfos.filename = MemAlloc(ofn->nMaxFile*sizeof(WCHAR));
lstrcpynW(fodInfos.filename,ofn->lpstrFile,ofn->nMaxFile);
}
else
fodInfos.filename = NULL;
fodInfos.initdir = NULL; /* free what was duplicated */
if(ofn->lpstrInitialDir) MemFree((WCHAR *)ofnW.lpstrInitialDir);
{ MemFree((WCHAR *)ofnW.lpstrFile);
/* fodInfos.initdir = strdupW(ofn->lpstrInitialDir); */ }
DWORD len = ExpandEnvironmentStringsW(ofn->lpstrInitialDir, NULL, 0);
if (len) /***********************************************************************
* GetFileDialog95
*
* Call GetFileName95 with this structure and clean the memory.
*/
static BOOL GetFileDialog95(FileOpenDlgInfos *info, UINT dlg_type)
{
WCHAR *current_dir = NULL;
BOOL ret;
/* save current directory */
if (info->ofnInfos->Flags & OFN_NOCHANGEDIR)
{ {
fodInfos.initdir = MemAlloc(len * sizeof(WCHAR)); current_dir = MemAlloc(MAX_PATH * sizeof(WCHAR));
ExpandEnvironmentStringsW(ofn->lpstrInitialDir, fodInfos.initdir, len); GetCurrentDirectoryW(MAX_PATH, current_dir);
} }
}
/* save current directory */
if (ofn->Flags & OFN_NOCHANGEDIR)
{
lpstrSavDir = MemAlloc(MAX_PATH*sizeof(WCHAR));
GetCurrentDirectoryW(MAX_PATH, lpstrSavDir);
}
fodInfos.unicode = TRUE; switch (dlg_type)
{
case OPEN_DIALOG:
ret = GetFileName95(info);
break;
case SAVE_DIALOG:
info->DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
ret = GetFileName95(info);
break;
default:
ret = FALSE;
}
switch(iDlgType) if (current_dir)
{ {
case OPEN_DIALOG : SetCurrentDirectoryW(current_dir);
ret = GetFileName95(&fodInfos); MemFree(current_dir);
break; }
case SAVE_DIALOG :
fodInfos.DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
ret = GetFileName95(&fodInfos);
break;
default :
ret = FALSE;
}
if (lpstrSavDir) if (!info->unicode)
{ {
SetCurrentDirectoryW(lpstrSavDir); MemFree((WCHAR *)info->defext);
MemFree(lpstrSavDir); MemFree((WCHAR *)info->title);
} MemFree((WCHAR *)info->filter);
MemFree((WCHAR *)info->customfilter);
}
/* restore saved IN arguments and convert OUT arguments back */ MemFree(info->filename);
MemFree(fodInfos.filename); MemFree(info->initdir);
MemFree(fodInfos.initdir); return ret;
return ret;
} }
/****************************************************************************** /******************************************************************************
...@@ -4080,8 +4006,7 @@ static inline BOOL is_win16_looks(DWORD flags) ...@@ -4080,8 +4006,7 @@ static inline BOOL is_win16_looks(DWORD flags)
* FALSE on cancel, error, close or filename-does-not-fit-in-buffer. * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
* *
*/ */
BOOL WINAPI GetOpenFileNameA( BOOL WINAPI GetOpenFileNameA(OPENFILENAMEA *ofn)
LPOPENFILENAMEA ofn) /* [in/out] address of init structure */
{ {
TRACE("flags %08x\n", ofn->Flags); TRACE("flags %08x\n", ofn->Flags);
...@@ -4098,7 +4023,12 @@ BOOL WINAPI GetOpenFileNameA( ...@@ -4098,7 +4023,12 @@ BOOL WINAPI GetOpenFileNameA(
if (is_win16_looks(ofn->Flags)) if (is_win16_looks(ofn->Flags))
return GetFileName31A(ofn, OPEN_DIALOG); return GetFileName31A(ofn, OPEN_DIALOG);
else else
return GetFileDialog95A(ofn, OPEN_DIALOG); {
FileOpenDlgInfos info;
init_filedlg_infoA(ofn, &info);
return GetFileDialog95(&info, OPEN_DIALOG);
}
} }
/*********************************************************************** /***********************************************************************
...@@ -4111,8 +4041,7 @@ BOOL WINAPI GetOpenFileNameA( ...@@ -4111,8 +4041,7 @@ BOOL WINAPI GetOpenFileNameA(
* FALSE on cancel, error, close or filename-does-not-fit-in-buffer. * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
* *
*/ */
BOOL WINAPI GetOpenFileNameW( BOOL WINAPI GetOpenFileNameW(OPENFILENAMEW *ofn)
LPOPENFILENAMEW ofn) /* [in/out] address of init structure */
{ {
TRACE("flags %08x\n", ofn->Flags); TRACE("flags %08x\n", ofn->Flags);
...@@ -4129,7 +4058,12 @@ BOOL WINAPI GetOpenFileNameW( ...@@ -4129,7 +4058,12 @@ BOOL WINAPI GetOpenFileNameW(
if (is_win16_looks(ofn->Flags)) if (is_win16_looks(ofn->Flags))
return GetFileName31W(ofn, OPEN_DIALOG); return GetFileName31W(ofn, OPEN_DIALOG);
else else
return GetFileDialog95W(ofn, OPEN_DIALOG); {
FileOpenDlgInfos info;
init_filedlg_infoW(ofn, &info);
return GetFileDialog95(&info, OPEN_DIALOG);
}
} }
...@@ -4143,8 +4077,7 @@ BOOL WINAPI GetOpenFileNameW( ...@@ -4143,8 +4077,7 @@ BOOL WINAPI GetOpenFileNameW(
* FALSE on cancel, error, close or filename-does-not-fit-in-buffer. * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
* *
*/ */
BOOL WINAPI GetSaveFileNameA( BOOL WINAPI GetSaveFileNameA(OPENFILENAMEA *ofn)
LPOPENFILENAMEA ofn) /* [in/out] address of init structure */
{ {
if (!valid_struct_size( ofn->lStructSize )) if (!valid_struct_size( ofn->lStructSize ))
{ {
...@@ -4155,7 +4088,12 @@ BOOL WINAPI GetSaveFileNameA( ...@@ -4155,7 +4088,12 @@ BOOL WINAPI GetSaveFileNameA(
if (is_win16_looks(ofn->Flags)) if (is_win16_looks(ofn->Flags))
return GetFileName31A(ofn, SAVE_DIALOG); return GetFileName31A(ofn, SAVE_DIALOG);
else else
return GetFileDialog95A(ofn, SAVE_DIALOG); {
FileOpenDlgInfos info;
init_filedlg_infoA(ofn, &info);
return GetFileDialog95(&info, SAVE_DIALOG);
}
} }
/*********************************************************************** /***********************************************************************
...@@ -4180,7 +4118,12 @@ BOOL WINAPI GetSaveFileNameW( ...@@ -4180,7 +4118,12 @@ BOOL WINAPI GetSaveFileNameW(
if (is_win16_looks(ofn->Flags)) if (is_win16_looks(ofn->Flags))
return GetFileName31W(ofn, SAVE_DIALOG); return GetFileName31W(ofn, SAVE_DIALOG);
else else
return GetFileDialog95W(ofn, SAVE_DIALOG); {
FileOpenDlgInfos info;
init_filedlg_infoW(ofn, &info);
return GetFileDialog95(&info, SAVE_DIALOG);
}
} }
/*********************************************************************** /***********************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment