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