]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
Introduce wide streams. This fixes the remaining problems of plain text
[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 "cursor.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/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include <sstream>
30
31 using lyx::docstring;
32
33 using std::istringstream;
34 using std::ostream;
35 using std::ostringstream;
36 using std::string;
37 using std::max;
38
39
40 namespace {
41
42 int const ADD_TO_VSPACE_WIDTH = 5;
43
44 } // namespace anon
45
46
47 InsetVSpace::InsetVSpace(VSpace const & space)
48         : space_(space)
49 {}
50
51
52 InsetVSpace::~InsetVSpace()
53 {
54         InsetVSpaceMailer(*this).hideDialog();
55 }
56
57
58 std::auto_ptr<InsetBase> InsetVSpace::doClone() const
59 {
60         return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
61 }
62
63
64 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & cmd)
65 {
66         switch (cmd.action) {
67
68         case LFUN_INSET_MODIFY: {
69                 InsetVSpaceMailer::string2params(lyx::to_utf8(cmd.argument()), space_);
70                 break;
71         }
72
73         case LFUN_MOUSE_RELEASE:
74                 InsetVSpaceMailer(*this).showDialog(&cur.bv());
75                 break;
76
77         default:
78                 InsetBase::doDispatch(cur, cmd);
79                 break;
80         }
81 }
82
83
84 void InsetVSpace::read(Buffer const &, LyXLex & lex)
85 {
86         BOOST_ASSERT(lex.isOK());
87         string vsp;
88         lex >> vsp;
89         if (lex)
90                 space_ = VSpace(vsp);
91
92         string end_token;
93         lex >> end_token;
94         if (end_token != "\\end_inset")
95                 lex.printError("Missing \\end_inset at this point. "
96                                "Read: `$$Token'");
97 }
98
99
100 void InsetVSpace::write(Buffer const &, ostream & os) const
101 {
102         os << "VSpace " << space_.asLyXCommand();
103 }
104
105
106 string const InsetVSpace::label() const
107 {
108         // FIXME UNICODE
109         static string const label = lyx::to_utf8(_("Vertical Space"));
110         return label + " (" + space_.asGUIName() + ')';
111 }
112
113
114 namespace {
115 int const arrow_size = 4;
116 }
117
118
119 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
120 {
121         int height = 3 * arrow_size;
122         if (space_.length().len().value() >= 0.0)
123                 height = max(height, space_.inPixels(*mi.base.bv));
124
125         LyXFont font;
126         font.decSize();
127         font.decSize();
128
129         int w = 0;
130         int a = 0;
131         int d = 0;
132         string lab = label();
133         docstring dlab(lab.begin(), lab.end());
134         theFontMetrics(font).rectText(dlab, w, a, d);
135
136         height = max(height, a + d);
137
138         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
139         dim.des = height - dim.asc;         // label text
140         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
141         dim_ = dim;
142 }
143
144
145 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
146 {
147         setPosCache(pi, x, y);
148
149         x += ADD_TO_VSPACE_WIDTH;
150
151         int const start = y - dim_.asc;
152         int const end   = y + dim_.des;
153
154         // y-values for top arrow
155         int ty1, ty2;
156         // y-values for bottom arrow
157         int by1, by2;
158
159         if (space_.kind() == VSpace::VFILL) {
160                 ty1 = ty2 = start;
161                 by1 = by2 = end;
162         } else {
163                 // adding or removing space
164                 bool const added = space_.kind() != VSpace::LENGTH ||
165                                    space_.length().len().value() >= 0.0;
166                 ty1 = added ? (start + arrow_size) : start;
167                 ty2 = added ? start : (start + arrow_size);
168                 by1 = added ? (end - arrow_size) : end;
169                 by2 = added ? end : (end - arrow_size);
170         }
171
172         int const midx = x + arrow_size;
173         int const rightx = midx + arrow_size;
174
175         // first the string
176         int w = 0;
177         int a = 0;
178         int d = 0;
179
180         LyXFont font;
181         font.setColor(LColor::added_space);
182         font.decSize();
183         font.decSize();
184         string lab = label();
185         docstring dlab(lab.begin(), lab.end());
186         theFontMetrics(font).rectText(dlab, w, a, d);
187
188         pi.pain.rectText(x + 2 * arrow_size + 5,
189                          start + (end - start) / 2 + (a - d) / 2,
190                          dlab, font, LColor::none, LColor::none);
191
192         // top arrow
193         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
194         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
195
196         // bottom arrow
197         pi.pain.line(x, by1, midx, by2, LColor::added_space);
198         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
199
200         // joining line
201         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
202 }
203
204
205 int InsetVSpace::latex(Buffer const & buf, ostream & os,
206                           OutputParams const &) const
207 {
208         os << space_.asLatexCommand(buf.params()) << '\n';
209         return 1;
210 }
211
212
213 int InsetVSpace::plaintext(Buffer const &, lyx::odocstream & os,
214                            OutputParams const &) const
215 {
216         os << "\n\n";
217         return 2;
218 }
219
220
221 int InsetVSpace::docbook(Buffer const &, std::ostream & os,
222                          OutputParams const &) const
223 {
224         os << '\n';
225         return 1;
226 }
227
228
229 string const InsetVSpaceMailer::name_ = "vspace";
230
231
232 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
233         : inset_(inset)
234 {}
235
236
237 string const InsetVSpaceMailer::inset2string(Buffer const &) const
238 {
239         return params2string(inset_.space());
240 }
241
242
243 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
244 {
245         vspace = VSpace();
246         if (in.empty())
247                 return;
248
249         istringstream data(in);
250         LyXLex lex(0,0);
251         lex.setStream(data);
252
253         string name;
254         lex >> name;
255         if (!lex || name != name_)
256                 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
257
258         string vsp;
259         lex >> vsp;
260         if (lex)
261                 vspace = VSpace(vsp);
262 }
263
264
265 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
266 {
267         ostringstream data;
268         data << name_ << ' ' << vspace.asLyXCommand();
269         return data.str();
270 }