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"
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/lstrings.h"
45 using frontend::Painter;
48 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
49 : InsetCommand(buf, p), height_(0), offset_(0)
53 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
55 static ParamInfo param_info_;
56 if (param_info_.empty()) {
57 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
58 param_info_.add("width", ParamInfo::LYX_INTERNAL);
59 param_info_.add("height", ParamInfo::LYX_INTERNAL);
65 docstring InsetLine::screenLabel() const
67 return _("Horizontal line");
71 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
73 switch (cmd.action()) {
75 case LFUN_INSET_MODIFY: {
76 InsetCommandParams p(LINE_CODE);
78 InsetCommand::string2params(to_utf8(cmd.argument()), p);
79 if (p.getCmdName().empty()) {
89 InsetCommand::doDispatch(cur, cmd);
95 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
96 FuncStatus & status) const
98 switch (cmd.action()) {
99 case LFUN_INSET_DIALOG_UPDATE:
100 case LFUN_INSET_MODIFY:
101 status.setEnabled(true);
104 return InsetCommand::getStatus(cur, cmd, status);
109 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
111 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
112 int const max_width = mi.base.textwidth;
114 Length const width(to_ascii(getParam("width")));
115 dim.wid = mi.base.inPixels(width);
117 // assure that the line inset is not outside of the window
118 // check that it doesn't exceed the outer boundary
119 if (dim.wid > max_width)
122 // set a minimal width
123 int const minw = (dim.wid < 0) ? 24 : 4;
124 dim.wid = max(minw, abs(dim.wid));
126 Length height = Length(to_ascii(getParam("height")));
127 height_ = mi.base.inPixels(height);
129 // get the length of the parameters in pixels
130 Length offset = Length(to_ascii(getParam("offset")));
131 offset_ = mi.base.inPixels(offset);
133 dim.asc = max(fm.maxAscent(), offset_ + height_);
134 dim.des = max(fm.maxDescent(), - offset_);
138 void InsetLine::draw(PainterInfo & pi, int x, int y) const
140 Dimension const dim = dimension(*pi.base.bv);
142 // get the surrounding text color
143 Color Line_color = pi.base.font.realColor();
145 // the offset is a vertical one
146 // the horizontal dimension must be corrected with the heigth because
147 // of left and right border of the painted line for big heigth.
148 pi.pain.line(x + height_/2 + 1,
149 y - offset_ - height_/2,
150 x + dim.wid - height_/2 - 2,
151 y - offset_ - height_/2,
152 Line_color, Painter::line_solid, height_);
156 void InsetLine::latex(otexstream & os, OutputParams const &) const
158 bool have_offset = true;
159 Length offset_len = Length(to_ascii(getParam("offset")));
160 if (offset_len.value() == 0)
163 string const offset =
164 Length(to_ascii(getParam("offset"))).asLatexString();
166 Length(to_ascii(getParam("width"))).asLatexString();
167 string const height =
168 Length(to_ascii(getParam("height"))).asLatexString();
171 // only output the optional parameter if the offset is not 0
173 os << "[" << from_ascii(offset) << "]";
174 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
178 int InsetLine::plaintext(odocstringstream & os,
179 OutputParams const &, size_t) const
181 os << "\n-------------------------------------------\n";
182 return PLAINTEXT_NEWLINE;
186 int InsetLine::docbook(odocstream & os, OutputParams const &) const
193 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
195 xs << html::CompTag("hr") << html::CR();