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/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
40 using frontend::Painter;
43 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
44 : InsetCommand(buf, p, "line")
48 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
50 static ParamInfo param_info_;
51 if (param_info_.empty()) {
52 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
53 param_info_.add("width", ParamInfo::LYX_INTERNAL);
54 param_info_.add("height", ParamInfo::LYX_INTERNAL);
60 docstring InsetLine::screenLabel() const
62 return _("Horizontal line");
66 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
68 switch (cmd.action()) {
70 case LFUN_INSET_MODIFY: {
71 InsetCommandParams p(LINE_CODE);
73 InsetCommand::string2params("line",
74 to_utf8(cmd.argument()), p);
75 if (p.getCmdName().empty()) {
84 InsetCommand::doDispatch(cur, cmd);
90 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
91 FuncStatus & status) const
93 switch (cmd.action()) {
94 case LFUN_INSET_DIALOG_UPDATE:
95 case LFUN_INSET_MODIFY:
96 status.setEnabled(true);
99 return InsetCommand::getStatus(cur, cmd, status);
104 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
106 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
107 dim.asc = fm.maxAscent();
108 dim.des = fm.maxDescent();
109 int const max_width = mi.base.textwidth;
111 Length const width(to_ascii(getParam("width")));
112 dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
114 // assure that the line inset is not outside of the window
115 // check that it doesn't exceed the outer boundary
116 if (dim.wid > max_width)
119 // set a minimal width
120 int const minw = (dim.wid < 0) ? 24 : 4;
121 dim.wid = max(minw, max(dim.wid, -dim.wid));
123 // Cache the inset dimension
124 setDimCache(mi, dim);
128 Dimension const InsetLine::dimension(BufferView const & bv) const
130 // We cannot use InsetCommand::dimension() as this returns the dimension
131 // of the button, which is not used here.
132 return Inset::dimension(bv);
136 void InsetLine::draw(PainterInfo & pi, int x, int y) const
138 Dimension const dim = dimension(*pi.base.bv);
139 int const max_width = dim.width();
141 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
143 Length height = Length(to_ascii(getParam("height")));
144 int const h = height.inPixels(dim.height(), fm.width(char_type('M')));
146 // get the length of the parameters in pixels
147 Length offset = Length(to_ascii(getParam("offset")));
148 int o = offset.inPixels(max_width, fm.width(char_type('M')));
150 // check that it doesn't exceed the upper boundary
154 // get the surrounding text color
155 Color Line_color = pi.base.font.realColor();
157 // the offset is a vertical one
158 pi.pain.line(x + 1, y - o - h/2, x + dim.wid - 2, y - o - h/2,
159 Line_color, Painter::line_solid, float(h));
163 int InsetLine::latex(odocstream & os, OutputParams const &) const
165 bool have_offset = true;
166 Length offset_len = Length(to_ascii(getParam("offset")));
167 if (offset_len.value() == 0)
170 string const offset =
171 Length(to_ascii(getParam("offset"))).asLatexString();
173 Length(to_ascii(getParam("width"))).asLatexString();
174 string const height =
175 Length(to_ascii(getParam("height"))).asLatexString();
178 // only output the optional parameter if the offset is not 0
180 os << "[" << from_ascii(offset) << "]";
181 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
187 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
189 os << "\n-------------------------------------------\n";
190 return PLAINTEXT_NEWLINE;
194 int InsetLine::docbook(odocstream & os, OutputParams const &) const
201 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
203 xs << html::CompTag("hr");