3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsetLine.h"
17 #include "BufferView.h"
18 #include "CoordCache.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"
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/lstrings.h"
42 using frontend::Painter;
45 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
46 : InsetCommand(buf, p, "line")
50 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
52 static ParamInfo param_info_;
53 if (param_info_.empty()) {
54 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
55 param_info_.add("width", ParamInfo::LYX_INTERNAL);
56 param_info_.add("height", ParamInfo::LYX_INTERNAL);
62 docstring InsetLine::screenLabel() const
64 return _("Horizontal line");
68 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
70 switch (cmd.action()) {
72 case LFUN_INSET_MODIFY: {
73 InsetCommandParams p(LINE_CODE);
75 InsetCommand::string2params("line",
76 to_utf8(cmd.argument()), p);
77 if (p.getCmdName().empty()) {
86 InsetCommand::doDispatch(cur, cmd);
92 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
93 FuncStatus & status) const
95 switch (cmd.action()) {
96 case LFUN_INSET_DIALOG_UPDATE:
97 case LFUN_INSET_MODIFY:
98 status.setEnabled(true);
101 return InsetCommand::getStatus(cur, cmd, status);
106 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
108 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
109 dim.asc = fm.maxAscent();
110 dim.des = fm.maxDescent();
112 Length const width(to_ascii(getParam("width")));
113 int w = width.inPixels(mi.base.textwidth,
114 fm.width(char_type('M')));
116 // assure that the line inset is not outside of the window
117 // check that it doesn't exceed the outer boundary
118 if (w > mi.base.textwidth)
119 w = mi.base.textwidth;
121 // set a minimal width
122 int const minw = (w < 0) ? 24 : 4;
123 dim.wid = max(minw, max(w, -w));
125 // Cache the inset dimension
126 setDimCache(mi, dim);
130 void InsetLine::draw(PainterInfo & pi, int x, int y) const
132 // We cannot use InsetCommand::dimension() as this returns the dimension
133 // of the button, which is not used here!
134 Dimension const dim = pi.base.bv->coordCache().getInsets().dim(this);
135 int const max_width = dim.width();
137 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
139 // get the length of the parameters in pixels
140 Length offset = Length(to_ascii(getParam("offset")));
141 int o = offset.inPixels(max_width,
142 fm.width(char_type('M')));
143 Length const width(to_ascii(getParam("width")));
144 int w = width.inPixels(max_width,
145 fm.width(char_type('M')));
146 Length height = Length(to_ascii(getParam("height")));
147 int h = height.inPixels(max_width,
148 fm.width(char_type('M')));
150 // get the surrounding text color
151 FontInfo f = pi.base.font;
152 Color Line_color = f.realColor();
154 // assure that the drawn line is not outside of the window
155 // check that it doesn't exceed the outer boundary
158 // check that it doesn't exceed the upper boundary
162 // the offset is a vertical one
163 pi.pain.line(x, y - o - h/2, x + w, y - o - h/2,
164 Line_color, Painter::line_solid, float(h));
168 int InsetLine::latex(odocstream & os, OutputParams const &) const
170 bool have_offset = true;
171 Length offset_len = Length(to_ascii(getParam("offset")));
172 if (offset_len.value() == 0)
175 string const offset =
176 Length(to_ascii(getParam("offset"))).asLatexString();
178 Length(to_ascii(getParam("width"))).asLatexString();
179 string const height =
180 Length(to_ascii(getParam("height"))).asLatexString();
183 // only output the optional parameter if the offset is not 0
185 os << "[" << from_ascii(offset) << "]";
186 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
192 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
194 os << "\n-------------------------------------------\n";
195 return PLAINTEXT_NEWLINE;
199 int InsetLine::docbook(odocstream & os, OutputParams const &) const
206 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
208 xs << html::CompTag("hr");