2010-11-18 19:50:29 +00:00
|
|
|
/**
|
|
|
|
* \file InsetPreview.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Vincent van Ravesteijn
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "InsetPreview.h"
|
|
|
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Cursor.h"
|
2020-11-22 22:23:39 +00:00
|
|
|
#include "Dimension.h"
|
2010-11-18 19:50:29 +00:00
|
|
|
#include "MetricsInfo.h"
|
|
|
|
#include "RenderPreview.h"
|
2016-06-19 02:39:38 +00:00
|
|
|
#include "texstream.h"
|
2010-11-18 19:50:29 +00:00
|
|
|
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
#include "graphics/PreviewImage.h"
|
|
|
|
|
2015-06-14 16:10:29 +00:00
|
|
|
#include "mathed/InsetMathHull.h"
|
|
|
|
#include "mathed/MacroTable.h"
|
|
|
|
|
2010-11-18 19:50:29 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2016-02-03 13:50:00 +00:00
|
|
|
InsetPreview::InsetPreview(Buffer * buf)
|
|
|
|
: InsetText(buf), preview_(new RenderPreview(this))
|
2010-11-18 19:50:29 +00:00
|
|
|
{
|
|
|
|
setDrawFrame(true);
|
|
|
|
setFrameColor(Color_previewframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-03 13:50:00 +00:00
|
|
|
InsetPreview::~InsetPreview()
|
2010-11-18 19:50:29 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
InsetPreview::InsetPreview(InsetPreview const & other)
|
|
|
|
: InsetText(other)
|
|
|
|
{
|
|
|
|
preview_.reset(new RenderPreview(*other.preview_, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-11 09:16:09 +00:00
|
|
|
InsetPreview & InsetPreview::operator=(InsetPreview const & other)
|
|
|
|
{
|
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
InsetText::operator=(other);
|
|
|
|
preview_.reset(new RenderPreview(*other.preview_, this));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-18 19:50:29 +00:00
|
|
|
void InsetPreview::write(ostream & os) const
|
|
|
|
{
|
|
|
|
os << "Preview" << "\n";
|
|
|
|
text().write(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetPreview::addPreview(DocIterator const & inset_pos,
|
|
|
|
graphics::PreviewLoader &) const
|
|
|
|
{
|
|
|
|
preparePreview(inset_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-03 13:50:00 +00:00
|
|
|
void InsetPreview::preparePreview(DocIterator const & pos) const
|
2010-11-18 19:50:29 +00:00
|
|
|
{
|
2015-04-24 04:10:00 +00:00
|
|
|
odocstringstream str;
|
2016-09-04 02:02:47 +00:00
|
|
|
otexstream os(str);
|
2015-06-14 16:05:39 +00:00
|
|
|
OutputParams runparams(&pos.buffer()->params().encoding());
|
Introduce a wrapper class for odocstream to help ensuring that no
blank lines may be inadvertently output. This is achieved by using two
special iomanip-like variables (breakln and safebreakln) in the lyx::
namespace. When they are inserted in the stream, a newline is output
only if not already at the beginning of a line. The difference between
breakln and safebreakln is that, if needed, the former outputs '\n'
and the latter "%\n".
In future, the new class will also be used for counting the number of
newlines issued. Even if the infractrure for doing that is already in
place, the counting is essentially still done the old way.
There are still places in the code where the functionality of the
class could be used, most probably. ATM, it is used for InsetTabular,
InsetListings, InsetFloat, and InsetText.
The Comment and GreyedOut insets required a special treatment and a
new InsetLayout parameter (Display) has been introduced. The default
for Display is "true", meaning that the corresponding latex
environment is of "display" type, i.e., it stands on its own, whereas
"false" means that the contents appear inline with the text. The
latter is the case for both Comment and GreyedOut insets.
Mostly, the only visible effects on latex exports should be the
disappearing of some redundant % chars and the appearing/disappearing
of null {} latex groups after a comment or lyxgreyedout environments
(they are related to the presence or absence of a space immediately
after those environments), as well as the fact that math environments
are now started on their own lines.
As a last thing, only the latex code between \begin{document} and
\end{document} goes through the new class, the preamble being directly
output through odocstream, as usual.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37360 a592a061-630c-0410-9148-cb99ea01b6c8
2011-01-29 02:41:13 +00:00
|
|
|
latex(os, runparams);
|
2015-06-14 16:10:29 +00:00
|
|
|
|
|
|
|
// collect macros at this position
|
|
|
|
MacroNameSet macros;
|
|
|
|
pos.buffer()->listMacroNames(macros);
|
|
|
|
|
|
|
|
// look for math insets and collect definitions for the used macros
|
|
|
|
MacroNameSet defs;
|
|
|
|
DocIterator dit = doc_iterator_begin(pos.buffer(), this);
|
|
|
|
DocIterator const dend = doc_iterator_end(pos.buffer(), this);
|
|
|
|
if (!dit.nextInset())
|
|
|
|
dit.forwardInset();
|
|
|
|
for (; dit != dend; dit.forwardInset()) {
|
|
|
|
InsetMath * im = dit.nextInset()->asInsetMath();
|
2019-09-12 02:13:25 +00:00
|
|
|
InsetMathHull * hull = im ? im->asHullInset() : nullptr;
|
2015-06-14 16:10:29 +00:00
|
|
|
if (!hull)
|
|
|
|
continue;
|
|
|
|
for (idx_type idx = 0; idx < hull->nargs(); ++idx)
|
|
|
|
hull->usedMacros(hull->cell(idx), pos, macros, defs);
|
|
|
|
}
|
|
|
|
MacroNameSet::iterator it = defs.begin();
|
|
|
|
MacroNameSet::iterator end = defs.end();
|
|
|
|
docstring macro_preamble;
|
|
|
|
for (; it != end; ++it)
|
|
|
|
macro_preamble.append(*it);
|
|
|
|
|
|
|
|
docstring const snippet = macro_preamble + str.str();
|
2015-06-14 16:05:39 +00:00
|
|
|
preview_->addPreview(snippet, *pos.buffer());
|
2010-11-18 19:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetPreview::previewState(BufferView * bv) const
|
|
|
|
{
|
2014-05-20 10:19:26 +00:00
|
|
|
if (!editing(bv) && RenderPreview::previewText()) {
|
2010-11-18 19:50:29 +00:00
|
|
|
graphics::PreviewImage const * pimage =
|
|
|
|
preview_->getPreviewImage(bv->buffer());
|
|
|
|
return pimage && pimage->image();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetPreview::reloadPreview(DocIterator const & pos) const
|
|
|
|
{
|
|
|
|
preparePreview(pos);
|
|
|
|
preview_->startLoading(*pos.buffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetPreview::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
{
|
2016-02-03 13:50:00 +00:00
|
|
|
if (previewState(pi.base.bv)) {
|
2010-11-18 19:50:29 +00:00
|
|
|
// one pixel gap in front
|
2016-07-21 18:16:27 +00:00
|
|
|
preview_->draw(pi, x + 1, y);
|
2016-02-03 13:50:00 +00:00
|
|
|
} else
|
|
|
|
InsetText::draw(pi, x, y);
|
2010-11-18 19:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetPreview::edit(Cursor & cur, bool front, EntryDirection entry_from)
|
|
|
|
{
|
|
|
|
cur.push(*this);
|
|
|
|
InsetText::edit(cur, front, entry_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Inset * InsetPreview::editXY(Cursor & cur, int x, int y)
|
|
|
|
{
|
2016-02-03 13:50:00 +00:00
|
|
|
if (previewState(&cur.bv())) {
|
2010-11-18 19:50:29 +00:00
|
|
|
edit(cur, true, ENTRY_DIRECTION_IGNORE);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
cur.push(*this);
|
|
|
|
return InsetText::editXY(cur, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetPreview::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
{
|
|
|
|
if (previewState(mi.base.bv)) {
|
|
|
|
preview_->metrics(mi, dim);
|
2016-02-03 13:50:00 +00:00
|
|
|
|
2010-11-18 19:50:29 +00:00
|
|
|
dim.wid = max(dim.wid, 4);
|
|
|
|
dim.asc = max(dim.asc, 4);
|
2016-02-03 13:50:00 +00:00
|
|
|
|
2020-01-13 21:09:06 +00:00
|
|
|
dim.asc += topOffset(mi.base.bv);
|
|
|
|
dim.des += bottomOffset(mi.base.bv);
|
2010-11-18 19:50:29 +00:00
|
|
|
// insert a one pixel gap
|
|
|
|
dim.wid += 1;
|
|
|
|
Dimension dim_dummy;
|
|
|
|
MetricsInfo mi_dummy = mi;
|
|
|
|
InsetText::metrics(mi_dummy, dim_dummy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InsetText::metrics(mi, dim);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool InsetPreview::notifyCursorLeaves(Cursor const & old, Cursor & cur)
|
|
|
|
{
|
|
|
|
reloadPreview(old);
|
|
|
|
cur.screenUpdateFlags(Update::Force);
|
|
|
|
return InsetText::notifyCursorLeaves(old, cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|