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