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_WRAP_INSERT:
285 flag.setEnabled(false);
287 case LFUN_INSET_MODIFY:
288 flag.setEnabled(true);
290 case LFUN_COMMAND_SEQUENCE: {
291 // argument contains ';'-terminated commands
292 string arg = to_utf8(cmd.argument());
293 // prevent insertion of display math formulas like AMS align
294 while (!arg.empty()) {
296 arg = support::split(arg, first, ';');
297 FuncRequest func(lyxaction.lookupFunc(first));
298 if (func.action() == LFUN_MATH_MUTATE) {
299 flag.setEnabled(false);
308 return InsetText::getStatus(cur, cmd, flag);
312 docstring InsetScript::toolTip(BufferView const &, int, int) const
314 OutputParams rp(&buffer().params().encoding());
315 odocstringstream ods;
316 InsetText::plaintext(ods, rp, 200);
317 docstring content_tip = ods.str();
318 // shorten it if necessary
319 support::truncateWithEllipsis(content_tip, 200);
320 docstring res = scripttranslator_loc().find(params_.type);
321 if (!content_tip.empty())
322 res += from_ascii(": ") + content_tip;
327 int InsetScript::plaintext(odocstringstream & os,
328 OutputParams const & runparams, size_t max_length) const
330 odocstringstream oss;
331 InsetText::plaintext(oss, runparams, max_length);
332 docstring const text = oss.str();
333 switch (params_.type) {
334 case InsetScriptParams::Subscript:
335 if (text.size() == 1) {
336 char_type const c = support::subscript(text[0]);
342 os << '[' << buffer().B_("subscript") << ':';
344 case InsetScriptParams::Superscript:
345 if (text.size() == 1) {
346 char_type const c = support::superscript(text[0]);
352 os << '[' << buffer().B_("superscript") << ':';
355 InsetText::plaintext(os, runparams, max_length);
358 return PLAINTEXT_NEWLINE;
362 int InsetScript::docbook(odocstream & os, OutputParams const & runparams) const
365 switch (params_.type) {
366 case InsetScriptParams::Subscript:
367 cmdname = from_ascii("subscript");
369 case InsetScriptParams::Superscript:
370 cmdname = from_ascii("superscript");
373 os << '<' + cmdname + '>';
374 int const i = InsetText::docbook(os, runparams);
375 os << "</" + cmdname + '>';
381 string InsetScript::contextMenuName() const
383 return "context-script";
387 string InsetScript::params2string(InsetScriptParams const & params)
396 void InsetScript::string2params(string const & in, InsetScriptParams & params)
398 params = InsetScriptParams();
403 istringstream data(in);
406 lex.setContext("InsetScript::string2params");
407 lex >> "script" >> "script";