]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
* src/insets/InsetCaption.cpp:
[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         string const token = lex.GetString();
74         if (token != "Caption") {
75                 lyxerr << "InsetCaption::Read: consistency check failed."
76                        << endl;
77         }
78 #endif
79         InsetText::read(lex);
80 }
81
82
83 docstring InsetCaption::editMessage() const
84 {
85         return _("Opened Caption Inset");
86 }
87
88
89 void InsetCaption::cursorPos(BufferView const & bv,
90                 CursorSlice const & sl, bool boundary, int & x, int & y) const
91 {
92         InsetText::cursorPos(bv, sl, boundary, x, y);
93         x += labelwidth_;
94 }
95
96
97 void InsetCaption::setCustomLabel(docstring const & label)
98 {
99         if (!isAscii(label) || label.empty())
100                 // This must be a user defined layout. We cannot translate
101                 // this, since gettext accepts only ascii keys.
102                 custom_label_ = label;
103         else
104                 custom_label_ = _(to_ascii(label));
105 }
106
107
108 void InsetCaption::addToToc(ParConstIterator const & cpit) const
109 {
110         if (type_.empty())
111                 return;
112
113         ParConstIterator pit = cpit;
114         pit.push_back(*this);
115
116         Toc & toc = buffer().tocBackend().toc(type_);
117         docstring const str = full_label_ + ". " + pit->asString(false);
118         toc.push_back(TocItem(pit, 0, str));
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.enabled(false);
198                 return true;
199
200         case LFUN_OPTIONAL_INSERT:
201                 status.enabled(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 void InsetCaption::updateLabels(ParIterator const & it)
277 {
278         DocumentClass const & tclass = buffer().params().documentClass();
279         Counters & cnts = tclass.counters();
280         string const & type = cnts.current_float();
281         // Memorize type for addToToc().
282         type_ = type;
283         in_subfloat_ = cnts.isSubfloat();
284         if (type.empty())
285                 full_label_ = buffer().B_("Senseless!!! ");
286         else {
287                 // FIXME: life would be _much_ simpler if listings was
288                 // listed in Floating.
289                 docstring name;
290                 if (type == "listing")
291                         name = buffer().B_("Listing");
292                 else
293                         name = buffer().B_(tclass.floats().getType(type).name());
294                 docstring counter = from_utf8(type);
295                 if (in_subfloat_) {
296                         counter = "sub-" + from_utf8(type);
297                         name = bformat(_("Sub-%1$s"),
298                                        buffer().B_(tclass.floats().getType(type).name()));
299                 }
300                 if (cnts.hasCounter(counter)) {
301                         cnts.step(counter);
302                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
303                                               name,
304                                               cnts.theCounter(counter));
305                 } else
306                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
307         }
308
309         // Do the real work now.
310         InsetText::updateLabels(it);
311 }
312
313
314 } // namespace lyx