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 "math_macrotemplate.h"
44 #include "mathed/support.h"
46 static MathedArray selarray;
50 // This was very smaller, I'll change it later
52 bool IsMacro(short tok, int id)
54 return tok != LM_TK_STACK &&
60 tok != LM_TK_FUNCLIM &&
61 tok != LM_TK_BIGSYM &&
62 tok != LM_TK_ACCENT &&
63 !(tok == LM_TK_SYM && id < 255);
66 static int const MAX_STACK_ITEMS = 32;
68 struct MathStackXIter {
69 std::vector<MathedXIter> item;
72 MathStackXIter(int n = MAX_STACK_ITEMS)
76 MathedXIter * push() {
84 MathedXIter * Item(int idx) {
86 cerr << "Wrong index: " << idx << " i: " << i << endl;
87 return &item[i - idx - 1];
95 return i >= MAX_STACK_ITEMS;
102 int Level() { return i; }
108 /***---------------- Mathed Cursor ---------------------------***/
110 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
114 lastcode = LM_TC_MIN;
116 if (!MathMacroTable::built)
117 MathMacroTable::mathMTable.builtinMacros();
121 void MathedCursor::SetPar(MathParInset * p)
124 selection = false; // not SelClear() ?
126 cursor = mathstk.push();
128 cursor->SetData(par);
132 void MathedCursor::draw(Painter & pain, int x, int y)
134 // lyxerr << "Cursor[" << x << " " << y << "] ";
135 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
138 int w = par->Width() + 2;
139 int a = par->Ascent() + 1;
140 int h = par->Height() + 1;
142 if (par->GetType() > LM_OT_PAR) {
147 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
149 par->draw(pain, x, y);
154 void MathedCursor::Redraw(Painter & pain)
156 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
158 int w = par->Width();
159 int h = par->Height();
163 //mathed_set_font(LM_TC_VAR, 1);
164 pain.fillRectangle(x, y - par->Ascent(),
165 x + w, y - par->Ascent() + h,
167 par->draw(pain, x, y);
171 bool MathedCursor::Left(bool sel)
174 // was MacroModeBack()
175 if (!imacro->GetName().empty()) {
176 imacro->SetName(imacro->GetName()
177 .substr(0, imacro->GetName()
187 if (sel && !selection)
190 if (!sel && selection)
193 bool result = cursor->Prev();
195 if (!result && !mathstk.Empty()) {
196 cursor = mathstk.pop();
201 } else if (result && cursor->IsActive()) {
202 if (cursor->IsScript()) {
204 if (!cursor->IsScript())
211 MathParInset * p = cursor->GetActiveInset();
215 p->setArgumentIdx(p->getMaxArgumentIdx());
216 cursor = mathstk.push();
226 bool MathedCursor::Pop()
228 if (!mathstk.Empty()) {
229 cursor = mathstk.pop();
238 bool MathedCursor::Push()
240 if (cursor->IsActive()) {
241 MathParInset * p = cursor->GetActiveInset();
244 cursor = mathstk.push();
252 bool MathedCursor::Right(bool sel)
261 if (sel && !selection)
264 if (!sel && selection)
269 if (cursor->IsActive()) {
270 if (cursor->IsScript()) {
272 // A script may be followed by another script
273 if (cursor->IsScript())
279 MathParInset *p = cursor->GetActiveInset();
281 lyxerr << "Math error: Inset expected." << endl;
282 return cursor->Next();
284 p->setArgumentIdx(0);
285 cursor = mathstk.push();
289 result = cursor->Next();
292 if (cursor->GetChar()!= LM_TC_CR)
293 result = cursor->Next();
294 if (!result && !mathstk.Empty()) {
295 cursor = mathstk.pop();
307 void MathedCursor::SetPos(int x, int y)
314 lastcode = LM_TC_MIN;
316 cursor = mathstk.push();
317 cursor->SetData(par);
318 cursor->fitCoord(x, y);
320 while (cursor->GetX()<x && cursor->OK()) {
321 if (cursor->IsActive()) {
322 MathParInset * p = cursor->GetActiveInset();
323 if (p->Inside(x, y)) {
325 cursor = mathstk.push();
327 cursor->fitCoord(x, y);
333 if (!cursor->Next() && !Pop())
336 if (x - xp < cursor->GetX() - x)
342 void MathedCursor::Home()
348 cursor = mathstk.push();
353 void MathedCursor::End()
359 cursor = mathstk.push();
364 MathMatrixInset * create_multiline(short int type, int cols)
375 for (int i = 0; i < cols; ++i)
382 for (int i = 0; i < cols; ++i)
387 case LM_OT_MULTLINEN:
400 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
401 mt->SetAlign(' ', align);
406 void MathedCursor::Insert(byte c, MathedTextCodes t)
414 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
418 MathParInset * p = cursor->getPar();
419 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
420 short int type = LM_OT_MPAR;
422 if (c >= '1' && c <= '9') {
425 } else if (c >= 'A' && c <= 'I') {
426 type = LM_OT_ALIGNAT;
429 type = LM_OT_MULTLINE;
433 if (p->GetType() == LM_OT_PARN)
435 MathMatrixInset * mt = create_multiline(type, cols);
436 mt->SetStyle(LM_ST_DISPLAY);
438 mt->setData(p->GetData());
443 int pos = cursor->getPos();
444 cursor->SetData(par);
445 cursor->goPosAbs(pos);
447 if (p && p->Permit(LMPF_ALLOW_CR)) {
450 } else if (t == LM_TC_TAB) {
451 MathParInset * p = cursor->getPar();
452 if (p && p->Permit(LMPF_ALLOW_TAB)) {
454 cursor->insert(c, t);
457 cursor->goNextColumn();
458 } else // Navigate between arguments
459 if (p && p->GetType() == LM_OT_MACRO) {
460 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
461 p->setArgumentIdx(p->getArgumentIdx() + 1);
468 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
469 // was MacroModeInsert(c);
470 imacro->SetName(imacro->GetName() + static_cast<char>(c));
478 cursor->insert(c, t);
487 void MathedCursor::insertInset(MathedInset * p, int t)
493 if (MathIsActive(t)) {
495 static_cast<MathParInset*>(p)->setData(selarray);
500 if (mathstk.i < MAX_STACK_ITEMS - 1) {
501 if (accent && !MathIsActive(t)) {
504 cursor->insertInset(p, t);
505 if (MathIsActive(t)) {
511 lyxerr << "Math error: Full stack." << endl;
515 void MathedCursor::Delete()
525 if (cursor->Empty() && !mathstk.Empty())
526 cursor = mathstk.pop();
528 // if (cursor->GetChar()!= LM_TC_TAB)
534 void MathedCursor::DelLine()
544 MathParInset * p = cursor->getPar();
546 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
552 bool MathedCursor::Up(bool sel)
559 if (sel && !selection)
562 if (!sel && selection)
565 if (cursor->IsScript()) {
566 char cd = cursor->GetChar();
571 // A subscript may be followed by a superscript
574 if (MathIsUp(cursor->GetChar())) {
577 } else // return to the previous state
582 result = cursor->Up();
583 if (!result && cursor->getPar()) {
584 MathParInset * p = cursor->getPar();
586 if (p->GetType() == LM_OT_SCRIPT) {
587 MathedXIter * cx = mathstk.Item(1);
588 bool is_down = (cx->GetChar() == LM_TC_DOWN);
589 cursor = mathstk.pop();
591 result = (is_down) ? true: Up();
593 result = (p->getArgumentIdx() > 0);
595 p->setArgumentIdx(p->getArgumentIdx() - 1);
600 if (!result && !mathstk.Empty()) {
601 cursor = mathstk.pop();
609 bool MathedCursor::Down(bool sel)
616 if (sel && !selection)
619 if (!sel && selection)
622 if (cursor->IsScript()) {
623 char cd = cursor->GetChar();
624 if (MathIsDown(cd)) {
628 // A superscript may be followed by a subscript
631 if (MathIsDown(cursor->GetChar())) {
639 result = cursor->Down();
640 if (!result && cursor->getPar()) {
641 MathParInset * p= cursor->getPar();
642 if (p->GetType() == LM_OT_SCRIPT) {
643 MathedXIter * cx = mathstk.Item(1);
644 bool is_up = (cx->GetChar() == LM_TC_UP);
645 cursor = mathstk.pop();
647 result = (is_up) ? true : Down();
649 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
651 p->setArgumentIdx(p->getArgumentIdx() + 1);
655 if (!result && !mathstk.Empty()) {
656 cursor = mathstk.pop();
664 bool MathedCursor::Limits()
666 if (cursor->IsInset()) {
667 MathedInset * p = cursor->GetInset();
668 bool ol = p->GetLimits();
670 return (ol!= p->GetLimits());
676 void MathedCursor::SetSize(short size)
678 MathParInset * p = cursor->getPar();
679 p->UserSetSize(size);
684 void MathedCursor::setLabel(string const & label)
686 // ugly hack and possible bug
687 if (!cursor->setLabel(label))
688 lyxerr << "MathErr: Bad place to set labels." << endl;
692 void MathedCursor::setNumbered()
695 MathedRowContainer::iterator crow = cursor->currentRow();
697 crow->setNumbered(!crow->isNumbered());
701 void MathedCursor::Interpret(string const & s)
704 latexkeys const * l = 0;
705 MathedTextCodes tcode = LM_TC_INSET;
707 if (s[0] == '^' || s[0] == '_') {
708 char c = cursor->GetChar();
709 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
714 // A script may be followed by a script
715 if (MathIsUp(c) || MathIsDown(c)) {
718 c = cursor->GetChar();
719 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
725 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
726 insertInset(p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
729 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
730 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
731 p = new MathSpaceInset(sp);
732 insertInset(p, LM_TC_INSET);
738 p = MathMacroTable::mathMTable.createMacro(s);
740 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
742 p = new MathRootInset;
743 tcode = LM_TC_ACTIVE_INSET;
745 p = new MathFuncInset(s, LM_OT_UNDEF);
747 tcode = static_cast<MathMacro*>(p)->getTCode();
748 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
751 MathedInsetTypes fractype = LM_OT_FRAC;
754 p = new MathBigopInset(l->name, l->id);
759 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
760 LM_TC_BOPS: LM_TC_SYMB);
762 p = new MathFuncInset(l->name);
767 fractype = LM_OT_STACKREL;
768 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
771 p = new MathFracInset(fractype);
772 tcode = LM_TC_ACTIVE_INSET;
776 p = new MathSqrtInset;
777 tcode = LM_TC_ACTIVE_INSET;
781 p = new MathDecorationInset(l->id);
782 tcode = LM_TC_ACTIVE_INSET;
786 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
790 p = new MathSpaceInset(l->id);
794 p = new MathDotsInset(l->name, l->id);
802 p = MathMacroTable::mathMTable.createMacro(s);
803 tcode = static_cast<MathMacro*>(p)->getTCode();
804 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
808 p = new MathFuncInset(l->name);
814 insertInset(p, tcode);
820 bool MathedCursor::pullArg()
822 if (cursor->IsActive()) {
823 MathParInset * p = cursor->GetActiveInset();
827 MathedArray a = p->GetData();
841 void MathedCursor::MacroModeOpen()
844 imacro = new MathFuncInset("");
845 insertInset(imacro, LM_TC_INSET);
848 lyxerr << "Mathed Warning: Already in macro mode" << endl;
852 void MathedCursor::MacroModeClose()
856 latexkeys const * l = in_word_set(imacro->GetName());
857 if (!imacro->GetName().empty()
858 && (!l || (l && IsMacro(l->token, l->id))) &&
859 !MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
861 //imacro->SetName(macrobf);
862 // This guarantees that the string will be removed by destructor
863 imacro->SetType(LM_OT_UNDEF);
865 imacro->SetName(l->name);
868 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
870 static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
873 if (l || MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
874 Interpret(imacro->GetName());
883 void MathedCursor::SelCopy()
886 int const p1 = (cursor->getPos() < selpos) ?
887 cursor->getPos() : selpos;
888 int const p2 = (cursor->getPos() > selpos) ?
889 cursor->getPos() : selpos;
890 selarray = *(cursor->GetData());
891 selarray.shrink(p1, p2);
898 void MathedCursor::SelCut()
901 if (cursor->getPos() == selpos)
904 int const p1 = (cursor->getPos() < selpos) ?
905 cursor->getPos() : selpos;
906 int const p2 = (cursor->getPos() > selpos) ?
907 cursor->getPos() : selpos;
908 selarray = *(cursor->GetData());
909 selarray.shrink(p1, p2);
910 cursor->Clean(selpos);
917 void MathedCursor::SelDel()
919 // lyxerr << "Deleting sel "
921 if (cursor->getPos() == selpos)
923 cursor->Clean(selpos);
930 void MathedCursor::SelPaste()
932 // lyxerr << "paste " << selarray << " " << curor->pos;
936 if (!selarray.empty()) {
937 cursor->Merge(selarray);
943 void MathedCursor::SelStart()
945 lyxerr[Debug::MATHED] << "Starting sel " << endl;
947 selpos = cursor->getPos();
949 anchor = selstk.Item(-1);
950 anchor->SetData(cursor->getPar());
952 anchor->goPosAbs(selpos);
958 void MathedCursor::SelClear()
960 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
967 // Anchor position must be at the same level that stack.
968 void MathedCursor::SelBalance()
970 int d = mathstk.Level() - selstk.Level();
972 // If unbalanced, balance them
975 // lyxerr << "b[" << mathstk.Level() << " " << selstk.Level
976 // << " " << anchor->GetX() << " " << cursor->GetX() << "]";
977 anchor = selstk.pop();
978 if (anchor->GetX() >= cursor->GetX())
981 // lyxerr <<"a[" << mathstk.Level() << " " << selstk.Level() <<"]";
984 d = mathstk.Level() - selstk.Level();
987 // Once balanced the levels, check that they are at the same paragraph
988 selpos = anchor->getPos();
992 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
994 static int xpoint[10];
995 static int ypoint[10];
1006 // Balance anchor and cursor
1011 cursor->getPar()->GetXY(xo, yo);
1012 int w = cursor->getPar()->Width();
1015 cursor->GetPos(x1, y1);
1018 cursor->getAD(a1, d1);
1021 anchor->GetPos(x, y);
1024 anchor->getAD(a, d);
1026 // single row selection
1029 ypoint[i++] = y + d;
1031 ypoint[i++] = y - a;
1035 ypoint[i++] = y - a;
1039 ypoint[i++] = y1 - a;
1044 ypoint[i++] = y1 - a;
1046 ypoint[i++] = y1 + d;
1050 ypoint[i++] = y1 + d;
1053 ypoint[i++] = y + d;
1056 xpoint[i] = xpoint[0];
1057 ypoint[i++] = ypoint[0];
1062 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1063 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1064 // for (i = 0; i < np; ++i)
1065 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1069 void MathedCursor::setAccent(int ac)
1071 if (ac > 0 && accent < 8) {
1072 nestaccent[accent++] = ac;
1074 accent = 0; // consumed!
1078 int MathedCursor::getAccent() const
1080 return (accent > 0) ? nestaccent[accent - 1]: 0;
1084 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1086 MathedInset * ac = 0;
1088 for (int i = accent - 1; i >= 0; --i) {
1089 if (i == accent - 1)
1090 ac = new MathAccentInset(c, t, nestaccent[i]);
1092 ac = new MathAccentInset(ac, nestaccent[i]);
1096 cursor->insertInset(ac, LM_TC_INSET);
1098 accent = 0; // consumed!
1102 void MathedCursor::doAccent(MathedInset * p)
1104 MathedInset * ac = 0;
1106 for (int i = accent - 1; i >= 0; --i) {
1107 if (i == accent - 1)
1108 ac = new MathAccentInset(p, nestaccent[i]);
1110 ac = new MathAccentInset(ac, nestaccent[i]);
1114 cursor->insertInset(ac, LM_TC_INSET);
1116 accent = 0; // consumed!
1120 void MathedCursor::toggleLastCode(MathedTextCodes t)
1123 lastcode = LM_TC_VAR;
1129 void MathedCursor::GetPos(int & x, int & y)
1131 cursor->GetPos(x, y);
1135 short MathedCursor::GetFCode()
1137 return cursor->fcode();
1141 MathParInset * MathedCursor::GetPar()
1147 MathParInset * MathedCursor::getCurrentPar() const
1149 return cursor->getPar();
1153 string const & MathedCursor::getLabel() const
1155 return cursor->getLabel();
1159 bool MathedCursor::IsEnd() const
1161 return !cursor->OK();
1165 bool MathedCursor::InMacroMode()
1171 bool MathedCursor::Selection()
1177 void MathedCursor::clearLastCode()
1179 lastcode = LM_TC_MIN;
1183 void MathedCursor::setLastCode(MathedTextCodes t)
1189 MathedTextCodes MathedCursor::getLastCode() const