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