]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
fix #4717
[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
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
30
31 #include "support/convert.h"
32 #include "support/docstream.h"
33 #include "support/debug.h"
34 #include "support/gettext.h"
35
36 #include "frontends/Application.h"
37
38 using namespace std;
39
40
41 namespace lyx {
42
43 InsetWrap::InsetWrap(Buffer const & buf, string const & type)
44         : InsetCollapsable(buf)
45 {
46         setLabel(_("wrap: ") + floatName(type, buf.params()));
47         params_.type = type;
48         params_.lines = 0;
49         params_.placement = "o";
50         params_.overhang = Length(0, Length::PCW);
51         params_.width = Length(50, Length::PCW);
52 }
53
54
55 InsetWrap::~InsetWrap()
56 {
57         hideDialogs("wrap", this);
58 }
59
60
61 docstring InsetWrap::name() const
62 {
63         return from_utf8(params_.type);
64 }
65
66
67 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
68 {
69         switch (cmd.action) {
70         case LFUN_INSET_MODIFY: {
71                 InsetWrapParams params;
72                 InsetWrap::string2params(to_utf8(cmd.argument()), params);
73                 params_.lines = params.lines;
74                 params_.placement = params.placement;
75                 params_.overhang = params.overhang;
76                 params_.width = params.width;
77                 break;
78         }
79
80         case LFUN_INSET_DIALOG_UPDATE:
81                 cur.bv().updateDialog("wrap", params2string(params()));
82                 break;
83
84         default:
85                 InsetCollapsable::doDispatch(cur, cmd);
86                 break;
87         }
88 }
89
90
91 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
92                 FuncStatus & flag) const
93 {
94         switch (cmd.action) {
95         case LFUN_INSET_MODIFY:
96         case LFUN_INSET_DIALOG_UPDATE:
97                 flag.enabled(true);
98                 return true;
99
100         default:
101                 return InsetCollapsable::getStatus(cur, cmd, flag);
102         }
103 }
104
105
106 void InsetWrap::updateLabels(ParIterator const & it)
107 {
108         setLabel(_("wrap: ") + floatName(params_.type, buffer().params()));
109         Counters & cnts = buffer().params().documentClass().counters();
110         string const saveflt = cnts.current_float();
111
112         // Tell to captions what the current float is
113         cnts.current_float(params().type);
114
115         InsetCollapsable::updateLabels(it);
116
117         // reset afterwards
118         cnts.current_float(saveflt);
119 }
120
121
122 void InsetWrapParams::write(ostream & os) const
123 {
124         os << "Wrap " << type << '\n';
125         os << "lines " << lines << '\n';
126         os << "placement " << placement << '\n';
127         os << "overhang " << overhang.asString() << '\n';
128         os << "width \"" << width.asString() << "\"\n";
129 }
130
131
132 void InsetWrapParams::read(Lexer & lex)
133 {
134         lex.setContext("InsetWrapParams::read");
135         lex >> "lines" >> lines;
136         lex >> "placement" >> placement;
137         lex >> "overhang" >> overhang;
138         lex >> "width" >> width;
139 }
140
141
142 void InsetWrap::write(ostream & os) const
143 {
144         params_.write(os);
145         InsetCollapsable::write(os);
146 }
147
148
149 void InsetWrap::read(Lexer & lex)
150 {
151         params_.read(lex);
152         InsetCollapsable::read(lex);
153 }
154
155
156 void InsetWrap::validate(LaTeXFeatures & features) const
157 {
158         features.require("wrapfig");
159         InsetCollapsable::validate(features);
160 }
161
162
163 docstring InsetWrap::editMessage() const
164 {
165         return _("Opened Wrap Inset");
166 }
167
168
169 int InsetWrap::latex(odocstream & os, OutputParams const & runparams) const
170 {
171         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
172         // no optional argument when lines are zero
173         if (params_.lines != 0)
174                 os << '[' << params_.lines << ']';
175         os << '{' << from_ascii(params_.placement) << '}';
176         Length over(params_.overhang);
177         // no optional argument when the value is zero
178         if (over.value() != 0)
179                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
180         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
181         int const i = InsetText::latex(os, runparams);
182         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
183         return i + 2;
184 }
185
186
187 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
188 {
189         os << '[' << buffer().B_("wrap") << ' '
190                 << floatName(params_.type, buffer().params()) << ":\n";
191         InsetText::plaintext(os, runparams);
192         os << "\n]";
193
194         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
195 }
196
197
198 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
199 {
200         // FIXME UNICODE
201         os << '<' << from_ascii(params_.type) << '>';
202         int const i = InsetText::docbook(os, runparams);
203         os << "</" << from_ascii(params_.type) << '>';
204         return i;
205 }
206
207
208 bool InsetWrap::insetAllowed(InsetCode code) const
209 {
210         switch(code) {
211         case FLOAT_CODE:
212         case FOOT_CODE:
213         case MARGIN_CODE:
214                 return false;
215         default:
216                 return InsetCollapsable::insetAllowed(code);
217         }
218 }
219
220
221 bool InsetWrap::showInsetDialog(BufferView * bv) const
222 {
223         if (!InsetText::showInsetDialog(bv))
224                 bv->showDialog("wrap", params2string(params()),
225                         const_cast<InsetWrap *>(this));
226         return true;
227 }
228
229
230 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
231 {
232         params = InsetWrapParams();
233         istringstream data(in);
234         Lexer lex;
235         lex.setStream(data);
236         lex.setContext("InsetWrap::string2params");
237         lex >> "wrap";
238         lex >> "Wrap";  // Part of the inset proper, swallowed by Text::readInset
239         lex >> params.type; // We have to read the type here!
240         params.read(lex);
241 }
242
243
244 string InsetWrap::params2string(InsetWrapParams const & params)
245 {
246         ostringstream data;
247         data << "wrap" << ' ';
248         params.write(data);
249         return data.str();
250 }
251
252
253 } // namespace lyx