]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Fix lists of figures and tables. Prepare the code for user defined captions.
[lyx.git] / src / insets / insetcaption.C
1 /**
2  * \file insetcaption.C
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 "LColor.h"
28 #include "metricsinfo.h"
29 #include "output_latex.h"
30 #include "paragraph.h"
31 #include "TocBackend.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "support/lstrings.h"
37 #include "support/convert.h"
38
39 #include <sstream>
40
41
42 using std::auto_ptr;
43 using std::endl;
44 using std::string;
45 using std::ostream;
46
47
48 namespace lyx {
49
50 using support::bformat;
51
52 InsetCaption::InsetCaption(BufferParams const & bp)
53         : InsetText(bp), textclass_(bp.getLyXTextClass())
54 {
55         setAutoBreakRows(true);
56         setDrawFrame(true);
57         setFrameColor(LColor::captionframe);
58 }
59
60
61 void InsetCaption::write(Buffer const & buf, ostream & os) const
62 {
63         os << "Caption\n";
64         text_.write(buf, os);
65 }
66
67
68 void InsetCaption::read(Buffer const & buf, LyXLex & 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(buf, lex);
80 }
81
82
83 docstring const 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 (!support::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(TocList & toclist, Buffer const & buf) const
109 {
110         if (type_.empty())
111                 return;
112
113         ParConstIterator pit = par_const_iterator_begin(*this);
114
115         Toc & toc = toclist[type_];
116         docstring const str = convert<docstring>(counter_)
117                 + ". " + pit->asString(buf, false);
118         toc.push_back(TocItem(pit, 0, str));
119 }
120
121
122 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
123 {
124         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
125         if (type_.empty())
126                 full_label_ = _("Senseless!!! ");
127         else {
128                 docstring const number = convert<docstring>(counter_);
129                 docstring label = custom_label_.empty()? _(type_): custom_label_;
130                 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
131         }
132         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
133         dim.wid = labelwidth_;
134         Dimension textdim;
135         InsetText::metrics(mi, textdim);
136         // Correct for button width, and re-fit
137         mi.base.textwidth -= dim.wid;
138         InsetText::metrics(mi, textdim);
139         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
140         dim.asc = textdim.asc;
141         dim.wid += textdim.wid;
142         dim.asc += TEXT_TO_INSET_OFFSET;
143         dim.des += TEXT_TO_INSET_OFFSET;
144         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
146         bool const changed = dim_ != dim;
147         dim_ = dim;
148         return changed;
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.C: setCaption).
161
162         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
163         InsetText::draw(pi, x + labelwidth_, y);
164         setPosCache(pi, x, y);
165 }
166
167
168 void InsetCaption::edit(LCursor & cur, bool left)
169 {
170         cur.push(*this);
171         InsetText::edit(cur, left);
172 }
173
174
175 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
176 {
177         cur.push(*this);
178         return InsetText::editXY(cur, x, y);
179 }
180
181
182 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
183         FuncStatus & status) const
184 {
185         switch (cmd.action) {
186
187         case LFUN_CAPTION_INSERT:
188         case LFUN_FLOAT_INSERT:
189         case LFUN_FLOAT_WIDE_INSERT:
190         case LFUN_WRAP_INSERT:
191         case LFUN_PARAGRAPH_MOVE_UP:
192         case LFUN_PARAGRAPH_MOVE_DOWN:
193         case LFUN_BREAK_PARAGRAPH:
194         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
195         case LFUN_BREAK_PARAGRAPH_SKIP:
196         case LFUN_PARAGRAPH_SPACING:
197         case LFUN_PAGEBREAK_INSERT:
198                 status.enabled(false);
199                 return true;
200
201         default:
202                 return InsetText::getStatus(cur, cmd, status);
203         }
204 }
205
206
207 int InsetCaption::latex(Buffer const & buf, odocstream & os,
208                         OutputParams const & runparams) const
209 {
210         // This is a bit too simplistic to take advantage of
211         // caption options we must add more later. (Lgb)
212         // This code is currently only able to handle the simple
213         // \caption{...}, later we will make it take advantage
214         // of the one of the caption packages. (Lgb)
215         os << "\\caption";
216         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
217         os << '{';
218         l += InsetText::latex(buf, os, runparams);
219         os << "}\n";
220         return l + 1;
221 }
222
223
224 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
225                 OutputParams const & runparams) const
226 {
227         os << full_label_ << ' ';
228         return InsetText::plaintext(buf, os, runparams);
229 }
230
231
232 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
233                           OutputParams const & runparams) const
234 {
235         int ret;
236         os << "<title>";
237         ret = InsetText::docbook(buf, os, runparams);
238         os << "</title>\n";
239         return ret;
240 }
241
242
243 auto_ptr<InsetBase> InsetCaption::doClone() const
244 {
245         return auto_ptr<InsetBase>(new InsetCaption(*this));
246 }
247
248
249 } // namespace lyx