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"
32 #include "wx/textctrl.h"
35 // ============================================================================
37 // ============================================================================
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED
)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED
)
41 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent
, wxCommandEvent
)
43 // ----------------------------------------------------------------------------
44 // wxFileDirPickerCtrlBase
45 // ----------------------------------------------------------------------------
47 #define M_PICKER ((wxFilePickerWidget*)m_picker)
49 bool wxFileDirPickerCtrlBase::CreateBase( wxWindow
*parent
, wxWindowID id
,
50 const wxString
&path
, const wxString
&message
,
51 const wxString
&wildcard
,
52 const wxPoint
&pos
, const wxSize
&size
,
53 long style
, const wxValidator
& validator
,
54 const wxString
&name
)
56 wxASSERT_MSG(path
.empty() || CheckPath(path
), wxT("Invalid initial path !"));
58 if (!wxPickerBase::CreateBase(parent
, id
, path
, pos
, size
,
59 style
, validator
, name
))
62 if (!HasFlag(wxFLP_OPEN
) && !HasFlag(wxFLP_SAVE
))
63 m_windowStyle
|= wxFLP_OPEN
; // wxFD_OPEN is the default
65 // check that the styles are not contradictory
66 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE
) && HasFlag(wxFLP_OPEN
)),
67 _T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
69 wxASSERT_MSG( !HasFlag(wxFLP_SAVE
) || !HasFlag(wxFLP_FILE_MUST_EXIST
),
70 _T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
72 wxASSERT_MSG( !HasFlag(wxFLP_OPEN
) || !HasFlag(wxFLP_OVERWRITE_PROMPT
),
73 _T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
75 // create a wxFilePickerWidget or a wxDirPickerWidget...
76 if (!CreatePicker(this, path
, message
, wildcard
))
78 m_picker
->Connect(GetEventType(),
79 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange
),
82 // default's wxPickerBase textctrl limit is too small for this control:
84 if (m_text
) m_text
->SetMaxLength(512);
89 void wxFileDirPickerCtrlBase::SetPath(const wxString
&path
)
91 M_PICKER
->SetPath(path
);
92 UpdateTextCtrlFromPicker();
95 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
99 if (m_bIgnoreNextTextCtrlUpdate
)
101 // ignore this update
102 m_bIgnoreNextTextCtrlUpdate
= false;
106 // remove the eventually present path-separator from the end of the textctrl
107 // string otherwise we would generate a wxFileDirPickerEvent when changing
108 // from e.g. /home/user to /home/user/ and we want to avoid it !
109 wxString
newpath(m_text
->GetValue());
110 if (!newpath
.empty() && wxFileName::IsPathSeparator(newpath
.Last()))
111 newpath
.RemoveLast();
112 if (!CheckPath(newpath
))
113 return; // invalid user input
115 if (M_PICKER
->GetPath() != newpath
)
117 M_PICKER
->SetPath(newpath
);
119 // update current working directory, if necessary
120 // NOTE: the path separator is required because if newpath is "C:"
121 // then no change would happen
123 wxSetWorkingDirectory(newpath
+ wxFileName::GetPathSeparator());
126 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), newpath
);
127 GetEventHandler()->ProcessEvent(event
);
131 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
134 return; // no textctrl to update
136 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
137 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
138 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
139 m_bIgnoreNextTextCtrlUpdate
= true;
140 m_text
->SetValue(M_PICKER
->GetPath());
145 // ----------------------------------------------------------------------------
146 // wxFileDirPickerCtrlBase - event handlers
147 // ----------------------------------------------------------------------------
149 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent
&ev
)
151 UpdateTextCtrlFromPicker();
153 // the wxFilePickerWidget sent us a colour-change notification.
154 // forward this event to our parent
155 wxFileDirPickerEvent
event(GetEventType(), this, GetId(), ev
.GetPath());
156 GetEventHandler()->ProcessEvent(event
);
159 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
161 #if wxUSE_FILEPICKERCTRL
162 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl
, wxPickerBase
)
164 #if wxUSE_DIRPICKERCTRL
165 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl
, wxPickerBase
)