]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
Move #includes out of header files.
[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 #include "insettext.h"
15
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "funcrequest.h"
20 #include "Floating.h"
21 #include "FloatList.h"
22 #include "gettext.h"
23 #include "LaTeXFeatures.h"
24 #include "Lsstream.h"
25 #include "lyxfont.h"
26 #include "lyxlex.h"
27 #include "lyxtext.h"
28 #include "Lsstream.h"
29
30 #include "frontends/LyXView.h"
31 #include "frontends/Dialogs.h"
32
33 #include "support/LOstream.h"
34 #include "support/tostr.h"
35
36 using std::ostream;
37 using std::endl;
38 using std::auto_ptr;
39
40 namespace {
41
42 // this should not be hardcoded, but be part of the definition
43 // of the float (JMarc)
44 string const caplayout("Caption");
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         if (it == floats.end())
51                 return type;
52
53         return _(it->second.name());
54 }
55
56 } // namespace anon
57
58
59 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
60         : InsetCollapsable(bp)
61 {
62         string lab(_("wrap: "));
63         lab += floatname(type, bp);
64         setLabel(lab);
65         LyXFont font(LyXFont::ALL_SANE);
66         font.decSize();
67         font.decSize();
68         font.setColor(LColor::collapsable);
69         setLabelFont(font);
70         params_.type = type;
71         params_.width = LyXLength(50, LyXLength::PCW);
72         setInsetName(type);
73         LyXTextClass const & tclass = bp.getLyXTextClass();
74         if (tclass.hasLayout(caplayout))
75                 inset.paragraphs.begin()->layout(tclass[caplayout]);
76 }
77
78
79 InsetWrap::~InsetWrap()
80 {
81         InsetWrapMailer(*this).hideDialog();
82 }
83
84
85 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
86 {
87         switch (cmd.action) {
88         case LFUN_INSET_MODIFY: {
89                 InsetWrapParams params;
90                 InsetWrapMailer::string2params(cmd.argument, params);
91
92                 params_.placement = params.placement;
93                 params_.width     = params.width;
94
95                 cmd.view()->updateInset(this);
96                 return DISPATCHED;
97         }
98
99         case LFUN_INSET_DIALOG_UPDATE:
100                 InsetWrapMailer(*this).updateDialog(cmd.view());
101                 return DISPATCHED;
102
103         default:
104                 return InsetCollapsable::localDispatch(cmd);
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         if (lex.isOK()) {
123                 lex.next();
124                 string token = lex.getString();
125                 if (token == "placement") {
126                         lex.next();
127                         placement = lex.getString();
128                 } else {
129                         // take countermeasures
130                         lex.pushToken(token);
131                 }
132         }
133         if (lex.isOK()) {
134                 lex.next();
135                 string token = lex.getString();
136                 if (token == "width") {
137                         lex.next();
138                         width = LyXLength(lex.getString());
139                 } else {
140                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
141                                << endl;
142                         // take countermeasures
143                         lex.pushToken(token);
144                 }
145         }
146 }
147
148
149 void InsetWrap::write(Buffer const & buf, ostream & os) const
150 {
151         params_.write(os);
152         InsetCollapsable::write(buf, os);
153 }
154
155
156 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
157 {
158         params_.read(lex);
159         InsetCollapsable::read(buf, lex);
160 }
161
162
163 void InsetWrap::validate(LaTeXFeatures & features) const
164 {
165         features.require("floatflt");
166         InsetCollapsable::validate(features);
167 }
168
169
170 auto_ptr<InsetBase> InsetWrap::clone() const
171 {
172         return auto_ptr<InsetBase>(new InsetWrap(*this));
173 }
174
175
176 string const InsetWrap::editMessage() const
177 {
178         return _("Opened Wrap Inset");
179 }
180
181
182 int InsetWrap::latex(Buffer const & buf, ostream & os,
183                      LatexRunParams const & runparams) const
184 {
185         os << "\\begin{floating" << params_.type << '}';
186         if (!params_.placement.empty()) {
187                 os << '[' << params_.placement << ']';
188         }
189         os  << '{' << params_.width.asLatexString() << "}%\n";
190
191         int const i = inset.latex(buf, os, runparams);
192
193         os << "\\end{floating" << params_.type << "}%\n";
194         return i + 2;
195 }
196
197
198 int InsetWrap::docbook(Buffer const & buf, ostream & os, bool mixcont) const
199 {
200         os << '<' << params_.type << '>';
201         int const i = inset.docbook(buf, os, mixcont);
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 int InsetWrap::latexTextWidth(BufferView * bv) const
222 {
223         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
224 }
225
226
227 bool InsetWrap::showInsetDialog(BufferView * bv) const
228 {
229         if (!inset.showInsetDialog(bv)) {
230                 InsetWrap * tmp = const_cast<InsetWrap *>(this);
231                 InsetWrapMailer(*tmp).showDialog(bv);
232         }
233         return true;
234 }
235
236
237 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
238 {
239         // Now find the caption in the float...
240         ParagraphList::iterator tmp = inset.paragraphs.begin();
241         ParagraphList::iterator end = inset.paragraphs.end();
242
243         for (; tmp != end; ++tmp) {
244                 if (tmp->layout()->name() == caplayout) {
245                         string const name = floatname(params_.type, buf.params);
246                         string const str =
247                                 tostr(toclist[name].size() + 1)
248                                 + ". " + tmp->asString(buf, false);
249                         lyx::toc::TocItem const item(tmp->id(), 0 , str);
250                         toclist[name].push_back(item);
251                 }
252         }
253 }
254
255
256 string const InsetWrapMailer::name_("wrap");
257
258 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
259         : inset_(inset)
260 {}
261
262
263 string const InsetWrapMailer::inset2string(Buffer const &) const
264 {
265         return params2string(inset_.params());
266 }
267
268
269 void InsetWrapMailer::string2params(string const & in,
270                                     InsetWrapParams & params)
271 {
272         params = InsetWrapParams();
273
274         if (in.empty())
275                 return;
276
277         istringstream data(STRCONV(in));
278         LyXLex lex(0,0);
279         lex.setStream(data);
280
281         if (lex.isOK()) {
282                 lex.next();
283                 string const token = lex.getString();
284                 if (token != name_)
285                         return;
286         }
287
288         // This is part of the inset proper that is usually swallowed
289         // by Buffer::readInset
290         if (lex.isOK()) {
291                 lex.next();
292                 string const token = lex.getString();
293                 if (token != "Wrap" || !lex.eatLine())
294                         return;
295         }
296
297         if (lex.isOK()) {
298                 params.read(lex);
299         }
300 }
301
302
303 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
304 {
305         ostringstream data;
306         data << name_ << ' ';
307         params.write(data);
308         return STRCONV(data.str());
309 }