]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/filepickercmn.cpp
wxDialogBase only has one ctor, so just do initialization in ctor instead of Init()
[wxWidgets.git] / src / common / filepickercmn.cpp
... / ...
CommitLineData
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// 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
26#if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
27
28#include "wx/filepicker.h"
29#include "wx/filename.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33#endif
34
35// ============================================================================
36// implementation
37// ============================================================================
38
39const char wxFilePickerCtrlNameStr[] = "filepicker";
40const char wxFilePickerWidgetNameStr[] = "filepickerwidget";
41const char wxDirPickerCtrlNameStr[] = "dirpicker";
42const char wxDirPickerWidgetNameStr[] = "dirpickerwidget";
43const char wxFilePickerWidgetLabel[] = wxTRANSLATE("Browse");
44const char wxDirPickerWidgetLabel[] = wxTRANSLATE("Browse");
45
46wxDEFINE_EVENT( wxEVT_FILEPICKER_CHANGED, wxFileDirPickerEvent );
47wxDEFINE_EVENT( wxEVT_DIRPICKER_CHANGED, wxFileDirPickerEvent );
48IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
49
50// ----------------------------------------------------------------------------
51// wxFileDirPickerCtrlBase
52// ----------------------------------------------------------------------------
53
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 )
64{
65 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
66 style, validator, name))
67 return false;
68
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)),
74 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
75
76 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
77 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
78
79 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
80 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
81
82 // create a wxFilePickerWidget or a wxDirPickerWidget...
83 m_pickerIface = CreatePicker(this, path, message, wildcard);
84 if ( !m_pickerIface )
85 return false;
86 m_picker = m_pickerIface->AsControl();
87
88 // complete sizer creation
89 wxPickerBase::PostCreation();
90
91 DoConnect( m_picker, this );
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
100wxString wxFileDirPickerCtrlBase::GetPath() const
101{
102 return m_pickerIface->GetPath();
103}
104
105void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
106{
107 m_pickerIface->SetPath(path);
108 UpdateTextCtrlFromPicker();
109}
110
111void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
112{
113 wxASSERT(m_text);
114
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 !
118 wxString newpath(GetTextCtrlValue());
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.
126
127 if (m_pickerIface->GetPath() != newpath)
128 {
129 m_pickerIface->SetPath(newpath);
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())
135 wxSetWorkingDirectory(newpath);
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
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());
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
172// ----------------------------------------------------------------------------
173// wxFileDirPickerCtrl
174// ----------------------------------------------------------------------------
175
176#if wxUSE_FILEPICKERCTRL
177
178IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
179
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
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
216#if wxUSE_DIRPICKERCTRL
217IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
218
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
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