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