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