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