]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLine.cpp
InsetLine: support for the LaTeX command \rule; kick out the hardcoded \lyxline;...
[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/convert.h"
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, "line")
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("line",
75                         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         dim.asc = fm.maxAscent();
109         dim.des = fm.maxDescent();
110         
111         Length width = Length(to_ascii(getParam("width")));
112         int w = 
113                 width.inPixels(mi.base.textwidth,
114                 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 (w > mi.base.textwidth)
119                 w = mi.base.textwidth;
120
121         // set a minimal width
122         int const minw = (w < 0) ? 3 * 8 : 4;
123         dim.wid = max(minw, abs(w));
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         Dimension const dim = dimension(*pi.base.bv);
133
134         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
135
136         // get the length of the parameters in pixels
137         Length offset = Length(to_ascii(getParam("offset")));
138         int o = 
139                 offset.inPixels(pi.base.textwidth,
140                 fm.width(char_type('M')));
141         Length width = Length(to_ascii(getParam("width")));
142         int w = 
143                 width.inPixels(pi.base.textwidth,
144                 fm.width(char_type('M')));
145         Length height = Length(to_ascii(getParam("height")));
146         int h = 
147                 height.inPixels(pi.base.textwidth,
148                 fm.width(char_type('M')));
149
150         // get the surrounding text color
151         FontInfo f = pi.base.font;
152         Color Line_color = f.realColor();
153
154         // assure that the drawn line is not outside of the window
155         // check that it doesn't exceed the outer boundary
156         if (x + w - h/2 - 2 > pi.base.textwidth)
157                 w = pi.base.textwidth - x + h/2 + 2;
158         // check that it doesn't exceed the upper boundary
159         if (y - o - h/2 < 0)
160                 o = y - h/2 - 2;
161
162         // the offset is a vertical one
163         pi.pain.line(x + h/2 + 1, y - o - h/2, x + w - h/2 - 2, y - o - h/2,
164                 Line_color, Painter::line_solid, float(h));
165 }
166
167
168 int InsetLine::latex(odocstream & os, OutputParams const & runparams) const
169 {
170         bool have_offset = true;
171         Length offset_len = Length(to_ascii(getParam("offset")));
172         if (offset_len.value() == 0)
173                 have_offset = false;
174
175         string const offset =
176                 Length(to_ascii(getParam("offset"))).asLatexString();
177         string const width =
178                 Length(to_ascii(getParam("width"))).asLatexString();
179         string const height =
180                 Length(to_ascii(getParam("height"))).asLatexString();
181
182         os << "\\rule";
183         // only output the optional parameter if the offset is not 0
184         if (have_offset)
185                 os      << "[" << from_ascii(offset) << "]";
186         os << "{" << from_ascii(width) << "}{" << from_ascii(height) << '}';
187
188         return 0;
189 }
190
191
192 int InsetLine::plaintext(odocstream & os, OutputParams const &) const
193 {
194         os << "\n-------------------------------------------\n";
195         return PLAINTEXT_NEWLINE;
196 }
197
198
199 int InsetLine::docbook(odocstream & os, OutputParams const &) const
200 {
201         os << '\n';
202         return 0;
203 }
204
205
206 docstring InsetLine::xhtml(XHTMLStream & xs, OutputParams const &) const
207 {
208         xs << html::CompTag("hr");
209         xs.cr();
210         return docstring();
211 }
212
213
214 } // namespace lyx