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