]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
Rename insets/insetxxx to insets/InsetXxx, part 2
[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 "cursor.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "lyxtext.h"
24 #include "metricsinfo.h"
25 #include "outputparams.h"
26
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
29
30 #include <sstream>
31
32
33 namespace lyx {
34
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38 using std::string;
39 using std::max;
40
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         InsetVSpaceMailer(*this).hideDialog();
57 }
58
59
60 std::auto_ptr<InsetBase> InsetVSpace::doClone() const
61 {
62         return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
63 }
64
65
66 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
67 {
68         switch (cmd.action) {
69
70         case LFUN_INSET_MODIFY: {
71                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
72                 break;
73         }
74
75         case LFUN_MOUSE_RELEASE:
76                 InsetVSpaceMailer(*this).showDialog(&cur.bv());
77                 break;
78
79         default:
80                 InsetBase::doDispatch(cur, cmd);
81                 break;
82         }
83 }
84
85
86 void InsetVSpace::read(Buffer const &, LyXLex & lex)
87 {
88         BOOST_ASSERT(lex.isOK());
89         string vsp;
90         lex >> vsp;
91         if (lex)
92                 space_ = VSpace(vsp);
93
94         string end_token;
95         lex >> end_token;
96         if (end_token != "\\end_inset")
97                 lex.printError("Missing \\end_inset at this point. "
98                                "Read: `$$Token'");
99 }
100
101
102 void InsetVSpace::write(Buffer const &, ostream & os) const
103 {
104         os << "VSpace " << space_.asLyXCommand();
105 }
106
107
108 docstring const InsetVSpace::label() const
109 {
110         static docstring const label = _("Vertical Space");
111         return label + " (" + space_.asGUIName() + ')';
112 }
113
114
115 namespace {
116 int const arrow_size = 4;
117 }
118
119
120 bool InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
121 {
122         int height = 3 * arrow_size;
123         if (space_.length().len().value() >= 0.0)
124                 height = max(height, space_.inPixels(*mi.base.bv));
125
126         LyXFont font;
127         font.decSize();
128         font.decSize();
129
130         int w = 0;
131         int a = 0;
132         int d = 0;
133         theFontMetrics(font).rectText(label(), w, a, d);
134
135         height = max(height, a + d);
136
137         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
138         dim.des = height - dim.asc;         // label text
139         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
140         bool const changed = dim_ != dim;
141         dim_ = dim;
142         return changed;
143 }
144
145
146 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
147 {
148         setPosCache(pi, x, y);
149
150         x += ADD_TO_VSPACE_WIDTH;
151
152         int const start = y - dim_.asc;
153         int const end   = y + dim_.des;
154
155         // y-values for top arrow
156         int ty1, ty2;
157         // y-values for bottom arrow
158         int by1, by2;
159
160         if (space_.kind() == VSpace::VFILL) {
161                 ty1 = ty2 = start;
162                 by1 = by2 = end;
163         } else {
164                 // adding or removing space
165                 bool const added = space_.kind() != VSpace::LENGTH ||
166                                    space_.length().len().value() >= 0.0;
167                 ty1 = added ? (start + arrow_size) : start;
168                 ty2 = added ? start : (start + arrow_size);
169                 by1 = added ? (end - arrow_size) : end;
170                 by2 = added ? end : (end - arrow_size);
171         }
172
173         int const midx = x + arrow_size;
174         int const rightx = midx + arrow_size;
175
176         // first the string
177         int w = 0;
178         int a = 0;
179         int d = 0;
180
181         LyXFont font;
182         font.setColor(LColor::added_space);
183         font.decSize();
184         font.decSize();
185         docstring const lab = label();
186         theFontMetrics(font).rectText(lab, w, a, d);
187
188         pi.pain.rectText(x + 2 * arrow_size + 5,
189                          start + (end - start) / 2 + (a - d) / 2,
190                          lab, font, LColor::none, LColor::none);
191
192         // top arrow
193         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
194         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
195
196         // bottom arrow
197         pi.pain.line(x, by1, midx, by2, LColor::added_space);
198         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
199
200         // joining line
201         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
202 }
203
204
205 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
206                        OutputParams const &) const
207 {
208         os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
209         return 1;
210 }
211
212
213 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
214                            OutputParams const &) const
215 {
216         os << "\n\n";
217         return PLAINTEXT_NEWLINE;
218 }
219
220
221 int InsetVSpace::docbook(Buffer const &, odocstream & os,
222                          OutputParams const &) const
223 {
224         os << '\n';
225         return 1;
226 }
227
228
229 string const InsetVSpaceMailer::name_ = "vspace";
230
231
232 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
233         : inset_(inset)
234 {}
235
236
237 string const InsetVSpaceMailer::inset2string(Buffer const &) const
238 {
239         return params2string(inset_.space());
240 }
241
242
243 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
244 {
245         vspace = VSpace();
246         if (in.empty())
247                 return;
248
249         istringstream data(in);
250         LyXLex lex(0,0);
251         lex.setStream(data);
252
253         string name;
254         lex >> name;
255         if (!lex || name != name_)
256                 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
257
258         string vsp;
259         lex >> vsp;
260         if (lex)
261                 vspace = VSpace(vsp);
262 }
263
264
265 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
266 {
267         ostringstream data;
268         data << name_ << ' ' << vspace.asLyXCommand();
269         return data.str();
270 }
271
272
273 } // namespace lyx