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