]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormFiledialog.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / xforms / FormFiledialog.C
index 4c7b744a588ad41f1db73ece17729d95997b9f37..1c8aec402417cf13c4c52ed5313c3c84a7bed979 100644 (file)
@@ -20,6 +20,7 @@
 #include "frontends/Dialogs.h"
 
 #include "support/FileInfo.h"
+#include "support/filefilterlist.h"
 #include "support/filetools.h"
 #include "support/globbing.h"
 #include "support/lstrings.h"
 
 #include <boost/bind.hpp>
 #include <boost/regex.hpp>
+#include <boost/tokenizer.hpp>
 
 #include <algorithm>
 #include <map>
+#include <sstream>
+
 #include <grp.h>
 #include <pwd.h>
 
@@ -58,6 +62,7 @@
 using lyx::support::AbsolutePath;
 using lyx::support::AddName;
 using lyx::support::ExpandPath;
+using lyx::support::FileFilterList;
 using lyx::support::FileInfo;
 using lyx::support::getcwd;
 using lyx::support::GetEnvPath;
@@ -71,13 +76,42 @@ using lyx::support::trim;
 
 using std::max;
 using std::sort;
+using std::ostringstream;
 using std::string;
 using std::map;
 using std::vector;
 
+using namespace lyx::frontend;
 
 namespace {
 
+/** Given a string "<glob> <glob> <glob>", expand each glob in turn.
+ *  Any glob that cannot be expanded is ignored silently.
+ *  Invokes \c convert_brace_glob and \c glob internally, so use only
+ *  on systems supporting the Posix function 'glob'.
+ *  \param mask the string "<glob> <glob> <glob>".
+ *  \param directory the current working directory from
+ *  which \c glob is invoked.
+ *  \returns a vector of all matching file names.
+ */
+vector<string> const expand_globs(string const & mask,
+                                 string const & directory)
+{
+       // Split into individual globs and then call 'glob' on each one.
+       typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
+       boost::char_separator<char> const separator(" ");
+
+       vector<string> matches;
+       Tokenizer const tokens(mask, separator);
+       Tokenizer::const_iterator it = tokens.begin();
+       Tokenizer::const_iterator const end = tokens.end();
+       for (; it != end; ++it)
+               lyx::support::glob(matches, *it, directory);
+
+       return matches;
+}
+
+
 // six months, in seconds
 long const SIX_MONTH_SEC = 6L * 30L * 24L * 60L * 60L;
 //static
@@ -172,7 +206,7 @@ UserCache lyxUserCache;
 GroupCache lyxGroupCache;
 
 // compares two LyXDirEntry objects content (used for sort)
-class comp_direntry {
+class comp_direntry : public std::binary_function<DirEntry, DirEntry, bool> {
 public:
        bool operator()(DirEntry const & r1, DirEntry const & r2) const
        {
@@ -186,7 +220,6 @@ public:
        }
 };
 
-
 } // namespace anon
 
 
@@ -239,9 +272,8 @@ void FileDialog::Private::Reread()
                ++depth_;
        }
 
-       vector<string> const glob_matches =
-               lyx::support::expand_globs(mask_, directory_);
-       
+       vector<string> const glob_matches = expand_globs(mask_, directory_);
+
        time_t curTime = time(0);
        rewinddir(dir);
        while (dirent * entry = readdir(dir)) {
@@ -394,13 +426,32 @@ void FileDialog::Private::SetDirectory(string const & path)
 }
 
 
-// SetMask: sets dialog file mask
-void FileDialog::Private::SetMask(string const & newmask)
+void FileDialog::Private::SetFilters(string const & mask)
+{
+       SetFilters(FileFilterList(mask));
+}
+
+
+void FileDialog::Private::SetFilters(FileFilterList const & filters)
 {
-       mask_ = trim(newmask);
-       if (mask_.empty())
-               mask_ = "*";
+       if (filters.empty())
+               return;
 
+       // Just take the first one for now.
+       typedef FileFilterList::Filter::glob_iterator glob_iterator;
+       glob_iterator const begin = filters[0].begin();
+       glob_iterator const end = filters[0].end();
+       if (begin == end)
+               return;
+
+       ostringstream ss;
+       for (glob_iterator it = begin; it != end; ++it) {
+               if (it != begin)
+                       ss << ' ';
+               ss << *it;
+       }
+
+       mask_ = ss.str();
        fl_set_input(file_dlg_form_->PatBox, mask_.c_str());
 }
 
@@ -416,7 +467,6 @@ void FileDialog::Private::SetInfoLine(string const & line)
 FileDialog::Private::Private()
 {
        directory_ = MakeAbsPath(string("."));
-       mask_ = "*";
 
        // Creates form if necessary.
        if (!file_dlg_form_) {
@@ -552,7 +602,7 @@ void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
                break;
 
        case 1: // get mask
-               current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+               current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
                current_dlg_->Reread();
                break;
 
@@ -562,25 +612,25 @@ void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg)
 
        case 10: // rescan
                current_dlg_->SetDirectory(fl_get_input(file_dlg_form_->DirBox));
-               current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+               current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
                current_dlg_->Reread();
                break;
 
        case 11: // home
                current_dlg_->SetDirectory(GetEnvPath("HOME"));
-               current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+               current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
                current_dlg_->Reread();
                break;
 
        case 12: // user button 1
                current_dlg_->SetDirectory(current_dlg_->user_path1_);
-               current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+               current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
                current_dlg_->Reread();
                break;
 
        case 13: // user button 2
                current_dlg_->SetDirectory(current_dlg_->user_path2_);
-               current_dlg_->SetMask(fl_get_input(file_dlg_form_->PatBox));
+               current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox));
                current_dlg_->Reread();
                break;
 
@@ -666,7 +716,7 @@ bool FileDialog::Private::HandleOK()
        // mask was changed
        string tmp = fl_get_input(file_dlg_form_->PatBox);
        if (tmp != mask_) {
-               SetMask(tmp);
+               SetFilters(tmp);
                Reread();
                return false;
        }
@@ -724,21 +774,13 @@ void FileDialog::Private::Force(bool cancel)
 // Select: launches dialog and returns selected file
 string const FileDialog::Private::Select(string const & title,
                                         string const & path,
-                                        string const & mask,
+                                        FileFilterList const & filters,
                                         string const & suggested)
 {
        // handles new mask and path
-       bool isOk = true;
-       if (!mask.empty()) {
-               SetMask(mask);
-               isOk = false;
-       }
-       if (!path.empty()) {
-               SetDirectory(path);
-               isOk = false;
-       }
-       if (!isOk)
-               Reread();
+       SetFilters(filters);
+       SetDirectory(path);
+       Reread();
 
        // highlight the suggested file in the browser, if it exists.
        int sel = 0;
@@ -782,7 +824,7 @@ string const FileDialog::Private::Select(string const & title,
                     FL_PLACE_MOUSE | FL_FREE_SIZE, 0,
                     title.c_str());
 
-       isOk = RunDialog();
+       bool const isOk = RunDialog();
 
        fl_hide_form(file_dlg_form_->form);
        fl_activate_all_forms();
@@ -805,7 +847,7 @@ string const FileDialog::Private::SelectDir(string const & title,
                                         string const & path,
                                         string const & suggested)
 {
-       SetMask("*/");
+       SetFilters("*/");
        // handles new path
        bool isOk = true;
        if (!path.empty()) {