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