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