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"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
25 #include "LaTeXFeatures.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
34 #include "frontends/Application.h"
35 #include "frontends/FontLoader.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
45 using std::istringstream;
48 using std::ostringstream;
51 void InsetCharStyle::init()
53 setInsetName("CharStyle");
60 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
61 : InsetCollapsable(bp)
69 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
70 CharStyles::iterator cs)
71 : InsetCollapsable(bp)
73 params_.type = cs->name;
79 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
80 : InsetCollapsable(in), params_(in.params_)
86 auto_ptr<InsetBase> InsetCharStyle::doClone() const
88 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
92 bool InsetCharStyle::undefined() const
94 return params_.latexname.empty();
98 void InsetCharStyle::setUndefined()
100 params_.latextype.clear();
101 params_.latexname.clear();
102 params_.latexparam.clear();
103 params_.font = LyXFont(LyXFont::ALL_INHERIT);
104 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
105 params_.labelfont.setColor(LColor::error);
109 void InsetCharStyle::setDefined(CharStyles::iterator cs)
111 params_.latextype = cs->latextype;
112 params_.latexname = cs->latexname;
113 params_.latexparam = cs->latexparam;
114 params_.font = cs->font;
115 params_.labelfont = cs->labelfont;
119 docstring const InsetCharStyle::editMessage() const
121 return _("Opened CharStyle Inset");
125 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
128 InsetCollapsable::write(buf, os);
132 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
134 InsetCollapsable::read(buf, lex);
139 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
141 LyXFont tmpfont = mi.base.font;
142 getDrawFont(mi.base.font);
143 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
144 mi.base.font.realize(tmpfont);
145 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
146 InsetText::metrics(mi, dim);
147 mi.base.font = tmpfont;
149 // consider width of the inset label
150 LyXFont font(params_.labelfont);
151 font.realize(LyXFont(LyXFont::ALL_SANE));
157 string s(params_.type);
160 s = lyx::to_utf8(_("Undef: ")) + s;
161 docstring ds(s.begin(), s.end());
162 theApp->fontLoader().metrics(font).rectText(ds, w, a, d);
163 dim.wid = max(dim.wid, w);
165 dim.asc += TEXT_TO_INSET_OFFSET;
166 dim.des += TEXT_TO_INSET_OFFSET;
167 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
168 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
171 dim_.des += ascent();
175 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
177 setPosCache(pi, x, y);
179 LyXFont tmpfont = pi.base.font;
180 getDrawFont(pi.base.font);
181 // I don't understand why the above .reduce and .realize aren't
182 //needed, or even wanted, here. It just works. -- MV 10.04.2005
183 InsetText::draw(pi, x, y);
184 pi.base.font = tmpfont;
186 int desc = InsetText::descent();
190 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
191 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
192 params_.labelfont.color());
193 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
194 params_.labelfont.color());
196 // the name of the charstyle. Can be toggled.
198 LyXFont font(params_.labelfont);
199 font.realize(LyXFont(LyXFont::ALL_SANE));
205 string s(params_.type);
208 s = lyx::to_utf8(_("Undef: ")) + s;
209 docstring ds(s.begin(), s.end());
210 theApp->fontLoader().metrics(font).rectText(ds, w, a, d);
211 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
212 ds, font, LColor::none, LColor::none);
215 // a visual clue when the cursor is inside the inset
216 LCursor & cur = pi.base.bv->cursor();
217 if (cur.isInside(this)) {
219 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
220 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
221 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
222 params_.labelfont.color());
223 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
224 params_.labelfont.color());
229 void InsetCharStyle::getDrawFont(LyXFont & font) const
235 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
238 switch (cmd.action) {
240 case LFUN_MOUSE_PRESS:
241 if (cmd.button() == mouse_button::button3)
242 has_label_ = !has_label_;
244 InsetText::doDispatch(cur, cmd);
248 InsetCollapsable::doDispatch(cur, cmd);
254 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
255 FuncStatus & status) const
257 switch (cmd.action) {
258 // paragraph breaks not allowed in charstyle insets
259 case LFUN_BREAK_PARAGRAPH:
260 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
261 case LFUN_BREAK_PARAGRAPH_SKIP:
262 status.enabled(false);
266 return InsetCollapsable::getStatus(cur, cmd, status);
271 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
272 OutputParams const & runparams) const
275 os << "\\" << params_.latexname;
276 if (!params_.latexparam.empty())
277 os << params_.latexparam;
280 int i = InsetText::latex(buf, os, runparams);
287 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
288 OutputParams const & runparams) const
290 ParagraphList::const_iterator beg = paragraphs().begin();
291 ParagraphList::const_iterator par = paragraphs().begin();
292 ParagraphList::const_iterator end = paragraphs().end();
295 sgml::openTag(os, params_.latexname,
296 par->getID(buf, runparams) + params_.latexparam);
298 for (; par != end; ++par) {
299 par->simpleDocBookOnePar(buf, os, runparams,
300 outerFont(std::distance(beg, par),
305 sgml::closeTag(os, params_.latexname);
311 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
312 OutputParams const & runparams) const
314 return InsetText::plaintext(buf, os, runparams);
318 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
319 OutputParams const & op) const
321 return plaintext(buf, os, op);
325 void InsetCharStyle::validate(LaTeXFeatures & features) const
327 // Force inclusion of preamble snippet in layout file
328 features.require(params_.type);
329 InsetText::validate(features);
333 void InsetCharStyleParams::write(ostream & os) const
335 os << "CharStyle " << type << "\n";
339 void InsetCharStyleParams::read(LyXLex & lex)
343 string token = lex.getString();
348 type = lex.getString();