]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
0042f981d1792e86e4bb95c104638a51949adfec
[lyx.git] / src / insets / InsetVSpace.cpp
1 /**
2  * \file InsetVSpace.cpp
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 "Dimension.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Lexer.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "Text.h"
27
28 #include "support/debug.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
31
32 #include "frontends/Application.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include <sstream>
37
38 using namespace std;
39
40 namespace lyx {
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         hideDialogs("vspace", this);
57 }
58
59
60 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
61 {
62         switch (cmd.action) {
63
64         case LFUN_INSET_MODIFY: {
65                 InsetVSpace::string2params(to_utf8(cmd.argument()), space_);
66                 break;
67         }
68
69         case LFUN_MOUSE_RELEASE:
70                 if (!cur.selection() && cmd.button() == mouse_button::button1)
71                         cur.bv().showDialog("vspace", params2string(space()), 
72                                 const_cast<InsetVSpace *>(this));
73                 break;
74
75         default:
76                 Inset::doDispatch(cur, cmd);
77                 break;
78         }
79 }
80
81
82 bool InsetVSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
83         FuncStatus & status) const
84 {
85         switch (cmd.action) {
86         // we handle these
87         case LFUN_INSET_MODIFY:
88                 if (cmd.getArg(0) == "vspace") {
89                         VSpace vspace;
90                         InsetVSpace::string2params(to_utf8(cmd.argument()), vspace);
91                         status.setOnOff(vspace == space_);
92                 } 
93                 status.setEnabled(true);
94                 return true;
95         
96         case LFUN_INSET_SETTINGS:
97                 status.setEnabled(true);
98                 return true;
99
100         default:
101                 return Inset::getStatus(cur, cmd, status);
102         }
103 }
104
105
106 void InsetVSpace::edit(Cursor & cur, bool, EntryDirection)
107 {
108         showInsetDialog(&cur.bv());
109 }
110
111
112 bool InsetVSpace::showInsetDialog(BufferView * bv) const
113 {
114         bv->showDialog("vspace", params2string(space()),
115                 const_cast<InsetVSpace *>(this));
116         return true;
117 }
118
119
120 void InsetVSpace::read(Lexer & lex)
121 {
122         LASSERT(lex.isOK(), /**/);
123         string vsp;
124         lex >> vsp;
125         if (lex)
126                 space_ = VSpace(vsp);
127         lex >> "\\end_inset";
128 }
129
130
131 void InsetVSpace::write(ostream & os) const
132 {
133         os << "VSpace " << space_.asLyXCommand();
134 }
135
136
137 docstring const InsetVSpace::label() const
138 {
139         static docstring const label = _("Vertical Space");
140         return label + " (" + space_.asGUIName() + ')';
141 }
142
143
144 namespace {
145 int const vspace_arrow_size = 4;
146 }
147
148
149 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
150 {
151         int height = 3 * vspace_arrow_size;
152         if (space_.length().len().value() >= 0.0)
153                 height = max(height, space_.inPixels(*mi.base.bv));
154
155         FontInfo font;
156         font.decSize();
157         font.decSize();
158
159         int w = 0;
160         int a = 0;
161         int d = 0;
162         theFontMetrics(font).rectText(label(), w, a, d);
163
164         height = max(height, a + d);
165
166         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
167         dim.des = height - dim.asc;         // label text
168         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * vspace_arrow_size + 5 + w;
169         // Cache the inset dimension. 
170         setDimCache(mi, dim);
171 }
172
173
174 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
175 {
176         Dimension const dim = dimension(*pi.base.bv);
177         x += ADD_TO_VSPACE_WIDTH;
178         int const start = y - dim.asc;
179         int const end   = y + dim.des;
180
181         // y-values for top arrow
182         int ty1, ty2;
183         // y-values for bottom arrow
184         int by1, by2;
185
186         if (space_.kind() == VSpace::VFILL) {
187                 ty1 = ty2 = start;
188                 by1 = by2 = end;
189         } else {
190                 // adding or removing space
191                 bool const added = space_.kind() != VSpace::LENGTH ||
192                                    space_.length().len().value() >= 0.0;
193                 ty1 = added ? (start + vspace_arrow_size) : start;
194                 ty2 = added ? start : (start + vspace_arrow_size);
195                 by1 = added ? (end - vspace_arrow_size) : end;
196                 by2 = added ? end : (end - vspace_arrow_size);
197         }
198
199         int const midx = x + vspace_arrow_size;
200         int const rightx = midx + vspace_arrow_size;
201
202         // first the string
203         int w = 0;
204         int a = 0;
205         int d = 0;
206
207         FontInfo font;
208         font.setColor(Color_added_space);
209         font.decSize();
210         font.decSize();
211         docstring const lab = label();
212         theFontMetrics(font).rectText(lab, w, a, d);
213
214         pi.pain.rectText(x + 2 * vspace_arrow_size + 5,
215                          start + (end - start) / 2 + (a - d) / 2,
216                          lab, font, Color_none, Color_none);
217
218         // top arrow
219         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
220         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
221
222         // bottom arrow
223         pi.pain.line(x, by1, midx, by2, Color_added_space);
224         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
225
226         // joining line
227         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
228 }
229
230
231 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
232 {
233         os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
234         return 1;
235 }
236
237
238 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
239 {
240         os << "\n\n";
241         return PLAINTEXT_NEWLINE;
242 }
243
244
245 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
246 {
247         os << '\n';
248         return 1;
249 }
250
251
252 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
253 {
254         return from_ascii("context-vspace");
255 }
256
257
258 void InsetVSpace::string2params(string const & in, VSpace & vspace)
259 {
260         vspace = VSpace();
261         if (in.empty())
262                 return;
263
264         istringstream data(in);
265         Lexer lex;
266         lex.setStream(data);
267         lex.setContext("InsetVSpace::string2params");
268         lex >> "vspace" >> vspace;
269 }
270
271
272 string InsetVSpace::params2string(VSpace const & vspace)
273 {
274         ostringstream data;
275         data << "vspace" << ' ' << vspace.asLyXCommand();
276         return data.str();
277 }
278
279
280 } // namespace lyx