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