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