]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/filectrl.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / samples / widgets / filectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filectrl.cpp
4 // Purpose: Part of the widgets sample showing wxFileCtrl
5 // Author: Diaa M. Sami
6 // Created: 28 Jul 2007
7 // Copyright: (c) 2007 Diaa M. Sami
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_FILECTRL
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/filectrl.h"
34
35 #include "wx/wupdlock.h"
36 #include "wx/filename.h"
37
38 #include "widgets.h"
39
40 // TODO change this
41 #include "icons/dirctrl.xpm"
42
43 // ----------------------------------------------------------------------------
44 // constants
45 // ----------------------------------------------------------------------------
46
47 // control ids
48 enum
49 {
50 FileCtrlPage_Reset = wxID_HIGHEST,
51 FileCtrlPage_SetDirectory,
52 FileCtrlPage_SetPath,
53 FileCtrlPage_SetFilename,
54 FileCtrlPage_Ctrl
55 };
56
57 enum
58 {
59 FileCtrlMode_Open = 0,
60 FileCtrlMode_Save
61 };
62
63 // ----------------------------------------------------------------------------
64 // CheckBoxWidgetsPage
65 // ----------------------------------------------------------------------------
66
67 class FileCtrlWidgetsPage : public WidgetsPage
68 {
69 public:
70 FileCtrlWidgetsPage( WidgetsBookCtrl *book, wxImageList *imaglist );
71 virtual ~FileCtrlWidgetsPage() {}
72
73 virtual wxControl *GetWidget() const { return m_fileCtrl; }
74 virtual void RecreateWidget() { CreateFileCtrl(); }
75
76 // lazy creation of the content
77 virtual void CreateContent();
78
79 protected:
80 // event handlers
81 void OnButtonSetDirectory( wxCommandEvent& event );
82 void OnButtonSetPath( wxCommandEvent& event );
83 void OnButtonSetFilename( wxCommandEvent& event );
84 void OnButtonReset( wxCommandEvent& event );
85 void OnCheckBox( wxCommandEvent& event );
86 void OnRadioBox( wxCommandEvent& event );
87 void OnFileCtrl( wxFileCtrlEvent& event );
88
89 // reset the control parameters
90 void Reset();
91
92 // (re)create the m_fileCtrl
93 void CreateFileCtrl();
94
95 // the controls
96 // ------------
97
98 // the control itself and the sizer it is in
99 wxFileCtrl *m_fileCtrl;
100
101 // the text entries for command parameters
102 wxTextCtrl *m_dir;
103 wxTextCtrl *m_path;
104 wxTextCtrl *m_filename;
105
106 // flags
107 wxCheckBox *m_chkMultiple,
108 *m_chkNoShowHidden;
109
110 wxRadioBox *m_radioFileCtrlMode;
111
112 // filters
113 wxCheckBox *m_fltr[3];
114
115 private:
116 DECLARE_EVENT_TABLE()
117 DECLARE_WIDGETS_PAGE( FileCtrlWidgetsPage )
118 };
119
120 // ----------------------------------------------------------------------------
121 // event tables
122 // ----------------------------------------------------------------------------
123
124 BEGIN_EVENT_TABLE( FileCtrlWidgetsPage, WidgetsPage )
125 EVT_BUTTON( FileCtrlPage_Reset, FileCtrlWidgetsPage::OnButtonReset )
126 EVT_BUTTON( FileCtrlPage_SetDirectory, FileCtrlWidgetsPage::OnButtonSetDirectory )
127 EVT_BUTTON( FileCtrlPage_SetPath, FileCtrlWidgetsPage::OnButtonSetPath )
128 EVT_BUTTON( FileCtrlPage_SetFilename, FileCtrlWidgetsPage::OnButtonSetFilename )
129 EVT_CHECKBOX( wxID_ANY, FileCtrlWidgetsPage::OnCheckBox )
130 EVT_RADIOBOX( wxID_ANY, FileCtrlWidgetsPage::OnRadioBox )
131
132 EVT_FILECTRL_FILTERCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
133 EVT_FILECTRL_FOLDERCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
134 EVT_FILECTRL_SELECTIONCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
135 EVT_FILECTRL_FILEACTIVATED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
136 END_EVENT_TABLE()
137
138 // ============================================================================
139 // implementation
140 // ============================================================================
141
142 #if defined(__WXGTK__)
143 #define FAMILY_CTRLS NATIVE_CTRLS
144 #else
145 #define FAMILY_CTRLS GENERIC_CTRLS
146 #endif
147
148 IMPLEMENT_WIDGETS_PAGE( FileCtrlWidgetsPage, wxT( "FileCtrl" ),
149 FAMILY_CTRLS );
150
151 FileCtrlWidgetsPage::FileCtrlWidgetsPage( WidgetsBookCtrl *book,
152 wxImageList *imaglist )
153 : WidgetsPage( book, imaglist, dirctrl_xpm )
154 {
155 }
156
157 void FileCtrlWidgetsPage::CreateContent()
158 {
159 wxSizer *sizerTop = new wxBoxSizer( wxHORIZONTAL );
160
161 // left pane
162 wxSizer *sizerLeft = new wxBoxSizer( wxVERTICAL );
163
164 static const wxString mode[] = { wxT( "open" ), wxT( "save" ) };
165 m_radioFileCtrlMode = new wxRadioBox( this, wxID_ANY, wxT( "wxFileCtrl mode" ),
166 wxDefaultPosition, wxDefaultSize,
167 WXSIZEOF( mode ), mode );
168
169 sizerLeft->Add( m_radioFileCtrlMode,
170 0, wxALL | wxEXPAND , 5 );
171
172 sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetDirectory , wxT( "Set &directory" ), wxID_ANY, &m_dir ),
173 0, wxALL | wxEXPAND , 5 );
174 sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetPath , wxT( "Set &path" ), wxID_ANY, &m_path ),
175 0, wxALL | wxEXPAND , 5 );
176 sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetFilename , wxT( "Set &filename" ), wxID_ANY, &m_filename ),
177 0, wxALL | wxEXPAND , 5 );
178
179 wxSizer *sizerUseFlags =
180 new wxStaticBoxSizer( wxVERTICAL, this, wxT( "&Flags" ) );
181
182 m_chkMultiple = CreateCheckBoxAndAddToSizer( sizerUseFlags, wxT( "wxFC_MULTIPLE" ) );
183 m_chkNoShowHidden = CreateCheckBoxAndAddToSizer( sizerUseFlags, wxT( "wxFC_NOSHOWHIDDEN" ) );
184 sizerLeft->Add( sizerUseFlags, wxSizerFlags().Expand().Border() );
185
186 wxSizer *sizerFilters =
187 new wxStaticBoxSizer( wxVERTICAL, this, wxT( "&Filters" ) );
188 m_fltr[0] = CreateCheckBoxAndAddToSizer( sizerFilters, wxString::Format( wxT( "all files (%s)|%s" ),
189 wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr ) );
190 m_fltr[1] = CreateCheckBoxAndAddToSizer( sizerFilters, wxT( "C++ files (*.cpp; *.h)|*.cpp;*.h" ) );
191 m_fltr[2] = CreateCheckBoxAndAddToSizer( sizerFilters, wxT( "PNG images (*.png)|*.png" ) );
192 sizerLeft->Add( sizerFilters, wxSizerFlags().Expand().Border() );
193
194 wxButton *btn = new wxButton( this, FileCtrlPage_Reset, wxT( "&Reset" ) );
195 sizerLeft->Add( btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15 );
196
197 // right pane
198 m_fileCtrl = new wxFileCtrl(
199 this,
200 FileCtrlPage_Ctrl,
201 wxEmptyString,
202 wxEmptyString,
203 wxEmptyString,
204 wxFC_OPEN,
205 wxDefaultPosition,
206 wxDefaultSize
207 );
208
209 // the 3 panes panes compose the window
210 sizerTop->Add( sizerLeft, 0, ( wxALL & ~wxLEFT ), 10 );
211 sizerTop->Add( m_fileCtrl, 1, wxGROW | ( wxALL & ~wxRIGHT ), 10 );
212
213 // final initializations
214 Reset();
215
216 SetSizer( sizerTop );
217 }
218
219 void FileCtrlWidgetsPage::Reset()
220 {
221 m_dir->SetValue( m_fileCtrl->GetDirectory() );
222 m_radioFileCtrlMode->SetSelection( ( wxFC_DEFAULT_STYLE & wxFC_OPEN ) ?
223 FileCtrlMode_Open : FileCtrlMode_Save );
224 }
225
226 void FileCtrlWidgetsPage::CreateFileCtrl()
227 {
228 wxWindowUpdateLocker noUpdates( this );
229
230 const int style =
231 ( m_radioFileCtrlMode->GetSelection() == FileCtrlMode_Open ?
232 wxFC_OPEN : wxFC_SAVE ) |
233 ( m_chkMultiple->IsChecked() ? wxFC_MULTIPLE : 0 ) |
234 ( m_chkNoShowHidden->IsChecked() ? wxFC_NOSHOWHIDDEN : 0 );
235
236 wxFileCtrl *fileCtrl = new wxFileCtrl(
237 this,
238 FileCtrlPage_Ctrl,
239 wxEmptyString,
240 wxEmptyString,
241 wxEmptyString,
242 style,
243 wxDefaultPosition,
244 wxDefaultSize
245 );
246
247 wxString wildcard;
248 for ( unsigned int i = 0; i < WXSIZEOF( m_fltr ); ++i )
249 {
250 if ( m_fltr[i]->IsChecked() )
251 {
252 if ( !wildcard.IsEmpty() )
253 wildcard += wxT( "|" );
254 wildcard += m_fltr[i]->GetLabel();
255 }
256 }
257 fileCtrl->SetWildcard( wildcard );
258
259 // update sizer's child window
260 GetSizer()->Replace( m_fileCtrl, fileCtrl, true );
261
262 // update our pointer
263 delete m_fileCtrl;
264 m_fileCtrl = fileCtrl;
265
266 // relayout the sizer
267 GetSizer()->Layout();
268 }
269
270 // ----------------------------------------------------------------------------
271 // event handlers
272 // ----------------------------------------------------------------------------
273
274 void FileCtrlWidgetsPage::OnButtonSetDirectory( wxCommandEvent& WXUNUSED( event ) )
275 {
276 m_fileCtrl->SetDirectory( m_dir->GetValue() );
277 }
278
279 void FileCtrlWidgetsPage::OnButtonSetPath( wxCommandEvent& WXUNUSED( event ) )
280 {
281 m_fileCtrl->SetPath( m_path->GetValue() );
282 }
283
284 void FileCtrlWidgetsPage::OnButtonSetFilename( wxCommandEvent& WXUNUSED( event ) )
285 {
286 m_fileCtrl->SetFilename( m_filename->GetValue() );
287 }
288
289 void FileCtrlWidgetsPage::OnButtonReset( wxCommandEvent& WXUNUSED( event ) )
290 {
291 Reset();
292
293 CreateFileCtrl();
294 }
295
296 void FileCtrlWidgetsPage::OnCheckBox( wxCommandEvent& WXUNUSED( event ) )
297 {
298 CreateFileCtrl();
299 }
300
301 void FileCtrlWidgetsPage::OnRadioBox( wxCommandEvent& WXUNUSED( event ) )
302 {
303 CreateFileCtrl();
304 }
305
306 void FileCtrlWidgetsPage::OnFileCtrl( wxFileCtrlEvent& event )
307 {
308 if ( event.GetEventType() == wxEVT_FILECTRL_FOLDERCHANGED )
309 {
310 wxLogMessage("Folder changed event, new folder: %s", event.GetDirectory());
311 }
312 else if ( event.GetEventType() == wxEVT_FILECTRL_FILEACTIVATED )
313 {
314 wxLogMessage("File activated event: %s", wxJoin(event.GetFiles(), ' '));
315 }
316 else if ( event.GetEventType() == wxEVT_FILECTRL_SELECTIONCHANGED )
317 {
318 wxLogMessage("Selection changed event: %s", wxJoin(event.GetFiles(), ' '));
319 }
320 else if ( event.GetEventType() == wxEVT_FILECTRL_FILTERCHANGED )
321 {
322 wxLogMessage("Filter changed event: filter %d selected",
323 event.GetFilterIndex() + 1);
324 }
325 }
326
327 #endif // wxUSE_FILECTRL