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