added multiple selections support to wxDirCtrl (closes #10830)
[wxWidgets.git] / include / wx / generic / dirctrlg.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dirctrlg.h
3 // Purpose: wxGenericDirCtrl class
4 // Builds on wxDirCtrl class written by Robert Roebling for the
5 // wxFile application, modified by Harm van der Heijden.
6 // Further modified for Windows.
7 // Author: Robert Roebling, Harm van der Heijden, Julian Smart et al
8 // Modified by:
9 // Created: 21/3/2000
10 // RCS-ID: $Id$
11 // Copyright: (c) Robert Roebling, Harm van der Heijden, Julian Smart
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
14
15 #ifndef _WX_DIRCTRL_H_
16 #define _WX_DIRCTRL_H_
17
18 #if wxUSE_DIRDLG || wxUSE_FILEDLG
19 #include "wx/imaglist.h"
20 #endif
21
22 #if wxUSE_DIRDLG
23
24 #include "wx/treectrl.h"
25 #include "wx/dialog.h"
26 #include "wx/dirdlg.h"
27 #include "wx/choice.h"
28
29 //-----------------------------------------------------------------------------
30 // classes
31 //-----------------------------------------------------------------------------
32
33 class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
34 class WXDLLIMPEXP_FWD_CORE wxImageList;
35 class WXDLLIMPEXP_FWD_BASE wxHashTable;
36
37 //-----------------------------------------------------------------------------
38 // Extra styles for wxGenericDirCtrl
39 //-----------------------------------------------------------------------------
40
41 enum
42 {
43 // Only allow directory viewing/selection, no files
44 wxDIRCTRL_DIR_ONLY = 0x0010,
45 // When setting the default path, select the first file in the directory
46 wxDIRCTRL_SELECT_FIRST = 0x0020,
47 #if WXWIN_COMPATIBILITY_2_8
48 // Unused, for compatibility only
49 wxDIRCTRL_SHOW_FILTERS = 0x0040,
50 #endif // WXWIN_COMPATIBILITY_2_8
51 // Use 3D borders on internal controls
52 wxDIRCTRL_3D_INTERNAL = 0x0080,
53 // Editable labels
54 wxDIRCTRL_EDIT_LABELS = 0x0100,
55 // Allow multiple selection
56 wxDIRCTRL_MULTIPLE = 0x0200
57 };
58
59 //-----------------------------------------------------------------------------
60 // wxDirItemData
61 //-----------------------------------------------------------------------------
62
63 class WXDLLIMPEXP_CORE wxDirItemData : public wxTreeItemData
64 {
65 public:
66 wxDirItemData(const wxString& path, const wxString& name, bool isDir);
67 virtual ~wxDirItemData(){}
68 void SetNewDirName(const wxString& path);
69
70 bool HasSubDirs() const;
71 bool HasFiles(const wxString& spec = wxEmptyString) const;
72
73 wxString m_path, m_name;
74 bool m_isHidden;
75 bool m_isExpanded;
76 bool m_isDir;
77 };
78
79 //-----------------------------------------------------------------------------
80 // wxDirCtrl
81 //-----------------------------------------------------------------------------
82
83 class WXDLLIMPEXP_FWD_CORE wxDirFilterListCtrl;
84
85 class WXDLLIMPEXP_CORE wxGenericDirCtrl: public wxControl
86 {
87 public:
88 wxGenericDirCtrl();
89 wxGenericDirCtrl(wxWindow *parent, const wxWindowID id = wxID_ANY,
90 const wxString &dir = wxDirDialogDefaultFolderStr,
91 const wxPoint& pos = wxDefaultPosition,
92 const wxSize& size = wxDefaultSize,
93 long style = wxDIRCTRL_3D_INTERNAL,
94 const wxString& filter = wxEmptyString,
95 int defaultFilter = 0,
96 const wxString& name = wxTreeCtrlNameStr )
97 {
98 Init();
99 Create(parent, id, dir, pos, size, style, filter, defaultFilter, name);
100 }
101
102 bool Create(wxWindow *parent, const wxWindowID id = wxID_ANY,
103 const wxString &dir = wxDirDialogDefaultFolderStr,
104 const wxPoint& pos = wxDefaultPosition,
105 const wxSize& size = wxDefaultSize,
106 long style = wxDIRCTRL_3D_INTERNAL,
107 const wxString& filter = wxEmptyString,
108 int defaultFilter = 0,
109 const wxString& name = wxTreeCtrlNameStr );
110
111 virtual void Init();
112
113 virtual ~wxGenericDirCtrl();
114
115 void OnExpandItem(wxTreeEvent &event );
116 void OnCollapseItem(wxTreeEvent &event );
117 void OnBeginEditItem(wxTreeEvent &event );
118 void OnEndEditItem(wxTreeEvent &event );
119 void OnSize(wxSizeEvent &event );
120
121 // Try to expand as much of the given path as possible.
122 virtual bool ExpandPath(const wxString& path);
123 // collapse the path
124 virtual bool CollapsePath(const wxString& path);
125
126 // Accessors
127
128 virtual inline wxString GetDefaultPath() const { return m_defaultPath; }
129 virtual void SetDefaultPath(const wxString& path) { m_defaultPath = path; }
130
131 // Get dir or filename
132 virtual wxString GetPath() const;
133 virtual void GetPaths(wxArrayString& paths) const;
134
135 // Get selected filename path only (else empty string).
136 // I.e. don't count a directory as a selection
137 virtual wxString GetFilePath() const;
138 virtual void GetFilePaths(wxArrayString& paths) const;
139 virtual void SetPath(const wxString& path);
140
141 virtual void SelectPath(const wxString& path, bool select = true);
142 virtual void SelectPaths(const wxArrayString& paths);
143
144 virtual void ShowHidden( bool show );
145 virtual bool GetShowHidden() { return m_showHidden; }
146
147 virtual wxString GetFilter() const { return m_filter; }
148 virtual void SetFilter(const wxString& filter);
149
150 virtual int GetFilterIndex() const { return m_currentFilter; }
151 virtual void SetFilterIndex(int n);
152
153 virtual wxTreeItemId GetRootId() { return m_rootId; }
154
155 virtual wxTreeCtrl* GetTreeCtrl() const { return m_treeCtrl; }
156 virtual wxDirFilterListCtrl* GetFilterListCtrl() const { return m_filterListCtrl; }
157
158 virtual void UnselectAll();
159
160 // Helper
161 virtual void SetupSections();
162
163 // Find the child that matches the first part of 'path'.
164 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
165 // then the child for /usr is returned.
166 // If the path string has been used (we're at the leaf), done is set to true
167 virtual wxTreeItemId FindChild(wxTreeItemId parentId, const wxString& path, bool& done);
168
169 // Resize the components of the control
170 virtual void DoResize();
171
172 // Collapse & expand the tree, thus re-creating it from scratch:
173 virtual void ReCreateTree();
174
175 // Collapse the entire tree
176 virtual void CollapseTree();
177
178 // overridden base class methods
179 virtual void SetFocus();
180
181 protected:
182 virtual void ExpandRoot();
183 virtual void ExpandDir(wxTreeItemId parentId);
184 virtual void CollapseDir(wxTreeItemId parentId);
185 virtual const wxTreeItemId AddSection(const wxString& path, const wxString& name, int imageId = 0);
186 virtual wxTreeItemId AppendItem (const wxTreeItemId & parent,
187 const wxString & text,
188 int image = -1, int selectedImage = -1,
189 wxTreeItemData * data = NULL);
190 //void FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames);
191 virtual wxTreeCtrl* CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle);
192
193 // Extract description and actual filter from overall filter string
194 bool ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description);
195
196 private:
197 bool m_showHidden;
198 wxTreeItemId m_rootId;
199 wxString m_defaultPath; // Starting path
200 long m_styleEx; // Extended style
201 wxString m_filter; // Wildcards in same format as per wxFileDialog
202 int m_currentFilter; // The current filter index
203 wxString m_currentFilterStr; // Current filter string
204 wxTreeCtrl* m_treeCtrl;
205 wxDirFilterListCtrl* m_filterListCtrl;
206
207 private:
208 DECLARE_EVENT_TABLE()
209 DECLARE_DYNAMIC_CLASS(wxGenericDirCtrl)
210 wxDECLARE_NO_COPY_CLASS(wxGenericDirCtrl);
211 };
212
213 //-----------------------------------------------------------------------------
214 // wxDirFilterListCtrl
215 //-----------------------------------------------------------------------------
216
217 class WXDLLIMPEXP_CORE wxDirFilterListCtrl: public wxChoice
218 {
219 public:
220 wxDirFilterListCtrl() { Init(); }
221 wxDirFilterListCtrl(wxGenericDirCtrl* parent, const wxWindowID id = wxID_ANY,
222 const wxPoint& pos = wxDefaultPosition,
223 const wxSize& size = wxDefaultSize,
224 long style = 0)
225 {
226 Init();
227 Create(parent, id, pos, size, style);
228 }
229
230 bool Create(wxGenericDirCtrl* parent, const wxWindowID id = wxID_ANY,
231 const wxPoint& pos = wxDefaultPosition,
232 const wxSize& size = wxDefaultSize,
233 long style = 0);
234
235 void Init();
236
237 virtual ~wxDirFilterListCtrl() {}
238
239 //// Operations
240 void FillFilterList(const wxString& filter, int defaultFilter);
241
242 //// Events
243 void OnSelFilter(wxCommandEvent& event);
244
245 protected:
246 wxGenericDirCtrl* m_dirCtrl;
247
248 DECLARE_EVENT_TABLE()
249 DECLARE_CLASS(wxDirFilterListCtrl)
250 wxDECLARE_NO_COPY_CLASS(wxDirFilterListCtrl);
251 };
252
253 #if !defined(__WXMSW__) && !defined(__WXMAC__) && !defined(__WXPM__)
254 #define wxDirCtrl wxGenericDirCtrl
255 #endif
256
257 // Symbols for accessing individual controls
258 #define wxID_TREECTRL 7000
259 #define wxID_FILTERLISTCTRL 7001
260
261 #endif // wxUSE_DIRDLG
262
263 //-------------------------------------------------------------------------
264 // wxFileIconsTable - use wxTheFileIconsTable which is created as necessary
265 //-------------------------------------------------------------------------
266
267 #if wxUSE_DIRDLG || wxUSE_FILEDLG
268
269 class WXDLLIMPEXP_CORE wxFileIconsTable
270 {
271 public:
272 wxFileIconsTable();
273 ~wxFileIconsTable();
274
275 enum iconId_Type
276 {
277 folder,
278 folder_open,
279 computer,
280 drive,
281 cdrom,
282 floppy,
283 removeable,
284 file,
285 executable
286 };
287
288 int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString);
289 wxImageList *GetSmallImageList();
290
291 protected:
292 void Create(); // create on first use
293
294 wxImageList *m_smallImageList;
295 wxHashTable *m_HashTable;
296 };
297
298 // The global fileicons table
299 extern WXDLLIMPEXP_DATA_CORE(wxFileIconsTable *) wxTheFileIconsTable;
300
301 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
302
303 #endif
304 // _WX_DIRCTRLG_H_