]>
git.saurik.com Git - wxWidgets.git/blob - user/wxFile/filectrl.cpp
2 * Author: Robert Roebling
4 * Copyright: (C) 1997,1998 Robert Roebling
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).
14 #pragma implementation "filectrl.h"
21 #include "sys/types.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
39 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
45 stat( m_fileName
.GetData(), &buff
);
47 lstat( m_fileName
.GetData(), &lbuff
);
49 struct tm
*t
= localtime( &lbuff
.st_mtime
);
50 // struct passwd *user = getpwuid( buff.st_uid );
51 // struct group *grp = getgrgid( buff.st_gid );
53 m_isDir
= S_ISDIR( buff
.st_mode
);
54 m_isLink
= S_ISLNK( lbuff
.st_mode
);
55 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
57 m_size
= buff
.st_size
;
61 m_month
= t
->tm_mon
+1;
65 m_permissions
.sprintf( "%c%c%c",
66 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? 'r' : '-'),
67 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? 'w' : '-'),
68 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? 'x' : '-') );
71 wxString
wxFileData::GetName(void) const
76 wxString
wxFileData::GetFullName(void) const
81 wxString
wxFileData::GetHint(void) const
83 wxString s
= m_fileName
;
85 if (m_isDir
) s
+= "<DIR> ";
86 else if (m_isLink
) s
+= "<LINK> ";
89 s
+= LongToString( m_size
);
92 s
+= IntToString( m_day
);
94 s
+= IntToString( m_month
);
96 s
+= IntToString( m_year
);
98 s
+= IntToString( m_hour
);
100 s
+= IntToString( m_minute
);
106 wxString
wxFileData::GetEntry( const int num
)
115 if (m_isDir
) s
= "<DIR>";
116 else if (m_isLink
) s
= "<LINK>";
117 else s
= LongToString( m_size
);
120 if (m_day
< 10) s
= "0"; else s
= "";
121 s
+= IntToString( m_day
);
123 if (m_month
< 10) s
+= "0";
124 s
+= IntToString( m_month
);
126 if (m_year
< 10) s
+= "0"; // this should happen real soon...
127 s
+= IntToString( m_year
);
130 if (m_hour
< 10) s
= "0"; else s
= "";
131 s
+= IntToString( m_hour
);
133 if (m_minute
< 10) s
+= "0";
134 s
+= IntToString( m_minute
);
146 bool wxFileData::IsDir( void )
151 bool wxFileData::IsExe( void )
156 bool wxFileData::IsLink( void )
161 long wxFileData::GetSize( void )
166 bool wxFileData::NewNameIsLegal( const wxString
&s
)
168 wxString fileName
= wxPathOnly( m_fileName
);
171 return (!wxFileExists( fileName
));
174 bool wxFileData::Rename( const wxString
&s
)
176 wxString fileName
= wxPathOnly( m_fileName
);
179 bool ret
= wxRenameFile( m_fileName
, fileName
);
182 m_fileName
= fileName
;
188 void wxFileData::MakeItem( wxListItem
&item
)
190 item
.m_text
= m_name
;
191 item
.m_colour
= wxBLACK
;
192 if (IsExe()) item
.m_colour
= wxRED
;
193 if (IsDir()) item
.m_colour
= wxBLUE
;
196 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
199 item
.m_data
= (long)this;
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
208 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
209 EVT_SET_FOCUS (wxFileCtrl::OnSetFocus
)
212 wxFileCtrl
*wxFileCtrl::m_lastFocus
= NULL
;
214 wxFileCtrl::wxFileCtrl( void )
217 m_showHidden
= FALSE
;
220 wxFileCtrl::wxFileCtrl( wxWindow
*win
, const wxWindowID id
, const wxString
&dirName
,
221 const wxPoint
&pos
, const wxSize
&size
,
222 const long style
, const wxString
&name
) :
223 wxListCtrl( win
, id
, pos
, size
, style
, name
)
225 SetItemSpacing( 20 );
226 wxImageList
*imageList
= new wxImageList( 18, 18 );
227 imageList
->Add( wxBitmap( folder_xpm
) );
228 imageList
->Add( wxBitmap( txt_xpm
) );
229 imageList
->Add( wxBitmap( list_xpm
) );
230 imageList
->Add( wxBitmap( find_xpm
) );
232 SetImageList( imageList
, wxIMAGE_LIST_NORMAL
);
235 m_showHidden
= FALSE
;
240 SetDropTarget( new wxTextDropTarget() );
243 void wxFileCtrl::ChangeToListMode()
245 SetSingleStyle( wxLC_LIST
);
249 void wxFileCtrl::ChangeToReportMode()
251 SetSingleStyle( wxLC_REPORT
);
255 void wxFileCtrl::ChangeToIconMode()
257 SetSingleStyle( wxLC_ICON
);
261 void wxFileCtrl::ShowHidden( bool show
)
267 int ListCompare( const long data1
, const long data2
, const long WXUNUSED(data
) )
269 wxFileData
*fd1
= (wxFileData
*)data1
;
270 wxFileData
*fd2
= (wxFileData
*)data2
;
271 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
272 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
273 return strcmp( fd1
->GetName(), fd2
->GetName() );
276 void wxFileCtrl::Update( void )
279 for (int i
= 0; i
< 5; i
++) DeleteColumn( 0 );
280 long my_style
= GetWindowStyleFlag();
281 if (my_style
& wxLC_REPORT
)
283 InsertColumn( 0, "Name", wxLIST_FORMAT_LEFT
, 110 );
284 InsertColumn( 1, "Size", wxLIST_FORMAT_LEFT
, 60 );
285 InsertColumn( 2, "Date", wxLIST_FORMAT_LEFT
, 55 );
286 InsertColumn( 3, "Time", wxLIST_FORMAT_LEFT
, 50 );
287 InsertColumn( 4, "Permissions", wxLIST_FORMAT_LEFT
, 120 );
289 wxFileData
*fd
= NULL
;
291 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
;
292 if (my_style
& wxLC_ICON
) item
.m_mask
+= wxLIST_MASK_IMAGE
;
296 wxString res
= m_dirName
+ "/*";
297 char *f
= wxFindFirstFile( res
.GetData(), 0 );
300 res
= wxFileNameFromPath( f
);
301 fd
= new wxFileData( res
, f
);
303 if (m_showHidden
|| (s
[0] != '.'))
305 fd
->MakeItem( item
);
306 if (my_style
& wxLC_REPORT
)
309 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
311 else if (my_style
& wxLC_LIST
)
315 else if (my_style
& wxLC_ICON
)
317 if (fd
->IsDir()) item
.m_image
= 0; else item
.m_image
= 1;
322 f
= wxFindNextFile();
324 SortItems( ListCompare
, 0 );
328 int wxFileCtrl::FillList( wxStringList
&list
)
335 index
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
336 if (index
== -1) break;
338 item
.m_itemId
= index
;
340 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
341 list
.Add( fd
->GetFullName() );
347 index
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
348 if (index
== -1) return 0;
350 item
.m_itemId
= index
;
352 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
353 list
.Add( fd
->GetFullName() );
359 void wxFileCtrl::DeleteFiles(void)
363 int count = FillList( list );
366 wxString s = "Delete ";
367 s += wxIntToString( count );
368 s += " selected file";
369 if (count > 1) s += "s";
371 if (count > 1) s += "ies?"; else s+= "y?";
372 if (wxYES == wxMessageBox( s, "Delete", wxYES_NO ))
373 wxDeleteStatusDia( NULL, &list );
378 void wxFileCtrl::CopyFiles( char *WXUNUSED(dest
) )
382 int count = FillList( list );
384 int ret = 0; // 0 = nix, 1 = copy, 2 = move
385 wxCopyMoveDia( (wxFrame*)GetParent(), count, &ret, &s );
387 wxCopyStatusDia( NULL, s, &list );
391 void wxFileCtrl::MoveFiles( char *WXUNUSED(dest
) )
395 void wxFileCtrl::RenameFile(void)
399 void wxFileCtrl::MakeDir(void)
402 wxString s = wxGetTextFromUser( "Enter new directory name:", "Make directory" );
403 if (s.IsNull()) return;
405 if ((s == ".") || (s == ".."))
407 wxMessageBox( "This was obviously an invalid directory name.", "Go away." );
414 if (wxFileExists( dir ))
416 wxMessageBox( "Filename exists already. Cannot create directoy.", "Make directory" );
424 void wxFileCtrl::GoToParentDir(void)
426 wxString s
= m_dirName
;
427 int pos
= s
.Last( '/' );
428 if ((pos
>= 0) && (s
!= "/"))
430 s
.Remove( pos
, s
.Length()-pos
);
431 if (s
.Length() == 0) s
= "/";
437 void wxFileCtrl::GoToHomeDir(void)
439 wxString s
= wxGetUserHome( wxString() );
444 void wxFileCtrl::GoToDir( const wxString
&dir
)
450 void wxFileCtrl::GetDir( wxString
&dir
)
456 void wxFileCtrl::OnDropFiles( int WXUNUSED(n), char **WXUNUSED(data), int WXUNUSED(x), int WXUNUSED(y) )
460 int flag = wxLIST_HITTEST_ONITEM;
461 long hit = HitTest( pt, flag );
467 wxFileData *fd = (wxFileData*)li.m_data;
468 if (fd->IsDir()) fd->GetFullName( destDir );
470 if (destDir.IsNull()) destDir = m_dirName;
471 int ret = 0; // 0 = nix, 1 = copy, 2 = move
472 wxCopyMoveDia( (wxFrame*)GetParent(), n, &ret, &destDir );
476 for (int i = 0; i < n; i++) slist.Add( data[i] );
477 wxCopyStatusDia( NULL, destDir.GetData(), &slist );
483 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
485 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
489 void wxFileCtrl::OnListKeyDown( wxListEvent
&event
)
491 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
494 m_dirName
= fd
->GetFullName();
501 wxExecute( fd
->GetFullName() );
506 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
508 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
509 wxString newName
= event
.m_item
.m_text
;
510 if (fd
->NewNameIsLegal( newName
))
512 if (fd
->Rename( newName
))
518 wxString s
= "Could not rename file to ";
521 wxMessageBox( s
, "FileMaker", wxOK
);
526 wxString s
= "File name ";
528 s
+= " exists already or is invalid.\n";
529 s
+= "Could not rename file.";
530 wxMessageBox( s
, "FileMaker", wxOK
);
535 void wxFileCtrl::OnSetFocus( wxFocusEvent
&event
)