]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
Fix crash after undo following replacement of a multicell selection (#8973).
[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 #include "InsetCaption.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Counters.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "output_xhtml.h"
30 #include "TextClass.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35
36 #include "frontends/Application.h"
37
38 #include <climits>
39
40 using namespace std;
41
42
43 namespace lyx {
44
45 InsetWrap::InsetWrap(Buffer * buf, string const & type)
46         : InsetCollapsable(buf)
47 {
48         setLabel(_("wrap: ") + floatName(type));
49         params_.type = type;
50         params_.lines = 0;
51         params_.placement = "o";
52         params_.overhang = Length(0, Length::PCW);
53         params_.width = Length(50, Length::PCW);
54 }
55
56
57 InsetWrap::~InsetWrap()
58 {
59         hideDialogs("wrap", this);
60 }
61
62
63 docstring InsetWrap::layoutName() const
64 {
65         return "Wrap:" + from_utf8(params_.type);
66 }
67
68
69 docstring InsetWrap::toolTip(BufferView const & bv, int x, int y) const
70 {
71         if (isOpen(bv))
72                 return InsetCollapsable::toolTip(bv, x, y);
73         OutputParams rp(&buffer().params().encoding());
74         docstring caption_tip = getCaptionText(rp);
75         if (!caption_tip.empty())
76                 caption_tip += from_ascii("\n");
77         return toolTipText(caption_tip);
78 }
79
80
81 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
82 {
83         switch (cmd.action()) {
84         case LFUN_INSET_MODIFY: {
85                 cur.recordUndoInset(ATOMIC_UNDO, this);
86                 InsetWrapParams params;
87                 InsetWrap::string2params(to_utf8(cmd.argument()), params);
88                 params_.lines = params.lines;
89                 params_.placement = params.placement;
90                 params_.overhang = params.overhang;
91                 params_.width = params.width;
92                 break;
93         }
94
95         case LFUN_INSET_DIALOG_UPDATE:
96                 cur.bv().updateDialog("wrap", params2string(params()));
97                 break;
98
99         default:
100                 InsetCollapsable::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
107                 FuncStatus & flag) const
108 {
109         switch (cmd.action()) {
110         case LFUN_INSET_MODIFY:
111         case LFUN_INSET_DIALOG_UPDATE:
112                 flag.setEnabled(true);
113                 return true;
114
115         default:
116                 return InsetCollapsable::getStatus(cur, cmd, flag);
117         }
118 }
119
120
121 void InsetWrap::updateBuffer(ParIterator const & it, UpdateType utype)
122 {
123         setLabel(_("wrap: ") + floatName(params_.type));
124         Counters & cnts =
125                 buffer().masterBuffer()->params().documentClass().counters();
126         if (utype == OutputUpdate) {
127                 // counters are local to the wrap
128                 cnts.saveLastCounter();
129         }
130         string const saveflt = cnts.current_float();
131
132         // Tell to captions what the current float is
133         cnts.current_float(params().type);
134
135         InsetCollapsable::updateBuffer(it, utype);
136
137         // reset afterwards
138         cnts.current_float(saveflt);
139         if (utype == OutputUpdate)
140                 cnts.restoreLastCounter();
141 }
142
143
144 void InsetWrapParams::write(ostream & os) const
145 {
146         os << "Wrap " << type << '\n';
147         os << "lines " << lines << '\n';
148         os << "placement " << placement << '\n';
149         os << "overhang " << overhang.asString() << '\n';
150         os << "width \"" << width.asString() << "\"\n";
151 }
152
153
154 void InsetWrapParams::read(Lexer & lex)
155 {
156         lex.setContext("InsetWrapParams::read");
157         lex >> "lines" >> lines;
158         lex >> "placement" >> placement;
159         lex >> "overhang" >> overhang;
160         lex >> "width" >> width;
161 }
162
163
164 void InsetWrap::write(ostream & os) const
165 {
166         params_.write(os);
167         InsetCollapsable::write(os);
168 }
169
170
171 void InsetWrap::read(Lexer & lex)
172 {
173         params_.read(lex);
174         InsetCollapsable::read(lex);
175 }
176
177
178 void InsetWrap::validate(LaTeXFeatures & features) const
179 {
180         features.require("wrapfig");
181         features.inFloat(true);
182         InsetCollapsable::validate(features);
183         features.inFloat(false);
184 }
185
186
187 void InsetWrap::latex(otexstream & os, OutputParams const & runparams_in) const
188 {
189         OutputParams runparams(runparams_in);
190         runparams.inFloat = OutputParams::MAINFLOAT;
191         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
192         // no optional argument when lines are zero
193         if (params_.lines != 0)
194                 os << '[' << params_.lines << ']';
195         os << '{' << from_ascii(params_.placement) << '}';
196         Length over(params_.overhang);
197         // no optional argument when the value is zero
198         if (over.value() != 0)
199                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
200         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
201         InsetText::latex(os, runparams);
202         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
203 }
204
205
206 int InsetWrap::plaintext(odocstringstream & os,
207         OutputParams const & runparams, size_t max_length) const
208 {
209         os << '[' << buffer().B_("wrap") << ' '
210                 << floatName(params_.type) << ":\n";
211         InsetText::plaintext(os, runparams, max_length);
212         os << "\n]";
213
214         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
215 }
216
217
218 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
219 {
220         // FIXME UNICODE
221         os << '<' << from_ascii(params_.type) << '>';
222         int const i = InsetText::docbook(os, runparams);
223         os << "</" << from_ascii(params_.type) << '>';
224         return i;
225 }
226
227
228 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
229 {
230         string const len = params_.width.asHTMLString();
231         string const width = len.empty() ? "50%" : len;
232         InsetLayout const & il = getLayout();
233         string const tag = il.htmltag();
234         string const attr = il.htmlattr() + " style='width:" + width + ";'";
235         xs << html::StartTag(tag, attr);
236         docstring const deferred = 
237                 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
238         xs << html::EndTag(tag);
239         return deferred;
240 }
241
242
243 bool InsetWrap::insetAllowed(InsetCode code) const
244 {
245         switch(code) {
246         case WRAP_CODE:
247         case FOOT_CODE:
248         case MARGIN_CODE:
249                 return false;
250         default:
251                 return InsetCollapsable::insetAllowed(code);
252         }
253 }
254
255
256 bool InsetWrap::showInsetDialog(BufferView * bv) const
257 {
258         if (!InsetText::showInsetDialog(bv))
259                 bv->showDialog("wrap", params2string(params()),
260                         const_cast<InsetWrap *>(this));
261         return true;
262 }
263
264
265 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
266 {
267         params = InsetWrapParams();
268         istringstream data(in);
269         Lexer lex;
270         lex.setStream(data);
271         lex.setContext("InsetWrap::string2params");
272         lex >> "wrap";
273         lex >> "Wrap";  // Part of the inset proper, swallowed by Text::readInset
274         lex >> params.type; // We have to read the type here!
275         params.read(lex);
276 }
277
278
279 string InsetWrap::params2string(InsetWrapParams const & params)
280 {
281         ostringstream data;
282         data << "wrap" << ' ';
283         params.write(data);
284         return data.str();
285 }
286
287
288 } // namespace lyx