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