]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
remove harcoding for Caption layout
[lyx.git] / src / insets / insetwrap.C
1 /**
2  * \file insetwrap.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetwrap.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "outputparams.h"
29 #include "paragraph.h"
30 #include "pariterator.h"
31
32 #include "support/convert.h"
33
34 #include <sstream>
35
36 using std::string;
37 using std::endl;
38 using std::auto_ptr;
39 using std::istringstream;
40 using std::ostream;
41 using std::ostringstream;
42
43
44 namespace {
45
46 string floatname(string const & type, BufferParams const & bp)
47 {
48         FloatList const & floats = bp.getLyXTextClass().floats();
49         FloatList::const_iterator it = floats[type];
50         return (it == floats.end()) ? type : _(it->second.name());
51 }
52
53 } // namespace anon
54
55
56 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
57         : InsetCollapsable(bp)
58 {
59         setLabel(_("wrap: ") + floatname(type, bp));
60         LyXFont font(LyXFont::ALL_SANE);
61         font.decSize();
62         font.decSize();
63         font.setColor(LColor::collapsable);
64         setLabelFont(font);
65         params_.type = type;
66         params_.width = LyXLength(50, LyXLength::PCW);
67         setInsetName(type);
68 }
69
70
71 InsetWrap::~InsetWrap()
72 {
73         InsetWrapMailer(*this).hideDialog();
74 }
75
76
77 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
78 {
79         switch (cmd.action) {
80         case LFUN_INSET_MODIFY: {
81                 InsetWrapParams params;
82                 InsetWrapMailer::string2params(cmd.argument, params);
83                 params_.placement = params.placement;
84                 params_.width     = params.width;
85                 cur.bv().update();
86                 break;
87         }
88
89         case LFUN_INSET_DIALOG_UPDATE:
90                 InsetWrapMailer(*this).updateDialog(&cur.bv());
91                 break;
92
93         case LFUN_MOUSE_RELEASE: {
94                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
95                         InsetWrapMailer(*this).showDialog(&cur.bv());
96                         break;
97                 }
98                 InsetCollapsable::doDispatch(cur, cmd);
99                 break;
100         }
101
102         default:
103                 InsetCollapsable::doDispatch(cur, cmd);
104                 break;
105         }
106 }
107
108
109 void InsetWrapParams::write(ostream & os) const
110 {
111         os << "Wrap " << type << '\n';
112
113         if (!placement.empty())
114                 os << "placement " << placement << "\n";
115
116         os << "width \"" << width.asString() << "\"\n";
117 }
118
119
120 void InsetWrapParams::read(LyXLex & lex)
121 {
122         string token;
123         lex >> token;
124         if (token == "placement")
125                 lex >> placement;
126         else {
127                 // take countermeasures
128                 lex.pushToken(token);
129         }
130         if (!lex)
131                 return;
132         lex >> token;
133         if (token == "width") {
134                 lex.next();
135                 width = LyXLength(lex.getString());
136         } else {
137                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
138                         << endl;
139                 // take countermeasures
140                 lex.pushToken(token);
141         }
142 }
143
144
145 void InsetWrap::write(Buffer const & buf, ostream & os) const
146 {
147         params_.write(os);
148         InsetCollapsable::write(buf, os);
149 }
150
151
152 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
153 {
154         params_.read(lex);
155         InsetCollapsable::read(buf, lex);
156 }
157
158
159 void InsetWrap::validate(LaTeXFeatures & features) const
160 {
161         features.require("floatflt");
162         InsetCollapsable::validate(features);
163 }
164
165
166 auto_ptr<InsetBase> InsetWrap::doClone() const
167 {
168         return auto_ptr<InsetBase>(new InsetWrap(*this));
169 }
170
171
172 string const InsetWrap::editMessage() const
173 {
174         return _("Opened Wrap Inset");
175 }
176
177
178 int InsetWrap::latex(Buffer const & buf, ostream & os,
179                      OutputParams const & runparams) const
180 {
181         os << "\\begin{floating" << params_.type << '}';
182         if (!params_.placement.empty())
183                 os << '[' << params_.placement << ']';
184         os << '{' << params_.width.asLatexString() << "}%\n";
185         int const i = InsetText::latex(buf, os, runparams);
186         os << "\\end{floating" << params_.type << "}%\n";
187         return i + 2;
188 }
189
190
191 int InsetWrap::docbook(Buffer const & buf, ostream & os,
192                        OutputParams const & runparams) const
193 {
194         os << '<' << params_.type << '>';
195         int const i = InsetText::docbook(buf, os, runparams);
196         os << "</" << params_.type << '>';
197         return i;
198 }
199
200
201 bool InsetWrap::insetAllowed(InsetBase::Code code) const
202 {
203         switch(code) {
204         case FLOAT_CODE:
205         case FOOT_CODE:
206         case MARGIN_CODE:
207                 return false;
208         default:
209                 return InsetCollapsable::insetAllowed(code);
210         }
211 }
212
213
214 bool InsetWrap::showInsetDialog(BufferView * bv) const
215 {
216         if (!InsetText::showInsetDialog(bv))
217                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
218         return true;
219 }
220
221
222 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
223 {
224         ParConstIterator pit = par_const_iterator_begin(*this);
225         ParConstIterator end = par_const_iterator_end(*this);
226
227         // Find a caption layout in one of the (child inset's) pars
228         for (; pit != end; ++pit) {
229                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
230                         string const name = floatname(params_.type, buf.params());
231                         string const str =
232                                 convert<string>(toclist[name].size() + 1)
233                                 + ". " + pit->asString(buf, false);
234                         lyx::toc::TocItem const item(pit->id(), 0 , str);
235                         toclist[name].push_back(item);
236                 }
237         }
238 }
239
240
241 string const InsetWrapMailer::name_("wrap");
242
243 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
244         : inset_(inset)
245 {}
246
247
248 string const InsetWrapMailer::inset2string(Buffer const &) const
249 {
250         return params2string(inset_.params());
251 }
252
253
254 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
255 {
256         params = InsetWrapParams();
257         if (in.empty())
258                 return;
259
260         istringstream data(in);
261         LyXLex lex(0,0);
262         lex.setStream(data);
263
264         string name;
265         lex >> name;
266         if (!lex || name != name_)
267                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
268
269         // This is part of the inset proper that is usually swallowed
270         // by LyXText::readInset
271         string id;
272         lex >> id;
273         if (!lex || id != "Wrap")
274                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
275
276         // We have to read the type here!
277         lex >> params.type;
278         params.read(lex);
279 }
280
281
282 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
283 {
284         ostringstream data;
285         data << name_ << ' ';
286         params.write(data);
287         return data.str();
288 }