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