1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/filepicker.h
3 // Purpose: wxFilePickerCtrl, wxDirPickerCtrl base header
4 // Author: Francesco Montorsi
7 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows Licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_FILEDIRPICKER_H_BASE_
13 #define _WX_FILEDIRPICKER_H_BASE_
17 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
19 #include "wx/pickerbase.h"
20 #include "wx/filename.h"
22 class WXDLLIMPEXP_FWD_CORE wxDialog
;
23 class WXDLLIMPEXP_FWD_CORE wxFileDirPickerEvent
;
25 extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerWidgetLabel
[];
26 extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerWidgetNameStr
[];
27 extern WXDLLIMPEXP_DATA_CORE(const char) wxFilePickerCtrlNameStr
[];
28 extern WXDLLIMPEXP_DATA_CORE(const char) wxFileSelectorPromptStr
[];
30 extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerWidgetLabel
[];
31 extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerWidgetNameStr
[];
32 extern WXDLLIMPEXP_DATA_CORE(const char) wxDirPickerCtrlNameStr
[];
33 extern WXDLLIMPEXP_DATA_CORE(const char) wxDirSelectorPromptStr
[];
36 // ----------------------------------------------------------------------------
37 // wxFileDirPickerWidgetBase: a generic abstract interface which must be
38 // implemented by controls used by wxFileDirPickerCtrlBase
39 // ----------------------------------------------------------------------------
41 class WXDLLIMPEXP_CORE wxFileDirPickerWidgetBase
44 wxFileDirPickerWidgetBase() { }
45 virtual ~wxFileDirPickerWidgetBase() { }
47 wxString
GetPath() const { return m_path
; }
48 virtual void SetPath(const wxString
&str
) { m_path
=str
; }
50 // returns the picker widget cast to wxControl
51 virtual wxControl
*AsControl() = 0;
54 virtual void UpdateDialogPath(wxDialog
*) = 0;
55 virtual void UpdatePathFromDialog(wxDialog
*) = 0;
60 // Styles which must be supported by all controls implementing wxFileDirPickerWidgetBase
61 // NB: these styles must be defined to carefully-chosen values to
62 // avoid conflicts with wxButton's styles
64 #define wxFLP_OPEN 0x0400
65 #define wxFLP_SAVE 0x0800
66 #define wxFLP_OVERWRITE_PROMPT 0x1000
67 #define wxFLP_FILE_MUST_EXIST 0x2000
68 #define wxFLP_CHANGE_DIR 0x4000
70 // NOTE: wxMULTIPLE is not supported !
73 #define wxDIRP_DIR_MUST_EXIST 0x0008
74 #define wxDIRP_CHANGE_DIR 0x0010
77 // map platform-dependent controls which implement the wxFileDirPickerWidgetBase
78 // under the name "wxFilePickerWidget" and "wxDirPickerWidget".
79 // NOTE: wxFileDirPickerCtrlBase will allocate a wx{File|Dir}PickerWidget and this
80 // requires that all classes being mapped as wx{File|Dir}PickerWidget have the
81 // same prototype for the contructor...
82 // since GTK >= 2.6, there is GtkFileButton
83 #if defined(__WXGTK26__) && !defined(__WXUNIVERSAL__)
84 #include "wx/gtk/filepicker.h"
85 #define wxFilePickerWidget wxFileButton
86 #define wxDirPickerWidget wxDirButton
88 #include "wx/generic/filepickerg.h"
89 #define wxFilePickerWidget wxGenericFileButton
90 #define wxDirPickerWidget wxGenericDirButton
95 // ----------------------------------------------------------------------------
96 // wxFileDirPickerCtrlBase
97 // ----------------------------------------------------------------------------
99 class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase
: public wxPickerBase
102 wxFileDirPickerCtrlBase() : m_bIgnoreNextTextCtrlUpdate(false) {}
105 // NB: no default values since this function will never be used
106 // directly by the user and derived classes wouldn't use them
107 bool CreateBase(wxWindow
*parent
,
109 const wxString
& path
,
110 const wxString
&message
,
111 const wxString
&wildcard
,
115 const wxValidator
& validator
,
116 const wxString
& name
);
118 public: // public API
120 wxString
GetPath() const;
121 void SetPath(const wxString
&str
);
123 public: // internal functions
125 void UpdatePickerFromTextCtrl();
126 void UpdateTextCtrlFromPicker();
128 // event handler for our picker
129 void OnFileDirChange(wxFileDirPickerEvent
&);
131 // Returns TRUE if the current path is a valid one
132 // (i.e. a valid file for a wxFilePickerWidget or a valid
133 // folder for a wxDirPickerWidget).
134 virtual bool CheckPath(const wxString
&str
) const = 0;
136 // TRUE if any textctrl change should update the current working directory
137 virtual bool IsCwdToUpdate() const = 0;
139 // Returns the event type sent by this picker
140 virtual wxEventType
GetEventType() const = 0;
142 // Returns the filtered value currently placed in the text control (if present).
143 virtual wxString
GetTextCtrlValue() const = 0;
146 // creates the picker control
148 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
149 const wxString
& path
,
150 const wxString
& message
,
151 const wxString
& wildcard
) = 0;
155 // true if the next UpdateTextCtrl() call is to ignore
156 bool m_bIgnoreNextTextCtrlUpdate
;
158 // m_picker object as wxFileDirPickerWidgetBase interface
159 wxFileDirPickerWidgetBase
*m_pickerIface
;
162 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
165 #if wxUSE_FILEPICKERCTRL
167 // ----------------------------------------------------------------------------
168 // wxFilePickerCtrl: platform-independent class which embeds the
169 // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
170 // used, a textctrl next to it.
171 // ----------------------------------------------------------------------------
173 #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
176 // GTK apps usually don't have a textctrl next to the picker
177 #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
179 #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
182 class WXDLLIMPEXP_CORE wxFilePickerCtrl
: public wxFileDirPickerCtrlBase
185 wxFilePickerCtrl() {}
187 wxFilePickerCtrl(wxWindow
*parent
,
189 const wxString
& path
= wxEmptyString
,
190 const wxString
& message
= wxFileSelectorPromptStr
,
191 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
192 const wxPoint
& pos
= wxDefaultPosition
,
193 const wxSize
& size
= wxDefaultSize
,
194 long style
= wxFLP_DEFAULT_STYLE
,
195 const wxValidator
& validator
= wxDefaultValidator
,
196 const wxString
& name
= wxFilePickerCtrlNameStr
)
198 Create(parent
, id
, path
, message
, wildcard
, pos
, size
, style
,
202 bool Create(wxWindow
*parent
,
204 const wxString
& path
= wxEmptyString
,
205 const wxString
& message
= wxFileSelectorPromptStr
,
206 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
207 const wxPoint
& pos
= wxDefaultPosition
,
208 const wxSize
& size
= wxDefaultSize
,
209 long style
= wxFLP_DEFAULT_STYLE
,
210 const wxValidator
& validator
= wxDefaultValidator
,
211 const wxString
& name
= wxFilePickerCtrlNameStr
)
213 return wxFileDirPickerCtrlBase::CreateBase(parent
, id
, path
,
219 void SetFileName(const wxFileName
&filename
)
220 { SetPath(filename
.GetFullPath()); }
222 wxFileName
GetFileName() const
223 { return wxFileName(GetPath()); }
227 // return true if the given path is valid for this control
228 bool CheckPath(const wxString
& path
) const;
230 // return the text control value in canonical form
231 wxString
GetTextCtrlValue() const;
233 bool IsCwdToUpdate() const
234 { return HasFlag(wxFLP_CHANGE_DIR
); }
236 wxEventType
GetEventType() const
237 { return wxEVT_COMMAND_FILEPICKER_CHANGED
; }
241 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
242 const wxString
& path
,
243 const wxString
& message
,
244 const wxString
& wildcard
)
246 return new wxFilePickerWidget(parent
, wxID_ANY
,
247 wxGetTranslation(wxFilePickerWidgetLabel
),
248 path
, message
, wildcard
,
249 wxDefaultPosition
, wxDefaultSize
,
250 GetPickerStyle(GetWindowStyle()));
253 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
254 long GetPickerStyle(long style
) const
256 return (style
& (wxFLP_OPEN
|wxFLP_SAVE
|wxFLP_OVERWRITE_PROMPT
|
257 wxFLP_FILE_MUST_EXIST
|wxFLP_CHANGE_DIR
));
261 DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl
)
264 #endif // wxUSE_FILEPICKERCTRL
267 #if wxUSE_DIRPICKERCTRL
269 // ----------------------------------------------------------------------------
270 // wxDirPickerCtrl: platform-independent class which embeds the
271 // platform-dependent wxDirPickerWidget and eventually a textctrl
272 // (see wxDIRP_USE_TEXTCTRL) next to it.
273 // ----------------------------------------------------------------------------
275 #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
278 // GTK apps usually don't have a textctrl next to the picker
279 #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
281 #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
284 class WXDLLIMPEXP_CORE wxDirPickerCtrl
: public wxFileDirPickerCtrlBase
289 wxDirPickerCtrl(wxWindow
*parent
, wxWindowID id
,
290 const wxString
& path
= wxEmptyString
,
291 const wxString
& message
= wxDirSelectorPromptStr
,
292 const wxPoint
& pos
= wxDefaultPosition
,
293 const wxSize
& size
= wxDefaultSize
,
294 long style
= wxDIRP_DEFAULT_STYLE
,
295 const wxValidator
& validator
= wxDefaultValidator
,
296 const wxString
& name
= wxDirPickerCtrlNameStr
)
298 Create(parent
, id
, path
, message
, pos
, size
, style
, validator
, name
);
301 bool Create(wxWindow
*parent
, wxWindowID id
,
302 const wxString
& path
= wxEmptyString
,
303 const wxString
& message
= wxDirSelectorPromptStr
,
304 const wxPoint
& pos
= wxDefaultPosition
,
305 const wxSize
& size
= wxDefaultSize
,
306 long style
= wxDIRP_DEFAULT_STYLE
,
307 const wxValidator
& validator
= wxDefaultValidator
,
308 const wxString
& name
= wxDirPickerCtrlNameStr
)
310 return wxFileDirPickerCtrlBase::CreateBase
312 parent
, id
, path
, message
, wxEmptyString
,
313 pos
, size
, style
, validator
, name
317 void SetDirName(const wxFileName
&dirname
)
318 { SetPath(dirname
.GetPath()); }
320 wxFileName
GetDirName() const
321 { return wxFileName::DirName(GetPath()); }
325 bool CheckPath(const wxString
&path
) const;
327 wxString
GetTextCtrlValue() const;
329 bool IsCwdToUpdate() const
330 { return HasFlag(wxDIRP_CHANGE_DIR
); }
332 wxEventType
GetEventType() const
333 { return wxEVT_COMMAND_DIRPICKER_CHANGED
; }
337 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
338 const wxString
& path
,
339 const wxString
& message
,
340 const wxString
& WXUNUSED(wildcard
))
342 return new wxDirPickerWidget(parent
, wxID_ANY
,
343 wxGetTranslation(wxDirPickerWidgetLabel
),
345 wxDefaultPosition
, wxDefaultSize
,
346 GetPickerStyle(GetWindowStyle()));
349 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
350 long GetPickerStyle(long style
) const
351 { return (style
& (wxDIRP_DIR_MUST_EXIST
|wxDIRP_CHANGE_DIR
)); }
354 DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl
)
357 #endif // wxUSE_DIRPICKERCTRL
360 #if wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
362 // ----------------------------------------------------------------------------
363 // wxFileDirPickerEvent: used by wxFilePickerCtrl and wxDirPickerCtrl only
364 // ----------------------------------------------------------------------------
366 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_FILEPICKER_CHANGED
;
367 extern WXDLLIMPEXP_CORE
const wxEventType wxEVT_COMMAND_DIRPICKER_CHANGED
;
369 class WXDLLIMPEXP_CORE wxFileDirPickerEvent
: public wxCommandEvent
372 wxFileDirPickerEvent() {}
373 wxFileDirPickerEvent(wxEventType type
, wxObject
*generator
, int id
, const wxString
&path
)
374 : wxCommandEvent(type
, id
),
377 SetEventObject(generator
);
380 wxString
GetPath() const { return m_path
; }
381 void SetPath(const wxString
&p
) { m_path
= p
; }
383 // default copy ctor, assignment operator and dtor are ok
384 virtual wxEvent
*Clone() const { return new wxFileDirPickerEvent(*this); }
389 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileDirPickerEvent
)
392 // ----------------------------------------------------------------------------
393 // event types and macros
394 // ----------------------------------------------------------------------------
396 typedef void (wxEvtHandler::*wxFileDirPickerEventFunction
)(wxFileDirPickerEvent
&);
398 #define wxFileDirPickerEventHandler(func) \
399 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxFileDirPickerEventFunction, &func)
401 #define EVT_FILEPICKER_CHANGED(id, fn) \
402 wx__DECLARE_EVT1(wxEVT_COMMAND_FILEPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
403 #define EVT_DIRPICKER_CHANGED(id, fn) \
404 wx__DECLARE_EVT1(wxEVT_COMMAND_DIRPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
407 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
409 #endif // _WX_FILEDIRPICKER_H_BASE_