lyx_mirror/src/insets/InsetPreview.cpp

195 lines
4.2 KiB
C++
Raw Normal View History

/**
* \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"
#include "Lexer.h"
#include "MetricsInfo.h"
#include "OutputParams.h"
#include "RenderPreview.h"
#include "frontends/Painter.h"
#include "graphics/PreviewImage.h"
2015-05-30 22:52:49 +00:00
#include "mathed/MacroTable.h"
#include "support/lstrings.h"
#include <sstream>
using namespace std;
namespace lyx {
2015-05-30 22:52:49 +00:00
using support::prefixIs;
InsetPreview::InsetPreview(Buffer * buf)
: InsetText(buf),
preview_(new RenderPreview(this)), use_preview_(true)
{
setDrawFrame(true);
setFrameColor(Color_previewframe);
}
InsetPreview::~InsetPreview()
{}
InsetPreview::InsetPreview(InsetPreview const & other)
: InsetText(other)
{
preview_.reset(new RenderPreview(*other.preview_, this));
}
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);
}
void InsetPreview::preparePreview(DocIterator const & pos) const
{
TexRow texrow;
2015-04-24 04:10:00 +00:00
odocstringstream str;
otexstream os(str, texrow);
2015-05-30 22:52:49 +00:00
Buffer const * buffer = pos.buffer();
OutputParams runparams(&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-05-30 22:52:49 +00:00
// collect macros at this position in case they are used in a math inset
MacroNameSet macros;
buffer->listMacroNames(macros);
MacroNameSet::iterator it = macros.begin();
MacroNameSet::iterator end = macros.end();
docstring macro_preamble;
for (; it != end; ++it) {
MacroData const * data = buffer->getMacro(*it, pos, true);
if (data) {
odocstringstream macro_def;
data->write(macro_def, false);
docstring const md = macro_def.str();
macro_def << endl;
bool is_newcomm = prefixIs(md, from_ascii("\\newcomm"));
// assure that \newcommand defs are only added once
if (!is_newcomm || !preview_->hasMacroDef(md, *buffer)) {
if (is_newcomm)
preview_->addMacroDef(md, *buffer);
macro_preamble.append(macro_def.str());
}
}
}
docstring const snippet = macro_preamble + str.str();
preview_->addPreview(snippet, *buffer);
}
bool InsetPreview::previewState(BufferView * bv) const
{
if (!editing(bv) && RenderPreview::previewText()) {
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
{
use_preview_ = previewState(pi.base.bv);
if (use_preview_) {
// one pixel gap in front
preview_->draw(pi, x + 1 + TEXT_TO_INSET_OFFSET, y);
setPosCache(pi, x, y);
return;
}
InsetText::draw(pi, x, y);
}
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)
{
if (use_preview_) {
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);
mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
dim.wid = max(dim.wid, 4);
dim.asc = max(dim.asc, 4);
dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
dim.wid += TEXT_TO_INSET_OFFSET;
dim_ = dim;
dim.wid += TEXT_TO_INSET_OFFSET;
// insert a one pixel gap
dim.wid += 1;
// Cache the inset dimension.
setDimCache(mi, dim);
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