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