filepickerg.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/filepickerg.h
  3. // Purpose: wxGenericFileDirButton, wxGenericFileButton, wxGenericDirButton
  4. // Author: Francesco Montorsi
  5. // Modified by:
  6. // Created: 14/4/2006
  7. // Copyright: (c) Francesco Montorsi
  8. // Licence: wxWindows Licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_FILEDIRPICKER_H_
  11. #define _WX_FILEDIRPICKER_H_
  12. #include "wx/button.h"
  13. #include "wx/filedlg.h"
  14. #include "wx/dirdlg.h"
  15. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_DIRPICKER_CHANGED, wxFileDirPickerEvent );
  16. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_FILEPICKER_CHANGED, wxFileDirPickerEvent );
  17. //-----------------------------------------------------------------------------
  18. // wxGenericFileDirButton: a button which brings up a wx{File|Dir}Dialog
  19. //-----------------------------------------------------------------------------
  20. class WXDLLIMPEXP_CORE wxGenericFileDirButton : public wxButton,
  21. public wxFileDirPickerWidgetBase
  22. {
  23. public:
  24. wxGenericFileDirButton() { Init(); }
  25. wxGenericFileDirButton(wxWindow *parent,
  26. wxWindowID id,
  27. const wxString& label = wxFilePickerWidgetLabel,
  28. const wxString& path = wxEmptyString,
  29. const wxString &message = wxFileSelectorPromptStr,
  30. const wxString &wildcard = wxFileSelectorDefaultWildcardStr,
  31. const wxPoint& pos = wxDefaultPosition,
  32. const wxSize& size = wxDefaultSize,
  33. long style = 0,
  34. const wxValidator& validator = wxDefaultValidator,
  35. const wxString& name = wxFilePickerWidgetNameStr)
  36. {
  37. Init();
  38. Create(parent, id, label, path, message, wildcard,
  39. pos, size, style, validator, name);
  40. }
  41. virtual wxControl *AsControl() { return this; }
  42. public: // overridable
  43. virtual wxDialog *CreateDialog() = 0;
  44. virtual wxWindow *GetDialogParent()
  45. { return GetParent(); }
  46. virtual wxEventType GetEventType() const = 0;
  47. virtual void SetInitialDirectory(const wxString& dir);
  48. public:
  49. bool Create(wxWindow *parent, wxWindowID id,
  50. const wxString& label = wxFilePickerWidgetLabel,
  51. const wxString& path = wxEmptyString,
  52. const wxString &message = wxFileSelectorPromptStr,
  53. const wxString &wildcard = wxFileSelectorDefaultWildcardStr,
  54. const wxPoint& pos = wxDefaultPosition,
  55. const wxSize& size = wxDefaultSize,
  56. long style = 0,
  57. const wxValidator& validator = wxDefaultValidator,
  58. const wxString& name = wxFilePickerWidgetNameStr);
  59. // event handler for the click
  60. void OnButtonClick(wxCommandEvent &);
  61. protected:
  62. wxString m_message, m_wildcard;
  63. // we just store the style passed to the ctor here instead of passing it to
  64. // wxButton as some of our bits can conflict with wxButton styles and it
  65. // just doesn't make sense to use picker styles for wxButton anyhow
  66. long m_pickerStyle;
  67. // Initial directory set by SetInitialDirectory() call or empty.
  68. wxString m_initialDir;
  69. private:
  70. // common part of all ctors
  71. void Init() { m_pickerStyle = -1; }
  72. };
  73. //-----------------------------------------------------------------------------
  74. // wxGenericFileButton: a button which brings up a wxFileDialog
  75. //-----------------------------------------------------------------------------
  76. #define wxFILEBTN_DEFAULT_STYLE (wxFLP_OPEN)
  77. class WXDLLIMPEXP_CORE wxGenericFileButton : public wxGenericFileDirButton
  78. {
  79. public:
  80. wxGenericFileButton() {}
  81. wxGenericFileButton(wxWindow *parent,
  82. wxWindowID id,
  83. const wxString& label = wxFilePickerWidgetLabel,
  84. const wxString& path = wxEmptyString,
  85. const wxString &message = wxFileSelectorPromptStr,
  86. const wxString &wildcard = wxFileSelectorDefaultWildcardStr,
  87. const wxPoint& pos = wxDefaultPosition,
  88. const wxSize& size = wxDefaultSize,
  89. long style = wxFILEBTN_DEFAULT_STYLE,
  90. const wxValidator& validator = wxDefaultValidator,
  91. const wxString& name = wxFilePickerWidgetNameStr)
  92. {
  93. Create(parent, id, label, path, message, wildcard,
  94. pos, size, style, validator, name);
  95. }
  96. public: // overridable
  97. virtual long GetDialogStyle() const
  98. {
  99. // the derived class must initialize it if it doesn't use the
  100. // non-default wxGenericFileDirButton ctor
  101. wxASSERT_MSG( m_pickerStyle != -1,
  102. "forgot to initialize m_pickerStyle?" );
  103. long filedlgstyle = 0;
  104. if ( m_pickerStyle & wxFLP_OPEN )
  105. filedlgstyle |= wxFD_OPEN;
  106. if ( m_pickerStyle & wxFLP_SAVE )
  107. filedlgstyle |= wxFD_SAVE;
  108. if ( m_pickerStyle & wxFLP_OVERWRITE_PROMPT )
  109. filedlgstyle |= wxFD_OVERWRITE_PROMPT;
  110. if ( m_pickerStyle & wxFLP_FILE_MUST_EXIST )
  111. filedlgstyle |= wxFD_FILE_MUST_EXIST;
  112. if ( m_pickerStyle & wxFLP_CHANGE_DIR )
  113. filedlgstyle |= wxFD_CHANGE_DIR;
  114. return filedlgstyle;
  115. }
  116. virtual wxDialog *CreateDialog();
  117. wxEventType GetEventType() const
  118. { return wxEVT_FILEPICKER_CHANGED; }
  119. protected:
  120. void UpdateDialogPath(wxDialog *p)
  121. { wxStaticCast(p, wxFileDialog)->SetPath(m_path); }
  122. void UpdatePathFromDialog(wxDialog *p)
  123. { m_path = wxStaticCast(p, wxFileDialog)->GetPath(); }
  124. private:
  125. DECLARE_DYNAMIC_CLASS(wxGenericFileButton)
  126. };
  127. //-----------------------------------------------------------------------------
  128. // wxGenericDirButton: a button which brings up a wxDirDialog
  129. //-----------------------------------------------------------------------------
  130. #define wxDIRBTN_DEFAULT_STYLE 0
  131. class WXDLLIMPEXP_CORE wxGenericDirButton : public wxGenericFileDirButton
  132. {
  133. public:
  134. wxGenericDirButton() {}
  135. wxGenericDirButton(wxWindow *parent,
  136. wxWindowID id,
  137. const wxString& label = wxDirPickerWidgetLabel,
  138. const wxString& path = wxEmptyString,
  139. const wxString &message = wxDirSelectorPromptStr,
  140. const wxPoint& pos = wxDefaultPosition,
  141. const wxSize& size = wxDefaultSize,
  142. long style = wxDIRBTN_DEFAULT_STYLE,
  143. const wxValidator& validator = wxDefaultValidator,
  144. const wxString& name = wxDirPickerWidgetNameStr)
  145. {
  146. Create(parent, id, label, path, message, wxEmptyString,
  147. pos, size, style, validator, name);
  148. }
  149. public: // overridable
  150. virtual long GetDialogStyle() const
  151. {
  152. long dirdlgstyle = wxDD_DEFAULT_STYLE;
  153. if ( m_pickerStyle & wxDIRP_DIR_MUST_EXIST )
  154. dirdlgstyle |= wxDD_DIR_MUST_EXIST;
  155. if ( m_pickerStyle & wxDIRP_CHANGE_DIR )
  156. dirdlgstyle |= wxDD_CHANGE_DIR;
  157. return dirdlgstyle;
  158. }
  159. virtual wxDialog *CreateDialog();
  160. wxEventType GetEventType() const
  161. { return wxEVT_DIRPICKER_CHANGED; }
  162. protected:
  163. void UpdateDialogPath(wxDialog *p)
  164. { wxStaticCast(p, wxDirDialog)->SetPath(m_path); }
  165. void UpdatePathFromDialog(wxDialog *p)
  166. { m_path = wxStaticCast(p, wxDirDialog)->GetPath(); }
  167. private:
  168. DECLARE_DYNAMIC_CLASS(wxGenericDirButton)
  169. };
  170. // old wxEVT_COMMAND_* constants
  171. //#define wxEVT_COMMAND_DIRPICKER_CHANGED wxEVT_DIRPICKER_CHANGED
  172. //#define wxEVT_COMMAND_FILEPICKER_CHANGED wxEVT_FILEPICKER_CHANGED
  173. #endif // _WX_FILEDIRPICKER_H_