]>
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"
17 #include "wx/dialog.h"
18 #include "wx/button.h"
19 #include "wx/layout.h"
20 #include "wx/msgdlg.h"
21 #include "wx/textdlg.h"
22 #include "wx/filefn.h"
23 #include "wx/cmndata.h"
24 #include "wx/gdicmn.h"
26 #include "wx/imaglist.h"
28 #include "wx/generic/dirdlgg.h"
31 static char * icon1_xpm
[] = {
32 /* width height ncolors chars_per_pixel */
60 static char * icon2_xpm
[] = {
61 /* width height ncolors chars_per_pixel */
88 static const int ID_DIRCTRL
= 1000;
89 static const int ID_TEXTCTRL
= 1001;
90 static const int ID_OK
= 1002;
91 static const int ID_CANCEL
= 1003;
92 static const int ID_NEW
= 1004;
93 //static const int ID_CHECK = 1005;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 class wxDirItemData
: public wxTreeItemData
102 wxDirItemData(wxString
& path
, wxString
& name
);
105 wxString
*m_path
, *m_name
;
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 class wxDirCtrl
: public wxTreeCtrl
116 DECLARE_DYNAMIC_CLASS(wxDirCtrl
)
120 wxTreeItemId m_rootId
;
123 wxDirCtrl(wxWindow
*parent
, const wxWindowID id
= -1,
124 const wxString
&dir
= "/",
125 const wxPoint
& pos
= wxDefaultPosition
,
126 const wxSize
& size
= wxDefaultSize
,
127 const long style
= wxTR_HAS_BUTTONS
,
128 const wxString
& name
= "wxTreeCtrl" );
129 void OnExpandItem( const wxTreeEvent
&event
);
130 void OnCollapseItem( const wxTreeEvent
&event
);
131 void ShowHidden( const bool yesno
);
132 DECLARE_EVENT_TABLE()
134 void CreateItems(const wxTreeItemId
&parent
);
135 void SetupSections(void);
136 wxArrayString m_paths
, m_names
;
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
143 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
145 m_path
= new wxString(path
);
146 m_name
= new wxString(name
);
147 /* Insert logic to detect hidden files here
148 * In UnixLand we just check whether the first char is a dot
149 * For FileNameFromPath read LastDirNameInThisPath ;-) */
150 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
152 m_hasSubDirs
= HasSubDirs();
155 wxDirItemData:: ~wxDirItemData()
161 bool wxDirItemData::HasSubDirs()
163 wxString search
= *m_path
+ "/*";
164 wxString path
= wxFindFirstFile( search
, wxDIR
);
165 return (bool)(!path
.IsNull());
168 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
172 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
174 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
175 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
176 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
179 wxDirCtrl::wxDirCtrl(void)
181 m_showHidden
= FALSE
;
184 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
185 const wxPoint
& pos
, const wxSize
& size
,
186 const long style
, const wxString
& name
)
188 wxTreeCtrl( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
)
190 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
191 m_imageListNormal
->Add(wxICON(icon1
));
192 m_imageListNormal
->Add(wxICON(icon2
));
193 SetImageList(m_imageListNormal
);
195 m_showHidden
= FALSE
;
196 m_rootId
= AddRoot("Sections");
197 SetItemHasChildren(m_rootId
);
198 Expand(m_rootId
); // automatically expand first level
201 /* Quick macro. Don't worry, I'll #undef it later */
202 #define ADD_SECTION(a,b) \
203 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
205 void wxDirCtrl::SetupSections()
211 ADD_SECTION("/", _("The Computer") )
213 ADD_SECTION(home
, _("My Home") )
214 ADD_SECTION("/mnt", _("Mounted Devices") )
215 ADD_SECTION("/usr", _("User") )
216 ADD_SECTION("/usr/local", _("User Local") )
217 ADD_SECTION("/var", _("Variables") )
218 ADD_SECTION("/etc", _("Etcetera") )
219 ADD_SECTION("/tmp", _("Temporary") )
223 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
226 wxDirItemData
*dir_item
;
228 // wxASSERT(m_paths.Count() == m_names.Count()); ?
230 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
232 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
233 id
= AppendItem( parent
, m_names
[i
], 0, 1, dir_item
);
234 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
238 void wxDirCtrl::OnExpandItem( const wxTreeEvent
&event
)
240 if (event
.GetItem() == m_rootId
)
243 CreateItems(m_rootId
);
247 // This may take a longish time. Go to busy cursor
250 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
253 wxString search
,path
,filename
;
257 search
= *(data
->m_path
) + "/*";
258 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
259 path
=wxFindNextFile() ) {
260 filename
= wxFileNameFromPath( path
);
261 /* Don't add "." and ".." to the tree. I think wxFindNextFile
262 * also checks this, but I don't quite understand what happens
263 * there. Also wxFindNextFile seems to swallow hidden dirs */
264 if ((filename
!= ".") && (filename
!= "..")) {
266 m_names
.Add(filename
);
269 CreateItems(event
.GetItem());
272 SortChildren( event
.GetItem() );
276 void wxDirCtrl::OnCollapseItem( const wxTreeEvent
&event
)
278 wxTreeItemId child
, parent
= event
.GetItem();
280 /* Workaround because DeleteChildren has disapeared (why?) and
281 * CollapseAndReset doesn't work as advertised (deletes parent too) */
282 child
= GetFirstChild(parent
, cookie
);
283 while (child
.IsOk()) {
285 /* Not GetNextChild below, because the cookie mechanism can't
286 * handle disappearing children! */
287 child
= GetFirstChild(parent
, cookie
);
291 //-----------------------------------------------------------------------------
293 //-----------------------------------------------------------------------------
296 #if !USE_SHARED_LIBRARY
297 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
299 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
302 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
303 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
304 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
305 EVT_SIZE ( wxDirDialog::OnSize
)
306 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
307 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
308 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
309 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
310 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
313 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
314 const wxString
& defaultPath
, long style
,
315 const wxPoint
& pos
) :
316 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
317 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
320 m_dialogStyle
= style
;
323 m_path
= defaultPath
;
325 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxDefaultSize
, wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
326 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
327 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
328 m_ok
= new wxButton( this, ID_OK
, _("OK") );
329 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
330 m_new
= new wxButton( this, ID_NEW
, _("New...") );
332 // m_check->SetValue(TRUE);
339 void wxDirDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
344 void wxDirDialog::doSize()
346 /* Figure out height of DirCtrl, which is what is left over by
347 * the textctrl and the buttons. Manually, because I can't seem
348 * to get the constraints stuff to do this */
351 GetClientSize(&w
, &h
);
352 m_input
->GetSize(&w
,&h2
); h
-= h2
;
353 m_ok
->GetSize(&w
, &h2
); h
-= h2
;
354 //m_check->GetSize(&w, &h2); h -= h2;
357 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
358 c
->left
.SameAs (this, wxLeft
,5);
359 c
->right
.SameAs (this, wxRight
,5);
360 c
->height
.Absolute (h
);
361 c
->top
.SameAs (this, wxTop
,5);
362 m_dir
->SetConstraints(c
);
364 c
= new wxLayoutConstraints
;
365 c
->left
.SameAs (this, wxLeft
,5);
366 c
->right
.SameAs (this, wxRight
,5);
368 c
->top
.Below (m_dir
,5);
369 m_input
->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_input,5);
376 m_check->SetConstraints(c); */
378 c
= new wxLayoutConstraints
;
379 c
->width
.SameAs (m_cancel
, wxWidth
);
381 c
->top
.Below (m_input
,5);
382 c
->centreX
.PercentOf (this, wxWidth
, 25);
383 m_ok
->SetConstraints(c
);
385 c
= new wxLayoutConstraints
;
386 c
->width
.SameAs (m_cancel
, wxWidth
);
388 c
->top
.Below (m_input
,5);
389 c
->bottom
.SameAs (this, wxBottom
, 5);
390 c
->centreX
.PercentOf (this, wxWidth
, 50);
391 m_new
->SetConstraints(c
);
393 c
= new wxLayoutConstraints
;
396 c
->top
.Below (m_input
,5);
397 c
->centreX
.PercentOf (this, wxWidth
, 75);
398 m_cancel
->SetConstraints(c
);
403 int wxDirDialog::ShowModal()
405 m_input
->SetValue( m_path
);
406 return wxDialog::ShowModal();
409 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
411 wxDirItemData
*data
=
412 (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
414 m_input
->SetValue( *(data
->m_path
) );
417 void wxDirDialog::OnTreeKeyDown( wxKeyEvent
&WXUNUSED(event
) )
419 wxDirItemData
*data
=
420 (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
422 m_input
->SetValue( *(data
->m_path
) );
425 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
427 m_path
= m_input
->GetValue();
428 // Does the path exist? (User may have typed anything in m_input)
429 if (wxPathExists(m_path
)) {
430 // OK, path exists, we're done.
434 // Interact with user, find out if the dir is a typo or to be created
435 wxString
msg( _("The directory ") );
437 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
438 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
);
439 if ( dialog
.ShowModal() == wxID_YES
) {
440 // Okay, let's make it
441 if (wxMkdir(m_path
)) {
442 // The new dir was created okay.
448 msg
= _("Failed to create directory ")+m_path
+
449 _("\n(Do you have the required permissions?)");
450 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
452 // We still don't have a valid dir. Back to the main dialog.
455 // User has answered NO to create dir.
458 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
460 EndModal(wxID_CANCEL
);
463 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
465 wxTextEntryDialog
dialog(this, _("Enter the name of the directory to create"),
466 _("Create New Directory"), m_input
->GetValue(), wxOK
|wxCANCEL
);
468 while (dialog
.ShowModal() == wxID_OK
)
470 // Okay, let's make it
471 if (wxMkdir(dialog
.GetValue())) {
472 // The new dir was created okay.
473 m_path
= dialog
.GetValue();
476 wxString msg
= _("Failed to create directory ")+dialog
.GetValue()+
477 _("\n(Do you have the required permissions?)") ;
478 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
480 // Show the create dialog again, until user clicks cancel or enters
486 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
488 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );