]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
* add PreBabelPreamble to Language definition (fixes #4786).
[lyx.git] / src / insets / InsetVSpace.cpp
1 /**
2  * \file InsetVSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetVSpace.h"
15
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Lexer.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "output_xhtml.h"
27 #include "Text.h"
28
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33
34 #include "frontends/Application.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
37
38 #include <sstream>
39
40 using namespace std;
41
42 namespace lyx {
43
44 namespace {
45
46 int const ADD_TO_VSPACE_WIDTH = 5;
47
48 } // namespace anon
49
50
51 InsetVSpace::InsetVSpace(VSpace const & space)
52         : Inset(0), space_(space)
53 {}
54
55
56 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
57 {
58         switch (cmd.action()) {
59
60         case LFUN_INSET_MODIFY: {
61                 InsetVSpace::string2params(to_utf8(cmd.argument()), space_);
62                 break;
63         }
64
65         default:
66                 Inset::doDispatch(cur, cmd);
67                 break;
68         }
69 }
70
71
72 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
73         FuncStatus & status) const
74 {
75         switch (cmd.action()) {
76         // we handle these
77         case LFUN_INSET_MODIFY:
78                 if (cmd.getArg(0) == "vspace") {
79                         VSpace vspace;
80                         InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
81                         status.setOnOff(vspace == space_);
82                 } 
83                 status.setEnabled(true);
84                 return true;
85         
86         default:
87                 return Inset::getStatus(cur, cmd, status);
88         }
89 }
90
91
92 void InsetVSpace::read(Lexer & lex)
93 {
94         LASSERT(lex.isOK(), /**/);
95         string vsp;
96         lex >> vsp;
97         if (lex)
98                 space_ = VSpace(vsp);
99         lex >> "\\end_inset";
100 }
101
102
103 void InsetVSpace::write(ostream & os) const
104 {
105         os << "VSpace " << space_.asLyXCommand();
106 }
107
108
109 docstring const InsetVSpace::label() const
110 {
111         static docstring const label = _("Vertical Space");
112         return label + " (" + space_.asGUIName() + ')';
113 }
114
115
116 namespace {
117 int const vspace_arrow_size = 4;
118 }
119
120
121 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
122 {
123         int height = 3 * vspace_arrow_size;
124         if (space_.length().len().value() >= 0.0)
125                 height = max(height, space_.inPixels(*mi.base.bv));
126
127         FontInfo font;
128         font.decSize();
129         font.decSize();
130
131         int w = 0;
132         int a = 0;
133         int d = 0;
134         theFontMetrics(font).rectText(label(), w, a, d);
135
136         height = max(height, a + d);
137
138         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
139         dim.des = height - dim.asc;         // label text
140         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
141         // Cache the inset dimension. 
142         setDimCache(mi, dim);
143 }
144
145
146 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
147 {
148         Dimension const dim = dimension(*pi.base.bv);
149         x += ADD_TO_VSPACE_WIDTH;
150         int const start = y - dim.asc;
151         int const end   = y + dim.des;
152
153         // y-values for top arrow
154         int ty1, ty2;
155         // y-values for bottom arrow
156         int by1, by2;
157
158         if (space_.kind() == VSpace::VFILL) {
159                 ty1 = ty2 = start;
160                 by1 = by2 = end;
161         } else {
162                 // adding or removing space
163                 bool const added = space_.kind() != VSpace::LENGTH ||
164                                    space_.length().len().value() >= 0.0;
165                 ty1 = added ? (start + vspace_arrow_size) : start;
166                 ty2 = added ? start : (start + vspace_arrow_size);
167                 by1 = added ? (end - vspace_arrow_size) : end;
168                 by2 = added ? end : (end - vspace_arrow_size);
169         }
170
171         int const midx = x + vspace_arrow_size;
172         int const rightx = midx + vspace_arrow_size;
173
174         // first the string
175         int w = 0;
176         int a = 0;
177         int d = 0;
178
179         FontInfo font;
180         font.setColor(Color_added_space);
181         font.decSize();
182         font.decSize();
183         docstring const lab = label();
184         theFontMetrics(font).rectText(lab, w, a, d);
185
186         pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
187                          start + (end - start) / 2 + (a - d) / 2,
188                          lab, font, Color_none, Color_none);
189
190         // top arrow
191         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
192         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
193
194         // bottom arrow
195         pi.pain.line(x, by1, midx, by2, Color_added_space);
196         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
197
198         // joining line
199         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
200 }
201
202
203 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
204 {
205         os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
206         return 1;
207 }
208
209
210 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
211 {
212         os << "\n\n";
213         return PLAINTEXT_NEWLINE;
214 }
215
216
217 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
218 {
219         os << '\n';
220         return 1;
221 }
222
223
224 docstring InsetVSpace::xhtml(XHTMLStream &, OutputParams const &) const
225 {
226         odocstringstream ods;
227         XHTMLStream xds(ods);
228         string const len = space_.asHTMLLength();
229         string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
230         xds << html::StartTag("div", attr, true) << html::EndTag("div");
231         return ods.str();
232 }
233
234
235 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
236 {
237         return from_ascii("context-vspace");
238 }
239
240
241 void InsetVSpace::string2params(string const & in, VSpace & vspace)
242 {
243         vspace = VSpace();
244         if (in.empty())
245                 return;
246
247         istringstream data(in);
248         Lexer lex;
249         lex.setStream(data);
250         lex.setContext("InsetVSpace::string2params");
251         lex >> "vspace" >> vspace;
252 }
253
254
255 string InsetVSpace::params2string(VSpace const & vspace)
256 {
257         ostringstream data;
258         data << "vspace" << ' ' << vspace.asLyXCommand();
259         return data.str();
260 }
261
262
263 } // namespace lyx