]>
git.saurik.com Git - wxWidgets.git/blob - user/wxFile/filectrl.cpp
5956e5ef424ade4537f28f2928712fa470d5bbd3
   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 
= (wxFileCtrl 
*) 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( 40 ); 
 226   wxImageList 
*imageList 
= new wxImageList( 30, 30 ); 
 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
; 
 244   SetDropTarget( new wxFileDropTarget() ); 
 247 void wxFileCtrl::ChangeToListMode() 
 249   SetSingleStyle( wxLC_LIST 
); 
 253 void wxFileCtrl::ChangeToReportMode() 
 255   SetSingleStyle( wxLC_REPORT 
); 
 259 void wxFileCtrl::ChangeToIconMode() 
 261   SetSingleStyle( wxLC_ICON 
); 
 265 void wxFileCtrl::ShowHidden( bool show 
) 
 271 int ListCompare( const long data1
, const long data2
, const long WXUNUSED(data
) ) 
 273    wxFileData 
*fd1 
= (wxFileData
*)data1 
; 
 274    wxFileData 
*fd2 
= (wxFileData
*)data2 
; 
 275    if (fd1
->IsDir() && !fd2
->IsDir()) return -1; 
 276    if (fd2
->IsDir() && !fd1
->IsDir()) return 1; 
 277    return strcmp( fd1
->GetName(), fd2
->GetName() ); 
 280 void wxFileCtrl::Update( void ) 
 283   for (int i 
= 0; i 
< 5; i
++) DeleteColumn( 0 ); 
 284   long my_style 
= GetWindowStyleFlag(); 
 285   if (my_style 
& wxLC_REPORT
) 
 287     InsertColumn( 0, "Name", wxLIST_FORMAT_LEFT
, 110 ); 
 288     InsertColumn( 1, "Size", wxLIST_FORMAT_LEFT
, 60 ); 
 289     InsertColumn( 2, "Date", wxLIST_FORMAT_LEFT
, 55 ); 
 290     InsertColumn( 3, "Time", wxLIST_FORMAT_LEFT
, 50 ); 
 291     InsertColumn( 4, "Permissions", wxLIST_FORMAT_LEFT
, 120 ); 
 293   wxFileData 
*fd 
= (wxFileData 
*) NULL
; 
 295   item
.m_mask 
= wxLIST_MASK_TEXT 
+ wxLIST_MASK_DATA
; 
 296   if (my_style 
& wxLC_ICON
) item
.m_mask 
+= wxLIST_MASK_IMAGE
; 
 300   wxString res 
= m_dirName 
+ "/*"; 
 301   char *f 
= wxFindFirstFile( res
.GetData(), 0 ); 
 304     res 
= wxFileNameFromPath( f 
); 
 305     fd 
= new wxFileData( res
, f 
); 
 307     if (m_showHidden 
|| (s
[0] != '.')) 
 309       fd
->MakeItem( item 
); 
 310       if (my_style 
& wxLC_REPORT
) 
 313         for (int i 
= 1; i 
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) ); 
 315       else if (my_style 
& wxLC_LIST
) 
 319      else if (my_style 
& wxLC_ICON
) 
 321         if (fd
->IsDir()) item
.m_image 
= 0; else item
.m_image 
= 1; 
 326     f 
= wxFindNextFile(); 
 328   SortItems( ListCompare
, 0 ); 
 332 int wxFileCtrl::FillList( wxStringList 
&list 
) 
 339     index 
= GetNextItem( index
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED 
); 
 340     if (index 
== -1) break; 
 342     item
.m_itemId 
= index
; 
 344     wxFileData 
*fd 
= (wxFileData
*)item
.m_data
; 
 345     list
.Add( fd
->GetFullName() ); 
 351     index 
= GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED 
); 
 352     if (index 
== -1) return 0; 
 354     item
.m_itemId 
= index
; 
 356     wxFileData 
*fd 
= (wxFileData
*)item
.m_data
; 
 357     list
.Add( fd
->GetFullName() ); 
 363 void wxFileCtrl::DeleteFiles(void) 
 367   int count = FillList( list ); 
 370     wxString s = "Delete "; 
 371     s += wxIntToString( count ); 
 372     s += " selected file"; 
 373     if (count > 1) s += "s"; 
 375     if (count > 1) s += "ies?"; else s+= "y?"; 
 376     if (wxYES == wxMessageBox( s, "Delete", wxYES_NO )) 
 377       wxDeleteStatusDia( NULL, &list ); 
 382 void wxFileCtrl::CopyFiles( char *WXUNUSED(dest
) ) 
 386   int count = FillList( list ); 
 388   int ret = 0; // 0 = nix, 1 = copy, 2 = move 
 389   wxCopyMoveDia( (wxFrame*)GetParent(), count, &ret, &s ); 
 391     wxCopyStatusDia( NULL, s, &list ); 
 395 void wxFileCtrl::MoveFiles( char *WXUNUSED(dest
) ) 
 399 void wxFileCtrl::RenameFile(void) 
 403 void wxFileCtrl::MakeDir(void) 
 406   wxString s = wxGetTextFromUser( "Enter new directory name:", "Make directory" ); 
 407   if (s.IsNull()) return; 
 409   if ((s == ".") || (s == "..")) 
 411     wxMessageBox( "This was obviously an invalid directory name.", "Go away." ); 
 418   if (wxFileExists( dir )) 
 420     wxMessageBox( "Filename exists already. Cannot create directoy.", "Make directory" ); 
 428 void wxFileCtrl::GoToParentDir(void) 
 430   wxString s 
= m_dirName
; 
 431   int pos 
= s
.Last( '/' );   
 432   if ((pos 
>= 0) && (s 
!= "/")) 
 434     s
.Remove( pos
, s
.Length()-pos 
); 
 435     if (s
.Length() == 0) s 
= "/"; 
 441 void wxFileCtrl::GoToHomeDir(void) 
 443   wxString s 
= wxGetUserHome( wxString() ); 
 448 void wxFileCtrl::GoToDir( const wxString 
&dir 
) 
 454 void wxFileCtrl::GetDir( wxString 
&dir 
) 
 460 void wxFileCtrl::OnDropFiles( int WXUNUSED(n), char **WXUNUSED(data), int WXUNUSED(x), int WXUNUSED(y) ) 
 464   int flag = wxLIST_HITTEST_ONITEM; 
 465   long hit = HitTest( pt, flag ); 
 471     wxFileData *fd = (wxFileData*)li.m_data; 
 472     if (fd->IsDir()) fd->GetFullName( destDir ); 
 474   if (destDir.IsNull()) destDir = m_dirName; 
 475   int ret = 0; // 0 = nix, 1 = copy, 2 = move 
 476   wxCopyMoveDia( (wxFrame*)GetParent(), n, &ret, &destDir ); 
 480      for (int i = 0; i < n; i++) slist.Add( data[i] ); 
 481      wxCopyStatusDia( NULL, destDir.GetData(), &slist ); 
 487 void wxFileCtrl::OnListDeleteItem( wxListEvent 
&event 
) 
 489   wxFileData 
*fd 
= (wxFileData
*)event
.m_item
.m_data
; 
 493 void wxFileCtrl::OnListKeyDown( wxListEvent 
&event 
) 
 495   wxFileData 
*fd 
= (wxFileData
*)event
.m_item
.m_data
; 
 498     m_dirName 
= fd
->GetFullName(); 
 505     wxExecute( fd
->GetFullName() ); 
 510 void wxFileCtrl::OnListEndLabelEdit( wxListEvent 
&event 
) 
 512   wxFileData 
*fd 
= (wxFileData
*)event
.m_item
.m_data
; 
 513   wxString newName 
= event
.m_item
.m_text
; 
 514   if (fd
->NewNameIsLegal( newName 
)) 
 516     if (fd
->Rename( newName 
)) 
 522       wxString s 
= "Could not rename file to "; 
 525       wxMessageBox( s
, "FileMaker", wxOK 
); 
 530     wxString s 
= "File name "; 
 532     s 
+= " exists already or is invalid.\n"; 
 533     s 
+= "Could not rename file."; 
 534     wxMessageBox( s
, "FileMaker", wxOK 
); 
 539 void wxFileCtrl::OnSetFocus( wxFocusEvent 
&event 
)