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