fixed crashes in wxFile/DirPickerCtrl::GetPath() due to incorrect casting in multiple...
[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 #if defined(__WXGTK26__) // since GTK >= 2.6, there is GtkFileButton
82 #include "wx/gtk/filepicker.h"
83 #define wxFilePickerWidget wxFileButton
84 #define wxDirPickerWidget wxDirButton
85 #else
86 #include "wx/generic/filepickerg.h"
87 #define wxFilePickerWidget wxGenericFileButton
88 #define wxDirPickerWidget wxGenericDirButton
89 #endif
90
91
92
93 // ----------------------------------------------------------------------------
94 // wxFileDirPickerCtrlBase
95 // ----------------------------------------------------------------------------
96
97 class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase : public wxPickerBase
98 {
99 public:
100 wxFileDirPickerCtrlBase() : m_bIgnoreNextTextCtrlUpdate(false) {}
101
102 protected:
103 // NB: no default values since this function will never be used
104 // directly by the user and derived classes wouldn't use them
105 bool CreateBase(wxWindow *parent,
106 wxWindowID id,
107 const wxString& path,
108 const wxString &message,
109 const wxString &wildcard,
110 const wxPoint& pos,
111 const wxSize& size,
112 long style,
113 const wxValidator& validator,
114 const wxString& name);
115
116 public: // public API
117
118 wxString GetPath() const;
119 void SetPath(const wxString &str);
120
121 public: // internal functions
122
123 void UpdatePickerFromTextCtrl();
124 void UpdateTextCtrlFromPicker();
125
126 // event handler for our picker
127 void OnFileDirChange(wxFileDirPickerEvent &);
128
129 // Returns TRUE if the current path is a valid one
130 // (i.e. a valid file for a wxFilePickerWidget or a valid
131 // folder for a wxDirPickerWidget).
132 virtual bool CheckPath(const wxString &str) const = 0;
133
134 // TRUE if any textctrl change should update the current working directory
135 virtual bool IsCwdToUpdate() const = 0;
136
137 // Returns the event type sent by this picker
138 virtual wxEventType GetEventType() const = 0;
139
140 // Returns the filtered value currently placed in the text control (if present).
141 virtual wxString GetTextCtrlValue() const = 0;
142
143 protected:
144 // creates the picker control
145 virtual
146 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
147 const wxString& path,
148 const wxString& message,
149 const wxString& wildcard) = 0;
150
151 protected:
152
153 // true if the next UpdateTextCtrl() call is to ignore
154 bool m_bIgnoreNextTextCtrlUpdate;
155
156 // m_picker object as wxFileDirPickerWidgetBase interface
157 wxFileDirPickerWidgetBase *m_pickerIface;
158 };
159
160 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
161
162
163 #if wxUSE_FILEPICKERCTRL
164
165 // ----------------------------------------------------------------------------
166 // wxFilePickerCtrl: platform-independent class which embeds the
167 // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
168 // used, a textctrl next to it.
169 // ----------------------------------------------------------------------------
170
171 #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
172
173 #ifdef __WXGTK__
174 // GTK apps usually don't have a textctrl next to the picker
175 #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
176 #else
177 #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
178 #endif
179
180 class WXDLLIMPEXP_CORE wxFilePickerCtrl : public wxFileDirPickerCtrlBase
181 {
182 public:
183 wxFilePickerCtrl() {}
184
185 wxFilePickerCtrl(wxWindow *parent,
186 wxWindowID id,
187 const wxString& path = wxEmptyString,
188 const wxString& message = wxFileSelectorPromptStr,
189 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
190 const wxPoint& pos = wxDefaultPosition,
191 const wxSize& size = wxDefaultSize,
192 long style = wxFLP_DEFAULT_STYLE,
193 const wxValidator& validator = wxDefaultValidator,
194 const wxString& name = wxFilePickerCtrlNameStr)
195 {
196 Create(parent, id, path, message, wildcard, pos, size, style,
197 validator, name);
198 }
199
200 bool Create(wxWindow *parent,
201 wxWindowID id,
202 const wxString& path = wxEmptyString,
203 const wxString& message = wxFileSelectorPromptStr,
204 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
205 const wxPoint& pos = wxDefaultPosition,
206 const wxSize& size = wxDefaultSize,
207 long style = wxFLP_DEFAULT_STYLE,
208 const wxValidator& validator = wxDefaultValidator,
209 const wxString& name = wxFilePickerCtrlNameStr)
210 {
211 return wxFileDirPickerCtrlBase::CreateBase(parent, id, path,
212 message, wildcard,
213 pos, size, style,
214 validator, name);
215 }
216
217
218 public: // overrides
219
220 // return true if the given path is valid for this control
221 bool CheckPath(const wxString& path) const;
222
223 // return the text control value in canonical form
224 wxString GetTextCtrlValue() const;
225
226 bool IsCwdToUpdate() const
227 { return HasFlag(wxFLP_CHANGE_DIR); }
228
229 wxEventType GetEventType() const
230 { return wxEVT_COMMAND_FILEPICKER_CHANGED; }
231
232 protected:
233 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
234 const wxString& path,
235 const wxString& message,
236 const wxString& wildcard)
237 {
238 return new wxFilePickerWidget(parent, wxID_ANY,
239 wxFilePickerWidgetLabel,
240 path, message, wildcard,
241 wxDefaultPosition, wxDefaultSize,
242 GetPickerStyle(GetWindowStyle()));
243 }
244
245 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
246 long GetPickerStyle(long style) const
247 {
248 return (style & (wxFLP_OPEN|wxFLP_SAVE|wxFLP_OVERWRITE_PROMPT|
249 wxFLP_FILE_MUST_EXIST|wxFLP_CHANGE_DIR));
250 }
251
252 private:
253 DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl)
254 };
255
256 #endif // wxUSE_FILEPICKERCTRL
257
258
259 #if wxUSE_DIRPICKERCTRL
260
261 // ----------------------------------------------------------------------------
262 // wxDirPickerCtrl: platform-independent class which embeds the
263 // platform-dependent wxDirPickerWidget and eventually a textctrl
264 // (see wxDIRP_USE_TEXTCTRL) next to it.
265 // ----------------------------------------------------------------------------
266
267 #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
268
269 #ifdef __WXGTK__
270 // GTK apps usually don't have a textctrl next to the picker
271 #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
272 #else
273 #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
274 #endif
275
276 class WXDLLIMPEXP_CORE wxDirPickerCtrl : public wxFileDirPickerCtrlBase
277 {
278 public:
279 wxDirPickerCtrl() {}
280
281 wxDirPickerCtrl(wxWindow *parent, wxWindowID id,
282 const wxString& path = wxEmptyString,
283 const wxString& message = wxDirSelectorPromptStr,
284 const wxPoint& pos = wxDefaultPosition,
285 const wxSize& size = wxDefaultSize,
286 long style = wxDIRP_DEFAULT_STYLE,
287 const wxValidator& validator = wxDefaultValidator,
288 const wxString& name = wxDirPickerCtrlNameStr)
289 {
290 Create(parent, id, path, message, pos, size, style, validator, name);
291 }
292
293 bool Create(wxWindow *parent, wxWindowID id,
294 const wxString& path = wxEmptyString,
295 const wxString& message = wxDirSelectorPromptStr,
296 const wxPoint& pos = wxDefaultPosition,
297 const wxSize& size = wxDefaultSize,
298 long style = wxDIRP_DEFAULT_STYLE,
299 const wxValidator& validator = wxDefaultValidator,
300 const wxString& name = wxDirPickerCtrlNameStr)
301 {
302 return wxFileDirPickerCtrlBase::CreateBase
303 (
304 parent, id, path, message, wxEmptyString,
305 pos, size, style, validator, name
306 );
307 }
308
309
310 public: // overrides
311
312 bool CheckPath(const wxString &path) const;
313
314 wxString GetTextCtrlValue() const;
315
316 bool IsCwdToUpdate() const
317 { return HasFlag(wxDIRP_CHANGE_DIR); }
318
319 wxEventType GetEventType() const
320 { return wxEVT_COMMAND_DIRPICKER_CHANGED; }
321
322 protected:
323 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
324 const wxString& path,
325 const wxString& message,
326 const wxString& WXUNUSED(wildcard))
327 {
328 return new wxDirPickerWidget(parent, wxID_ANY, wxDirPickerWidgetLabel,
329 path, message,
330 wxDefaultPosition, wxDefaultSize,
331 GetPickerStyle(GetWindowStyle()));
332 }
333
334 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
335 long GetPickerStyle(long style) const
336 { return (style & (wxDIRP_DIR_MUST_EXIST|wxDIRP_CHANGE_DIR)); }
337
338 private:
339 DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl)
340 };
341
342 #endif // wxUSE_DIRPICKERCTRL
343
344
345 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
346
347 // ----------------------------------------------------------------------------
348 // wxFileDirPickerEvent: used by wxFilePickerCtrl and wxDirPickerCtrl only
349 // ----------------------------------------------------------------------------
350
351 BEGIN_DECLARE_EVENT_TYPES()
352 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CORE, wxEVT_COMMAND_FILEPICKER_CHANGED, 1102)
353 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CORE, wxEVT_COMMAND_DIRPICKER_CHANGED, 1103)
354 END_DECLARE_EVENT_TYPES()
355
356 class WXDLLIMPEXP_CORE wxFileDirPickerEvent : public wxCommandEvent
357 {
358 public:
359 wxFileDirPickerEvent() {}
360 wxFileDirPickerEvent(wxEventType type, wxObject *generator, int id, const wxString &path)
361 : wxCommandEvent(type, id),
362 m_path(path)
363 {
364 SetEventObject(generator);
365 }
366
367 wxString GetPath() const { return m_path; }
368 void SetPath(const wxString &p) { m_path = p; }
369
370 // default copy ctor, assignment operator and dtor are ok
371 virtual wxEvent *Clone() const { return new wxFileDirPickerEvent(*this); }
372
373 private:
374 wxString m_path;
375
376 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileDirPickerEvent)
377 };
378
379 // ----------------------------------------------------------------------------
380 // event types and macros
381 // ----------------------------------------------------------------------------
382
383 typedef void (wxEvtHandler::*wxFileDirPickerEventFunction)(wxFileDirPickerEvent&);
384
385 #define wxFileDirPickerEventHandler(func) \
386 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxFileDirPickerEventFunction, &func)
387
388 #define EVT_FILEPICKER_CHANGED(id, fn) \
389 wx__DECLARE_EVT1(wxEVT_COMMAND_FILEPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
390 #define EVT_DIRPICKER_CHANGED(id, fn) \
391 wx__DECLARE_EVT1(wxEVT_COMMAND_DIRPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
392
393 #ifdef _WX_DEFINE_DATE_EVENTS_
394 DEFINE_EVENT_TYPE(wxEVT_COMMAND_FILEPICKER_CHANGED)
395 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DIRPICKER_CHANGED)
396
397 IMPLEMENT_DYNAMIC_CLASS(wxFileDirPickerEvent, wxCommandEvent)
398 #endif
399
400
401 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
402
403 #endif // _WX_FILEDIRPICKER_H_BASE_
404