lyx_mirror/src/insets/InsetNewline.cpp

294 lines
5.6 KiB
C++
Raw Normal View History

/**
* \file InsetNewline.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author John Levon
* \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetNewline.h"
#include "Cursor.h"
#include "Dimension.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
#include "Lexer.h"
#include "MetricsInfo.h"
#include "OutputParams.h"
#include "output_xhtml.h"
#include "frontends/Application.h"
#include "frontends/FontMetrics.h"
#include "frontends/Painter.h"
#include "support/debug.h"
#include "support/docstream.h"
#include "support/docstring.h"
using namespace std;
namespace lyx {
InsetNewline::InsetNewline() : Inset(0)
{}
void InsetNewlineParams::write(ostream & os) const
{
string command;
switch (kind) {
case InsetNewlineParams::NEWLINE:
os << "newline";
break;
case InsetNewlineParams::LINEBREAK:
os << "linebreak";
break;
}
}
void InsetNewlineParams::read(Lexer & lex)
{
string token;
lex.setContext("InsetNewlineParams::read");
lex >> token;
if (token == "newline")
kind = InsetNewlineParams::NEWLINE;
else if (token == "linebreak")
kind = InsetNewlineParams::LINEBREAK;
else
lex.printError("Unknown kind: `$$Token'");
}
void InsetNewline::write(ostream & os) const
{
os << "Newline ";
params_.write(os);
}
void InsetNewline::read(Lexer & lex)
{
params_.read(lex);
lex >> "\\end_inset";
}
void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
{
frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
dim.asc = fm.maxAscent();
dim.des = fm.maxDescent();
dim.wid = fm.width('n');
}
void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
{
switch (cmd.action()) {
case LFUN_INSET_MODIFY: {
InsetNewlineParams params;
cur.recordUndo();
string2params(to_utf8(cmd.argument()), params);
params_.kind = params.kind;
break;
}
default:
Inset::doDispatch(cur, cmd);
break;
}
}
bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
FuncStatus & status) const
{
switch (cmd.action()) {
// we handle these
case LFUN_INSET_MODIFY:
if (cmd.getArg(0) == "newline") {
InsetNewlineParams params;
string2params(to_utf8(cmd.argument()), params);
status.setOnOff(params_.kind == params.kind);
}
status.setEnabled(true);
return true;
default:
return Inset::getStatus(cur, cmd, status);
}
}
ColorCode InsetNewline::ColorName() const
{
switch (params_.kind) {
case InsetNewlineParams::NEWLINE:
return Color_eolmarker;
break;
case InsetNewlineParams::LINEBREAK:
return Color_pagebreak;
break;
}
// not really useful, but to avoids gcc complaints
return Color_eolmarker;
}
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
int InsetNewline::latex(otexstream & os, OutputParams const & rp) const
{
switch (params_.kind) {
case InsetNewlineParams::NEWLINE:
if (rp.inTableCell == OutputParams::PLAIN)
os << "\\newline\n";
else
os << "\\\\\n";
break;
case InsetNewlineParams::LINEBREAK:
os << "\\linebreak{}\n";
break;
default:
os << "\\\\\n";
break;
}
return 0;
}
int InsetNewline::plaintext(odocstream & os, OutputParams const &) const
{
os << '\n';
return PLAINTEXT_NEWLINE;
}
int InsetNewline::docbook(odocstream & os, OutputParams const &) const
{
os << '\n';
return 0;
}
docstring InsetNewline::xhtml(XHTMLStream & xs, OutputParams const &) const
{
xs << html::CompTag("br");
xs.cr();
return docstring();
}
void InsetNewline::draw(PainterInfo & pi, int x, int y) const
{
FontInfo font;
font.setColor(ColorName());
frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
int const wid = fm.width('n');
int const asc = fm.maxAscent();
int xp[3];
int yp[3];
yp[0] = int(y - 0.875 * asc * 0.75);
yp[1] = int(y - 0.500 * asc * 0.75);
yp[2] = int(y - 0.125 * asc * 0.75);
if (pi.ltr_pos) {
xp[0] = int(x + wid * 0.375);
xp[1] = int(x);
xp[2] = int(x + wid * 0.375);
} else {
xp[0] = int(x + wid * 0.625);
xp[1] = int(x + wid);
xp[2] = int(x + wid * 0.625);
}
pi.pain.lines(xp, yp, 3, ColorName());
yp[0] = int(y - 0.500 * asc * 0.75);
yp[1] = int(y - 0.500 * asc * 0.75);
yp[2] = int(y - asc * 0.75);
if (pi.ltr_pos) {
xp[0] = int(x);
xp[1] = int(x + wid);
xp[2] = int(x + wid);
} else {
xp[0] = int(x + wid);
xp[1] = int(x);
xp[2] = int(x);
}
pi.pain.lines(xp, yp, 3, ColorName());
if (params_.kind == InsetNewlineParams::LINEBREAK) {
yp[2] = int(y - 0.500 * asc * 0.75);
if (pi.ltr_pos) {
xp[0] = int(x + 1.3 * wid);
xp[1] = int(x + 2 * wid);
xp[2] = int(x + 2 * wid);
} else {
xp[0] = int(x - 0.3 * wid);
xp[1] = int(x - wid);
xp[2] = int(x - wid);
}
pi.pain.lines(xp, yp, 3, ColorName());
yp[0] = int(y - 0.875 * asc * 0.75);
yp[1] = int(y - 0.500 * asc * 0.75);
yp[2] = int(y - 0.125 * asc * 0.75);
if (pi.ltr_pos) {
xp[0] = int(x + 2 * wid * 0.813);
xp[1] = int(x + 2 * wid);
xp[2] = int(x + 2 * wid * 0.813);
} else {
xp[0] = int(x - wid * 0.625);
xp[1] = int(x - wid);
xp[2] = int(x - wid * 0.625);
}
pi.pain.lines(xp, yp, 3, ColorName());
}
}
docstring InsetNewline::contextMenuName() const
{
return from_ascii("context-newline");
}
void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
{
params = InsetNewlineParams();
if (in.empty())
return;
istringstream data(in);
Lexer lex;
lex.setStream(data);
lex.setContext("InsetNewline::string2params");
lex >> "newline";
params.read(lex);
}
string InsetNewline::params2string(InsetNewlineParams const & params)
{
ostringstream data;
data << "newline" << ' ';
params.write(data);
return data.str();
}
} // namespace lyx