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