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