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 "OutputParams.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 Inset::DisplayType InsetScript::display() const
160 void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
162 int const shift = params_.shift(mi.base.font);
163 // Remember the value of the outser font, so that it can be used in cursorPos.
164 outer_font_ = mi.base.font;
165 Changer dummy = mi.base.changeScript();
166 InsetText::metrics(mi, dim);
172 void InsetScript::draw(PainterInfo & pi, int x, int y) const
174 int const shift = params_.shift(pi.base.font);
175 Changer dummy = pi.base.changeScript();
176 InsetText::draw(pi, x, y + shift);
180 void InsetScript::cursorPos(BufferView const & bv,
181 CursorSlice const & sl, bool boundary, int & x, int & y) const
183 int const shift = params_.shift(outer_font_);
184 InsetText::cursorPos(bv, sl, boundary, x, y);
189 void InsetScript::write(ostream & os) const
196 void InsetScript::read(Lexer & lex)
199 InsetText::read(lex);
203 void InsetScript::edit(Cursor & cur, bool front, EntryDirection entry_from)
206 InsetText::edit(cur, front, entry_from);
210 Inset * InsetScript::editXY(Cursor & cur, int x, int y)
213 return InsetText::editXY(cur, x, y);
216 void InsetScript::doDispatch(Cursor & cur, FuncRequest & cmd)
218 switch (cmd.action()) {
219 case LFUN_INSET_MODIFY:
220 cur.recordUndoInset(this);
221 string2params(to_utf8(cmd.argument()), params_);
224 InsetText::doDispatch(cur, cmd);
230 bool InsetScript::insetAllowed(InsetCode code) const
233 // code that is not allowed in a script
239 case COLLAPSIBLE_CODE:
241 case FLOAT_LIST_CODE:
244 case INDEX_PRINT_CODE:
247 case MATH_MACRO_CODE:
251 case NOMENCL_PRINT_CODE:
259 return InsetText::insetAllowed(code);
263 bool InsetScript::getStatus(Cursor & cur, FuncRequest const & cmd,
264 FuncStatus & flag) const
266 switch (cmd.action()) {
267 case LFUN_MATH_DISPLAY:
268 case LFUN_BOX_INSERT:
269 case LFUN_BRANCH_INSERT:
270 case LFUN_CAPTION_INSERT:
271 case LFUN_FLOAT_INSERT:
272 case LFUN_FLOAT_LIST_INSERT:
273 case LFUN_FLOAT_WIDE_INSERT:
274 case LFUN_FOOTNOTE_INSERT:
275 case LFUN_INDEX_PRINT:
276 case LFUN_LISTING_INSERT:
277 case LFUN_MARGINALNOTE_INSERT:
278 case LFUN_NEWLINE_INSERT:
279 case LFUN_NEWPAGE_INSERT:
280 case LFUN_NOMENCL_PRINT:
281 case LFUN_PREVIEW_INSERT:
282 case LFUN_QUOTE_INSERT:
283 case LFUN_TABULAR_INSERT:
284 case LFUN_TABULAR_STYLE_INSERT:
285 case LFUN_WRAP_INSERT:
286 flag.setEnabled(false);
288 case LFUN_INSET_MODIFY:
289 flag.setEnabled(true);
291 case LFUN_COMMAND_SEQUENCE: {
292 // argument contains ';'-terminated commands
293 string arg = to_utf8(cmd.argument());
294 // prevent insertion of display math formulas like AMS align
295 while (!arg.empty()) {
297 arg = support::split(arg, first, ';');
298 FuncRequest func(lyxaction.lookupFunc(first));
299 if (func.action() == LFUN_MATH_MUTATE) {
300 flag.setEnabled(false);
309 return InsetText::getStatus(cur, cmd, flag);
313 docstring InsetScript::toolTip(BufferView const &, int, int) const
315 OutputParams rp(&buffer().params().encoding());
316 odocstringstream ods;
317 InsetText::plaintext(ods, rp, 200);
318 docstring content_tip = ods.str();
319 // shorten it if necessary
320 support::truncateWithEllipsis(content_tip, 200);
321 docstring res = scripttranslator_loc().find(params_.type);
322 if (!content_tip.empty())
323 res += from_ascii(": ") + content_tip;
328 int InsetScript::plaintext(odocstringstream & os,
329 OutputParams const & runparams, size_t max_length) const
331 odocstringstream oss;
332 InsetText::plaintext(oss, runparams, max_length);
333 docstring const text = oss.str();
334 switch (params_.type) {
335 case InsetScriptParams::Subscript:
336 if (text.size() == 1) {
337 char_type const c = support::subscript(text[0]);
343 os << '[' << buffer().B_("subscript") << ':';
345 case InsetScriptParams::Superscript:
346 if (text.size() == 1) {
347 char_type const c = support::superscript(text[0]);
353 os << '[' << buffer().B_("superscript") << ':';
356 InsetText::plaintext(os, runparams, max_length);
359 return PLAINTEXT_NEWLINE;
363 int InsetScript::docbook(odocstream & os, OutputParams const & runparams) const
366 switch (params_.type) {
367 case InsetScriptParams::Subscript:
368 cmdname = from_ascii("subscript");
370 case InsetScriptParams::Superscript:
371 cmdname = from_ascii("superscript");
374 os << '<' + cmdname + '>';
375 int const i = InsetText::docbook(os, runparams);
376 os << "</" + cmdname + '>';
382 string InsetScript::contextMenuName() const
384 return "context-script";
388 string InsetScript::params2string(InsetScriptParams const & params)
397 void InsetScript::string2params(string const & in, InsetScriptParams & params)
399 params = InsetScriptParams();
404 istringstream data(in);
407 lex.setContext("InsetScript::string2params");
408 lex >> "script" >> "script";