Added wxFilePickerCtrl::SetInitialDirectory().

This method allows to configure the initial directory to be shown when
browsing for files in an initially empty wxFileDirPickerCtrl.

It is also available for wxDirPickerCtrl but is less useful there.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70043 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-12-18 12:34:47 +00:00
parent b9bb74e9fc
commit 75cb911c7c
9 changed files with 182 additions and 17 deletions

View File

@@ -445,6 +445,14 @@ Major new features in this release
was added. was added.
2.9.4:
------
All (GUI):
- Added wxFilePickerCtrl::SetInitialDirectory().
2.9.3: (released 2011-12-14) 2.9.3: (released 2011-12-14)
------ ------

View File

@@ -87,9 +87,13 @@ public:
wxFileDirPickerWidgetBase() { } wxFileDirPickerWidgetBase() { }
virtual ~wxFileDirPickerWidgetBase() { } virtual ~wxFileDirPickerWidgetBase() { }
// Path here is the name of the selected file or directory.
wxString GetPath() const { return m_path; } wxString GetPath() const { return m_path; }
virtual void SetPath(const wxString &str) { m_path=str; } virtual void SetPath(const wxString &str) { m_path=str; }
// Set the directory to open the file browse dialog at initially.
virtual void SetInitialDirectory(const wxString& dir) = 0;
// returns the picker widget cast to wxControl // returns the picker widget cast to wxControl
virtual wxControl *AsControl() = 0; virtual wxControl *AsControl() = 0;
@@ -165,6 +169,12 @@ public: // public API
wxString GetPath() const; wxString GetPath() const;
void SetPath(const wxString &str); void SetPath(const wxString &str);
// Set the directory to open the file browse dialog at initially.
void SetInitialDirectory(const wxString& dir)
{
m_pickerIface->SetInitialDirectory(dir);
}
public: // internal functions public: // internal functions
void UpdatePickerFromTextCtrl(); void UpdatePickerFromTextCtrl();

View File

@@ -58,6 +58,8 @@ public: // overridable
virtual wxEventType GetEventType() const = 0; virtual wxEventType GetEventType() const = 0;
virtual void SetInitialDirectory(const wxString& dir);
public: public:
bool Create(wxWindow *parent, wxWindowID id, bool Create(wxWindow *parent, wxWindowID id,
@@ -82,6 +84,9 @@ protected:
// just doesn't make sense to use picker styles for wxButton anyhow // just doesn't make sense to use picker styles for wxButton anyhow
long m_pickerStyle; long m_pickerStyle;
// Initial directory set by SetInitialDirectory() call or empty.
wxString m_initialDir;
private: private:
// common part of all ctors // common part of all ctors
void Init() { m_pickerStyle = -1; } void Init() { m_pickerStyle = -1; }
@@ -140,16 +145,7 @@ public: // overridable
return filedlgstyle; return filedlgstyle;
} }
virtual wxDialog *CreateDialog() virtual wxDialog *CreateDialog();
{
wxFileDialog *p = new wxFileDialog(GetDialogParent(), m_message,
wxEmptyString, wxEmptyString,
m_wildcard, GetDialogStyle());
// this sets both the default folder and the default file of the dialog
p->SetPath(m_path);
return p;
}
wxEventType GetEventType() const wxEventType GetEventType() const
{ return wxEVT_COMMAND_FILEPICKER_CHANGED; } { return wxEVT_COMMAND_FILEPICKER_CHANGED; }
@@ -160,6 +156,10 @@ protected:
void UpdatePathFromDialog(wxDialog *p) void UpdatePathFromDialog(wxDialog *p)
{ m_path = wxStaticCast(p, wxFileDialog)->GetPath(); } { m_path = wxStaticCast(p, wxFileDialog)->GetPath(); }
// Set the initial directory for the dialog but without overriding the
// directory of the currently selected file, if any.
void DoSetInitialDirectory(wxFileDialog* dialog, const wxString& dir);
private: private:
DECLARE_DYNAMIC_CLASS(wxGenericFileButton) DECLARE_DYNAMIC_CLASS(wxGenericFileButton)
}; };
@@ -204,11 +204,7 @@ public: // overridable
return dirdlgstyle; return dirdlgstyle;
} }
virtual wxDialog *CreateDialog() virtual wxDialog *CreateDialog();
{
return new wxDirDialog(GetDialogParent(), m_message, m_path,
GetDialogStyle());
}
wxEventType GetEventType() const wxEventType GetEventType() const
{ return wxEVT_COMMAND_DIRPICKER_CHANGED; } { return wxEVT_COMMAND_DIRPICKER_CHANGED; }

View File

@@ -101,6 +101,7 @@ public: // overrides
void OnDialogOK(wxCommandEvent &); void OnDialogOK(wxCommandEvent &);
virtual void SetPath(const wxString &str); virtual void SetPath(const wxString &str);
virtual void SetInitialDirectory(const wxString& dir);
// see macro defined above // see macro defined above
FILEDIRBTN_OVERRIDES FILEDIRBTN_OVERRIDES
@@ -171,6 +172,7 @@ public: // overrides
} }
virtual void SetPath(const wxString &str); virtual void SetPath(const wxString &str);
virtual void SetInitialDirectory(const wxString& dir);
// see macro defined above // see macro defined above
FILEDIRBTN_OVERRIDES FILEDIRBTN_OVERRIDES

View File

@@ -130,6 +130,20 @@ public:
*/ */
void SetFileName(const wxFileName& filename); void SetFileName(const wxFileName& filename);
/**
Set the directory to show when starting to browse for files.
This function is mostly useful for the file picker controls which have
no selection initially to configure the directory that should be shown
if the user starts browsing for files as otherwise the directory of
initially selected file is used, which is usually the desired
behaviour and so the directory specified by this function is ignored in
this case.
@since 2.9.4
*/
void SetInitialDirectory(const wxString& dir);
/** /**
Sets the absolute path of the currently selected file. Sets the absolute path of the currently selected file.
This must be a valid file if the @c wxFLP_FILE_MUST_EXIST style was given. This must be a valid file if the @c wxFLP_FILE_MUST_EXIST style was given.
@@ -252,6 +266,20 @@ public:
*/ */
void SetDirName(const wxFileName& dirname); void SetDirName(const wxFileName& dirname);
/**
Set the directory to show when starting to browse for directories.
This function is mostly useful for the directory picker controls which
have no selection initially to configure the directory that should be
shown if the user starts browsing for directories as otherwise the
initially selected directory is used, which is usually the desired
behaviour and so the directory specified by this function is ignored in
this case.
@since 2.9.4
*/
void SetInitialDirectory(const wxString& dir);
/** /**
Sets the absolute path of (the default converter uses current locale's Sets the absolute path of (the default converter uses current locale's
charset)the currently selected directory. charset)the currently selected directory.

View File

@@ -31,6 +31,7 @@
#include "wx/app.h" #include "wx/app.h"
#include "wx/log.h" #include "wx/log.h"
#include "wx/radiobox.h" #include "wx/radiobox.h"
#include "wx/textctrl.h"
#endif #endif
#include "wx/artprov.h" #include "wx/artprov.h"
@@ -52,7 +53,8 @@
enum enum
{ {
PickerPage_Reset = wxID_HIGHEST, PickerPage_Reset = wxID_HIGHEST,
PickerPage_Dir PickerPage_Dir,
PickerPage_SetDir
}; };
@@ -90,6 +92,8 @@ protected:
void OnDirChange(wxFileDirPickerEvent &ev); void OnDirChange(wxFileDirPickerEvent &ev);
void OnCheckBox(wxCommandEvent &ev); void OnCheckBox(wxCommandEvent &ev);
void OnButtonReset(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev);
void OnButtonSetDir(wxCommandEvent &ev);
// the picker // the picker
wxDirPickerCtrl *m_dirPicker; wxDirPickerCtrl *m_dirPicker;
@@ -102,6 +106,8 @@ protected:
*m_chkDirChangeDir, *m_chkDirChangeDir,
*m_chkDirMustExist, *m_chkDirMustExist,
*m_chkSmall; *m_chkSmall;
wxTextCtrl *m_textInitialDir;
wxBoxSizer *m_sizer; wxBoxSizer *m_sizer;
private: private:
@@ -115,6 +121,7 @@ private:
BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage) BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage)
EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset) EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset)
EVT_BUTTON(PickerPage_SetDir, DirPickerWidgetsPage::OnButtonSetDir)
EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange) EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange)
@@ -152,6 +159,16 @@ void DirPickerWidgetsPage::CreateContent()
m_chkSmall = CreateCheckBoxAndAddToSizer(dirbox, "&Small version", false); m_chkSmall = CreateCheckBoxAndAddToSizer(dirbox, "&Small version", false);
boxleft->Add(dirbox, 0, wxALL|wxGROW, 5); boxleft->Add(dirbox, 0, wxALL|wxGROW, 5);
boxleft->Add(CreateSizerWithTextAndButton
(
PickerPage_SetDir,
"&Initial directory",
wxID_ANY,
&m_textInitialDir
), wxSizerFlags().Expand().Border());
boxleft->AddSpacer(10);
boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")), boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
@@ -226,6 +243,11 @@ void DirPickerWidgetsPage::Reset()
// event handlers // event handlers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
{
m_dirPicker->SetInitialDirectory(m_textInitialDir->GetValue());
}
void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{ {
Reset(); Reset();

View File

@@ -31,6 +31,7 @@
#include "wx/app.h" #include "wx/app.h"
#include "wx/log.h" #include "wx/log.h"
#include "wx/radiobox.h" #include "wx/radiobox.h"
#include "wx/textctrl.h"
#endif #endif
#include "wx/artprov.h" #include "wx/artprov.h"
@@ -58,7 +59,8 @@ enum
enum enum
{ {
PickerPage_Reset = wxID_HIGHEST, PickerPage_Reset = wxID_HIGHEST,
PickerPage_File PickerPage_File,
PickerPage_SetDir
}; };
@@ -99,6 +101,7 @@ protected:
void OnFileChange(wxFileDirPickerEvent &ev); void OnFileChange(wxFileDirPickerEvent &ev);
void OnCheckBox(wxCommandEvent &ev); void OnCheckBox(wxCommandEvent &ev);
void OnButtonReset(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev);
void OnButtonSetDir(wxCommandEvent &ev);
// the picker // the picker
@@ -114,6 +117,7 @@ protected:
*m_chkFileChangeDir, *m_chkFileChangeDir,
*m_chkSmall; *m_chkSmall;
wxRadioBox *m_radioFilePickerMode; wxRadioBox *m_radioFilePickerMode;
wxTextCtrl *m_textInitialDir;
wxBoxSizer *m_sizer; wxBoxSizer *m_sizer;
@@ -128,6 +132,7 @@ private:
BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage) BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset) EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
EVT_BUTTON(PickerPage_SetDir, FilePickerWidgetsPage::OnButtonSetDir)
EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange) EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
@@ -174,6 +179,16 @@ void FilePickerWidgetsPage::CreateContent()
boxleft->Add(filebox, 0, wxALL|wxGROW, 5); boxleft->Add(filebox, 0, wxALL|wxGROW, 5);
boxleft->Add(CreateSizerWithTextAndButton
(
PickerPage_SetDir,
"&Initial directory",
wxID_ANY,
&m_textInitialDir
), wxSizerFlags().Expand().Border());
boxleft->AddSpacer(10);
boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")), boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
@@ -280,6 +295,11 @@ void FilePickerWidgetsPage::UpdateFilePickerMode()
// event handlers // event handlers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
{
m_filePicker->SetInitialDirectory(m_textInitialDir->GetValue());
}
void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{ {
Reset(); Reset();

View File

@@ -26,6 +26,7 @@
#if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
#include "wx/filename.h"
#include "wx/filepicker.h" #include "wx/filepicker.h"
#include "wx/scopedptr.h" #include "wx/scopedptr.h"
@@ -106,4 +107,63 @@ void wxGenericFileDirButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
} }
} }
void wxGenericFileDirButton::SetInitialDirectory(const wxString& dir)
{
m_initialDir = dir;
}
// ----------------------------------------------------------------------------
// wxGenericFileutton
// ----------------------------------------------------------------------------
void
wxGenericFileButton::DoSetInitialDirectory(wxFileDialog* dialog,
const wxString& dir)
{
if ( m_path.find_first_of(wxFileName::GetPathSeparators()) ==
wxString::npos )
{
dialog->SetDirectory(dir);
}
}
wxDialog *wxGenericFileButton::CreateDialog()
{
wxFileDialog* const dialog = new wxFileDialog
(
GetDialogParent(),
m_message,
wxEmptyString,
wxEmptyString,
m_wildcard,
GetDialogStyle()
);
// this sets both the default folder and the default file of the dialog
dialog->SetPath(m_path);
// If there is no default file or if it doesn't have any path, use the
// explicitly set initial directory.
if ( !m_initialDir.empty() )
DoSetInitialDirectory(dialog, m_initialDir);
return dialog;
}
// ----------------------------------------------------------------------------
// wxGenericDirButton
// ----------------------------------------------------------------------------
wxDialog *wxGenericDirButton::CreateDialog()
{
wxDirDialog* const dialog = new wxDirDialog
(
GetDialogParent(),
m_message,
m_path.empty() ? m_initialDir : m_path,
GetDialogStyle()
);
return dialog;
}
#endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL

View File

@@ -129,6 +129,14 @@ void wxFileButton::SetPath(const wxString &str)
UpdateDialogPath(m_dialog); UpdateDialogPath(m_dialog);
} }
void wxFileButton::SetInitialDirectory(const wxString& dir)
{
if (m_dialog)
DoSetInitialDirectory(static_cast<wxFileDialog*>(m_dialog), dir);
else
wxGenericFileButton::SetInitialDirectory(dir);
}
#endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__) #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__)
@@ -274,4 +282,15 @@ void wxDirButton::SetPath(const wxString& str)
UpdateDialogPath(m_dialog); UpdateDialogPath(m_dialog);
} }
void wxDirButton::SetInitialDirectory(const wxString& dir)
{
if (m_dialog)
{
if (m_path.empty())
static_cast<wxDirDialog*>(m_dialog)->SetPath(dir);
}
else
wxGenericDirButton::SetInitialDirectory(dir);
}
#endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__) #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)