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