]>
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"
43 #include "wx/statline.h"
46 #include "wx/generic/dirdlgg.h"
48 // If compiled under Windows, this macro can cause problems
54 static char * icon1_xpm
[] = {
55 /* width height ncolors chars_per_pixel */
83 static char * icon2_xpm
[] = {
84 /* width height ncolors chars_per_pixel */
111 static const int ID_DIRCTRL
= 1000;
112 static const int ID_TEXTCTRL
= 1001;
113 static const int ID_OK
= 1002;
114 static const int ID_CANCEL
= 1003;
115 static const int ID_NEW
= 1004;
116 //static const int ID_CHECK = 1005;
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
126 /* Insert logic to detect hidden files here
127 * In UnixLand we just check whether the first char is a dot
128 * For FileNameFromPath read LastDirNameInThisPath ;-) */
129 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
131 m_hasSubDirs
= HasSubDirs();
134 wxDirItemData::~wxDirItemData()
138 void wxDirItemData::SetNewDirName( wxString path
)
141 m_name
= wxFileNameFromPath( path
);
144 bool wxDirItemData::HasSubDirs()
146 wxString search
= m_path
+ "/*";
148 wxString path
= wxFindFirstFile( search
, wxDIR
);
149 return (bool)(!path
.IsNull());
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
156 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
158 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
159 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
160 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
161 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem
)
162 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem
)
165 wxDirCtrl::wxDirCtrl(void)
167 m_showHidden
= FALSE
;
170 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
171 const wxPoint
& pos
, const wxSize
& size
,
172 const long style
, const wxString
& name
)
174 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
177 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
178 m_imageListNormal
->Add(wxICON(icon1
));
179 m_imageListNormal
->Add(wxICON(icon2
));
180 SetImageList(m_imageListNormal
);
183 m_showHidden
= FALSE
;
184 m_rootId
= AddRoot( _("Sections") );
185 SetItemHasChildren(m_rootId
);
186 Expand(m_rootId
); // automatically expand first level
189 /* Quick macro. Don't worry, I'll #undef it later */
190 #define ADD_SECTION(a,b) \
191 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
193 void wxDirCtrl::SetupSections()
200 // better than nothing
201 ADD_SECTION(T("c:\\"), _("My Harddisk") )
203 ADD_SECTION(T("/"), _("The Computer") )
205 ADD_SECTION(home
, _("My Home") )
206 ADD_SECTION(T("/mnt"), _("Mounted Devices") )
207 ADD_SECTION(T("/usr"), _("User") )
208 ADD_SECTION(T("/usr/local"), _("User Local") )
209 ADD_SECTION(T("/var"), _("Variables") )
210 ADD_SECTION(T("/etc"), _("Etcetera") )
211 ADD_SECTION(T("/tmp"), _("Temporary") )
216 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
219 wxDirItemData
*dir_item
;
221 // wxASSERT(m_paths.Count() == m_names.Count()); ?
223 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
225 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
227 id
= AppendItem( parent
, m_names
[i
], -1, -1, dir_item
);
229 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
231 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
235 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
237 // don't rename the main entry "Sections"
238 if (event
.GetItem() == m_rootId
)
244 // don't rename the individual sections
245 if (GetParent( event
.GetItem() ) == m_rootId
)
252 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
254 if ((event
.GetLabel().IsEmpty()) ||
255 (event
.GetLabel() == _(".")) ||
256 (event
.GetLabel() == _("..")) ||
257 (event
.GetLabel().First( T("/") ) != wxNOT_FOUND
))
259 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
265 wxTreeItemId id
= event
.GetItem();
266 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
269 wxString
new_name( wxPathOnly( data
->m_path
) );
271 new_name
+= event
.GetLabel();
275 if (wxFileExists(new_name
))
277 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
282 if (wxRenameFile(data
->m_path
,new_name
))
284 data
->SetNewDirName( new_name
);
288 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
294 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
296 if (event
.GetItem() == m_rootId
)
299 CreateItems(m_rootId
);
303 // This may take a longish time. Go to busy cursor
306 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
309 wxString search
,path
,filename
;
313 search
= data
->m_path
+ "/*";
314 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
315 path
=wxFindNextFile() )
317 filename
= wxFileNameFromPath( path
);
318 /* Don't add "." and ".." to the tree. I think wxFindNextFile
319 * also checks this, but I don't quite understand what happens
320 * there. Also wxFindNextFile seems to swallow hidden dirs */
321 if ((filename
!= ".") && (filename
!= ".."))
324 m_names
.Add(filename
);
328 CreateItems( event
.GetItem() );
329 SortChildren( event
.GetItem() );
334 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
336 wxTreeItemId child
, parent
= event
.GetItem();
338 /* Workaround because DeleteChildren has disapeared (why?) and
339 * CollapseAndReset doesn't work as advertised (deletes parent too) */
340 child
= GetFirstChild(parent
, cookie
);
344 /* Not GetNextChild below, because the cookie mechanism can't
345 * handle disappearing children! */
346 child
= GetFirstChild(parent
, cookie
);
350 //-----------------------------------------------------------------------------
352 //-----------------------------------------------------------------------------
355 #if !USE_SHARED_LIBRARY
356 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
358 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
, const wxString
& message
,
373 const wxString
& defaultPath
, long style
,
374 const wxPoint
& pos
) :
375 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
376 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
379 m_dialogStyle
= style
;
382 m_path
= defaultPath
;
386 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
389 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxSize(200,200),
390 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
| wxTR_EDIT_LABELS
);
391 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
394 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
395 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
397 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
398 // m_check->SetValue(TRUE);
402 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
406 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
407 m_ok
= new wxButton( this, ID_OK
, _("OK") );
408 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
409 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
410 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
411 m_new
= new wxButton( this, ID_NEW
, _("New...") );
412 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
414 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
419 SetAutoLayout( TRUE
);
420 SetSizer( topsizer
);
422 topsizer
->SetSizeHints( this );
423 topsizer
->Fit( this );
430 int wxDirDialog::ShowModal()
432 m_input
->SetValue( m_path
);
433 return wxDialog::ShowModal();
436 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
438 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
440 m_input
->SetValue( data
->m_path
);
443 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
445 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
447 m_input
->SetValue( data
->m_path
);
450 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
452 m_path
= m_input
->GetValue();
453 // Does the path exist? (User may have typed anything in m_input)
454 if (wxPathExists(m_path
)) {
455 // OK, path exists, we're done.
459 // Interact with user, find out if the dir is a typo or to be created
460 wxString
msg( _("The directory ") );
462 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
463 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
464 if ( dialog
.ShowModal() == wxID_YES
) {
465 // Okay, let's make it
467 if (wxMkdir(m_path
)) {
468 // The new dir was created okay.
474 msg
= _("Failed to create directory ")+m_path
+
475 _("\n(Do you have the required permissions?)");
476 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
478 // We still don't have a valid dir. Back to the main dialog.
481 // User has answered NO to create dir.
484 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
486 EndModal(wxID_CANCEL
);
489 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
491 wxTreeItemId id
= m_dir
->GetSelection();
492 if ((id
== m_dir
->GetRootItem()) ||
493 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
495 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
496 _("Create directory"), wxOK
| wxICON_INFORMATION
);
501 wxTreeItemId parent
= m_dir
->GetParent( id
);
502 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
505 wxString
new_name( T("NewName") );
506 wxString
path( data
->m_path
);
509 if (wxFileExists(path
))
511 // try NewName0, NewName1 etc.
514 new_name
= T("NewName");
516 num
.Printf( "%d", i
);
523 } while (wxFileExists(path
));
529 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
534 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
535 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
536 m_dir
->EnsureVisible( new_id
);
537 m_dir
->EditLabel( new_id
);
541 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
543 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );