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