3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetLine.h"
17 #include "Dimension.h"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "LaTeXFeatures.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
41 using frontend::Painter;
44 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
45 : InsetCommand(buf, p, "line")
49 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
51 static ParamInfo param_info_;
52 if (param_info_.empty()) {
53 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
54 param_info_.add("width", ParamInfo::LYX_INTERNAL);
55 param_info_.add("height", ParamInfo::LYX_INTERNAL);
61 docstring InsetLine::screenLabel() const
63 return _("Horizontal line");
67 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
69 switch (cmd.action()) {
71 case LFUN_INSET_MODIFY: {
72 InsetCommandParams p(LINE_CODE);
74 InsetCommand::string2params("line",
75 to_utf8(cmd.argument()), p);
76 if (p.getCmdName().empty()) {
85 InsetCommand::doDispatch(cur, cmd);
91 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
92 FuncStatus & status) const
94 switch (cmd.action()) {
95 case LFUN_INSET_DIALOG_UPDATE:
96 case LFUN_INSET_MODIFY:
97 status.setEnabled(true);
100 return InsetCommand::getStatus(cur, cmd, status);
105 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
107 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
108 dim.asc = fm.maxAscent();
109 dim.des = fm.maxDescent();
111 Length width = Length(to_ascii(getParam("width")));
113 width.inPixels(mi.base.textwidth,
114 fm.width(char_type('M')));
116 // assure that the line inset is not outside of the window
117 // check that it doesn't exceed the outer boundary
118 if (w > mi.base.textwidth)
119 w = mi.base.textwidth;
121 // set a minimal width
122 int const minw = (w < 0) ? 3 * 8 : 4;
123 dim.wid = max(minw, abs(w));
125 // Cache the inset dimension
126 setDimCache(mi, dim);
130 void InsetLine::draw(PainterInfo & pi, int x, int y) const
132 Dimension const dim = dimension(*pi.base.bv);
134 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
136 // get the length of the parameters in pixels
137 Length offset = Length(to_ascii(getParam("offset")));
139 offset.inPixels(pi.base.textwidth,
140 fm.width(char_type('M')));
141 Length width = Length(to_ascii(getParam("width")));
143 width.inPixels(pi.base.textwidth,
144 fm.width(char_type('M')));
145 Length height = Length(to_ascii(getParam("height")));
147 height.inPixels(pi.base.textwidth,
148 fm.width(char_type('M')));
150 // get the surrounding text color
151 FontInfo f = pi.base.font;
152 Color Line_color = f.realColor();
154 // assure that the drawn line is not outside of the window
155 // check that it doesn't exceed the outer boundary
156 if (x + w - h/2 - 2 > pi.base.textwidth)
157 w = pi.base.textwidth - x + h/2 + 2;
158 // check that it doesn't exceed the upper boundary
162 // the offset is a vertical one
163 pi.pain.line(x + h/2 + 1, y - o - h/2, x + w - h/2 - 2, y - o - h/2,
164 Line_color, Painter::line_solid, float(h));
168 int InsetLine::latex(odocstream & os, OutputParams const & runparams) const
170 bool have_offset = true;
171 Length offset_len = Length(to_ascii(getParam("offset")));
172 if (offset_len.value() == 0)
175 string const offset =
176 Length(to_ascii(getParam("offset"))).asLatexString();
178 Length(to_ascii(getParam("width"))).asLatexString();
179 string const height =
180 Length(to_ascii(getParam("height"))).asLatexString();
183 // only output the optional parameter if the offset is not 0
185 os << "[" << from_ascii(offset) << "]";
186 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
192 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
194 os << "\n-------------------------------------------\n";
195 return PLAINTEXT_NEWLINE;
199 int InsetLine::docbook(odocstream & os, OutputParams const &) const
206 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
208 xs << html::CompTag("hr");