]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
aea6976a526561b70aa3bdb6a127a1862416ff40
[lyx.git] / src / insets / InsetWrap.cpp
1 /**
2  * \file InsetWrap.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetWrap.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "support/debug.h"
22 #include "DispatchResult.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "OutputParams.h"
31 #include "TextClass.h"
32 #include "TocBackend.h"
33
34 #include "support/convert.h"
35 #include "support/docstream.h"
36
37
38 namespace lyx {
39
40 using std::string;
41 using std::endl;
42 using std::istringstream;
43 using std::ostream;
44 using std::ostringstream;
45
46
47 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
48         : InsetCollapsable(bp), name_(from_utf8(type))
49 {
50         setLabel(_("wrap: ") + floatName(type, bp));
51         params_.type = type;
52         params_.lines = 0;
53         params_.placement = "o";
54         params_.overhang = Length(0, Length::PCW);
55         params_.width = Length(50, Length::PCW);
56 }
57
58
59 InsetWrap::~InsetWrap()
60 {
61         InsetWrapMailer(*this).hideDialog();
62 }
63
64
65 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
66 {
67         switch (cmd.action) {
68         case LFUN_INSET_MODIFY: {
69                 InsetWrapParams params;
70                 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
71                 params_.lines = params.lines;
72                 params_.placement = params.placement;
73                 params_.overhang = params.overhang;
74                 params_.width = params.width;
75                 break;
76         }
77
78         case LFUN_INSET_DIALOG_UPDATE:
79                 InsetWrapMailer(*this).updateDialog(&cur.bv());
80                 break;
81
82         case LFUN_MOUSE_RELEASE: {
83                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
84                         InsetWrapMailer(*this).showDialog(&cur.bv());
85                         break;
86                 }
87                 InsetCollapsable::doDispatch(cur, cmd);
88                 break;
89         }
90
91         default:
92                 InsetCollapsable::doDispatch(cur, cmd);
93                 break;
94         }
95 }
96
97
98 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
99                 FuncStatus & flag) const
100 {
101         switch (cmd.action) {
102         case LFUN_INSET_MODIFY:
103         case LFUN_INSET_DIALOG_UPDATE:
104                 flag.enabled(true);
105                 return true;
106
107         default:
108                 return InsetCollapsable::getStatus(cur, cmd, flag);
109         }
110 }
111
112
113 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
114 {
115         Counters & cnts = buf.params().getTextClass().counters();
116         string const saveflt = cnts.current_float();
117
118         // Tell to captions what the current float is
119         cnts.current_float(params().type);
120
121         InsetCollapsable::updateLabels(buf, it);
122
123         //reset afterwards
124         cnts.current_float(saveflt);
125 }
126
127
128 void InsetWrapParams::write(ostream & os) const
129 {
130         os << "Wrap " << type << '\n';
131         os << "lines " << lines << "\n";
132         os << "placement " << placement << "\n";
133         os << "overhang " << overhang.asString() << "\n";
134         os << "width \"" << width.asString() << "\"\n";
135 }
136
137
138 void InsetWrapParams::read(Lexer & lex)
139 {
140         string token;
141
142         lex >> token;
143         if (token == "lines")
144                 lex >> lines;
145         else {
146                 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
147                         << endl;
148                 // take countermeasures
149                 lex.pushToken(token);
150         }
151         if (!lex)
152                 return;
153         lex >> token;
154         if (token == "placement")
155                 lex >> placement;
156         else {
157                 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
158                         << endl;
159                 lex.pushToken(token);
160         }
161         if (!lex)
162                 return;
163         lex >> token;
164         if (token == "overhang") {
165                 lex.next();
166                 overhang = Length(lex.getString());
167         } else {
168                 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
169                         << endl;
170                 lex.pushToken(token);
171         }
172         if (!lex)
173                 return;
174         lex >> token;
175         if (token == "width") {
176                 lex.next();
177                 width = Length(lex.getString());
178         } else {
179                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
180                         << endl;
181                 lex.pushToken(token);
182         }
183 }
184
185
186 void InsetWrap::write(Buffer const & buf, ostream & os) const
187 {
188         params_.write(os);
189         InsetCollapsable::write(buf, os);
190 }
191
192
193 void InsetWrap::read(Buffer const & buf, Lexer & lex)
194 {
195         params_.read(lex);
196         InsetCollapsable::read(buf, lex);
197 }
198
199
200 void InsetWrap::validate(LaTeXFeatures & features) const
201 {
202         features.require("wrapfig");
203         InsetCollapsable::validate(features);
204 }
205
206
207 Inset * InsetWrap::clone() const
208 {
209         return new InsetWrap(*this);
210 }
211
212
213 docstring const InsetWrap::editMessage() const
214 {
215         return _("Opened Wrap Inset");
216 }
217
218
219 int InsetWrap::latex(Buffer const & buf, odocstream & os,
220                      OutputParams const & runparams) const
221 {
222         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
223         // no optional argument when lines are zero
224         if (params_.lines != 0)
225                 os << '[' << params_.lines << ']';
226         os << '{' << from_ascii(params_.placement) << '}';
227         Length over(params_.overhang);
228         // no optional argument when the value is zero
229         if (over.value() != 0)
230                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
231         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
232         int const i = InsetText::latex(buf, os, runparams);
233         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
234         return i + 2;
235 }
236
237
238 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
239                          OutputParams const & runparams) const
240 {
241         os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
242         InsetText::plaintext(buf, os, runparams);
243         os << "\n]";
244
245         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
246 }
247
248
249 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
250                        OutputParams const & runparams) const
251 {
252         // FIXME UNICODE
253         os << '<' << from_ascii(params_.type) << '>';
254         int const i = InsetText::docbook(buf, os, runparams);
255         os << "</" << from_ascii(params_.type) << '>';
256         return i;
257 }
258
259
260 bool InsetWrap::insetAllowed(InsetCode code) const
261 {
262         switch(code) {
263         case FLOAT_CODE:
264         case FOOT_CODE:
265         case MARGIN_CODE:
266                 return false;
267         default:
268                 return InsetCollapsable::insetAllowed(code);
269         }
270 }
271
272
273 bool InsetWrap::showInsetDialog(BufferView * bv) const
274 {
275         if (!InsetText::showInsetDialog(bv))
276                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
277         return true;
278 }
279
280
281 string const InsetWrapMailer::name_("wrap");
282
283 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
284         : inset_(inset)
285 {}
286
287
288 string const InsetWrapMailer::inset2string(Buffer const &) const
289 {
290         return params2string(inset_.params());
291 }
292
293
294 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
295 {
296         params = InsetWrapParams();
297         if (in.empty())
298                 return;
299
300         istringstream data(in);
301         Lexer lex(0,0);
302         lex.setStream(data);
303
304         string name;
305         lex >> name;
306         if (!lex || name != name_)
307                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
308
309         // This is part of the inset proper that is usually swallowed
310         // by Text::readInset
311         string id;
312         lex >> id;
313         if (!lex || id != "Wrap")
314                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
315
316         // We have to read the type here!
317         lex >> params.type;
318         params.read(lex);
319 }
320
321
322 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
323 {
324         ostringstream data;
325         data << name_ << ' ';
326         params.write(data);
327         return data.str();
328 }
329
330
331 } // namespace lyx