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 "BufferView.h"
19 #include "bufferparams.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/font_metrics.h"
35 #include "frontends/Painter.h"
42 using std::istringstream;
45 using std::ostringstream;
48 void InsetCharStyle::init()
50 setInsetName("CharStyle");
57 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
58 : InsetCollapsable(bp)
66 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
67 CharStyles::iterator cs)
68 : InsetCollapsable(bp)
70 params_.type = cs->name;
76 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
77 : InsetCollapsable(in), params_(in.params_)
83 auto_ptr<InsetBase> InsetCharStyle::doClone() const
85 return auto_ptr<InsetBase>(new InsetCharStyle(*this));
89 bool InsetCharStyle::undefined() const
91 return params_.latexname.empty();
95 void InsetCharStyle::setUndefined()
97 params_.latextype.clear();
98 params_.latexname.clear();
99 params_.latexparam.clear();
100 params_.font = LyXFont(LyXFont::ALL_INHERIT);
101 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
102 params_.labelfont.setColor(LColor::error);
106 void InsetCharStyle::setDefined(CharStyles::iterator cs)
108 params_.latextype = cs->latextype;
109 params_.latexname = cs->latexname;
110 params_.latexparam = cs->latexparam;
111 params_.font = cs->font;
112 params_.labelfont = cs->labelfont;
116 string const InsetCharStyle::editMessage() const
118 return _("Opened CharStyle Inset");
122 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
125 InsetCollapsable::write(buf, os);
129 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
131 InsetCollapsable::read(buf, lex);
136 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
138 LyXFont tmpfont = mi.base.font;
139 getDrawFont(mi.base.font);
140 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
141 mi.base.font.realize(tmpfont);
142 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
143 InsetText::metrics(mi, dim);
144 mi.base.font = tmpfont;
146 // consider width of the inset label
147 LyXFont font(params_.labelfont);
148 font.realize(LyXFont(LyXFont::ALL_SANE));
154 string s(params_.type);
156 s = _("Undef: ") + s;
157 font_metrics::rectText(s, font, w, a, d);
158 dim.wid = max(dim.wid, w);
160 dim.asc += TEXT_TO_INSET_OFFSET;
161 dim.des += TEXT_TO_INSET_OFFSET;
162 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
163 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
166 dim_.des += ascent();
170 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
172 setPosCache(pi, x, y);
174 LyXFont tmpfont = pi.base.font;
175 getDrawFont(pi.base.font);
176 // I don't understand why the above .reduce and .realize aren't
177 //needed, or even wanted, here. It just works. -- MV 10.04.2005
178 InsetText::draw(pi, x, y);
179 pi.base.font = tmpfont;
181 int desc = InsetText::descent();
185 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
186 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
187 params_.labelfont.color());
188 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
189 params_.labelfont.color());
191 // the name of the charstyle. Can be toggled.
193 LyXFont font(params_.labelfont);
194 font.realize(LyXFont(LyXFont::ALL_SANE));
200 string s(params_.type);
202 s = _("Undef: ") + s;
203 font_metrics::rectText(s, font, w, a, d);
204 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
205 s, font, LColor::none, LColor::none);
208 // a visual clue when the cursor is inside the inset
209 LCursor & cur = pi.base.bv->cursor();
210 if (cur.isInside(this)) {
212 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
213 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
214 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
215 params_.labelfont.color());
216 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
217 params_.labelfont.color());
222 void InsetCharStyle::getDrawFont(LyXFont & font) const
228 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
231 switch (cmd.action) {
232 case LFUN_MOUSE_PRESS:
233 if (cmd.button() == mouse_button::button3)
234 has_label_ = !has_label_;
236 InsetText::doDispatch(cur, cmd);
239 case LFUN_PASTESELECTION: {
240 InsetCollapsable::doDispatch(cur, cmd);
241 forceParagraphsToDefault(cur);
245 InsetCollapsable::doDispatch(cur, cmd);
251 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
252 FuncStatus & status) const
254 switch (cmd.action) {
255 // paragraph breaks not allowed in charstyle insets
256 case LFUN_BREAKPARAGRAPH:
257 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
258 case LFUN_BREAKPARAGRAPH_SKIP:
259 status.enabled(false);
263 return InsetCollapsable::getStatus(cur, cmd, status);
268 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
269 OutputParams const & runparams) const
272 os << "\\" << params_.latexname;
273 if (!params_.latexparam.empty())
274 os << params_.latexparam;
277 int i = InsetText::latex(buf, os, runparams);
284 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
285 OutputParams const & runparams) const
288 sgml::openTag(os, params_.latexname, params_.latexparam);
289 int i = InsetText::linuxdoc(buf, os, runparams);
291 sgml::closeTag(os, params_.latexname);
296 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
297 OutputParams const & runparams) const
299 ParagraphList::const_iterator beg = paragraphs().begin();
300 ParagraphList::const_iterator par = paragraphs().begin();
301 ParagraphList::const_iterator end = paragraphs().end();
304 sgml::openTag(os, params_.latexname,
305 par->getID(buf, runparams) + params_.latexparam);
307 for (; par != end; ++par) {
308 par->simpleDocBookOnePar(buf, os, runparams,
309 outerFont(std::distance(beg, par),
314 sgml::closeTag(os, params_.latexname);
320 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
321 OutputParams const & runparams) const
323 return InsetText::plaintext(buf, os, runparams);
327 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
328 OutputParams const & op) const
330 return plaintext(buf, os, op);
334 void InsetCharStyle::validate(LaTeXFeatures & features) const
336 // Force inclusion of preamble snippet in layout file
337 features.require(params_.type);
338 InsetText::validate(features);
342 void InsetCharStyleParams::write(ostream & os) const
344 os << "CharStyle " << type << "\n";
348 void InsetCharStyleParams::read(LyXLex & lex)
352 string token = lex.getString();
357 type = lex.getString();