]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
c8b774c0bcfde857b7e3adb58ef39b6d0b80dc10
[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 "BufferView.h"
20 #include "Counters.h"
21 #include "Cursor.h"
22 #include "Dimension.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "InsetList.h"
28 #include "Language.h"
29 #include "MetricsInfo.h"
30 #include "output_latex.h"
31 #include "output_xhtml.h"
32 #include "OutputParams.h"
33 #include "Paragraph.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
36
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
39
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42
43 #include <sstream>
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49
50
51 InsetCaption::InsetCaption(Buffer * buf)
52         : InsetText(buf, InsetText::PlainLayout)
53 {
54         setAutoBreakRows(true);
55         setDrawFrame(true);
56         setFrameColor(Color_captionframe);
57 }
58
59
60 void InsetCaption::write(ostream & os) const
61 {
62         os << "Caption\n";
63         text().write(os);
64 }
65
66
67 docstring InsetCaption::name() const
68 {
69         if (type_.empty())
70                 return from_ascii("Caption");
71         return from_utf8("Caption:" + type_);
72 }
73
74
75 void InsetCaption::read(Lexer & lex)
76 {
77 #if 0
78         // We will enably this check again when the compability
79         // code is removed from Buffer::Read (Lgb)
80         lex.setContext("InsetCaption::Read: consistency check");
81         lex >> "Caption";
82 #endif
83         InsetText::read(lex);
84 }
85
86
87 void InsetCaption::cursorPos(BufferView const & bv,
88                 CursorSlice const & sl, bool boundary, int & x, int & y) const
89 {
90         InsetText::cursorPos(bv, sl, boundary, x, y);
91         x += labelwidth_;
92 }
93
94
95 void InsetCaption::setCustomLabel(docstring const & label)
96 {
97         if (!isAscii(label) || label.empty())
98                 // This must be a user defined layout. We cannot translate
99                 // this, since gettext accepts only ascii keys.
100                 custom_label_ = label;
101         else
102                 custom_label_ = _(to_ascii(label));
103 }
104
105
106 void InsetCaption::addToToc(DocIterator const & cpit)
107 {
108         if (type_.empty())
109                 return;
110
111         DocIterator pit = cpit;
112         pit.push_back(CursorSlice(*this));
113
114         Toc & toc = buffer().tocBackend().toc(type_);
115         docstring const str = full_label_ + ". " + text().getPar(0).asString();
116         toc.push_back(TocItem(pit, 0, str));
117
118         // Proceed with the rest of the inset.
119         InsetText::addToToc(cpit);
120 }
121
122
123 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
124 {
125         FontInfo tmpfont = mi.base.font;
126         mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
127         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
128         // add some space to separate the label from the inset text
129         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
130         dim.wid = labelwidth_;
131         Dimension textdim;
132         // Correct for button and label width
133         mi.base.textwidth -= dim.wid;
134         InsetText::metrics(mi, textdim);
135         mi.base.font = tmpfont;
136         mi.base.textwidth += dim.wid;
137         dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
138         dim.asc = textdim.asc;
139         dim.wid += textdim.wid;
140 }
141
142
143 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
144 {
145         // We must draw the label, we should get the label string
146         // from the enclosing float inset.
147         // The question is: Who should draw the label, the caption inset,
148         // the text inset or the paragraph?
149         // We should also draw the float number (Lgb)
150
151         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
152
153         FontInfo tmpfont = pi.base.font;
154         pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
155         pi.pain.text(x, y, full_label_, pi.base.font);
156         InsetText::draw(pi, x + labelwidth_, y);
157         pi.base.font = tmpfont;
158 }
159
160
161 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
162 {
163         cur.push(*this);
164         InsetText::edit(cur, front, entry_from);
165 }
166
167
168 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
169 {
170         cur.push(*this);
171         return InsetText::editXY(cur, x, y);
172 }
173
174
175 bool InsetCaption::insetAllowed(InsetCode code) const
176 {
177         switch (code) {
178         // code that is not allowed in a caption
179         case CAPTION_CODE:
180         case FLOAT_CODE:
181         case FOOT_CODE:
182         case NEWPAGE_CODE:
183         case MARGIN_CODE:
184         case MATHMACRO_CODE:
185         case TABULAR_CODE:
186         case WRAP_CODE:
187                 return false;
188         default:
189                 return InsetText::insetAllowed(code);
190         }
191 }
192
193
194 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
195         FuncStatus & status) const
196 {
197         switch (cmd.action()) {
198
199         case LFUN_BREAK_PARAGRAPH:
200                 status.setEnabled(false);
201                 return true;
202
203         case LFUN_ARGUMENT_INSERT:
204                 status.setEnabled(cur.paragraph().insetList().find(ARG_CODE) == -1);
205                 return true;
206
207         case LFUN_INSET_TOGGLE:
208                 // pass back to owner
209                 cur.undispatched();
210                 return false;
211
212         default:
213                 return InsetText::getStatus(cur, cmd, status);
214         }
215 }
216
217
218 int InsetCaption::latex(odocstream & os,
219                         OutputParams const & runparams_in) const
220 {
221         if (runparams_in.inFloat == OutputParams::SUBFLOAT)
222                 // caption is output as an optional argument
223                 return 0;
224         // This is a bit too simplistic to take advantage of
225         // caption options we must add more later. (Lgb)
226         // This code is currently only able to handle the simple
227         // \caption{...}, later we will make it take advantage
228         // of the one of the caption packages. (Lgb)
229         OutputParams runparams = runparams_in;
230         // FIXME: actually, it is moving only when there is no
231         // optional argument.
232         runparams.moving_arg = true;
233         os << "\\caption";
234         int l = latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
235         os << '{';
236         l += InsetText::latex(os, runparams);
237         os << "}\n";
238         runparams_in.encoding = runparams.encoding;
239         return l + 1;
240 }
241
242
243 int InsetCaption::plaintext(odocstream & os,
244                             OutputParams const & runparams) const
245 {
246         os << '[' << full_label_ << "\n";
247         InsetText::plaintext(os, runparams);
248         os << "\n]";
249
250         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
251 }
252
253
254 int InsetCaption::docbook(odocstream & os,
255                           OutputParams const & runparams) const
256 {
257         int ret;
258         os << "<title>";
259         ret = InsetText::docbook(os, runparams);
260         os << "</title>\n";
261         return ret;
262 }
263
264
265 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
266 {
267         if (rp.html_disable_captions)
268                 return docstring();
269         string attr = "class='float-caption";
270         if (!type_.empty())
271                 attr += " float-caption-" + type_;
272         attr += "'";
273         xs << html::StartTag("div", attr);
274         docstring def = getCaptionAsHTML(xs, rp);
275         xs << html::EndTag("div");
276         return def;
277 }
278
279
280 int InsetCaption::getArgument(odocstream & os,
281                         OutputParams const & runparams) const
282 {
283         return InsetText::latex(os, runparams);
284 }
285
286
287 int InsetCaption::getOptArg(odocstream & os,
288                         OutputParams const & runparams) const
289 {
290         return latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
291 }
292
293
294 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
295                         OutputParams const & runparams) const
296 {
297         os << full_label_ << ' ';
298         return InsetText::plaintext(os, runparams);
299 }
300
301
302 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
303                         OutputParams const & runparams) const
304 {
305         xs << full_label_ << ' ';
306         InsetText::XHTMLOptions const opts = 
307                 InsetText::WriteLabel | InsetText::WriteInnerTag;
308         return InsetText::insetAsXHTML(xs, runparams, opts);
309 }
310
311
312 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
313 {
314         Buffer const & master = *buffer().masterBuffer();
315         DocumentClass const & tclass = master.params().documentClass();
316         string const & lang = it.paragraph().getParLanguage(master.params())->code();
317         Counters & cnts = tclass.counters();
318         string const & type = cnts.current_float();
319         if (utype == OutputUpdate) {
320                 // counters are local to the caption
321                 cnts.saveLastCounter();
322         }
323         // Memorize type for addToToc().
324         type_ = type;
325         if (type.empty())
326                 full_label_ = master.B_("Senseless!!! ");
327         else {
328                 // FIXME: life would be _much_ simpler if listings was
329                 // listed in Floating.
330                 docstring name;
331                 if (type == "listing")
332                         name = master.B_("Listing");
333                 else
334                         name = master.B_(tclass.floats().getType(type).name());
335                 docstring counter = from_utf8(type);
336                 if (cnts.isSubfloat()) {
337                         counter = "sub-" + from_utf8(type);
338                         name = bformat(_("Sub-%1$s"),
339                                        master.B_(tclass.floats().getType(type).name()));
340                 }
341                 if (cnts.hasCounter(counter)) {
342                         cnts.step(counter, utype);
343                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
344                                               name,
345                                               cnts.theCounter(counter, lang));
346                 } else
347                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
348         }
349
350         // Do the real work now.
351         InsetText::updateBuffer(it, utype);
352         if (utype == OutputUpdate)
353                 cnts.restoreLastCounter();
354 }
355
356
357 } // namespace lyx