]> git.saurik.com Git - wxWidgets.git/blame - user/wxFile/dirctrl.cpp
GTK
[wxWidgets.git] / user / wxFile / dirctrl.cpp
CommitLineData
c801d85f
KB
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
24IMPLEMENT_DYNAMIC_CLASS(wxDirInfo,wxObject)
25
26wxDirInfo::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
58wxString wxDirInfo::GetName(void) const
59{
60 return m_name;
61};
62
63wxString wxDirInfo::GetPath(void) const
64{
65 return m_path;
66};
67
68//-----------------------------------------------------------------------------
69// wxDirCtrl
70//-----------------------------------------------------------------------------
71
72IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl)
73
74BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl)
75 EVT_TREE_ITEM_EXPANDED (-1, wxDirCtrl::OnExpandItem)
76 EVT_TREE_DELETE_ITEM (-1, wxDirCtrl::OnDeleteItem)
77 EVT_MOUSE_EVENTS (wxDirCtrl::OnMouse)
78END_EVENT_TABLE()
79
80wxDirCtrl::wxDirCtrl(void)
81{
82 m_showHidden = FALSE;
83};
84
85wxDirCtrl::wxDirCtrl(wxWindow *parent, const wxWindowID id, const wxString &dir,
86 const wxPoint& pos, const wxSize& size,
87 const long style, const wxString& name )
88 :
89 wxTreeCtrl( parent, id, pos, size, style, name )
90{
91 m_showHidden = FALSE;
92
93 wxTreeItem item;
94 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
95 item.m_text = "root.";
96 item.m_children = 1;
97 wxDirInfo *info = new wxDirInfo( dir );
98 item.m_data = (long)info;
99
100 long root_id = InsertItem( 0, item );
101
102 info = new wxDirInfo( "/" );
103 item.m_text = info->GetName();
104 item.m_data = (long)info;
105 InsertItem( root_id, item );
106
107 info = new wxDirInfo( "/home" );
108 item.m_text = info->GetName();
109 item.m_data = (long)info;
110 InsertItem( root_id, item );
111
112 info = new wxDirInfo( "/mnt" );
113 item.m_text = info->GetName();
114 item.m_data = (long)info;
115 InsertItem( root_id, item );
116
117 info = new wxDirInfo( "/usr" );
118 item.m_text = info->GetName();
119 item.m_data = (long)info;
120 InsertItem( root_id, item );
121
122 info = new wxDirInfo( "/usr/X11R6" );
123 item.m_text = info->GetName();
124 item.m_data = (long)info;
125 InsertItem( root_id, item );
126
127 info = new wxDirInfo( "/usr/local" );
128 item.m_text = info->GetName();
129 item.m_data = (long)info;
130 InsertItem( root_id, item );
131
132 info = new wxDirInfo( "/var" );
133 item.m_text = info->GetName();
134 item.m_data = (long)info;
135 InsertItem( root_id, item );
136
137 info = new wxDirInfo( "/proc" );
138 item.m_text = info->GetName();
139 item.m_data = (long)info;
140 InsertItem( root_id, item );
141};
142
143void wxDirCtrl::OnExpandItem( const wxTreeEvent &event )
144{
145 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
146 if (!info) return;
147
148 wxArrayString slist;
149 wxString search,path,filename;
150
151 search = info->GetPath();
152 search += "/*";
153
154 path = wxFindFirstFile( search, wxDIR );
155 while (!path.IsNull())
156 {
157 filename = wxFileNameFromPath( path );
158 if (m_showHidden || (filename[0] != '.'))
159 {
160 if ((filename != ".") &&
161 (filename != "..") &&
162 (path != "/home") &&
163 (path != "/usr/X11R6") &&
164 (path != "/usr/local") &&
165 (path != "/usr") &&
166 (path != "/var") &&
167 (path != "/home") &&
168 (path != "/proc") &&
169 (path != "/mnt")
170 )
171 slist.Add( path ); // ref counting in action !
172 };
173 path = wxFindNextFile();
174 };
175
176 for (int i = 0; i < slist.Count(); i++)
177 {
178 search = slist[i];
179 search += "/*";
180 path = wxFindFirstFile( search, wxDIR );
181
182 wxDirInfo *child = new wxDirInfo( slist[i] );
183 wxTreeItem item;
184 item.m_mask = wxTREE_MASK_TEXT | wxTREE_MASK_CHILDREN | wxTREE_MASK_DATA;
185 item.m_text = child->GetName();
186 item.m_children = 0;
187 if (!path.IsNull()) item.m_children = 1;
188 item.m_data = (long)child;
189 InsertItem( event.m_item.m_itemId, item );
190 };
191};
192
193void wxDirCtrl::OnDeleteItem( const wxTreeEvent &event )
194{
195 wxDirInfo *info = (wxDirInfo *)event.m_item.m_data;
196 if (info) delete info;
197};
198
199void wxDirCtrl::OnMouse( wxMouseEvent &event )
200{
201 event.Skip(TRUE);
202
203 if (event.LeftDown())
204 {
205 m_dragX = event.GetX();
206 m_dragY = event.GetY();
207 return;
208 };
209
210 if (event.Dragging())
211 {
212 if ((abs(m_dragX-event.GetX()) < 2) &&
213 (abs(m_dragY-event.GetY()) < 2)) return;
214
215 wxTextDragSource drag( this );
216 drag.SetTextData( "Oh, what a drag." );
217 drag.Start( event.GetX(), event.GetY() );
218 };
219};
220