]>
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
55 static char * icon1_xpm
[] = {
56 /* width height ncolors chars_per_pixel */
84 static char * icon2_xpm
[] = {
85 /* width height ncolors chars_per_pixel */
114 static const int ID_DIRCTRL
= 1000;
115 static const int ID_TEXTCTRL
= 1001;
116 static const int ID_OK
= 1002;
117 static const int ID_CANCEL
= 1003;
118 static const int ID_NEW
= 1004;
119 //static const int ID_CHECK = 1005;
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
125 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
129 /* Insert logic to detect hidden files here
130 * In UnixLand we just check whether the first char is a dot
131 * For FileNameFromPath read LastDirNameInThisPath ;-) */
132 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
134 m_hasSubDirs
= HasSubDirs();
137 wxDirItemData::~wxDirItemData()
141 void wxDirItemData::SetNewDirName( wxString path
)
144 m_name
= wxFileNameFromPath( path
);
147 bool wxDirItemData::HasSubDirs()
149 wxString search
= m_path
+ wxT("/*");
151 wxString path
= wxFindFirstFile( search
, wxDIR
);
152 return (bool)(!path
.IsNull());
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
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
174 const wxPoint
& pos
, const wxSize
& size
,
175 const long style
, const wxString
& name
)
177 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
180 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
181 m_imageListNormal
->Add(wxICON(icon1
));
182 m_imageListNormal
->Add(wxICON(icon2
));
183 SetImageList(m_imageListNormal
);
186 m_showHidden
= FALSE
;
187 m_rootId
= AddRoot( _("Sections") );
188 SetItemHasChildren(m_rootId
);
189 Expand(m_rootId
); // automatically expand first level
192 /* Quick macro. Don't worry, I'll #undef it later */
193 #define ADD_SECTION(a,b) \
194 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
196 void wxDirCtrl::SetupSections()
203 // better than nothing
204 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
206 ADD_SECTION(wxT("/"), _("The Computer") )
208 ADD_SECTION(home
, _("My Home") )
209 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
210 ADD_SECTION(wxT("/usr"), _("User") )
211 ADD_SECTION(wxT("/usr/local"), _("User Local") )
212 ADD_SECTION(wxT("/var"), _("Variables") )
213 ADD_SECTION(wxT("/etc"), _("Etcetera") )
214 ADD_SECTION(wxT("/tmp"), _("Temporary") )
219 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
222 wxDirItemData
*dir_item
;
224 // wxASSERT(m_paths.Count() == m_names.Count()); ?
226 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
228 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
230 id
= AppendItem( parent
, m_names
[i
], -1, -1, dir_item
);
232 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
234 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
238 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
240 // don't rename the main entry "Sections"
241 if (event
.GetItem() == m_rootId
)
247 // don't rename the individual sections
248 if (GetParent( event
.GetItem() ) == m_rootId
)
255 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
257 if ((event
.GetLabel().IsEmpty()) ||
258 (event
.GetLabel() == _(".")) ||
259 (event
.GetLabel() == _("..")) ||
260 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
262 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
268 wxTreeItemId id
= event
.GetItem();
269 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
272 wxString
new_name( wxPathOnly( data
->m_path
) );
273 new_name
+= wxT("/");
274 new_name
+= event
.GetLabel();
278 if (wxFileExists(new_name
))
280 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
285 if (wxRenameFile(data
->m_path
,new_name
))
287 data
->SetNewDirName( new_name
);
291 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
297 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
299 if (event
.GetItem() == m_rootId
)
302 CreateItems(m_rootId
);
306 // This may take a longish time. Go to busy cursor
309 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
312 wxString search
,path
,filename
;
317 search
= data
->m_path
+ "\\*.*";
319 search
= data
->m_path
+ "/*";
321 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
322 path
=wxFindNextFile() )
324 filename
= wxFileNameFromPath( path
);
325 /* Don't add "." and ".." to the tree. I think wxFindNextFile
326 * also checks this, but I don't quite understand what happens
327 * there. Also wxFindNextFile seems to swallow hidden dirs */
328 if ((filename
!= ".") && (filename
!= ".."))
331 m_names
.Add(filename
);
335 CreateItems( event
.GetItem() );
336 SortChildren( event
.GetItem() );
341 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
343 wxTreeItemId child
, parent
= event
.GetItem();
345 /* Workaround because DeleteChildren has disapeared (why?) and
346 * CollapseAndReset doesn't work as advertised (deletes parent too) */
347 child
= GetFirstChild(parent
, cookie
);
351 /* Not GetNextChild below, because the cookie mechanism can't
352 * handle disappearing children! */
353 child
= GetFirstChild(parent
, cookie
);
357 //-----------------------------------------------------------------------------
359 //-----------------------------------------------------------------------------
362 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
364 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
365 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
366 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
367 EVT_SIZE ( wxDirDialog::OnSize
)
368 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
369 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
370 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
371 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
372 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
375 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
376 const wxString
& defaultPath
, long style
,
377 const wxPoint
& pos
) :
378 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
379 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
382 m_dialogStyle
= style
;
385 m_path
= defaultPath
;
389 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
392 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxSize(200,200),
393 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
| wxTR_EDIT_LABELS
);
394 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
397 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
398 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
400 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
401 // m_check->SetValue(TRUE);
405 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
409 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
410 m_ok
= new wxButton( this, ID_OK
, _("OK") );
411 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
412 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
413 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
414 m_new
= new wxButton( this, ID_NEW
, _("New...") );
415 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
417 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
422 SetAutoLayout( TRUE
);
423 SetSizer( topsizer
);
425 topsizer
->SetSizeHints( this );
426 topsizer
->Fit( this );
433 int wxDirDialog::ShowModal()
435 m_input
->SetValue( m_path
);
436 return wxDialog::ShowModal();
439 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
441 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
443 m_input
->SetValue( data
->m_path
);
446 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
448 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
450 m_input
->SetValue( data
->m_path
);
453 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
455 m_path
= m_input
->GetValue();
456 // Does the path exist? (User may have typed anything in m_input)
457 if (wxPathExists(m_path
)) {
458 // OK, path exists, we're done.
462 // Interact with user, find out if the dir is a typo or to be created
463 wxString
msg( _("The directory ") );
465 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
466 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
467 if ( dialog
.ShowModal() == wxID_YES
) {
468 // Okay, let's make it
470 if (wxMkdir(m_path
)) {
471 // The new dir was created okay.
477 msg
= _("Failed to create directory ")+m_path
+
478 _("\n(Do you have the required permissions?)");
479 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
481 // We still don't have a valid dir. Back to the main dialog.
484 // User has answered NO to create dir.
487 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
489 EndModal(wxID_CANCEL
);
492 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
494 wxTreeItemId id
= m_dir
->GetSelection();
495 if ((id
== m_dir
->GetRootItem()) ||
496 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
498 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
499 _("Create directory"), wxOK
| wxICON_INFORMATION
);
504 wxTreeItemId parent
= m_dir
->GetParent( id
);
505 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
508 wxString
new_name( wxT("NewName") );
509 wxString
path( data
->m_path
);
512 if (wxFileExists(path
))
514 // try NewName0, NewName1 etc.
517 new_name
= wxT("NewName");
519 num
.Printf( wxT("%d"), i
);
526 } while (wxFileExists(path
));
532 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
537 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
538 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
539 m_dir
->EnsureVisible( new_id
);
540 m_dir
->EditLabel( new_id
);
544 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
546 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );