]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
The markDirty() and fitCursor() changes
[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.paragraph()->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                 cmd.view()->updateInset(this);
169                 result = DISPATCHED;
170         }
171         break;
172
173         case LFUN_INSET_DIALOG_UPDATE: {
174                 InsetFloatMailer mailer(*this);
175                 mailer.updateDialog(cmd.view());
176         }
177         break;
178
179         default:
180                 result = InsetCollapsable::localDispatch(cmd);
181         }
182
183         return result;
184 }
185
186
187 void InsetFloatParams::write(ostream & os) const
188 {
189         os << "Float " // getInsetName()
190            << type << '\n';
191
192         if (!placement.empty()) {
193                 os << "placement " << placement << "\n";
194         }
195         if (wide) {
196                 os << "wide true\n";
197         } else {
198                 os << "wide false\n";
199         }
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(placement(), "H")) {
249                 features.require("float");
250         }
251
252         features.useFloat(params_.type);
253         InsetCollapsable::validate(features);
254 }
255
256
257 Inset * InsetFloat::clone(Buffer const &, bool same_id) const
258 {
259         return new InsetFloat(*const_cast<InsetFloat *>(this), same_id);
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,
270                       ostream & os, bool fragile, bool fp) 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, fragile, fp);
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::docbook(Buffer const * buf, ostream & os, bool mixcont) const
312 {
313         os << '<' << params_.type << '>';
314         int const i = inset.docbook(buf, os, mixcont);
315         os << "</" << params_.type << '>';
316
317         return i;
318 }
319
320
321 bool InsetFloat::insetAllowed(Inset::Code code) const
322 {
323         if (code == Inset::FLOAT_CODE)
324                 return false;
325         if (inset.getLockingInset() != const_cast<InsetFloat *>(this))
326                 return inset.insetAllowed(code);
327         if ((code == Inset::FOOT_CODE) || (code == Inset::MARGIN_CODE))
328                 return false;
329         return true;
330 }
331
332
333 bool InsetFloat::showInsetDialog(BufferView * bv) const
334 {
335         if (!inset.showInsetDialog(bv)) {
336                 InsetFloat * tmp = const_cast<InsetFloat *>(this);
337                 InsetFloatMailer mailer(*tmp);
338                 mailer.showDialog(bv);
339         }
340         return true;
341 }
342
343
344 string const & InsetFloat::type() const
345 {
346         return params_.type;
347 }
348
349
350 void InsetFloat::placement(string const & p)
351 {
352         // FIX: Here we should only allow the placement to be set
353         // if a valid value.
354         params_.placement = p;
355 }
356
357
358 string const & InsetFloat::placement() const
359 {
360         return params_.placement;
361 }
362
363
364 void InsetFloat::wide(bool w, BufferParams const & bp)
365 {
366         params_.wide = w;
367
368         string lab(_("float:"));
369         lab += floatname(params_.type, bp);
370
371         if (params_.wide)
372                 lab += '*';
373
374         setLabel(lab);
375 }
376
377
378 bool InsetFloat::wide() const
379 {
380         return params_.wide;
381 }
382
383
384 void InsetFloat::addToToc(toc::TocList & toclist, Buffer const * buf) const
385 {
386         ParIterator pit(inset.paragraph());
387         ParIterator end;
388
389         // Find a caption layout in one of the (child inset's) pars
390         for (; pit != end; ++pit) {
391                 Paragraph * tmp = *pit;
392
393                 if (tmp->layout()->name() == caplayout) {
394                         string const name = floatname(type(), buf->params);
395                         string const str =
396                                 tostr(toclist[name].size() + 1)
397                                 + ". " + tmp->asString(buf, false);
398                         toc::TocItem const item(tmp->id(), 0 , str);
399                         toclist[name].push_back(item);
400                 }
401         }
402 }
403
404
405 string const InsetFloatMailer::name_("float");
406
407 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
408         : inset_(inset)
409 {}
410
411
412 string const InsetFloatMailer::inset2string() const
413 {
414         return params2string(inset_.params());
415 }
416
417
418 void InsetFloatMailer::string2params(string const & in,
419                                      InsetFloatParams & params)
420 {
421         params = InsetFloatParams();
422
423         istringstream data(in);
424         LyXLex lex(0,0);
425         lex.setStream(data);
426
427         if (lex.isOK()) {
428                 lex.next();
429                 string const token = lex.getString();
430                 if (token != name_)
431                         return;
432         }
433
434         // This is part of the inset proper that is usually swallowed
435         // by Buffer::readInset
436         if (lex.isOK()) {
437                 lex.next();
438                 string const token = lex.getString();
439                 if (token != "Float" || !lex.eatLine())
440                         return;
441         }
442
443         if (lex.isOK()) {
444                 params.read(lex);
445         }
446 }
447
448
449 string const
450 InsetFloatMailer::params2string(InsetFloatParams const & params)
451 {
452         ostringstream data;
453         data << name_ << ' ';
454         params.write(data);
455
456         return data.str();
457 }