Added wxFilePickerCtrl::SetInitialDirectory().
[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 #include "wx/filename.h"
21
22 class WXDLLIMPEXP_FWD_CORE wxDialog;
23 class WXDLLIMPEXP_FWD_CORE wxFileDirPickerEvent;
24
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[];
29
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[];
34
35 // ----------------------------------------------------------------------------
36 // wxFileDirPickerEvent: used by wxFilePickerCtrl and wxDirPickerCtrl only
37 // ----------------------------------------------------------------------------
38
39 class WXDLLIMPEXP_CORE wxFileDirPickerEvent : public wxCommandEvent
40 {
41 public:
42 wxFileDirPickerEvent() {}
43 wxFileDirPickerEvent(wxEventType type, wxObject *generator, int id, const wxString &path)
44 : wxCommandEvent(type, id),
45 m_path(path)
46 {
47 SetEventObject(generator);
48 }
49
50 wxString GetPath() const { return m_path; }
51 void SetPath(const wxString &p) { m_path = p; }
52
53 // default copy ctor, assignment operator and dtor are ok
54 virtual wxEvent *Clone() const { return new wxFileDirPickerEvent(*this); }
55
56 private:
57 wxString m_path;
58
59 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileDirPickerEvent)
60 };
61
62 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COMMAND_FILEPICKER_CHANGED, wxFileDirPickerEvent );
63 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COMMAND_DIRPICKER_CHANGED, wxFileDirPickerEvent );
64
65 // ----------------------------------------------------------------------------
66 // event types and macros
67 // ----------------------------------------------------------------------------
68
69 typedef void (wxEvtHandler::*wxFileDirPickerEventFunction)(wxFileDirPickerEvent&);
70
71 #define wxFileDirPickerEventHandler(func) \
72 wxEVENT_HANDLER_CAST(wxFileDirPickerEventFunction, func)
73
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))
78
79 // ----------------------------------------------------------------------------
80 // wxFileDirPickerWidgetBase: a generic abstract interface which must be
81 // implemented by controls used by wxFileDirPickerCtrlBase
82 // ----------------------------------------------------------------------------
83
84 class WXDLLIMPEXP_CORE wxFileDirPickerWidgetBase
85 {
86 public:
87 wxFileDirPickerWidgetBase() { }
88 virtual ~wxFileDirPickerWidgetBase() { }
89
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; }
93
94 // Set the directory to open the file browse dialog at initially.
95 virtual void SetInitialDirectory(const wxString& dir) = 0;
96
97 // returns the picker widget cast to wxControl
98 virtual wxControl *AsControl() = 0;
99
100 protected:
101 virtual void UpdateDialogPath(wxDialog *) = 0;
102 virtual void UpdatePathFromDialog(wxDialog *) = 0;
103
104 wxString m_path;
105 };
106
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
110
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
117
118 // NOTE: wxMULTIPLE is not supported !
119
120
121 #define wxDIRP_DIR_MUST_EXIST 0x0008
122 #define wxDIRP_CHANGE_DIR 0x0010
123 #define wxDIRP_SMALL wxPB_SMALL
124
125
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(__WXGTK26__) && !defined(__WXUNIVERSAL__)
133 #include "wx/gtk/filepicker.h"
134 #define wxFilePickerWidget wxFileButton
135 #define wxDirPickerWidget wxDirButton
136 #else
137 #include "wx/generic/filepickerg.h"
138 #define wxFilePickerWidget wxGenericFileButton
139 #define wxDirPickerWidget wxGenericDirButton
140 #endif
141
142
143
144 // ----------------------------------------------------------------------------
145 // wxFileDirPickerCtrlBase
146 // ----------------------------------------------------------------------------
147
148 class WXDLLIMPEXP_CORE wxFileDirPickerCtrlBase : public wxPickerBase
149 {
150 public:
151 wxFileDirPickerCtrlBase() : m_bIgnoreNextTextCtrlUpdate(false) {}
152
153 protected:
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,
157 wxWindowID id,
158 const wxString& path,
159 const wxString &message,
160 const wxString &wildcard,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxValidator& validator,
165 const wxString& name);
166
167 public: // public API
168
169 wxString GetPath() const;
170 void SetPath(const wxString &str);
171
172 // Set the directory to open the file browse dialog at initially.
173 void SetInitialDirectory(const wxString& dir)
174 {
175 m_pickerIface->SetInitialDirectory(dir);
176 }
177
178 public: // internal functions
179
180 void UpdatePickerFromTextCtrl();
181 void UpdateTextCtrlFromPicker();
182
183 // event handler for our picker
184 void OnFileDirChange(wxFileDirPickerEvent &);
185
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;
190
191 // TRUE if any textctrl change should update the current working directory
192 virtual bool IsCwdToUpdate() const = 0;
193
194 // Returns the event type sent by this picker
195 virtual wxEventType GetEventType() const = 0;
196
197 virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink ) = 0;
198
199 // Returns the filtered value currently placed in the text control (if present).
200 virtual wxString GetTextCtrlValue() const = 0;
201
202 protected:
203 // creates the picker control
204 virtual
205 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
206 const wxString& path,
207 const wxString& message,
208 const wxString& wildcard) = 0;
209
210 protected:
211
212 // true if the next UpdateTextCtrl() call is to ignore
213 bool m_bIgnoreNextTextCtrlUpdate;
214
215 // m_picker object as wxFileDirPickerWidgetBase interface
216 wxFileDirPickerWidgetBase *m_pickerIface;
217 };
218
219 #endif // wxUSE_FILEPICKERCTRL || wxUSE_DIRPICKERCTRL
220
221
222 #if wxUSE_FILEPICKERCTRL
223
224 // ----------------------------------------------------------------------------
225 // wxFilePickerCtrl: platform-independent class which embeds the
226 // platform-dependent wxFilePickerWidget and, if wxFLP_USE_TEXTCTRL style is
227 // used, a textctrl next to it.
228 // ----------------------------------------------------------------------------
229
230 #define wxFLP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
231
232 #ifdef __WXGTK__
233 // GTK apps usually don't have a textctrl next to the picker
234 #define wxFLP_DEFAULT_STYLE (wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
235 #else
236 #define wxFLP_DEFAULT_STYLE (wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST)
237 #endif
238
239 class WXDLLIMPEXP_CORE wxFilePickerCtrl : public wxFileDirPickerCtrlBase
240 {
241 public:
242 wxFilePickerCtrl() {}
243
244 wxFilePickerCtrl(wxWindow *parent,
245 wxWindowID id,
246 const wxString& path = wxEmptyString,
247 const wxString& message = wxFileSelectorPromptStr,
248 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
249 const wxPoint& pos = wxDefaultPosition,
250 const wxSize& size = wxDefaultSize,
251 long style = wxFLP_DEFAULT_STYLE,
252 const wxValidator& validator = wxDefaultValidator,
253 const wxString& name = wxFilePickerCtrlNameStr)
254 {
255 Create(parent, id, path, message, wildcard, pos, size, style,
256 validator, name);
257 }
258
259 bool Create(wxWindow *parent,
260 wxWindowID id,
261 const wxString& path = wxEmptyString,
262 const wxString& message = wxFileSelectorPromptStr,
263 const wxString& wildcard = wxFileSelectorDefaultWildcardStr,
264 const wxPoint& pos = wxDefaultPosition,
265 const wxSize& size = wxDefaultSize,
266 long style = wxFLP_DEFAULT_STYLE,
267 const wxValidator& validator = wxDefaultValidator,
268 const wxString& name = wxFilePickerCtrlNameStr);
269
270 void SetFileName(const wxFileName &filename)
271 { SetPath(filename.GetFullPath()); }
272
273 wxFileName GetFileName() const
274 { return wxFileName(GetPath()); }
275
276 public: // overrides
277
278 // return true if the given path is valid for this control
279 bool CheckPath(const wxString& path) const;
280
281 // return the text control value in canonical form
282 wxString GetTextCtrlValue() const;
283
284 bool IsCwdToUpdate() const
285 { return HasFlag(wxFLP_CHANGE_DIR); }
286
287 wxEventType GetEventType() const
288 { return wxEVT_COMMAND_FILEPICKER_CHANGED; }
289
290 virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink )
291 {
292 sender->Connect( wxEVT_COMMAND_FILEPICKER_CHANGED,
293 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange ),
294 NULL, eventSink );
295 }
296
297
298 protected:
299 virtual
300 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
301 const wxString& path,
302 const wxString& message,
303 const wxString& wildcard)
304 {
305 return new wxFilePickerWidget(parent, wxID_ANY,
306 wxGetTranslation(wxFilePickerWidgetLabel),
307 path, message, wildcard,
308 wxDefaultPosition, wxDefaultSize,
309 GetPickerStyle(GetWindowStyle()));
310 }
311
312 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
313 long GetPickerStyle(long style) const
314 {
315 return style & (wxFLP_OPEN |
316 wxFLP_SAVE |
317 wxFLP_OVERWRITE_PROMPT |
318 wxFLP_FILE_MUST_EXIST |
319 wxFLP_CHANGE_DIR |
320 wxFLP_USE_TEXTCTRL |
321 wxFLP_SMALL);
322 }
323
324 private:
325 DECLARE_DYNAMIC_CLASS(wxFilePickerCtrl)
326 };
327
328 #endif // wxUSE_FILEPICKERCTRL
329
330
331 #if wxUSE_DIRPICKERCTRL
332
333 // ----------------------------------------------------------------------------
334 // wxDirPickerCtrl: platform-independent class which embeds the
335 // platform-dependent wxDirPickerWidget and eventually a textctrl
336 // (see wxDIRP_USE_TEXTCTRL) next to it.
337 // ----------------------------------------------------------------------------
338
339 #define wxDIRP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
340
341 #ifdef __WXGTK__
342 // GTK apps usually don't have a textctrl next to the picker
343 #define wxDIRP_DEFAULT_STYLE (wxDIRP_DIR_MUST_EXIST)
344 #else
345 #define wxDIRP_DEFAULT_STYLE (wxDIRP_USE_TEXTCTRL|wxDIRP_DIR_MUST_EXIST)
346 #endif
347
348 class WXDLLIMPEXP_CORE wxDirPickerCtrl : public wxFileDirPickerCtrlBase
349 {
350 public:
351 wxDirPickerCtrl() {}
352
353 wxDirPickerCtrl(wxWindow *parent, wxWindowID id,
354 const wxString& path = wxEmptyString,
355 const wxString& message = wxDirSelectorPromptStr,
356 const wxPoint& pos = wxDefaultPosition,
357 const wxSize& size = wxDefaultSize,
358 long style = wxDIRP_DEFAULT_STYLE,
359 const wxValidator& validator = wxDefaultValidator,
360 const wxString& name = wxDirPickerCtrlNameStr)
361 {
362 Create(parent, id, path, message, pos, size, style, validator, name);
363 }
364
365 bool Create(wxWindow *parent, wxWindowID id,
366 const wxString& path = wxEmptyString,
367 const wxString& message = wxDirSelectorPromptStr,
368 const wxPoint& pos = wxDefaultPosition,
369 const wxSize& size = wxDefaultSize,
370 long style = wxDIRP_DEFAULT_STYLE,
371 const wxValidator& validator = wxDefaultValidator,
372 const wxString& name = wxDirPickerCtrlNameStr);
373
374 void SetDirName(const wxFileName &dirname)
375 { SetPath(dirname.GetPath()); }
376
377 wxFileName GetDirName() const
378 { return wxFileName::DirName(GetPath()); }
379
380 public: // overrides
381
382 bool CheckPath(const wxString &path) const;
383
384 wxString GetTextCtrlValue() const;
385
386 bool IsCwdToUpdate() const
387 { return HasFlag(wxDIRP_CHANGE_DIR); }
388
389 wxEventType GetEventType() const
390 { return wxEVT_COMMAND_DIRPICKER_CHANGED; }
391
392 virtual void DoConnect( wxControl *sender, wxFileDirPickerCtrlBase *eventSink )
393 {
394 sender->Connect( wxEVT_COMMAND_DIRPICKER_CHANGED,
395 wxFileDirPickerEventHandler( wxFileDirPickerCtrlBase::OnFileDirChange ),
396 NULL, eventSink );
397 }
398
399
400 protected:
401 virtual
402 wxFileDirPickerWidgetBase *CreatePicker(wxWindow *parent,
403 const wxString& path,
404 const wxString& message,
405 const wxString& WXUNUSED(wildcard))
406 {
407 return new wxDirPickerWidget(parent, wxID_ANY,
408 wxGetTranslation(wxDirPickerWidgetLabel),
409 path, message,
410 wxDefaultPosition, wxDefaultSize,
411 GetPickerStyle(GetWindowStyle()));
412 }
413
414 // extracts the style for our picker from wxFileDirPickerCtrlBase's style
415 long GetPickerStyle(long style) const
416 {
417 return style & (wxDIRP_DIR_MUST_EXIST |
418 wxDIRP_CHANGE_DIR |
419 wxDIRP_USE_TEXTCTRL |
420 wxDIRP_SMALL);
421 }
422
423 private:
424 DECLARE_DYNAMIC_CLASS(wxDirPickerCtrl)
425 };
426
427 #endif // wxUSE_DIRPICKERCTRL
428
429 #endif // _WX_FILEDIRPICKER_H_BASE_
430