]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Fix toc generation after rev25096.
[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 "support/gettext.h"
28 #include "InsetList.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/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 const & buf)
51         : InsetText(buf)
52 {
53         setAutoBreakRows(true);
54         setDrawFrame(true);
55         setFrameColor(Color_captionframe);
56         // There will always be only one
57         paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
58 }
59
60
61 void InsetCaption::write(ostream & os) const
62 {
63         os << "Caption\n";
64         text_.write(buffer(), os);
65 }
66
67
68 void InsetCaption::read(Lexer & lex)
69 {
70 #if 0
71         // We will enably this check again when the compability
72         // code is removed from Buffer::Read (Lgb)
73         lex.setContext("InsetCaption::Read: consistency check");
74         lex >> "Caption";
75 #endif
76         InsetText::read(lex);
77 }
78
79
80 docstring InsetCaption::editMessage() const
81 {
82         return _("Opened Caption Inset");
83 }
84
85
86 void InsetCaption::cursorPos(BufferView const & bv,
87                 CursorSlice const & sl, bool boundary, int & x, int & y) const
88 {
89         InsetText::cursorPos(bv, sl, boundary, x, y);
90         x += labelwidth_;
91 }
92
93
94 void InsetCaption::setCustomLabel(docstring const & label)
95 {
96         if (!isAscii(label) || label.empty())
97                 // This must be a user defined layout. We cannot translate
98                 // this, since gettext accepts only ascii keys.
99                 custom_label_ = label;
100         else
101                 custom_label_ = _(to_ascii(label));
102 }
103
104
105 void InsetCaption::addToToc(DocIterator const & cpit)
106 {
107         if (type_.empty())
108                 return;
109
110         DocIterator pit = cpit;
111         pit.push_back(CursorSlice(*this));
112
113         Toc & toc = buffer().tocBackend().toc(type_);
114         docstring const str = full_label_ + ". " + text_.getPar(0).asString();
115         toc.push_back(TocItem(pit, 0, str));
116
117         // Proceed with the rest of the inset.
118         InsetText::addToToc(cpit);
119 }
120
121
122 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
123 {
124         FontInfo tmpfont = mi.base.font;
125         mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
126         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
127         // add some space to separate the label from the inset text
128         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
129         dim.wid = labelwidth_;
130         Dimension textdim;
131         // Correct for button and label width
132         mi.base.textwidth -= dim.wid;
133         InsetText::metrics(mi, textdim);
134         mi.base.font = tmpfont;
135         mi.base.textwidth += dim.wid;
136         dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
137         dim.asc = textdim.asc;
138         dim.wid += textdim.wid;
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.pain.text(x, y, full_label_, pi.base.font);
155         InsetText::draw(pi, x + labelwidth_, y);
156         pi.base.font = tmpfont;
157 }
158
159
160 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
161 {
162         cur.push(*this);
163         InsetText::edit(cur, front, entry_from);
164 }
165
166
167 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
168 {
169         cur.push(*this);
170         return InsetText::editXY(cur, x, y);
171 }
172
173
174 bool InsetCaption::insetAllowed(InsetCode code) const
175 {
176         switch (code) {
177         case FLOAT_CODE:
178         case TABULAR_CODE:
179         case WRAP_CODE:
180         case CAPTION_CODE:
181         case NEWPAGE_CODE:
182         case MATHMACRO_CODE:
183                 return false;
184         default:
185                 return InsetText::insetAllowed(code);
186         }
187 }
188
189
190 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
191         FuncStatus & status) const
192 {
193         switch (cmd.action) {
194
195         case LFUN_BREAK_PARAGRAPH:
196         case LFUN_BREAK_PARAGRAPH_SKIP:
197                 status.setEnabled(false);
198                 return true;
199
200         case LFUN_OPTIONAL_INSERT:
201                 status.setEnabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
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 int InsetCaption::latex(odocstream & os,
216                         OutputParams const & runparams_in) const
217 {
218         if (in_subfloat_)
219                 // caption is output as an optional argument
220                 return 0;
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         int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
232         os << '{';
233         l += InsetText::latex(os, runparams);
234         os << "}\n";
235         runparams_in.encoding = runparams.encoding;
236         return l + 1;
237 }
238
239
240 int InsetCaption::plaintext(odocstream & os,
241                             OutputParams const & runparams) const
242 {
243         os << '[' << full_label_ << "\n";
244         InsetText::plaintext(os, runparams);
245         os << "\n]";
246
247         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
248 }
249
250
251 int InsetCaption::docbook(odocstream & os,
252                           OutputParams const & runparams) const
253 {
254         int ret;
255         os << "<title>";
256         ret = InsetText::docbook(os, runparams);
257         os << "</title>\n";
258         return ret;
259 }
260
261
262 int InsetCaption::getArgument(odocstream & os,
263                         OutputParams const & runparams) const
264 {
265         return InsetText::latex(os, runparams);
266 }
267
268
269 int InsetCaption::getOptArg(odocstream & os,
270                         OutputParams const & runparams) const
271 {
272         return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
273 }
274
275
276 int InsetCaption::getCaptionText(odocstream & os,
277                         OutputParams const & runparams) const
278 {
279         os << full_label_ << ' ';
280         return InsetText::plaintext(os, runparams);
281 }
282
283
284 void InsetCaption::updateLabels(ParIterator const & it)
285 {
286         Buffer const & master = *buffer().masterBuffer();
287         DocumentClass const & tclass = master.params().documentClass();
288         Counters & cnts = tclass.counters();
289         string const & type = cnts.current_float();
290         // Memorize type for addToToc().
291         type_ = type;
292         in_subfloat_ = cnts.isSubfloat();
293         if (type.empty())
294                 full_label_ = master.B_("Senseless!!! ");
295         else {
296                 // FIXME: life would be _much_ simpler if listings was
297                 // listed in Floating.
298                 docstring name;
299                 if (type == "listing")
300                         name = master.B_("Listing");
301                 else
302                         name = master.B_(tclass.floats().getType(type).name());
303                 docstring counter = from_utf8(type);
304                 if (in_subfloat_) {
305                         counter = "sub-" + from_utf8(type);
306                         name = bformat(_("Sub-%1$s"),
307                                        master.B_(tclass.floats().getType(type).name()));
308                 }
309                 if (cnts.hasCounter(counter)) {
310                         cnts.step(counter);
311                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
312                                               name,
313                                               cnts.theCounter(counter));
314                 } else
315                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
316         }
317
318         // Do the real work now.
319         InsetText::updateLabels(it);
320 }
321
322
323 } // namespace lyx