]> git.lyx.org Git - lyx.git/blob - src/insets/insetvspace.C
The speed patch: redraw only rows that have changed
[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 <sstream>
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::doClone() const
58 {
59         return std::auto_ptr<InsetBase>(new InsetVSpace(*this));
60 }
61
62
63 void InsetVSpace::doDispatch(LCursor & cur, FuncRequest & 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                 InsetBase::doDispatch(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 string const InsetVSpace::label() const
106 {
107         static string const label = _("Vertical Space");
108         return label + " (" + space_.asGUIName() + ')';
109 }
110
111
112 namespace {
113 int const arrow_size = 4;
114 }
115
116
117 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
118 {
119         int height = 3 * arrow_size;
120         if (space_.length().len().value() >= 0.0)
121                 height = max(height, space_.inPixels(*mi.base.bv));
122
123         LyXFont font;
124         font.decSize();
125         font.decSize();
126
127         int w = 0;
128         int a = 0;
129         int d = 0;
130         font_metrics::rectText(label(), font, w, a, d);
131
132         height = max(height, a + d);
133
134         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
135         dim.des = height - dim.asc;         // label text
136         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
137         dim_ = dim;
138 }
139
140
141 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
142 {
143         setPosCache(pi, x, y);
144
145         x += ADD_TO_VSPACE_WIDTH;
146
147         int const start = y - dim_.asc;
148         int const end   = y + dim_.des;
149
150         // y-values for top arrow
151         int ty1, ty2;
152         // y-values for bottom arrow
153         int by1, by2;
154
155         if (space_.kind() == VSpace::VFILL) {
156                 ty1 = ty2 = start;
157                 by1 = by2 = end;
158         } else {
159                 // adding or removing space
160                 bool const added = space_.kind() != VSpace::LENGTH ||
161                                    space_.length().len().value() >= 0.0;
162                 ty1 = added ? (start + arrow_size) : start;
163                 ty2 = added ? start : (start + arrow_size);
164                 by1 = added ? (end - arrow_size) : end;
165                 by2 = added ? end : (end - arrow_size);
166         }
167
168         int const midx = x + arrow_size;
169         int const rightx = midx + arrow_size;
170
171         // first the string
172         int w = 0;
173         int a = 0;
174         int d = 0;
175
176         LyXFont font;
177         font.setColor(LColor::added_space);
178         font.decSize();
179         font.decSize();
180         font_metrics::rectText(label(), font, w, a, d);
181
182         pi.pain.rectText(x + 2 * arrow_size + 5, 
183                          start + (end - start) / 2 + (a - d) / 2,
184                          label(), font, LColor::none, LColor::none);
185
186         // top arrow
187         pi.pain.line(x, ty1, midx, ty2, LColor::added_space);
188         pi.pain.line(midx, ty2, rightx, ty1, LColor::added_space);
189
190         // bottom arrow
191         pi.pain.line(x, by1, midx, by2, LColor::added_space);
192         pi.pain.line(midx, by2, rightx, by1, LColor::added_space);
193
194         // joining line
195         pi.pain.line(midx, ty2, midx, by2, LColor::added_space);
196 }
197
198
199 int InsetVSpace::latex(Buffer const & buf, ostream & os,
200                           OutputParams const &) const
201 {
202         os << space_.asLatexCommand(buf.params()) << '\n';
203         return 1;
204 }
205
206
207 int InsetVSpace::plaintext(Buffer const &, ostream & os,
208                            OutputParams const &) const
209 {
210         os << "\n\n";
211         return 2;
212 }
213
214
215 int InsetVSpace::linuxdoc(Buffer const &, std::ostream & os,
216                           OutputParams const &) const
217 {
218         os << '\n';
219         return 1;
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 }