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