]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
clone() at long last !
[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 #include "insettext.h"
15
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "funcrequest.h"
20 #include "FloatList.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "Lsstream.h"
24 #include "lyxfont.h"
25 #include "lyxlex.h"
26 #include "lyxtext.h"
27 #include "Lsstream.h"
28
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
31
32 #include "support/LOstream.h"
33 #include "support/tostr.h"
34
35 using std::ostream;
36 using std::endl;
37
38
39 namespace {
40
41 // this should not be hardcoded, but be part of the definition
42 // of the float (JMarc)
43 string const caplayout("Caption");
44
45 string floatname(string const & type, BufferParams const & bp)
46 {
47         FloatList const & floats = bp.getLyXTextClass().floats();
48         FloatList::const_iterator it = floats[type];
49         if (it == floats.end())
50                 return type;
51
52         return _(it->second.name());
53 }
54
55 } // namespace anon
56
57
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59         : InsetCollapsable(bp)
60 {
61         string lab(_("wrap: "));
62         lab += floatname(type, bp);
63         setLabel(lab);
64         LyXFont font(LyXFont::ALL_SANE);
65         font.decSize();
66         font.decSize();
67         font.setColor(LColor::collapsable);
68         setLabelFont(font);
69         params_.type = type;
70         params_.width = LyXLength(50, LyXLength::PCW);
71         setInsetName(type);
72         LyXTextClass const & tclass = bp.getLyXTextClass();
73         if (tclass.hasLayout(caplayout))
74                 inset.paragraphs.begin()->layout(tclass[caplayout]);
75 }
76
77
78 InsetWrap::InsetWrap(InsetWrap const & in)
79         : InsetCollapsable(in), params_(in.params_)
80 {}
81
82
83 InsetWrap::~InsetWrap()
84 {
85         InsetWrapMailer mailer(*this);
86         mailer.hideDialog();
87 }
88
89
90 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
91 {
92         Inset::RESULT result = UNDISPATCHED;
93
94         switch (cmd.action) {
95         case LFUN_INSET_MODIFY: {
96                 InsetWrapParams params;
97                 InsetWrapMailer::string2params(cmd.argument, params);
98
99                 params_.placement = params.placement;
100                 params_.width     = params.width;
101
102                 cmd.view()->updateInset(this);
103                 result = DISPATCHED;
104         }
105         break;
106
107         case LFUN_INSET_DIALOG_UPDATE: {
108                 InsetWrapMailer mailer(*this);
109                 mailer.updateDialog(cmd.view());
110         }
111         break;
112
113         default:
114                 result = InsetCollapsable::localDispatch(cmd);
115         }
116
117         return result;
118 }
119
120
121 void InsetWrapParams::write(ostream & os) const
122 {
123         os << "Wrap " // getInsetName()
124            << type << '\n';
125
126         if (!placement.empty()) {
127                 os << "placement " << placement << "\n";
128         }
129         os << "width \"" << width.asString() << "\"\n";
130 }
131
132
133 void InsetWrapParams::read(LyXLex & lex)
134 {
135         if (lex.isOK()) {
136                 lex.next();
137                 string token = lex.getString();
138                 if (token == "placement") {
139                         lex.next();
140                         placement = lex.getString();
141                 } else {
142                         // take countermeasures
143                         lex.pushToken(token);
144                 }
145         }
146         if (lex.isOK()) {
147                 lex.next();
148                 string token = lex.getString();
149                 if (token == "width") {
150                         lex.next();
151                         width = LyXLength(lex.getString());
152                 } else {
153                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
154                                << endl;
155                         // take countermeasures
156                         lex.pushToken(token);
157                 }
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 Inset * InsetWrap::clone() const
184 {
185         return new InsetWrap(*this);
186 }
187
188
189 string const InsetWrap::editMessage() const
190 {
191         return _("Opened Wrap Inset");
192 }
193
194
195 int InsetWrap::latex(Buffer const * buf, ostream & os,
196                      LatexRunParams const & runparams) const
197 {
198         os << "\\begin{floating" << params_.type << '}';
199         if (!params_.placement.empty()) {
200                 os << '[' << params_.placement << ']';
201         }
202         os  << '{' << params_.width.asLatexString() << "}%\n";
203
204         int const i = inset.latex(buf, os, runparams);
205
206         os << "\\end{floating" << params_.type << "}%\n";
207         return i + 2;
208 }
209
210
211 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
212 {
213         os << '<' << params_.type << '>';
214         int const i = inset.docbook(buf, os, mixcont);
215         os << "</" << params_.type << '>';
216
217         return i;
218 }
219
220
221 bool InsetWrap::insetAllowed(Inset::Code code) const
222 {
223         switch(code) {
224         case FLOAT_CODE:
225         case FOOT_CODE:
226         case MARGIN_CODE:
227                 return false;
228         default:
229                 return InsetCollapsable::insetAllowed(code);
230         }
231 }
232
233
234 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
235         const
236 {
237         if (owner() &&
238             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
239                 return -1;
240         }
241         if (!params_.width.zero()) {
242                 int const ww1 = latexTextWidth(bv);
243                 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
244                 if (ww2 > 0 && ww2 < ww1) {
245                         return ww2;
246                 }
247                 return ww1;
248         }
249         // this should not happen!
250         return InsetCollapsable::getMaxWidth(bv, inset);
251 }
252
253
254 int InsetWrap::latexTextWidth(BufferView * bv) const
255 {
256         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
257 }
258
259
260 bool InsetWrap::showInsetDialog(BufferView * bv) const
261 {
262         if (!inset.showInsetDialog(bv)) {
263                 InsetWrap * tmp = const_cast<InsetWrap *>(this);
264                 InsetWrapMailer mailer(*tmp);
265                 mailer.showDialog(bv);
266         }
267         return true;
268 }
269
270
271 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
272 {
273         // Now find the caption in the float...
274         ParagraphList::iterator tmp = inset.paragraphs.begin();
275         ParagraphList::iterator end = inset.paragraphs.end();
276
277         for (; tmp != end; ++tmp) {
278                 if (tmp->layout()->name() == caplayout) {
279                         string const name = floatname(params_.type, buf->params);
280                         string const str =
281                                 tostr(toclist[name].size() + 1)
282                                 + ". " + tmp->asString(buf, false);
283                         toc::TocItem const item(tmp->id(), 0 , str);
284                         toclist[name].push_back(item);
285                 }
286         }
287 }
288
289
290 string const InsetWrapMailer::name_("wrap");
291
292 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
293         : inset_(inset)
294 {}
295
296
297 string const InsetWrapMailer::inset2string() const
298 {
299         return params2string(inset_.params());
300 }
301
302
303 void InsetWrapMailer::string2params(string const & in,
304                                     InsetWrapParams & params)
305 {
306         params = InsetWrapParams();
307
308         if (in.empty())
309                 return;
310
311         istringstream data(STRCONV(in));
312         LyXLex lex(0,0);
313         lex.setStream(data);
314
315         if (lex.isOK()) {
316                 lex.next();
317                 string const token = lex.getString();
318                 if (token != name_)
319                         return;
320         }
321
322         // This is part of the inset proper that is usually swallowed
323         // by Buffer::readInset
324         if (lex.isOK()) {
325                 lex.next();
326                 string const token = lex.getString();
327                 if (token != "Wrap" || !lex.eatLine())
328                         return;
329         }
330
331         if (lex.isOK()) {
332                 params.read(lex);
333         }
334 }
335
336
337 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
338 {
339         ostringstream data;
340         data << name_ << ' ';
341         params.write(data);
342         return STRCONV(data.str());
343 }