filepicker.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: filepicker.cpp
  4. // Purpose: Part of the widgets sample showing wx*PickerCtrl
  5. // Author: Francesco Montorsi
  6. // Created: 20/6/2006
  7. // Copyright: (c) 2006 Francesco Montorsi
  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_FILEPICKERCTRL
  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/radiobox.h"
  27. #include "wx/textctrl.h"
  28. #endif
  29. #include "wx/artprov.h"
  30. #include "wx/sizer.h"
  31. #include "wx/stattext.h"
  32. #include "wx/checkbox.h"
  33. #include "wx/imaglist.h"
  34. #include "wx/filepicker.h"
  35. #include "widgets.h"
  36. #include "icons/filepicker.xpm"
  37. // ----------------------------------------------------------------------------
  38. // constants
  39. // ----------------------------------------------------------------------------
  40. enum
  41. {
  42. FilePickerMode_Open = 0,
  43. FilePickerMode_Save
  44. };
  45. // control ids
  46. enum
  47. {
  48. PickerPage_Reset = wxID_HIGHEST,
  49. PickerPage_File,
  50. PickerPage_SetDir,
  51. PickerPage_CurrentPath
  52. };
  53. // ----------------------------------------------------------------------------
  54. // FilePickerWidgetsPage
  55. // ----------------------------------------------------------------------------
  56. class FilePickerWidgetsPage : public WidgetsPage
  57. {
  58. public:
  59. FilePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  60. virtual ~FilePickerWidgetsPage(){};
  61. virtual wxControl *GetWidget() const { return m_filePicker; }
  62. virtual void RecreateWidget() { RecreatePicker(); }
  63. // lazy creation of the content
  64. virtual void CreateContent();
  65. protected:
  66. // called only once at first construction
  67. void CreatePicker();
  68. // called to recreate an existing control
  69. void RecreatePicker();
  70. // restore the checkboxes state to the initial values
  71. void Reset();
  72. // get the initial style for the picker of the given kind
  73. long GetPickerStyle();
  74. // update filepicker radiobox
  75. void UpdateFilePickerMode();
  76. // the pickers and the relative event handlers
  77. void OnFileChange(wxFileDirPickerEvent &ev);
  78. void OnCheckBox(wxCommandEvent &ev);
  79. void OnButtonReset(wxCommandEvent &ev);
  80. void OnButtonSetDir(wxCommandEvent &ev);
  81. void OnUpdatePath(wxUpdateUIEvent &ev);
  82. // the picker
  83. wxFilePickerCtrl *m_filePicker;
  84. // other controls
  85. // --------------
  86. wxCheckBox *m_chkFileTextCtrl,
  87. *m_chkFileOverwritePrompt,
  88. *m_chkFileMustExist,
  89. *m_chkFileChangeDir,
  90. *m_chkSmall;
  91. wxRadioBox *m_radioFilePickerMode;
  92. wxStaticText *m_labelPath;
  93. wxTextCtrl *m_textInitialDir;
  94. wxBoxSizer *m_sizer;
  95. private:
  96. wxDECLARE_EVENT_TABLE();
  97. DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage)
  98. };
  99. // ----------------------------------------------------------------------------
  100. // event tables
  101. // ----------------------------------------------------------------------------
  102. wxBEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
  103. EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
  104. EVT_BUTTON(PickerPage_SetDir, FilePickerWidgetsPage::OnButtonSetDir)
  105. EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
  106. EVT_CHECKBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
  107. EVT_RADIOBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
  108. EVT_UPDATE_UI(PickerPage_CurrentPath, FilePickerWidgetsPage::OnUpdatePath)
  109. wxEND_EVENT_TABLE()
  110. // ============================================================================
  111. // implementation
  112. // ============================================================================
  113. #if defined(__WXGTK24__)
  114. #define FAMILY_CTRLS NATIVE_CTRLS
  115. #else
  116. #define FAMILY_CTRLS GENERIC_CTRLS
  117. #endif
  118. IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage, wxT("FilePicker"),
  119. PICKER_CTRLS | FAMILY_CTRLS);
  120. FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl *book,
  121. wxImageList *imaglist)
  122. : WidgetsPage(book, imaglist, filepicker_xpm)
  123. {
  124. }
  125. void FilePickerWidgetsPage::CreateContent()
  126. {
  127. // left pane
  128. wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
  129. static const wxString mode[] = { wxT("open"), wxT("save") };
  130. m_radioFilePickerMode = new wxRadioBox(this, wxID_ANY, wxT("wxFilePicker mode"),
  131. wxDefaultPosition, wxDefaultSize,
  132. WXSIZEOF(mode), mode);
  133. boxleft->Add(m_radioFilePickerMode, 0, wxALL|wxGROW, 5);
  134. wxStaticBoxSizer *filebox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&FilePicker style"));
  135. m_chkFileTextCtrl = CreateCheckBoxAndAddToSizer(filebox, wxT("With textctrl"));
  136. m_chkFileOverwritePrompt = CreateCheckBoxAndAddToSizer(filebox, wxT("Overwrite prompt"));
  137. m_chkFileMustExist = CreateCheckBoxAndAddToSizer(filebox, wxT("File must exist"));
  138. m_chkFileChangeDir = CreateCheckBoxAndAddToSizer(filebox, wxT("Change working dir"));
  139. m_chkSmall = CreateCheckBoxAndAddToSizer(filebox, "&Small version");
  140. boxleft->Add(filebox, 0, wxALL|wxGROW, 5);
  141. boxleft->Add(CreateSizerWithTextAndButton
  142. (
  143. PickerPage_SetDir,
  144. "&Initial directory",
  145. wxID_ANY,
  146. &m_textInitialDir
  147. ), wxSizerFlags().Expand().Border());
  148. boxleft->AddSpacer(10);
  149. boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
  150. 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  151. Reset(); // set checkboxes state
  152. // create the picker and the static text displaying its current value
  153. m_labelPath = new wxStaticText(this, PickerPage_CurrentPath, "");
  154. m_filePicker = NULL;
  155. CreatePicker();
  156. // right pane
  157. m_sizer = new wxBoxSizer(wxVERTICAL);
  158. m_sizer->AddStretchSpacer();
  159. m_sizer->Add(m_filePicker, wxSizerFlags().Expand().Border());
  160. m_sizer->AddStretchSpacer();
  161. m_sizer->Add(m_labelPath, wxSizerFlags().Expand().Border());
  162. m_sizer->AddStretchSpacer();
  163. // global pane
  164. wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
  165. sz->Add(boxleft, 0, wxGROW|wxALL, 5);
  166. sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
  167. SetSizer(sz);
  168. }
  169. void FilePickerWidgetsPage::CreatePicker()
  170. {
  171. delete m_filePicker;
  172. // pass an empty string as initial file
  173. m_filePicker = new wxFilePickerCtrl(this, PickerPage_File,
  174. wxEmptyString,
  175. wxT("Hello!"), wxT("*"),
  176. wxDefaultPosition, wxDefaultSize,
  177. GetPickerStyle());
  178. }
  179. long FilePickerWidgetsPage::GetPickerStyle()
  180. {
  181. long style = 0;
  182. if ( m_chkFileTextCtrl->GetValue() )
  183. style |= wxFLP_USE_TEXTCTRL;
  184. if ( m_chkFileOverwritePrompt->GetValue() )
  185. style |= wxFLP_OVERWRITE_PROMPT;
  186. if ( m_chkFileMustExist->GetValue() )
  187. style |= wxFLP_FILE_MUST_EXIST;
  188. if ( m_chkFileChangeDir->GetValue() )
  189. style |= wxFLP_CHANGE_DIR;
  190. if ( m_chkSmall->GetValue() )
  191. style |= wxFLP_SMALL;
  192. if (m_radioFilePickerMode->GetSelection() == FilePickerMode_Open)
  193. style |= wxFLP_OPEN;
  194. else
  195. style |= wxFLP_SAVE;
  196. return style;
  197. }
  198. void FilePickerWidgetsPage::RecreatePicker()
  199. {
  200. m_sizer->Remove(1);
  201. CreatePicker();
  202. m_sizer->Insert(1, m_filePicker, 0, wxEXPAND|wxALL, 5);
  203. m_sizer->Layout();
  204. }
  205. void FilePickerWidgetsPage::Reset()
  206. {
  207. m_radioFilePickerMode->SetSelection((wxFLP_DEFAULT_STYLE & wxFLP_OPEN) ?
  208. FilePickerMode_Open : FilePickerMode_Save);
  209. m_chkFileTextCtrl->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_USE_TEXTCTRL) != 0);
  210. m_chkFileOverwritePrompt->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_OVERWRITE_PROMPT) != 0);
  211. m_chkFileMustExist->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_FILE_MUST_EXIST) != 0);
  212. m_chkFileChangeDir->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_CHANGE_DIR) != 0);
  213. m_chkSmall->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_SMALL) != 0);
  214. UpdateFilePickerMode();
  215. }
  216. void FilePickerWidgetsPage::UpdateFilePickerMode()
  217. {
  218. switch (m_radioFilePickerMode->GetSelection())
  219. {
  220. case FilePickerMode_Open:
  221. m_chkFileOverwritePrompt->SetValue(false);
  222. m_chkFileOverwritePrompt->Disable();
  223. m_chkFileMustExist->Enable();
  224. break;
  225. case FilePickerMode_Save:
  226. m_chkFileMustExist->SetValue(false);
  227. m_chkFileMustExist->Disable();
  228. m_chkFileOverwritePrompt->Enable();
  229. break;
  230. }
  231. }
  232. // ----------------------------------------------------------------------------
  233. // event handlers
  234. // ----------------------------------------------------------------------------
  235. void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
  236. {
  237. const wxString& dir = m_textInitialDir->GetValue();
  238. m_filePicker->SetInitialDirectory(dir);
  239. wxLogMessage("Initial directory set to \"%s\"", dir);
  240. }
  241. void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  242. {
  243. Reset();
  244. RecreatePicker();
  245. }
  246. void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent& event)
  247. {
  248. wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
  249. event.GetPath().c_str(), wxGetCwd().c_str());
  250. }
  251. void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
  252. {
  253. if (event.GetEventObject() == m_chkFileTextCtrl ||
  254. event.GetEventObject() == m_chkFileOverwritePrompt ||
  255. event.GetEventObject() == m_chkFileMustExist ||
  256. event.GetEventObject() == m_chkFileChangeDir ||
  257. event.GetEventObject() == m_chkSmall)
  258. RecreatePicker();
  259. if (event.GetEventObject() == m_radioFilePickerMode)
  260. {
  261. UpdateFilePickerMode();
  262. RecreatePicker();
  263. }
  264. }
  265. void FilePickerWidgetsPage::OnUpdatePath(wxUpdateUIEvent& ev)
  266. {
  267. ev.SetText( "Current path: " + m_filePicker->GetPath() );
  268. }
  269. #endif // wxUSE_FILEPICKERCTRL