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"
20 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
32 #include "frontends/font_metrics.h"
33 #include "frontends/Painter.h"
40 using std::istringstream;
42 using std::ostringstream;
45 void InsetCharStyle::init()
47 setInsetName("CharStyle");
54 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
55 CharStyles::iterator cs)
56 : InsetCollapsable(bp)
58 params_.type = cs->name;
59 params_.latextype = cs->latextype;
60 params_.latexname = cs->latexname;
61 params_.latexparam = cs->latexparam;
62 params_.font = cs->font;
63 params_.labelfont = cs->labelfont;
68 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
69 : InsetCollapsable(in), params_(in.params_)
75 auto_ptr<InsetBase> InsetCharStyle::doClone() const
77 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
81 string const InsetCharStyle::editMessage() const
83 return _("Opened CharStyle Inset");
87 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
90 InsetCollapsable::write(buf, os);
94 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
96 InsetCollapsable::read(buf, lex);
101 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
103 LyXFont tmpfont = mi.base.font;
104 getDrawFont(mi.base.font);
105 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
106 InsetText::metrics(mi, dim);
107 mi.base.font = tmpfont;
108 dim.asc += TEXT_TO_INSET_OFFSET;
109 dim.des += TEXT_TO_INSET_OFFSET;
110 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
111 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
114 dim_.des += ascent();
118 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
120 setPosCache(pi, x, y);
122 LyXFont tmpfont = pi.base.font;
123 getDrawFont(pi.base.font);
124 InsetText::draw(pi, x, y);
125 pi.base.font = tmpfont;
127 int desc = InsetText::descent();
131 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
132 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
133 params_.labelfont.color());
134 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
135 params_.labelfont.color());
137 // the name of the charstyle. Can be toggled.
139 LyXFont font(params_.labelfont);
140 font.realize(LyXFont(LyXFont::ALL_SANE));
146 font_metrics::rectText(params_.type, font, w, a, d);
147 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
148 params_.type, font, LColor::none, LColor::none);
151 // a visual clue when the cursor is inside the inset
152 LCursor & cur = pi.base.bv->cursor();
153 if (cur.isInside(this)) {
155 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
156 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
157 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
158 params_.labelfont.color());
159 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
160 params_.labelfont.color());
165 void InsetCharStyle::getDrawFont(LyXFont & font) const
171 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
174 switch (cmd.action) {
175 case LFUN_MOUSE_PRESS:
176 if (cmd.button() == mouse_button::button3)
177 has_label_ = !has_label_;
179 InsetText::doDispatch(cur, cmd);
183 InsetCollapsable::doDispatch(cur, cmd);
189 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
190 FuncStatus & status) const
192 switch (cmd.action) {
193 // paragraph breaks not allowed in charstyle insets
194 case LFUN_BREAKPARAGRAPH:
195 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
196 case LFUN_BREAKPARAGRAPH_SKIP:
197 status.enabled(false);
201 return InsetCollapsable::getStatus(cur, cmd, status);
206 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
207 OutputParams const & runparams) const
209 os << "\\" << params_.latexname;
210 if (!params_.latexparam.empty())
211 os << params_.latexparam;
213 int i = InsetText::latex(buf, os, runparams);
219 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
220 OutputParams const & runparams) const
222 sgml::openTag(os, params_.latexname, params_.latexparam);
223 int i = InsetText::linuxdoc(buf, os, runparams);
224 sgml::closeTag(os, params_.latexname);
229 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
230 OutputParams const & runparams) const
232 ParagraphList::const_iterator par = paragraphs().begin();
233 ParagraphList::const_iterator end = paragraphs().end();
235 sgml::openTag(os, params_.latexname, par->getID(buf, runparams) + params_.latexparam);
237 for (; par != end; ++par) {
238 par->simpleDocBookOnePar(buf, os, runparams,
239 outerFont(par - paragraphs().begin(),
243 sgml::closeTag(os, params_.latexname);
248 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
249 OutputParams const & runparams) const
251 return InsetText::plaintext(buf, os, runparams);
255 void InsetCharStyle::validate(LaTeXFeatures & features) const
257 features.require(params_.type);
261 void InsetCharStyleParams::write(ostream & os) const
263 os << "CharStyle " << type << "\n";
267 void InsetCharStyleParams::read(LyXLex & lex)
271 string token = lex.getString();
276 type = lex.getString();