]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/MathsSymbols.C
John's spellchecker patch plus a new helper function getStringFromBrowser.
[lyx.git] / src / frontends / xforms / MathsSymbols.C
1 /**
2  * \file MathsSymbols.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author John Levon
8  */
9
10 #include <config.h>
11 #include <algorithm>
12  
13 #include XPM_H_LOCATION
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "support/LAssert.h" 
20 #include "support/lstrings.h"
21 #include "debug.h"
22 #include "MathsSymbols.h"
23 #include "FormMathsPanel.h"
24  
25 using std::max;
26 using std::endl;
27 using std::ostream;
28
29 #ifndef CXX_GLOBAL_CSTD
30 using std::strstr;
31 #endif
32
33
34 /* Latex code for those bitmaps */
35
36 #include "greek.xbm"
37 #include "arrows.xbm"
38 #include "brel.xbm"
39 #include "bop.xbm"
40 #include "misc.xbm"
41 #include "varsz.xbm"
42 #include "dots.xbm"
43 #include "mathed/math_parser.h"
44 #include "frac.xpm"
45 #include "sub.xpm"
46 #include "super.xpm"
47 #include "sqrt.xpm"
48 #include "delim.xbm"
49 #include "delim.xpm"
50 #include "deco.xbm"
51 #include "deco.xpm"
52 #include "space.xpm"
53 #include "matrix.xpm"
54 #include "equation.xpm"
55
56 char const * function_names[] = {
57         "arccos", "arcsin", "arctan", "arg", "bmod",
58         "cos", "cosh", "cot", "coth", "csc", "deg",
59         "det", "dim", "exp", "gcd", "hom", "inf", "ker",
60         "lg", "lim", "liminf", "limsup", "ln", "log",
61         "max", "min", "sec", "sin", "sinh", "sup",
62         "tan", "tanh"
63 };
64
65 int const nr_function_names = sizeof(function_names) /
66                                      sizeof(char const *);
67
68 char const * latex_arrow[] = {
69         "downarrow", "leftarrow", "Downarrow", "Leftarrow",
70         "hookleftarrow", "rightarrow", "uparrow", "Rightarrow", "Uparrow",
71         "hookrightarrow", "updownarrow", "Leftrightarrow", "leftharpoonup",
72         "rightharpoonup", "rightleftharpoons", "leftrightarrow", "Updownarrow",
73         "leftharpoondown", "rightharpoondown", "mapsto",
74         "Longleftarrow", "Longrightarrow", "Longleftrightarrow",
75         "longleftrightarrow", "longleftarrow", "longrightarrow", "longmapsto",
76         "nwarrow", "nearrow", "swarrow", "searrow",  "",
77 };
78
79 int const nr_latex_arrow = sizeof(latex_arrow) / sizeof(char const *);
80
81 char const * latex_bop[] = {
82         "pm", "cap", "diamond", "oplus",
83         "mp", "cup", "bigtriangleup", "ominus",
84         "times", "uplus", "bigtriangledown", "otimes",
85         "div", "sqcap", "triangleright", "oslash",
86         "cdot", "sqcup", "triangleleft", "odot",
87         "star", "vee", "amalg", "bigcirc",
88         "setminus", "wedge", "dagger", "circ",
89         "bullet", "wr", "ddagger", ""
90 };
91
92 int const nr_latex_bop = sizeof(latex_bop) / sizeof(char const *);
93
94 char const * latex_brel[] = {
95         "leq", "geq", "equiv", "models",
96         "prec", "succ", "sim", "perp",
97         "preceq", "succeq", "simeq", "mid",
98         "ll", "gg", "asymp", "parallel",
99         "subset", "supset", "approx", "smile",
100         "subseteq", "supseteq", "cong", "frown",
101         "sqsubseteq", "sqsupseteq", "doteq", "neq",
102         "in", "ni", "propto", "notin",
103         "vdash", "dashv", "bowtie", ""
104 };
105
106 int const nr_latex_brel = sizeof(latex_brel) / sizeof(char const *);
107
108 char const * latex_dots[] = {
109         "ldots", "cdots", "vdots", "ddots"
110 };
111
112 int const nr_latex_dots = sizeof(latex_dots) / sizeof(char const *);
113
114 char const * latex_greek[] = {
115         "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi",
116         "Sigma", "Upsilon", "Phi", "Psi", "Omega",
117         "alpha", "beta", "gamma", "delta", "epsilon", "varepsilon", "zeta",
118         "eta", "theta", "vartheta", "iota", "kappa", "lambda", "mu",
119         "nu", "xi", "pi", "varpi", "rho", "sigma", "varsigma",
120         "tau", "upsilon", "phi", "varphi", "chi", "psi", "omega", ""
121 };
122
123 int const nr_latex_greek = sizeof(latex_greek) / sizeof(char const *);
124
125 char const * latex_misc[] = {
126         "nabla", "partial", "infty", "prime", "ell",
127         "emptyset", "exists", "forall", "imath",  "jmath",
128         "Re", "Im", "aleph", "wp", "hbar",
129         "angle", "top", "bot", "Vert", "neg",
130         "flat", "natural", "sharp", "surd", "triangle",
131         "diamondsuit", "heartsuit", "clubsuit", "spadesuit", ""
132 };
133
134 int const nr_latex_misc = sizeof(latex_misc) / sizeof(char const *);
135
136 char const * latex_varsz[] = {
137         "sum", "int", "oint",
138         "prod", "coprod", "bigsqcup",
139         "bigotimes", "bigodot", "bigoplus",
140         "bigcap", "bigcup", "biguplus",
141         "bigvee", "bigwedge", ""
142 };
143
144 int const nr_latex_varsz = sizeof(latex_varsz) / sizeof(char const *);
145
146 static char const ** mathed_get_pixmap_from_icon(int d)
147 {
148         switch (d) {
149                 case MM_FRAC: return frac;
150                 case MM_SQRT: return sqrt_xpm;
151                 case MM_SUPER: return super_xpm;
152                 case MM_SUB: return sub_xpm;
153                 case MM_DELIM: return delim;
154                 case MM_MATRIX: return matrix;
155                 case MM_EQU: return equation;
156                 case MM_DECO: return deco;
157                 case MM_SPACE: return space_xpm;
158                 default: return 0;
159         }
160 }
161  
162 static char const ** pixmapFromBitmapData(char const * s, int wx, int hx)
163 {
164         char const ** data = 0;
165
166         int id = -1;
167
168         int i = 0;
169         for (; i < 6; ++i) {
170                 char const ** latex_str = 0;
171                 switch (i) {
172                 case 0: latex_str = latex_greek; break;
173                 case 1: latex_str = latex_bop; break;
174                 case 2: latex_str = latex_brel; break;
175                 case 3: latex_str = latex_arrow; break;
176                 case 4: latex_str = latex_varsz; break;
177                 case 5: latex_str = latex_misc; break;
178                 }
179
180                 for (int k = 0; latex_str[k][0] > ' '; ++k) {
181                         if (compare(latex_str[k], s) == 0) {
182                                 id = k;
183                                 break;
184                         }
185                 }
186                 if (id >= 0) break;
187         }
188         if (i < 6 && id >= 0) {
189                 unsigned char const * bdata = 0;
190                 int w = 0;
191                 int h = 0;
192                 int dw = 0;
193                 int dh = 0;
194
195                 lyxerr[Debug::MATHED] << "Imando " << i << ", " << id << endl;
196                 switch (i) {
197                 case 0:
198                         if (id <= 10) {
199                                 w = Greek_width;
200                                 h = Greek_height;
201                                 bdata = Greek_bits;
202                                 dw = 6;
203                                 dh = 2;
204                         } else {
205                                 w = greek_width;
206                                 h = greek_height;
207                                 bdata = greek_bits;
208                                 dw = 7;
209                                 dh = 4;
210                                 id -= 11;
211                         }
212                         break;
213                 case 1:
214                         w = bop_width;
215                         h = bop_height;
216                         bdata = bop_bits;
217                         dw = 4;
218                         dh = 8;
219                         break;
220                 case 2:
221                         w = brel_width;
222                         h = brel_height;
223                         bdata = brel_bits;
224                         dw = 4;
225                         dh = 9;
226                         break;
227                 case 3:
228                         if (id < 20) {
229                                 w = arrow_width;
230                                 h = arrow_height;
231                                 bdata = arrow_bits;
232                                 dw = 5;
233                                 dh = 4;
234                         } else if (id > 28) {
235                                 w = darrow_width;
236                                 h = darrow_height;
237                                 bdata = darrow_bits;
238                                 dw = 2;
239                                 dh = 2;
240                                 id -= 29;
241                         } else {
242                                 w = larrow_width;
243                                 h = larrow_height;
244                                 bdata = larrow_bits;
245                                 dw = 2;
246                                 dh = 4;
247                                 id -= 20;
248                         }
249                         break;
250                 case 4:
251                         w = varsz_width;
252                         h = varsz_height;
253                         bdata = varsz_bits;
254                         dw = 3;
255                         dh = 5;
256                         break;
257                 case 5:
258                         w = misc_width;
259                         h = misc_height;
260                         bdata = misc_bits;
261                         dw = 5;
262                         dh = 6;
263                         break;
264                 }
265                 int ww = w / dw;
266                 int hh = h / dh;
267
268                 XImage * xima = XCreateImage(fl_get_display(), 0, 1, XYBitmap, 0,
269                                              const_cast<char*>(reinterpret_cast<char const *>(bdata)), w, h, 8, 0);
270                 xima->byte_order = LSBFirst;
271                 xima->bitmap_bit_order = LSBFirst;
272                 int x = (id % dw) * ww;
273                 int y = (id/dw) * hh;
274                 if (ww > wx) ww = wx;
275                 if (hh > hx) hh = hx;
276                 XImage * sbima = XSubImage(xima, x, y, ww, hh);
277                 XpmCreateDataFromImage(fl_get_display(), const_cast<char***>(&data), sbima, sbima, 0);
278
279                 // Dirty hack to get blue symbols quickly
280                 char * sx = const_cast<char*>(strstr(data[2], "FFFFFFFF"));
281                 if (sx) {
282                         for (int k = 0; k < 8; ++k) sx[k] = '0';
283                 }
284
285 //      XDestroyImage(xima);
286         }
287
288         return data;
289 }
290
291  
292 char const ** get_pixmap_from_symbol(char const * arg, int wx, int hx)
293 {
294         lyx::Assert(arg);
295         
296         char const ** data = 0;
297         latexkeys const * l = in_word_set(arg);
298         if (!l)
299                 return 0;
300
301         switch (l->token) {
302         case LM_TK_FRAC:
303                 data = mathed_get_pixmap_from_icon(MM_FRAC);
304                 break;
305         case LM_TK_SQRT:
306                 data = mathed_get_pixmap_from_icon(MM_SQRT);
307                 break;
308         case LM_TK_SYM:
309         case LM_TK_CMR:
310         case LM_TK_CMSY:
311         case LM_TK_CMEX:
312         case LM_TK_CMM:
313         case LM_TK_MSA:
314         case LM_TK_MSB:
315                 // I have to use directly the bitmap data since the
316                 // bitmap tables are not yet created when this
317                 // function is called.
318                 data = pixmapFromBitmapData(arg, wx, hx);
319                 break;
320         }
321
322         return data;
323 }