]> git.lyx.org Git - features.git/blob - src/insets/insetfloat.C
Replace LString.h with support/std_string.h,
[features.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
27 #include "support/lstrings.h"
28 #include "support/tostr.h"
29
30 #include "support/std_sstream.h"
31
32 using namespace lyx::support;
33
34 using std::endl;
35 using std::auto_ptr;
36 using std::ostream;
37
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.paragraphs.begin()->layout(tclass[caplayout]);
141 }
142
143
144 InsetFloat::InsetFloat(InsetFloat const & in)
145         : InsetCollapsable(in), 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         switch (cmd.action) {
159
160         case LFUN_INSET_MODIFY: {
161                 InsetFloatParams params;
162                 InsetFloatMailer::string2params(cmd.argument, params);
163
164                 params_.placement = params.placement;
165                 params_.wide      = params.wide;
166
167                 wide(params_.wide, cmd.view()->buffer()->params);
168                 cmd.view()->updateInset(this);
169                 return DISPATCHED;
170         }
171
172         case LFUN_INSET_DIALOG_UPDATE: {
173                 InsetFloatMailer(*this).updateDialog(cmd.view());
174                 return DISPATCHED;
175         }
176
177         default:
178                 return InsetCollapsable::localDispatch(cmd);
179         }
180 }
181
182
183 void InsetFloatParams::write(ostream & os) const
184 {
185         os << "Float " // getInsetName()
186            << type << '\n';
187
188         if (!placement.empty())
189                 os << "placement " << placement << "\n";
190
191         if (wide)
192                 os << "wide true\n";
193         else
194                 os << "wide false\n";
195 }
196
197
198 void InsetFloatParams::read(LyXLex & lex)
199 {
200         if (lex.isOK()) {
201                 lex.next();
202                 string token = lex.getString();
203                 if (token == "placement") {
204                         lex.next();
205                         placement = lex.getString();
206                 } else {
207                         // take countermeasures
208                         lex.pushToken(token);
209                 }
210                 lex.next();
211                 token = lex.getString();
212                 if (token == "wide") {
213                         lex.next();
214                         string const tmptoken = lex.getString();
215                         wide = (tmptoken == "true");
216                 } else {
217                         lyxerr << "InsetFloat::Read:: Missing wide!"
218                                << endl;
219                         // take countermeasures
220                         lex.pushToken(token);
221                 }
222         }
223 }
224
225
226 void InsetFloat::write(Buffer const & buf, ostream & os) const
227 {
228         params_.write(os);
229         InsetCollapsable::write(buf, os);
230 }
231
232
233 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
234 {
235         params_.read(lex);
236         wide(params_.wide, buf.params);
237         InsetCollapsable::read(buf, lex);
238 }
239
240
241 void InsetFloat::validate(LaTeXFeatures & features) const
242 {
243         if (contains(params_.placement, "H")) {
244                 features.require("float");
245         }
246
247         features.useFloat(params_.type);
248         InsetCollapsable::validate(features);
249 }
250
251
252 auto_ptr<InsetBase> InsetFloat::clone() const
253 {
254         return auto_ptr<InsetBase>(new InsetFloat(*this));
255 }
256
257
258 string const InsetFloat::editMessage() const
259 {
260         return _("Opened Float Inset");
261 }
262
263
264 int InsetFloat::latex(Buffer const & buf, ostream & os,
265                       LatexRunParams const & runparams) const
266 {
267         FloatList const & floats = buf.params.getLyXTextClass().floats();
268         string const tmptype = (params_.wide ? params_.type + "*" : params_.type);
269         // Figure out the float placement to use.
270         // From lowest to highest:
271         // - float default placement
272         // - document wide default placement
273         // - specific float placement
274         string placement;
275         string const buf_placement = buf.params.float_placement;
276         string const def_placement = floats.defaultPlacement(params_.type);
277         if (!params_.placement.empty()
278             && params_.placement != def_placement) {
279                 placement = params_.placement;
280         } else if (params_.placement.empty()
281                    && !buf_placement.empty()
282                    && buf_placement != def_placement) {
283                 placement = buf_placement;
284         }
285
286         // The \n is used to force \begin{<floatname>} to appear in a new line.
287         // The % is needed to prevent two consecutive \n chars in the case
288         // when the current output line is empty.
289         os << "%\n\\begin{" << tmptype << '}';
290         // We only output placement if different from the def_placement.
291         if (!placement.empty()) {
292                 os << '[' << placement << ']';
293         }
294         os << '\n';
295
296         int const i = inset.latex(buf, os, runparams);
297
298         // The \n is used to force \end{<floatname>} to appear in a new line.
299         // In this case, we do not case if the current output line is empty.
300         os << "\n\\end{" << tmptype << "}\n";
301
302         return i + 4;
303 }
304
305
306 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os) 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 = inset.linuxdoc(buf, os);
337         os << "</" << tmptype << ">\n";
338
339         return i;
340 }
341
342
343 int InsetFloat::docbook(Buffer const & buf, ostream & os, bool mixcont) const
344 {
345         os << '<' << params_.type << '>';
346         int const i = inset.docbook(buf, os, mixcont);
347         os << "</" << params_.type << '>';
348
349         return i;
350 }
351
352
353 bool InsetFloat::insetAllowed(InsetOld::Code code) const
354 {
355         if (code == InsetOld::FLOAT_CODE)
356                 return false;
357         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
358                 return inset.insetAllowed(code);
359         if ((code == InsetOld::FOOT_CODE) || (code == InsetOld::MARGIN_CODE))
360                 return false;
361         return true;
362 }
363
364
365 bool InsetFloat::showInsetDialog(BufferView * bv) const
366 {
367         if (!inset.showInsetDialog(bv)) {
368                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
369                 InsetFloatMailer mailer(*tmp);
370                 mailer.showDialog(bv);
371         }
372         return true;
373 }
374
375
376 void InsetFloat::wide(bool w, BufferParams const & bp)
377 {
378         params_.wide = w;
379
380         string lab(_("float: "));
381         lab += floatname(params_.type, bp);
382
383         if (params_.wide)
384                 lab += '*';
385
386         setLabel(lab);
387 }
388
389
390 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
391 {
392         ParIterator pit(inset.paragraphs.begin(), inset.paragraphs);
393         ParIterator end(inset.paragraphs.end(), inset.paragraphs);
394
395         // Find a caption layout in one of the (child inset's) pars
396         for (; pit != end; ++pit) {
397                 if (pit->layout()->name() == caplayout) {
398                         string const name = floatname(params_.type, buf.params);
399                         string const str =
400                                 tostr(toclist[name].size() + 1)
401                                 + ". " + pit->asString(buf, false);
402                         lyx::toc::TocItem const item(pit->id(), 0 , str);
403                         toclist[name].push_back(item);
404                 }
405         }
406 }
407
408
409 string const InsetFloatMailer::name_("float");
410
411 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
412         : inset_(inset)
413 {}
414
415
416 string const InsetFloatMailer::inset2string(Buffer const &) const
417 {
418         return params2string(inset_.params());
419 }
420
421
422 void InsetFloatMailer::string2params(string const & in,
423                                      InsetFloatParams & params)
424 {
425         params = InsetFloatParams();
426
427         if (in.empty())
428                 return;
429
430         istringstream data(STRCONV(in));
431         LyXLex lex(0,0);
432         lex.setStream(data);
433
434         if (lex.isOK()) {
435                 lex.next();
436                 string const token = lex.getString();
437                 if (token != name_)
438                         return;
439         }
440
441         // This is part of the inset proper that is usually swallowed
442         // by Buffer::readInset
443         if (lex.isOK()) {
444                 lex.next();
445                 string const token = lex.getString();
446                 if (token != "Float" || !lex.eatLine())
447                         return;
448         }
449
450         if (lex.isOK()) {
451                 params.read(lex);
452         }
453 }
454
455
456 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
457 {
458         ostringstream data;
459         data << name_ << ' ';
460         params.write(data);
461         return STRCONV(data.str());
462 }