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
50 case InsetSeparatorParams::PLAIN:
53 case InsetSeparatorParams::PARBREAK:
60 void InsetSeparatorParams::read(Lexer & lex)
63 lex.setContext("InsetSeparatorParams::read");
66 kind = InsetSeparatorParams::PLAIN;
67 else if (token == "parbreak")
68 kind = InsetSeparatorParams::PARBREAK;
70 lex.printError("Unknown kind: `$$Token'");
74 void InsetSeparator::write(ostream & os) const
81 void InsetSeparator::read(Lexer & lex)
88 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
90 switch (cmd.action()) {
92 case LFUN_INSET_MODIFY: {
93 InsetSeparatorParams params;
95 string2params(to_utf8(cmd.argument()), params);
96 params_.kind = params.kind;
101 Inset::doDispatch(cur, cmd);
107 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
108 FuncStatus & status) const
110 switch (cmd.action()) {
112 case LFUN_INSET_MODIFY: {
113 if (cmd.getArg(0) != "separator")
115 InsetSeparatorParams params;
116 string2params(to_utf8(cmd.argument()), params);
117 status.setOnOff(params_.kind == params.kind);
118 status.setEnabled(true);
122 return Inset::getStatus(cur, cmd, status);
128 ColorCode InsetSeparator::ColorName() const
134 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
136 // Do nothing if a paragraph break was just output
137 if (!os.afterParbreak()) {
138 switch (params_.kind) {
139 case InsetSeparatorParams::PLAIN:
140 os << breakln << "%\n";
142 case InsetSeparatorParams::PARBREAK:
143 os << breakln << "\n";
146 os << breakln << "%\n";
153 int InsetSeparator::plaintext(odocstringstream & os,
154 OutputParams const &, size_t) const
157 return PLAINTEXT_NEWLINE;
161 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
168 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
170 xs << html::CR() << html::CompTag("br") << html::CR();
175 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
177 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
178 dim.asc = fm.maxAscent();
179 dim.des = fm.maxDescent();
180 dim.wid = fm.width('m');
181 if (params_.kind == InsetSeparatorParams::PLAIN)
186 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
189 font.setColor(ColorName());
191 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
192 int const wid = fm.width('m');
193 int const asc = fm.maxAscent();
198 if (params_.kind == InsetSeparatorParams::PLAIN) {
199 yp[0] = int(y - 0.500 * asc * 0.75);
200 yp[1] = int(y - 0.500 * asc * 0.75);
203 xp[1] = int(x + wid * 5);
205 pi.pain.lines(xp, yp, 2, ColorName());
207 yp[0] = int(y - 0.875 * asc * 0.5);
208 yp[1] = int(y - 0.500 * asc * 0.5);
209 yp[2] = int(y - 0.125 * asc * 0.5);
212 xp[0] = int(x + wid * 0.375);
214 xp[2] = int(x + wid * 0.375);
216 xp[0] = int(x + wid * 0.625);
217 xp[1] = int(x + wid);
218 xp[2] = int(x + wid * 0.625);
221 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
223 yp[0] = int(y - 0.500 * asc * 0.5);
224 yp[1] = int(y - 0.500 * asc * 0.5);
225 yp[2] = int(y - asc * 0.5);
229 xp[1] = int(x + wid);
230 xp[2] = int(x + wid);
232 xp[0] = int(x + wid);
237 pi.pain.lines(xp, yp, 3, ColorName());
242 string InsetSeparator::contextMenuName() const
244 return "context-separator";
248 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
250 params = InsetSeparatorParams();
253 istringstream data(in);
256 lex.setContext("InsetSeparator::string2params");
262 string InsetSeparator::params2string(InsetSeparatorParams const & params)
265 data << "separator" << ' ';