]>
Commit | Line | Data |
---|---|---|
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 | 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"); | |
46 | ||
47 | wxDEFINE_EVENT( wxEVT_COMMAND_FILEPICKER_CHANGED, wxFileDirPickerEvent ); | |
48 | wxDEFINE_EVENT( wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEvent ); | |
49 | IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent) | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // wxFileDirPickerCtrlBase | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | bool wxFileDirPickerCtrlBase::CreateBase(wxWindow *parent, | |
56 | wxWindowID id, | |
57 | const wxString &path, | |
58 | const wxString &message, | |
59 | const wxString &wildcard, | |
60 | const wxPoint &pos, | |
61 | const wxSize &size, | |
62 | long style, | |
63 | const wxValidator& validator, | |
64 | const wxString &name ) | |
65 | { | |
66 | wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path!")); | |
67 | ||
68 | if (!wxPickerBase::CreateBase(parent, id, path, pos, size, | |
69 | style, validator, name)) | |
70 | return false; | |
71 | ||
72 | if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE)) | |
73 | m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default | |
74 | ||
75 | // check that the styles are not contradictory | |
76 | wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)), | |
77 | wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") ); | |
78 | ||
79 | wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST), | |
80 | wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) ); | |
81 | ||
82 | wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT), | |
83 | wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") ); | |
84 | ||
85 | // create a wxFilePickerWidget or a wxDirPickerWidget... | |
86 | m_pickerIface = CreatePicker(this, path, message, wildcard); | |
87 | if ( !m_pickerIface ) | |
88 | return false; | |
89 | m_picker = m_pickerIface->AsControl(); | |
90 | ||
91 | // complete sizer creation | |
92 | wxPickerBase::PostCreation(); | |
93 | ||
94 | DoConnect( m_picker, this ); | |
95 | ||
96 | // default's wxPickerBase textctrl limit is too small for this control: | |
97 | // make it bigger | |
98 | if (m_text) m_text->SetMaxLength(512); | |
99 | ||
100 | return true; | |
101 | } | |
102 | ||
103 | wxString wxFileDirPickerCtrlBase::GetPath() const | |
104 | { | |
105 | return m_pickerIface->GetPath(); | |
106 | } | |
107 | ||
108 | void wxFileDirPickerCtrlBase::SetPath(const wxString &path) | |
109 | { | |
110 | m_pickerIface->SetPath(path); | |
111 | UpdateTextCtrlFromPicker(); | |
112 | } | |
113 | ||
114 | void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl() | |
115 | { | |
116 | wxASSERT(m_text); | |
117 | ||
118 | if (m_bIgnoreNextTextCtrlUpdate) | |
119 | { | |
120 | // ignore this update | |
121 | m_bIgnoreNextTextCtrlUpdate = false; | |
122 | return; | |
123 | } | |
124 | ||
125 | // remove the eventually present path-separator from the end of the textctrl | |
126 | // string otherwise we would generate a wxFileDirPickerEvent when changing | |
127 | // from e.g. /home/user to /home/user/ and we want to avoid it ! | |
128 | wxString newpath(GetTextCtrlValue()); | |
129 | if (!CheckPath(newpath)) | |
130 | return; // invalid user input | |
131 | ||
132 | if (m_pickerIface->GetPath() != newpath) | |
133 | { | |
134 | m_pickerIface->SetPath(newpath); | |
135 | ||
136 | // update current working directory, if necessary | |
137 | // NOTE: the path separator is required because if newpath is "C:" | |
138 | // then no change would happen | |
139 | if (IsCwdToUpdate()) | |
140 | wxSetWorkingDirectory(newpath); | |
141 | ||
142 | // fire an event | |
143 | wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath); | |
144 | GetEventHandler()->ProcessEvent(event); | |
145 | } | |
146 | } | |
147 | ||
148 | void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker() | |
149 | { | |
150 | if (!m_text) | |
151 | return; // no textctrl to update | |
152 | ||
153 | // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED | |
154 | // which will trigger a unneeded UpdateFromTextCtrl(); thus before using | |
155 | // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag... | |
156 | m_bIgnoreNextTextCtrlUpdate = true; | |
157 | m_text->SetValue(m_pickerIface->GetPath()); | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | // ---------------------------------------------------------------------------- | |
163 | // wxFileDirPickerCtrlBase - event handlers | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
166 | void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev) | |
167 | { | |
168 | UpdateTextCtrlFromPicker(); | |
169 | ||
170 | // the wxFilePickerWidget sent us a colour-change notification. | |
171 | // forward this event to our parent | |
172 | wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath()); | |
173 | GetEventHandler()->ProcessEvent(event); | |
174 | } | |
175 | ||
176 | #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // wxFileDirPickerCtrl | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | #if wxUSE_FILEPICKERCTRL | |
183 | ||
184 | IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase) | |
185 | ||
186 | bool wxFilePickerCtrl::Create(wxWindow *parent, | |
187 | wxWindowID id, | |
188 | const wxString& path, | |
189 | const wxString& message, | |
190 | const wxString& wildcard, | |
191 | const wxPoint& pos, | |
192 | const wxSize& size, | |
193 | long style, | |
194 | const wxValidator& validator, | |
195 | const wxString& name) | |
196 | { | |
197 | if ( !wxFileDirPickerCtrlBase::CreateBase | |
198 | ( | |
199 | parent, id, path, message, wildcard, | |
200 | pos, size, style, validator, name | |
201 | ) ) | |
202 | return false; | |
203 | ||
204 | if ( HasTextCtrl() ) | |
205 | GetTextCtrl()->AutoCompleteFileNames(); | |
206 | ||
207 | return true; | |
208 | } | |
209 | ||
210 | bool wxFilePickerCtrl::CheckPath(const wxString& path) const | |
211 | { | |
212 | // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we | |
213 | // must accept any path | |
214 | return HasFlag(wxFLP_SAVE) || | |
215 | !HasFlag(wxFLP_FILE_MUST_EXIST) || | |
216 | wxFileName::FileExists(path); | |
217 | } | |
218 | ||
219 | wxString wxFilePickerCtrl::GetTextCtrlValue() const | |
220 | { | |
221 | // filter it through wxFileName to remove any spurious path separator | |
222 | return wxFileName(m_text->GetValue()).GetFullPath(); | |
223 | } | |
224 | ||
225 | #endif // wxUSE_FILEPICKERCTRL | |
226 | ||
227 | // ---------------------------------------------------------------------------- | |
228 | // wxDirPickerCtrl | |
229 | // ---------------------------------------------------------------------------- | |
230 | ||
231 | #if wxUSE_DIRPICKERCTRL | |
232 | IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase) | |
233 | ||
234 | bool wxDirPickerCtrl::Create(wxWindow *parent, | |
235 | wxWindowID id, | |
236 | const wxString& path, | |
237 | const wxString& message, | |
238 | const wxPoint& pos, | |
239 | const wxSize& size, | |
240 | long style, | |
241 | const wxValidator& validator, | |
242 | const wxString& name) | |
243 | { | |
244 | if ( !wxFileDirPickerCtrlBase::CreateBase | |
245 | ( | |
246 | parent, id, path, message, wxString(), | |
247 | pos, size, style, validator, name | |
248 | ) ) | |
249 | return false; | |
250 | ||
251 | if ( HasTextCtrl() ) | |
252 | GetTextCtrl()->AutoCompleteDirectories(); | |
253 | ||
254 | return true; | |
255 | } | |
256 | ||
257 | bool wxDirPickerCtrl::CheckPath(const wxString& path) const | |
258 | { | |
259 | // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path | |
260 | return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path); | |
261 | } | |
262 | ||
263 | wxString wxDirPickerCtrl::GetTextCtrlValue() const | |
264 | { | |
265 | // filter it through wxFileName to remove any spurious path separator | |
266 | return wxFileName::DirName(m_text->GetValue()).GetPath(); | |
267 | } | |
268 | ||
269 | #endif // wxUSE_DIRPICKERCTRL |