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