diff --git a/docs/changes.txt b/docs/changes.txt index 8be8cf5024..914f239308 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -445,6 +445,14 @@ Major new features in this release was added. +2.9.4: +------ + +All (GUI): + +- Added wxFilePickerCtrl::SetInitialDirectory(). + + 2.9.3: (released 2011-12-14) ------ diff --git a/include/wx/filepicker.h b/include/wx/filepicker.h index 3b471b7aca..aa0a4c1a5d 100644 --- a/include/wx/filepicker.h +++ b/include/wx/filepicker.h @@ -87,9 +87,13 @@ public: wxFileDirPickerWidgetBase() { } virtual ~wxFileDirPickerWidgetBase() { } + // Path here is the name of the selected file or directory. wxString GetPath() const { return m_path; } 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 virtual wxControl *AsControl() = 0; @@ -165,6 +169,12 @@ public: // public API wxString GetPath() const; 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 void UpdatePickerFromTextCtrl(); diff --git a/include/wx/generic/filepickerg.h b/include/wx/generic/filepickerg.h index d7a0b495e6..203bd6e682 100644 --- a/include/wx/generic/filepickerg.h +++ b/include/wx/generic/filepickerg.h @@ -58,6 +58,8 @@ public: // overridable virtual wxEventType GetEventType() const = 0; + virtual void SetInitialDirectory(const wxString& dir); + public: bool Create(wxWindow *parent, wxWindowID id, @@ -82,6 +84,9 @@ protected: // just doesn't make sense to use picker styles for wxButton anyhow long m_pickerStyle; + // Initial directory set by SetInitialDirectory() call or empty. + wxString m_initialDir; + private: // common part of all ctors void Init() { m_pickerStyle = -1; } @@ -140,16 +145,7 @@ public: // overridable return filedlgstyle; } - 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; - } + virtual wxDialog *CreateDialog(); wxEventType GetEventType() const { return wxEVT_COMMAND_FILEPICKER_CHANGED; } @@ -160,6 +156,10 @@ protected: void UpdatePathFromDialog(wxDialog *p) { 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: DECLARE_DYNAMIC_CLASS(wxGenericFileButton) }; @@ -204,11 +204,7 @@ public: // overridable return dirdlgstyle; } - virtual wxDialog *CreateDialog() - { - return new wxDirDialog(GetDialogParent(), m_message, m_path, - GetDialogStyle()); - } + virtual wxDialog *CreateDialog(); wxEventType GetEventType() const { return wxEVT_COMMAND_DIRPICKER_CHANGED; } diff --git a/include/wx/gtk/filepicker.h b/include/wx/gtk/filepicker.h index 2276335e91..14267d6260 100644 --- a/include/wx/gtk/filepicker.h +++ b/include/wx/gtk/filepicker.h @@ -101,6 +101,7 @@ public: // overrides void OnDialogOK(wxCommandEvent &); virtual void SetPath(const wxString &str); + virtual void SetInitialDirectory(const wxString& dir); // see macro defined above FILEDIRBTN_OVERRIDES @@ -171,6 +172,7 @@ public: // overrides } virtual void SetPath(const wxString &str); + virtual void SetInitialDirectory(const wxString& dir); // see macro defined above FILEDIRBTN_OVERRIDES diff --git a/interface/wx/filepicker.h b/interface/wx/filepicker.h index 89466aaf09..3053fcefc3 100644 --- a/interface/wx/filepicker.h +++ b/interface/wx/filepicker.h @@ -130,6 +130,20 @@ public: */ 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. 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); + /** + 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 charset)the currently selected directory. diff --git a/samples/widgets/dirpicker.cpp b/samples/widgets/dirpicker.cpp index 7bdfc69d34..f7f96fb37b 100644 --- a/samples/widgets/dirpicker.cpp +++ b/samples/widgets/dirpicker.cpp @@ -31,6 +31,7 @@ #include "wx/app.h" #include "wx/log.h" #include "wx/radiobox.h" + #include "wx/textctrl.h" #endif #include "wx/artprov.h" @@ -52,7 +53,8 @@ enum { PickerPage_Reset = wxID_HIGHEST, - PickerPage_Dir + PickerPage_Dir, + PickerPage_SetDir }; @@ -90,6 +92,8 @@ protected: void OnDirChange(wxFileDirPickerEvent &ev); void OnCheckBox(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev); + void OnButtonSetDir(wxCommandEvent &ev); + // the picker wxDirPickerCtrl *m_dirPicker; @@ -102,6 +106,8 @@ protected: *m_chkDirChangeDir, *m_chkDirMustExist, *m_chkSmall; + wxTextCtrl *m_textInitialDir; + wxBoxSizer *m_sizer; private: @@ -115,6 +121,7 @@ private: BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage) EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset) + EVT_BUTTON(PickerPage_SetDir, DirPickerWidgetsPage::OnButtonSetDir) EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange) @@ -152,6 +159,16 @@ void DirPickerWidgetsPage::CreateContent() m_chkSmall = CreateCheckBoxAndAddToSizer(dirbox, "&Small version", false); 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")), 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); @@ -226,6 +243,11 @@ void DirPickerWidgetsPage::Reset() // event handlers // ---------------------------------------------------------------------------- +void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event)) +{ + m_dirPicker->SetInitialDirectory(m_textInitialDir->GetValue()); +} + void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) { Reset(); diff --git a/samples/widgets/filepicker.cpp b/samples/widgets/filepicker.cpp index 8e39578158..d1468bf30e 100644 --- a/samples/widgets/filepicker.cpp +++ b/samples/widgets/filepicker.cpp @@ -31,6 +31,7 @@ #include "wx/app.h" #include "wx/log.h" #include "wx/radiobox.h" + #include "wx/textctrl.h" #endif #include "wx/artprov.h" @@ -58,7 +59,8 @@ enum enum { PickerPage_Reset = wxID_HIGHEST, - PickerPage_File + PickerPage_File, + PickerPage_SetDir }; @@ -99,6 +101,7 @@ protected: void OnFileChange(wxFileDirPickerEvent &ev); void OnCheckBox(wxCommandEvent &ev); void OnButtonReset(wxCommandEvent &ev); + void OnButtonSetDir(wxCommandEvent &ev); // the picker @@ -114,6 +117,7 @@ protected: *m_chkFileChangeDir, *m_chkSmall; wxRadioBox *m_radioFilePickerMode; + wxTextCtrl *m_textInitialDir; wxBoxSizer *m_sizer; @@ -128,6 +132,7 @@ private: BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage) EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset) + EVT_BUTTON(PickerPage_SetDir, FilePickerWidgetsPage::OnButtonSetDir) EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange) @@ -174,6 +179,16 @@ void FilePickerWidgetsPage::CreateContent() 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")), 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); @@ -280,6 +295,11 @@ void FilePickerWidgetsPage::UpdateFilePickerMode() // event handlers // ---------------------------------------------------------------------------- +void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event)) +{ + m_filePicker->SetInitialDirectory(m_textInitialDir->GetValue()); +} + void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) { Reset(); diff --git a/src/generic/filepickerg.cpp b/src/generic/filepickerg.cpp index aaeffe2e34..edfcfc5fea 100644 --- a/src/generic/filepickerg.cpp +++ b/src/generic/filepickerg.cpp @@ -26,6 +26,7 @@ #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL +#include "wx/filename.h" #include "wx/filepicker.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 diff --git a/src/gtk/filepicker.cpp b/src/gtk/filepicker.cpp index 2b0f7fca9e..f457717e8a 100644 --- a/src/gtk/filepicker.cpp +++ b/src/gtk/filepicker.cpp @@ -129,6 +129,14 @@ void wxFileButton::SetPath(const wxString &str) UpdateDialogPath(m_dialog); } +void wxFileButton::SetInitialDirectory(const wxString& dir) +{ + if (m_dialog) + DoSetInitialDirectory(static_cast(m_dialog), dir); + else + wxGenericFileButton::SetInitialDirectory(dir); +} + #endif // wxUSE_FILEPICKERCTRL && defined(__WXGTK26__) @@ -274,4 +282,15 @@ void wxDirButton::SetPath(const wxString& str) UpdateDialogPath(m_dialog); } +void wxDirButton::SetInitialDirectory(const wxString& dir) +{ + if (m_dialog) + { + if (m_path.empty()) + static_cast(m_dialog)->SetPath(dir); + } + else + wxGenericDirButton::SetInitialDirectory(dir); +} + #endif // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)