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