]> git.saurik.com Git - wxWidgets.git/blame - src/common/filepickercmn.cpp
Removed two instances of 'template<>' - looks like VC6 did not need it after all...
[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
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
4ce7b1e4 27#if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
ec376c8f 28
4ce7b1e4 29#include "wx/filepicker.h"
58772e49 30#include "wx/filename.h"
ec376c8f 31
a2a7ad6c
PC
32#ifndef WX_PRECOMP
33 #include "wx/textctrl.h"
34#endif
35
ec376c8f
VZ
36// ============================================================================
37// implementation
38// ============================================================================
39
f36e602b
VZ
40const char wxFilePickerCtrlNameStr[] = "filepicker";
41const char wxFilePickerWidgetNameStr[] = "filepickerwidget";
42const char wxDirPickerCtrlNameStr[] = "dirpicker";
43const char wxDirPickerWidgetNameStr[] = "dirpickerwidget";
174e8f45
VZ
44const char wxFilePickerWidgetLabel[] = wxTRANSLATE("Browse");
45const char wxDirPickerWidgetLabel[] = wxTRANSLATE("Browse");
43af39c8 46
9b11752c
VZ
47wxDEFINE_EVENT( wxEVT_COMMAND_FILEPICKER_CHANGED, wxFileDirPickerEvent );
48wxDEFINE_EVENT( wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEvent );
ec376c8f
VZ
49IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
50
51// ----------------------------------------------------------------------------
52// wxFileDirPickerCtrlBase
53// ----------------------------------------------------------------------------
54
5f6475c1
VZ
55bool 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 )
ec376c8f 65{
af6ad984 66 wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path!"));
ec376c8f
VZ
67
68 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
55b43eaa 69 style, validator, name))
ec376c8f
VZ
70 return false;
71
556151f5
MW
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)),
9a83f860 77 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
556151f5
MW
78
79 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
9a83f860 80 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
556151f5
MW
81
82 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
9a83f860 83 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
556151f5 84
ec376c8f 85 // create a wxFilePickerWidget or a wxDirPickerWidget...
af6ad984
VS
86 m_pickerIface = CreatePicker(this, path, message, wildcard);
87 if ( !m_pickerIface )
ec376c8f 88 return false;
af6ad984 89 m_picker = m_pickerIface->AsControl();
a65ffcb2
VZ
90
91 // complete sizer creation
92 wxPickerBase::PostCreation();
93
3c778901 94 DoConnect( m_picker, this );
ec376c8f
VZ
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
af6ad984
VS
103wxString wxFileDirPickerCtrlBase::GetPath() const
104{
105 return m_pickerIface->GetPath();
106}
107
ec376c8f
VZ
108void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
109{
af6ad984 110 m_pickerIface->SetPath(path);
ec376c8f
VZ
111 UpdateTextCtrlFromPicker();
112}
113
114void 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 !
58772e49 128 wxString newpath(GetTextCtrlValue());
ec376c8f
VZ
129 if (!CheckPath(newpath))
130 return; // invalid user input
131
af6ad984 132 if (m_pickerIface->GetPath() != newpath)
ec376c8f 133 {
af6ad984 134 m_pickerIface->SetPath(newpath);
ec376c8f
VZ
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())
58772e49 140 wxSetWorkingDirectory(newpath);
ec376c8f
VZ
141
142 // fire an event
143 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
144 GetEventHandler()->ProcessEvent(event);
145 }
146}
147
148void 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;
af6ad984 157 m_text->SetValue(m_pickerIface->GetPath());
ec376c8f
VZ
158}
159
160
161
162// ----------------------------------------------------------------------------
163// wxFileDirPickerCtrlBase - event handlers
164// ----------------------------------------------------------------------------
165
166void 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
58772e49
VZ
178// ----------------------------------------------------------------------------
179// wxFileDirPickerCtrl
180// ----------------------------------------------------------------------------
181
ec376c8f 182#if wxUSE_FILEPICKERCTRL
58772e49 183
ec376c8f 184IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
58772e49
VZ
185
186bool wxFilePickerCtrl::CheckPath(const wxString& path) const
187{
188 // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we
189 // must accept any path
190 return HasFlag(wxFLP_SAVE) ||
191 !HasFlag(wxFLP_FILE_MUST_EXIST) ||
192 wxFileName::FileExists(path);
193}
194
195wxString wxFilePickerCtrl::GetTextCtrlValue() const
196{
197 // filter it through wxFileName to remove any spurious path separator
198 return wxFileName(m_text->GetValue()).GetFullPath();
199}
200
201#endif // wxUSE_FILEPICKERCTRL
202
203// ----------------------------------------------------------------------------
204// wxDirPickerCtrl
205// ----------------------------------------------------------------------------
206
ec376c8f
VZ
207#if wxUSE_DIRPICKERCTRL
208IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
58772e49
VZ
209
210bool wxDirPickerCtrl::CheckPath(const wxString& path) const
211{
212 // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path
213 return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path);
214}
215
216wxString wxDirPickerCtrl::GetTextCtrlValue() const
217{
218 // filter it through wxFileName to remove any spurious path separator
219 return wxFileName::DirName(m_text->GetValue()).GetPath();
220}
221
222#endif // wxUSE_DIRPICKERCTRL