]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
revert r35374 and further improve the metrics and drawing code.
[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 "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"
24 #include "Length.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "Text.h"
29
30 #include "frontends/FontMetrics.h"
31 #include "frontends/Painter.h"
32
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
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         int const max_width = mi.base.textwidth;
112
113         Length const width(to_ascii(getParam("width")));
114         dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
115
116         // assure that the line inset is not outside of the window
117         // check that it doesn't exceed the outer boundary
118         if (dim.wid > max_width)
119                 dim.wid = max_width;
120
121         // set a minimal width
122         int const minw = (dim.wid < 0) ? 24 : 4;
123         dim.wid = max(minw, max(dim.wid, -dim.wid));
124
125         // Cache the inset dimension
126         setDimCache(mi, dim);
127 }
128
129
130 void InsetLine::draw(PainterInfo & pi, int x, int y) const
131 {
132         // FIXME: 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();
136
137         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
138
139         // get the surrounding text color
140         FontInfo f = pi.base.font;
141         Color Line_color = f.realColor();
142
143         Length height = Length(to_ascii(getParam("height")));
144         int const h = height.inPixels(dim.height(), fm.width(char_type('M')));
145
146         // get the length of the parameters in pixels
147         Length offset = Length(to_ascii(getParam("offset")));
148         int o = offset.inPixels(max_width, fm.width(char_type('M')));
149
150         // check that it doesn't exceed the upper boundary
151         if (y - o - h/2 < 0)
152                 o = y - h/2 - 2;
153
154         // the offset is a vertical one
155         pi.pain.line(x + 1, y - o - h/2, x + dim.wid - 2, y - o - h/2,
156                 Line_color, Painter::line_solid, float(h));
157 }
158
159
160 int InsetLine::latex(odocstream & os, OutputParams const &) const
161 {
162         bool have_offset = true;
163         Length offset_len = Length(to_ascii(getParam("offset")));
164         if (offset_len.value() == 0)
165                 have_offset = false;
166
167         string const offset =
168                 Length(to_ascii(getParam("offset"))).asLatexString();
169         string const width =
170                 Length(to_ascii(getParam("width"))).asLatexString();
171         string const height =
172                 Length(to_ascii(getParam("height"))).asLatexString();
173
174         os << "\\rule";
175         // only output the optional parameter if the offset is not 0
176         if (have_offset)
177                 os      << "[" << from_ascii(offset) << "]";
178         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
179
180         return 0;
181 }
182
183
184 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
185 {
186         os << "\n-------------------------------------------\n";
187         return PLAINTEXT_NEWLINE;
188 }
189
190
191 int InsetLine::docbook(odocstream & os, OutputParams const &) const
192 {
193         os << '\n';
194         return 0;
195 }
196
197
198 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
199 {
200         xs << html::CompTag("hr");
201         xs.cr();
202         return docstring();
203 }
204
205
206 } // namespace lyx