]>
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"
38 static char * icon1_xpm
[] = {
39 /* width height ncolors chars_per_pixel */
67 static char * icon2_xpm
[] = {
68 /* width height ncolors chars_per_pixel */
95 static const int ID_DIRCTRL
= 1000;
96 static const int ID_TEXTCTRL
= 1001;
97 static const int ID_OK
= 1002;
98 static const int ID_CANCEL
= 1003;
99 static const int ID_NEW
= 1004;
100 //static const int ID_CHECK = 1005;
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 class wxDirItemData
: public wxTreeItemData
109 wxDirItemData(wxString
& path
, wxString
& name
);
112 wxString
*m_path
, *m_name
;
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 class wxDirCtrl
: public wxTreeCtrl
123 DECLARE_DYNAMIC_CLASS(wxDirCtrl
)
127 wxTreeItemId m_rootId
;
130 wxDirCtrl(wxWindow
*parent
, const wxWindowID id
= -1,
131 const wxString
&dir
= "/",
132 const wxPoint
& pos
= wxDefaultPosition
,
133 const wxSize
& size
= wxDefaultSize
,
134 const long style
= wxTR_HAS_BUTTONS
,
135 const wxString
& name
= "wxTreeCtrl" );
136 void OnExpandItem(wxTreeEvent
&event
);
137 void OnCollapseItem(wxTreeEvent
&event
);
138 void ShowHidden( const bool yesno
);
139 DECLARE_EVENT_TABLE()
141 void CreateItems(const wxTreeItemId
&parent
);
142 void SetupSections(void);
143 wxArrayString m_paths
, m_names
;
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
152 m_path
= new wxString(path
);
153 m_name
= new wxString(name
);
154 /* Insert logic to detect hidden files here
155 * In UnixLand we just check whether the first char is a dot
156 * For FileNameFromPath read LastDirNameInThisPath ;-) */
157 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
159 m_hasSubDirs
= HasSubDirs();
162 wxDirItemData:: ~wxDirItemData()
168 bool wxDirItemData::HasSubDirs()
170 wxString search
= *m_path
+ "/*";
171 wxString path
= wxFindFirstFile( search
, wxDIR
);
172 return (bool)(!path
.IsNull());
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
181 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
182 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
183 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
186 wxDirCtrl::wxDirCtrl(void)
188 m_showHidden
= FALSE
;
191 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
192 const wxPoint
& pos
, const wxSize
& size
,
193 const long style
, const wxString
& name
)
195 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
197 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
198 m_imageListNormal
->Add(wxICON(icon1
));
199 m_imageListNormal
->Add(wxICON(icon2
));
200 SetImageList(m_imageListNormal
);
202 m_showHidden
= FALSE
;
203 m_rootId
= AddRoot("Sections");
204 SetItemHasChildren(m_rootId
);
205 Expand(m_rootId
); // automatically expand first level
208 /* Quick macro. Don't worry, I'll #undef it later */
209 #define ADD_SECTION(a,b) \
210 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
212 void wxDirCtrl::SetupSections()
218 ADD_SECTION("/", _("The Computer") )
220 ADD_SECTION(home
, _("My Home") )
221 ADD_SECTION("/mnt", _("Mounted Devices") )
222 ADD_SECTION("/usr", _("User") )
223 ADD_SECTION("/usr/local", _("User Local") )
224 ADD_SECTION("/var", _("Variables") )
225 ADD_SECTION("/etc", _("Etcetera") )
226 ADD_SECTION("/tmp", _("Temporary") )
230 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
233 wxDirItemData
*dir_item
;
235 // wxASSERT(m_paths.Count() == m_names.Count()); ?
237 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
239 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
240 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
241 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
245 void wxDirCtrl::OnExpandItem(wxTreeEvent
&event
)
247 if (event
.GetItem() == m_rootId
)
250 CreateItems(m_rootId
);
254 // This may take a longish time. Go to busy cursor
257 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
260 wxString search
,path
,filename
;
264 search
= *(data
->m_path
) + "/*";
265 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
266 path
=wxFindNextFile() ) {
267 filename
= wxFileNameFromPath( path
);
268 /* Don't add "." and ".." to the tree. I think wxFindNextFile
269 * also checks this, but I don't quite understand what happens
270 * there. Also wxFindNextFile seems to swallow hidden dirs */
271 if ((filename
!= ".") && (filename
!= "..")) {
273 m_names
.Add(filename
);
276 CreateItems(event
.GetItem());
279 SortChildren( event
.GetItem() );
283 void wxDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
285 wxTreeItemId child
, parent
= event
.GetItem();
287 /* Workaround because DeleteChildren has disapeared (why?) and
288 * CollapseAndReset doesn't work as advertised (deletes parent too) */
289 child
= GetFirstChild(parent
, cookie
);
290 while (child
.IsOk()) {
292 /* Not GetNextChild below, because the cookie mechanism can't
293 * handle disappearing children! */
294 child
= GetFirstChild(parent
, cookie
);
298 //-----------------------------------------------------------------------------
300 //-----------------------------------------------------------------------------
303 #if !USE_SHARED_LIBRARY
304 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
306 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
309 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
310 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
311 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
312 EVT_SIZE ( wxDirDialog::OnSize
)
313 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
314 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
315 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
316 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
317 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
320 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
321 const wxString
& defaultPath
, long style
,
322 const wxPoint
& pos
) :
323 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
324 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
327 m_dialogStyle
= style
;
330 m_path
= defaultPath
;
332 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxDefaultSize
, wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
333 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
334 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
335 m_ok
= new wxButton( this, ID_OK
, _("OK") );
336 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
337 m_new
= new wxButton( this, ID_NEW
, _("New...") );
339 // m_check->SetValue(TRUE);
346 void wxDirDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
351 void wxDirDialog::doSize()
353 /* Figure out height of DirCtrl, which is what is left over by
354 * the textctrl and the buttons. Manually, because I can't seem
355 * to get the constraints stuff to do this */
358 GetClientSize(&w
, &h
);
359 m_input
->GetSize(&w
,&h2
); h
-= h2
;
360 m_ok
->GetSize(&w
, &h2
); h
-= h2
;
361 //m_check->GetSize(&w, &h2); h -= h2;
364 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
365 c
->left
.SameAs (this, wxLeft
,5);
366 c
->right
.SameAs (this, wxRight
,5);
367 c
->height
.Absolute (h
);
368 c
->top
.SameAs (this, wxTop
,5);
369 m_dir
->SetConstraints(c
);
371 c
= new wxLayoutConstraints
;
372 c
->left
.SameAs (this, wxLeft
,5);
373 c
->right
.SameAs (this, wxRight
,5);
375 c
->top
.Below (m_dir
,5);
376 m_input
->SetConstraints(c
);
378 /* c = new wxLayoutConstraints;
379 c->left.SameAs (this, wxLeft,5);
380 c->right.SameAs (this, wxRight,5);
382 c->top.Below (m_input,5);
383 m_check->SetConstraints(c); */
385 c
= new wxLayoutConstraints
;
386 c
->width
.SameAs (m_cancel
, wxWidth
);
388 c
->top
.Below (m_input
,5);
389 c
->centreX
.PercentOf (this, wxWidth
, 25);
390 m_ok
->SetConstraints(c
);
392 c
= new wxLayoutConstraints
;
393 c
->width
.SameAs (m_cancel
, wxWidth
);
395 c
->top
.Below (m_input
,5);
396 c
->bottom
.SameAs (this, wxBottom
, 5);
397 c
->centreX
.PercentOf (this, wxWidth
, 50);
398 m_new
->SetConstraints(c
);
400 c
= new wxLayoutConstraints
;
403 c
->top
.Below (m_input
,5);
404 c
->centreX
.PercentOf (this, wxWidth
, 75);
405 m_cancel
->SetConstraints(c
);
410 int wxDirDialog::ShowModal()
412 m_input
->SetValue( m_path
);
413 return wxDialog::ShowModal();
416 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
418 wxDirItemData
*data
=
419 (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
421 m_input
->SetValue( *(data
->m_path
) );
424 void wxDirDialog::OnTreeKeyDown( wxTreeEvent
&WXUNUSED(event
) )
426 wxDirItemData
*data
=
427 (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
429 m_input
->SetValue( *(data
->m_path
) );
432 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
434 m_path
= m_input
->GetValue();
435 // Does the path exist? (User may have typed anything in m_input)
436 if (wxPathExists(m_path
)) {
437 // OK, path exists, we're done.
441 // Interact with user, find out if the dir is a typo or to be created
442 wxString
msg( _("The directory ") );
444 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
445 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
);
446 if ( dialog
.ShowModal() == wxID_YES
) {
447 // Okay, let's make it
448 if (wxMkdir(m_path
)) {
449 // The new dir was created okay.
455 msg
= _("Failed to create directory ")+m_path
+
456 _("\n(Do you have the required permissions?)");
457 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
459 // We still don't have a valid dir. Back to the main dialog.
462 // User has answered NO to create dir.
465 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
467 EndModal(wxID_CANCEL
);
470 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
472 wxTextEntryDialog
dialog(this, _("Enter the name of the directory to create"),
473 _("Create New Directory"), m_input
->GetValue(), wxOK
|wxCANCEL
);
475 while (dialog
.ShowModal() == wxID_OK
)
477 // Okay, let's make it
478 if (wxMkdir(dialog
.GetValue())) {
479 // The new dir was created okay.
480 m_path
= dialog
.GetValue();
483 wxString msg
= _("Failed to create directory ")+dialog
.GetValue()+
484 _("\n(Do you have the required permissions?)") ;
485 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
487 // Show the create dialog again, until user clicks cancel or enters
493 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
495 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );