dirpicker.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: dirpicker.cpp
  4. // Purpose: Shows wxDirPickerCtrl
  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_DIRPICKERCTRL
  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/dirpicker.xpm"
  37. // ----------------------------------------------------------------------------
  38. // constants
  39. // ----------------------------------------------------------------------------
  40. // control ids
  41. enum
  42. {
  43. PickerPage_Reset = wxID_HIGHEST,
  44. PickerPage_Dir,
  45. PickerPage_SetDir
  46. };
  47. // ----------------------------------------------------------------------------
  48. // DirPickerWidgetsPage
  49. // ----------------------------------------------------------------------------
  50. class DirPickerWidgetsPage : public WidgetsPage
  51. {
  52. public:
  53. DirPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  54. virtual ~DirPickerWidgetsPage(){};
  55. virtual wxControl *GetWidget() const { return m_dirPicker; }
  56. virtual void RecreateWidget() { RecreatePicker(); }
  57. // lazy creation of the content
  58. virtual void CreateContent();
  59. protected:
  60. // called only once at first construction
  61. void CreatePicker();
  62. // called to recreate an existing control
  63. void RecreatePicker();
  64. // restore the checkboxes state to the initial values
  65. void Reset();
  66. // get the initial style for the picker of the given kind
  67. long GetPickerStyle();
  68. void OnDirChange(wxFileDirPickerEvent &ev);
  69. void OnCheckBox(wxCommandEvent &ev);
  70. void OnButtonReset(wxCommandEvent &ev);
  71. void OnButtonSetDir(wxCommandEvent &ev);
  72. // the picker
  73. wxDirPickerCtrl *m_dirPicker;
  74. // other controls
  75. // --------------
  76. wxCheckBox *m_chkDirTextCtrl,
  77. *m_chkDirChangeDir,
  78. *m_chkDirMustExist,
  79. *m_chkSmall;
  80. wxTextCtrl *m_textInitialDir;
  81. wxBoxSizer *m_sizer;
  82. private:
  83. wxDECLARE_EVENT_TABLE();
  84. DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage)
  85. };
  86. // ----------------------------------------------------------------------------
  87. // event tables
  88. // ----------------------------------------------------------------------------
  89. wxBEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage)
  90. EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset)
  91. EVT_BUTTON(PickerPage_SetDir, DirPickerWidgetsPage::OnButtonSetDir)
  92. EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange)
  93. EVT_CHECKBOX(wxID_ANY, DirPickerWidgetsPage::OnCheckBox)
  94. wxEND_EVENT_TABLE()
  95. // ============================================================================
  96. // implementation
  97. // ============================================================================
  98. #if defined(__WXGTK24__)
  99. #define FAMILY_CTRLS NATIVE_CTRLS
  100. #else
  101. #define FAMILY_CTRLS GENERIC_CTRLS
  102. #endif
  103. IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage, wxT("DirPicker"),
  104. PICKER_CTRLS | FAMILY_CTRLS);
  105. DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl *book,
  106. wxImageList *imaglist)
  107. : WidgetsPage(book, imaglist, dirpicker_xpm)
  108. {
  109. }
  110. void DirPickerWidgetsPage::CreateContent()
  111. {
  112. // left pane
  113. wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
  114. wxStaticBoxSizer *dirbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&DirPicker style"));
  115. m_chkDirTextCtrl = CreateCheckBoxAndAddToSizer(dirbox, wxT("With textctrl"));
  116. m_chkDirMustExist = CreateCheckBoxAndAddToSizer(dirbox, wxT("Dir must exist"));
  117. m_chkDirChangeDir = CreateCheckBoxAndAddToSizer(dirbox, wxT("Change working dir"));
  118. m_chkSmall = CreateCheckBoxAndAddToSizer(dirbox, "&Small version");
  119. boxleft->Add(dirbox, 0, wxALL|wxGROW, 5);
  120. boxleft->Add(CreateSizerWithTextAndButton
  121. (
  122. PickerPage_SetDir,
  123. "&Initial directory",
  124. wxID_ANY,
  125. &m_textInitialDir
  126. ), wxSizerFlags().Expand().Border());
  127. boxleft->AddSpacer(10);
  128. boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
  129. 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  130. Reset(); // set checkboxes state
  131. // create pickers
  132. m_dirPicker = NULL;
  133. CreatePicker();
  134. // right pane
  135. m_sizer = new wxBoxSizer(wxVERTICAL);
  136. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  137. m_sizer->Add(m_dirPicker, 0, wxEXPAND|wxALL, 5);
  138. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  139. // global pane
  140. wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
  141. sz->Add(boxleft, 0, wxGROW|wxALL, 5);
  142. sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
  143. SetSizer(sz);
  144. }
  145. void DirPickerWidgetsPage::CreatePicker()
  146. {
  147. delete m_dirPicker;
  148. m_dirPicker = new wxDirPickerCtrl(this, PickerPage_Dir,
  149. wxGetHomeDir(), wxT("Hello!"),
  150. wxDefaultPosition, wxDefaultSize,
  151. GetPickerStyle());
  152. }
  153. long DirPickerWidgetsPage::GetPickerStyle()
  154. {
  155. long style = 0;
  156. if ( m_chkDirTextCtrl->GetValue() )
  157. style |= wxDIRP_USE_TEXTCTRL;
  158. if ( m_chkDirMustExist->GetValue() )
  159. style |= wxDIRP_DIR_MUST_EXIST;
  160. if ( m_chkDirChangeDir->GetValue() )
  161. style |= wxDIRP_CHANGE_DIR;
  162. if ( m_chkSmall->GetValue() )
  163. style |= wxDIRP_SMALL;
  164. return style;
  165. }
  166. void DirPickerWidgetsPage::RecreatePicker()
  167. {
  168. m_sizer->Remove(1);
  169. CreatePicker();
  170. m_sizer->Insert(1, m_dirPicker, 0, wxEXPAND|wxALL, 5);
  171. m_sizer->Layout();
  172. }
  173. void DirPickerWidgetsPage::Reset()
  174. {
  175. m_chkDirTextCtrl->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_USE_TEXTCTRL) != 0);
  176. m_chkDirMustExist->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_DIR_MUST_EXIST) != 0);
  177. m_chkDirChangeDir->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_CHANGE_DIR) != 0);
  178. m_chkSmall->SetValue((wxFLP_DEFAULT_STYLE & wxDIRP_SMALL) != 0);
  179. }
  180. // ----------------------------------------------------------------------------
  181. // event handlers
  182. // ----------------------------------------------------------------------------
  183. void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
  184. {
  185. m_dirPicker->SetInitialDirectory(m_textInitialDir->GetValue());
  186. }
  187. void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  188. {
  189. Reset();
  190. RecreatePicker();
  191. }
  192. void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent& event)
  193. {
  194. wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
  195. event.GetPath().c_str(), wxGetCwd().c_str());
  196. }
  197. void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
  198. {
  199. if (event.GetEventObject() == m_chkDirTextCtrl ||
  200. event.GetEventObject() == m_chkDirChangeDir ||
  201. event.GetEventObject() == m_chkDirMustExist ||
  202. event.GetEventObject() == m_chkSmall)
  203. RecreatePicker();
  204. }
  205. #endif // wxUSE_DIRPICKERCTRL