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 "output_docbook.h"
26 #include "output_xhtml.h"
27 #include "texstream.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/Length.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 pi.pain.fillRectangle(x, y - offset_ - height_, dim.wid, height_, line_color);
150 void InsetLine::latex(otexstream & os, OutputParams const &) const
152 bool have_offset = true;
153 Length offset_len = Length(to_ascii(getParam("offset")));
154 if (offset_len.value() == 0)
157 string const offset =
158 Length(to_ascii(getParam("offset"))).asLatexString();
160 Length(to_ascii(getParam("width"))).asLatexString();
161 string const height =
162 Length(to_ascii(getParam("height"))).asLatexString();
165 // only output the optional parameter if the offset is not 0
167 os << "[" << from_ascii(offset) << "]";
168 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
172 int InsetLine::plaintext(odocstringstream & os,
173 OutputParams const &, size_t) const
175 os << "\n-------------------------------------------\n";
176 return PLAINTEXT_NEWLINE;
180 void InsetLine::docbook(XMLStream & xs, OutputParams const &) const
186 docstring InsetLine::xhtml(XMLStream & xs, OutputParams const &) const
188 xs << xml::CompTag("hr") << xml::CR();