2 * \file InsetSeparator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Enrico Forestieri
8 * Full author contact details are available in file CREDITS.
13 #include "InsetSeparator.h"
16 #include "Dimension.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
24 #include "frontends/Application.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/docstring.h"
36 InsetSeparator::InsetSeparator() : Inset(0)
40 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
41 : Inset(0), params_(params)
45 void InsetSeparatorParams::write(ostream & os) const
49 case InsetSeparatorParams::PLAIN:
52 case InsetSeparatorParams::PARBREAK:
59 void InsetSeparatorParams::read(Lexer & lex)
62 lex.setContext("InsetSeparatorParams::read");
65 kind = InsetSeparatorParams::PLAIN;
66 else if (token == "parbreak")
67 kind = InsetSeparatorParams::PARBREAK;
69 lex.printError("Unknown kind: `$$Token'");
73 void InsetSeparator::write(ostream & os) const
80 void InsetSeparator::read(Lexer & lex)
87 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
89 switch (cmd.action()) {
91 case LFUN_INSET_MODIFY: {
92 InsetSeparatorParams params;
94 string2params(to_utf8(cmd.argument()), params);
95 params_.kind = params.kind;
100 Inset::doDispatch(cur, cmd);
106 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
107 FuncStatus & status) const
109 switch (cmd.action()) {
111 case LFUN_INSET_MODIFY:
112 if (cmd.getArg(0) == "plain") {
113 InsetSeparatorParams params;
114 string2params(to_utf8(cmd.argument()), params);
115 status.setOnOff(params_.kind == params.kind);
117 status.setEnabled(true);
120 return Inset::getStatus(cur, cmd, status);
125 ColorCode InsetSeparator::ColorName() const
127 switch (params_.kind) {
128 case InsetSeparatorParams::PLAIN:
131 case InsetSeparatorParams::PARBREAK:
132 return Color_pagebreak;
135 // not really useful, but to avoids gcc complaints
140 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
142 // Do nothing if a paragraph break was just output
143 if (!os.afterParbreak()) {
144 switch (params_.kind) {
145 case InsetSeparatorParams::PLAIN:
146 os << breakln << "%\n";
148 case InsetSeparatorParams::PARBREAK:
149 os << breakln << "\n";
152 os << breakln << "%\n";
159 int InsetSeparator::plaintext(odocstringstream & os,
160 OutputParams const &, size_t) const
163 return PLAINTEXT_NEWLINE;
167 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
174 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
176 xs << html::CR() << html::CompTag("br") << html::CR();
181 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
183 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
184 dim.asc = fm.maxAscent();
185 dim.des = fm.maxDescent();
186 dim.wid = fm.width('m');
187 if (params_.kind == InsetSeparatorParams::PLAIN)
192 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
195 font.setColor(ColorName());
197 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
198 int const wid = fm.width('m');
199 int const asc = fm.maxAscent();
204 if (params_.kind == InsetSeparatorParams::PLAIN) {
205 yp[0] = int(y - 0.500 * asc * 0.75);
206 yp[1] = int(y - 0.500 * asc * 0.75);
209 xp[1] = int(x + wid * 5);
211 pi.pain.lines(xp, yp, 2, ColorName());
213 yp[0] = int(y - 0.875 * asc * 0.75);
214 yp[1] = int(y - 0.500 * asc * 0.75);
215 yp[2] = int(y - 0.125 * asc * 0.75);
218 xp[0] = int(x + wid * 0.375);
220 xp[2] = int(x + wid * 0.375);
222 xp[0] = int(x + wid * 0.625);
223 xp[1] = int(x + wid);
224 xp[2] = int(x + wid * 0.625);
227 pi.pain.lines(xp, yp, 3, ColorName());
229 yp[0] = int(y - 0.500 * asc * 0.75);
230 yp[1] = int(y - 0.500 * asc * 0.75);
231 yp[2] = int(y - asc * 0.75);
235 xp[1] = int(x + wid);
236 xp[2] = int(x + wid);
238 xp[0] = int(x + wid);
243 pi.pain.lines(xp, yp, 3, ColorName());
248 string InsetSeparator::contextMenuName() const
250 return "context-separator";
254 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
256 params = InsetSeparatorParams();
259 istringstream data(in);
262 lex.setContext("InsetSeparator::string2params");
268 string InsetSeparator::params2string(InsetSeparatorParams const & params)
271 data << "separator" << ' ';