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