]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirdlgg.cpp
Added clipboard cut and paste
[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/textctrl.h"
33 #include "wx/textdlg.h"
34 #include "wx/filefn.h"
35 #include "wx/cmndata.h"
36 #include "wx/gdicmn.h"
37 #include "wx/intl.h"
38 #include "wx/imaglist.h"
39 #include "wx/icon.h"
40 #include "wx/log.h"
41 #include "wx/sizer.h"
42 #include "wx/tokenzr.h"
43 #include "wx/dir.h"
44
45 #if wxUSE_STATLINE
46 #include "wx/statline.h"
47 #endif
48
49 #include "wx/generic/dirdlgg.h"
50
51 // If compiled under Windows, this macro can cause problems
52 #ifdef GetFirstChild
53 #undef GetFirstChild
54 #endif
55
56 #ifndef __WXMSW__
57 /* XPM */
58 static char * icon1_xpm[] = {
59 /* width height ncolors chars_per_pixel */
60 "16 16 6 1",
61 /* colors */
62 " s None c None",
63 ". c #000000",
64 "+ c #c0c0c0",
65 "@ c #808080",
66 "# c #ffff00",
67 "$ c #ffffff",
68 /* pixels */
69 " ",
70 " @@@@@ ",
71 " @#+#+#@ ",
72 " @#+#+#+#@@@@@@ ",
73 " @$$$$$$$$$$$$@.",
74 " @$#+#+#+#+#+#@.",
75 " @$+#+#+#+#+#+@.",
76 " @$#+#+#+#+#+#@.",
77 " @$+#+#+#+#+#+@.",
78 " @$#+#+#+#+#+#@.",
79 " @$+#+#+#+#+#+@.",
80 " @$#+#+#+#+#+#@.",
81 " @@@@@@@@@@@@@@.",
82 " ..............",
83 " ",
84 " "};
85
86 /* XPM */
87 static char * icon2_xpm[] = {
88 /* width height ncolors chars_per_pixel */
89 "16 16 6 1",
90 /* colors */
91 " s None c None",
92 ". c #000000",
93 "+ c #c0c0c0",
94 "@ c #808080",
95 "# c #ffff00",
96 "$ c #ffffff",
97 /* pixels */
98 " ",
99 " @@@@@ ",
100 " @$$$$$@ ",
101 " @$#+#+#$@@@@@@ ",
102 " @$+#+#+$$$$$$@.",
103 " @$#+#+#+#+#+#@.",
104 "@@@@@@@@@@@@@#@.",
105 "@$$$$$$$$$$@@+@.",
106 "@$#+#+#+#+##.@@.",
107 " @$#+#+#+#+#+.@.",
108 " @$+#+#+#+#+#.@.",
109 " @$+#+#+#+##@..",
110 " @@@@@@@@@@@@@.",
111 " .............",
112 " ",
113 " "};
114
115 #endif // !wxMSW
116
117 static const int ID_DIRCTRL = 1000;
118 static const int ID_TEXTCTRL = 1001;
119 static const int ID_OK = 1002;
120 static const int ID_CANCEL = 1003;
121 static const int ID_NEW = 1004;
122 //static const int ID_CHECK = 1005;
123
124 //-----------------------------------------------------------------------------
125 // wxDirItemData
126 //-----------------------------------------------------------------------------
127
128 wxDirItemData::wxDirItemData(wxString& path, wxString& name)
129 {
130 m_path = path;
131 m_name = name;
132 /* Insert logic to detect hidden files here
133 * In UnixLand we just check whether the first char is a dot
134 * For FileNameFromPath read LastDirNameInThisPath ;-) */
135 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
136 m_isHidden = FALSE;
137 m_hasSubDirs = HasSubDirs();
138 }
139
140 wxDirItemData::~wxDirItemData()
141 {
142 }
143
144 void wxDirItemData::SetNewDirName( wxString path )
145 {
146 m_path = path;
147 m_name = wxFileNameFromPath( path );
148 }
149
150 bool wxDirItemData::HasSubDirs()
151 {
152 return wxDir(m_path).HasSubDirs();
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,
174 const wxWindowID id,
175 const wxString &WXUNUSED(dir),
176 const wxPoint& pos,
177 const wxSize& size,
178 const long style,
179 const wxString& name )
180 : wxTreeCtrl( parent, id, pos, size, style, wxDefaultValidator, name )
181 {
182 #ifndef __WXMSW__
183 m_imageListNormal = new wxImageList(16, 16, TRUE);
184 m_imageListNormal->Add(wxICON(icon1));
185 m_imageListNormal->Add(wxICON(icon2));
186 SetImageList(m_imageListNormal);
187 #endif // !MSW
188
189 m_showHidden = FALSE;
190 m_rootId = AddRoot( _("Sections") );
191 SetItemHasChildren(m_rootId);
192 Expand(m_rootId); // automatically expand first level
193 }
194
195 /* Quick macro. Don't worry, I'll #undef it later */
196 #define ADD_SECTION(a,b) \
197 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
198
199 void wxDirCtrl::SetupSections()
200 {
201 wxString home;
202
203 m_paths.Clear();
204 m_names.Clear();
205 #ifdef __WXMSW__
206 // better than nothing
207 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
208 #else
209 ADD_SECTION(wxT("/"), _("The Computer") )
210 wxGetHomeDir(&home);
211 ADD_SECTION(home, _("My Home") )
212 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
213 ADD_SECTION(wxT("/usr/local"), _("User Local") )
214 ADD_SECTION(wxT("/usr"), _("User") )
215 ADD_SECTION(wxT("/var"), _("Variables") )
216 ADD_SECTION(wxT("/etc"), _("Etcetera") )
217 ADD_SECTION(wxT("/tmp"), _("Temporary") )
218 #endif
219 }
220 #undef ADD_SECTION
221
222 void wxDirCtrl::CreateItems(const wxTreeItemId &parent)
223 {
224 wxTreeItemId id;
225 wxDirItemData *dir_item;
226
227 // wxASSERT(m_paths.Count() == m_names.Count()); ?
228
229 size_t count = m_paths.GetCount();
230 for ( size_t i=0; i<count; i++)
231 {
232 dir_item = new wxDirItemData(m_paths[i],m_names[i]);
233 #ifdef __WXMSW__
234 id = AppendItem( parent, m_names[i], -1, -1, dir_item);
235 #else
236 id = AppendItem( parent, m_names[i], 0, -1, dir_item);
237 SetItemImage( id, 1, wxTreeItemIcon_Expanded );
238 #endif
239 if (dir_item->m_hasSubDirs) SetItemHasChildren(id);
240 }
241 }
242
243 void wxDirCtrl::OnBeginEditItem(wxTreeEvent &event)
244 {
245 // don't rename the main entry "Sections"
246 if (event.GetItem() == m_rootId)
247 {
248 event.Veto();
249 return;
250 }
251
252 // don't rename the individual sections
253 if (GetParent( event.GetItem() ) == m_rootId)
254 {
255 event.Veto();
256 return;
257 }
258 }
259
260 void wxDirCtrl::OnEndEditItem(wxTreeEvent &event)
261 {
262 if ((event.GetLabel().IsEmpty()) ||
263 (event.GetLabel() == _(".")) ||
264 (event.GetLabel() == _("..")) ||
265 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
266 {
267 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
268 dialog.ShowModal();
269 event.Veto();
270 return;
271 }
272
273 wxTreeItemId id = event.GetItem();
274 wxDirItemData *data = (wxDirItemData*)GetItemData( id );
275 wxASSERT( data );
276
277 wxString new_name( wxPathOnly( data->m_path ) );
278 new_name += wxT("/");
279 new_name += event.GetLabel();
280
281 wxLogNull log;
282
283 if (wxFileExists(new_name))
284 {
285 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
286 dialog.ShowModal();
287 event.Veto();
288 }
289
290 if (wxRenameFile(data->m_path,new_name))
291 {
292 data->SetNewDirName( new_name );
293 }
294 else
295 {
296 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
297 dialog.ShowModal();
298 event.Veto();
299 }
300 }
301
302 void wxDirCtrl::OnExpandItem(wxTreeEvent &event)
303 {
304 if (event.GetItem() == m_rootId)
305 {
306 SetupSections();
307 CreateItems(m_rootId);
308 return;
309 }
310
311 // This may take a longish time. Go to busy cursor
312 wxBeginBusyCursor();
313
314 wxDirItemData *data = (wxDirItemData *)GetItemData(event.GetItem());
315
316 m_paths.Clear();
317 m_names.Clear();
318
319 wxString path = data->m_path;
320
321 wxDir dir(path);
322
323 path += _T('/');
324
325 wxString filename;
326 bool cont = dir.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN);
327 while ( cont )
328 {
329 m_paths.Add(path + filename);
330 m_names.Add(filename);
331
332 cont = dir.GetNext(&filename);
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 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog, wxDialog )
363
364 BEGIN_EVENT_TABLE( wxDirDialog, wxDialog )
365 EVT_TREE_KEY_DOWN (ID_DIRCTRL, wxDirDialog::OnTreeKeyDown)
366 EVT_TREE_SEL_CHANGED (ID_DIRCTRL, wxDirDialog::OnTreeSelected)
367 EVT_SIZE ( wxDirDialog::OnSize)
368 EVT_BUTTON (ID_OK, wxDirDialog::OnOK)
369 EVT_BUTTON (ID_CANCEL, wxDirDialog::OnCancel)
370 EVT_BUTTON (ID_NEW, wxDirDialog::OnNew)
371 EVT_TEXT_ENTER (ID_TEXTCTRL, wxDirDialog::OnOK)
372 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
373 END_EVENT_TABLE()
374
375 wxDirDialog::wxDirDialog(wxWindow *parent,
376 const wxString& message,
377 const wxString& defaultPath,
378 long style,
379 const wxPoint& pos)
380 : wxDialog(parent, -1, message, pos, wxSize(300,300),
381 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
382 {
383 m_message = message;
384 m_dialogStyle = style;
385 m_parent = parent;
386
387 m_path = defaultPath;
388
389 wxBeginBusyCursor();
390
391 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
392
393 // 1) dir ctrl
394 m_dir = new wxDirCtrl( this, ID_DIRCTRL, _T("/"),
395 wxDefaultPosition,
396 wxSize(200,200),
397 wxTR_HAS_BUTTONS |
398 wxSUNKEN_BORDER |
399 wxTR_EDIT_LABELS );
400 topsizer->Add( m_dir, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
401
402 // 2) text ctrl
403 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
404 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
405
406 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
407 // m_check->SetValue(TRUE);
408
409 #if wxUSE_STATLINE
410 // 3) static line
411 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
412 #endif
413
414 // 4) buttons
415 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
416 m_ok = new wxButton( this, ID_OK, _("OK") );
417 buttonsizer->Add( m_ok, 0, wxLEFT|wxRIGHT, 10 );
418 m_cancel = new wxButton( this, ID_CANCEL, _("Cancel") );
419 buttonsizer->Add( m_cancel, 0, wxLEFT|wxRIGHT, 10 );
420 m_new = new wxButton( this, ID_NEW, _("New...") );
421 buttonsizer->Add( m_new, 0, wxLEFT|wxRIGHT, 10 );
422
423 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
424
425 m_ok->SetDefault();
426 m_dir->SetFocus();
427
428 SetAutoLayout( TRUE );
429 SetSizer( topsizer );
430
431 topsizer->SetSizeHints( this );
432 topsizer->Fit( this );
433
434 Centre( wxBOTH );
435
436 if (m_path == wxT("~"))
437 wxGetHomeDir( &m_path );
438
439 // choose the directory corresponding to defaultPath in the tree
440 // VZ: using wxStringTokenizer is probably unsafe here (escaped slashes
441 // will not be processed correctly...)
442 wxStringTokenizer tk(m_path, wxFILE_SEP_PATH, wxTOKEN_STRTOK);
443
444 wxString path;
445
446 long cookie = 0;
447 // default to root dir
448 wxTreeItemId item = m_dir->GetFirstChild(m_dir->GetRootItem(), cookie);
449
450 if (!m_path.IsEmpty() && (m_path != wxT("/")) && (m_dir->m_paths.Count() > 1))
451 {
452 size_t count = m_dir->m_paths.GetCount();
453 for ( size_t i=1; i<count; i++)
454 {
455 if (m_path.Find( m_dir->m_paths[i] ) == 0)
456 {
457 path = m_dir->m_paths[i];
458
459 for (size_t j = 0; j < i; j++)
460 item = m_dir->GetNextChild(m_dir->GetRootItem(), cookie);
461
462 wxStringTokenizer tk2(path, wxFILE_SEP_PATH, wxTOKEN_STRTOK);
463 for (size_t h = 0; h < tk2.CountTokens(); h++)
464 tk.GetNextToken();
465
466 break;
467 }
468 }
469 }
470 while ( tk.HasMoreTokens() && item.IsOk() )
471 {
472 path << wxFILE_SEP_PATH << tk.GetNextToken();
473
474 m_dir->Expand(item);
475
476 wxTreeItemId child = m_dir->GetFirstChild(item, cookie);
477 while ( child.IsOk() )
478 {
479 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(child);
480 if ( data->m_path == path )
481 break;
482
483 child = m_dir->GetNextChild(item, cookie);
484 }
485
486 item = child;
487 }
488
489 if ( item.IsOk() )
490 {
491 m_dir->Expand(item);
492 m_dir->SelectItem(item);
493 m_dir->EnsureVisible(item);
494 }
495
496 wxEndBusyCursor();
497 }
498
499 int wxDirDialog::ShowModal()
500 {
501 m_input->SetValue( m_path );
502 return wxDialog::ShowModal();
503 }
504
505 void wxDirDialog::OnTreeSelected( wxTreeEvent &event )
506 {
507 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(event.GetItem());
508 if (data)
509 m_input->SetValue( data->m_path );
510 };
511
512 void wxDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
513 {
514 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(m_dir->GetSelection());
515 if (data)
516 m_input->SetValue( data->m_path );
517 };
518
519 void wxDirDialog::OnOK( wxCommandEvent& WXUNUSED(event) )
520 {
521 m_path = m_input->GetValue();
522 // Does the path exist? (User may have typed anything in m_input)
523 if (wxPathExists(m_path)) {
524 // OK, path exists, we're done.
525 EndModal(wxID_OK);
526 return;
527 }
528 // Interact with user, find out if the dir is a typo or to be created
529 wxString msg( _("The directory ") );
530 msg = msg + m_path;
531 msg = msg + _("\ndoes not exist\nCreate it now?") ;
532 wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING );
533 if ( dialog.ShowModal() == wxID_YES ) {
534 // Okay, let's make it
535 wxLogNull log;
536 if (wxMkdir(m_path)) {
537 // The new dir was created okay.
538 EndModal(wxID_OK);
539 return;
540 }
541 else {
542 // Trouble...
543 msg = _("Failed to create directory ")+m_path+
544 _("\n(Do you have the required permissions?)");
545 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
546 errmsg.ShowModal();
547 // We still don't have a valid dir. Back to the main dialog.
548 }
549 }
550 // User has answered NO to create dir.
551 }
552
553 void wxDirDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
554 {
555 EndModal(wxID_CANCEL);
556 }
557
558 void wxDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
559 {
560 wxTreeItemId id = m_dir->GetSelection();
561 if ((id == m_dir->GetRootItem()) ||
562 (m_dir->GetParent(id) == m_dir->GetRootItem()))
563 {
564 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
565 _("Create directory"), wxOK | wxICON_INFORMATION );
566 msg.ShowModal();
567 return;
568 }
569
570 wxTreeItemId parent = m_dir->GetParent( id );
571 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData( parent );
572 wxASSERT( data );
573
574 wxString new_name( wxT("NewName") );
575 wxString path( data->m_path );
576 path += wxT("/");
577 path += new_name;
578 if (wxFileExists(path))
579 {
580 // try NewName0, NewName1 etc.
581 int i = 0;
582 do {
583 new_name = wxT("NewName");
584 wxString num;
585 num.Printf( wxT("%d"), i );
586 new_name += num;
587
588 path = data->m_path;
589 path += wxT("/");
590 path += new_name;
591 i++;
592 } while (wxFileExists(path));
593 }
594
595 wxLogNull log;
596 if (!wxMkdir(path))
597 {
598 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
599 dialog.ShowModal();
600 return;
601 }
602
603 wxDirItemData *new_data = new wxDirItemData( path, new_name );
604 wxTreeItemId new_id = m_dir->AppendItem( parent, new_name, 0, 1, new_data );
605 m_dir->EnsureVisible( new_id );
606 m_dir->EditLabel( new_id );
607 }
608
609 /*
610 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
611 {
612 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
613 }
614 */
615
616 #endif // wxUSE_DIRDLG