]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
FuncStatus::enabled(bool) --> FuncStatus::setEnabled(bool)
[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 "Text.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27
28 #include "support/lassert.h"
29 #include "support/debug.h"
30 #include "support/gettext.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         default:
96                 return Inset::getStatus(cur, cmd, status);
97         }
98 }
99
100
101 void InsetVSpace::edit(Cursor & cur, bool, EntryDirection)
102 {
103         cur.bv().showDialog("vspace", params2string(space()), 
104                 const_cast<InsetVSpace *>(this));
105 }
106
107
108 void InsetVSpace::read(Lexer & lex)
109 {
110         LASSERT(lex.isOK(), /**/);
111         string vsp;
112         lex >> vsp;
113         if (lex)
114                 space_ = VSpace(vsp);
115         lex >> "\\end_inset";
116 }
117
118
119 void InsetVSpace::write(ostream & os) const
120 {
121         os << "VSpace " << space_.asLyXCommand();
122 }
123
124
125 docstring const InsetVSpace::label() const
126 {
127         static docstring const label = _("Vertical Space");
128         return label + " (" + space_.asGUIName() + ')';
129 }
130
131
132 namespace {
133 int const arrow_size = 4;
134 }
135
136
137 void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
138 {
139         int height = 3 * arrow_size;
140         if (space_.length().len().value() >= 0.0)
141                 height = max(height, space_.inPixels(*mi.base.bv));
142
143         FontInfo font;
144         font.decSize();
145         font.decSize();
146
147         int w = 0;
148         int a = 0;
149         int d = 0;
150         theFontMetrics(font).rectText(label(), w, a, d);
151
152         height = max(height, a + d);
153
154         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
155         dim.des = height - dim.asc;         // label text
156         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
157         // Cache the inset dimension. 
158         setDimCache(mi, dim);
159 }
160
161
162 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
163 {
164         Dimension const dim = dimension(*pi.base.bv);
165         x += ADD_TO_VSPACE_WIDTH;
166         int const start = y - dim.asc;
167         int const end   = y + dim.des;
168
169         // y-values for top arrow
170         int ty1, ty2;
171         // y-values for bottom arrow
172         int by1, by2;
173
174         if (space_.kind() == VSpace::VFILL) {
175                 ty1 = ty2 = start;
176                 by1 = by2 = end;
177         } else {
178                 // adding or removing space
179                 bool const added = space_.kind() != VSpace::LENGTH ||
180                                    space_.length().len().value() >= 0.0;
181                 ty1 = added ? (start + arrow_size) : start;
182                 ty2 = added ? start : (start + arrow_size);
183                 by1 = added ? (end - arrow_size) : end;
184                 by2 = added ? end : (end - arrow_size);
185         }
186
187         int const midx = x + arrow_size;
188         int const rightx = midx + arrow_size;
189
190         // first the string
191         int w = 0;
192         int a = 0;
193         int d = 0;
194
195         FontInfo font;
196         font.setColor(Color_added_space);
197         font.decSize();
198         font.decSize();
199         docstring const lab = label();
200         theFontMetrics(font).rectText(lab, w, a, d);
201
202         pi.pain.rectText(x + 2 * arrow_size + 5,
203                          start + (end - start) / 2 + (a - d) / 2,
204                          lab, font, Color_none, Color_none);
205
206         // top arrow
207         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
208         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
209
210         // bottom arrow
211         pi.pain.line(x, by1, midx, by2, Color_added_space);
212         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
213
214         // joining line
215         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
216 }
217
218
219 int InsetVSpace::latex(odocstream & os, OutputParams const &) const
220 {
221         os << from_ascii(space_.asLatexCommand(buffer().params())) << '\n';
222         return 1;
223 }
224
225
226 int InsetVSpace::plaintext(odocstream & os, OutputParams const &) const
227 {
228         os << "\n\n";
229         return PLAINTEXT_NEWLINE;
230 }
231
232
233 int InsetVSpace::docbook(odocstream & os, OutputParams const &) const
234 {
235         os << '\n';
236         return 1;
237 }
238
239
240 docstring InsetVSpace::contextMenu(BufferView const &, int, int) const
241 {
242         return from_ascii("context-vspace");
243 }
244
245
246 void InsetVSpace::string2params(string const & in, VSpace & vspace)
247 {
248         vspace = VSpace();
249         if (in.empty())
250                 return;
251
252         istringstream data(in);
253         Lexer lex;
254         lex.setStream(data);
255         lex.setContext("InsetVSpace::string2params");
256         lex >> "vspace" >> vspace;
257 }
258
259
260 string InsetVSpace::params2string(VSpace const & vspace)
261 {
262         ostringstream data;
263         data << "vspace" << ' ' << vspace.asLyXCommand();
264         return data.str();
265 }
266
267
268 } // namespace lyx