]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
Fix InsetLine painting for exotic offset and heigth.
[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 using namespace std;
37
38 namespace lyx {
39
40 using frontend::Painter;
41
42
43 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
44         : InsetCommand(buf, p, "line")
45 {}
46
47
48 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
49 {
50         static ParamInfo param_info_;
51         if (param_info_.empty()) {
52                 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
53                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
54                 param_info_.add("height", ParamInfo::LYX_INTERNAL);
55         }
56         return param_info_;
57 }
58
59
60 docstring InsetLine::screenLabel() const
61 {
62         return _("Horizontal line");
63 }
64
65
66 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
67 {
68         switch (cmd.action()) {
69
70         case LFUN_INSET_MODIFY: {
71                 InsetCommandParams p(LINE_CODE);
72                 // FIXME UNICODE
73                 InsetCommand::string2params("line",
74                         to_utf8(cmd.argument()), p);
75                 if (p.getCmdName().empty()) {
76                         cur.noScreenUpdate();
77                         break;
78                 }
79                 setParams(p);
80                 break;
81         }
82
83         default:
84                 InsetCommand::doDispatch(cur, cmd);
85                 break;
86         }
87 }
88
89
90 bool InsetLine::getStatus(Cursor & cur, FuncRequest const & cmd,
91         FuncStatus & status) const
92 {
93         switch (cmd.action()) {
94         case LFUN_INSET_DIALOG_UPDATE:
95         case LFUN_INSET_MODIFY:
96                 status.setEnabled(true);
97                 return true;
98         default:
99                 return InsetCommand::getStatus(cur, cmd, status);
100         }
101 }
102
103
104 void InsetLine::metrics(MetricsInfo & mi, Dimension & dim) const
105 {
106         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
107         int const max_width = mi.base.textwidth;
108
109         Length const width(to_ascii(getParam("width")));
110         dim.wid = width.inPixels(max_width, fm.width(char_type('M')));
111
112         // assure that the line inset is not outside of the window
113         // check that it doesn't exceed the outer boundary
114         if (dim.wid > max_width)
115                 dim.wid = max_width;
116
117         // set a minimal width
118         int const minw = (dim.wid < 0) ? 24 : 4;
119         dim.wid = max(minw, max(dim.wid, -dim.wid));
120
121         Length height = Length(to_ascii(getParam("height")));
122         height_ = height.inPixels(dim.height(), fm.width(char_type('M')));
123
124         // get the length of the parameters in pixels
125         Length offset = Length(to_ascii(getParam("offset")));
126         offset_ = offset.inPixels(max_width, fm.width(char_type('M')));
127
128         dim.asc = max(fm.maxAscent(), offset_ + height_/2);
129         dim.des = max(fm.maxDescent(), height_/2 - offset_);
130
131         // Cache the inset dimension
132         setDimCache(mi, dim);
133 }
134
135
136 Dimension const InsetLine::dimension(BufferView const & bv) const
137 {
138         // We cannot use InsetCommand::dimension() as this returns the dimension
139         // of the button, which is not used here.
140         return Inset::dimension(bv);
141 }
142
143
144 void InsetLine::draw(PainterInfo & pi, int x, int y) const
145 {
146         Dimension const dim = dimension(*pi.base.bv);
147
148         // check that it doesn't exceed the upper boundary
149         // FIXME: not sure this should be done...
150         if (y - offset_ - height_/2 < 0)
151                 offset_ = y - height_/2 - 2;
152
153         // get the surrounding text color
154         Color Line_color = pi.base.font.realColor();
155
156         // the offset is a vertical one
157         // the horizontal dimension must be corrected with the heigth because
158         // of left and right border of the painted line for big heigth.
159         pi.pain.line(x + height_/2 + 1,
160                      y - offset_ - height_/2,
161                      x + dim.wid - height_/2 - 2,
162                      y - offset_ - height_/2,
163                      Line_color, Painter::line_solid, float(height_));
164 }
165
166
167 int InsetLine::latex(odocstream & os, OutputParams const &) const
168 {
169         bool have_offset = true;
170         Length offset_len = Length(to_ascii(getParam("offset")));
171         if (offset_len.value() == 0)
172                 have_offset = false;
173
174         string const offset =
175                 Length(to_ascii(getParam("offset"))).asLatexString();
176         string const width =
177                 Length(to_ascii(getParam("width"))).asLatexString();
178         string const height =
179                 Length(to_ascii(getParam("height"))).asLatexString();
180
181         os << "\\rule";
182         // only output the optional parameter if the offset is not 0
183         if (have_offset)
184                 os      << "[" << from_ascii(offset) << "]";
185         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
186
187         return 0;
188 }
189
190
191 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
192 {
193         os << "\n-------------------------------------------\n";
194         return PLAINTEXT_NEWLINE;
195 }
196
197
198 int InsetLine::docbook(odocstream & os, OutputParams const &) const
199 {
200         os << '\n';
201         return 0;
202 }
203
204
205 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
206 {
207         xs << html::CompTag("hr");
208         xs.cr();
209         return docstring();
210 }
211
212
213 } // namespace lyx