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
[];
35 // ----------------------------------------------------------------------------
36 // wxFileDirPickerEvent: used by wxFilePickerCtrl and wxDirPickerCtrl only
37 // ----------------------------------------------------------------------------
39 class WXDLLIMPEXP_CORE wxFileDirPickerEvent
: public wxCommandEvent
42 wxFileDirPickerEvent() {}
43 wxFileDirPickerEvent(wxEventType type
, wxObject
*generator
, int id
, const wxString
&path
)
44 : wxCommandEvent(type
, id
),
47 SetEventObject(generator
);
50 wxString
GetPath() const { return m_path
; }
51 void SetPath(const wxString
&p
) { m_path
= p
; }
53 // default copy ctor, assignment operator and dtor are ok
54 virtual wxEvent
*Clone() const { return new wxFileDirPickerEvent(*this); }
59 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileDirPickerEvent
)
62 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_FILEPICKER_CHANGED
, wxFileDirPickerEvent
);
63 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE
, wxEVT_COMMAND_DIRPICKER_CHANGED
, wxFileDirPickerEvent
);
65 // ----------------------------------------------------------------------------
66 // event types and macros
67 // ----------------------------------------------------------------------------
69 typedef void (wxEvtHandler
::*wxFileDirPickerEventFunction
)(wxFileDirPickerEvent
&);
71 #define wxFileDirPickerEventHandler(func) \
72 wxEVENT_HANDLER_CAST(wxFileDirPickerEventFunction, func)
74 #define EVT_FILEPICKER_CHANGED(id, fn) \
75 wx__DECLARE_EVT1(wxEVT_COMMAND_FILEPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
76 #define EVT_DIRPICKER_CHANGED(id, fn) \
77 wx__DECLARE_EVT1(wxEVT_COMMAND_DIRPICKER_CHANGED, id, wxFileDirPickerEventHandler(fn))
79 // ----------------------------------------------------------------------------
80 // wxFileDirPickerWidgetBase: a generic abstract interface which must be
81 // implemented by controls used by wxFileDirPickerCtrlBase
82 // ----------------------------------------------------------------------------
84 class WXDLLIMPEXP_CORE wxFileDirPickerWidgetBase
87 wxFileDirPickerWidgetBase() { }
88 virtual ~wxFileDirPickerWidgetBase() { }
90 wxString
GetPath() const { return m_path
; }
91 virtual void SetPath(const wxString
&str
) { m_path
=str
; }
93 // returns the picker widget cast to wxControl
94 virtual wxControl
*AsControl() = 0;
97 virtual void UpdateDialogPath(wxDialog
*) = 0;
98 virtual void UpdatePathFromDialog(wxDialog
*) = 0;
103 // Styles which must be supported by all controls implementing wxFileDirPickerWidgetBase
104 // NB: these styles must be defined to carefully-chosen values to
105 // avoid conflicts with wxButton's styles
107 #define wxFLP_OPEN 0x0400
108 #define wxFLP_SAVE 0x0800
109 #define wxFLP_OVERWRITE_PROMPT 0x1000
110 #define wxFLP_FILE_MUST_EXIST 0x2000
111 #define wxFLP_CHANGE_DIR 0x4000
113 // NOTE: wxMULTIPLE is not supported !
116 #define wxDIRP_DIR_MUST_EXIST 0x0008
117 #define wxDIRP_CHANGE_DIR 0x0010
120 // map platform-dependent controls which implement the wxFileDirPickerWidgetBase
121 // under the name "wxFilePickerWidget" and "wxDirPickerWidget".
122 // NOTE: wxFileDirPickerCtrlBase will allocate a wx{File|Dir}PickerWidget and this
123 // requires that all classes being mapped as wx{File|Dir}PickerWidget have the
124 // same prototype for the contructor...
125 // since GTK >= 2.6, there is GtkFileButton
126 #if defined(__WXGTK26__) && !defined(__WXUNIVERSAL__)
127 #include "wx/gtk/filepicker.h"
128 #define wxFilePickerWidget wxFileButton
129 #define wxDirPickerWidget wxDirButton
131 #include "wx/generic/filepickerg.h"
132 #define wxFilePickerWidget wxGenericFileButton
133 #define wxDirPickerWidget wxGenericDirButton
138 // ----------------------------------------------------------------------------
139 // wxFileDirPickerCtrlBase
140 // ----------------------------------------------------------------------------
142 class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase
: public wxPickerBase
145 wxFileDirPickerCtrlBase() : m_bIgnoreNextTextCtrlUpdate(false) {}
148 // NB: no default values since this function will never be used
149 // directly by the user and derived classes wouldn't use them
150 bool CreateBase(wxWindow
*parent
,
152 const wxString
& path
,
153 const wxString
&message
,
154 const wxString
&wildcard
,
158 const wxValidator
& validator
,
159 const wxString
& name
);
161 public: // public API
163 wxString
GetPath() const;
164 void SetPath(const wxString
&str
);
166 public: // internal functions
168 void UpdatePickerFromTextCtrl();
169 void UpdateTextCtrlFromPicker();
171 // event handler for our picker
172 void OnFileDirChange(wxFileDirPickerEvent
&);
174 // Returns TRUE if the current path is a valid one
175 // (i.e. a valid file for a wxFilePickerWidget or a valid
176 // folder for a wxDirPickerWidget).
177 virtual bool CheckPath(const wxString
&str
) const = 0;
179 // TRUE if any textctrl change should update the current working directory
180 virtual bool IsCwdToUpdate() const = 0;
182 // Returns the event type sent by this picker
183 virtual wxEventType
GetEventType() const = 0;
185 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
) = 0;
187 // Returns the filtered value currently placed in the text control (if present).
188 virtual wxString
GetTextCtrlValue() const = 0;
191 // creates the picker control
193 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
194 const wxString
& path
,
195 const wxString
& message
,
196 const wxString
& wildcard
) = 0;
200 // true if the next UpdateTextCtrl() call is to ignore
201 bool m_bIgnoreNextTextCtrlUpdate
;
203 // m_picker object as wxFileDirPickerWidgetBase interface
204 wxFileDirPickerWidgetBase
*m_pickerIface
;
207 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
210 #if wxUSE_FILEPICKERCTRL
212 // ----------------------------------------------------------------------------
213 // wxFilePickerCtrl: platform-independent class which embeds the
214 // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
215 // used, a textctrl next to it.
216 // ----------------------------------------------------------------------------
218 #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
221 // GTK apps usually don't have a textctrl next to the picker
222 #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
224 #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
227 class WXDLLIMPEXP_CORE wxFilePickerCtrl
: public wxFileDirPickerCtrlBase
230 wxFilePickerCtrl() {}
232 wxFilePickerCtrl(wxWindow
*parent
,
234 const wxString
& path
= wxEmptyString
,
235 const wxString
& message
= wxFileSelectorPromptStr
,
236 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
237 const wxPoint
& pos
= wxDefaultPosition
,
238 const wxSize
& size
= wxDefaultSize
,
239 long style
= wxFLP_DEFAULT_STYLE
,
240 const wxValidator
& validator
= wxDefaultValidator
,
241 const wxString
& name
= wxFilePickerCtrlNameStr
)
243 Create(parent
, id
, path
, message
, wildcard
, pos
, size
, style
,
247 bool Create(wxWindow
*parent
,
249 const wxString
& path
= wxEmptyString
,
250 const wxString
& message
= wxFileSelectorPromptStr
,
251 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
252 const wxPoint
& pos
= wxDefaultPosition
,
253 const wxSize
& size
= wxDefaultSize
,
254 long style
= wxFLP_DEFAULT_STYLE
,
255 const wxValidator
& validator
= wxDefaultValidator
,
256 const wxString
& name
= wxFilePickerCtrlNameStr
);
258 void SetFileName(const wxFileName
&filename
)
259 { SetPath(filename
.GetFullPath()); }
261 wxFileName
GetFileName() const
262 { return wxFileName(GetPath()); }
266 // return true if the given path is valid for this control
267 bool CheckPath(const wxString
& path
) const;
269 // return the text control value in canonical form
270 wxString
GetTextCtrlValue() const;
272 bool IsCwdToUpdate() const
273 { return HasFlag(wxFLP_CHANGE_DIR
); }
275 wxEventType
GetEventType() const
276 { return wxEVT_COMMAND_FILEPICKER_CHANGED
; }
278 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
)
280 sender
->Connect( wxEVT_COMMAND_FILEPICKER_CHANGED
,
281 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase
::OnFileDirChange
),
288 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
289 const wxString
& path
,
290 const wxString
& message
,
291 const wxString
& wildcard
)
293 return new wxFilePickerWidget(parent
, wxID_ANY
,
294 wxGetTranslation(wxFilePickerWidgetLabel
),
295 path
, message
, wildcard
,
296 wxDefaultPosition
, wxDefaultSize
,
297 GetPickerStyle(GetWindowStyle()));
300 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
301 long GetPickerStyle(long style
) const
303 return (style
& (wxFLP_OPEN
|wxFLP_SAVE
|wxFLP_OVERWRITE_PROMPT
|
304 wxFLP_FILE_MUST_EXIST
|wxFLP_CHANGE_DIR
|wxFLP_USE_TEXTCTRL
));
308 DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl
)
311 #endif // wxUSE_FILEPICKERCTRL
314 #if wxUSE_DIRPICKERCTRL
316 // ----------------------------------------------------------------------------
317 // wxDirPickerCtrl: platform-independent class which embeds the
318 // platform-dependent wxDirPickerWidget and eventually a textctrl
319 // (see wxDIRP_USE_TEXTCTRL) next to it.
320 // ----------------------------------------------------------------------------
322 #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
325 // GTK apps usually don't have a textctrl next to the picker
326 #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
328 #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
331 class WXDLLIMPEXP_CORE wxDirPickerCtrl
: public wxFileDirPickerCtrlBase
336 wxDirPickerCtrl(wxWindow
*parent
, wxWindowID id
,
337 const wxString
& path
= wxEmptyString
,
338 const wxString
& message
= wxDirSelectorPromptStr
,
339 const wxPoint
& pos
= wxDefaultPosition
,
340 const wxSize
& size
= wxDefaultSize
,
341 long style
= wxDIRP_DEFAULT_STYLE
,
342 const wxValidator
& validator
= wxDefaultValidator
,
343 const wxString
& name
= wxDirPickerCtrlNameStr
)
345 Create(parent
, id
, path
, message
, pos
, size
, style
, validator
, name
);
348 bool Create(wxWindow
*parent
, wxWindowID id
,
349 const wxString
& path
= wxEmptyString
,
350 const wxString
& message
= wxDirSelectorPromptStr
,
351 const wxPoint
& pos
= wxDefaultPosition
,
352 const wxSize
& size
= wxDefaultSize
,
353 long style
= wxDIRP_DEFAULT_STYLE
,
354 const wxValidator
& validator
= wxDefaultValidator
,
355 const wxString
& name
= wxDirPickerCtrlNameStr
);
357 void SetDirName(const wxFileName
&dirname
)
358 { SetPath(dirname
.GetPath()); }
360 wxFileName
GetDirName() const
361 { return wxFileName
::DirName(GetPath()); }
365 bool CheckPath(const wxString
&path
) const;
367 wxString
GetTextCtrlValue() const;
369 bool IsCwdToUpdate() const
370 { return HasFlag(wxDIRP_CHANGE_DIR
); }
372 wxEventType
GetEventType() const
373 { return wxEVT_COMMAND_DIRPICKER_CHANGED
; }
375 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
)
377 sender
->Connect( wxEVT_COMMAND_DIRPICKER_CHANGED
,
378 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase
::OnFileDirChange
),
385 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
386 const wxString
& path
,
387 const wxString
& message
,
388 const wxString
& WXUNUSED(wildcard
))
390 return new wxDirPickerWidget(parent
, wxID_ANY
,
391 wxGetTranslation(wxDirPickerWidgetLabel
),
393 wxDefaultPosition
, wxDefaultSize
,
394 GetPickerStyle(GetWindowStyle()));
397 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
398 long GetPickerStyle(long style
) const
399 { return (style
& (wxDIRP_DIR_MUST_EXIST
|wxDIRP_CHANGE_DIR
|wxDIRP_USE_TEXTCTRL
)); }
402 DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl
)
405 #endif // wxUSE_DIRPICKERCTRL
407 #endif // _WX_FILEDIRPICKER_H_BASE_