3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "InsetLine.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
30 #include "frontends/FontMetrics.h"
31 #include "frontends/Painter.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
44 using frontend::Painter;
47 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
48 : InsetCommand(buf, p)
52 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
54 static ParamInfo param_info_;
55 if (param_info_.empty()) {
56 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
57 param_info_.add("width", ParamInfo::LYX_INTERNAL);
58 param_info_.add("height", ParamInfo::LYX_INTERNAL);
64 docstring InsetLine::screenLabel() const
66 return _("Horizontal line");
70 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
72 switch (cmd.action()) {
74 case LFUN_INSET_MODIFY: {
75 InsetCommandParams p(LINE_CODE);
77 InsetCommand::string2params(to_utf8(cmd.argument()), p);
78 if (p.getCmdName().empty()) {
88 InsetCommand::doDispatch(cur, cmd);
94 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
95 FuncStatus & status) const
97 switch (cmd.action()) {
98 case LFUN_INSET_DIALOG_UPDATE:
99 case LFUN_INSET_MODIFY:
100 status.setEnabled(true);
103 return InsetCommand::getStatus(cur, cmd, status);
108 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
110 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
111 int const max_width = mi.base.textwidth;
113 Length const width(to_ascii(getParam("width")));
114 dim.wid = width.inPixels(mi.base);
116 // assure that the line inset is not outside of the window
117 // check that it doesn't exceed the outer boundary
118 if (dim.wid > max_width)
121 // set a minimal width
122 int const minw = (dim.wid < 0) ? 24 : 4;
123 dim.wid = max(minw, abs(dim.wid));
125 Length height = Length(to_ascii(getParam("height")));
126 height_ = height.inPixels(mi.base);
128 // get the length of the parameters in pixels
129 Length offset = Length(to_ascii(getParam("offset")));
130 offset_ = offset.inPixels(mi.base);
132 dim.asc = max(fm.maxAscent(), offset_ + height_);
133 dim.des = max(fm.maxDescent(), - offset_);
135 // Cache the inset dimension
136 setDimCache(mi, dim);
140 Dimension const InsetLine::dimension(BufferView const & bv) const
142 // We cannot use InsetCommand::dimension() as this returns the dimension
143 // of the button, which is not used here.
144 return Inset::dimension(bv);
148 void InsetLine::draw(PainterInfo & pi, int x, int y) const
150 Dimension const dim = dimension(*pi.base.bv);
152 // get the surrounding text color
153 Color Line_color = pi.base.font.realColor();
155 // the offset is a vertical one
156 // the horizontal dimension must be corrected with the heigth because
157 // of left and right border of the painted line for big heigth.
158 pi.pain.line(x + height_/2 + 1,
159 y - offset_ - height_/2,
160 x + dim.wid - height_/2 - 2,
161 y - offset_ - height_/2,
162 Line_color, Painter::line_solid, float(height_));
166 void InsetLine::latex(otexstream & os, OutputParams const &) const
168 bool have_offset = true;
169 Length offset_len = Length(to_ascii(getParam("offset")));
170 if (offset_len.value() == 0)
173 string const offset =
174 Length(to_ascii(getParam("offset"))).asLatexString();
176 Length(to_ascii(getParam("width"))).asLatexString();
177 string const height =
178 Length(to_ascii(getParam("height"))).asLatexString();
181 // only output the optional parameter if the offset is not 0
183 os << "[" << from_ascii(offset) << "]";
184 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
188 int InsetLine::plaintext(odocstringstream & os,
189 OutputParams const &, size_t) const
191 os << "\n-------------------------------------------\n";
192 return PLAINTEXT_NEWLINE;
196 int InsetLine::docbook(odocstream & os, OutputParams const &) const
203 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
205 xs << html::CompTag("hr") << html::CR();