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