]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
Another set of text changes from Michael, approved by John
[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                 cur.bv().update();
164                 break;
165         }
166
167         case LFUN_INSET_DIALOG_UPDATE: {
168                 InsetFloatMailer(*this).updateDialog(&cur.bv());
169                 break;
170         }
171
172         case LFUN_MOUSE_RELEASE: {
173                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
174                         InsetFloatMailer(*this).showDialog(&cur.bv());
175                         break;
176                 }
177                 InsetCollapsable::doDispatch(cur, cmd);
178                 break;
179         }
180
181         default:
182                 InsetCollapsable::doDispatch(cur, cmd);
183                 break;
184         }
185 }
186
187
188 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
189                 FuncStatus & flag) const
190 {
191         switch (cmd.action) {
192
193         case LFUN_INSET_MODIFY:
194         case LFUN_INSET_DIALOG_UPDATE:
195                 flag.enabled(true);
196                 return true;
197
198         default:
199                 return InsetCollapsable::getStatus(cur, cmd, flag);
200         }
201 }
202
203
204 void InsetFloatParams::write(ostream & os) const
205 {
206         os << "Float " << type << '\n';
207
208         if (!placement.empty())
209                 os << "placement " << placement << "\n";
210
211         if (wide)
212                 os << "wide true\n";
213         else
214                 os << "wide false\n";
215
216         if (sideways)
217                 os << "sideways true\n";
218         else
219                 os << "sideways false\n";
220 }
221
222
223 void InsetFloatParams::read(LyXLex & lex)
224 {
225         string token;
226         lex >> token;
227         if (token == "placement") {
228                 lex >> placement;
229         } else {
230                 // take countermeasures
231                 lex.pushToken(token);
232         }
233         lex >> token;
234         if (token == "wide") {
235                 lex >> wide;
236         } else {
237                 lyxerr << "InsetFloat::Read:: Missing wide!"
238                 << endl;
239                 // take countermeasures
240                 lex.pushToken(token);
241         }
242         lex >> token;
243         if (token == "sideways") {
244                 lex >> sideways;
245         } else {
246                 lyxerr << "InsetFloat::Read:: Missing sideways!"
247                 << endl;
248                 // take countermeasures
249                 lex.pushToken(token);
250         }
251 }
252
253
254 void InsetFloat::write(Buffer const & buf, ostream & os) const
255 {
256         params_.write(os);
257         InsetCollapsable::write(buf, os);
258 }
259
260
261 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
262 {
263         params_.read(lex);
264         wide(params_.wide, buf.params());
265         sideways(params_.sideways, buf.params());
266         InsetCollapsable::read(buf, lex);
267 }
268
269
270 void InsetFloat::validate(LaTeXFeatures & features) const
271 {
272         if (contains(params_.placement, 'H')) {
273                 features.require("float");
274         }
275
276         if (params_.sideways)
277                 features.require("rotating");
278
279         features.useFloat(params_.type);
280         InsetCollapsable::validate(features);
281 }
282
283
284 auto_ptr<InsetBase> InsetFloat::doClone() const
285 {
286         return auto_ptr<InsetBase>(new InsetFloat(*this));
287 }
288
289
290 string const InsetFloat::editMessage() const
291 {
292         return _("Opened Float Inset");
293 }
294
295
296 int InsetFloat::latex(Buffer const & buf, ostream & os,
297                       OutputParams const & runparams) const
298 {
299         FloatList const & floats = buf.params().getLyXTextClass().floats();
300         string tmptype = (params_.wide ? params_.type + "*" : params_.type);
301         if (params_.sideways) {
302                 if (params_.type == "table")
303                         tmptype = "sidewaystable";
304                 else if (params_.type == "figure")
305                         tmptype = "sidewaysfigure";
306         }
307         // Figure out the float placement to use.
308         // From lowest to highest:
309         // - float default placement
310         // - document wide default placement
311         // - specific float placement
312         string placement;
313         string const buf_placement = buf.params().float_placement;
314         string const def_placement = floats.defaultPlacement(params_.type);
315         if (!params_.placement.empty()
316             && params_.placement != def_placement) {
317                 placement = params_.placement;
318         } else if (params_.placement.empty()
319                    && !buf_placement.empty()
320                    && buf_placement != def_placement) {
321                 placement = buf_placement;
322         }
323
324         // The \n is used to force \begin{<floatname>} to appear in a new line.
325         // The % is needed to prevent two consecutive \n chars in the case
326         // when the current output line is empty.
327         os << "%\n\\begin{" << tmptype << '}';
328         // We only output placement if different from the def_placement.
329         // sidewaysfloats always use their own page
330         if (!placement.empty() && !params_.sideways) {
331                 os << '[' << placement << ']';
332         }
333         os << '\n';
334
335         int const i = InsetText::latex(buf, os, runparams);
336
337         // The \n is used to force \end{<floatname>} to appear in a new line.
338         // In this case, we do not case if the current output line is empty.
339         os << "\n\\end{" << tmptype << "}\n";
340
341         return i + 4;
342 }
343
344
345 int InsetFloat::linuxdoc(Buffer const & buf, ostream & os,
346                          OutputParams const & runparams) const
347 {
348         FloatList const & floats = buf.params().getLyXTextClass().floats();
349         string const tmptype =  params_.type;
350         // Figure out the float placement to use.
351         // From lowest to highest:
352         // - float default placement
353         // - document wide default placement
354         // - specific float placement
355         // This is the same as latex, as linuxdoc is modeled after latex.
356
357         string placement;
358         string const buf_placement = buf.params().float_placement;
359         string const def_placement = floats.defaultPlacement(params_.type);
360         if (!params_.placement.empty()
361             && params_.placement != def_placement) {
362                 placement = params_.placement;
363         } else if (params_.placement.empty()
364                    && !buf_placement.empty()
365                    && buf_placement != def_placement) {
366                 placement = buf_placement;
367         }
368
369         os << "\n<" << tmptype ;
370         // We only output placement if different from the def_placement.
371         if (!placement.empty()) {
372                 os << " loc=\"" << placement << '"';
373         }
374         os << ">";
375
376         int const i = InsetText::linuxdoc(buf, os, runparams);
377         os << "</" << tmptype << ">\n";
378
379         return i;
380 }
381
382
383 int InsetFloat::docbook(Buffer const & buf, ostream & os,
384                         OutputParams const & runparams) const
385 {
386         os << '<' << params_.type << '>';
387         int const i = InsetText::docbook(buf, os, runparams);
388         os << "</" << params_.type << '>';
389
390         return i;
391 }
392
393
394 bool InsetFloat::insetAllowed(InsetBase::Code code) const
395 {
396         return code != InsetBase::FLOAT_CODE
397             && code != InsetBase::FOOT_CODE
398             && code != InsetBase::MARGIN_CODE;
399 }
400
401
402 bool InsetFloat::showInsetDialog(BufferView * bv) const
403 {
404         if (!InsetText::showInsetDialog(bv))
405                 InsetFloatMailer(const_cast<InsetFloat &>(*this)).showDialog(bv);
406         return true;
407 }
408
409
410 void InsetFloat::wide(bool w, BufferParams const & bp)
411 {
412         params_.wide = w;
413         string lab = _("float: ") + floatname(params_.type, bp);
414         if (params_.wide)
415                 lab += '*';
416         setLabel(lab);
417 }
418
419
420 void InsetFloat::sideways(bool s, BufferParams const & bp)
421 {
422         params_.sideways = s;
423         string lab = _("float: ") + floatname(params_.type, bp);
424         if (params_.sideways)
425                 lab += _(" (sideways)");
426         setLabel(lab);
427 }
428
429
430 void InsetFloat::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
431 {
432         ParConstIterator pit = par_const_iterator_begin(*this);
433         ParConstIterator end = par_const_iterator_end(*this);
434
435         // Find a caption layout in one of the (child inset's) pars
436         for (; pit != end; ++pit) {
437                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
438                         string const name = floatname(params_.type, buf.params());
439                         string const str =
440                                 convert<string>(toclist[name].size() + 1)
441                                 + ". " + pit->asString(buf, false);
442                         lyx::toc::TocItem const item(pit->id(), 0 , str);
443                         toclist[name].push_back(item);
444                 }
445         }
446 }
447
448
449 string const InsetFloatMailer::name_("float");
450
451 InsetFloatMailer::InsetFloatMailer(InsetFloat & inset)
452         : inset_(inset)
453 {}
454
455
456 string const InsetFloatMailer::inset2string(Buffer const &) const
457 {
458         return params2string(inset_.params());
459 }
460
461
462 void InsetFloatMailer::string2params(string const & in,
463                                      InsetFloatParams & params)
464 {
465         params = InsetFloatParams();
466         if (in.empty())
467                 return;
468
469         istringstream data(in);
470         LyXLex lex(0,0);
471         lex.setStream(data);
472
473         string name;
474         lex >> name;
475         if (!lex || name != name_)
476                 return print_mailer_error("InsetFloatMailer", in, 1, name_);
477
478         // This is part of the inset proper that is usually swallowed
479         // by LyXText::readInset
480         string id;
481         lex >> id;
482         if (!lex || id != "Float")
483                 return print_mailer_error("InsetBoxMailer", in, 2, "Float");
484
485         // We have to read the type here!
486         lex >> params.type;
487         params.read(lex);
488 }
489
490
491 string const InsetFloatMailer::params2string(InsetFloatParams const & params)
492 {
493         ostringstream data;
494         data << name_ << ' ';
495         params.write(data);
496         return data.str();
497 }