]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/helper_funcs.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / helper_funcs.C
index 34ac0f1207a9a156061d489d7cd1f5c9a26cc61d..ddf9ce9dff8bbcbe7c4ec2c5978a708dcbad2e9f 100644 (file)
@@ -3,32 +3,50 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
+ * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
-#include <vector>
-
 
 #include <config.h>
-#include "LString.h"
+
 #include "helper_funcs.h"
 
+#include "gettext.h"
+#include "lyxlength.h"
+
 #include "frontends/FileDialog.h"
-#include "support/filetools.h" // OnlyPath, OnlyFilename
+
+#include "support/filetools.h"
 #include "support/lstrings.h"
-#include "gettext.h" // _()
-#include "frontends/Alert.h"
+#include "support/package.h"
 
 using std::pair;
 using std::vector;
-using std::make_pair;
+using std::string;
+
+namespace lyx {
+
+using support::AddName;
+using support::ChangeExtension;
+using support::FileFilterList;
+using support::GetExtension;
+using support::LibFileSearch;
+using support::MakeAbsPath;
+using support::MakeRelPath;
+using support::OnlyFilename;
+using support::OnlyPath;
+using support::package;
+using support::prefixIs;
+
+namespace frontend {
 
 
 string const browseFile(string const & filename,
                        string const & title,
-                       string const & pattern,
+                       FileFilterList const & filters,
                        bool save,
                        pair<string,string> const & dir1,
                        pair<string,string> const & dir2)
@@ -41,42 +59,28 @@ string const browseFile(string const & filename,
 
        FileDialog::Result result;
 
-       while (true) {
-               if (save)
-                       result = fileDlg.save(lastPath, pattern,
-                               OnlyFilename(filename));
-               else
-                       result = fileDlg.open(lastPath, pattern,
-                               OnlyFilename(filename));
-
-               if (result.second.empty())
-                       return result.second;
-
-               lastPath = OnlyPath(result.second);
-
-               if (result.second.find_first_of("#~$% ") == string::npos)
-                       break;
-
-               Alert::alert(_("Filename can't contain any "
-                       "of these characters:"),
-                       _("space, '#', '~', '$' or '%'."));
-       }
+       if (save)
+               result = fileDlg.save(lastPath, filters,
+                                     OnlyFilename(filename));
+       else
+               result = fileDlg.open(lastPath, filters,
+                                     OnlyFilename(filename));
 
        return result.second;
 }
 
 
 string const browseRelFile(string const & filename,
-                       string const & refpath,
-                       string const & title,
-                       string const & pattern,
-                       bool save,
-                       pair<string,string> const & dir1,
-                       pair<string,string> const & dir2)
+                          string const & refpath,
+                          string const & title,
+                          FileFilterList const & filters,
+                          bool save,
+                          pair<string,string> const & dir1,
+                          pair<string,string> const & dir2)
 {
        string const fname = MakeAbsPath(filename, refpath);
 
-       string const outname = browseFile(fname, title, pattern, save,
+       string const outname = browseFile(fname, title, filters, save,
                                          dir1, dir2);
        string const reloutname = MakeRelPath(outname, refpath);
        if (prefixIs(reloutname, "../"))
@@ -86,49 +90,66 @@ string const browseRelFile(string const & filename,
 }
 
 
-string const browseDir(string const & pathname,
-                       string const & title,
-                       pair<string,string> const & dir1,
-                       pair<string,string> const & dir2)
+
+string const browseLibFile(string const & dir,
+                          string const & name,
+                          string const & ext,
+                          string const & title,
+                          FileFilterList const & filters)
 {
-       string lastPath(".");
-       if (!pathname.empty())
-               lastPath = OnlyPath(pathname);
+       pair<string,string> const dir1(_("System files|#S#s"),
+                                      AddName(package().system_support(), dir));
 
-       FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
+       pair<string,string> const dir2(_("User files|#U#u"),
+                                      AddName(package().user_support(), dir));
 
-       FileDialog::Result result;
+       string const result = browseFile(LibFileSearch(dir, name, ext), title,
+                                        filters, false, dir1, dir2);
 
-       while (true) {
-               result = fileDlg.opendir(lastPath,
-                               OnlyFilename(pathname));
+       // remove the extension if it is the default one
+       string noextresult;
+       if (GetExtension(result) == ext)
+               noextresult = ChangeExtension(result, string());
+       else
+               noextresult = result;
 
-               if (result.second.empty())
-                       return result.second;
+       // remove the directory, if it is the default one
+       string const file = OnlyFilename(noextresult);
+       if (LibFileSearch(dir, file, ext) == result)
+               return file;
+       else
+               return noextresult;
+}
 
-               lastPath = OnlyPath(result.second);
 
-               if (result.second.find_first_of("#~$% ") == string::npos)
-                       break;
+string const browseDir(string const & pathname,
+                      string const & title,
+                      pair<string,string> const & dir1,
+                      pair<string,string> const & dir2)
+{
+       string lastPath(".");
+       if (!pathname.empty())
+               lastPath = OnlyPath(pathname);
 
-               Alert::alert(_("directory name can't contain any "
-                       "of these characters:"),
-                       _("space, '#', '~', '$' or '%'."));
-       }
+       FileDialog fileDlg(title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
+
+       FileDialog::Result const result =
+               fileDlg.opendir(lastPath, OnlyFilename(pathname));
 
        return result.second;
 }
 
 
-// sorry this is just a temporary hack we should include vspace.h! (Jug)
-extern const char * stringFromUnit(int);
-
 vector<string> const getLatexUnits()
 {
        vector<string> units;
-       char const * str;
-       for (int i = 0; (str = stringFromUnit(i)); ++i)
-           units.push_back(str);
+       int i = 0;
+       char const * str = stringFromUnit(i);
+       for (; str != 0; ++i, str = stringFromUnit(i))
+               units.push_back(str);
 
        return units;
 }
+
+} // namespace frontend
+} // namespace lyx