1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filepickercmn.cpp
3 // Purpose: wxFilePickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
29 #include "wx/filepicker.h"
30 #include "wx/filename.h"
33 #include "wx/textctrl.h"
36 // ============================================================================
38 // ============================================================================
40 const char wxFilePickerCtrlNameStr
[] = "filepicker";
41 const char wxFilePickerWidgetNameStr
[] = "filepickerwidget";
42 const char wxDirPickerCtrlNameStr
[] = "dirpicker";
43 const char wxDirPickerWidgetNameStr
[] = "dirpickerwidget";
44 const char wxFilePickerWidgetLabel
[] = wxTRANSLATE("Browse");
45 const char wxDirPickerWidgetLabel
[] = wxTRANSLATE("Browse");
47 wxDEFINE_EVENT( wxEVT_COMMAND_FILEPICKER_CHANGED
, wxFileDirPickerEvent
);
48 wxDEFINE_EVENT( wxEVT_COMMAND_DIRPICKER_CHANGED
, wxFileDirPickerEvent
);
49 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent
, wxCommandEvent
)
51 // ----------------------------------------------------------------------------
52 // wxFileDirPickerCtrlBase
53 // ----------------------------------------------------------------------------
55 bool wxFileDirPickerCtrlBase::CreateBase(wxWindow
*parent
,
58 const wxString
&message
,
59 const wxString
&wildcard
,
63 const wxValidator
& validator
,
64 const wxString
&name
)
66 wxASSERT_MSG(path
.empty() || CheckPath(path
), wxT("Invalid initial path!"));
68 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
69 style
, validator
, name
))
72 if (!HasFlag(wxFLP_OPEN
) && !HasFlag(wxFLP_SAVE
))
73 m_windowStyle
|= wxFLP_OPEN
; // wxFD_OPEN is the default
75 // check that the styles are not contradictory
76 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE
) && HasFlag(wxFLP_OPEN
)),
77 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
79 wxASSERT_MSG( !HasFlag(wxFLP_SAVE
) || !HasFlag(wxFLP_FILE_MUST_EXIST
),
80 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
82 wxASSERT_MSG( !HasFlag(wxFLP_OPEN
) || !HasFlag(wxFLP_OVERWRITE_PROMPT
),
83 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
85 // create a wxFilePickerWidget or a wxDirPickerWidget...
86 m_pickerIface
= CreatePicker(this, path
, message
, wildcard
);
89 m_picker
= m_pickerIface
->AsControl();
91 // complete sizer creation
92 wxPickerBase::PostCreation();
94 DoConnect( m_picker
, this );
96 // default's wxPickerBase textctrl limit is too small for this control:
98 if (m_text
) m_text
->SetMaxLength(512);
103 wxString
wxFileDirPickerCtrlBase::GetPath() const
105 return m_pickerIface
->GetPath();
108 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
110 m_pickerIface
->SetPath(path
);
111 UpdateTextCtrlFromPicker();
114 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
118 // remove the eventually present path-separator from the end of the textctrl
119 // string otherwise we would generate a wxFileDirPickerEvent when changing
120 // from e.g. /home/user to /home/user/ and we want to avoid it !
121 wxString
newpath(GetTextCtrlValue());
122 if (!CheckPath(newpath
))
123 return; // invalid user input
125 if (m_pickerIface
->GetPath() != newpath
)
127 m_pickerIface
->SetPath(newpath
);
129 // update current working directory, if necessary
130 // NOTE: the path separator is required because if newpath is "C:"
131 // then no change would happen
133 wxSetWorkingDirectory(newpath
);
136 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
137 GetEventHandler()->ProcessEvent(event
);
141 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
144 return; // no textctrl to update
146 // Take care to use ChangeValue() here and not SetValue() to avoid
147 // generating an event that would trigger UpdateTextCtrlFromPicker()
148 // resulting in infinite recursion.
149 m_text
->ChangeValue(m_pickerIface
->GetPath());
154 // ----------------------------------------------------------------------------
155 // wxFileDirPickerCtrlBase - event handlers
156 // ----------------------------------------------------------------------------
158 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
160 UpdateTextCtrlFromPicker();
162 // the wxFilePickerWidget sent us a colour-change notification.
163 // forward this event to our parent
164 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
165 GetEventHandler()->ProcessEvent(event
);
168 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
170 // ----------------------------------------------------------------------------
171 // wxFileDirPickerCtrl
172 // ----------------------------------------------------------------------------
174 #if wxUSE_FILEPICKERCTRL
176 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
178 bool wxFilePickerCtrl::Create(wxWindow
*parent
,
180 const wxString
& path
,
181 const wxString
& message
,
182 const wxString
& wildcard
,
186 const wxValidator
& validator
,
187 const wxString
& name
)
189 if ( !wxFileDirPickerCtrlBase::CreateBase
191 parent
, id
, path
, message
, wildcard
,
192 pos
, size
, style
, validator
, name
197 GetTextCtrl()->AutoCompleteFileNames();
202 bool wxFilePickerCtrl::CheckPath(const wxString
& path
) const
204 // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we
205 // must accept any path
206 return HasFlag(wxFLP_SAVE
) ||
207 !HasFlag(wxFLP_FILE_MUST_EXIST
) ||
208 wxFileName::FileExists(path
);
211 wxString
wxFilePickerCtrl::GetTextCtrlValue() const
213 // filter it through wxFileName to remove any spurious path separator
214 return wxFileName(m_text
->GetValue()).GetFullPath();
217 #endif // wxUSE_FILEPICKERCTRL
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 #if wxUSE_DIRPICKERCTRL
224 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)
226 bool wxDirPickerCtrl::Create(wxWindow
*parent
,
228 const wxString
& path
,
229 const wxString
& message
,
233 const wxValidator
& validator
,
234 const wxString
& name
)
236 if ( !wxFileDirPickerCtrlBase::CreateBase
238 parent
, id
, path
, message
, wxString(),
239 pos
, size
, style
, validator
, name
244 GetTextCtrl()->AutoCompleteDirectories();
249 bool wxDirPickerCtrl::CheckPath(const wxString
& path
) const
251 // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path
252 return !HasFlag(wxDIRP_DIR_MUST_EXIST
) || wxFileName::DirExists(path
);
255 wxString
wxDirPickerCtrl::GetTextCtrlValue() const
257 // filter it through wxFileName to remove any spurious path separator
258 return wxFileName::DirName(m_text
->GetValue()).GetPath();
261 #endif // wxUSE_DIRPICKERCTRL