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