]> git.saurik.com Git - wxWidgets.git/blame - src/common/filepickercmn.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / common / filepickercmn.cpp
CommitLineData
ec376c8f
VZ
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
ec376c8f
VZ
7// Copyright: (c) Vadim Zeitlin, Francesco Montorsi
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
4ce7b1e4 26#if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
ec376c8f 27
4ce7b1e4 28#include "wx/filepicker.h"
58772e49 29#include "wx/filename.h"
ec376c8f 30
a2a7ad6c
PC
31#ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33#endif
34
ec376c8f
VZ
35// ============================================================================
36// implementation
37// ============================================================================
38
f36e602b
VZ
39const char wxFilePickerCtrlNameStr[] = "filepicker";
40const char wxFilePickerWidgetNameStr[] = "filepickerwidget";
41const char wxDirPickerCtrlNameStr[] = "dirpicker";
42const char wxDirPickerWidgetNameStr[] = "dirpickerwidget";
174e8f45
VZ
43const char wxFilePickerWidgetLabel[] = wxTRANSLATE("Browse");
44const char wxDirPickerWidgetLabel[] = wxTRANSLATE("Browse");
43af39c8 45
ce7fe42e
VZ
46wxDEFINE_EVENT( wxEVT_FILEPICKER_CHANGED, wxFileDirPickerEvent );
47wxDEFINE_EVENT( wxEVT_DIRPICKER_CHANGED, wxFileDirPickerEvent );
ec376c8f
VZ
48IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
49
50// ----------------------------------------------------------------------------
51// wxFileDirPickerCtrlBase
52// ----------------------------------------------------------------------------
53
5f6475c1
VZ
54bool wxFileDirPickerCtrlBase::CreateBase(wxWindow *parent,
55 wxWindowID id,
56 const wxString &path,
57 const wxString &message,
58 const wxString &wildcard,
59 const wxPoint &pos,
60 const wxSize &size,
61 long style,
62 const wxValidator& validator,
63 const wxString &name )
ec376c8f 64{
ec376c8f 65 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
55b43eaa 66 style, validator, name))
ec376c8f
VZ
67 return false;
68
556151f5
MW
69 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
70 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
71
72 // check that the styles are not contradictory
73 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
9a83f860 74 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
556151f5
MW
75
76 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
9a83f860 77 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
556151f5
MW
78
79 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
9a83f860 80 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
556151f5 81
ec376c8f 82 // create a wxFilePickerWidget or a wxDirPickerWidget...
af6ad984
VS
83 m_pickerIface = CreatePicker(this, path, message, wildcard);
84 if ( !m_pickerIface )
ec376c8f 85 return false;
af6ad984 86 m_picker = m_pickerIface->AsControl();
a65ffcb2
VZ
87
88 // complete sizer creation
89 wxPickerBase::PostCreation();
90
3c778901 91 DoConnect( m_picker, this );
ec376c8f
VZ
92
93 // default's wxPickerBase textctrl limit is too small for this control:
94 // make it bigger
95 if (m_text) m_text->SetMaxLength(512);
96
97 return true;
98}
99
af6ad984
VS
100wxString wxFileDirPickerCtrlBase::GetPath() const
101{
102 return m_pickerIface->GetPath();
103}
104
ec376c8f
VZ
105void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
106{
af6ad984 107 m_pickerIface->SetPath(path);
ec376c8f
VZ
108 UpdateTextCtrlFromPicker();
109}
110
111void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
112{
113 wxASSERT(m_text);
114
ec376c8f
VZ
115 // remove the eventually present path-separator from the end of the textctrl
116 // string otherwise we would generate a wxFileDirPickerEvent when changing
117 // from e.g. /home/user to /home/user/ and we want to avoid it !
58772e49 118 wxString newpath(GetTextCtrlValue());
3ccea097
VZ
119
120 // Notice that we use to check here whether the current path is valid, i.e.
121 // if the corresponding file or directory exists for the controls with
122 // wxFLP_FILE_MUST_EXIST or wxDIRP_DIR_MUST_EXIST flag, however we don't do
123 // this any more as we still must notify the program about any changes in
124 // the control, otherwise its view of it would be different from what is
125 // actually shown on the screen, resulting in very confusing UI.
ec376c8f 126
af6ad984 127 if (m_pickerIface->GetPath() != newpath)
ec376c8f 128 {
af6ad984 129 m_pickerIface->SetPath(newpath);
ec376c8f
VZ
130
131 // update current working directory, if necessary
132 // NOTE: the path separator is required because if newpath is "C:"
133 // then no change would happen
134 if (IsCwdToUpdate())
58772e49 135 wxSetWorkingDirectory(newpath);
ec376c8f
VZ
136
137 // fire an event
138 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
139 GetEventHandler()->ProcessEvent(event);
140 }
141}
142
143void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
144{
145 if (!m_text)
146 return; // no textctrl to update
147
44b72116
VZ
148 // Take care to use ChangeValue() here and not SetValue() to avoid
149 // generating an event that would trigger UpdateTextCtrlFromPicker()
150 // resulting in infinite recursion.
151 m_text->ChangeValue(m_pickerIface->GetPath());
ec376c8f
VZ
152}
153
154
155
156// ----------------------------------------------------------------------------
157// wxFileDirPickerCtrlBase - event handlers
158// ----------------------------------------------------------------------------
159
160void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev)
161{
162 UpdateTextCtrlFromPicker();
163
164 // the wxFilePickerWidget sent us a colour-change notification.
165 // forward this event to our parent
166 wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath());
167 GetEventHandler()->ProcessEvent(event);
168}
169
170#endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
171
58772e49
VZ
172// ----------------------------------------------------------------------------
173// wxFileDirPickerCtrl
174// ----------------------------------------------------------------------------
175
ec376c8f 176#if wxUSE_FILEPICKERCTRL
58772e49 177
ec376c8f 178IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
58772e49 179
ea7ff9ad
VZ
180bool wxFilePickerCtrl::Create(wxWindow *parent,
181 wxWindowID id,
182 const wxString& path,
183 const wxString& message,
184 const wxString& wildcard,
185 const wxPoint& pos,
186 const wxSize& size,
187 long style,
188 const wxValidator& validator,
189 const wxString& name)
190{
191 if ( !wxFileDirPickerCtrlBase::CreateBase
192 (
193 parent, id, path, message, wildcard,
194 pos, size, style, validator, name
195 ) )
196 return false;
197
198 if ( HasTextCtrl() )
199 GetTextCtrl()->AutoCompleteFileNames();
200
201 return true;
202}
203
58772e49
VZ
204wxString wxFilePickerCtrl::GetTextCtrlValue() const
205{
206 // filter it through wxFileName to remove any spurious path separator
207 return wxFileName(m_text->GetValue()).GetFullPath();
208}
209
210#endif // wxUSE_FILEPICKERCTRL
211
212// ----------------------------------------------------------------------------
213// wxDirPickerCtrl
214// ----------------------------------------------------------------------------
215
ec376c8f
VZ
216#if wxUSE_DIRPICKERCTRL
217IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
58772e49 218
ea7ff9ad
VZ
219bool wxDirPickerCtrl::Create(wxWindow *parent,
220 wxWindowID id,
221 const wxString& path,
222 const wxString& message,
223 const wxPoint& pos,
224 const wxSize& size,
225 long style,
226 const wxValidator& validator,
227 const wxString& name)
228{
229 if ( !wxFileDirPickerCtrlBase::CreateBase
230 (
231 parent, id, path, message, wxString(),
232 pos, size, style, validator, name
233 ) )
234 return false;
235
236 if ( HasTextCtrl() )
237 GetTextCtrl()->AutoCompleteDirectories();
238
239 return true;
240}
241
58772e49
VZ
242wxString wxDirPickerCtrl::GetTextCtrlValue() const
243{
244 // filter it through wxFileName to remove any spurious path separator
245 return wxFileName::DirName(m_text->GetValue()).GetPath();
246}
247
248#endif // wxUSE_DIRPICKERCTRL