]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[lyx.git] / src / insets / insetwrap.C
1 /**
2  * \file insetwrap.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetwrap.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "LaTeXFeatures.h"
27 #include "LColor.h"
28 #include "lyxlex.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "pariterator.h"
32
33 #include "support/convert.h"
34
35 #include <sstream>
36
37 using std::string;
38 using std::endl;
39 using std::auto_ptr;
40 using std::istringstream;
41 using std::ostream;
42 using std::ostringstream;
43
44
45 namespace {
46
47 string floatname(string const & type, BufferParams const & bp)
48 {
49         FloatList const & floats = bp.getLyXTextClass().floats();
50         FloatList::const_iterator it = floats[type];
51         // FIXME UNICODE
52         return (it == floats.end()) ? type : lyx::to_utf8(_(it->second.name()));
53 }
54
55 } // namespace anon
56
57
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59         : InsetCollapsable(bp)
60 {
61         // FIXME UNICODE
62         setLabel(lyx::to_utf8(_("wrap: ")) + floatname(type, bp));
63         LyXFont font(LyXFont::ALL_SANE);
64         font.decSize();
65         font.decSize();
66         font.setColor(LColor::collapsable);
67         setLabelFont(font);
68         params_.type = type;
69         params_.width = LyXLength(50, LyXLength::PCW);
70         setInsetName(type);
71 }
72
73
74 InsetWrap::~InsetWrap()
75 {
76         InsetWrapMailer(*this).hideDialog();
77 }
78
79
80 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
81 {
82         switch (cmd.action) {
83         case LFUN_INSET_MODIFY: {
84                 InsetWrapParams params;
85                 InsetWrapMailer::string2params(lyx::to_utf8(cmd.argument()), params);
86                 params_.placement = params.placement;
87                 params_.width     = params.width;
88                 break;
89         }
90
91         case LFUN_INSET_DIALOG_UPDATE:
92                 InsetWrapMailer(*this).updateDialog(&cur.bv());
93                 break;
94
95         case LFUN_MOUSE_RELEASE: {
96                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
97                         InsetWrapMailer(*this).showDialog(&cur.bv());
98                         break;
99                 }
100                 InsetCollapsable::doDispatch(cur, cmd);
101                 break;
102         }
103
104         default:
105                 InsetCollapsable::doDispatch(cur, cmd);
106                 break;
107         }
108 }
109
110
111 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
112                 FuncStatus & flag) const
113 {
114         switch (cmd.action) {
115         case LFUN_INSET_MODIFY:
116         case LFUN_INSET_DIALOG_UPDATE:
117                 flag.enabled(true);
118                 return true;
119
120         default:
121                 return InsetCollapsable::getStatus(cur, cmd, flag);
122         }
123 }
124
125
126 void InsetWrapParams::write(ostream & os) const
127 {
128         os << "Wrap " << type << '\n';
129
130         if (!placement.empty())
131                 os << "placement " << placement << "\n";
132
133         os << "width \"" << width.asString() << "\"\n";
134 }
135
136
137 void InsetWrapParams::read(LyXLex & lex)
138 {
139         string token;
140         lex >> token;
141         if (token == "placement")
142                 lex >> placement;
143         else {
144                 // take countermeasures
145                 lex.pushToken(token);
146         }
147         if (!lex)
148                 return;
149         lex >> token;
150         if (token == "width") {
151                 lex.next();
152                 width = LyXLength(lex.getString());
153         } else {
154                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
155                         << endl;
156                 // take countermeasures
157                 lex.pushToken(token);
158         }
159 }
160
161
162 void InsetWrap::write(Buffer const & buf, ostream & os) const
163 {
164         params_.write(os);
165         InsetCollapsable::write(buf, os);
166 }
167
168
169 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
170 {
171         params_.read(lex);
172         InsetCollapsable::read(buf, lex);
173 }
174
175
176 void InsetWrap::validate(LaTeXFeatures & features) const
177 {
178         features.require("floatflt");
179         InsetCollapsable::validate(features);
180 }
181
182
183 auto_ptr<InsetBase> InsetWrap::doClone() const
184 {
185         return auto_ptr<InsetBase>(new InsetWrap(*this));
186 }
187
188
189 string const InsetWrap::editMessage() const
190 {
191         // FIXME UNICODE
192         return lyx::to_utf8(_("Opened Wrap Inset"));
193 }
194
195
196 int InsetWrap::latex(Buffer const & buf, ostream & os,
197                      OutputParams const & runparams) const
198 {
199         os << "\\begin{floating" << params_.type << '}';
200         if (!params_.placement.empty())
201                 os << '[' << params_.placement << ']';
202         os << '{' << params_.width.asLatexString() << "}%\n";
203         int const i = InsetText::latex(buf, os, runparams);
204         os << "\\end{floating" << params_.type << "}%\n";
205         return i + 2;
206 }
207
208
209 int InsetWrap::docbook(Buffer const & buf, ostream & os,
210                        OutputParams const & runparams) const
211 {
212         os << '<' << params_.type << '>';
213         int const i = InsetText::docbook(buf, os, runparams);
214         os << "</" << params_.type << '>';
215         return i;
216 }
217
218
219 bool InsetWrap::insetAllowed(InsetBase::Code code) const
220 {
221         switch(code) {
222         case FLOAT_CODE:
223         case FOOT_CODE:
224         case MARGIN_CODE:
225                 return false;
226         default:
227                 return InsetCollapsable::insetAllowed(code);
228         }
229 }
230
231
232 bool InsetWrap::showInsetDialog(BufferView * bv) const
233 {
234         if (!InsetText::showInsetDialog(bv))
235                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
236         return true;
237 }
238
239
240 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
241 {
242         ParConstIterator pit = par_const_iterator_begin(*this);
243         ParConstIterator end = par_const_iterator_end(*this);
244
245         // Find a caption layout in one of the (child inset's) pars
246         for (; pit != end; ++pit) {
247                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
248                         string const type = params_.type;
249                         string const str =
250                                 convert<string>(toclist[type].size() + 1)
251                                 + ". " + pit->asString(buf, false);
252                         lyx::toc::TocItem const item(pit, 0 , str);
253                         toclist[type].push_back(item);
254                 }
255         }
256 }
257
258
259 string const InsetWrapMailer::name_("wrap");
260
261 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
262         : inset_(inset)
263 {}
264
265
266 string const InsetWrapMailer::inset2string(Buffer const &) const
267 {
268         return params2string(inset_.params());
269 }
270
271
272 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
273 {
274         params = InsetWrapParams();
275         if (in.empty())
276                 return;
277
278         istringstream data(in);
279         LyXLex lex(0,0);
280         lex.setStream(data);
281
282         string name;
283         lex >> name;
284         if (!lex || name != name_)
285                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
286
287         // This is part of the inset proper that is usually swallowed
288         // by LyXText::readInset
289         string id;
290         lex >> id;
291         if (!lex || id != "Wrap")
292                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
293
294         // We have to read the type here!
295         lex >> params.type;
296         params.read(lex);
297 }
298
299
300 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
301 {
302         ostringstream data;
303         data << name_ << ' ';
304         params.write(data);
305         return data.str();
306 }