dirctrlg.h 11 KB

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