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