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