]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Enable convertDefault.sh to run even if its executable bit is not set.
[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 namespace lyx::support;
30
31 using std::ostream;
32 using std::endl;
33
34
35 InsetCaption::InsetCaption(BufferParams const & bp)
36         : InsetText(bp)
37 {
38         setAutoBreakRows(true);
39         setDrawFrame(InsetText::LOCKED);
40         setFrameColor(LColor::captionframe);
41 }
42
43
44 void InsetCaption::write(Buffer const & buf, ostream & os) const
45 {
46         os << "Caption\n";
47         writeParagraphData(buf, os);
48 }
49
50
51 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
52 {
53 #if 0
54         // We will enably this check again when the compability
55         // code is removed from Buffer::Read (Lgb)
56         string const token = lex.GetString();
57         if (token != "Caption") {
58                 lyxerr << "InsetCaption::Read: consistency check failed."
59                        << endl;
60         }
61 #endif
62         InsetText::read(buf, lex);
63 }
64
65
66 string const InsetCaption::editMessage() const
67 {
68         return _("Opened Caption Inset");
69 }
70
71
72 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
73 {
74         // We must draw the label, we should get the label string
75         // from the enclosing float inset.
76         // The question is: Who should draw the label, the caption inset,
77         // the text inset or the paragraph?
78         // We should also draw the float number (Lgb)
79
80         // See if we can find the name of the float this caption
81         // belongs to.
82         InsetOld * i1 = owner();
83         InsetOld * i2 = i1 ? i1->owner() : 0;
84         string type;
85         if (i2->lyxCode() == FLOAT_CODE)
86                 type = static_cast<InsetFloat *>(i2)->params().type;
87         else if (i2->lyxCode() == WRAP_CODE)
88                 type = static_cast<InsetWrap *>(i2)->params().type;
89         else
90                 Assert(0);
91
92         FloatList const & floats =
93                 pi.base.bv->buffer()->params.getLyXTextClass().floats();
94         string const fl = i2 ? floats.getType(type).name() : N_("Float");
95
96         // Discover the number...
97         string const num = "#";
98
99         // Generate the label
100         string const label = bformat("%1$s %2$s:", _(fl), num);
101         int const w = font_metrics::width(label, pi.base.font);
102         pi.pain.text(x, y, label, pi.base.font);
103         InsetText::draw(pi, x + w, y);
104 }
105
106
107 int InsetCaption::latex(Buffer const & buf, ostream & os,
108                         LatexRunParams const & runparams) const
109 {
110         // This is a bit too simplistic to take advantage of
111         // caption options we must add more later. (Lgb)
112         // This code is currently only able to handle the simple
113         // \caption{...}, later we will make it take advantage
114         // of the one of the caption packages. (Lgb)
115         ostringstream ost;
116         int const l = InsetText::latex(buf, ost, runparams);
117         os << "\\caption{" << ost.str() << "}\n";
118         return l + 1;
119 }
120
121
122 int InsetCaption::ascii(Buffer const & /*buf*/,
123                         ostream & /*os*/, int /*linelen*/) const
124 {
125         // FIX: Implement me!
126         return 0;
127 }
128
129
130 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
131 {
132         int ret;
133         os << "<title>";
134         ret = InsetText::docbook(buf, os, mixcont);
135         os << "</title>\n";
136         return ret;
137 }