2 * \file insetcharstyle.C
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"
17 #include "BufferView.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
37 using std::istringstream;
39 using std::ostringstream;
42 void InsetCharStyle::init()
44 setInsetName("CharStyle");
49 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
50 CharStyles::iterator cs)
51 : InsetCollapsable(bp), has_label_(true)
53 params_.type = cs->name;
54 params_.latextype = cs->latextype;
55 params_.latexname = cs->latexname;
56 params_.latexparam = cs->latexparam;
57 params_.font = cs->font;
58 params_.labelfont = cs->labelfont;
63 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
64 : InsetCollapsable(in), params_(in.params_), has_label_(true)
70 auto_ptr<InsetBase> InsetCharStyle::clone() const
72 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
76 string const InsetCharStyle::editMessage() const
78 return _("Opened CharStyle Inset");
82 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
85 InsetCollapsable::write(buf, os);
89 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
91 InsetCollapsable::read(buf, lex);
96 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
98 LyXFont tmpfont = mi.base.font;
99 getDrawFont(mi.base.font);
100 InsetCollapsable::metrics(mi, dim);
101 mi.base.font = tmpfont;
104 dim_.des += ascent();
108 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
110 setPosCache(pi, x, y);
112 // FIXME: setStatus(Inlined); this is not a const operation
113 LyXFont tmpfont = pi.base.font;
114 //setDrawFrame(InsetText::NEVER);
115 getDrawFont(pi.base.font);
116 InsetText::draw(pi, x, y);
117 pi.base.font = tmpfont;
119 pi.pain.line(x + 2, y + InsetText::descent() - 4, x + 2,
120 y + InsetText::descent(), params_.labelfont.color());
121 pi.pain.line(x + 2, y + InsetText::descent(), x + dim_.wid - 2,
122 y + InsetText::descent(), params_.labelfont.color());
123 pi.pain.line(x + dim_.wid - 2, y + InsetText::descent(), x + dim_.wid - 2,
124 y + InsetText::descent() - 4, params_.labelfont.color());
127 LyXFont font(params_.labelfont);
128 font.realize(LyXFont(LyXFont::ALL_SANE));
134 font_metrics::rectText(params_.type, font, w, a, d);
135 pi.pain.rectText(x + (dim_.wid - w) / 2,
136 y + InsetText::descent() + a,
137 params_.type, font, LColor::none, LColor::none);
142 void InsetCharStyle::getDrawFont(LyXFont & font) const
148 void InsetCharStyle::priv_dispatch(LCursor & cur, FuncRequest & cmd)
151 switch (cmd.action) {
152 case LFUN_MOUSE_PRESS:
153 if (cmd.button() == mouse_button::button3)
154 has_label_ = !has_label_;
156 InsetText::dispatch(cur, cmd);
160 InsetCollapsable::priv_dispatch(cur, cmd);
168 int outputVerbatim(std::ostream & os, InsetText const & inset)
171 ParagraphList::const_iterator par = inset.paragraphs().begin();
172 ParagraphList::const_iterator end = inset.paragraphs().end();
174 lyx::pos_type siz = par->size();
175 for (lyx::pos_type i = 0; i < siz; ++i) {
176 if (par->isNewline(i)) {
180 os << par->getChar(i);
195 int InsetCharStyle::latex(Buffer const &, ostream & os,
196 OutputParams const &) const
198 os << "%\n\\" << params_.latexname;
199 if (!params_.latexparam.empty())
200 os << params_.latexparam;
202 int i = outputVerbatim(os, *this);
209 int InsetCharStyle::linuxdoc(Buffer const &, std::ostream & os,
210 OutputParams const &) const
213 int i = outputVerbatim(oss, *this);
214 sgml::openTag(os, params_.latexname, params_.latexparam);
215 os << sgml::escapeString(oss.str());
216 sgml::closeTag(os, params_.latexname);
221 int InsetCharStyle::docbook(Buffer const &, std::ostream & os,
222 OutputParams const &) const
225 int i = outputVerbatim(oss, *this);
226 sgml::openTag(os, params_.latexname, params_.latexparam);
227 os << sgml::escapeString(oss.str());
228 sgml::closeTag(os, params_.latexname);
233 int InsetCharStyle::plaintext(Buffer const &, std::ostream & os,
234 OutputParams const & /*runparams*/) const
236 return outputVerbatim(os, *this);
240 void InsetCharStyle::validate(LaTeXFeatures & features) const
242 features.require(params_.type);
246 void InsetCharStyleParams::write(ostream & os) const
248 os << "CharStyle " << type << "\n";
252 void InsetCharStyleParams::read(LyXLex & lex)
256 string token = lex.getString();
261 type = lex.getString();