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 ScriptChanger dummy(mi.base);
164 InsetText::metrics(mi, dim);
170 void InsetScript::draw(PainterInfo & pi, int x, int y) const
172 int const shift = params_.shift(pi.base.font);
173 ScriptChanger dummy(pi.base);
174 InsetText::draw(pi, x, y + shift);
178 void InsetScript::cursorPos(BufferView const & bv,
179 CursorSlice const & sl, bool boundary, int & x, int & y) const
181 Font const font = bv.textMetrics(&text()).displayFont(sl.pit(), sl.pos());
182 int const shift = params_.shift(font.fontInfo());
183 InsetText::cursorPos(bv, sl, boundary, x, y);
188 void InsetScript::write(ostream & os) const
195 void InsetScript::read(Lexer & lex)
198 InsetText::read(lex);
202 void InsetScript::edit(Cursor & cur, bool front, EntryDirection entry_from)
205 InsetText::edit(cur, front, entry_from);
209 Inset * InsetScript::editXY(Cursor & cur, int x, int y)
212 return InsetText::editXY(cur, x, y);
215 void InsetScript::doDispatch(Cursor & cur, FuncRequest & cmd)
217 switch (cmd.action()) {
218 case LFUN_INSET_MODIFY:
219 cur.recordUndoInset(ATOMIC_UNDO, this);
220 string2params(to_utf8(cmd.argument()), params_);
223 InsetText::doDispatch(cur, cmd);
229 bool InsetScript::insetAllowed(InsetCode code) const
232 // code that is not allowed in a script
238 case COLLAPSABLE_CODE:
240 case FLOAT_LIST_CODE:
243 case INDEX_PRINT_CODE:
246 case MATH_MACRO_CODE:
250 case NOMENCL_PRINT_CODE:
258 return InsetText::insetAllowed(code);
262 bool InsetScript::getStatus(Cursor & cur, FuncRequest const & cmd,
263 FuncStatus & flag) const
265 switch (cmd.action()) {
266 case LFUN_PARAGRAPH_BREAK:
268 case LFUN_LAYOUT_PARAGRAPH:
269 case LFUN_MATH_DISPLAY:
270 case LFUN_BOX_INSERT:
271 case LFUN_BRANCH_INSERT:
272 case LFUN_CAPTION_INSERT:
273 case LFUN_FLOAT_INSERT:
274 case LFUN_FLOAT_LIST_INSERT:
275 case LFUN_FLOAT_WIDE_INSERT:
276 case LFUN_FOOTNOTE_INSERT:
277 case LFUN_INDEX_PRINT:
278 case LFUN_LISTING_INSERT:
279 case LFUN_MARGINALNOTE_INSERT:
280 case LFUN_NEWLINE_INSERT:
281 case LFUN_NEWPAGE_INSERT:
282 case LFUN_NOMENCL_PRINT:
283 case LFUN_PREVIEW_INSERT:
284 case LFUN_QUOTE_INSERT:
285 case LFUN_TABULAR_INSERT:
286 case LFUN_WRAP_INSERT:
287 flag.setEnabled(false);
289 case LFUN_INSET_MODIFY:
290 flag.setEnabled(true);
292 case LFUN_COMMAND_SEQUENCE: {
293 // argument contains ';'-terminated commands
294 string arg = to_utf8(cmd.argument());
295 // prevent insertion of display math formulas like AMS align
296 while (!arg.empty()) {
298 arg = support::split(arg, first, ';');
299 FuncRequest func(lyxaction.lookupFunc(first));
300 if (func.action() == LFUN_MATH_MUTATE) {
301 flag.setEnabled(false);
310 return InsetText::getStatus(cur, cmd, flag);
314 docstring InsetScript::toolTip(BufferView const &, int, int) const
316 OutputParams rp(&buffer().params().encoding());
317 odocstringstream ods;
318 InsetText::plaintext(ods, rp, 200);
319 docstring content_tip = ods.str();
320 // shorten it if necessary
321 if (content_tip.size() >= 200)
322 content_tip = content_tip.substr(0, 197) + "...";
323 docstring res = scripttranslator_loc().find(params_.type);
324 if (!content_tip.empty())
325 res += from_ascii(": ") + content_tip;
330 int InsetScript::plaintext(odocstringstream & os,
331 OutputParams const & runparams, size_t max_length) const
333 odocstringstream oss;
334 InsetText::plaintext(oss, runparams, max_length);
335 docstring const text = oss.str();
336 switch (params_.type) {
337 case InsetScriptParams::Subscript:
338 if (text.size() == 1) {
339 char_type const c = support::subscript(text[0]);
345 os << '[' << buffer().B_("subscript") << ':';
347 case InsetScriptParams::Superscript:
348 if (text.size() == 1) {
349 char_type const c = support::superscript(text[0]);
355 os << '[' << buffer().B_("superscript") << ':';
358 InsetText::plaintext(os, runparams, max_length);
361 return PLAINTEXT_NEWLINE;
365 int InsetScript::docbook(odocstream & os, OutputParams const & runparams) const
368 switch (params_.type) {
369 case InsetScriptParams::Subscript:
370 cmdname = "subscript";
372 case InsetScriptParams::Superscript:
373 cmdname = "superscript";
376 os << '<' + cmdname + '>';
377 int const i = InsetText::docbook(os, runparams);
378 os << "</" + cmdname + '>';
384 string InsetScript::contextMenuName() const
386 return "context-script";
390 string InsetScript::params2string(InsetScriptParams const & params)
399 void InsetScript::string2params(string const & in, InsetScriptParams & params)
401 params = InsetScriptParams();
406 istringstream data(in);
409 lex.setContext("InsetScript::string2params");
410 lex >> "script" >> "script";