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