]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
put namespace toc inside namespace lxy
[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 "FloatList.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "Lsstream.h"
24 #include "lyxfont.h"
25 #include "lyxlex.h"
26 #include "lyxtext.h"
27 #include "Lsstream.h"
28
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
31
32 #include "support/LOstream.h"
33 #include "support/tostr.h"
34
35 using std::ostream;
36 using std::endl;
37 using std::auto_ptr;
38
39 namespace {
40
41 // this should not be hardcoded, but be part of the definition
42 // of the float (JMarc)
43 string const caplayout("Caption");
44
45 string floatname(string const & type, BufferParams const & bp)
46 {
47         FloatList const & floats = bp.getLyXTextClass().floats();
48         FloatList::const_iterator it = floats[type];
49         if (it == floats.end())
50                 return type;
51
52         return _(it->second.name());
53 }
54
55 } // namespace anon
56
57
58 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
59         : InsetCollapsable(bp)
60 {
61         string lab(_("wrap: "));
62         lab += floatname(type, bp);
63         setLabel(lab);
64         LyXFont font(LyXFont::ALL_SANE);
65         font.decSize();
66         font.decSize();
67         font.setColor(LColor::collapsable);
68         setLabelFont(font);
69         params_.type = type;
70         params_.width = LyXLength(50, LyXLength::PCW);
71         setInsetName(type);
72         LyXTextClass const & tclass = bp.getLyXTextClass();
73         if (tclass.hasLayout(caplayout))
74                 inset.paragraphs.begin()->layout(tclass[caplayout]);
75 }
76
77
78 InsetWrap::~InsetWrap()
79 {
80         InsetWrapMailer(*this).hideDialog();
81 }
82
83
84 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
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()->updateInset(this);
95                 return DISPATCHED;
96         }
97
98         case LFUN_INSET_DIALOG_UPDATE:
99                 InsetWrapMailer(*this).updateDialog(cmd.view());
100                 return DISPATCHED;
101
102         default:
103                 return InsetCollapsable::localDispatch(cmd);
104         }
105 }
106
107
108 void InsetWrapParams::write(ostream & os) const
109 {
110         os << "Wrap " // getInsetName()
111            << 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 }