]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormFiledialog.C
Tiny clean-ups.
[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 getRegex(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         return boost::regex_match(a, reg);
221 }
222
223 } // namespace anon
224
225
226 // Reread: updates dialog list to match class directory
227 void FileDialog::Private::Reread()
228 {
229         // Opens directory
230         DIR * dir = ::opendir(directory_.c_str());
231         if (!dir) {
232 // FIXME: re-add ...
233 #if 0
234                 Alert::err_alert(_("Warning! Couldn't open directory."),
235                         directory_);
236 #endif
237                 directory_ = getcwd();
238                 dir = ::opendir(directory_.c_str());
239         }
240
241         // Clear the present namelist
242         dir_entries_.clear();
243
244         // Updates display
245         fl_hide_object(file_dlg_form_->List);
246         fl_clear_browser(file_dlg_form_->List);
247         fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
248
249         // Splits complete directory name into directories and compute depth
250         depth_ = 0;
251         string line, Temp;
252         string mode;
253         string File = directory_;
254         if (File != "/") {
255                 File = split(File, Temp, '/');
256         }
257         while (!File.empty() || !Temp.empty()) {
258                 string dline = "@b" + line + Temp + '/';
259                 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
260                 File = split(File, Temp, '/');
261                 line += ' ';
262                 ++depth_;
263         }
264
265         // Parses all entries of the given subdirectory
266         boost::regex reg = getRegex(mask_);
267
268         time_t curTime = time(0);
269         rewinddir(dir);
270         while (dirent * entry = readdir(dir)) {
271                 bool isLink = false, isDir = false;
272
273                 // If the pattern doesn't start with a dot, skip hidden files
274                 if (!mask_.empty() && mask_[0] != '.' &&
275                     entry->d_name[0] == '.')
276                         continue;
277
278                 // Gets filename
279                 string fname = entry->d_name;
280
281                 // Under all circumstances, "." and ".." are not wanted
282                 if (fname == "." || fname == "..")
283                         continue;
284
285                 // gets file status
286                 File = AddName(directory_, fname);
287
288                 FileInfo fileInfo(File, true);
289
290                 // can this really happen?
291                 if (!fileInfo.isOK())
292                         continue;
293
294                 mode = fileInfo.modeString();
295                 unsigned int const nlink = fileInfo.getNumberOfLinks();
296                 string const user  = lyxUserCache.find(fileInfo.getUid());
297                 string const group = lyxGroupCache.find(fileInfo.getGid());
298
299                 time_t modtime = fileInfo.getModificationTime();
300                 string Time = ctime(&modtime);
301
302                 if (curTime > modtime + SIX_MONTH_SEC
303                     || curTime < modtime + ONE_HOUR_SEC) {
304                         // The file is fairly old or in the future. POSIX says
305                         // the cutoff is 6 months old. Allow a 1 hour slop
306                         // factor for what is considered "the future", to
307                         // allow for NFS server/client clock disagreement.
308                         // Show the year instead of the time of day.
309                         Time.erase(10, 9);
310                         Time.erase(15, string::npos);
311                 } else {
312                         Time.erase(16, string::npos);
313                 }
314
315                 string buffer = mode + ' ' +
316                         tostr(nlink) + ' ' +
317                         user + ' ' +
318                         group + ' ' +
319                         Time.substr(4, string::npos) + ' ';
320
321                 buffer += entry->d_name;
322                 buffer += fileInfo.typeIndicator();
323
324                 isLink = fileInfo.isLink();
325                 if (isLink) {
326                         string Link;
327
328                         if (LyXReadLink(File, Link)) {
329                                 buffer += " -> ";
330                                 buffer += Link;
331
332                                 // This gives the FileType of the file that
333                                 // is really pointed too after resolving all
334                                 // symlinks. This is not necessarily the same
335                                 // as the type of Link (which could again be a
336                                 // link). Is that intended?
337                                 //                              JV 199902
338                                 fileInfo.newFile(File);
339                                 if (fileInfo.isOK())
340                                         buffer += fileInfo.typeIndicator();
341                                 else
342                                         continue;
343                         }
344                 }
345
346                 // filters files according to pattern and type
347                 if (fileInfo.isRegular()
348                     || fileInfo.isChar()
349                     || fileInfo.isBlock()
350                     || fileInfo.isFifo()) {
351                         if (!globMatch(fname, reg))
352                                 continue;
353                 } else if (!(isDir = fileInfo.isDir()))
354                         continue;
355
356                 DirEntry tmp;
357
358                 // Note ls_entry_ is an string!
359                 tmp.ls_entry_ = buffer;
360                 // creates used name
361                 string temp = fname;
362                 if (isDir)
363                         temp += '/';
364
365                 tmp.name_ = temp;
366                 // creates displayed name
367                 temp = entry->d_name;
368                 if (isLink)
369                         temp += '@';
370                 else
371                         temp += fileInfo.typeIndicator();
372                 tmp.displayed_ = temp;
373
374                 dir_entries_.push_back(tmp);
375         }
376
377         closedir(dir);
378
379         // Sort the names
380         sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
381
382         // Add them to directory box
383         for (DirEntries::const_iterator cit = dir_entries_.begin();
384              cit != dir_entries_.end(); ++cit) {
385                 string const temp = line + cit->displayed_;
386                 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
387         }
388         fl_set_browser_topline(file_dlg_form_->List, depth_);
389         fl_show_object(file_dlg_form_->List);
390         last_sel_ = -1;
391 }
392
393
394 // SetDirectory: sets dialog current directory
395 void FileDialog::Private::SetDirectory(string const & path)
396 {
397         string tmp;
398         if (path.empty())
399                 tmp = getcwd();
400         else
401                 tmp = MakeAbsPath(ExpandPath(path), directory_);
402
403         // must check the directory exists
404         DIR * dir = ::opendir(tmp.c_str());
405         if (!dir) {
406 // FIXME: re-add ...
407 #if 0
408                 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
409 #endif
410         } else {
411                 ::closedir(dir);
412                 directory_ = tmp;
413         }
414 }
415
416
417 // SetMask: sets dialog file mask
418 void FileDialog::Private::SetMask(string const & newmask)
419 {
420         mask_ = newmask;
421         fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
422 }
423
424
425 // SetInfoLine: sets dialog information line
426 void FileDialog::Private::SetInfoLine(string const & line)
427 {
428         info_line_ = line;
429         fl_set_object_label(file_dlg_form_->FileInfo, info_line_.c_str());
430 }
431
432
433 FileDialog::Private::Private()
434 {
435         directory_ = MakeAbsPath(string("."));
436         mask_ = '*';
437
438         // Creates form if necessary.
439         if (!file_dlg_form_) {
440                 file_dlg_form_ = build_filedialog(this);
441                 minw_ = file_dlg_form_->form->w;
442                 minh_ = file_dlg_form_->form->h;
443                 // Set callbacks. This means that we don't need a patch file
444                 fl_set_object_callback(file_dlg_form_->DirBox,
445                                        C_LyXFileDlg_FileDlgCB, 0);
446                 fl_set_object_callback(file_dlg_form_->PatBox,
447                                        C_LyXFileDlg_FileDlgCB, 1);
448                 fl_set_object_callback(file_dlg_form_->List,
449                                        C_LyXFileDlg_FileDlgCB, 2);
450                 fl_set_object_callback(file_dlg_form_->Filename,
451                                        C_LyXFileDlg_FileDlgCB, 3);
452                 fl_set_object_callback(file_dlg_form_->Rescan,
453                                        C_LyXFileDlg_FileDlgCB, 10);
454                 fl_set_object_callback(file_dlg_form_->Home,
455                                        C_LyXFileDlg_FileDlgCB, 11);
456                 fl_set_object_callback(file_dlg_form_->User1,
457                                        C_LyXFileDlg_FileDlgCB, 12);
458                 fl_set_object_callback(file_dlg_form_->User2,
459                                        C_LyXFileDlg_FileDlgCB, 13);
460
461                 // Make sure pressing the close box doesn't crash LyX. (RvdK)
462                 fl_set_form_atclose(file_dlg_form_->form,
463                                     C_LyXFileDlg_CancelCB, 0);
464                 // Register doubleclick callback
465                 fl_set_browser_dblclick_callback(file_dlg_form_->List,
466                                                  C_LyXFileDlg_DoubleClickCB,
467                                                  0);
468         }
469         fl_hide_object(file_dlg_form_->User1);
470         fl_hide_object(file_dlg_form_->User2);
471
472         r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
473 }
474
475
476 FileDialog::Private::~Private()
477 {
478         r_.disconnect();
479 }
480
481
482 void FileDialog::Private::redraw()
483 {
484         if (file_dlg_form_->form && file_dlg_form_->form->visible)
485                 fl_redraw_form(file_dlg_form_->form);
486 }
487
488
489 // SetButton: sets file selector user button action
490 void FileDialog::Private::SetButton(int index, string const & name,
491                            string const & path)
492 {
493         FL_OBJECT * ob;
494         string * tmp;
495
496         if (index == 0) {
497                 ob = file_dlg_form_->User1;
498                 tmp = &user_path1_;
499         } else if (index == 1) {
500                 ob = file_dlg_form_->User2;
501                 tmp = &user_path2_;
502         } else {
503                 return;
504         }
505
506         if (!name.empty()) {
507                 fl_set_object_label(ob, idex(name).c_str());
508                 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
509                 fl_show_object(ob);
510                 *tmp = path;
511         } else {
512                 fl_hide_object(ob);
513                 tmp->erase();
514         }
515 }
516
517
518 // GetDirectory: gets last dialog directory
519 string const FileDialog::Private::GetDirectory() const
520 {
521         if (!directory_.empty())
522                 return directory_;
523         else
524                 return string(".");
525 }
526
527 namespace {
528         bool x_sync_kludge(bool ret)
529         {
530                 XSync(fl_get_display(), false);
531                 return ret;
532         }
533 } // namespace anon
534
535 // RunDialog: handle dialog during file selection
536 bool FileDialog::Private::RunDialog()
537 {
538         force_cancel_ = false;
539         force_ok_ = false;
540
541         // event loop
542         while (true) {
543                 FL_OBJECT * ob = fl_do_forms();
544
545                 if (ob == file_dlg_form_->Ready) {
546                         if (HandleOK())
547                                 return x_sync_kludge(true);
548
549                 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
550                         return x_sync_kludge(false);
551
552                 else if (force_ok_)
553                         return x_sync_kludge(true);
554         }
555 }
556
557
558 // XForms objects callback (static)
559 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
560 {
561         if (!current_dlg_)
562                 return;
563
564         switch (arg) {
565
566         case 0: // get directory
567                 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
568                 current_dlg_->Reread();
569                 break;
570
571         case 1: // get mask
572                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
573                 current_dlg_->Reread();
574                 break;
575
576         case 2: // list
577                 current_dlg_->HandleListHit();
578                 break;
579
580         case 10: // rescan
581                 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
582                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
583                 current_dlg_->Reread();
584                 break;
585
586         case 11: // home
587                 current_dlg_->SetDirectory(GetEnvPath("HOME"));
588                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
589                 current_dlg_->Reread();
590                 break;
591
592         case 12: // user button 1
593                 current_dlg_->SetDirectory(current_dlg_->user_path1_);
594                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
595                 current_dlg_->Reread();
596                 break;
597
598         case 13: // user button 2
599                 current_dlg_->SetDirectory(current_dlg_->user_path2_);
600                 current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
601                 current_dlg_->Reread();
602                 break;
603
604         }
605 }
606
607
608 // Handle callback from list
609 void FileDialog::Private::HandleListHit()
610 {
611         // set info line
612         int const select_ = fl_get_browser(file_dlg_form_->List);
613         if (select_ > depth_)
614                 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
615         else
616                 SetInfoLine(string());
617 }
618
619
620 // Callback for double click in list
621 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
622 {
623         // Simulate click on OK button
624         if (current_dlg_->HandleDoubleClick())
625                 current_dlg_->Force(false);
626 }
627
628
629 // Handle double click from list
630 bool FileDialog::Private::HandleDoubleClick()
631 {
632         string tmp;
633
634         // set info line
635         bool isDir = true;
636         int const select_ = fl_get_browser(file_dlg_form_->List);
637         if (select_ > depth_) {
638                 tmp = dir_entries_[select_ - depth_ - 1].name_;
639                 SetInfoLine(dir_entries_[select_ - depth_ - 1].ls_entry_);
640                 if (!suffixIs(tmp, '/')) {
641                         isDir = false;
642                         fl_set_input(file_dlg_form_->Filename, tmp.c_str());
643                 }
644         } else if (select_ != 0) {
645                 SetInfoLine(string());
646         } else
647                 return true;
648
649         // executes action
650         if (isDir) {
651                 string Temp;
652
653                 // builds new directory name
654                 if (select_ > depth_) {
655                         // Directory deeper down
656                         // First, get directory with trailing /
657                         Temp = fl_get_input(file_dlg_form_->DirBox);
658                         if (!suffixIs(Temp, '/'))
659                                 Temp += '/';
660                         Temp += tmp;
661                 } else {
662                         // Directory higher up
663                         Temp.erase();
664                         for (int i = 0; i < select_; ++i) {
665                                 string piece = fl_get_browser_line(file_dlg_form_->List, i+1);
666                                 // The '+2' is here to count the '@b' (JMarc)
667                                 Temp += piece.substr(i + 2);
668                         }
669                 }
670
671                 // assigns it
672                 SetDirectory(Temp);
673                 Reread();
674                 return false;
675         }
676         return true;
677 }
678
679
680 // Handle OK button call
681 bool FileDialog::Private::HandleOK()
682 {
683         // mask was changed
684         string tmp = fl_get_input(file_dlg_form_->PatBox);
685         if (tmp != mask_) {
686                 SetMask(tmp);
687                 Reread();
688                 return false;
689         }
690
691         // directory was changed
692         tmp = fl_get_input(file_dlg_form_->DirBox);
693         if (tmp != directory_) {
694                 SetDirectory(tmp);
695                 Reread();
696                 return false;
697         }
698
699         // Handle return from list
700         int const select = fl_get_browser(file_dlg_form_->List);
701         if (select > depth_) {
702                 string const temp = dir_entries_[select - depth_ - 1].name_;
703                 if (!suffixIs(temp, '/')) {
704                         // If user didn't type anything, use browser
705                         string const name = fl_get_input(file_dlg_form_->Filename);
706                         if (name.empty())
707                                 fl_set_input(file_dlg_form_->Filename, temp.c_str());
708                         return true;
709                 }
710         }
711
712         // Emulate a doubleclick
713         return HandleDoubleClick();
714 }
715
716
717 // Handle Cancel CB from WM close
718 int FileDialog::Private::CancelCB(FL_FORM *, void *)
719 {
720         // Simulate a click on the cancel button
721         current_dlg_->Force(true);
722         return FL_IGNORE;
723 }
724
725
726 // Simulates a click on OK/Cancel
727 void FileDialog::Private::Force(bool cancel)
728 {
729         if (cancel) {
730                 force_cancel_ = true;
731                 fl_set_button(file_dlg_form_->Cancel, 1);
732         } else {
733                 force_ok_ = true;
734                 fl_set_button(file_dlg_form_->Ready, 1);
735         }
736         // Start timer to break fl_do_forms loop soon
737         fl_set_timer(file_dlg_form_->timer, 0.1);
738 }
739
740
741 // Select: launches dialog and returns selected file
742 string const FileDialog::Private::Select(string const & title,
743                                          string const & path,
744                                          string const & mask,
745                                          string const & suggested)
746 {
747         // handles new mask and path
748         bool isOk = true;
749         if (!mask.empty()) {
750                 SetMask(mask);
751                 isOk = false;
752         }
753         if (!path.empty()) {
754                 SetDirectory(path);
755                 isOk = false;
756         }
757         if (!isOk)
758                 Reread();
759
760         // highlight the suggested file in the browser, if it exists.
761         int sel = 0;
762         string const filename = OnlyFilename(suggested);
763         if (!filename.empty()) {
764                 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
765                         string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
766                         s = trim(s);
767                         if (s == filename) {
768                                 sel = i + 1;
769                                 break;
770                         }
771                 }
772         }
773
774         if (sel != 0)
775                 fl_select_browser_line(file_dlg_form_->List, sel);
776         int const top = max(sel - 5, 1);
777         fl_set_browser_topline(file_dlg_form_->List, top);
778
779         // checks whether dialog can be started
780         if (current_dlg_)
781                 return string();
782         current_dlg_ = this;
783
784         // runs dialog
785         SetInfoLine(string());
786         setEnabled(file_dlg_form_->Filename, true);
787         fl_set_input(file_dlg_form_->Filename, suggested.c_str());
788         fl_set_button(file_dlg_form_->Cancel, 0);
789         fl_set_button(file_dlg_form_->Ready, 0);
790         fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
791         fl_deactivate_all_forms();
792         // Prevent xforms crashing if the dialog gets too small by preventing
793         // it from being shrunk beyond a minimum size.
794         // calls to fl_set_form_minsize/maxsize apply only to the next
795         // fl_show_form(), so this comes first.
796         fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
797
798         fl_show_form(file_dlg_form_->form,
799                      FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
800                      title.c_str());
801
802         isOk = RunDialog();
803
804         fl_hide_form(file_dlg_form_->form);
805         fl_activate_all_forms();
806         current_dlg_ = 0;
807
808         // Returns filename or string() if no valid selection was made
809         if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
810                 return string();
811
812         file_name_ = fl_get_input(file_dlg_form_->Filename);
813
814         if (!AbsolutePath(file_name_))
815                 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
816         return file_name_;
817 }
818
819
820 // SelectDir: launches dialog and returns selected directory
821 string const FileDialog::Private::SelectDir(string const & title,
822                                          string const & path,
823                                          string const & suggested)
824 {
825         SetMask("*/");
826         // handles new path
827         bool isOk = true;
828         if (!path.empty()) {
829                 // handle case where path does not end with "/"
830                 // remerge path+suggested and check if it is a valid path
831                 if (!suggested.empty()) {
832                         string tmp = suggested;
833                         if (!suffixIs(tmp, '/'))
834                                 tmp += '/';
835                         string full_path = path;
836                         full_path += tmp;
837                         // check if this is really a directory
838                         DIR * dir = ::opendir(full_path.c_str());
839                         if (dir)
840                                 SetDirectory(full_path);
841                         else
842                                 SetDirectory(path);
843                 } else
844                         SetDirectory(path);
845                 isOk = false;
846         }
847         if (!isOk)
848                 Reread();
849
850         // checks whether dialog can be started
851         if (current_dlg_)
852                 return string();
853         current_dlg_ = this;
854
855         // runs dialog
856         SetInfoLine(string());
857         fl_set_input(file_dlg_form_->Filename, "");
858         setEnabled(file_dlg_form_->Filename, false);
859         fl_set_button(file_dlg_form_->Cancel, 0);
860         fl_set_button(file_dlg_form_->Ready, 0);
861         fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
862         fl_deactivate_all_forms();
863         fl_show_form(file_dlg_form_->form,
864                      FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
865                      title.c_str());
866
867         isOk = RunDialog();
868
869         fl_hide_form(file_dlg_form_->form);
870         fl_activate_all_forms();
871         current_dlg_ = 0;
872
873         // Returns directory or string() if no valid selection was made
874         if (!isOk)
875                 return string();
876
877         file_name_ = fl_get_input(file_dlg_form_->DirBox);
878         return file_name_;
879 }