]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
move space above/below from Paragraph into a separate vspace inset.
[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 "debug.h"
18 #include "LColor.h"
19 #include "lyxlex.h"
20 #include "lyxtext.h"
21 #include "metricsinfo.h"
22 #include "gettext.h"
23
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26
27 #include "support/std_sstream.h"
28
29 using std::endl;
30 using std::istringstream;
31 using std::ostream;
32 using std::ostringstream;
33 using std::string;
34 using std::max;
35
36
37
38 InsetVSpace::InsetVSpace(VSpace const & space)
39         : space_(space)
40 {}
41
42
43 std::auto_ptr<InsetBase> InsetVSpace::clone() const
44 {
45         return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
46 }
47
48
49 void InsetVSpace::read(Buffer const &, LyXLex & lex)
50 {
51         if (lex.isOK()) {
52                 lex.next();
53                 space_ = VSpace(lex.getString());
54         }
55 }
56
57
58 void InsetVSpace::write(Buffer const &, ostream & os) const
59 {
60         os << "VSpace " << space_.asLyXCommand();
61 }
62
63
64 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
65 {
66         int size = 10;
67         if (space_.kind() != VSpace::NONE) {
68                 int const arrow_size = 4;
69                 int const space_size = space_.inPixels(*mi.base.bv);
70
71                 LyXFont font;
72                 font.decSize();
73                 int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
74
75                 if (space_.length().len().value() < 0.0)
76                         size = min_size;
77                 else
78                         size = max(min_size, space_size);
79         }
80
81         dim.asc = size / 2;
82         dim.des = size / 2;
83         dim.wid = 10;
84
85         dim_ = dim;
86 }
87
88
89 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
90 {
91         static std::string const label = _("Vertical Space");
92
93         xo_ = x;
94         yo_ = y;
95
96         //if (space_.kind() == VSpace::NONE)
97         //      return 0;
98
99         int const arrow_size = 4;
100         int const start = y - dim_.asc;
101         int const end   = y + dim_.des;
102
103         // the label to display (if any)
104         string str;
105         // y-values for top arrow
106         int ty1, ty2;
107         // y-values for bottom arrow
108         int by1, by2;
109
110         str = label + " (" + space_.asLyXCommand() + ")";
111
112         if (space_.kind() == VSpace::VFILL) {
113                 ty1 = ty2 = start;
114                 by1 = by2 = end;
115         } else {
116                 // adding or removing space
117                 bool const added = space_.kind() != VSpace::LENGTH ||
118                                    space_.length().len().value() > 0.0;
119                 ty1 = added ? (start + arrow_size) : start;
120                 ty2 = added ? start : (start + arrow_size);
121                 by1 = added ? (end - arrow_size) : end;
122                 by2 = added ? end : (end - arrow_size);
123         }
124
125         int const midx = xo_ + arrow_size;
126         int const rightx = midx + arrow_size;
127
128         // first the string
129         int w = 0;
130         int a = 0;
131         int d = 0;
132
133         LyXFont font;
134         font.setColor(LColor::added_space);
135         font.decSize();
136         font.decSize();
137         font_metrics::rectText(str, font, w, a, d);
138
139         pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
140                        str, font, LColor::none, LColor::none);
141
142         // top arrow
143         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
144         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
145
146         // bottom arrow
147         pi.pain.line(x, by1, midx, by2, LColor::added_space);
148         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
149
150         // joining line
151         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
152 }
153
154
155 int InsetVSpace::latex(Buffer const & buf, ostream & os,
156                           OutputParams const &) const
157 {
158         os << space_.asLatexCommand(buf.params()) << '\n';
159         return 1;
160 }
161
162
163 int InsetVSpace::plaintext(Buffer const &, ostream & os,
164                            OutputParams const &) const
165 {
166         os << "\n\n";
167         return 2;
168 }
169
170
171 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
172                           OutputParams const &) const
173 {
174         os << '\n';
175         return 1;
176 }
177
178
179 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
180                          OutputParams const &) const
181 {
182         os << '\n';
183         return 1;
184 }
185
186
187 string const InsetVSpaceMailer::name_ = "vspace";
188
189
190 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
191         : inset_(inset)
192 {}
193
194
195 string const InsetVSpaceMailer::inset2string(Buffer const &) const
196 {
197         return params2string(inset_.space_);
198 }
199
200
201 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
202 {
203         //lyxerr << "\nInsetVSpaceMailer::string2params, got: '" << in << "'" << endl;
204
205         vspace = VSpace();
206
207         if (in.empty())
208                 return;
209
210         istringstream data(in);
211         LyXLex lex(0,0);
212         lex.setStream(data);
213
214         if (lex.isOK()) {
215                 lex.next();
216                 string const name = lex.getString();
217         }
218
219         // This is part of the inset proper that is usually swallowed
220         // by Buffer::readInset
221         if (lex.isOK()) {
222                 lex.next();
223                 vspace = VSpace(lex.getString());
224         }
225 }
226
227
228 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
229 {
230         ostringstream data;
231         data << name_ << ' ' << vspace.asLyXCommand();
232         return data.str();
233 }