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"
43 using std::istringstream;
46 using std::ostringstream;
49 void InsetCharStyle::init()
51 setInsetName("CharStyle");
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<InsetBase> InsetCharStyle::doClone() const
86 return auto_ptr<InsetBase>(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 = LyXFont(LyXFont::ALL_INHERIT);
102 params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
103 params_.labelfont.setColor(LColor::error);
107 void InsetCharStyle::setDefined(CharStyles::iterator cs)
109 params_.latextype = cs->latextype;
110 params_.latexname = cs->latexname;
111 params_.latexparam = cs->latexparam;
112 params_.font = cs->font;
113 params_.labelfont = cs->labelfont;
117 string const InsetCharStyle::editMessage() const
119 return _("Opened CharStyle Inset");
123 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
126 InsetCollapsable::write(buf, os);
130 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
132 InsetCollapsable::read(buf, lex);
137 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
139 LyXFont tmpfont = mi.base.font;
140 getDrawFont(mi.base.font);
141 mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
142 mi.base.font.realize(tmpfont);
143 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
144 InsetText::metrics(mi, dim);
145 mi.base.font = tmpfont;
147 // consider width of the inset label
148 LyXFont font(params_.labelfont);
149 font.realize(LyXFont(LyXFont::ALL_SANE));
155 string s(params_.type);
157 s = _("Undef: ") + s;
158 docstring ds(s.begin(), s.end());
159 font_metrics::rectText(ds, font, w, a, d);
160 dim.wid = max(dim.wid, w);
162 dim.asc += TEXT_TO_INSET_OFFSET;
163 dim.des += TEXT_TO_INSET_OFFSET;
164 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
165 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
168 dim_.des += ascent();
172 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
174 setPosCache(pi, x, y);
176 LyXFont tmpfont = pi.base.font;
177 getDrawFont(pi.base.font);
178 // I don't understand why the above .reduce and .realize aren't
179 //needed, or even wanted, here. It just works. -- MV 10.04.2005
180 InsetText::draw(pi, x, y);
181 pi.base.font = tmpfont;
183 int desc = InsetText::descent();
187 pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
188 pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
189 params_.labelfont.color());
190 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
191 params_.labelfont.color());
193 // the name of the charstyle. Can be toggled.
195 LyXFont font(params_.labelfont);
196 font.realize(LyXFont(LyXFont::ALL_SANE));
202 string s(params_.type);
204 s = _("Undef: ") + s;
205 docstring ds(s.begin(), s.end());
206 font_metrics::rectText(ds, font, w, a, d);
207 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
208 ds, font, LColor::none, LColor::none);
211 // a visual clue when the cursor is inside the inset
212 LCursor & cur = pi.base.bv->cursor();
213 if (cur.isInside(this)) {
215 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
216 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
217 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
218 params_.labelfont.color());
219 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
220 params_.labelfont.color());
225 void InsetCharStyle::getDrawFont(LyXFont & font) const
231 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
234 switch (cmd.action) {
235 case LFUN_MOUSE_PRESS:
236 if (cmd.button() == mouse_button::button3)
237 has_label_ = !has_label_;
239 InsetText::doDispatch(cur, cmd);
242 case LFUN_CLIPBOARD_PASTE:
243 case LFUN_PRIMARY_SELECTION_PASTE: {
244 InsetCollapsable::doDispatch(cur, cmd);
245 forceParagraphsToDefault(cur);
249 InsetCollapsable::doDispatch(cur, cmd);
255 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
256 FuncStatus & status) const
258 switch (cmd.action) {
259 // paragraph breaks not allowed in charstyle insets
260 case LFUN_BREAK_PARAGRAPH:
261 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
262 case LFUN_BREAK_PARAGRAPH_SKIP:
263 status.enabled(false);
267 return InsetCollapsable::getStatus(cur, cmd, status);
272 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
273 OutputParams const & runparams) const
276 os << "\\" << params_.latexname;
277 if (!params_.latexparam.empty())
278 os << params_.latexparam;
281 int i = InsetText::latex(buf, os, runparams);
288 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
289 OutputParams const & runparams) const
291 ParagraphList::const_iterator beg = paragraphs().begin();
292 ParagraphList::const_iterator par = paragraphs().begin();
293 ParagraphList::const_iterator end = paragraphs().end();
296 sgml::openTag(os, params_.latexname,
297 par->getID(buf, runparams) + params_.latexparam);
299 for (; par != end; ++par) {
300 par->simpleDocBookOnePar(buf, os, runparams,
301 outerFont(std::distance(beg, par),
306 sgml::closeTag(os, params_.latexname);
312 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
313 OutputParams const & runparams) const
315 return InsetText::plaintext(buf, os, runparams);
319 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
320 OutputParams const & op) const
322 return plaintext(buf, os, op);
326 void InsetCharStyle::validate(LaTeXFeatures & features) const
328 // Force inclusion of preamble snippet in layout file
329 features.require(params_.type);
330 InsetText::validate(features);
334 void InsetCharStyleParams::write(ostream & os) const
336 os << "CharStyle " << type << "\n";
340 void InsetCharStyleParams::read(LyXLex & lex)
344 string token = lex.getString();
349 type = lex.getString();