- char *s1 = new char[strlen(s)+1];
- strcpy(s1, s);
- return s1;
-}
-
-
-
-#define MAX_STACK_ITEMS 32
-
-struct MathStackXIter {
-
- int i, imax;
- MathedXIter *item;
-
- MathStackXIter(int n=MAX_STACK_ITEMS): imax(n) {
- item = new MathedXIter[imax];
- i = 0;
- }
-
- MathStackXIter(MathStackXIter &stk);
-
- ~MathStackXIter() {
- delete[] item;
- }
-
- void push(MathedXIter** a) {
- *a = &item[i++];
- }
-
- MathedXIter* pop() {
- i--;
- return &item[i-1];
- }
-
- MathedXIter* Item(int idx) {
- return (idx+1 <= i) ? &item[i-idx-1]: (MathedXIter*)0;
- }
-
- void Reset() {
- i = 0;
- }
-
- int Full() {
- return (i>=MAX_STACK_ITEMS);
- }
-
- int Empty() {
- return (i<=1);
- }
-
- int Level() { return i; }
-
-} mathstk, *selstk=0;
-
-
-MathStackXIter::MathStackXIter(MathStackXIter &stk) {
- imax = stk.imax;
- item = new MathedXIter[imax];
- i = stk.i;
- for (int k=0; k<i; k++) {
- item[k].SetData(stk.item[k].getPar());
- item[k].GoBegin();
- item[k].goPosAbs(stk.item[k].getPos());
- }
-}
-
-
-/***---------------- Mathed Cursor ---------------------------***/
-
-MathedCursor::MathedCursor(MathParInset *p) // : par(p)
-{
- accent = 0;
- anchor = 0;
- lastcode = LM_TC_MIN;
- SetPar(p);
-// selarray = 0;
- if (!MathMacroTable::built)
- MathMacroTable::mathMTable.builtinMacros();
-}
-
-
-void MathedCursor::SetPar(MathParInset *p)
-{
- win = 0;
- is_visible = False;
- macro_mode = false;
- selection = false; // not SelClear() ?
- mathstk.Reset();
- mathstk.push(&cursor);
- par = p;
- cursor->SetData(par);
-}
-
-void MathedCursor::Draw(long unsigned pm, int x, int y)
-{
-// lyxerr << "Cursor[" << x << " " << y << "] ";
- win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
- par->Metrics();
- int w = par->Width()+2, a = par->Ascent()+1, h = par->Height()+1;
- if (par->GetType()>LM_OT_PAR) { a += 4; h += 8; }
-
- if (!canvasGC) mathed_set_font(LM_TC_VAR, 1);
- // XFillRectangle(fl_display,pm, canvasGC, x, y-a, w, h);
- XDrawRectangle(fl_display,pm, mathFrameGC, x-1, y-a, w, h);
- XFlush(fl_display);
- MathParInset::pm = pm;
- par->Draw(x, y);
- cursor->Adjust();
-}
-
-
-void MathedCursor::Redraw()
-{
- lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
- par->Metrics();
- int w = par->Width(), h = par->Height();
- int x, y;
- par->GetXY(x, y);
- mathed_set_font(LM_TC_VAR, 1);
- XFillRectangle(fl_display, win,canvasGC,x, y-par->Ascent(), w, h);
- XFlush(fl_display);
- MathParInset::pm = win;
- par->Draw(x, y);
-}
-
-bool MathedCursor::Left(bool sel)
-{
- if (macro_mode) {
- MacroModeBack();
- return true;
- }
- clearLastCode();
- if (sel && !selection) SelStart();
- if (!sel && selection) SelClear();
- bool result = cursor->Prev();
- if (!result && !mathstk.Empty()) {
- cursor = mathstk.pop();
- cursor->Adjust();
- result = true;
- if (selection) SelClear();
- } else
- if (result && cursor->IsActive()) {
- if (cursor->IsScript()) {
- cursor->Prev();
- if (!cursor->IsScript())
- cursor->Next();
- cursor->Adjust();
- return true;
- }
- if (!selection) {
- MathParInset *p = cursor->GetActiveInset();
- if (!p)
- return result;
-
- p->setArgumentIdx(p->getMaxArgumentIdx());
- mathstk.push(&cursor);
- cursor->SetData(p);
- cursor->GoLast();
- }
- }
- return result;
-}
-
-// Leave the inset
-bool MathedCursor::Pop()
-{
- if (!mathstk.Empty()) {
- cursor = mathstk.pop();
- cursor->Next();
- return true;
- }
- return false;
-}
-
-// Go to the inset
-bool MathedCursor::Push()
-{
- if (cursor->IsActive()) {
- MathParInset *p = cursor->GetActiveInset();
- if (!p) return false;
- mathstk.push(&cursor);
- cursor->SetData(p);
- return true;
- }
- return false;
-}
-
-bool MathedCursor::Right(bool sel)
-{
- if (macro_mode) {
- MacroModeClose();
- return true;
- }
- clearLastCode();
- if (sel && !selection) SelStart();
- if (!sel && selection) SelClear();
- bool result = false;
-
- if (cursor->IsActive()) {
- if (cursor->IsScript()) {
- cursor->Next();
- // A script may be followed by another script
- if (cursor->IsScript())
- cursor->Next();
- return true;
- }
- if (!selection) {
- MathParInset *p = cursor->GetActiveInset();
- if (!p) {
- lyxerr << "Math error: Inset expected." << endl;
- return cursor->Next();
- }
- p->setArgumentIdx(0);
- mathstk.push(&cursor);
- cursor->SetData(p);
- result = true;
- } else
- result = cursor->Next();
- } else {
- if (cursor->GetChar()!=LM_TC_CR)
- result = cursor->Next();
- if (!result && !mathstk.Empty()) {
- cursor = mathstk.pop();
- cursor->Next();
- cursor->Adjust();
- result = true;
- if (selection) SelClear();
- }
- }
- return result;
-}
-
-
-void MathedCursor::SetPos(int x, int y)
-{
- int xp = 0;
-
- if (macro_mode) MacroModeClose();
- lastcode = LM_TC_MIN;
- mathstk.Reset();
- mathstk.push(&cursor);
- cursor->SetData(par);
- cursor->fitCoord(x, y);
- while (cursor->GetX()<x && cursor->OK()) {
- if (cursor->IsActive()) {
- MathParInset *p = cursor->GetActiveInset();
- if (p->Inside(x, y)) {
- p->SetFocus(x, y);
- mathstk.push(&cursor);
- cursor->SetData(p);
- cursor->fitCoord(x, y);
- continue;
- }
- }
- xp = cursor->GetX();
- cursor->ipush();
- if (!cursor->Next() && !Pop())
- break;
- }
- if (x-xp < cursor->GetX()-x) cursor->ipop();
- cursor->Adjust();
-}
-
-
-void MathedCursor::Home()
-{
- if (macro_mode) MacroModeClose();
- clearLastCode();
- mathstk.Reset();
- mathstk.push(&cursor);
- cursor->GoBegin();
-}
-
-void MathedCursor::End()
-{
- if (macro_mode) MacroModeClose();
- clearLastCode();
- mathstk.Reset();
- mathstk.push(&cursor);
- cursor->GoLast();
-}
-
-void MathedCursor::Insert(byte c, MathedTextCodes t)
-{
- if (selection) SelDel();
-
- if (t==LM_TC_MIN)
- t = lastcode;
-
- if (macro_mode && !(MathIsAlphaFont(t) || t==LM_TC_MIN))
- MacroModeClose();
-
- if (t==LM_TC_CR) {
- MathParInset *p= cursor->p;
- if (p==par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
- MathMatrixInset* mt = new MathMatrixInset(3, 0);
- mt->SetAlign(' ', "rcl");
- mt->SetStyle(LM_ST_DISPLAY);
- mt->SetType((p->GetType()==LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
- mt->SetData(p->GetData());
- p->SetData(0);//BUG duda
- delete p;
- par = mt;
- p = mt;
- p->Metrics();
- int pos = cursor->getPos();
- cursor->SetData(par);
- cursor->goPosAbs(pos);
- }
- if (p && p->Permit(LMPF_ALLOW_CR)) {
- cursor->addRow();
- }
- } else
- if (t==LM_TC_TAB) {
- MathParInset *p = cursor->p;
- if (p && p->Permit(LMPF_ALLOW_TAB)) {
- if (c) {
- cursor->Insert(c, t);
- cursor->checkTabs();
- } else
- cursor->goNextColumn();
- } else // Navigate between arguments
- if (p && p->GetType()==LM_OT_MACRO) {
- if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
- p->setArgumentIdx(p->getArgumentIdx()+1);
- cursor->SetData(p);
- return;
- }
- }
- } else {
- if (macro_mode) {
- if (MathIsAlphaFont(t) || t==LM_TC_MIN) {
- MacroModeInsert(c);
- return;
- }
- }
- if (accent) {
- doAccent(c, t);
- } else
- cursor->Insert(c, t);
- lastcode = t;
-
- return;
- }
- clearLastCode();
-}
-
-void MathedCursor::Insert(MathedInset* p, int t)
-{
- if (macro_mode) MacroModeClose();
- if (selection) {
- if (MathIsActive(t)) {
- SelCut();
- ((MathParInset*)p)->SetData(selarray);
- } else
- SelDel();
- }
-
- if (mathstk.i<MAX_STACK_ITEMS-1) {
-
- if (accent && !MathIsActive(t)) {
- doAccent(p);
- } else {
- cursor->Insert(p, t);
-
- if (MathIsActive(t)) {
- cursor->Prev();
- Push();
- }
- }
-
- } else
- lyxerr << "Math error: Full stack." << endl;
-}
-
-void MathedCursor::Delete()
-{
- if (macro_mode) return;
- if (selection) {
- SelDel();
- return;
- }
- if (cursor->Empty() && !mathstk.Empty()) {
- cursor = mathstk.pop();
- }
-// if (cursor->GetChar()!=LM_TC_TAB)
- cursor->Delete();
- cursor->checkTabs();
-}
-
-void MathedCursor::DelLine()
-{
- if (macro_mode) MacroModeClose();
- if (selection) {
- SelDel();