]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
removed wxVERSION_* macros
[wxWidgets.git] / src / generic / dirdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dirdlg.cpp
3 // Purpose: wxDirDialog
4 // Author: Harm van der Heijden and Robert Roebling
5 // Modified by:
6 // Created: 12/12/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Harm van der Heijden and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dirdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #if wxUSE_DIRDLG
26
27 #include "wx/utils.h"
28 #include "wx/dialog.h"
29 #include "wx/button.h"
30 #include "wx/layout.h"
31 #include "wx/msgdlg.h"
32 #include "wx/textdlg.h"
33 #include "wx/filefn.h"
34 #include "wx/cmndata.h"
35 #include "wx/gdicmn.h"
36 #include "wx/intl.h"
37 #include "wx/imaglist.h"
38 #include "wx/icon.h"
39 #include "wx/log.h"
40 #include "wx/sizer.h"
41
42 #if wxUSE_STATLINE
43 #include "wx/statline.h"
44 #endif
45
46 #include "wx/generic/dirdlgg.h"
47
48 // If compiled under Windows, this macro can cause problems
49 #ifdef GetFirstChild
50 #undef GetFirstChild
51 #endif
52
53 #ifndef __WXMSW__
54 /* XPM */
55 static char * icon1_xpm[] = {
56 /* width height ncolors chars_per_pixel */
57 "16 16 6 1",
58 /* colors */
59 " s None c None",
60 ". c #000000",
61 "+ c #c0c0c0",
62 "@ c #808080",
63 "# c #ffff00",
64 "$ c #ffffff",
65 /* pixels */
66 " ",
67 " @@@@@ ",
68 " @#+#+#@ ",
69 " @#+#+#+#@@@@@@ ",
70 " @$$$$$$$$$$$$@.",
71 " @$#+#+#+#+#+#@.",
72 " @$+#+#+#+#+#+@.",
73 " @$#+#+#+#+#+#@.",
74 " @$+#+#+#+#+#+@.",
75 " @$#+#+#+#+#+#@.",
76 " @$+#+#+#+#+#+@.",
77 " @$#+#+#+#+#+#@.",
78 " @@@@@@@@@@@@@@.",
79 " ..............",
80 " ",
81 " "};
82
83 /* XPM */
84 static char * icon2_xpm[] = {
85 /* width height ncolors chars_per_pixel */
86 "16 16 6 1",
87 /* colors */
88 " s None c None",
89 ". c #000000",
90 "+ c #c0c0c0",
91 "@ c #808080",
92 "# c #ffff00",
93 "$ c #ffffff",
94 /* pixels */
95 " ",
96 " @@@@@ ",
97 " @$$$$$@ ",
98 " @$#+#+#$@@@@@@ ",
99 " @$+#+#+$$$$$$@.",
100 " @$#+#+#+#+#+#@.",
101 "@@@@@@@@@@@@@#@.",
102 "@$$$$$$$$$$@@+@.",
103 "@$#+#+#+#+##.@@.",
104 " @$#+#+#+#+#+.@.",
105 " @$+#+#+#+#+#.@.",
106 " @$+#+#+#+##@..",
107 " @@@@@@@@@@@@@.",
108 " .............",
109 " ",
110 " "};
111
112 #endif // !wxMSW
113
114 static const int ID_DIRCTRL = 1000;
115 static const int ID_TEXTCTRL = 1001;
116 static const int ID_OK = 1002;
117 static const int ID_CANCEL = 1003;
118 static const int ID_NEW = 1004;
119 //static const int ID_CHECK = 1005;
120
121 //-----------------------------------------------------------------------------
122 // wxDirItemData
123 //-----------------------------------------------------------------------------
124
125 wxDirItemData::wxDirItemData(wxString& path, wxString& name)
126 {
127 m_path = path;
128 m_name = name;
129 /* Insert logic to detect hidden files here
130 * In UnixLand we just check whether the first char is a dot
131 * For FileNameFromPath read LastDirNameInThisPath ;-) */
132 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
133 m_isHidden = FALSE;
134 m_hasSubDirs = HasSubDirs();
135 }
136
137 wxDirItemData::~wxDirItemData()
138 {
139 }
140
141 void wxDirItemData::SetNewDirName( wxString path )
142 {
143 m_path = path;
144 m_name = wxFileNameFromPath( path );
145 }
146
147 bool wxDirItemData::HasSubDirs()
148 {
149 wxString search = m_path + "/*";
150 wxLogNull log;
151 wxString path = wxFindFirstFile( search, wxDIR );
152 return (bool)(!path.IsNull());
153 }
154
155 //-----------------------------------------------------------------------------
156 // wxDirCtrl
157 //-----------------------------------------------------------------------------
158
159 IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl)
160
161 BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl)
162 EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem)
163 EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem)
164 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem)
165 EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem)
166 END_EVENT_TABLE()
167
168 wxDirCtrl::wxDirCtrl(void)
169 {
170 m_showHidden = FALSE;
171 }
172
173 wxDirCtrl::wxDirCtrl(wxWindow *parent, const wxWindowID id, const wxString &WXUNUSED(dir),
174 const wxPoint& pos, const wxSize& size,
175 const long style, const wxString& name )
176 :
177 wxTreeCtrl( parent, id, pos, size, style, wxDefaultValidator, name )
178 {
179 #ifndef __WXMSW__
180 m_imageListNormal = new wxImageList(16, 16, TRUE);
181 m_imageListNormal->Add(wxICON(icon1));
182 m_imageListNormal->Add(wxICON(icon2));
183 SetImageList(m_imageListNormal);
184 #endif
185
186 m_showHidden = FALSE;
187 m_rootId = AddRoot( _("Sections") );
188 SetItemHasChildren(m_rootId);
189 Expand(m_rootId); // automatically expand first level
190 }
191
192 /* Quick macro. Don't worry, I'll #undef it later */
193 #define ADD_SECTION(a,b) \
194 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
195
196 void wxDirCtrl::SetupSections()
197 {
198 wxString home;
199
200 m_paths.Clear();
201 m_names.Clear();
202 #ifdef __WXMSW__
203 // better than nothing
204 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
205 #else
206 ADD_SECTION(wxT("/"), _("The Computer") )
207 wxGetHomeDir(&home);
208 ADD_SECTION(home, _("My Home") )
209 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
210 ADD_SECTION(wxT("/usr"), _("User") )
211 ADD_SECTION(wxT("/usr/local"), _("User Local") )
212 ADD_SECTION(wxT("/var"), _("Variables") )
213 ADD_SECTION(wxT("/etc"), _("Etcetera") )
214 ADD_SECTION(wxT("/tmp"), _("Temporary") )
215 #endif
216 }
217 #undef ADD_SECTION
218
219 void wxDirCtrl::CreateItems(const wxTreeItemId &parent)
220 {
221 wxTreeItemId id;
222 wxDirItemData *dir_item;
223
224 // wxASSERT(m_paths.Count() == m_names.Count()); ?
225
226 for (unsigned int i=0; i<m_paths.Count(); i++)
227 {
228 dir_item = new wxDirItemData(m_paths[i],m_names[i]);
229 #ifdef __WXMSW__
230 id = AppendItem( parent, m_names[i], -1, -1, dir_item);
231 #else
232 id = AppendItem( parent, m_names[i], 0, 1, dir_item);
233 #endif
234 if (dir_item->m_hasSubDirs) SetItemHasChildren(id);
235 }
236 }
237
238 void wxDirCtrl::OnBeginEditItem(wxTreeEvent &event)
239 {
240 // don't rename the main entry "Sections"
241 if (event.GetItem() == m_rootId)
242 {
243 event.Veto();
244 return;
245 }
246
247 // don't rename the individual sections
248 if (GetParent( event.GetItem() ) == m_rootId)
249 {
250 event.Veto();
251 return;
252 }
253 }
254
255 void wxDirCtrl::OnEndEditItem(wxTreeEvent &event)
256 {
257 if ((event.GetLabel().IsEmpty()) ||
258 (event.GetLabel() == _(".")) ||
259 (event.GetLabel() == _("..")) ||
260 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
261 {
262 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
263 dialog.ShowModal();
264 event.Veto();
265 return;
266 }
267
268 wxTreeItemId id = event.GetItem();
269 wxDirItemData *data = (wxDirItemData*)GetItemData( id );
270 wxASSERT( data );
271
272 wxString new_name( wxPathOnly( data->m_path ) );
273 new_name += wxT("/");
274 new_name += event.GetLabel();
275
276 wxLogNull log;
277
278 if (wxFileExists(new_name))
279 {
280 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
281 dialog.ShowModal();
282 event.Veto();
283 }
284
285 if (wxRenameFile(data->m_path,new_name))
286 {
287 data->SetNewDirName( new_name );
288 }
289 else
290 {
291 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
292 dialog.ShowModal();
293 event.Veto();
294 }
295 }
296
297 void wxDirCtrl::OnExpandItem(wxTreeEvent &event)
298 {
299 if (event.GetItem() == m_rootId)
300 {
301 SetupSections();
302 CreateItems(m_rootId);
303 return;
304 }
305
306 // This may take a longish time. Go to busy cursor
307 wxBeginBusyCursor();
308
309 wxDirItemData *data = (wxDirItemData *)GetItemData(event.GetItem());
310 wxASSERT(data);
311
312 wxString search,path,filename;
313
314 m_paths.Clear();
315 m_names.Clear();
316 #ifdef __WXMSW__
317 search = data->m_path + "\\*.*";
318 #else
319 search = data->m_path + "/*";
320 #endif
321 for (path = wxFindFirstFile( search, wxDIR ); !path.IsNull();
322 path=wxFindNextFile() )
323 {
324 filename = wxFileNameFromPath( path );
325 /* Don't add "." and ".." to the tree. I think wxFindNextFile
326 * also checks this, but I don't quite understand what happens
327 * there. Also wxFindNextFile seems to swallow hidden dirs */
328 if ((filename != ".") && (filename != ".."))
329 {
330 m_paths.Add(path);
331 m_names.Add(filename);
332 }
333 }
334
335 CreateItems( event.GetItem() );
336 SortChildren( event.GetItem() );
337
338 wxEndBusyCursor();
339 }
340
341 void wxDirCtrl::OnCollapseItem(wxTreeEvent &event )
342 {
343 wxTreeItemId child, parent = event.GetItem();
344 long cookie;
345 /* Workaround because DeleteChildren has disapeared (why?) and
346 * CollapseAndReset doesn't work as advertised (deletes parent too) */
347 child = GetFirstChild(parent, cookie);
348 while (child.IsOk())
349 {
350 Delete(child);
351 /* Not GetNextChild below, because the cookie mechanism can't
352 * handle disappearing children! */
353 child = GetFirstChild(parent, cookie);
354 }
355 }
356
357 //-----------------------------------------------------------------------------
358 // wxDirDialog
359 //-----------------------------------------------------------------------------
360
361
362 #if !USE_SHARED_LIBRARY
363 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
364 #else
365 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog, wxDialog )
366 #endif
367
368 BEGIN_EVENT_TABLE( wxDirDialog, wxDialog )
369 EVT_TREE_KEY_DOWN (ID_DIRCTRL, wxDirDialog::OnTreeKeyDown)
370 EVT_TREE_SEL_CHANGED (ID_DIRCTRL, wxDirDialog::OnTreeSelected)
371 EVT_SIZE ( wxDirDialog::OnSize)
372 EVT_BUTTON (ID_OK, wxDirDialog::OnOK)
373 EVT_BUTTON (ID_CANCEL, wxDirDialog::OnCancel)
374 EVT_BUTTON (ID_NEW, wxDirDialog::OnNew)
375 EVT_TEXT_ENTER (ID_TEXTCTRL, wxDirDialog::OnOK)
376 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
377 END_EVENT_TABLE()
378
379 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
380 const wxString& defaultPath, long style,
381 const wxPoint& pos) :
382 wxDialog(parent, -1, message, pos, wxSize(300,300),
383 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
384 {
385 m_message = message;
386 m_dialogStyle = style;
387 m_parent = parent;
388
389 m_path = defaultPath;
390
391 wxBeginBusyCursor();
392
393 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
394
395 // 1) dir ctrl
396 m_dir = new wxDirCtrl( this, ID_DIRCTRL, "/", wxDefaultPosition, wxSize(200,200),
397 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER | wxTR_EDIT_LABELS);
398 topsizer->Add( m_dir, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
399
400 // 2) text ctrl
401 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
402 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
403
404 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
405 // m_check->SetValue(TRUE);
406
407 #if wxUSE_STATLINE
408 // 3) static line
409 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
410 #endif
411
412 // 4) buttons
413 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
414 m_ok = new wxButton( this, ID_OK, _("OK") );
415 buttonsizer->Add( m_ok, 0, wxLEFT|wxRIGHT, 10 );
416 m_cancel = new wxButton( this, ID_CANCEL, _("Cancel") );
417 buttonsizer->Add( m_cancel, 0, wxLEFT|wxRIGHT, 10 );
418 m_new = new wxButton( this, ID_NEW, _("New...") );
419 buttonsizer->Add( m_new, 0, wxLEFT|wxRIGHT, 10 );
420
421 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
422
423 m_ok->SetDefault();
424 m_dir->SetFocus();
425
426 SetAutoLayout( TRUE );
427 SetSizer( topsizer );
428
429 topsizer->SetSizeHints( this );
430 topsizer->Fit( this );
431
432 Centre( wxBOTH );
433
434 wxEndBusyCursor();
435 }
436
437 int wxDirDialog::ShowModal()
438 {
439 m_input->SetValue( m_path );
440 return wxDialog::ShowModal();
441 }
442
443 void wxDirDialog::OnTreeSelected( wxTreeEvent &event )
444 {
445 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(event.GetItem());
446 if (data)
447 m_input->SetValue( data->m_path );
448 };
449
450 void wxDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
451 {
452 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(m_dir->GetSelection());
453 if (data)
454 m_input->SetValue( data->m_path );
455 };
456
457 void wxDirDialog::OnOK( wxCommandEvent& WXUNUSED(event) )
458 {
459 m_path = m_input->GetValue();
460 // Does the path exist? (User may have typed anything in m_input)
461 if (wxPathExists(m_path)) {
462 // OK, path exists, we're done.
463 EndModal(wxID_OK);
464 return;
465 }
466 // Interact with user, find out if the dir is a typo or to be created
467 wxString msg( _("The directory ") );
468 msg = msg + m_path;
469 msg = msg + _("\ndoes not exist\nCreate it now?") ;
470 wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING );
471 if ( dialog.ShowModal() == wxID_YES ) {
472 // Okay, let's make it
473 wxLogNull log;
474 if (wxMkdir(m_path)) {
475 // The new dir was created okay.
476 EndModal(wxID_OK);
477 return;
478 }
479 else {
480 // Trouble...
481 msg = _("Failed to create directory ")+m_path+
482 _("\n(Do you have the required permissions?)");
483 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
484 errmsg.ShowModal();
485 // We still don't have a valid dir. Back to the main dialog.
486 }
487 }
488 // User has answered NO to create dir.
489 }
490
491 void wxDirDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
492 {
493 EndModal(wxID_CANCEL);
494 }
495
496 void wxDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
497 {
498 wxTreeItemId id = m_dir->GetSelection();
499 if ((id == m_dir->GetRootItem()) ||
500 (m_dir->GetParent(id) == m_dir->GetRootItem()))
501 {
502 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
503 _("Create directory"), wxOK | wxICON_INFORMATION );
504 msg.ShowModal();
505 return;
506 }
507
508 wxTreeItemId parent = m_dir->GetParent( id );
509 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData( parent );
510 wxASSERT( data );
511
512 wxString new_name( wxT("NewName") );
513 wxString path( data->m_path );
514 path += wxT("/");
515 path += new_name;
516 if (wxFileExists(path))
517 {
518 // try NewName0, NewName1 etc.
519 int i = 0;
520 do {
521 new_name = wxT("NewName");
522 wxString num;
523 num.Printf( "%d", i );
524 new_name += num;
525
526 path = data->m_path;
527 path += wxT("/");
528 path += new_name;
529 i++;
530 } while (wxFileExists(path));
531 }
532
533 wxLogNull log;
534 if (!wxMkdir(path))
535 {
536 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
537 dialog.ShowModal();
538 return;
539 }
540
541 wxDirItemData *new_data = new wxDirItemData( path, new_name );
542 wxTreeItemId new_id = m_dir->AppendItem( parent, new_name, 0, 1, new_data );
543 m_dir->EnsureVisible( new_id );
544 m_dir->EditLabel( new_id );
545 }
546
547 /*
548 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
549 {
550 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
551 }
552 */
553
554 #endif