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_FILEPICKER_CHANGED
, wxFileDirPickerEvent
);
48 wxDEFINE_EVENT( wxEVT_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 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
67 style
, validator
, name
))
70 if (!HasFlag(wxFLP_OPEN
) && !HasFlag(wxFLP_SAVE
))
71 m_windowStyle
|= wxFLP_OPEN
; // wxFD_OPEN is the default
73 // check that the styles are not contradictory
74 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE
) && HasFlag(wxFLP_OPEN
)),
75 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
77 wxASSERT_MSG( !HasFlag(wxFLP_SAVE
) || !HasFlag(wxFLP_FILE_MUST_EXIST
),
78 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
80 wxASSERT_MSG( !HasFlag(wxFLP_OPEN
) || !HasFlag(wxFLP_OVERWRITE_PROMPT
),
81 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
83 // create a wxFilePickerWidget or a wxDirPickerWidget...
84 m_pickerIface
= CreatePicker(this, path
, message
, wildcard
);
87 m_picker
= m_pickerIface
->AsControl();
89 // complete sizer creation
90 wxPickerBase::PostCreation();
92 DoConnect( m_picker
, this );
94 // default's wxPickerBase textctrl limit is too small for this control:
96 if (m_text
) m_text
->SetMaxLength(512);
101 wxString
wxFileDirPickerCtrlBase::GetPath() const
103 return m_pickerIface
->GetPath();
106 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
108 m_pickerIface
->SetPath(path
);
109 UpdateTextCtrlFromPicker();
112 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
116 // remove the eventually present path-separator from the end of the textctrl
117 // string otherwise we would generate a wxFileDirPickerEvent when changing
118 // from e.g. /home/user to /home/user/ and we want to avoid it !
119 wxString
newpath(GetTextCtrlValue());
121 // Notice that we use to check here whether the current path is valid, i.e.
122 // if the corresponding file or directory exists for the controls with
123 // wxFLP_FILE_MUST_EXIST or wxDIRP_DIR_MUST_EXIST flag, however we don't do
124 // this any more as we still must notify the program about any changes in
125 // the control, otherwise its view of it would be different from what is
126 // actually shown on the screen, resulting in very confusing UI.
128 if (m_pickerIface
->GetPath() != newpath
)
130 m_pickerIface
->SetPath(newpath
);
132 // update current working directory, if necessary
133 // NOTE: the path separator is required because if newpath is "C:"
134 // then no change would happen
136 wxSetWorkingDirectory(newpath
);
139 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
140 GetEventHandler()->ProcessEvent(event
);
144 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
147 return; // no textctrl to update
149 // Take care to use ChangeValue() here and not SetValue() to avoid
150 // generating an event that would trigger UpdateTextCtrlFromPicker()
151 // resulting in infinite recursion.
152 m_text
->ChangeValue(m_pickerIface
->GetPath());
157 // ----------------------------------------------------------------------------
158 // wxFileDirPickerCtrlBase - event handlers
159 // ----------------------------------------------------------------------------
161 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
163 UpdateTextCtrlFromPicker();
165 // the wxFilePickerWidget sent us a colour-change notification.
166 // forward this event to our parent
167 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
168 GetEventHandler()->ProcessEvent(event
);
171 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
173 // ----------------------------------------------------------------------------
174 // wxFileDirPickerCtrl
175 // ----------------------------------------------------------------------------
177 #if wxUSE_FILEPICKERCTRL
179 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
181 bool wxFilePickerCtrl::Create(wxWindow
*parent
,
183 const wxString
& path
,
184 const wxString
& message
,
185 const wxString
& wildcard
,
189 const wxValidator
& validator
,
190 const wxString
& name
)
192 if ( !wxFileDirPickerCtrlBase::CreateBase
194 parent
, id
, path
, message
, wildcard
,
195 pos
, size
, style
, validator
, name
200 GetTextCtrl()->AutoCompleteFileNames();
205 wxString
wxFilePickerCtrl::GetTextCtrlValue() const
207 // filter it through wxFileName to remove any spurious path separator
208 return wxFileName(m_text
->GetValue()).GetFullPath();
211 #endif // wxUSE_FILEPICKERCTRL
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 #if wxUSE_DIRPICKERCTRL
218 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)
220 bool wxDirPickerCtrl::Create(wxWindow
*parent
,
222 const wxString
& path
,
223 const wxString
& message
,
227 const wxValidator
& validator
,
228 const wxString
& name
)
230 if ( !wxFileDirPickerCtrlBase::CreateBase
232 parent
, id
, path
, message
, wxString(),
233 pos
, size
, style
, validator
, name
238 GetTextCtrl()->AutoCompleteDirectories();
243 wxString
wxDirPickerCtrl::GetTextCtrlValue() const
245 // filter it through wxFileName to remove any spurious path separator
246 return wxFileName::DirName(m_text
->GetValue()).GetPath();
249 #endif // wxUSE_DIRPICKERCTRL