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)
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(to_utf8(cmd.argument()), p);
75 if (p.getCmdName().empty()) {
84 InsetCommand::doDispatch(cur, cmd);
90 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
91 FuncStatus & status) const
93 switch (cmd.action()) {
94 case LFUN_INSET_DIALOG_UPDATE:
95 case LFUN_INSET_MODIFY:
96 status.setEnabled(true);
99 return InsetCommand::getStatus(cur, cmd, status);
104 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
106 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
107 int const max_width = mi.base.textwidth;
109 Length const width(to_ascii(getParam("width")));
110 dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
112 // assure that the line inset is not outside of the window
113 // check that it doesn't exceed the outer boundary
114 if (dim.wid > max_width)
117 // set a minimal width
118 int const minw = (dim.wid < 0) ? 24 : 4;
119 dim.wid = max(minw, max(dim.wid, -dim.wid));
121 Length height = Length(to_ascii(getParam("height")));
122 height_ = height.inPixels(dim.height(), fm.width(char_type('M')));
124 // get the length of the parameters in pixels
125 Length offset = Length(to_ascii(getParam("offset")));
126 offset_ = offset.inPixels(max_width, fm.width(char_type('M')));
128 dim.asc = max(fm.maxAscent(), offset_ + height_);
129 dim.des = max(fm.maxDescent(), - offset_);
131 // Cache the inset dimension
132 setDimCache(mi, dim);
136 Dimension const InsetLine::dimension(BufferView const & bv) const
138 // We cannot use InsetCommand::dimension() as this returns the dimension
139 // of the button, which is not used here.
140 return Inset::dimension(bv);
144 void InsetLine::draw(PainterInfo & pi, int x, int y) const
146 Dimension const dim = dimension(*pi.base.bv);
148 // get the surrounding text color
149 Color Line_color = pi.base.font.realColor();
151 // the offset is a vertical one
152 // the horizontal dimension must be corrected with the heigth because
153 // of left and right border of the painted line for big heigth.
154 pi.pain.line(x + height_/2 + 1,
155 y - offset_ - height_/2,
156 x + dim.wid - height_/2 - 2,
157 y - offset_ - height_/2,
158 Line_color, Painter::line_solid, float(height_));
162 int InsetLine::latex(odocstream & os, OutputParams const &) const
164 bool have_offset = true;
165 Length offset_len = Length(to_ascii(getParam("offset")));
166 if (offset_len.value() == 0)
169 string const offset =
170 Length(to_ascii(getParam("offset"))).asLatexString();
172 Length(to_ascii(getParam("width"))).asLatexString();
173 string const height =
174 Length(to_ascii(getParam("height"))).asLatexString();
177 // only output the optional parameter if the offset is not 0
179 os << "[" << from_ascii(offset) << "]";
180 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");