]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
6158e8d0e6d9b6d0dd135437f66b07440bfb0a9b
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDirDialog
4 // Author: Harm van der Heijden and Robert Roebling
8 // Copyright: (c) Harm van der Heijden and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dirdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
30 #include "wx/layout.h"
31 #include "wx/msgdlg.h"
32 #include "wx/textdlg.h"
33 #include "wx/filefn.h"
34 #include "wx/cmndata.h"
35 #include "wx/gdicmn.h"
37 #include "wx/imaglist.h"
41 #include "wx/tokenzr.h"
44 #include "wx/statline.h"
47 #include "wx/generic/dirdlgg.h"
49 // If compiled under Windows, this macro can cause problems
56 static char * icon1_xpm
[] = {
57 /* width height ncolors chars_per_pixel */
85 static char * icon2_xpm
[] = {
86 /* width height ncolors chars_per_pixel */
115 static const int ID_DIRCTRL
= 1000;
116 static const int ID_TEXTCTRL
= 1001;
117 static const int ID_OK
= 1002;
118 static const int ID_CANCEL
= 1003;
119 static const int ID_NEW
= 1004;
120 //static const int ID_CHECK = 1005;
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
130 /* Insert logic to detect hidden files here
131 * In UnixLand we just check whether the first char is a dot
132 * For FileNameFromPath read LastDirNameInThisPath ;-) */
133 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
135 m_hasSubDirs
= HasSubDirs();
138 wxDirItemData::~wxDirItemData()
142 void wxDirItemData::SetNewDirName( wxString path
)
145 m_name
= wxFileNameFromPath( path
);
148 bool wxDirItemData::HasSubDirs()
150 wxString search
= m_path
+ wxT("/*");
152 wxString path
= wxFindFirstFile( search
, wxDIR
);
153 return (bool)(!path
.IsNull());
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
162 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
163 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
164 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
165 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem
)
166 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem
)
169 wxDirCtrl::wxDirCtrl(void)
171 m_showHidden
= FALSE
;
174 wxDirCtrl::wxDirCtrl(wxWindow
*parent
,
176 const wxString
&WXUNUSED(dir
),
180 const wxString
& name
)
181 : wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
184 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
185 m_imageListNormal
->Add(wxICON(icon1
));
186 m_imageListNormal
->Add(wxICON(icon2
));
187 SetImageList(m_imageListNormal
);
190 m_showHidden
= FALSE
;
191 m_rootId
= AddRoot( _("Sections") );
192 SetItemHasChildren(m_rootId
);
193 Expand(m_rootId
); // automatically expand first level
196 /* Quick macro. Don't worry, I'll #undef it later */
197 #define ADD_SECTION(a,b) \
198 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
200 void wxDirCtrl::SetupSections()
207 // better than nothing
208 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
210 ADD_SECTION(wxT("/"), _("The Computer") )
212 ADD_SECTION(home
, _("My Home") )
213 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
214 ADD_SECTION(wxT("/usr/local"), _("User Local") )
215 ADD_SECTION(wxT("/usr"), _("User") )
216 ADD_SECTION(wxT("/var"), _("Variables") )
217 ADD_SECTION(wxT("/etc"), _("Etcetera") )
218 ADD_SECTION(wxT("/tmp"), _("Temporary") )
223 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
226 wxDirItemData
*dir_item
;
228 // wxASSERT(m_paths.Count() == m_names.Count()); ?
230 size_t count
= m_paths
.GetCount();
231 for ( size_t i
=0; i
<count
; i
++)
233 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
235 id
= AppendItem( parent
, m_names
[i
], -1, -1, dir_item
);
237 id
= AppendItem( parent
, m_names
[i
], 0, -1, dir_item
);
238 SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
240 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
244 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
246 // don't rename the main entry "Sections"
247 if (event
.GetItem() == m_rootId
)
253 // don't rename the individual sections
254 if (GetParent( event
.GetItem() ) == m_rootId
)
261 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
263 if ((event
.GetLabel().IsEmpty()) ||
264 (event
.GetLabel() == _(".")) ||
265 (event
.GetLabel() == _("..")) ||
266 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
268 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
274 wxTreeItemId id
= event
.GetItem();
275 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
278 wxString
new_name( wxPathOnly( data
->m_path
) );
279 new_name
+= wxT("/");
280 new_name
+= event
.GetLabel();
284 if (wxFileExists(new_name
))
286 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
291 if (wxRenameFile(data
->m_path
,new_name
))
293 data
->SetNewDirName( new_name
);
297 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
303 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
305 if (event
.GetItem() == m_rootId
)
308 CreateItems(m_rootId
);
312 // This may take a longish time. Go to busy cursor
315 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
318 wxString search
,path
,filename
;
323 search
= data
->m_path
+ _T("\\*.*");
325 search
= data
->m_path
+ _T("/*");
327 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
328 path
=wxFindNextFile() )
330 filename
= wxFileNameFromPath( path
);
331 /* Don't add "." and ".." to the tree. I think wxFindNextFile
332 * also checks this, but I don't quite understand what happens
333 * there. Also wxFindNextFile seems to swallow hidden dirs */
334 if ( (filename
!= _T(".")) && (filename
!= _T("..")) )
337 m_names
.Add(filename
);
341 CreateItems( event
.GetItem() );
342 SortChildren( event
.GetItem() );
347 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
349 wxTreeItemId child
, parent
= event
.GetItem();
351 /* Workaround because DeleteChildren has disapeared (why?) and
352 * CollapseAndReset doesn't work as advertised (deletes parent too) */
353 child
= GetFirstChild(parent
, cookie
);
357 /* Not GetNextChild below, because the cookie mechanism can't
358 * handle disappearing children! */
359 child
= GetFirstChild(parent
, cookie
);
363 //-----------------------------------------------------------------------------
365 //-----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
370 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
371 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
372 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
373 EVT_SIZE ( wxDirDialog::OnSize
)
374 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
375 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
376 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
377 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
378 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
381 wxDirDialog::wxDirDialog(wxWindow
*parent
,
382 const wxString
& message
,
383 const wxString
& defaultPath
,
386 : wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
387 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
390 m_dialogStyle
= style
;
393 m_path
= defaultPath
;
397 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
400 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, _T("/"),
406 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
409 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
410 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
412 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
413 // m_check->SetValue(TRUE);
417 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
421 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
422 m_ok
= new wxButton( this, ID_OK
, _("OK") );
423 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
424 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
425 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
426 m_new
= new wxButton( this, ID_NEW
, _("New...") );
427 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
429 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
434 SetAutoLayout( TRUE
);
435 SetSizer( topsizer
);
437 topsizer
->SetSizeHints( this );
438 topsizer
->Fit( this );
442 if (m_path
== wxT("~"))
443 wxGetHomeDir( &m_path
);
445 // choose the directory corresponding to defaultPath in the tree
446 // VZ: using wxStringTokenizer is probably unsafe here (escaped slashes
447 // will not be processed correctly...)
448 wxStringTokenizer
tk(m_path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
453 // default to root dir
454 wxTreeItemId item
= m_dir
->GetFirstChild(m_dir
->GetRootItem(), cookie
);
456 if (!m_path
.IsEmpty() && (m_path
!= wxT("/")) && (m_dir
->m_paths
.Count() > 1))
458 size_t count
= m_dir
->m_paths
.GetCount();
459 for ( size_t i
=1; i
<count
; i
++)
461 if (m_path
.Find( m_dir
->m_paths
[i
] ) == 0)
463 path
= m_dir
->m_paths
[i
];
465 for (size_t j
= 0; j
< i
; j
++)
466 item
= m_dir
->GetNextChild(m_dir
->GetRootItem(), cookie
);
468 wxStringTokenizer
tk2(path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
469 for (size_t h
= 0; h
< tk2
.CountTokens(); h
++)
476 while ( tk
.HasMoreTokens() && item
.IsOk() )
478 path
<< wxFILE_SEP_PATH
<< tk
.GetNextToken();
482 wxTreeItemId child
= m_dir
->GetFirstChild(item
, cookie
);
483 while ( child
.IsOk() )
485 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(child
);
486 if ( data
->m_path
== path
)
489 child
= m_dir
->GetNextChild(item
, cookie
);
498 m_dir
->SelectItem(item
);
499 m_dir
->EnsureVisible(item
);
505 int wxDirDialog::ShowModal()
507 m_input
->SetValue( m_path
);
508 return wxDialog::ShowModal();
511 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
513 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
515 m_input
->SetValue( data
->m_path
);
518 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
520 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
522 m_input
->SetValue( data
->m_path
);
525 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
527 m_path
= m_input
->GetValue();
528 // Does the path exist? (User may have typed anything in m_input)
529 if (wxPathExists(m_path
)) {
530 // OK, path exists, we're done.
534 // Interact with user, find out if the dir is a typo or to be created
535 wxString
msg( _("The directory ") );
537 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
538 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
539 if ( dialog
.ShowModal() == wxID_YES
) {
540 // Okay, let's make it
542 if (wxMkdir(m_path
)) {
543 // The new dir was created okay.
549 msg
= _("Failed to create directory ")+m_path
+
550 _("\n(Do you have the required permissions?)");
551 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
553 // We still don't have a valid dir. Back to the main dialog.
556 // User has answered NO to create dir.
559 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
561 EndModal(wxID_CANCEL
);
564 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
566 wxTreeItemId id
= m_dir
->GetSelection();
567 if ((id
== m_dir
->GetRootItem()) ||
568 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
570 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
571 _("Create directory"), wxOK
| wxICON_INFORMATION
);
576 wxTreeItemId parent
= m_dir
->GetParent( id
);
577 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
580 wxString
new_name( wxT("NewName") );
581 wxString
path( data
->m_path
);
584 if (wxFileExists(path
))
586 // try NewName0, NewName1 etc.
589 new_name
= wxT("NewName");
591 num
.Printf( wxT("%d"), i
);
598 } while (wxFileExists(path
));
604 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
609 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
610 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
611 m_dir
->EnsureVisible( new_id
);
612 m_dir
->EditLabel( new_id
);
616 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
618 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
622 #endif // wxUSE_DIRDLG