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"
26 #include "LyXAction.h"
27 #include "MetricsInfo.h"
28 #include "output_docbook.h"
29 #include "output_xhtml.h"
30 #include "TextClass.h"
31 #include "TextMetrics.h"
33 #include "support/docstream.h"
34 #include "support/gettext.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"
50 typedef Translator<string, InsetScriptParams::Type> ScriptTranslator;
51 typedef Translator<docstring, InsetScriptParams::Type> ScriptTranslatorLoc;
53 ScriptTranslator const init_scripttranslator()
55 ScriptTranslator translator("subscript", InsetScriptParams::Subscript);
56 translator.addPair("superscript", InsetScriptParams::Superscript);
61 ScriptTranslatorLoc const init_scripttranslator_loc()
63 ScriptTranslatorLoc translator(_("Subscript"), InsetScriptParams::Subscript);
64 translator.addPair(_("Superscript"), InsetScriptParams::Superscript);
69 ScriptTranslator const & scripttranslator()
71 static ScriptTranslator const translator =
72 init_scripttranslator();
77 ScriptTranslatorLoc const & scripttranslator_loc()
79 static ScriptTranslatorLoc const translator =
80 init_scripttranslator_loc();
87 InsetScriptParams::InsetScriptParams()
92 void InsetScriptParams::write(ostream & os) const
94 string const label = scripttranslator().find(type);
95 os << "script " << label << "\n";
99 void InsetScriptParams::read(Lexer & lex)
104 type = scripttranslator().find(label);
108 int InsetScriptParams::shift(FontInfo const & font) const
110 frontend::FontMetrics const & fm = theFontMetrics(font);
113 return fm.maxAscent() / 3;
115 return -fm.maxAscent() / 2;
122 /////////////////////////////////////////////////////////////////////
126 /////////////////////////////////////////////////////////////////////
128 InsetScript::InsetScript(Buffer * buf, InsetScriptParams const & params)
129 : InsetText(buf, InsetText::PlainLayout), params_(params)
135 InsetScript::InsetScript(Buffer * buf, string const & label)
139 params_.type = scripttranslator().find(label);
143 InsetScript::~InsetScript()
148 docstring InsetScript::layoutName() const
150 return from_ascii("Script:" + scripttranslator().find(params_.type));
154 void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
156 int const shift = params_.shift(mi.base.font);
157 // Remember the value of the outser font, so that it can be used in cursorPos.
158 outer_font_ = mi.base.font;
159 Changer dummy = mi.base.changeScript();
160 InsetText::metrics(mi, dim);
166 void InsetScript::draw(PainterInfo & pi, int x, int y) const
168 int const shift = params_.shift(pi.base.font);
169 Changer dummy = pi.base.changeScript();
170 InsetText::draw(pi, x, y + shift);
174 void InsetScript::cursorPos(BufferView const & bv,
175 CursorSlice const & sl, bool boundary, int & x, int & y) const
177 int const shift = params_.shift(outer_font_);
178 InsetText::cursorPos(bv, sl, boundary, x, y);
183 void InsetScript::write(ostream & os) const
190 void InsetScript::read(Lexer & lex)
193 InsetText::read(lex);
197 void InsetScript::edit(Cursor & cur, bool front, EntryDirection entry_from)
200 InsetText::edit(cur, front, entry_from);
204 Inset * InsetScript::editXY(Cursor & cur, int x, int y)
207 return InsetText::editXY(cur, x, y);
210 void InsetScript::doDispatch(Cursor & cur, FuncRequest & cmd)
212 switch (cmd.action()) {
213 case LFUN_INSET_MODIFY:
214 cur.recordUndoInset(this);
215 string2params(to_utf8(cmd.argument()), params_);
218 InsetText::doDispatch(cur, cmd);
224 bool InsetScript::insetAllowed(InsetCode code) const
227 // code that is not allowed in a script
233 case COLLAPSIBLE_CODE:
235 case FLOAT_LIST_CODE:
238 case INDEX_PRINT_CODE:
241 case MATH_MACRO_CODE:
245 case NOMENCL_PRINT_CODE:
253 return InsetText::insetAllowed(code);
257 bool InsetScript::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & flag) const
260 switch (cmd.action()) {
261 case LFUN_MATH_DISPLAY:
262 case LFUN_BOX_INSERT:
263 case LFUN_BRANCH_INSERT:
264 case LFUN_CAPTION_INSERT:
265 case LFUN_FLOAT_INSERT:
266 case LFUN_FLOAT_LIST_INSERT:
267 case LFUN_FLOAT_WIDE_INSERT:
268 case LFUN_FOOTNOTE_INSERT:
269 case LFUN_INDEX_PRINT:
270 case LFUN_LISTING_INSERT:
271 case LFUN_MARGINALNOTE_INSERT:
272 case LFUN_NEWLINE_INSERT:
273 case LFUN_NEWPAGE_INSERT:
274 case LFUN_NOMENCL_PRINT:
275 case LFUN_PREVIEW_INSERT:
276 case LFUN_QUOTE_INSERT:
277 case LFUN_TABULAR_INSERT:
278 case LFUN_TABULAR_STYLE_INSERT:
279 case LFUN_WRAP_INSERT:
280 flag.setEnabled(false);
282 case LFUN_INSET_MODIFY:
283 flag.setEnabled(true);
285 case LFUN_COMMAND_SEQUENCE: {
286 // argument contains ';'-terminated commands
287 string arg = to_utf8(cmd.argument());
288 // prevent insertion of display math formulas like AMS align
289 while (!arg.empty()) {
291 arg = support::split(arg, first, ';');
292 FuncRequest func(lyxaction.lookupFunc(first));
293 if (func.action() == LFUN_MATH_MUTATE) {
294 flag.setEnabled(false);
303 return InsetText::getStatus(cur, cmd, flag);
307 docstring InsetScript::toolTip(BufferView const &, int, int) const
309 OutputParams rp(&buffer().params().encoding());
310 odocstringstream ods;
311 InsetText::plaintext(ods, rp, 200);
312 docstring content_tip = ods.str();
313 // shorten it if necessary
314 support::truncateWithEllipsis(content_tip, 200);
315 docstring res = scripttranslator_loc().find(params_.type);
316 if (!content_tip.empty())
317 res += from_ascii(": ") + content_tip;
322 int InsetScript::plaintext(odocstringstream & os,
323 OutputParams const & runparams, size_t max_length) const
325 odocstringstream oss;
326 InsetText::plaintext(oss, runparams, max_length);
327 docstring const text = oss.str();
328 switch (params_.type) {
329 case InsetScriptParams::Subscript:
330 if (text.size() == 1) {
331 char_type const c = support::subscript(text[0]);
337 os << '[' << buffer().B_("subscript") << ':';
339 case InsetScriptParams::Superscript:
340 if (text.size() == 1) {
341 char_type const c = support::superscript(text[0]);
347 os << '[' << buffer().B_("superscript") << ':';
350 InsetText::plaintext(os, runparams, max_length);
353 return PLAINTEXT_NEWLINE;
357 void InsetScript::docbook(XMLStream & xs, OutputParams const & runparams) const
360 switch (params_.type) {
361 case InsetScriptParams::Subscript:
362 cmdname = from_ascii("subscript");
364 case InsetScriptParams::Superscript:
365 cmdname = from_ascii("superscript");
369 xs << xml::StartTag(cmdname);
370 InsetText::docbook(xs, runparams);
371 xs << xml::EndTag(cmdname);
375 string InsetScript::contextMenuName() const
377 return "context-script";
381 string InsetScript::params2string(InsetScriptParams const & params)
390 void InsetScript::string2params(string const & in, InsetScriptParams & params)
392 params = InsetScriptParams();
397 istringstream data(in);
400 lex.setContext("InsetScript::string2params");
401 lex >> "script" >> "script";