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 if (m_bIgnoreNextTextCtrlUpdate
)
120 // ignore this update
121 m_bIgnoreNextTextCtrlUpdate
= false;
125 // remove the eventually present path-separator from the end of the textctrl
126 // string otherwise we would generate a wxFileDirPickerEvent when changing
127 // from e.g. /home/user to /home/user/ and we want to avoid it !
128 wxString
newpath(GetTextCtrlValue());
129 if (!CheckPath(newpath
))
130 return; // invalid user input
132 if (m_pickerIface
->GetPath() != newpath
)
134 m_pickerIface
->SetPath(newpath
);
136 // update current working directory, if necessary
137 // NOTE: the path separator is required because if newpath is "C:"
138 // then no change would happen
140 wxSetWorkingDirectory(newpath
);
143 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
144 GetEventHandler()->ProcessEvent(event
);
148 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
151 return; // no textctrl to update
153 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
154 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
155 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
156 m_bIgnoreNextTextCtrlUpdate
= true;
157 m_text
->SetValue(m_pickerIface
->GetPath());
162 // ----------------------------------------------------------------------------
163 // wxFileDirPickerCtrlBase - event handlers
164 // ----------------------------------------------------------------------------
166 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
168 UpdateTextCtrlFromPicker();
170 // the wxFilePickerWidget sent us a colour-change notification.
171 // forward this event to our parent
172 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
173 GetEventHandler()->ProcessEvent(event
);
176 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
178 // ----------------------------------------------------------------------------
179 // wxFileDirPickerCtrl
180 // ----------------------------------------------------------------------------
182 #if wxUSE_FILEPICKERCTRL
184 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
186 bool wxFilePickerCtrl::Create(wxWindow
*parent
,
188 const wxString
& path
,
189 const wxString
& message
,
190 const wxString
& wildcard
,
194 const wxValidator
& validator
,
195 const wxString
& name
)
197 if ( !wxFileDirPickerCtrlBase::CreateBase
199 parent
, id
, path
, message
, wildcard
,
200 pos
, size
, style
, validator
, name
205 GetTextCtrl()->AutoCompleteFileNames();
210 bool wxFilePickerCtrl::CheckPath(const wxString
& path
) const
212 // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we
213 // must accept any path
214 return HasFlag(wxFLP_SAVE
) ||
215 !HasFlag(wxFLP_FILE_MUST_EXIST
) ||
216 wxFileName::FileExists(path
);
219 wxString
wxFilePickerCtrl::GetTextCtrlValue() const
221 // filter it through wxFileName to remove any spurious path separator
222 return wxFileName(m_text
->GetValue()).GetFullPath();
225 #endif // wxUSE_FILEPICKERCTRL
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
231 #if wxUSE_DIRPICKERCTRL
232 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)
234 bool wxDirPickerCtrl::Create(wxWindow
*parent
,
236 const wxString
& path
,
237 const wxString
& message
,
241 const wxValidator
& validator
,
242 const wxString
& name
)
244 if ( !wxFileDirPickerCtrlBase::CreateBase
246 parent
, id
, path
, message
, wxString(),
247 pos
, size
, style
, validator
, name
252 GetTextCtrl()->AutoCompleteDirectories();
257 bool wxDirPickerCtrl::CheckPath(const wxString
& path
) const
259 // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path
260 return !HasFlag(wxDIRP_DIR_MUST_EXIST
) || wxFileName::DirExists(path
);
263 wxString
wxDirPickerCtrl::GetTextCtrlValue() const
265 // filter it through wxFileName to remove any spurious path separator
266 return wxFileName::DirName(m_text
->GetValue()).GetPath();
269 #endif // wxUSE_DIRPICKERCTRL