]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
There was no need to use ColorChanger after all :)
[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_collapsableframe);
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.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
156         pi.pain.text(x, 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_BREAK_PARAGRAPH:
201                 status.setEnabled(false);
202                 return true;
203
204         case LFUN_ARGUMENT_INSERT:
205                 status.setEnabled(cur.paragraph().insetList().find(ARG_CODE) == -1);
206                 return true;
207
208         case LFUN_INSET_TOGGLE:
209                 // pass back to owner
210                 cur.undispatched();
211                 return false;
212
213         default:
214                 return InsetText::getStatus(cur, cmd, status);
215         }
216 }
217
218
219 int InsetCaption::latex(odocstream & os,
220                         OutputParams const & runparams_in) const
221 {
222         if (runparams_in.inFloat == OutputParams::SUBFLOAT)
223                 // caption is output as an optional argument
224                 return 0;
225         // This is a bit too simplistic to take advantage of
226         // caption options we must add more later. (Lgb)
227         // This code is currently only able to handle the simple
228         // \caption{...}, later we will make it take advantage
229         // of the one of the caption packages. (Lgb)
230         OutputParams runparams = runparams_in;
231         // FIXME: actually, it is moving only when there is no
232         // optional argument.
233         runparams.moving_arg = true;
234         os << "\\caption";
235         int l = latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
236         os << '{';
237         l += InsetText::latex(os, runparams);
238         os << "}\n";
239         runparams_in.encoding = runparams.encoding;
240         return l + 1;
241 }
242
243
244 int InsetCaption::plaintext(odocstream & os,
245                             OutputParams const & runparams) const
246 {
247         os << '[' << full_label_ << "\n";
248         InsetText::plaintext(os, runparams);
249         os << "\n]";
250
251         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
252 }
253
254
255 int InsetCaption::docbook(odocstream & os,
256                           OutputParams const & runparams) const
257 {
258         int ret;
259         os << "<title>";
260         ret = InsetText::docbook(os, runparams);
261         os << "</title>\n";
262         return ret;
263 }
264
265
266 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
267 {
268         if (rp.html_disable_captions)
269                 return docstring();
270         string attr = "class='float-caption";
271         if (!type_.empty())
272                 attr += " float-caption-" + type_;
273         attr += "'";
274         xs << html::StartTag("div", attr);
275         docstring def = getCaptionAsHTML(xs, rp);
276         xs << html::EndTag("div");
277         return def;
278 }
279
280
281 int InsetCaption::getArgument(odocstream & os,
282                         OutputParams const & runparams) const
283 {
284         return InsetText::latex(os, runparams);
285 }
286
287
288 int InsetCaption::getOptArg(odocstream & os,
289                         OutputParams const & runparams) const
290 {
291         return latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
292 }
293
294
295 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
296                         OutputParams const & runparams) const
297 {
298         os << full_label_ << ' ';
299         return InsetText::plaintext(os, runparams);
300 }
301
302
303 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
304                         OutputParams const & runparams) const
305 {
306         xs << full_label_ << ' ';
307         InsetText::XHTMLOptions const opts = 
308                 InsetText::WriteLabel | InsetText::WriteInnerTag;
309         return InsetText::insetAsXHTML(xs, runparams, opts);
310 }
311
312
313 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
314 {
315         Buffer const & master = *buffer().masterBuffer();
316         DocumentClass const & tclass = master.params().documentClass();
317         string const & lang = it.paragraph().getParLanguage(master.params())->code();
318         Counters & cnts = tclass.counters();
319         string const & type = cnts.current_float();
320         if (utype == OutputUpdate) {
321                 // counters are local to the caption
322                 cnts.saveLastCounter();
323         }
324         // Memorize type for addToToc().
325         type_ = type;
326         if (type.empty())
327                 full_label_ = master.B_("Senseless!!! ");
328         else {
329                 // FIXME: life would be _much_ simpler if listings was
330                 // listed in Floating.
331                 docstring name;
332                 if (type == "listing")
333                         name = master.B_("Listing");
334                 else
335                         name = master.B_(tclass.floats().getType(type).name());
336                 docstring counter = from_utf8(type);
337                 if (cnts.isSubfloat()) {
338                         counter = "sub-" + from_utf8(type);
339                         name = bformat(_("Sub-%1$s"),
340                                        master.B_(tclass.floats().getType(type).name()));
341                 }
342                 if (cnts.hasCounter(counter)) {
343                         cnts.step(counter, utype);
344                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
345                                               name,
346                                               cnts.theCounter(counter, lang));
347                 } else
348                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
349         }
350
351         // Do the real work now.
352         InsetText::updateBuffer(it, utype);
353         if (utype == OutputUpdate)
354                 cnts.restoreLastCounter();
355 }
356
357
358 } // namespace lyx