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