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