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