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