2 * \file FormFiledialog.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "FormFiledialog.h"
15 #include "forms/form_filedialog.h"
17 #include "forms_gettext.h"
18 #include "xforms_helpers.h"
20 #include "frontends/Dialogs.h"
22 #include "support/filefilterlist.h"
23 #include "support/filetools.h"
24 #include "support/globbing.h"
25 #include "support/lstrings.h"
26 #include "support/lyxlib.h"
27 #include "support/package.h"
29 #include "lyx_forms.h"
31 #include <boost/bind.hpp>
32 #include <boost/filesystem/operations.hpp>
33 #include <boost/tokenizer.hpp>
38 using lyx::support::AbsolutePath;
39 using lyx::support::AddName;
40 using lyx::support::ExpandPath;
41 using lyx::support::FileFilterList;
42 using lyx::support::getcwd;
43 using lyx::support::MakeAbsPath;
44 using lyx::support::OnlyFilename;
45 using lyx::support::package;
46 using lyx::support::split;
47 using lyx::support::suffixIs;
48 using lyx::support::trim;
52 using std::ostringstream;
56 using namespace lyx::frontend;
57 namespace fs = boost::filesystem;
61 /** Given a string "<glob> <glob> <glob>", expand each glob in turn.
62 * Any glob that cannot be expanded is ignored silently.
63 * Invokes \c convert_brace_glob and \c glob internally, so use only
64 * on systems supporting the Posix function 'glob'.
65 * \param mask the string "<glob> <glob> <glob>".
66 * \param directory the current working directory from
67 * which \c glob is invoked.
68 * \returns a vector of all matching file names.
70 vector<string> const expand_globs(string const & mask,
71 string const & directory)
73 // Split into individual globs and then call 'glob' on each one.
74 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
75 boost::char_separator<char> const separator(" ");
77 vector<string> matches;
78 Tokenizer const tokens(mask, separator);
79 Tokenizer::const_iterator it = tokens.begin();
80 Tokenizer::const_iterator const end = tokens.end();
81 for (; it != end; ++it)
82 lyx::support::glob(matches, *it, directory);
91 int C_LyXFileDlg_CancelCB(FL_FORM *fl, void *xev)
93 return FileDialog::Private::CancelCB(fl, xev);
97 void C_LyXFileDlg_DoubleClickCB(FL_OBJECT * ob, long data)
99 FileDialog::Private::DoubleClickCB(ob, data);
103 void C_LyXFileDlg_FileDlgCB(FL_OBJECT * ob, long data)
105 FileDialog::Private::FileDlgCB(ob, data);
111 // compares two LyXDirEntry objects content (used for sort)
112 class comp_direntry : public std::binary_function<DirEntry, DirEntry, bool> {
114 bool operator()(DirEntry const & r1, DirEntry const & r2) const
116 bool const r1d = suffixIs(r1.name_, '/');
117 bool const r2d = suffixIs(r2.name_, '/');
122 return r1.name_ < r2.name_;
130 // *** FileDialog::Private class implementation
133 FD_filedialog * FileDialog::Private::file_dlg_form_ = 0;
134 FileDialog::Private * FileDialog::Private::current_dlg_ = 0;
135 int FileDialog::Private::minw_ = 0;
136 int FileDialog::Private::minh_ = 0;
139 // Reread: updates dialog list to match class directory
140 void FileDialog::Private::Reread()
143 if (!fs::exists(directory_) || !fs::is_directory(directory_)) {
146 Alert::err_alert(_("Warning! Couldn't open directory."),
149 directory_ = getcwd();
152 // Clear the present namelist
153 dir_entries_.clear();
156 fl_hide_object(file_dlg_form_->List);
157 fl_clear_browser(file_dlg_form_->List);
158 fl_set_input(file_dlg_form_->DirBox, directory_.c_str());
160 // Splits complete directory name into directories and compute depth
164 string File = directory_;
166 File = split(File, Temp, '/');
168 while (!File.empty() || !Temp.empty()) {
169 string const dline = "@b" + line + Temp + '/';
170 fl_add_browser_line(file_dlg_form_->List, dline.c_str());
171 File = split(File, Temp, '/');
176 vector<string> const glob_matches = expand_globs(mask_, directory_);
178 fs::directory_iterator beg(directory_);
179 fs::directory_iterator end;
180 for (; beg != end; ++beg) {
181 string const fname = beg->leaf();
183 // If the pattern doesn't start with a dot, skip hidden files
184 if (!mask_.empty() && mask_[0] != '.' && fname[0] == '.')
187 bool const isDir = fs::is_directory(*beg);
189 // filters files according to pattern and type
190 typedef vector<string>::const_iterator viterator;
191 viterator gbegin = glob_matches.begin();
192 viterator const gend = glob_matches.end();
194 if (!isDir && std::find(gbegin, gend, fname) == gend)
204 // creates displayed name
205 tmp.displayed_ = fname;
206 dir_entries_.push_back(tmp);
210 sort(dir_entries_.begin(), dir_entries_.end(), comp_direntry());
212 // Add them to directory box
213 for (DirEntries::const_iterator cit = dir_entries_.begin();
214 cit != dir_entries_.end(); ++cit) {
215 string const temp = line + cit->displayed_;
216 fl_add_browser_line(file_dlg_form_->List, temp.c_str());
218 fl_set_browser_topline(file_dlg_form_->List, depth_);
219 fl_show_object(file_dlg_form_->List);
224 // SetDirectory: sets dialog current directory
225 void FileDialog::Private::SetDirectory(string const & path)
231 tmp = MakeAbsPath(ExpandPath(path), directory_);
233 // must check the directory exists
234 if (!fs::exists(tmp) || !fs::is_directory(tmp)) {
237 Alert::err_alert(_("Warning! Couldn't open directory."), tmp);
245 void FileDialog::Private::SetFilters(string const & mask)
247 SetFilters(FileFilterList(mask));
251 void FileDialog::Private::SetFilters(FileFilterList const & filters)
256 // Just take the first one for now.
257 typedef FileFilterList::Filter::glob_iterator glob_iterator;
258 glob_iterator const begin = filters[0].begin();
259 glob_iterator const end = filters[0].end();
264 for (glob_iterator it = begin; it != end; ++it) {
271 fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
275 FileDialog::Private::Private()
277 directory_ = MakeAbsPath(string("."));
279 // Creates form if necessary.
280 if (!file_dlg_form_) {
281 file_dlg_form_ = build_filedialog(this);
282 minw_ = file_dlg_form_->form->w;
283 minh_ = file_dlg_form_->form->h;
284 // Set callbacks. This means that we don't need a patch file
285 fl_set_object_callback(file_dlg_form_->DirBox,
286 C_LyXFileDlg_FileDlgCB, 0);
287 fl_set_object_callback(file_dlg_form_->PatBox,
288 C_LyXFileDlg_FileDlgCB, 1);
289 fl_set_object_callback(file_dlg_form_->List,
290 C_LyXFileDlg_FileDlgCB, 2);
291 fl_set_object_callback(file_dlg_form_->Filename,
292 C_LyXFileDlg_FileDlgCB, 3);
293 fl_set_object_callback(file_dlg_form_->Rescan,
294 C_LyXFileDlg_FileDlgCB, 10);
295 fl_set_object_callback(file_dlg_form_->Home,
296 C_LyXFileDlg_FileDlgCB, 11);
297 fl_set_object_callback(file_dlg_form_->User1,
298 C_LyXFileDlg_FileDlgCB, 12);
299 fl_set_object_callback(file_dlg_form_->User2,
300 C_LyXFileDlg_FileDlgCB, 13);
302 // Make sure pressing the close box doesn't crash LyX. (RvdK)
303 fl_set_form_atclose(file_dlg_form_->form,
304 C_LyXFileDlg_CancelCB, 0);
305 // Register doubleclick callback
306 fl_set_browser_dblclick_callback(file_dlg_form_->List,
307 C_LyXFileDlg_DoubleClickCB,
310 fl_hide_object(file_dlg_form_->User1);
311 fl_hide_object(file_dlg_form_->User2);
313 r_ = Dialogs::redrawGUI().connect(boost::bind(&FileDialog::Private::redraw, this));
317 FileDialog::Private::~Private()
323 void FileDialog::Private::redraw()
325 if (file_dlg_form_->form && file_dlg_form_->form->visible)
326 fl_redraw_form(file_dlg_form_->form);
330 // SetButton: sets file selector user button action
331 void FileDialog::Private::SetButton(int index, string const & name,
338 ob = file_dlg_form_->User1;
340 } else if (index == 1) {
341 ob = file_dlg_form_->User2;
348 fl_set_object_label(ob, idex(name).c_str());
349 fl_set_button_shortcut(ob, scex(name).c_str(), 1);
359 // GetDirectory: gets last dialog directory
360 string const FileDialog::Private::GetDirectory() const
362 if (!directory_.empty())
369 bool x_sync_kludge(bool ret)
371 XSync(fl_get_display(), false);
376 // RunDialog: handle dialog during file selection
377 bool FileDialog::Private::RunDialog()
379 force_cancel_ = false;
384 FL_OBJECT * ob = fl_do_forms();
386 if (ob == file_dlg_form_->Ready) {
388 return x_sync_kludge(true);
390 } else if (ob == file_dlg_form_->Cancel || force_cancel_)
391 return x_sync_kludge(false);
394 return x_sync_kludge(true);
399 // XForms objects callback (static)
400 void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
407 case 0: // get directory
408 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
409 current_dlg_->Reread();
413 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
414 current_dlg_->Reread();
418 current_dlg_->HandleListHit();
422 current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
423 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
424 current_dlg_->Reread();
428 current_dlg_->SetDirectory(package().home_dir());
429 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
430 current_dlg_->Reread();
433 case 12: // user button 1
434 current_dlg_->SetDirectory(current_dlg_->user_path1_);
435 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
436 current_dlg_->Reread();
439 case 13: // user button 2
440 current_dlg_->SetDirectory(current_dlg_->user_path2_);
441 current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
442 current_dlg_->Reread();
449 // Handle callback from list
450 void FileDialog::Private::HandleListHit()
453 int const select_ = fl_get_browser(file_dlg_form_->List);
454 string line = (select_ > depth_ ?
455 dir_entries_[select_ - depth_ - 1].name_ :
457 if (suffixIs(line, '/'))
459 fl_set_input(file_dlg_form_->Filename, line.c_str());
463 // Callback for double click in list
464 void FileDialog::Private::DoubleClickCB(FL_OBJECT *, long)
466 // Simulate click on OK button
467 if (current_dlg_->HandleDoubleClick())
468 current_dlg_->Force(false);
472 // Handle double click from list
473 bool FileDialog::Private::HandleDoubleClick()
479 int const select_ = fl_get_browser(file_dlg_form_->List);
480 if (select_ > depth_) {
481 tmp = dir_entries_[select_ - depth_ - 1].name_;
482 if (!suffixIs(tmp, '/')) {
484 fl_set_input(file_dlg_form_->Filename, tmp.c_str());
486 } else if (select_ == 0)
493 // builds new directory name
494 if (select_ > depth_) {
495 // Directory deeper down
496 // First, get directory with trailing /
497 Temp = fl_get_input(file_dlg_form_->DirBox);
498 if (!suffixIs(Temp, '/'))
502 // Directory higher up
504 for (int i = 0; i < select_; ++i) {
505 string const piece = fl_get_browser_line(file_dlg_form_->List, i + 1);
506 // The '+2' is here to count the '@b' (JMarc)
507 Temp += piece.substr(i + 2);
520 // Handle OK button call
521 bool FileDialog::Private::HandleOK()
524 string tmp = fl_get_input(file_dlg_form_->PatBox);
531 // directory was changed
532 tmp = fl_get_input(file_dlg_form_->DirBox);
533 if (tmp != directory_) {
539 // Handle return from list
540 int const select = fl_get_browser(file_dlg_form_->List);
541 if (select > depth_) {
542 string const temp = dir_entries_[select - depth_ - 1].name_;
543 if (!suffixIs(temp, '/')) {
544 // If user didn't type anything, use browser
545 string const name = fl_get_input(file_dlg_form_->Filename);
547 fl_set_input(file_dlg_form_->Filename, temp.c_str());
552 // Emulate a doubleclick
553 return HandleDoubleClick();
557 // Handle Cancel CB from WM close
558 int FileDialog::Private::CancelCB(FL_FORM *, void *)
560 // Simulate a click on the cancel button
561 current_dlg_->Force(true);
566 // Simulates a click on OK/Cancel
567 void FileDialog::Private::Force(bool cancel)
570 force_cancel_ = true;
571 fl_set_button(file_dlg_form_->Cancel, 1);
574 fl_set_button(file_dlg_form_->Ready, 1);
576 // Start timer to break fl_do_forms loop soon
577 fl_set_timer(file_dlg_form_->timer, 0.1);
581 // Select: launches dialog and returns selected file
582 string const FileDialog::Private::Select(string const & title,
584 FileFilterList const & filters,
585 string const & suggested)
587 // handles new mask and path
592 // highlight the suggested file in the browser, if it exists.
594 string const filename = OnlyFilename(suggested);
595 if (!filename.empty()) {
596 for (int i = 0; i < fl_get_browser_maxline(file_dlg_form_->List); ++i) {
597 string s = fl_get_browser_line(file_dlg_form_->List, i + 1);
607 fl_select_browser_line(file_dlg_form_->List, sel);
608 int const top = max(sel - 5, 1);
609 fl_set_browser_topline(file_dlg_form_->List, top);
611 // checks whether dialog can be started
617 setEnabled(file_dlg_form_->Filename, true);
618 fl_set_input(file_dlg_form_->Filename, suggested.c_str());
619 fl_set_button(file_dlg_form_->Cancel, 0);
620 fl_set_button(file_dlg_form_->Ready, 0);
621 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->Filename);
622 fl_deactivate_all_forms();
623 // Prevent xforms crashing if the dialog gets too small by preventing
624 // it from being shrunk beyond a minimum size.
625 // calls to fl_set_form_minsize/maxsize apply only to the next
626 // fl_show_form(), so this comes first.
627 fl_set_form_minsize(file_dlg_form_->form, minw_, minh_);
629 fl_show_form(file_dlg_form_->form,
630 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
633 bool const isOk = RunDialog();
635 fl_hide_form(file_dlg_form_->form);
636 fl_activate_all_forms();
639 // Returns filename or string() if no valid selection was made
640 if (!isOk || !fl_get_input(file_dlg_form_->Filename)[0])
643 file_name_ = fl_get_input(file_dlg_form_->Filename);
645 if (!AbsolutePath(file_name_))
646 file_name_ = AddName(fl_get_input(file_dlg_form_->DirBox), file_name_);
651 // SelectDir: launches dialog and returns selected directory
652 string const FileDialog::Private::SelectDir(string const & title,
654 string const & suggested)
660 // handle case where path does not end with "/"
661 // remerge path+suggested and check if it is a valid path
662 if (!suggested.empty()) {
663 string tmp = suggested;
664 if (!suffixIs(tmp, '/'))
666 string const full_path = path + tmp;
667 // check if this is really a directory
668 if (fs::exists(full_path)
669 && fs::is_directory(full_path))
670 SetDirectory(full_path);
680 // checks whether dialog can be started
686 fl_set_input(file_dlg_form_->Filename, "");
687 setEnabled(file_dlg_form_->Filename, false);
688 fl_set_button(file_dlg_form_->Cancel, 0);
689 fl_set_button(file_dlg_form_->Ready, 0);
690 fl_set_focus_object(file_dlg_form_->form, file_dlg_form_->DirBox);
691 fl_deactivate_all_forms();
692 fl_show_form(file_dlg_form_->form,
693 FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
698 fl_hide_form(file_dlg_form_->form);
699 fl_activate_all_forms();
702 // Returns directory or string() if no valid selection was made
706 file_name_ = fl_get_input(file_dlg_form_->DirBox);