wxFileDialog & default directory

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

wxFileDialog & default directory

Post by Widgets »

Trying to set the initial default directory for wxFileDialog is driving me to distraction.
My current code looks something like this (under Win10-64-bit, MSVC Express 2010, wxWidgets 3.02):

Code: Select all

  wxString caption = _("Choose a job file");
  wxString wildcard = _("Job files (*.wbj)|*.wbj|All files (*.*)|*.*");
  if ( wsLastPath.IsEmpty() )
    defaultDir = _T(".");
  else
    defaultDir = wsLastPath;
  wxString defaultFilename = wxEmptyString;

  wxFileDialog dialog( this, caption, defaultDir, defaultFilename,
    wildcard, wxFD_OPEN /*| wxFD_CHANGE_DIR*/ ); <<<<< with or without, no difference
  // to avoid some awfully curious Win default behaviour, which nearly 
  // drove me crazy, one has to call SetPath() before showing the file dialog
  // except it DOES NOT WORK!!!!!!
  // see: https://forums.wxwidgets.org/viewtopic.php?f=1&t=40711
  dialog.SetPath( defaultDir );			<<<<<<<<<<<< does nothing
  wxSetWorkingDirectory( defaultDir );	<<<<<<<<<<<< does nothing
  if (dialog.ShowModal() != wxID_OK)
  {
    return false;
  }
As I am selecting different directories within the rest of the app, when I am trying to load a new file with the dialog, it always starts out pointing to the wrong directory.
While this is not fatal, it is more than annoying during development and will be equally frustrating for a user.
How can I force the dialog to open the desired directory every time?

I have seen this issue addressed once on this forum, but I never did find a real solution.
See: viewtopic.php?f=23&t=37856&p=154439&hil ... ry#p154439

Most likely I have missed something, but ....
TIA for any pointers or help
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxFileDialog & default directory

Post by PB »

Are you sure you are passing a valid directory to the dialog? I don't have Win10 to test with, but the thread you linked describes similar issue on Win7 and never saw wxFileDialog ignoring the default directory there. This simple code also works as expected, i.e. the dialog is opened with preset directory all three times:

Code: Select all

#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/stdpaths.h>

class MyApp : public wxApp
{
public:
    void ShowDialog(const wxString& title, const wxString& defaultDir)
    {
        wxFileDialog dlg(NULL, title, defaultDir, wxEmptyString,
                         "All files (*.*)|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
        dlg.ShowModal();
    }

    virtual bool OnInit()
    {                
        ShowDialog("Documents", 
                   wxStandardPaths::Get().GetDocumentsDir());
        ShowDialog("TempDir",
                   wxStandardPaths::Get().GetTempDir());
        ShowDialog("DataDir",
                   wxStandardPaths::Get().GetDataDir());
        return false;
    }
};
IMPLEMENT_APP(MyApp)
I suppose I'm missing something here.

Perhaps it doesn't work when also providing default path directory of which differs from the default dir? But doing that would be odd from the user viewpoint IMO...

Edit
Yes, the default directory is ignored when you pass a default path including folder different from the default directory and the folder from path is used instead. As I said before, that's actually a behaviour I would expect as a user. You wanted to have the full path put into the field for the file name?
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxFileDialog & default directory

Post by Widgets »

Thank you for looking into this.
At this time, my code seems to do what I expect;
whether it was merely a matter of doing a full clean and recompile or whether it was some other finger trouble on my part, I'll have to wait and see. :oops:
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Victory
Earned some good credits
Earned some good credits
Posts: 115
Joined: Fri Mar 19, 2010 1:20 am

Re: wxFileDialog & default directory

Post by Victory »

Resurrecting this somewhat old topic since I am also seeing the behavior mentioned by OP. wxFileDialog ignores the value of the "default directory" argument that gets passed in.

What have people done to get around this issue?
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxFileDialog & default directory

Post by Widgets »

It is a long time ago and I am hard pressed to recall just what I did to fix it, but ....
My current code looks like this - and it seems to do the expected thing - ie start in the expected directory.

Code: Select all

bool MyFrame::GetFileFromUser( wxString &wsJobFile )
{
  wxString wsSourceDrive;
  wxString wsSourceDir;
  wxString wsDestDirRoot;
  wxString defaultDir;
  wxFileName wfnLastFile = g_iniPrefs.data[IE_LAST_JOB_FILE].dataCurrent.wsVal;
  wxString wsLastPath = wfnLastFile.GetPathWithSep();
 
  wxString caption = _("Choose a job file");
  wxString wildcard = _("Job files (*.wbj)|*.wbj|All files (*.*)|*.*");
  if ( wsLastPath.IsEmpty() )
    defaultDir = wxGetApp().m_wsConfigDir;
  else
    defaultDir = wsLastPath;
  wxString defaultFilename = wxEmptyString;

  wxFileDialog dialog( this, caption, defaultDir, 	//<<<< IIRC, make very sure this is a valid, existing directory
    g_iniPrefs.data[IE_LAST_JOB_FILE].dataCurrent.wsVal,
    wildcard, wxFD_OPEN );
  if (dialog.ShowModal() != wxID_OK)
  {
    return false;
  }
  wsJobFile = dialog.GetPath();
  return true;
}
The trick seems to be to make very sure that the directory you want to end up in, is, in fact, a valid, acessible directory and exists.
HTH
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Victory
Earned some good credits
Earned some good credits
Posts: 115
Joined: Fri Mar 19, 2010 1:20 am

Re: wxFileDialog & default directory

Post by Victory »

Yes, I have verified that the folder indeed exists. Based on the posts of others on this issue, it seems like it sometimes "appears" to work, but, doesn't work if the user loads from/saves into another directory. Are you quite sure that it works for you even after such actions?

Thanks.
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxFileDialog & default directory

Post by Widgets »

All I have verified recently, is that it starts out in the expected directory.
I would (almost) expect - though I have not verified - that if you then change directories once in the dialog, that the behavior might/would change, although I cannot quite imagine the scenario you describe. I would certainly assume that in the background the code might record some paths, or assume defaults if anything goes wrong.
You would have to describe a very specific sequence of operations - as well as the existing and/or expected directory tree - in detail for anyone to be able to try and replicate your problem.
My initial problem was that the dialog did not open in the expected directory and that is now 'solved' as far as I am concerned.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Victory
Earned some good credits
Earned some good credits
Posts: 115
Joined: Fri Mar 19, 2010 1:20 am

Re: wxFileDialog & default directory

Post by Victory »

For me, it does not even start in the directory I want. For the "default directory" argument, I am passing "wxStandardPaths::Get().GetDocumentsDir()" which returns the string "C:\\Users\\myUserName\\Documents" on my Windows 10 machine. But, the initial directory shown in the save dialog happens to be something else.
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxFileDialog & default directory

Post by Widgets »

IIRC, it is important to make sure the path is terminated with either a forward or backward slash, otherwise the last component seems to be interpreted as a file name, rather than part of the path.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Victory
Earned some good credits
Earned some good credits
Posts: 115
Joined: Fri Mar 19, 2010 1:20 am

Re: wxFileDialog & default directory

Post by Victory »

Yes, that was it. Ensuring that the string ends with path separator makes it work.
Post Reply