2 * \file insetcharstyle.C
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()
54 setInsetName(from_utf8("CharStyle"));
60 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
61 : InsetCollapsable(bp)
69 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
70 CharStyles::iterator cs)
71 : InsetCollapsable(bp)
73 params_.type = cs->name;
79 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
80 : InsetCollapsable(in), params_(in.params_)
86 auto_ptr<InsetBase> InsetCharStyle::doClone() const
88 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
92 bool InsetCharStyle::undefined() const
94 return params_.latexname.empty();
98 void InsetCharStyle::setUndefined()
100 params_.latextype.clear();
101 params_.latexname.clear();
102 params_.latexparam.clear();
103 params_.font = LyXFont(LyXFont::ALL_INHERIT);
104 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
105 params_.labelfont.setColor(LColor::error);
106 params_.show_label = true;
110 void InsetCharStyle::setDefined(CharStyles::iterator cs)
112 params_.latextype = cs->latextype;
113 params_.latexname = cs->latexname;
114 params_.latexparam = cs->latexparam;
115 params_.font = cs->font;
116 params_.labelfont = cs->labelfont;
117 params_.show_label = true;
121 docstring const InsetCharStyle::editMessage() const
123 return _("Opened CharStyle Inset");
127 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
130 InsetCollapsable::write(buf, os);
134 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
137 InsetCollapsable::read(buf, lex);
142 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
144 LyXFont tmpfont = mi.base.font;
145 getDrawFont(mi.base.font);
146 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
147 mi.base.font.realize(tmpfont);
148 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
149 InsetText::metrics(mi, dim);
150 mi.base.font = tmpfont;
151 if (params_.show_label) {
152 // consider width of the inset label
153 LyXFont font(params_.labelfont);
154 font.realize(LyXFont(LyXFont::ALL_SANE));
161 docstring s(from_utf8(params_.type));
163 s = _("Undef: ") + s;
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 LyXFont 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, 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 LyXFont font(params_.labelfont);
203 font.realize(LyXFont(LyXFont::ALL_SANE));
210 docstring s(from_utf8(params_.type));
212 s = _("Undef: ") + s;
213 theFontMetrics(font).rectText(s, w, a, d);
214 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
215 s, font, LColor::none, LColor::none);
218 // a visual clue when the cursor is inside the inset
219 LCursor & cur = pi.base.bv->cursor();
220 if (cur.isInside(this)) {
222 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
223 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
224 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
225 params_.labelfont.color());
226 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
227 params_.labelfont.color());
232 void InsetCharStyle::getDrawFont(LyXFont & font) const
238 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
241 switch (cmd.action) {
243 case LFUN_MOUSE_PRESS:
244 if (cmd.button() == mouse_button::button3)
245 params_.show_label = !params_.show_label;
247 InsetText::doDispatch(cur, cmd);
250 case LFUN_INSET_TOGGLE:
251 if (cmd.argument() == "open")
252 params_.show_label = true;
253 else if (cmd.argument() == "close")
254 params_.show_label = false;
255 else if (cmd.argument() == "toggle" || cmd.argument().empty())
256 params_.show_label = !params_.show_label;
257 else // if assign or anything else
263 InsetCollapsable::doDispatch(cur, cmd);
269 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
270 FuncStatus & status) const
272 switch (cmd.action) {
273 // paragraph breaks not allowed in charstyle insets
274 case LFUN_BREAK_PARAGRAPH:
275 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
276 case LFUN_BREAK_PARAGRAPH_SKIP:
277 status.enabled(false);
281 return InsetCollapsable::getStatus(cur, cmd, status);
286 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
287 OutputParams const & runparams) const
291 os << '\\' << from_utf8(params_.latexname);
292 if (!params_.latexparam.empty())
293 os << from_utf8(params_.latexparam);
296 int i = InsetText::latex(buf, os, runparams);
303 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
304 OutputParams const & runparams) const
306 ParagraphList::const_iterator beg = paragraphs().begin();
307 ParagraphList::const_iterator par = paragraphs().begin();
308 ParagraphList::const_iterator end = paragraphs().end();
312 sgml::openTag(os, params_.latexname,
313 par->getID(buf, runparams) + params_.latexparam);
315 for (; par != end; ++par) {
316 par->simpleDocBookOnePar(buf, os, runparams,
317 outerFont(std::distance(beg, par),
322 sgml::closeTag(os, params_.latexname);
328 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
329 OutputParams const & runparams) const
331 return InsetText::plaintext(buf, os, runparams);
335 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
337 os << paragraphs().begin()->asString(buf, true);
341 void InsetCharStyle::validate(LaTeXFeatures & features) const
343 // Force inclusion of preamble snippet in layout file
344 features.require(params_.type);
345 InsetText::validate(features);
349 void InsetCharStyleParams::write(ostream & os) const
351 os << "CharStyle " << type << "\n";
352 os << "show_label " << convert<string>(show_label) << "\n";
356 void InsetCharStyleParams::read(LyXLex & lex)
360 string token = lex.getString();
362 if (token == "CharStyle") {
364 type = lex.getString();
367 else if (token == "show_label") {
369 show_label = lex.getBool();
372 else if (token == "status") {
373 lex.pushToken(token);