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