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