]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
* Inset:
[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 "Counters.h"
20 #include "Cursor.h"
21 #include "BufferView.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "Color.h"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TocBackend.h"
34
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
37
38 #include "support/lstrings.h"
39
40 #include <sstream>
41
42
43 using std::endl;
44 using std::string;
45 using std::ostream;
46
47
48 namespace lyx {
49
50
51 InsetCaption::InsetCaption(InsetCaption const & ic)
52         : InsetText(ic), textclass_(ic.textclass_)
53 {
54         setAutoBreakRows(true);
55         setDrawFrame(true);
56         setFrameColor(Color::captionframe);
57 }
58
59 InsetCaption::InsetCaption(BufferParams const & bp)
60         : InsetText(bp), textclass_(bp.getTextClass())
61 {
62         setAutoBreakRows(true);
63         setDrawFrame(true);
64         setFrameColor(Color::captionframe);
65 }
66
67
68 void InsetCaption::write(Buffer const & buf, ostream & os) const
69 {
70         os << "Caption\n";
71         text_.write(buf, os);
72 }
73
74
75 void InsetCaption::read(Buffer const & buf, 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         string const token = lex.GetString();
81         if (token != "Caption") {
82                 lyxerr << "InsetCaption::Read: consistency check failed."
83                        << endl;
84         }
85 #endif
86         InsetText::read(buf, lex);
87 }
88
89
90 docstring const InsetCaption::editMessage() const
91 {
92         return _("Opened Caption Inset");
93 }
94
95
96 void InsetCaption::cursorPos(BufferView const & bv,
97                 CursorSlice const & sl, bool boundary, int & x, int & y) const
98 {
99         InsetText::cursorPos(bv, sl, boundary, x, y);
100         x += labelwidth_;
101 }
102
103
104 void InsetCaption::setCustomLabel(docstring const & label)
105 {
106         if (!support::isAscii(label) || label.empty())
107                 // This must be a user defined layout. We cannot translate
108                 // this, since gettext accepts only ascii keys.
109                 custom_label_ = label;
110         else
111                 custom_label_ = _(to_ascii(label));
112 }
113
114
115 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
116 {
117         if (type_.empty())
118                 return;
119
120         ParConstIterator pit = par_const_iterator_begin(*this);
121
122         Toc & toc = toclist[type_];
123         docstring const str = full_label_ + ". " + pit->asString(buf, false);
124         toc.push_back(TocItem(pit, 0, str));
125 }
126
127
128 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
129 {
130         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
131         // add some space to separate the label from the inset text
132         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
133         dim.wid = labelwidth_;
134         Dimension textdim;
135         // Correct for button and label width
136         mi.base.textwidth -= dim.wid;
137         InsetText::metrics(mi, textdim);
138         mi.base.textwidth += dim.wid;
139         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
140         dim.asc = textdim.asc;
141         dim.wid += textdim.wid;
142 }
143
144
145 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
146 {
147         // We must draw the label, we should get the label string
148         // from the enclosing float inset.
149         // The question is: Who should draw the label, the caption inset,
150         // the text inset or the paragraph?
151         // We should also draw the float number (Lgb)
152
153         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
154
155         pi.pain.text(x, y, full_label_, pi.base.font);
156         InsetText::draw(pi, x + labelwidth_, y);
157 }
158
159
160 void InsetCaption::edit(Cursor & cur, bool left)
161 {
162         cur.push(*this);
163         InsetText::edit(cur, left);
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(Inset::Code code) const
175 {
176         switch (code) {
177         case FLOAT_CODE:
178         case TABULAR_CODE:
179         case WRAP_CODE:
180         case CAPTION_CODE:
181         case PAGEBREAK_CODE:
182                 return false;
183         default:
184                 return InsetText::insetAllowed(code);
185         }
186 }
187
188
189 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
190         FuncStatus & status) const
191 {
192         switch (cmd.action) {
193
194         case LFUN_BREAK_PARAGRAPH:
195         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
196         case LFUN_BREAK_PARAGRAPH_SKIP:
197                 status.enabled(false);
198                 return true;
199
200         case LFUN_OPTIONAL_INSERT:
201                 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
202                 return true;
203
204         default:
205                 return InsetText::getStatus(cur, cmd, status);
206         }
207 }
208
209
210 int InsetCaption::latex(Buffer const & buf, odocstream & os,
211                         OutputParams const & runparams_in) const
212 {
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(buf, paragraphs()[0], os, runparams, 1);
224         os << '{';
225         l += InsetText::latex(buf, os, runparams);
226         os << "}\n";
227         runparams_in.encoding = runparams.encoding;
228         return l + 1;
229 }
230
231
232 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
233                             OutputParams const & runparams) const
234 {
235         os << '[' << full_label_ << "\n";
236         InsetText::plaintext(buf, os, runparams);
237         os << "\n]";
238
239         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
240 }
241
242
243 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
244                           OutputParams const & runparams) const
245 {
246         int ret;
247         os << "<title>";
248         ret = InsetText::docbook(buf, os, runparams);
249         os << "</title>\n";
250         return ret;
251 }
252
253
254 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
255                         OutputParams const & runparams) const
256 {
257         return InsetText::latex(buf, os, runparams);
258 }
259
260
261 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
262                         OutputParams const & runparams) const
263 {
264         return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
265 }
266
267
268 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
269 {
270         using support::bformat;
271         TextClass const & tclass = buf.params().getTextClass();
272         Counters & cnts = tclass.counters();
273         string const & type = cnts.current_float();
274         if (type.empty())
275                 full_label_ = buf.B_("Senseless!!! ");
276         else {
277                 // FIXME: life would be _much_ simpler if listings was
278                 // listed in Floating.
279                 docstring name;
280                 if (type == "listing")
281                         name = buf.B_("Listing");
282                 else
283                         name = buf.B_(tclass.floats().getType(type).name());
284                 if (cnts.hasCounter(from_utf8(type))) {
285                         cnts.step(from_utf8(type));
286                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
287                                               name, 
288                                               cnts.theCounter(from_utf8(type)));
289                 } else
290                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
291         }
292
293         // Do the real work now.
294         InsetText::updateLabels(buf, it);
295 }
296
297
298 Inset * InsetCaption::clone() const
299 {
300         return new InsetCaption(*this);
301 }
302
303
304 } // namespace lyx