]>
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" | |
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" | |
f6bcfd97 | 41 | #include "wx/tokenzr.h" |
d9ff0f91 | 42 | #include "wx/dir.h" |
dc6c62a9 RR |
43 | |
44 | #if wxUSE_STATLINE | |
45 | #include "wx/statline.h" | |
46 | #endif | |
d7a15103 JS |
47 | |
48 | #include "wx/generic/dirdlgg.h" | |
49 | ||
4bf78aae JS |
50 | // If compiled under Windows, this macro can cause problems |
51 | #ifdef GetFirstChild | |
52 | #undef GetFirstChild | |
53 | #endif | |
54 | ||
a17e237f | 55 | #ifndef __WXMSW__ |
d7a15103 JS |
56 | /* XPM */ |
57 | static char * icon1_xpm[] = { | |
58 | /* width height ncolors chars_per_pixel */ | |
59 | "16 16 6 1", | |
60 | /* colors */ | |
e90c1d2a VZ |
61 | " s None c None", |
62 | ". c #000000", | |
63 | "+ c #c0c0c0", | |
64 | "@ c #808080", | |
65 | "# c #ffff00", | |
66 | "$ c #ffffff", | |
d7a15103 JS |
67 | /* pixels */ |
68 | " ", | |
69 | " @@@@@ ", | |
70 | " @#+#+#@ ", | |
71 | " @#+#+#+#@@@@@@ ", | |
72 | " @$$$$$$$$$$$$@.", | |
73 | " @$#+#+#+#+#+#@.", | |
74 | " @$+#+#+#+#+#+@.", | |
75 | " @$#+#+#+#+#+#@.", | |
76 | " @$+#+#+#+#+#+@.", | |
77 | " @$#+#+#+#+#+#@.", | |
78 | " @$+#+#+#+#+#+@.", | |
79 | " @$#+#+#+#+#+#@.", | |
80 | " @@@@@@@@@@@@@@.", | |
81 | " ..............", | |
82 | " ", | |
83 | " "}; | |
84 | ||
0659e7ee RR |
85 | /* XPM */ |
86 | static char * icon2_xpm[] = { | |
87 | /* width height ncolors chars_per_pixel */ | |
88 | "16 16 6 1", | |
89 | /* colors */ | |
e90c1d2a VZ |
90 | " s None c None", |
91 | ". c #000000", | |
92 | "+ c #c0c0c0", | |
93 | "@ c #808080", | |
94 | "# c #ffff00", | |
95 | "$ c #ffffff", | |
0659e7ee RR |
96 | /* pixels */ |
97 | " ", | |
98 | " @@@@@ ", | |
99 | " @$$$$$@ ", | |
100 | " @$#+#+#$@@@@@@ ", | |
101 | " @$+#+#+$$$$$$@.", | |
102 | " @$#+#+#+#+#+#@.", | |
103 | "@@@@@@@@@@@@@#@.", | |
104 | "@$$$$$$$$$$@@+@.", | |
105 | "@$#+#+#+#+##.@@.", | |
106 | " @$#+#+#+#+#+.@.", | |
107 | " @$+#+#+#+#+#.@.", | |
108 | " @$+#+#+#+##@..", | |
109 | " @@@@@@@@@@@@@.", | |
110 | " .............", | |
111 | " ", | |
112 | " "}; | |
d7a15103 | 113 | |
a17e237f VZ |
114 | #endif // !wxMSW |
115 | ||
d7a15103 JS |
116 | static const int ID_DIRCTRL = 1000; |
117 | static const int ID_TEXTCTRL = 1001; | |
118 | static const int ID_OK = 1002; | |
119 | static const int ID_CANCEL = 1003; | |
120 | static const int ID_NEW = 1004; | |
121 | //static const int ID_CHECK = 1005; | |
122 | ||
123 | //----------------------------------------------------------------------------- | |
124 | // wxDirItemData | |
125 | //----------------------------------------------------------------------------- | |
126 | ||
dc6c62a9 | 127 | wxDirItemData::wxDirItemData(wxString& path, wxString& name) |
0659e7ee | 128 | { |
dc6c62a9 RR |
129 | m_path = path; |
130 | m_name = name; | |
e90c1d2a VZ |
131 | /* Insert logic to detect hidden files here |
132 | * In UnixLand we just check whether the first char is a dot | |
dc6c62a9 RR |
133 | * For FileNameFromPath read LastDirNameInThisPath ;-) */ |
134 | // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.'); | |
135 | m_isHidden = FALSE; | |
136 | m_hasSubDirs = HasSubDirs(); | |
137 | } | |
0659e7ee | 138 | |
dc6c62a9 | 139 | wxDirItemData::~wxDirItemData() |
d7a15103 | 140 | { |
d7a15103 JS |
141 | } |
142 | ||
dc6c62a9 | 143 | void wxDirItemData::SetNewDirName( wxString path ) |
d7a15103 | 144 | { |
dc6c62a9 RR |
145 | m_path = path; |
146 | m_name = wxFileNameFromPath( path ); | |
d7a15103 JS |
147 | } |
148 | ||
149 | bool wxDirItemData::HasSubDirs() | |
150 | { | |
d9ff0f91 | 151 | return wxDir(m_path).HasSubDirs(); |
d7a15103 JS |
152 | } |
153 | ||
154 | //----------------------------------------------------------------------------- | |
155 | // wxDirCtrl | |
156 | //----------------------------------------------------------------------------- | |
157 | ||
158 | IMPLEMENT_DYNAMIC_CLASS(wxDirCtrl,wxTreeCtrl) | |
159 | ||
160 | BEGIN_EVENT_TABLE(wxDirCtrl,wxTreeCtrl) | |
161 | EVT_TREE_ITEM_EXPANDING (-1, wxDirCtrl::OnExpandItem) | |
162 | EVT_TREE_ITEM_COLLAPSED (-1, wxDirCtrl::OnCollapseItem) | |
dc6c62a9 RR |
163 | EVT_TREE_BEGIN_LABEL_EDIT (-1, wxDirCtrl::OnBeginEditItem) |
164 | EVT_TREE_END_LABEL_EDIT (-1, wxDirCtrl::OnEndEditItem) | |
d7a15103 JS |
165 | END_EVENT_TABLE() |
166 | ||
167 | wxDirCtrl::wxDirCtrl(void) | |
168 | { | |
dc6c62a9 RR |
169 | m_showHidden = FALSE; |
170 | } | |
d7a15103 | 171 | |
f6bcfd97 BP |
172 | wxDirCtrl::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 ) | |
d7a15103 | 180 | { |
f6bcfd97 | 181 | #ifndef __WXMSW__ |
e951f847 | 182 | m_imageListNormal = new wxImageList(16, 16, TRUE); |
dc6c62a9 RR |
183 | m_imageListNormal->Add(wxICON(icon1)); |
184 | m_imageListNormal->Add(wxICON(icon2)); | |
185 | SetImageList(m_imageListNormal); | |
f6bcfd97 | 186 | #endif // !MSW |
e90c1d2a | 187 | |
dc6c62a9 RR |
188 | m_showHidden = FALSE; |
189 | m_rootId = AddRoot( _("Sections") ); | |
190 | SetItemHasChildren(m_rootId); | |
191 | Expand(m_rootId); // automatically expand first level | |
192 | } | |
d7a15103 JS |
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 | ||
e90c1d2a | 198 | void wxDirCtrl::SetupSections() |
d7a15103 JS |
199 | { |
200 | wxString home; | |
201 | ||
202 | m_paths.Clear(); | |
203 | m_names.Clear(); | |
3f3cec48 RR |
204 | #ifdef __WXMSW__ |
205 | // better than nothing | |
223d09f6 | 206 | ADD_SECTION(wxT("c:\\"), _("My Harddisk") ) |
3f3cec48 | 207 | #else |
223d09f6 | 208 | ADD_SECTION(wxT("/"), _("The Computer") ) |
d7a15103 JS |
209 | wxGetHomeDir(&home); |
210 | ADD_SECTION(home, _("My Home") ) | |
223d09f6 | 211 | ADD_SECTION(wxT("/mnt"), _("Mounted Devices") ) |
223d09f6 | 212 | ADD_SECTION(wxT("/usr/local"), _("User Local") ) |
f6bcfd97 | 213 | ADD_SECTION(wxT("/usr"), _("User") ) |
223d09f6 KB |
214 | ADD_SECTION(wxT("/var"), _("Variables") ) |
215 | ADD_SECTION(wxT("/etc"), _("Etcetera") ) | |
216 | ADD_SECTION(wxT("/tmp"), _("Temporary") ) | |
3f3cec48 | 217 | #endif |
d7a15103 JS |
218 | } |
219 | #undef ADD_SECTION | |
220 | ||
221 | void wxDirCtrl::CreateItems(const wxTreeItemId &parent) | |
222 | { | |
dc6c62a9 RR |
223 | wxTreeItemId id; |
224 | wxDirItemData *dir_item; | |
d7a15103 JS |
225 | |
226 | // wxASSERT(m_paths.Count() == m_names.Count()); ? | |
e90c1d2a | 227 | |
f6bcfd97 BP |
228 | size_t count = m_paths.GetCount(); |
229 | for ( size_t i=0; i<count; i++) | |
dc6c62a9 | 230 | { |
17bec151 | 231 | dir_item = new wxDirItemData(m_paths[i],m_names[i]); |
3f3cec48 | 232 | #ifdef __WXMSW__ |
17bec151 | 233 | id = AppendItem( parent, m_names[i], -1, -1, dir_item); |
3f3cec48 | 234 | #else |
17bec151 | 235 | id = AppendItem( parent, m_names[i], 0, -1, dir_item); |
f6bcfd97 | 236 | SetItemImage( id, 1, wxTreeItemIcon_Expanded ); |
3f3cec48 | 237 | #endif |
dc6c62a9 RR |
238 | if (dir_item->m_hasSubDirs) SetItemHasChildren(id); |
239 | } | |
d7a15103 JS |
240 | } |
241 | ||
dc6c62a9 | 242 | void wxDirCtrl::OnBeginEditItem(wxTreeEvent &event) |
d7a15103 | 243 | { |
dc6c62a9 RR |
244 | // don't rename the main entry "Sections" |
245 | if (event.GetItem() == m_rootId) | |
246 | { | |
247 | event.Veto(); | |
248 | return; | |
249 | } | |
e90c1d2a | 250 | |
dc6c62a9 RR |
251 | // don't rename the individual sections |
252 | if (GetParent( event.GetItem() ) == m_rootId) | |
253 | { | |
254 | event.Veto(); | |
255 | return; | |
256 | } | |
257 | } | |
d7a15103 | 258 | |
dc6c62a9 RR |
259 | void wxDirCtrl::OnEndEditItem(wxTreeEvent &event) |
260 | { | |
261 | if ((event.GetLabel().IsEmpty()) || | |
262 | (event.GetLabel() == _(".")) || | |
263 | (event.GetLabel() == _("..")) || | |
f6bcfd97 | 264 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) |
dc6c62a9 RR |
265 | { |
266 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
f6bcfd97 | 267 | dialog.ShowModal(); |
dc6c62a9 | 268 | event.Veto(); |
f6bcfd97 | 269 | return; |
dc6c62a9 | 270 | } |
d7a15103 | 271 | |
dc6c62a9 RR |
272 | wxTreeItemId id = event.GetItem(); |
273 | wxDirItemData *data = (wxDirItemData*)GetItemData( id ); | |
274 | wxASSERT( data ); | |
e90c1d2a | 275 | |
dc6c62a9 | 276 | wxString new_name( wxPathOnly( data->m_path ) ); |
223d09f6 | 277 | new_name += wxT("/"); |
dc6c62a9 | 278 | new_name += event.GetLabel(); |
e90c1d2a | 279 | |
dc6c62a9 | 280 | wxLogNull log; |
e90c1d2a | 281 | |
dc6c62a9 RR |
282 | if (wxFileExists(new_name)) |
283 | { | |
284 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
f6bcfd97 | 285 | dialog.ShowModal(); |
dc6c62a9 RR |
286 | event.Veto(); |
287 | } | |
e90c1d2a | 288 | |
dc6c62a9 RR |
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 ); | |
f6bcfd97 | 296 | dialog.ShowModal(); |
dc6c62a9 RR |
297 | event.Veto(); |
298 | } | |
299 | } | |
d7a15103 | 300 | |
dc6c62a9 RR |
301 | void wxDirCtrl::OnExpandItem(wxTreeEvent &event) |
302 | { | |
303 | if (event.GetItem() == m_rootId) | |
304 | { | |
305 | SetupSections(); | |
306 | CreateItems(m_rootId); | |
307 | return; | |
d7a15103 | 308 | } |
d7a15103 | 309 | |
dc6c62a9 RR |
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 | ||
dc6c62a9 RR |
316 | m_paths.Clear(); |
317 | m_names.Clear(); | |
d9ff0f91 VZ |
318 | |
319 | wxDir dir(data->m_path); | |
320 | ||
321 | wxString filename; | |
322 | bool cont = dir.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN); | |
323 | while ( cont ) | |
dc6c62a9 | 324 | { |
d9ff0f91 VZ |
325 | m_paths.Add(data->m_path); |
326 | m_names.Add(filename); | |
327 | ||
328 | cont = dir.GetNext(&filename); | |
dc6c62a9 | 329 | } |
e90c1d2a | 330 | |
dc6c62a9 RR |
331 | CreateItems( event.GetItem() ); |
332 | SortChildren( event.GetItem() ); | |
e90c1d2a | 333 | |
dc6c62a9 RR |
334 | wxEndBusyCursor(); |
335 | } | |
d7a15103 | 336 | |
1e6d9499 | 337 | void wxDirCtrl::OnCollapseItem(wxTreeEvent &event ) |
d7a15103 | 338 | { |
dc6c62a9 RR |
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) */ | |
d7a15103 | 343 | child = GetFirstChild(parent, cookie); |
e90c1d2a | 344 | while (child.IsOk()) |
dc6c62a9 RR |
345 | { |
346 | Delete(child); | |
e90c1d2a | 347 | /* Not GetNextChild below, because the cookie mechanism can't |
dc6c62a9 RR |
348 | * handle disappearing children! */ |
349 | child = GetFirstChild(parent, cookie); | |
350 | } | |
351 | } | |
d7a15103 JS |
352 | |
353 | //----------------------------------------------------------------------------- | |
354 | // wxDirDialog | |
355 | //----------------------------------------------------------------------------- | |
356 | ||
357 | ||
d7a15103 | 358 | IMPLEMENT_DYNAMIC_CLASS( wxDirDialog, wxDialog ) |
d7a15103 JS |
359 | |
360 | BEGIN_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) | |
369 | END_EVENT_TABLE() | |
370 | ||
f6bcfd97 BP |
371 | wxDirDialog::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) | |
d7a15103 | 378 | { |
dc6c62a9 RR |
379 | m_message = message; |
380 | m_dialogStyle = style; | |
381 | m_parent = parent; | |
e90c1d2a | 382 | |
dc6c62a9 | 383 | m_path = defaultPath; |
e90c1d2a | 384 | |
dc6c62a9 | 385 | wxBeginBusyCursor(); |
e90c1d2a | 386 | |
dc6c62a9 RR |
387 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
388 | ||
389 | // 1) dir ctrl | |
eb085907 | 390 | m_dir = new wxDirCtrl( this, ID_DIRCTRL, _T("/"), |
f6bcfd97 BP |
391 | wxDefaultPosition, |
392 | wxSize(200,200), | |
393 | wxTR_HAS_BUTTONS | | |
394 | wxSUNKEN_BORDER | | |
395 | wxTR_EDIT_LABELS ); | |
dc6c62a9 | 396 | topsizer->Add( m_dir, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); |
e90c1d2a | 397 | |
dc6c62a9 | 398 | // 2) text ctrl |
e90c1d2a | 399 | m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition ); |
dc6c62a9 | 400 | topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); |
e90c1d2a | 401 | |
dc6c62a9 RR |
402 | // m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden") ); |
403 | // m_check->SetValue(TRUE); | |
e90c1d2a | 404 | |
dc6c62a9 RR |
405 | #if wxUSE_STATLINE |
406 | // 3) static line | |
407 | topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
408 | #endif | |
d7a15103 | 409 | |
dc6c62a9 RR |
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 ); | |
e90c1d2a | 418 | |
dc6c62a9 RR |
419 | topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 ); |
420 | ||
421 | m_ok->SetDefault(); | |
422 | m_dir->SetFocus(); | |
e90c1d2a | 423 | |
dc6c62a9 RR |
424 | SetAutoLayout( TRUE ); |
425 | SetSizer( topsizer ); | |
e90c1d2a | 426 | |
dc6c62a9 RR |
427 | topsizer->SetSizeHints( this ); |
428 | topsizer->Fit( this ); | |
429 | ||
430 | Centre( wxBOTH ); | |
431 | ||
f6bcfd97 BP |
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 | ||
dc6c62a9 | 492 | wxEndBusyCursor(); |
d7a15103 JS |
493 | } |
494 | ||
495 | int wxDirDialog::ShowModal() | |
496 | { | |
dc6c62a9 RR |
497 | m_input->SetValue( m_path ); |
498 | return wxDialog::ShowModal(); | |
d7a15103 JS |
499 | } |
500 | ||
501 | void wxDirDialog::OnTreeSelected( wxTreeEvent &event ) | |
502 | { | |
dc6c62a9 | 503 | wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(event.GetItem()); |
e90c1d2a | 504 | if (data) |
dc6c62a9 | 505 | m_input->SetValue( data->m_path ); |
d7a15103 JS |
506 | }; |
507 | ||
1e6d9499 | 508 | void wxDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) ) |
d7a15103 | 509 | { |
dc6c62a9 | 510 | wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData(m_dir->GetSelection()); |
e90c1d2a | 511 | if (data) |
dc6c62a9 | 512 | m_input->SetValue( data->m_path ); |
d7a15103 JS |
513 | }; |
514 | ||
515 | void 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?") ; | |
dc6c62a9 | 528 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING ); |
d7a15103 JS |
529 | if ( dialog.ShowModal() == wxID_YES ) { |
530 | // Okay, let's make it | |
dc6c62a9 | 531 | wxLogNull log; |
d7a15103 JS |
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+ | |
e90c1d2a | 540 | _("\n(Do you have the required permissions?)"); |
dc6c62a9 | 541 | wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR); |
d7a15103 JS |
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 | ||
549 | void wxDirDialog::OnCancel( wxCommandEvent& WXUNUSED(event) ) | |
550 | { | |
551 | EndModal(wxID_CANCEL); | |
552 | } | |
553 | ||
554 | void wxDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) ) | |
555 | { | |
dc6c62a9 RR |
556 | wxTreeItemId id = m_dir->GetSelection(); |
557 | if ((id == m_dir->GetRootItem()) || | |
558 | (m_dir->GetParent(id) == m_dir->GetRootItem())) | |
559 | { | |
e90c1d2a VZ |
560 | wxMessageDialog msg(this, _("You cannot add a new directory to this section."), |
561 | _("Create directory"), wxOK | wxICON_INFORMATION ); | |
dc6c62a9 RR |
562 | msg.ShowModal(); |
563 | return; | |
564 | } | |
d7a15103 | 565 | |
dc6c62a9 RR |
566 | wxTreeItemId parent = m_dir->GetParent( id ); |
567 | wxDirItemData *data = (wxDirItemData*)m_dir->GetItemData( parent ); | |
568 | wxASSERT( data ); | |
e90c1d2a | 569 | |
223d09f6 | 570 | wxString new_name( wxT("NewName") ); |
dc6c62a9 | 571 | wxString path( data->m_path ); |
223d09f6 | 572 | path += wxT("/"); |
dc6c62a9 RR |
573 | path += new_name; |
574 | if (wxFileExists(path)) | |
575 | { | |
576 | // try NewName0, NewName1 etc. | |
577 | int i = 0; | |
f6bcfd97 | 578 | do { |
223d09f6 | 579 | new_name = wxT("NewName"); |
f6bcfd97 BP |
580 | wxString num; |
581 | num.Printf( wxT("%d"), i ); | |
582 | new_name += num; | |
e90c1d2a | 583 | |
dc6c62a9 | 584 | path = data->m_path; |
223d09f6 | 585 | path += wxT("/"); |
dc6c62a9 | 586 | path += new_name; |
f6bcfd97 BP |
587 | i++; |
588 | } while (wxFileExists(path)); | |
d7a15103 | 589 | } |
e90c1d2a | 590 | |
dc6c62a9 | 591 | wxLogNull log; |
e90c1d2a | 592 | if (!wxMkdir(path)) |
dc6c62a9 RR |
593 | { |
594 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
f6bcfd97 | 595 | dialog.ShowModal(); |
dc6c62a9 RR |
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 ); | |
d7a15103 JS |
603 | } |
604 | ||
605 | /* | |
606 | void wxDirDialog::OnCheck( wxCommandEvent& WXUNUSED(event) ) | |
607 | { | |
608 | printf("Checkbox clicked: %s\n", ( m_check->GetValue() ? "on" : "off" ) ); | |
609 | } | |
610 | */ | |
ce4169a4 | 611 | |
f6bcfd97 | 612 | #endif // wxUSE_DIRDLG |