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;
162 if (s.find(':') != string::npos)
163 s = s.substr(s.find(':'));
164 theFontMetrics(font).rectText(s, w, a, d);
165 dim.wid = max(dim.wid, w);
167 dim.asc += TEXT_TO_INSET_OFFSET;
168 dim.des += TEXT_TO_INSET_OFFSET;
169 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
170 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
171 if (params_.show_label)
173 bool const changed = dim_ != dim;
179 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
181 setPosCache(pi, x, y);
183 Font tmpfont = pi.base.font;
184 getDrawFont(pi.base.font);
185 // I don't understand why the above .reduce and .realize aren't
186 //needed, or even wanted, here. It just works. -- MV 10.04.2005
187 InsetText::draw(pi, x + TEXT_TO_INSET_OFFSET, y);
188 pi.base.font = tmpfont;
190 int desc = InsetText::descent();
191 if (params_.show_label)
194 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
195 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
196 params_.labelfont.color());
197 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
198 params_.labelfont.color());
200 // the name of the charstyle. Can be toggled.
201 if (params_.show_label) {
202 Font font(params_.labelfont);
203 font.realize(Font(Font::ALL_SANE));
210 docstring s(from_utf8(params_.type));
212 s = _("Undef: ") + s;
214 if (s.find(':') != string::npos)
215 s = s.substr(s.find(':'));
216 theFontMetrics(font).rectText(s, w, a, d);
217 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
218 s, font, Color::none, Color::none);
221 // a visual clue when the cursor is inside the inset
222 Cursor & cur = pi.base.bv->cursor();
223 if (cur.isInside(this)) {
225 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
226 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
227 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
228 params_.labelfont.color());
229 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
230 params_.labelfont.color());
235 void InsetCharStyle::getDrawFont(Font & font) const
241 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
243 switch (cmd.action) {
245 case LFUN_MOUSE_RELEASE:
246 if (cmd.button() == mouse_button::button3)
247 params_.show_label = !params_.show_label;
249 InsetCollapsable::doDispatch(cur, cmd);
252 case LFUN_INSET_TOGGLE:
253 if (cmd.argument() == "open")
254 params_.show_label = true;
255 else if (cmd.argument() == "close")
256 params_.show_label = false;
257 else if (cmd.argument() == "toggle" || cmd.argument().empty())
258 params_.show_label = !params_.show_label;
259 else // if assign or anything else
265 InsetCollapsable::doDispatch(cur, cmd);
271 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
272 FuncStatus & status) const
274 switch (cmd.action) {
275 // paragraph breaks not allowed in charstyle insets
276 case LFUN_BREAK_PARAGRAPH:
277 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
278 case LFUN_BREAK_PARAGRAPH_SKIP:
279 status.enabled(false);
283 return InsetCollapsable::getStatus(cur, cmd, status);
288 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
289 OutputParams const & runparams) const
293 os << '\\' << from_utf8(params_.latexname);
294 if (!params_.latexparam.empty())
295 os << from_utf8(params_.latexparam);
298 int i = InsetText::latex(buf, os, runparams);
305 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
306 OutputParams const & runparams) const
308 return InsetText::plaintext(buf, os, runparams);
312 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
313 OutputParams const & runparams) const
315 ParagraphList::const_iterator beg = paragraphs().begin();
316 ParagraphList::const_iterator par = paragraphs().begin();
317 ParagraphList::const_iterator end = paragraphs().end();
321 sgml::openTag(os, params_.latexname,
322 par->getID(buf, runparams) + params_.latexparam);
324 for (; par != end; ++par) {
325 par->simpleDocBookOnePar(buf, os, runparams,
326 outerFont(std::distance(beg, par),
331 sgml::closeTag(os, params_.latexname);
337 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
339 os << paragraphs().begin()->asString(buf, true);
343 void InsetCharStyle::validate(LaTeXFeatures & features) const
345 // Force inclusion of preamble snippet in layout file
346 features.require(params_.type);
347 InsetText::validate(features);
351 void InsetCharStyleParams::write(ostream & os) const
353 os << "CharStyle " << type << "\n";
354 os << "show_label " << convert<string>(show_label) << "\n";
358 void InsetCharStyleParams::read(Lexer & lex)
362 string token = lex.getString();
364 if (token == "CharStyle") {
366 type = lex.getString();
369 else if (token == "show_label") {
371 show_label = lex.getBool();
374 else if (token == "status") {
375 lex.pushToken(token);