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_.type = 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 params_.latexname.empty();
94 void InsetCharStyle::setUndefined()
96 params_.latextype.clear();
97 params_.latexname.clear();
98 params_.latexparam.clear();
99 params_.font = Font(Font::ALL_INHERIT);
100 params_.labelfont = Font(Font::ALL_INHERIT);
101 params_.labelfont.setColor(Color::error);
105 void InsetCharStyle::setDefined(CharStyles::iterator cs)
107 params_.latextype = cs->latextype;
108 params_.latexname = cs->latexname;
109 params_.latexparam = cs->latexparam;
110 params_.font = cs->font;
111 params_.labelfont = cs->labelfont;
115 docstring const InsetCharStyle::editMessage() const
117 return _("Opened CharStyle Inset");
121 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
124 InsetCollapsable::write(buf, os);
128 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
131 InsetCollapsable::read(buf, lex);
135 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
137 Font tmpfont = mi.base.font;
138 getDrawFont(mi.base.font);
139 mi.base.font.reduce(Font(Font::ALL_SANE));
140 mi.base.font.realize(tmpfont);
141 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
142 InsetText::metrics(mi, dim);
143 mi.base.font = tmpfont;
144 if (status() == Open) {
145 // consider width of the inset label
146 Font font(params_.labelfont);
147 font.realize(Font(Font::ALL_SANE));
154 docstring s(from_utf8(params_.type));
156 s = _("Undef: ") + s;
158 if (s.find(':') != string::npos)
159 s = s.substr(s.find(':'));
160 theFontMetrics(font).rectText(s, w, a, d);
161 dim.wid = max(dim.wid, w);
163 dim.asc += TEXT_TO_INSET_OFFSET;
164 dim.des += TEXT_TO_INSET_OFFSET;
165 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
166 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
167 if (status() == Open)
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 InsetCollapsable::draw(pi, x, y);
188 pi.base.font = tmpfont;
190 int desc = InsetText::descent();
191 if (status() == Open)
196 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
197 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
198 params_.labelfont.color());
199 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
200 params_.labelfont.color());
202 // the name of the charstyle. Can be toggled.
203 if (status() == Open) {
204 Font font(params_.labelfont);
205 font.realize(Font(Font::ALL_SANE));
212 docstring s(from_utf8(params_.type));
214 s = _("Undef: ") + s;
216 if (s.find(':') != string::npos)
217 s = s.substr(s.find(':'));
218 theFontMetrics(font).rectText(s, w, a, d);
219 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
220 s, font, Color::none, Color::none);
223 // a visual cue when the cursor is inside the inset
224 Cursor & cur = pi.base.bv->cursor();
225 if (cur.isInside(this)) {
228 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
229 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
230 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
231 params_.labelfont.color());
232 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
233 params_.labelfont.color());
238 void InsetCharStyle::getDrawFont(Font & font) const
244 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
246 switch (cmd.action) {
248 case LFUN_MOUSE_RELEASE:
249 if (cmd.button() == mouse_button::button3)
250 if (status() == Open)
251 setStatus(cur, Collapsed);
253 setStatus(cur, Open);
255 InsetCollapsable::doDispatch(cur, cmd);
258 case LFUN_INSET_TOGGLE:
259 if (cmd.argument() == "open")
260 setStatus(cur, Open);
261 else if (cmd.argument() == "close")
262 setStatus(cur, Collapsed);
263 else if (cmd.argument() == "toggle" || cmd.argument().empty())
264 if (status() == Open)
265 setStatus(cur, Collapsed);
267 setStatus(cur, Open);
268 else // if assign or anything else
274 InsetCollapsable::doDispatch(cur, cmd);
280 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
281 FuncStatus & status) const
283 switch (cmd.action) {
284 // paragraph breaks not allowed in charstyle insets
285 case LFUN_BREAK_PARAGRAPH:
286 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
287 case LFUN_BREAK_PARAGRAPH_SKIP:
288 status.enabled(false);
292 return InsetCollapsable::getStatus(cur, cmd, status);
297 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
298 OutputParams const & runparams) const
302 os << '\\' << from_utf8(params_.latexname);
303 if (!params_.latexparam.empty())
304 os << from_utf8(params_.latexparam);
307 int i = InsetText::latex(buf, os, runparams);
314 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
315 OutputParams const & runparams) const
317 return InsetText::plaintext(buf, os, runparams);
321 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
322 OutputParams const & runparams) const
324 ParagraphList::const_iterator beg = paragraphs().begin();
325 ParagraphList::const_iterator par = paragraphs().begin();
326 ParagraphList::const_iterator end = paragraphs().end();
330 sgml::openTag(os, params_.latexname,
331 par->getID(buf, runparams) + params_.latexparam);
333 for (; par != end; ++par) {
334 par->simpleDocBookOnePar(buf, os, runparams,
335 outerFont(std::distance(beg, par),
340 sgml::closeTag(os, params_.latexname);
346 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
348 os << paragraphs().begin()->asString(buf, true);
352 void InsetCharStyle::validate(LaTeXFeatures & features) const
354 // Force inclusion of preamble snippet in layout file
355 features.require(params_.type);
356 InsetText::validate(features);
360 void InsetCharStyleParams::write(ostream & os) const
362 os << "CharStyle " << type << "\n";
366 void InsetCharStyleParams::read(Lexer & lex)
370 string token = lex.getString();
372 if (token == "CharStyle") {
374 type = lex.getString();
377 // This is handled in Collapsable
378 else if (token == "status") {
379 lex.pushToken(token);