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"
42 using frontend::Painter;
45 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
46 : InsetCommand(buf, p)
50 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
52 static ParamInfo param_info_;
53 if (param_info_.empty()) {
54 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
55 param_info_.add("width", ParamInfo::LYX_INTERNAL);
56 param_info_.add("height", ParamInfo::LYX_INTERNAL);
62 docstring InsetLine::screenLabel() const
64 return _("Horizontal line");
68 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
70 switch (cmd.action()) {
72 case LFUN_INSET_MODIFY: {
73 InsetCommandParams p(LINE_CODE);
75 InsetCommand::string2params(to_utf8(cmd.argument()), p);
76 if (p.getCmdName().empty()) {
86 InsetCommand::doDispatch(cur, cmd);
92 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
93 FuncStatus & status) const
95 switch (cmd.action()) {
96 case LFUN_INSET_DIALOG_UPDATE:
97 case LFUN_INSET_MODIFY:
98 status.setEnabled(true);
101 return InsetCommand::getStatus(cur, cmd, status);
106 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
108 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
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 Length height = Length(to_ascii(getParam("height")));
124 height_ = height.inPixels(dim.height(), fm.width(char_type('M')));
126 // get the length of the parameters in pixels
127 Length offset = Length(to_ascii(getParam("offset")));
128 offset_ = offset.inPixels(max_width, fm.width(char_type('M')));
130 dim.asc = max(fm.maxAscent(), offset_ + height_);
131 dim.des = max(fm.maxDescent(), - offset_);
133 // Cache the inset dimension
134 setDimCache(mi, dim);
138 Dimension const InsetLine::dimension(BufferView const & bv) const
140 // We cannot use InsetCommand::dimension() as this returns the dimension
141 // of the button, which is not used here.
142 return Inset::dimension(bv);
146 void InsetLine::draw(PainterInfo & pi, int x, int y) const
148 Dimension const dim = dimension(*pi.base.bv);
150 // get the surrounding text color
151 Color Line_color = pi.base.font.realColor();
153 // the offset is a vertical one
154 // the horizontal dimension must be corrected with the heigth because
155 // of left and right border of the painted line for big heigth.
156 pi.pain.line(x + height_/2 + 1,
157 y - offset_ - height_/2,
158 x + dim.wid - height_/2 - 2,
159 y - offset_ - height_/2,
160 Line_color, Painter::line_solid, float(height_));
164 void InsetLine::latex(otexstream & os, OutputParams const &) const
166 bool have_offset = true;
167 Length offset_len = Length(to_ascii(getParam("offset")));
168 if (offset_len.value() == 0)
171 string const offset =
172 Length(to_ascii(getParam("offset"))).asLatexString();
174 Length(to_ascii(getParam("width"))).asLatexString();
175 string const height =
176 Length(to_ascii(getParam("height"))).asLatexString();
179 // only output the optional parameter if the offset is not 0
181 os << "[" << from_ascii(offset) << "]";
182 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
186 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
188 os << "\n-------------------------------------------\n";
189 return PLAINTEXT_NEWLINE;
193 int InsetLine::docbook(odocstream & os, OutputParams const &) const
200 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
202 xs << html::CompTag("hr") << html::CR();