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