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"
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "support/std_sstream.h"
35 using std::istringstream;
37 using std::ostringstream;
40 void InsetCharStyle::init()
42 setInsetName("CharStyle");
47 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
48 CharStyles::iterator cs)
49 : InsetCollapsable(bp), has_label_(true)
51 params_.type = cs->name;
52 params_.latextype = cs->latextype;
53 params_.latexname = cs->latexname;
54 params_.latexparam = cs->latexparam;
55 params_.font = cs->font;
56 params_.labelfont = cs->labelfont;
61 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
62 : InsetCollapsable(in), params_(in.params_), has_label_(true)
68 auto_ptr<InsetBase> InsetCharStyle::clone() const
70 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
74 string const InsetCharStyle::editMessage() const
76 return _("Opened CharStyle Inset");
80 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
83 InsetCollapsable::write(buf, os);
87 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
89 InsetCollapsable::read(buf, lex);
94 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
96 LyXFont tmpfont = mi.base.font;
97 getDrawFont(mi.base.font);
98 InsetCollapsable::metrics(mi, dim);
99 mi.base.font = tmpfont;
102 dim_.des += ascent();
106 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
108 setPosCache(pi, x, y);
110 // FIXME: setStatus(Inlined); this is not a const operation
111 LyXFont tmpfont = pi.base.font;
112 inset.setDrawFrame(InsetText::NEVER);
113 getDrawFont(pi.base.font);
114 inset.draw(pi, x, y);
115 pi.base.font = tmpfont;
117 pi.pain.line(x + 2, y + inset.descent() - 4, x + 2,
118 y + inset.descent(), params_.labelfont.color());
119 pi.pain.line(x + 2, y + inset.descent(), x + dim_.wid - 2,
120 y + inset.descent(), params_.labelfont.color());
121 pi.pain.line(x + dim_.wid - 2, y + inset.descent(), x + dim_.wid - 2,
122 y + inset.descent() - 4, params_.labelfont.color());
128 LyXFont font(params_.labelfont);
129 font.realize(LyXFont(LyXFont::ALL_SANE));
135 font_metrics::rectText(params_.type, font, w, a, d);
136 pi.pain.rectText(x + (dim_.wid - w) / 2,
137 y + inset.descent() + a,
138 params_.type, font, LColor::none, LColor::none);
143 void InsetCharStyle::getDrawFont(LyXFont & font) const
150 InsetCharStyle::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
153 switch (cmd.action) {
154 case LFUN_MOUSE_PRESS:
155 if (cmd.button() == mouse_button::button3) {
156 has_label_ = !has_label_;
157 return DispatchResult(true);
159 inset.dispatch(cur, cmd);
160 return DispatchResult(true, true);
162 return InsetCollapsable::priv_dispatch(cur, cmd);
169 int outputVerbatim(std::ostream & os, InsetText inset)
172 ParagraphList::iterator par = inset.paragraphs().begin();
173 ParagraphList::iterator end = inset.paragraphs().end();
175 lyx::pos_type siz = par->size();
176 for (lyx::pos_type i = 0; i < siz; ++i) {
177 if (par->isNewline(i)) {
181 os << par->getChar(i);
196 int InsetCharStyle::latex(Buffer const &, ostream & os,
197 OutputParams const &) const
199 os << "%\n\\" << params_.latexname;
200 if (!params_.latexparam.empty())
201 os << params_.latexparam;
203 int i = outputVerbatim(os, inset);
210 int InsetCharStyle::linuxdoc(Buffer const &, std::ostream & os,
211 OutputParams const &) const
213 os << "<" << params_.latexname;
214 if (!params_.latexparam.empty())
215 os << " " << params_.latexparam;
217 int const i = outputVerbatim(os, inset);
218 os << "</" << params_.latexname << ">";
223 int InsetCharStyle::docbook(Buffer const &, std::ostream & os,
224 OutputParams const &) const
226 os << "<" << params_.latexname;
227 if (!params_.latexparam.empty())
228 os << " " << params_.latexparam;
230 int const i = outputVerbatim(os, inset);
231 os << "</" << params_.latexname << ">";
236 int InsetCharStyle::plaintext(Buffer const &, std::ostream & os,
237 OutputParams const & /*runparams*/) const
239 return outputVerbatim(os, inset);
243 void InsetCharStyle::validate(LaTeXFeatures & features) const
245 features.require(params_.type);
249 void InsetCharStyleParams::write(ostream & os) const
251 os << "CharStyle " << type << "\n";
255 void InsetCharStyleParams::read(LyXLex & lex)
259 string token = lex.getString();
264 type = lex.getString();