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()
56 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
57 : InsetCollapsable(bp, Collapsed)
65 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
66 CharStyles::iterator cs)
67 : InsetCollapsable(bp, Collapsed)
69 params_.name = cs->name;
75 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
76 : InsetCollapsable(in), params_(in.params_)
82 auto_ptr<Inset> InsetCharStyle::doClone() const
84 return auto_ptr<Inset>(new InsetCharStyle(*this));
88 bool InsetCharStyle::undefined() const
90 return layout_.latexname.empty();
94 void InsetCharStyle::setUndefined()
96 layout_.latextype.clear();
97 layout_.latexname.clear();
98 layout_.latexparam.clear();
99 layout_.font = Font(Font::ALL_INHERIT);
100 layout_.labelfont = Font(Font::ALL_INHERIT);
101 layout_.labelfont.setColor(Color::error);
105 void InsetCharStyle::setDefined(CharStyles::iterator cs)
111 docstring const InsetCharStyle::editMessage() const
113 return _("Opened CharStyle Inset");
117 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
120 InsetCollapsable::write(buf, os);
124 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
127 InsetCollapsable::read(buf, lex);
131 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
133 Font tmpfont = mi.base.font;
134 getDrawFont(mi.base.font);
135 mi.base.font.reduce(Font(Font::ALL_SANE));
136 mi.base.font.realize(tmpfont);
137 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
138 InsetText::metrics(mi, dim);
139 mi.base.font = tmpfont;
140 if (status() == Open) {
141 // consider width of the inset label
142 Font font(layout_.labelfont);
143 font.realize(Font(Font::ALL_SANE));
150 docstring s(from_utf8(params_.name));
152 s = _("Undef: ") + s;
154 if (s.find(':') != string::npos)
155 s = s.substr(s.find(':'));
156 theFontMetrics(font).rectText(s, w, a, d);
157 dim.wid = max(dim.wid, w);
159 dim.asc += TEXT_TO_INSET_OFFSET;
160 dim.des += TEXT_TO_INSET_OFFSET;
161 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
162 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
163 if (status() == Open)
169 bool const changed = dim_ != dim;
175 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
177 setPosCache(pi, x, y);
179 Font 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 InsetCollapsable::draw(pi, x, y);
184 pi.base.font = tmpfont;
186 int desc = InsetText::descent();
187 if (status() == Open)
192 pi.pain.line(x, y + desc - 4, x, y + desc, layout_.labelfont.color());
193 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
194 layout_.labelfont.color());
195 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
196 layout_.labelfont.color());
198 // the name of the charstyle. Can be toggled.
199 if (status() == Open) {
200 Font font(layout_.labelfont);
201 font.realize(Font(Font::ALL_SANE));
208 docstring s(from_utf8(params_.name));
210 s = _("Undef: ") + s;
212 if (s.find(':') != string::npos)
213 s = s.substr(s.find(':'));
214 theFontMetrics(font).rectText(s, w, a, d);
215 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
216 s, font, Color::none, Color::none);
219 // a visual cue when the cursor is inside the inset
220 Cursor & cur = pi.base.bv->cursor();
221 if (cur.isInside(this)) {
224 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
225 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
226 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
227 layout_.labelfont.color());
228 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
229 layout_.labelfont.color());
234 void InsetCharStyle::getDrawFont(Font & font) const
240 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
242 switch (cmd.action) {
244 case LFUN_MOUSE_RELEASE:
245 if (cmd.button() == mouse_button::button3)
246 if (status() == Open)
247 setStatus(cur, Collapsed);
249 setStatus(cur, Open);
251 InsetCollapsable::doDispatch(cur, cmd);
254 case LFUN_INSET_TOGGLE:
255 if (cmd.argument() == "open")
256 setStatus(cur, Open);
257 else if (cmd.argument() == "close")
258 setStatus(cur, Collapsed);
259 else if (cmd.argument() == "toggle" || cmd.argument().empty())
260 if (status() == Open)
261 setStatus(cur, Collapsed);
263 setStatus(cur, Open);
264 else // if assign or anything else
270 InsetCollapsable::doDispatch(cur, cmd);
276 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
277 FuncStatus & status) const
279 switch (cmd.action) {
280 // paragraph breaks not allowed in charstyle insets
281 case LFUN_BREAK_PARAGRAPH:
282 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
283 case LFUN_BREAK_PARAGRAPH_SKIP:
284 status.enabled(false);
288 return InsetCollapsable::getStatus(cur, cmd, status);
293 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
294 OutputParams const & runparams) const
298 os << '\\' << from_utf8(layout_.latexname);
299 if (!layout_.latexparam.empty())
300 os << from_utf8(layout_.latexparam);
303 int i = InsetText::latex(buf, os, runparams);
310 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
311 OutputParams const & runparams) const
313 return InsetText::plaintext(buf, os, runparams);
317 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
318 OutputParams const & runparams) const
320 ParagraphList::const_iterator beg = paragraphs().begin();
321 ParagraphList::const_iterator par = paragraphs().begin();
322 ParagraphList::const_iterator end = paragraphs().end();
326 sgml::openTag(os, layout_.latexname,
327 par->getID(buf, runparams) + layout_.latexparam);
329 for (; par != end; ++par) {
330 par->simpleDocBookOnePar(buf, os, runparams,
331 outerFont(std::distance(beg, par),
336 sgml::closeTag(os, layout_.latexname);
342 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
344 os << paragraphs().begin()->asString(buf, true);
348 void InsetCharStyle::validate(LaTeXFeatures & features) const
350 // Force inclusion of preamble snippet in layout file
351 features.require(params_.name);
352 InsetText::validate(features);
356 void InsetCharStyleParams::write(ostream & os) const
358 os << "CharStyle " << name << "\n";
362 void InsetCharStyleParams::read(Lexer & lex)
366 string token = lex.getString();
368 if (token == "CharStyle") {
370 name = lex.getString();
373 // This is handled in Collapsable
374 else if (token == "status") {
375 lex.pushToken(token);