filectrl.cpp 10 KB

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