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, string const s)
55 : InsetCollapsable(bp)
63 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
64 CharStyles::iterator cs)
65 : InsetCollapsable(bp)
67 params_.type = cs->name;
73 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
74 : InsetCollapsable(in), params_(in.params_)
80 auto_ptr<InsetBase> InsetCharStyle::doClone() const
82 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
86 bool InsetCharStyle::undefined() const
88 return params_.latexname.empty();
92 void InsetCharStyle::setUndefined()
94 params_.latextype.clear();
95 params_.latexname.clear();
96 params_.latexparam.clear();
97 params_.font = LyXFont(LyXFont::ALL_INHERIT);
98 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
99 params_.labelfont.setColor(LColor::red);
103 void InsetCharStyle::setDefined(CharStyles::iterator cs)
105 params_.latextype = cs->latextype;
106 params_.latexname = cs->latexname;
107 params_.latexparam = cs->latexparam;
108 params_.font = cs->font;
109 params_.labelfont = cs->labelfont;
113 string const InsetCharStyle::editMessage() const
115 return _("Opened CharStyle Inset");
119 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
122 InsetCollapsable::write(buf, os);
126 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
128 InsetCollapsable::read(buf, lex);
133 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
135 LyXFont tmpfont = mi.base.font;
136 getDrawFont(mi.base.font);
137 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
138 mi.base.font.realize(tmpfont);
139 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
140 InsetText::metrics(mi, dim);
141 mi.base.font = tmpfont;
142 dim.asc += TEXT_TO_INSET_OFFSET;
143 dim.des += TEXT_TO_INSET_OFFSET;
144 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
148 dim_.des += ascent();
152 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
154 setPosCache(pi, x, y);
156 LyXFont 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 InsetText::draw(pi, x, y);
161 pi.base.font = tmpfont;
163 int desc = InsetText::descent();
167 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
168 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
169 params_.labelfont.color());
170 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
171 params_.labelfont.color());
173 // the name of the charstyle. Can be toggled.
175 LyXFont font(params_.labelfont);
176 font.realize(LyXFont(LyXFont::ALL_SANE));
182 string s(params_.type);
184 s = _("Undef: ") + s;
185 font_metrics::rectText(params_.type, font, w, a, d);
186 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
187 s, font, LColor::none, LColor::none);
190 // a visual clue when the cursor is inside the inset
191 LCursor & cur = pi.base.bv->cursor();
192 if (cur.isInside(this)) {
194 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
195 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
196 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
197 params_.labelfont.color());
198 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
199 params_.labelfont.color());
204 void InsetCharStyle::getDrawFont(LyXFont & font) const
210 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
213 switch (cmd.action) {
214 case LFUN_MOUSE_PRESS:
215 if (cmd.button() == mouse_button::button3)
216 has_label_ = !has_label_;
218 InsetText::doDispatch(cur, cmd);
222 InsetCollapsable::doDispatch(cur, cmd);
228 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
229 FuncStatus & status) const
231 switch (cmd.action) {
232 // paragraph breaks not allowed in charstyle insets
233 case LFUN_BREAKPARAGRAPH:
234 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
235 case LFUN_BREAKPARAGRAPH_SKIP:
236 status.enabled(false);
240 return InsetCollapsable::getStatus(cur, cmd, status);
245 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
246 OutputParams const & runparams) const
249 os << "\\" << params_.latexname;
250 if (!params_.latexparam.empty())
251 os << params_.latexparam;
254 int i = InsetText::latex(buf, os, runparams);
261 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
262 OutputParams const & runparams) const
265 sgml::openTag(os, params_.latexname, params_.latexparam);
266 int i = InsetText::linuxdoc(buf, os, runparams);
268 sgml::closeTag(os, params_.latexname);
273 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
274 OutputParams const & runparams) const
276 ParagraphList::const_iterator par = paragraphs().begin();
277 ParagraphList::const_iterator end = paragraphs().end();
280 sgml::openTag(os, params_.latexname,
281 par->getID(buf, runparams) + params_.latexparam);
283 for (; par != end; ++par) {
284 par->simpleDocBookOnePar(buf, os, runparams,
285 outerFont(par - paragraphs().begin(),
290 sgml::closeTag(os, params_.latexname);
296 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
297 OutputParams const & runparams) const
299 return InsetText::plaintext(buf, os, runparams);
303 void InsetCharStyle::validate(LaTeXFeatures & features) const
305 // Force inclusion of preamble snippet in layout file
306 features.require(params_.type);
307 InsetText::validate(features);
311 void InsetCharStyleParams::write(ostream & os) const
313 os << "CharStyle " << type << "\n";
317 void InsetCharStyleParams::read(LyXLex & lex)
321 string token = lex.getString();
326 type = lex.getString();