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