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"
31 // ============================================================================
33 // ============================================================================
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED
)
37 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent
, wxCommandEvent
)
39 // ----------------------------------------------------------------------------
40 // wxFileDirPickerCtrlBase
41 // ----------------------------------------------------------------------------
43 #define M_PICKER ((wxFilePickerWidget*)m_picker)
45 bool wxFileDirPickerCtrlBase::CreateBase( wxWindow
*parent
, wxWindowID id
,
46 const wxString
&path
, const wxString
&message
,
47 const wxString
&wildcard
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 long style
, const wxValidator
& validator
,
50 const wxString
&name
)
52 wxASSERT_MSG(path
.empty() || CheckPath(path
), wxT("Invalid initial path !"));
54 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
55 style
, validator
, name
))
58 // create a wxFilePickerWidget or a wxDirPickerWidget...
59 if (!CreatePicker(this, path
, message
, wildcard
))
61 m_picker
->Connect(GetEventType(),
62 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange
),
65 // default's wxPickerBase textctrl limit is too small for this control:
67 if (m_text
) m_text
->SetMaxLength(512);
72 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
74 M_PICKER
->SetPath(path
);
75 UpdateTextCtrlFromPicker();
78 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
82 if (m_bIgnoreNextTextCtrlUpdate
)
85 m_bIgnoreNextTextCtrlUpdate
= false;
89 // remove the eventually present path-separator from the end of the textctrl
90 // string otherwise we would generate a wxFileDirPickerEvent when changing
91 // from e.g. /home/user to /home/user/ and we want to avoid it !
92 wxString
newpath(m_text
->GetValue());
93 if (!newpath
.empty() && wxFileName::IsPathSeparator(newpath
.Last()))
95 if (!CheckPath(newpath
))
96 return; // invalid user input
98 if (M_PICKER
->GetPath() != newpath
)
100 M_PICKER
->SetPath(newpath
);
102 // update current working directory, if necessary
103 // NOTE: the path separator is required because if newpath is "C:"
104 // then no change would happen
106 wxSetWorkingDirectory(newpath
+ wxFileName::GetPathSeparator());
109 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
110 GetEventHandler()->ProcessEvent(event
);
114 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
117 return; // no textctrl to update
119 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
120 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
121 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
122 m_bIgnoreNextTextCtrlUpdate
= true;
123 m_text
->SetValue(M_PICKER
->GetPath());
128 // ----------------------------------------------------------------------------
129 // wxFileDirPickerCtrlBase - event handlers
130 // ----------------------------------------------------------------------------
132 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
134 UpdateTextCtrlFromPicker();
136 // the wxFilePickerWidget sent us a colour-change notification.
137 // forward this event to our parent
138 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
139 GetEventHandler()->ProcessEvent(event
);
142 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
144 #if wxUSE_FILEPICKERCTRL
145 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
147 #if wxUSE_DIRPICKERCTRL
148 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)