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