]> git.saurik.com Git - wxWidgets.git/blob - src/common/filepickercmn.cpp
picker controls improvements: fixes to valid paths recognition and event generation...
[wxWidgets.git] / src / common / filepickercmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filepickercmn.cpp
3 // Purpose: wxFilePickerCtrl class implementation
4 // Author: Francesco Montorsi (readapted code written by Vadim Zeitlin)
5 // Modified by:
6 // Created: 15/04/2006
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Francesco Montorsi
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
28
29 #include "wx/filepicker.h"
30 #include "wx/filename.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/textctrl.h"
34 #endif
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED)
42 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
43
44 // ----------------------------------------------------------------------------
45 // wxFileDirPickerCtrlBase
46 // ----------------------------------------------------------------------------
47
48 #define M_PICKER ((wxFilePickerWidget*)m_picker)
49
50 bool wxFileDirPickerCtrlBase::CreateBase( wxWindow *parent, wxWindowID id,
51 const wxString &path, const wxString &message,
52 const wxString &wildcard,
53 const wxPoint &pos, const wxSize &size,
54 long style, const wxValidator& validator,
55 const wxString &name )
56 {
57 wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path !"));
58
59 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
60 style, validator, name))
61 return false;
62
63 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
64 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
65
66 // check that the styles are not contradictory
67 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
68 _T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
69
70 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
71 _T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
72
73 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
74 _T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
75
76 // create a wxFilePickerWidget or a wxDirPickerWidget...
77 if (!CreatePicker(this, path, message, wildcard))
78 return false;
79 m_picker->Connect(GetEventType(),
80 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange),
81 NULL, this);
82
83 // default's wxPickerBase textctrl limit is too small for this control:
84 // make it bigger
85 if (m_text) m_text->SetMaxLength(512);
86
87 return true;
88 }
89
90 void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
91 {
92 M_PICKER->SetPath(path);
93 UpdateTextCtrlFromPicker();
94 }
95
96 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
97 {
98 wxASSERT(m_text);
99
100 if (m_bIgnoreNextTextCtrlUpdate)
101 {
102 // ignore this update
103 m_bIgnoreNextTextCtrlUpdate = false;
104 return;
105 }
106
107 // remove the eventually present path-separator from the end of the textctrl
108 // string otherwise we would generate a wxFileDirPickerEvent when changing
109 // from e.g. /home/user to /home/user/ and we want to avoid it !
110 wxString newpath(GetTextCtrlValue());
111 if (!CheckPath(newpath))
112 return; // invalid user input
113
114 if (M_PICKER->GetPath() != newpath)
115 {
116 M_PICKER->SetPath(newpath);
117
118 // update current working directory, if necessary
119 // NOTE: the path separator is required because if newpath is "C:"
120 // then no change would happen
121 if (IsCwdToUpdate())
122 wxSetWorkingDirectory(newpath);
123
124 // fire an event
125 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
126 GetEventHandler()->ProcessEvent(event);
127 }
128 }
129
130 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
131 {
132 if (!m_text)
133 return; // no textctrl to update
134
135 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
136 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
137 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
138 m_bIgnoreNextTextCtrlUpdate = true;
139 m_text->SetValue(M_PICKER->GetPath());
140 }
141
142
143
144 // ----------------------------------------------------------------------------
145 // wxFileDirPickerCtrlBase - event handlers
146 // ----------------------------------------------------------------------------
147
148 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev)
149 {
150 UpdateTextCtrlFromPicker();
151
152 // the wxFilePickerWidget sent us a colour-change notification.
153 // forward this event to our parent
154 wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath());
155 GetEventHandler()->ProcessEvent(event);
156 }
157
158 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
159
160 // ----------------------------------------------------------------------------
161 // wxFileDirPickerCtrl
162 // ----------------------------------------------------------------------------
163
164 #if wxUSE_FILEPICKERCTRL
165
166 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
167
168 bool wxFilePickerCtrl::CheckPath(const wxString& path) const
169 {
170 // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we
171 // must accept any path
172 return HasFlag(wxFLP_SAVE) ||
173 !HasFlag(wxFLP_FILE_MUST_EXIST) ||
174 wxFileName::FileExists(path);
175 }
176
177 wxString wxFilePickerCtrl::GetTextCtrlValue() const
178 {
179 // filter it through wxFileName to remove any spurious path separator
180 return wxFileName(m_text->GetValue()).GetFullPath();
181 }
182
183 #endif // wxUSE_FILEPICKERCTRL
184
185 // ----------------------------------------------------------------------------
186 // wxDirPickerCtrl
187 // ----------------------------------------------------------------------------
188
189 #if wxUSE_DIRPICKERCTRL
190 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
191
192 bool wxDirPickerCtrl::CheckPath(const wxString& path) const
193 {
194 // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path
195 return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path);
196 }
197
198 wxString wxDirPickerCtrl::GetTextCtrlValue() const
199 {
200 // filter it through wxFileName to remove any spurious path separator
201 return wxFileName::DirName(m_text->GetValue()).GetPath();
202 }
203
204 #endif // wxUSE_DIRPICKERCTRL