]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[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("wrapfig");
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{wrap" << from_ascii(params_.type) << '}';
199         if (!params_.placement.empty())
200                 os << '{' << from_ascii(params_.placement) << '}';
201                 else os << "{o}"; //Outer is default in the current UI
202         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
203         int const i = InsetText::latex(buf, os, runparams);
204         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
205         return i + 2;
206 }
207
208
209 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
210                          OutputParams const & runparams) const
211 {
212         os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
213         InsetText::plaintext(buf, os, runparams);
214         os << "\n]";
215
216         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
217 }
218
219
220 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
221                        OutputParams const & runparams) const
222 {
223         // FIXME UNICODE
224         os << '<' << from_ascii(params_.type) << '>';
225         int const i = InsetText::docbook(buf, os, runparams);
226         os << "</" << from_ascii(params_.type) << '>';
227         return i;
228 }
229
230
231 bool InsetWrap::insetAllowed(Inset::Code code) const
232 {
233         switch(code) {
234         case FLOAT_CODE:
235         case FOOT_CODE:
236         case MARGIN_CODE:
237                 return false;
238         default:
239                 return InsetCollapsable::insetAllowed(code);
240         }
241 }
242
243
244 bool InsetWrap::showInsetDialog(BufferView * bv) const
245 {
246         if (!InsetText::showInsetDialog(bv))
247                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
248         return true;
249 }
250
251
252 string const InsetWrapMailer::name_("wrap");
253
254 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
255         : inset_(inset)
256 {}
257
258
259 string const InsetWrapMailer::inset2string(Buffer const &) const
260 {
261         return params2string(inset_.params());
262 }
263
264
265 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
266 {
267         params = InsetWrapParams();
268         if (in.empty())
269                 return;
270
271         istringstream data(in);
272         Lexer lex(0,0);
273         lex.setStream(data);
274
275         string name;
276         lex >> name;
277         if (!lex || name != name_)
278                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
279
280         // This is part of the inset proper that is usually swallowed
281         // by Text::readInset
282         string id;
283         lex >> id;
284         if (!lex || id != "Wrap")
285                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
286
287         // We have to read the type here!
288         lex >> params.type;
289         params.read(lex);
290 }
291
292
293 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
294 {
295         ostringstream data;
296         data << name_ << ' ';
297         params.write(data);
298         return data.str();
299 }
300
301
302 } // namespace lyx