1 |
william |
31 |
/////////////////////////////////////////////////////////////////////////////// |
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: filepickercmn.cpp 42219 2006-10-21 19:53:05Z PC $ |
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 wxChar wxFilePickerCtrlNameStr[] = wxT("filepicker"); |
41 |
|
|
const wxChar wxFilePickerWidgetNameStr[] = wxT("filepickerwidget"); |
42 |
|
|
const wxChar wxDirPickerCtrlNameStr[] = wxT("dirpicker"); |
43 |
|
|
const wxChar wxDirPickerWidgetNameStr[] = wxT("dirpickerwidget"); |
44 |
|
|
const wxChar wxFilePickerWidgetLabel[] = wxT("Browse"); |
45 |
|
|
const wxChar wxDirPickerWidgetLabel[] = wxT("Browse"); |
46 |
|
|
|
47 |
|
|
DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED) |
48 |
|
|
DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED) |
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 |
|
|
wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path!")); |
67 |
|
|
|
68 |
|
|
if (!wxPickerBase::CreateBase(parent, id, path, pos, size, |
69 |
|
|
style, validator, name)) |
70 |
|
|
return false; |
71 |
|
|
|
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)), |
77 |
|
|
_T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") ); |
78 |
|
|
|
79 |
|
|
wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST), |
80 |
|
|
_T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) ); |
81 |
|
|
|
82 |
|
|
wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT), |
83 |
|
|
_T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") ); |
84 |
|
|
|
85 |
|
|
// create a wxFilePickerWidget or a wxDirPickerWidget... |
86 |
|
|
m_pickerIface = CreatePicker(this, path, message, wildcard); |
87 |
|
|
if ( !m_pickerIface ) |
88 |
|
|
return false; |
89 |
|
|
m_picker = m_pickerIface->AsControl(); |
90 |
|
|
|
91 |
|
|
// complete sizer creation |
92 |
|
|
wxPickerBase::PostCreation(); |
93 |
|
|
|
94 |
|
|
m_picker->Connect(GetEventType(), |
95 |
|
|
wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange), |
96 |
|
|
NULL, this); |
97 |
|
|
|
98 |
|
|
// default's wxPickerBase textctrl limit is too small for this control: |
99 |
|
|
// make it bigger |
100 |
|
|
if (m_text) m_text->SetMaxLength(512); |
101 |
|
|
|
102 |
|
|
return true; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
wxString wxFileDirPickerCtrlBase::GetPath() const |
106 |
|
|
{ |
107 |
|
|
return m_pickerIface->GetPath(); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
void wxFileDirPickerCtrlBase::SetPath(const wxString &path) |
111 |
|
|
{ |
112 |
|
|
m_pickerIface->SetPath(path); |
113 |
|
|
UpdateTextCtrlFromPicker(); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl() |
117 |
|
|
{ |
118 |
|
|
wxASSERT(m_text); |
119 |
|
|
|
120 |
|
|
if (m_bIgnoreNextTextCtrlUpdate) |
121 |
|
|
{ |
122 |
|
|
// ignore this update |
123 |
|
|
m_bIgnoreNextTextCtrlUpdate = false; |
124 |
|
|
return; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
// remove the eventually present path-separator from the end of the textctrl |
128 |
|
|
// string otherwise we would generate a wxFileDirPickerEvent when changing |
129 |
|
|
// from e.g. /home/user to /home/user/ and we want to avoid it ! |
130 |
|
|
wxString newpath(GetTextCtrlValue()); |
131 |
|
|
if (!CheckPath(newpath)) |
132 |
|
|
return; // invalid user input |
133 |
|
|
|
134 |
|
|
if (m_pickerIface->GetPath() != newpath) |
135 |
|
|
{ |
136 |
|
|
m_pickerIface->SetPath(newpath); |
137 |
|
|
|
138 |
|
|
// update current working directory, if necessary |
139 |
|
|
// NOTE: the path separator is required because if newpath is "C:" |
140 |
|
|
// then no change would happen |
141 |
|
|
if (IsCwdToUpdate()) |
142 |
|
|
wxSetWorkingDirectory(newpath); |
143 |
|
|
|
144 |
|
|
// fire an event |
145 |
|
|
wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath); |
146 |
|
|
GetEventHandler()->ProcessEvent(event); |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker() |
151 |
|
|
{ |
152 |
|
|
if (!m_text) |
153 |
|
|
return; // no textctrl to update |
154 |
|
|
|
155 |
|
|
// NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED |
156 |
|
|
// which will trigger a unneeded UpdateFromTextCtrl(); thus before using |
157 |
|
|
// SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag... |
158 |
|
|
m_bIgnoreNextTextCtrlUpdate = true; |
159 |
|
|
m_text->SetValue(m_pickerIface->GetPath()); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
// ---------------------------------------------------------------------------- |
165 |
|
|
// wxFileDirPickerCtrlBase - event handlers |
166 |
|
|
// ---------------------------------------------------------------------------- |
167 |
|
|
|
168 |
|
|
void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev) |
169 |
|
|
{ |
170 |
|
|
UpdateTextCtrlFromPicker(); |
171 |
|
|
|
172 |
|
|
// the wxFilePickerWidget sent us a colour-change notification. |
173 |
|
|
// forward this event to our parent |
174 |
|
|
wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath()); |
175 |
|
|
GetEventHandler()->ProcessEvent(event); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
#endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL |
179 |
|
|
|
180 |
|
|
// ---------------------------------------------------------------------------- |
181 |
|
|
// wxFileDirPickerCtrl |
182 |
|
|
// ---------------------------------------------------------------------------- |
183 |
|
|
|
184 |
|
|
#if wxUSE_FILEPICKERCTRL |
185 |
|
|
|
186 |
|
|
IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase) |
187 |
|
|
|
188 |
|
|
bool wxFilePickerCtrl::CheckPath(const wxString& path) const |
189 |
|
|
{ |
190 |
|
|
// if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we |
191 |
|
|
// must accept any path |
192 |
|
|
return HasFlag(wxFLP_SAVE) || |
193 |
|
|
!HasFlag(wxFLP_FILE_MUST_EXIST) || |
194 |
|
|
wxFileName::FileExists(path); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
wxString wxFilePickerCtrl::GetTextCtrlValue() const |
198 |
|
|
{ |
199 |
|
|
// filter it through wxFileName to remove any spurious path separator |
200 |
|
|
return wxFileName(m_text->GetValue()).GetFullPath(); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
#endif // wxUSE_FILEPICKERCTRL |
204 |
|
|
|
205 |
|
|
// ---------------------------------------------------------------------------- |
206 |
|
|
// wxDirPickerCtrl |
207 |
|
|
// ---------------------------------------------------------------------------- |
208 |
|
|
|
209 |
|
|
#if wxUSE_DIRPICKERCTRL |
210 |
|
|
IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase) |
211 |
|
|
|
212 |
|
|
bool wxDirPickerCtrl::CheckPath(const wxString& path) const |
213 |
|
|
{ |
214 |
|
|
// if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path |
215 |
|
|
return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
wxString wxDirPickerCtrl::GetTextCtrlValue() const |
219 |
|
|
{ |
220 |
|
|
// filter it through wxFileName to remove any spurious path separator |
221 |
|
|
return wxFileName::DirName(m_text->GetValue()).GetPath(); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
#endif // wxUSE_DIRPICKERCTRL |