]> git.lyx.org Git - features.git/blob - src/insets/InsetScript.cpp
Use new display() values to remove some inset hardcoding.
[features.git] / src / insets / InsetScript.cpp
1 /**
2  * \file InsetScript.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetScript.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "Dimension.h"
20 #include "DispatchResult.h"
21 #include "Exporter.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "LaTeXFeatures.h"
25 #include "Lexer.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"
32
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
37
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
41
42 #include <algorithm>
43
44 using namespace std;
45
46 namespace lyx {
47
48 namespace {
49
50 typedef Translator<string, InsetScriptParams::Type> ScriptTranslator;
51 typedef Translator<docstring, InsetScriptParams::Type> ScriptTranslatorLoc;
52
53 ScriptTranslator const init_scripttranslator()
54 {
55         ScriptTranslator translator("subscript", InsetScriptParams::Subscript);
56         translator.addPair("superscript", InsetScriptParams::Superscript);
57         return translator;
58 }
59
60
61 ScriptTranslatorLoc const init_scripttranslator_loc()
62 {
63         ScriptTranslatorLoc translator(_("Subscript"), InsetScriptParams::Subscript);
64         translator.addPair(_("Superscript"), InsetScriptParams::Superscript);
65         return translator;
66 }
67
68
69 ScriptTranslator const & scripttranslator()
70 {
71         static ScriptTranslator const translator =
72             init_scripttranslator();
73         return translator;
74 }
75
76
77 ScriptTranslatorLoc const & scripttranslator_loc()
78 {
79         static ScriptTranslatorLoc const translator =
80             init_scripttranslator_loc();
81         return translator;
82 }
83
84 } // namespace
85
86
87 InsetScriptParams::InsetScriptParams()
88         : type(Subscript)
89 {}
90
91
92 void InsetScriptParams::write(ostream & os) const
93 {
94         string const label = scripttranslator().find(type);
95         os << "script " << label << "\n";
96 }
97
98
99 void InsetScriptParams::read(Lexer & lex)
100 {
101         string label;
102         lex >> label;
103         if (lex)
104                 type = scripttranslator().find(label);
105 }
106
107
108 int InsetScriptParams::shift(FontInfo const & font) const
109 {
110         frontend::FontMetrics const & fm = theFontMetrics(font);
111         switch (type) {
112         case Subscript:
113                 return fm.maxAscent() / 3;
114         case Superscript:
115                 return -fm.maxAscent() / 2;
116         }
117         // shut up compiler
118         return 0;
119 }
120
121
122 /////////////////////////////////////////////////////////////////////
123 //
124 // InsetScript
125 //
126 /////////////////////////////////////////////////////////////////////
127
128 InsetScript::InsetScript(Buffer * buf, InsetScriptParams const & params)
129         : InsetText(buf, InsetText::PlainLayout), params_(params)
130 {
131         setDrawFrame(false);
132 }
133
134
135 InsetScript::InsetScript(Buffer * buf, string const & label)
136         : InsetText(buf)
137 {
138         setDrawFrame(false);
139         params_.type = scripttranslator().find(label);
140 }
141
142
143 InsetScript::~InsetScript()
144 {
145 }
146
147
148 docstring InsetScript::layoutName() const
149 {
150         return from_ascii("Script:" + scripttranslator().find(params_.type));
151 }
152
153
154 void InsetScript::metrics(MetricsInfo & mi, Dimension & dim) const
155 {
156         int const shift = params_.shift(mi.base.font);
157         Changer dummy = mi.base.changeScript();
158         InsetText::metrics(mi, dim);
159         dim.asc -= shift;
160         dim.des += shift;
161 }
162
163
164 void InsetScript::draw(PainterInfo & pi, int x, int y) const
165 {
166         int const shift = params_.shift(pi.base.font);
167         Changer dummy = pi.base.changeScript();
168         InsetText::draw(pi, x, y + shift);
169 }
170
171
172 void InsetScript::cursorPos(BufferView const & bv,
173                 CursorSlice const & sl, bool boundary, int & x, int & y) const
174 {
175         Font const font = bv.textMetrics(&text()).displayFont(sl.pit(), sl.pos());
176         int const shift = params_.shift(font.fontInfo());
177         InsetText::cursorPos(bv, sl, boundary, x, y);
178         y += shift;
179 }
180
181
182 void InsetScript::write(ostream & os) const
183 {
184         params_.write(os);
185         text().write(os);
186 }
187
188
189 void InsetScript::read(Lexer & lex)
190 {
191         params_.read(lex);
192         InsetText::read(lex);
193 }
194
195
196 void InsetScript::edit(Cursor & cur, bool front, EntryDirection entry_from)
197 {
198         cur.push(*this);
199         InsetText::edit(cur, front, entry_from);
200 }
201
202
203 Inset * InsetScript::editXY(Cursor & cur, int x, int y)
204 {
205         cur.push(*this);
206         return InsetText::editXY(cur, x, y);
207 }
208
209 void InsetScript::doDispatch(Cursor & cur, FuncRequest & cmd)
210 {
211         switch (cmd.action()) {
212         case LFUN_INSET_MODIFY:
213                 cur.recordUndoInset(this);
214                 string2params(to_utf8(cmd.argument()), params_);
215                 break;
216         default:
217                 InsetText::doDispatch(cur, cmd);
218                 break;
219         }
220 }
221
222
223 bool InsetScript::insetAllowed(InsetCode code) const
224 {
225         switch (code) {
226         // code that is not allowed in a script
227         case BIBITEM_CODE:
228         case BIBTEX_CODE:
229         case BOX_CODE:
230         case BRANCH_CODE:
231         case CAPTION_CODE:
232         case COLLAPSIBLE_CODE:
233         case FLOAT_CODE:
234         case FLOAT_LIST_CODE:
235         case FOOT_CODE:
236         case INCLUDE_CODE:
237         case INDEX_PRINT_CODE:
238         case LISTINGS_CODE:
239         case MARGIN_CODE:
240         case MATH_MACRO_CODE:
241         case MATHMACRO_CODE:
242         case NEWLINE_CODE:
243         case NEWPAGE_CODE:
244         case NOMENCL_PRINT_CODE:
245         case QUOTE_CODE:
246         case PREVIEW_CODE:
247         case TABULAR_CODE:
248         case TOC_CODE:
249         case WRAP_CODE:
250                 return false;
251         default:
252                 return InsetText::insetAllowed(code);
253         }
254 }
255
256 bool InsetScript::getStatus(Cursor & cur, FuncRequest const & cmd,
257                 FuncStatus & flag) const
258 {
259         switch (cmd.action()) {
260         case LFUN_MATH_DISPLAY:
261         case LFUN_BOX_INSERT:
262         case LFUN_BRANCH_INSERT:
263         case LFUN_CAPTION_INSERT:
264         case LFUN_FLOAT_INSERT:
265         case LFUN_FLOAT_LIST_INSERT:
266         case LFUN_FLOAT_WIDE_INSERT:
267         case LFUN_FOOTNOTE_INSERT:
268         case LFUN_INDEX_PRINT:
269         case LFUN_LISTING_INSERT:
270         case LFUN_MARGINALNOTE_INSERT:
271         case LFUN_NEWLINE_INSERT:
272         case LFUN_NEWPAGE_INSERT:
273         case LFUN_NOMENCL_PRINT:
274         case LFUN_PREVIEW_INSERT:
275         case LFUN_QUOTE_INSERT:
276         case LFUN_TABULAR_INSERT:
277         case LFUN_WRAP_INSERT:
278                 flag.setEnabled(false);
279                 return true;
280         case LFUN_INSET_MODIFY:
281                 flag.setEnabled(true);
282                 return true;
283         case LFUN_COMMAND_SEQUENCE: {
284                 // argument contains ';'-terminated commands
285                 string arg = to_utf8(cmd.argument());
286                 // prevent insertion of display math formulas like AMS align
287                 while (!arg.empty()) {
288                         string first;
289                         arg = support::split(arg, first, ';');
290                         FuncRequest func(lyxaction.lookupFunc(first));
291                         if (func.action() == LFUN_MATH_MUTATE) {
292                                 flag.setEnabled(false);
293                                 return true;
294                         }
295                 }
296                 break;
297         }
298         default:
299                 break;
300         }
301         return InsetText::getStatus(cur, cmd, flag);
302 }
303
304
305 docstring InsetScript::toolTip(BufferView const &, int, int) const
306 {
307         OutputParams rp(&buffer().params().encoding());
308         odocstringstream ods;
309         InsetText::plaintext(ods, rp, 200);
310         docstring content_tip = ods.str();
311         // shorten it if necessary
312         support::truncateWithEllipsis(content_tip, 200);
313         docstring res = scripttranslator_loc().find(params_.type);
314         if (!content_tip.empty())
315                 res += from_ascii(": ") + content_tip;
316         return res;
317 }
318
319
320 int InsetScript::plaintext(odocstringstream & os,
321         OutputParams const & runparams, size_t max_length) const
322 {
323         odocstringstream oss;
324         InsetText::plaintext(oss, runparams, max_length);
325         docstring const text = oss.str();
326         switch (params_.type) {
327         case InsetScriptParams::Subscript:
328                 if (text.size() == 1) {
329                         char_type const c = support::subscript(text[0]);
330                         if (c != text[0]) {
331                                 os.put(c);
332                                 return 0;
333                         }
334                 }
335                 os << '[' << buffer().B_("subscript") << ':';
336                 break;
337         case InsetScriptParams::Superscript:
338                 if (text.size() == 1) {
339                         char_type const c = support::superscript(text[0]);
340                         if (c != text[0]) {
341                                 os.put(c);
342                                 return 0;
343                         }
344                 }
345                 os << '[' << buffer().B_("superscript") << ':';
346                 break;
347         }
348         InsetText::plaintext(os, runparams, max_length);
349         os << ']';
350
351         return PLAINTEXT_NEWLINE;
352 }
353
354
355 int InsetScript::docbook(odocstream & os, OutputParams const & runparams) const
356 {
357         docstring cmdname;
358         switch (params_.type) {
359         case InsetScriptParams::Subscript:
360                 cmdname = from_ascii("subscript");
361                 break;
362         case InsetScriptParams::Superscript:
363                 cmdname = from_ascii("superscript");
364                 break;
365         }
366         os << '<' + cmdname + '>';
367         int const i = InsetText::docbook(os, runparams);
368         os << "</" + cmdname + '>';
369
370         return i;
371 }
372
373
374 string InsetScript::contextMenuName() const
375 {
376         return "context-script";
377 }
378
379
380 string InsetScript::params2string(InsetScriptParams const & params)
381 {
382         ostringstream data;
383         data << "script ";
384         params.write(data);
385         return data.str();
386 }
387
388
389 void InsetScript::string2params(string const & in, InsetScriptParams & params)
390 {
391         params = InsetScriptParams();
392
393         if (in.empty())
394                 return;
395
396         istringstream data(in);
397         Lexer lex;
398         lex.setStream(data);
399         lex.setContext("InsetScript::string2params");
400         lex >> "script" >> "script";
401
402         params.read(lex);
403 }
404
405
406 } // namespace lyx