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