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