]>
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" |
8b17ba72 | 35 | |
e6daf794 RR |
36 | #if wxUSE_TOOLTIPS |
37 | #include "wx/tooltip.h" | |
38 | #endif | |
39 | ||
ac2def68 RR |
40 | #include <sys/types.h> |
41 | #include <sys/stat.h> | |
42 | #include <dirent.h> | |
43 | #include <pwd.h> | |
44 | #include <grp.h> | |
45 | #include <time.h> | |
46 | #include <unistd.h> | |
8b17ba72 | 47 | |
0b855868 RR |
48 | #include "wx/generic/home.xpm" |
49 | #include "wx/generic/listview.xpm" | |
50 | #include "wx/generic/repview.xpm" | |
e6daf794 RR |
51 | #include "wx/generic/new_dir.xpm" |
52 | #include "wx/generic/dir_up.xpm" | |
0b855868 RR |
53 | |
54 | /* XPM */ | |
55 | static char * folder_xpm[] = { | |
56 | /* width height ncolors chars_per_pixel */ | |
57 | "16 16 6 1", | |
58 | /* colors */ | |
c8c0e54c VZ |
59 | " s None c None", |
60 | ". c #000000", | |
61 | "+ c #c0c0c0", | |
62 | "@ c #808080", | |
63 | "# c #ffff00", | |
64 | "$ c #ffffff", | |
0b855868 RR |
65 | /* pixels */ |
66 | " ", | |
67 | " @@@@@ ", | |
68 | " @#+#+#@ ", | |
69 | " @#+#+#+#@@@@@@ ", | |
70 | " @$$$$$$$$$$$$@.", | |
71 | " @$#+#+#+#+#+#@.", | |
72 | " @$+#+#+#+#+#+@.", | |
73 | " @$#+#+#+#+#+#@.", | |
74 | " @$+#+#+#+#+#+@.", | |
75 | " @$#+#+#+#+#+#@.", | |
76 | " @$+#+#+#+#+#+@.", | |
77 | " @$#+#+#+#+#+#@.", | |
78 | " @@@@@@@@@@@@@@.", | |
79 | " ..............", | |
80 | " ", | |
81 | " "}; | |
8b17ba72 | 82 | |
c8c0e54c VZ |
83 | // ---------------------------------------------------------------------------- |
84 | // private functions | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | static | |
88 | int ListCompare( long data1, long data2, long WXUNUSED(data) ) | |
89 | { | |
90 | wxFileData *fd1 = (wxFileData*)data1 ; | |
91 | wxFileData *fd2 = (wxFileData*)data2 ; | |
223d09f6 KB |
92 | if (fd1->GetName() == wxT("..")) return -1; |
93 | if (fd2->GetName() == wxT("..")) return 1; | |
c8c0e54c VZ |
94 | if (fd1->IsDir() && !fd2->IsDir()) return -1; |
95 | if (fd2->IsDir() && !fd1->IsDir()) return 1; | |
96 | return strcmp( fd1->GetName(), fd2->GetName() ); | |
97 | } | |
98 | ||
8b17ba72 RR |
99 | //----------------------------------------------------------------------------- |
100 | // wxFileData | |
101 | //----------------------------------------------------------------------------- | |
102 | ||
103 | IMPLEMENT_DYNAMIC_CLASS(wxFileData,wxObject); | |
104 | ||
105 | wxFileData::wxFileData( const wxString &name, const wxString &fname ) | |
106 | { | |
107 | m_name = name; | |
108 | m_fileName = fname; | |
c8c0e54c | 109 | |
8b17ba72 RR |
110 | struct stat buff; |
111 | stat( m_fileName.GetData(), &buff ); | |
112 | struct stat lbuff; | |
113 | lstat( m_fileName.GetData(), &lbuff ); | |
114 | ||
115 | struct tm *t = localtime( &lbuff.st_mtime ); | |
116 | // struct passwd *user = getpwuid( buff.st_uid ); | |
117 | // struct group *grp = getgrgid( buff.st_gid ); | |
118 | ||
119 | m_isDir = S_ISDIR( buff.st_mode ); | |
120 | m_isLink = S_ISLNK( lbuff.st_mode ); | |
121 | m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR ); | |
122 | ||
123 | m_size = buff.st_size; | |
124 | ||
125 | m_hour = t->tm_hour; | |
126 | m_minute = t->tm_min; | |
127 | m_month = t->tm_mon+1; | |
128 | m_day = t->tm_mday; | |
129 | m_year = t->tm_year; | |
130 | ||
c8c0e54c | 131 | m_permissions.sprintf( "%c%c%c", |
8b17ba72 RR |
132 | ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? 'r' : '-'), |
133 | ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? 'w' : '-'), | |
134 | ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? 'x' : '-') ); | |
135 | } | |
136 | ||
137 | wxString wxFileData::GetName() const | |
138 | { | |
139 | return m_name; | |
140 | } | |
141 | ||
142 | wxString wxFileData::GetFullName() const | |
143 | { | |
144 | return m_fileName; | |
145 | } | |
146 | ||
147 | wxString wxFileData::GetHint() const | |
148 | { | |
149 | wxString s = m_fileName; | |
150 | s += " "; | |
151 | if (m_isDir) s += _("<DIR> "); | |
152 | else if (m_isLink) s += _("<LINK> "); | |
153 | else | |
154 | { | |
155 | s += LongToString( m_size ); | |
156 | s += _(" bytes "); | |
157 | } | |
158 | s += IntToString( m_day ); | |
223d09f6 | 159 | s += wxT("."); |
8b17ba72 | 160 | s += IntToString( m_month ); |
223d09f6 | 161 | s += wxT("."); |
8b17ba72 | 162 | s += IntToString( m_year ); |
223d09f6 | 163 | s += wxT(" "); |
8b17ba72 | 164 | s += IntToString( m_hour ); |
223d09f6 | 165 | s += wxT(":"); |
8b17ba72 | 166 | s += IntToString( m_minute ); |
223d09f6 | 167 | s += wxT(" "); |
8b17ba72 RR |
168 | s += m_permissions; |
169 | return s; | |
170 | }; | |
171 | ||
c8c0e54c | 172 | wxString wxFileData::GetEntry( int num ) |
8b17ba72 RR |
173 | { |
174 | wxString s; | |
175 | switch (num) | |
176 | { | |
177 | case 0: | |
178 | s = m_name; | |
179 | break; | |
180 | case 1: | |
181 | if (m_isDir) s = _("<DIR>"); | |
182 | else if (m_isLink) s = _("<LINK>"); | |
183 | else s = LongToString( m_size ); | |
184 | break; | |
185 | case 2: | |
223d09f6 | 186 | if (m_day < 10) s = wxT("0"); else s = wxT(""); |
8b17ba72 | 187 | s += IntToString( m_day ); |
223d09f6 KB |
188 | s += wxT("."); |
189 | if (m_month < 10) s += wxT("0"); | |
8b17ba72 | 190 | s += IntToString( m_month ); |
223d09f6 KB |
191 | s += wxT("."); |
192 | if (m_year < 10) s += wxT("0"); // this should happen real soon... | |
8b17ba72 RR |
193 | s += IntToString( m_year ); |
194 | break; | |
195 | case 3: | |
223d09f6 | 196 | if (m_hour < 10) s = wxT("0"); else s = wxT(""); |
8b17ba72 | 197 | s += IntToString( m_hour ); |
223d09f6 KB |
198 | s += wxT(":"); |
199 | if (m_minute < 10) s += wxT("0"); | |
8b17ba72 RR |
200 | s += IntToString( m_minute ); |
201 | break; | |
202 | case 4: | |
203 | s = m_permissions; | |
204 | break; | |
205 | default: | |
223d09f6 | 206 | s = wxT("No entry"); |
8b17ba72 RR |
207 | break; |
208 | } | |
209 | return s; | |
210 | } | |
211 | ||
212 | bool wxFileData::IsDir() | |
213 | { | |
214 | return m_isDir; | |
215 | } | |
216 | ||
217 | bool wxFileData::IsExe() | |
218 | { | |
219 | return m_isExe; | |
220 | } | |
221 | ||
222 | bool wxFileData::IsLink() | |
223 | { | |
224 | return m_isLink; | |
225 | } | |
226 | ||
227 | long wxFileData::GetSize() | |
228 | { | |
229 | return m_size; | |
230 | } | |
231 | ||
0b855868 | 232 | void wxFileData::SetNewName( const wxString &name, const wxString &fname ) |
8b17ba72 | 233 | { |
0b855868 RR |
234 | m_name = name; |
235 | m_fileName = fname; | |
8b17ba72 RR |
236 | } |
237 | ||
238 | void wxFileData::MakeItem( wxListItem &item ) | |
239 | { | |
240 | item.m_text = m_name; | |
241 | item.m_colour = wxBLACK; | |
242 | if (IsExe()) item.m_colour = wxRED; | |
243 | if (IsDir()) item.m_colour = wxBLUE; | |
0b855868 | 244 | if (IsDir()) item.m_image = 0; else item.m_image = -1; |
8b17ba72 RR |
245 | if (IsLink()) |
246 | { | |
247 | wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" ); | |
248 | item.m_colour = dg; | |
249 | } | |
250 | item.m_data = (long)this; | |
251 | } | |
c8c0e54c | 252 | |
8b17ba72 RR |
253 | //----------------------------------------------------------------------------- |
254 | // wxFileCtrl | |
255 | //----------------------------------------------------------------------------- | |
256 | ||
257 | IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl); | |
258 | ||
259 | BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl) | |
0b855868 RR |
260 | EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem) |
261 | EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit) | |
8b17ba72 RR |
262 | END_EVENT_TABLE() |
263 | ||
264 | wxFileCtrl::wxFileCtrl() | |
265 | { | |
223d09f6 | 266 | m_dirName = wxT("/"); |
8b17ba72 RR |
267 | m_showHidden = FALSE; |
268 | } | |
269 | ||
c8c0e54c | 270 | wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id, |
0b855868 | 271 | const wxString &dirName, const wxString &wild, |
8b17ba72 RR |
272 | const wxPoint &pos, const wxSize &size, |
273 | long style, const wxValidator &validator, const wxString &name ) : | |
274 | wxListCtrl( win, id, pos, size, style, validator, name ) | |
275 | { | |
0b855868 | 276 | wxImageList *imageList = new wxImageList( 16, 16 ); |
8b17ba72 | 277 | imageList->Add( wxBitmap( folder_xpm ) ); |
0b855868 | 278 | SetImageList( imageList, wxIMAGE_LIST_SMALL ); |
c8c0e54c | 279 | |
8b17ba72 | 280 | m_dirName = dirName; |
0b855868 | 281 | m_wild = wild; |
8b17ba72 RR |
282 | m_showHidden = FALSE; |
283 | Update(); | |
8b17ba72 RR |
284 | } |
285 | ||
286 | void wxFileCtrl::ChangeToListMode() | |
287 | { | |
288 | SetSingleStyle( wxLC_LIST ); | |
289 | Update(); | |
290 | } | |
291 | ||
292 | void wxFileCtrl::ChangeToReportMode() | |
293 | { | |
294 | SetSingleStyle( wxLC_REPORT ); | |
295 | Update(); | |
296 | } | |
297 | ||
298 | void wxFileCtrl::ChangeToIconMode() | |
299 | { | |
300 | SetSingleStyle( wxLC_ICON ); | |
301 | Update(); | |
302 | } | |
303 | ||
304 | void wxFileCtrl::ShowHidden( bool show ) | |
305 | { | |
306 | m_showHidden = show; | |
307 | Update(); | |
308 | } | |
309 | ||
0b855868 RR |
310 | long wxFileCtrl::Add( wxFileData *fd, wxListItem &item ) |
311 | { | |
312 | long ret = -1; | |
313 | item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE; | |
314 | fd->MakeItem( item ); | |
315 | long my_style = GetWindowStyleFlag(); | |
316 | if (my_style & wxLC_REPORT) | |
317 | { | |
318 | ret = InsertItem( item ); | |
319 | for (int i = 1; i < 5; i++) SetItem( item.m_itemId, i, fd->GetEntry( i) ); | |
320 | } | |
321 | else if (my_style & wxLC_LIST) | |
322 | { | |
323 | ret = InsertItem( item ); | |
c8c0e54c | 324 | } |
0b855868 RR |
325 | return ret; |
326 | } | |
327 | ||
8b17ba72 | 328 | void wxFileCtrl::Update() |
c8c0e54c | 329 | { |
8b17ba72 RR |
330 | ClearAll(); |
331 | long my_style = GetWindowStyleFlag(); | |
332 | if (my_style & wxLC_REPORT) | |
333 | { | |
e6daf794 | 334 | InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, 130 ); |
8b17ba72 RR |
335 | InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 ); |
336 | InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 55 ); | |
337 | InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 ); | |
338 | InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 ); | |
339 | } | |
340 | wxFileData *fd = (wxFileData *) NULL; | |
341 | wxListItem item; | |
8b17ba72 RR |
342 | item.m_itemId = 0; |
343 | item.m_col = 0; | |
0b855868 | 344 | |
223d09f6 | 345 | if (m_dirName != wxT("/")) |
0b855868 RR |
346 | { |
347 | wxString p( wxPathOnly(m_dirName) ); | |
223d09f6 KB |
348 | if (p.IsEmpty()) p = wxT("/"); |
349 | fd = new wxFileData( wxT(".."), p ); | |
c8c0e54c | 350 | Add( fd, item ); |
0b855868 RR |
351 | item.m_itemId++; |
352 | } | |
353 | ||
223d09f6 | 354 | wxString res = m_dirName + wxT("/*"); |
cae5359f | 355 | wxString f( wxFindFirstFile( res.GetData(), wxDIR ) ); |
8b17ba72 RR |
356 | while (!f.IsEmpty()) |
357 | { | |
358 | res = wxFileNameFromPath( f ); | |
359 | fd = new wxFileData( res, f ); | |
0b855868 | 360 | wxString s = fd->GetName(); |
223d09f6 | 361 | if (m_showHidden || (s[0] != wxT('.'))) |
8b17ba72 | 362 | { |
c8c0e54c | 363 | Add( fd, item ); |
8b17ba72 RR |
364 | item.m_itemId++; |
365 | } | |
366 | f = wxFindNextFile(); | |
367 | } | |
c8c0e54c | 368 | |
223d09f6 | 369 | res = m_dirName + wxT("/") + m_wild; |
cae5359f RR |
370 | f = wxFindFirstFile( res.GetData(), wxFILE ); |
371 | while (!f.IsEmpty()) | |
372 | { | |
373 | res = wxFileNameFromPath( f ); | |
374 | fd = new wxFileData( res, f ); | |
375 | wxString s = fd->GetName(); | |
223d09f6 | 376 | if (m_showHidden || (s[0] != wxT('.'))) |
cae5359f | 377 | { |
c8c0e54c | 378 | Add( fd, item ); |
cae5359f RR |
379 | item.m_itemId++; |
380 | } | |
381 | f = wxFindNextFile(); | |
382 | } | |
c8c0e54c | 383 | |
8b17ba72 RR |
384 | SortItems( ListCompare, 0 ); |
385 | } | |
386 | ||
0b855868 | 387 | void wxFileCtrl::SetWild( const wxString &wild ) |
8b17ba72 | 388 | { |
0b855868 RR |
389 | m_wild = wild; |
390 | Update(); | |
391 | } | |
392 | ||
393 | void wxFileCtrl::MakeDir() | |
394 | { | |
223d09f6 | 395 | wxString new_name( wxT("NewName") ); |
0b855868 | 396 | wxString path( m_dirName ); |
223d09f6 | 397 | path += wxT("/"); |
0b855868 RR |
398 | path += new_name; |
399 | if (wxFileExists(path)) | |
8b17ba72 | 400 | { |
0b855868 RR |
401 | // try NewName0, NewName1 etc. |
402 | int i = 0; | |
c8c0e54c | 403 | do { |
0b855868 | 404 | new_name = _("NewName"); |
c8c0e54c | 405 | wxString num; |
223d09f6 | 406 | num.Printf( wxT("%d"), i ); |
c8c0e54c VZ |
407 | new_name += num; |
408 | ||
0b855868 | 409 | path = m_dirName; |
223d09f6 | 410 | path += wxT("/"); |
0b855868 | 411 | path += new_name; |
c8c0e54c VZ |
412 | i++; |
413 | } while (wxFileExists(path)); | |
8b17ba72 | 414 | } |
c8c0e54c | 415 | |
0b855868 | 416 | wxLogNull log; |
c8c0e54c | 417 | if (!wxMkdir(path)) |
8b17ba72 | 418 | { |
0b855868 | 419 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 420 | dialog.ShowModal(); |
0b855868 | 421 | return; |
8b17ba72 | 422 | } |
8b17ba72 | 423 | |
0b855868 RR |
424 | wxFileData *fd = new wxFileData( new_name, path ); |
425 | wxListItem item; | |
426 | item.m_itemId = 0; | |
427 | item.m_col = 0; | |
428 | int id = Add( fd, item ); | |
c8c0e54c | 429 | |
0b855868 RR |
430 | if (id != -1) |
431 | { | |
432 | SortItems( ListCompare, 0 ); | |
c8c0e54c | 433 | id = FindItem( 0, (long)fd ); |
0b855868 RR |
434 | EnsureVisible( id ); |
435 | EditLabel( id ); | |
436 | } | |
8b17ba72 RR |
437 | } |
438 | ||
439 | void wxFileCtrl::GoToParentDir() | |
440 | { | |
223d09f6 | 441 | if (m_dirName != wxT("/")) |
8b17ba72 | 442 | { |
c8c0e54c | 443 | wxString fname( wxFileNameFromPath(m_dirName) ); |
0b855868 | 444 | m_dirName = wxPathOnly( m_dirName ); |
223d09f6 | 445 | if (m_dirName.IsEmpty()) m_dirName = wxT("/"); |
8b17ba72 | 446 | Update(); |
c8c0e54c VZ |
447 | int id = FindItem( 0, fname ); |
448 | if (id != -1) | |
449 | { | |
450 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
451 | EnsureVisible( id ); | |
452 | } | |
8b17ba72 RR |
453 | } |
454 | } | |
455 | ||
456 | void wxFileCtrl::GoToHomeDir() | |
457 | { | |
458 | wxString s = wxGetUserHome( wxString() ); | |
459 | m_dirName = s; | |
460 | Update(); | |
e6daf794 RR |
461 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
462 | EnsureVisible( 0 ); | |
8b17ba72 RR |
463 | } |
464 | ||
465 | void wxFileCtrl::GoToDir( const wxString &dir ) | |
466 | { | |
467 | m_dirName = dir; | |
468 | Update(); | |
e6daf794 RR |
469 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
470 | EnsureVisible( 0 ); | |
8b17ba72 RR |
471 | } |
472 | ||
473 | void wxFileCtrl::GetDir( wxString &dir ) | |
474 | { | |
475 | dir = m_dirName; | |
476 | } | |
477 | ||
8b17ba72 RR |
478 | void wxFileCtrl::OnListDeleteItem( wxListEvent &event ) |
479 | { | |
480 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
481 | delete fd; | |
482 | } | |
483 | ||
0b855868 | 484 | void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event ) |
8b17ba72 RR |
485 | { |
486 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
0b855868 | 487 | wxASSERT( fd ); |
c8c0e54c | 488 | |
0b855868 RR |
489 | if ((event.GetLabel().IsEmpty()) || |
490 | (event.GetLabel() == _(".")) || | |
491 | (event.GetLabel() == _("..")) || | |
223d09f6 | 492 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) |
8b17ba72 | 493 | { |
0b855868 | 494 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 495 | dialog.ShowModal(); |
0b855868 | 496 | event.Veto(); |
c8c0e54c | 497 | return; |
8b17ba72 | 498 | } |
c8c0e54c | 499 | |
0b855868 | 500 | wxString new_name( wxPathOnly( fd->GetFullName() ) ); |
223d09f6 | 501 | new_name += wxT("/"); |
0b855868 | 502 | new_name += event.GetLabel(); |
c8c0e54c | 503 | |
0b855868 | 504 | wxLogNull log; |
c8c0e54c | 505 | |
0b855868 | 506 | if (wxFileExists(new_name)) |
8b17ba72 | 507 | { |
0b855868 | 508 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 509 | dialog.ShowModal(); |
0b855868 | 510 | event.Veto(); |
8b17ba72 | 511 | } |
c8c0e54c | 512 | |
0b855868 | 513 | if (wxRenameFile(fd->GetFullName(),new_name)) |
8b17ba72 | 514 | { |
0b855868 | 515 | fd->SetNewName( new_name, event.GetLabel() ); |
c8c0e54c | 516 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
0b855868 | 517 | EnsureVisible( event.GetItem() ); |
8b17ba72 RR |
518 | } |
519 | else | |
520 | { | |
0b855868 | 521 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 522 | dialog.ShowModal(); |
0b855868 | 523 | event.Veto(); |
8b17ba72 | 524 | } |
8b17ba72 RR |
525 | } |
526 | ||
527 | //----------------------------------------------------------------------------- | |
528 | // wxFileDialog | |
529 | //----------------------------------------------------------------------------- | |
530 | ||
a16d3c04 KB |
531 | #define ID_LIST_MODE wxID_FILEDLGG |
532 | #define ID_REPORT_MODE wxID_FILEDLGG + 1 | |
533 | #define ID_UP_DIR wxID_FILEDLGG + 5 | |
534 | #define ID_PARENT_DIR wxID_FILEDLGG + 6 | |
535 | #define ID_NEW_DIR wxID_FILEDLGG + 7 | |
536 | #define ID_CHOICE wxID_FILEDLGG + 8 | |
537 | #define ID_TEXT wxID_FILEDLGG + 9 | |
538 | #define ID_LIST_CTRL wxID_FILEDLGG + 10 | |
539 | #define ID_ACTIVATED wxID_FILEDLGG + 11 | |
8b17ba72 RR |
540 | |
541 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog) | |
542 | ||
543 | BEGIN_EVENT_TABLE(wxFileDialog,wxDialog) | |
544 | EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList) | |
545 | EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport) | |
8b17ba72 RR |
546 | EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp) |
547 | EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome) | |
0b855868 | 548 | EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew) |
8b17ba72 RR |
549 | EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk) |
550 | EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected) | |
c8c0e54c VZ |
551 | EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated) |
552 | EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice) | |
553 | EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter) | |
8b17ba72 RR |
554 | END_EVENT_TABLE() |
555 | ||
556 | wxFileDialog::wxFileDialog(wxWindow *parent, | |
557 | const wxString& message, | |
558 | const wxString& defaultDir, | |
559 | const wxString& defaultFile, | |
560 | const wxString& wildCard, | |
561 | long style, | |
562 | const wxPoint& pos ) : | |
9c884972 | 563 | wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) |
8b17ba72 RR |
564 | { |
565 | wxBeginBusyCursor(); | |
c8c0e54c | 566 | |
8b17ba72 RR |
567 | m_message = message; |
568 | m_dialogStyle = style; | |
569 | m_dir = defaultDir; | |
ac2def68 RR |
570 | if (m_dir.IsEmpty()) |
571 | { | |
572 | char buf[200]; | |
c8c0e54c | 573 | m_dir = getcwd( buf, sizeof(buf) ); |
ac2def68 | 574 | } |
8b17ba72 | 575 | m_path = defaultDir; |
223d09f6 | 576 | m_path += wxT("/"); |
8b17ba72 RR |
577 | m_path += defaultFile; |
578 | m_fileName = defaultFile; | |
579 | m_wildCard = wildCard; | |
580 | m_filterIndex = 0; | |
c8c0e54c | 581 | |
cae5359f | 582 | // interpret wildcards |
c8c0e54c | 583 | |
cae5359f RR |
584 | if (m_wildCard.IsEmpty()) |
585 | m_wildCard = _("All files (*)|*"); | |
c8c0e54c | 586 | |
223d09f6 | 587 | wxStringTokenizer tokens( m_wildCard, wxT("|") ); |
cae5359f RR |
588 | wxString firstWild; |
589 | wxString firstWildText; | |
590 | if (tokens.CountTokens() == 1) | |
591 | { | |
592 | firstWildText = tokens.GetNextToken(); | |
593 | firstWild = firstWildText; | |
594 | } | |
595 | else | |
596 | { | |
223d09f6 | 597 | wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") ); |
cae5359f RR |
598 | firstWildText = tokens.GetNextToken(); |
599 | firstWild = tokens.GetNextToken(); | |
600 | } | |
601 | ||
602 | // layout | |
c8c0e54c | 603 | |
8b17ba72 | 604 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); |
c8c0e54c | 605 | |
8b17ba72 | 606 | wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
c8c0e54c | 607 | |
e6daf794 | 608 | wxBitmapButton *but; |
c8c0e54c | 609 | |
e6daf794 RR |
610 | but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) ); |
611 | #if wxUSE_TOOLTIPS | |
612 | but->SetToolTip( _("View files as a list view") ); | |
613 | #endif | |
614 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 615 | |
e6daf794 RR |
616 | but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) ); |
617 | #if wxUSE_TOOLTIPS | |
618 | but->SetToolTip( _("View files as a detailed view") ); | |
619 | #endif | |
620 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 621 | |
0b855868 | 622 | buttonsizer->Add( 30, 5, 1 ); |
e6daf794 RR |
623 | |
624 | but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) ); | |
625 | #if wxUSE_TOOLTIPS | |
626 | but->SetToolTip( _("Go to parent directory") ); | |
627 | #endif | |
628 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 629 | |
e6daf794 RR |
630 | but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) ); |
631 | #if wxUSE_TOOLTIPS | |
632 | but->SetToolTip( _("Go to home directory") ); | |
633 | #endif | |
634 | buttonsizer->Add( but, 0, wxALL, 5); | |
c8c0e54c | 635 | |
e6daf794 | 636 | buttonsizer->Add( 20, 20 ); |
c8c0e54c | 637 | |
e6daf794 RR |
638 | but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) ); |
639 | #if wxUSE_TOOLTIPS | |
640 | but->SetToolTip( _("Create new directory") ); | |
641 | #endif | |
642 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 643 | |
0b855868 | 644 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 ); |
c8c0e54c | 645 | |
9c884972 RR |
646 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); |
647 | staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 ); | |
648 | m_static = new wxStaticText( this, -1, m_dir ); | |
649 | staticsizer->Add( m_static, 1 ); | |
650 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 ); | |
651 | ||
c8c0e54c | 652 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, wxSize(440,180), |
8b17ba72 | 653 | wxLC_LIST | wxSUNKEN_BORDER | wxLC_SINGLE_SEL ); |
0b855868 | 654 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 ); |
c8c0e54c | 655 | |
8b17ba72 | 656 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); |
cae5359f | 657 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER ); |
0b855868 | 658 | textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
8b17ba72 RR |
659 | textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
660 | mainsizer->Add( textsizer, 0, wxEXPAND ); | |
661 | ||
662 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
cae5359f | 663 | m_choice = new wxChoice( this, ID_CHOICE ); |
8b17ba72 RR |
664 | choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 ); |
665 | choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 ); | |
666 | mainsizer->Add( choicesizer, 0, wxEXPAND ); | |
c8c0e54c | 667 | |
cae5359f RR |
668 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); |
669 | while (tokens.HasMoreTokens()) | |
670 | { | |
671 | firstWildText = tokens.GetNextToken(); | |
672 | firstWild = tokens.GetNextToken(); | |
673 | m_choice->Append( firstWildText, (void*) new wxString( firstWild ) ); | |
674 | } | |
675 | m_choice->SetSelection( 0 ); | |
8b17ba72 RR |
676 | |
677 | SetAutoLayout( TRUE ); | |
678 | SetSizer( mainsizer ); | |
c8c0e54c | 679 | |
8b17ba72 RR |
680 | mainsizer->Fit( this ); |
681 | mainsizer->SetSizeHints( this ); | |
c8c0e54c | 682 | |
8b17ba72 | 683 | Centre( wxBOTH ); |
ed58dbea RR |
684 | |
685 | if (m_fileName.IsEmpty()) | |
686 | m_list->SetFocus(); | |
687 | else | |
688 | m_text->SetFocus(); | |
c8c0e54c | 689 | |
8b17ba72 RR |
690 | wxEndBusyCursor(); |
691 | } | |
692 | ||
cae5359f RR |
693 | wxFileDialog::~wxFileDialog() |
694 | { | |
695 | } | |
696 | ||
697 | void wxFileDialog::OnChoice( wxCommandEvent &event ) | |
698 | { | |
699 | wxString *str = (wxString*) m_choice->GetClientData( event.GetInt() ); | |
700 | m_list->SetWild( *str ); | |
701 | } | |
702 | ||
e1811a01 | 703 | void wxFileDialog::OnActivated( wxListEvent &event ) |
8b17ba72 | 704 | { |
e1811a01 | 705 | HandleAction( event.m_item.m_text ); |
8b17ba72 RR |
706 | } |
707 | ||
cae5359f RR |
708 | void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) |
709 | { | |
710 | wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
711 | cevent.SetEventObject( this ); | |
712 | GetEventHandler()->ProcessEvent( cevent ); | |
713 | } | |
714 | ||
8b17ba72 RR |
715 | void wxFileDialog::OnSelected( wxListEvent &event ) |
716 | { | |
e1811a01 RR |
717 | if (FindFocus() != m_list) return; |
718 | ||
719 | wxString filename( event.m_item.m_text ); | |
223d09f6 | 720 | if (filename == wxT("..")) return; |
e1811a01 RR |
721 | |
722 | wxString dir; | |
723 | m_list->GetDir( dir ); | |
223d09f6 | 724 | if (dir != wxT("/")) dir += wxT("/"); |
e1811a01 RR |
725 | dir += filename; |
726 | if (wxDirExists(dir)) return; | |
727 | ||
728 | m_text->SetValue( filename ); | |
8b17ba72 RR |
729 | } |
730 | ||
e1811a01 | 731 | void wxFileDialog::HandleAction( const wxString &fn ) |
8b17ba72 | 732 | { |
e1811a01 | 733 | wxString filename( fn ); |
8b17ba72 RR |
734 | wxString dir; |
735 | m_list->GetDir( dir ); | |
736 | if (filename.IsEmpty()) return; | |
223d09f6 | 737 | if (filename == wxT(".")) return; |
c8c0e54c | 738 | |
223d09f6 | 739 | if (filename == wxT("..")) |
0b855868 RR |
740 | { |
741 | m_list->GoToParentDir(); | |
742 | m_list->SetFocus(); | |
9c884972 RR |
743 | m_list->GetDir( dir ); |
744 | m_static->SetLabel( dir ); | |
c8c0e54c | 745 | return; |
0b855868 RR |
746 | } |
747 | ||
223d09f6 | 748 | if (filename == wxT("~")) |
ed58dbea RR |
749 | { |
750 | m_list->GoToHomeDir(); | |
751 | m_list->SetFocus(); | |
752 | m_list->GetDir( dir ); | |
753 | m_static->SetLabel( dir ); | |
c8c0e54c | 754 | return; |
ed58dbea | 755 | } |
c8c0e54c | 756 | |
223d09f6 | 757 | if (filename[0] == wxT('~')) |
ed58dbea RR |
758 | { |
759 | filename.Remove( 0, 1 ); | |
c8c0e54c | 760 | wxString tmp( wxGetUserHome() ); |
223d09f6 | 761 | tmp += wxT('/'); |
c8c0e54c VZ |
762 | tmp += filename; |
763 | filename = tmp; | |
ed58dbea RR |
764 | } |
765 | ||
223d09f6 KB |
766 | if ((filename.Find(wxT('*')) != wxNOT_FOUND) || |
767 | (filename.Find(wxT('?')) != wxNOT_FOUND)) | |
cae5359f | 768 | { |
223d09f6 | 769 | if (filename.Find(wxT('/')) != wxNOT_FOUND) |
c8c0e54c VZ |
770 | { |
771 | wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR ); | |
772 | return; | |
773 | } | |
774 | m_list->SetWild( filename ); | |
775 | return; | |
cae5359f RR |
776 | } |
777 | ||
223d09f6 KB |
778 | if (dir != wxT("/")) dir += wxT("/"); |
779 | if (filename[0] != wxT('/')) | |
ed58dbea RR |
780 | { |
781 | dir += filename; | |
782 | filename = dir; | |
783 | } | |
c8c0e54c | 784 | |
8b17ba72 RR |
785 | if (wxDirExists(filename)) |
786 | { | |
787 | m_list->GoToDir( filename ); | |
9c884972 RR |
788 | m_list->GetDir( dir ); |
789 | m_static->SetLabel( dir ); | |
c8c0e54c | 790 | return; |
8b17ba72 | 791 | } |
c8c0e54c | 792 | |
8b17ba72 RR |
793 | if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) ) |
794 | { | |
795 | if (wxFileExists( filename )) | |
796 | { | |
797 | wxString msg; | |
798 | msg.Printf( _("File '%s' already exists, do you really want to " | |
799 | "overwrite it?"), filename.c_str() ); | |
800 | ||
801 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) | |
802 | return; | |
803 | } | |
804 | } | |
805 | else if ( (m_dialogStyle & wxOPEN) && (m_dialogStyle & wxFILE_MUST_EXIST) ) | |
806 | { | |
807 | if ( !wxFileExists( filename ) ) | |
808 | { | |
cae5359f | 809 | wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR ); |
8b17ba72 RR |
810 | return; |
811 | } | |
812 | } | |
813 | ||
814 | SetPath( filename ); | |
9b7e522a RR |
815 | |
816 | wxCommandEvent event; | |
817 | wxDialog::OnOK(event); | |
e1811a01 RR |
818 | } |
819 | ||
5e0201ea | 820 | void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) ) |
e1811a01 RR |
821 | { |
822 | HandleAction( m_text->GetValue() ); | |
8b17ba72 RR |
823 | } |
824 | ||
825 | void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) ) | |
826 | { | |
827 | m_list->ChangeToListMode(); | |
0b855868 | 828 | m_list->SetFocus(); |
8b17ba72 RR |
829 | } |
830 | ||
831 | void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) ) | |
832 | { | |
833 | m_list->ChangeToReportMode(); | |
0b855868 | 834 | m_list->SetFocus(); |
8b17ba72 RR |
835 | } |
836 | ||
837 | void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) ) | |
838 | { | |
839 | m_list->GoToParentDir(); | |
0b855868 | 840 | m_list->SetFocus(); |
9c884972 RR |
841 | wxString dir; |
842 | m_list->GetDir( dir ); | |
843 | m_static->SetLabel( dir ); | |
8b17ba72 RR |
844 | } |
845 | ||
846 | void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) ) | |
847 | { | |
848 | m_list->GoToHomeDir(); | |
0b855868 | 849 | m_list->SetFocus(); |
9c884972 RR |
850 | wxString dir; |
851 | m_list->GetDir( dir ); | |
852 | m_static->SetLabel( dir ); | |
0b855868 RR |
853 | } |
854 | ||
855 | void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) ) | |
856 | { | |
857 | m_list->MakeDir(); | |
8b17ba72 RR |
858 | } |
859 | ||
860 | void wxFileDialog::SetPath( const wxString& path ) | |
861 | { | |
862 | // not only set the full path but also update filename and dir | |
863 | m_path = path; | |
864 | if ( !!path ) | |
865 | { | |
866 | wxString ext; | |
867 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
c8c0e54c VZ |
868 | if (!ext.IsEmpty()) |
869 | { | |
223d09f6 | 870 | m_fileName += wxT("."); |
8b17ba72 | 871 | m_fileName += ext; |
c8c0e54c | 872 | } |
8b17ba72 RR |
873 | } |
874 | } | |
c8c0e54c | 875 | |
8b17ba72 RR |
876 | // ---------------------------------------------------------------------------- |
877 | // global functions | |
878 | // ---------------------------------------------------------------------------- | |
879 | ||
880 | wxString | |
881 | wxFileSelectorEx(const wxChar *message, | |
882 | const wxChar *default_path, | |
883 | const wxChar *default_filename, | |
5e0201ea | 884 | int *WXUNUSED(indexDefaultExtension), |
8b17ba72 RR |
885 | const wxChar *wildcard, |
886 | int flags, | |
887 | wxWindow *parent, | |
888 | int x, int y) | |
889 | { | |
890 | // TODO: implement this somehow | |
223d09f6 | 891 | return wxFileSelector(message, default_path, default_filename, wxT(""), |
8b17ba72 RR |
892 | wildcard, flags, parent, x, y); |
893 | } | |
894 | ||
895 | wxString wxFileSelector( const wxChar *title, | |
896 | const wxChar *defaultDir, const wxChar *defaultFileName, | |
897 | const wxChar *defaultExtension, const wxChar *filter, int flags, | |
898 | wxWindow *parent, int x, int y ) | |
899 | { | |
900 | wxString filter2; | |
901 | if ( defaultExtension && !filter ) | |
223d09f6 | 902 | filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ; |
8b17ba72 RR |
903 | else if ( filter ) |
904 | filter2 = filter; | |
905 | ||
906 | wxString defaultDirString; | |
907 | if (defaultDir) | |
908 | defaultDirString = defaultDir; | |
909 | ||
910 | wxString defaultFilenameString; | |
911 | if (defaultFileName) | |
912 | defaultFilenameString = defaultFileName; | |
913 | ||
914 | wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) ); | |
915 | ||
916 | if ( fileDialog.ShowModal() == wxID_OK ) | |
917 | { | |
918 | return fileDialog.GetPath(); | |
919 | } | |
920 | else | |
921 | { | |
922 | return wxEmptyString; | |
923 | } | |
924 | } | |
925 | ||
926 | wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent ) | |
927 | { | |
928 | wxChar *ext = (wxChar *)extension; | |
929 | ||
930 | wxChar prompt[50]; | |
931 | wxString str = _("Load %s file"); | |
932 | wxSprintf(prompt, str, what); | |
933 | ||
223d09f6 | 934 | if (*ext == wxT('.')) ext++; |
8b17ba72 | 935 | wxChar wild[60]; |
223d09f6 | 936 | wxSprintf(wild, wxT("*.%s"), ext); |
8b17ba72 RR |
937 | |
938 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
939 | } | |
940 | ||
941 | wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name, | |
942 | wxWindow *parent ) | |
943 | { | |
944 | wxChar *ext = (wxChar *)extension; | |
945 | ||
946 | wxChar prompt[50]; | |
947 | wxString str = _("Save %s file"); | |
948 | wxSprintf(prompt, str, what); | |
949 | ||
223d09f6 | 950 | if (*ext == wxT('.')) ext++; |
8b17ba72 | 951 | wxChar wild[60]; |
223d09f6 | 952 | wxSprintf(wild, wxT("*.%s"), ext); |
8b17ba72 RR |
953 | |
954 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); | |
955 | } | |
956 |