2 * \file InsetCharStyle.cpp
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/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "support/convert.h"
46 using std::istringstream;
49 using std::ostringstream;
52 void InsetCharStyle::init()
58 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
59 : InsetCollapsable(bp)
67 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
68 CharStyles::iterator cs)
69 : InsetCollapsable(bp)
71 params_.type = cs->name;
77 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
78 : InsetCollapsable(in), params_(in.params_)
84 auto_ptr<Inset> InsetCharStyle::doClone() const
86 return auto_ptr<Inset>(new InsetCharStyle(*this));
90 bool InsetCharStyle::undefined() const
92 return params_.latexname.empty();
96 void InsetCharStyle::setUndefined()
98 params_.latextype.clear();
99 params_.latexname.clear();
100 params_.latexparam.clear();
101 params_.font = Font(Font::ALL_INHERIT);
102 params_.labelfont = Font(Font::ALL_INHERIT);
103 params_.labelfont.setColor(Color::error);
104 params_.show_label = true;
108 void InsetCharStyle::setDefined(CharStyles::iterator cs)
110 params_.latextype = cs->latextype;
111 params_.latexname = cs->latexname;
112 params_.latexparam = cs->latexparam;
113 params_.font = cs->font;
114 params_.labelfont = cs->labelfont;
115 params_.show_label = true;
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, Lexer & lex)
135 InsetCollapsable::read(buf, lex);
139 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
141 Font tmpfont = mi.base.font;
142 getDrawFont(mi.base.font);
143 mi.base.font.reduce(Font(Font::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;
148 if (params_.show_label) {
149 // consider width of the inset label
150 Font font(params_.labelfont);
151 font.realize(Font(Font::ALL_SANE));
158 docstring s(from_utf8(params_.type));
160 s = _("Undef: ") + s;
161 theFontMetrics(font).rectText(s, w, a, d);
162 dim.wid = max(dim.wid, w);
164 dim.asc += TEXT_TO_INSET_OFFSET;
165 dim.des += TEXT_TO_INSET_OFFSET;
166 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
167 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
168 if (params_.show_label)
170 bool const changed = dim_ != dim;
176 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
178 setPosCache(pi, x, y);
180 Font tmpfont = pi.base.font;
181 getDrawFont(pi.base.font);
182 // I don't understand why the above .reduce and .realize aren't
183 //needed, or even wanted, here. It just works. -- MV 10.04.2005
184 InsetText::draw(pi, x + TEXT_TO_INSET_OFFSET, y);
185 pi.base.font = tmpfont;
187 int desc = InsetText::descent();
188 if (params_.show_label)
191 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
192 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
193 params_.labelfont.color());
194 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
195 params_.labelfont.color());
197 // the name of the charstyle. Can be toggled.
198 if (params_.show_label) {
199 Font font(params_.labelfont);
200 font.realize(Font(Font::ALL_SANE));
207 docstring s(from_utf8(params_.type));
209 s = _("Undef: ") + s;
210 theFontMetrics(font).rectText(s, w, a, d);
211 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
212 s, font, Color::none, Color::none);
215 // a visual clue when the cursor is inside the inset
216 Cursor & 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(Font & font) const
235 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
237 switch (cmd.action) {
239 case LFUN_MOUSE_RELEASE:
240 if (cmd.button() == mouse_button::button3)
241 params_.show_label = !params_.show_label;
243 InsetCollapsable::doDispatch(cur, cmd);
246 case LFUN_INSET_TOGGLE:
247 if (cmd.argument() == "open")
248 params_.show_label = true;
249 else if (cmd.argument() == "close")
250 params_.show_label = false;
251 else if (cmd.argument() == "toggle" || cmd.argument().empty())
252 params_.show_label = !params_.show_label;
253 else // if assign or anything else
259 InsetCollapsable::doDispatch(cur, cmd);
265 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
266 FuncStatus & status) const
268 switch (cmd.action) {
269 // paragraph breaks not allowed in charstyle insets
270 case LFUN_BREAK_PARAGRAPH:
271 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
272 case LFUN_BREAK_PARAGRAPH_SKIP:
273 status.enabled(false);
277 return InsetCollapsable::getStatus(cur, cmd, status);
282 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
283 OutputParams const & runparams) const
287 os << '\\' << from_utf8(params_.latexname);
288 if (!params_.latexparam.empty())
289 os << from_utf8(params_.latexparam);
292 int i = InsetText::latex(buf, os, runparams);
299 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
300 OutputParams const & runparams) const
302 return InsetText::plaintext(buf, os, runparams);
306 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
307 OutputParams const & runparams) const
309 ParagraphList::const_iterator beg = paragraphs().begin();
310 ParagraphList::const_iterator par = paragraphs().begin();
311 ParagraphList::const_iterator end = paragraphs().end();
315 sgml::openTag(os, params_.latexname,
316 par->getID(buf, runparams) + params_.latexparam);
318 for (; par != end; ++par) {
319 par->simpleDocBookOnePar(buf, os, runparams,
320 outerFont(std::distance(beg, par),
325 sgml::closeTag(os, params_.latexname);
331 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
333 os << paragraphs().begin()->asString(buf, true);
337 void InsetCharStyle::validate(LaTeXFeatures & features) const
339 // Force inclusion of preamble snippet in layout file
340 features.require(params_.type);
341 InsetText::validate(features);
345 void InsetCharStyleParams::write(ostream & os) const
347 os << "CharStyle " << type << "\n";
348 os << "show_label " << convert<string>(show_label) << "\n";
352 void InsetCharStyleParams::read(Lexer & lex)
356 string token = lex.getString();
358 if (token == "CharStyle") {
360 type = lex.getString();
363 else if (token == "show_label") {
365 show_label = lex.getBool();
368 else if (token == "status") {
369 lex.pushToken(token);