]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
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"
24 #include "wx/dialog.h"
25 #include "wx/button.h"
26 #include "wx/layout.h"
27 #include "wx/msgdlg.h"
28 #include "wx/textdlg.h"
29 #include "wx/filefn.h"
30 #include "wx/cmndata.h"
31 #include "wx/gdicmn.h"
33 #include "wx/imaglist.h"
36 #include "wx/generic/dirdlgg.h"
38 // If compiled under Windows, this macro can cause problems
44 static char * icon1_xpm
[] = {
45 /* width height ncolors chars_per_pixel */
73 static char * icon2_xpm
[] = {
74 /* width height ncolors chars_per_pixel */
101 static const int ID_DIRCTRL
= 1000;
102 static const int ID_TEXTCTRL
= 1001;
103 static const int ID_OK
= 1002;
104 static const int ID_CANCEL
= 1003;
105 static const int ID_NEW
= 1004;
106 //static const int ID_CHECK = 1005;
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
112 class wxDirItemData
: public wxTreeItemData
115 wxDirItemData(wxString
& path
, wxString
& name
);
118 wxString
*m_path
, *m_name
;
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 class wxDirCtrl
: public wxTreeCtrl
129 DECLARE_DYNAMIC_CLASS(wxDirCtrl
)
133 wxTreeItemId m_rootId
;
136 wxDirCtrl(wxWindow
*parent
, const wxWindowID id
= -1,
137 const wxString
&dir
= "/",
138 const wxPoint
& pos
= wxDefaultPosition
,
139 const wxSize
& size
= wxDefaultSize
,
140 const long style
= wxTR_HAS_BUTTONS
,
141 const wxString
& name
= "wxTreeCtrl" );
142 void OnExpandItem(wxTreeEvent
&event
);
143 void OnCollapseItem(wxTreeEvent
&event
);
144 void ShowHidden( const bool yesno
);
145 DECLARE_EVENT_TABLE()
147 void CreateItems(const wxTreeItemId
&parent
);
148 void SetupSections(void);
149 wxArrayString m_paths
, m_names
;
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
156 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
158 m_path
= new wxString(path
);
159 m_name
= new wxString(name
);
160 /* Insert logic to detect hidden files here
161 * In UnixLand we just check whether the first char is a dot
162 * For FileNameFromPath read LastDirNameInThisPath ;-) */
163 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
165 m_hasSubDirs
= HasSubDirs();
168 wxDirItemData:: ~wxDirItemData()
174 bool wxDirItemData::HasSubDirs()
176 wxString search
= *m_path
+ "/*";
177 wxString path
= wxFindFirstFile( search
, wxDIR
);
178 return (bool)(!path
.IsNull());
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
187 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
188 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
189 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
192 wxDirCtrl::wxDirCtrl(void)
194 m_showHidden
= FALSE
;
197 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
198 const wxPoint
& pos
, const wxSize
& size
,
199 const long style
, const wxString
& name
)
201 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
203 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
204 m_imageListNormal
->Add(wxICON(icon1
));
205 m_imageListNormal
->Add(wxICON(icon2
));
206 SetImageList(m_imageListNormal
);
208 m_showHidden
= FALSE
;
209 m_rootId
= AddRoot("Sections");
210 SetItemHasChildren(m_rootId
);
211 Expand(m_rootId
); // automatically expand first level
214 /* Quick macro. Don't worry, I'll #undef it later */
215 #define ADD_SECTION(a,b) \
216 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
218 void wxDirCtrl::SetupSections()
224 ADD_SECTION(_T("/"), _("The Computer") )
226 ADD_SECTION(home
, _("My Home") )
227 ADD_SECTION(_T("/mnt"), _("Mounted Devices") )
228 ADD_SECTION(_T("/usr"), _("User") )
229 ADD_SECTION(_T("/usr/local"), _("User Local") )
230 ADD_SECTION(_T("/var"), _("Variables") )
231 ADD_SECTION(_T("/etc"), _("Etcetera") )
232 ADD_SECTION(_T("/tmp"), _("Temporary") )
236 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
239 wxDirItemData
*dir_item
;
241 // wxASSERT(m_paths.Count() == m_names.Count()); ?
243 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
245 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
246 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
247 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
251 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
253 if (event
.GetItem() == m_rootId
)
256 CreateItems(m_rootId
);
260 // This may take a longish time. Go to busy cursor
263 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
266 wxString search
,path
,filename
;
270 search
= *(data
->m_path
) + "/*";
271 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
272 path
=wxFindNextFile() ) {
273 filename
= wxFileNameFromPath( path
);
274 /* Don't add "." and ".." to the tree. I think wxFindNextFile
275 * also checks this, but I don't quite understand what happens
276 * there. Also wxFindNextFile seems to swallow hidden dirs */
277 if ((filename
!= ".") && (filename
!= "..")) {
279 m_names
.Add(filename
);
282 CreateItems(event
.GetItem());
285 SortChildren( event
.GetItem() );
289 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
291 wxTreeItemId child
, parent
= event
.GetItem();
293 /* Workaround because DeleteChildren has disapeared (why?) and
294 * CollapseAndReset doesn't work as advertised (deletes parent too) */
295 child
= GetFirstChild(parent
, cookie
);
296 while (child
.IsOk()) {
298 /* Not GetNextChild below, because the cookie mechanism can't
299 * handle disappearing children! */
300 child
= GetFirstChild(parent
, cookie
);
304 //-----------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------
309 #if !USE_SHARED_LIBRARY
310 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
312 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
315 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
316 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
317 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
318 EVT_SIZE ( wxDirDialog::OnSize
)
319 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
320 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
321 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
322 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
323 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
326 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
327 const wxString
& defaultPath
, long style
,
328 const wxPoint
& pos
) :
329 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
330 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
333 m_dialogStyle
= style
;
336 m_path
= defaultPath
;
338 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxDefaultSize
, wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
339 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
340 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
341 m_ok
= new wxButton( this, ID_OK
, _("OK") );
342 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxSize(75,-1) );
343 m_new
= new wxButton( this, ID_NEW
, _("New...") );
345 // m_check->SetValue(TRUE);
352 void wxDirDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
357 void wxDirDialog::doSize()
359 /* Figure out height of DirCtrl, which is what is left over by
360 * the textctrl and the buttons. Manually, because I can't seem
361 * to get the constraints stuff to do this */
364 GetClientSize(&w
, &h
);
365 m_input
->GetSize(&w
,&h2
); h
-= h2
;
366 m_ok
->GetSize(&w
, &h2
); h
-= h2
;
367 //m_check->GetSize(&w, &h2); h -= h2;
370 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
371 c
->left
.SameAs (this, wxLeft
,5);
372 c
->right
.SameAs (this, wxRight
,5);
373 c
->height
.Absolute (h
);
374 c
->top
.SameAs (this, wxTop
,5);
375 m_dir
->SetConstraints(c
);
377 c
= new wxLayoutConstraints
;
378 c
->left
.SameAs (this, wxLeft
,5);
379 c
->right
.SameAs (this, wxRight
,5);
381 c
->top
.Below (m_dir
,5);
382 m_input
->SetConstraints(c
);
384 /* c = new wxLayoutConstraints;
385 c->left.SameAs (this, wxLeft,5);
386 c->right.SameAs (this, wxRight,5);
388 c->top.Below (m_input,5);
389 m_check->SetConstraints(c); */
391 c
= new wxLayoutConstraints
;
392 c
->width
.SameAs (m_cancel
, wxWidth
);
394 c
->top
.Below (m_input
,10);
395 c
->centreX
.PercentOf (this, wxWidth
, 20);
396 m_ok
->SetConstraints(c
);
398 c
= new wxLayoutConstraints
;
399 c
->width
.SameAs (m_cancel
, wxWidth
);
401 c
->top
.Below (m_input
,10);
402 c
->bottom
.SameAs (this, wxBottom
, 5);
403 c
->centreX
.PercentOf (this, wxWidth
, 50);
404 m_new
->SetConstraints(c
);
406 c
= new wxLayoutConstraints
;
409 c
->top
.Below (m_input
,10);
410 c
->centreX
.PercentOf (this, wxWidth
, 80);
411 m_cancel
->SetConstraints(c
);
416 int wxDirDialog::ShowModal()
418 m_input
->SetValue( m_path
);
419 return wxDialog::ShowModal();
422 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
424 wxDirItemData
*data
=
425 (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
427 m_input
->SetValue( *(data
->m_path
) );
430 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
432 wxDirItemData
*data
=
433 (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
);
452 if ( dialog
.ShowModal() == wxID_YES
) {
453 // Okay, let's make it
454 if (wxMkdir(m_path
)) {
455 // The new dir was created okay.
461 msg
= _("Failed to create directory ")+m_path
+
462 _("\n(Do you have the required permissions?)");
463 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
465 // We still don't have a valid dir. Back to the main dialog.
468 // User has answered NO to create dir.
471 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
473 EndModal(wxID_CANCEL
);
476 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
478 wxTextEntryDialog
dialog(this, _("Enter the name of the directory to create"),
479 _("Create New Directory"), m_input
->GetValue(), wxOK
|wxCANCEL
);
481 while (dialog
.ShowModal() == wxID_OK
)
483 // Okay, let's make it
484 if (wxMkdir(dialog
.GetValue())) {
485 // The new dir was created okay.
486 m_path
= dialog
.GetValue();
489 wxString msg
= _("Failed to create directory ")+dialog
.GetValue()+
490 _("\n(Do you have the required permissions?)") ;
491 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
493 // Show the create dialog again, until user clicks cancel or enters
499 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
501 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );