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