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()
59 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
60 : InsetCollapsable(bp)
68 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
69 CharStyles::iterator cs)
70 : InsetCollapsable(bp)
72 params_.type = cs->name;
78 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
79 : InsetCollapsable(in), params_(in.params_)
85 auto_ptr<Inset> InsetCharStyle::doClone() const
87 return auto_ptr<Inset>(new InsetCharStyle(*this));
91 bool InsetCharStyle::undefined() const
93 return params_.latexname.empty();
97 void InsetCharStyle::setUndefined()
99 params_.latextype.clear();
100 params_.latexname.clear();
101 params_.latexparam.clear();
102 params_.font = Font(Font::ALL_INHERIT);
103 params_.labelfont = Font(Font::ALL_INHERIT);
104 params_.labelfont.setColor(Color::error);
105 params_.show_label = true;
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;
116 params_.show_label = true;
120 docstring const InsetCharStyle::editMessage() const
122 return _("Opened CharStyle Inset");
126 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
129 InsetCollapsable::write(buf, os);
133 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
136 InsetCollapsable::read(buf, lex);
141 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
143 Font tmpfont = mi.base.font;
144 getDrawFont(mi.base.font);
145 mi.base.font.reduce(Font(Font::ALL_SANE));
146 mi.base.font.realize(tmpfont);
147 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
148 InsetText::metrics(mi, dim);
149 mi.base.font = tmpfont;
150 if (params_.show_label) {
151 // consider width of the inset label
152 Font font(params_.labelfont);
153 font.realize(Font(Font::ALL_SANE));
160 docstring s(from_utf8(params_.type));
162 s = _("Undef: ") + s;
163 theFontMetrics(font).rectText(s, w, a, d);
164 dim.wid = max(dim.wid, w);
166 dim.asc += TEXT_TO_INSET_OFFSET;
167 dim.des += TEXT_TO_INSET_OFFSET;
168 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
169 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
170 if (params_.show_label)
172 bool const changed = dim_ != dim;
178 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
180 setPosCache(pi, x, y);
182 Font tmpfont = pi.base.font;
183 getDrawFont(pi.base.font);
184 // I don't understand why the above .reduce and .realize aren't
185 //needed, or even wanted, here. It just works. -- MV 10.04.2005
186 InsetText::draw(pi, x + TEXT_TO_INSET_OFFSET, y);
187 pi.base.font = tmpfont;
189 int desc = InsetText::descent();
190 if (params_.show_label)
193 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
194 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
195 params_.labelfont.color());
196 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
197 params_.labelfont.color());
199 // the name of the charstyle. Can be toggled.
200 if (params_.show_label) {
201 Font font(params_.labelfont);
202 font.realize(Font(Font::ALL_SANE));
209 docstring s(from_utf8(params_.type));
211 s = _("Undef: ") + s;
212 theFontMetrics(font).rectText(s, w, a, d);
213 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
214 s, font, Color::none, Color::none);
217 // a visual clue when the cursor is inside the inset
218 Cursor & cur = pi.base.bv->cursor();
219 if (cur.isInside(this)) {
221 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
222 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
223 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
224 params_.labelfont.color());
225 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
226 params_.labelfont.color());
231 void InsetCharStyle::getDrawFont(Font & font) const
237 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
240 switch (cmd.action) {
242 case LFUN_MOUSE_RELEASE:
243 if (cmd.button() == mouse_button::button3)
244 params_.show_label = !params_.show_label;
246 InsetCollapsable::doDispatch(cur, cmd);
249 case LFUN_INSET_TOGGLE:
250 if (cmd.argument() == "open")
251 params_.show_label = true;
252 else if (cmd.argument() == "close")
253 params_.show_label = false;
254 else if (cmd.argument() == "toggle" || cmd.argument().empty())
255 params_.show_label = !params_.show_label;
256 else // if assign or anything else
262 InsetCollapsable::doDispatch(cur, cmd);
268 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
269 FuncStatus & status) const
271 switch (cmd.action) {
272 // paragraph breaks not allowed in charstyle insets
273 case LFUN_BREAK_PARAGRAPH:
274 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
275 case LFUN_BREAK_PARAGRAPH_SKIP:
276 status.enabled(false);
280 return InsetCollapsable::getStatus(cur, cmd, status);
285 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
286 OutputParams const & runparams) const
290 os << '\\' << from_utf8(params_.latexname);
291 if (!params_.latexparam.empty())
292 os << from_utf8(params_.latexparam);
295 int i = InsetText::latex(buf, os, runparams);
302 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
303 OutputParams const & runparams) const
305 return InsetText::plaintext(buf, os, runparams);
309 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
310 OutputParams const & runparams) const
312 ParagraphList::const_iterator beg = paragraphs().begin();
313 ParagraphList::const_iterator par = paragraphs().begin();
314 ParagraphList::const_iterator end = paragraphs().end();
318 sgml::openTag(os, params_.latexname,
319 par->getID(buf, runparams) + params_.latexparam);
321 for (; par != end; ++par) {
322 par->simpleDocBookOnePar(buf, os, runparams,
323 outerFont(std::distance(beg, par),
328 sgml::closeTag(os, params_.latexname);
334 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
336 os << paragraphs().begin()->asString(buf, true);
340 void InsetCharStyle::validate(LaTeXFeatures & features) const
342 // Force inclusion of preamble snippet in layout file
343 features.require(params_.type);
344 InsetText::validate(features);
348 void InsetCharStyleParams::write(ostream & os) const
350 os << "CharStyle " << type << "\n";
351 os << "show_label " << convert<string>(show_label) << "\n";
355 void InsetCharStyleParams::read(Lexer & lex)
359 string token = lex.getString();
361 if (token == "CharStyle") {
363 type = lex.getString();
366 else if (token == "show_label") {
368 show_label = lex.getBool();
371 else if (token == "status") {
372 lex.pushToken(token);