]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
Do not reset custom space values if accessed via context menu (#8847)
[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                 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         // Cache the inset dimension. 
153         setDimCache(mi, dim);
154 }
155
156
157 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
158 {
159         Dimension const dim = dimension(*pi.base.bv);
160         x += ADD_TO_VSPACE_WIDTH;
161         int const start = y - dim.asc;
162         int const end   = y + dim.des;
163
164         // y-values for top arrow
165         int ty1, ty2;
166         // y-values for bottom arrow
167         int by1, by2;
168
169         if (space_.kind() == VSpace::VFILL) {
170                 ty1 = ty2 = start;
171                 by1 = by2 = end;
172         } else {
173                 // adding or removing space
174                 bool const added = space_.kind() != VSpace::LENGTH ||
175                                    space_.length().len().value() >= 0.0;
176                 ty1 = added ? (start + vspace_arrow_size) : start;
177                 ty2 = added ? start : (start + vspace_arrow_size);
178                 by1 = added ? (end - vspace_arrow_size) : end;
179                 by2 = added ? end : (end - vspace_arrow_size);
180         }
181
182         int const midx = x + vspace_arrow_size;
183         int const rightx = midx + vspace_arrow_size;
184
185         // first the string
186         int w = 0;
187         int a = 0;
188         int d = 0;
189
190         FontInfo font;
191         font.setColor(Color_added_space);
192         font.decSize();
193         font.decSize();
194         docstring const lab = label();
195         theFontMetrics(font).rectText(lab, w, a, d);
196
197         pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
198                          start + (end - start) / 2 + (a - d) / 2,
199                          lab, font, Color_none, Color_none);
200
201         // top arrow
202         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
203         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
204
205         // bottom arrow
206         pi.pain.line(x, by1, midx, by2, Color_added_space);
207         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
208
209         // joining line
210         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
211 }
212
213
214 void InsetVSpace::latex(otexstream & os, OutputParams const &) const
215 {
216         os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
217 }
218
219
220 int InsetVSpace::plaintext(odocstringstream & os,
221         OutputParams const &, size_t) const
222 {
223         os << "\n\n";
224         return PLAINTEXT_NEWLINE;
225 }
226
227
228 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
229 {
230         os << '\n';
231         return 1;
232 }
233
234
235 docstring InsetVSpace::xhtml(XHTMLStream &, OutputParams const &) const
236 {
237         odocstringstream ods;
238         XHTMLStream xds(ods);
239         string const len = space_.asHTMLLength();
240         string const attr = "style='height:" + (len.empty() ? "1em" : len) + "'";
241         xds << html::StartTag("div", attr, true) << html::EndTag("div");
242         return ods.str();
243 }
244
245
246 string InsetVSpace::contextMenuName() const
247 {
248         return "context-vspace";
249 }
250
251
252 void InsetVSpace::string2params(string const & in, VSpace & vspace)
253 {
254         vspace = VSpace();
255         if (in.empty())
256                 return;
257
258         istringstream data(in);
259         Lexer lex;
260         lex.setStream(data);
261         lex.setContext("InsetVSpace::string2params");
262         lex >> "vspace" >> vspace;
263 }
264
265
266 string InsetVSpace::params2string(VSpace const & vspace)
267 {
268         ostringstream data;
269         data << "vspace" << ' ' << vspace.asLyXCommand();
270         return data.str();
271 }
272
273
274 } // namespace lyx