]> git.saurik.com Git - wxWidgets.git/blame - src/common/filepickercmn.cpp
adding ContentScaleFactor support to dc - defaulting to 1.0
[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
f36e602b
VZ
40const char wxFilePickerCtrlNameStr[] = "filepicker";
41const char wxFilePickerWidgetNameStr[] = "filepickerwidget";
42const char wxDirPickerCtrlNameStr[] = "dirpicker";
43const char wxDirPickerWidgetNameStr[] = "dirpickerwidget";
174e8f45
VZ
44const char wxFilePickerWidgetLabel[] = wxTRANSLATE("Browse");
45const char wxDirPickerWidgetLabel[] = wxTRANSLATE("Browse");
43af39c8 46
ce7fe42e
VZ
47wxDEFINE_EVENT( wxEVT_FILEPICKER_CHANGED, wxFileDirPickerEvent );
48wxDEFINE_EVENT( wxEVT_DIRPICKER_CHANGED, wxFileDirPickerEvent );
ec376c8f
VZ
49IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
50
51// ----------------------------------------------------------------------------
52// wxFileDirPickerCtrlBase
53// ----------------------------------------------------------------------------
54
5f6475c1
VZ
55bool 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 )
ec376c8f 65{
ec376c8f 66 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
55b43eaa 67 style, validator, name))
ec376c8f
VZ
68 return false;
69
556151f5
MW
70 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
71 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
72
73 // check that the styles are not contradictory
74 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
9a83f860 75 wxT("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
556151f5
MW
76
77 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
9a83f860 78 wxT("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
556151f5
MW
79
80 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
9a83f860 81 wxT("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
556151f5 82
ec376c8f 83 // create a wxFilePickerWidget or a wxDirPickerWidget...
af6ad984
VS
84 m_pickerIface = CreatePicker(this, path, message, wildcard);
85 if ( !m_pickerIface )
ec376c8f 86 return false;
af6ad984 87 m_picker = m_pickerIface->AsControl();
a65ffcb2
VZ
88
89 // complete sizer creation
90 wxPickerBase::PostCreation();
91
3c778901 92 DoConnect( m_picker, this );
ec376c8f
VZ
93
94 // default's wxPickerBase textctrl limit is too small for this control:
95 // make it bigger
96 if (m_text) m_text->SetMaxLength(512);
97
98 return true;
99}
100
af6ad984
VS
101wxString wxFileDirPickerCtrlBase::GetPath() const
102{
103 return m_pickerIface->GetPath();
104}
105
ec376c8f
VZ
106void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
107{
af6ad984 108 m_pickerIface->SetPath(path);
ec376c8f
VZ
109 UpdateTextCtrlFromPicker();
110}
111
112void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
113{
114 wxASSERT(m_text);
115
ec376c8f
VZ
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());
3ccea097
VZ
120
121 // Notice that we use to check here whether the current path is valid, i.e.
122 // if the corresponding file or directory exists for the controls with
123 // wxFLP_FILE_MUST_EXIST or wxDIRP_DIR_MUST_EXIST flag, however we don't do
124 // this any more as we still must notify the program about any changes in
125 // the control, otherwise its view of it would be different from what is
126 // actually shown on the screen, resulting in very confusing UI.
ec376c8f 127
af6ad984 128 if (m_pickerIface->GetPath() != newpath)
ec376c8f 129 {
af6ad984 130 m_pickerIface->SetPath(newpath);
ec376c8f
VZ
131
132 // update current working directory, if necessary
133 // NOTE: the path separator is required because if newpath is "C:"
134 // then no change would happen
135 if (IsCwdToUpdate())
58772e49 136 wxSetWorkingDirectory(newpath);
ec376c8f
VZ
137
138 // fire an event
139 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
140 GetEventHandler()->ProcessEvent(event);
141 }
142}
143
144void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
145{
146 if (!m_text)
147 return; // no textctrl to update
148
44b72116
VZ
149 // Take care to use ChangeValue() here and not SetValue() to avoid
150 // generating an event that would trigger UpdateTextCtrlFromPicker()
151 // resulting in infinite recursion.
152 m_text->ChangeValue(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 180
ea7ff9ad
VZ
181bool wxFilePickerCtrl::Create(wxWindow *parent,
182 wxWindowID id,
183 const wxString& path,
184 const wxString& message,
185 const wxString& wildcard,
186 const wxPoint& pos,
187 const wxSize& size,
188 long style,
189 const wxValidator& validator,
190 const wxString& name)
191{
192 if ( !wxFileDirPickerCtrlBase::CreateBase
193 (
194 parent, id, path, message, wildcard,
195 pos, size, style, validator, name
196 ) )
197 return false;
198
199 if ( HasTextCtrl() )
200 GetTextCtrl()->AutoCompleteFileNames();
201
202 return true;
203}
204
58772e49
VZ
205wxString wxFilePickerCtrl::GetTextCtrlValue() const
206{
207 // filter it through wxFileName to remove any spurious path separator
208 return wxFileName(m_text->GetValue()).GetFullPath();
209}
210
211#endif // wxUSE_FILEPICKERCTRL
212
213// ----------------------------------------------------------------------------
214// wxDirPickerCtrl
215// ----------------------------------------------------------------------------
216
ec376c8f
VZ
217#if wxUSE_DIRPICKERCTRL
218IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
58772e49 219
ea7ff9ad
VZ
220bool wxDirPickerCtrl::Create(wxWindow *parent,
221 wxWindowID id,
222 const wxString& path,
223 const wxString& message,
224 const wxPoint& pos,
225 const wxSize& size,
226 long style,
227 const wxValidator& validator,
228 const wxString& name)
229{
230 if ( !wxFileDirPickerCtrlBase::CreateBase
231 (
232 parent, id, path, message, wxString(),
233 pos, size, style, validator, name
234 ) )
235 return false;
236
237 if ( HasTextCtrl() )
238 GetTextCtrl()->AutoCompleteDirectories();
239
240 return true;
241}
242
58772e49
VZ
243wxString wxDirPickerCtrl::GetTextCtrlValue() const
244{
245 // filter it through wxFileName to remove any spurious path separator
246 return wxFileName::DirName(m_text->GetValue()).GetPath();
247}
248
249#endif // wxUSE_DIRPICKERCTRL