]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Clean-up in CharStyles, externalize detection of undefined styles to
[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
52 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
53         : InsetCollapsable(bp, Collapsed)
54 {
55         params_.name = s;
56 }
57
58
59 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
60                                 InsetLayout il)
61         : InsetCollapsable(bp, Collapsed)
62 {
63         params_.name = il.name;
64         setLayout(il);
65 }
66
67
68 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
69         : InsetCollapsable(in), params_(in.params_)
70 {}
71
72
73 auto_ptr<Inset> InsetCharStyle::doClone() const
74 {
75         return auto_ptr<Inset>(new InsetCharStyle(*this));
76 }
77
78
79 bool InsetCharStyle::undefined() const
80 {
81         return layout_.labelstring == from_utf8("UNDEFINED");
82 }
83
84
85 void InsetCharStyle::setLayout(InsetLayout il)
86 {
87         layout_ = il;
88 }
89
90
91 docstring const InsetCharStyle::editMessage() const
92 {
93         return _("Opened CharStyle Inset");
94 }
95
96
97 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
98 {
99         params_.write(os);
100         InsetCollapsable::write(buf, os);
101 }
102
103
104 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
105 {
106         params_.read(lex);
107         InsetCollapsable::read(buf, lex);
108 }
109
110
111 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
112 {
113         Font tmpfont = mi.base.font;
114         getDrawFont(mi.base.font);
115         mi.base.font.reduce(Font(Font::ALL_SANE));
116         mi.base.font.realize(tmpfont);
117         bool changed = InsetCollapsable::metrics(mi, dim);
118         mi.base.font = tmpfont;
119         return changed;
120 }
121
122
123 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
124 {
125         setPosCache(pi, x, y);
126
127         Font tmpfont = pi.base.font;
128         getDrawFont(pi.base.font);
129         // I don't understand why the above .reduce and .realize aren't
130         //needed, or even wanted, here. It just works. -- MV 10.04.2005
131         InsetCollapsable::draw(pi, x, y);
132         pi.base.font = tmpfont;
133 }
134
135
136 void InsetCharStyle::getDrawFont(Font & font) const
137 {
138         font = layout_.font;
139 }
140
141
142 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
143 {
144         InsetCollapsable::doDispatch(cur, cmd);
145 }
146
147
148 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
149         FuncStatus & status) const
150 {
151         switch (cmd.action) {
152                 // paragraph breaks not allowed in charstyle insets
153                 case LFUN_BREAK_PARAGRAPH:
154                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
155                 case LFUN_BREAK_PARAGRAPH_SKIP:
156                         status.enabled(false);
157                         return true;
158
159                 default:
160                         return InsetCollapsable::getStatus(cur, cmd, status);
161                 }
162 }
163
164
165 InsetCollapsable::Decoration InsetCharStyle::decoration() const
166 {
167         if (layout_.decoration == "classic")
168                 return Classic;
169         if (layout_.decoration == "minimalistic")
170                 return Minimalistic;
171         if (layout_.decoration == "conglomerate")
172                 return Conglomerate;
173         return Conglomerate;
174 }
175
176
177 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
178                               OutputParams const & runparams) const
179 {
180         return InsetText::plaintext(buf, os, runparams);
181 }
182
183
184 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
185                             OutputParams const & runparams) const
186 {
187         ParagraphList::const_iterator beg = paragraphs().begin();
188         ParagraphList::const_iterator par = paragraphs().begin();
189         ParagraphList::const_iterator end = paragraphs().end();
190
191         if (!undefined())
192                 // FIXME UNICODE
193                 sgml::openTag(os, layout_.latexname,
194                               par->getID(buf, runparams) + layout_.latexparam);
195
196         for (; par != end; ++par) {
197                 par->simpleDocBookOnePar(buf, os, runparams,
198                                          outerFont(std::distance(beg, par),
199                                                    paragraphs()));
200         }
201
202         if (!undefined())
203                 sgml::closeTag(os, layout_.latexname);
204
205         return 0;
206 }
207
208
209 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
210 {
211         os << paragraphs().begin()->asString(buf, true);
212 }
213
214
215 void InsetCharStyleParams::write(ostream & os) const
216 {
217         os << "CharStyle " << name << "\n";
218 }
219
220
221 void InsetCharStyleParams::read(Lexer & lex)
222 {
223         while (lex.isOK()) {
224                 lex.next();
225                 string token = lex.getString();
226
227                 if (token == "CharStyle") {
228                         lex.next();
229                         name = lex.getString();
230                 }
231
232                 // This is handled in Collapsable
233                 else if (token == "status") {
234                         lex.pushToken(token);
235                         break;
236                 }
237         }
238 }
239
240
241 } // namespace lyx