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