]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
Part of IU.
[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 DispatchResult
64 InsetVSpace::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
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(&cur.bv());
75                 return DispatchResult(true, true);
76
77         default:
78                 return InsetOld::priv_dispatch(cur, cmd);
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         xo_ = x;
133         yo_ = y;
134
135         x += ADD_TO_VSPACE_WIDTH;
136
137         int const arrow_size = 4;
138         int const start = y - dim_.asc;
139         int const end   = y + dim_.des;
140
141         // the label to display (if any)
142         string str;
143         // y-values for top arrow
144         int ty1, ty2;
145         // y-values for bottom arrow
146         int by1, by2;
147
148         str = label + " (" + space_.asLyXCommand() + ")";
149
150         if (space_.kind() == VSpace::VFILL) {
151                 ty1 = ty2 = start;
152                 by1 = by2 = end;
153         } else {
154                 // adding or removing space
155                 bool const added = space_.kind() != VSpace::LENGTH ||
156                                    space_.length().len().value() > 0.0;
157                 ty1 = added ? (start + arrow_size) : start;
158                 ty2 = added ? start : (start + arrow_size);
159                 by1 = added ? (end - arrow_size) : end;
160                 by2 = added ? end : (end - arrow_size);
161         }
162
163         int const midx = x + arrow_size;
164         int const rightx = midx + arrow_size;
165
166         // first the string
167         int w = 0;
168         int a = 0;
169         int d = 0;
170
171         LyXFont font;
172         font.setColor(LColor::added_space);
173         font.decSize();
174         font.decSize();
175         font_metrics::rectText(str, font, w, a, d);
176
177         pi.pain.rectText(x + 2 * arrow_size + 5, y + d,
178                        str, font, LColor::none, LColor::none);
179
180         // top arrow
181         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
182         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
183
184         // bottom arrow
185         pi.pain.line(x, by1, midx, by2, LColor::added_space);
186         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
187
188         // joining line
189         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
190 }
191
192
193 int InsetVSpace::latex(Buffer const & buf, ostream & os,
194                           OutputParams const &) const
195 {
196         os << space_.asLatexCommand(buf.params()) << '\n';
197         return 1;
198 }
199
200
201 int InsetVSpace::plaintext(Buffer const &, ostream & os,
202                            OutputParams const &) const
203 {
204         os << "\n\n";
205         return 2;
206 }
207
208
209 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
210                           OutputParams const &) const
211 {
212         os << '\n';
213         return 1;
214 }
215
216
217 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
218                          OutputParams const &) const
219 {
220         os << '\n';
221         return 1;
222 }
223
224
225 string const InsetVSpaceMailer::name_ = "vspace";
226
227
228 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
229         : inset_(inset)
230 {}
231
232
233 string const InsetVSpaceMailer::inset2string(Buffer const &) const
234 {
235         return params2string(inset_.space());
236 }
237
238
239 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
240 {
241         vspace = VSpace();
242         if (in.empty())
243                 return;
244
245         istringstream data(in);
246         LyXLex lex(0,0);
247         lex.setStream(data);
248
249         string name;
250         lex >> name;
251         if (!lex || name != name_)
252                 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
253
254         string vsp;
255         lex >> vsp;
256         if (lex)
257                 vspace = VSpace(vsp);
258 }
259
260
261 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
262 {
263         ostringstream data;
264         data << name_ << ' ' << vspace.asLyXCommand();
265         return data.str();
266 }