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