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"
25 #include "LaTeXFeatures.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "paragraph_funcs.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "support/convert.h"
46 using std::istringstream;
49 using std::ostringstream;
52 void InsetCharStyle::init()
56 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
57 : InsetCollapsable(bp, Collapsed)
65 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
66 CharStyles::iterator cs)
67 : InsetCollapsable(bp, Collapsed)
69 params_.name = cs->name;
75 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
76 : InsetCollapsable(in), params_(in.params_)
82 auto_ptr<Inset> InsetCharStyle::doClone() const
84 return auto_ptr<Inset>(new InsetCharStyle(*this));
88 bool InsetCharStyle::undefined() const
90 return layout_.latexname.empty();
94 void InsetCharStyle::setUndefined()
96 layout_.latextype.clear();
97 layout_.latexname.clear();
98 layout_.latexparam.clear();
99 layout_.font = Font(Font::ALL_INHERIT);
100 layout_.labelfont = Font(Font::ALL_INHERIT);
101 layout_.labelfont.setColor(Color::error);
105 void InsetCharStyle::setDefined(CharStyles::iterator cs)
111 docstring const InsetCharStyle::editMessage() const
113 return _("Opened CharStyle Inset");
117 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
120 InsetCollapsable::write(buf, os);
124 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
127 InsetCollapsable::read(buf, lex);
131 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
133 Font tmpfont = mi.base.font;
134 getDrawFont(mi.base.font);
135 mi.base.font.reduce(Font(Font::ALL_SANE));
136 mi.base.font.realize(tmpfont);
137 bool changed = InsetCollapsable::metrics(mi, dim);
138 mi.base.font = tmpfont;
139 if (status() == Open) {
141 docstring s(from_utf8(params_.name));
143 s = _("Undef: ") + s;
145 if (s.find(':') != string::npos)
146 s = s.substr(s.find(':'));
147 layout_.labelstring = s;
153 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
155 setPosCache(pi, x, y);
157 Font tmpfont = pi.base.font;
158 getDrawFont(pi.base.font);
159 // I don't understand why the above .reduce and .realize aren't
160 //needed, or even wanted, here. It just works. -- MV 10.04.2005
161 InsetCollapsable::draw(pi, x, y);
162 pi.base.font = tmpfont;
164 // the name of the charstyle. Can be toggled.
165 if (status() == Open) {
167 docstring s(from_utf8(params_.name));
169 s = _("Undef: ") + s;
171 if (s.find(':') != string::npos)
172 s = s.substr(s.find(':'));
173 layout_.labelstring = s;
178 void InsetCharStyle::getDrawFont(Font & font) const
184 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
186 InsetCollapsable::doDispatch(cur, cmd);
190 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & status) const
193 switch (cmd.action) {
194 // paragraph breaks not allowed in charstyle insets
195 case LFUN_BREAK_PARAGRAPH:
196 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
197 case LFUN_BREAK_PARAGRAPH_SKIP:
198 status.enabled(false);
202 return InsetCollapsable::getStatus(cur, cmd, status);
207 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
208 OutputParams const & runparams) const
212 os << '\\' << from_utf8(layout_.latexname);
213 if (!layout_.latexparam.empty())
214 os << from_utf8(layout_.latexparam);
217 int i = InsetText::latex(buf, os, runparams);
224 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
225 OutputParams const & runparams) const
227 return InsetText::plaintext(buf, os, runparams);
231 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
232 OutputParams const & runparams) const
234 ParagraphList::const_iterator beg = paragraphs().begin();
235 ParagraphList::const_iterator par = paragraphs().begin();
236 ParagraphList::const_iterator end = paragraphs().end();
240 sgml::openTag(os, layout_.latexname,
241 par->getID(buf, runparams) + layout_.latexparam);
243 for (; par != end; ++par) {
244 par->simpleDocBookOnePar(buf, os, runparams,
245 outerFont(std::distance(beg, par),
250 sgml::closeTag(os, layout_.latexname);
256 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
258 os << paragraphs().begin()->asString(buf, true);
262 void InsetCharStyle::validate(LaTeXFeatures & features) const
264 // Force inclusion of preamble snippet in layout file
265 features.require(params_.name);
266 InsetText::validate(features);
270 void InsetCharStyleParams::write(ostream & os) const
272 os << "CharStyle " << name << "\n";
276 void InsetCharStyleParams::read(Lexer & lex)
280 string token = lex.getString();
282 if (token == "CharStyle") {
284 name = lex.getString();
287 // This is handled in Collapsable
288 else if (token == "status") {
289 lex.pushToken(token);