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