]>
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 | #if wxUSE_FILEDLG | |
24 | ||
25 | #ifndef __UNIX__ | |
26 | #error wxFileDialog currently only supports unix | |
27 | #endif | |
28 | ||
29 | #include "wx/checkbox.h" | |
30 | #include "wx/textctrl.h" | |
31 | #include "wx/choice.h" | |
32 | #include "wx/checkbox.h" | |
33 | #include "wx/stattext.h" | |
34 | #include "wx/filedlg.h" | |
35 | #include "wx/debug.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/intl.h" | |
38 | #include "wx/listctrl.h" | |
39 | #include "wx/msgdlg.h" | |
40 | #include "wx/sizer.h" | |
41 | #include "wx/bmpbuttn.h" | |
42 | #include "wx/tokenzr.h" | |
43 | #include "wx/mimetype.h" | |
44 | #include "wx/image.h" | |
45 | #include "wx/module.h" | |
46 | #include "wx/config.h" | |
47 | #include "wx/imaglist.h" | |
48 | ||
49 | #if wxUSE_TOOLTIPS | |
50 | #include "wx/tooltip.h" | |
51 | #endif | |
52 | ||
53 | #include <sys/types.h> | |
54 | #include <sys/stat.h> | |
55 | #include <dirent.h> | |
56 | #include <pwd.h> | |
57 | #ifndef __VMS | |
58 | # include <grp.h> | |
59 | #endif | |
60 | # include <time.h> | |
61 | #include <unistd.h> | |
62 | ||
63 | #include "wx/generic/home.xpm" | |
64 | #include "wx/generic/listview.xpm" | |
65 | #include "wx/generic/repview.xpm" | |
66 | #include "wx/generic/new_dir.xpm" | |
67 | #include "wx/generic/dir_up.xpm" | |
68 | #include "wx/generic/folder.xpm" | |
69 | #include "wx/generic/deffile.xpm" | |
70 | #include "wx/generic/exefile.xpm" | |
71 | ||
72 | //----------------------------------------------------------------------------- | |
73 | // wxFileData | |
74 | //----------------------------------------------------------------------------- | |
75 | ||
76 | class wxFileData : public wxObject | |
77 | { | |
78 | private: | |
79 | wxString m_name; | |
80 | wxString m_fileName; | |
81 | long m_size; | |
82 | int m_hour; | |
83 | int m_minute; | |
84 | int m_year; | |
85 | int m_month; | |
86 | int m_day; | |
87 | wxString m_permissions; | |
88 | bool m_isDir; | |
89 | bool m_isLink; | |
90 | bool m_isExe; | |
91 | ||
92 | public: | |
93 | wxFileData() { } | |
94 | wxFileData( const wxString &name, const wxString &fname ); | |
95 | wxString GetName() const; | |
96 | wxString GetFullName() const; | |
97 | wxString GetHint() const; | |
98 | wxString GetEntry( int num ); | |
99 | bool IsDir(); | |
100 | bool IsLink(); | |
101 | bool IsExe(); | |
102 | long GetSize(); | |
103 | void MakeItem( wxListItem &item ); | |
104 | void SetNewName( const wxString &name, const wxString &fname ); | |
105 | ||
106 | private: | |
107 | DECLARE_DYNAMIC_CLASS(wxFileData); | |
108 | }; | |
109 | ||
110 | //----------------------------------------------------------------------------- | |
111 | // wxFileCtrl | |
112 | //----------------------------------------------------------------------------- | |
113 | ||
114 | class wxFileCtrl : public wxListCtrl | |
115 | { | |
116 | private: | |
117 | wxString m_dirName; | |
118 | bool m_showHidden; | |
119 | wxString m_wild; | |
120 | ||
121 | public: | |
122 | wxFileCtrl(); | |
123 | wxFileCtrl( wxWindow *win, | |
124 | wxWindowID id, | |
125 | const wxString &dirName, | |
126 | const wxString &wild, | |
127 | const wxPoint &pos = wxDefaultPosition, | |
128 | const wxSize &size = wxDefaultSize, | |
129 | long style = wxLC_LIST, | |
130 | const wxValidator &validator = wxDefaultValidator, | |
131 | const wxString &name = wxT("filelist") ); | |
132 | void ChangeToListMode(); | |
133 | void ChangeToReportMode(); | |
134 | void ChangeToIconMode(); | |
135 | void ShowHidden( bool show = TRUE ); | |
136 | long Add( wxFileData *fd, wxListItem &item ); | |
137 | void Update(); | |
138 | virtual void StatusbarText( wxChar *WXUNUSED(text) ) {}; | |
139 | void MakeDir(); | |
140 | void GoToParentDir(); | |
141 | void GoToHomeDir(); | |
142 | void GoToDir( const wxString &dir ); | |
143 | void SetWild( const wxString &wild ); | |
144 | void GetDir( wxString &dir ); | |
145 | void OnListDeleteItem( wxListEvent &event ); | |
146 | void OnListDeleteAllItems( wxListEvent &event ); | |
147 | void OnListEndLabelEdit( wxListEvent &event ); | |
148 | ||
149 | private: | |
150 | DECLARE_DYNAMIC_CLASS(wxFileCtrl); | |
151 | DECLARE_EVENT_TABLE() | |
152 | }; | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // private classes - icons list management | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | class wxFileIconEntry : public wxObject | |
159 | { | |
160 | public: | |
161 | wxFileIconEntry(int i) { id = i; } | |
162 | ||
163 | int id; | |
164 | }; | |
165 | ||
166 | ||
167 | class wxFileIconsTable | |
168 | { | |
169 | public: | |
170 | wxFileIconsTable(); | |
171 | ||
172 | int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString); | |
173 | wxImageList *GetImageList() { return &m_ImageList; } | |
174 | ||
175 | protected: | |
176 | wxImageList m_ImageList; | |
177 | wxHashTable m_HashTable; | |
178 | }; | |
179 | ||
180 | static wxFileIconsTable *g_IconsTable = NULL; | |
181 | ||
182 | #define FI_FOLDER 0 | |
183 | #define FI_UNKNOWN 1 | |
184 | #define FI_EXECUTABLE 2 | |
185 | ||
186 | wxFileIconsTable::wxFileIconsTable() : | |
187 | m_ImageList(16, 16), | |
188 | m_HashTable(wxKEY_STRING) | |
189 | { | |
190 | m_HashTable.DeleteContents(TRUE); | |
191 | m_ImageList.Add(wxBitmap(folder_xpm)); // FI_FOLDER | |
192 | m_ImageList.Add(wxBitmap(deffile_xpm)); // FI_UNKNOWN | |
193 | if (GetIconID(wxEmptyString, _T("application/x-executable")) == FI_UNKNOWN) | |
194 | { // FI_EXECUTABLE | |
195 | m_ImageList.Add(wxBitmap(exefile_xpm)); | |
196 | m_HashTable.Delete(_T("exe")); | |
197 | m_HashTable.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE)); | |
198 | } | |
199 | /* else put into list by GetIconID | |
200 | (KDE defines application/x-executable for *.exe and has nice icon) | |
201 | */ | |
202 | } | |
203 | ||
204 | ||
205 | ||
206 | static wxBitmap CreateAntialiasedBitmap(const wxImage& img) | |
207 | { | |
208 | wxImage small(16, 16); | |
209 | unsigned char *p1, *p2, *ps; | |
210 | unsigned char mr = img.GetMaskRed(), | |
211 | mg = img.GetMaskGreen(), | |
212 | mb = img.GetMaskBlue(); | |
213 | ||
214 | unsigned x, y; | |
215 | unsigned sr, sg, sb, smask; | |
216 | ||
217 | p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = small.GetData(); | |
218 | small.SetMaskColour(mr, mr, mr); | |
219 | ||
220 | for (y = 0; y < 16; y++) | |
221 | { | |
222 | for (x = 0; x < 16; x++) | |
223 | { | |
224 | sr = sg = sb = smask = 0; | |
225 | if (p1[0] != mr || p1[1] != mg || p1[2] != mb) | |
226 | sr += p1[0], sg += p1[1], sb += p1[2]; | |
227 | else smask++; | |
228 | p1 += 3; | |
229 | if (p1[0] != mr || p1[1] != mg || p1[2] != mb) | |
230 | sr += p1[0], sg += p1[1], sb += p1[2]; | |
231 | else smask++; | |
232 | p1 += 3; | |
233 | if (p2[0] != mr || p2[1] != mg || p2[2] != mb) | |
234 | sr += p2[0], sg += p2[1], sb += p2[2]; | |
235 | else smask++; | |
236 | p2 += 3; | |
237 | if (p2[0] != mr || p2[1] != mg || p2[2] != mb) | |
238 | sr += p2[0], sg += p2[1], sb += p2[2]; | |
239 | else smask++; | |
240 | p2 += 3; | |
241 | ||
242 | if (smask > 2) | |
243 | ps[0] = ps[1] = ps[2] = mr; | |
244 | else | |
245 | ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2; | |
246 | ps += 3; | |
247 | } | |
248 | p1 += 32 * 3, p2 += 32 * 3; | |
249 | } | |
250 | ||
251 | return small.ConvertToBitmap(); | |
252 | } | |
253 | ||
254 | ||
255 | // finds empty borders and return non-empty area of image: | |
256 | static wxImage CutEmptyBorders(const wxImage& img) | |
257 | { | |
258 | unsigned char mr = img.GetMaskRed(), | |
259 | mg = img.GetMaskGreen(), | |
260 | mb = img.GetMaskBlue(); | |
261 | unsigned char *dt = img.GetData(), *dttmp; | |
262 | unsigned w = img.GetWidth(), h = img.GetHeight(); | |
263 | ||
264 | unsigned top, bottom, left, right, i; | |
265 | bool empt; | |
266 | ||
267 | #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3) | |
268 | #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;} | |
269 | ||
270 | for (empt = TRUE, top = 0; empt && top < h; top++) | |
271 | { | |
272 | MK_DTTMP(0, top); | |
273 | for (i = 0; i < w; i++, dttmp+=3) | |
274 | NOEMPTY_PIX(empt) | |
275 | } | |
276 | for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--) | |
277 | { | |
278 | MK_DTTMP(0, bottom); | |
279 | for (i = 0; i < w; i++, dttmp+=3) | |
280 | NOEMPTY_PIX(empt) | |
281 | } | |
282 | for (empt = TRUE, left = 0; empt && left < w; left++) | |
283 | { | |
284 | MK_DTTMP(left, 0); | |
285 | for (i = 0; i < h; i++, dttmp+=3*w) | |
286 | NOEMPTY_PIX(empt) | |
287 | } | |
288 | for (empt = TRUE, right = w-1; empt && right > left; right--) | |
289 | { | |
290 | MK_DTTMP(right, 0); | |
291 | for (i = 0; i < h; i++, dttmp+=3*w) | |
292 | NOEMPTY_PIX(empt) | |
293 | } | |
294 | top--, left--, bottom++, right++; | |
295 | ||
296 | return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1)); | |
297 | } | |
298 | ||
299 | ||
300 | ||
301 | int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime) | |
302 | { | |
303 | if (!extension.IsEmpty()) | |
304 | { | |
305 | wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable.Get(extension); | |
306 | if (entry) return (entry -> id); | |
307 | } | |
308 | ||
309 | wxFileType *ft = (mime.IsEmpty()) ? | |
310 | wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) : | |
311 | wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime); | |
312 | wxIcon ic; | |
313 | if (ft == NULL || (!ft -> GetIcon(&ic)) || (!ic.Ok())) | |
314 | { | |
315 | int newid = FI_UNKNOWN; | |
316 | m_HashTable.Put(extension, new wxFileIconEntry(newid)); | |
317 | return newid; | |
318 | } | |
319 | wxImage img(ic); | |
320 | delete ft; | |
321 | ||
322 | int id = m_ImageList.GetImageCount(); | |
323 | if (img.GetWidth() == 16 && img.GetHeight() == 16) | |
324 | m_ImageList.Add(img.ConvertToBitmap()); | |
325 | else | |
326 | { | |
327 | if (img.GetWidth() != 32 || img.GetHeight() != 32) | |
328 | m_ImageList.Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32))); | |
329 | else | |
330 | m_ImageList.Add(CreateAntialiasedBitmap(img)); | |
331 | } | |
332 | m_HashTable.Put(extension, new wxFileIconEntry(id)); | |
333 | return id; | |
334 | } | |
335 | ||
336 | ||
337 | ||
338 | // ---------------------------------------------------------------------------- | |
339 | // private functions | |
340 | // ---------------------------------------------------------------------------- | |
341 | ||
342 | static | |
343 | int ListCompare( long data1, long data2, long WXUNUSED(data) ) | |
344 | { | |
345 | wxFileData *fd1 = (wxFileData*)data1 ; | |
346 | wxFileData *fd2 = (wxFileData*)data2 ; | |
347 | if (fd1->GetName() == wxT("..")) return -1; | |
348 | if (fd2->GetName() == wxT("..")) return 1; | |
349 | if (fd1->IsDir() && !fd2->IsDir()) return -1; | |
350 | if (fd2->IsDir() && !fd1->IsDir()) return 1; | |
351 | return wxStrcmp( fd1->GetName(), fd2->GetName() ); | |
352 | } | |
353 | ||
354 | //----------------------------------------------------------------------------- | |
355 | // wxFileData | |
356 | //----------------------------------------------------------------------------- | |
357 | ||
358 | IMPLEMENT_DYNAMIC_CLASS(wxFileData,wxObject); | |
359 | ||
360 | wxFileData::wxFileData( const wxString &name, const wxString &fname ) | |
361 | { | |
362 | m_name = name; | |
363 | m_fileName = fname; | |
364 | ||
365 | struct stat buff; | |
366 | stat( m_fileName.fn_str(), &buff ); | |
367 | ||
368 | #if !defined( __EMX__ ) && !defined(__VMS) | |
369 | struct stat lbuff; | |
370 | lstat( m_fileName.fn_str(), &lbuff ); | |
371 | m_isLink = S_ISLNK( lbuff.st_mode ); | |
372 | struct tm *t = localtime( &lbuff.st_mtime ); | |
373 | #else | |
374 | m_isLink = FALSE; | |
375 | struct tm *t = localtime( &buff.st_mtime ); | |
376 | #endif | |
377 | ||
378 | // struct passwd *user = getpwuid( buff.st_uid ); | |
379 | // struct group *grp = getgrgid( buff.st_gid ); | |
380 | ||
381 | m_isDir = S_ISDIR( buff.st_mode ); | |
382 | m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR ); | |
383 | ||
384 | m_size = buff.st_size; | |
385 | ||
386 | m_hour = t->tm_hour; | |
387 | m_minute = t->tm_min; | |
388 | m_month = t->tm_mon+1; | |
389 | m_day = t->tm_mday; | |
390 | m_year = t->tm_year; | |
391 | m_year += 1900; | |
392 | ||
393 | m_permissions.sprintf( wxT("%c%c%c"), | |
394 | ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')), | |
395 | ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')), | |
396 | ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) ); | |
397 | } | |
398 | ||
399 | wxString wxFileData::GetName() const | |
400 | { | |
401 | return m_name; | |
402 | } | |
403 | ||
404 | wxString wxFileData::GetFullName() const | |
405 | { | |
406 | return m_fileName; | |
407 | } | |
408 | ||
409 | wxString wxFileData::GetHint() const | |
410 | { | |
411 | wxString s = m_fileName; | |
412 | s += " "; | |
413 | if (m_isDir) s += _("<DIR> "); | |
414 | else if (m_isLink) s += _("<LINK> "); | |
415 | else | |
416 | { | |
417 | s += LongToString( m_size ); | |
418 | s += _(" bytes "); | |
419 | } | |
420 | s += IntToString( m_day ); | |
421 | s += wxT("."); | |
422 | s += IntToString( m_month ); | |
423 | s += wxT("."); | |
424 | s += IntToString( m_year ); | |
425 | s += wxT(" "); | |
426 | s += IntToString( m_hour ); | |
427 | s += wxT(":"); | |
428 | s += IntToString( m_minute ); | |
429 | s += wxT(" "); | |
430 | s += m_permissions; | |
431 | return s; | |
432 | }; | |
433 | ||
434 | wxString wxFileData::GetEntry( int num ) | |
435 | { | |
436 | wxString s; | |
437 | switch (num) | |
438 | { | |
439 | case 0: | |
440 | { | |
441 | s = m_name; | |
442 | } | |
443 | break; | |
444 | case 1: | |
445 | { | |
446 | if (m_isDir) s = _("<DIR>"); | |
447 | else if (m_isLink) s = _("<LINK>"); | |
448 | else s = LongToString( m_size ); | |
449 | } | |
450 | break; | |
451 | case 2: | |
452 | { | |
453 | if (m_day < 10) s = wxT("0"); else s = wxT(""); | |
454 | s += IntToString( m_day ); | |
455 | s += wxT("."); | |
456 | if (m_month < 10) s += wxT("0"); | |
457 | s += IntToString( m_month ); | |
458 | s += wxT("."); | |
459 | s += IntToString( m_year ); | |
460 | } | |
461 | break; | |
462 | case 3: | |
463 | { | |
464 | if (m_hour < 10) s = wxT("0"); else s = wxT(""); | |
465 | s += IntToString( m_hour ); | |
466 | s += wxT(":"); | |
467 | if (m_minute < 10) s += wxT("0"); | |
468 | s += IntToString( m_minute ); | |
469 | break; | |
470 | } | |
471 | case 4: | |
472 | s = m_permissions; | |
473 | break; | |
474 | default: | |
475 | s = wxT("No entry"); | |
476 | break; | |
477 | } | |
478 | return s; | |
479 | } | |
480 | ||
481 | bool wxFileData::IsDir() | |
482 | { | |
483 | return m_isDir; | |
484 | } | |
485 | ||
486 | bool wxFileData::IsExe() | |
487 | { | |
488 | return m_isExe; | |
489 | } | |
490 | ||
491 | bool wxFileData::IsLink() | |
492 | { | |
493 | return m_isLink; | |
494 | } | |
495 | ||
496 | long wxFileData::GetSize() | |
497 | { | |
498 | return m_size; | |
499 | } | |
500 | ||
501 | void wxFileData::SetNewName( const wxString &name, const wxString &fname ) | |
502 | { | |
503 | m_name = name; | |
504 | m_fileName = fname; | |
505 | } | |
506 | ||
507 | void wxFileData::MakeItem( wxListItem &item ) | |
508 | { | |
509 | item.m_text = m_name; | |
510 | item.ClearAttributes(); | |
511 | if (IsExe()) item.SetTextColour(*wxRED); | |
512 | if (IsDir()) item.SetTextColour(*wxBLUE); | |
513 | ||
514 | if (IsDir()) | |
515 | item.m_image = FI_FOLDER; | |
516 | else if (IsExe()) | |
517 | item.m_image = FI_EXECUTABLE; | |
518 | else if (m_name.Find(wxT('.')) != wxNOT_FOUND) | |
519 | item.m_image = g_IconsTable -> GetIconID(m_name.AfterLast(wxT('.'))); | |
520 | else | |
521 | item.m_image = FI_UNKNOWN; | |
522 | ||
523 | if (IsLink()) | |
524 | { | |
525 | wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" ); | |
526 | item.SetTextColour(*dg); | |
527 | } | |
528 | item.m_data = (long)this; | |
529 | } | |
530 | ||
531 | //----------------------------------------------------------------------------- | |
532 | // wxFileCtrl | |
533 | //----------------------------------------------------------------------------- | |
534 | ||
535 | IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl); | |
536 | ||
537 | BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl) | |
538 | EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem) | |
539 | EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems) | |
540 | EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit) | |
541 | END_EVENT_TABLE() | |
542 | ||
543 | ||
544 | wxFileCtrl::wxFileCtrl() | |
545 | { | |
546 | m_dirName = wxT("/"); | |
547 | m_showHidden = FALSE; | |
548 | } | |
549 | ||
550 | wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id, | |
551 | const wxString &dirName, const wxString &wild, | |
552 | const wxPoint &pos, const wxSize &size, | |
553 | long style, const wxValidator &validator, const wxString &name ) : | |
554 | wxListCtrl( win, id, pos, size, style, validator, name ) | |
555 | { | |
556 | if (! g_IconsTable) g_IconsTable = new wxFileIconsTable; | |
557 | wxImageList *imageList = g_IconsTable -> GetImageList(); | |
558 | ||
559 | SetImageList( imageList, wxIMAGE_LIST_SMALL ); | |
560 | ||
561 | m_dirName = dirName; | |
562 | m_wild = wild; | |
563 | m_showHidden = FALSE; | |
564 | Update(); | |
565 | } | |
566 | ||
567 | void wxFileCtrl::ChangeToListMode() | |
568 | { | |
569 | SetSingleStyle( wxLC_LIST ); | |
570 | Update(); | |
571 | } | |
572 | ||
573 | void wxFileCtrl::ChangeToReportMode() | |
574 | { | |
575 | SetSingleStyle( wxLC_REPORT ); | |
576 | Update(); | |
577 | } | |
578 | ||
579 | void wxFileCtrl::ChangeToIconMode() | |
580 | { | |
581 | SetSingleStyle( wxLC_ICON ); | |
582 | Update(); | |
583 | } | |
584 | ||
585 | void wxFileCtrl::ShowHidden( bool show ) | |
586 | { | |
587 | m_showHidden = show; | |
588 | Update(); | |
589 | } | |
590 | ||
591 | long wxFileCtrl::Add( wxFileData *fd, wxListItem &item ) | |
592 | { | |
593 | long ret = -1; | |
594 | item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE; | |
595 | fd->MakeItem( item ); | |
596 | long my_style = GetWindowStyleFlag(); | |
597 | if (my_style & wxLC_REPORT) | |
598 | { | |
599 | ret = InsertItem( item ); | |
600 | for (int i = 1; i < 5; i++) SetItem( item.m_itemId, i, fd->GetEntry( i) ); | |
601 | } | |
602 | else if (my_style & wxLC_LIST) | |
603 | { | |
604 | ret = InsertItem( item ); | |
605 | } | |
606 | return ret; | |
607 | } | |
608 | ||
609 | void wxFileCtrl::Update() | |
610 | { | |
611 | long my_style = GetWindowStyleFlag(); | |
612 | int name_col_width = 0; | |
613 | if (my_style & wxLC_REPORT) | |
614 | { | |
615 | if (GetColumnCount() > 0) | |
616 | name_col_width = GetColumnWidth( 0 ); | |
617 | } | |
618 | ||
619 | ClearAll(); | |
620 | if (my_style & wxLC_REPORT) | |
621 | { | |
622 | if (name_col_width < 140) name_col_width = 140; | |
623 | InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, name_col_width ); | |
624 | InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 ); | |
625 | InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 65 ); | |
626 | InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 ); | |
627 | InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 ); | |
628 | } | |
629 | wxFileData *fd = (wxFileData *) NULL; | |
630 | wxListItem item; | |
631 | item.m_itemId = 0; | |
632 | item.m_col = 0; | |
633 | ||
634 | if (m_dirName != wxT("/")) | |
635 | { | |
636 | wxString p( wxPathOnly(m_dirName) ); | |
637 | if (p.IsEmpty()) p = wxT("/"); | |
638 | fd = new wxFileData( wxT(".."), p ); | |
639 | Add( fd, item ); | |
640 | item.m_itemId++; | |
641 | } | |
642 | ||
643 | wxString res = m_dirName + wxT("/*"); | |
644 | wxString f( wxFindFirstFile( res.GetData(), wxDIR ) ); | |
645 | while (!f.IsEmpty()) | |
646 | { | |
647 | res = wxFileNameFromPath( f ); | |
648 | fd = new wxFileData( res, f ); | |
649 | wxString s = fd->GetName(); | |
650 | if (m_showHidden || (s[0u] != wxT('.'))) | |
651 | { | |
652 | Add( fd, item ); | |
653 | item.m_itemId++; | |
654 | } | |
655 | f = wxFindNextFile(); | |
656 | } | |
657 | ||
658 | // Tokenize the wildcard string, so we can handle more than 1 | |
659 | // search pattern in a wildcard. | |
660 | wxStringTokenizer tokenWild( m_wild, ";" ); | |
661 | while ( tokenWild.HasMoreTokens() ) | |
662 | { | |
663 | res = m_dirName + wxT("/") + tokenWild.GetNextToken(); | |
664 | f = wxFindFirstFile( res.GetData(), wxFILE ); | |
665 | while (!f.IsEmpty()) | |
666 | { | |
667 | res = wxFileNameFromPath( f ); | |
668 | fd = new wxFileData( res, f ); | |
669 | wxString s = fd->GetName(); | |
670 | if (m_showHidden || (s[0u] != wxT('.'))) | |
671 | { | |
672 | Add( fd, item ); | |
673 | item.m_itemId++; | |
674 | } | |
675 | f = wxFindNextFile(); | |
676 | } | |
677 | } | |
678 | ||
679 | SortItems( ListCompare, 0 ); | |
680 | ||
681 | if (my_style & wxLC_REPORT) | |
682 | { | |
683 | SetColumnWidth( 1, wxLIST_AUTOSIZE ); | |
684 | SetColumnWidth( 2, wxLIST_AUTOSIZE ); | |
685 | SetColumnWidth( 3, wxLIST_AUTOSIZE ); | |
686 | } | |
687 | } | |
688 | ||
689 | void wxFileCtrl::SetWild( const wxString &wild ) | |
690 | { | |
691 | m_wild = wild; | |
692 | Update(); | |
693 | } | |
694 | ||
695 | void wxFileCtrl::MakeDir() | |
696 | { | |
697 | wxString new_name( wxT("NewName") ); | |
698 | wxString path( m_dirName ); | |
699 | path += wxT("/"); | |
700 | path += new_name; | |
701 | if (wxFileExists(path)) | |
702 | { | |
703 | // try NewName0, NewName1 etc. | |
704 | int i = 0; | |
705 | do { | |
706 | new_name = _("NewName"); | |
707 | wxString num; | |
708 | num.Printf( wxT("%d"), i ); | |
709 | new_name += num; | |
710 | ||
711 | path = m_dirName; | |
712 | path += wxT("/"); | |
713 | path += new_name; | |
714 | i++; | |
715 | } while (wxFileExists(path)); | |
716 | } | |
717 | ||
718 | wxLogNull log; | |
719 | if (!wxMkdir(path)) | |
720 | { | |
721 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
722 | dialog.ShowModal(); | |
723 | return; | |
724 | } | |
725 | ||
726 | wxFileData *fd = new wxFileData( new_name, path ); | |
727 | wxListItem item; | |
728 | item.m_itemId = 0; | |
729 | item.m_col = 0; | |
730 | long id = Add( fd, item ); | |
731 | ||
732 | if (id != -1) | |
733 | { | |
734 | SortItems( ListCompare, 0 ); | |
735 | id = FindItem( 0, (long)fd ); | |
736 | EnsureVisible( id ); | |
737 | EditLabel( id ); | |
738 | } | |
739 | } | |
740 | ||
741 | void wxFileCtrl::GoToParentDir() | |
742 | { | |
743 | if (m_dirName != wxT("/")) | |
744 | { | |
745 | wxString fname( wxFileNameFromPath(m_dirName) ); | |
746 | m_dirName = wxPathOnly( m_dirName ); | |
747 | if (m_dirName.IsEmpty()) m_dirName = wxT("/"); | |
748 | Update(); | |
749 | long id = FindItem( 0, fname ); | |
750 | if (id != -1) | |
751 | { | |
752 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
753 | EnsureVisible( id ); | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | void wxFileCtrl::GoToHomeDir() | |
759 | { | |
760 | wxString s = wxGetUserHome( wxString() ); | |
761 | m_dirName = s; | |
762 | Update(); | |
763 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
764 | EnsureVisible( 0 ); | |
765 | } | |
766 | ||
767 | void wxFileCtrl::GoToDir( const wxString &dir ) | |
768 | { | |
769 | m_dirName = dir; | |
770 | Update(); | |
771 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
772 | EnsureVisible( 0 ); | |
773 | } | |
774 | ||
775 | void wxFileCtrl::GetDir( wxString &dir ) | |
776 | { | |
777 | dir = m_dirName; | |
778 | } | |
779 | ||
780 | void wxFileCtrl::OnListDeleteItem( wxListEvent &event ) | |
781 | { | |
782 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
783 | delete fd; | |
784 | } | |
785 | ||
786 | void wxFileCtrl::OnListDeleteAllItems( wxListEvent &WXUNUSED(event) ) | |
787 | { | |
788 | wxListItem item; | |
789 | item.m_mask = wxLIST_MASK_DATA; | |
790 | ||
791 | item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL ); | |
792 | while ( item.m_itemId != -1 ) | |
793 | { | |
794 | GetItem( item ); | |
795 | wxFileData *fd = (wxFileData*)item.m_data; | |
796 | delete fd; | |
797 | item.m_data = 0; | |
798 | SetItem( item ); | |
799 | item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL ); | |
800 | } | |
801 | } | |
802 | ||
803 | void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event ) | |
804 | { | |
805 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
806 | wxASSERT( fd ); | |
807 | ||
808 | if ((event.GetLabel().IsEmpty()) || | |
809 | (event.GetLabel() == _(".")) || | |
810 | (event.GetLabel() == _("..")) || | |
811 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) | |
812 | { | |
813 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
814 | dialog.ShowModal(); | |
815 | event.Veto(); | |
816 | return; | |
817 | } | |
818 | ||
819 | wxString new_name( wxPathOnly( fd->GetFullName() ) ); | |
820 | new_name += wxT("/"); | |
821 | new_name += event.GetLabel(); | |
822 | ||
823 | wxLogNull log; | |
824 | ||
825 | if (wxFileExists(new_name)) | |
826 | { | |
827 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
828 | dialog.ShowModal(); | |
829 | event.Veto(); | |
830 | } | |
831 | ||
832 | if (wxRenameFile(fd->GetFullName(),new_name)) | |
833 | { | |
834 | fd->SetNewName( new_name, event.GetLabel() ); | |
835 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
836 | EnsureVisible( event.GetItem() ); | |
837 | } | |
838 | else | |
839 | { | |
840 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
841 | dialog.ShowModal(); | |
842 | event.Veto(); | |
843 | } | |
844 | } | |
845 | ||
846 | //----------------------------------------------------------------------------- | |
847 | // wxFileDialog | |
848 | //----------------------------------------------------------------------------- | |
849 | ||
850 | #define ID_LIST_MODE wxID_FILEDLGG | |
851 | #define ID_REPORT_MODE wxID_FILEDLGG + 1 | |
852 | #define ID_UP_DIR wxID_FILEDLGG + 5 | |
853 | #define ID_PARENT_DIR wxID_FILEDLGG + 6 | |
854 | #define ID_NEW_DIR wxID_FILEDLGG + 7 | |
855 | #define ID_CHOICE wxID_FILEDLGG + 8 | |
856 | #define ID_TEXT wxID_FILEDLGG + 9 | |
857 | #define ID_LIST_CTRL wxID_FILEDLGG + 10 | |
858 | #define ID_ACTIVATED wxID_FILEDLGG + 11 | |
859 | #define ID_CHECK wxID_FILEDLGG + 12 | |
860 | ||
861 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog) | |
862 | ||
863 | BEGIN_EVENT_TABLE(wxFileDialog,wxDialog) | |
864 | EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList) | |
865 | EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport) | |
866 | EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp) | |
867 | EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome) | |
868 | EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew) | |
869 | EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk) | |
870 | EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected) | |
871 | EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated) | |
872 | EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice) | |
873 | EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter) | |
874 | EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck) | |
875 | END_EVENT_TABLE() | |
876 | ||
877 | long wxFileDialog::s_lastViewStyle = wxLC_LIST; | |
878 | bool wxFileDialog::s_lastShowHidden = FALSE; | |
879 | ||
880 | wxFileDialog::wxFileDialog(wxWindow *parent, | |
881 | const wxString& message, | |
882 | const wxString& defaultDir, | |
883 | const wxString& defaultFile, | |
884 | const wxString& wildCard, | |
885 | long style, | |
886 | const wxPoint& pos ) : | |
887 | wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) | |
888 | { | |
889 | wxBeginBusyCursor(); | |
890 | ||
891 | if (wxConfig::Get(FALSE)) | |
892 | { | |
893 | wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle); | |
894 | wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden); | |
895 | } | |
896 | ||
897 | m_message = message; | |
898 | m_dialogStyle = style; | |
899 | ||
900 | if (m_dialogStyle == 0) m_dialogStyle = wxOPEN; | |
901 | if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN)) | |
902 | m_dialogStyle |= wxOPEN; | |
903 | ||
904 | m_dir = defaultDir; | |
905 | if ((m_dir.IsEmpty()) || (m_dir == wxT("."))) | |
906 | { | |
907 | char buf[200]; | |
908 | m_dir = getcwd( buf, sizeof(buf) ); | |
909 | } | |
910 | m_path = defaultDir; | |
911 | m_path += wxT("/"); | |
912 | m_path += defaultFile; | |
913 | m_fileName = defaultFile; | |
914 | m_wildCard = wildCard; | |
915 | m_filterIndex = 0; | |
916 | m_filterExtension = wxEmptyString; | |
917 | ||
918 | // interpret wildcards | |
919 | ||
920 | if (m_wildCard.IsEmpty()) | |
921 | m_wildCard = _("All files (*)|*"); | |
922 | ||
923 | wxStringTokenizer tokens( m_wildCard, wxT("|") ); | |
924 | wxString firstWild; | |
925 | wxString firstWildText; | |
926 | if (tokens.CountTokens() == 1) | |
927 | { | |
928 | firstWildText = tokens.GetNextToken(); | |
929 | firstWild = firstWildText; | |
930 | } | |
931 | else | |
932 | { | |
933 | wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") ); | |
934 | firstWildText = tokens.GetNextToken(); | |
935 | firstWild = tokens.GetNextToken(); | |
936 | } | |
937 | if ( firstWild.Left( 2 ) == wxT("*.") ) | |
938 | m_filterExtension = firstWild.Mid( 1 ); | |
939 | if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString; | |
940 | ||
941 | // layout | |
942 | ||
943 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
944 | ||
945 | wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL ); | |
946 | ||
947 | wxBitmapButton *but; | |
948 | ||
949 | but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) ); | |
950 | #if wxUSE_TOOLTIPS | |
951 | but->SetToolTip( _("View files as a list view") ); | |
952 | #endif | |
953 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
954 | ||
955 | but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) ); | |
956 | #if wxUSE_TOOLTIPS | |
957 | but->SetToolTip( _("View files as a detailed view") ); | |
958 | #endif | |
959 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
960 | ||
961 | buttonsizer->Add( 30, 5, 1 ); | |
962 | ||
963 | but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) ); | |
964 | #if wxUSE_TOOLTIPS | |
965 | but->SetToolTip( _("Go to parent directory") ); | |
966 | #endif | |
967 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
968 | ||
969 | but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) ); | |
970 | #if wxUSE_TOOLTIPS | |
971 | but->SetToolTip( _("Go to home directory") ); | |
972 | #endif | |
973 | buttonsizer->Add( but, 0, wxALL, 5); | |
974 | ||
975 | buttonsizer->Add( 20, 20 ); | |
976 | ||
977 | but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) ); | |
978 | #if wxUSE_TOOLTIPS | |
979 | but->SetToolTip( _("Create new directory") ); | |
980 | #endif | |
981 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
982 | ||
983 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 ); | |
984 | ||
985 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); | |
986 | staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 ); | |
987 | m_static = new wxStaticText( this, -1, m_dir ); | |
988 | staticsizer->Add( m_static, 1 ); | |
989 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 ); | |
990 | ||
991 | if (m_dialogStyle & wxMULTIPLE) | |
992 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, | |
993 | wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER ); | |
994 | else | |
995 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, | |
996 | wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL ); | |
997 | m_list -> ShowHidden(s_lastShowHidden); | |
998 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 ); | |
999 | ||
1000 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
1001 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER ); | |
1002 | textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
1003 | textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
1004 | mainsizer->Add( textsizer, 0, wxEXPAND ); | |
1005 | ||
1006 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
1007 | m_choice = new wxChoice( this, ID_CHOICE ); | |
1008 | choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 ); | |
1009 | m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") ); | |
1010 | m_check->SetValue( s_lastShowHidden ); | |
1011 | choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 ); | |
1012 | choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 ); | |
1013 | mainsizer->Add( choicesizer, 0, wxEXPAND ); | |
1014 | ||
1015 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
1016 | while (tokens.HasMoreTokens()) | |
1017 | { | |
1018 | firstWildText = tokens.GetNextToken(); | |
1019 | firstWild = tokens.GetNextToken(); | |
1020 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
1021 | } | |
1022 | m_choice->SetSelection( 0 ); | |
1023 | ||
1024 | SetAutoLayout( TRUE ); | |
1025 | SetSizer( mainsizer ); | |
1026 | ||
1027 | mainsizer->Fit( this ); | |
1028 | mainsizer->SetSizeHints( this ); | |
1029 | ||
1030 | Centre( wxBOTH ); | |
1031 | ||
1032 | /* | |
1033 | if (m_fileName.IsEmpty()) | |
1034 | m_list->SetFocus(); | |
1035 | else | |
1036 | */ | |
1037 | m_text->SetFocus(); | |
1038 | ||
1039 | wxEndBusyCursor(); | |
1040 | } | |
1041 | ||
1042 | wxFileDialog::~wxFileDialog() | |
1043 | { | |
1044 | if (wxConfig::Get(FALSE)) | |
1045 | { | |
1046 | wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle); | |
1047 | wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | void wxFileDialog::OnChoice( wxCommandEvent &event ) | |
1052 | { | |
1053 | int index = (int)event.GetInt(); | |
1054 | wxString *str = (wxString*) m_choice->GetClientData( index ); | |
1055 | m_list->SetWild( *str ); | |
1056 | m_filterIndex = index; | |
1057 | if ( str -> Left( 2 ) == wxT("*.") ) | |
1058 | { | |
1059 | m_filterExtension = str -> Mid( 1 ); | |
1060 | if (m_filterExtension == ".*") m_filterExtension = wxEmptyString; | |
1061 | } | |
1062 | else | |
1063 | m_filterExtension = wxEmptyString; | |
1064 | } | |
1065 | ||
1066 | void wxFileDialog::OnCheck( wxCommandEvent &event ) | |
1067 | { | |
1068 | m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) ); | |
1069 | } | |
1070 | ||
1071 | void wxFileDialog::OnActivated( wxListEvent &event ) | |
1072 | { | |
1073 | HandleAction( event.m_item.m_text ); | |
1074 | } | |
1075 | ||
1076 | void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) | |
1077 | { | |
1078 | wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
1079 | cevent.SetEventObject( this ); | |
1080 | GetEventHandler()->ProcessEvent( cevent ); | |
1081 | } | |
1082 | ||
1083 | void wxFileDialog::OnSelected( wxListEvent &event ) | |
1084 | { | |
1085 | if (FindFocus() != m_list) return; | |
1086 | ||
1087 | wxString filename( event.m_item.m_text ); | |
1088 | if (filename == wxT("..")) return; | |
1089 | ||
1090 | wxString dir; | |
1091 | m_list->GetDir( dir ); | |
1092 | if (dir != wxT("/")) dir += wxT("/"); | |
1093 | dir += filename; | |
1094 | if (wxDirExists(dir)) return; | |
1095 | ||
1096 | m_text->SetValue( filename ); | |
1097 | } | |
1098 | ||
1099 | void wxFileDialog::HandleAction( const wxString &fn ) | |
1100 | { | |
1101 | wxString filename( fn ); | |
1102 | wxString dir; | |
1103 | m_list->GetDir( dir ); | |
1104 | if (filename.IsEmpty()) return; | |
1105 | if (filename == wxT(".")) return; | |
1106 | ||
1107 | if (filename == wxT("..")) | |
1108 | { | |
1109 | m_list->GoToParentDir(); | |
1110 | m_list->SetFocus(); | |
1111 | m_list->GetDir( dir ); | |
1112 | m_static->SetLabel( dir ); | |
1113 | return; | |
1114 | } | |
1115 | ||
1116 | if (filename == wxT("~")) | |
1117 | { | |
1118 | m_list->GoToHomeDir(); | |
1119 | m_list->SetFocus(); | |
1120 | m_list->GetDir( dir ); | |
1121 | m_static->SetLabel( dir ); | |
1122 | return; | |
1123 | } | |
1124 | ||
1125 | if (filename[0u] == wxT('~')) | |
1126 | { | |
1127 | filename.Remove( 0, 1 ); | |
1128 | wxString tmp( wxGetUserHome() ); | |
1129 | tmp += wxT('/'); | |
1130 | tmp += filename; | |
1131 | filename = tmp; | |
1132 | } | |
1133 | ||
1134 | if ((filename.Find(wxT('*')) != wxNOT_FOUND) || | |
1135 | (filename.Find(wxT('?')) != wxNOT_FOUND)) | |
1136 | { | |
1137 | if (filename.Find(wxT('/')) != wxNOT_FOUND) | |
1138 | { | |
1139 | wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR ); | |
1140 | return; | |
1141 | } | |
1142 | m_list->SetWild( filename ); | |
1143 | return; | |
1144 | } | |
1145 | ||
1146 | if (dir != wxT("/")) dir += wxT("/"); | |
1147 | if (filename[0u] != wxT('/')) | |
1148 | { | |
1149 | dir += filename; | |
1150 | filename = dir; | |
1151 | } | |
1152 | ||
1153 | if (wxDirExists(filename)) | |
1154 | { | |
1155 | m_list->GoToDir( filename ); | |
1156 | m_list->GetDir( dir ); | |
1157 | m_static->SetLabel( dir ); | |
1158 | return; | |
1159 | } | |
1160 | ||
1161 | ||
1162 | if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) ) | |
1163 | { | |
1164 | if (filename.Find( wxT('.') ) == wxNOT_FOUND || | |
1165 | filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND) | |
1166 | filename << m_filterExtension; | |
1167 | if (wxFileExists( filename )) | |
1168 | { | |
1169 | wxString msg; | |
1170 | msg.Printf( _("File '%s' already exists, do you really want to " | |
1171 | "overwrite it?"), filename.c_str() ); | |
1172 | ||
1173 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) | |
1174 | return; | |
1175 | } | |
1176 | } | |
1177 | else if ( m_dialogStyle & wxOPEN ) | |
1178 | { | |
1179 | if ( !wxFileExists( filename ) ) | |
1180 | if (filename.Find( wxT('.') ) == wxNOT_FOUND || | |
1181 | filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND) | |
1182 | filename << m_filterExtension; | |
1183 | ||
1184 | if ( m_dialogStyle & wxFILE_MUST_EXIST ) | |
1185 | { | |
1186 | if ( !wxFileExists( filename ) ) | |
1187 | { | |
1188 | wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR ); | |
1189 | return; | |
1190 | } | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | SetPath( filename ); | |
1195 | ||
1196 | // change to the directory where the user went if asked | |
1197 | if ( m_dialogStyle & wxCHANGE_DIR ) | |
1198 | { | |
1199 | wxString cwd; | |
1200 | wxSplitPath(filename, &cwd, NULL, NULL); | |
1201 | ||
1202 | if ( cwd != wxGetWorkingDirectory() ) | |
1203 | { | |
1204 | wxSetWorkingDirectory(cwd); | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | wxCommandEvent event; | |
1209 | wxDialog::OnOK(event); | |
1210 | } | |
1211 | ||
1212 | void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) ) | |
1213 | { | |
1214 | HandleAction( m_text->GetValue() ); | |
1215 | } | |
1216 | ||
1217 | void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) ) | |
1218 | { | |
1219 | m_list->ChangeToListMode(); | |
1220 | s_lastViewStyle = wxLC_LIST; | |
1221 | m_list->SetFocus(); | |
1222 | } | |
1223 | ||
1224 | void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) ) | |
1225 | { | |
1226 | m_list->ChangeToReportMode(); | |
1227 | s_lastViewStyle = wxLC_REPORT; | |
1228 | m_list->SetFocus(); | |
1229 | } | |
1230 | ||
1231 | void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) ) | |
1232 | { | |
1233 | m_list->GoToParentDir(); | |
1234 | m_list->SetFocus(); | |
1235 | wxString dir; | |
1236 | m_list->GetDir( dir ); | |
1237 | m_static->SetLabel( dir ); | |
1238 | } | |
1239 | ||
1240 | void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) ) | |
1241 | { | |
1242 | m_list->GoToHomeDir(); | |
1243 | m_list->SetFocus(); | |
1244 | wxString dir; | |
1245 | m_list->GetDir( dir ); | |
1246 | m_static->SetLabel( dir ); | |
1247 | } | |
1248 | ||
1249 | void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) ) | |
1250 | { | |
1251 | m_list->MakeDir(); | |
1252 | } | |
1253 | ||
1254 | void wxFileDialog::SetPath( const wxString& path ) | |
1255 | { | |
1256 | // not only set the full path but also update filename and dir | |
1257 | m_path = path; | |
1258 | if ( !!path ) | |
1259 | { | |
1260 | wxString ext; | |
1261 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
1262 | if (!ext.IsEmpty()) | |
1263 | { | |
1264 | m_fileName += wxT("."); | |
1265 | m_fileName += ext; | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | void wxFileDialog::GetPaths( wxArrayString& paths ) const | |
1271 | { | |
1272 | paths.Empty(); | |
1273 | if (m_list->GetSelectedItemCount() == 0) | |
1274 | { | |
1275 | paths.Add( GetPath() ); | |
1276 | return; | |
1277 | } | |
1278 | ||
1279 | paths.Alloc( m_list->GetSelectedItemCount() ); | |
1280 | ||
1281 | wxString dir; | |
1282 | m_list->GetDir( dir ); | |
1283 | if (dir != wxT("/")) dir += wxT("/"); | |
1284 | ||
1285 | wxListItem item; | |
1286 | item.m_mask = wxLIST_MASK_TEXT; | |
1287 | ||
1288 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1289 | while ( item.m_itemId != -1 ) | |
1290 | { | |
1291 | m_list->GetItem( item ); | |
1292 | paths.Add( dir + item.m_text ); | |
1293 | item.m_itemId = m_list->GetNextItem( item.m_itemId, | |
1294 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | void wxFileDialog::GetFilenames(wxArrayString& files) const | |
1299 | { | |
1300 | files.Empty(); | |
1301 | if (m_list->GetSelectedItemCount() == 0) | |
1302 | { | |
1303 | files.Add( GetFilename() ); | |
1304 | return; | |
1305 | } | |
1306 | files.Alloc( m_list->GetSelectedItemCount() ); | |
1307 | ||
1308 | wxListItem item; | |
1309 | item.m_mask = wxLIST_MASK_TEXT; | |
1310 | ||
1311 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1312 | while ( item.m_itemId != -1 ) | |
1313 | { | |
1314 | m_list->GetItem( item ); | |
1315 | files.Add( item.m_text ); | |
1316 | item.m_itemId = m_list->GetNextItem( item.m_itemId, | |
1317 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | ||
1322 | ||
1323 | // ---------------------------------------------------------------------------- | |
1324 | // global functions | |
1325 | // ---------------------------------------------------------------------------- | |
1326 | ||
1327 | wxString | |
1328 | wxFileSelectorEx(const wxChar *message, | |
1329 | const wxChar *default_path, | |
1330 | const wxChar *default_filename, | |
1331 | int *WXUNUSED(indexDefaultExtension), | |
1332 | const wxChar *wildcard, | |
1333 | int flags, | |
1334 | wxWindow *parent, | |
1335 | int x, int y) | |
1336 | { | |
1337 | // TODO: implement this somehow | |
1338 | return wxFileSelector(message, default_path, default_filename, wxT(""), | |
1339 | wildcard, flags, parent, x, y); | |
1340 | } | |
1341 | ||
1342 | wxString wxFileSelector( const wxChar *title, | |
1343 | const wxChar *defaultDir, const wxChar *defaultFileName, | |
1344 | const wxChar *defaultExtension, const wxChar *filter, int flags, | |
1345 | wxWindow *parent, int x, int y ) | |
1346 | { | |
1347 | wxString filter2; | |
1348 | if ( defaultExtension && !filter ) | |
1349 | filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ; | |
1350 | else if ( filter ) | |
1351 | filter2 = filter; | |
1352 | ||
1353 | wxString defaultDirString; | |
1354 | if (defaultDir) | |
1355 | defaultDirString = defaultDir; | |
1356 | ||
1357 | wxString defaultFilenameString; | |
1358 | if (defaultFileName) | |
1359 | defaultFilenameString = defaultFileName; | |
1360 | ||
1361 | wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) ); | |
1362 | ||
1363 | if ( fileDialog.ShowModal() == wxID_OK ) | |
1364 | { | |
1365 | return fileDialog.GetPath(); | |
1366 | } | |
1367 | else | |
1368 | { | |
1369 | return wxEmptyString; | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | wxString wxLoadFileSelector( const wxChar *what, const wxChar *ext, const wxChar *default_name, wxWindow *parent ) | |
1374 | { | |
1375 | wxString prompt = wxString::Format(_("Load %s file"), what); | |
1376 | ||
1377 | if (*ext == wxT('.')) | |
1378 | ext++; | |
1379 | ||
1380 | wxString wild = wxString::Format(_T("*.%s"), ext); | |
1381 | ||
1382 | return wxFileSelector(prompt, (const wxChar *) NULL, default_name, | |
1383 | ext, wild, 0, parent); | |
1384 | } | |
1385 | ||
1386 | wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name, | |
1387 | wxWindow *parent ) | |
1388 | { | |
1389 | wxChar *ext = (wxChar *)extension; | |
1390 | ||
1391 | wxString prompt = wxString::Format(_("Save %s file"), what); | |
1392 | ||
1393 | if (*ext == wxT('.')) | |
1394 | ext++; | |
1395 | ||
1396 | wxString wild = wxString::Format(_T("*.%s"), ext); | |
1397 | ||
1398 | return wxFileSelector(prompt, (const wxChar *) NULL, default_name, | |
1399 | ext, wild, 0, parent); | |
1400 | } | |
1401 | ||
1402 | ||
1403 | ||
1404 | ||
1405 | ||
1406 | ||
1407 | // A module to allow icons table cleanup | |
1408 | ||
1409 | class wxFileDialogGenericModule: public wxModule | |
1410 | { | |
1411 | DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule) | |
1412 | public: | |
1413 | wxFileDialogGenericModule() {} | |
1414 | bool OnInit() { return TRUE; } | |
1415 | void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} } | |
1416 | }; | |
1417 | ||
1418 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule) | |
1419 | ||
1420 | #endif // wxUSE_FILEDLG |