]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
ab78bdb83ed16585d1de69fc57fdd9529d4d0fd8
[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
34 #include "frontends/LyXView.h"
35 #include "frontends/Dialogs.h"
36
37 using std::ostream;
38 using std::endl;
39
40 // With this inset it will be possible to support the latex package
41 // float.sty, and I am sure that with this and some additional support
42 // classes we can support similar functionality in other formats
43 // (read DocBook).
44 // By using float.sty we will have the same handling for all floats, both
45 // for those already in existance (table and figure) and all user created
46 // ones¹. So suddenly we give the users the possibility of creating new
47 // kinds of floats on the fly. (and with a uniform look)
48 //
49 // API to float.sty:
50 //   \newfloat{type}{placement}{ext}[within]
51 //     type      - The "type" of the new class of floats, like program or
52 //                 algorithm. After the appropriate \newfloat, commands
53 //                 such as \begin{program} or \end{algorithm*} will be
54 //                 available.
55 //     placement - The default placement for the given class of floats.
56 //                 They are like in standard LaTeX: t, b, p and h for top,
57 //                 bottom, page, and here, respectively. On top of that
58 //                 there is a new type, H, which does not really correspond
59 //                 to a float, since it means: put it "here" and nowhere else.
60 //                 Note, however that the H specifier is special and, because
61 //                 of implementation details cannot be used in the second
62 //                 argument of \newfloat.
63 //     ext       - The file name extension of an auxiliary file for the list
64 //                 of figures (or whatever). LaTeX writes the captions to
65 //                 this file.
66 //     within    - This (optional) argument determines whether floats of this
67 //                 class will be numbered within some sectional unit of the
68 //                 document. For example, if within is equal to chapter, the
69 //                 floats will be numbered within chapters.
70 //   \floatstyle{style}
71 //     style -  plain, boxed, ruled
72 //   \floatname{float}{floatname}
73 //     float     -
74 //     floatname -
75 //   \floatplacement{float}{placement}
76 //     float     -
77 //     placement -
78 //   \restylefloat{float}
79 //     float -
80 //   \listof{type}{title}
81 //     title -
82
83 // ¹ the algorithm float is defined using the float.sty package. Like this
84 //   \floatstyle{ruled}
85 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
86 //   \floatname{algorithm}{Algorithm}
87 //
88 // The intention is that floats should be definable from two places:
89 //          - layout files
90 //          - the "gui" (i.e. by the user)
91 //
92 // From layout files.
93 // This should only be done for floats defined in a documentclass and that
94 // does not need any additional packages. The two most known floats in this
95 // category is "table" and "figure". Floats defined in layout files are only
96 // stored in lyx files if the user modifies them.
97 //
98 // By the user.
99 // There should be a gui dialog (and also a collection of lyxfuncs) where
100 // the user can modify existing floats and/or create new ones.
101 //
102 // The individual floats will also have some settable
103 // variables: wide and placement.
104 //
105 // Lgb
106
107 namespace {
108
109 // this should not be hardcoded, but be part of the definition
110 // of the float (JMarc)
111 string const caplayout("Caption");
112
113 string floatname(string const & type, BufferParams const & bp)
114 {
115         FloatList const & floats = bp.getLyXTextClass().floats();
116         FloatList::const_iterator it = floats[type];
117         if (it == floats.end())
118                 return type;
119
120         return _(it->second.name());
121 }
122
123 } // namespace anon
124
125
126 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
127         : InsetCollapsable(bp)
128 {
129         string lab(_("float: "));
130         lab += floatname(type, bp);
131         setLabel(lab);
132         LyXFont font(LyXFont::ALL_SANE);
133         font.decSize();
134         font.decSize();
135         font.setColor(LColor::collapsable);
136         setLabelFont(font);
137         params_.type = type;
138         setInsetName(type);
139         LyXTextClass const & tclass = bp.getLyXTextClass();
140         if (tclass.hasLayout(caplayout))
141                 inset.paragraphs.begin()->layout(tclass[caplayout]);
142 }
143
144
145 InsetFloat::InsetFloat(InsetFloat const & in, bool same_id)
146         : InsetCollapsable(in, same_id), params_(in.params_)
147 {}
148
149
150 InsetFloat::~InsetFloat()
151 {
152         InsetFloatMailer mailer(*this);
153         mailer.hideDialog();
154 }
155
156
157 dispatch_result InsetFloat::localDispatch(FuncRequest const & cmd)
158 {
159         Inset::RESULT result = UNDISPATCHED;
160
161         switch (cmd.action) {
162         case LFUN_INSET_MODIFY: {
163                 InsetFloatParams params;
164                 InsetFloatMailer::string2params(cmd.argument, params);
165
166                 params_.placement = params.placement;
167                 params_.wide      = params.wide;
168
169                 wide(params_.wide, cmd.view()->buffer()->params);
170                 cmd.view()->updateInset(this);
171                 result = DISPATCHED;
172         }
173         break;
174
175         case LFUN_INSET_DIALOG_UPDATE: {
176                 InsetFloatMailer mailer(*this);
177                 mailer.updateDialog(cmd.view());
178         }
179         break;
180
181         default:
182                 result = InsetCollapsable::localDispatch(cmd);
183         }
184
185         return result;
186 }
187
188
189 void InsetFloatParams::write(ostream & os) const
190 {
191         os << "Float " // getInsetName()
192            << type << '\n';
193
194         if (!placement.empty()) {
195                 os << "placement " << placement << "\n";
196         }
197         if (wide) {
198                 os << "wide true\n";
199         } else {
200                 os << "wide false\n";
201         }
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 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
260 {
261         return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
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,
272                       ostream & os, bool fragile, bool fp) 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, fragile, fp);
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::docbook(Buffer const * buf, ostream & os, bool mixcont) const
314 {
315         os << '<' << params_.type << '>';
316         int const i = inset.docbook(buf, os, mixcont);
317         os << "</" << params_.type << '>';
318
319         return i;
320 }
321
322
323 bool InsetFloat::insetAllowed(Inset::Code code) const
324 {
325         if (code == Inset::FLOAT_CODE)
326                 return false;
327         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
328                 return inset.insetAllowed(code);
329         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
330                 return false;
331         return true;
332 }
333
334
335 bool InsetFloat::showInsetDialog(BufferView * bv) const
336 {
337         if (!inset.showInsetDialog(bv)) {
338                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
339                 InsetFloatMailer mailer(*tmp);
340                 mailer.showDialog(bv);
341         }
342         return true;
343 }
344
345
346 void InsetFloat::wide(bool w, BufferParams const & bp)
347 {
348         params_.wide = w;
349
350         string lab(_("float:"));
351         lab += floatname(params_.type, bp);
352
353         if (params_.wide)
354                 lab += '*';
355
356         setLabel(lab);
357 }
358
359
360 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
361 {
362         ParIterator pit(&*inset.paragraphs.begin());
363         ParIterator end;
364
365         // Find a caption layout in one of the (child inset's) pars
366         for (; pit != end; ++pit) {
367                 Paragraph * tmp = *pit;
368
369                 if (tmp->layout()->name() == caplayout) {
370                         string const name = floatname(params_.type, buf->params);
371                         string const str =
372                                 tostr(toclist[name].size() + 1)
373                                 + ". " + tmp->asString(buf, false);
374                         toc::TocItem const item(tmp->id(), 0 , str);
375                         toclist[name].push_back(item);
376                 }
377         }
378 }
379
380
381 string const InsetFloatMailer::name_("float");
382
383 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
384         : inset_(inset)
385 {}
386
387
388 string const InsetFloatMailer::inset2string() const
389 {
390         return params2string(inset_.params());
391 }
392
393
394 void InsetFloatMailer::string2params(string const & in,
395                                      InsetFloatParams & params)
396 {
397         params = InsetFloatParams();
398
399         if (in.empty())
400                 return;
401         
402         istringstream data(in);
403         LyXLex lex(0,0);
404         lex.setStream(data);
405
406         if (lex.isOK()) {
407                 lex.next();
408                 string const token = lex.getString();
409                 if (token != name_)
410                         return;
411         }
412
413         // This is part of the inset proper that is usually swallowed
414         // by Buffer::readInset
415         if (lex.isOK()) {
416                 lex.next();
417                 string const token = lex.getString();
418                 if (token != "Float" || !lex.eatLine())
419                         return;
420         }
421
422         if (lex.isOK()) {
423                 params.read(lex);
424         }
425 }
426
427
428 string const
429 InsetFloatMailer::params2string(InsetFloatParams const & params)
430 {
431         ostringstream data;
432         data << name_ << ' ';
433         params.write(data);
434
435         return data.str();
436 }