]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Enable the external inset to handle unknown templates gracefully.
[lyx.git] / src / insets / insetcaption.C
1 /**
2  * \file insetcaption.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "insetcaption.h"
14 #include "frontends/Painter.h"
15 #include "frontends/font_metrics.h"
16 #include "BufferView.h"
17 #include "buffer.h"
18 #include "FloatList.h"
19 #include "insets/insetfloat.h"
20 #include "insets/insetwrap.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "Lsstream.h"
24 #include "metricsinfo.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "support/BoostFormat.h"
28
29 using std::ostream;
30 using std::endl;
31
32
33 InsetCaption::InsetCaption(BufferParams const & bp)
34         : InsetText(bp)
35 {
36         setAutoBreakRows(true);
37         setDrawFrame(0, InsetText::LOCKED);
38         setFrameColor(0, LColor::captionframe);
39 }
40
41
42 void InsetCaption::write(Buffer const * buf, ostream & os) const
43 {
44         os << "Caption\n";
45         writeParagraphData(buf, os);
46 }
47
48
49 void InsetCaption::read(Buffer const * buf, LyXLex & lex)
50 {
51 #if 0
52         // We will enably this check again when the compability
53         // code is removed from Buffer::Read (Lgb)
54         string const token = lex.GetString();
55         if (token != "Caption") {
56                 lyxerr << "InsetCaption::Read: consistency check failed."
57                        << endl;
58         }
59 #endif
60         InsetText::read(buf, lex);
61 }
62
63
64 string const InsetCaption::editMessage() const
65 {
66         return _("Opened Caption Inset");
67 }
68
69
70 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
71 {
72         // We must draw the label, we should get the label string
73         // from the enclosing float inset.
74         // The question is: Who should draw the label, the caption inset,
75         // the text inset or the paragraph?
76         // We should also draw the float number (Lgb)
77
78         // See if we can find the name of the float this caption
79         // belongs to.
80         Inset * i1 = owner();
81         Inset * i2 = i1 ? i1->owner() : 0;
82         string type;
83         if (i2->lyxCode() == FLOAT_CODE)
84                 type = static_cast<InsetFloat *>(i2)->params().type;
85         else if (i2->lyxCode() == WRAP_CODE)
86                 type = static_cast<InsetWrap *>(i2)->params().type;
87         else
88                 lyx::Assert(0);
89
90         FloatList const & floats =
91                 pi.base.bv->buffer()->params.getLyXTextClass().floats();
92         string const fl = i2 ? floats.getType(type).name() : N_("Float");
93
94         // Discover the number...
95         string const num = "#";
96
97         // Generate the label
98         string const label = bformat("%1$s %2$s:", _(fl), num);
99         int const w = font_metrics::width(label, pi.base.font);
100         pi.pain.text(x, y, label, pi.base.font);
101         InsetText::draw(pi, x + w, y);
102 }
103
104
105 int InsetCaption::latex(Buffer const * buf, ostream & os,
106                         LatexRunParams const & runparams) const
107 {
108         // This is a bit too simplistic to take advantage of
109         // caption options we must add more later. (Lgb)
110         // This code is currently only able to handle the simple
111         // \caption{...}, later we will make it take advantage
112         // of the one of the caption packages. (Lgb)
113         ostringstream ost;
114         int const l = InsetText::latex(buf, ost, runparams);
115         os << "\\caption{" << ost.str() << "}\n";
116         return l + 1;
117 }
118
119
120 int InsetCaption::ascii(Buffer const * /*buf*/,
121                         ostream & /*os*/, int /*linelen*/) const
122 {
123         // FIX: Implement me!
124         return 0;
125 }
126
127
128 int InsetCaption::docbook(Buffer const * buf, ostream & os, bool mixcont) const
129 {
130         int ret;
131         os << "<title>";
132         ret = InsetText::docbook(buf, os, mixcont);
133         os << "</title>\n";
134         return ret;
135 }