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