]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[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 docstring InsetWrap::editMessage() const
177 {
178         return _("Opened Wrap Inset");
179 }
180
181
182 int InsetWrap::latex(odocstream & os, OutputParams const & runparams_in) const
183 {
184         OutputParams runparams(runparams_in);
185         runparams.inFloat = OutputParams::MAINFLOAT;
186         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
187         // no optional argument when lines are zero
188         if (params_.lines != 0)
189                 os << '[' << params_.lines << ']';
190         os << '{' << from_ascii(params_.placement) << '}';
191         Length over(params_.overhang);
192         // no optional argument when the value is zero
193         if (over.value() != 0)
194                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
195         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
196         int const i = InsetText::latex(os, runparams);
197         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
198         return i + 2;
199 }
200
201
202 int InsetWrap::plaintext(odocstream & os, OutputParams const & runparams) const
203 {
204         os << '[' << buffer().B_("wrap") << ' '
205                 << floatName(params_.type) << ":\n";
206         InsetText::plaintext(os, runparams);
207         os << "\n]";
208
209         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
210 }
211
212
213 int InsetWrap::docbook(odocstream & os, OutputParams const & runparams) const
214 {
215         // FIXME UNICODE
216         os << '<' << from_ascii(params_.type) << '>';
217         int const i = InsetText::docbook(os, runparams);
218         os << "</" << from_ascii(params_.type) << '>';
219         return i;
220 }
221
222
223 docstring InsetWrap::xhtml(odocstream &, OutputParams const & rp) const
224 {
225         string const len = params_.width.asHTMLString();
226         docstring retval = from_ascii("<div class='wrap'");
227         if (!len.empty())
228                 retval += from_ascii(" style='width: " + len + ";");
229         retval += from_ascii("'>");
230         odocstringstream os;
231         docstring const deferred = InsetText::xhtml(os, rp);
232         retval += os.str() + from_ascii("</div>");
233         retval += deferred;
234         return retval;
235 }
236
237
238 bool InsetWrap::insetAllowed(InsetCode code) const
239 {
240         switch(code) {
241         case WRAP_CODE:
242         case FOOT_CODE:
243         case MARGIN_CODE:
244                 return false;
245         default:
246                 return InsetCollapsable::insetAllowed(code);
247         }
248 }
249
250
251 bool InsetWrap::showInsetDialog(BufferView * bv) const
252 {
253         if (!InsetText::showInsetDialog(bv))
254                 bv->showDialog("wrap", params2string(params()),
255                         const_cast<InsetWrap *>(this));
256         return true;
257 }
258
259
260 void InsetWrap::string2params(string const & in, InsetWrapParams & params)
261 {
262         params = InsetWrapParams();
263         istringstream data(in);
264         Lexer lex;
265         lex.setStream(data);
266         lex.setContext("InsetWrap::string2params");
267         lex >> "wrap";
268         lex >> "Wrap";  // Part of the inset proper, swallowed by Text::readInset
269         lex >> params.type; // We have to read the type here!
270         params.read(lex);
271 }
272
273
274 string InsetWrap::params2string(InsetWrapParams const & params)
275 {
276         ostringstream data;
277         data << "wrap" << ' ';
278         params.write(data);
279         return data.str();
280 }
281
282
283 } // namespace lyx