]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
02f91965e4bd9d4375b19a45e78e04f37586a3c9
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDirDialog
4 // Author: Harm van der Heijden and Robert Roebling
7 // Copyright: (c) Harm van der Heijden and Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "dirdlgg.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/dialog.h"
28 #include "wx/button.h"
29 #include "wx/layout.h"
30 #include "wx/msgdlg.h"
31 #include "wx/textdlg.h"
32 #include "wx/filefn.h"
33 #include "wx/cmndata.h"
34 #include "wx/gdicmn.h"
36 #include "wx/imaglist.h"
42 #include "wx/statline.h"
45 #include "wx/generic/dirdlgg.h"
47 // If compiled under Windows, this macro can cause problems
53 static char * icon1_xpm
[] = {
54 /* width height ncolors chars_per_pixel */
82 static char * icon2_xpm
[] = {
83 /* width height ncolors chars_per_pixel */
110 static const int ID_DIRCTRL
= 1000;
111 static const int ID_TEXTCTRL
= 1001;
112 static const int ID_OK
= 1002;
113 static const int ID_CANCEL
= 1003;
114 static const int ID_NEW
= 1004;
115 //static const int ID_CHECK = 1005;
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
125 /* Insert logic to detect hidden files here
126 * In UnixLand we just check whether the first char is a dot
127 * For FileNameFromPath read LastDirNameInThisPath ;-) */
128 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
130 m_hasSubDirs
= HasSubDirs();
133 wxDirItemData::~wxDirItemData()
137 void wxDirItemData::SetNewDirName( wxString path
)
140 m_name
= wxFileNameFromPath( path
);
143 bool wxDirItemData::HasSubDirs()
145 wxString search
= m_path
+ "/*";
147 wxString path
= wxFindFirstFile( search
, wxDIR
);
148 return (bool)(!path
.IsNull());
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
157 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
158 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
159 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
160 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem
)
161 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem
)
164 wxDirCtrl::wxDirCtrl(void)
166 m_showHidden
= FALSE
;
169 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
170 const wxPoint
& pos
, const wxSize
& size
,
171 const long style
, const wxString
& name
)
173 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
175 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
176 m_imageListNormal
->Add(wxICON(icon1
));
177 m_imageListNormal
->Add(wxICON(icon2
));
178 SetImageList(m_imageListNormal
);
180 m_showHidden
= FALSE
;
181 m_rootId
= AddRoot( _("Sections") );
182 SetItemHasChildren(m_rootId
);
183 Expand(m_rootId
); // automatically expand first level
186 /* Quick macro. Don't worry, I'll #undef it later */
187 #define ADD_SECTION(a,b) \
188 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
190 void wxDirCtrl::SetupSections()
196 ADD_SECTION(_T("/"), _("The Computer") )
198 ADD_SECTION(home
, _("My Home") )
199 ADD_SECTION(_T("/mnt"), _("Mounted Devices") )
200 ADD_SECTION(_T("/usr"), _("User") )
201 ADD_SECTION(_T("/usr/local"), _("User Local") )
202 ADD_SECTION(_T("/var"), _("Variables") )
203 ADD_SECTION(_T("/etc"), _("Etcetera") )
204 ADD_SECTION(_T("/tmp"), _("Temporary") )
208 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
211 wxDirItemData
*dir_item
;
213 // wxASSERT(m_paths.Count() == m_names.Count()); ?
215 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
217 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
218 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
219 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
223 void wxDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
225 // don't rename the main entry "Sections"
226 if (event
.GetItem() == m_rootId
)
232 // don't rename the individual sections
233 if (GetParent( event
.GetItem() ) == m_rootId
)
240 void wxDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
242 if ((event
.GetLabel().IsEmpty()) ||
243 (event
.GetLabel() == _(".")) ||
244 (event
.GetLabel() == _("..")) ||
245 (event
.GetLabel().First( _T("/") ) != wxNOT_FOUND
))
247 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
253 wxTreeItemId id
= event
.GetItem();
254 wxDirItemData
*data
= (wxDirItemData
*)GetItemData( id
);
257 wxString
new_name( wxPathOnly( data
->m_path
) );
259 new_name
+= event
.GetLabel();
263 if (wxFileExists(new_name
))
265 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
270 if (wxRenameFile(data
->m_path
,new_name
))
272 data
->SetNewDirName( new_name
);
276 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
282 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
284 if (event
.GetItem() == m_rootId
)
287 CreateItems(m_rootId
);
291 // This may take a longish time. Go to busy cursor
294 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
297 wxString search
,path
,filename
;
301 search
= data
->m_path
+ "/*";
302 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
303 path
=wxFindNextFile() )
305 filename
= wxFileNameFromPath( path
);
306 /* Don't add "." and ".." to the tree. I think wxFindNextFile
307 * also checks this, but I don't quite understand what happens
308 * there. Also wxFindNextFile seems to swallow hidden dirs */
309 if ((filename
!= ".") && (filename
!= ".."))
312 m_names
.Add(filename
);
316 CreateItems( event
.GetItem() );
317 SortChildren( event
.GetItem() );
322 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
324 wxTreeItemId child
, parent
= event
.GetItem();
326 /* Workaround because DeleteChildren has disapeared (why?) and
327 * CollapseAndReset doesn't work as advertised (deletes parent too) */
328 child
= GetFirstChild(parent
, cookie
);
332 /* Not GetNextChild below, because the cookie mechanism can't
333 * handle disappearing children! */
334 child
= GetFirstChild(parent
, cookie
);
338 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
343 #if !USE_SHARED_LIBRARY
344 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
346 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
349 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
350 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
351 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
352 EVT_SIZE ( wxDirDialog::OnSize
)
353 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
354 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
355 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
356 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
357 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
360 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
361 const wxString
& defaultPath
, long style
,
362 const wxPoint
& pos
) :
363 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
364 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
367 m_dialogStyle
= style
;
370 m_path
= defaultPath
;
374 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
377 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxSize(200,200),
378 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
| wxTR_EDIT_LABELS
);
379 topsizer
->Add( m_dir
, 1, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
382 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
383 topsizer
->Add( m_input
, 0, wxTOP
|wxLEFT
|wxRIGHT
| wxEXPAND
, 10 );
385 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
386 // m_check->SetValue(TRUE);
390 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
394 wxSizer
* buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
395 m_ok
= new wxButton( this, ID_OK
, _("OK") );
396 buttonsizer
->Add( m_ok
, 0, wxLEFT
|wxRIGHT
, 10 );
397 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
398 buttonsizer
->Add( m_cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
399 m_new
= new wxButton( this, ID_NEW
, _("New...") );
400 buttonsizer
->Add( m_new
, 0, wxLEFT
|wxRIGHT
, 10 );
402 topsizer
->Add( buttonsizer
, 0, wxALL
| wxCENTER
, 10 );
407 SetAutoLayout( TRUE
);
408 SetSizer( topsizer
);
410 topsizer
->SetSizeHints( this );
411 topsizer
->Fit( this );
418 int wxDirDialog::ShowModal()
420 m_input
->SetValue( m_path
);
421 return wxDialog::ShowModal();
424 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
426 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
428 m_input
->SetValue( data
->m_path
);
431 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
433 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
435 m_input
->SetValue( data
->m_path
);
438 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
440 m_path
= m_input
->GetValue();
441 // Does the path exist? (User may have typed anything in m_input)
442 if (wxPathExists(m_path
)) {
443 // OK, path exists, we're done.
447 // Interact with user, find out if the dir is a typo or to be created
448 wxString
msg( _("The directory ") );
450 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
451 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
| wxICON_WARNING
);
452 if ( dialog
.ShowModal() == wxID_YES
) {
453 // Okay, let's make it
455 if (wxMkdir(m_path
)) {
456 // The new dir was created okay.
462 msg
= _("Failed to create directory ")+m_path
+
463 _("\n(Do you have the required permissions?)");
464 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
| wxICON_ERROR
);
466 // We still don't have a valid dir. Back to the main dialog.
469 // User has answered NO to create dir.
472 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
474 EndModal(wxID_CANCEL
);
477 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
479 wxTreeItemId id
= m_dir
->GetSelection();
480 if ((id
== m_dir
->GetRootItem()) ||
481 (m_dir
->GetParent(id
) == m_dir
->GetRootItem()))
483 wxMessageDialog
msg(this, _("You cannot add a new directory to this section."),
484 _("Create directory"), wxOK
| wxICON_INFORMATION
);
489 wxTreeItemId parent
= m_dir
->GetParent( id
);
490 wxDirItemData
*data
= (wxDirItemData
*)m_dir
->GetItemData( parent
);
493 wxString
new_name( _T("NewName") );
494 wxString
path( data
->m_path
);
497 if (wxFileExists(path
))
499 // try NewName0, NewName1 etc.
502 new_name
= _T("NewName");
504 num
.Printf( "%d", i
);
511 } while (wxFileExists(path
));
517 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
522 wxDirItemData
*new_data
= new wxDirItemData( path
, new_name
);
523 wxTreeItemId new_id
= m_dir
->AppendItem( parent
, new_name
, 0, 1, new_data
);
524 m_dir
->EnsureVisible( new_id
);
525 m_dir
->EditLabel( new_id
);
529 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
531 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );