| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /////////////////////////////////////////////////////////////////////////////
- // Program: wxWidgets Widgets Sample
- // Name: filectrl.cpp
- // Purpose: Part of the widgets sample showing wxFileCtrl
- // Author: Diaa M. Sami
- // Created: 28 Jul 2007
- // Copyright: (c) 2007 Diaa M. Sami
- // Licence: wxWindows licence
- /////////////////////////////////////////////////////////////////////////////
- // ============================================================================
- // declarations
- // ============================================================================
- // ----------------------------------------------------------------------------
- // headers
- // ----------------------------------------------------------------------------
- // for compilers that support precompilation, includes "wx/wx.h".
- #include "wx/wxprec.h"
- #ifdef __BORLANDC__
- #pragma hdrstop
- #endif
- #if wxUSE_FILECTRL
- // for all others, include the necessary headers
- #ifndef WX_PRECOMP
- #include "wx/wx.h"
- #endif
- #include "wx/filectrl.h"
- #include "wx/wupdlock.h"
- #include "wx/filename.h"
- #include "widgets.h"
- // TODO change this
- #include "icons/dirctrl.xpm"
- // ----------------------------------------------------------------------------
- // constants
- // ----------------------------------------------------------------------------
- // control ids
- enum
- {
- FileCtrlPage_Reset = wxID_HIGHEST,
- FileCtrlPage_SetDirectory,
- FileCtrlPage_SetPath,
- FileCtrlPage_SetFilename,
- FileCtrlPage_Ctrl
- };
- enum
- {
- FileCtrlMode_Open = 0,
- FileCtrlMode_Save
- };
- // ----------------------------------------------------------------------------
- // CheckBoxWidgetsPage
- // ----------------------------------------------------------------------------
- class FileCtrlWidgetsPage : public WidgetsPage
- {
- public:
- FileCtrlWidgetsPage( WidgetsBookCtrl *book, wxImageList *imaglist );
- virtual ~FileCtrlWidgetsPage() {}
- virtual wxControl *GetWidget() const { return m_fileCtrl; }
- virtual void RecreateWidget() { CreateFileCtrl(); }
- // lazy creation of the content
- virtual void CreateContent();
- protected:
- // event handlers
- void OnButtonSetDirectory( wxCommandEvent& event );
- void OnButtonSetPath( wxCommandEvent& event );
- void OnButtonSetFilename( wxCommandEvent& event );
- void OnButtonReset( wxCommandEvent& event );
- void OnCheckBox( wxCommandEvent& event );
- void OnRadioBox( wxCommandEvent& event );
- void OnFileCtrl( wxFileCtrlEvent& event );
- // reset the control parameters
- void Reset();
- // (re)create the m_fileCtrl
- void CreateFileCtrl();
- // the controls
- // ------------
- // the control itself and the sizer it is in
- wxFileCtrl *m_fileCtrl;
- // the text entries for command parameters
- wxTextCtrl *m_dir;
- wxTextCtrl *m_path;
- wxTextCtrl *m_filename;
- // flags
- wxCheckBox *m_chkMultiple,
- *m_chkNoShowHidden;
- wxRadioBox *m_radioFileCtrlMode;
- // filters
- wxCheckBox *m_fltr[3];
- private:
- wxDECLARE_EVENT_TABLE();
- DECLARE_WIDGETS_PAGE( FileCtrlWidgetsPage )
- };
- // ----------------------------------------------------------------------------
- // event tables
- // ----------------------------------------------------------------------------
- wxBEGIN_EVENT_TABLE( FileCtrlWidgetsPage, WidgetsPage )
- EVT_BUTTON( FileCtrlPage_Reset, FileCtrlWidgetsPage::OnButtonReset )
- EVT_BUTTON( FileCtrlPage_SetDirectory, FileCtrlWidgetsPage::OnButtonSetDirectory )
- EVT_BUTTON( FileCtrlPage_SetPath, FileCtrlWidgetsPage::OnButtonSetPath )
- EVT_BUTTON( FileCtrlPage_SetFilename, FileCtrlWidgetsPage::OnButtonSetFilename )
- EVT_CHECKBOX( wxID_ANY, FileCtrlWidgetsPage::OnCheckBox )
- EVT_RADIOBOX( wxID_ANY, FileCtrlWidgetsPage::OnRadioBox )
- EVT_FILECTRL_FILTERCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
- EVT_FILECTRL_FOLDERCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
- EVT_FILECTRL_SELECTIONCHANGED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
- EVT_FILECTRL_FILEACTIVATED( wxID_ANY, FileCtrlWidgetsPage::OnFileCtrl )
- wxEND_EVENT_TABLE()
- // ============================================================================
- // implementation
- // ============================================================================
- #if defined(__WXGTK__)
- #define FAMILY_CTRLS NATIVE_CTRLS
- #else
- #define FAMILY_CTRLS GENERIC_CTRLS
- #endif
- IMPLEMENT_WIDGETS_PAGE( FileCtrlWidgetsPage, wxT( "FileCtrl" ),
- FAMILY_CTRLS );
- FileCtrlWidgetsPage::FileCtrlWidgetsPage( WidgetsBookCtrl *book,
- wxImageList *imaglist )
- : WidgetsPage( book, imaglist, dirctrl_xpm )
- {
- }
- void FileCtrlWidgetsPage::CreateContent()
- {
- wxSizer *sizerTop = new wxBoxSizer( wxHORIZONTAL );
- // left pane
- wxSizer *sizerLeft = new wxBoxSizer( wxVERTICAL );
- static const wxString mode[] = { wxT( "open" ), wxT( "save" ) };
- m_radioFileCtrlMode = new wxRadioBox( this, wxID_ANY, wxT( "wxFileCtrl mode" ),
- wxDefaultPosition, wxDefaultSize,
- WXSIZEOF( mode ), mode );
- sizerLeft->Add( m_radioFileCtrlMode,
- 0, wxALL | wxEXPAND , 5 );
- sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetDirectory , wxT( "Set &directory" ), wxID_ANY, &m_dir ),
- 0, wxALL | wxEXPAND , 5 );
- sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetPath , wxT( "Set &path" ), wxID_ANY, &m_path ),
- 0, wxALL | wxEXPAND , 5 );
- sizerLeft->Add( CreateSizerWithTextAndButton( FileCtrlPage_SetFilename , wxT( "Set &filename" ), wxID_ANY, &m_filename ),
- 0, wxALL | wxEXPAND , 5 );
- wxSizer *sizerUseFlags =
- new wxStaticBoxSizer( wxVERTICAL, this, wxT( "&Flags" ) );
- m_chkMultiple = CreateCheckBoxAndAddToSizer( sizerUseFlags, wxT( "wxFC_MULTIPLE" ) );
- m_chkNoShowHidden = CreateCheckBoxAndAddToSizer( sizerUseFlags, wxT( "wxFC_NOSHOWHIDDEN" ) );
- sizerLeft->Add( sizerUseFlags, wxSizerFlags().Expand().Border() );
- wxSizer *sizerFilters =
- new wxStaticBoxSizer( wxVERTICAL, this, wxT( "&Filters" ) );
- m_fltr[0] = CreateCheckBoxAndAddToSizer( sizerFilters, wxString::Format( wxT( "all files (%s)|%s" ),
- wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr ) );
- m_fltr[1] = CreateCheckBoxAndAddToSizer( sizerFilters, wxT( "C++ files (*.cpp; *.h)|*.cpp;*.h" ) );
- m_fltr[2] = CreateCheckBoxAndAddToSizer( sizerFilters, wxT( "PNG images (*.png)|*.png" ) );
- sizerLeft->Add( sizerFilters, wxSizerFlags().Expand().Border() );
- wxButton *btn = new wxButton( this, FileCtrlPage_Reset, wxT( "&Reset" ) );
- sizerLeft->Add( btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15 );
- // right pane
- m_fileCtrl = new wxFileCtrl(
- this,
- FileCtrlPage_Ctrl,
- wxEmptyString,
- wxEmptyString,
- wxEmptyString,
- wxFC_OPEN,
- wxDefaultPosition,
- wxDefaultSize
- );
- // the 3 panes panes compose the window
- sizerTop->Add( sizerLeft, 0, ( wxALL & ~wxLEFT ), 10 );
- sizerTop->Add( m_fileCtrl, 1, wxGROW | ( wxALL & ~wxRIGHT ), 10 );
- // final initializations
- Reset();
- SetSizer( sizerTop );
- }
- void FileCtrlWidgetsPage::Reset()
- {
- m_dir->SetValue( m_fileCtrl->GetDirectory() );
- m_radioFileCtrlMode->SetSelection( ( wxFC_DEFAULT_STYLE & wxFC_OPEN ) ?
- FileCtrlMode_Open : FileCtrlMode_Save );
- }
- void FileCtrlWidgetsPage::CreateFileCtrl()
- {
- wxWindowUpdateLocker noUpdates( this );
- const int style =
- ( m_radioFileCtrlMode->GetSelection() == FileCtrlMode_Open ?
- wxFC_OPEN : wxFC_SAVE ) |
- ( m_chkMultiple->IsChecked() ? wxFC_MULTIPLE : 0 ) |
- ( m_chkNoShowHidden->IsChecked() ? wxFC_NOSHOWHIDDEN : 0 );
- wxFileCtrl *fileCtrl = new wxFileCtrl(
- this,
- FileCtrlPage_Ctrl,
- wxEmptyString,
- wxEmptyString,
- wxEmptyString,
- style,
- wxDefaultPosition,
- wxDefaultSize
- );
- wxString wildcard;
- for ( unsigned int i = 0; i < WXSIZEOF( m_fltr ); ++i )
- {
- if ( m_fltr[i]->IsChecked() )
- {
- if ( !wildcard.IsEmpty() )
- wildcard += wxT( "|" );
- wildcard += m_fltr[i]->GetLabel();
- }
- }
- fileCtrl->SetWildcard( wildcard );
- // update sizer's child window
- GetSizer()->Replace( m_fileCtrl, fileCtrl, true );
- // update our pointer
- delete m_fileCtrl;
- m_fileCtrl = fileCtrl;
- // relayout the sizer
- GetSizer()->Layout();
- }
- // ----------------------------------------------------------------------------
- // event handlers
- // ----------------------------------------------------------------------------
- void FileCtrlWidgetsPage::OnButtonSetDirectory( wxCommandEvent& WXUNUSED( event ) )
- {
- m_fileCtrl->SetDirectory( m_dir->GetValue() );
- }
- void FileCtrlWidgetsPage::OnButtonSetPath( wxCommandEvent& WXUNUSED( event ) )
- {
- m_fileCtrl->SetPath( m_path->GetValue() );
- }
- void FileCtrlWidgetsPage::OnButtonSetFilename( wxCommandEvent& WXUNUSED( event ) )
- {
- m_fileCtrl->SetFilename( m_filename->GetValue() );
- }
- void FileCtrlWidgetsPage::OnButtonReset( wxCommandEvent& WXUNUSED( event ) )
- {
- Reset();
- CreateFileCtrl();
- }
- void FileCtrlWidgetsPage::OnCheckBox( wxCommandEvent& WXUNUSED( event ) )
- {
- CreateFileCtrl();
- }
- void FileCtrlWidgetsPage::OnRadioBox( wxCommandEvent& WXUNUSED( event ) )
- {
- CreateFileCtrl();
- }
- void FileCtrlWidgetsPage::OnFileCtrl( wxFileCtrlEvent& event )
- {
- if ( event.GetEventType() == wxEVT_FILECTRL_FOLDERCHANGED )
- {
- wxLogMessage("Folder changed event, new folder: %s", event.GetDirectory());
- }
- else if ( event.GetEventType() == wxEVT_FILECTRL_FILEACTIVATED )
- {
- wxLogMessage("File activated event: %s", wxJoin(event.GetFiles(), ' '));
- }
- else if ( event.GetEventType() == wxEVT_FILECTRL_SELECTIONCHANGED )
- {
- wxLogMessage("Selection changed event: %s", wxJoin(event.GetFiles(), ' '));
- }
- else if ( event.GetEventType() == wxEVT_FILECTRL_FILTERCHANGED )
- {
- wxLogMessage("Filter changed event: filter %d selected",
- event.GetFilterIndex() + 1);
- }
- }
- #endif // wxUSE_FILECTRL
|