2 * \file InsetCharStyle.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "InsetCharStyle.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
28 #include "MetricsInfo.h"
29 #include "Paragraph.h"
30 #include "paragraph_funcs.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "support/convert.h"
45 using std::istringstream;
48 using std::ostringstream;
51 void InsetCharStyle::init()
55 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
56 : InsetCollapsable(bp, Collapsed)
64 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
65 CharStyles::iterator cs)
66 : InsetCollapsable(bp, Collapsed)
68 params_.name = cs->name;
74 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
75 : InsetCollapsable(in), params_(in.params_)
81 auto_ptr<Inset> InsetCharStyle::doClone() const
83 return auto_ptr<Inset>(new InsetCharStyle(*this));
87 bool InsetCharStyle::undefined() const
89 return layout_.latexname.empty();
93 void InsetCharStyle::setUndefined()
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);
104 void InsetCharStyle::setDefined(CharStyles::iterator cs)
110 docstring const InsetCharStyle::editMessage() const
112 return _("Opened CharStyle Inset");
116 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
119 InsetCollapsable::write(buf, os);
123 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
126 InsetCollapsable::read(buf, lex);
130 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
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) {
140 docstring s(from_utf8(params_.name));
142 if (s.find(':') != string::npos)
143 s = s.substr(s.find(':'));
145 s = _("Undef: ") + s;
146 layout_.labelstring = s;
152 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
154 setPosCache(pi, x, y);
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;
163 // the name of the charstyle. Can be toggled.
164 if (status() == Open) {
166 docstring s(from_utf8(params_.name));
168 if (s.find(':') != string::npos)
169 s = s.substr(s.find(':'));
171 s = _("Undef: ") + s;
172 layout_.labelstring = s;
177 void InsetCharStyle::getDrawFont(Font & font) const
183 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
185 InsetCollapsable::doDispatch(cur, cmd);
189 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
190 FuncStatus & status) const
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);
201 return InsetCollapsable::getStatus(cur, cmd, status);
206 InsetCollapsable::Decoration InsetCharStyle::decoration() const
208 if (layout_.decoration == "classic")
210 if (layout_.decoration == "minimalistic")
212 if (layout_.decoration == "conglomerate")
218 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
219 OutputParams const & runparams) const
221 return InsetText::plaintext(buf, os, runparams);
225 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
226 OutputParams const & runparams) const
228 ParagraphList::const_iterator beg = paragraphs().begin();
229 ParagraphList::const_iterator par = paragraphs().begin();
230 ParagraphList::const_iterator end = paragraphs().end();
234 sgml::openTag(os, layout_.latexname,
235 par->getID(buf, runparams) + layout_.latexparam);
237 for (; par != end; ++par) {
238 par->simpleDocBookOnePar(buf, os, runparams,
239 outerFont(std::distance(beg, par),
244 sgml::closeTag(os, layout_.latexname);
250 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
252 os << paragraphs().begin()->asString(buf, true);
256 void InsetCharStyleParams::write(ostream & os) const
258 os << "CharStyle " << name << "\n";
262 void InsetCharStyleParams::read(Lexer & lex)
266 string token = lex.getString();
268 if (token == "CharStyle") {
270 name = lex.getString();
273 // This is handled in Collapsable
274 else if (token == "status") {
275 lex.pushToken(token);