1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxFileCtrl
5 // Author: Diaa M. Sami
6 // Created: 28 Jul 2007
8 // Copyright: (c) 2007 Diaa M. Sami
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
34 #include "wx/filectrl.h"
36 #include "wx/wupdlock.h"
37 #include "wx/filename.h"
42 #include "icons/dirctrl.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
51 FileCtrlPage_Reset
= wxID_HIGHEST
,
52 FileCtrlPage_SetDirectory
,
54 FileCtrlPage_SetFilename
,
60 FileCtrlMode_Open
= 0,
64 // ----------------------------------------------------------------------------
65 // CheckBoxWidgetsPage
66 // ----------------------------------------------------------------------------
68 class FileCtrlWidgetsPage
: public WidgetsPage
71 FileCtrlWidgetsPage( WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
72 virtual ~FileCtrlWidgetsPage() {}
74 virtual wxControl
*GetWidget() const { return /*m_fileCtrl*/NULL
; }
75 virtual void RecreateWidget() { CreateFileCtrl(); }
77 // lazy creation of the content
78 virtual void CreateContent();
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
);
90 // reset the control parameters
93 // (re)create the m_fileCtrl
94 void CreateFileCtrl();
99 // the control itself and the sizer it is in
100 wxFileCtrl
*m_fileCtrl
;
102 // the text entries for command parameters
105 wxTextCtrl
*m_filename
;
108 wxCheckBox
*m_chkMultiple
,
111 wxRadioBox
*m_radioFileCtrlMode
;
114 wxCheckBox
*m_fltr
[3];
117 DECLARE_EVENT_TABLE()
118 DECLARE_WIDGETS_PAGE( FileCtrlWidgetsPage
)
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
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
)
133 EVT_FILECTRL_FOLDERCHANGED( wxID_ANY
, FileCtrlWidgetsPage::OnFileCtrl
)
134 EVT_FILECTRL_SELECTIONCHANGED( wxID_ANY
, FileCtrlWidgetsPage::OnFileCtrl
)
135 EVT_FILECTRL_FILEACTIVATED( wxID_ANY
, FileCtrlWidgetsPage::OnFileCtrl
)
138 // ============================================================================
140 // ============================================================================
142 #if defined(__WXGTK__)
143 #define FAMILY_CTRLS NATIVE_CTRLS
145 #define FAMILY_CTRLS GENERIC_CTRLS
148 IMPLEMENT_WIDGETS_PAGE( FileCtrlWidgetsPage
, wxT( "FileCtrl" ),
151 FileCtrlWidgetsPage::FileCtrlWidgetsPage( WidgetsBookCtrl
*book
,
152 wxImageList
*imaglist
)
153 : WidgetsPage( book
, imaglist
, dirctrl_xpm
)
157 void FileCtrlWidgetsPage::CreateContent()
159 wxSizer
*sizerTop
= new wxBoxSizer( wxHORIZONTAL
);
162 wxSizer
*sizerLeft
= new wxBoxSizer( wxVERTICAL
);
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
);
169 sizerLeft
->Add( m_radioFileCtrlMode
,
170 0, wxALL
| wxEXPAND
, 5 );
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 );
179 wxSizer
*sizerUseFlags
=
180 new wxStaticBoxSizer( wxVERTICAL
, this, _T( "&Flags" ) );
182 m_chkMultiple
= CreateCheckBoxAndAddToSizer( sizerUseFlags
, _T( "wxFC_MULTIPLE" ) );
183 m_chkNoShowHidden
= CreateCheckBoxAndAddToSizer( sizerUseFlags
, _T( "wxFC_NOSHOWHIDDEN" ) );
184 sizerLeft
->Add( sizerUseFlags
, wxSizerFlags().Expand().Border() );
186 wxSizer
*sizerFilters
=
187 new wxStaticBoxSizer( wxVERTICAL
, this, _T( "&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() );
194 wxButton
*btn
= new wxButton( this, FileCtrlPage_Reset
, _T( "&Reset" ) );
195 sizerLeft
->Add( btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15 );
198 m_fileCtrl
= new wxFileCtrl(
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 );
213 // final initializations
216 SetSizer( sizerTop
);
219 void FileCtrlWidgetsPage::Reset()
221 m_dir
->SetValue( m_fileCtrl
->GetDirectory() );
222 m_radioFileCtrlMode
->SetSelection( ( wxFC_DEFAULT_STYLE
& wxFC_OPEN
) ?
223 FileCtrlMode_Open
: FileCtrlMode_Save
);
226 void FileCtrlWidgetsPage::CreateFileCtrl()
228 wxWindowUpdateLocker
noUpdates( this );
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 );
236 wxFileCtrl
*fileCtrl
= new wxFileCtrl(
248 for ( unsigned int i
= 0; i
< WXSIZEOF( m_fltr
); ++i
)
250 if ( m_fltr
[i
]->IsChecked() )
252 if ( !wildcard
.IsEmpty() )
253 wildcard
+= wxT( "|" );
254 wildcard
+= m_fltr
[i
]->GetLabel();
257 fileCtrl
->SetWildcard( wildcard
);
259 // update sizer's child window
260 GetSizer()->Replace( m_fileCtrl
, fileCtrl
, true );
262 // update our pointer
264 m_fileCtrl
= fileCtrl
;
266 // relayout the sizer
267 GetSizer()->Layout();
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 void FileCtrlWidgetsPage::OnButtonSetDirectory( wxCommandEvent
& WXUNUSED( event
) )
276 m_fileCtrl
->SetDirectory( m_dir
->GetValue() );
279 void FileCtrlWidgetsPage::OnButtonSetPath( wxCommandEvent
& WXUNUSED( event
) )
281 m_fileCtrl
->SetPath( m_path
->GetValue() );
284 void FileCtrlWidgetsPage::OnButtonSetFilename( wxCommandEvent
& WXUNUSED( event
) )
286 m_fileCtrl
->SetFilename( m_filename
->GetValue() );
289 void FileCtrlWidgetsPage::OnButtonReset( wxCommandEvent
& WXUNUSED( event
) )
296 void FileCtrlWidgetsPage::OnCheckBox( wxCommandEvent
& WXUNUSED( event
) )
301 void FileCtrlWidgetsPage::OnRadioBox( wxCommandEvent
& WXUNUSED( event
) )
306 void FileCtrlWidgetsPage::OnFileCtrl( wxFileCtrlEvent
& event
)
308 if ( event
.GetEventType() == wxEVT_FILECTRL_FOLDERCHANGED
)
309 wxLogMessage("Folder changed event, new folder: %s", event
.GetDirectory());
310 else if ( event
.GetEventType() == wxEVT_FILECTRL_FILEACTIVATED
)
311 wxLogMessage("File activated event: %s", wxJoin(event
.GetFiles(), ' '));
312 else if ( event
.GetEventType() == wxEVT_FILECTRL_SELECTIONCHANGED
)
313 wxLogMessage("Selection changed event: %s", wxJoin(event
.GetFiles(), ' '));
316 #endif // wxUSE_FILECTRL