]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
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"
45 #include "wx/statline.h"
48 #include "wx/generic/dirdlgg.h"
50 // If compiled under Windows, this macro can cause problems
57 static char * icon1_xpm
[] = {
58 /* width height ncolors chars_per_pixel */
86 static char * icon2_xpm
[] = {
87 /* width height ncolors chars_per_pixel */
116 static const int ID_DIRCTRL
= 1000;
117 static const int ID_TEXTCTRL
= 1001;
118 static const int ID_OK
= 1002;
119 static const int ID_CANCEL
= 1003;
120 static const int ID_NEW
= 1004;
121 //static const int ID_CHECK = 1005;
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
131 /* Insert logic to detect hidden files here
132 * In UnixLand we just check whether the first char is a dot
133 * For FileNameFromPath read LastDirNameInThisPath ;-) */
134 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
136 m_hasSubDirs
= HasSubDirs();
139 wxDirItemData::~wxDirItemData()
143 void wxDirItemData::SetNewDirName( wxString path
)
146 m_name
= wxFileNameFromPath( path
);
149 bool wxDirItemData::HasSubDirs()
151 return wxDir(m_path
).HasSubDirs();
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
158 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
160 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
161 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
162 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
163 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem
)
164 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem
)
167 wxDirCtrl::wxDirCtrl(void)
169 m_showHidden
= FALSE
;
172 wxDirCtrl::wxDirCtrl(wxWindow
*parent
,
174 const wxString
&WXUNUSED(dir
),
178 const wxString
& name
)
179 : wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
182 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
183 m_imageListNormal
->Add(wxICON(icon1
));
184 m_imageListNormal
->Add(wxICON(icon2
));
185 SetImageList(m_imageListNormal
);
188 m_showHidden
= FALSE
;
189 m_rootId
= AddRoot( _("Sections") );
190 SetItemHasChildren(m_rootId
);
191 Expand(m_rootId
); // automatically expand first level
194 /* Quick macro. Don't worry, I'll #undef it later */
195 #define ADD_SECTION(a,b) \
196 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
198 void wxDirCtrl::SetupSections()
205 // better than nothing
206 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
208 ADD_SECTION(wxT("/"), _("The Computer") )
210 ADD_SECTION(home
, _("My Home") )
211 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
212 ADD_SECTION(wxT("/usr/local"), _("User Local") )
213 ADD_SECTION(wxT("/usr"), _("User") )
214 ADD_SECTION(wxT("/var"), _("Variables") )
215 ADD_SECTION(wxT("/etc"), _("Etcetera") )
216 ADD_SECTION(wxT("/tmp"), _("Temporary") )
221 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
224 wxDirItemData
*dir_item
;
226 // wxASSERT(m_paths.Count() == m_names.Count()); ?
228 size_t count
= m_paths
.GetCount();
229 for ( size_t i
=0; i
<count
; i
++)
231 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
233 id
= AppendItem( parent
, m_names
[i
], -1, -1, dir_item
);
235 id
= AppendItem( parent
, m_names
[i
], 0, -1, dir_item
);
236 SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
238 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
242 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
244 // don't rename the main entry "Sections"
245 if (event
.GetItem() == m_rootId
)
251 // don't rename the individual sections
252 if (GetParent( event
.GetItem() ) == m_rootId
)
259 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
261 if ((event
.GetLabel().IsEmpty()) ||
262 (event
.GetLabel() == _(".")) ||
263 (event
.GetLabel() == _("..")) ||
264 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
266 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
272 wxTreeItemId id
= event
.GetItem();
273 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
276 wxString
new_name( wxPathOnly( data
->m_path
) );
277 new_name
+= wxT("/");
278 new_name
+= event
.GetLabel();
282 if (wxFileExists(new_name
))
284 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
289 if (wxRenameFile(data
->m_path
,new_name
))
291 data
->SetNewDirName( new_name
);
295 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
301 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
303 if (event
.GetItem() == m_rootId
)
306 CreateItems(m_rootId
);
310 // This may take a longish time. Go to busy cursor
313 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
319 wxDir
dir(data
->m_path
);
322 bool cont
= dir
.GetFirst(&filename
, "", wxDIR_DIRS
| wxDIR_HIDDEN
);
325 m_paths
.Add(data
->m_path
);
326 m_names
.Add(filename
);
328 cont
= dir
.GetNext(&filename
);
331 CreateItems( event
.GetItem() );
332 SortChildren( event
.GetItem() );
337 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
339 wxTreeItemId child
, parent
= event
.GetItem();
341 /* Workaround because DeleteChildren has disapeared (why?) and
342 * CollapseAndReset doesn't work as advertised (deletes parent too) */
343 child
= GetFirstChild(parent
, cookie
);
347 /* Not GetNextChild below, because the cookie mechanism can't
348 * handle disappearing children! */
349 child
= GetFirstChild(parent
, cookie
);
353 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
358 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
360 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
361 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
362 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
363 EVT_SIZE ( wxDirDialog::OnSize
)
364 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
365 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
366 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
367 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
368 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
371 wxDirDialog::wxDirDialog(wxWindow
*parent
,
372 const wxString
& message
,
373 const wxString
& defaultPath
,
376 : wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
377 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
380 m_dialogStyle
= style
;
383 m_path
= defaultPath
;
387 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
390 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, _T("/"),
396 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
399 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
400 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
402 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
403 // m_check->SetValue(TRUE);
407 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
411 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
412 m_ok
= new wxButton( this, ID_OK
, _("OK") );
413 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
414 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
415 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
416 m_new
= new wxButton( this, ID_NEW
, _("New...") );
417 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
419 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
424 SetAutoLayout( TRUE
);
425 SetSizer( topsizer
);
427 topsizer
->SetSizeHints( this );
428 topsizer
->Fit( this );
432 if (m_path
== wxT("~"))
433 wxGetHomeDir( &m_path
);
435 // choose the directory corresponding to defaultPath in the tree
436 // VZ: using wxStringTokenizer is probably unsafe here (escaped slashes
437 // will not be processed correctly...)
438 wxStringTokenizer
tk(m_path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
443 // default to root dir
444 wxTreeItemId item
= m_dir
->GetFirstChild(m_dir
->GetRootItem(), cookie
);
446 if (!m_path
.IsEmpty() && (m_path
!= wxT("/")) && (m_dir
->m_paths
.Count() > 1))
448 size_t count
= m_dir
->m_paths
.GetCount();
449 for ( size_t i
=1; i
<count
; i
++)
451 if (m_path
.Find( m_dir
->m_paths
[i
] ) == 0)
453 path
= m_dir
->m_paths
[i
];
455 for (size_t j
= 0; j
< i
; j
++)
456 item
= m_dir
->GetNextChild(m_dir
->GetRootItem(), cookie
);
458 wxStringTokenizer
tk2(path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
459 for (size_t h
= 0; h
< tk2
.CountTokens(); h
++)
466 while ( tk
.HasMoreTokens() && item
.IsOk() )
468 path
<< wxFILE_SEP_PATH
<< tk
.GetNextToken();
472 wxTreeItemId child
= m_dir
->GetFirstChild(item
, cookie
);
473 while ( child
.IsOk() )
475 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(child
);
476 if ( data
->m_path
== path
)
479 child
= m_dir
->GetNextChild(item
, cookie
);
488 m_dir
->SelectItem(item
);
489 m_dir
->EnsureVisible(item
);
495 int wxDirDialog::ShowModal()
497 m_input
->SetValue( m_path
);
498 return wxDialog::ShowModal();
501 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
503 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
505 m_input
->SetValue( data
->m_path
);
508 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
510 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
512 m_input
->SetValue( data
->m_path
);
515 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
517 m_path
= m_input
->GetValue();
518 // Does the path exist? (User may have typed anything in m_input)
519 if (wxPathExists(m_path
)) {
520 // OK, path exists, we're done.
524 // Interact with user, find out if the dir is a typo or to be created
525 wxString
msg( _("The directory ") );
527 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
528 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
529 if ( dialog
.ShowModal() == wxID_YES
) {
530 // Okay, let's make it
532 if (wxMkdir(m_path
)) {
533 // The new dir was created okay.
539 msg
= _("Failed to create directory ")+m_path
+
540 _("\n(Do you have the required permissions?)");
541 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
543 // We still don't have a valid dir. Back to the main dialog.
546 // User has answered NO to create dir.
549 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
551 EndModal(wxID_CANCEL
);
554 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
556 wxTreeItemId id
= m_dir
->GetSelection();
557 if ((id
== m_dir
->GetRootItem()) ||
558 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
560 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
561 _("Create directory"), wxOK
| wxICON_INFORMATION
);
566 wxTreeItemId parent
= m_dir
->GetParent( id
);
567 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
570 wxString
new_name( wxT("NewName") );
571 wxString
path( data
->m_path
);
574 if (wxFileExists(path
))
576 // try NewName0, NewName1 etc.
579 new_name
= wxT("NewName");
581 num
.Printf( wxT("%d"), i
);
588 } while (wxFileExists(path
));
594 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
599 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
600 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
601 m_dir
->EnsureVisible( new_id
);
602 m_dir
->EditLabel( new_id
);
606 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
608 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
612 #endif // wxUSE_DIRDLG