]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetfloat.C
1 /**
2  * \file insetfloat.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "insetfloat.h"
15 #include "insettext.h"
16
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "debug.h"
20 #include "Floating.h"
21 #include "FloatList.h"
22 #include "funcrequest.h"
23 #include "gettext.h"
24 #include "iterators.h"
25 #include "LaTeXFeatures.h"
26 #include "Lsstream.h"
27 #include "lyxfont.h"
28 #include "lyxlex.h"
29 #include "lyxtext.h"
30 #include "Lsstream.h"
31
32 #include "support/LOstream.h"
33 #include "support/lstrings.h"
34 #include "support/tostr.h"
35
36 #include "frontends/LyXView.h"
37 #include "frontends/Dialogs.h"
38
39 using namespace lyx::support;
40
41 using std::ostream;
42 using std::endl;
43 using std::auto_ptr;
44
45
46 // With this inset it will be possible to support the latex package
47 // float.sty, and I am sure that with this and some additional support
48 // classes we can support similar functionality in other formats
49 // (read DocBook).
50 // By using float.sty we will have the same handling for all floats, both
51 // for those already in existance (table and figure) and all user created
52 // ones¹. So suddenly we give the users the possibility of creating new
53 // kinds of floats on the fly. (and with a uniform look)
54 //
55 // API to float.sty:
56 //   \newfloat{type}{placement}{ext}[within]
57 //     type      - The "type" of the new class of floats, like program or
58 //                 algorithm. After the appropriate \newfloat, commands
59 //                 such as \begin{program} or \end{algorithm*} will be
60 //                 available.
61 //     placement - The default placement for the given class of floats.
62 //                 They are like in standard LaTeX: t, b, p and h for top,
63 //                 bottom, page, and here, respectively. On top of that
64 //                 there is a new type, H, which does not really correspond
65 //                 to a float, since it means: put it "here" and nowhere else.
66 //                 Note, however that the H specifier is special and, because
67 //                 of implementation details cannot be used in the second
68 //                 argument of \newfloat.
69 //     ext       - The file name extension of an auxiliary file for the list
70 //                 of figures (or whatever). LaTeX writes the captions to
71 //                 this file.
72 //     within    - This (optional) argument determines whether floats of this
73 //                 class will be numbered within some sectional unit of the
74 //                 document. For example, if within is equal to chapter, the
75 //                 floats will be numbered within chapters.
76 //   \floatstyle{style}
77 //     style -  plain, boxed, ruled
78 //   \floatname{float}{floatname}
79 //     float     -
80 //     floatname -
81 //   \floatplacement{float}{placement}
82 //     float     -
83 //     placement -
84 //   \restylefloat{float}
85 //     float -
86 //   \listof{type}{title}
87 //     title -
88
89 // ¹ the algorithm float is defined using the float.sty package. Like this
90 //   \floatstyle{ruled}
91 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
92 //   \floatname{algorithm}{Algorithm}
93 //
94 // The intention is that floats should be definable from two places:
95 //          - layout files
96 //          - the "gui" (i.e. by the user)
97 //
98 // From layout files.
99 // This should only be done for floats defined in a documentclass and that
100 // does not need any additional packages. The two most known floats in this
101 // category is "table" and "figure". Floats defined in layout files are only
102 // stored in lyx files if the user modifies them.
103 //
104 // By the user.
105 // There should be a gui dialog (and also a collection of lyxfuncs) where
106 // the user can modify existing floats and/or create new ones.
107 //
108 // The individual floats will also have some settable
109 // variables: wide and placement.
110 //
111 // Lgb
112
113 namespace {
114
115 // this should not be hardcoded, but be part of the definition
116 // of the float (JMarc)
117 string const caplayout("Caption");
118
119 string floatname(string const & type, BufferParams const & bp)
120 {
121         FloatList const & floats = bp.getLyXTextClass().floats();
122         FloatList::const_iterator it = floats[type];
123         if (it == floats.end())
124                 return type;
125
126         return _(it->second.name());
127 }
128
129 } // namespace anon
130
131
132 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
133         : InsetCollapsable(bp)
134 {
135         string lab(_("float: "));
136         lab += floatname(type, bp);
137         setLabel(lab);
138         LyXFont font(LyXFont::ALL_SANE);
139         font.decSize();
140         font.decSize();
141         font.setColor(LColor::collapsable);
142         setLabelFont(font);
143         params_.type = type;
144         setInsetName(type);
145         LyXTextClass const & tclass = bp.getLyXTextClass();
146         if (tclass.hasLayout(caplayout))
147                 inset.paragraphs.begin()->layout(tclass[caplayout]);
148 }
149
150
151 InsetFloat::InsetFloat(InsetFloat const & in)
152         : InsetCollapsable(in), params_(in.params_)
153 {}
154
155
156 InsetFloat::~InsetFloat()
157 {
158         InsetFloatMailer mailer(*this);
159         mailer.hideDialog();
160 }
161
162
163 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
164 {
165         switch (cmd.action) {
166
167         case LFUN_INSET_MODIFY: {
168                 InsetFloatParams params;
169                 InsetFloatMailer::string2params(cmd.argument, params);
170
171                 params_.placement = params.placement;
172                 params_.wide      = params.wide;
173
174                 wide(params_.wide, cmd.view()->buffer()->params);
175                 cmd.view()->updateInset(this);
176                 return DISPATCHED;
177         }
178
179         case LFUN_INSET_DIALOG_UPDATE: {
180                 InsetFloatMailer(*this).updateDialog(cmd.view());
181                 return DISPATCHED;
182         }
183
184         default:
185                 return InsetCollapsable::localDispatch(cmd);
186         }
187 }
188
189
190 void InsetFloatParams::write(ostream & os) const
191 {
192         os << "Float " // getInsetName()
193            << type << '\n';
194
195         if (!placement.empty())
196                 os << "placement " << placement << "\n";
197
198         if (wide)
199                 os << "wide true\n";
200         else
201                 os << "wide false\n";
202 }
203
204
205 void InsetFloatParams::read(LyXLex & lex)
206 {
207         if (lex.isOK()) {
208                 lex.next();
209                 string token = lex.getString();
210                 if (token == "placement") {
211                         lex.next();
212                         placement = lex.getString();
213                 } else {
214                         // take countermeasures
215                         lex.pushToken(token);
216                 }
217                 lex.next();
218                 token = lex.getString();
219                 if (token == "wide") {
220                         lex.next();
221                         string const tmptoken = lex.getString();
222                         wide = (tmptoken == "true");
223                 } else {
224                         lyxerr << "InsetFloat::Read:: Missing wide!"
225                                << endl;
226                         // take countermeasures
227                         lex.pushToken(token);
228                 }
229         }
230 }
231
232
233 void InsetFloat::write(Buffer const & buf, ostream & os) const
234 {
235         params_.write(os);
236         InsetCollapsable::write(buf, os);
237 }
238
239
240 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
241 {
242         params_.read(lex);
243         wide(params_.wide, buf.params);
244         InsetCollapsable::read(buf, lex);
245 }
246
247
248 void InsetFloat::validate(LaTeXFeatures & features) const
249 {
250         if (contains(params_.placement, "H")) {
251                 features.require("float");
252         }
253
254         features.useFloat(params_.type);
255         InsetCollapsable::validate(features);
256 }
257
258
259 auto_ptr<InsetBase> InsetFloat::clone() const
260 {
261         return auto_ptr<InsetBase>(new InsetFloat(*this));
262 }
263
264
265 string const InsetFloat::editMessage() const
266 {
267         return _("Opened Float Inset");
268 }
269
270
271 int InsetFloat::latex(Buffer const & buf, ostream & os,
272                       LatexRunParams const & runparams) const
273 {
274         FloatList const & floats = buf.params.getLyXTextClass().floats();
275         string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
276         // Figure out the float placement to use.
277         // From lowest to highest:
278         // - float default placement
279         // - document wide default placement
280         // - specific float placement
281         string placement;
282         string const buf_placement = buf.params.float_placement;
283         string const def_placement = floats.defaultPlacement(params_.type);
284         if (!params_.placement.empty()
285             && params_.placement != def_placement) {
286                 placement = params_.placement;
287         } else if (params_.placement.empty()
288                    && !buf_placement.empty()
289                    && buf_placement != def_placement) {
290                 placement = buf_placement;
291         }
292
293         // The \n is used to force \begin{<floatname>} to appear in a new line.
294         // The % is needed to prevent two consecutive \n chars in the case
295         // when the current output line is empty.
296         os << "%\n\\begin{" << tmptype << '}';
297         // We only output placement if different from the def_placement.
298         if (!placement.empty()) {
299                 os << '[' << placement << ']';
300         }
301         os << '\n';
302
303         int const i = inset.latex(buf, os, runparams);
304
305         // The \n is used to force \end{<floatname>} to appear in a new line.
306         // In this case, we do not case if the current output line is empty.
307         os << "\n\\end{" << tmptype << "}\n";
308
309         return i + 4;
310 }
311
312
313 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) const
314 {
315         FloatList const & floats = buf.params.getLyXTextClass().floats();
316         string const tmptype =  params_.type;
317         // Figure out the float placement to use.
318         // From lowest to highest:
319         // - float default placement
320         // - document wide default placement
321         // - specific float placement
322         // This is the same as latex, as linuxdoc is modeled after latex.
323
324         string placement;
325         string const buf_placement = buf.params.float_placement;
326         string const def_placement = floats.defaultPlacement(params_.type);
327         if (!params_.placement.empty()
328             && params_.placement != def_placement) {
329                 placement = params_.placement;
330         } else if (params_.placement.empty()
331                    && !buf_placement.empty()
332                    && buf_placement != def_placement) {
333                 placement = buf_placement;
334         }
335
336         os << "\n<" << tmptype ;
337         // We only output placement if different from the def_placement.
338         if (!placement.empty()) {
339                 os << " loc=\"" << placement << '"';
340         }
341         os << ">";
342
343         int const i = inset.linuxdoc(buf, os);
344         os << "</" << tmptype << ">\n";
345
346         return i;
347 }
348
349
350 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
351 {
352         os << '<' << params_.type << '>';
353         int const i = inset.docbook(buf, os, mixcont);
354         os << "</" << params_.type << '>';
355
356         return i;
357 }
358
359
360 bool InsetFloat::insetAllowed(InsetOld::Code code) const
361 {
362         if (code == InsetOld::FLOAT_CODE)
363                 return false;
364         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
365                 return inset.insetAllowed(code);
366         if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
367                 return false;
368         return true;
369 }
370
371
372 bool InsetFloat::showInsetDialog(BufferView * bv) const
373 {
374         if (!inset.showInsetDialog(bv)) {
375                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
376                 InsetFloatMailer mailer(*tmp);
377                 mailer.showDialog(bv);
378         }
379         return true;
380 }
381
382
383 void InsetFloat::wide(bool w, BufferParams const & bp)
384 {
385         params_.wide = w;
386
387         string lab(_("float: "));
388         lab += floatname(params_.type, bp);
389
390         if (params_.wide)
391                 lab += '*';
392
393         setLabel(lab);
394 }
395
396
397 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
398 {
399         ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
400         ParIterator end(inset.paragraphs.end(), inset.paragraphs);
401
402         // Find a caption layout in one of the (child inset's) pars
403         for (; pit != end; ++pit) {
404                 if (pit->layout()->name() == caplayout) {
405                         string const name = floatname(params_.type, buf.params);
406                         string const str =
407                                 tostr(toclist[name].size() + 1)
408                                 + ". " + pit->asString(buf, false);
409                         lyx::toc::TocItem const item(pit->id(), 0 , str);
410                         toclist[name].push_back(item);
411                 }
412         }
413 }
414
415
416 string const InsetFloatMailer::name_("float");
417
418 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
419         : inset_(inset)
420 {}
421
422
423 string const InsetFloatMailer::inset2string(Buffer const &) const
424 {
425         return params2string(inset_.params());
426 }
427
428
429 void InsetFloatMailer::string2params(string const & in,
430                                      InsetFloatParams & params)
431 {
432         params = InsetFloatParams();
433
434         if (in.empty())
435                 return;
436
437         istringstream data(STRCONV(in));
438         LyXLex lex(0,0);
439         lex.setStream(data);
440
441         if (lex.isOK()) {
442                 lex.next();
443                 string const token = lex.getString();
444                 if (token != name_)
445                         return;
446         }
447
448         // This is part of the inset proper that is usually swallowed
449         // by Buffer::readInset
450         if (lex.isOK()) {
451                 lex.next();
452                 string const token = lex.getString();
453                 if (token != "Float" || !lex.eatLine())
454                         return;
455         }
456
457         if (lex.isOK()) {
458                 params.read(lex);
459         }
460 }
461
462
463 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
464 {
465         ostringstream data;
466         data << name_ << ' ';
467         params.write(data);
468         return STRCONV(data.str());
469 }