Added wxFilePickerCtrl::SetInitialDirectory().
[wxWidgets.git] / src / generic / filepickerg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filepickerg.cpp
3 // Purpose: wxGenericFileDirButton class implementation
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
28
29 #include "wx/filename.h"
30 #include "wx/filepicker.h"
31
32 #include "wx/scopedptr.h"
33
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileButton, wxButton)
40 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirButton, wxButton)
41
42 // ----------------------------------------------------------------------------
43 // wxGenericFileButton
44 // ----------------------------------------------------------------------------
45
46 bool wxGenericFileDirButton::Create(wxWindow *parent,
47 wxWindowID id,
48 const wxString& label,
49 const wxString& path,
50 const wxString& message,
51 const wxString& wildcard,
52 const wxPoint& pos,
53 const wxSize& size,
54 long style,
55 const wxValidator& validator,
56 const wxString& name)
57 {
58 m_pickerStyle = style;
59
60 // If the special wxPB_SMALL flag is used, ignore the provided label and
61 // use the shortest possible label and the smallest possible button fitting
62 // it.
63 long styleButton = 0;
64 wxString labelButton;
65 if ( m_pickerStyle & wxPB_SMALL )
66 {
67 labelButton = _("...");
68 styleButton = wxBU_EXACTFIT;
69 }
70 else
71 {
72 labelButton = label;
73 }
74
75 // create this button
76 if ( !wxButton::Create(parent, id, labelButton,
77 pos, size, styleButton, validator, name) )
78 {
79 wxFAIL_MSG( wxT("wxGenericFileButton creation failed") );
80 return false;
81 }
82
83 // and handle user clicks on it
84 Connect(GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
85 wxCommandEventHandler(wxGenericFileDirButton::OnButtonClick),
86 NULL, this);
87
88 // create the dialog associated with this button
89 m_path = path;
90 m_message = message;
91 m_wildcard = wildcard;
92
93 return true;
94 }
95
96 void wxGenericFileDirButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev))
97 {
98 wxScopedPtr<wxDialog> p(CreateDialog());
99 if (p->ShowModal() == wxID_OK)
100 {
101 // save updated path in m_path
102 UpdatePathFromDialog(p.get());
103
104 // fire an event
105 wxFileDirPickerEvent event(GetEventType(), this, GetId(), m_path);
106 GetEventHandler()->ProcessEvent(event);
107 }
108 }
109
110 void wxGenericFileDirButton::SetInitialDirectory(const wxString& dir)
111 {
112 m_initialDir = dir;
113 }
114
115 // ----------------------------------------------------------------------------
116 // wxGenericFileutton
117 // ----------------------------------------------------------------------------
118
119 void
120 wxGenericFileButton::DoSetInitialDirectory(wxFileDialog* dialog,
121 const wxString& dir)
122 {
123 if ( m_path.find_first_of(wxFileName::GetPathSeparators()) ==
124 wxString::npos )
125 {
126 dialog->SetDirectory(dir);
127 }
128 }
129
130 wxDialog *wxGenericFileButton::CreateDialog()
131 {
132 wxFileDialog* const dialog = new wxFileDialog
133 (
134 GetDialogParent(),
135 m_message,
136 wxEmptyString,
137 wxEmptyString,
138 m_wildcard,
139 GetDialogStyle()
140 );
141
142 // this sets both the default folder and the default file of the dialog
143 dialog->SetPath(m_path);
144
145 // If there is no default file or if it doesn't have any path, use the
146 // explicitly set initial directory.
147 if ( !m_initialDir.empty() )
148 DoSetInitialDirectory(dialog, m_initialDir);
149
150 return dialog;
151 }
152
153 // ----------------------------------------------------------------------------
154 // wxGenericDirButton
155 // ----------------------------------------------------------------------------
156
157 wxDialog *wxGenericDirButton::CreateDialog()
158 {
159 wxDirDialog* const dialog = new wxDirDialog
160 (
161 GetDialogParent(),
162 m_message,
163 m_path.empty() ? m_initialDir : m_path,
164 GetDialogStyle()
165 );
166 return dialog;
167 }
168
169 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL