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