]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
bdfd1ff50dbe39e7d70f1b57cc4090801d9a723a
[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::auto_ptr;
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         Font font(Font::ALL_SANE);
51         font.decSize();
52         font.decSize();
53         font.setColor(Color::collapsable);
54         setLabelFont(font);
55         params_.type = type;
56         params_.width = Length(50, Length::PCW);
57 }
58
59
60 InsetWrap::~InsetWrap()
61 {
62         InsetWrapMailer(*this).hideDialog();
63 }
64
65
66 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
67 {
68         switch (cmd.action) {
69         case LFUN_INSET_MODIFY: {
70                 InsetWrapParams params;
71                 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
72                 params_.placement = params.placement;
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
131         if (!placement.empty())
132                 os << "placement " << placement << "\n";
133
134         os << "width \"" << width.asString() << "\"\n";
135 }
136
137
138 void InsetWrapParams::read(Lexer & lex)
139 {
140         string token;
141         lex >> token;
142         if (token == "placement")
143                 lex >> placement;
144         else {
145                 // take countermeasures
146                 lex.pushToken(token);
147         }
148         if (!lex)
149                 return;
150         lex >> token;
151         if (token == "width") {
152                 lex.next();
153                 width = Length(lex.getString());
154         } else {
155                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
156                         << endl;
157                 // take countermeasures
158                 lex.pushToken(token);
159         }
160 }
161
162
163 void InsetWrap::write(Buffer const & buf, ostream & os) const
164 {
165         params_.write(os);
166         InsetCollapsable::write(buf, os);
167 }
168
169
170 void InsetWrap::read(Buffer const & buf, Lexer & lex)
171 {
172         params_.read(lex);
173         InsetCollapsable::read(buf, lex);
174 }
175
176
177 void InsetWrap::validate(LaTeXFeatures & features) const
178 {
179         features.require("floatflt");
180         InsetCollapsable::validate(features);
181 }
182
183
184 auto_ptr<Inset> InsetWrap::doClone() const
185 {
186         return auto_ptr<Inset>(new InsetWrap(*this));
187 }
188
189
190 docstring const InsetWrap::editMessage() const
191 {
192         return _("Opened Wrap Inset");
193 }
194
195
196 int InsetWrap::latex(Buffer const & buf, odocstream & os,
197                      OutputParams const & runparams) const
198 {
199         os << "\\begin{floating" << from_ascii(params_.type) << '}';
200         if (!params_.placement.empty())
201                 os << '[' << from_ascii(params_.placement) << ']';
202         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
203         int const i = InsetText::latex(buf, os, runparams);
204         os << "\\end{floating" << 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