]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
The VSpace fixes (removal of VSPACE::NONE) from Michael and myself
[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(FuncRequest const & cmd,
64                            idx_type & idx, pos_type & pos)
65 {
66         switch (cmd.action) {
67
68         case LFUN_INSET_MODIFY: {
69                 InsetVSpaceMailer::string2params(cmd.argument, space_);
70                 return DispatchResult(true, true);
71         }
72
73         case LFUN_MOUSE_PRESS:
74                 InsetVSpaceMailer(*this).showDialog(cmd.view());
75                 return DispatchResult(true, true);
76
77         default:
78                 return InsetOld::priv_dispatch(cmd, idx, pos);
79         }
80 }
81                 
82
83 void InsetVSpace::read(Buffer const &, LyXLex & lex)
84 {
85         if (lex.isOK()) {
86                 lex.next();
87                 space_ = VSpace(lex.getString());
88         }
89 }
90
91
92 void InsetVSpace::write(Buffer const &, ostream & os) const
93 {
94         os << "VSpace " << space_.asLyXCommand();
95 }
96
97
98 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
99 {
100         int size = 10;
101         int const arrow_size = 4;
102         int const space_size = space_.inPixels(*mi.base.bv);
103
104         LyXFont font;
105         font.decSize();
106         int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
107
108         if (space_.length().len().value() < 0.0)
109                 size = min_size;
110         else
111                 size = max(min_size, space_size);
112
113         dim.asc = size / 2;
114         dim.des = size / 2;
115         dim.wid = 10 + 2 * ADD_TO_VSPACE_WIDTH;
116
117         dim_ = dim;
118 }
119
120
121 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
122 {
123         static std::string const label = _("Vertical Space");
124
125         xo_ = x;
126         yo_ = y;
127
128         x += ADD_TO_VSPACE_WIDTH;
129
130         int const arrow_size = 4;
131         int const start = y - dim_.asc;
132         int const end   = y + dim_.des;
133
134         // the label to display (if any)
135         string str;
136         // y-values for top arrow
137         int ty1, ty2;
138         // y-values for bottom arrow
139         int by1, by2;
140
141         str = label + " (" + space_.asLyXCommand() + ")";
142
143         if (space_.kind() == VSpace::VFILL) {
144                 ty1 = ty2 = start;
145                 by1 = by2 = end;
146         } else {
147                 // adding or removing space
148                 bool const added = space_.kind() != VSpace::LENGTH ||
149                                    space_.length().len().value() > 0.0;
150                 ty1 = added ? (start + arrow_size) : start;
151                 ty2 = added ? start : (start + arrow_size);
152                 by1 = added ? (end - arrow_size) : end;
153                 by2 = added ? end : (end - arrow_size);
154         }
155
156         int const midx = x + arrow_size;
157         int const rightx = midx + arrow_size;
158
159         // first the string
160         int w = 0;
161         int a = 0;
162         int d = 0;
163
164         LyXFont font;
165         font.setColor(LColor::added_space);
166         font.decSize();
167         font.decSize();
168         font_metrics::rectText(str, font, w, a, d);
169
170         pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
171                        str, font, LColor::none, LColor::none);
172
173         // top arrow
174         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
175         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
176
177         // bottom arrow
178         pi.pain.line(x, by1, midx, by2, LColor::added_space);
179         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
180
181         // joining line
182         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
183 }
184
185
186 int InsetVSpace::latex(Buffer const & buf, ostream & os,
187                           OutputParams const &) const
188 {
189         os << space_.asLatexCommand(buf.params()) << '\n';
190         return 1;
191 }
192
193
194 int InsetVSpace::plaintext(Buffer const &, ostream & os,
195                            OutputParams const &) const
196 {
197         os << "\n\n";
198         return 2;
199 }
200
201
202 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
203                           OutputParams const &) const
204 {
205         os << '\n';
206         return 1;
207 }
208
209
210 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
211                          OutputParams const &) const
212 {
213         os << '\n';
214         return 1;
215 }
216
217
218 string const InsetVSpaceMailer::name_ = "vspace";
219
220
221 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
222         : inset_(inset)
223 {}
224
225
226 string const InsetVSpaceMailer::inset2string(Buffer const &) const
227 {
228         return params2string(inset_.space_);
229 }
230
231
232 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
233 {
234         vspace = VSpace();
235
236         if (in.empty())
237                 return;
238
239         istringstream data(in);
240         LyXLex lex(0,0);
241         lex.setStream(data);
242
243         if (lex.isOK()) {
244                 lex.next();
245                 string const name = lex.getString();
246         }
247
248         // This is part of the inset proper that is usually swallowed
249         // by Buffer::readInset
250         if (lex.isOK()) {
251                 lex.next();
252                 vspace = VSpace(lex.getString());
253         }
254 }
255
256
257 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
258 {
259         ostringstream data;
260         data << name_ << ' ' << vspace.asLyXCommand();
261         return data.str();
262 }