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"
33 using namespace lyx::frontend;
37 InsetSeparator::InsetSeparator() : Inset(0)
41 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
42 : Inset(0), params_(params)
46 void InsetSeparatorParams::write(ostream & os) const
49 case InsetSeparatorParams::PLAIN:
52 case InsetSeparatorParams::PARBREAK:
55 case InsetSeparatorParams::LATEXPAR:
62 void InsetSeparatorParams::read(Lexer & lex)
65 lex.setContext("InsetSeparatorParams::read");
68 kind = InsetSeparatorParams::PLAIN;
69 else if (token == "parbreak")
70 kind = InsetSeparatorParams::PARBREAK;
71 else if (token == "latexpar")
72 kind = InsetSeparatorParams::LATEXPAR;
74 lex.printError("Unknown kind: `$$Token'");
78 void InsetSeparator::write(ostream & os) const
85 void InsetSeparator::read(Lexer & lex)
92 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
94 switch (cmd.action()) {
96 case LFUN_INSET_MODIFY: {
97 InsetSeparatorParams params;
99 string2params(to_utf8(cmd.argument()), params);
100 params_.kind = params.kind;
105 Inset::doDispatch(cur, cmd);
111 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
112 FuncStatus & status) const
114 switch (cmd.action()) {
116 case LFUN_INSET_MODIFY: {
117 if (cmd.getArg(0) != "separator")
119 InsetSeparatorParams params;
120 string2params(to_utf8(cmd.argument()), params);
121 status.setOnOff(params_.kind == params.kind);
122 status.setEnabled(true);
126 return Inset::getStatus(cur, cmd, status);
132 ColorCode InsetSeparator::ColorName() const
138 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
140 // Do nothing if a paragraph break was just output
141 if (!os.afterParbreak()) {
142 switch (params_.kind) {
143 case InsetSeparatorParams::PLAIN:
144 os << breakln << "%\n";
146 case InsetSeparatorParams::PARBREAK:
147 case InsetSeparatorParams::LATEXPAR:
148 os << breakln << "\n";
151 os << breakln << "%\n";
158 int InsetSeparator::plaintext(odocstringstream & os,
159 OutputParams const &, size_t) const
162 return PLAINTEXT_NEWLINE;
166 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
173 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
175 xs << html::CR() << html::CompTag("br") << html::CR();
180 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
182 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
183 dim.asc = fm.maxAscent();
184 dim.des = fm.maxDescent();
185 dim.wid = fm.width('n');
186 if (params_.kind != InsetSeparatorParams::LATEXPAR)
191 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
194 font.setColor(ColorName());
196 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
197 int const wid = fm.width('n');
198 int const asc = fm.maxAscent();
203 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
204 yp[0] = int(y - 0.500 * asc * 0.75);
208 xp[1] = int(x + wid * 8);
210 pi.pain.lines(xp, yp, 2, ColorName());
212 if (params_.kind == InsetSeparatorParams::PARBREAK) {
213 yp[0] += 0.25 * asc * 0.75;
215 pi.pain.lines(xp, yp, 2, ColorName());
218 yp[0] = int(y - 0.500 * asc * 0.5);
219 yp[1] = int(y - 0.250 * asc * 0.5);
223 xp[0] = int(x + 1 + wid * 0.375);
226 xp[0] = int(x - 1 + wid * 0.625);
227 xp[1] = int(x - 1 + wid);
231 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
234 yp[2] = int(y - 0.850 * asc * 0.5);
235 yp[3] = int(y - 1.250 * asc * 0.5);
242 xp[1] = int(x + 1 + wid * 0.50);
243 xp[2] = int(x + wid);
245 xp[4] = int(x + wid * 0.75);
247 xp[1] = int(x + wid * 0.50);
250 xp[4] = int(x + wid * 0.25);
260 for (int i = 1; i < 7; ++i) {
267 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
270 c2y[2] = int(y - 0.500 * asc * 0.5);
274 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
279 string InsetSeparator::contextMenuName() const
281 if (params_.kind == InsetSeparatorParams::LATEXPAR)
284 return "context-separator";
288 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
290 params = InsetSeparatorParams();
293 istringstream data(in);
296 lex.setContext("InsetSeparator::string2params");
302 string InsetSeparator::params2string(InsetSeparatorParams const & params)
305 data << "separator" << ' ';