]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
Cleanup mouse/selection/context-menu interactions.
[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(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         default:
78                 InsetCollapsable::doDispatch(cur, cmd);
79                 break;
80         }
81 }
82
83
84 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
85                 FuncStatus & flag) const
86 {
87         switch (cmd.action) {
88         case LFUN_INSET_MODIFY:
89         case LFUN_INSET_DIALOG_UPDATE:
90                 flag.enabled(true);
91                 return true;
92
93         default:
94                 return InsetCollapsable::getStatus(cur, cmd, flag);
95         }
96 }
97
98
99 void InsetWrap::updateLabels(ParIterator const & it)
100 {
101         setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
102         Counters & cnts = buffer().params().documentClass().counters();
103         string const saveflt = cnts.current_float();
104
105         // Tell to captions what the current float is
106         cnts.current_float(params().type);
107
108         InsetCollapsable::updateLabels(it);
109
110         // reset afterwards
111         cnts.current_float(saveflt);
112 }
113
114
115 void InsetWrapParams::write(ostream & os) const
116 {
117         os << "Wrap " << type << '\n';
118         os << "lines " << lines << "\n";
119         os << "placement " << placement << "\n";
120         os << "overhang " << overhang.asString() << "\n";
121         os << "width \"" << width.asString() << "\"\n";
122 }
123
124
125 void InsetWrapParams::read(Lexer & lex)
126 {
127         string token;
128
129         lex >> token;
130         if (token == "lines")
131                 lex >> lines;
132         else {
133                 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
134                         << endl;
135                 // take countermeasures
136                 lex.pushToken(token);
137         }
138         if (!lex)
139                 return;
140         lex >> token;
141         if (token == "placement")
142                 lex >> placement;
143         else {
144                 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
145                         << endl;
146                 lex.pushToken(token);
147         }
148         if (!lex)
149                 return;
150         lex >> token;
151         if (token == "overhang") {
152                 lex.next();
153                 overhang = Length(lex.getString());
154         } else {
155                 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
156                         << endl;
157                 lex.pushToken(token);
158         }
159         if (!lex)
160                 return;
161         lex >> token;
162         if (token == "width") {
163                 lex.next();
164                 width = Length(lex.getString());
165         } else {
166                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
167                         << endl;
168                 lex.pushToken(token);
169         }
170 }
171
172
173 void InsetWrap::write(ostream & os) const
174 {
175         params_.write(os);
176         InsetCollapsable::write(os);
177 }
178
179
180 void InsetWrap::read(Lexer & lex)
181 {
182         params_.read(lex);
183         InsetCollapsable::read(lex);
184 }
185
186
187 void InsetWrap::validate(LaTeXFeatures & features) const
188 {
189         features.require("wrapfig");
190         InsetCollapsable::validate(features);
191 }
192
193
194 docstring InsetWrap::editMessage() const
195 {
196         return _("Opened Wrap Inset");
197 }
198
199
200 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
201 {
202         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
203         // no optional argument when lines are zero
204         if (params_.lines != 0)
205                 os << '[' << params_.lines << ']';
206         os << '{' << from_ascii(params_.placement) << '}';
207         Length over(params_.overhang);
208         // no optional argument when the value is zero
209         if (over.value() != 0)
210                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
211         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
212         int const i = InsetText::latex(os, runparams);
213         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
214         return i + 2;
215 }
216
217
218 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
219 {
220         os << '[' << buffer().B_("wrap") << ' '
221                 << floatName(params_.type, buffer().params()) << ":\n";
222         InsetText::plaintext(os, runparams);
223         os << "\n]";
224
225         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
226 }
227
228
229 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
230 {
231         // FIXME UNICODE
232         os << '<' << from_ascii(params_.type) << '>';
233         int const i = InsetText::docbook(os, runparams);
234         os << "</" << from_ascii(params_.type) << '>';
235         return i;
236 }
237
238
239 bool InsetWrap::insetAllowed(InsetCode code) const
240 {
241         switch(code) {
242         case FLOAT_CODE:
243         case FOOT_CODE:
244         case MARGIN_CODE:
245                 return false;
246         default:
247                 return InsetCollapsable::insetAllowed(code);
248         }
249 }
250
251
252 bool InsetWrap::showInsetDialog(BufferView * bv) const
253 {
254         if (!InsetText::showInsetDialog(bv))
255                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
256         return true;
257 }
258
259
260 string const InsetWrapMailer::name_("wrap");
261
262 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
263         : inset_(inset)
264 {}
265
266
267 string const InsetWrapMailer::inset2string(Buffer const &) const
268 {
269         return params2string(inset_.params());
270 }
271
272
273 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
274 {
275         params = InsetWrapParams();
276         if (in.empty())
277                 return;
278
279         istringstream data(in);
280         Lexer lex(0,0);
281         lex.setStream(data);
282
283         string name;
284         lex >> name;
285         if (!lex || name != name_)
286                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
287
288         // This is part of the inset proper that is usually swallowed
289         // by Text::readInset
290         string id;
291         lex >> id;
292         if (!lex || id != "Wrap")
293                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
294
295         // We have to read the type here!
296         lex >> params.type;
297         params.read(lex);
298 }
299
300
301 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
302 {
303         ostringstream data;
304         data << name_ << ' ';
305         params.write(data);
306         return data.str();
307 }
308
309
310 } // namespace lyx