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