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