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