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