]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
* insettabular.[Ch]: remove remains of the 'update' mechanism,
[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 "cursor.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "metricsinfo.h"
26
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29
30 #include "support/std_sstream.h"
31
32 using std::istringstream;
33 using std::ostream;
34 using std::ostringstream;
35 using std::string;
36 using std::max;
37
38
39 namespace {
40
41 int const ADD_TO_VSPACE_WIDTH = 5;
42
43 } // namespace anon
44
45
46 InsetVSpace::InsetVSpace(VSpace const & space)
47         : space_(space)
48 {}
49
50
51 InsetVSpace::~InsetVSpace()
52 {
53         InsetVSpaceMailer(*this).hideDialog();
54 }
55
56
57 std::auto_ptr<InsetBase> InsetVSpace::clone() const
58 {
59         return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
60 }
61
62
63 void InsetVSpace::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
64 {
65         switch (cmd.action) {
66
67         case LFUN_INSET_MODIFY: {
68                 InsetVSpaceMailer::string2params(cmd.argument, space_);
69                 break;
70         }
71
72         case LFUN_MOUSE_PRESS:
73                 InsetVSpaceMailer(*this).showDialog(&cur.bv());
74                 break;
75
76         default:
77                 InsetOld::priv_dispatch(cur, cmd);
78                 break;
79         }
80 }
81
82
83 void InsetVSpace::read(Buffer const &, LyXLex & lex)
84 {
85         BOOST_ASSERT(lex.isOK());
86         string vsp;
87         lex >> vsp;
88         if (lex)
89                 space_ = VSpace(vsp);
90
91         string end_token;
92         lex >> end_token;
93         if (end_token != "\\end_inset")
94                 lex.printError("Missing \\end_inset at this point. "
95                                "Read: `$$Token'");
96 }
97
98
99 void InsetVSpace::write(Buffer const &, ostream & os) const
100 {
101         os << "VSpace " << space_.asLyXCommand();
102 }
103
104
105 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         int size = 10;
108         int const arrow_size = 4;
109         int const space_size = space_.inPixels(*mi.base.bv);
110
111         LyXFont font;
112         font.decSize();
113         int const min_size = max(3 * arrow_size, font_metrics::maxHeight(font));
114
115         if (space_.length().len().value() < 0.0)
116                 size = min_size;
117         else
118                 size = max(min_size, space_size);
119
120         dim.asc = size / 2;
121         dim.des = size / 2;
122         dim.wid = 10 + 2 * ADD_TO_VSPACE_WIDTH;
123
124         dim_ = dim;
125 }
126
127
128 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
129 {
130         static std::string const label = _("Vertical Space");
131
132         setPosCache(pi, x, 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 }