filepicker.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/filepicker.h
  3. // Purpose: wxFilePickerCtrl, wxDirPickerCtrl base header
  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_BASE_
  11. #define _WX_FILEDIRPICKER_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
  14. #include "wx/pickerbase.h"
  15. #include "wx/filename.h"
  16. class WXDLLIMPEXP_FWD_CORE wxDialog;
  17. class WXDLLIMPEXP_FWD_CORE wxFileDirPickerEvent;
  18. extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerWidgetLabel[];
  19. extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerWidgetNameStr[];
  20. extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerCtrlNameStr[];
  21. extern WXDLLIMPEXP_DATA_CORE(const char) wxFileSelectorPromptStr[];
  22. extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerWidgetLabel[];
  23. extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerWidgetNameStr[];
  24. extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerCtrlNameStr[];
  25. extern WXDLLIMPEXP_DATA_CORE(const char) wxDirSelectorPromptStr[];
  26. // ----------------------------------------------------------------------------
  27. // wxFileDirPickerEvent: used by wxFilePickerCtrl and wxDirPickerCtrl only
  28. // ----------------------------------------------------------------------------
  29. class WXDLLIMPEXP_CORE wxFileDirPickerEvent : public wxCommandEvent
  30. {
  31. public:
  32. wxFileDirPickerEvent() {}
  33. wxFileDirPickerEvent(wxEventType type, wxObject *generator, int id, const wxString &path)
  34. : wxCommandEvent(type, id),
  35. m_path(path)
  36. {
  37. SetEventObject(generator);
  38. }
  39. wxString GetPath() const { return m_path; }
  40. void SetPath(const wxString &p) { m_path = p; }
  41. // default copy ctor, assignment operator and dtor are ok
  42. virtual wxEvent *Clone() const { return new wxFileDirPickerEvent(*this); }
  43. private:
  44. wxString m_path;
  45. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileDirPickerEvent)
  46. };
  47. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_FILEPICKER_CHANGED, wxFileDirPickerEvent );
  48. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_DIRPICKER_CHANGED, wxFileDirPickerEvent );
  49. // ----------------------------------------------------------------------------
  50. // event types and macros
  51. // ----------------------------------------------------------------------------
  52. typedef void (wxEvtHandler::*wxFileDirPickerEventFunction)(wxFileDirPickerEvent&);
  53. #define wxFileDirPickerEventHandler(func) \
  54. wxEVENT_HANDLER_CAST(wxFileDirPickerEventFunction, func)
  55. #define EVT_FILEPICKER_CHANGED(id, fn) \
  56. wx__DECLARE_EVT1(wxEVT_FILEPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
  57. #define EVT_DIRPICKER_CHANGED(id, fn) \
  58. wx__DECLARE_EVT1(wxEVT_DIRPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
  59. // ----------------------------------------------------------------------------
  60. // wxFileDirPickerWidgetBase: a generic abstract interface which must be
  61. // implemented by controls used by wxFileDirPickerCtrlBase
  62. // ----------------------------------------------------------------------------
  63. class WXDLLIMPEXP_CORE wxFileDirPickerWidgetBase
  64. {
  65. public:
  66. wxFileDirPickerWidgetBase() { }
  67. virtual ~wxFileDirPickerWidgetBase() { }
  68. // Path here is the name of the selected file or directory.
  69. wxString GetPath() const { return m_path; }
  70. virtual void SetPath(const wxString &str) { m_path=str; }
  71. // Set the directory to open the file browse dialog at initially.
  72. virtual void SetInitialDirectory(const wxString& dir) = 0;
  73. // returns the picker widget cast to wxControl
  74. virtual wxControl *AsControl() = 0;
  75. protected:
  76. virtual void UpdateDialogPath(wxDialog *) = 0;
  77. virtual void UpdatePathFromDialog(wxDialog *) = 0;
  78. wxString m_path;
  79. };
  80. // Styles which must be supported by all controls implementing wxFileDirPickerWidgetBase
  81. // NB: these styles must be defined to carefully-chosen values to
  82. // avoid conflicts with wxButton's styles
  83. #define wxFLP_OPEN 0x0400
  84. #define wxFLP_SAVE 0x0800
  85. #define wxFLP_OVERWRITE_PROMPT 0x1000
  86. #define wxFLP_FILE_MUST_EXIST 0x2000
  87. #define wxFLP_CHANGE_DIR 0x4000
  88. #define wxFLP_SMALL wxPB_SMALL
  89. // NOTE: wxMULTIPLE is not supported !
  90. #define wxDIRP_DIR_MUST_EXIST 0x0008
  91. #define wxDIRP_CHANGE_DIR 0x0010
  92. #define wxDIRP_SMALL wxPB_SMALL
  93. // map platform-dependent controls which implement the wxFileDirPickerWidgetBase
  94. // under the name "wxFilePickerWidget" and "wxDirPickerWidget".
  95. // NOTE: wxFileDirPickerCtrlBase will allocate a wx{File|Dir}PickerWidget and this
  96. // requires that all classes being mapped as wx{File|Dir}PickerWidget have the
  97. // same prototype for the contructor...
  98. // since GTK >= 2.6, there is GtkFileButton
  99. #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
  100. #include "wx/gtk/filepicker.h"
  101. #define wxFilePickerWidget wxFileButton
  102. #define wxDirPickerWidget wxDirButton
  103. #else
  104. #include "wx/generic/filepickerg.h"
  105. #define wxFilePickerWidget wxGenericFileButton
  106. #define wxDirPickerWidget wxGenericDirButton
  107. #endif
  108. // ----------------------------------------------------------------------------
  109. // wxFileDirPickerCtrlBase
  110. // ----------------------------------------------------------------------------
  111. class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase : public wxPickerBase
  112. {
  113. public:
  114. wxFileDirPickerCtrlBase() {}
  115. protected:
  116. // NB: no default values since this function will never be used
  117. // directly by the user and derived classes wouldn't use them
  118. bool CreateBase(wxWindow *parent,
  119. wxWindowID id,
  120. const wxString& path,
  121. const wxString &message,
  122. const wxString &wildcard,
  123. const wxPoint& pos,
  124. const wxSize& size,
  125. long style,
  126. const wxValidator& validator,
  127. const wxString& name);
  128. public: // public API
  129. wxString GetPath() const;
  130. void SetPath(const wxString &str);
  131. // Set the directory to open the file browse dialog at initially.
  132. void SetInitialDirectory(const wxString& dir)
  133. {
  134. m_pickerIface->SetInitialDirectory(dir);
  135. }
  136. public: // internal functions
  137. void UpdatePickerFromTextCtrl();
  138. void UpdateTextCtrlFromPicker();
  139. // event handler for our picker
  140. void OnFileDirChange(wxFileDirPickerEvent &);
  141. // TRUE if any textctrl change should update the current working directory
  142. virtual bool IsCwdToUpdate() const = 0;
  143. // Returns the event type sent by this picker
  144. virtual wxEventType GetEventType() const = 0;
  145. virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink ) = 0;
  146. // Returns the filtered value currently placed in the text control (if present).
  147. virtual wxString GetTextCtrlValue() const = 0;
  148. protected:
  149. // creates the picker control
  150. virtual
  151. wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
  152. const wxString& path,
  153. const wxString& message,
  154. const wxString& wildcard) = 0;
  155. protected:
  156. // m_picker object as wxFileDirPickerWidgetBase interface
  157. wxFileDirPickerWidgetBase *m_pickerIface;
  158. };
  159. #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
  160. #if wxUSE_FILEPICKERCTRL
  161. // ----------------------------------------------------------------------------
  162. // wxFilePickerCtrl: platform-independent class which embeds the
  163. // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
  164. // used, a textctrl next to it.
  165. // ----------------------------------------------------------------------------
  166. #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
  167. #ifdef __WXGTK__
  168. // GTK apps usually don't have a textctrl next to the picker
  169. #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
  170. #else
  171. #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
  172. #endif
  173. class WXDLLIMPEXP_CORE wxFilePickerCtrl : public wxFileDirPickerCtrlBase
  174. {
  175. public:
  176. wxFilePickerCtrl() {}
  177. wxFilePickerCtrl(wxWindow *parent,
  178. wxWindowID id,
  179. const wxString& path = wxEmptyString,
  180. const wxString& message = wxFileSelectorPromptStr,
  181. const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
  182. const wxPoint& pos = wxDefaultPosition,
  183. const wxSize& size = wxDefaultSize,
  184. long style = wxFLP_DEFAULT_STYLE,
  185. const wxValidator& validator = wxDefaultValidator,
  186. const wxString& name = wxFilePickerCtrlNameStr)
  187. {
  188. Create(parent, id, path, message, wildcard, pos, size, style,
  189. validator, name);
  190. }
  191. bool Create(wxWindow *parent,
  192. wxWindowID id,
  193. const wxString& path = wxEmptyString,
  194. const wxString& message = wxFileSelectorPromptStr,
  195. const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
  196. const wxPoint& pos = wxDefaultPosition,
  197. const wxSize& size = wxDefaultSize,
  198. long style = wxFLP_DEFAULT_STYLE,
  199. const wxValidator& validator = wxDefaultValidator,
  200. const wxString& name = wxFilePickerCtrlNameStr);
  201. void SetFileName(const wxFileName &filename)
  202. { SetPath(filename.GetFullPath()); }
  203. wxFileName GetFileName() const
  204. { return wxFileName(GetPath()); }
  205. public: // overrides
  206. // return the text control value in canonical form
  207. wxString GetTextCtrlValue() const;
  208. bool IsCwdToUpdate() const
  209. { return HasFlag(wxFLP_CHANGE_DIR); }
  210. wxEventType GetEventType() const
  211. { return wxEVT_FILEPICKER_CHANGED; }
  212. virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink )
  213. {
  214. sender->Connect( wxEVT_FILEPICKER_CHANGED,
  215. wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange ),
  216. NULL, eventSink );
  217. }
  218. protected:
  219. virtual
  220. wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
  221. const wxString& path,
  222. const wxString& message,
  223. const wxString& wildcard)
  224. {
  225. return new wxFilePickerWidget(parent, wxID_ANY,
  226. wxGetTranslation(wxFilePickerWidgetLabel),
  227. path, message, wildcard,
  228. wxDefaultPosition, wxDefaultSize,
  229. GetPickerStyle(GetWindowStyle()));
  230. }
  231. // extracts the style for our picker from wxFileDirPickerCtrlBase's style
  232. long GetPickerStyle(long style) const
  233. {
  234. return style & (wxFLP_OPEN |
  235. wxFLP_SAVE |
  236. wxFLP_OVERWRITE_PROMPT |
  237. wxFLP_FILE_MUST_EXIST |
  238. wxFLP_CHANGE_DIR |
  239. wxFLP_USE_TEXTCTRL |
  240. wxFLP_SMALL);
  241. }
  242. private:
  243. DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl)
  244. };
  245. #endif // wxUSE_FILEPICKERCTRL
  246. #if wxUSE_DIRPICKERCTRL
  247. // ----------------------------------------------------------------------------
  248. // wxDirPickerCtrl: platform-independent class which embeds the
  249. // platform-dependent wxDirPickerWidget and eventually a textctrl
  250. // (see wxDIRP_USE_TEXTCTRL) next to it.
  251. // ----------------------------------------------------------------------------
  252. #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
  253. #ifdef __WXGTK__
  254. // GTK apps usually don't have a textctrl next to the picker
  255. #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
  256. #else
  257. #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
  258. #endif
  259. class WXDLLIMPEXP_CORE wxDirPickerCtrl : public wxFileDirPickerCtrlBase
  260. {
  261. public:
  262. wxDirPickerCtrl() {}
  263. wxDirPickerCtrl(wxWindow *parent, wxWindowID id,
  264. const wxString& path = wxEmptyString,
  265. const wxString& message = wxDirSelectorPromptStr,
  266. const wxPoint& pos = wxDefaultPosition,
  267. const wxSize& size = wxDefaultSize,
  268. long style = wxDIRP_DEFAULT_STYLE,
  269. const wxValidator& validator = wxDefaultValidator,
  270. const wxString& name = wxDirPickerCtrlNameStr)
  271. {
  272. Create(parent, id, path, message, pos, size, style, validator, name);
  273. }
  274. bool Create(wxWindow *parent, wxWindowID id,
  275. const wxString& path = wxEmptyString,
  276. const wxString& message = wxDirSelectorPromptStr,
  277. const wxPoint& pos = wxDefaultPosition,
  278. const wxSize& size = wxDefaultSize,
  279. long style = wxDIRP_DEFAULT_STYLE,
  280. const wxValidator& validator = wxDefaultValidator,
  281. const wxString& name = wxDirPickerCtrlNameStr);
  282. void SetDirName(const wxFileName &dirname)
  283. { SetPath(dirname.GetPath()); }
  284. wxFileName GetDirName() const
  285. { return wxFileName::DirName(GetPath()); }
  286. public: // overrides
  287. wxString GetTextCtrlValue() const;
  288. bool IsCwdToUpdate() const
  289. { return HasFlag(wxDIRP_CHANGE_DIR); }
  290. wxEventType GetEventType() const
  291. { return wxEVT_DIRPICKER_CHANGED; }
  292. virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink )
  293. {
  294. sender->Connect( wxEVT_DIRPICKER_CHANGED,
  295. wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange ),
  296. NULL, eventSink );
  297. }
  298. protected:
  299. virtual
  300. wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
  301. const wxString& path,
  302. const wxString& message,
  303. const wxString& WXUNUSED(wildcard))
  304. {
  305. return new wxDirPickerWidget(parent, wxID_ANY,
  306. wxGetTranslation(wxDirPickerWidgetLabel),
  307. path, message,
  308. wxDefaultPosition, wxDefaultSize,
  309. GetPickerStyle(GetWindowStyle()));
  310. }
  311. // extracts the style for our picker from wxFileDirPickerCtrlBase's style
  312. long GetPickerStyle(long style) const
  313. {
  314. return style & (wxDIRP_DIR_MUST_EXIST |
  315. wxDIRP_CHANGE_DIR |
  316. wxDIRP_USE_TEXTCTRL |
  317. wxDIRP_SMALL);
  318. }
  319. private:
  320. DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl)
  321. };
  322. #endif // wxUSE_DIRPICKERCTRL
  323. // old wxEVT_COMMAND_* constants
  324. #define wxEVT_COMMAND_FILEPICKER_CHANGED wxEVT_FILEPICKER_CHANGED
  325. #define wxEVT_COMMAND_DIRPICKER_CHANGED wxEVT_DIRPICKER_CHANGED
  326. #endif // _WX_FILEDIRPICKER_H_BASE_