]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Rewrite the label numbering code.
[lyx.git] / src / insets / InsetCaption.cpp
1 /**
2  * \file InsetCaption.cpp
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 "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "Color.h"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TocBackend.h"
34
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
37
38 #include "support/lstrings.h"
39 #include "support/convert.h"
40
41 #include <sstream>
42
43
44 using std::auto_ptr;
45 using std::endl;
46 using std::string;
47 using std::ostream;
48
49
50 namespace lyx {
51
52 using support::bformat;
53
54 InsetCaption::InsetCaption(InsetCaption const & ic)
55         : InsetText(ic), textclass_(ic.textclass_)
56 {
57         setAutoBreakRows(true);
58         setDrawFrame(true);
59         setFrameColor(Color::captionframe);
60 }
61
62 InsetCaption::InsetCaption(BufferParams const & bp)
63         : InsetText(bp), textclass_(bp.getTextClass())
64 {
65         setAutoBreakRows(true);
66         setDrawFrame(true);
67         setFrameColor(Color::captionframe);
68 }
69
70
71 void InsetCaption::write(Buffer const & buf, ostream & os) const
72 {
73         os << "Caption\n";
74         text_.write(buf, os);
75 }
76
77
78 void InsetCaption::read(Buffer const & buf, Lexer & lex)
79 {
80 #if 0
81         // We will enably this check again when the compability
82         // code is removed from Buffer::Read (Lgb)
83         string const token = lex.GetString();
84         if (token != "Caption") {
85                 lyxerr << "InsetCaption::Read: consistency check failed."
86                        << endl;
87         }
88 #endif
89         InsetText::read(buf, lex);
90 }
91
92
93 docstring const InsetCaption::editMessage() const
94 {
95         return _("Opened Caption Inset");
96 }
97
98
99 void InsetCaption::cursorPos(BufferView const & bv,
100                 CursorSlice const & sl, bool boundary, int & x, int & y) const
101 {
102         InsetText::cursorPos(bv, sl, boundary, x, y);
103         x += labelwidth_;
104 }
105
106
107 void InsetCaption::setCustomLabel(docstring const & label)
108 {
109         if (!support::isAscii(label) || label.empty())
110                 // This must be a user defined layout. We cannot translate
111                 // this, since gettext accepts only ascii keys.
112                 custom_label_ = label;
113         else
114                 custom_label_ = _(to_ascii(label));
115 }
116
117
118 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
119 {
120         if (type_.empty())
121                 return;
122
123         ParConstIterator pit = par_const_iterator_begin(*this);
124
125         Toc & toc = toclist[type_];
126         docstring const str = full_label_ + ". " + pit->asString(buf, false);
127         toc.push_back(TocItem(pit, 0, str));
128 }
129
130
131 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         int const width_offset = TEXT_TO_INSET_OFFSET / 2;
134         mi.base.textwidth -= width_offset;
135
136         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
137         // add some space to separate the label from the inset text
138         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
139         dim.wid = labelwidth_;
140         Dimension textdim;
141         InsetText::metrics(mi, textdim);
142         // Correct for button width, and re-fit
143         mi.base.textwidth -= dim.wid;
144         InsetText::metrics(mi, textdim);
145         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
146         dim.asc = textdim.asc;
147         dim.wid += textdim.wid;
148         dim.asc += TEXT_TO_INSET_OFFSET;
149         dim.des += TEXT_TO_INSET_OFFSET;
150         dim.wid += width_offset;
151         mi.base.textwidth += width_offset;
152         bool const changed = dim_ != dim;
153         dim_ = dim;
154         return changed;
155 }
156
157
158 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
159 {
160         // We must draw the label, we should get the label string
161         // from the enclosing float inset.
162         // The question is: Who should draw the label, the caption inset,
163         // the text inset or the paragraph?
164         // We should also draw the float number (Lgb)
165
166         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
167
168         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
169         // add some space to separate the label from the inset text
170         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
171         InsetText::draw(pi, x + labelwidth_, y);
172         setPosCache(pi, x, y);
173 }
174
175
176 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
177 {
178         InsetText::drawSelection(pi, x + labelwidth_, y);
179 }
180
181
182 void InsetCaption::edit(Cursor & cur, bool left)
183 {
184         cur.push(*this);
185         InsetText::edit(cur, left);
186 }
187
188
189 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
190 {
191         cur.push(*this);
192         return InsetText::editXY(cur, x, y);
193 }
194
195
196 bool InsetCaption::insetAllowed(Inset::Code code) const
197 {
198         switch (code) {
199         case FLOAT_CODE:
200         case TABULAR_CODE:
201         case WRAP_CODE:
202         case CAPTION_CODE:
203         case PAGEBREAK_CODE:
204                 return false;
205         default:
206                 return InsetText::insetAllowed(code);
207         }
208 }
209
210
211 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
212         FuncStatus & status) const
213 {
214         switch (cmd.action) {
215
216         case LFUN_BREAK_PARAGRAPH:
217         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
218         case LFUN_BREAK_PARAGRAPH_SKIP:
219                 status.enabled(false);
220                 return true;
221
222         case LFUN_OPTIONAL_INSERT:
223                 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
224                 return true;
225
226         default:
227                 return InsetText::getStatus(cur, cmd, status);
228         }
229 }
230
231
232 int InsetCaption::latex(Buffer const & buf, odocstream & os,
233                         OutputParams const & runparams_in) const
234 {
235         // This is a bit too simplistic to take advantage of
236         // caption options we must add more later. (Lgb)
237         // This code is currently only able to handle the simple
238         // \caption{...}, later we will make it take advantage
239         // of the one of the caption packages. (Lgb)
240         OutputParams runparams = runparams_in;
241         // FIXME: actually, it is moving only when there is no
242         // optional argument.
243         runparams.moving_arg = true;
244         os << "\\caption";
245         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
246         os << '{';
247         l += InsetText::latex(buf, os, runparams);
248         os << "}\n";
249         runparams_in.encoding = runparams.encoding;
250         return l + 1;
251 }
252
253
254 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
255                             OutputParams const & runparams) const
256 {
257         os << '[' << full_label_ << "\n";
258         InsetText::plaintext(buf, os, runparams);
259         os << "\n]";
260
261         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
262 }
263
264
265 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
266                           OutputParams const & runparams) const
267 {
268         int ret;
269         os << "<title>";
270         ret = InsetText::docbook(buf, os, runparams);
271         os << "</title>\n";
272         return ret;
273 }
274
275
276 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
277                         OutputParams const & runparams) const
278 {
279         return InsetText::latex(buf, os, runparams);
280 }
281
282
283 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
284                         OutputParams const & runparams) const
285 {
286         return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
287 }
288
289
290 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
291 {
292         TextClass const & tclass = buf.params().getTextClass();
293         Counters & cnts = tclass.counters();
294         string const & type = cnts.current_float();
295         if (type.empty())
296                 full_label_ = buf.B_("Senseless!!! ");
297         else {
298                 // FIXME: life would be _much_ simpler if listings was
299                 // listed in Floating.
300                 docstring name;
301                 if (type == "listing")
302                         name = buf.B_("Listing");
303                 else
304                         name = buf.B_(tclass.floats().getType(type).name());
305                 if (cnts.hasCounter(from_utf8(type))) {
306                         cnts.step(from_utf8(type));
307                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
308                                               name, 
309                                               convert<docstring>(cnts.value(from_utf8(type))));
310                 } else
311                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
312         }
313
314         // Do the real work now.
315         InsetText::updateLabels(buf, it);
316 }
317
318
319 auto_ptr<Inset> InsetCaption::doClone() const
320 {
321         return auto_ptr<Inset>(new InsetCaption(*this));
322 }
323
324
325 } // namespace lyx