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 const 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, string const &);
31 extern int mathed_string_height(short, int, string const &, 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()
155 ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des);
159 if (array->empty()) return;
162 MathedXIter data(this);
167 s = reinterpret_cast<char *>(data.GetString(ls));
168 mathed_string_height(data.FCode(), size, s, asc, des);
169 if (asc > ascent) ascent = asc;
170 if (des > descent) descent = des;
172 mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
174 if (MathIsInset(cx)) {
175 MathedInset * p = data.GetInset();
178 if (cx == LM_TC_UP) {
179 asc += (limits) ? p->Height() + 4: p->Ascent() +
180 ((p->Descent()>asc) ? p->Descent() - asc + 4: 0);
182 if (cx == LM_TC_DOWN) {
183 des += ((limits) ? p->Height() + 4: p->Height() - p->Ascent() / 2);
188 if (asc > ascent) ascent = asc;
189 if (des > descent) descent = des;
190 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
191 limits = p->GetLimits();
194 if (cx == LM_TC_TAB) {
196 data.GetIncPos(x, y);
197 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
198 if (ascent<df_asc) ascent = df_asc;
201 data.setTab(x-tb, tab);
207 if (cx == LM_TC_CR) {
210 data.GetIncPos(x, y);
211 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
212 if (ascent<df_asc) ascent = df_asc;
215 data.setTab(x - tb, tab);
216 } else //if (GetColumns() == 1)
219 data.GetIncPos(x, y);
221 if (ascent<df_asc) ascent = df_asc;
224 data.subMetrics(ascent, descent);
229 lyxerr << "Mathed Error: Unrecognized code[" << cx
235 data.GetIncPos(width, ls);
237 // No matter how simple is a matrix, it is NOT a subparagraph
239 if (cxp == LM_TC_TAB) {
240 if (ascent<df_asc) ascent = df_asc;
243 data.setTab(width - tb, tab);
247 data.subMetrics(ascent, descent);
252 MathSqrtInset::draw(Painter & pain, int x, int y)
254 MathParInset::draw(pain, x + hmax + 2, y);
257 int h2 = Height() / 2;
258 int w2 = (Height() > 4 * hmax) ? hmax : hmax / 2;
260 xp[0] = x + hmax + wbody; yp[0] = y - h;
261 xp[1] = x + hmax; yp[1] = y - h;
262 xp[2] = x + w2; yp[2] = y + d;
263 xp[3] = x; yp[3] = y + d - h2;
264 pain.lines(xp, yp, 4, LColor::mathline);
269 MathSqrtInset::Metrics()
271 MathParInset::Metrics();
275 hmax = mathed_char_height(LM_TC_VAR, size, 'I', a, b);
276 if (hmax < 10) hmax = 10;
283 MathFracInset::draw(Painter & pain, int x, int y)
289 if (size == LM_ST_DISPLAY) ++size;
290 MathParInset::draw(pain, x + (width - w0) / 2, y - des0);
291 den->draw(pain, x + (width - w1) / 2, y + den->Ascent() + 2 - dh);
293 if (objtype == LM_OT_FRAC)
294 pain.line(x + 2, y - dh, x + width - 4, y - dh, LColor::mathline);
300 MathFracInset::Metrics()
304 dh = mathed_char_height(LM_TC_CONST, size, 'I', a, b)/2;
309 if (size == LM_ST_DISPLAY) ++size;
310 MathParInset::Metrics();
313 int as = Height() + 2 + dh;
314 des0 = Descent() + 2 + dh;
317 width = ((w0 > w1) ? w0: w1) + 12;
319 descent = den->Height()+ 2 - dh;
325 MathBigopInset::draw(Painter & pain, int x, int y)
330 if (sym < 256 || sym == LM_oint) {
331 s += (sym == LM_oint) ? LM_int : sym;
337 if (sym == LM_oint) {
338 pain.arc(x, y - 5 * width / 4, width, width, 0, 360*64,
342 pain.text(x, y, s, mathed_get_font(t, size));
347 MathBigopInset::Metrics()
353 if (sym < 256 || sym == LM_oint) {
354 c = (sym == LM_oint) ? LM_int: sym;
361 mathed_string_height(t, size, s, ascent, descent);
362 width = mathed_string_width(t, size, s);
363 if (sym == LM_oint) width += 2;