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