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