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