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