Skip to content

Commit cf13c96

Browse files
joecarlSiegeLord
authored andcommitted
Fix al_show_native_file_dialog ignores initial directory when flag ALLEGRO_FILECHOOSER_FOLDER is set
1 parent 02382ed commit cf13c96

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

addons/native_dialog/win_dialog.c

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,50 @@ void _al_shutdown_native_dialog_addon(void)
9393
wm_size_cond = NULL;
9494
}
9595

96+
static INT CALLBACK _browse_callback_proc(HWND hwnd, UINT uMsg, LPARAM unused, LPARAM pData)
97+
{
98+
(void)unused;
99+
if (uMsg == BFFM_INITIALIZED) SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
100+
return 0;
101+
}
102+
103+
static TCHAR* _extract_dirname(ALLEGRO_PATH *fullpath)
104+
{
105+
TCHAR* wpath = NULL;
106+
bool is_dir;
107+
const ALLEGRO_USTR *path = al_path_ustr(fullpath, ALLEGRO_NATIVE_PATH_SEP);
108+
109+
if (al_filename_exists(al_cstr(path))) {
110+
ALLEGRO_FS_ENTRY *fs = al_create_fs_entry(al_cstr(path));
111+
is_dir = al_get_fs_entry_mode(fs) & ALLEGRO_FILEMODE_ISDIR;
112+
al_destroy_fs_entry(fs);
113+
}
114+
else {
115+
is_dir = false;
116+
}
117+
118+
if (is_dir) {
119+
wpath = _twin_ustr_to_tchar(path);
120+
}
121+
else {
122+
/* Extract the directory from the path. */
123+
124+
ALLEGRO_PATH* initial_dir_path = NULL;
125+
initial_dir_path = al_clone_path(fullpath);
126+
if (initial_dir_path) {
127+
al_set_path_filename(initial_dir_path, NULL);
128+
wpath = _twin_utf8_to_tchar(al_path_cstr(initial_dir_path, ALLEGRO_NATIVE_PATH_SEP));
129+
al_destroy_path(initial_dir_path);
130+
}
131+
}
132+
return wpath;
133+
}
96134

97135
static bool select_folder(ALLEGRO_DISPLAY_WIN *win_display,
98136
ALLEGRO_NATIVE_DIALOG *fd)
99137
{
100138
BROWSEINFO folderinfo;
139+
TCHAR* wpath = NULL;
101140
LPCITEMIDLIST pidl;
102141
/* Selected path */
103142
TCHAR buf[MAX_PATH] = TEXT("");
@@ -110,10 +149,17 @@ static bool select_folder(ALLEGRO_DISPLAY_WIN *win_display,
110149
folderinfo.lpszTitle = _twin_ustr_to_tchar(fd->title);
111150
folderinfo.ulFlags = 0;
112151
folderinfo.lpfn = NULL;
152+
153+
if (fd->fc_initial_path) {
154+
wpath = _extract_dirname(fd->fc_initial_path);
155+
folderinfo.lpfn = _browse_callback_proc;
156+
folderinfo.lParam = (LPARAM) wpath;
157+
}
113158

114159
pidl = SHBrowseForFolder(&folderinfo);
115160

116161
al_free((void*) folderinfo.lpszTitle);
162+
al_free(wpath);
117163

118164
if (pidl) {
119165
SHGetPathFromIDList(pidl, buf);
@@ -197,7 +243,6 @@ bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
197243
TCHAR* wfilter = NULL;
198244
TCHAR* wpath = NULL;
199245
ALLEGRO_USTR *filter_string = NULL;
200-
ALLEGRO_PATH* initial_dir_path = NULL;
201246

202247
buf[0] = '\0';
203248

@@ -229,29 +274,8 @@ bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
229274

230275
/* Initialize file name buffer and starting directory. */
231276
if (fd->fc_initial_path) {
232-
bool is_dir;
233-
const ALLEGRO_USTR *path = al_path_ustr(fd->fc_initial_path, ALLEGRO_NATIVE_PATH_SEP);
234277

235-
if (al_filename_exists(al_cstr(path))) {
236-
ALLEGRO_FS_ENTRY *fs = al_create_fs_entry(al_cstr(path));
237-
is_dir = al_get_fs_entry_mode(fs) & ALLEGRO_FILEMODE_ISDIR;
238-
al_destroy_fs_entry(fs);
239-
}
240-
else {
241-
is_dir = false;
242-
}
243-
244-
if (is_dir) {
245-
wpath = _twin_ustr_to_tchar(path);
246-
}
247-
else {
248-
/* Extract the directory from the path. */
249-
initial_dir_path = al_clone_path(fd->fc_initial_path);
250-
if (initial_dir_path) {
251-
al_set_path_filename(initial_dir_path, NULL);
252-
wpath = _twin_utf8_to_tchar(al_path_cstr(initial_dir_path, ALLEGRO_NATIVE_PATH_SEP));
253-
}
254-
}
278+
wpath = _extract_dirname(fd->fc_initial_path);
255279
ofn.lpstrInitialDir = wpath;
256280
}
257281

@@ -276,9 +300,6 @@ bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
276300
ret = GetOpenFileName(&ofn);
277301
}
278302

279-
if (initial_dir_path) {
280-
al_destroy_path(initial_dir_path);
281-
}
282303
al_free((void*) ofn.lpstrTitle);
283304
al_free(wfilter);
284305
al_free(wpath);

0 commit comments

Comments
 (0)