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