]> git.saurik.com Git - wxWidgets.git/blob - src/common/filepickercmn.cpp
only use Mac-specific menu item under Mac
[wxWidgets.git] / src / common / filepickercmn.cpp
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
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 char wxFilePickerCtrlNameStr[] = "filepicker";
41 const char wxFilePickerWidgetNameStr[] = "filepickerwidget";
42 const char wxDirPickerCtrlNameStr[] = "dirpicker";
43 const char wxDirPickerWidgetNameStr[] = "dirpickerwidget";
44 const char wxFilePickerWidgetLabel[] = wxTRANSLATE("Browse");
45 const char wxDirPickerWidgetLabel[] = wxTRANSLATE("Browse");
46
47 wxDEFINE_EVENT( wxEVT_COMMAND_FILEPICKER_CHANGED, wxFileDirPickerEvent );
48 wxDEFINE_EVENT( wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEvent );
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 DoConnect( m_picker, this );
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
103 wxString wxFileDirPickerCtrlBase::GetPath() const
104 {
105 return m_pickerIface->GetPath();
106 }
107
108 void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
109 {
110 m_pickerIface->SetPath(path);
111 UpdateTextCtrlFromPicker();
112 }
113
114 void 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 !
128 wxString newpath(GetTextCtrlValue());
129 if (!CheckPath(newpath))
130 return; // invalid user input
131
132 if (m_pickerIface->GetPath() != newpath)
133 {
134 m_pickerIface->SetPath(newpath);
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())
140 wxSetWorkingDirectory(newpath);
141
142 // fire an event
143 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
144 GetEventHandler()->ProcessEvent(event);
145 }
146 }
147
148 void 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;
157 m_text->SetValue(m_pickerIface->GetPath());
158 }
159
160
161
162 // ----------------------------------------------------------------------------
163 // wxFileDirPickerCtrlBase - event handlers
164 // ----------------------------------------------------------------------------
165
166 void 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
178 // ----------------------------------------------------------------------------
179 // wxFileDirPickerCtrl
180 // ----------------------------------------------------------------------------
181
182 #if wxUSE_FILEPICKERCTRL
183
184 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
185
186 bool 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
195 wxString 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
207 #if wxUSE_DIRPICKERCTRL
208 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
209
210 bool 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
216 wxString 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