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