]> git.saurik.com Git - wxWidgets.git/blob - user/wxFile/dirctrl.cpp
Added simple notebook sample
[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 wxGetHomeDir( &m_path );
38 }
39 else
40 if (m_path == "/proc") m_name = "Info Filesystem";
41 else
42 if (m_path == "/mnt") m_name = "Mounted Devices";
43 else
44 if (m_path == "/usr/X11R6") m_name = "User X11";
45 else
46 if (m_path == "/usr") m_name = "User";
47 else
48 if (m_path == "/var") m_name = "Variables";
49 else
50 if (m_path == "/usr/local") m_name = "User local";
51 else
52 if (m_path == "/mnt") m_name = "Mounted Devices";
53 else
54 m_name = wxFileNameFromPath( m_path );
55 };
56
57 wxString wxDirInfo::GetName(void) const
58 {
59 return m_name;
60 };
61
62 wxString wxDirInfo::GetPath(void) const
63 {
64 return m_path;
65 };
66
67 //-----------------------------------------------------------------------------
68 // wxDirCtrl
69 //-----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl)
72
73 BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl)
74 EVT_TREE_ITEM_EXPANDED (-1, wxDirCtrl::OnExpandItem)
75 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem)
76 EVT_TREE_DELETE_ITEM (-1, wxDirCtrl::OnDeleteItem)
77 END_EVENT_TABLE()
78
79 wxDirCtrl::wxDirCtrl(void)
80 {
81 m_showHidden = FALSE;
82 };
83
84 wxDirCtrl::wxDirCtrl(wxWindow *parent, const wxWindowID id, const wxString &WXUNUSED(dir),
85 const wxPoint& pos, const wxSize& size,
86 const long style, const wxString& name )
87 :
88 wxTreeCtrl( parent, id, pos, size, style, name )
89 {
90 m_showHidden = FALSE;
91
92 wxTreeItem item;
93 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
94 item.m_text = "Sections";
95 item.m_children = 1;
96 m_rootId = InsertItem( 0, item );
97
98 // SetDropTarget( new wxFileDropTarget() );
99 };
100
101 void wxDirCtrl::OnExpandItem( const wxTreeEvent &event )
102 {
103 if (event.m_item.m_itemId == m_rootId)
104 {
105
106 wxTreeItem item;
107 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
108 item.m_children = 1;
109
110 wxDirInfo *info = new wxDirInfo( "/" );
111 item.m_text = info->GetName();
112 item.m_data = (long)info;
113 InsertItem( m_rootId, item );
114
115 info = new wxDirInfo( "/home" );
116 item.m_text = info->GetName();
117 item.m_data = (long)info;
118 InsertItem( m_rootId, item );
119
120 info = new wxDirInfo( "/mnt" );
121 item.m_text = info->GetName();
122 item.m_data = (long)info;
123 InsertItem( m_rootId, item );
124
125 info = new wxDirInfo( "/usr" );
126 item.m_text = info->GetName();
127 item.m_data = (long)info;
128 InsertItem( m_rootId, item );
129
130 info = new wxDirInfo( "/usr/X11R6" );
131 item.m_text = info->GetName();
132 item.m_data = (long)info;
133 InsertItem( m_rootId, item );
134
135 info = new wxDirInfo( "/usr/local" );
136 item.m_text = info->GetName();
137 item.m_data = (long)info;
138 InsertItem( m_rootId, item );
139
140 info = new wxDirInfo( "/var" );
141 item.m_text = info->GetName();
142 item.m_data = (long)info;
143 InsertItem( m_rootId, item );
144
145 info = new wxDirInfo( "/proc" );
146 item.m_text = info->GetName();
147 item.m_data = (long)info;
148 InsertItem( m_rootId, item );
149
150 return;
151 };
152
153 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
154 if (!info) return;
155
156 wxArrayString slist;
157 wxString search,path,filename;
158
159 search = info->GetPath();
160 search += "/*";
161
162 path = wxFindFirstFile( search, wxDIR );
163 while (!path.IsNull())
164 {
165 filename = wxFileNameFromPath( path );
166 if (m_showHidden || (filename[0] != '.'))
167 {
168 if ((filename != ".") &&
169 (filename != "..") &&
170 (path != "/home") &&
171 (path != "/usr/X11R6") &&
172 (path != "/usr/local") &&
173 (path != "/usr") &&
174 (path != "/var") &&
175 (path != "/home") &&
176 (path != "/proc") &&
177 (path != "/mnt")
178 )
179
180 slist.Add( path ); // ref counting in action !
181 };
182 path = wxFindNextFile();
183 };
184
185 for (uint i = 0; i < slist.Count(); i++)
186 {
187 search = slist[i];
188 search += "/*";
189 path = wxFindFirstFile( search, wxDIR );
190
191 wxDirInfo *child = new wxDirInfo( slist[i] );
192 wxTreeItem item;
193 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
194 item.m_text = child->GetName();
195 item.m_children = 0;
196 if (!path.IsNull()) item.m_children = 1;
197 item.m_data = (long)child;
198 InsertItem( event.m_item.m_itemId, item );
199 };
200 };
201
202 void wxDirCtrl::OnCollapseItem( const wxTreeEvent &event )
203 {
204 DeleteChildren( event.m_item.m_itemId );
205 };
206
207 void wxDirCtrl::OnDeleteItem( const wxTreeEvent &event )
208 {
209 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
210 if (info) delete info;
211 };