]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
2c88c061fd0c9853f9495fd0837b6179cce10f60
[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 "Dimension.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
23 #include "Length.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "output_xhtml.h"
27 #include "Text.h"
28
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 using namespace std;
38
39 namespace lyx {
40
41 using frontend::Painter;
42
43
44 InsetLine::InsetLine(Buffer * buf, InsetCommandParams const & p)
45         : InsetCommand(buf, p)
46 {}
47
48
49 ParamInfo const & InsetLine::findInfo(string const & /* cmdName */)
50 {
51         static ParamInfo param_info_;
52         if (param_info_.empty()) {
53                 param_info_.add("offset", ParamInfo::LYX_INTERNAL);
54                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
55                 param_info_.add("height", ParamInfo::LYX_INTERNAL);
56         }
57         return param_info_;
58 }
59
60
61 docstring InsetLine::screenLabel() const
62 {
63         return _("Horizontal line");
64 }
65
66
67 void InsetLine::doDispatch(Cursor & cur, FuncRequest & cmd)
68 {
69         switch (cmd.action()) {
70
71         case LFUN_INSET_MODIFY: {
72                 InsetCommandParams p(LINE_CODE);
73                 // FIXME UNICODE
74                 InsetCommand::string2params(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_);
129         dim.des = max(fm.maxDescent(), - 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         // get the surrounding text color
149         Color Line_color = pi.base.font.realColor();
150
151         // the offset is a vertical one
152         // the horizontal dimension must be corrected with the heigth because
153         // of left and right border of the painted line for big heigth.
154         pi.pain.line(x + height_/2 + 1,
155                      y - offset_ - height_/2,
156                      x + dim.wid - height_/2 - 2,
157                      y - offset_ - height_/2,
158                      Line_color, Painter::line_solid, float(height_));
159 }
160
161
162 int InsetLine::latex(odocstream & os, OutputParams const &) const
163 {
164         bool have_offset = true;
165         Length offset_len = Length(to_ascii(getParam("offset")));
166         if (offset_len.value() == 0)
167                 have_offset = false;
168
169         string const offset =
170                 Length(to_ascii(getParam("offset"))).asLatexString();
171         string const width =
172                 Length(to_ascii(getParam("width"))).asLatexString();
173         string const height =
174                 Length(to_ascii(getParam("height"))).asLatexString();
175
176         os << "\\rule";
177         // only output the optional parameter if the offset is not 0
178         if (have_offset)
179                 os      << "[" << from_ascii(offset) << "]";
180         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
181
182         return 0;
183 }
184
185
186 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
187 {
188         os << "\n-------------------------------------------\n";
189         return PLAINTEXT_NEWLINE;
190 }
191
192
193 int InsetLine::docbook(odocstream & os, OutputParams const &) const
194 {
195         os << '\n';
196         return 0;
197 }
198
199
200 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
201 {
202         xs << html::CompTag("hr");
203         xs.cr();
204         return docstring();
205 }
206
207
208 } // namespace lyx