]> git.lyx.org Git - lyx.git/blob - src/insets/InsetFloat.cpp
Norwegian Intro.lyx: - fix image paths to make it compilable
[lyx.git] / src / insets / InsetFloat.cpp
1 /**
2  * \file InsetFloat.cpp
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  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetFloat.h"
16 #include "InsetCaption.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Counters.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "Floating.h"
25 #include "FloatList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "InsetList.h"
29 #include "LaTeXFeatures.h"
30 #include "Lexer.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
34
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include "frontends/Application.h"
42
43 using namespace std;
44
45
46 namespace lyx {
47
48 // With this inset it will be possible to support the latex package
49 // float.sty, and I am sure that with this and some additional support
50 // classes we can support similar functionality in other formats
51 // (read DocBook).
52 // By using float.sty we will have the same handling for all floats, both
53 // for those already in existance (table and figure) and all user created
54 // ones¹. So suddenly we give the users the possibility of creating new
55 // kinds of floats on the fly. (and with a uniform look)
56 //
57 // API to float.sty:
58 //   \newfloat{type}{placement}{ext}[within]
59 //     type      - The "type" of the new class of floats, like program or
60 //                 algorithm. After the appropriate \newfloat, commands
61 //                 such as \begin{program} or \end{algorithm*} will be
62 //                 available.
63 //     placement - The default placement for the given class of floats.
64 //                 They are like in standard LaTeX: t, b, p and h for top,
65 //                 bottom, page, and here, respectively. On top of that
66 //                 there is a new type, H, which does not really correspond
67 //                 to a float, since it means: put it "here" and nowhere else.
68 //                 Note, however that the H specifier is special and, because
69 //                 of implementation details cannot be used in the second
70 //                 argument of \newfloat.
71 //     ext       - The file name extension of an auxiliary file for the list
72 //                 of figures (or whatever). LaTeX writes the captions to
73 //                 this file.
74 //     within    - This (optional) argument determines whether floats of this
75 //                 class will be numbered within some sectional unit of the
76 //                 document. For example, if within is equal to chapter, the
77 //                 floats will be numbered within chapters.
78 //   \floatstyle{style}
79 //     style -  plain, boxed, ruled
80 //   \floatname{float}{floatname}
81 //     float     -
82 //     floatname -
83 //   \floatplacement{float}{placement}
84 //     float     -
85 //     placement -
86 //   \restylefloat{float}
87 //     float -
88 //   \listof{type}{title}
89 //     title -
90
91 // ¹ the algorithm float is defined using the float.sty package. Like this
92 //   \floatstyle{ruled}
93 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
94 //   \floatname{algorithm}{Algorithm}
95 //
96 // The intention is that floats should be definable from two places:
97 //          - layout files
98 //          - the "gui" (i.e. by the user)
99 //
100 // From layout files.
101 // This should only be done for floats defined in a documentclass and that
102 // does not need any additional packages. The two most known floats in this
103 // category is "table" and "figure". Floats defined in layout files are only
104 // stored in lyx files if the user modifies them.
105 //
106 // By the user.
107 // There should be a gui dialog (and also a collection of lyxfuncs) where
108 // the user can modify existing floats and/or create new ones.
109 //
110 // The individual floats will also have some settable
111 // variables: wide and placement.
112 //
113 // Lgb
114
115
116 InsetFloat::InsetFloat(Buffer const & buf, string const & type)
117         : InsetCollapsable(buf), name_(from_utf8(type))
118 {
119         setLabel(_("float: ") + floatName(type, buf.params()));
120         params_.type = type;
121 }
122
123
124 InsetFloat::~InsetFloat()
125 {
126         hideDialogs("float", this);
127 }
128
129
130 docstring InsetFloat::name() const 
131
132         return "Float:" + name_; 
133 }
134
135
136 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
137 {
138         if (InsetCollapsable::toolTip(bv, x, y).empty() || isOpen(bv))
139                 return docstring();
140
141         OutputParams rp(&buffer().params().encoding());
142         return getCaptionText(rp);
143 }
144
145
146 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
147 {
148         switch (cmd.action) {
149
150         case LFUN_INSET_MODIFY: {
151                 InsetFloatParams params;
152                 string2params(to_utf8(cmd.argument()), params);
153
154                 // placement, wide and sideways are not used for subfloats
155                 if (!params_.subfloat) {
156                         params_.placement = params.placement;
157                         params_.wide      = params.wide;
158                         params_.sideways  = params.sideways;
159                         setWide(params_.wide, cur.buffer()->params(), false);
160                         setSideways(params_.sideways, cur.buffer()->params(), false);
161                 }
162
163                 setNewLabel(cur.buffer()->params());
164                 break;
165         }
166
167         case LFUN_INSET_DIALOG_UPDATE: {
168                 cur.bv().updateDialog("float", params2string(params()));
169                 break;
170         }
171
172         default:
173                 InsetCollapsable::doDispatch(cur, cmd);
174                 break;
175         }
176 }
177
178
179 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
180                 FuncStatus & flag) const
181 {
182         switch (cmd.action) {
183
184         case LFUN_INSET_MODIFY:
185         case LFUN_INSET_DIALOG_UPDATE:
186         case LFUN_INSET_SETTINGS:
187                 flag.setEnabled(true);
188                 return true;
189
190         default:
191                 return InsetCollapsable::getStatus(cur, cmd, flag);
192         }
193 }
194
195
196 void InsetFloat::updateLabels(ParIterator const & it)
197 {
198         Counters & cnts =
199                 buffer().masterBuffer()->params().documentClass().counters();
200         string const saveflt = cnts.current_float();
201         bool const savesubflt = cnts.isSubfloat();
202
203         bool const subflt = (it.innerInsetOfType(FLOAT_CODE)
204                              || it.innerInsetOfType(WRAP_CODE));
205         // floats can only embed subfloats of their own kind
206         if (subflt)
207                 params_.type = saveflt;
208         setSubfloat(subflt, buffer().params());
209
210         // Tell to captions what the current float is
211         cnts.current_float(params().type);
212         cnts.isSubfloat(subflt);
213
214         InsetCollapsable::updateLabels(it);
215
216         //reset afterwards
217         cnts.current_float(saveflt);
218         cnts.isSubfloat(savesubflt);
219 }
220
221
222 void InsetFloatParams::write(ostream & os) const
223 {
224         os << "Float " << type << '\n';
225
226         if (!placement.empty())
227                 os << "placement " << placement << "\n";
228
229         if (wide)
230                 os << "wide true\n";
231         else
232                 os << "wide false\n";
233
234         if (sideways)
235                 os << "sideways true\n";
236         else
237                 os << "sideways false\n";
238 }
239
240
241 void InsetFloatParams::read(Lexer & lex)
242 {
243         lex.setContext("InsetFloatParams::read");
244         if (lex.checkFor("placement"))
245                 lex >> placement;
246         lex >> "wide" >> wide;
247         lex >> "sideways" >> sideways;
248 }
249
250
251 void InsetFloat::write(ostream & os) const
252 {
253         params_.write(os);
254         InsetCollapsable::write(os);
255 }
256
257
258 void InsetFloat::read(Lexer & lex)
259 {
260         params_.read(lex);
261         InsetCollapsable::read(lex);
262 }
263
264
265 void InsetFloat::validate(LaTeXFeatures & features) const
266 {
267         if (support::contains(params_.placement, 'H'))
268                 features.require("float");
269
270         if (params_.sideways)
271                 features.require("rotfloat");
272
273         if (features.inFloat())
274                 features.require("subfig");
275
276         features.useFloat(params_.type, features.inFloat());
277         features.inFloat(true);
278         InsetCollapsable::validate(features);
279         features.inFloat(false);
280 }
281
282
283 docstring InsetFloat::editMessage() const
284 {
285         return _("Opened Float Inset");
286 }
287
288
289 int InsetFloat::latex(odocstream & os, OutputParams const & runparams_in) const
290 {
291         if (runparams_in.inFloat != OutputParams::NONFLOAT) {
292                 if (runparams_in.moving_arg)
293                         os << "\\protect";
294                 os << "\\subfloat";
295         
296                 OutputParams rp = runparams_in;
297                 docstring const caption = getCaption(rp);
298                 if (!caption.empty()) {
299                         os << caption;
300                 }
301                 os << '{';
302                 rp.inFloat = OutputParams::SUBFLOAT;
303                 int const i = InsetText::latex(os, rp);
304                 os << "}";
305         
306                 return i + 1;
307         }
308         OutputParams runparams(runparams_in);
309         runparams.inFloat = OutputParams::MAINFLOAT;
310
311         FloatList const & floats = buffer().params().documentClass().floats();
312         string tmptype = params_.type;
313         if (params_.sideways)
314                 tmptype = "sideways" + params_.type;
315         if (params_.wide && (!params_.sideways ||
316                              params_.type == "figure" ||
317                              params_.type == "table"))
318                 tmptype += "*";
319         // Figure out the float placement to use.
320         // From lowest to highest:
321         // - float default placement
322         // - document wide default placement
323         // - specific float placement
324         string placement;
325         string const buf_placement = buffer().params().float_placement;
326         string const def_placement = floats.defaultPlacement(params_.type);
327         if (!params_.placement.empty()
328             && params_.placement != def_placement) {
329                 placement = params_.placement;
330         } else if (params_.placement.empty()
331                    && !buf_placement.empty()
332                    && buf_placement != def_placement) {
333                 placement = buf_placement;
334         }
335
336         // The \n is used to force \begin{<floatname>} to appear in a new line.
337         // The % is needed to prevent two consecutive \n chars in the case
338         // when the current output line is empty.
339         os << "%\n\\begin{" << from_ascii(tmptype) << '}';
340         // We only output placement if different from the def_placement.
341         // sidewaysfloats always use their own page
342         if (!placement.empty() && !params_.sideways) {
343                 os << '[' << from_ascii(placement) << ']';
344         }
345         os << '\n';
346
347         int const i = InsetText::latex(os, runparams);
348
349         // The \n is used to force \end{<floatname>} to appear in a new line.
350         // In this case, we do not case if the current output line is empty.
351         os << "\n\\end{" << from_ascii(tmptype) << "}\n";
352
353         return i + 4;
354 }
355
356
357 int InsetFloat::plaintext(odocstream & os, OutputParams const & runparams) const
358 {
359         os << '[' << buffer().B_("float") << ' '
360                 << floatName(params_.type, buffer().params()) << ":\n";
361         InsetText::plaintext(os, runparams);
362         os << "\n]";
363
364         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
365 }
366
367
368 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
369 {
370         // FIXME Implement subfloat!
371         // FIXME UNICODE
372         os << '<' << from_ascii(params_.type) << '>';
373         int const i = InsetText::docbook(os, runparams);
374         os << "</" << from_ascii(params_.type) << '>';
375
376         return i;
377 }
378
379
380 bool InsetFloat::insetAllowed(InsetCode code) const
381 {
382         return code != FOOT_CODE
383             && code != MARGIN_CODE
384             && (code != FLOAT_CODE || !params_.subfloat);
385 }
386
387
388 bool InsetFloat::showInsetDialog(BufferView * bv) const
389 {
390         if (!InsetText::showInsetDialog(bv))
391                 bv->showDialog("float", params2string(params()),
392                         const_cast<InsetFloat *>(this));
393         return true;
394 }
395
396
397 void InsetFloat::setWide(bool w, BufferParams const & bp, bool update_label)
398 {
399         params_.wide = w;
400         if (update_label)
401                 setNewLabel(bp);
402 }
403
404
405 void InsetFloat::setSideways(bool s, BufferParams const & bp, bool update_label)
406 {
407         params_.sideways = s;
408         if (update_label)
409                 setNewLabel(bp);
410 }
411
412
413 void InsetFloat::setSubfloat(bool s, BufferParams const & bp, bool update_label)
414 {
415         params_.subfloat = s;
416         if (update_label)
417                 setNewLabel(bp);
418 }
419
420
421 void InsetFloat::setNewLabel(BufferParams const & bp)
422 {
423         docstring lab = _("float: ");
424
425         if (params_.subfloat)
426                 lab = _("subfloat: ");
427
428         lab += floatName(params_.type, bp);
429
430         if (params_.wide)
431                 lab += '*';
432
433         if (params_.sideways)
434                 lab += _(" (sideways)");
435
436         setLabel(lab);
437 }
438
439
440 docstring InsetFloat::getCaption(OutputParams const & runparams) const
441 {
442         if (paragraphs().empty())
443                 return docstring();
444
445         ParagraphList::const_iterator pit = paragraphs().begin();
446         for (; pit != paragraphs().end(); ++pit) {
447                 InsetList::const_iterator it = pit->insetList().begin();
448                 for (; it != pit->insetList().end(); ++it) {
449                         Inset & inset = *it->inset;
450                         if (inset.lyxCode() == CAPTION_CODE) {
451                                 odocstringstream ods;
452                                 InsetCaption * ins =
453                                         static_cast<InsetCaption *>(it->inset);
454                                 ins->getOptArg(ods, runparams);
455                                 ods << '[';
456                                 ins->getArgument(ods, runparams);
457                                 ods << ']';
458                                 return ods.str();
459                         }
460                 }
461         }
462         return docstring();
463 }
464
465
466 docstring InsetFloat::getCaptionText(OutputParams const & runparams) const
467 {
468         if (paragraphs().empty())
469                 return docstring();
470
471         ParagraphList::const_iterator pit = paragraphs().begin();
472         for (; pit != paragraphs().end(); ++pit) {
473                 InsetList::const_iterator it = pit->insetList().begin();
474                 for (; it != pit->insetList().end(); ++it) {
475                         Inset & inset = *it->inset;
476                         if (inset.lyxCode() == CAPTION_CODE) {
477                                 odocstringstream ods;
478                                 InsetCaption * ins =
479                                         static_cast<InsetCaption *>(it->inset);
480                                 ins->getCaptionText(ods, runparams);
481                                 return ods.str();
482                         }
483                 }
484         }
485         return docstring();
486 }
487
488
489 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
490 {
491         params = InsetFloatParams();
492         if (in.empty())
493                 return;
494
495         istringstream data(in);
496         Lexer lex;
497         lex.setStream(data);
498         lex.setContext("InsetFloat::string2params");
499         lex >> "float" >> "Float";
500         lex >> params.type; // We have to read the type here!
501         params.read(lex);
502 }
503
504
505 string InsetFloat::params2string(InsetFloatParams const & params)
506 {
507         ostringstream data;
508         data << "float" << ' ';
509         params.write(data);
510         return data.str();
511 }
512
513
514 } // namespace lyx