]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlMath.C
Finish the task of removing all cruft from the header files.
[lyx.git] / src / frontends / controllers / ControlMath.C
1 /**
2  * \file ControlMath.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlMath.h"
14 #include "debug.h"
15 #include "funcrequest.h"
16
17 #include "support/LAssert.h"
18 #include "support/lyxalgo.h" // sorted
19 #include "support/lstrings.h"
20 #include "support/filetools.h"
21
22
23 using namespace lyx::support;
24
25
26 ControlMath::ControlMath(Dialog & dialog)
27         : Dialog::Controller(dialog)
28 {}
29
30
31 void ControlMath::dispatchFunc(kb_action action, string const & arg) const
32 {
33         kernel().dispatch(FuncRequest(action, arg));
34 }
35
36
37 void ControlMath::dispatchInsert(string const & name) const
38 {
39         dispatchFunc(LFUN_INSERT_MATH, '\\' + name);
40 }
41
42
43 void ControlMath::dispatchSubscript() const
44 {
45         dispatchFunc(LFUN_SUBSCRIPT);
46 }
47
48
49 void ControlMath::dispatchSuperscript() const
50 {
51         dispatchFunc(LFUN_SUPERSCRIPT);
52 }
53
54
55 void ControlMath::dispatchCubeRoot() const
56 {
57         dispatchFunc(LFUN_INSERT_MATH, "\\root");
58         dispatchFunc(LFUN_SELFINSERT, "3");
59         dispatchFunc(LFUN_RIGHT);
60 }
61
62
63 void ControlMath::dispatchMatrix(string const & str) const
64 {
65         dispatchFunc(LFUN_INSERT_MATRIX, str);
66 }
67
68
69 void ControlMath::dispatchDelim(string const & str) const
70 {
71         dispatchFunc(LFUN_MATH_DELIM, str);
72 }
73
74
75 void ControlMath::dispatchToggleDisplay() const
76 {
77         dispatchFunc(LFUN_MATH_DISPLAY);
78 }
79
80
81 void ControlMath::showDialog(string const & name) const
82 {
83         dispatchFunc(LFUN_DIALOG_SHOW, name);
84 }
85
86
87 char const * function_names[] = {
88         "arccos", "arcsin", "arctan", "arg", "bmod",
89         "cos", "cosh", "cot", "coth", "csc", "deg",
90         "det", "dim", "exp", "gcd", "hom", "inf", "ker",
91         "lg", "lim", "liminf", "limsup", "ln", "log",
92         "max", "min", "sec", "sin", "sinh", "sup",
93         "tan", "tanh", "Pr", ""
94 };
95
96 int const nr_function_names = sizeof(function_names) / sizeof(char const *) - 1;
97
98 char const * latex_dots[] = {
99         "ldots", "cdots", "vdots", "ddots", ""
100 };
101
102 int const nr_latex_dots = sizeof(latex_dots) / sizeof(char const *) - 1;
103
104 char const * latex_deco[] = {
105         "widehat", "widetilde", "overbrace", "overleftarrow", "overrightarrow",
106         "overline", "underbrace", "underline", "underleftarrow", "underrightarrow",
107         "underleftrightarrow", "overleftrightarrow",
108         "hat", "acute", "bar", "dot",
109         "check", "grave", "vec", "ddot",
110         "breve", "tilde", ""
111 };
112
113 int const nr_latex_deco = sizeof(latex_deco) / sizeof(char const *) - 1;
114
115 char const * latex_arrow[] = {
116         "downarrow", "leftarrow", "Downarrow", "Leftarrow",
117         "hookleftarrow", "rightarrow", "uparrow", "Rightarrow", "Uparrow",
118         "hookrightarrow", "updownarrow", "Leftrightarrow", "leftharpoonup",
119         "rightharpoonup", "rightleftharpoons", "leftrightarrow", "Updownarrow",
120         "leftharpoondown", "rightharpoondown", "mapsto",
121         "Longleftarrow", "Longrightarrow", "Longleftrightarrow",
122         "longleftrightarrow", "longleftarrow", "longrightarrow", "longmapsto",
123         "nwarrow", "nearrow", "swarrow", "searrow",  "",
124 };
125
126 int const nr_latex_arrow = sizeof(latex_arrow) / sizeof(char const *);
127
128 char const * latex_bop[] = {
129         "pm", "cap", "diamond", "oplus",
130         "mp", "cup", "bigtriangleup", "ominus",
131         "times", "uplus", "bigtriangledown", "otimes",
132         "div", "sqcap", "triangleright", "oslash",
133         "cdot", "sqcup", "triangleleft", "odot",
134         "star", "vee", "amalg", "bigcirc",
135         "setminus", "wedge", "dagger", "circ",
136         "bullet", "wr", "ddagger", ""
137 };
138
139 int const nr_latex_bop = sizeof(latex_bop) / sizeof(char const *);
140
141 char const * latex_brel[] = {
142         "leq", "geq", "equiv", "models",
143         "prec", "succ", "sim", "perp",
144         "preceq", "succeq", "simeq", "mid",
145         "ll", "gg", "asymp", "parallel",
146         "subset", "supset", "approx", "smile",
147         "subseteq", "supseteq", "cong", "frown",
148         "sqsubseteq", "sqsupseteq", "doteq", "neq",
149         "in", "ni", "propto", "notin",
150         "vdash", "dashv", "bowtie", ""
151 };
152
153 int const nr_latex_brel = sizeof(latex_brel) / sizeof(char const *);
154
155 char const * latex_greek[] = {
156         "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi",
157         "Sigma", "Upsilon", "Phi", "Psi", "Omega",
158         "alpha", "beta", "gamma", "delta", "epsilon", "varepsilon", "zeta",
159         "eta", "theta", "vartheta", "iota", "kappa", "lambda", "mu",
160         "nu", "xi", "pi", "varpi", "rho", "sigma", "varsigma",
161         "tau", "upsilon", "phi", "varphi", "chi", "psi", "omega", ""
162 };
163
164 int const nr_latex_greek = sizeof(latex_greek) / sizeof(char const *);
165
166 char const * latex_misc[] = {
167         "nabla", "partial", "infty", "prime", "ell",
168         "emptyset", "exists", "forall", "imath",  "jmath",
169         "Re", "Im", "aleph", "wp", "hbar",
170         "angle", "top", "bot", "Vert", "neg",
171         "flat", "natural", "sharp", "surd", "triangle",
172         "diamondsuit", "heartsuit", "clubsuit", "spadesuit",
173         "textrm Å", "textrm Ø", "mathcircumflex", "_",
174         "mathrm T",
175         "mathbb N", "mathbb Z", "mathbb Q",
176         "mathbb R", "mathbb C", "mathbb H",
177         "mathcal F", "mathcal L",
178         "mathcal H", "mathcal O", ""
179 };
180
181 int const nr_latex_misc = sizeof(latex_misc) / sizeof(char const *);
182
183 char const * latex_varsz[] = {
184         "sum", "int", "oint",
185         "prod", "coprod", "bigsqcup",
186         "bigotimes", "bigodot", "bigoplus",
187         "bigcap", "bigcup", "biguplus",
188         "bigvee", "bigwedge", ""
189 };
190
191 int const nr_latex_varsz = sizeof(latex_varsz) / sizeof(char const *);
192
193 char const * latex_ams_misc[] = {
194         "digamma", "varkappa", "beth", "daleth", "gimel",
195         "ulcorner", "urcorner", "llcorner", "lrcorner",
196         "hbar", "hslash", "vartriangle",
197         "triangledown", "square", "lozenge",
198         "circledS", "angle", "measuredangle",
199         "nexists", "mho", "Finv",
200         "Game", "Bbbk", "backprime",
201         "varnothing", "blacktriangle", "blacktriangledown",
202         "blacksquare", "blacklozenge", "bigstar",
203         "sphericalangle", "complement", "eth",
204         "diagup", "diagdown", ""
205 };
206
207 int const nr_latex_ams_misc = sizeof(latex_ams_misc) / sizeof(char const *);
208
209 char const * latex_ams_arrows[] = {
210         "dashrightarrow", "dashleftarrow", "leftleftarrows",
211         "leftrightarrows", "Lleftarrow", "twoheadleftarrow",
212         "leftarrowtail", "looparrowleft", "leftrightharpoons",
213         "curvearrowleft", "circlearrowleft", "Lsh",
214         "upuparrows", "upharpoonleft", "downharpoonleft",
215         "multimap", "leftrightsquigarrow", "rightrightarrows",
216         "rightleftarrows", "rightrightarrows", "rightleftarrows",
217         "twoheadrightarrow", "rightarrowtail", "looparrowright",
218         "rightleftharpoons", "curvearrowright", "circlearrowright",
219         "Rsh", "downdownarrows", "upharpoonright",
220         "downharpoonright", "rightsquigarrow",
221         "nleftarrow", "nrightarrow", "nLeftarrow",
222         "nRightarrow", "nleftrightarrow", "nLeftrightarrow", ""
223 };
224
225 int const nr_latex_ams_arrows = sizeof(latex_ams_arrows) / sizeof(char const *);
226
227 char const * latex_ams_rel[] = {
228         "leqq", "leqslant", "eqslantless", "eqslantgtr", "gtrsim", "gtrapprox",
229         "lesssim", "lessapprox", "approxeq", "gtrdot", "ggg", "gtrless",
230         "lessdot", "lll", "lessgtr", "gtreqless", "gtreqqless", "eqcirc",
231         "lesseqgtr", "lesseqqgtr", "doteqdot", "circeq", "triangleq", "thicksim",
232         "risingdotseq", "fallingdotseq", "backsim", "thickapprox", "supseteqq", "Supset",
233         "backsimeq", "subseteqq", "Subset", "sqsupset", "succcurlyeq", "curlyeqsucc",
234         "sqsubset", "preccurlyeq", "curlyeqprec", "succsim", "succapprox", "vartriangleright",
235         "precsim", "precapprox", "vartriangleleft", "trianglerighteq", "Vdash", "shortmid",
236         "trianglelefteq", "vDash", "Vvdash", "shortparallel", "between", "pitchfork",
237         "smallsmile", "smallfrown", "bumpeq", "varpropto", "blacktriangleleft", "therefore",
238         "Bumpeq", "geqq", "geqslant", "backepsilon", "blacktriangleright", "because", ""
239 };
240
241 int const nr_latex_ams_rel = sizeof(latex_ams_rel) / sizeof(char const *);
242
243 char const * latex_ams_nrel[] = {
244         "nless", "nleq", "nleqslant", "ngeqslant", "ngeqq", "gneq",
245         "nleqq", "lneq", "lneqq", "gneqq", "gvertneqq", "gnsim",
246         "lvertneqq", "lnsim", "lnapprox", "gnapprox", "nsucc", "nsucceq",
247         "nprec", "npreceq", "precnsim","succnsim", "succnapprox", "ncong",
248         "precnapprox", "nsim", "nshortmid", "nshortparallel", "nparallel", "nvDash",
249
250         "nmid", "nvdash", "nvDash", "nVDash", "ntriangleright", "ntrianglerighteq",
251         "ntriangleleft", "ntrianglelefteq", "nsubseteq", "nsupseteq", "nsupseteqq", "supsetneq",
252         "subsetneq", "varsubsetneq", "subsetneqq", "varsupsetneq", "supsetneqq", "varsupsetneqq",
253         "varsubsetneqq", "ngtr", "ngeq", "", "", ""
254 };
255
256 int const nr_latex_ams_nrel = sizeof(latex_ams_nrel) / sizeof(char const *);
257
258
259 char const * latex_ams_ops[] = {
260         "dotplus", "smallsetminus", "Cap",
261         "Cup", "barwedge", "veebar",
262         "doublebarwedge", "boxminus", "boxtimes",
263         "boxdot", "boxplus", "divideontimes",
264         "ltimes", "rtimes", "leftthreetimes",
265         "rightthreetimes", "curlywedge", "curlyvee",
266         "circleddash", "circledast", "circledcirc",
267         "centerdot", "intercal", ""
268 };
269
270 int const nr_latex_ams_ops = sizeof(latex_ams_ops) / sizeof(char const *);
271
272
273 namespace {
274
275 struct XPMmap {
276         char const * key;
277         char const * value;
278 };
279
280
281 bool operator<(XPMmap const & lhs, XPMmap const & rhs)
282 {
283                 return compare(lhs.key, rhs.key) < 0;
284 }
285
286
287 struct CompareKey {
288         CompareKey(string const & name) : name_(name) {}
289         bool operator()(XPMmap const & other) {
290                 return compare(other.key, name_.c_str()) == 0;
291         }
292 private:
293         string const name_;
294 };
295
296
297 XPMmap sorted_xpm_map[] = {
298         { "Bumpeq", "bumpeq2" },
299         { "Cap", "cap2" },
300         { "Cup", "cup2" },
301         { "Delta", "delta2" },
302         { "Downarrow", "downarrow2" },
303         { "Gamma", "gamma2" },
304         { "Lambda", "lambda2" },
305         { "Leftarrow", "leftarrow2" },
306         { "Leftrightarrow", "leftrightarrow2" },
307         { "Longleftarrow", "longleftarrow2" },
308         { "Longleftrightarrow", "longleftrightarrow2" },
309         { "Longrightarrow", "longrightarrow2" },
310         { "Omega", "omega2" },
311         { "Phi", "phi2" },
312         { "Pi", "pi2" },
313         { "Psi", "psi2" },
314         { "Rightarrow", "rightarrow2" },
315         { "Sigma", "sigma2" },
316         { "Subset", "subset2" },
317         { "Supset", "supset2" },
318         { "Theta", "theta2" },
319         { "Uparrow", "uparrow2" },
320         { "Updownarrow", "updownarrow2" },
321         { "Upsilon", "upsilon2" },
322         { "Vdash", "vdash3" },
323         { "Xi", "xi2" },
324         { "nLeftarrow", "nleftarrow2" },
325         { "nLeftrightarrow", "nleftrightarrow2" },
326         { "nRightarrow", "nrightarrow2" },
327         { "nVDash", "nvdash3" },
328         { "nvDash", "nvdash2" },
329         { "textrm_Å", "textrm_A" },
330         { "textrm_Ø", "textrm_0" },
331         { "vDash", "vdash2" }
332 };
333
334 size_t const nr_sorted_xpm_map = sizeof(sorted_xpm_map) / sizeof(XPMmap);
335
336 } // namespace anon
337
338
339 string const find_xpm(string const & name)
340 {
341         XPMmap const * const begin = sorted_xpm_map;
342         XPMmap const * const end = begin + nr_sorted_xpm_map;
343         Assert(lyx::sorted(begin, end));
344
345         XPMmap const * const it =
346                 std::find_if(begin, end, CompareKey(name));
347
348         string xpm_name;
349         if (it != end)
350                 xpm_name = it->value;
351         else {
352                 xpm_name = subst(name, "_", "underscore");
353                 xpm_name = subst(xpm_name, ' ', '_');
354
355                 // This way we can have "math-delim { }" on the toolbar.
356                 xpm_name = subst(xpm_name, "(", "lparen");
357                 xpm_name = subst(xpm_name, ")", "rparen");
358                 xpm_name = subst(xpm_name, "[", "lbracket");
359                 xpm_name = subst(xpm_name, "]", "rbracket");
360                 xpm_name = subst(xpm_name, "{", "lbrace");
361                 xpm_name = subst(xpm_name, "}", "rbrace");
362                 xpm_name = subst(xpm_name, "|", "bars");
363         }
364
365         lyxerr[Debug::GUI] << "find_xpm(" << name << ")\n"
366                            << "Looking for math XPM called \""
367                            << xpm_name << '"' << std::endl;
368
369         return LibFileSearch("images/math/", xpm_name, "xpm");
370 }