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) != "separator")
114 InsetSeparatorParams params;
115 string2params(to_utf8(cmd.argument()), params);
116 status.setOnOff(params_.kind == params.kind);
117 status.setEnabled(true);
121 return Inset::getStatus(cur, cmd, status);
127 ColorCode InsetSeparator::ColorName() const
129 switch (params_.kind) {
130 case InsetSeparatorParams::PLAIN:
133 case InsetSeparatorParams::PARBREAK:
134 return Color_pagebreak;
137 // not really useful, but to avoids gcc complaints
142 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
144 // Do nothing if a paragraph break was just output
145 if (!os.afterParbreak()) {
146 switch (params_.kind) {
147 case InsetSeparatorParams::PLAIN:
148 os << breakln << "%\n";
150 case InsetSeparatorParams::PARBREAK:
151 os << breakln << "\n";
154 os << breakln << "%\n";
161 int InsetSeparator::plaintext(odocstringstream & os,
162 OutputParams const &, size_t) const
165 return PLAINTEXT_NEWLINE;
169 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
176 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
178 xs << html::CR() << html::CompTag("br") << html::CR();
183 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
185 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
186 dim.asc = fm.maxAscent();
187 dim.des = fm.maxDescent();
188 dim.wid = fm.width('m');
189 if (params_.kind == InsetSeparatorParams::PLAIN)
194 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
197 font.setColor(ColorName());
199 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
200 int const wid = fm.width('m');
201 int const asc = fm.maxAscent();
206 if (params_.kind == InsetSeparatorParams::PLAIN) {
207 yp[0] = int(y - 0.500 * asc * 0.75);
208 yp[1] = int(y - 0.500 * asc * 0.75);
211 xp[1] = int(x + wid * 5);
213 pi.pain.lines(xp, yp, 2, ColorName());
215 yp[0] = int(y - 0.875 * asc * 0.75);
216 yp[1] = int(y - 0.500 * asc * 0.75);
217 yp[2] = int(y - 0.125 * asc * 0.75);
220 xp[0] = int(x + wid * 0.375);
222 xp[2] = int(x + wid * 0.375);
224 xp[0] = int(x + wid * 0.625);
225 xp[1] = int(x + wid);
226 xp[2] = int(x + wid * 0.625);
229 pi.pain.lines(xp, yp, 3, ColorName());
231 yp[0] = int(y - 0.500 * asc * 0.75);
232 yp[1] = int(y - 0.500 * asc * 0.75);
233 yp[2] = int(y - asc * 0.75);
237 xp[1] = int(x + wid);
238 xp[2] = int(x + wid);
240 xp[0] = int(x + wid);
245 pi.pain.lines(xp, yp, 3, ColorName());
250 string InsetSeparator::contextMenuName() const
252 return "context-separator";
256 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
258 params = InsetSeparatorParams();
261 istringstream data(in);
264 lex.setContext("InsetSeparator::string2params");
270 string InsetSeparator::params2string(InsetSeparatorParams const & params)
273 data << "separator" << ' ';