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