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 = 0;
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;
134 if (!MathMacroTable::built)
135 MathMacroTable::mathMTable.builtinMacros();
139 void MathedCursor::SetPar(MathParInset * p)
142 selection = false; // not SelClear() ?
144 mathstk.push(&cursor);
146 cursor->SetData(par);
150 void MathedCursor::draw(Painter & pain, int x, int y)
152 // lyxerr << "Cursor[" << x << " " << y << "] ";
153 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
156 int w = par->Width() + 2;
157 int a = par->Ascent() + 1;
158 int h = par->Height() + 1;
160 if (par->GetType() > LM_OT_PAR) {
165 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
167 par->draw(pain, x, y);
172 void MathedCursor::Redraw(Painter & pain)
174 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
176 int w = par->Width();
177 int h = par->Height();
181 //mathed_set_font(LM_TC_VAR, 1);
182 pain.fillRectangle(x, y - par->Ascent(),
183 x + w, y - par->Ascent() + h,
185 par->draw(pain, x, y);
189 bool MathedCursor::Left(bool sel)
192 // was MacroModeBack()
193 if (!imacro->GetName().empty()) {
194 imacro->SetName(imacro->GetName()
195 .substr(0, imacro->GetName()
205 if (sel && !selection)
208 if (!sel && selection)
211 bool result = cursor->Prev();
213 if (!result && !mathstk.Empty()) {
214 cursor = mathstk.pop();
219 } else if (result && cursor->IsActive()) {
220 if (cursor->IsScript()) {
222 if (!cursor->IsScript())
229 MathParInset * p = cursor->GetActiveInset();
233 p->setArgumentIdx(p->getMaxArgumentIdx());
234 mathstk.push(&cursor);
244 bool MathedCursor::Pop()
246 if (!mathstk.Empty()) {
247 cursor = mathstk.pop();
256 bool MathedCursor::Push()
258 if (cursor->IsActive()) {
259 MathParInset * p = cursor->GetActiveInset();
262 mathstk.push(&cursor);
270 bool MathedCursor::Right(bool sel)
279 if (sel && !selection)
282 if (!sel && selection)
287 if (cursor->IsActive()) {
288 if (cursor->IsScript()) {
290 // A script may be followed by another script
291 if (cursor->IsScript())
297 MathParInset *p = cursor->GetActiveInset();
299 lyxerr << "Math error: Inset expected." << endl;
300 return cursor->Next();
302 p->setArgumentIdx(0);
303 mathstk.push(&cursor);
307 result = cursor->Next();
310 if (cursor->GetChar()!= LM_TC_CR)
311 result = cursor->Next();
312 if (!result && !mathstk.Empty()) {
313 cursor = mathstk.pop();
325 void MathedCursor::SetPos(int x, int y)
332 lastcode = LM_TC_MIN;
334 mathstk.push(&cursor);
335 cursor->SetData(par);
336 cursor->fitCoord(x, y);
338 while (cursor->GetX()<x && cursor->OK()) {
339 if (cursor->IsActive()) {
340 MathParInset * p = cursor->GetActiveInset();
341 if (p->Inside(x, y)) {
343 mathstk.push(&cursor);
345 cursor->fitCoord(x, y);
351 if (!cursor->Next() && !Pop())
354 if (x-xp < cursor->GetX()-x) cursor->ipop();
359 void MathedCursor::Home()
365 mathstk.push(&cursor);
370 void MathedCursor::End()
376 mathstk.push(&cursor);
381 MathMatrixInset * create_multiline(short int type, int cols)
392 for (int i = 0; i < cols; ++i)
399 for (int i = 0; i < cols; ++i)
404 case LM_OT_MULTLINEN:
417 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
418 mt->SetAlign(' ', align);
423 void MathedCursor::Insert(byte c, MathedTextCodes t)
431 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
435 MathParInset * p = cursor->getPar();
436 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
437 short int type = LM_OT_MPAR;
439 if (c >= '1' && c <= '9') {
442 } else if (c >= 'A' && c <= 'I') {
443 type = LM_OT_ALIGNAT;
446 type = LM_OT_MULTLINE;
450 if (p->GetType() == LM_OT_PARN)
452 MathMatrixInset * mt = create_multiline(type, cols);
453 mt->SetStyle(LM_ST_DISPLAY);
455 mt->setData(p->GetData());
456 p->setData(0); // BUG duda
461 int pos = cursor->getPos();
462 cursor->SetData(par);
463 cursor->goPosAbs(pos);
465 if (p && p->Permit(LMPF_ALLOW_CR)) {
468 } else if (t == LM_TC_TAB) {
469 MathParInset * p = cursor->getPar();
470 if (p && p->Permit(LMPF_ALLOW_TAB)) {
472 cursor->insert(c, t);
475 cursor->goNextColumn();
476 } else // Navigate between arguments
477 if (p && p->GetType() == LM_OT_MACRO) {
478 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
479 p->setArgumentIdx(p->getArgumentIdx() + 1);
486 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
487 // was MacroModeInsert(c);
488 imacro->SetName(imacro->GetName() + static_cast<char>(c));
496 cursor->insert(c, t);
505 void MathedCursor::insertInset(MathedInset * p, int t)
511 if (MathIsActive(t)) {
513 static_cast<MathParInset*>(p)->setData(selarray);
518 if (mathstk.i < MAX_STACK_ITEMS - 1) {
519 if (accent && !MathIsActive(t)) {
522 cursor->insertInset(p, t);
523 if (MathIsActive(t)) {
529 lyxerr << "Math error: Full stack." << endl;
533 void MathedCursor::Delete()
543 if (cursor->Empty() && !mathstk.Empty())
544 cursor = mathstk.pop();
546 // if (cursor->GetChar()!= LM_TC_TAB)
552 void MathedCursor::DelLine()
562 MathParInset * p = cursor->getPar();
564 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
570 bool MathedCursor::Up(bool sel)
577 if (sel && !selection)
580 if (!sel && selection)
583 if (cursor->IsScript()) {
584 char cd = cursor->GetChar();
589 // A subscript may be followed by a superscript
592 if (MathIsUp(cursor->GetChar())) {
595 } else // return to the previous state
600 result = cursor->Up();
601 if (!result && cursor->getPar()) {
602 MathParInset * p = cursor->getPar();
604 if (p->GetType() == LM_OT_SCRIPT) {
605 MathedXIter * cx = mathstk.Item(1);
606 bool is_down = (cx->GetChar() == LM_TC_DOWN);
607 cursor = mathstk.pop();
609 result = (is_down) ? true: Up();
611 result = (p->getArgumentIdx() > 0);
613 p->setArgumentIdx(p->getArgumentIdx() - 1);
618 if (!result && !mathstk.Empty()) {
619 cursor = mathstk.pop();
627 bool MathedCursor::Down(bool sel)
634 if (sel && !selection)
637 if (!sel && selection)
640 if (cursor->IsScript()) {
641 char cd = cursor->GetChar();
642 if (MathIsDown(cd)) {
646 // A superscript may be followed by a subscript
649 if (MathIsDown(cursor->GetChar())) {
657 result = cursor->Down();
658 if (!result && cursor->getPar()) {
659 MathParInset * p= cursor->getPar();
660 if (p->GetType() == LM_OT_SCRIPT) {
661 MathedXIter * cx = mathstk.Item(1);
662 bool is_up = (cx->GetChar() == LM_TC_UP);
663 cursor = mathstk.pop();
665 result = (is_up) ? true: Down();
667 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
669 p->setArgumentIdx(p->getArgumentIdx() + 1);
673 if (!result && !mathstk.Empty()) {
674 cursor = mathstk.pop();
682 bool MathedCursor::Limits()
684 if (cursor->IsInset()) {
685 MathedInset * p = cursor->GetInset();
686 bool ol = p->GetLimits();
688 return (ol!= p->GetLimits());
694 void MathedCursor::SetSize(short size)
696 MathParInset * p = cursor->getPar();
697 p->UserSetSize(size);
702 void MathedCursor::setLabel(string const & label)
704 // ugly hack and possible bug
705 if (!cursor->setLabel(label))
706 lyxerr << "MathErr: Bad place to set labels." << endl;
710 void MathedCursor::setNumbered()
713 MathedRowSt * crow = cursor->currentRow();
715 crow->setNumbered(!crow->isNumbered());
719 void MathedCursor::Interpret(string const & s)
722 latexkeys const * l = 0;
723 MathedTextCodes tcode = LM_TC_INSET;
725 if (s[0] == '^' || s[0] == '_') {
726 char c = cursor->GetChar();
727 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
732 // A script may be followed by a script
733 if (MathIsUp(c) || MathIsDown(c)) {
736 c = cursor->GetChar();
737 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
743 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
744 insertInset(p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
747 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
748 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
749 p = new MathSpaceInset(sp);
750 insertInset(p, LM_TC_INSET);
756 p = MathMacroTable::mathMTable.getMacro(s);
758 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
760 p = new MathRootInset;
761 tcode = LM_TC_ACTIVE_INSET;
763 p = new MathFuncInset(s, LM_OT_UNDEF);
765 tcode = static_cast<MathMacro*>(p)->getTCode();
766 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
769 MathedInsetTypes fractype = LM_OT_FRAC;
772 p = new MathBigopInset(l->name, l->id);
777 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
778 LM_TC_BOPS: LM_TC_SYMB);
780 p = new MathFuncInset(l->name);
785 fractype = LM_OT_STACKREL;
786 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
789 p = new MathFracInset(fractype);
790 tcode = LM_TC_ACTIVE_INSET;
794 p = new MathSqrtInset;
795 tcode = LM_TC_ACTIVE_INSET;
799 p = new MathDecorationInset(l->id);
800 tcode = LM_TC_ACTIVE_INSET;
804 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
808 p = new MathSpaceInset(l->id);
812 p = new MathDotsInset(l->name, l->id);
820 p = MathMacroTable::mathMTable.getMacro(s);
821 tcode = static_cast<MathMacro*>(p)->getTCode();
822 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
826 p = new MathFuncInset(l->name);
832 insertInset(p, tcode);
838 bool MathedCursor::pullArg()
840 if (cursor->IsActive()) {
841 MathParInset * p = cursor->GetActiveInset();
845 MathedArray * a = p->GetData();
859 void MathedCursor::MacroModeOpen()
862 imacro = new MathFuncInset("");
863 insertInset(imacro, LM_TC_INSET);
866 lyxerr << "Mathed Warning: Already in macro mode" << endl;
870 void MathedCursor::MacroModeClose()
874 latexkeys const * l = in_word_set(imacro->GetName());
875 if (!imacro->GetName().empty()
876 && (!l || (l && IsMacro(l->token, l->id))) &&
877 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
879 //imacro->SetName(macrobf);
880 // This guarantees that the string will be removed by destructor
881 imacro->SetType(LM_OT_UNDEF);
883 imacro->SetName(l->name);
886 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
888 static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
891 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
892 Interpret(imacro->GetName());
901 void MathedCursor::SelCopy()
904 int p1 = (cursor->getPos() < selpos) ? cursor->getPos() : selpos;
905 int p2 = (cursor->getPos() > selpos) ?
906 cursor->getPos() : selpos;
907 selarray = cursor->Copy(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->Copy(p1, p2);
923 cursor->Clean(selpos);
930 void MathedCursor::SelDel()
932 // lyxerr << "Deleting sel "
934 if (cursor->getPos() == selpos)
936 cursor->Clean(selpos);
943 void MathedCursor::SelPaste()
945 // lyxerr << "paste " << selarray << " " << curor->pos;
950 cursor->Merge(selarray);
956 void MathedCursor::SelStart()
958 lyxerr[Debug::MATHED] << "Starting sel " << endl;
960 selpos = cursor->getPos();
961 selstk = new MathStackXIter(mathstk);
962 anchor = selstk->Item(-1);
963 anchor->SetData(cursor->getPar());
965 anchor->goPosAbs(selpos);
971 void MathedCursor::SelClear()
973 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
982 // Anchor position must be at the same level that stack.
983 void MathedCursor::SelBalance()
985 int d = mathstk.Level() - selstk->Level();
987 // If unbalanced, balance them
990 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level
991 // << " " << anchor->GetX() << " " << cursor->GetX() << "]";
992 anchor = selstk->pop();
993 if (anchor->GetX() >= cursor->GetX())
996 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
999 d = mathstk.Level() - selstk->Level();
1002 // Once balanced the levels, check that they are at the same paragraph
1003 selpos = anchor->getPos();
1007 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
1009 static int xpoint[10];
1010 static int ypoint[10];
1021 // Balance anchor and cursor
1026 cursor->getPar()->GetXY(xo, yo);
1027 int w = cursor->getPar()->Width();
1030 cursor->GetPos(x1, y1);
1033 cursor->getAD(a1, d1);
1036 anchor->GetPos(x, y);
1039 anchor->getAD(a, d);
1041 // single row selection
1044 ypoint[i++] = y + d;
1046 ypoint[i++] = y - a;
1050 ypoint[i++] = y - a;
1054 ypoint[i++] = y1 - a;
1059 ypoint[i++] = y1 - a;
1061 ypoint[i++] = y1 + d;
1065 ypoint[i++] = y1 + d;
1068 ypoint[i++] = y + d;
1071 xpoint[i] = xpoint[0];
1072 ypoint[i++] = ypoint[0];
1077 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1078 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1079 // for (i = 0; i < np; ++i)
1080 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1084 void MathedCursor::setAccent(int ac)
1086 if (ac > 0 && accent < 8) {
1087 nestaccent[accent++] = ac;
1089 accent = 0; // consumed!
1093 int MathedCursor::getAccent() const
1095 return (accent > 0) ? nestaccent[accent - 1]: 0;
1099 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1101 MathedInset * ac = 0;
1103 for (int i = accent - 1; i >= 0; --i) {
1104 if (i == accent - 1)
1105 ac = new MathAccentInset(c, t, nestaccent[i]);
1107 ac = new MathAccentInset(ac, nestaccent[i]);
1111 cursor->insertInset(ac, LM_TC_INSET);
1113 accent = 0; // consumed!
1117 void MathedCursor::doAccent(MathedInset * p)
1119 MathedInset * ac = 0;
1121 for (int i = accent - 1; i >= 0; --i) {
1122 if (i == accent - 1)
1123 ac = new MathAccentInset(p, nestaccent[i]);
1125 ac = new MathAccentInset(ac, nestaccent[i]);
1129 cursor->insertInset(ac, LM_TC_INSET);
1131 accent = 0; // consumed!
1135 void MathedCursor::toggleLastCode(MathedTextCodes t)
1138 lastcode = LM_TC_VAR;
1144 void MathedCursor::GetPos(int & x, int & y)
1146 cursor->GetPos(x, y);
1150 short MathedCursor::GetFCode()
1152 return cursor->fcode();
1156 MathParInset * MathedCursor::GetPar()
1162 MathParInset * MathedCursor::getCurrentPar() const
1164 return cursor->getPar();
1168 string const & MathedCursor::getLabel() const
1170 return cursor->getLabel();
1174 bool MathedCursor::IsEnd() const
1176 return !cursor->OK();
1180 bool MathedCursor::InMacroMode()
1186 bool MathedCursor::Selection()
1192 void MathedCursor::clearLastCode()
1194 lastcode = LM_TC_MIN;
1198 void MathedCursor::setLastCode(MathedTextCodes t)
1204 MathedTextCodes MathedCursor::getLastCode() const