]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormFiledialog.C
Prevent crash in the file browser if the file mask is an invalid regex.
[lyx.git] / src / frontends / xforms / FormFiledialog.C
1 /**
2  * \file FormFiledialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "FormFiledialog.h"
15 #include "forms/form_filedialog.h"
16
17 #include "forms_gettext.h"
18 #include "xforms_helpers.h"
19
20 #include "frontends/Dialogs.h"
21
22 #include "support/FileInfo.h"
23 #include "support/lyxlib.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
26 #include "support/filetools.h"
27
28 #include "lyx_forms.h"
29
30 #include <boost/bind.hpp>
31 #include <boost/regex.hpp>
32
33 #include <algorithm>
34 #include <map>
35 #include <grp.h>
36 #include <pwd.h>
37
38 //#ifdef HAVE_ERRNO_H
39 //#include <cerrno>
40 //#endif
41
42 #if HAVE_DIRENT_H
43 # include <dirent.h>
44 #else
45 # define dirent direct
46 # if HAVE_SYS_NDIR_H
47 #  include <sys/ndir.h>
48 # endif
49 # if HAVE_SYS_DIR_H
50 #  include <sys/dir.h>
51 # endif
52 # if HAVE_NDIR_H
53 #  include <ndir.h>
54 # endif
55 #endif
56
57 using lyx::support::AbsolutePath;
58 using lyx::support::AddName;
59 using lyx::support::ExpandPath;
60 using lyx::support::FileInfo;
61 using lyx::support::getcwd;
62 using lyx::support::GetEnvPath;
63 using lyx::support::LyXReadLink;
64 using lyx::support::MakeAbsPath;
65 using lyx::support::OnlyFilename;
66 using lyx::support::split;
67 using lyx::support::subst;
68 using lyx::support::suffixIs;
69 using lyx::support::trim;
70
71 using std::max;
72 using std::sort;
73 using std::string;
74 using std::map;
75
76
77 namespace {
78
79 // six months, in seconds
80 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
81 //static
82 long const ONE_HOUR_SEC = 60L * 60L;
83
84 extern "C" {
85
86         static
87         int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
88         {
89                 return FileDialog::Private::CancelCB(fl, xev);
90         }
91
92         static
93         void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
94         {
95                 FileDialog::Private::DoubleClickCB(ob, data);
96         }
97
98         static
99         void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
100         {
101                 FileDialog::Private::FileDlgCB(ob, data);
102         }
103
104 }
105
106 // *** User cache class implementation
107 /// User cache class definition
108 class UserCache {
109 public:
110         /// seeks user name from group ID
111         string const & find(uid_t ID) const {
112                 Users::const_iterator cit = users.find(ID);
113                 if (cit == users.end()) {
114                         add(ID);
115                         return users[ID];
116                 }
117                 return cit->second;
118         }
119 private:
120         ///
121         void add(uid_t ID) const;
122         ///
123         typedef map<uid_t, string> Users;
124         ///
125         mutable Users users;
126 };
127
128
129 void UserCache::add(uid_t ID) const
130 {
131         struct passwd const * entry = getpwuid(ID);
132         users[ID] = entry ? entry->pw_name : tostr(int(ID));
133 }
134
135
136 /// Group cache class definition
137 class GroupCache {
138 public:
139         /// seeks group name from group ID
140         string const & find(gid_t ID) const ;
141 private:
142         ///
143         void add(gid_t ID) const;
144         ///
145         typedef map<gid_t, string> Groups;
146         ///
147         mutable Groups groups;
148 };
149
150
151 string const & GroupCache::find(gid_t ID) const
152 {
153         Groups::const_iterator cit = groups.find(ID);
154         if (cit == groups.end()) {
155                 add(ID);
156                 return groups[ID];
157         }
158         return cit->second;
159 }
160
161
162 void GroupCache::add(gid_t ID) const
163 {
164         struct group const * entry = getgrgid(ID);
165         groups[ID] = entry ? entry->gr_name : tostr(int(ID));
166 }
167
168 // local instances
169 UserCache lyxUserCache;
170 GroupCache lyxGroupCache;
171
172 // compares two LyXDirEntry objects content (used for sort)
173 class comp_direntry {
174 public:
175         bool operator()(DirEntry const & r1, DirEntry const & r2) const
176         {
177                 bool const r1d = suffixIs(r1.name_, '/');
178                 bool const r2d = suffixIs(r2.name_, '/');
179                 if (r1d && !r2d)
180                         return true;
181                 if (!r1d && r2d)
182                         return false;
183                 return r1.name_ < r2.name_;
184         }
185 };
186
187
188 } // namespace anon
189
190
191
192 // *** FileDialog::Private class implementation
193
194 // static members
195 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
196 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
197 int FileDialog::Private::minw_ = 0;
198 int FileDialog::Private::minh_ = 0;
199
200
201 namespace {
202
203 boost::regex glob2regex(string const & pat)
204 {
205         // We massage the pattern a bit so that the usual
206         // shell pattern we all are used to will work.
207         // One nice thing about using a real regex is that
208         // things like "*.*[^~]" will work also.
209         // build the regex string.
210         string pattern = subst(pat, ".", "\\.");
211         pattern = subst(pattern, "*", ".*");
212
213         boost::regex reg(pattern);
214         return reg;
215 }
216
217
218 bool globMatch(string const & a, boost::regex const & reg)
219 {
220         // If the glob is invalid then match everything.
221         return reg.empty() ? true : boost::regex_match(a, reg);
222 }
223
224 } // namespace anon
225
226
227 // Reread: updates dialog list to match class directory
228 void FileDialog::Private::Reread()
229 {
230         // Opens directory
231         DIR * dir = ::opendir(directory_.c_str());
232         if (!dir) {
233 // FIXME: re-add ...
234 #if 0
235                 Alert::err_alert(_("Warning! Couldn't open directory."),
236                         directory_);
237 #endif
238                 directory_ = getcwd();
239                 dir = ::opendir(directory_.c_str());
240         }
241
242         // Clear the present namelist
243         dir_entries_.clear();
244
245         // Updates display
246         fl_hide_object(file_dlg_form_->List);
247         fl_clear_browser(file_dlg_form_->List);
248         fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
249
250         // Splits complete directory name into directories and compute depth
251         depth_ = 0;
252         string line, Temp;
253         string mode;
254         string File = directory_;
255         if (File != "/")
256                 File = split(File, Temp, '/');
257
258         while (!File.empty() || !Temp.empty()) {
259                 string dline = "@b" + line + Temp + '/';
260                 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
261                 File = split(File, Temp, '/');
262                 line += ' ';
263                 ++depth_;
264         }
265
266         // Parses all entries of the given subdirectory
267         boost::regex const mask_regex = glob2regex(mask_);
268
269         time_t curTime = time(0);
270         rewinddir(dir);
271         while (dirent * entry = readdir(dir)) {
272                 bool isLink = false, isDir = false;
273
274                 // If the pattern doesn't start with a dot, skip hidden files
275                 if (!mask_.empty() && mask_[0] != '.' &&
276                     entry->d_name[0] == '.')
277                         continue;
278
279                 // Gets filename
280                 string fname = entry->d_name;
281
282                 // Under all circumstances, "." and ".." are not wanted
283                 if (fname == "." || fname == "..")
284                         continue;
285
286                 // gets file status
287                 File = AddName(directory_, fname);
288
289                 FileInfo fileInfo(File, true);
290
291                 // can this really happen?
292                 if (!fileInfo.isOK())
293                         continue;
294
295                 mode = fileInfo.modeString();
296                 unsigned int const nlink = fileInfo.getNumberOfLinks();
297                 string const user  = lyxUserCache.find(fileInfo.getUid());
298                 string const group = lyxGroupCache.find(fileInfo.getGid());
299
300                 time_t modtime = fileInfo.getModificationTime();
301                 string Time = ctime(&modtime);
302
303                 if (curTime > modtime + SIX_MONTH_SEC
304                     || curTime < modtime + ONE_HOUR_SEC) {
305                         // The file is fairly old or in the future. POSIX says
306                         // the cutoff is 6 months old. Allow a 1 hour slop
307                         // factor for what is considered "the future", to
308                         // allow for NFS server/client clock disagreement.
309                         // Show the year instead of the time of day.
310                         Time.erase(10, 9);
311                         Time.erase(15, string::npos);
312                 } else {
313                         Time.erase(16, string::npos);
314                 }
315
316                 string buffer = mode + ' ' +
317                         tostr(nlink) + ' ' +
318                         user + ' ' +
319                         group + ' ' +
320                         Time.substr(4, string::npos) + ' ';
321
322                 buffer += entry->d_name;
323                 buffer += fileInfo.typeIndicator();
324
325                 isLink = fileInfo.isLink();
326                 if (isLink) {
327                         string Link;
328
329                         if (LyXReadLink(File, Link)) {
330                                 buffer += " -> ";
331                                 buffer += Link;
332
333                                 // This gives the FileType of the file that
334                                 // is really pointed too after resolving all
335                                 // symlinks. This is not necessarily the same
336                                 // as the type of Link (which could again be a
337                                 // link). Is that intended?
338                                 //                              JV 199902
339                                 fileInfo.newFile(File);
340                                 if (fileInfo.isOK())
341                                         buffer += fileInfo.typeIndicator();
342                                 else
343                                         continue;
344                         }
345                 }
346
347                 // filters files according to pattern and type
348                 if (fileInfo.isRegular()
349                     || fileInfo.isChar()
350                     || fileInfo.isBlock()
351                     || fileInfo.isFifo()) {
352                         if (!globMatch(fname, mask_regex))
353                                 continue;
354                 } else if (!(isDir = fileInfo.isDir()))
355                         continue;
356
357                 DirEntry tmp;
358
359                 // Note ls_entry_ is an string!
360                 tmp.ls_entry_ = buffer;
361                 // creates used name
362                 string temp = fname;
363                 if (isDir)
364                         temp += '/';
365
366                 tmp.name_ = temp;
367                 // creates displayed name
368                 temp = entry->d_name;
369                 if (isLink)
370                         temp += '@';
371                 else
372                         temp += fileInfo.typeIndicator();
373                 tmp.displayed_ = temp;
374
375                 dir_entries_.push_back(tmp);
376         }
377
378         closedir(dir);
379
380         // Sort the names
381         sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
382
383         // Add them to directory box
384         for (DirEntries::const_iterator cit = dir_entries_.begin();
385              cit != dir_entries_.end(); ++cit) {
386                 string const temp = line + cit->displayed_;
387                 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
388         }
389         fl_set_browser_topline(file_dlg_form_->List, depth_);
390         fl_show_object(file_dlg_form_->List);
391         last_sel_ = -1;
392 }
393
394
395 // SetDirectory: sets dialog current directory
396 void FileDialog::Private::SetDirectory(string const & path)
397 {
398         string tmp;
399         if (path.empty())
400                 tmp = getcwd();
401         else
402                 tmp = MakeAbsPath(ExpandPath(path), directory_);
403
404         // must check the directory exists
405         DIR * dir = ::opendir(tmp.c_str());
406         if (!dir) {
407 // FIXME: re-add ...
408 #if 0
409                 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
410 #endif
411         } else {
412                 ::closedir(dir);
413                 directory_ = tmp;
414         }
415 }
416
417
418 // SetMask: sets dialog file mask
419 void FileDialog::Private::SetMask(string const & newmask)
420 {
421         mask_ = trim(newmask);
422         if (mask_.empty())
423                 mask_ = "*";
424
425         fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
426 }
427
428
429 // SetInfoLine: sets dialog information line
430 void FileDialog::Private::SetInfoLine(string const & line)
431 {
432         info_line_ = line;
433         fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
434 }
435
436
437 FileDialog::Private::Private()
438 {
439         directory_ = MakeAbsPath(string("."));
440         mask_ = "*";
441
442         // Creates form if necessary.
443         if (!file_dlg_form_) {
444                 file_dlg_form_ = build_filedialog(this);
445                 minw_ = file_dlg_form_->form->w;
446                 minh_ = file_dlg_form_->form->h;
447                 // Set callbacks. This means that we don't need a patch file
448                 fl_set_object_callback(file_dlg_form_->DirBox,
449                                        C_LyXFileDlg_FileDlgCB, 0);
450                 fl_set_object_callback(file_dlg_form_->PatBox,
451                                        C_LyXFileDlg_FileDlgCB, 1);
452                 fl_set_object_callback(file_dlg_form_->List,
453                                        C_LyXFileDlg_FileDlgCB, 2);
454                 fl_set_object_callback(file_dlg_form_->Filename,
455                                        C_LyXFileDlg_FileDlgCB, 3);
456                 fl_set_object_callback(file_dlg_form_->Rescan,
457                                        C_LyXFileDlg_FileDlgCB, 10);
458                 fl_set_object_callback(file_dlg_form_->Home,
459                                        C_LyXFileDlg_FileDlgCB, 11);
460                 fl_set_object_callback(file_dlg_form_->User1,
461                                        C_LyXFileDlg_FileDlgCB, 12);
462                 fl_set_object_callback(file_dlg_form_->User2,
463                                        C_LyXFileDlg_FileDlgCB, 13);
464
465                 // Make sure pressing the close box doesn't crash LyX. (RvdK)
466                 fl_set_form_atclose(file_dlg_form_->form,
467                                     C_LyXFileDlg_CancelCB, 0);
468                 // Register doubleclick callback
469                 fl_set_browser_dblclick_callback(file_dlg_form_->List,
470                                                  C_LyXFileDlg_DoubleClickCB,
471                                                  0);
472         }
473         fl_hide_object(file_dlg_form_->User1);
474         fl_hide_object(file_dlg_form_->User2);
475
476         r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
477 }
478
479
480 FileDialog::Private::~Private()
481 {
482         r_.disconnect();
483 }
484
485
486 void FileDialog::Private::redraw()
487 {
488         if (file_dlg_form_->form && file_dlg_form_->form->visible)
489                 fl_redraw_form(file_dlg_form_->form);
490 }
491
492
493 // SetButton: sets file selector user button action
494 void FileDialog::Private::SetButton(int index, string const & name,
495                            string const & path)
496 {
497         FL_OBJECT * ob;
498         string * tmp;
499
500         if (index == 0) {
501                 ob = file_dlg_form_->User1;
502                 tmp = &user_path1_;
503         } else if (index == 1) {
504                 ob = file_dlg_form_->User2;
505                 tmp = &user_path2_;
506         } else {
507                 return;
508         }
509
510         if (!name.empty()) {
511                 fl_set_object_label(ob, idex(name).c_str());
512                 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
513                 fl_show_object(ob);
514                 *tmp = path;
515         } else {
516                 fl_hide_object(ob);
517                 tmp->erase();
518         }
519 }
520
521
522 // GetDirectory: gets last dialog directory
523 string const FileDialog::Private::GetDirectory() const
524 {
525         if (!directory_.empty())
526                 return directory_;
527         else
528                 return string(".");
529 }
530
531 namespace {
532         bool x_sync_kludge(bool ret)
533         {
534                 XSync(fl_get_display(), false);
535                 return ret;
536         }
537 } // namespace anon
538
539 // RunDialog: handle dialog during file selection
540 bool FileDialog::Private::RunDialog()
541 {
542         force_cancel_ = false;
543         force_ok_ = false;
544
545         // event loop
546         while (true) {
547                 FL_OBJECT * ob = fl_do_forms();
548
549                 if (ob == file_dlg_form_->Ready) {
550                         if (HandleOK())
551                                 return x_sync_kludge(true);
552
553                 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
554                         return x_sync_kludge(false);
555
556                 else if (force_ok_)
557                         return x_sync_kludge(true);
558         }
559 }
560
561
562 // XForms objects callback (static)
563 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
564 {
565         if (!current_dlg_)
566                 return;
567
568         switch (arg) {
569
570         case 0: // get directory
571                 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
572                 current_dlg_->Reread();
573                 break;
574
575         case 1: // get mask
576                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
577                 current_dlg_->Reread();
578                 break;
579
580         case 2: // list
581                 current_dlg_->HandleListHit();
582                 break;
583
584         case 10: // rescan
585                 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
586                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
587                 current_dlg_->Reread();
588                 break;
589
590         case 11: // home
591                 current_dlg_->SetDirectory(GetEnvPath("HOME"));
592                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
593                 current_dlg_->Reread();
594                 break;
595
596         case 12: // user button 1
597                 current_dlg_->SetDirectory(current_dlg_->user_path1_);
598                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
599                 current_dlg_->Reread();
600                 break;
601
602         case 13: // user button 2
603                 current_dlg_->SetDirectory(current_dlg_->user_path2_);
604                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
605                 current_dlg_->Reread();
606                 break;
607
608         }
609 }
610
611
612 // Handle callback from list
613 void FileDialog::Private::HandleListHit()
614 {
615         // set info line
616         int const select_ = fl_get_browser(file_dlg_form_->List);
617         if (select_ > depth_)
618                 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
619         else
620                 SetInfoLine(string());
621 }
622
623
624 // Callback for double click in list
625 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
626 {
627         // Simulate click on OK button
628         if (current_dlg_->HandleDoubleClick())
629                 current_dlg_->Force(false);
630 }
631
632
633 // Handle double click from list
634 bool FileDialog::Private::HandleDoubleClick()
635 {
636         string tmp;
637
638         // set info line
639         bool isDir = true;
640         int const select_ = fl_get_browser(file_dlg_form_->List);
641         if (select_ > depth_) {
642                 tmp = dir_entries_[select_ - depth_ - 1].name_;
643                 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
644                 if (!suffixIs(tmp, '/')) {
645                         isDir = false;
646                         fl_set_input(file_dlg_form_->Filename, tmp.c_str());
647                 }
648         } else if (select_ != 0) {
649                 SetInfoLine(string());
650         } else
651                 return true;
652
653         // executes action
654         if (isDir) {
655                 string Temp;
656
657                 // builds new directory name
658                 if (select_ > depth_) {
659                         // Directory deeper down
660                         // First, get directory with trailing /
661                         Temp = fl_get_input(file_dlg_form_->DirBox);
662                         if (!suffixIs(Temp, '/'))
663                                 Temp += '/';
664                         Temp += tmp;
665                 } else {
666                         // Directory higher up
667                         Temp.erase();
668                         for (int i = 0; i < select_; ++i) {
669                                 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
670                                 // The '+2' is here to count the '@b' (JMarc)
671                                 Temp += piece.substr(i + 2);
672                         }
673                 }
674
675                 // assigns it
676                 SetDirectory(Temp);
677                 Reread();
678                 return false;
679         }
680         return true;
681 }
682
683
684 // Handle OK button call
685 bool FileDialog::Private::HandleOK()
686 {
687         // mask was changed
688         string tmp = fl_get_input(file_dlg_form_->PatBox);
689         if (tmp != mask_) {
690                 SetMask(tmp);
691                 Reread();
692                 return false;
693         }
694
695         // directory was changed
696         tmp = fl_get_input(file_dlg_form_->DirBox);
697         if (tmp != directory_) {
698                 SetDirectory(tmp);
699                 Reread();
700                 return false;
701         }
702
703         // Handle return from list
704         int const select = fl_get_browser(file_dlg_form_->List);
705         if (select > depth_) {
706                 string const temp = dir_entries_[select - depth_ - 1].name_;
707                 if (!suffixIs(temp, '/')) {
708                         // If user didn't type anything, use browser
709                         string const name = fl_get_input(file_dlg_form_->Filename);
710                         if (name.empty())
711                                 fl_set_input(file_dlg_form_->Filename, temp.c_str());
712                         return true;
713                 }
714         }
715
716         // Emulate a doubleclick
717         return HandleDoubleClick();
718 }
719
720
721 // Handle Cancel CB from WM close
722 int FileDialog::Private::CancelCB(FL_FORM *, void *)
723 {
724         // Simulate a click on the cancel button
725         current_dlg_->Force(true);
726         return FL_IGNORE;
727 }
728
729
730 // Simulates a click on OK/Cancel
731 void FileDialog::Private::Force(bool cancel)
732 {
733         if (cancel) {
734                 force_cancel_ = true;
735                 fl_set_button(file_dlg_form_->Cancel, 1);
736         } else {
737                 force_ok_ = true;
738                 fl_set_button(file_dlg_form_->Ready, 1);
739         }
740         // Start timer to break fl_do_forms loop soon
741         fl_set_timer(file_dlg_form_->timer, 0.1);
742 }
743
744
745 // Select: launches dialog and returns selected file
746 string const FileDialog::Private::Select(string const & title,
747                                          string const & path,
748                                          string const & mask,
749                                          string const & suggested)
750 {
751         // handles new mask and path
752         bool isOk = true;
753         if (!mask.empty()) {
754                 SetMask(mask);
755                 isOk = false;
756         }
757         if (!path.empty()) {
758                 SetDirectory(path);
759                 isOk = false;
760         }
761         if (!isOk)
762                 Reread();
763
764         // highlight the suggested file in the browser, if it exists.
765         int sel = 0;
766         string const filename = OnlyFilename(suggested);
767         if (!filename.empty()) {
768                 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
769                         string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
770                         s = trim(s);
771                         if (s == filename) {
772                                 sel = i + 1;
773                                 break;
774                         }
775                 }
776         }
777
778         if (sel != 0)
779                 fl_select_browser_line(file_dlg_form_->List, sel);
780         int const top = max(sel - 5, 1);
781         fl_set_browser_topline(file_dlg_form_->List, top);
782
783         // checks whether dialog can be started
784         if (current_dlg_)
785                 return string();
786         current_dlg_ = this;
787
788         // runs dialog
789         SetInfoLine(string());
790         setEnabled(file_dlg_form_->Filename, true);
791         fl_set_input(file_dlg_form_->Filename, suggested.c_str());
792         fl_set_button(file_dlg_form_->Cancel, 0);
793         fl_set_button(file_dlg_form_->Ready, 0);
794         fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
795         fl_deactivate_all_forms();
796         // Prevent xforms crashing if the dialog gets too small by preventing
797         // it from being shrunk beyond a minimum size.
798         // calls to fl_set_form_minsize/maxsize apply only to the next
799         // fl_show_form(), so this comes first.
800         fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
801
802         fl_show_form(file_dlg_form_->form,
803                      FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
804                      title.c_str());
805
806         isOk = RunDialog();
807
808         fl_hide_form(file_dlg_form_->form);
809         fl_activate_all_forms();
810         current_dlg_ = 0;
811
812         // Returns filename or string() if no valid selection was made
813         if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
814                 return string();
815
816         file_name_ = fl_get_input(file_dlg_form_->Filename);
817
818         if (!AbsolutePath(file_name_))
819                 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
820         return file_name_;
821 }
822
823
824 // SelectDir: launches dialog and returns selected directory
825 string const FileDialog::Private::SelectDir(string const & title,
826                                          string const & path,
827                                          string const & suggested)
828 {
829         SetMask("*/");
830         // handles new path
831         bool isOk = true;
832         if (!path.empty()) {
833                 // handle case where path does not end with "/"
834                 // remerge path+suggested and check if it is a valid path
835                 if (!suggested.empty()) {
836                         string tmp = suggested;
837                         if (!suffixIs(tmp, '/'))
838                                 tmp += '/';
839                         string full_path = path;
840                         full_path += tmp;
841                         // check if this is really a directory
842                         DIR * dir = ::opendir(full_path.c_str());
843                         if (dir)
844                                 SetDirectory(full_path);
845                         else
846                                 SetDirectory(path);
847                 } else
848                         SetDirectory(path);
849                 isOk = false;
850         }
851         if (!isOk)
852                 Reread();
853
854         // checks whether dialog can be started
855         if (current_dlg_)
856                 return string();
857         current_dlg_ = this;
858
859         // runs dialog
860         SetInfoLine(string());
861         fl_set_input(file_dlg_form_->Filename, "");
862         setEnabled(file_dlg_form_->Filename, false);
863         fl_set_button(file_dlg_form_->Cancel, 0);
864         fl_set_button(file_dlg_form_->Ready, 0);
865         fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
866         fl_deactivate_all_forms();
867         fl_show_form(file_dlg_form_->form,
868                      FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
869                      title.c_str());
870
871         isOk = RunDialog();
872
873         fl_hide_form(file_dlg_form_->form);
874         fl_activate_all_forms();
875         current_dlg_ = 0;
876
877         // Returns directory or string() if no valid selection was made
878         if (!isOk)
879                 return string();
880
881         file_name_ = fl_get_input(file_dlg_form_->DirBox);
882         return file_name_;
883 }