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