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