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