]> git.saurik.com Git - wxWidgets.git/blob - src/common/filepickercmn.cpp
use common bottleneck
[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 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_FILEPICKER_CHANGED, wxFileDirPickerEvent );
48 wxDEFINE_EVENT( wxEVT_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 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
67 style, validator, name))
68 return false;
69
70 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
71 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
72
73 // check that the styles are not contradictory
74 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
75 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
76
77 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
78 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
79
80 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
81 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
82
83 // create a wxFilePickerWidget or a wxDirPickerWidget...
84 m_pickerIface = CreatePicker(this, path, message, wildcard);
85 if ( !m_pickerIface )
86 return false;
87 m_picker = m_pickerIface->AsControl();
88
89 // complete sizer creation
90 wxPickerBase::PostCreation();
91
92 DoConnect( m_picker, this );
93
94 // default's wxPickerBase textctrl limit is too small for this control:
95 // make it bigger
96 if (m_text) m_text->SetMaxLength(512);
97
98 return true;
99 }
100
101 wxString wxFileDirPickerCtrlBase::GetPath() const
102 {
103 return m_pickerIface->GetPath();
104 }
105
106 void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
107 {
108 m_pickerIface->SetPath(path);
109 UpdateTextCtrlFromPicker();
110 }
111
112 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
113 {
114 wxASSERT(m_text);
115
116 // remove the eventually present path-separator from the end of the textctrl
117 // string otherwise we would generate a wxFileDirPickerEvent when changing
118 // from e.g. /home/user to /home/user/ and we want to avoid it !
119 wxString newpath(GetTextCtrlValue());
120
121 // Notice that we use to check here whether the current path is valid, i.e.
122 // if the corresponding file or directory exists for the controls with
123 // wxFLP_FILE_MUST_EXIST or wxDIRP_DIR_MUST_EXIST flag, however we don't do
124 // this any more as we still must notify the program about any changes in
125 // the control, otherwise its view of it would be different from what is
126 // actually shown on the screen, resulting in very confusing UI.
127
128 if (m_pickerIface->GetPath() != newpath)
129 {
130 m_pickerIface->SetPath(newpath);
131
132 // update current working directory, if necessary
133 // NOTE: the path separator is required because if newpath is "C:"
134 // then no change would happen
135 if (IsCwdToUpdate())
136 wxSetWorkingDirectory(newpath);
137
138 // fire an event
139 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
140 GetEventHandler()->ProcessEvent(event);
141 }
142 }
143
144 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
145 {
146 if (!m_text)
147 return; // no textctrl to update
148
149 // Take care to use ChangeValue() here and not SetValue() to avoid
150 // generating an event that would trigger UpdateTextCtrlFromPicker()
151 // resulting in infinite recursion.
152 m_text->ChangeValue(m_pickerIface->GetPath());
153 }
154
155
156
157 // ----------------------------------------------------------------------------
158 // wxFileDirPickerCtrlBase - event handlers
159 // ----------------------------------------------------------------------------
160
161 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev)
162 {
163 UpdateTextCtrlFromPicker();
164
165 // the wxFilePickerWidget sent us a colour-change notification.
166 // forward this event to our parent
167 wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath());
168 GetEventHandler()->ProcessEvent(event);
169 }
170
171 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
172
173 // ----------------------------------------------------------------------------
174 // wxFileDirPickerCtrl
175 // ----------------------------------------------------------------------------
176
177 #if wxUSE_FILEPICKERCTRL
178
179 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
180
181 bool wxFilePickerCtrl::Create(wxWindow *parent,
182 wxWindowID id,
183 const wxString& path,
184 const wxString& message,
185 const wxString& wildcard,
186 const wxPoint& pos,
187 const wxSize& size,
188 long style,
189 const wxValidator& validator,
190 const wxString& name)
191 {
192 if ( !wxFileDirPickerCtrlBase::CreateBase
193 (
194 parent, id, path, message, wildcard,
195 pos, size, style, validator, name
196 ) )
197 return false;
198
199 if ( HasTextCtrl() )
200 GetTextCtrl()->AutoCompleteFileNames();
201
202 return true;
203 }
204
205 wxString wxFilePickerCtrl::GetTextCtrlValue() const
206 {
207 // filter it through wxFileName to remove any spurious path separator
208 return wxFileName(m_text->GetValue()).GetFullPath();
209 }
210
211 #endif // wxUSE_FILEPICKERCTRL
212
213 // ----------------------------------------------------------------------------
214 // wxDirPickerCtrl
215 // ----------------------------------------------------------------------------
216
217 #if wxUSE_DIRPICKERCTRL
218 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
219
220 bool wxDirPickerCtrl::Create(wxWindow *parent,
221 wxWindowID id,
222 const wxString& path,
223 const wxString& message,
224 const wxPoint& pos,
225 const wxSize& size,
226 long style,
227 const wxValidator& validator,
228 const wxString& name)
229 {
230 if ( !wxFileDirPickerCtrlBase::CreateBase
231 (
232 parent, id, path, message, wxString(),
233 pos, size, style, validator, name
234 ) )
235 return false;
236
237 if ( HasTextCtrl() )
238 GetTextCtrl()->AutoCompleteDirectories();
239
240 return true;
241 }
242
243 wxString wxDirPickerCtrl::GetTextCtrlValue() const
244 {
245 // filter it through wxFileName to remove any spurious path separator
246 return wxFileName::DirName(m_text->GetValue()).GetPath();
247 }
248
249 #endif // wxUSE_DIRPICKERCTRL