]>
Commit | Line | Data |
---|---|---|
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 |
40 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED) |
41 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED) | |
42 | IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent) | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // wxFileDirPickerCtrlBase | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | #define M_PICKER ((wxFilePickerWidget*)m_picker) | |
49 | ||
50 | bool wxFileDirPickerCtrlBase::CreateBase( wxWindow *parent, wxWindowID id, | |
51 | const wxString &path, const wxString &message, | |
52 | const wxString &wildcard, | |
53 | const wxPoint &pos, const wxSize &size, | |
54 | long style, const wxValidator& validator, | |
55 | const wxString &name ) | |
56 | { | |
4ce7b1e4 | 57 | wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path !")); |
ec376c8f VZ |
58 | |
59 | if (!wxPickerBase::CreateBase(parent, id, path, pos, size, | |
60 | style, validator, name)) | |
61 | return false; | |
62 | ||
556151f5 MW |
63 | if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE)) |
64 | m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default | |
65 | ||
66 | // check that the styles are not contradictory | |
67 | wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)), | |
68 | _T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") ); | |
69 | ||
70 | wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST), | |
71 | _T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) ); | |
72 | ||
73 | wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT), | |
74 | _T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") ); | |
75 | ||
ec376c8f VZ |
76 | // create a wxFilePickerWidget or a wxDirPickerWidget... |
77 | if (!CreatePicker(this, path, message, wildcard)) | |
78 | return false; | |
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 | ||
94 | void wxFileDirPickerCtrlBase::SetPath(const wxString &path) | |
95 | { | |
96 | M_PICKER->SetPath(path); | |
97 | UpdateTextCtrlFromPicker(); | |
98 | } | |
99 | ||
100 | void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl() | |
101 | { | |
102 | wxASSERT(m_text); | |
103 | ||
104 | if (m_bIgnoreNextTextCtrlUpdate) | |
105 | { | |
106 | // ignore this update | |
107 | m_bIgnoreNextTextCtrlUpdate = false; | |
108 | return; | |
109 | } | |
110 | ||
111 | // remove the eventually present path-separator from the end of the textctrl | |
112 | // string otherwise we would generate a wxFileDirPickerEvent when changing | |
113 | // from e.g. /home/user to /home/user/ and we want to avoid it ! | |
58772e49 | 114 | wxString newpath(GetTextCtrlValue()); |
ec376c8f VZ |
115 | if (!CheckPath(newpath)) |
116 | return; // invalid user input | |
117 | ||
118 | if (M_PICKER->GetPath() != newpath) | |
119 | { | |
120 | M_PICKER->SetPath(newpath); | |
121 | ||
122 | // update current working directory, if necessary | |
123 | // NOTE: the path separator is required because if newpath is "C:" | |
124 | // then no change would happen | |
125 | if (IsCwdToUpdate()) | |
58772e49 | 126 | wxSetWorkingDirectory(newpath); |
ec376c8f VZ |
127 | |
128 | // fire an event | |
129 | wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath); | |
130 | GetEventHandler()->ProcessEvent(event); | |
131 | } | |
132 | } | |
133 | ||
134 | void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker() | |
135 | { | |
136 | if (!m_text) | |
137 | return; // no textctrl to update | |
138 | ||
139 | // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED | |
140 | // which will trigger a unneeded UpdateFromTextCtrl(); thus before using | |
141 | // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag... | |
142 | m_bIgnoreNextTextCtrlUpdate = true; | |
143 | m_text->SetValue(M_PICKER->GetPath()); | |
144 | } | |
145 | ||
146 | ||
147 | ||
148 | // ---------------------------------------------------------------------------- | |
149 | // wxFileDirPickerCtrlBase - event handlers | |
150 | // ---------------------------------------------------------------------------- | |
151 | ||
152 | void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev) | |
153 | { | |
154 | UpdateTextCtrlFromPicker(); | |
155 | ||
156 | // the wxFilePickerWidget sent us a colour-change notification. | |
157 | // forward this event to our parent | |
158 | wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath()); | |
159 | GetEventHandler()->ProcessEvent(event); | |
160 | } | |
161 | ||
162 | #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL | |
163 | ||
58772e49 VZ |
164 | // ---------------------------------------------------------------------------- |
165 | // wxFileDirPickerCtrl | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
ec376c8f | 168 | #if wxUSE_FILEPICKERCTRL |
58772e49 | 169 | |
ec376c8f | 170 | IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase) |
58772e49 VZ |
171 | |
172 | bool wxFilePickerCtrl::CheckPath(const wxString& path) const | |
173 | { | |
174 | // if wxFLP_SAVE was given or wxFLP_FILE_MUST_EXIST has NOT been given we | |
175 | // must accept any path | |
176 | return HasFlag(wxFLP_SAVE) || | |
177 | !HasFlag(wxFLP_FILE_MUST_EXIST) || | |
178 | wxFileName::FileExists(path); | |
179 | } | |
180 | ||
181 | wxString wxFilePickerCtrl::GetTextCtrlValue() const | |
182 | { | |
183 | // filter it through wxFileName to remove any spurious path separator | |
184 | return wxFileName(m_text->GetValue()).GetFullPath(); | |
185 | } | |
186 | ||
187 | #endif // wxUSE_FILEPICKERCTRL | |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // wxDirPickerCtrl | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
ec376c8f VZ |
193 | #if wxUSE_DIRPICKERCTRL |
194 | IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase) | |
58772e49 VZ |
195 | |
196 | bool wxDirPickerCtrl::CheckPath(const wxString& path) const | |
197 | { | |
198 | // if wxDIRP_DIR_MUST_EXIST has NOT been given we must accept any path | |
199 | return !HasFlag(wxDIRP_DIR_MUST_EXIST) || wxFileName::DirExists(path); | |
200 | } | |
201 | ||
202 | wxString wxDirPickerCtrl::GetTextCtrlValue() const | |
203 | { | |
204 | // filter it through wxFileName to remove any spurious path separator | |
205 | return wxFileName::DirName(m_text->GetValue()).GetPath(); | |
206 | } | |
207 | ||
208 | #endif // wxUSE_DIRPICKERCTRL |