3 * Purpose: Interaction and drawing for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Math drawing and interaction for a WYSIWYG math editor.
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, Alejandro Aguilar Sierra
12 * Version: 0.8beta, Mathed & Lyx project.
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
19 #include FORMS_H_LOCATION
20 #include "math_cursor.h"
21 #include "math_parser.h"
28 extern LyXFont mathed_get_font(short type, int size);
29 extern int mathed_char_width(short type, int style, byte c);
30 extern int mathed_string_width(short type, int style, byte const* s, int ls);
31 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
32 extern int mathed_char_height(short, int, byte, int&, int&);
35 MathSpaceInset::draw(Painter & pain, int x, int y)
38 // XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
40 // Sadly, HP-UX CC can't handle that kind of initialization.
45 xp[0] = ++x; yp[0] = y - 3;
47 xp[2] = x + width - 2; yp[2] = y;
48 xp[3] = x + width - 2; yp[3] = y - 3;
50 pain.lines(xp, yp, 4, (space) ? LColor::latex : LColor::math);
55 MathParInset::draw(Painter & pain, int x, int y)
59 int asc = df_asc, des = 0;
63 if (!array || array->empty()) {
65 MathedXIter data(this);
68 pain.rectangle(x, y - df_asc, df_width, df_asc, LColor::mathline);
71 MathedXIter data(this);
77 byte * s = data.GetString(ls);
78 drawStr(pain, data.FCode(), size, x, y, s, ls);
79 mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
83 if (MathIsInset(cx)) {
85 MathedInset * p = data.GetInset();
88 x -= (xp>p->Width()) ? p->Width()+(xp-p->Width())/2: xp;
89 yy -= (asc + p->Descent()+4);
91 yy -= (p->Descent()>asc) ? p->Descent()+4: asc;
93 if (cx == LM_TC_DOWN) {
95 x -= (xp>p->Width()) ? p->Width()+(xp-p->Width())/2: xp;
96 yy += des + p->Ascent() + 2;
98 yy += des + p->Ascent()/2;
103 p->draw(pain, x, yy);
104 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN) {
105 limits = p->GetLimits();
106 if (limits) xp = p->Width();
110 if (cx == LM_TC_TAB) {
111 if ((cxp == cx || cxp == LM_TC_CR || data.IsFirst())) { // && objtype == L
112 pain.rectangle(x, y - df_asc, df_width, df_asc,
118 if (cx == LM_TC_CR) {
119 if (cxp == LM_TC_TAB || cxp == LM_TC_CR || data.IsFirst()) { // && objtype == LM_OT_MATRIX) {
120 pain.rectangle(x, y - df_asc, df_width, df_asc,
127 lyxerr << "GMathed Error: Unrecognized code[" << cx
134 if (cxp == LM_TC_TAB || cxp == LM_TC_CR) { // && objtype == LM_OT_MATRIX) {
136 pain.rectangle(x, y - df_asc, df_width, df_asc, LColor::mathline);
142 MathParInset::Metrics()
146 int asc= df_asc, des= 0;
151 ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des);
155 if (array->empty()) return;
158 MathedXIter data(this);
163 s = data.GetString(ls);
164 mathed_string_height(data.FCode(), size, s, ls, asc, des);
165 if (asc > ascent) ascent = asc;
166 if (des > descent) descent = des;
168 mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
170 if (MathIsInset(cx)) {
171 MathedInset *p = data.GetInset();
174 if (cx == LM_TC_UP) {
175 asc += (limits) ? p->Height()+4: p->Ascent() +
176 ((p->Descent()>asc) ? p->Descent()-asc+4: 0);
178 if (cx == LM_TC_DOWN) {
179 des += ((limits) ? p->Height()+4: p->Height()-p->Ascent()/2);
184 if (asc > ascent) ascent = asc;
185 if (des > descent) descent = des;
186 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
187 limits = p->GetLimits();
190 if (cx == LM_TC_TAB) {
192 data.GetIncPos(x, y);
193 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
194 if (ascent<df_asc) ascent = df_asc;
197 data.setTab(x-tb, tab);
203 if (cx == LM_TC_CR) {
206 data.GetIncPos(x, y);
207 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
208 if (ascent<df_asc) ascent = df_asc;
211 data.setTab(x-tb, tab);
212 } else //if (GetColumns() == 1)
215 data.GetIncPos(x, y);
217 if (ascent<df_asc) ascent = df_asc;
220 data.subMetrics(ascent, descent);
226 lyxerr << "Mathed Error: Unrecognized code[" << cx
232 data.GetIncPos(width, ls);
234 // No matter how simple is a matrix, it is NOT a subparagraph
236 if (cxp == LM_TC_TAB) {
237 if (ascent<df_asc) ascent = df_asc;
240 data.setTab(width-tb, tab);
244 data.subMetrics(ascent, descent);
249 MathSqrtInset::draw(Painter & pain, int x, int y)
251 MathParInset::draw(pain, x + hmax + 2, y);
254 int h2 = Height() / 2;
255 int w2 = (Height() > 4 * hmax) ? hmax : hmax / 2;
257 xp[0] = x + hmax + wbody; yp[0] = y - h;
258 xp[1] = x + hmax; yp[1] = y - h;
259 xp[2] = x + w2; yp[2] = y + d;
260 xp[3] = x; yp[3] = y + d - h2;
261 pain.lines(xp, yp, 4, LColor::mathline);
266 MathSqrtInset::Metrics()
268 MathParInset::Metrics();
272 hmax = mathed_char_height(LM_TC_VAR, size, 'I', a, b);
273 if (hmax < 10) hmax = 10;
280 MathFracInset::draw(Painter & pain, int x, int y)
286 if (size == LM_ST_DISPLAY) ++size;
287 MathParInset::draw(pain, x + (width - w0) / 2, y - des0);
288 den->draw(pain, x + (width - w1) / 2, y + den->Ascent() + 2 - dh);
290 if (objtype == LM_OT_FRAC)
291 pain.line(x + 2, y - dh, x + width - 4, y - dh, LColor::mathline);
297 MathFracInset::Metrics()
301 dh = mathed_char_height(LM_TC_CONST, size, 'I', a, b)/2;
306 if (size == LM_ST_DISPLAY) ++size;
307 MathParInset::Metrics();
310 int as = Height() + 2 + dh;
311 des0 = Descent() + 2 + dh;
314 width = ((w0 > w1) ? w0: w1) + 12;
316 descent = den->Height()+ 2 - dh;
322 MathBigopInset::draw(Painter & pain, int x, int y)
329 if (sym < 256 || sym == LM_oint) {
331 c = (sym == LM_oint) ? LM_int : sym;
339 if (sym == LM_oint) {
340 pain.arc(x, y - 5 * width / 4, width, width, 0, 360*64,
344 pain.text(x, y, s, ls, mathed_get_font(t, size));
349 MathBigopInset::Metrics()
356 if (sym < 256 || sym == LM_oint) {
358 c = (sym == LM_oint) ? LM_int: sym;
366 mathed_string_height(t, size,
367 reinterpret_cast<const unsigned char*>(s),
368 ls, ascent, descent);
369 width = mathed_string_width(t, size,
370 reinterpret_cast<const unsigned char*>(s),
372 if (sym == LM_oint) width += 2;