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