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