Minor changes to keep my MSW source
[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 #ifndef __WXMSW__
177 m_imageListNormal = new wxImageList(16, 16, TRUE);
178 m_imageListNormal->Add(wxICON(icon1));
179 m_imageListNormal->Add(wxICON(icon2));
180 SetImageList(m_imageListNormal);
181 #endif
182
183 m_showHidden = FALSE;
184 m_rootId = AddRoot( _("Sections") );
185 SetItemHasChildren(m_rootId);
186 Expand(m_rootId); // automatically expand first level
187 }
188
189 /* Quick macro. Don't worry, I'll #undef it later */
190 #define ADD_SECTION(a,b) \
191 if (wxPathExists((a))) { m_paths.Add( (a) ); m_names.Add( (b) ); };
192
193 void wxDirCtrl::SetupSections()
194 {
195 wxString home;
196
197 m_paths.Clear();
198 m_names.Clear();
199 #ifdef __WXMSW__
200 // better than nothing
201 ADD_SECTION(wxT("c:\\"), _("My Harddisk") )
202 #else
203 ADD_SECTION(wxT("/"), _("The Computer") )
204 wxGetHomeDir(&home);
205 ADD_SECTION(home, _("My Home") )
206 ADD_SECTION(wxT("/mnt"), _("Mounted Devices") )
207 ADD_SECTION(wxT("/usr"), _("User") )
208 ADD_SECTION(wxT("/usr/local"), _("User Local") )
209 ADD_SECTION(wxT("/var"), _("Variables") )
210 ADD_SECTION(wxT("/etc"), _("Etcetera") )
211 ADD_SECTION(wxT("/tmp"), _("Temporary") )
212 #endif
213 }
214 #undef ADD_SECTION
215
216 void wxDirCtrl::CreateItems(const wxTreeItemId &parent)
217 {
218 wxTreeItemId id;
219 wxDirItemData *dir_item;
220
221 // wxASSERT(m_paths.Count() == m_names.Count()); ?
222
223 for (unsigned int i=0; i<m_paths.Count(); i++)
224 {
225 dir_item = new wxDirItemData(m_paths[i],m_names[i]);
226 #ifdef __WXMSW__
227 id = AppendItem( parent, m_names[i], -1, -1, dir_item);
228 #else
229 id = AppendItem( parent, m_names[i], 0, 1, dir_item);
230 #endif
231 if (dir_item->m_hasSubDirs) SetItemHasChildren(id);
232 }
233 }
234
235 void wxDirCtrl::OnBeginEditItem(wxTreeEvent &event)
236 {
237 // don't rename the main entry "Sections"
238 if (event.GetItem() == m_rootId)
239 {
240 event.Veto();
241 return;
242 }
243
244 // don't rename the individual sections
245 if (GetParent( event.GetItem() ) == m_rootId)
246 {
247 event.Veto();
248 return;
249 }
250 }
251
252 void wxDirCtrl::OnEndEditItem(wxTreeEvent &event)
253 {
254 if ((event.GetLabel().IsEmpty()) ||
255 (event.GetLabel() == _(".")) ||
256 (event.GetLabel() == _("..")) ||
257 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
258 {
259 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
260 dialog.ShowModal();
261 event.Veto();
262 return;
263 }
264
265 wxTreeItemId id = event.GetItem();
266 wxDirItemData *data = (wxDirItemData*)GetItemData( id );
267 wxASSERT( data );
268
269 wxString new_name( wxPathOnly( data->m_path ) );
270 new_name += wxT("/");
271 new_name += event.GetLabel();
272
273 wxLogNull log;
274
275 if (wxFileExists(new_name))
276 {
277 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
278 dialog.ShowModal();
279 event.Veto();
280 }
281
282 if (wxRenameFile(data->m_path,new_name))
283 {
284 data->SetNewDirName( new_name );
285 }
286 else
287 {
288 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
289 dialog.ShowModal();
290 event.Veto();
291 }
292 }
293
294 void wxDirCtrl::OnExpandItem(wxTreeEvent &event)
295 {
296 if (event.GetItem() == m_rootId)
297 {
298 SetupSections();
299 CreateItems(m_rootId);
300 return;
301 }
302
303 // This may take a longish time. Go to busy cursor
304 wxBeginBusyCursor();
305
306 wxDirItemData *data = (wxDirItemData *)GetItemData(event.GetItem());
307 wxASSERT(data);
308
309 wxString search,path,filename;
310
311 m_paths.Clear();
312 m_names.Clear();
313 #ifdef __WXMSW__
314 search = data->m_path + "\\*.*";
315 #else
316 search = data->m_path + "/*";
317 #endif
318 for (path = wxFindFirstFile( search, wxDIR ); !path.IsNull();
319 path=wxFindNextFile() )
320 {
321 filename = wxFileNameFromPath( path );
322 /* Don't add "." and ".." to the tree. I think wxFindNextFile
323 * also checks this, but I don't quite understand what happens
324 * there. Also wxFindNextFile seems to swallow hidden dirs */
325 if ((filename != ".") && (filename != ".."))
326 {
327 m_paths.Add(path);
328 m_names.Add(filename);
329 }
330 }
331
332 CreateItems( event.GetItem() );
333 SortChildren( event.GetItem() );
334
335 wxEndBusyCursor();
336 }
337
338 void wxDirCtrl::OnCollapseItem(wxTreeEvent &event )
339 {
340 wxTreeItemId child, parent = event.GetItem();
341 long cookie;
342 /* Workaround because DeleteChildren has disapeared (why?) and
343 * CollapseAndReset doesn't work as advertised (deletes parent too) */
344 child = GetFirstChild(parent, cookie);
345 while (child.IsOk())
346 {
347 Delete(child);
348 /* Not GetNextChild below, because the cookie mechanism can't
349 * handle disappearing children! */
350 child = GetFirstChild(parent, cookie);
351 }
352 }
353
354 //-----------------------------------------------------------------------------
355 // wxDirDialog
356 //-----------------------------------------------------------------------------
357
358
359 #if !USE_SHARED_LIBRARY
360 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
361 #else
362 IMPLEMENT_DYNAMIC_CLASS( wxDirDialog, wxDialog )
363 #endif
364
365 BEGIN_EVENT_TABLE( wxDirDialog, wxDialog )
366 EVT_TREE_KEY_DOWN (ID_DIRCTRL, wxDirDialog::OnTreeKeyDown)
367 EVT_TREE_SEL_CHANGED (ID_DIRCTRL, wxDirDialog::OnTreeSelected)
368 EVT_SIZE ( wxDirDialog::OnSize)
369 EVT_BUTTON (ID_OK, wxDirDialog::OnOK)
370 EVT_BUTTON (ID_CANCEL, wxDirDialog::OnCancel)
371 EVT_BUTTON (ID_NEW, wxDirDialog::OnNew)
372 EVT_TEXT_ENTER (ID_TEXTCTRL, wxDirDialog::OnOK)
373 // EVT_CHECKBOX (ID_CHECK, wxDirDialog::OnCheck)
374 END_EVENT_TABLE()
375
376 wxDirDialog::wxDirDialog(wxWindow *parent, const wxString& message,
377 const wxString& defaultPath, long style,
378 const wxPoint& pos) :
379 wxDialog(parent, -1, message, pos, wxSize(300,300),
380 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
381 {
382 m_message = message;
383 m_dialogStyle = style;
384 m_parent = parent;
385
386 m_path = defaultPath;
387
388 wxBeginBusyCursor();
389
390 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
391
392 // 1) dir ctrl
393 m_dir = new wxDirCtrl( this, ID_DIRCTRL, "/", wxDefaultPosition, wxSize(200,200),
394 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER | wxTR_EDIT_LABELS);
395 topsizer->Add( m_dir, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
396
397 // 2) text ctrl
398 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
399 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
400
401 // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") );
402 // m_check->SetValue(TRUE);
403
404 #if wxUSE_STATLINE
405 // 3) static line
406 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
407 #endif
408
409 // 4) buttons
410 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
411 m_ok = new wxButton( this, ID_OK, _("OK") );
412 buttonsizer->Add( m_ok, 0, wxLEFT|wxRIGHT, 10 );
413 m_cancel = new wxButton( this, ID_CANCEL, _("Cancel") );
414 buttonsizer->Add( m_cancel, 0, wxLEFT|wxRIGHT, 10 );
415 m_new = new wxButton( this, ID_NEW, _("New...") );
416 buttonsizer->Add( m_new, 0, wxLEFT|wxRIGHT, 10 );
417
418 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
419
420 m_ok->SetDefault();
421 m_dir->SetFocus();
422
423 SetAutoLayout( TRUE );
424 SetSizer( topsizer );
425
426 topsizer->SetSizeHints( this );
427 topsizer->Fit( this );
428
429 Centre( wxBOTH );
430
431 wxEndBusyCursor();
432 }
433
434 int wxDirDialog::ShowModal()
435 {
436 m_input->SetValue( m_path );
437 return wxDialog::ShowModal();
438 }
439
440 void wxDirDialog::OnTreeSelected( wxTreeEvent &event )
441 {
442 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(event.GetItem());
443 if (data)
444 m_input->SetValue( data->m_path );
445 };
446
447 void wxDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
448 {
449 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(m_dir->GetSelection());
450 if (data)
451 m_input->SetValue( data->m_path );
452 };
453
454 void wxDirDialog::OnOK( wxCommandEvent& WXUNUSED(event) )
455 {
456 m_path = m_input->GetValue();
457 // Does the path exist? (User may have typed anything in m_input)
458 if (wxPathExists(m_path)) {
459 // OK, path exists, we're done.
460 EndModal(wxID_OK);
461 return;
462 }
463 // Interact with user, find out if the dir is a typo or to be created
464 wxString msg( _("The directory ") );
465 msg = msg + m_path;
466 msg = msg + _("\ndoes not exist\nCreate it now?") ;
467 wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING );
468 if ( dialog.ShowModal() == wxID_YES ) {
469 // Okay, let's make it
470 wxLogNull log;
471 if (wxMkdir(m_path)) {
472 // The new dir was created okay.
473 EndModal(wxID_OK);
474 return;
475 }
476 else {
477 // Trouble...
478 msg = _("Failed to create directory ")+m_path+
479 _("\n(Do you have the required permissions?)");
480 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
481 errmsg.ShowModal();
482 // We still don't have a valid dir. Back to the main dialog.
483 }
484 }
485 // User has answered NO to create dir.
486 }
487
488 void wxDirDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
489 {
490 EndModal(wxID_CANCEL);
491 }
492
493 void wxDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
494 {
495 wxTreeItemId id = m_dir->GetSelection();
496 if ((id == m_dir->GetRootItem()) ||
497 (m_dir->GetParent(id) == m_dir->GetRootItem()))
498 {
499 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
500 _("Create directory"), wxOK | wxICON_INFORMATION );
501 msg.ShowModal();
502 return;
503 }
504
505 wxTreeItemId parent = m_dir->GetParent( id );
506 wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData( parent );
507 wxASSERT( data );
508
509 wxString new_name( wxT("NewName") );
510 wxString path( data->m_path );
511 path += wxT("/");
512 path += new_name;
513 if (wxFileExists(path))
514 {
515 // try NewName0, NewName1 etc.
516 int i = 0;
517 do {
518 new_name = wxT("NewName");
519 wxString num;
520 num.Printf( "%d", i );
521 new_name += num;
522
523 path = data->m_path;
524 path += wxT("/");
525 path += new_name;
526 i++;
527 } while (wxFileExists(path));
528 }
529
530 wxLogNull log;
531 if (!wxMkdir(path))
532 {
533 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
534 dialog.ShowModal();
535 return;
536 }
537
538 wxDirItemData *new_data = new wxDirItemData( path, new_name );
539 wxTreeItemId new_id = m_dir->AppendItem( parent, new_name, 0, 1, new_data );
540 m_dir->EnsureVisible( new_id );
541 m_dir->EditLabel( new_id );
542 }
543
544 /*
545 void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) )
546 {
547 printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) );
548 }
549 */
550
551 #endif