dirctrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: dirctrl.cpp
  4. // Purpose: Part of the widgets sample showing wxGenericDirCtrl
  5. // Author: Wlodzimierz 'ABX' Skiba
  6. // Created: 4 Oct 2006
  7. // Copyright: (c) 2006 wxWindows team
  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_DIRDLG
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/app.h"
  25. #include "wx/log.h"
  26. #include "wx/sizer.h"
  27. #include "wx/statbox.h"
  28. #include "wx/radiobox.h"
  29. #include "wx/checkbox.h"
  30. #include "wx/button.h"
  31. #include "wx/filedlg.h"
  32. #endif
  33. #include "wx/generic/dirctrlg.h"
  34. #include "wx/wupdlock.h"
  35. #include "wx/stdpaths.h"
  36. #include "wx/filename.h"
  37. #include "widgets.h"
  38. #include "icons/dirctrl.xpm"
  39. // ----------------------------------------------------------------------------
  40. // constants
  41. // ----------------------------------------------------------------------------
  42. // control ids
  43. enum
  44. {
  45. DirCtrlPage_Reset = wxID_HIGHEST,
  46. DirCtrlPage_SetPath,
  47. DirCtrlPage_Ctrl
  48. };
  49. static const wxString stdPaths[] =
  50. {
  51. wxT("&none"),
  52. wxT("&config"),
  53. wxT("&data"),
  54. wxT("&documents"),
  55. wxT("&local data"),
  56. wxT("&plugins"),
  57. wxT("&resources"),
  58. wxT("&user config"),
  59. wxT("&user data"),
  60. wxT("&user local data")
  61. };
  62. enum
  63. {
  64. stdPathUnknown = 0,
  65. stdPathConfig,
  66. stdPathData,
  67. stdPathDocuments,
  68. stdPathLocalData,
  69. stdPathPlugins,
  70. stdPathResources,
  71. stdPathUserConfig,
  72. stdPathUserData,
  73. stdPathUserLocalData,
  74. stdPathMax
  75. };
  76. // ----------------------------------------------------------------------------
  77. // CheckBoxWidgetsPage
  78. // ----------------------------------------------------------------------------
  79. class DirCtrlWidgetsPage : public WidgetsPage
  80. {
  81. public:
  82. DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  83. virtual ~DirCtrlWidgetsPage() {}
  84. virtual wxControl *GetWidget() const { return m_dirCtrl; }
  85. virtual void RecreateWidget() { CreateDirCtrl(); }
  86. // lazy creation of the content
  87. virtual void CreateContent();
  88. protected:
  89. // event handlers
  90. void OnButtonSetPath(wxCommandEvent& event);
  91. void OnButtonReset(wxCommandEvent& event);
  92. void OnStdPath(wxCommandEvent& event);
  93. void OnCheckBox(wxCommandEvent& event);
  94. void OnRadioBox(wxCommandEvent& event);
  95. void OnSelChanged(wxTreeEvent& event);
  96. void OnFileActivated(wxTreeEvent& event);
  97. // reset the control parameters
  98. void Reset();
  99. // (re)create the m_dirCtrl
  100. void CreateDirCtrl();
  101. // the controls
  102. // ------------
  103. // the control itself and the sizer it is in
  104. wxGenericDirCtrl *m_dirCtrl;
  105. // the text entries for command parameters
  106. wxTextCtrl *m_path;
  107. wxRadioBox *m_radioStdPath;
  108. // flags
  109. wxCheckBox *m_chkDirOnly,
  110. *m_chk3D,
  111. *m_chkFirst,
  112. *m_chkFilters,
  113. *m_chkLabels,
  114. *m_chkMulti;
  115. // filters
  116. wxCheckBox *m_fltr[3];
  117. private:
  118. wxDECLARE_EVENT_TABLE();
  119. DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
  120. };
  121. // ----------------------------------------------------------------------------
  122. // event tables
  123. // ----------------------------------------------------------------------------
  124. wxBEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
  125. EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
  126. EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
  127. EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
  128. EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
  129. EVT_DIRCTRL_SELECTIONCHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged)
  130. EVT_DIRCTRL_FILEACTIVATED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnFileActivated)
  131. wxEND_EVENT_TABLE()
  132. // ============================================================================
  133. // implementation
  134. // ============================================================================
  135. IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
  136. GENERIC_CTRLS
  137. );
  138. DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
  139. wxImageList *imaglist)
  140. :WidgetsPage(book, imaglist, dirctrl_xpm)
  141. {
  142. m_dirCtrl = NULL;
  143. }
  144. void DirCtrlWidgetsPage::CreateContent()
  145. {
  146. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  147. // left pane
  148. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
  149. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  150. sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
  151. 0, wxALL | wxALIGN_RIGHT , 5 );
  152. wxSizer *sizerUseFlags =
  153. new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Flags"));
  154. m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_DIR_ONLY"));
  155. m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_3D_INTERNAL"));
  156. m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SELECT_FIRST"));
  157. m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SHOW_FILTERS"));
  158. m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_EDIT_LABELS"));
  159. m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_MULTIPLE"));
  160. sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
  161. wxSizer *sizerFilters =
  162. new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Filters"));
  163. m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
  164. wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
  165. m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
  166. m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
  167. sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
  168. wxButton *btn = new wxButton(this, DirCtrlPage_Reset, wxT("&Reset"));
  169. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  170. // keep consistency between enum and labels of radiobox
  171. wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
  172. // middle pane
  173. m_radioStdPath = new wxRadioBox(this, wxID_ANY, wxT("Standard path"),
  174. wxDefaultPosition, wxDefaultSize,
  175. WXSIZEOF(stdPaths), stdPaths, 1);
  176. // right pane
  177. m_dirCtrl = new wxGenericDirCtrl(
  178. this,
  179. DirCtrlPage_Ctrl,
  180. wxDirDialogDefaultFolderStr,
  181. wxDefaultPosition,
  182. wxDefaultSize,
  183. 0
  184. );
  185. // the 3 panes panes compose the window
  186. sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
  187. sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
  188. sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  189. // final initializations
  190. Reset();
  191. SetSizer(sizerTop);
  192. }
  193. void DirCtrlWidgetsPage::Reset()
  194. {
  195. m_path->SetValue(m_dirCtrl->GetPath());
  196. }
  197. void DirCtrlWidgetsPage::CreateDirCtrl()
  198. {
  199. wxWindowUpdateLocker noUpdates(this);
  200. wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
  201. this,
  202. DirCtrlPage_Ctrl,
  203. wxDirDialogDefaultFolderStr,
  204. wxDefaultPosition,
  205. wxDefaultSize,
  206. ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
  207. ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
  208. ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
  209. ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
  210. ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
  211. ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
  212. );
  213. wxString filter;
  214. for (int i = 0; i < 3; ++i)
  215. {
  216. if (m_fltr[i]->IsChecked())
  217. {
  218. if (!filter.IsEmpty())
  219. filter += wxT("|");
  220. filter += m_fltr[i]->GetLabel();
  221. }
  222. }
  223. dirCtrl->SetFilter(filter);
  224. // update sizer's child window
  225. GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
  226. // update our pointer
  227. delete m_dirCtrl;
  228. m_dirCtrl = dirCtrl;
  229. // relayout the sizer
  230. GetSizer()->Layout();
  231. }
  232. // ----------------------------------------------------------------------------
  233. // event handlers
  234. // ----------------------------------------------------------------------------
  235. void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
  236. {
  237. m_dirCtrl->SetPath(m_path->GetValue());
  238. }
  239. void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  240. {
  241. Reset();
  242. CreateDirCtrl();
  243. }
  244. void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
  245. {
  246. CreateDirCtrl();
  247. }
  248. void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
  249. {
  250. wxString path;
  251. wxTheApp->SetAppName(wxT("widgets"));
  252. wxStandardPathsBase& stdp = wxStandardPaths::Get();
  253. switch ( m_radioStdPath->GetSelection() )
  254. {
  255. default:
  256. case stdPathUnknown:
  257. case stdPathMax:
  258. // leave path
  259. break;
  260. case stdPathConfig:
  261. path = stdp.GetConfigDir();
  262. break;
  263. case stdPathData:
  264. path = stdp.GetDataDir();
  265. break;
  266. case stdPathDocuments:
  267. path = stdp.GetDocumentsDir();
  268. break;
  269. case stdPathLocalData:
  270. path = stdp.GetLocalDataDir();
  271. break;
  272. case stdPathPlugins:
  273. path = stdp.GetPluginsDir();
  274. break;
  275. case stdPathResources:
  276. path = stdp.GetResourcesDir();
  277. break;
  278. case stdPathUserConfig:
  279. path = stdp.GetUserConfigDir();
  280. break;
  281. case stdPathUserData:
  282. path = stdp.GetUserDataDir();
  283. break;
  284. case stdPathUserLocalData:
  285. path = stdp.GetUserLocalDataDir();
  286. break;
  287. }
  288. m_dirCtrl->SetPath(path);
  289. // Notice that we must use wxFileName comparison instead of simple wxString
  290. // comparison as the paths returned may differ by case only.
  291. if ( wxFileName(m_dirCtrl->GetPath()) != path )
  292. {
  293. wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
  294. path, m_dirCtrl->GetPath());
  295. }
  296. }
  297. void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event)
  298. {
  299. if ( m_dirCtrl )
  300. {
  301. wxLogMessage("Selection changed to \"%s\"",
  302. m_dirCtrl->GetPath(event.GetItem()));
  303. }
  304. event.Skip();
  305. }
  306. void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent& event)
  307. {
  308. if ( m_dirCtrl )
  309. {
  310. wxLogMessage("File activated \"%s\"",
  311. m_dirCtrl->GetPath(event.GetItem()));
  312. }
  313. event.Skip();
  314. }
  315. #endif // wxUSE_DIRDLG