]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
parlist-16-a.diff
[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 std::ostream;
40 using std::endl;
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, bool same_id)
149         : InsetCollapsable(in, same_id), 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         Inset::RESULT result = UNDISPATCHED;
163
164         switch (cmd.action) {
165         case LFUN_INSET_MODIFY: {
166                 InsetFloatParams params;
167                 InsetFloatMailer::string2params(cmd.argument, params);
168
169                 params_.placement = params.placement;
170                 params_.wide      = params.wide;
171
172                 wide(params_.wide, cmd.view()->buffer()->params);
173                 cmd.view()->updateInset(this);
174                 result = DISPATCHED;
175         }
176         break;
177
178         case LFUN_INSET_DIALOG_UPDATE: {
179                 InsetFloatMailer mailer(*this);
180                 mailer.updateDialog(cmd.view());
181         }
182         break;
183
184         default:
185                 result = InsetCollapsable::localDispatch(cmd);
186         }
187
188         return result;
189 }
190
191
192 void InsetFloatParams::write(ostream & os) const
193 {
194         os << "Float " // getInsetName()
195            << type << '\n';
196
197         if (!placement.empty()) {
198                 os << "placement " << placement << "\n";
199         }
200         if (wide) {
201                 os << "wide true\n";
202         } else {
203                 os << "wide false\n";
204         }
205 }
206
207
208 void InsetFloatParams::read(LyXLex & lex)
209 {
210         if (lex.isOK()) {
211                 lex.next();
212                 string token = lex.getString();
213                 if (token == "placement") {
214                         lex.next();
215                         placement = lex.getString();
216                 } else {
217                         // take countermeasures
218                         lex.pushToken(token);
219                 }
220                 lex.next();
221                 token = lex.getString();
222                 if (token == "wide") {
223                         lex.next();
224                         string const tmptoken = lex.getString();
225                         wide = (tmptoken == "true");
226                 } else {
227                         lyxerr << "InsetFloat::Read:: Missing wide!"
228                                << endl;
229                         // take countermeasures
230                         lex.pushToken(token);
231                 }
232         }
233 }
234
235
236 void InsetFloat::write(Buffer const * buf, ostream & os) const
237 {
238         params_.write(os);
239         InsetCollapsable::write(buf, os);
240 }
241
242
243 void InsetFloat::read(Buffer const * buf, LyXLex & lex)
244 {
245         params_.read(lex);
246         wide(params_.wide, buf->params);
247         InsetCollapsable::read(buf, lex);
248 }
249
250
251 void InsetFloat::validate(LaTeXFeatures & features) const
252 {
253         if (contains(params_.placement, "H")) {
254                 features.require("float");
255         }
256
257         features.useFloat(params_.type);
258         InsetCollapsable::validate(features);
259 }
260
261
262 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
263 {
264         return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
265 }
266
267
268 string const InsetFloat::editMessage() const
269 {
270         return _("Opened Float Inset");
271 }
272
273
274 int InsetFloat::latex(Buffer const * buf,
275                       ostream & os, bool fragile, bool fp) const
276 {
277         FloatList const & floats = buf->params.getLyXTextClass().floats();
278         string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
279         // Figure out the float placement to use.
280         // From lowest to highest:
281         // - float default placement
282         // - document wide default placement
283         // - specific float placement
284         string placement;
285         string const buf_placement = buf->params.float_placement;
286         string const def_placement = floats.defaultPlacement(params_.type);
287         if (!params_.placement.empty()
288             && params_.placement != def_placement) {
289                 placement = params_.placement;
290         } else if (params_.placement.empty()
291                    && !buf_placement.empty()
292                    && buf_placement != def_placement) {
293                 placement = buf_placement;
294         }
295
296         // The \n is used to force \begin{<floatname>} to appear in a new line.
297         // The % is needed to prevent two consecutive \n chars in the case
298         // when the current output line is empty.
299         os << "%\n\\begin{" << tmptype << '}';
300         // We only output placement if different from the def_placement.
301         if (!placement.empty()) {
302                 os << '[' << placement << ']';
303         }
304         os << '\n';
305
306         int const i = inset.latex(buf, os, fragile, fp);
307
308         // The \n is used to force \end{<floatname>} to appear in a new line.
309         // In this case, we do not case if the current output line is empty.
310         os << "\n\\end{" << tmptype << "}\n";
311
312         return i + 4;
313 }
314
315
316 int InsetFloat::docbook(Buffer const * buf, ostream & os, bool mixcont) const
317 {
318         os << '<' << params_.type << '>';
319         int const i = inset.docbook(buf, os, mixcont);
320         os << "</" << params_.type << '>';
321
322         return i;
323 }
324
325
326 bool InsetFloat::insetAllowed(Inset::Code code) const
327 {
328         if (code == Inset::FLOAT_CODE)
329                 return false;
330         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
331                 return inset.insetAllowed(code);
332         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
333                 return false;
334         return true;
335 }
336
337
338 bool InsetFloat::showInsetDialog(BufferView * bv) const
339 {
340         if (!inset.showInsetDialog(bv)) {
341                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
342                 InsetFloatMailer mailer(*tmp);
343                 mailer.showDialog(bv);
344         }
345         return true;
346 }
347
348
349 void InsetFloat::wide(bool w, BufferParams const & bp)
350 {
351         params_.wide = w;
352
353         string lab(_("float:"));
354         lab += floatname(params_.type, bp);
355
356         if (params_.wide)
357                 lab += '*';
358
359         setLabel(lab);
360 }
361
362
363 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
364 {
365         ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
366         ParIterator end(inset.paragraphs.end(), inset.paragraphs);
367
368 a       // Find a caption layout in one of the (child inset's) pars
369         for (; pit != end; ++pit) {
370                 if (pit->layout()->name() == caplayout) {
371                         string const name = floatname(params_.type, buf->params);
372                         string const str =
373                                 tostr(toclist[name].size() + 1)
374                                 + ". " + pit->asString(buf, false);
375                         toc::TocItem const item(pit->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(STRCONV(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 InsetFloatMailer::params2string(InsetFloatParams const & params)
430 {
431         ostringstream data;
432         data << name_ << ' ';
433         params.write(data);
434         return STRCONV(data.str());
435 }