]>
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(); |
223d09f6 | 560 | if (m_showHidden || (s[0] != 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(); | |
223d09f6 | 575 | if (m_showHidden || (s[0] != 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 | |
e6527f9d | 585 | SetColumnWidth( 1, wxLIST_AUTOSIZE ); |
d3e90957 RR |
586 | SetColumnWidth( 2, wxLIST_AUTOSIZE ); |
587 | SetColumnWidth( 3, wxLIST_AUTOSIZE ); | |
8b17ba72 RR |
588 | } |
589 | ||
0b855868 | 590 | void wxFileCtrl::SetWild( const wxString &wild ) |
8b17ba72 | 591 | { |
0b855868 RR |
592 | m_wild = wild; |
593 | Update(); | |
594 | } | |
595 | ||
596 | void wxFileCtrl::MakeDir() | |
597 | { | |
223d09f6 | 598 | wxString new_name( wxT("NewName") ); |
0b855868 | 599 | wxString path( m_dirName ); |
223d09f6 | 600 | path += wxT("/"); |
0b855868 RR |
601 | path += new_name; |
602 | if (wxFileExists(path)) | |
8b17ba72 | 603 | { |
0b855868 RR |
604 | // try NewName0, NewName1 etc. |
605 | int i = 0; | |
c8c0e54c | 606 | do { |
0b855868 | 607 | new_name = _("NewName"); |
c8c0e54c | 608 | wxString num; |
223d09f6 | 609 | num.Printf( wxT("%d"), i ); |
c8c0e54c VZ |
610 | new_name += num; |
611 | ||
0b855868 | 612 | path = m_dirName; |
223d09f6 | 613 | path += wxT("/"); |
0b855868 | 614 | path += new_name; |
c8c0e54c VZ |
615 | i++; |
616 | } while (wxFileExists(path)); | |
8b17ba72 | 617 | } |
c8c0e54c | 618 | |
0b855868 | 619 | wxLogNull log; |
c8c0e54c | 620 | if (!wxMkdir(path)) |
8b17ba72 | 621 | { |
0b855868 | 622 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 623 | dialog.ShowModal(); |
0b855868 | 624 | return; |
8b17ba72 | 625 | } |
8b17ba72 | 626 | |
0b855868 RR |
627 | wxFileData *fd = new wxFileData( new_name, path ); |
628 | wxListItem item; | |
629 | item.m_itemId = 0; | |
630 | item.m_col = 0; | |
13111b2a | 631 | long id = Add( fd, item ); |
c8c0e54c | 632 | |
0b855868 RR |
633 | if (id != -1) |
634 | { | |
635 | SortItems( ListCompare, 0 ); | |
c8c0e54c | 636 | id = FindItem( 0, (long)fd ); |
0b855868 RR |
637 | EnsureVisible( id ); |
638 | EditLabel( id ); | |
639 | } | |
8b17ba72 RR |
640 | } |
641 | ||
642 | void wxFileCtrl::GoToParentDir() | |
643 | { | |
223d09f6 | 644 | if (m_dirName != wxT("/")) |
8b17ba72 | 645 | { |
c8c0e54c | 646 | wxString fname( wxFileNameFromPath(m_dirName) ); |
0b855868 | 647 | m_dirName = wxPathOnly( m_dirName ); |
223d09f6 | 648 | if (m_dirName.IsEmpty()) m_dirName = wxT("/"); |
8b17ba72 | 649 | Update(); |
13111b2a | 650 | long id = FindItem( 0, fname ); |
c8c0e54c VZ |
651 | if (id != -1) |
652 | { | |
653 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
654 | EnsureVisible( id ); | |
655 | } | |
8b17ba72 RR |
656 | } |
657 | } | |
658 | ||
659 | void wxFileCtrl::GoToHomeDir() | |
660 | { | |
661 | wxString s = wxGetUserHome( wxString() ); | |
662 | m_dirName = s; | |
663 | Update(); | |
e6daf794 RR |
664 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
665 | EnsureVisible( 0 ); | |
8b17ba72 RR |
666 | } |
667 | ||
668 | void wxFileCtrl::GoToDir( const wxString &dir ) | |
669 | { | |
670 | m_dirName = dir; | |
671 | Update(); | |
e6daf794 RR |
672 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
673 | EnsureVisible( 0 ); | |
8b17ba72 RR |
674 | } |
675 | ||
676 | void wxFileCtrl::GetDir( wxString &dir ) | |
677 | { | |
678 | dir = m_dirName; | |
679 | } | |
680 | ||
8b17ba72 RR |
681 | void wxFileCtrl::OnListDeleteItem( wxListEvent &event ) |
682 | { | |
683 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
684 | delete fd; | |
685 | } | |
686 | ||
12c1b46a RR |
687 | void wxFileCtrl::OnListDeleteAllItems( wxListEvent &WXUNUSED(event) ) |
688 | { | |
689 | wxListItem item; | |
690 | item.m_mask = wxLIST_MASK_DATA; | |
691 | ||
692 | item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL ); | |
92da8bde | 693 | while ( item.m_itemId != -1 ) |
12c1b46a RR |
694 | { |
695 | GetItem( item ); | |
696 | wxFileData *fd = (wxFileData*)item.m_data; | |
697 | delete fd; | |
92da8bde | 698 | item.m_data = 0; |
12c1b46a RR |
699 | SetItem( item ); |
700 | item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL ); | |
701 | } | |
702 | } | |
703 | ||
0b855868 | 704 | void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event ) |
8b17ba72 RR |
705 | { |
706 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
0b855868 | 707 | wxASSERT( fd ); |
c8c0e54c | 708 | |
0b855868 RR |
709 | if ((event.GetLabel().IsEmpty()) || |
710 | (event.GetLabel() == _(".")) || | |
711 | (event.GetLabel() == _("..")) || | |
223d09f6 | 712 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) |
8b17ba72 | 713 | { |
0b855868 | 714 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 715 | dialog.ShowModal(); |
0b855868 | 716 | event.Veto(); |
c8c0e54c | 717 | return; |
8b17ba72 | 718 | } |
c8c0e54c | 719 | |
0b855868 | 720 | wxString new_name( wxPathOnly( fd->GetFullName() ) ); |
223d09f6 | 721 | new_name += wxT("/"); |
0b855868 | 722 | new_name += event.GetLabel(); |
c8c0e54c | 723 | |
0b855868 | 724 | wxLogNull log; |
c8c0e54c | 725 | |
0b855868 | 726 | if (wxFileExists(new_name)) |
8b17ba72 | 727 | { |
0b855868 | 728 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 729 | dialog.ShowModal(); |
0b855868 | 730 | event.Veto(); |
8b17ba72 | 731 | } |
c8c0e54c | 732 | |
0b855868 | 733 | if (wxRenameFile(fd->GetFullName(),new_name)) |
8b17ba72 | 734 | { |
0b855868 | 735 | fd->SetNewName( new_name, event.GetLabel() ); |
c8c0e54c | 736 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
0b855868 | 737 | EnsureVisible( event.GetItem() ); |
8b17ba72 RR |
738 | } |
739 | else | |
740 | { | |
0b855868 | 741 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 742 | dialog.ShowModal(); |
0b855868 | 743 | event.Veto(); |
8b17ba72 | 744 | } |
8b17ba72 RR |
745 | } |
746 | ||
747 | //----------------------------------------------------------------------------- | |
748 | // wxFileDialog | |
749 | //----------------------------------------------------------------------------- | |
750 | ||
a16d3c04 KB |
751 | #define ID_LIST_MODE wxID_FILEDLGG |
752 | #define ID_REPORT_MODE wxID_FILEDLGG + 1 | |
753 | #define ID_UP_DIR wxID_FILEDLGG + 5 | |
754 | #define ID_PARENT_DIR wxID_FILEDLGG + 6 | |
755 | #define ID_NEW_DIR wxID_FILEDLGG + 7 | |
756 | #define ID_CHOICE wxID_FILEDLGG + 8 | |
757 | #define ID_TEXT wxID_FILEDLGG + 9 | |
758 | #define ID_LIST_CTRL wxID_FILEDLGG + 10 | |
759 | #define ID_ACTIVATED wxID_FILEDLGG + 11 | |
bf9e3e73 | 760 | #define ID_CHECK wxID_FILEDLGG + 12 |
8b17ba72 RR |
761 | |
762 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog) | |
763 | ||
764 | BEGIN_EVENT_TABLE(wxFileDialog,wxDialog) | |
765 | EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList) | |
766 | EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport) | |
8b17ba72 RR |
767 | EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp) |
768 | EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome) | |
0b855868 | 769 | EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew) |
8b17ba72 RR |
770 | EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk) |
771 | EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected) | |
c8c0e54c VZ |
772 | EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated) |
773 | EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice) | |
774 | EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter) | |
bf9e3e73 | 775 | EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck) |
8b17ba72 RR |
776 | END_EVENT_TABLE() |
777 | ||
eaf40b23 VS |
778 | long wxFileDialog::s_lastViewStyle = wxLC_LIST; |
779 | bool wxFileDialog::s_lastShowHidden = FALSE; | |
655cf310 | 780 | |
8b17ba72 RR |
781 | wxFileDialog::wxFileDialog(wxWindow *parent, |
782 | const wxString& message, | |
783 | const wxString& defaultDir, | |
784 | const wxString& defaultFile, | |
785 | const wxString& wildCard, | |
786 | long style, | |
787 | const wxPoint& pos ) : | |
9c884972 | 788 | wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) |
8b17ba72 RR |
789 | { |
790 | wxBeginBusyCursor(); | |
c8c0e54c | 791 | |
eaf40b23 VS |
792 | if (wxConfig::Get(FALSE)) |
793 | { | |
794 | wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle); | |
795 | wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden); | |
796 | } | |
797 | ||
8b17ba72 RR |
798 | m_message = message; |
799 | m_dialogStyle = style; | |
479cd5de | 800 | |
655cf310 | 801 | if (m_dialogStyle == 0) m_dialogStyle = wxOPEN; |
7941ba11 | 802 | if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN)) |
9b00bb16 | 803 | m_dialogStyle |= wxOPEN; |
479cd5de | 804 | |
8b17ba72 | 805 | m_dir = defaultDir; |
f59d80ca | 806 | if ((m_dir.IsEmpty()) || (m_dir == wxT("."))) |
ac2def68 RR |
807 | { |
808 | char buf[200]; | |
c8c0e54c | 809 | m_dir = getcwd( buf, sizeof(buf) ); |
ac2def68 | 810 | } |
8b17ba72 | 811 | m_path = defaultDir; |
223d09f6 | 812 | m_path += wxT("/"); |
8b17ba72 RR |
813 | m_path += defaultFile; |
814 | m_fileName = defaultFile; | |
815 | m_wildCard = wildCard; | |
816 | m_filterIndex = 0; | |
655cf310 | 817 | m_filterExtension = wxEmptyString; |
c8c0e54c | 818 | |
cae5359f | 819 | // interpret wildcards |
c8c0e54c | 820 | |
cae5359f RR |
821 | if (m_wildCard.IsEmpty()) |
822 | m_wildCard = _("All files (*)|*"); | |
c8c0e54c | 823 | |
223d09f6 | 824 | wxStringTokenizer tokens( m_wildCard, wxT("|") ); |
cae5359f RR |
825 | wxString firstWild; |
826 | wxString firstWildText; | |
827 | if (tokens.CountTokens() == 1) | |
828 | { | |
829 | firstWildText = tokens.GetNextToken(); | |
830 | firstWild = firstWildText; | |
831 | } | |
832 | else | |
833 | { | |
223d09f6 | 834 | wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") ); |
cae5359f RR |
835 | firstWildText = tokens.GetNextToken(); |
836 | firstWild = tokens.GetNextToken(); | |
837 | } | |
655cf310 VS |
838 | if ( firstWild.Left( 2 ) == wxT("*.") ) |
839 | m_filterExtension = firstWild.Mid( 1 ); | |
840 | if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString; | |
cae5359f RR |
841 | |
842 | // layout | |
c8c0e54c | 843 | |
8b17ba72 | 844 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); |
c8c0e54c | 845 | |
8b17ba72 | 846 | wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
c8c0e54c | 847 | |
e6daf794 | 848 | wxBitmapButton *but; |
c8c0e54c | 849 | |
e6daf794 RR |
850 | but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) ); |
851 | #if wxUSE_TOOLTIPS | |
852 | but->SetToolTip( _("View files as a list view") ); | |
853 | #endif | |
854 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 855 | |
e6daf794 RR |
856 | but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) ); |
857 | #if wxUSE_TOOLTIPS | |
858 | but->SetToolTip( _("View files as a detailed view") ); | |
859 | #endif | |
860 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 861 | |
0b855868 | 862 | buttonsizer->Add( 30, 5, 1 ); |
e6daf794 RR |
863 | |
864 | but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) ); | |
865 | #if wxUSE_TOOLTIPS | |
866 | but->SetToolTip( _("Go to parent directory") ); | |
867 | #endif | |
868 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 869 | |
e6daf794 RR |
870 | but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) ); |
871 | #if wxUSE_TOOLTIPS | |
872 | but->SetToolTip( _("Go to home directory") ); | |
873 | #endif | |
874 | buttonsizer->Add( but, 0, wxALL, 5); | |
c8c0e54c | 875 | |
e6daf794 | 876 | buttonsizer->Add( 20, 20 ); |
c8c0e54c | 877 | |
e6daf794 RR |
878 | but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) ); |
879 | #if wxUSE_TOOLTIPS | |
880 | but->SetToolTip( _("Create new directory") ); | |
881 | #endif | |
882 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 883 | |
0b855868 | 884 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 ); |
c8c0e54c | 885 | |
9c884972 RR |
886 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); |
887 | staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 ); | |
888 | m_static = new wxStaticText( this, -1, m_dir ); | |
889 | staticsizer->Add( m_static, 1 ); | |
890 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 ); | |
891 | ||
7941ba11 | 892 | if (m_dialogStyle & wxMULTIPLE) |
479cd5de | 893 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, |
48fe8374 | 894 | wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER ); |
7941ba11 | 895 | else |
479cd5de | 896 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, |
48fe8374 | 897 | wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL ); |
eaf40b23 | 898 | m_list -> ShowHidden(s_lastShowHidden); |
0b855868 | 899 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 ); |
c8c0e54c | 900 | |
8b17ba72 | 901 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); |
cae5359f | 902 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER ); |
0b855868 | 903 | textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
8b17ba72 RR |
904 | textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
905 | mainsizer->Add( textsizer, 0, wxEXPAND ); | |
906 | ||
907 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
cae5359f | 908 | m_choice = new wxChoice( this, ID_CHOICE ); |
8b17ba72 | 909 | choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 ); |
bf9e3e73 | 910 | m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") ); |
eaf40b23 | 911 | m_check->SetValue( s_lastShowHidden ); |
bf9e3e73 | 912 | choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 ); |
8b17ba72 RR |
913 | choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 ); |
914 | mainsizer->Add( choicesizer, 0, wxEXPAND ); | |
c8c0e54c | 915 | |
cae5359f RR |
916 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); |
917 | while (tokens.HasMoreTokens()) | |
918 | { | |
919 | firstWildText = tokens.GetNextToken(); | |
920 | firstWild = tokens.GetNextToken(); | |
921 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
922 | } | |
923 | m_choice->SetSelection( 0 ); | |
8b17ba72 RR |
924 | |
925 | SetAutoLayout( TRUE ); | |
926 | SetSizer( mainsizer ); | |
c8c0e54c | 927 | |
8b17ba72 RR |
928 | mainsizer->Fit( this ); |
929 | mainsizer->SetSizeHints( this ); | |
48fe8374 | 930 | |
8b17ba72 | 931 | Centre( wxBOTH ); |
ed58dbea | 932 | |
b6fa52db | 933 | /* |
ed58dbea RR |
934 | if (m_fileName.IsEmpty()) |
935 | m_list->SetFocus(); | |
936 | else | |
b6fa52db | 937 | */ |
ed58dbea | 938 | m_text->SetFocus(); |
c8c0e54c | 939 | |
8b17ba72 RR |
940 | wxEndBusyCursor(); |
941 | } | |
942 | ||
cae5359f RR |
943 | wxFileDialog::~wxFileDialog() |
944 | { | |
eaf40b23 VS |
945 | if (wxConfig::Get(FALSE)) |
946 | { | |
947 | wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle); | |
948 | wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden); | |
949 | } | |
cae5359f RR |
950 | } |
951 | ||
952 | void wxFileDialog::OnChoice( wxCommandEvent &event ) | |
953 | { | |
13111b2a VZ |
954 | int index = (int)event.GetInt(); |
955 | wxString *str = (wxString*) m_choice->GetClientData( index ); | |
cae5359f | 956 | m_list->SetWild( *str ); |
13111b2a | 957 | m_filterIndex = index; |
655cf310 VS |
958 | if ( str -> Left( 2 ) == wxT("*.") ) |
959 | { | |
960 | m_filterExtension = str -> Mid( 1 ); | |
961 | if (m_filterExtension == ".*") m_filterExtension = wxEmptyString; | |
962 | } | |
963 | else | |
964 | m_filterExtension = wxEmptyString; | |
cae5359f RR |
965 | } |
966 | ||
bf9e3e73 RR |
967 | void wxFileDialog::OnCheck( wxCommandEvent &event ) |
968 | { | |
eaf40b23 | 969 | m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) ); |
bf9e3e73 RR |
970 | } |
971 | ||
e1811a01 | 972 | void wxFileDialog::OnActivated( wxListEvent &event ) |
8b17ba72 | 973 | { |
e1811a01 | 974 | HandleAction( event.m_item.m_text ); |
8b17ba72 RR |
975 | } |
976 | ||
cae5359f RR |
977 | void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) |
978 | { | |
979 | wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
980 | cevent.SetEventObject( this ); | |
981 | GetEventHandler()->ProcessEvent( cevent ); | |
982 | } | |
983 | ||
8b17ba72 RR |
984 | void wxFileDialog::OnSelected( wxListEvent &event ) |
985 | { | |
e1811a01 | 986 | if (FindFocus() != m_list) return; |
479cd5de | 987 | |
e1811a01 | 988 | wxString filename( event.m_item.m_text ); |
223d09f6 | 989 | if (filename == wxT("..")) return; |
479cd5de | 990 | |
e1811a01 RR |
991 | wxString dir; |
992 | m_list->GetDir( dir ); | |
223d09f6 | 993 | if (dir != wxT("/")) dir += wxT("/"); |
e1811a01 RR |
994 | dir += filename; |
995 | if (wxDirExists(dir)) return; | |
479cd5de | 996 | |
e1811a01 | 997 | m_text->SetValue( filename ); |
8b17ba72 RR |
998 | } |
999 | ||
e1811a01 | 1000 | void wxFileDialog::HandleAction( const wxString &fn ) |
8b17ba72 | 1001 | { |
e1811a01 | 1002 | wxString filename( fn ); |
8b17ba72 RR |
1003 | wxString dir; |
1004 | m_list->GetDir( dir ); | |
1005 | if (filename.IsEmpty()) return; | |
223d09f6 | 1006 | if (filename == wxT(".")) return; |
c8c0e54c | 1007 | |
223d09f6 | 1008 | if (filename == wxT("..")) |
0b855868 RR |
1009 | { |
1010 | m_list->GoToParentDir(); | |
1011 | m_list->SetFocus(); | |
9c884972 RR |
1012 | m_list->GetDir( dir ); |
1013 | m_static->SetLabel( dir ); | |
c8c0e54c | 1014 | return; |
0b855868 RR |
1015 | } |
1016 | ||
223d09f6 | 1017 | if (filename == wxT("~")) |
ed58dbea RR |
1018 | { |
1019 | m_list->GoToHomeDir(); | |
1020 | m_list->SetFocus(); | |
1021 | m_list->GetDir( dir ); | |
1022 | m_static->SetLabel( dir ); | |
c8c0e54c | 1023 | return; |
ed58dbea | 1024 | } |
c8c0e54c | 1025 | |
223d09f6 | 1026 | if (filename[0] == wxT('~')) |
ed58dbea RR |
1027 | { |
1028 | filename.Remove( 0, 1 ); | |
c8c0e54c | 1029 | wxString tmp( wxGetUserHome() ); |
223d09f6 | 1030 | tmp += wxT('/'); |
c8c0e54c VZ |
1031 | tmp += filename; |
1032 | filename = tmp; | |
ed58dbea RR |
1033 | } |
1034 | ||
223d09f6 KB |
1035 | if ((filename.Find(wxT('*')) != wxNOT_FOUND) || |
1036 | (filename.Find(wxT('?')) != wxNOT_FOUND)) | |
cae5359f | 1037 | { |
223d09f6 | 1038 | if (filename.Find(wxT('/')) != wxNOT_FOUND) |
c8c0e54c VZ |
1039 | { |
1040 | wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR ); | |
1041 | return; | |
1042 | } | |
1043 | m_list->SetWild( filename ); | |
1044 | return; | |
cae5359f RR |
1045 | } |
1046 | ||
223d09f6 KB |
1047 | if (dir != wxT("/")) dir += wxT("/"); |
1048 | if (filename[0] != wxT('/')) | |
ed58dbea RR |
1049 | { |
1050 | dir += filename; | |
1051 | filename = dir; | |
1052 | } | |
c8c0e54c | 1053 | |
8b17ba72 RR |
1054 | if (wxDirExists(filename)) |
1055 | { | |
1056 | m_list->GoToDir( filename ); | |
9c884972 RR |
1057 | m_list->GetDir( dir ); |
1058 | m_static->SetLabel( dir ); | |
c8c0e54c | 1059 | return; |
8b17ba72 | 1060 | } |
c8c0e54c | 1061 | |
655cf310 | 1062 | |
8b17ba72 RR |
1063 | if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) ) |
1064 | { | |
655cf310 VS |
1065 | if (filename.Find( wxT('.') ) == wxNOT_FOUND || |
1066 | filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND) | |
1067 | filename << m_filterExtension; | |
8b17ba72 RR |
1068 | if (wxFileExists( filename )) |
1069 | { | |
1070 | wxString msg; | |
1071 | msg.Printf( _("File '%s' already exists, do you really want to " | |
1072 | "overwrite it?"), filename.c_str() ); | |
1073 | ||
1074 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) | |
1075 | return; | |
1076 | } | |
1077 | } | |
655cf310 | 1078 | else if ( m_dialogStyle & wxOPEN ) |
8b17ba72 RR |
1079 | { |
1080 | if ( !wxFileExists( filename ) ) | |
655cf310 VS |
1081 | if (filename.Find( wxT('.') ) == wxNOT_FOUND || |
1082 | filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND) | |
1083 | filename << m_filterExtension; | |
1084 | ||
1085 | if ( m_dialogStyle & wxFILE_MUST_EXIST ) | |
8b17ba72 | 1086 | { |
655cf310 VS |
1087 | if ( !wxFileExists( filename ) ) |
1088 | { | |
1089 | wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR ); | |
1090 | return; | |
1091 | } | |
8b17ba72 RR |
1092 | } |
1093 | } | |
1094 | ||
1095 | SetPath( filename ); | |
479cd5de | 1096 | |
9b7e522a RR |
1097 | wxCommandEvent event; |
1098 | wxDialog::OnOK(event); | |
e1811a01 RR |
1099 | } |
1100 | ||
5e0201ea | 1101 | void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) ) |
e1811a01 RR |
1102 | { |
1103 | HandleAction( m_text->GetValue() ); | |
8b17ba72 RR |
1104 | } |
1105 | ||
1106 | void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) ) | |
1107 | { | |
1108 | m_list->ChangeToListMode(); | |
eaf40b23 | 1109 | s_lastViewStyle = wxLC_LIST; |
0b855868 | 1110 | m_list->SetFocus(); |
8b17ba72 RR |
1111 | } |
1112 | ||
1113 | void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) ) | |
1114 | { | |
1115 | m_list->ChangeToReportMode(); | |
eaf40b23 | 1116 | s_lastViewStyle = wxLC_REPORT; |
0b855868 | 1117 | m_list->SetFocus(); |
8b17ba72 RR |
1118 | } |
1119 | ||
1120 | void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) ) | |
1121 | { | |
1122 | m_list->GoToParentDir(); | |
0b855868 | 1123 | m_list->SetFocus(); |
9c884972 RR |
1124 | wxString dir; |
1125 | m_list->GetDir( dir ); | |
1126 | m_static->SetLabel( dir ); | |
8b17ba72 RR |
1127 | } |
1128 | ||
1129 | void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) ) | |
1130 | { | |
1131 | m_list->GoToHomeDir(); | |
0b855868 | 1132 | m_list->SetFocus(); |
9c884972 RR |
1133 | wxString dir; |
1134 | m_list->GetDir( dir ); | |
1135 | m_static->SetLabel( dir ); | |
0b855868 RR |
1136 | } |
1137 | ||
1138 | void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) ) | |
1139 | { | |
1140 | m_list->MakeDir(); | |
8b17ba72 RR |
1141 | } |
1142 | ||
1143 | void wxFileDialog::SetPath( const wxString& path ) | |
1144 | { | |
1145 | // not only set the full path but also update filename and dir | |
1146 | m_path = path; | |
1147 | if ( !!path ) | |
1148 | { | |
1149 | wxString ext; | |
1150 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
c8c0e54c VZ |
1151 | if (!ext.IsEmpty()) |
1152 | { | |
223d09f6 | 1153 | m_fileName += wxT("."); |
8b17ba72 | 1154 | m_fileName += ext; |
c8c0e54c | 1155 | } |
8b17ba72 RR |
1156 | } |
1157 | } | |
c8c0e54c | 1158 | |
7941ba11 RR |
1159 | void wxFileDialog::GetPaths( wxArrayString& paths ) const |
1160 | { | |
1161 | paths.Empty(); | |
28c9c76e RR |
1162 | if (m_list->GetSelectedItemCount() == 0) |
1163 | { | |
1164 | paths.Add( GetPath() ); | |
1165 | return; | |
1166 | } | |
479cd5de | 1167 | |
7941ba11 RR |
1168 | paths.Alloc( m_list->GetSelectedItemCount() ); |
1169 | ||
1170 | wxString dir; | |
1171 | m_list->GetDir( dir ); | |
1172 | if (dir != wxT("/")) dir += wxT("/"); | |
1173 | ||
1174 | wxListItem item; | |
1175 | item.m_mask = wxLIST_MASK_TEXT; | |
1176 | ||
1177 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
92da8bde | 1178 | while ( item.m_itemId != -1 ) |
68df5777 | 1179 | { |
7941ba11 RR |
1180 | m_list->GetItem( item ); |
1181 | paths.Add( dir + item.m_text ); | |
68df5777 | 1182 | item.m_itemId = m_list->GetNextItem( item.m_itemId, |
7941ba11 RR |
1183 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); |
1184 | } | |
1185 | } | |
1186 | ||
1187 | void wxFileDialog::GetFilenames(wxArrayString& files) const | |
1188 | { | |
1189 | files.Empty(); | |
28c9c76e RR |
1190 | if (m_list->GetSelectedItemCount() == 0) |
1191 | { | |
1192 | files.Add( GetFilename() ); | |
1193 | return; | |
1194 | } | |
7941ba11 RR |
1195 | files.Alloc( m_list->GetSelectedItemCount() ); |
1196 | ||
1197 | wxListItem item; | |
1198 | item.m_mask = wxLIST_MASK_TEXT; | |
1199 | ||
1200 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
92da8bde | 1201 | while ( item.m_itemId != -1 ) |
68df5777 | 1202 | { |
7941ba11 RR |
1203 | m_list->GetItem( item ); |
1204 | files.Add( item.m_text ); | |
68df5777 | 1205 | item.m_itemId = m_list->GetNextItem( item.m_itemId, |
7941ba11 RR |
1206 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); |
1207 | } | |
1208 | } | |
1209 | ||
655cf310 VS |
1210 | |
1211 | ||
8b17ba72 RR |
1212 | // ---------------------------------------------------------------------------- |
1213 | // global functions | |
1214 | // ---------------------------------------------------------------------------- | |
1215 | ||
1216 | wxString | |
1217 | wxFileSelectorEx(const wxChar *message, | |
1218 | const wxChar *default_path, | |
1219 | const wxChar *default_filename, | |
5e0201ea | 1220 | int *WXUNUSED(indexDefaultExtension), |
8b17ba72 RR |
1221 | const wxChar *wildcard, |
1222 | int flags, | |
1223 | wxWindow *parent, | |
1224 | int x, int y) | |
1225 | { | |
1226 | // TODO: implement this somehow | |
223d09f6 | 1227 | return wxFileSelector(message, default_path, default_filename, wxT(""), |
8b17ba72 RR |
1228 | wildcard, flags, parent, x, y); |
1229 | } | |
1230 | ||
1231 | wxString wxFileSelector( const wxChar *title, | |
1232 | const wxChar *defaultDir, const wxChar *defaultFileName, | |
1233 | const wxChar *defaultExtension, const wxChar *filter, int flags, | |
1234 | wxWindow *parent, int x, int y ) | |
1235 | { | |
1236 | wxString filter2; | |
1237 | if ( defaultExtension && !filter ) | |
223d09f6 | 1238 | filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ; |
8b17ba72 RR |
1239 | else if ( filter ) |
1240 | filter2 = filter; | |
1241 | ||
1242 | wxString defaultDirString; | |
1243 | if (defaultDir) | |
1244 | defaultDirString = defaultDir; | |
1245 | ||
1246 | wxString defaultFilenameString; | |
1247 | if (defaultFileName) | |
1248 | defaultFilenameString = defaultFileName; | |
1249 | ||
1250 | wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) ); | |
1251 | ||
1252 | if ( fileDialog.ShowModal() == wxID_OK ) | |
1253 | { | |
1254 | return fileDialog.GetPath(); | |
1255 | } | |
1256 | else | |
1257 | { | |
1258 | return wxEmptyString; | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent ) | |
1263 | { | |
1264 | wxChar *ext = (wxChar *)extension; | |
1265 | ||
1266 | wxChar prompt[50]; | |
1267 | wxString str = _("Load %s file"); | |
1268 | wxSprintf(prompt, str, what); | |
1269 | ||
223d09f6 | 1270 | if (*ext == wxT('.')) ext++; |
8b17ba72 | 1271 | wxChar wild[60]; |
223d09f6 | 1272 | wxSprintf(wild, wxT("*.%s"), ext); |
8b17ba72 RR |
1273 | |
1274 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
1275 | } | |
1276 | ||
1277 | wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name, | |
1278 | wxWindow *parent ) | |
1279 | { | |
1280 | wxChar *ext = (wxChar *)extension; | |
1281 | ||
1282 | wxChar prompt[50]; | |
1283 | wxString str = _("Save %s file"); | |
1284 | wxSprintf(prompt, str, what); | |
1285 | ||
223d09f6 | 1286 | if (*ext == wxT('.')) ext++; |
8b17ba72 | 1287 | wxChar wild[60]; |
223d09f6 | 1288 | wxSprintf(wild, wxT("*.%s"), ext); |
8b17ba72 RR |
1289 | |
1290 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
1291 | } | |
1292 | ||
655cf310 VS |
1293 | |
1294 | ||
1295 | ||
1296 | ||
1297 | ||
1298 | // A module to allow icons table cleanup | |
1299 | ||
1300 | class wxFileDialogGenericModule: public wxModule | |
1301 | { | |
1302 | DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule) | |
1303 | public: | |
1304 | wxFileDialogGenericModule() {} | |
1305 | bool OnInit() { return TRUE; } | |
1306 | void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} } | |
1307 | }; | |
1308 | ||
1309 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule) |