]>
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/textctrl.h"
33 #include "wx/textdlg.h"
34 #include "wx/filefn.h"
35 #include "wx/cmndata.h"
36 #include "wx/gdicmn.h"
38 #include "wx/imaglist.h"
42 #include "wx/tokenzr.h"
46 #include "wx/statline.h"
49 #include "wx/generic/dirdlgg.h"
51 // If compiled under Windows, this macro can cause problems
58 static char * icon1_xpm
[] = {
59 /* width height ncolors chars_per_pixel */
87 static char * icon2_xpm
[] = {
88 /* width height ncolors chars_per_pixel */
117 static const int ID_DIRCTRL
= 1000;
118 static const int ID_TEXTCTRL
= 1001;
119 static const int ID_OK
= 1002;
120 static const int ID_CANCEL
= 1003;
121 static const int ID_NEW
= 1004;
122 //static const int ID_CHECK = 1005;
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
132 /* Insert logic to detect hidden files here
133 * In UnixLand we just check whether the first char is a dot
134 * For FileNameFromPath read LastDirNameInThisPath ;-) */
135 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
137 m_hasSubDirs
= HasSubDirs();
140 wxDirItemData::~wxDirItemData()
144 void wxDirItemData::SetNewDirName( wxString path
)
147 m_name
= wxFileNameFromPath( path
);
150 bool wxDirItemData::HasSubDirs()
152 return wxDir(m_path
).HasSubDirs();
155 //-----------------------------------------------------------------------------
157 //-----------------------------------------------------------------------------
159 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
161 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
162 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
163 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
164 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem
)
165 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem
)
168 wxDirCtrl::wxDirCtrl(void)
170 m_showHidden
= FALSE
;
173 wxDirCtrl::wxDirCtrl(wxWindow
*parent
,
175 const wxString
&WXUNUSED(dir
),
179 const wxString
& name
)
180 : wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
183 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
184 m_imageListNormal
->Add(wxICON(icon1
));
185 m_imageListNormal
->Add(wxICON(icon2
));
186 SetImageList(m_imageListNormal
);
189 m_showHidden
= FALSE
;
190 m_rootId
= AddRoot( _("Sections") );
191 SetItemHasChildren(m_rootId
);
192 Expand(m_rootId
); // automatically expand first level
195 /* Quick macro. Don't worry, I'll #undef it later */
196 #define ADD_SECTION(a,b) \
197 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
199 void wxDirCtrl::SetupSections()
206 // better than nothing
207 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
209 ADD_SECTION(wxT("/"), _("The Computer") )
211 ADD_SECTION(home
, _("My Home") )
212 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
213 ADD_SECTION(wxT("/usr/local"), _("User Local") )
214 ADD_SECTION(wxT("/usr"), _("User") )
215 ADD_SECTION(wxT("/var"), _("Variables") )
216 ADD_SECTION(wxT("/etc"), _("Etcetera") )
217 ADD_SECTION(wxT("/tmp"), _("Temporary") )
222 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
225 wxDirItemData
*dir_item
;
227 // wxASSERT(m_paths.Count() == m_names.Count()); ?
229 size_t count
= m_paths
.GetCount();
230 for ( size_t i
=0; i
<count
; i
++)
232 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
234 id
= AppendItem( parent
, m_names
[i
], -1, -1, dir_item
);
236 id
= AppendItem( parent
, m_names
[i
], 0, -1, dir_item
);
237 SetItemImage( id
, 1, wxTreeItemIcon_Expanded
);
239 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
243 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
245 // don't rename the main entry "Sections"
246 if (event
.GetItem() == m_rootId
)
252 // don't rename the individual sections
253 if (GetParent( event
.GetItem() ) == m_rootId
)
260 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
262 if ((event
.GetLabel().IsEmpty()) ||
263 (event
.GetLabel() == _(".")) ||
264 (event
.GetLabel() == _("..")) ||
265 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
267 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
273 wxTreeItemId id
= event
.GetItem();
274 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
277 wxString
new_name( wxPathOnly( data
->m_path
) );
278 new_name
+= wxT("/");
279 new_name
+= event
.GetLabel();
283 if (wxFileExists(new_name
))
285 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
290 if (wxRenameFile(data
->m_path
,new_name
))
292 data
->SetNewDirName( new_name
);
296 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
302 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
304 if (event
.GetItem() == m_rootId
)
307 CreateItems(m_rootId
);
311 // This may take a longish time. Go to busy cursor
314 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
320 wxDir
dir(data
->m_path
);
323 bool cont
= dir
.GetFirst(&filename
, "", wxDIR_DIRS
| wxDIR_HIDDEN
);
326 m_paths
.Add(data
->m_path
);
327 m_names
.Add(filename
);
329 cont
= dir
.GetNext(&filename
);
332 CreateItems( event
.GetItem() );
333 SortChildren( event
.GetItem() );
338 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
340 wxTreeItemId child
, parent
= event
.GetItem();
342 /* Workaround because DeleteChildren has disapeared (why?) and
343 * CollapseAndReset doesn't work as advertised (deletes parent too) */
344 child
= GetFirstChild(parent
, cookie
);
348 /* Not GetNextChild below, because the cookie mechanism can't
349 * handle disappearing children! */
350 child
= GetFirstChild(parent
, cookie
);
354 //-----------------------------------------------------------------------------
356 //-----------------------------------------------------------------------------
359 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
361 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
362 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
363 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
364 EVT_SIZE ( wxDirDialog::OnSize
)
365 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
366 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
367 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
368 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
369 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
372 wxDirDialog::wxDirDialog(wxWindow
*parent
,
373 const wxString
& message
,
374 const wxString
& defaultPath
,
377 : wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
378 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
381 m_dialogStyle
= style
;
384 m_path
= defaultPath
;
388 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
391 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, _T("/"),
397 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
400 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
401 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
403 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
404 // m_check->SetValue(TRUE);
408 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
412 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
413 m_ok
= new wxButton( this, ID_OK
, _("OK") );
414 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
415 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
416 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
417 m_new
= new wxButton( this, ID_NEW
, _("New...") );
418 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
420 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
425 SetAutoLayout( TRUE
);
426 SetSizer( topsizer
);
428 topsizer
->SetSizeHints( this );
429 topsizer
->Fit( this );
433 if (m_path
== wxT("~"))
434 wxGetHomeDir( &m_path
);
436 // choose the directory corresponding to defaultPath in the tree
437 // VZ: using wxStringTokenizer is probably unsafe here (escaped slashes
438 // will not be processed correctly...)
439 wxStringTokenizer
tk(m_path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
444 // default to root dir
445 wxTreeItemId item
= m_dir
->GetFirstChild(m_dir
->GetRootItem(), cookie
);
447 if (!m_path
.IsEmpty() && (m_path
!= wxT("/")) && (m_dir
->m_paths
.Count() > 1))
449 size_t count
= m_dir
->m_paths
.GetCount();
450 for ( size_t i
=1; i
<count
; i
++)
452 if (m_path
.Find( m_dir
->m_paths
[i
] ) == 0)
454 path
= m_dir
->m_paths
[i
];
456 for (size_t j
= 0; j
< i
; j
++)
457 item
= m_dir
->GetNextChild(m_dir
->GetRootItem(), cookie
);
459 wxStringTokenizer
tk2(path
, wxFILE_SEP_PATH
, wxTOKEN_STRTOK
);
460 for (size_t h
= 0; h
< tk2
.CountTokens(); h
++)
467 while ( tk
.HasMoreTokens() && item
.IsOk() )
469 path
<< wxFILE_SEP_PATH
<< tk
.GetNextToken();
473 wxTreeItemId child
= m_dir
->GetFirstChild(item
, cookie
);
474 while ( child
.IsOk() )
476 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(child
);
477 if ( data
->m_path
== path
)
480 child
= m_dir
->GetNextChild(item
, cookie
);
489 m_dir
->SelectItem(item
);
490 m_dir
->EnsureVisible(item
);
496 int wxDirDialog::ShowModal()
498 m_input
->SetValue( m_path
);
499 return wxDialog::ShowModal();
502 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
504 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
506 m_input
->SetValue( data
->m_path
);
509 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
511 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
513 m_input
->SetValue( data
->m_path
);
516 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
518 m_path
= m_input
->GetValue();
519 // Does the path exist? (User may have typed anything in m_input)
520 if (wxPathExists(m_path
)) {
521 // OK, path exists, we're done.
525 // Interact with user, find out if the dir is a typo or to be created
526 wxString
msg( _("The directory ") );
528 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
529 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
530 if ( dialog
.ShowModal() == wxID_YES
) {
531 // Okay, let's make it
533 if (wxMkdir(m_path
)) {
534 // The new dir was created okay.
540 msg
= _("Failed to create directory ")+m_path
+
541 _("\n(Do you have the required permissions?)");
542 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
544 // We still don't have a valid dir. Back to the main dialog.
547 // User has answered NO to create dir.
550 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
552 EndModal(wxID_CANCEL
);
555 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
557 wxTreeItemId id
= m_dir
->GetSelection();
558 if ((id
== m_dir
->GetRootItem()) ||
559 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
561 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
562 _("Create directory"), wxOK
| wxICON_INFORMATION
);
567 wxTreeItemId parent
= m_dir
->GetParent( id
);
568 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
571 wxString
new_name( wxT("NewName") );
572 wxString
path( data
->m_path
);
575 if (wxFileExists(path
))
577 // try NewName0, NewName1 etc.
580 new_name
= wxT("NewName");
582 num
.Printf( wxT("%d"), i
);
589 } while (wxFileExists(path
));
595 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
600 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
601 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
602 m_dir
->EnsureVisible( new_id
);
603 m_dir
->EditLabel( new_id
);
607 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
609 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
613 #endif // wxUSE_DIRDLG