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