]> git.lyx.org Git - features.git/blob - src/insets/InsetVSpace.cpp
Regenerate previews after zoom (#11919)
[features.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 "xml.h"
26 #include "texstream.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
49
50
51 InsetVSpace::InsetVSpace(VSpace const & space)
52         : Inset(nullptr), 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                 cur.recordUndo();
62                 string arg = to_utf8(cmd.argument());
63                 if (arg == "vspace custom")
64                         arg = (space_.kind() == VSpace::LENGTH)
65                                 ? "vspace " + space_.length().asString()
66                                 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
67                 InsetVSpace::string2params(arg, space_);
68                 break;
69         }
70
71         default:
72                 Inset::doDispatch(cur, cmd);
73                 break;
74         }
75 }
76
77
78 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
79         FuncStatus & status) const
80 {
81         switch (cmd.action()) {
82         // we handle these
83         case LFUN_INSET_MODIFY:
84                 if (cmd.getArg(0) == "vspace") {
85                         VSpace vspace;
86                         string arg = to_utf8(cmd.argument());
87                         if (arg == "vspace custom")
88                                 arg = (space_.kind() == VSpace::LENGTH)
89                                 ? "vspace " + space_.length().asString()
90                                 : "vspace 1" + string(stringFromUnit(Length::defaultUnit()));
91                         InsetVSpace::string2params(arg, vspace);
92                         status.setOnOff(vspace == space_);
93                 }
94                 status.setEnabled(true);
95                 return true;
96
97         default:
98                 return Inset::getStatus(cur, cmd, status);
99         }
100 }
101
102
103 void InsetVSpace::read(Lexer & lex)
104 {
105         LASSERT(lex.isOK(), return);
106         string vsp;
107         lex >> vsp;
108         if (lex)
109                 space_ = VSpace(vsp);
110         lex >> "\\end_inset";
111 }
112
113
114 void InsetVSpace::write(ostream & os) const
115 {
116         os << "VSpace " << space_.asLyXCommand();
117 }
118
119
120 docstring const InsetVSpace::label() const
121 {
122         static docstring const label = _("Vertical Space");
123         return label + " (" + space_.asGUIName() + ')';
124 }
125
126
127 namespace {
128 int const vspace_arrow_size = 4;
129 }
130
131
132 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
133 {
134         int height = 3 * vspace_arrow_size;
135         if (space_.length().len().value() >= 0.0)
136                 height = max(height, space_.inPixels(*mi.base.bv));
137
138         FontInfo font;
139         font.decSize();
140         font.decSize();
141
142         int w = 0;
143         int a = 0;
144         int d = 0;
145         theFontMetrics(font).rectText(label(), w, a, d);
146
147         height = max(height, a + d);
148
149         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
150         dim.des = height - dim.asc;         // label text
151         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
152 }
153
154
155 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
156 {
157         Dimension const dim = dimension(*pi.base.bv);
158         x += ADD_TO_VSPACE_WIDTH;
159         int const start = y - dim.asc;
160         int const end   = y + dim.des;
161
162         // y-values for top arrow
163         int ty1, ty2;
164         // y-values for bottom arrow
165         int by1, by2;
166
167         if (space_.kind() == VSpace::VFILL) {
168                 ty1 = ty2 = start;
169                 by1 = by2 = end;
170         } else {
171                 // adding or removing space
172                 bool const added = space_.kind() != VSpace::LENGTH ||
173                                    space_.length().len().value() >= 0.0;
174                 ty1 = added ? (start + vspace_arrow_size) : start;
175                 ty2 = added ? start : (start + vspace_arrow_size);
176                 by1 = added ? (end - vspace_arrow_size) : end;
177                 by2 = added ? end : (end - vspace_arrow_size);
178         }
179
180         int const midx = x + vspace_arrow_size;
181         int const rightx = midx + vspace_arrow_size;
182
183         // first the string
184         int w = 0;
185         int a = 0;
186         int d = 0;
187
188         FontInfo font;
189         font.setColor(Color_added_space);
190         font.decSize();
191         font.decSize();
192         docstring const lab = label();
193         theFontMetrics(font).rectText(lab, w, a, d);
194
195         pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
196                          start + (end - start) / 2 + (a - d) / 2,
197                          lab, font, Color_none, Color_none);
198
199         // top arrow
200         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
201         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
202
203         // bottom arrow
204         pi.pain.line(x, by1, midx, by2, Color_added_space);
205         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
206
207         // joining line
208         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
209 }
210
211
212 void InsetVSpace::latex(otexstream & os, OutputParams const & rp) const
213 {
214         os << from_ascii(space_.asLatexCommand(buffer().params())) << breakln;
215         if (rp.need_noindent) {
216                 // If the paragraph starts with a vspace and has more than that
217                 // content, the \\noindent needs to come after that
218                 // (as \\noindent leaves vmode).
219                 os << "\\noindent" << termcmd;
220         }
221 }
222
223
224 int InsetVSpace::plaintext(odocstringstream & os,
225         OutputParams const &, size_t) const
226 {
227         os << "\n\n";
228         return PLAINTEXT_NEWLINE;
229 }
230
231
232 void InsetVSpace::docbook(XMLStream & xs, OutputParams const &) const
233 {
234         xs << xml::CR();
235 }
236
237
238 docstring InsetVSpace::xhtml(XMLStream & os, OutputParams const &) const
239 {
240         string const len = space_.asHTMLLength();
241         string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
242         os << xml::StartTag("div", attr, true) << xml::EndTag("div");
243         return docstring();
244 }
245
246
247 string InsetVSpace::contextMenuName() const
248 {
249         return "context-vspace";
250 }
251
252
253 void InsetVSpace::string2params(string const & in, VSpace & vspace)
254 {
255         vspace = VSpace();
256         if (in.empty())
257                 return;
258
259         istringstream data(in);
260         Lexer lex;
261         lex.setStream(data);
262         lex.setContext("InsetVSpace::string2params");
263         lex >> "vspace" >> vspace;
264 }
265
266
267 string InsetVSpace::params2string(VSpace const & vspace)
268 {
269         ostringstream data;
270         data << "vspace" << ' ' << vspace.asLyXCommand();
271         return data.str();
272 }
273
274
275 } // namespace lyx