]> git.lyx.org Git - lyx.git/blob - src/insets/insetfloat.C
Change editMessage to return a docstring, change functions to not use 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::docstring;
40 using lyx::support::contains;
41
42 using std::endl;
43 using std::string;
44 using std::auto_ptr;
45 using std::istringstream;
46 using std::ostream;
47 using std::ostringstream;
48
49
50 // With this inset it will be possible to support the latex package
51 // float.sty, and I am sure that with this and some additional support
52 // classes we can support similar functionality in other formats
53 // (read DocBook).
54 // By using float.sty we will have the same handling for all floats, both
55 // for those already in existance (table and figure) and all user created
56 // ones¹. So suddenly we give the users the possibility of creating new
57 // kinds of floats on the fly. (and with a uniform look)
58 //
59 // API to float.sty:
60 //   \newfloat{type}{placement}{ext}[within]
61 //     type      - The "type" of the new class of floats, like program or
62 //                 algorithm. After the appropriate \newfloat, commands
63 //                 such as \begin{program} or \end{algorithm*} will be
64 //                 available.
65 //     placement - The default placement for the given class of floats.
66 //                 They are like in standard LaTeX: t, b, p and h for top,
67 //                 bottom, page, and here, respectively. On top of that
68 //                 there is a new type, H, which does not really correspond
69 //                 to a float, since it means: put it "here" and nowhere else.
70 //                 Note, however that the H specifier is special and, because
71 //                 of implementation details cannot be used in the second
72 //                 argument of \newfloat.
73 //     ext       - The file name extension of an auxiliary file for the list
74 //                 of figures (or whatever). LaTeX writes the captions to
75 //                 this file.
76 //     within    - This (optional) argument determines whether floats of this
77 //                 class will be numbered within some sectional unit of the
78 //                 document. For example, if within is equal to chapter, the
79 //                 floats will be numbered within chapters.
80 //   \floatstyle{style}
81 //     style -  plain, boxed, ruled
82 //   \floatname{float}{floatname}
83 //     float     -
84 //     floatname -
85 //   \floatplacement{float}{placement}
86 //     float     -
87 //     placement -
88 //   \restylefloat{float}
89 //     float -
90 //   \listof{type}{title}
91 //     title -
92
93 // ¹ the algorithm float is defined using the float.sty package. Like this
94 //   \floatstyle{ruled}
95 //   \newfloat{algorithm}{htbp}{loa}[<sect>]
96 //   \floatname{algorithm}{Algorithm}
97 //
98 // The intention is that floats should be definable from two places:
99 //          - layout files
100 //          - the "gui" (i.e. by the user)
101 //
102 // From layout files.
103 // This should only be done for floats defined in a documentclass and that
104 // does not need any additional packages. The two most known floats in this
105 // category is "table" and "figure". Floats defined in layout files are only
106 // stored in lyx files if the user modifies them.
107 //
108 // By the user.
109 // There should be a gui dialog (and also a collection of lyxfuncs) where
110 // the user can modify existing floats and/or create new ones.
111 //
112 // The individual floats will also have some settable
113 // variables: wide and placement.
114 //
115 // Lgb
116
117 namespace {
118
119 string floatname(string const & type, BufferParams const & bp)
120 {
121         FloatList const & floats = bp.getLyXTextClass().floats();
122         FloatList::const_iterator it = floats[type];
123         if (it == floats.end())
124                 return type;
125
126         // FIXME UNICODE
127         return lyx::to_utf8(_(it->second.name()));
128 }
129
130 } // namespace anon
131
132
133 InsetFloat::InsetFloat(BufferParams const & bp, string const & type)
134         : InsetCollapsable(bp)
135 {
136         // FIXME UNICODE
137         setLabel(lyx::to_utf8(_("float: ")) + floatname(type, bp));
138         LyXFont font(LyXFont::ALL_SANE);
139         font.decSize();
140         font.decSize();
141         font.setColor(LColor::collapsable);
142         setLabelFont(font);
143         params_.type = type;
144         setInsetName(type);
145 }
146
147
148 InsetFloat::~InsetFloat()
149 {
150         InsetFloatMailer(*this).hideDialog();
151 }
152
153
154 void InsetFloat::doDispatch(LCursor & cur, FuncRequest & cmd)
155 {
156         switch (cmd.action) {
157
158         case LFUN_INSET_MODIFY: {
159                 InsetFloatParams params;
160                 InsetFloatMailer::string2params(lyx::to_utf8(cmd.argument()), params);
161                 params_.placement = params.placement;
162                 params_.wide      = params.wide;
163                 params_.sideways  = params.sideways;
164                 wide(params_.wide, cur.buffer().params());
165                 sideways(params_.sideways, cur.buffer().params());
166                 break;
167         }
168
169         case LFUN_INSET_DIALOG_UPDATE: {
170                 InsetFloatMailer(*this).updateDialog(&cur.bv());
171                 break;
172         }
173
174         case LFUN_MOUSE_RELEASE: {
175                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
176                         InsetFloatMailer(*this).showDialog(&cur.bv());
177                         break;
178                 }
179                 InsetCollapsable::doDispatch(cur, cmd);
180                 break;
181         }
182
183         default:
184                 InsetCollapsable::doDispatch(cur, cmd);
185                 break;
186         }
187 }
188
189
190 bool InsetFloat::getStatus(LCursor & cur, FuncRequest const & cmd,
191                 FuncStatus & flag) const
192 {
193         switch (cmd.action) {
194
195         case LFUN_INSET_MODIFY:
196         case LFUN_INSET_DIALOG_UPDATE:
197                 flag.enabled(true);
198                 return true;
199
200         default:
201                 return InsetCollapsable::getStatus(cur, cmd, flag);
202         }
203 }
204
205
206 void InsetFloatParams::write(ostream & os) const
207 {
208         os << "Float " << type << '\n';
209
210         if (!placement.empty())
211                 os << "placement " << placement << "\n";
212
213         if (wide)
214                 os << "wide true\n";
215         else
216                 os << "wide false\n";
217
218         if (sideways)
219                 os << "sideways true\n";
220         else
221                 os << "sideways false\n";
222 }
223
224
225 void InsetFloatParams::read(LyXLex & lex)
226 {
227         string token;
228         lex >> token;
229         if (token == "placement") {
230                 lex >> placement;
231         } else {
232                 // take countermeasures
233                 lex.pushToken(token);
234         }
235         lex >> token;
236         if (token == "wide") {
237                 lex >> wide;
238         } else {
239                 lyxerr << "InsetFloat::Read:: Missing wide!"
240                 << endl;
241                 // take countermeasures
242                 lex.pushToken(token);
243         }
244         lex >> token;
245         if (token == "sideways") {
246                 lex >> sideways;
247         } else {
248                 lyxerr << "InsetFloat::Read:: Missing sideways!"
249                 << endl;
250                 // take countermeasures
251                 lex.pushToken(token);
252         }
253 }
254
255
256 void InsetFloat::write(Buffer const & buf, ostream & os) const
257 {
258         params_.write(os);
259         InsetCollapsable::write(buf, os);
260 }
261
262
263 void InsetFloat::read(Buffer const & buf, LyXLex & lex)
264 {
265         params_.read(lex);
266         wide(params_.wide, buf.params());
267         sideways(params_.sideways, buf.params());
268         InsetCollapsable::read(buf, lex);
269 }
270
271
272 void InsetFloat::validate(LaTeXFeatures & features) const
273 {
274         if (contains(params_.placement, 'H')) {
275                 features.require("float");
276         }
277
278         if (params_.sideways)
279                 features.require("rotating");
280
281         features.useFloat(params_.type);
282         InsetCollapsable::validate(features);
283 }
284
285
286 auto_ptr<InsetBase> InsetFloat::doClone() const
287 {
288         return auto_ptr<InsetBase>(new InsetFloat(*this));
289 }
290
291
292 docstring const InsetFloat::editMessage() const
293 {
294         return _("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 }