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