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"
18 #include "Dimension.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "output_xhtml.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.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 int const max_width = mi.base.textwidth;
110 Length const width(to_ascii(getParam("width")));
111 dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
113 // assure that the line inset is not outside of the window
114 // check that it doesn't exceed the outer boundary
115 if (dim.wid > max_width)
118 // set a minimal width
119 int const minw = (dim.wid < 0) ? 24 : 4;
120 dim.wid = max(minw, max(dim.wid, -dim.wid));
122 Length height = Length(to_ascii(getParam("height")));
123 height_ = height.inPixels(dim.height(), fm.width(char_type('M')));
125 // get the length of the parameters in pixels
126 Length offset = Length(to_ascii(getParam("offset")));
127 offset_ = offset.inPixels(max_width, fm.width(char_type('M')));
129 dim.asc = max(fm.maxAscent(), offset_ + height_);
130 dim.des = max(fm.maxDescent(), - offset_);
132 // Cache the inset dimension
133 setDimCache(mi, dim);
137 Dimension const InsetLine::dimension(BufferView const & bv) const
139 // We cannot use InsetCommand::dimension() as this returns the dimension
140 // of the button, which is not used here.
141 return Inset::dimension(bv);
145 void InsetLine::draw(PainterInfo & pi, int x, int y) const
147 Dimension const dim = dimension(*pi.base.bv);
149 // get the surrounding text color
150 Color Line_color = pi.base.font.realColor();
152 // the offset is a vertical one
153 // the horizontal dimension must be corrected with the heigth because
154 // of left and right border of the painted line for big heigth.
155 pi.pain.line(x + height_/2 + 1,
156 y - offset_ - height_/2,
157 x + dim.wid - height_/2 - 2,
158 y - offset_ - height_/2,
159 Line_color, Painter::line_solid, float(height_));
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");