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