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 #include "wx/filepicker.h"
31 // ============================================================================
33 // ============================================================================
35 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED
)
39 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent
, wxCommandEvent
)
41 // ----------------------------------------------------------------------------
42 // wxFileDirPickerCtrlBase
43 // ----------------------------------------------------------------------------
45 #define M_PICKER ((wxFilePickerWidget*)m_picker)
47 bool wxFileDirPickerCtrlBase::CreateBase( wxWindow
*parent
, wxWindowID id
,
48 const wxString
&path
, const wxString
&message
,
49 const wxString
&wildcard
,
50 const wxPoint
&pos
, const wxSize
&size
,
51 long style
, const wxValidator
& validator
,
52 const wxString
&name
)
54 wxASSERT_MSG(path
.IsEmpty() || CheckPath(path
), wxT("Invalid initial path !"));
56 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
57 style
, validator
, name
))
60 // create a wxFilePickerWidget or a wxDirPickerWidget...
61 if (!CreatePicker(this, path
, message
, wildcard
))
63 m_picker
->Connect(GetEventType(),
64 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange
),
67 // default's wxPickerBase textctrl limit is too small for this control:
69 if (m_text
) m_text
->SetMaxLength(512);
74 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
76 M_PICKER
->SetPath(path
);
77 UpdateTextCtrlFromPicker();
80 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
84 if (m_bIgnoreNextTextCtrlUpdate
)
87 m_bIgnoreNextTextCtrlUpdate
= false;
91 // remove the eventually present path-separator from the end of the textctrl
92 // string otherwise we would generate a wxFileDirPickerEvent when changing
93 // from e.g. /home/user to /home/user/ and we want to avoid it !
94 wxString
newpath(m_text
->GetValue());
95 if (!newpath
.empty() && wxFileName::IsPathSeparator(newpath
.Last()))
97 if (!CheckPath(newpath
))
98 return; // invalid user input
100 if (M_PICKER
->GetPath() != newpath
)
102 M_PICKER
->SetPath(newpath
);
104 // update current working directory, if necessary
105 // NOTE: the path separator is required because if newpath is "C:"
106 // then no change would happen
108 wxSetWorkingDirectory(newpath
+ wxFileName::GetPathSeparator());
111 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
112 GetEventHandler()->ProcessEvent(event
);
116 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
119 return; // no textctrl to update
121 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
122 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
123 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
124 m_bIgnoreNextTextCtrlUpdate
= true;
125 m_text
->SetValue(M_PICKER
->GetPath());
130 // ----------------------------------------------------------------------------
131 // wxFileDirPickerCtrlBase - event handlers
132 // ----------------------------------------------------------------------------
134 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
136 UpdateTextCtrlFromPicker();
138 // the wxFilePickerWidget sent us a colour-change notification.
139 // forward this event to our parent
140 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
141 GetEventHandler()->ProcessEvent(event
);
144 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
146 #if wxUSE_FILEPICKERCTRL
147 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
149 #if wxUSE_DIRPICKERCTRL
150 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)