]> git.saurik.com Git - wxWidgets.git/blob - user/wxFile/dirctrl.cpp
wxFrame::CreateToolBar() stuff
[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
20 //-----------------------------------------------------------------------------
21 // wxDirInfo
22 //-----------------------------------------------------------------------------
23
24 IMPLEMENT_DYNAMIC_CLASS(wxDirInfo,wxObject)
25
26 wxDirInfo::wxDirInfo( const wxString &path )
27 {
28 m_showHidden = FALSE;
29 m_path = path;
30 if (m_path == "/") m_name ="The Computer";
31 else
32 if (m_path == "/home")
33 {
34 m_name = "My Home";
35 m_path += "/";
36 char buf[300];
37 wxGetHomeDir( buf );
38 m_path = buf;
39 }
40 else
41 if (m_path == "/proc") m_name = "Info Filesystem";
42 else
43 if (m_path == "/mnt") m_name = "Mounted Devices";
44 else
45 if (m_path == "/usr/X11R6") m_name = "User X11";
46 else
47 if (m_path == "/usr") m_name = "User";
48 else
49 if (m_path == "/var") m_name = "Variables";
50 else
51 if (m_path == "/usr/local") m_name = "User local";
52 else
53 if (m_path == "/mnt") m_name = "Mounted Devices";
54 else
55 m_name = wxFileNameFromPath( m_path );
56 };
57
58 wxString wxDirInfo::GetName(void) const
59 {
60 return m_name;
61 };
62
63 wxString wxDirInfo::GetPath(void) const
64 {
65 return m_path;
66 };
67
68 //-----------------------------------------------------------------------------
69 // wxDirCtrl
70 //-----------------------------------------------------------------------------
71
72 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl)
73
74 BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl)
75 EVT_TREE_ITEM_EXPANDED (-1, wxDirCtrl::OnExpandItem)
76 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem)
77 EVT_TREE_DELETE_ITEM (-1, wxDirCtrl::OnDeleteItem)
78 EVT_MOUSE_EVENTS (wxDirCtrl::OnMouse)
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 /*
99 wxDirInfo *info = new wxDirInfo( dir );
100 item.m_data = (long)info;
101 */
102 m_rootId = InsertItem( 0, item );
103 };
104
105 void wxDirCtrl::OnExpandItem( const wxTreeEvent &event )
106 {
107 if (event.m_item.m_itemId == m_rootId)
108 {
109
110 wxTreeItem item;
111 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
112 item.m_children = 1;
113
114 wxDirInfo *info = new wxDirInfo( "/" );
115 item.m_text = info->GetName();
116 item.m_data = (long)info;
117 InsertItem( m_rootId, item );
118
119 info = new wxDirInfo( "/home" );
120 item.m_text = info->GetName();
121 item.m_data = (long)info;
122 InsertItem( m_rootId, item );
123
124 info = new wxDirInfo( "/mnt" );
125 item.m_text = info->GetName();
126 item.m_data = (long)info;
127 InsertItem( m_rootId, item );
128
129 info = new wxDirInfo( "/usr" );
130 item.m_text = info->GetName();
131 item.m_data = (long)info;
132 InsertItem( m_rootId, item );
133
134 info = new wxDirInfo( "/usr/X11R6" );
135 item.m_text = info->GetName();
136 item.m_data = (long)info;
137 InsertItem( m_rootId, item );
138
139 info = new wxDirInfo( "/usr/local" );
140 item.m_text = info->GetName();
141 item.m_data = (long)info;
142 InsertItem( m_rootId, item );
143
144 info = new wxDirInfo( "/var" );
145 item.m_text = info->GetName();
146 item.m_data = (long)info;
147 InsertItem( m_rootId, item );
148
149 info = new wxDirInfo( "/proc" );
150 item.m_text = info->GetName();
151 item.m_data = (long)info;
152 InsertItem( m_rootId, item );
153
154 return;
155 };
156
157 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
158 if (!info) return;
159
160 wxArrayString slist;
161 wxString search,path,filename;
162
163 search = info->GetPath();
164 search += "/*";
165
166 path = wxFindFirstFile( search, wxDIR );
167 while (!path.IsNull())
168 {
169 filename = wxFileNameFromPath( path );
170 if (m_showHidden || (filename[0] != '.'))
171 {
172 if ((filename != ".") &&
173 (filename != "..") &&
174 (path != "/home") &&
175 (path != "/usr/X11R6") &&
176 (path != "/usr/local") &&
177 (path != "/usr") &&
178 (path != "/var") &&
179 (path != "/home") &&
180 (path != "/proc") &&
181 (path != "/mnt")
182 )
183 slist.Add( path ); // ref counting in action !
184 };
185 path = wxFindNextFile();
186 };
187
188 for (uint i = 0; i < slist.Count(); i++)
189 {
190 search = slist[i];
191 search += "/*";
192 path = wxFindFirstFile( search, wxDIR );
193
194 wxDirInfo *child = new wxDirInfo( slist[i] );
195 wxTreeItem item;
196 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
197 item.m_text = child->GetName();
198 item.m_children = 0;
199 if (!path.IsNull()) item.m_children = 1;
200 item.m_data = (long)child;
201 InsertItem( event.m_item.m_itemId, item );
202 };
203 };
204
205 void wxDirCtrl::OnCollapseItem( const wxTreeEvent &event )
206 {
207 DeleteChildren( event.m_item.m_itemId );
208 };
209
210 void wxDirCtrl::OnDeleteItem( const wxTreeEvent &event )
211 {
212 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
213 if (info) delete info;
214 };
215
216 void wxDirCtrl::OnMouse( wxMouseEvent &event )
217 {
218 event.Skip(TRUE);
219
220 if (event.LeftDown())
221 {
222 m_dragX = event.GetX();
223 m_dragY = event.GetY();
224 return;
225 };
226
227 if (event.Dragging())
228 {
229 if ((abs(m_dragX-event.GetX()) < 2) &&
230 (abs(m_dragY-event.GetY()) < 2)) return;
231
232 wxTextDragSource drag( this );
233 drag.SetTextData( "Oh, what a drag." );
234 drag.Start( event.GetX(), event.GetY() );
235 };
236 };
237