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();
87 if (limits) { x -= (xp>p->Width()) ? p->Width()+(xp-p->Width())/2: xp;
88 yy -= (asc + p->Descent()+4);
90 yy -= (p->Descent()>asc) ? p->Descent()+4: asc;
92 if (cx == LM_TC_DOWN) {
94 x -= (xp>p->Width()) ? p->Width()+(xp-p->Width())/2: xp;
95 yy += des + p->Ascent() + 2;
97 yy += des + p->Ascent()/2;
102 p->draw(pain, x, yy);
103 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN) {
104 limits = p->GetLimits();
105 if (limits) xp = p->Width();
109 if (cx == LM_TC_TAB) {
110 if ((cxp == cx || cxp == LM_TC_CR || data.IsFirst())) { // && objtype == L
111 pain.rectangle(x, y - df_asc, df_width, df_asc,
117 if (cx == LM_TC_CR) {
118 if (cxp == LM_TC_TAB || cxp == LM_TC_CR || data.IsFirst()) { // && objtype == LM_OT_MATRIX) {
119 pain.rectangle(x, y - df_asc, df_width, df_asc,
126 lyxerr << "GMathed Error: Unrecognized code[" << cx
133 if (cxp == LM_TC_TAB || cxp == LM_TC_CR) { // && objtype == LM_OT_MATRIX) {
135 pain.rectangle(x, y - df_asc, df_width, df_asc, LColor::mathline);
141 MathParInset::Metrics()
154 ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des);
158 if (array->empty()) return;
161 MathedXIter data(this);
166 s = reinterpret_cast<char *>(data.GetString(ls));
167 mathed_string_height(data.FCode(), size, s, asc, des);
168 if (asc > ascent) ascent = asc;
169 if (des > descent) descent = des;
171 mathed_char_height(LM_TC_CONST, size, 'y', asc, des);
173 if (MathIsInset(cx)) {
174 MathedInset * p = data.GetInset();
177 if (cx == LM_TC_UP) {
178 asc += (limits) ? p->Height() + 4: p->Ascent() +
179 ((p->Descent()>asc) ? p->Descent() - asc + 4: 0);
181 if (cx == LM_TC_DOWN) {
182 des += ((limits) ? p->Height() + 4: p->Height() - p->Ascent() / 2);
187 if (asc > ascent) ascent = asc;
188 if (des > descent) descent = des;
189 if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
190 limits = p->GetLimits();
193 if (cx == LM_TC_TAB) {
195 data.GetIncPos(x, y);
196 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
197 if (ascent<df_asc) ascent = df_asc;
200 data.setTab(x-tb, tab);
206 if (cx == LM_TC_CR) {
209 data.GetIncPos(x, y);
210 if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
211 if (ascent<df_asc) ascent = df_asc;
214 data.setTab(x - tb, tab);
215 } else //if (GetColumns() == 1)
218 data.GetIncPos(x, y);
220 if (ascent<df_asc) ascent = df_asc;
223 data.subMetrics(ascent, descent);
228 lyxerr << "Mathed Error: Unrecognized code[" << cx
234 data.GetIncPos(width, ls);
236 // No matter how simple is a matrix, it is NOT a subparagraph
238 if (cxp == LM_TC_TAB) {
239 if (ascent<df_asc) ascent = df_asc;
242 data.setTab(width - tb, tab);
246 data.subMetrics(ascent, descent);
251 MathSqrtInset::draw(Painter & pain, int x, int y)
253 MathParInset::draw(pain, x + hmax + 2, y);
256 int h2 = Height() / 2;
257 int w2 = (Height() > 4 * hmax) ? hmax : hmax / 2;
259 xp[0] = x + hmax + wbody; yp[0] = y - h;
260 xp[1] = x + hmax; yp[1] = y - h;
261 xp[2] = x + w2; yp[2] = y + d;
262 xp[3] = x; yp[3] = y + d - h2;
263 pain.lines(xp, yp, 4, LColor::mathline);
268 MathSqrtInset::Metrics()
270 MathParInset::Metrics();
274 hmax = mathed_char_height(LM_TC_VAR, size, 'I', a, b);
275 if (hmax < 10) hmax = 10;
282 MathFracInset::draw(Painter & pain, int x, int y)
288 if (size == LM_ST_DISPLAY) ++size;
289 MathParInset::draw(pain, x + (width - w0) / 2, y - des0);
290 den->draw(pain, x + (width - w1) / 2, y + den->Ascent() + 2 - dh);
292 if (objtype == LM_OT_FRAC)
293 pain.line(x + 2, y - dh, x + width - 4, y - dh, LColor::mathline);
299 MathFracInset::Metrics()
303 dh = mathed_char_height(LM_TC_CONST, size, 'I', a, b)/2;
308 if (size == LM_ST_DISPLAY) ++size;
309 MathParInset::Metrics();
312 int as = Height() + 2 + dh;
313 des0 = Descent() + 2 + dh;
316 width = ((w0 > w1) ? w0: w1) + 12;
318 descent = den->Height()+ 2 - dh;
324 MathBigopInset::draw(Painter & pain, int x, int y)
329 if (sym < 256 || sym == LM_oint) {
330 s += (sym == LM_oint) ? LM_int : sym;
336 if (sym == LM_oint) {
337 pain.arc(x, y - 5 * width / 4, width, width, 0, 360*64,
341 pain.text(x, y, s, mathed_get_font(t, size));
346 MathBigopInset::Metrics()
352 if (sym < 256 || sym == LM_oint) {
353 c = (sym == LM_oint) ? LM_int: sym;
360 mathed_string_height(t, size, s, ascent, descent);
361 width = mathed_string_width(t, size, s);
362 if (sym == LM_oint) width += 2;