3 * Purpose: Implementation of insets for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
8 * Dependencies: Xlib, XForms
10 * Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
21 #pragma implementation "math_inset.h"
24 #include "math_iter.h"
25 #include "math_inset.h"
26 #include "symbol_def.h"
30 char *strnew(char const* s)
32 char *s1 = new char[strlen(s)+1];
37 MathedInset::MathedInset(MathedInset* inset)
40 name = inset->GetName();
41 objtype = inset->GetType();
42 size = inset->GetStyle();
43 width = inset->Width();
44 ascent = inset->Ascent();
45 descent = inset->Descent();
47 objtype = LM_OT_UNDEF;
49 width = ascent = descent = 0;
55 MathFuncInset::MathFuncInset(char const *nm, short ot, short st):
56 MathedInset("", ot, st)
59 lims = (GetType() == LM_OT_FUNCLIM);
60 if (GetType() == LM_OT_UNDEF) {
69 MathedInset *MathFuncInset::Clone()
71 MathedInset *l = new MathFuncInset(name, GetType(), GetStyle());
75 MathSpaceInset::MathSpaceInset(int sp, short ot, short st):
76 MathedInset("", ot, st), space(sp)
80 MathedInset *MathSpaceInset::Clone()
82 MathedInset *l = new MathSpaceInset(space, GetType(), GetStyle());
86 MathParInset::MathParInset(short st, char const *nm, short ot):
87 MathedInset(nm, ot, st)
94 if (objtype == LM_OT_SCRIPT)
98 MathParInset::MathParInset(MathParInset* p): MathedInset(p)
101 p->setArgumentIdx(0);
102 MathedIter it(p->GetData());
107 MathParInset::~MathParInset()
110 MathedIter it(array);
117 MathedInset *MathParInset::Clone()
119 MathParInset* p = new MathParInset(this);
124 void MathParInset::SetData(LyxArrayBase *a)
128 // A standard paragraph shouldn't have any tabs nor CRs.
130 MathedIter it(array);
132 char c = it.GetChar();
133 if (c == LM_TC_TAB || c == LM_TC_CR)
142 MathSqrtInset::MathSqrtInset(short st): MathParInset(st, "sqrt", LM_OT_SQRT)
147 MathedInset *MathSqrtInset::Clone()
149 MathSqrtInset* p = new MathSqrtInset(GetStyle());
150 MathedIter it(array);
151 p->SetData(it.Copy());
156 bool MathSqrtInset::Inside(int x, int y)
158 return (x>= xo-hmax && x<= xo+width-hmax && y<= yo+descent && y>= yo-ascent);
162 MathDelimInset::MathDelimInset(int l, int r, short st):
163 MathParInset(st, "", LM_OT_DELIM), left(l), right(r)
167 MathedInset * MathDelimInset::Clone()
169 MathDelimInset * p = new MathDelimInset(left, right, GetStyle());
170 MathedIter it(array);
171 p->SetData(it.Copy());
176 MathDecorationInset::MathDecorationInset(int d, short st):
177 MathParInset(st, "", LM_OT_DECO), deco(d)
179 upper = (deco!= LM_underline && deco!= LM_underbrace);
182 MathedInset * MathDecorationInset::Clone()
184 MathDecorationInset * p = new MathDecorationInset(deco, GetStyle());
185 MathedIter it(array);
186 p->SetData(it.Copy());
190 MathFracInset::MathFracInset(short ot): MathParInset(LM_ST_TEXT, "frac", ot)
193 den = new MathParInset(LM_ST_TEXT); // this leaks
196 if (objtype == LM_OT_STACKREL) {
202 MathFracInset::~MathFracInset()
207 MathedInset * MathFracInset::Clone()
209 MathFracInset * p = new MathFracInset(GetType());
210 MathedIter itn(array);
211 MathedIter itd(den->GetData());
212 p->SetData(itn.Copy(), itd.Copy());
218 bool MathFracInset::setArgumentIdx(int i)
220 if (i == 0 || i == 1) {
228 void MathFracInset::SetStyle(short st)
230 MathParInset::SetStyle(st);
232 den->SetStyle((size == LM_ST_DISPLAY) ? (short)LM_ST_TEXT: size);
235 void MathFracInset::SetData(LyxArrayBase *n, LyxArrayBase *d)
238 MathParInset::SetData(n);
241 void MathFracInset::SetData(LyxArrayBase *d)
244 MathParInset::SetData(d);
250 void MathFracInset::GetXY(int& x, int& y) const
253 MathParInset::GetXY(x, y);
258 LyxArrayBase *MathFracInset::GetData()
263 return den->GetData();
267 bool MathFracInset::Inside(int x, int y)
269 int xx = xo - (width-w0)/2;
271 return (x>= xx && x<= xx+width && y<= yo+descent && y>= yo-ascent);
274 void MathFracInset::SetFocus(int /*x*/, int y)
276 // lyxerr << "y " << y << " " << yo << " " << den->yo << " ";
277 idx = (y > yo) ? 1: 0;
281 MathMatrixInset::MathMatrixInset(int m, int n, short st):
282 MathParInset(st, "array", LM_OT_MATRIX), nc(m)
286 h_align = new char[nc+1];
287 for (int i = 0; i < nc; i++) h_align[i] = 'c';
293 row = new MathedRowSt(nc+1);
294 MathedXIter it(this);
295 for (int j = 1; j < n; j++) it.addRow();
297 if (nr == 1 && nc>1) {
298 for (int j = 0; j < nc - 1; j++)
299 it.Insert('T', LM_TC_TAB);
302 row = new MathedRowSt(nc+1);
308 MathMatrixInset::MathMatrixInset(MathMatrixInset *mt):
309 MathParInset(mt->GetStyle(), mt->GetName(), mt->GetType())
314 h_align = new char[nc+1];
315 strcpy(h_align, mt->GetAlign(&v_align));
317 it.SetData(mt->GetData());
320 MathedRowSt *r, *ro= 0, *mrow = mt->row;
323 r = new MathedRowSt(nc+1);
324 r->numbered = mrow->numbered;
326 r->label = strnew(mrow->label);
341 MathMatrixInset::~MathMatrixInset()
345 MathedRowSt *r = row;
347 MathedRowSt *q = r->next;
353 MathedInset *MathMatrixInset::Clone()
355 MathMatrixInset* mt = new MathMatrixInset(this);
359 void MathMatrixInset::SetAlign(char vv, char const* hh)
362 strncpy(h_align, hh, nc);
366 // Check the number of tabs and crs
367 void MathMatrixInset::SetData(LyxArrayBase *a)
373 // count tabs per row
386 it.Insert(' ', LM_TC_TAB);
396 // Automatically inserts tabs around bops
397 // DISABLED because it's very easy to insert tabs
402 void MathMatrixInset::Draw(int x, int baseline)
404 MathParInset::Draw(x, baseline);
407 void MathMatrixInset::Metrics()
409 int i, /*cy,*/ hl, h= 0;
410 MathedRowSt *cprow= 0, *cxrow;
413 // lyxerr << " MIDA ";
414 MathedXIter it(this);
415 row = it.adjustVerticalSt();
421 for (i= 0; i<= nc; i++) cxrow->w[i] = 0;
426 MathParInset::Metrics();
428 if (nc<= 1 && !row->next) {
433 // Vertical positions of each row
436 for (i= 0; i<nc; i++) {
437 if (cxrow == row || ws[i]<cxrow->w[i]) ws[i]= cxrow->w[i];
438 if (cxrow->next == 0 && ws[i] == 0) ws[i] = df_width;
441 cxrow->y = (cxrow == row) ? cxrow->asc:
442 cxrow->asc + cprow->desc + MATH_ROWSEP + cprow->y;
443 h += cxrow->asc + cxrow->desc + MATH_ROWSEP;
451 // Compute vertical align
453 case 't': ascent = row->y; break;
454 case 'b': ascent = h - hl; break;
455 default: ascent = (row->next) ? h/2: h - hl; break;
457 descent = h - ascent + 2;
464 int rg= MATH_COLSEP, ww, lf= 0, *w = cxrow->w;
465 for (i= 0; i<nc; i++) {
471 switch (h_align[i]) {
472 case 'l': lf = 0; break;
473 case 'c': lf = (ws[i] - w[i])/2;
475 case 'r': lf = ws[i] - w[i]; break;
477 ww = (isvoid) ? lf: lf + w[i];
479 rg = ws[i] - ww + MATH_COLSEP;
480 if (cxrow == row) width += ws[i] + MATH_COLSEP;
487 MathAccentInset::MathAccentInset(byte cx, MathedTextCodes f, int cd, short st):
488 MathedInset("", LM_OT_ACCENT, st), c(cx), fn(f), code(cd)
493 MathAccentInset::MathAccentInset(MathedInset *ins, int cd, short st):
494 MathedInset("", LM_OT_ACCENT, st), c(0), fn(LM_TC_MIN), code(cd), inset(ins)
499 MathAccentInset::~MathAccentInset()
505 MathedInset *MathAccentInset::Clone()
510 p = new MathAccentInset(inset->Clone(), code, GetStyle());
512 p = new MathAccentInset(c, fn, code, GetStyle());
518 MathBigopInset::MathBigopInset(char const* nam, int id, short st):
519 MathedInset(nam, LM_OT_BIGOP, st), sym(id)
524 MathedInset *MathBigopInset::Clone()
526 MathBigopInset* p = new MathBigopInset(name, sym, GetStyle());
530 MathDotsInset::MathDotsInset(char const* nam, int id, short st):
531 MathedInset(nam, LM_OT_DOTS, st), code(id)
535 MathedInset *MathDotsInset::Clone()
537 MathDotsInset* p = new MathDotsInset(name, code, GetStyle());