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