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