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;
43 using std::ostringstream;
46 void InsetCharStyle::init()
48 setInsetName("CharStyle");
55 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
56 : InsetCollapsable(bp)
64 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
65 CharStyles::iterator cs)
66 : InsetCollapsable(bp)
68 params_.type = cs->name;
74 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
75 : InsetCollapsable(in), params_(in.params_)
81 auto_ptr<InsetBase> InsetCharStyle::doClone() const
83 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
87 bool InsetCharStyle::undefined() const
89 return params_.latexname.empty();
93 void InsetCharStyle::setUndefined()
95 params_.latextype.clear();
96 params_.latexname.clear();
97 params_.latexparam.clear();
98 params_.font = LyXFont(LyXFont::ALL_INHERIT);
99 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
100 params_.labelfont.setColor(LColor::error);
104 void InsetCharStyle::setDefined(CharStyles::iterator cs)
106 params_.latextype = cs->latextype;
107 params_.latexname = cs->latexname;
108 params_.latexparam = cs->latexparam;
109 params_.font = cs->font;
110 params_.labelfont = cs->labelfont;
114 string const InsetCharStyle::editMessage() const
116 return _("Opened CharStyle Inset");
120 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
123 InsetCollapsable::write(buf, os);
127 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
129 InsetCollapsable::read(buf, lex);
134 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
136 LyXFont tmpfont = mi.base.font;
137 getDrawFont(mi.base.font);
138 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
139 mi.base.font.realize(tmpfont);
140 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
141 InsetText::metrics(mi, dim);
142 mi.base.font = tmpfont;
144 // consider width of the inset label
145 LyXFont font(params_.labelfont);
146 font.realize(LyXFont(LyXFont::ALL_SANE));
152 string s(params_.type);
154 s = _("Undef: ") + s;
155 font_metrics::rectText(s, font, w, a, d);
156 dim.wid = max(dim.wid, w);
158 dim.asc += TEXT_TO_INSET_OFFSET;
159 dim.des += TEXT_TO_INSET_OFFSET;
160 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
161 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
164 dim_.des += ascent();
168 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
170 setPosCache(pi, x, y);
172 LyXFont tmpfont = pi.base.font;
173 getDrawFont(pi.base.font);
174 // I don't understand why the above .reduce and .realize aren't
175 //needed, or even wanted, here. It just works. -- MV 10.04.2005
176 InsetText::draw(pi, x, y);
177 pi.base.font = tmpfont;
179 int desc = InsetText::descent();
183 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
184 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
185 params_.labelfont.color());
186 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
187 params_.labelfont.color());
189 // the name of the charstyle. Can be toggled.
191 LyXFont font(params_.labelfont);
192 font.realize(LyXFont(LyXFont::ALL_SANE));
198 string s(params_.type);
200 s = _("Undef: ") + s;
201 font_metrics::rectText(s, font, w, a, d);
202 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
203 s, font, LColor::none, LColor::none);
206 // a visual clue when the cursor is inside the inset
207 LCursor & cur = pi.base.bv->cursor();
208 if (cur.isInside(this)) {
210 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
211 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
212 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
213 params_.labelfont.color());
214 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
215 params_.labelfont.color());
220 void InsetCharStyle::getDrawFont(LyXFont & font) const
226 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
229 switch (cmd.action) {
230 case LFUN_MOUSE_PRESS:
231 if (cmd.button() == mouse_button::button3)
232 has_label_ = !has_label_;
234 InsetText::doDispatch(cur, cmd);
238 InsetCollapsable::doDispatch(cur, cmd);
244 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
245 FuncStatus & status) const
247 switch (cmd.action) {
248 // paragraph breaks not allowed in charstyle insets
249 case LFUN_BREAKPARAGRAPH:
250 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
251 case LFUN_BREAKPARAGRAPH_SKIP:
252 status.enabled(false);
256 return InsetCollapsable::getStatus(cur, cmd, status);
261 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
262 OutputParams const & runparams) const
265 os << "\\" << params_.latexname;
266 if (!params_.latexparam.empty())
267 os << params_.latexparam;
270 int i = InsetText::latex(buf, os, runparams);
277 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
278 OutputParams const & runparams) const
281 sgml::openTag(os, params_.latexname, params_.latexparam);
282 int i = InsetText::linuxdoc(buf, os, runparams);
284 sgml::closeTag(os, params_.latexname);
289 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
290 OutputParams const & runparams) const
292 ParagraphList::const_iterator par = paragraphs().begin();
293 ParagraphList::const_iterator end = paragraphs().end();
296 sgml::openTag(os, params_.latexname,
297 par->getID(buf, runparams) + params_.latexparam);
299 for (; par != end; ++par) {
300 par->simpleDocBookOnePar(buf, os, runparams,
301 outerFont(par - paragraphs().begin(),
306 sgml::closeTag(os, params_.latexname);
312 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
313 OutputParams const & runparams) const
315 return InsetText::plaintext(buf, os, runparams);
319 void InsetCharStyle::validate(LaTeXFeatures & features) const
321 // Force inclusion of preamble snippet in layout file
322 features.require(params_.type);
323 InsetText::validate(features);
327 void InsetCharStyleParams::write(ostream & os) const
329 os << "CharStyle " << type << "\n";
333 void InsetCharStyleParams::read(LyXLex & lex)
337 string token = lex.getString();
342 type = lex.getString();