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