]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/dirdlg.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 "dirdlg.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"
25 #include "wx/dirdlg.h"
27 #include "wx/imaglist.h"
31 static char * icon1_xpm
[] = {
32 /* width height ncolors chars_per_pixel */
60 static const int ID_DIRCTRL
= 1000;
61 static const int ID_TEXTCTRL
= 1001;
62 static const int ID_OK
= 1002;
63 static const int ID_CANCEL
= 1003;
64 static const int ID_NEW
= 1004;
65 //static const int ID_CHECK = 1005;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 wxDirItemData::wxDirItemData(wxString
& path
, wxString
& name
)
73 m_path
= new wxString(path
);
74 m_name
= new wxString(name
);
75 /* Insert logic to detect hidden files here
76 * In UnixLand we just check whether the first char is a dot
77 * For FileNameFromPath read LastDirNameInThisPath ;-) */
78 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
80 m_hasSubDirs
= HasSubDirs();
83 wxDirItemData:: ~wxDirItemData()
89 bool wxDirItemData::HasSubDirs()
91 wxString search
= *m_path
+ "/*";
92 wxString path
= wxFindFirstFile( search
, wxDIR
);
93 return (bool)(!path
.IsNull());
96 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
100 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl
,wxTreeCtrl
)
102 BEGIN_EVENT_TABLE(wxDirCtrl
,wxTreeCtrl
)
103 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem
)
104 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem
)
107 wxDirCtrl::wxDirCtrl(void)
109 m_showHidden
= FALSE
;
112 wxDirCtrl::wxDirCtrl(wxWindow
*parent
, const wxWindowID id
, const wxString
&WXUNUSED(dir
),
113 const wxPoint
& pos
, const wxSize
& size
,
114 const long style
, const wxString
& name
)
116 wxTreeCtrl( parent
, id
, pos
, size
, style
, name
)
118 m_imageListNormal
= new wxImageList(16, 16, TRUE
);
119 m_imageListNormal
->Add(wxICON(icon1
));
120 SetImageList(m_imageListNormal
);
122 m_showHidden
= FALSE
;
123 m_rootId
= AddRoot("Sections");
124 SetItemHasChildren(m_rootId
);
125 Expand(m_rootId
); // automatically expand first level
128 /* Quick macro. Don't worry, I'll #undef it later */
129 #define ADD_SECTION(a,b) \
130 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
132 void wxDirCtrl::SetupSections()
138 ADD_SECTION("/", _("The Computer") )
140 ADD_SECTION(home
, _("My Home") )
141 ADD_SECTION("/mnt", _("Mounted Devices") )
142 ADD_SECTION("/usr", _("User") )
143 ADD_SECTION("/usr/local", _("User Local") )
144 ADD_SECTION("/var", _("Variables") )
145 ADD_SECTION("/etc", _("Etcetera") )
146 ADD_SECTION("/tmp", _("Temporary") )
150 void wxDirCtrl::CreateItems(const wxTreeItemId
&parent
)
153 wxDirItemData
*dir_item
;
155 // wxASSERT(m_paths.Count() == m_names.Count()); ?
157 for (unsigned int i
=0; i
<m_paths
.Count(); i
++)
159 dir_item
= new wxDirItemData(m_paths
[i
],m_names
[i
]);
160 id
= AppendItem( parent
, m_names
[i
], 0, -1, dir_item
);
161 if (dir_item
->m_hasSubDirs
) SetItemHasChildren(id
);
165 void wxDirCtrl::OnExpandItem( const wxTreeEvent
&event
)
167 if (event
.GetItem() == m_rootId
)
170 CreateItems(m_rootId
);
174 // This may take a longish time. Go to busy cursor
177 wxDirItemData
*data
= (wxDirItemData
*)GetItemData(event
.GetItem());
180 wxString search
,path
,filename
;
184 search
= *(data
->m_path
) + "/*";
185 for (path
= wxFindFirstFile( search
, wxDIR
); !path
.IsNull();
186 path
=wxFindNextFile() ) {
187 filename
= wxFileNameFromPath( path
);
188 /* Don't add "." and ".." to the tree. I think wxFindNextFile
189 * also checks this, but I don't quite understand what happens
190 * there. Also wxFindNextFile seems to swallow hidden dirs */
191 if ((filename
!= ".") && (filename
!= "..")) {
193 m_names
.Add(filename
);
196 CreateItems(event
.GetItem());
201 void wxDirCtrl::OnCollapseItem( const wxTreeEvent
&event
)
203 wxTreeItemId child
, parent
= event
.GetItem();
205 /* Workaround because DeleteChildren has disapeared (why?) and
206 * CollapseAndReset doesn't work as advertised (deletes parent too) */
207 child
= GetFirstChild(parent
, cookie
);
208 while (child
.IsOk()) {
210 /* Not GetNextChild below, because the cookie mechanism can't
211 * handle disappearing children! */
212 child
= GetFirstChild(parent
, cookie
);
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
221 #if !USE_SHARED_LIBRARY
222 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
224 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog
, wxDialog
)
227 BEGIN_EVENT_TABLE( wxDirDialog
, wxDialog
)
228 EVT_TREE_KEY_DOWN (ID_DIRCTRL
, wxDirDialog::OnTreeKeyDown
)
229 EVT_TREE_SEL_CHANGED (ID_DIRCTRL
, wxDirDialog::OnTreeSelected
)
230 EVT_SIZE ( wxDirDialog::OnSize
)
231 EVT_BUTTON (ID_OK
, wxDirDialog::OnOK
)
232 EVT_BUTTON (ID_CANCEL
, wxDirDialog::OnCancel
)
233 EVT_BUTTON (ID_NEW
, wxDirDialog::OnNew
)
234 EVT_TEXT_ENTER (ID_TEXTCTRL
, wxDirDialog::OnOK
)
235 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
238 wxDirDialog::wxDirDialog(wxWindow
*parent
, const wxString
& message
,
239 const wxString
& defaultPath
, long style
,
240 const wxPoint
& pos
) :
241 wxDialog(parent
, -1, message
, pos
, wxSize(300,300),
242 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
245 m_dialogStyle
= style
;
248 m_path
= defaultPath
;
250 m_dir
= new wxDirCtrl( this, ID_DIRCTRL
, "/", wxDefaultPosition
, wxDefaultSize
, wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
);
251 m_input
= new wxTextCtrl( this, ID_TEXTCTRL
, m_path
, wxDefaultPosition
);
252 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
253 m_ok
= new wxButton( this, ID_OK
, _("OK") );
254 m_cancel
= new wxButton( this, ID_CANCEL
, _("Cancel") );
255 m_new
= new wxButton( this, ID_NEW
, _("New...") );
257 // m_check->SetValue(TRUE);
264 void wxDirDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
269 void wxDirDialog::doSize()
271 /* Figure out height of DirCtrl, which is what is left over by
272 * the textctrl and the buttons. Manually, because I can't seem
273 * to get the constraints stuff to do this */
276 GetClientSize(&w
, &h
);
277 m_input
->GetSize(&w
,&h2
); h
-= h2
;
278 m_ok
->GetSize(&w
, &h2
); h
-= h2
;
279 //m_check->GetSize(&w, &h2); h -= h2;
282 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
283 c
->left
.SameAs (this, wxLeft
,5);
284 c
->right
.SameAs (this, wxRight
,5);
285 c
->height
.Absolute (h
);
286 c
->top
.SameAs (this, wxTop
,5);
287 m_dir
->SetConstraints(c
);
289 c
= new wxLayoutConstraints
;
290 c
->left
.SameAs (this, wxLeft
,5);
291 c
->right
.SameAs (this, wxRight
,5);
293 c
->top
.Below (m_dir
,5);
294 m_input
->SetConstraints(c
);
296 /* c = new wxLayoutConstraints;
297 c->left.SameAs (this, wxLeft,5);
298 c->right.SameAs (this, wxRight,5);
300 c->top.Below (m_input,5);
301 m_check->SetConstraints(c); */
303 c
= new wxLayoutConstraints
;
304 c
->width
.SameAs (m_cancel
, wxWidth
);
306 c
->top
.Below (m_input
,5);
307 c
->centreX
.PercentOf (this, wxWidth
, 25);
308 m_ok
->SetConstraints(c
);
310 c
= new wxLayoutConstraints
;
311 c
->width
.SameAs (m_cancel
, wxWidth
);
313 c
->top
.Below (m_input
,5);
314 c
->bottom
.SameAs (this, wxBottom
, 5);
315 c
->centreX
.PercentOf (this, wxWidth
, 50);
316 m_new
->SetConstraints(c
);
318 c
= new wxLayoutConstraints
;
321 c
->top
.Below (m_input
,5);
322 c
->centreX
.PercentOf (this, wxWidth
, 75);
323 m_cancel
->SetConstraints(c
);
328 int wxDirDialog::ShowModal()
330 m_input
->SetValue( m_path
);
331 return wxDialog::ShowModal();
334 void wxDirDialog::OnTreeSelected( wxTreeEvent
&event
)
336 wxDirItemData
*data
=
337 (wxDirItemData
*)m_dir
->GetItemData(event
.GetItem());
339 m_input
->SetValue( *(data
->m_path
) );
342 void wxDirDialog::OnTreeKeyDown( wxKeyEvent
&WXUNUSED(event
) )
344 wxDirItemData
*data
=
345 (wxDirItemData
*)m_dir
->GetItemData(m_dir
->GetSelection());
347 m_input
->SetValue( *(data
->m_path
) );
350 void wxDirDialog::OnOK( wxCommandEvent
& WXUNUSED(event
) )
352 m_path
= m_input
->GetValue();
353 // Does the path exist? (User may have typed anything in m_input)
354 if (wxPathExists(m_path
)) {
355 // OK, path exists, we're done.
359 // Interact with user, find out if the dir is a typo or to be created
360 wxString
msg( _("The directory ") );
362 msg
= msg
+ _("\ndoes not exist\nCreate it now?") ;
363 wxMessageDialog
dialog(this, msg
, _("Directory does not exist"), wxYES_NO
);
364 if ( dialog
.ShowModal() == wxID_YES
) {
365 // Okay, let's make it
366 if (wxMkdir(m_path
)) {
367 // The new dir was created okay.
373 msg
= _("Failed to create directory ")+m_path
+
374 _("\n(Do you have the required permissions?)");
375 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
377 // We still don't have a valid dir. Back to the main dialog.
380 // User has answered NO to create dir.
383 void wxDirDialog::OnCancel( wxCommandEvent
& WXUNUSED(event
) )
385 EndModal(wxID_CANCEL
);
388 void wxDirDialog::OnNew( wxCommandEvent
& WXUNUSED(event
) )
390 wxTextEntryDialog
dialog(this, _("Enter the name of the directory to create"),
391 _("Create New Directory"), m_input
->GetValue(), wxOK
|wxCANCEL
);
393 while (dialog
.ShowModal() == wxID_OK
)
395 // Okay, let's make it
396 if (wxMkdir(dialog
.GetValue())) {
397 // The new dir was created okay.
398 m_path
= dialog
.GetValue();
401 wxString msg
= _("Failed to create directory ")+dialog
.GetValue()+
402 _("\n(Do you have the required permissions?)") ;
403 wxMessageDialog
errmsg(this, msg
, _("Error creating directory"), wxOK
);
405 // Show the create dialog again, until user clicks cancel or enters
411 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
413 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );