]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxFilePickerCtrl::SetInitialDirectory().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 18 Dec 2011 12:34:47 +0000 (12:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 18 Dec 2011 12:34:47 +0000 (12:34 +0000)
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

docs/changes.txt
include/wx/filepicker.h
include/wx/generic/filepickerg.h
include/wx/gtk/filepicker.h
interface/wx/filepicker.h
samples/widgets/dirpicker.cpp
samples/widgets/filepicker.cpp
src/generic/filepickerg.cpp
src/gtk/filepicker.cpp

index 8be8cf50240ffa25c8d58ca5476df47b09777863..914f2393088c4158c03f38513a391fc90ffc28ef 100644 (file)
@@ -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)
 ------
 
index 3b471b7aca3c81d5975109a6961331f617ad99f4..aa0a4c1a5d5c1a7ae3b80863b647c4106a493554 100644 (file)
@@ -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();
index d7a0b495e637b23dcef8e48d2b22f5437f21b85c..203bd6e682af944e7652d7009bac6c422177c37c 100644 (file)
@@ -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; }
index 2276335e9127cc53c70901e5f1f84320143c5849..14267d626017c7ef749a18cddde702a35b443568 100644 (file)
@@ -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
index 89466aaf095b435fc692793778aa90114abb3405..3053fcefc38c3e32856293d9fb4b768242a16d00 100644 (file)
@@ -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.
index 7bdfc69d34b58fe561b91430c0f9dcbaed9d0ead..f7f96fb37bd9e8de68466dc14020545ed1493c9c 100644 (file)
@@ -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();
index 8e3957815857717eaa7480270fe0f0a95867e03b..d1468bf30ef39a35296ab2daf6510ea73573de62 100644 (file)
@@ -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();
index aaeffe2e34e14488afc48d2035d7188d385cac3f..edfcfc5fea3ad0eaead50a79baf5b99151fc4463 100644 (file)
@@ -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
index 2b0f7fca9e8d7b468fc9f1ffcf33a9c129caf6a9..f457717e8af886c147e7f7695991ea0b9a9982f5 100644 (file)
@@ -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<wxFileDialog*>(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<wxDirDialog*>(m_dialog)->SetPath(dir);
+    }
+    else
+        wxGenericDirButton::SetInitialDirectory(dir);
+}
+
 #endif      // wxUSE_DIRPICKERCTRL && defined(__WXGTK26__)