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