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 &) 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 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('n');
187 if (params_.kind != InsetSeparatorParams::LATEXPAR)
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('n');
199 int const asc = fm.maxAscent();
204 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
205 yp[0] = int(y - 0.500 * asc * 0.75);
209 xp[1] = int(x + wid * 8);
211 pi.pain.lines(xp, yp, 2, ColorName());
213 if (params_.kind == InsetSeparatorParams::PARBREAK) {
214 yp[0] += int(0.25 * asc * 0.75);
216 pi.pain.lines(xp, yp, 2, ColorName());
219 yp[0] = int(y - 0.500 * asc * 0.5);
220 yp[1] = int(y - 0.250 * asc * 0.5);
224 xp[0] = int(x + 1 + wid * 0.375);
227 xp[0] = int(x - 1 + wid * 0.625);
228 xp[1] = int(x - 1 + wid);
232 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
235 yp[2] = int(y - 0.850 * asc * 0.5);
236 yp[3] = int(y - 1.250 * asc * 0.5);
243 xp[1] = int(x + 1 + wid * 0.50);
244 xp[2] = int(x + wid);
246 xp[4] = int(x + wid * 0.75);
248 xp[1] = int(x + wid * 0.50);
251 xp[4] = int(x + wid * 0.25);
261 for (int i = 1; i < 7; ++i) {
268 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
271 c2y[2] = int(y - 0.500 * asc * 0.5);
275 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
280 string InsetSeparator::contextMenuName() const
282 if (params_.kind == InsetSeparatorParams::LATEXPAR)
285 return "context-separator";
289 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
291 params = InsetSeparatorParams();
294 istringstream data(in);
297 lex.setContext("InsetSeparator::string2params");
303 string InsetSeparator::params2string(InsetSeparatorParams const & params)
306 data << "separator" << ' ';