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