]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
dont use pragma impementation and interface anymore
[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
14 #include "insetwrap.h"
15 #include "gettext.h"
16 #include "lyxfont.h"
17 #include "BufferView.h"
18 #include "lyxtext.h"
19 #include "insets/insettext.h"
20 #include "support/LOstream.h"
21 #include "support/lstrings.h"
22 #include "LaTeXFeatures.h"
23 #include "debug.h"
24 #include "buffer.h"
25 #include "frontends/LyXView.h"
26 #include "frontends/Dialogs.h"
27 #include "lyxlex.h"
28 #include "FloatList.h"
29
30 using std::ostream;
31 using std::endl;
32
33 namespace {
34
35 // this should not be hardcoded, but be part of the definition
36 // of the float (JMarc)
37 string const caplayout("Caption");
38 string floatname(string const & type, BufferParams const & bp)
39 {
40         FloatList const & floats = bp.getLyXTextClass().floats();
41         FloatList::const_iterator it = floats[type];
42         if (it == floats.end())
43                 return type;
44
45         return _(it->second.name());
46 }
47
48 } // namespace anon
49
50
51 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
52         : InsetCollapsable(bp), width_(50, LyXLength::PCW)
53 {
54         string lab(_("wrap: "));
55         lab += floatname(type, bp);
56         setLabel(lab);
57         LyXFont font(LyXFont::ALL_SANE);
58         font.decSize();
59         font.decSize();
60         font.setColor(LColor::collapsable);
61         setLabelFont(font);
62         Type_ = type;
63         setInsetName(type);
64         LyXTextClass const & tclass = bp.getLyXTextClass();
65         if (tclass.hasLayout(caplayout))
66                 inset.paragraph()->layout(tclass[caplayout]);
67 }
68
69
70 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
71         : InsetCollapsable(in, same_id), Type_(in.Type_),
72           Placement_(in.Placement_), width_(in.width_)
73 {}
74
75
76 InsetWrap::~InsetWrap()
77 {
78         hideDialog();
79 }
80
81
82 void InsetWrap::write(Buffer const * buf, ostream & os) const
83 {
84         os << "Wrap " // getInsetName()
85            << Type_ << '\n';
86
87         if (!Placement_.empty()) {
88                 os << "placement " << Placement_ << "\n";
89         }
90         os << "width \"" << width_.asString() << "\"\n";
91
92         InsetCollapsable::write(buf, os);
93 }
94
95
96 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
97 {
98         if (lex.isOK()) {
99                 lex.next();
100                 string token = lex.getString();
101                 if (token == "placement") {
102                         lex.next();
103                         Placement_ = lex.getString();
104                 } else {
105                         // take countermeasures
106                         lex.pushToken(token);
107                 }
108         }
109         if (lex.isOK()) {
110                 lex.next();
111                 string token = lex.getString();
112                 if (token == "width") {
113                         lex.next();
114                         width_ = LyXLength(lex.getString());
115                 } else {
116                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
117                                << endl;
118                         // take countermeasures
119                         lex.pushToken(token);
120                 }
121         }
122         InsetCollapsable::read(buf, lex);
123 }
124
125
126 void InsetWrap::validate(LaTeXFeatures & features) const
127 {
128         features.require("floatflt");
129         InsetCollapsable::validate(features);
130 }
131
132
133 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
134 {
135         return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
136 }
137
138
139 string const InsetWrap::editMessage() const
140 {
141         return _("Opened Wrap Inset");
142 }
143
144
145 int InsetWrap::latex(Buffer const * buf,
146                       ostream & os, bool fragile, bool fp) const
147 {
148         os << "\\begin{floating" << Type_ << '}';
149         if (!Placement_.empty()) {
150                 os << '[' << Placement_ << ']';
151         }
152         os  << '{' << width_.asLatexString() << "}%\n";
153
154         int const i = inset.latex(buf, os, fragile, fp);
155
156         os << "\\end{floating" << Type_ << "}%\n";
157         return i + 2;
158 }
159
160
161 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
162 {
163         os << '<' << Type_ << '>';
164         int const i = inset.docbook(buf, os, mixcont);
165         os << "</" << Type_ << '>';
166
167         return i;
168 }
169
170
171 bool InsetWrap::insetAllowed(Inset::Code code) const
172 {
173         switch(code) {
174         case FLOAT_CODE:
175         case FOOT_CODE:
176         case MARGIN_CODE:
177                 return false;
178         default:
179                 return InsetCollapsable::insetAllowed(code);
180         }
181 }
182
183
184 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
185         const
186 {
187         if (owner() &&
188             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
189                 return -1;
190         }
191         if (!width_.zero()) {
192                 int const ww1 = latexTextWidth(bv);
193                 int const ww2 = InsetCollapsable::getMaxWidth(bv, inset);
194                 if (ww2 > 0 && ww2 < ww1) {
195                         return ww2;
196                 }
197                 return ww1;
198         }
199         // this should not happen!
200         return InsetCollapsable::getMaxWidth(bv, inset);
201 }
202
203
204 int InsetWrap::latexTextWidth(BufferView * bv) const
205 {
206         return width_.inPixels(InsetCollapsable::latexTextWidth(bv));
207 }
208
209
210 string const & InsetWrap::type() const
211 {
212         return Type_;
213 }
214
215
216 LyXLength const & InsetWrap::pageWidth() const
217 {
218         return width_;
219 }
220
221
222 void InsetWrap::pageWidth(LyXLength const & ll)
223 {
224         if (ll != width_) {
225                 width_ = ll;
226                 need_update = FULL;
227         }
228 }
229
230
231 void InsetWrap::placement(string const & p)
232 {
233         Placement_ = p;
234 }
235
236
237 string const & InsetWrap::placement() const
238 {
239         return Placement_;
240 }
241
242
243 bool InsetWrap::showInsetDialog(BufferView * bv) const
244 {
245         if (!inset.showInsetDialog(bv)) {
246                 bv->owner()->getDialogs().showWrap(const_cast<InsetWrap *>(this));
247         }
248         return true;
249 }
250
251
252 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
253 {
254         // Now find the caption in the float...
255         // We now tranverse the paragraphs of
256         // the inset...
257         Paragraph * tmp = inset.paragraph();
258         while (tmp) {
259                 if (tmp->layout()->name() == caplayout) {
260                         string const name = floatname(type(), buf->params);
261                         string const str =
262                                 tostr(toclist[name].size() + 1)
263                                 + ". " + tmp->asString(buf, false);
264                         toc::TocItem const item(tmp, 0 , str);
265                         toclist[name].push_back(item);
266                 }
267                 tmp = tmp->next();
268         }
269 }