1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filepickercmn.cpp
3 // Purpose: wxFilePickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
7 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
28 #include "wx/filepicker.h"
29 #include "wx/filename.h"
32 #include "wx/textctrl.h"
35 // ============================================================================
37 // ============================================================================
39 const char wxFilePickerCtrlNameStr
[] = "filepicker";
40 const char wxFilePickerWidgetNameStr
[] = "filepickerwidget";
41 const char wxDirPickerCtrlNameStr
[] = "dirpicker";
42 const char wxDirPickerWidgetNameStr
[] = "dirpickerwidget";
43 const char wxFilePickerWidgetLabel
[] = wxTRANSLATE("Browse");
44 const char wxDirPickerWidgetLabel
[] = wxTRANSLATE("Browse");
46 wxDEFINE_EVENT( wxEVT_FILEPICKER_CHANGED
, wxFileDirPickerEvent
);
47 wxDEFINE_EVENT( wxEVT_DIRPICKER_CHANGED
, wxFileDirPickerEvent
);
48 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent
, wxCommandEvent
)
50 // ----------------------------------------------------------------------------
51 // wxFileDirPickerCtrlBase
52 // ----------------------------------------------------------------------------
54 bool wxFileDirPickerCtrlBase::CreateBase(wxWindow
*parent
,
57 const wxString
&message
,
58 const wxString
&wildcard
,
62 const wxValidator
& validator
,
63 const wxString
&name
)
65 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
66 style
, validator
, name
))
69 if (!HasFlag(wxFLP_OPEN
) && !HasFlag(wxFLP_SAVE
))
70 m_windowStyle
|= wxFLP_OPEN
; // wxFD_OPEN is the default
72 // check that the styles are not contradictory
73 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE
) && HasFlag(wxFLP_OPEN
)),
74 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
76 wxASSERT_MSG( !HasFlag(wxFLP_SAVE
) || !HasFlag(wxFLP_FILE_MUST_EXIST
),
77 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
79 wxASSERT_MSG( !HasFlag(wxFLP_OPEN
) || !HasFlag(wxFLP_OVERWRITE_PROMPT
),
80 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
82 // create a wxFilePickerWidget or a wxDirPickerWidget...
83 m_pickerIface
= CreatePicker(this, path
, message
, wildcard
);
86 m_picker
= m_pickerIface
->AsControl();
88 // complete sizer creation
89 wxPickerBase::PostCreation();
91 DoConnect( m_picker
, this );
93 // default's wxPickerBase textctrl limit is too small for this control:
95 if (m_text
) m_text
->SetMaxLength(512);
100 wxString
wxFileDirPickerCtrlBase::GetPath() const
102 return m_pickerIface
->GetPath();
105 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
107 m_pickerIface
->SetPath(path
);
108 UpdateTextCtrlFromPicker();
111 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
115 // remove the eventually present path-separator from the end of the textctrl
116 // string otherwise we would generate a wxFileDirPickerEvent when changing
117 // from e.g. /home/user to /home/user/ and we want to avoid it !
118 wxString
newpath(GetTextCtrlValue());
120 // Notice that we use to check here whether the current path is valid, i.e.
121 // if the corresponding file or directory exists for the controls with
122 // wxFLP_FILE_MUST_EXIST or wxDIRP_DIR_MUST_EXIST flag, however we don't do
123 // this any more as we still must notify the program about any changes in
124 // the control, otherwise its view of it would be different from what is
125 // actually shown on the screen, resulting in very confusing UI.
127 if (m_pickerIface
->GetPath() != newpath
)
129 m_pickerIface
->SetPath(newpath
);
131 // update current working directory, if necessary
132 // NOTE: the path separator is required because if newpath is "C:"
133 // then no change would happen
135 wxSetWorkingDirectory(newpath
);
138 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
139 GetEventHandler()->ProcessEvent(event
);
143 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
146 return; // no textctrl to update
148 // Take care to use ChangeValue() here and not SetValue() to avoid
149 // generating an event that would trigger UpdateTextCtrlFromPicker()
150 // resulting in infinite recursion.
151 m_text
->ChangeValue(m_pickerIface
->GetPath());
156 // ----------------------------------------------------------------------------
157 // wxFileDirPickerCtrlBase - event handlers
158 // ----------------------------------------------------------------------------
160 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
162 UpdateTextCtrlFromPicker();
164 // the wxFilePickerWidget sent us a colour-change notification.
165 // forward this event to our parent
166 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
167 GetEventHandler()->ProcessEvent(event
);
170 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
172 // ----------------------------------------------------------------------------
173 // wxFileDirPickerCtrl
174 // ----------------------------------------------------------------------------
176 #if wxUSE_FILEPICKERCTRL
178 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
180 bool wxFilePickerCtrl::Create(wxWindow
*parent
,
182 const wxString
& path
,
183 const wxString
& message
,
184 const wxString
& wildcard
,
188 const wxValidator
& validator
,
189 const wxString
& name
)
191 if ( !wxFileDirPickerCtrlBase::CreateBase
193 parent
, id
, path
, message
, wildcard
,
194 pos
, size
, style
, validator
, name
199 GetTextCtrl()->AutoCompleteFileNames();
204 wxString
wxFilePickerCtrl::GetTextCtrlValue() const
206 // filter it through wxFileName to remove any spurious path separator
207 return wxFileName(m_text
->GetValue()).GetFullPath();
210 #endif // wxUSE_FILEPICKERCTRL
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 #if wxUSE_DIRPICKERCTRL
217 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)
219 bool wxDirPickerCtrl::Create(wxWindow
*parent
,
221 const wxString
& path
,
222 const wxString
& message
,
226 const wxValidator
& validator
,
227 const wxString
& name
)
229 if ( !wxFileDirPickerCtrlBase::CreateBase
231 parent
, id
, path
, message
, wxString(),
232 pos
, size
, style
, validator
, name
237 GetTextCtrl()->AutoCompleteDirectories();
242 wxString
wxDirPickerCtrl::GetTextCtrlValue() const
244 // filter it through wxFileName to remove any spurious path separator
245 return wxFileName::DirName(m_text
->GetValue()).GetPath();
248 #endif // wxUSE_DIRPICKERCTRL