2 * \file InsetScript.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetScript.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "LaTeXFeatures.h"
25 #include "LyXAction.h"
26 #include "MetricsInfo.h"
27 #include "output_docbook.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30 #include "TextMetrics.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/Lexer.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
52 typedef Translator<string, InsetScriptParams::Type> ScriptTranslator;
53 typedef Translator<docstring, InsetScriptParams::Type> ScriptTranslatorLoc;
55 ScriptTranslator const init_scripttranslator()
57 ScriptTranslator translator("subscript", InsetScriptParams::Subscript);
58 translator.addPair("superscript", InsetScriptParams::Superscript);
63 ScriptTranslatorLoc const init_scripttranslator_loc()
65 ScriptTranslatorLoc translator(_("Subscript"), InsetScriptParams::Subscript);
66 translator.addPair(_("Superscript"), InsetScriptParams::Superscript);
71 ScriptTranslator const & scripttranslator()
73 static ScriptTranslator const translator =
74 init_scripttranslator();
79 ScriptTranslatorLoc const & scripttranslator_loc()
81 static ScriptTranslatorLoc const translator =
82 init_scripttranslator_loc();
89 InsetScriptParams::InsetScriptParams()
94 void InsetScriptParams::write(ostream & os) const
96 string const label = scripttranslator().find(type);
97 os << "script " << label << "\n";
101 void InsetScriptParams::read(Lexer & lex)
106 type = scripttranslator().find(label);
110 int InsetScriptParams::shift(FontInfo const & font) const
112 frontend::FontMetrics const & fm = theFontMetrics(font);
115 return fm.maxAscent() / 3;
117 return -fm.maxAscent() / 2;
124 /////////////////////////////////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////
130 InsetScript::InsetScript(Buffer * buf, InsetScriptParams const & params)
131 : InsetText(buf, InsetText::PlainLayout), params_(params)
137 InsetScript::InsetScript(Buffer * buf, string const & label)
141 params_.type = scripttranslator().find(label);
145 InsetScript::~InsetScript()
150 docstring InsetScript::layoutName() const
152 return from_ascii("Script:" + scripttranslator().find(params_.type));
156 void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
158 int const shift = params_.shift(mi.base.font);
159 // Remember the value of the outser font, so that it can be used in cursorPos.
160 outer_font_ = mi.base.font;
161 Changer dummy = mi.base.changeScript();
162 InsetText::metrics(mi, dim);
168 void InsetScript::draw(PainterInfo & pi, int x, int y) const
170 int const shift = params_.shift(pi.base.font);
171 Changer dummy = pi.base.changeScript();
172 InsetText::draw(pi, x, y + shift);
176 void InsetScript::cursorPos(BufferView const & bv,
177 CursorSlice const & sl, bool boundary, int & x, int & y) const
179 int const shift = params_.shift(outer_font_);
180 InsetText::cursorPos(bv, sl, boundary, x, y);
185 void InsetScript::write(ostream & os) const
192 void InsetScript::read(Lexer & lex)
195 InsetText::read(lex);
199 void InsetScript::edit(Cursor & cur, bool front, EntryDirection entry_from)
202 InsetText::edit(cur, front, entry_from);
206 Inset * InsetScript::editXY(Cursor & cur, int x, int y)
209 return InsetText::editXY(cur, x, y);
212 void InsetScript::doDispatch(Cursor & cur, FuncRequest & cmd)
214 switch (cmd.action()) {
215 case LFUN_INSET_MODIFY:
216 cur.recordUndoInset(this);
217 string2params(to_utf8(cmd.argument()), params_);
220 InsetText::doDispatch(cur, cmd);
226 bool InsetScript::insetAllowed(InsetCode code) const
229 // code that is not allowed in a script
235 case COLLAPSIBLE_CODE:
237 case FLOAT_LIST_CODE:
240 case INDEX_PRINT_CODE:
243 case MATH_MACRO_CODE:
247 case NOMENCL_PRINT_CODE:
255 return InsetText::insetAllowed(code);
259 bool InsetScript::getStatus(Cursor & cur, FuncRequest const & cmd,
260 FuncStatus & flag) const
262 switch (cmd.action()) {
263 case LFUN_MATH_DISPLAY:
264 case LFUN_BOX_INSERT:
265 case LFUN_BRANCH_INSERT:
266 case LFUN_CAPTION_INSERT:
267 case LFUN_FLOAT_INSERT:
268 case LFUN_FLOAT_LIST_INSERT:
269 case LFUN_FLOAT_WIDE_INSERT:
270 case LFUN_FOOTNOTE_INSERT:
271 case LFUN_INDEX_PRINT:
272 case LFUN_LISTING_INSERT:
273 case LFUN_MARGINALNOTE_INSERT:
274 case LFUN_NEWLINE_INSERT:
275 case LFUN_NEWPAGE_INSERT:
276 case LFUN_NOMENCL_PRINT:
277 case LFUN_PREVIEW_INSERT:
278 case LFUN_QUOTE_INSERT:
279 case LFUN_TABULAR_INSERT:
280 case LFUN_TABULAR_STYLE_INSERT:
281 case LFUN_WRAP_INSERT:
282 flag.setEnabled(false);
284 case LFUN_INSET_MODIFY:
285 flag.setEnabled(true);
287 case LFUN_COMMAND_SEQUENCE: {
288 // argument contains ';'-terminated commands
289 string arg = to_utf8(cmd.argument());
290 // prevent insertion of display math formulas like AMS align
291 while (!arg.empty()) {
293 arg = support::split(arg, first, ';');
294 FuncRequest func(lyxaction.lookupFunc(first));
295 if (func.action() == LFUN_MATH_MUTATE) {
296 flag.setEnabled(false);
305 return InsetText::getStatus(cur, cmd, flag);
309 docstring InsetScript::toolTip(BufferView const &, int, int) const
311 OutputParams rp(&buffer().params().encoding());
312 odocstringstream ods;
313 InsetText::plaintext(ods, rp, 200);
314 docstring content_tip = ods.str();
315 // shorten it if necessary
316 support::truncateWithEllipsis(content_tip, 200);
317 docstring res = scripttranslator_loc().find(params_.type);
318 if (!content_tip.empty())
319 res += from_ascii(": ") + content_tip;
324 int InsetScript::plaintext(odocstringstream & os,
325 OutputParams const & runparams, size_t max_length) const
327 odocstringstream oss;
328 InsetText::plaintext(oss, runparams, max_length);
329 docstring const text = oss.str();
330 switch (params_.type) {
331 case InsetScriptParams::Subscript:
332 if (text.size() == 1) {
333 char_type const c = support::subscript(text[0]);
339 os << '[' << buffer().B_("subscript") << ':';
341 case InsetScriptParams::Superscript:
342 if (text.size() == 1) {
343 char_type const c = support::superscript(text[0]);
349 os << '[' << buffer().B_("superscript") << ':';
352 InsetText::plaintext(os, runparams, max_length);
355 return PLAINTEXT_NEWLINE;
359 void InsetScript::docbook(XMLStream & xs, OutputParams const & runparams) const
362 switch (params_.type) {
363 case InsetScriptParams::Subscript:
364 cmdname = from_ascii("subscript");
366 case InsetScriptParams::Superscript:
367 cmdname = from_ascii("superscript");
371 xs << xml::StartTag(cmdname);
372 InsetText::docbook(xs, runparams);
373 xs << xml::EndTag(cmdname);
377 string InsetScript::contextMenuName() const
379 return "context-script";
383 string InsetScript::params2string(InsetScriptParams const & params)
392 void InsetScript::string2params(string const & in, InsetScriptParams & params)
394 params = InsetScriptParams();
399 istringstream data(in);
402 lex.setContext("InsetScript::string2params");
403 lex >> "script" >> "script";