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