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