From 19e1271c83618c2ee92c47c9b98b6209db2b37c9 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Wed, 11 Jun 2003 14:30:04 +0000 Subject: [PATCH] Enable the external inset to handle unknown templates gracefully. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7146 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/controllers/ChangeLog | 9 ++- src/frontends/controllers/ControlExternal.C | 26 ++++-- src/frontends/controllers/ControlExternal.h | 3 + src/frontends/qt2/ChangeLog | 5 ++ src/frontends/qt2/QExternal.C | 8 +- src/frontends/xforms/ChangeLog | 5 ++ src/frontends/xforms/FormExternal.C | 6 +- src/insets/ChangeLog | 11 +++ src/insets/insetexternal.C | 87 +++++++++++++++------ src/insets/insetexternal.h | 5 +- 10 files changed, 126 insertions(+), 39 deletions(-) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index b9c6dd957e..0656c5e7b0 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,7 +1,12 @@ +2003-06-11 Angus Leeming + + * ControlExternal.[Ch]: changes due to InsetExternal::Params no longer + storing the ExternalTemplate but its name. + 2003-06-10 Angus Leeming - * ControlExternal.C (editExternal): invokes new LFUN_EXTERNAL_EDIT rather - than generate a new inset itself. + * ControlExternal.C (editExternal): invokes new LFUN_EXTERNAL_EDIT + rather than generate a new inset itself. 2003-06-06 John Levon diff --git a/src/frontends/controllers/ControlExternal.C b/src/frontends/controllers/ControlExternal.C index c985ef4fd5..287cdaed1b 100644 --- a/src/frontends/controllers/ControlExternal.C +++ b/src/frontends/controllers/ControlExternal.C @@ -19,6 +19,8 @@ #include "helper_funcs.h" #include "lyxrc.h" +#include "insets/ExternalTemplate.h" + #include "support/LAssert.h" #include @@ -111,12 +113,26 @@ ExternalTemplate ControlExternal::getTemplate(int i) const ExternalTemplateManager::Templates::const_iterator i1 = ExternalTemplateManager::get().getTemplates().begin(); - std::advance(i1, i); + std::advance(i1, i); return i1->second; } +namespace { + +ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params) +{ + ExternalTemplateManager & etm = ExternalTemplateManager::get(); + ExternalTemplate const & templ = etm.getTemplateByName(params.templatename); + if (templ.lyxName.empty()) + return 0; + return &templ; +} + +} // namespace anon + + string const ControlExternal::Browse(string const & input) const { string const title = _("Select external file"); @@ -124,10 +140,10 @@ string const ControlExternal::Browse(string const & input) const string const bufpath = kernel().buffer()->filePath(); /// Determine the template file extension - ExternalTemplate const & et = params().templ; - string pattern = et.fileRegExp; - if (pattern.empty()) - pattern = "*"; + string pattern = "*"; + ExternalTemplate const * const et_ptr = getTemplatePtr(params()); + if (et_ptr) + pattern = et_ptr->fileRegExp; // FIXME: a temporary hack until the FileDialog interface is updated pattern += '|'; diff --git a/src/frontends/controllers/ControlExternal.h b/src/frontends/controllers/ControlExternal.h index bdef4881eb..9f7acd8cdd 100644 --- a/src/frontends/controllers/ControlExternal.h +++ b/src/frontends/controllers/ControlExternal.h @@ -20,6 +20,9 @@ #include +class ExternalTemplate; + + class ControlExternal : public Dialog::Controller { public: /// diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index cc261b33c3..31f341be93 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,8 @@ +2003-06-11 Angus Leeming + + * QExternal.C: changes due to InsetExternal::Params no longer + storing the ExternalTemplate but its name. + 2003-06-10 Angus Leeming * QDocument.C (apply): compilation fix after Lars' changes ;-) diff --git a/src/frontends/qt2/QExternal.C b/src/frontends/qt2/QExternal.C index 2196f56c92..da958b70ef 100644 --- a/src/frontends/qt2/QExternal.C +++ b/src/frontends/qt2/QExternal.C @@ -13,6 +13,9 @@ #include "ControlExternal.h" #include "qt_helpers.h" + +#include "insets/ExternalTemplate.h" + #include "support/lstrings.h" #include "support/tostr.h" @@ -63,7 +66,7 @@ void QExternal::update_contents() dialog_->fileED->setText(toqstr(params.filename)); - dialog_->externalCO->setCurrentItem(controller().getTemplateNumber(params.templ.lyxName)); + dialog_->externalCO->setCurrentItem(controller().getTemplateNumber(params.templatename)); dialog_->externalTV->setText(toqstr(helpText())); int item = 0; @@ -100,7 +103,8 @@ void QExternal::apply() params.filename = fromqstr(dialog_->fileED->text()); - params.templ = controller().getTemplate(dialog_->externalCO->currentItem()); + params.templatename = + controller().getTemplate(dialog_->externalCO->currentItem()).lyxName; switch (dialog_->showCB->currentItem()) { case 0: params.display = grfx::DefaultDisplay; break; diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index c6214f4552..dcd3832c7c 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,8 @@ +2003-06-11 Angus Leeming + + * FormExternal.C: changes due to InsetExternal::Params no longer + storing the ExternalTemplate but its name. + 2003-06-10 Lars Gullik Bjønnes * XFormsView.C: remvoe current_view global variable. diff --git a/src/frontends/xforms/FormExternal.C b/src/frontends/xforms/FormExternal.C index 0546add807..a24b020739 100644 --- a/src/frontends/xforms/FormExternal.C +++ b/src/frontends/xforms/FormExternal.C @@ -24,6 +24,8 @@ #include "gettext.h" +#include "insets/ExternalTemplate.h" + #include "support/tostr.h" #include "support/lstrings.h" #include "lyx_forms.h" @@ -42,7 +44,7 @@ void FormExternal::apply() params.filename = fl_get_input(dialog_->input_filename); int const choice = fl_get_choice(dialog_->choice_template) - 1; - params.templ = controller().getTemplate(choice); + params.templatename = controller().getTemplate(choice).lyxName; params.lyxscale = strToInt(getString(dialog_->input_lyxscale)); if (params.lyxscale == 0) @@ -120,7 +122,7 @@ void FormExternal::update() fl_set_input(dialog_->input_filename, params.filename.c_str()); - int ID = controller().getTemplateNumber(params.templ.lyxName); + int ID = controller().getTemplateNumber(params.templatename); if (ID < 0) ID = 0; fl_set_choice(dialog_->choice_template, ID+1); diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index c5c7a3b631..b0f09f656d 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,14 @@ +2003-06-11 Angus Leeming + + * insetexternal.[Ch]: InsetExternal::Params no longer stores the + ExternalTemplate but its name, templatename. This means that a file + containing a reference to an External Template not installed on the + current machine will be saved retaining this info rather than inserting + the first "valid" template from the list of templates. + + As a by product, the visual statement that the Template is missing is + now very clear! + 2003-06-10 Lars Gullik Bjønnes * insettext.C (appendParagraphs): remove some commented code. diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 0cfc8586e5..ee0cbea2bc 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -85,7 +85,6 @@ InsetExternal::InsetExternal() : renderer_(new GraphicInset) { renderer_->connect(boost::bind(&InsetExternal::statusChanged, this)); - params_.templ = ExternalTemplateManager::get().getTemplates().begin()->second; } @@ -201,34 +200,60 @@ grfx::Params get_grfx_params(InsetExternal::Params const & eparams, return gparams; } + +ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params) +{ + ExternalTemplateManager & etm = ExternalTemplateManager::get(); + ExternalTemplate const & templ = etm.getTemplateByName(params.templatename); + if (templ.lyxName.empty()) + return 0; + return &templ; +} + + +string const getScreenLabel(InsetExternal::Params const & params) +{ + ExternalTemplate const * const ptr = getTemplatePtr(params); + if (!ptr) + return bformat(_("External template %1$s is not installed"), + params.templatename); + return doSubstitution(params, 0, ptr->guiName); +} + } // namespace anon void InsetExternal::setParams(Params const & p, string const & filepath) { + // The stored params; what we would like to happen in an ideal world. params_.filename = p.filename; - params_.templ = p.templ; + params_.templatename = p.templatename; params_.display = p.display; params_.lyxscale = p.lyxscale; + // A temporary set of params; whether the thing can be displayed + // within LyX depends on the availability of this template. + Params tmp = params_; + if (!getTemplatePtr(params_)) + tmp.display = grfx::NoDisplay; + // Update the display using the new parameters. if (params_.filename.empty() || !filepath.empty()) - renderer_->update(get_grfx_params(params_, filepath)); - string const msg = doSubstitution(params_, 0, params_.templ.guiName); - renderer_->setNoDisplayMessage(msg); + renderer_->update(get_grfx_params(tmp, filepath)); + renderer_->setNoDisplayMessage(getScreenLabel(params_)); } string const InsetExternal::editMessage() const { - return doSubstitution(params_, 0, params_.templ.guiName); + return getScreenLabel(params_); } void InsetExternal::write(Buffer const *, ostream & os) const { os << "External\n" - << "\ttemplate " << params_.templ.lyxName << '\n'; + << "\ttemplate " << params_.templatename << '\n'; if (!params_.filename.empty()) os << "\tfilename " << params_.filename << '\n'; @@ -270,10 +295,7 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex) switch (lex.lex()) { case EX_TEMPLATE: { lex.next(); - string const name = lex.getString(); - ExternalTemplateManager & etm = - ExternalTemplateManager::get(); - params.templ = etm.getTemplateByName(name); + params.templatename = lex.getString(); break; } @@ -320,20 +342,15 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex) lex.popTable(); // Replace the inset's store - params_ = params; + string const path = buffer ? buffer->filePath() : string(); + setParams(params, path); lyxerr[Debug::INFO] << "InsetExternal::Read: " - << "template: '" << params_.templ.lyxName + << "template: '" << params_.templatename << "' filename: '" << params_.filename << "' display: '" << params_.display << "' scale: '" << params_.lyxscale << '\'' << endl; - - // Update the display using the new parameters. - if (buffer) - renderer_->update(get_grfx_params(params_, buffer->filePath())); - string const msg = doSubstitution(params_, 0, params_.templ.guiName); - renderer_->setNoDisplayMessage(msg); } @@ -341,13 +358,17 @@ int InsetExternal::write(string const & format, Buffer const * buf, ostream & os, bool external_in_tmpdir) const { - ExternalTemplate const & et = params_.templ; + ExternalTemplate const * const et_ptr = getTemplatePtr(params_); + if (!et_ptr) + return 0; + ExternalTemplate const & et = *et_ptr; + ExternalTemplate::Formats::const_iterator cit = et.formats.find(format); if (cit == et.formats.end()) { lyxerr << "External template format '" << format << "' not specified in template " - << params_.templ.lyxName << endl; + << params_.templatename << endl; return 0; } @@ -371,7 +392,11 @@ int InsetExternal::latex(Buffer const * buf, ostream & os, // If the template has specified a PDFLaTeX output, then we try and // use that. if (runparams.flavor == LatexRunParams::PDFLATEX) { - ExternalTemplate const & et = params_.templ; + ExternalTemplate const * const et_ptr = getTemplatePtr(params_); + if (!et_ptr) + return 0; + ExternalTemplate const & et = *et_ptr; + ExternalTemplate::Formats::const_iterator cit = et.formats.find("PDFLaTeX"); if (cit != et.formats.end()) @@ -402,7 +427,11 @@ int InsetExternal::docbook(Buffer const * buf, ostream & os, bool) const void InsetExternal::validate(LaTeXFeatures & features) const { - ExternalTemplate const & et = params_.templ; + ExternalTemplate const * const et_ptr = getTemplatePtr(params_); + if (!et_ptr) + return; + ExternalTemplate const & et = *et_ptr; + ExternalTemplate::Formats::const_iterator cit = et.formats.find("LaTeX"); @@ -422,7 +451,11 @@ void InsetExternal::updateExternal(string const & format, Buffer const * buf, bool external_in_tmpdir) const { - ExternalTemplate const & et = params_.templ; + ExternalTemplate const * const et_ptr = getTemplatePtr(params_); + if (!et_ptr) + return; + ExternalTemplate const & et = *et_ptr; + if (!et.automaticProduction) return; @@ -535,7 +568,11 @@ void editExternal(InsetExternal::Params const & params, Buffer const * buffer) if (!buffer) return; - ExternalTemplate const & et = params.templ; + ExternalTemplate const * const et_ptr = getTemplatePtr(params); + if (!et_ptr) + return; + ExternalTemplate const & et = *et_ptr; + if (et.editCommand.empty()) return; diff --git a/src/insets/insetexternal.h b/src/insets/insetexternal.h index 83285347fe..4916bae6da 100644 --- a/src/insets/insetexternal.h +++ b/src/insets/insetexternal.h @@ -13,7 +13,6 @@ #define INSET_EXTERNAL_H #include "inset.h" -#include "ExternalTemplate.h" #include "graphics/GraphicsTypes.h" #include "LString.h" @@ -32,10 +31,10 @@ public: ~Params(); /// the filename string filename; + /// the current template used + string templatename; /// The name of the tempfile used for manipulations. string tempname; - /// the current template used - ExternalTemplate templ; /// how the inset is displayed by LyX grfx::DisplayType display; /// The scale of the displayed graphic (If shown).