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