]> git.saurik.com Git - wxWidgets.git/blob - user/wxFile/dirctrl.cpp
Looks like I've fixed the editing
[wxWidgets.git] / user / wxFile / dirctrl.cpp
1 /*
2 * Author: Robert Roebling
3 *
4 * Copyright: (C) 1997,1998 Robert Roebling
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the wxWindows Licence, which
8 * you have received with this library (see Licence.htm).
9 *
10 */
11
12 #ifdef __GNUG__
13 #pragma implementation "dirctrl.h"
14 #endif
15
16 #include "dirctrl.h"
17 #include "wx/gdicmn.h"
18 #include "wx/utils.h"
19 #include "wx/dnd.h"
20
21 //-----------------------------------------------------------------------------
22 // wxDirInfo
23 //-----------------------------------------------------------------------------
24
25 IMPLEMENT_DYNAMIC_CLASS(wxDirInfo,wxObject)
26
27 wxDirInfo::wxDirInfo( const wxString &path )
28 {
29 m_showHidden = FALSE;
30 m_path = path;
31 if (m_path == "/") m_name ="The Computer";
32 else
33 if (m_path == "/home")
34 {
35 m_name = "My Home";
36 m_path += "/";
37 char buf[300];
38 wxGetHomeDir( buf );
39 m_path = buf;
40 }
41 else
42 if (m_path == "/proc") m_name = "Info Filesystem";
43 else
44 if (m_path == "/mnt") m_name = "Mounted Devices";
45 else
46 if (m_path == "/usr/X11R6") m_name = "User X11";
47 else
48 if (m_path == "/usr") m_name = "User";
49 else
50 if (m_path == "/var") m_name = "Variables";
51 else
52 if (m_path == "/usr/local") m_name = "User local";
53 else
54 if (m_path == "/mnt") m_name = "Mounted Devices";
55 else
56 m_name = wxFileNameFromPath( m_path );
57 };
58
59 wxString wxDirInfo::GetName(void) const
60 {
61 return m_name;
62 };
63
64 wxString wxDirInfo::GetPath(void) const
65 {
66 return m_path;
67 };
68
69 //-----------------------------------------------------------------------------
70 // wxDirCtrl
71 //-----------------------------------------------------------------------------
72
73 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl)
74
75 BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl)
76 EVT_TREE_ITEM_EXPANDED (-1, wxDirCtrl::OnExpandItem)
77 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem)
78 EVT_TREE_DELETE_ITEM (-1, wxDirCtrl::OnDeleteItem)
79 END_EVENT_TABLE()
80
81 wxDirCtrl::wxDirCtrl(void)
82 {
83 m_showHidden = FALSE;
84 };
85
86 wxDirCtrl::wxDirCtrl(wxWindow *parent, const wxWindowID id, const wxString &WXUNUSED(dir),
87 const wxPoint& pos, const wxSize& size,
88 const long style, const wxString& name )
89 :
90 wxTreeCtrl( parent, id, pos, size, style, name )
91 {
92 m_showHidden = FALSE;
93
94 wxTreeItem item;
95 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
96 item.m_text = "Sections";
97 item.m_children = 1;
98 m_rootId = InsertItem( 0, item );
99
100 SetDropTarget( new wxFileDropTarget() );
101 };
102
103 void wxDirCtrl::OnExpandItem( const wxTreeEvent &event )
104 {
105 if (event.m_item.m_itemId == m_rootId)
106 {
107
108 wxTreeItem item;
109 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
110 item.m_children = 1;
111
112 wxDirInfo *info = new wxDirInfo( "/" );
113 item.m_text = info->GetName();
114 item.m_data = (long)info;
115 InsertItem( m_rootId, item );
116
117 info = new wxDirInfo( "/home" );
118 item.m_text = info->GetName();
119 item.m_data = (long)info;
120 InsertItem( m_rootId, item );
121
122 info = new wxDirInfo( "/mnt" );
123 item.m_text = info->GetName();
124 item.m_data = (long)info;
125 InsertItem( m_rootId, item );
126
127 info = new wxDirInfo( "/usr" );
128 item.m_text = info->GetName();
129 item.m_data = (long)info;
130 InsertItem( m_rootId, item );
131
132 info = new wxDirInfo( "/usr/X11R6" );
133 item.m_text = info->GetName();
134 item.m_data = (long)info;
135 InsertItem( m_rootId, item );
136
137 info = new wxDirInfo( "/usr/local" );
138 item.m_text = info->GetName();
139 item.m_data = (long)info;
140 InsertItem( m_rootId, item );
141
142 info = new wxDirInfo( "/var" );
143 item.m_text = info->GetName();
144 item.m_data = (long)info;
145 InsertItem( m_rootId, item );
146
147 info = new wxDirInfo( "/proc" );
148 item.m_text = info->GetName();
149 item.m_data = (long)info;
150 InsertItem( m_rootId, item );
151
152 return;
153 };
154
155 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
156 if (!info) return;
157
158 wxArrayString slist;
159 wxString search,path,filename;
160
161 search = info->GetPath();
162 search += "/*";
163
164 path = wxFindFirstFile( search, wxDIR );
165 while (!path.IsNull())
166 {
167 filename = wxFileNameFromPath( path );
168 if (m_showHidden || (filename[0] != '.'))
169 {
170 if ((filename != ".") &&
171 (filename != "..") &&
172 (path != "/home") &&
173 (path != "/usr/X11R6") &&
174 (path != "/usr/local") &&
175 (path != "/usr") &&
176 (path != "/var") &&
177 (path != "/home") &&
178 (path != "/proc") &&
179 (path != "/mnt")
180 )
181
182 slist.Add( path ); // ref counting in action !
183 };
184 path = wxFindNextFile();
185 };
186
187 for (uint i = 0; i < slist.Count(); i++)
188 {
189 search = slist[i];
190 search += "/*";
191 path = wxFindFirstFile( search, wxDIR );
192
193 wxDirInfo *child = new wxDirInfo( slist[i] );
194 wxTreeItem item;
195 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
196 item.m_text = child->GetName();
197 item.m_children = 0;
198 if (!path.IsNull()) item.m_children = 1;
199 item.m_data = (long)child;
200 InsertItem( event.m_item.m_itemId, item );
201 };
202 };
203
204 void wxDirCtrl::OnCollapseItem( const wxTreeEvent &event )
205 {
206 DeleteChildren( event.m_item.m_itemId );
207 };
208
209 void wxDirCtrl::OnDeleteItem( const wxTreeEvent &event )
210 {
211 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
212 if (info) delete info;
213 };