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"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "output_docbook.h"
27 #include "output_xhtml.h"
28 #include "texstream.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/Length.h"
38 #include "support/lstrings.h"
46 using frontend::Painter;
49 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
50 : InsetCommand(buf, p), height_(0), offset_(0)
54 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
56 static ParamInfo param_info_;
57 if (param_info_.empty()) {
58 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
59 param_info_.add("width", ParamInfo::LYX_INTERNAL);
60 param_info_.add("height", ParamInfo::LYX_INTERNAL);
66 docstring InsetLine::screenLabel() const
68 return _("Horizontal line");
72 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
74 switch (cmd.action()) {
76 case LFUN_INSET_MODIFY: {
77 InsetCommandParams p(LINE_CODE);
79 InsetCommand::string2params(to_utf8(cmd.argument()), p);
80 if (p.getCmdName().empty()) {
90 InsetCommand::doDispatch(cur, cmd);
96 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
97 FuncStatus & status) const
99 switch (cmd.action()) {
100 case LFUN_INSET_DIALOG_UPDATE:
101 case LFUN_INSET_MODIFY:
102 status.setEnabled(true);
105 return InsetCommand::getStatus(cur, cmd, status);
110 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
112 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
113 int const max_width = mi.base.textwidth;
115 Length const width(to_ascii(getParam("width")));
116 dim.wid = mi.base.inPixels(width);
118 // assure that the line inset is not outside of the window
119 // check that it doesn't exceed the outer boundary
120 if (dim.wid > max_width)
123 // set a minimal width
124 int const minw = (dim.wid < 0) ? 24 : 4;
125 dim.wid = max(minw, abs(dim.wid));
127 Length height = Length(to_ascii(getParam("height")));
128 height_ = mi.base.inPixels(height);
130 // get the length of the parameters in pixels
131 Length offset = Length(to_ascii(getParam("offset")));
132 offset_ = mi.base.inPixels(offset);
134 dim.asc = max(fm.maxAscent(), offset_ + height_);
135 dim.des = max(fm.maxDescent(), - offset_);
139 void InsetLine::draw(PainterInfo & pi, int x, int y) const
141 Dimension const dim = dimension(*pi.base.bv);
143 // get the surrounding text color
144 Color Line_color = pi.base.font.realColor();
146 // the offset is a vertical one
147 // the horizontal dimension must be corrected with the height because
148 // of left and right border of the painted line for big height.
149 pi.pain.line(x + height_/2 + 1,
150 y - offset_ - height_/2,
151 x + dim.wid - height_/2 - 2,
152 y - offset_ - height_/2,
153 Line_color, Painter::line_solid, height_);
157 void InsetLine::latex(otexstream & os, OutputParams const &) const
159 bool have_offset = true;
160 Length offset_len = Length(to_ascii(getParam("offset")));
161 if (offset_len.value() == 0)
164 string const offset =
165 Length(to_ascii(getParam("offset"))).asLatexString();
167 Length(to_ascii(getParam("width"))).asLatexString();
168 string const height =
169 Length(to_ascii(getParam("height"))).asLatexString();
172 // only output the optional parameter if the offset is not 0
174 os << "[" << from_ascii(offset) << "]";
175 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
179 int InsetLine::plaintext(odocstringstream & os,
180 OutputParams const &, size_t) const
182 os << "\n-------------------------------------------\n";
183 return PLAINTEXT_NEWLINE;
187 void InsetLine::docbook(XMLStream & xs, OutputParams const &) const
193 docstring InsetLine::xhtml(XMLStream & xs, OutputParams const &) const
195 xs << xml::CompTag("hr") << xml::CR();