1 /**
2 * \file InsetLine.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
5 *
6 * \author Abdelrazak Younes
7 * \author André Pönitz
8 * \author Uwe Stöhr
9 *
10 * Full author contact details are available in file CREDITS.
11 */
12
13 #include <config.h>
14
15 #include "InsetLine.h"
16
17 #include "Buffer.h"
18 #include "Cursor.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "Length.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "texstream.h"
29 #include "Text.h"
30
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include <cstdlib>
40
41 using namespace std;
42
43 namespace lyx {
44
45 using frontend::Painter;
46
47
InsetLine(Buffer * buf,InsetCommandParams const & p)48 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
49 : InsetCommand(buf, p), height_(0), offset_(0)
50 {}
51
52
findInfo(string const &)53 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
54 {
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);
60 }
61 return param_info_;
62 }
63
64
screenLabel() const65 docstring InsetLine::screenLabel() const
66 {
67 return _("Horizontal line");
68 }
69
70
doDispatch(Cursor & cur,FuncRequest & cmd)71 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
72 {
73 switch (cmd.action()) {
74
75 case LFUN_INSET_MODIFY: {
76 InsetCommandParams p(LINE_CODE);
77 // FIXME UNICODE
78 InsetCommand::string2params(to_utf8(cmd.argument()), p);
79 if (p.getCmdName().empty()) {
80 cur.noScreenUpdate();
81 break;
82 }
83 cur.recordUndo();
84 setParams(p);
85 break;
86 }
87
88 default:
89 InsetCommand::doDispatch(cur, cmd);
90 break;
91 }
92 }
93
94
getStatus(Cursor & cur,FuncRequest const & cmd,FuncStatus & status) const95 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
96 FuncStatus & status) const
97 {
98 switch (cmd.action()) {
99 case LFUN_INSET_DIALOG_UPDATE:
100 case LFUN_INSET_MODIFY:
101 status.setEnabled(true);
102 return true;
103 default:
104 return InsetCommand::getStatus(cur, cmd, status);
105 }
106 }
107
108
metrics(MetricsInfo & mi,Dimension & dim) const109 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
110 {
111 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
112 int const max_width = mi.base.textwidth;
113
114 Length const width(to_ascii(getParam("width")));
115 dim.wid = width.inPixels(mi.base);
116
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)
120 dim.wid = max_width;
121
122 // set a minimal width
123 int const minw = (dim.wid < 0) ? 24 : 4;
124 dim.wid = max(minw, abs(dim.wid));
125
126 Length height = Length(to_ascii(getParam("height")));
127 height_ = height.inPixels(mi.base);
128
129 // get the length of the parameters in pixels
130 Length offset = Length(to_ascii(getParam("offset")));
131 offset_ = offset.inPixels(mi.base);
132
133 dim.asc = max(fm.maxAscent(), offset_ + height_);
134 dim.des = max(fm.maxDescent(), - offset_);
135 }
136
137
draw(PainterInfo & pi,int x,int y) const138 void InsetLine::draw(PainterInfo & pi, int x, int y) const
139 {
140 Dimension const dim = dimension(*pi.base.bv);
141
142 // get the surrounding text color
143 Color Line_color = pi.base.font.realColor();
144
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_);
153 }
154
155
latex(otexstream & os,OutputParams const &) const156 void InsetLine::latex(otexstream & os, OutputParams const &) const
157 {
158 bool have_offset = true;
159 Length offset_len = Length(to_ascii(getParam("offset")));
160 if (offset_len.value() == 0)
161 have_offset = false;
162
163 string const offset =
164 Length(to_ascii(getParam("offset"))).asLatexString();
165 string const width =
166 Length(to_ascii(getParam("width"))).asLatexString();
167 string const height =
168 Length(to_ascii(getParam("height"))).asLatexString();
169
170 os << "\\rule";
171 // only output the optional parameter if the offset is not 0
172 if (have_offset)
173 os << "[" << from_ascii(offset) << "]";
174 os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
175 }
176
177
plaintext(odocstringstream & os,OutputParams const &,size_t) const178 int InsetLine::plaintext(odocstringstream & os,
179 OutputParams const &, size_t) const
180 {
181 os << "\n-------------------------------------------\n";
182 return PLAINTEXT_NEWLINE;
183 }
184
185
docbook(odocstream & os,OutputParams const &) const186 int InsetLine::docbook(odocstream & os, OutputParams const &) const
187 {
188 os << '\n';
189 return 0;
190 }
191
192
xhtml(XHTMLStream & xs,OutputParams const &) const193 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
194 {
195 xs << html::CompTag("hr") << html::CR();
196 return docstring();
197 }
198
199
200 } // namespace lyx
201