]> git.saurik.com Git - wxWidgets.git/blame - src/common/filepickercmn.cpp
fixed crash in wxDb::Open() in Unicode build due to wrong interpretation of BufferLen...
[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
ec376c8f
VZ
40DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED)
41DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED)
42IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
43
44// ----------------------------------------------------------------------------
45// wxFileDirPickerCtrlBase
46// ----------------------------------------------------------------------------
47
ec376c8f
VZ
48bool wxFileDirPickerCtrlBase::CreateBase( wxWindow *parent, wxWindowID id,
49 const wxString &path, const wxString &message,
50 const wxString &wildcard,
51 const wxPoint &pos, const wxSize &size,
52 long style, const wxValidator& validator,
53 const wxString &name )
54{
af6ad984 55 wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path!"));
ec376c8f
VZ
56
57 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
58 style, validator, name))
59 return false;
60
556151f5
MW
61 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
62 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
63
64 // check that the styles are not contradictory
65 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
66 _T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
67
68 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
69 _T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
70
71 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
72 _T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
73
ec376c8f 74 // create a wxFilePickerWidget or a wxDirPickerWidget...
af6ad984
VS
75 m_pickerIface = CreatePicker(this, path, message, wildcard);
76 if ( !m_pickerIface )
ec376c8f 77 return false;
af6ad984 78 m_picker = m_pickerIface->AsControl();
a65ffcb2
VZ
79
80 // complete sizer creation
81 wxPickerBase::PostCreation();
82
ec376c8f
VZ
83 m_picker->Connect(GetEventType(),
84 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange),
85 NULL, this);
86
87 // default's wxPickerBase textctrl limit is too small for this control:
88 // make it bigger
89 if (m_text) m_text->SetMaxLength(512);
90
91 return true;
92}
93
af6ad984
VS
94wxString wxFileDirPickerCtrlBase::GetPath() const
95{
96 return m_pickerIface->GetPath();
97}
98
ec376c8f
VZ
99void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
100{
af6ad984 101 m_pickerIface->SetPath(path);
ec376c8f
VZ
102 UpdateTextCtrlFromPicker();
103}
104
105void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
106{
107 wxASSERT(m_text);
108
109 if (m_bIgnoreNextTextCtrlUpdate)
110 {
111 // ignore this update
112 m_bIgnoreNextTextCtrlUpdate = false;
113 return;
114 }
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 !
58772e49 119 wxString newpath(GetTextCtrlValue());
ec376c8f
VZ
120 if (!CheckPath(newpath))
121 return; // invalid user input
122
af6ad984 123 if (m_pickerIface->GetPath() != newpath)
ec376c8f 124 {
af6ad984 125 m_pickerIface->SetPath(newpath);
ec376c8f
VZ
126
127 // update current working directory, if necessary
128 // NOTE: the path separator is required because if newpath is "C:"
129 // then no change would happen
130 if (IsCwdToUpdate())
58772e49 131 wxSetWorkingDirectory(newpath);
ec376c8f
VZ
132
133 // fire an event
134 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
135 GetEventHandler()->ProcessEvent(event);
136 }
137}
138
139void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
140{
141 if (!m_text)
142 return; // no textctrl to update
143
144 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
145 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
146 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
147 m_bIgnoreNextTextCtrlUpdate = true;
af6ad984 148 m_text->SetValue(m_pickerIface->GetPath());
ec376c8f
VZ
149}
150
151
152
153// ----------------------------------------------------------------------------
154// wxFileDirPickerCtrlBase - event handlers
155// ----------------------------------------------------------------------------
156
157void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev)
158{
159 UpdateTextCtrlFromPicker();
160
161 // the wxFilePickerWidget sent us a colour-change notification.
162 // forward this event to our parent
163 wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath());
164 GetEventHandler()->ProcessEvent(event);
165}
166
167#endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
168
58772e49
VZ
169// ----------------------------------------------------------------------------
170// wxFileDirPickerCtrl
171// ----------------------------------------------------------------------------
172
ec376c8f 173#if wxUSE_FILEPICKERCTRL
58772e49 174
ec376c8f 175IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
58772e49
VZ
176
177bool wxFilePickerCtrl::CheckPath(const wxString& path) const
178{
179 // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we
180 // must accept any path
181 return HasFlag(wxFLP_SAVE) ||
182 !HasFlag(wxFLP_FILE_MUST_EXIST) ||
183 wxFileName::FileExists(path);
184}
185
186wxString wxFilePickerCtrl::GetTextCtrlValue() const
187{
188 // filter it through wxFileName to remove any spurious path separator
189 return wxFileName(m_text->GetValue()).GetFullPath();
190}
191
192#endif // wxUSE_FILEPICKERCTRL
193
194// ----------------------------------------------------------------------------
195// wxDirPickerCtrl
196// ----------------------------------------------------------------------------
197
ec376c8f
VZ
198#if wxUSE_DIRPICKERCTRL
199IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
58772e49
VZ
200
201bool wxDirPickerCtrl::CheckPath(const wxString& path) const
202{
203 // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path
204 return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path);
205}
206
207wxString wxDirPickerCtrl::GetTextCtrlValue() const
208{
209 // filter it through wxFileName to remove any spurious path separator
210 return wxFileName::DirName(m_text->GetValue()).GetPath();
211}
212
213#endif // wxUSE_DIRPICKERCTRL