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