#include "InsetExternal.h"
#include "insets/ExternalSupport.h"
-#include "insets/ExternalTemplate.h"
#include "insets/RenderButton.h"
#include "insets/RenderGraphic.h"
#include "insets/RenderPreview.h"
#include "LyXRC.h"
#include "MetricsInfo.h"
#include "OutputParams.h"
+#include "output_latex.h"
#include "frontends/alert.h"
#include "frontends/Application.h"
#include "graphics/PreviewLoader.h"
+#include "support/bind.h"
+#include "support/convert.h"
#include "support/debug.h"
#include "support/ExceptionMessage.h"
#include "support/filetools.h"
#include "support/gettext.h"
+#include "support/lassert.h"
#include "support/lstrings.h"
#include "support/lyxlib.h"
-#include "support/convert.h"
-#include "support/Translator.h"
-
-#include <boost/bind.hpp>
#include <sstream>
+#include <vector>
using namespace std;
using namespace lyx::support;
namespace {
-lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
-
unsigned int const defaultLyxScale = 100;
string defaultTemplateName;
TempName::TempName()
{
FileName const tempname = FileName::tempName("lyxext");
- // FIXME: This is unsafe
- tempname.removeFile();
// must have an extension for the converter code to work correctly.
- tempname_ = FileName(tempname.absFilename() + ".tmp");
+ tempname_ = FileName(tempname.absFileName() + ".tmp");
}
return *this;
}
-
-namespace {
-
-/// The translator between the Display enum and corresponding lyx string.
-Translator<DisplayType, string> const initTranslator()
-{
- Translator<DisplayType, string> translator(DefaultDisplay, "default");
-
- // Fill the display translator
- translator.addPair(MonochromeDisplay, "monochrome");
- translator.addPair(GrayscaleDisplay, "grayscale");
- translator.addPair(ColorDisplay, "color");
- translator.addPair(PreviewDisplay, "preview");
- translator.addPair(NoDisplay, "none");
-
- return translator;
-}
-
-} // namespace anon
-
-
-Translator<DisplayType, string> const & displayTranslator()
-{
- static Translator<DisplayType, string> const translator =
- initTranslator();
- return translator;
-}
-
} // namespace external
InsetExternalParams::InsetExternalParams()
- : display(defaultDisplayType),
+ : display(true),
lyxscale(defaultLyxScale),
draft(false)
{
if (defaultTemplateName.empty()) {
external::TemplateManager const & etm =
external::TemplateManager::get();
- templatename_ = etm.getTemplates().begin()->first;
+ if (!etm.getTemplates().empty())
+ templatename_ = etm.getTemplates().begin()->first;
} else
templatename_ = defaultTemplateName;
}
clearIfNotFound(extradata, external::Extra, ids);
clearIfNotFound(resizedata, external::Resize, ids);
clearIfNotFound(rotationdata, external::Rotate, ids);
+
+ //
+ preview_mode = et->preview_mode;
}
os << "External\n"
<< "\ttemplate " << templatename() << '\n';
- if (!filename.empty()) {
- os << "\tfilename " << filename.outputFilename(buf.filePath()) << '\n';
- os << "\tembed " << (filename.embedded() ? filename.inzipName() : "\"\"") << '\n';
- }
- if (display != defaultDisplayType)
- os << "\tdisplay "
- << external::displayTranslator().find(display)
- << '\n';
+ if (!filename.empty())
+ os << "\tfilename " << filename.outputFileName(buf.filePath()) << '\n';
+
+ if (!display)
+ os << "\tdisplay false\n";
if (lyxscale != defaultLyxScale)
os << "\tlyxscale " << convert<string>(lyxscale) << '\n';
enum {
EX_TEMPLATE = 1,
EX_FILENAME,
- EX_EMBED,
EX_DISPLAY,
EX_LYXSCALE,
EX_DRAFT,
{ "clip", EX_CLIP },
{ "display", EX_DISPLAY},
{ "draft", EX_DRAFT},
- { "embed", EX_EMBED},
{ "extra", EX_EXTRA },
{ "filename", EX_FILENAME},
{ "height", EX_HEIGHT },
break;
}
- case EX_EMBED: {
- lex.next();
- string const name = lex.getString();
- filename.setInzipName(name);
- filename.setEmbed(!name.empty());
- break;
- }
-
case EX_DISPLAY: {
lex.next();
- string const name = lex.getString();
- display = external::displayTranslator().find(name);
+ display = lex.getString() != "false";
break;
}
}
-InsetExternal::InsetExternal(Buffer & buf)
- : renderer_(new RenderButton)
+InsetExternal::InsetExternal(Buffer * buf)
+ : Inset(buf), renderer_(new RenderButton)
{
- Inset::setBuffer(buf);
}
+// Mouse hover is not copied and remains empty
InsetExternal::InsetExternal(InsetExternal const & other)
: Inset(other),
boost::signals::trackable(),
InsetExternal::~InsetExternal()
{
hideDialogs("external", this);
+
+ map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
+ map<BufferView const *, bool>::iterator end = mouse_hover_.end();
+ for (; it != end; ++it)
+ if (it->second)
+ it->first->clearLastInset(this);
}
-void InsetExternal::setBuffer(Buffer & buffer)
+bool InsetExternal::setMouseHover(BufferView const * bv, bool mouse_hover)
{
- if (buffer_) {
- try {
- // a file may not be copied successfully when, e.g. buffer_
- // has already been closed.
- params_.filename = params_.filename.copyTo(buffer);
- } catch (ExceptionMessage const & message) {
- Alert::error(message.title_, message.details_);
- // failed to embed
- params_.filename.setEmbed(false);
- }
- }
- Inset::setBuffer(buffer);
+ mouse_hover_[bv] = mouse_hover;
+ return true;
}
void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
{
- switch (cmd.action) {
+ switch (cmd.action()) {
- case LFUN_EXTERNAL_EDIT: {
+ case LFUN_INSET_EDIT: {
InsetExternalParams p = params();
if (!cmd.argument().empty())
string2params(to_utf8(cmd.argument()), buffer(), p);
case LFUN_INSET_MODIFY: {
InsetExternalParams p;
string2params(to_utf8(cmd.argument()), buffer(), p);
+ cur.recordUndo();
setParams(p);
break;
}
params2string(params(), cur.bv().buffer()));
break;
- case LFUN_MOUSE_RELEASE:
- if (!cur.selection() && cmd.button() == mouse_button::button1)
- cur.bv().showDialog("external",
- params2string(params(), cur.bv().buffer()),
- this);
- break;
-
default:
Inset::doDispatch(cur, cmd);
}
bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
FuncStatus & flag) const
{
- switch (cmd.action) {
+ switch (cmd.action()) {
- case LFUN_EXTERNAL_EDIT:
+ case LFUN_INSET_EDIT:
case LFUN_INSET_MODIFY:
case LFUN_INSET_DIALOG_UPDATE:
- flag.enabled(true);
+ flag.setEnabled(true);
return true;
default:
}
-void InsetExternal::registerEmbeddedFiles(EmbeddedFileList & files) const
-{
- files.registerFile(params_.filename, this, buffer());
-}
-
-
-void InsetExternal::updateEmbeddedFile(EmbeddedFile const & file)
+bool InsetExternal::showInsetDialog(BufferView * bv) const
{
- params_.filename = file;
-}
-
-
-void InsetExternal::edit(Cursor & cur, bool, EntryDirection)
-{
- cur.bv().showDialog("external",
- params2string(params(), cur.bv().buffer()),
- this);
+ bv->showDialog("external", params2string(params(), bv->buffer()),
+ const_cast<InsetExternal *>(this));
+ return true;
}
void InsetExternal::draw(PainterInfo & pi, int x, int y) const
{
+ if (renderer_->asButton())
+ renderer_->setRenderState(mouse_hover_[pi.base.bv]);
renderer_->draw(pi, x, y);
}
namespace {
-enum RenderType {
- RENDERBUTTON,
- RENDERGRAPHIC,
- RENDERPREVIEW
-};
-
-
-RenderType getRenderType(InsetExternalParams const & p)
-{
- if (!external::getTemplatePtr(p) ||
- p.filename.empty() ||
- p.display == external::NoDisplay)
- return RENDERBUTTON;
-
- if (p.display == external::PreviewDisplay) {
- if (RenderPreview::status() != LyXRC::PREVIEW_OFF)
- return RENDERPREVIEW;
- return RENDERBUTTON;
- }
-
- if (p.display == external::DefaultDisplay &&
- lyxrc.display_graphics == graphics::NoDisplay)
- return RENDERBUTTON;
- return RENDERGRAPHIC;
-}
-
-
graphics::Params get_grfx_params(InsetExternalParams const & eparams)
{
graphics::Params gparams;
- gparams.filename = eparams.filename.availableFile();
- gparams.icon = eparams.filename.embedded() ? "pin.png" : "";
+ gparams.filename = eparams.filename;
gparams.scale = eparams.lyxscale;
if (eparams.clipdata.clip)
gparams.bb = eparams.clipdata.bbox;
gparams.angle = convert<double>(eparams.rotationdata.adjAngle());
-
- switch (eparams.display) {
- case external::DefaultDisplay:
- gparams.display = graphics::DefaultDisplay;
- break;
- case external::MonochromeDisplay:
- gparams.display = graphics::MonochromeDisplay;
- break;
- case external::GrayscaleDisplay:
- gparams.display = graphics::GrayscaleDisplay;
- break;
- case external::ColorDisplay:
- gparams.display = graphics::ColorDisplay;
- break;
- case external::NoDisplay:
- gparams.display = graphics::NoDisplay;
- break;
- default:
- LASSERT(false, /**/);
- }
- if (gparams.display == graphics::DefaultDisplay)
- gparams.display = graphics::DisplayType(lyxrc.display_graphics);
- // Override the above if we're not using a gui
- if (!use_gui)
- gparams.display = graphics::NoDisplay;
+ gparams.display = eparams.display;
return gparams;
}
from_utf8(params.templatename()));
// FIXME UNICODE
docstring gui = _(ptr->guiName);
- return from_utf8(external::doSubstitution(params, buffer,
- to_utf8(gui), false));
+ gui += ": ";
+
+ if (params.filename.empty())
+ gui += "???";
+ else
+ gui += from_utf8(params.filename.relFileName(buffer.filePath()));
+
+ return gui;
}
} // namespace anon
static bool isPreviewWanted(InsetExternalParams const & params)
{
- return params.display == external::PreviewDisplay &&
- params.filename.isReadableFile();
+ return params.display && params.filename.isReadableFile();
}
static docstring latexString(InsetExternal const & inset)
{
- odocstringstream os;
+ TexRow texrow;
+ odocstringstream ods;
+ otexstream os(ods, texrow);
// We don't need to set runparams.encoding since it is not used by
// latex().
OutputParams runparams(0);
runparams.flavor = OutputParams::LATEX;
inset.latex(os, runparams);
- return os.str();
+ return ods.str();
}
}
+void InsetExternal::updatePreview()
+{
+ setParams(params_);
+}
+
+
void InsetExternal::setParams(InsetExternalParams const & p)
{
params_ = p;
// will use this.
defaultTemplateName = params_.templatename();
- switch (getRenderType(params_)) {
- case RENDERBUTTON: {
+ if (!external::getTemplatePtr(params_) || params_.filename.empty()
+ || !params_.display
+ || !lyxrc.display_graphics
+ || params_.preview_mode == PREVIEW_OFF
+ || (params_.preview_mode == PREVIEW_INSTANT
+ && RenderPreview::status() == LyXRC::PREVIEW_OFF)) {
RenderButton * button_ptr = renderer_->asButton();
if (!button_ptr) {
renderer_.reset(new RenderButton);
button_ptr = renderer_->asButton();
}
-
button_ptr->update(screenLabel(params_, buffer()), true);
- break;
+ return;
}
- case RENDERGRAPHIC: {
+ switch (params_.preview_mode) {
+ case PREVIEW_OFF:
+ // Already taken care of above.
+ LASSERT(false, return);
+ break;
+ case PREVIEW_INSTANT: {
+ RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview();
+ renderer_.reset(new RenderMonitoredPreview(this));
+ preview_ptr = renderer_->asMonitoredPreview();
+ preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this));
+ if (preview_ptr->monitoring())
+ preview_ptr->stopMonitoring();
+ add_preview_and_start_loading(*preview_ptr, *this, buffer());
+ break;
+ }
+ case PREVIEW_GRAPHICS: {
RenderGraphic * graphic_ptr = renderer_->asGraphic();
if (!graphic_ptr) {
renderer_.reset(new RenderGraphic(this));
graphic_ptr = renderer_->asGraphic();
}
-
graphic_ptr->update(get_grfx_params(params_));
-
- break;
- }
-
- case RENDERPREVIEW: {
- RenderMonitoredPreview * preview_ptr =
- renderer_->asMonitoredPreview();
- if (!preview_ptr) {
- renderer_.reset(new RenderMonitoredPreview(this));
- preview_ptr = renderer_->asMonitoredPreview();
- preview_ptr->fileChanged(
- boost::bind(&InsetExternal::fileChanged, this));
- }
-
- if (preview_ptr->monitoring())
- preview_ptr->stopMonitoring();
- add_preview_and_start_loading(*preview_ptr, *this, buffer());
-
break;
}
}
void InsetExternal::read(Lexer & lex)
{
InsetExternalParams params;
- if (params.read(buffer(), lex)) {
- // exception handling is not needed as long as embedded files are in place.
- params.filename.enable(buffer().embedded(), buffer(), false);
+ if (params.read(buffer(), lex))
setParams(params);
- }
}
-int InsetExternal::latex(odocstream & os, OutputParams const & runparams) const
+void InsetExternal::latex(otexstream & os, OutputParams const & runparams) const
{
if (params_.draft) {
// FIXME UNICODE
os << "\\fbox{\\ttfamily{}"
- << from_utf8(params_.filename.outputFilename(buffer().filePath()))
+ << from_utf8(params_.filename.outputFileName(buffer().filePath()))
<< "}\n";
- return 1;
+ return;
}
// "nice" means that the buffer is exported to LaTeX format but not
external::Template const * const et_ptr =
external::getTemplatePtr(params_);
if (!et_ptr)
- return 0;
+ return;
external::Template const & et = *et_ptr;
external::Template::Formats::const_iterator cit =
et.formats.find("PDFLaTeX");
if (cit != et.formats.end()) {
- return external::writeExternal(params_, "PDFLaTeX",
- buffer(), os,
- *(runparams.exportdata),
- external_in_tmpdir,
- dryrun);
+ int l = external::writeExternal(params_, "PDFLaTeX",
+ buffer(), os.os(),
+ *(runparams.exportdata),
+ external_in_tmpdir,
+ dryrun);
+ os.texrow().newlines(l);
+ return;
}
}
- return external::writeExternal(params_, "LaTeX", buffer(), os,
- *(runparams.exportdata),
- external_in_tmpdir,
- dryrun);
+ int l = external::writeExternal(params_, "LaTeX", buffer(), os.os(),
+ *(runparams.exportdata),
+ external_in_tmpdir,
+ dryrun);
+ os.texrow().newlines(l);
}
}
+docstring InsetExternal::xhtml(XHTMLStream & /*xs*/,
+ OutputParams const & /*rp*/) const
+{
+// external::writeExternal(params_, "XHTML", buffer(), os,
+// *(runparams.exportdata), false,
+// runparams.dryrun || runparams.inComment);
+ return docstring();
+}
+
+
void InsetExternal::validate(LaTeXFeatures & features) const
{
if (params_.draft)
string format;
switch (features.runparams().flavor) {
case OutputParams::LATEX:
+ case OutputParams::DVILUATEX:
format = "LaTeX";
break;
+ case OutputParams::LUATEX:
case OutputParams::PDFLATEX:
+ case OutputParams::XETEX:
format = "PDFLaTeX";
break;
case OutputParams::XML:
format = "DocBook";
break;
+ case OutputParams::HTML:
+ format = "html";
+ break;
+ case OutputParams::TEXT:
+ format = "text";
+ break;
+ case OutputParams::LYX:
+ format = "lyx";
+ break;
}
external::Template::Formats::const_iterator cit =
et.formats.find(format);
return;
}
- // FIXME: We don't need that always
+ // FIXME: We don't need that always, see InsetGraphics
features.require("lyxdot");
vector<string>::const_iterator it = cit->second.requirements.begin();
}
-void InsetExternal::addPreview(graphics::PreviewLoader & ploader) const
+void InsetExternal::addPreview(DocIterator const & /*inset_pos*/,
+ graphics::PreviewLoader & ploader) const
{
RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview();
if (!ptr)
}
+string InsetExternal::contextMenuName() const
+{
+ return "context-external";
+}
+
+
void InsetExternal::string2params(string const & in, Buffer const & buffer,
InsetExternalParams & params)
{