]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Make Richard happy, add inset layout parm 'Decoration'
[lyx.git] / src / insets / InsetCharStyle.cpp
1 /**
2  * \file InsetCharStyle.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCharStyle.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Cursor.h"
24 #include "gettext.h"
25 #include "Color.h"
26 #include "Lexer.h"
27 #include "Text.h"
28 #include "MetricsInfo.h"
29 #include "Paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "sgml.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "support/convert.h"
37
38 #include <sstream>
39
40
41 namespace lyx {
42
43 using std::string;
44 using std::auto_ptr;
45 using std::istringstream;
46 using std::max;
47 using std::ostream;
48 using std::ostringstream;
49
50
51 void InsetCharStyle::init()
52 {}
53
54
55 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
56         : InsetCollapsable(bp, Collapsed)
57 {
58         params_.name = s;
59         setUndefined();
60         init();
61 }
62
63
64 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
65                                 CharStyles::iterator cs)
66         : InsetCollapsable(bp, Collapsed)
67 {
68         params_.name = cs->name;
69         setDefined(cs);
70         init();
71 }
72
73
74 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
75         : InsetCollapsable(in), params_(in.params_)
76 {
77         init();
78 }
79
80
81 auto_ptr<Inset> InsetCharStyle::doClone() const
82 {
83         return auto_ptr<Inset>(new InsetCharStyle(*this));
84 }
85
86
87 bool InsetCharStyle::undefined() const
88 {
89         return layout_.latexname.empty();
90 }
91
92
93 void InsetCharStyle::setUndefined()
94 {
95         layout_.latextype.clear();
96         layout_.latexname.clear();
97         layout_.latexparam.clear();
98         layout_.font = Font(Font::ALL_INHERIT);
99         layout_.labelfont = Font(Font::ALL_INHERIT);
100         layout_.labelfont.setColor(Color::error);
101 }
102
103
104 void InsetCharStyle::setDefined(CharStyles::iterator cs)
105 {
106         layout_ = *cs;
107 }
108
109
110 docstring const InsetCharStyle::editMessage() const
111 {
112         return _("Opened CharStyle Inset");
113 }
114
115
116 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
117 {
118         params_.write(os);
119         InsetCollapsable::write(buf, os);
120 }
121
122
123 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
124 {
125         params_.read(lex);
126         InsetCollapsable::read(buf, lex);
127 }
128
129
130 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         Font tmpfont = mi.base.font;
133         getDrawFont(mi.base.font);
134         mi.base.font.reduce(Font(Font::ALL_SANE));
135         mi.base.font.realize(tmpfont);
136         bool changed = InsetCollapsable::metrics(mi, dim);
137         mi.base.font = tmpfont;
138         if (status() == Open) {
139                 // FIXME UNICODE
140                 docstring s(from_utf8(params_.name));
141                 // Chop off prefix:
142                 if (s.find(':') != string::npos)
143                         s = s.substr(s.find(':'));
144                 if (undefined())
145                         s = _("Undef: ") + s;
146                 layout_.labelstring = s;
147         }
148         return changed;
149 }
150
151
152 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
153 {
154         setPosCache(pi, x, y);
155
156         Font tmpfont = pi.base.font;
157         getDrawFont(pi.base.font);
158         // I don't understand why the above .reduce and .realize aren't
159         //needed, or even wanted, here. It just works. -- MV 10.04.2005
160         InsetCollapsable::draw(pi, x, y);
161         pi.base.font = tmpfont;
162
163         // the name of the charstyle. Can be toggled.
164         if (status() == Open) {
165                 // FIXME UNICODE
166                 docstring s(from_utf8(params_.name));
167                 // Chop off prefix:
168                 if (s.find(':') != string::npos)
169                         s = s.substr(s.find(':'));
170                 if (undefined())
171                         s = _("Undef: ") + s;
172                 layout_.labelstring = s;
173         }
174 }
175
176
177 void InsetCharStyle::getDrawFont(Font & font) const
178 {
179         font = layout_.font;
180 }
181
182
183 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
184 {
185         InsetCollapsable::doDispatch(cur, cmd);
186 }
187
188
189 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
190         FuncStatus & status) const
191 {
192         switch (cmd.action) {
193                 // paragraph breaks not allowed in charstyle insets
194                 case LFUN_BREAK_PARAGRAPH:
195                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
196                 case LFUN_BREAK_PARAGRAPH_SKIP:
197                         status.enabled(false);
198                         return true;
199
200                 default:
201                         return InsetCollapsable::getStatus(cur, cmd, status);
202                 }
203 }
204
205
206 InsetCollapsable::Decoration InsetCharStyle::decoration() const
207 {
208         if (layout_.decoration == "classic")
209                 return Classic;
210         if (layout_.decoration == "minimalistic")
211                 return Minimalistic;
212         if (layout_.decoration == "conglomerate")
213                 return Conglomerate;
214         return Conglomerate;
215 }
216
217
218 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
219                               OutputParams const & runparams) const
220 {
221         return InsetText::plaintext(buf, os, runparams);
222 }
223
224
225 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
226                             OutputParams const & runparams) const
227 {
228         ParagraphList::const_iterator beg = paragraphs().begin();
229         ParagraphList::const_iterator par = paragraphs().begin();
230         ParagraphList::const_iterator end = paragraphs().end();
231
232         if (!undefined())
233                 // FIXME UNICODE
234                 sgml::openTag(os, layout_.latexname,
235                               par->getID(buf, runparams) + layout_.latexparam);
236
237         for (; par != end; ++par) {
238                 par->simpleDocBookOnePar(buf, os, runparams,
239                                          outerFont(std::distance(beg, par),
240                                                    paragraphs()));
241         }
242
243         if (!undefined())
244                 sgml::closeTag(os, layout_.latexname);
245
246         return 0;
247 }
248
249
250 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
251 {
252         os << paragraphs().begin()->asString(buf, true);
253 }
254
255
256 void InsetCharStyleParams::write(ostream & os) const
257 {
258         os << "CharStyle " << name << "\n";
259 }
260
261
262 void InsetCharStyleParams::read(Lexer & lex)
263 {
264         while (lex.isOK()) {
265                 lex.next();
266                 string token = lex.getString();
267
268                 if (token == "CharStyle") {
269                         lex.next();
270                         name = lex.getString();
271                 }
272
273                 // This is handled in Collapsable
274                 else if (token == "status") {
275                         lex.pushToken(token);
276                         break;
277                 }
278         }
279 }
280
281
282 } // namespace lyx