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 = width.inPixels(mi.base);
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_ = height.inPixels(mi.base);
129 // get the length of the parameters in pixels
130 Length offset = Length(to_ascii(getParam("offset")));
131 offset_ = offset.inPixels(mi.base);
133 dim.asc = max(fm.maxAscent(), offset_ + height_);
134 dim.des = max(fm.maxDescent(), - offset_);
136 // Cache the inset dimension
137 setDimCache(mi, dim);
141 void InsetLine::draw(PainterInfo & pi, int x, int y) const
143 Dimension const dim = dimension(*pi.base.bv);
145 // get the surrounding text color
146 Color Line_color = pi.base.font.realColor();
148 // the offset is a vertical one
149 // the horizontal dimension must be corrected with the heigth because
150 // of left and right border of the painted line for big heigth.
151 pi.pain.line(x + height_/2 + 1,
152 y - offset_ - height_/2,
153 x + dim.wid - height_/2 - 2,
154 y - offset_ - height_/2,
155 Line_color, Painter::line_solid, height_);
159 void InsetLine::latex(otexstream & os, OutputParams const &) const
161 bool have_offset = true;
162 Length offset_len = Length(to_ascii(getParam("offset")));
163 if (offset_len.value() == 0)
166 string const offset =
167 Length(to_ascii(getParam("offset"))).asLatexString();
169 Length(to_ascii(getParam("width"))).asLatexString();
170 string const height =
171 Length(to_ascii(getParam("height"))).asLatexString();
174 // only output the optional parameter if the offset is not 0
176 os << "[" << from_ascii(offset) << "]";
177 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
181 int InsetLine::plaintext(odocstringstream & os,
182 OutputParams const &, size_t) const
184 os << "\n-------------------------------------------\n";
185 return PLAINTEXT_NEWLINE;
189 int InsetLine::docbook(odocstream & os, OutputParams const &) const
196 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
198 xs << html::CompTag("hr") << html::CR();