]>
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"
35 #include "wx/generic/dirdlgg.h"
37 // If compiled under Windows, this macro can cause problems
43 static char * icon1_xpm
[] = {
44 /* width height ncolors chars_per_pixel */
72 static char * icon2_xpm
[] = {
73 /* width height ncolors chars_per_pixel */
100 static const int ID_DIRCTRL
= 1000;
101 static const int ID_TEXTCTRL
= 1001;
102 static const int ID_OK
= 1002;
103 static const int ID_CANCEL
= 1003;
104 static const int ID_NEW
= 1004;
105 //static const int ID_CHECK = 1005;
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 class wxDirItemData
: public wxTreeItemData
114 wxDirItemData(wxString
& path
, wxString
& name
);
117 wxString
*m_path
, *m_name
;
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 class wxDirCtrl
: public wxTreeCtrl
128 DECLARE_DYNAMIC_CLASS(wxDirCtrl
)
132 wxTreeItemId m_rootId
;
135 wxDirCtrl(wxWindow
*parent
, const wxWindowID id
= -1,
136 const wxString
&dir
= "/",
137 const wxPoint
& pos
= wxDefaultPosition
,
138 const wxSize
& size
= wxDefaultSize
,
139 const long style
= wxTR_HAS_BUTTONS
,
140 const wxString
& name
= "wxTreeCtrl" );
141 void OnExpandItem(wxTreeEvent
&event
);
142 void OnCollapseItem(wxTreeEvent
&event
);
143 void ShowHidden( const bool yesno
);
144 DECLARE_EVENT_TABLE()
146 void CreateItems(const wxTreeItemId
&parent
);
147 void SetupSections(void);
148 wxArrayString m_paths
, m_names
;
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
157 m_path
= new wxString(path
);
158 m_name
= new wxString(name
);
159 /* Insert logic to detect hidden files here
160 * In UnixLand we just check whether the first char is a dot
161 * For FileNameFromPath read LastDirNameInThisPath ;-) */
162 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
164 m_hasSubDirs
= HasSubDirs();
167 wxDirItemData:: ~wxDirItemData()
173 bool wxDirItemData::HasSubDirs()
175 wxString search
= *m_path
+ "/*";
176 wxString path
= wxFindFirstFile( search
, wxDIR
);
177 return (bool)(!path
.IsNull());
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
186 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
187 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
188 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
191 wxDirCtrl::wxDirCtrl(void)
193 m_showHidden
= FALSE
;
196 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
197 const wxPoint
& pos
, const wxSize
& size
,
198 const long style
, const wxString
& name
)
200 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
202 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
203 m_imageListNormal
->Add(wxICON(icon1
));
204 m_imageListNormal
->Add(wxICON(icon2
));
205 SetImageList(m_imageListNormal
);
207 m_showHidden
= FALSE
;
208 m_rootId
= AddRoot("Sections");
209 SetItemHasChildren(m_rootId
);
210 Expand(m_rootId
); // automatically expand first level
213 /* Quick macro. Don't worry, I'll #undef it later */
214 #define ADD_SECTION(a,b) \
215 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
217 void wxDirCtrl::SetupSections()
223 ADD_SECTION("/", _("The Computer") )
225 ADD_SECTION(home
, _("My Home") )
226 ADD_SECTION("/mnt", _("Mounted Devices") )
227 ADD_SECTION("/usr", _("User") )
228 ADD_SECTION("/usr/local", _("User Local") )
229 ADD_SECTION("/var", _("Variables") )
230 ADD_SECTION("/etc", _("Etcetera") )
231 ADD_SECTION("/tmp", _("Temporary") )
235 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
238 wxDirItemData
*dir_item
;
240 // wxASSERT(m_paths.Count() == m_names.Count()); ?
242 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
244 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
245 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
246 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
250 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
252 if (event
.GetItem() == m_rootId
)
255 CreateItems(m_rootId
);
259 // This may take a longish time. Go to busy cursor
262 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
265 wxString search
,path
,filename
;
269 search
= *(data
->m_path
) + "/*";
270 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
271 path
=wxFindNextFile() ) {
272 filename
= wxFileNameFromPath( path
);
273 /* Don't add "." and ".." to the tree. I think wxFindNextFile
274 * also checks this, but I don't quite understand what happens
275 * there. Also wxFindNextFile seems to swallow hidden dirs */
276 if ((filename
!= ".") && (filename
!= "..")) {
278 m_names
.Add(filename
);
281 CreateItems(event
.GetItem());
284 SortChildren( event
.GetItem() );
288 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
290 wxTreeItemId child
, parent
= event
.GetItem();
292 /* Workaround because DeleteChildren has disapeared (why?) and
293 * CollapseAndReset doesn't work as advertised (deletes parent too) */
294 child
= GetFirstChild(parent
, cookie
);
295 while (child
.IsOk()) {
297 /* Not GetNextChild below, because the cookie mechanism can't
298 * handle disappearing children! */
299 child
= GetFirstChild(parent
, cookie
);
303 //-----------------------------------------------------------------------------
305 //-----------------------------------------------------------------------------
308 #if !USE_SHARED_LIBRARY
309 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
311 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
314 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
315 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
316 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
317 EVT_SIZE ( wxDirDialog::OnSize
)
318 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
319 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
320 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
321 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
322 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
325 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
326 const wxString
& defaultPath
, long style
,
327 const wxPoint
& pos
) :
328 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
329 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
332 m_dialogStyle
= style
;
335 m_path
= defaultPath
;
337 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxDefaultSize
, wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
338 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
339 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
340 m_ok
= new wxButton( this, ID_OK
, _("OK") );
341 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxSize(75,-1) );
342 m_new
= new wxButton( this, ID_NEW
, _("New...") );
344 // m_check->SetValue(TRUE);
351 void wxDirDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
356 void wxDirDialog::doSize()
358 /* Figure out height of DirCtrl, which is what is left over by
359 * the textctrl and the buttons. Manually, because I can't seem
360 * to get the constraints stuff to do this */
363 GetClientSize(&w
, &h
);
364 m_input
->GetSize(&w
,&h2
); h
-= h2
;
365 m_ok
->GetSize(&w
, &h2
); h
-= h2
;
366 //m_check->GetSize(&w, &h2); h -= h2;
369 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
370 c
->left
.SameAs (this, wxLeft
,5);
371 c
->right
.SameAs (this, wxRight
,5);
372 c
->height
.Absolute (h
);
373 c
->top
.SameAs (this, wxTop
,5);
374 m_dir
->SetConstraints(c
);
376 c
= new wxLayoutConstraints
;
377 c
->left
.SameAs (this, wxLeft
,5);
378 c
->right
.SameAs (this, wxRight
,5);
380 c
->top
.Below (m_dir
,5);
381 m_input
->SetConstraints(c
);
383 /* c = new wxLayoutConstraints;
384 c->left.SameAs (this, wxLeft,5);
385 c->right.SameAs (this, wxRight,5);
387 c->top.Below (m_input,5);
388 m_check->SetConstraints(c); */
390 c
= new wxLayoutConstraints
;
391 c
->width
.SameAs (m_cancel
, wxWidth
);
393 c
->top
.Below (m_input
,10);
394 c
->centreX
.PercentOf (this, wxWidth
, 20);
395 m_ok
->SetConstraints(c
);
397 c
= new wxLayoutConstraints
;
398 c
->width
.SameAs (m_cancel
, wxWidth
);
400 c
->top
.Below (m_input
,10);
401 c
->bottom
.SameAs (this, wxBottom
, 5);
402 c
->centreX
.PercentOf (this, wxWidth
, 50);
403 m_new
->SetConstraints(c
);
405 c
= new wxLayoutConstraints
;
408 c
->top
.Below (m_input
,10);
409 c
->centreX
.PercentOf (this, wxWidth
, 80);
410 m_cancel
->SetConstraints(c
);
415 int wxDirDialog::ShowModal()
417 m_input
->SetValue( m_path
);
418 return wxDialog::ShowModal();
421 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
423 wxDirItemData
*data
=
424 (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
426 m_input
->SetValue( *(data
->m_path
) );
429 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
431 wxDirItemData
*data
=
432 (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
434 m_input
->SetValue( *(data
->m_path
) );
437 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
439 m_path
= m_input
->GetValue();
440 // Does the path exist? (User may have typed anything in m_input)
441 if (wxPathExists(m_path
)) {
442 // OK, path exists, we're done.
446 // Interact with user, find out if the dir is a typo or to be created
447 wxString
msg( _("The directory ") );
449 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
450 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
);
451 if ( dialog
.ShowModal() == wxID_YES
) {
452 // Okay, let's make it
453 if (wxMkdir(m_path
)) {
454 // The new dir was created okay.
460 msg
= _("Failed to create directory ")+m_path
+
461 _("\n(Do you have the required permissions?)");
462 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
464 // We still don't have a valid dir. Back to the main dialog.
467 // User has answered NO to create dir.
470 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
472 EndModal(wxID_CANCEL
);
475 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
477 wxTextEntryDialog
dialog(this, _("Enter the name of the directory to create"),
478 _("Create New Directory"), m_input
->GetValue(), wxOK
|wxCANCEL
);
480 while (dialog
.ShowModal() == wxID_OK
)
482 // Okay, let's make it
483 if (wxMkdir(dialog
.GetValue())) {
484 // The new dir was created okay.
485 m_path
= dialog
.GetValue();
488 wxString msg
= _("Failed to create directory ")+dialog
.GetValue()+
489 _("\n(Do you have the required permissions?)") ;
490 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
492 // Show the create dialog again, until user clicks cancel or enters
498 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
500 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );