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