3 * Purpose: Interaction for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Math 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 #pragma implementation
23 #include FORMS_H_LOCATION
24 #include "math_inset.h"
25 #include "math_parser.h"
26 #include "math_cursor.h"
27 #include "math_macro.h"
28 #include "math_macrotable.h"
29 #include "math_root.h"
30 #include "support/lstrings.h"
34 #include "math_matrixinset.h"
35 #include "math_rowst.h"
36 #include "math_spaceinset.h"
37 #include "math_funcinset.h"
38 #include "math_bigopinset.h"
39 #include "math_fracinset.h"
40 #include "math_decorationinset.h"
41 #include "math_dotsinset.h"
42 #include "math_accentinset.h"
43 #include "mathed/support.h"
45 static MathedArray selarray;
49 // This was very smaller, I'll change it later
51 bool IsMacro(short tok, int id)
53 return tok != LM_TK_STACK &&
59 tok != LM_TK_FUNCLIM &&
60 tok != LM_TK_BIGSYM &&
61 tok != LM_TK_ACCENT &&
62 !(tok == LM_TK_SYM && id < 255);
65 static int const MAX_STACK_ITEMS = 32;
67 struct MathStackXIter {
71 MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
72 item = new MathedXIter[imax];
76 MathStackXIter(MathStackXIter & stk);
82 void push(MathedXIter ** a) {
91 MathedXIter * Item(int idx) {
92 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
100 return i >= MAX_STACK_ITEMS;
107 int Level() { return i; }
109 } mathstk, *selstk = 0;
112 MathStackXIter::MathStackXIter(MathStackXIter & stk)
115 item = new MathedXIter[imax];
117 for (int k = 0; k < i; ++k) {
118 item[k].SetData(stk.item[k].getPar());
120 item[k].goPosAbs(stk.item[k].getPos());
125 /***---------------- Mathed Cursor ---------------------------***/
127 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
131 lastcode = LM_TC_MIN;
133 if (!MathMacroTable::built)
134 MathMacroTable::mathMTable.builtinMacros();
138 void MathedCursor::SetPar(MathParInset * p)
141 selection = false; // not SelClear() ?
143 mathstk.push(&cursor);
145 cursor->SetData(par);
149 void MathedCursor::draw(Painter & pain, int x, int y)
151 // lyxerr << "Cursor[" << x << " " << y << "] ";
152 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
155 int w = par->Width() + 2;
156 int a = par->Ascent() + 1;
157 int h = par->Height() + 1;
159 if (par->GetType() > LM_OT_PAR) {
164 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
166 par->draw(pain, x, y);
171 void MathedCursor::Redraw(Painter & pain)
173 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
175 int w = par->Width();
176 int h = par->Height();
180 //mathed_set_font(LM_TC_VAR, 1);
181 pain.fillRectangle(x, y - par->Ascent(),
182 x + w, y - par->Ascent() + h,
184 par->draw(pain, x, y);
188 bool MathedCursor::Left(bool sel)
191 // was MacroModeBack()
192 if (!imacro->GetName().empty()) {
193 imacro->SetName(imacro->GetName()
194 .substr(0, imacro->GetName()
204 if (sel && !selection)
207 if (!sel && selection)
210 bool result = cursor->Prev();
212 if (!result && !mathstk.Empty()) {
213 cursor = mathstk.pop();
218 } else if (result && cursor->IsActive()) {
219 if (cursor->IsScript()) {
221 if (!cursor->IsScript())
228 MathParInset * p = cursor->GetActiveInset();
232 p->setArgumentIdx(p->getMaxArgumentIdx());
233 mathstk.push(&cursor);
243 bool MathedCursor::Pop()
245 if (!mathstk.Empty()) {
246 cursor = mathstk.pop();
255 bool MathedCursor::Push()
257 if (cursor->IsActive()) {
258 MathParInset * p = cursor->GetActiveInset();
261 mathstk.push(&cursor);
269 bool MathedCursor::Right(bool sel)
278 if (sel && !selection)
281 if (!sel && selection)
286 if (cursor->IsActive()) {
287 if (cursor->IsScript()) {
289 // A script may be followed by another script
290 if (cursor->IsScript())
296 MathParInset *p = cursor->GetActiveInset();
298 lyxerr << "Math error: Inset expected." << endl;
299 return cursor->Next();
301 p->setArgumentIdx(0);
302 mathstk.push(&cursor);
306 result = cursor->Next();
309 if (cursor->GetChar()!= LM_TC_CR)
310 result = cursor->Next();
311 if (!result && !mathstk.Empty()) {
312 cursor = mathstk.pop();
324 void MathedCursor::SetPos(int x, int y)
331 lastcode = LM_TC_MIN;
333 mathstk.push(&cursor);
334 cursor->SetData(par);
335 cursor->fitCoord(x, y);
337 while (cursor->GetX()<x && cursor->OK()) {
338 if (cursor->IsActive()) {
339 MathParInset * p = cursor->GetActiveInset();
340 if (p->Inside(x, y)) {
342 mathstk.push(&cursor);
344 cursor->fitCoord(x, y);
350 if (!cursor->Next() && !Pop())
353 if (x-xp < cursor->GetX()-x) cursor->ipop();
358 void MathedCursor::Home()
364 mathstk.push(&cursor);
369 void MathedCursor::End()
375 mathstk.push(&cursor);
380 MathMatrixInset * create_multiline(short int type, int cols)
391 for (int i = 0; i < cols; ++i)
398 for (int i = 0; i < cols; ++i)
403 case LM_OT_MULTLINEN:
416 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
417 mt->SetAlign(' ', align);
422 void MathedCursor::Insert(byte c, MathedTextCodes t)
430 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
434 MathParInset * p = cursor->getPar();
435 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
436 short int type = LM_OT_MPAR;
438 if (c >= '1' && c <= '9') {
441 } else if (c >= 'A' && c <= 'I') {
442 type = LM_OT_ALIGNAT;
445 type = LM_OT_MULTLINE;
449 if (p->GetType() == LM_OT_PARN)
451 MathMatrixInset * mt = create_multiline(type, cols);
452 mt->SetStyle(LM_ST_DISPLAY);
454 mt->setData(p->GetData());
455 p->setData(0); // BUG duda
460 int pos = cursor->getPos();
461 cursor->SetData(par);
462 cursor->goPosAbs(pos);
464 if (p && p->Permit(LMPF_ALLOW_CR)) {
467 } else if (t == LM_TC_TAB) {
468 MathParInset * p = cursor->getPar();
469 if (p && p->Permit(LMPF_ALLOW_TAB)) {
471 cursor->insert(c, t);
474 cursor->goNextColumn();
475 } else // Navigate between arguments
476 if (p && p->GetType() == LM_OT_MACRO) {
477 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
478 p->setArgumentIdx(p->getArgumentIdx() + 1);
485 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
486 // was MacroModeInsert(c);
487 imacro->SetName(imacro->GetName() + static_cast<char>(c));
495 cursor->insert(c, t);
504 void MathedCursor::insertInset(MathedInset * p, int t)
510 if (MathIsActive(t)) {
512 static_cast<MathParInset*>(p)->setData(&selarray);
517 if (mathstk.i < MAX_STACK_ITEMS - 1) {
518 if (accent && !MathIsActive(t)) {
521 cursor->insertInset(p, t);
522 if (MathIsActive(t)) {
528 lyxerr << "Math error: Full stack." << endl;
532 void MathedCursor::Delete()
542 if (cursor->Empty() && !mathstk.Empty())
543 cursor = mathstk.pop();
545 // if (cursor->GetChar()!= LM_TC_TAB)
551 void MathedCursor::DelLine()
561 MathParInset * p = cursor->getPar();
563 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
569 bool MathedCursor::Up(bool sel)
576 if (sel && !selection)
579 if (!sel && selection)
582 if (cursor->IsScript()) {
583 char cd = cursor->GetChar();
588 // A subscript may be followed by a superscript
591 if (MathIsUp(cursor->GetChar())) {
594 } else // return to the previous state
599 result = cursor->Up();
600 if (!result && cursor->getPar()) {
601 MathParInset * p = cursor->getPar();
603 if (p->GetType() == LM_OT_SCRIPT) {
604 MathedXIter * cx = mathstk.Item(1);
605 bool is_down = (cx->GetChar() == LM_TC_DOWN);
606 cursor = mathstk.pop();
608 result = (is_down) ? true: Up();
610 result = (p->getArgumentIdx() > 0);
612 p->setArgumentIdx(p->getArgumentIdx() - 1);
617 if (!result && !mathstk.Empty()) {
618 cursor = mathstk.pop();
626 bool MathedCursor::Down(bool sel)
633 if (sel && !selection)
636 if (!sel && selection)
639 if (cursor->IsScript()) {
640 char cd = cursor->GetChar();
641 if (MathIsDown(cd)) {
645 // A superscript may be followed by a subscript
648 if (MathIsDown(cursor->GetChar())) {
656 result = cursor->Down();
657 if (!result && cursor->getPar()) {
658 MathParInset * p= cursor->getPar();
659 if (p->GetType() == LM_OT_SCRIPT) {
660 MathedXIter * cx = mathstk.Item(1);
661 bool is_up = (cx->GetChar() == LM_TC_UP);
662 cursor = mathstk.pop();
664 result = (is_up) ? true: Down();
666 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
668 p->setArgumentIdx(p->getArgumentIdx() + 1);
672 if (!result && !mathstk.Empty()) {
673 cursor = mathstk.pop();
681 bool MathedCursor::Limits()
683 if (cursor->IsInset()) {
684 MathedInset * p = cursor->GetInset();
685 bool ol = p->GetLimits();
687 return (ol!= p->GetLimits());
693 void MathedCursor::SetSize(short size)
695 MathParInset * p = cursor->getPar();
696 p->UserSetSize(size);
701 void MathedCursor::setLabel(string const & label)
703 // ugly hack and possible bug
704 if (!cursor->setLabel(label))
705 lyxerr << "MathErr: Bad place to set labels." << endl;
709 void MathedCursor::setNumbered()
712 MathedRowSt * crow = cursor->currentRow();
714 crow->setNumbered(!crow->isNumbered());
718 void MathedCursor::Interpret(string const & s)
721 latexkeys const * l = 0;
722 MathedTextCodes tcode = LM_TC_INSET;
724 if (s[0] == '^' || s[0] == '_') {
725 char c = cursor->GetChar();
726 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
731 // A script may be followed by a script
732 if (MathIsUp(c) || MathIsDown(c)) {
735 c = cursor->GetChar();
736 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
742 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
743 insertInset(p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
746 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
747 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
748 p = new MathSpaceInset(sp);
749 insertInset(p, LM_TC_INSET);
755 p = MathMacroTable::mathMTable.getMacro(s);
757 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
759 p = new MathRootInset;
760 tcode = LM_TC_ACTIVE_INSET;
762 p = new MathFuncInset(s, LM_OT_UNDEF);
764 tcode = static_cast<MathMacro*>(p)->getTCode();
765 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
768 MathedInsetTypes fractype = LM_OT_FRAC;
771 p = new MathBigopInset(l->name, l->id);
776 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
777 LM_TC_BOPS: LM_TC_SYMB);
779 p = new MathFuncInset(l->name);
784 fractype = LM_OT_STACKREL;
785 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
788 p = new MathFracInset(fractype);
789 tcode = LM_TC_ACTIVE_INSET;
793 p = new MathSqrtInset;
794 tcode = LM_TC_ACTIVE_INSET;
798 p = new MathDecorationInset(l->id);
799 tcode = LM_TC_ACTIVE_INSET;
803 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
807 p = new MathSpaceInset(l->id);
811 p = new MathDotsInset(l->name, l->id);
819 p = MathMacroTable::mathMTable.getMacro(s);
820 tcode = static_cast<MathMacro*>(p)->getTCode();
821 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
825 p = new MathFuncInset(l->name);
831 insertInset(p, tcode);
837 bool MathedCursor::pullArg()
839 if (cursor->IsActive()) {
840 MathParInset * p = cursor->GetActiveInset();
844 MathedArray * a = p->GetData();
858 void MathedCursor::MacroModeOpen()
861 imacro = new MathFuncInset("");
862 insertInset(imacro, LM_TC_INSET);
865 lyxerr << "Mathed Warning: Already in macro mode" << endl;
869 void MathedCursor::MacroModeClose()
873 latexkeys const * l = in_word_set(imacro->GetName());
874 if (!imacro->GetName().empty()
875 && (!l || (l && IsMacro(l->token, l->id))) &&
876 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
878 //imacro->SetName(macrobf);
879 // This guarantees that the string will be removed by destructor
880 imacro->SetType(LM_OT_UNDEF);
882 imacro->SetName(l->name);
885 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
887 static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
890 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
891 Interpret(imacro->GetName());
900 void MathedCursor::SelCopy()
903 int p1 = (cursor->getPos() < selpos) ? cursor->getPos() : selpos;
904 int p2 = (cursor->getPos() > selpos) ?
905 cursor->getPos() : selpos;
906 selarray = *(cursor->GetData());
907 selarray.shrink(p1, p2);
914 void MathedCursor::SelCut()
917 if (cursor->getPos() == selpos)
920 int p1 = (cursor->getPos() < selpos) ? cursor->getPos() : selpos;
921 int p2 = (cursor->getPos() > selpos) ? cursor->getPos() : selpos;
922 selarray = *(cursor->GetData());
923 selarray.shrink(p1, p2);
924 cursor->Clean(selpos);
931 void MathedCursor::SelDel()
933 // lyxerr << "Deleting sel "
935 if (cursor->getPos() == selpos)
937 cursor->Clean(selpos);
944 void MathedCursor::SelPaste()
946 // lyxerr << "paste " << selarray << " " << curor->pos;
950 if (!selarray.empty()) {
951 cursor->Merge(&selarray);
957 void MathedCursor::SelStart()
959 lyxerr[Debug::MATHED] << "Starting sel " << endl;
961 selpos = cursor->getPos();
962 selstk = new MathStackXIter(mathstk);
963 anchor = selstk->Item(-1);
964 anchor->SetData(cursor->getPar());
966 anchor->goPosAbs(selpos);
972 void MathedCursor::SelClear()
974 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
983 // Anchor position must be at the same level that stack.
984 void MathedCursor::SelBalance()
986 int d = mathstk.Level() - selstk->Level();
988 // If unbalanced, balance them
991 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level
992 // << " " << anchor->GetX() << " " << cursor->GetX() << "]";
993 anchor = selstk->pop();
994 if (anchor->GetX() >= cursor->GetX())
997 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
1000 d = mathstk.Level() - selstk->Level();
1003 // Once balanced the levels, check that they are at the same paragraph
1004 selpos = anchor->getPos();
1008 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
1010 static int xpoint[10];
1011 static int ypoint[10];
1022 // Balance anchor and cursor
1027 cursor->getPar()->GetXY(xo, yo);
1028 int w = cursor->getPar()->Width();
1031 cursor->GetPos(x1, y1);
1034 cursor->getAD(a1, d1);
1037 anchor->GetPos(x, y);
1040 anchor->getAD(a, d);
1042 // single row selection
1045 ypoint[i++] = y + d;
1047 ypoint[i++] = y - a;
1051 ypoint[i++] = y - a;
1055 ypoint[i++] = y1 - a;
1060 ypoint[i++] = y1 - a;
1062 ypoint[i++] = y1 + d;
1066 ypoint[i++] = y1 + d;
1069 ypoint[i++] = y + d;
1072 xpoint[i] = xpoint[0];
1073 ypoint[i++] = ypoint[0];
1078 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1079 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1080 // for (i = 0; i < np; ++i)
1081 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1085 void MathedCursor::setAccent(int ac)
1087 if (ac > 0 && accent < 8) {
1088 nestaccent[accent++] = ac;
1090 accent = 0; // consumed!
1094 int MathedCursor::getAccent() const
1096 return (accent > 0) ? nestaccent[accent - 1]: 0;
1100 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1102 MathedInset * ac = 0;
1104 for (int i = accent - 1; i >= 0; --i) {
1105 if (i == accent - 1)
1106 ac = new MathAccentInset(c, t, nestaccent[i]);
1108 ac = new MathAccentInset(ac, nestaccent[i]);
1112 cursor->insertInset(ac, LM_TC_INSET);
1114 accent = 0; // consumed!
1118 void MathedCursor::doAccent(MathedInset * p)
1120 MathedInset * ac = 0;
1122 for (int i = accent - 1; i >= 0; --i) {
1123 if (i == accent - 1)
1124 ac = new MathAccentInset(p, nestaccent[i]);
1126 ac = new MathAccentInset(ac, nestaccent[i]);
1130 cursor->insertInset(ac, LM_TC_INSET);
1132 accent = 0; // consumed!
1136 void MathedCursor::toggleLastCode(MathedTextCodes t)
1139 lastcode = LM_TC_VAR;
1145 void MathedCursor::GetPos(int & x, int & y)
1147 cursor->GetPos(x, y);
1151 short MathedCursor::GetFCode()
1153 return cursor->fcode();
1157 MathParInset * MathedCursor::GetPar()
1163 MathParInset * MathedCursor::getCurrentPar() const
1165 return cursor->getPar();
1169 string const & MathedCursor::getLabel() const
1171 return cursor->getLabel();
1175 bool MathedCursor::IsEnd() const
1177 return !cursor->OK();
1181 bool MathedCursor::InMacroMode()
1187 bool MathedCursor::Selection()
1193 void MathedCursor::clearLastCode()
1195 lastcode = LM_TC_MIN;
1199 void MathedCursor::setLastCode(MathedTextCodes t)
1205 MathedTextCodes MathedCursor::getLastCode() const