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"
23 #include "texstream.h"
25 #include "frontends/Application.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/docstring.h"
34 using namespace lyx::frontend;
38 InsetSeparator::InsetSeparator() : Inset(0)
42 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
43 : Inset(0), params_(params)
47 void InsetSeparatorParams::write(ostream & os) const
50 case InsetSeparatorParams::PLAIN:
53 case InsetSeparatorParams::PARBREAK:
56 case InsetSeparatorParams::LATEXPAR:
63 void InsetSeparatorParams::read(Lexer & lex)
66 lex.setContext("InsetSeparatorParams::read");
69 kind = InsetSeparatorParams::PLAIN;
70 else if (token == "parbreak")
71 kind = InsetSeparatorParams::PARBREAK;
72 else if (token == "latexpar")
73 kind = InsetSeparatorParams::LATEXPAR;
75 lex.printError("Unknown kind: `$$Token'");
79 void InsetSeparator::write(ostream & os) const
86 void InsetSeparator::read(Lexer & lex)
93 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
95 switch (cmd.action()) {
97 case LFUN_INSET_MODIFY: {
98 InsetSeparatorParams params;
100 string2params(to_utf8(cmd.argument()), params);
101 params_.kind = params.kind;
106 Inset::doDispatch(cur, cmd);
112 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
113 FuncStatus & status) const
115 switch (cmd.action()) {
117 case LFUN_INSET_MODIFY: {
118 if (cmd.getArg(0) != "separator")
120 InsetSeparatorParams params;
121 string2params(to_utf8(cmd.argument()), params);
122 status.setOnOff(params_.kind == params.kind);
123 status.setEnabled(true);
127 return Inset::getStatus(cur, cmd, status);
133 ColorCode InsetSeparator::ColorName() const
139 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
141 // Do nothing if a paragraph break was just output
142 if (!os.afterParbreak()) {
143 switch (params_.kind) {
144 case InsetSeparatorParams::PLAIN:
145 os << breakln << "%\n";
147 case InsetSeparatorParams::PARBREAK:
148 case InsetSeparatorParams::LATEXPAR:
149 if (runparams.inDeletedInset)
150 os << breakln << "}\n\n{";
152 os << breakln << "\n";
155 os << breakln << "%\n";
162 int InsetSeparator::plaintext(odocstringstream & os,
163 OutputParams const &, size_t) const
166 return PLAINTEXT_NEWLINE;
170 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
177 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
179 xs << html::CR() << html::CompTag("br") << html::CR();
184 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
186 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
187 dim.asc = fm.maxAscent();
188 dim.des = fm.maxDescent();
189 dim.wid = fm.width('n');
190 if (params_.kind != InsetSeparatorParams::LATEXPAR)
195 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
198 font.setColor(ColorName());
200 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
201 int const wid = fm.width('n');
202 int const asc = fm.maxAscent();
207 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
208 yp[0] = int(y - 0.500 * asc * 0.75);
212 xp[1] = int(x + wid * 8);
214 pi.pain.lines(xp, yp, 2, ColorName());
216 if (params_.kind == InsetSeparatorParams::PARBREAK) {
217 yp[0] += int(0.25 * asc * 0.75);
219 pi.pain.lines(xp, yp, 2, ColorName());
222 yp[0] = int(y - 0.500 * asc * 0.5);
223 yp[1] = int(y - 0.250 * asc * 0.5);
227 xp[0] = int(x + 1 + wid * 0.375);
230 xp[0] = int(x - 1 + wid * 0.625);
231 xp[1] = int(x - 1 + wid);
235 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
238 yp[2] = int(y - 0.850 * asc * 0.5);
239 yp[3] = int(y - 1.250 * asc * 0.5);
246 xp[1] = int(x + 1 + wid * 0.50);
247 xp[2] = int(x + wid);
249 xp[4] = int(x + wid * 0.75);
251 xp[1] = int(x + wid * 0.50);
254 xp[4] = int(x + wid * 0.25);
264 for (int i = 1; i < 7; ++i) {
271 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
274 c2y[2] = int(y - 0.500 * asc * 0.5);
278 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
283 string InsetSeparator::contextMenuName() const
285 if (params_.kind == InsetSeparatorParams::LATEXPAR)
288 return "context-separator";
292 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
294 params = InsetSeparatorParams();
297 istringstream data(in);
300 lex.setContext("InsetSeparator::string2params");
306 string InsetSeparator::params2string(InsetSeparatorParams const & params)
309 data << "separator" << ' ';