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