]> git.lyx.org Git - features.git/blob - src/insets/InsetWrap.cpp
try to pass a Buffer & to inset construction if some buffer(param)
[features.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(Buffer const & buf, string const & type)
43         : InsetCollapsable(buf), name_(from_utf8(type))
44 {
45         setLabel(_("wrap: ") + floatName(type, buf.params()));
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         setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
111         Counters & cnts = buffer().params().documentClass().counters();
112         string const saveflt = cnts.current_float();
113
114         // Tell to captions what the current float is
115         cnts.current_float(params().type);
116
117         InsetCollapsable::updateLabels(it);
118
119         // reset afterwards
120         cnts.current_float(saveflt);
121 }
122
123
124 void InsetWrapParams::write(ostream & os) const
125 {
126         os << "Wrap " << type << '\n';
127         os << "lines " << lines << "\n";
128         os << "placement " << placement << "\n";
129         os << "overhang " << overhang.asString() << "\n";
130         os << "width \"" << width.asString() << "\"\n";
131 }
132
133
134 void InsetWrapParams::read(Lexer & lex)
135 {
136         string token;
137
138         lex >> token;
139         if (token == "lines")
140                 lex >> lines;
141         else {
142                 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
143                         << endl;
144                 // take countermeasures
145                 lex.pushToken(token);
146         }
147         if (!lex)
148                 return;
149         lex >> token;
150         if (token == "placement")
151                 lex >> placement;
152         else {
153                 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
154                         << endl;
155                 lex.pushToken(token);
156         }
157         if (!lex)
158                 return;
159         lex >> token;
160         if (token == "overhang") {
161                 lex.next();
162                 overhang = Length(lex.getString());
163         } else {
164                 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
165                         << endl;
166                 lex.pushToken(token);
167         }
168         if (!lex)
169                 return;
170         lex >> token;
171         if (token == "width") {
172                 lex.next();
173                 width = Length(lex.getString());
174         } else {
175                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
176                         << endl;
177                 lex.pushToken(token);
178         }
179 }
180
181
182 void InsetWrap::write(ostream & os) const
183 {
184         params_.write(os);
185         InsetCollapsable::write(os);
186 }
187
188
189 void InsetWrap::read(Lexer & lex)
190 {
191         params_.read(lex);
192         InsetCollapsable::read(lex);
193 }
194
195
196 void InsetWrap::validate(LaTeXFeatures & features) const
197 {
198         features.require("wrapfig");
199         InsetCollapsable::validate(features);
200 }
201
202
203 Inset * InsetWrap::clone() const
204 {
205         return new InsetWrap(*this);
206 }
207
208
209 docstring InsetWrap::editMessage() const
210 {
211         return _("Opened Wrap Inset");
212 }
213
214
215 int InsetWrap::latex(odocstream & os, 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(os, runparams);
228         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
229         return i + 2;
230 }
231
232
233 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
234 {
235         os << '[' << buffer().B_("wrap") << ' '
236                 << floatName(params_.type, buffer().params()) << ":\n";
237         InsetText::plaintext(os, runparams);
238         os << "\n]";
239
240         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
241 }
242
243
244 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
245 {
246         // FIXME UNICODE
247         os << '<' << from_ascii(params_.type) << '>';
248         int const i = InsetText::docbook(os, runparams);
249         os << "</" << from_ascii(params_.type) << '>';
250         return i;
251 }
252
253
254 bool InsetWrap::insetAllowed(InsetCode code) const
255 {
256         switch(code) {
257         case FLOAT_CODE:
258         case FOOT_CODE:
259         case MARGIN_CODE:
260                 return false;
261         default:
262                 return InsetCollapsable::insetAllowed(code);
263         }
264 }
265
266
267 bool InsetWrap::showInsetDialog(BufferView * bv) const
268 {
269         if (!InsetText::showInsetDialog(bv))
270                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
271         return true;
272 }
273
274
275 string const InsetWrapMailer::name_("wrap");
276
277 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
278         : inset_(inset)
279 {}
280
281
282 string const InsetWrapMailer::inset2string(Buffer const &) const
283 {
284         return params2string(inset_.params());
285 }
286
287
288 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
289 {
290         params = InsetWrapParams();
291         if (in.empty())
292                 return;
293
294         istringstream data(in);
295         Lexer lex(0,0);
296         lex.setStream(data);
297
298         string name;
299         lex >> name;
300         if (!lex || name != name_)
301                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
302
303         // This is part of the inset proper that is usually swallowed
304         // by Text::readInset
305         string id;
306         lex >> id;
307         if (!lex || id != "Wrap")
308                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
309
310         // We have to read the type here!
311         lex >> params.type;
312         params.read(lex);
313 }
314
315
316 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
317 {
318         ostringstream data;
319         data << name_ << ' ';
320         params.write(data);
321         return data.str();
322 }
323
324
325 } // namespace lyx