]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
e92fdb76cd8eab8f1d8fa002334282ed9eb65a44
[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::updateLabels(ParIterator const & it, bool out)
118 {
119         setLabel(_("wrap: ") + floatName(params_.type));
120         Counters & cnts =
121                 buffer().masterBuffer()->params().documentClass().counters();
122         string const saveflt = cnts.current_float();
123
124         // Tell to captions what the current float is
125         cnts.current_float(params().type);
126
127         InsetCollapsable::updateLabels(it, out);
128
129         // reset afterwards
130         cnts.current_float(saveflt);
131 }
132
133
134 void InsetWrapParams::write(ostream & os) const
135 {
136         os << "Wrap " << type << '\n';
137         os << "lines " << lines << '\n';
138         os << "placement " << placement << '\n';
139         os << "overhang " << overhang.asString() << '\n';
140         os << "width \"" << width.asString() << "\"\n";
141 }
142
143
144 void InsetWrapParams::read(Lexer & lex)
145 {
146         lex.setContext("InsetWrapParams::read");
147         lex >> "lines" >> lines;
148         lex >> "placement" >> placement;
149         lex >> "overhang" >> overhang;
150         lex >> "width" >> width;
151 }
152
153
154 void InsetWrap::write(ostream & os) const
155 {
156         params_.write(os);
157         InsetCollapsable::write(os);
158 }
159
160
161 void InsetWrap::read(Lexer & lex)
162 {
163         params_.read(lex);
164         InsetCollapsable::read(lex);
165 }
166
167
168 void InsetWrap::validate(LaTeXFeatures & features) const
169 {
170         features.require("wrapfig");
171         features.inFloat(true);
172         InsetCollapsable::validate(features);
173         features.inFloat(false);
174 }
175
176
177 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
178 {
179         OutputParams runparams(runparams_in);
180         runparams.inFloat = OutputParams::MAINFLOAT;
181         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
182         // no optional argument when lines are zero
183         if (params_.lines != 0)
184                 os << '[' << params_.lines << ']';
185         os << '{' << from_ascii(params_.placement) << '}';
186         Length over(params_.overhang);
187         // no optional argument when the value is zero
188         if (over.value() != 0)
189                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
190         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
191         int const i = InsetText::latex(os, runparams);
192         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
193         return i + 2;
194 }
195
196
197 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
198 {
199         os << '[' << buffer().B_("wrap") << ' '
200                 << floatName(params_.type) << ":\n";
201         InsetText::plaintext(os, runparams);
202         os << "\n]";
203
204         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
205 }
206
207
208 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
209 {
210         // FIXME UNICODE
211         os << '<' << from_ascii(params_.type) << '>';
212         int const i = InsetText::docbook(os, runparams);
213         os << "</" << from_ascii(params_.type) << '>';
214         return i;
215 }
216
217
218 docstring InsetWrap::xhtml(XHTMLStream & xs, OutputParams const & rp) const
219 {
220         string const len = params_.width.asHTMLString();
221         string const width = len.empty() ? "50%" : len;
222         string const attr = "class='wrap' style='width: " + len + ";'";
223         xs << StartTag("div", attr);
224         docstring const deferred = 
225                 InsetText::insetAsXHTML(xs, rp, InsetText::WriteInnerTag);
226         if (!len.empty())
227                 xs << EndTag("div");
228         return deferred;
229 }
230
231
232 bool InsetWrap::insetAllowed(InsetCode code) const
233 {
234         switch(code) {
235         case WRAP_CODE:
236         case FOOT_CODE:
237         case MARGIN_CODE:
238                 return false;
239         default:
240                 return InsetCollapsable::insetAllowed(code);
241         }
242 }
243
244
245 bool InsetWrap::showInsetDialog(BufferView * bv) const
246 {
247         if (!InsetText::showInsetDialog(bv))
248                 bv->showDialog("wrap", params2string(params()),
249                         const_cast<InsetWrap *>(this));
250         return true;
251 }
252
253
254 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
255 {
256         params = InsetWrapParams();
257         istringstream data(in);
258         Lexer lex;
259         lex.setStream(data);
260         lex.setContext("InsetWrap::string2params");
261         lex >> "wrap";
262         lex >> "Wrap";  // Part of the inset proper, swallowed by Text::readInset
263         lex >> params.type; // We have to read the type here!
264         params.read(lex);
265 }
266
267
268 string InsetWrap::params2string(InsetWrapParams const & params)
269 {
270         ostringstream data;
271         data << "wrap" << ' ';
272         params.write(data);
273         return data.str();
274 }
275
276
277 } // namespace lyx