]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: filedlgg.cpp | |
3 | // Purpose: wxFileDialog | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 12/12/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "filedlgg.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 | #ifndef __UNIX__ | |
24 | #error wxFileDialog currently only supports unix | |
25 | #endif | |
26 | ||
27 | #include "wx/filedlg.h" | |
28 | #include "wx/debug.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/intl.h" | |
31 | #include "wx/msgdlg.h" | |
32 | #include "wx/sizer.h" | |
33 | #include "wx/bmpbuttn.h" | |
34 | #include "wx/tokenzr.h" | |
35 | ||
36 | #if wxUSE_TOOLTIPS | |
37 | #include "wx/tooltip.h" | |
38 | #endif | |
39 | ||
40 | #include <sys/types.h> | |
41 | #include <sys/stat.h> | |
42 | #include <dirent.h> | |
43 | #include <pwd.h> | |
44 | #include <grp.h> | |
45 | #include <time.h> | |
46 | #include <unistd.h> | |
47 | ||
48 | #include "wx/generic/home.xpm" | |
49 | #include "wx/generic/listview.xpm" | |
50 | #include "wx/generic/repview.xpm" | |
51 | #include "wx/generic/new_dir.xpm" | |
52 | #include "wx/generic/dir_up.xpm" | |
53 | ||
54 | /* XPM */ | |
55 | static char * folder_xpm[] = { | |
56 | /* width height ncolors chars_per_pixel */ | |
57 | "16 16 6 1", | |
58 | /* colors */ | |
59 | " s None c None", | |
60 | ". c #000000", | |
61 | "+ c #c0c0c0", | |
62 | "@ c #808080", | |
63 | "# c #ffff00", | |
64 | "$ c #ffffff", | |
65 | /* pixels */ | |
66 | " ", | |
67 | " @@@@@ ", | |
68 | " @#+#+#@ ", | |
69 | " @#+#+#+#@@@@@@ ", | |
70 | " @$$$$$$$$$$$$@.", | |
71 | " @$#+#+#+#+#+#@.", | |
72 | " @$+#+#+#+#+#+@.", | |
73 | " @$#+#+#+#+#+#@.", | |
74 | " @$+#+#+#+#+#+@.", | |
75 | " @$#+#+#+#+#+#@.", | |
76 | " @$+#+#+#+#+#+@.", | |
77 | " @$#+#+#+#+#+#@.", | |
78 | " @@@@@@@@@@@@@@.", | |
79 | " ..............", | |
80 | " ", | |
81 | " "}; | |
82 | ||
83 | // ---------------------------------------------------------------------------- | |
84 | // private functions | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | static | |
88 | int ListCompare( long data1, long data2, long WXUNUSED(data) ) | |
89 | { | |
90 | wxFileData *fd1 = (wxFileData*)data1 ; | |
91 | wxFileData *fd2 = (wxFileData*)data2 ; | |
92 | if (fd1->GetName() == wxT("..")) return -1; | |
93 | if (fd2->GetName() == wxT("..")) return 1; | |
94 | if (fd1->IsDir() && !fd2->IsDir()) return -1; | |
95 | if (fd2->IsDir() && !fd1->IsDir()) return 1; | |
96 | return wxStrcmp( fd1->GetName(), fd2->GetName() ); | |
97 | } | |
98 | ||
99 | //----------------------------------------------------------------------------- | |
100 | // wxFileData | |
101 | //----------------------------------------------------------------------------- | |
102 | ||
103 | IMPLEMENT_DYNAMIC_CLASS(wxFileData,wxObject); | |
104 | ||
105 | wxFileData::wxFileData( const wxString &name, const wxString &fname ) | |
106 | { | |
107 | m_name = name; | |
108 | m_fileName = fname; | |
109 | ||
110 | struct stat buff; | |
111 | stat( m_fileName.fn_str(), &buff ); | |
112 | struct stat lbuff; | |
113 | lstat( m_fileName.fn_str(), &lbuff ); | |
114 | ||
115 | struct tm *t = localtime( &lbuff.st_mtime ); | |
116 | // struct passwd *user = getpwuid( buff.st_uid ); | |
117 | // struct group *grp = getgrgid( buff.st_gid ); | |
118 | ||
119 | m_isDir = S_ISDIR( buff.st_mode ); | |
120 | m_isLink = S_ISLNK( lbuff.st_mode ); | |
121 | m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR ); | |
122 | ||
123 | m_size = buff.st_size; | |
124 | ||
125 | m_hour = t->tm_hour; | |
126 | m_minute = t->tm_min; | |
127 | m_month = t->tm_mon+1; | |
128 | m_day = t->tm_mday; | |
129 | m_year = t->tm_year; | |
130 | ||
131 | m_permissions.sprintf( wxT("%c%c%c"), | |
132 | ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')), | |
133 | ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')), | |
134 | ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) ); | |
135 | } | |
136 | ||
137 | wxString wxFileData::GetName() const | |
138 | { | |
139 | return m_name; | |
140 | } | |
141 | ||
142 | wxString wxFileData::GetFullName() const | |
143 | { | |
144 | return m_fileName; | |
145 | } | |
146 | ||
147 | wxString wxFileData::GetHint() const | |
148 | { | |
149 | wxString s = m_fileName; | |
150 | s += " "; | |
151 | if (m_isDir) s += _("<DIR> "); | |
152 | else if (m_isLink) s += _("<LINK> "); | |
153 | else | |
154 | { | |
155 | s += LongToString( m_size ); | |
156 | s += _(" bytes "); | |
157 | } | |
158 | s += IntToString( m_day ); | |
159 | s += wxT("."); | |
160 | s += IntToString( m_month ); | |
161 | s += wxT("."); | |
162 | s += IntToString( m_year ); | |
163 | s += wxT(" "); | |
164 | s += IntToString( m_hour ); | |
165 | s += wxT(":"); | |
166 | s += IntToString( m_minute ); | |
167 | s += wxT(" "); | |
168 | s += m_permissions; | |
169 | return s; | |
170 | }; | |
171 | ||
172 | wxString wxFileData::GetEntry( int num ) | |
173 | { | |
174 | wxString s; | |
175 | switch (num) | |
176 | { | |
177 | case 0: | |
178 | s = m_name; | |
179 | break; | |
180 | case 1: | |
181 | if (m_isDir) s = _("<DIR>"); | |
182 | else if (m_isLink) s = _("<LINK>"); | |
183 | else s = LongToString( m_size ); | |
184 | break; | |
185 | case 2: | |
186 | if (m_day < 10) s = wxT("0"); else s = wxT(""); | |
187 | s += IntToString( m_day ); | |
188 | s += wxT("."); | |
189 | if (m_month < 10) s += wxT("0"); | |
190 | s += IntToString( m_month ); | |
191 | s += wxT("."); | |
192 | if (m_year < 10) s += wxT("0"); // this should happen real soon... | |
193 | s += IntToString( m_year ); | |
194 | break; | |
195 | case 3: | |
196 | if (m_hour < 10) s = wxT("0"); else s = wxT(""); | |
197 | s += IntToString( m_hour ); | |
198 | s += wxT(":"); | |
199 | if (m_minute < 10) s += wxT("0"); | |
200 | s += IntToString( m_minute ); | |
201 | break; | |
202 | case 4: | |
203 | s = m_permissions; | |
204 | break; | |
205 | default: | |
206 | s = wxT("No entry"); | |
207 | break; | |
208 | } | |
209 | return s; | |
210 | } | |
211 | ||
212 | bool wxFileData::IsDir() | |
213 | { | |
214 | return m_isDir; | |
215 | } | |
216 | ||
217 | bool wxFileData::IsExe() | |
218 | { | |
219 | return m_isExe; | |
220 | } | |
221 | ||
222 | bool wxFileData::IsLink() | |
223 | { | |
224 | return m_isLink; | |
225 | } | |
226 | ||
227 | long wxFileData::GetSize() | |
228 | { | |
229 | return m_size; | |
230 | } | |
231 | ||
232 | void wxFileData::SetNewName( const wxString &name, const wxString &fname ) | |
233 | { | |
234 | m_name = name; | |
235 | m_fileName = fname; | |
236 | } | |
237 | ||
238 | void wxFileData::MakeItem( wxListItem &item ) | |
239 | { | |
240 | item.m_text = m_name; | |
241 | item.m_colour = wxBLACK; | |
242 | if (IsExe()) item.m_colour = wxRED; | |
243 | if (IsDir()) item.m_colour = wxBLUE; | |
244 | if (IsDir()) item.m_image = 0; else item.m_image = -1; | |
245 | if (IsLink()) | |
246 | { | |
247 | wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" ); | |
248 | item.m_colour = dg; | |
249 | } | |
250 | item.m_data = (long)this; | |
251 | } | |
252 | ||
253 | //----------------------------------------------------------------------------- | |
254 | // wxFileCtrl | |
255 | //----------------------------------------------------------------------------- | |
256 | ||
257 | IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl); | |
258 | ||
259 | BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl) | |
260 | EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem) | |
261 | EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit) | |
262 | END_EVENT_TABLE() | |
263 | ||
264 | wxFileCtrl::wxFileCtrl() | |
265 | { | |
266 | m_dirName = wxT("/"); | |
267 | m_showHidden = FALSE; | |
268 | } | |
269 | ||
270 | wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id, | |
271 | const wxString &dirName, const wxString &wild, | |
272 | const wxPoint &pos, const wxSize &size, | |
273 | long style, const wxValidator &validator, const wxString &name ) : | |
274 | wxListCtrl( win, id, pos, size, style, validator, name ) | |
275 | { | |
276 | wxImageList *imageList = new wxImageList( 16, 16 ); | |
277 | imageList->Add( wxBitmap( folder_xpm ) ); | |
278 | SetImageList( imageList, wxIMAGE_LIST_SMALL ); | |
279 | ||
280 | m_dirName = dirName; | |
281 | m_wild = wild; | |
282 | m_showHidden = FALSE; | |
283 | Update(); | |
284 | } | |
285 | ||
286 | void wxFileCtrl::ChangeToListMode() | |
287 | { | |
288 | SetSingleStyle( wxLC_LIST ); | |
289 | Update(); | |
290 | } | |
291 | ||
292 | void wxFileCtrl::ChangeToReportMode() | |
293 | { | |
294 | SetSingleStyle( wxLC_REPORT ); | |
295 | Update(); | |
296 | } | |
297 | ||
298 | void wxFileCtrl::ChangeToIconMode() | |
299 | { | |
300 | SetSingleStyle( wxLC_ICON ); | |
301 | Update(); | |
302 | } | |
303 | ||
304 | void wxFileCtrl::ShowHidden( bool show ) | |
305 | { | |
306 | m_showHidden = show; | |
307 | Update(); | |
308 | } | |
309 | ||
310 | long wxFileCtrl::Add( wxFileData *fd, wxListItem &item ) | |
311 | { | |
312 | long ret = -1; | |
313 | item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE; | |
314 | fd->MakeItem( item ); | |
315 | long my_style = GetWindowStyleFlag(); | |
316 | if (my_style & wxLC_REPORT) | |
317 | { | |
318 | ret = InsertItem( item ); | |
319 | for (int i = 1; i < 5; i++) SetItem( item.m_itemId, i, fd->GetEntry( i) ); | |
320 | } | |
321 | else if (my_style & wxLC_LIST) | |
322 | { | |
323 | ret = InsertItem( item ); | |
324 | } | |
325 | return ret; | |
326 | } | |
327 | ||
328 | void wxFileCtrl::Update() | |
329 | { | |
330 | ClearAll(); | |
331 | long my_style = GetWindowStyleFlag(); | |
332 | if (my_style & wxLC_REPORT) | |
333 | { | |
334 | InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, 130 ); | |
335 | InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 ); | |
336 | InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 55 ); | |
337 | InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 ); | |
338 | InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 ); | |
339 | } | |
340 | wxFileData *fd = (wxFileData *) NULL; | |
341 | wxListItem item; | |
342 | item.m_itemId = 0; | |
343 | item.m_col = 0; | |
344 | ||
345 | if (m_dirName != wxT("/")) | |
346 | { | |
347 | wxString p( wxPathOnly(m_dirName) ); | |
348 | if (p.IsEmpty()) p = wxT("/"); | |
349 | fd = new wxFileData( wxT(".."), p ); | |
350 | Add( fd, item ); | |
351 | item.m_itemId++; | |
352 | } | |
353 | ||
354 | wxString res = m_dirName + wxT("/*"); | |
355 | wxString f( wxFindFirstFile( res.GetData(), wxDIR ) ); | |
356 | while (!f.IsEmpty()) | |
357 | { | |
358 | res = wxFileNameFromPath( f ); | |
359 | fd = new wxFileData( res, f ); | |
360 | wxString s = fd->GetName(); | |
361 | if (m_showHidden || (s[0] != wxT('.'))) | |
362 | { | |
363 | Add( fd, item ); | |
364 | item.m_itemId++; | |
365 | } | |
366 | f = wxFindNextFile(); | |
367 | } | |
368 | ||
369 | res = m_dirName + wxT("/") + m_wild; | |
370 | f = wxFindFirstFile( res.GetData(), wxFILE ); | |
371 | while (!f.IsEmpty()) | |
372 | { | |
373 | res = wxFileNameFromPath( f ); | |
374 | fd = new wxFileData( res, f ); | |
375 | wxString s = fd->GetName(); | |
376 | if (m_showHidden || (s[0] != wxT('.'))) | |
377 | { | |
378 | Add( fd, item ); | |
379 | item.m_itemId++; | |
380 | } | |
381 | f = wxFindNextFile(); | |
382 | } | |
383 | ||
384 | SortItems( ListCompare, 0 ); | |
385 | } | |
386 | ||
387 | void wxFileCtrl::SetWild( const wxString &wild ) | |
388 | { | |
389 | m_wild = wild; | |
390 | Update(); | |
391 | } | |
392 | ||
393 | void wxFileCtrl::MakeDir() | |
394 | { | |
395 | wxString new_name( wxT("NewName") ); | |
396 | wxString path( m_dirName ); | |
397 | path += wxT("/"); | |
398 | path += new_name; | |
399 | if (wxFileExists(path)) | |
400 | { | |
401 | // try NewName0, NewName1 etc. | |
402 | int i = 0; | |
403 | do { | |
404 | new_name = _("NewName"); | |
405 | wxString num; | |
406 | num.Printf( wxT("%d"), i ); | |
407 | new_name += num; | |
408 | ||
409 | path = m_dirName; | |
410 | path += wxT("/"); | |
411 | path += new_name; | |
412 | i++; | |
413 | } while (wxFileExists(path)); | |
414 | } | |
415 | ||
416 | wxLogNull log; | |
417 | if (!wxMkdir(path)) | |
418 | { | |
419 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
420 | dialog.ShowModal(); | |
421 | return; | |
422 | } | |
423 | ||
424 | wxFileData *fd = new wxFileData( new_name, path ); | |
425 | wxListItem item; | |
426 | item.m_itemId = 0; | |
427 | item.m_col = 0; | |
428 | int id = Add( fd, item ); | |
429 | ||
430 | if (id != -1) | |
431 | { | |
432 | SortItems( ListCompare, 0 ); | |
433 | id = FindItem( 0, (long)fd ); | |
434 | EnsureVisible( id ); | |
435 | EditLabel( id ); | |
436 | } | |
437 | } | |
438 | ||
439 | void wxFileCtrl::GoToParentDir() | |
440 | { | |
441 | if (m_dirName != wxT("/")) | |
442 | { | |
443 | wxString fname( wxFileNameFromPath(m_dirName) ); | |
444 | m_dirName = wxPathOnly( m_dirName ); | |
445 | if (m_dirName.IsEmpty()) m_dirName = wxT("/"); | |
446 | Update(); | |
447 | int id = FindItem( 0, fname ); | |
448 | if (id != -1) | |
449 | { | |
450 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
451 | EnsureVisible( id ); | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | void wxFileCtrl::GoToHomeDir() | |
457 | { | |
458 | wxString s = wxGetUserHome( wxString() ); | |
459 | m_dirName = s; | |
460 | Update(); | |
461 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
462 | EnsureVisible( 0 ); | |
463 | } | |
464 | ||
465 | void wxFileCtrl::GoToDir( const wxString &dir ) | |
466 | { | |
467 | m_dirName = dir; | |
468 | Update(); | |
469 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
470 | EnsureVisible( 0 ); | |
471 | } | |
472 | ||
473 | void wxFileCtrl::GetDir( wxString &dir ) | |
474 | { | |
475 | dir = m_dirName; | |
476 | } | |
477 | ||
478 | void wxFileCtrl::OnListDeleteItem( wxListEvent &event ) | |
479 | { | |
480 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
481 | delete fd; | |
482 | } | |
483 | ||
484 | void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event ) | |
485 | { | |
486 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
487 | wxASSERT( fd ); | |
488 | ||
489 | if ((event.GetLabel().IsEmpty()) || | |
490 | (event.GetLabel() == _(".")) || | |
491 | (event.GetLabel() == _("..")) || | |
492 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) | |
493 | { | |
494 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
495 | dialog.ShowModal(); | |
496 | event.Veto(); | |
497 | return; | |
498 | } | |
499 | ||
500 | wxString new_name( wxPathOnly( fd->GetFullName() ) ); | |
501 | new_name += wxT("/"); | |
502 | new_name += event.GetLabel(); | |
503 | ||
504 | wxLogNull log; | |
505 | ||
506 | if (wxFileExists(new_name)) | |
507 | { | |
508 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
509 | dialog.ShowModal(); | |
510 | event.Veto(); | |
511 | } | |
512 | ||
513 | if (wxRenameFile(fd->GetFullName(),new_name)) | |
514 | { | |
515 | fd->SetNewName( new_name, event.GetLabel() ); | |
516 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
517 | EnsureVisible( event.GetItem() ); | |
518 | } | |
519 | else | |
520 | { | |
521 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
522 | dialog.ShowModal(); | |
523 | event.Veto(); | |
524 | } | |
525 | } | |
526 | ||
527 | //----------------------------------------------------------------------------- | |
528 | // wxFileDialog | |
529 | //----------------------------------------------------------------------------- | |
530 | ||
531 | #define ID_LIST_MODE wxID_FILEDLGG | |
532 | #define ID_REPORT_MODE wxID_FILEDLGG + 1 | |
533 | #define ID_UP_DIR wxID_FILEDLGG + 5 | |
534 | #define ID_PARENT_DIR wxID_FILEDLGG + 6 | |
535 | #define ID_NEW_DIR wxID_FILEDLGG + 7 | |
536 | #define ID_CHOICE wxID_FILEDLGG + 8 | |
537 | #define ID_TEXT wxID_FILEDLGG + 9 | |
538 | #define ID_LIST_CTRL wxID_FILEDLGG + 10 | |
539 | #define ID_ACTIVATED wxID_FILEDLGG + 11 | |
540 | ||
541 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog) | |
542 | ||
543 | BEGIN_EVENT_TABLE(wxFileDialog,wxDialog) | |
544 | EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList) | |
545 | EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport) | |
546 | EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp) | |
547 | EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome) | |
548 | EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew) | |
549 | EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk) | |
550 | EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected) | |
551 | EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated) | |
552 | EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice) | |
553 | EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter) | |
554 | END_EVENT_TABLE() | |
555 | ||
556 | wxFileDialog::wxFileDialog(wxWindow *parent, | |
557 | const wxString& message, | |
558 | const wxString& defaultDir, | |
559 | const wxString& defaultFile, | |
560 | const wxString& wildCard, | |
561 | long style, | |
562 | const wxPoint& pos ) : | |
563 | wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) | |
564 | { | |
565 | wxBeginBusyCursor(); | |
566 | ||
567 | m_message = message; | |
568 | m_dialogStyle = style; | |
569 | m_dir = defaultDir; | |
570 | if (m_dir.IsEmpty()) | |
571 | { | |
572 | char buf[200]; | |
573 | m_dir = getcwd( buf, sizeof(buf) ); | |
574 | } | |
575 | m_path = defaultDir; | |
576 | m_path += wxT("/"); | |
577 | m_path += defaultFile; | |
578 | m_fileName = defaultFile; | |
579 | m_wildCard = wildCard; | |
580 | m_filterIndex = 0; | |
581 | ||
582 | // interpret wildcards | |
583 | ||
584 | if (m_wildCard.IsEmpty()) | |
585 | m_wildCard = _("All files (*)|*"); | |
586 | ||
587 | wxStringTokenizer tokens( m_wildCard, wxT("|") ); | |
588 | wxString firstWild; | |
589 | wxString firstWildText; | |
590 | if (tokens.CountTokens() == 1) | |
591 | { | |
592 | firstWildText = tokens.GetNextToken(); | |
593 | firstWild = firstWildText; | |
594 | } | |
595 | else | |
596 | { | |
597 | wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") ); | |
598 | firstWildText = tokens.GetNextToken(); | |
599 | firstWild = tokens.GetNextToken(); | |
600 | } | |
601 | ||
602 | // layout | |
603 | ||
604 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
605 | ||
606 | wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL ); | |
607 | ||
608 | wxBitmapButton *but; | |
609 | ||
610 | but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) ); | |
611 | #if wxUSE_TOOLTIPS | |
612 | but->SetToolTip( _("View files as a list view") ); | |
613 | #endif | |
614 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
615 | ||
616 | but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) ); | |
617 | #if wxUSE_TOOLTIPS | |
618 | but->SetToolTip( _("View files as a detailed view") ); | |
619 | #endif | |
620 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
621 | ||
622 | buttonsizer->Add( 30, 5, 1 ); | |
623 | ||
624 | but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) ); | |
625 | #if wxUSE_TOOLTIPS | |
626 | but->SetToolTip( _("Go to parent directory") ); | |
627 | #endif | |
628 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
629 | ||
630 | but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) ); | |
631 | #if wxUSE_TOOLTIPS | |
632 | but->SetToolTip( _("Go to home directory") ); | |
633 | #endif | |
634 | buttonsizer->Add( but, 0, wxALL, 5); | |
635 | ||
636 | buttonsizer->Add( 20, 20 ); | |
637 | ||
638 | but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) ); | |
639 | #if wxUSE_TOOLTIPS | |
640 | but->SetToolTip( _("Create new directory") ); | |
641 | #endif | |
642 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
643 | ||
644 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 ); | |
645 | ||
646 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); | |
647 | staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 ); | |
648 | m_static = new wxStaticText( this, -1, m_dir ); | |
649 | staticsizer->Add( m_static, 1 ); | |
650 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 ); | |
651 | ||
652 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, wxSize(440,180), | |
653 | wxLC_LIST | wxSUNKEN_BORDER | wxLC_SINGLE_SEL ); | |
654 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 ); | |
655 | ||
656 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
657 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER ); | |
658 | textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
659 | textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
660 | mainsizer->Add( textsizer, 0, wxEXPAND ); | |
661 | ||
662 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
663 | m_choice = new wxChoice( this, ID_CHOICE ); | |
664 | choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 ); | |
665 | choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 ); | |
666 | mainsizer->Add( choicesizer, 0, wxEXPAND ); | |
667 | ||
668 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
669 | while (tokens.HasMoreTokens()) | |
670 | { | |
671 | firstWildText = tokens.GetNextToken(); | |
672 | firstWild = tokens.GetNextToken(); | |
673 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
674 | } | |
675 | m_choice->SetSelection( 0 ); | |
676 | ||
677 | SetAutoLayout( TRUE ); | |
678 | SetSizer( mainsizer ); | |
679 | ||
680 | mainsizer->Fit( this ); | |
681 | mainsizer->SetSizeHints( this ); | |
682 | ||
683 | Centre( wxBOTH ); | |
684 | ||
685 | if (m_fileName.IsEmpty()) | |
686 | m_list->SetFocus(); | |
687 | else | |
688 | m_text->SetFocus(); | |
689 | ||
690 | wxEndBusyCursor(); | |
691 | } | |
692 | ||
693 | wxFileDialog::~wxFileDialog() | |
694 | { | |
695 | } | |
696 | ||
697 | void wxFileDialog::OnChoice( wxCommandEvent &event ) | |
698 | { | |
699 | wxString *str = (wxString*) m_choice->GetClientData( event.GetInt() ); | |
700 | m_list->SetWild( *str ); | |
701 | } | |
702 | ||
703 | void wxFileDialog::OnActivated( wxListEvent &event ) | |
704 | { | |
705 | HandleAction( event.m_item.m_text ); | |
706 | } | |
707 | ||
708 | void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) | |
709 | { | |
710 | wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
711 | cevent.SetEventObject( this ); | |
712 | GetEventHandler()->ProcessEvent( cevent ); | |
713 | } | |
714 | ||
715 | void wxFileDialog::OnSelected( wxListEvent &event ) | |
716 | { | |
717 | if (FindFocus() != m_list) return; | |
718 | ||
719 | wxString filename( event.m_item.m_text ); | |
720 | if (filename == wxT("..")) return; | |
721 | ||
722 | wxString dir; | |
723 | m_list->GetDir( dir ); | |
724 | if (dir != wxT("/")) dir += wxT("/"); | |
725 | dir += filename; | |
726 | if (wxDirExists(dir)) return; | |
727 | ||
728 | m_text->SetValue( filename ); | |
729 | } | |
730 | ||
731 | void wxFileDialog::HandleAction( const wxString &fn ) | |
732 | { | |
733 | wxString filename( fn ); | |
734 | wxString dir; | |
735 | m_list->GetDir( dir ); | |
736 | if (filename.IsEmpty()) return; | |
737 | if (filename == wxT(".")) return; | |
738 | ||
739 | if (filename == wxT("..")) | |
740 | { | |
741 | m_list->GoToParentDir(); | |
742 | m_list->SetFocus(); | |
743 | m_list->GetDir( dir ); | |
744 | m_static->SetLabel( dir ); | |
745 | return; | |
746 | } | |
747 | ||
748 | if (filename == wxT("~")) | |
749 | { | |
750 | m_list->GoToHomeDir(); | |
751 | m_list->SetFocus(); | |
752 | m_list->GetDir( dir ); | |
753 | m_static->SetLabel( dir ); | |
754 | return; | |
755 | } | |
756 | ||
757 | if (filename[0] == wxT('~')) | |
758 | { | |
759 | filename.Remove( 0, 1 ); | |
760 | wxString tmp( wxGetUserHome() ); | |
761 | tmp += wxT('/'); | |
762 | tmp += filename; | |
763 | filename = tmp; | |
764 | } | |
765 | ||
766 | if ((filename.Find(wxT('*')) != wxNOT_FOUND) || | |
767 | (filename.Find(wxT('?')) != wxNOT_FOUND)) | |
768 | { | |
769 | if (filename.Find(wxT('/')) != wxNOT_FOUND) | |
770 | { | |
771 | wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR ); | |
772 | return; | |
773 | } | |
774 | m_list->SetWild( filename ); | |
775 | return; | |
776 | } | |
777 | ||
778 | if (dir != wxT("/")) dir += wxT("/"); | |
779 | if (filename[0] != wxT('/')) | |
780 | { | |
781 | dir += filename; | |
782 | filename = dir; | |
783 | } | |
784 | ||
785 | if (wxDirExists(filename)) | |
786 | { | |
787 | m_list->GoToDir( filename ); | |
788 | m_list->GetDir( dir ); | |
789 | m_static->SetLabel( dir ); | |
790 | return; | |
791 | } | |
792 | ||
793 | if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) ) | |
794 | { | |
795 | if (wxFileExists( filename )) | |
796 | { | |
797 | wxString msg; | |
798 | msg.Printf( _("File '%s' already exists, do you really want to " | |
799 | "overwrite it?"), filename.c_str() ); | |
800 | ||
801 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) | |
802 | return; | |
803 | } | |
804 | } | |
805 | else if ( (m_dialogStyle & wxOPEN) && (m_dialogStyle & wxFILE_MUST_EXIST) ) | |
806 | { | |
807 | if ( !wxFileExists( filename ) ) | |
808 | { | |
809 | wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR ); | |
810 | return; | |
811 | } | |
812 | } | |
813 | ||
814 | SetPath( filename ); | |
815 | ||
816 | wxCommandEvent event; | |
817 | wxDialog::OnOK(event); | |
818 | } | |
819 | ||
820 | void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) ) | |
821 | { | |
822 | HandleAction( m_text->GetValue() ); | |
823 | } | |
824 | ||
825 | void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) ) | |
826 | { | |
827 | m_list->ChangeToListMode(); | |
828 | m_list->SetFocus(); | |
829 | } | |
830 | ||
831 | void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) ) | |
832 | { | |
833 | m_list->ChangeToReportMode(); | |
834 | m_list->SetFocus(); | |
835 | } | |
836 | ||
837 | void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) ) | |
838 | { | |
839 | m_list->GoToParentDir(); | |
840 | m_list->SetFocus(); | |
841 | wxString dir; | |
842 | m_list->GetDir( dir ); | |
843 | m_static->SetLabel( dir ); | |
844 | } | |
845 | ||
846 | void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) ) | |
847 | { | |
848 | m_list->GoToHomeDir(); | |
849 | m_list->SetFocus(); | |
850 | wxString dir; | |
851 | m_list->GetDir( dir ); | |
852 | m_static->SetLabel( dir ); | |
853 | } | |
854 | ||
855 | void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) ) | |
856 | { | |
857 | m_list->MakeDir(); | |
858 | } | |
859 | ||
860 | void wxFileDialog::SetPath( const wxString& path ) | |
861 | { | |
862 | // not only set the full path but also update filename and dir | |
863 | m_path = path; | |
864 | if ( !!path ) | |
865 | { | |
866 | wxString ext; | |
867 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
868 | if (!ext.IsEmpty()) | |
869 | { | |
870 | m_fileName += wxT("."); | |
871 | m_fileName += ext; | |
872 | } | |
873 | } | |
874 | } | |
875 | ||
876 | // ---------------------------------------------------------------------------- | |
877 | // global functions | |
878 | // ---------------------------------------------------------------------------- | |
879 | ||
880 | wxString | |
881 | wxFileSelectorEx(const wxChar *message, | |
882 | const wxChar *default_path, | |
883 | const wxChar *default_filename, | |
884 | int *WXUNUSED(indexDefaultExtension), | |
885 | const wxChar *wildcard, | |
886 | int flags, | |
887 | wxWindow *parent, | |
888 | int x, int y) | |
889 | { | |
890 | // TODO: implement this somehow | |
891 | return wxFileSelector(message, default_path, default_filename, wxT(""), | |
892 | wildcard, flags, parent, x, y); | |
893 | } | |
894 | ||
895 | wxString wxFileSelector( const wxChar *title, | |
896 | const wxChar *defaultDir, const wxChar *defaultFileName, | |
897 | const wxChar *defaultExtension, const wxChar *filter, int flags, | |
898 | wxWindow *parent, int x, int y ) | |
899 | { | |
900 | wxString filter2; | |
901 | if ( defaultExtension && !filter ) | |
902 | filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ; | |
903 | else if ( filter ) | |
904 | filter2 = filter; | |
905 | ||
906 | wxString defaultDirString; | |
907 | if (defaultDir) | |
908 | defaultDirString = defaultDir; | |
909 | ||
910 | wxString defaultFilenameString; | |
911 | if (defaultFileName) | |
912 | defaultFilenameString = defaultFileName; | |
913 | ||
914 | wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) ); | |
915 | ||
916 | if ( fileDialog.ShowModal() == wxID_OK ) | |
917 | { | |
918 | return fileDialog.GetPath(); | |
919 | } | |
920 | else | |
921 | { | |
922 | return wxEmptyString; | |
923 | } | |
924 | } | |
925 | ||
926 | wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent ) | |
927 | { | |
928 | wxChar *ext = (wxChar *)extension; | |
929 | ||
930 | wxChar prompt[50]; | |
931 | wxString str = _("Load %s file"); | |
932 | wxSprintf(prompt, str, what); | |
933 | ||
934 | if (*ext == wxT('.')) ext++; | |
935 | wxChar wild[60]; | |
936 | wxSprintf(wild, wxT("*.%s"), ext); | |
937 | ||
938 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
939 | } | |
940 | ||
941 | wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name, | |
942 | wxWindow *parent ) | |
943 | { | |
944 | wxChar *ext = (wxChar *)extension; | |
945 | ||
946 | wxChar prompt[50]; | |
947 | wxString str = _("Save %s file"); | |
948 | wxSprintf(prompt, str, what); | |
949 | ||
950 | if (*ext == wxT('.')) ext++; | |
951 | wxChar wild[60]; | |
952 | wxSprintf(wild, wxT("*.%s"), ext); | |
953 | ||
954 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
955 | } | |
956 |