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 // Path here is the name of the selected file or directory.
91 wxString
GetPath() const { return m_path
; }
92 virtual void SetPath(const wxString
&str
) { m_path
=str
; }
94 // Set the directory to open the file browse dialog at initially.
95 virtual void SetInitialDirectory(const wxString
& dir
) = 0;
97 // returns the picker widget cast to wxControl
98 virtual wxControl
*AsControl() = 0;
101 virtual void UpdateDialogPath(wxDialog
*) = 0;
102 virtual void UpdatePathFromDialog(wxDialog
*) = 0;
107 // Styles which must be supported by all controls implementing wxFileDirPickerWidgetBase
108 // NB: these styles must be defined to carefully-chosen values to
109 // avoid conflicts with wxButton's styles
111 #define wxFLP_OPEN 0x0400
112 #define wxFLP_SAVE 0x0800
113 #define wxFLP_OVERWRITE_PROMPT 0x1000
114 #define wxFLP_FILE_MUST_EXIST 0x2000
115 #define wxFLP_CHANGE_DIR 0x4000
116 #define wxFLP_SMALL wxPB_SMALL
118 // NOTE: wxMULTIPLE is not supported !
121 #define wxDIRP_DIR_MUST_EXIST 0x0008
122 #define wxDIRP_CHANGE_DIR 0x0010
123 #define wxDIRP_SMALL wxPB_SMALL
126 // map platform-dependent controls which implement the wxFileDirPickerWidgetBase
127 // under the name "wxFilePickerWidget" and "wxDirPickerWidget".
128 // NOTE: wxFileDirPickerCtrlBase will allocate a wx{File|Dir}PickerWidget and this
129 // requires that all classes being mapped as wx{File|Dir}PickerWidget have the
130 // same prototype for the contructor...
131 // since GTK >= 2.6, there is GtkFileButton
132 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
133 #include "wx/gtk/filepicker.h"
134 #define wxFilePickerWidget wxFileButton
135 #define wxDirPickerWidget wxDirButton
137 #include "wx/generic/filepickerg.h"
138 #define wxFilePickerWidget wxGenericFileButton
139 #define wxDirPickerWidget wxGenericDirButton
144 // ----------------------------------------------------------------------------
145 // wxFileDirPickerCtrlBase
146 // ----------------------------------------------------------------------------
148 class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase
: public wxPickerBase
151 wxFileDirPickerCtrlBase() {}
154 // NB: no default values since this function will never be used
155 // directly by the user and derived classes wouldn't use them
156 bool CreateBase(wxWindow
*parent
,
158 const wxString
& path
,
159 const wxString
&message
,
160 const wxString
&wildcard
,
164 const wxValidator
& validator
,
165 const wxString
& name
);
167 public: // public API
169 wxString
GetPath() const;
170 void SetPath(const wxString
&str
);
172 // Set the directory to open the file browse dialog at initially.
173 void SetInitialDirectory(const wxString
& dir
)
175 m_pickerIface
->SetInitialDirectory(dir
);
178 public: // internal functions
180 void UpdatePickerFromTextCtrl();
181 void UpdateTextCtrlFromPicker();
183 // event handler for our picker
184 void OnFileDirChange(wxFileDirPickerEvent
&);
186 // Returns TRUE if the current path is a valid one
187 // (i.e. a valid file for a wxFilePickerWidget or a valid
188 // folder for a wxDirPickerWidget).
189 virtual bool CheckPath(const wxString
&str
) const = 0;
191 // TRUE if any textctrl change should update the current working directory
192 virtual bool IsCwdToUpdate() const = 0;
194 // Returns the event type sent by this picker
195 virtual wxEventType
GetEventType() const = 0;
197 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
) = 0;
199 // Returns the filtered value currently placed in the text control (if present).
200 virtual wxString
GetTextCtrlValue() const = 0;
203 // creates the picker control
205 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
206 const wxString
& path
,
207 const wxString
& message
,
208 const wxString
& wildcard
) = 0;
212 // m_picker object as wxFileDirPickerWidgetBase interface
213 wxFileDirPickerWidgetBase
*m_pickerIface
;
216 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
219 #if wxUSE_FILEPICKERCTRL
221 // ----------------------------------------------------------------------------
222 // wxFilePickerCtrl: platform-independent class which embeds the
223 // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
224 // used, a textctrl next to it.
225 // ----------------------------------------------------------------------------
227 #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
230 // GTK apps usually don't have a textctrl next to the picker
231 #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
233 #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
236 class WXDLLIMPEXP_CORE wxFilePickerCtrl
: public wxFileDirPickerCtrlBase
239 wxFilePickerCtrl() {}
241 wxFilePickerCtrl(wxWindow
*parent
,
243 const wxString
& path
= wxEmptyString
,
244 const wxString
& message
= wxFileSelectorPromptStr
,
245 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
246 const wxPoint
& pos
= wxDefaultPosition
,
247 const wxSize
& size
= wxDefaultSize
,
248 long style
= wxFLP_DEFAULT_STYLE
,
249 const wxValidator
& validator
= wxDefaultValidator
,
250 const wxString
& name
= wxFilePickerCtrlNameStr
)
252 Create(parent
, id
, path
, message
, wildcard
, pos
, size
, style
,
256 bool Create(wxWindow
*parent
,
258 const wxString
& path
= wxEmptyString
,
259 const wxString
& message
= wxFileSelectorPromptStr
,
260 const wxString
& wildcard
= wxFileSelectorDefaultWildcardStr
,
261 const wxPoint
& pos
= wxDefaultPosition
,
262 const wxSize
& size
= wxDefaultSize
,
263 long style
= wxFLP_DEFAULT_STYLE
,
264 const wxValidator
& validator
= wxDefaultValidator
,
265 const wxString
& name
= wxFilePickerCtrlNameStr
);
267 void SetFileName(const wxFileName
&filename
)
268 { SetPath(filename
.GetFullPath()); }
270 wxFileName
GetFileName() const
271 { return wxFileName(GetPath()); }
275 // return true if the given path is valid for this control
276 bool CheckPath(const wxString
& path
) const;
278 // return the text control value in canonical form
279 wxString
GetTextCtrlValue() const;
281 bool IsCwdToUpdate() const
282 { return HasFlag(wxFLP_CHANGE_DIR
); }
284 wxEventType
GetEventType() const
285 { return wxEVT_COMMAND_FILEPICKER_CHANGED
; }
287 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
)
289 sender
->Connect( wxEVT_COMMAND_FILEPICKER_CHANGED
,
290 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange
),
297 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
298 const wxString
& path
,
299 const wxString
& message
,
300 const wxString
& wildcard
)
302 return new wxFilePickerWidget(parent
, wxID_ANY
,
303 wxGetTranslation(wxFilePickerWidgetLabel
),
304 path
, message
, wildcard
,
305 wxDefaultPosition
, wxDefaultSize
,
306 GetPickerStyle(GetWindowStyle()));
309 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
310 long GetPickerStyle(long style
) const
312 return style
& (wxFLP_OPEN
|
314 wxFLP_OVERWRITE_PROMPT
|
315 wxFLP_FILE_MUST_EXIST
|
322 DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl
)
325 #endif // wxUSE_FILEPICKERCTRL
328 #if wxUSE_DIRPICKERCTRL
330 // ----------------------------------------------------------------------------
331 // wxDirPickerCtrl: platform-independent class which embeds the
332 // platform-dependent wxDirPickerWidget and eventually a textctrl
333 // (see wxDIRP_USE_TEXTCTRL) next to it.
334 // ----------------------------------------------------------------------------
336 #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
339 // GTK apps usually don't have a textctrl next to the picker
340 #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
342 #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
345 class WXDLLIMPEXP_CORE wxDirPickerCtrl
: public wxFileDirPickerCtrlBase
350 wxDirPickerCtrl(wxWindow
*parent
, wxWindowID id
,
351 const wxString
& path
= wxEmptyString
,
352 const wxString
& message
= wxDirSelectorPromptStr
,
353 const wxPoint
& pos
= wxDefaultPosition
,
354 const wxSize
& size
= wxDefaultSize
,
355 long style
= wxDIRP_DEFAULT_STYLE
,
356 const wxValidator
& validator
= wxDefaultValidator
,
357 const wxString
& name
= wxDirPickerCtrlNameStr
)
359 Create(parent
, id
, path
, message
, pos
, size
, style
, validator
, name
);
362 bool Create(wxWindow
*parent
, wxWindowID id
,
363 const wxString
& path
= wxEmptyString
,
364 const wxString
& message
= wxDirSelectorPromptStr
,
365 const wxPoint
& pos
= wxDefaultPosition
,
366 const wxSize
& size
= wxDefaultSize
,
367 long style
= wxDIRP_DEFAULT_STYLE
,
368 const wxValidator
& validator
= wxDefaultValidator
,
369 const wxString
& name
= wxDirPickerCtrlNameStr
);
371 void SetDirName(const wxFileName
&dirname
)
372 { SetPath(dirname
.GetPath()); }
374 wxFileName
GetDirName() const
375 { return wxFileName::DirName(GetPath()); }
379 bool CheckPath(const wxString
&path
) const;
381 wxString
GetTextCtrlValue() const;
383 bool IsCwdToUpdate() const
384 { return HasFlag(wxDIRP_CHANGE_DIR
); }
386 wxEventType
GetEventType() const
387 { return wxEVT_COMMAND_DIRPICKER_CHANGED
; }
389 virtual void DoConnect( wxControl
*sender
, wxFileDirPickerCtrlBase
*eventSink
)
391 sender
->Connect( wxEVT_COMMAND_DIRPICKER_CHANGED
,
392 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange
),
399 wxFileDirPickerWidgetBase
*CreatePicker(wxWindow
*parent
,
400 const wxString
& path
,
401 const wxString
& message
,
402 const wxString
& WXUNUSED(wildcard
))
404 return new wxDirPickerWidget(parent
, wxID_ANY
,
405 wxGetTranslation(wxDirPickerWidgetLabel
),
407 wxDefaultPosition
, wxDefaultSize
,
408 GetPickerStyle(GetWindowStyle()));
411 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
412 long GetPickerStyle(long style
) const
414 return style
& (wxDIRP_DIR_MUST_EXIST
|
416 wxDIRP_USE_TEXTCTRL
|
421 DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl
)
424 #endif // wxUSE_DIRPICKERCTRL
426 #endif // _WX_FILEDIRPICKER_H_BASE_