build fix
[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
31 #ifndef WX_PRECOMP
32 #include "wx/textctrl.h"
33 #endif
34
35 // ============================================================================
36 // implementation
37 // ============================================================================
38
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED)
41 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
42
43 // ----------------------------------------------------------------------------
44 // wxFileDirPickerCtrlBase
45 // ----------------------------------------------------------------------------
46
47 #define M_PICKER ((wxFilePickerWidget*)m_picker)
48
49 bool wxFileDirPickerCtrlBase::CreateBase( wxWindow *parent, wxWindowID id,
50 const wxString &path, const wxString &message,
51 const wxString &wildcard,
52 const wxPoint &pos, const wxSize &size,
53 long style, const wxValidator& validator,
54 const wxString &name )
55 {
56 wxASSERT_MSG(path.empty() || CheckPath(path), wxT("Invalid initial path !"));
57
58 if (!wxPickerBase::CreateBase(parent, id, path, pos, size,
59 style, validator, name))
60 return false;
61
62 if (!HasFlag(wxFLP_OPEN) && !HasFlag(wxFLP_SAVE))
63 m_windowStyle |= wxFLP_OPEN; // wxFD_OPEN is the default
64
65 // check that the styles are not contradictory
66 wxASSERT_MSG( !(HasFlag(wxFLP_SAVE) && HasFlag(wxFLP_OPEN)),
67 _T("can't specify both wxFLP_SAVE and wxFLP_OPEN at once") );
68
69 wxASSERT_MSG( !HasFlag(wxFLP_SAVE) || !HasFlag(wxFLP_FILE_MUST_EXIST),
70 _T("wxFLP_FILE_MUST_EXIST can't be used with wxFLP_SAVE" ) );
71
72 wxASSERT_MSG( !HasFlag(wxFLP_OPEN) || !HasFlag(wxFLP_OVERWRITE_PROMPT),
73 _T("wxFLP_OVERWRITE_PROMPT can't be used with wxFLP_OPEN") );
74
75 // create a wxFilePickerWidget or a wxDirPickerWidget...
76 if (!CreatePicker(this, path, message, wildcard))
77 return false;
78 m_picker->Connect(GetEventType(),
79 wxFileDirPickerEventHandler(wxFileDirPickerCtrlBase::OnFileDirChange),
80 NULL, this);
81
82 // default's wxPickerBase textctrl limit is too small for this control:
83 // make it bigger
84 if (m_text) m_text->SetMaxLength(512);
85
86 return true;
87 }
88
89 void wxFileDirPickerCtrlBase::SetPath(const wxString &path)
90 {
91 M_PICKER->SetPath(path);
92 UpdateTextCtrlFromPicker();
93 }
94
95 void wxFileDirPickerCtrlBase::UpdatePickerFromTextCtrl()
96 {
97 wxASSERT(m_text);
98
99 if (m_bIgnoreNextTextCtrlUpdate)
100 {
101 // ignore this update
102 m_bIgnoreNextTextCtrlUpdate = false;
103 return;
104 }
105
106 // remove the eventually present path-separator from the end of the textctrl
107 // string otherwise we would generate a wxFileDirPickerEvent when changing
108 // from e.g. /home/user to /home/user/ and we want to avoid it !
109 wxString newpath(m_text->GetValue());
110 if (!newpath.empty() && wxFileName::IsPathSeparator(newpath.Last()))
111 newpath.RemoveLast();
112 if (!CheckPath(newpath))
113 return; // invalid user input
114
115 if (M_PICKER->GetPath() != newpath)
116 {
117 M_PICKER->SetPath(newpath);
118
119 // update current working directory, if necessary
120 // NOTE: the path separator is required because if newpath is "C:"
121 // then no change would happen
122 if (IsCwdToUpdate())
123 wxSetWorkingDirectory(newpath + wxFileName::GetPathSeparator());
124
125 // fire an event
126 wxFileDirPickerEvent event(GetEventType(), this, GetId(), newpath);
127 GetEventHandler()->ProcessEvent(event);
128 }
129 }
130
131 void wxFileDirPickerCtrlBase::UpdateTextCtrlFromPicker()
132 {
133 if (!m_text)
134 return; // no textctrl to update
135
136 // NOTE: this SetValue() will generate an unwanted wxEVT_COMMAND_TEXT_UPDATED
137 // which will trigger a unneeded UpdateFromTextCtrl(); thus before using
138 // SetValue() we set the m_bIgnoreNextTextCtrlUpdate flag...
139 m_bIgnoreNextTextCtrlUpdate = true;
140 m_text->SetValue(M_PICKER->GetPath());
141 }
142
143
144
145 // ----------------------------------------------------------------------------
146 // wxFileDirPickerCtrlBase - event handlers
147 // ----------------------------------------------------------------------------
148
149 void wxFileDirPickerCtrlBase::OnFileDirChange(wxFileDirPickerEvent &ev)
150 {
151 UpdateTextCtrlFromPicker();
152
153 // the wxFilePickerWidget sent us a colour-change notification.
154 // forward this event to our parent
155 wxFileDirPickerEvent event(GetEventType(), this, GetId(), ev.GetPath());
156 GetEventHandler()->ProcessEvent(event);
157 }
158
159 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
160
161 #if wxUSE_FILEPICKERCTRL
162 IMPLEMENT_DYNAMIC_CLASS(wxFilePickerCtrl, wxPickerBase)
163 #endif
164 #if wxUSE_DIRPICKERCTRL
165 IMPLEMENT_DYNAMIC_CLASS(wxDirPickerCtrl, wxPickerBase)
166 #endif