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