]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
hopefully fix tex2lyx linking.
[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 "bufferparams.h"
19 #include "counters.h"
20 #include "cursor.h"
21 #include "BufferView.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "gettext.h"
25 #include "LColor.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/lstrings.h"
33 #include "support/convert.h"
34
35 #include <sstream>
36
37
38 namespace lyx {
39
40 using support::bformat;
41
42 using std::auto_ptr;
43 using std::endl;
44 using std::string;
45 using std::ostream;
46
47
48 InsetCaption::InsetCaption(BufferParams const & bp)
49         : InsetText(bp), textclass_(bp.getLyXTextClass())
50 {
51         setAutoBreakRows(true);
52         setDrawFrame(true);
53         setFrameColor(LColor::captionframe);
54 }
55
56
57 void InsetCaption::write(Buffer const & buf, ostream & os) const
58 {
59         os << "Caption\n";
60         text_.write(buf, os);
61 }
62
63
64 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
65 {
66 #if 0
67         // We will enably this check again when the compability
68         // code is removed from Buffer::Read (Lgb)
69         string const token = lex.GetString();
70         if (token != "Caption") {
71                 lyxerr << "InsetCaption::Read: consistency check failed."
72                        << endl;
73         }
74 #endif
75         InsetText::read(buf, lex);
76 }
77
78
79 docstring const InsetCaption::editMessage() const
80 {
81         return _("Opened Caption Inset");
82 }
83
84
85 void InsetCaption::cursorPos(BufferView const & bv,
86                 CursorSlice const & sl, bool boundary, int & x, int & y) const
87 {
88         InsetText::cursorPos(bv, sl, boundary, x, y);
89         x += labelwidth_;
90 }
91
92
93 void InsetCaption::setLabel(LCursor & cur) const
94 {
95         // Set caption label _only_ if the cursor is in _this_ float:
96         if (cur.top().text() == &text_) {
97                 string s;
98                 size_t i = cur.depth();
99                         while (i > 0) {
100                                 --i;
101                                 InsetBase * const in = &cur[i].inset();
102                                 if (in->lyxCode() == InsetBase::FLOAT_CODE
103                                     || in->lyxCode() == InsetBase::WRAP_CODE) {
104                                         s = to_utf8(in->getInsetName());
105                                         break;
106                                 }
107                         }
108                 Floating const & fl = textclass_.floats().getType(s);
109                 s = fl.name();
110                 docstring num;
111                 if (s.empty())
112                         s = "Senseless";
113                 else
114                         num = convert<docstring>(counter_);
115
116                 // Generate the label
117                 label = bformat(from_ascii("%1$s %2$s:"), _(s), num);
118         }
119 }
120
121
122 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
123 {
124         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
125         LCursor cur = mi.base.bv->cursor();
126         setLabel(cur);
127         labelwidth_ = theFontMetrics(mi.base.font).width(label);
128         dim.wid = labelwidth_;
129         Dimension textdim;
130         InsetText::metrics(mi, textdim);
131         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
132         dim.asc = textdim.asc;
133         dim.wid += textdim.wid;
134         dim.asc += TEXT_TO_INSET_OFFSET;
135         dim.des += TEXT_TO_INSET_OFFSET;
136         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
137         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
138         dim_ = dim;
139 }
140
141
142 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
143 {
144         // We must draw the label, we should get the label string
145         // from the enclosing float inset.
146         // The question is: Who should draw the label, the caption inset,
147         // the text inset or the paragraph?
148         // We should also draw the float number (Lgb)
149
150         // See if we can find the name of the float this caption
151         // belongs to.
152         LCursor cur = pi.base.bv->cursor();
153         setLabel(cur);
154         labelwidth_ = pi.pain.text(x, y, label, pi.base.font);
155         InsetText::draw(pi, x + labelwidth_, y);
156         setPosCache(pi, x, y);
157 }
158
159
160 void InsetCaption::edit(LCursor & cur, bool left)
161 {
162         cur.push(*this);
163         InsetText::edit(cur, left);
164 }
165
166
167 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
168 {
169         cur.push(*this);
170         return InsetText::editXY(cur, x, y);
171 }
172
173
174 int InsetCaption::latex(Buffer const & buf, odocstream & os,
175                         OutputParams const & runparams) const
176 {
177         // This is a bit too simplistic to take advantage of
178         // caption options we must add more later. (Lgb)
179         // This code is currently only able to handle the simple
180         // \caption{...}, later we will make it take advantage
181         // of the one of the caption packages. (Lgb)
182         odocstringstream ost;
183         int const l = InsetText::latex(buf, ost, runparams);
184         os << "\\caption{" << ost.str() << "}\n";
185         return l + 1;
186 }
187
188
189 int InsetCaption::plaintext(Buffer const & /*buf*/, odocstream & /*os*/,
190                         OutputParams const & /*runparams*/) const
191 {
192         // FIXME: Implement me!
193         return 0;
194 }
195
196
197 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
198                           OutputParams const & runparams) const
199 {
200         int ret;
201         os << "<title>";
202         ret = InsetText::docbook(buf, os, runparams);
203         os << "</title>\n";
204         return ret;
205 }
206
207
208 auto_ptr<InsetBase> InsetCaption::doClone() const
209 {
210         return auto_ptr<InsetBase>(new InsetCaption(*this));
211 }
212
213
214 } // namespace lyx