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