]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
* src/insets/InsetWrap.{cpp,h}:
[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(ParIterator const & it)
109 {
110         Counters & cnts = buffer().params().documentClass().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(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(ostream & os) const
182 {
183         params_.write(os);
184         InsetCollapsable::write(os);
185 }
186
187
188 void InsetWrap::read(Lexer & lex)
189 {
190         params_.read(lex);
191         InsetCollapsable::read(lex);
192 }
193
194
195 void InsetWrap::draw(PainterInfo & pi, int x, int y) const
196 {
197         const_cast<InsetWrap &>(*this).setLabel(
198                 _("wrap: ") + floatName(params_.type, buffer().params()));
199         InsetCollapsable::draw(pi, x, y);
200 }
201
202
203 void InsetWrap::validate(LaTeXFeatures & features) const
204 {
205         features.require("wrapfig");
206         InsetCollapsable::validate(features);
207 }
208
209
210 Inset * InsetWrap::clone() const
211 {
212         return new InsetWrap(*this);
213 }
214
215
216 docstring InsetWrap::editMessage() const
217 {
218         return _("Opened Wrap Inset");
219 }
220
221
222 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
223 {
224         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
225         // no optional argument when lines are zero
226         if (params_.lines != 0)
227                 os << '[' << params_.lines << ']';
228         os << '{' << from_ascii(params_.placement) << '}';
229         Length over(params_.overhang);
230         // no optional argument when the value is zero
231         if (over.value() != 0)
232                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
233         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
234         int const i = InsetText::latex(os, runparams);
235         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
236         return i + 2;
237 }
238
239
240 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
241 {
242         os << '[' << buffer().B_("wrap") << ' '
243                 << floatName(params_.type, buffer().params()) << ":\n";
244         InsetText::plaintext(os, runparams);
245         os << "\n]";
246
247         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
248 }
249
250
251 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
252 {
253         // FIXME UNICODE
254         os << '<' << from_ascii(params_.type) << '>';
255         int const i = InsetText::docbook(os, runparams);
256         os << "</" << from_ascii(params_.type) << '>';
257         return i;
258 }
259
260
261 bool InsetWrap::insetAllowed(InsetCode code) const
262 {
263         switch(code) {
264         case FLOAT_CODE:
265         case FOOT_CODE:
266         case MARGIN_CODE:
267                 return false;
268         default:
269                 return InsetCollapsable::insetAllowed(code);
270         }
271 }
272
273
274 bool InsetWrap::showInsetDialog(BufferView * bv) const
275 {
276         if (!InsetText::showInsetDialog(bv))
277                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
278         return true;
279 }
280
281
282 string const InsetWrapMailer::name_("wrap");
283
284 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
285         : inset_(inset)
286 {}
287
288
289 string const InsetWrapMailer::inset2string(Buffer const &) const
290 {
291         return params2string(inset_.params());
292 }
293
294
295 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
296 {
297         params = InsetWrapParams();
298         if (in.empty())
299                 return;
300
301         istringstream data(in);
302         Lexer lex(0,0);
303         lex.setStream(data);
304
305         string name;
306         lex >> name;
307         if (!lex || name != name_)
308                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
309
310         // This is part of the inset proper that is usually swallowed
311         // by Text::readInset
312         string id;
313         lex >> id;
314         if (!lex || id != "Wrap")
315                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
316
317         // We have to read the type here!
318         lex >> params.type;
319         params.read(lex);
320 }
321
322
323 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
324 {
325         ostringstream data;
326         data << name_ << ' ';
327         params.write(data);
328         return data.str();
329 }
330
331
332 } // namespace lyx