]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
Compilation fix for 'abs'
[lyx.git] / src / insets / InsetLine.cpp
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 André Pönitz
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetLine.h"
15
16 #include "Buffer.h"
17 #include "Dimension.h"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "LaTeXFeatures.h"
22 #include "Length.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
26 #include "Text.h"
27
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include <cstdlib>
37
38 using namespace std;
39
40 namespace lyx {
41
42 using frontend::Painter;
43
44
45 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
46         : InsetCommand(buf, p, "line")
47 {}
48
49
50 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
51 {
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);
57         }
58         return param_info_;
59 }
60
61
62 docstring InsetLine::screenLabel() const
63 {
64         return _("Horizontal line");
65 }
66
67
68 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
69 {
70         switch (cmd.action()) {
71
72         case LFUN_INSET_MODIFY: {
73                 InsetCommandParams p(LINE_CODE);
74                 // FIXME UNICODE
75                 InsetCommand::string2params("line",
76                         to_utf8(cmd.argument()), p);
77                 if (p.getCmdName().empty()) {
78                         cur.noScreenUpdate();
79                         break;
80                 }
81                 setParams(p);
82                 break;
83         }
84
85         default:
86                 InsetCommand::doDispatch(cur, cmd);
87                 break;
88         }
89 }
90
91
92 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
93         FuncStatus & status) const
94 {
95         switch (cmd.action()) {
96         case LFUN_INSET_DIALOG_UPDATE:
97         case LFUN_INSET_MODIFY:
98                 status.setEnabled(true);
99                 return true;
100         default:
101                 return InsetCommand::getStatus(cur, cmd, status);
102         }
103 }
104
105
106 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
107 {
108         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
109         dim.asc = fm.maxAscent();
110         dim.des = fm.maxDescent();
111
112         Length width = Length(to_ascii(getParam("width")));
113         int w =
114                 width.inPixels(mi.base.textwidth,
115                 fm.width(char_type('M')));
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 (w > mi.base.textwidth)
120                 w = mi.base.textwidth;
121
122         // set a minimal width
123         int const minw = (w < 0) ? 3 * 8 : 4;
124         dim.wid = max(minw, max(w, -w));
125
126         // Cache the inset dimension
127         setDimCache(mi, dim);
128 }
129
130
131 void InsetLine::draw(PainterInfo & pi, int x, int y) const
132 {
133         Dimension const dim = dimension(*pi.base.bv);
134
135         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
136
137         // get the length of the parameters in pixels
138         Length offset = Length(to_ascii(getParam("offset")));
139         int o =
140                 offset.inPixels(pi.base.textwidth,
141                 fm.width(char_type('M')));
142         Length width = Length(to_ascii(getParam("width")));
143         int w =
144                 width.inPixels(pi.base.textwidth,
145                 fm.width(char_type('M')));
146         Length height = Length(to_ascii(getParam("height")));
147         int h =
148                 height.inPixels(pi.base.textwidth,
149                 fm.width(char_type('M')));
150
151         // get the surrounding text color
152         FontInfo f = pi.base.font;
153         Color Line_color = f.realColor();
154
155         // assure that the drawn line is not outside of the window
156         // check that it doesn't exceed the outer boundary
157         if (x + w - h/2 - 2 > pi.base.textwidth)
158                 w = pi.base.textwidth - x + h/2 + 2;
159         // check that it doesn't exceed the upper boundary
160         if (y - o - h/2 < 0)
161                 o = y - h/2 - 2;
162
163         // the offset is a vertical one
164         pi.pain.line(x + h/2 + 1, y - o - h/2, x + w - h/2 - 2, y - o - h/2,
165                 Line_color, Painter::line_solid, float(h));
166 }
167
168
169 int InsetLine::latex(odocstream & os, OutputParams const &) const
170 {
171         bool have_offset = true;
172         Length offset_len = Length(to_ascii(getParam("offset")));
173         if (offset_len.value() == 0)
174                 have_offset = false;
175
176         string const offset =
177                 Length(to_ascii(getParam("offset"))).asLatexString();
178         string const width =
179                 Length(to_ascii(getParam("width"))).asLatexString();
180         string const height =
181                 Length(to_ascii(getParam("height"))).asLatexString();
182
183         os << "\\rule";
184         // only output the optional parameter if the offset is not 0
185         if (have_offset)
186                 os      << "[" << from_ascii(offset) << "]";
187         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
188
189         return 0;
190 }
191
192
193 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
194 {
195         os << "\n-------------------------------------------\n";
196         return PLAINTEXT_NEWLINE;
197 }
198
199
200 int InsetLine::docbook(odocstream & os, OutputParams const &) const
201 {
202         os << '\n';
203         return 0;
204 }
205
206
207 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
208 {
209         xs << html::CompTag("hr");
210         xs.cr();
211         return docstring();
212 }
213
214
215 } // namespace lyx