From 56c470c33d468198756ae993923d0e2be0c47a69 Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Sun, 8 Aug 2004 15:23:43 +0000 Subject: [PATCH] make browsing for lyxdir files DTRT git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8872 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/controllers/ChangeLog | 8 +++++ src/frontends/controllers/ControlPrefs.C | 32 ++++-------------- src/frontends/controllers/helper_funcs.C | 42 +++++++++++++++++++++++- src/frontends/controllers/helper_funcs.h | 23 ++++++++++--- 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 5a21f20d31..cc33e685cc 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,11 @@ +2004-08-08 Jean-Marc Lasgouttes + + * helper_funcs.C (browseLibFile): new function. Tries to do the + right thing to look for a file in lyxdir + + * ControlPrefs.C (browsebind, browseUI, browsekbmap): use + browseLibFile + 2004-08-07 Jean-Marc Lasgouttes * ControlBibtex.C (browseBst): new method, specialized to lookup diff --git a/src/frontends/controllers/ControlPrefs.C b/src/frontends/controllers/ControlPrefs.C index 8236af8c56..8f1a3775ca 100644 --- a/src/frontends/controllers/ControlPrefs.C +++ b/src/frontends/controllers/ControlPrefs.C @@ -20,9 +20,7 @@ #include "funcrequest.h" #include "LColor.h" -#include "support/filetools.h" #include "support/globbing.h" -#include "support/path_defines.h" #include @@ -36,10 +34,7 @@ extern BufferList bufferlist; namespace lyx { -using support::AddName; using support::FileFilterList; -using support::system_lyxdir; -using support::user_lyxdir; namespace frontend { @@ -123,37 +118,22 @@ void ControlPrefs::updateScreenFonts() string const ControlPrefs::browsebind(string const & file) const { - pair dir1(_("System Bind|#S#s"), - AddName(system_lyxdir(), "bind")); - - pair dir2(_("User Bind|#U#u"), - AddName(user_lyxdir(), "bind")); - - return browseFile(file, _("Choose bind file"), - FileFilterList("*.bind"), false, dir1, dir2); + return browseLibFile("bind", file, "bind", _("Choose bind file"), + FileFilterList("LyX bind files (*.bind)")); } string const ControlPrefs::browseUI(string const & file) const { - pair const dir1(_("Sys UI|#S#s"), - AddName(system_lyxdir(), "ui")); - - pair const dir2(_("User UI|#U#u"), - AddName(user_lyxdir(), "ui")); - - return browseFile(file, _("Choose UI file"), - FileFilterList("*.ui"), false, dir1, dir2); + return browseLibFile("ui", file, "ui", _("Choose UI file"), + FileFilterList("LyX UI files (*.ui)")); } string const ControlPrefs::browsekbmap(string const & file) const { - pair dir(_("Key maps|#K#k"), - AddName(system_lyxdir(), "kbd")); - - return browseFile(file, _("Choose keyboard map"), - FileFilterList("*.kmap"), false, dir); + return browseLibFile("kbd", file, "kmap", _("Choose keyboard map"), + FileFilterList("LyX keyboard maps (*.kmap)")); } diff --git a/src/frontends/controllers/helper_funcs.C b/src/frontends/controllers/helper_funcs.C index a817db0a1d..abead02569 100644 --- a/src/frontends/controllers/helper_funcs.C +++ b/src/frontends/controllers/helper_funcs.C @@ -3,6 +3,7 @@ * 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. @@ -18,7 +19,8 @@ #include "frontends/Alert.h" #include "frontends/FileDialog.h" -#include "support/filetools.h" // OnlyPath, OnlyFilename +#include "support/filetools.h" +#include "support/path_defines.h" #include "support/globbing.h" using std::pair; @@ -30,12 +32,18 @@ extern const char * stringFromUnit(int); 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::prefixIs; +using support::system_lyxdir; +using support::user_lyxdir; namespace frontend { @@ -101,6 +109,38 @@ string const browseRelFile(string const & filename, } + +string const browseLibFile(string const & dir, + string const & name, + string const & ext, + string const & title, + FileFilterList const & filters) +{ + pair const dir1(_("System files|#S#s"), + AddName(system_lyxdir(), dir)); + + pair const dir2(_("User files|#U#u"), + AddName(user_lyxdir(), dir)); + + string const result = browseFile(LibFileSearch(dir, name, ext), title, + filters, false, dir1, dir2); + + // remove the extension if it is the default one + string noextresult; + if (GetExtension(result) == ext) + noextresult = ChangeExtension(result, string()); + else + noextresult = result; + + // 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; +} + + string const browseDir(string const & pathname, string const & title, pair const & dir1, diff --git a/src/frontends/controllers/helper_funcs.h b/src/frontends/controllers/helper_funcs.h index aa9313f04c..36def74d20 100644 --- a/src/frontends/controllers/helper_funcs.h +++ b/src/frontends/controllers/helper_funcs.h @@ -44,11 +44,11 @@ browseFile(std::string const & filename, std::make_pair(std::string(), std::string())); -/* Wrapper around browseFile which tries to provide a filename - relative to relpath. If the relative path is of the form "foo.txt" - or "bar/foo.txt", then it is returned as relative. OTOH, if it is - of the form "../baz/foo.txt", an absolute path is returned. This is - intended to be useful for insets which encapsulate files/ +/** Wrapper around browseFile which tries to provide a filename + relative to relpath. If the relative path is of the form "foo.txt" + or "bar/foo.txt", then it is returned as relative. OTOH, if it is + of the form "../baz/foo.txt", an absolute path is returned. This is + intended to be useful for insets which encapsulate files/ */ std::string const browseRelFile(std::string const & filename, @@ -62,6 +62,19 @@ browseRelFile(std::string const & filename, std::make_pair(std::string(), std::string())); +/** Wrapper around browseFile which tries to provide a filename + * relative to the user or system directory. The dir, name and ext + * parameters have the same meaning as in the + * lyx::support::LibFileSearch function. + */ +std::string const +browseLibFile(std::string const & dir, + std::string const & name, + std::string const & ext, + std::string const & title, + support::FileFilterList const & filters); + + /** Launch a file dialog and return the chosen directory. pathname: a suggested pathname. title: the title of the dialog.