]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Fix bug 3170:
[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
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35 #include "support/lstrings.h"
36 #include "support/convert.h"
37
38 #include <sstream>
39
40
41 namespace lyx {
42
43 using support::bformat;
44
45 using std::auto_ptr;
46 using std::endl;
47 using std::string;
48 using std::ostream;
49
50
51 InsetCaption::InsetCaption(BufferParams const & bp)
52         : InsetText(bp), textclass_(bp.getLyXTextClass())
53 {
54         setAutoBreakRows(true);
55         setDrawFrame(true);
56         setFrameColor(LColor::captionframe);
57 }
58
59
60 void InsetCaption::write(Buffer const & buf, ostream & os) const
61 {
62         os << "Caption\n";
63         text_.write(buf, os);
64 }
65
66
67 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
68 {
69 #if 0
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."
75                        << endl;
76         }
77 #endif
78         InsetText::read(buf, lex);
79 }
80
81
82 docstring const InsetCaption::editMessage() const
83 {
84         return _("Opened Caption Inset");
85 }
86
87
88 void InsetCaption::cursorPos(BufferView const & bv,
89                 CursorSlice const & sl, bool boundary, int & x, int & y) const
90 {
91         InsetText::cursorPos(bv, sl, boundary, x, y);
92         x += labelwidth_;
93 }
94
95
96 void InsetCaption::setLabel(docstring const & label)
97 {
98         label_ = _(to_ascii(label));
99 }
100
101
102 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
103 {
104         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
105         docstring const number = convert<docstring>(counter_);
106         full_label_ = bformat(from_ascii("%1$s %2$s:"), label_, number);
107         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
108         dim.wid = labelwidth_;
109         Dimension textdim;
110         InsetText::metrics(mi, textdim);
111         // Correct for button width, and re-fit
112         mi.base.textwidth -= dim.wid;
113         InsetText::metrics(mi, textdim);
114         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
115         dim.asc = textdim.asc;
116         dim.wid += textdim.wid;
117         dim.asc += TEXT_TO_INSET_OFFSET;
118         dim.des += TEXT_TO_INSET_OFFSET;
119         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
120         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
121         bool const changed = dim_ != dim;
122         dim_ = dim;
123         return changed;
124 }
125
126
127 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
128 {
129         // We must draw the label, we should get the label string
130         // from the enclosing float inset.
131         // The question is: Who should draw the label, the caption inset,
132         // the text inset or the paragraph?
133         // We should also draw the float number (Lgb)
134
135         // Answer: the text inset (in buffer_funcs.C: setCaption).
136
137         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
138         InsetText::draw(pi, x + labelwidth_, y);
139         setPosCache(pi, x, y);
140 }
141
142
143 void InsetCaption::edit(LCursor & cur, bool left)
144 {
145         cur.push(*this);
146         InsetText::edit(cur, left);
147 }
148
149
150 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
151 {
152         cur.push(*this);
153         return InsetText::editXY(cur, x, y);
154 }
155
156
157 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
158         FuncStatus & status) const
159 {
160         switch (cmd.action) {
161
162         case LFUN_CAPTION_INSERT:
163         case LFUN_FLOAT_INSERT:
164         case LFUN_FLOAT_WIDE_INSERT:
165         case LFUN_WRAP_INSERT:
166         case LFUN_PARAGRAPH_MOVE_UP:
167         case LFUN_PARAGRAPH_MOVE_DOWN:
168         case LFUN_BREAK_PARAGRAPH:
169         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
170         case LFUN_BREAK_PARAGRAPH_SKIP:
171         case LFUN_PARAGRAPH_SPACING:
172         case LFUN_PAGEBREAK_INSERT:
173                 status.enabled(false);
174                 return true;
175
176         default:
177                 return InsetText::getStatus(cur, cmd, status);
178         }
179 }
180
181
182 int InsetCaption::latex(Buffer const & buf, odocstream & os,
183                         OutputParams const & runparams) const
184 {
185         // This is a bit too simplistic to take advantage of
186         // caption options we must add more later. (Lgb)
187         // This code is currently only able to handle the simple
188         // \caption{...}, later we will make it take advantage
189         // of the one of the caption packages. (Lgb)
190         os << "\\caption";
191         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
192         os << '{';
193         l += InsetText::latex(buf, os, runparams);
194         os << "}\n";
195         return l + 1;
196 }
197
198
199 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
200                 OutputParams const & runparams) const
201 {
202         os << full_label_ << ' ';
203         return InsetText::plaintext(buf, os, runparams);
204 }
205
206
207 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
208                           OutputParams const & runparams) const
209 {
210         int ret;
211         os << "<title>";
212         ret = InsetText::docbook(buf, os, runparams);
213         os << "</title>\n";
214         return ret;
215 }
216
217
218 auto_ptr<InsetBase> InsetCaption::doClone() const
219 {
220         return auto_ptr<InsetBase>(new InsetCaption(*this));
221 }
222
223
224 } // namespace lyx