3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "insetcaption.h"
14 #include "insetfloat.h"
15 #include "insetwrap.h"
18 #include "bufferparams.h"
21 #include "BufferView.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "output_latex.h"
30 #include "paragraph.h"
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
35 #include "support/lstrings.h"
36 #include "support/convert.h"
43 using support::bformat;
51 InsetCaption::InsetCaption(BufferParams const & bp)
52 : InsetText(bp), textclass_(bp.getLyXTextClass()), counter_(-1)
54 setAutoBreakRows(true);
56 setFrameColor(LColor::captionframe);
60 void InsetCaption::write(Buffer const & buf, ostream & os) const
67 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
70 // We will enably this check again when the compability
71 // code is removed from Buffer::Read (Lgb)
72 string const token = lex.GetString();
73 if (token != "Caption") {
74 lyxerr << "InsetCaption::Read: consistency check failed."
78 InsetText::read(buf, lex);
82 docstring const InsetCaption::editMessage() const
84 return _("Opened Caption Inset");
88 void InsetCaption::cursorPos(BufferView const & bv,
89 CursorSlice const & sl, bool boundary, int & x, int & y) const
91 InsetText::cursorPos(bv, sl, boundary, x, y);
96 void InsetCaption::setLabel(docstring const & label)
98 label_ = _(to_ascii(label));
102 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
104 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
106 full_label_ = _("Senseless!!! ");
108 docstring const number = convert<docstring>(counter_);
109 full_label_ = bformat(from_ascii("%1$s %2$s:"), label_, number);
111 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
112 dim.wid = labelwidth_;
114 InsetText::metrics(mi, textdim);
115 // Correct for button width, and re-fit
116 mi.base.textwidth -= dim.wid;
117 InsetText::metrics(mi, textdim);
118 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
119 dim.asc = textdim.asc;
120 dim.wid += textdim.wid;
121 dim.asc += TEXT_TO_INSET_OFFSET;
122 dim.des += TEXT_TO_INSET_OFFSET;
123 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
124 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
125 bool const changed = dim_ != dim;
131 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
133 // We must draw the label, we should get the label string
134 // from the enclosing float inset.
135 // The question is: Who should draw the label, the caption inset,
136 // the text inset or the paragraph?
137 // We should also draw the float number (Lgb)
139 // Answer: the text inset (in buffer_funcs.C: setCaption).
141 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
142 InsetText::draw(pi, x + labelwidth_, y);
143 setPosCache(pi, x, y);
147 void InsetCaption::edit(LCursor & cur, bool left)
150 InsetText::edit(cur, left);
154 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
157 return InsetText::editXY(cur, x, y);
161 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
162 FuncStatus & status) const
164 switch (cmd.action) {
166 case LFUN_CAPTION_INSERT:
167 case LFUN_FLOAT_INSERT:
168 case LFUN_FLOAT_WIDE_INSERT:
169 case LFUN_WRAP_INSERT:
170 case LFUN_PARAGRAPH_MOVE_UP:
171 case LFUN_PARAGRAPH_MOVE_DOWN:
172 case LFUN_BREAK_PARAGRAPH:
173 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
174 case LFUN_BREAK_PARAGRAPH_SKIP:
175 case LFUN_PARAGRAPH_SPACING:
176 case LFUN_PAGEBREAK_INSERT:
177 status.enabled(false);
181 return InsetText::getStatus(cur, cmd, status);
186 int InsetCaption::latex(Buffer const & buf, odocstream & os,
187 OutputParams const & runparams) const
189 // This is a bit too simplistic to take advantage of
190 // caption options we must add more later. (Lgb)
191 // This code is currently only able to handle the simple
192 // \caption{...}, later we will make it take advantage
193 // of the one of the caption packages. (Lgb)
195 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
197 l += InsetText::latex(buf, os, runparams);
203 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
204 OutputParams const & runparams) const
206 os << full_label_ << ' ';
207 return InsetText::plaintext(buf, os, runparams);
211 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
212 OutputParams const & runparams) const
216 ret = InsetText::docbook(buf, os, runparams);
222 auto_ptr<InsetBase> InsetCaption::doClone() const
224 return auto_ptr<InsetBase>(new InsetCaption(*this));