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;
51 // This was very smaller, I'll change it later
53 bool IsMacro(short tok, int id)
55 return tok != LM_TK_STACK &&
61 tok != LM_TK_FUNCLIM &&
62 tok != LM_TK_BIGSYM &&
63 tok != LM_TK_ACCENT &&
64 !(tok == LM_TK_SYM && id < 255);
67 static int const MAX_STACK_ITEMS = 32;
69 struct MathStackXIter {
70 std::vector<MathedXIter> item;
73 MathStackXIter(int n = MAX_STACK_ITEMS)
77 MathedXIter * push() {
86 MathedXIter * Item(int idx) {
88 cerr << "Wrong index: " << idx << " i: " << i << endl;
89 return &item[i - idx - 1];
97 return i >= MAX_STACK_ITEMS;
104 int Level() { return i; }
110 /***---------------- Mathed Cursor ---------------------------***/
112 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
116 lastcode = LM_TC_MIN;
118 if (!MathMacroTable::built)
119 MathMacroTable::mathMTable.builtinMacros();
123 void MathedCursor::SetPar(MathParInset * p)
126 selection = false; // not SelClear() ?
128 cursor = mathstk.push();
130 cursor->SetData(par);
134 void MathedCursor::draw(Painter & pain, int x, int y)
136 // lyxerr << "Cursor[" << x << " " << y << "] ";
137 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
140 int w = par->Width() + 2;
141 int a = par->Ascent() + 1;
142 int h = par->Height() + 1;
144 if (par->GetType() > LM_OT_PAR) {
149 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
151 par->draw(pain, x, y);
156 void MathedCursor::Redraw(Painter & pain)
158 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
160 int w = par->Width();
161 int h = par->Height();
165 //mathed_set_font(LM_TC_VAR, 1);
166 pain.fillRectangle(x, y - par->Ascent(),
167 x + w, y - par->Ascent() + h,
169 par->draw(pain, x, y);
173 bool MathedCursor::Left(bool sel)
176 // was MacroModeBack()
177 if (!imacro->GetName().empty()) {
178 imacro->SetName(imacro->GetName()
179 .substr(0, imacro->GetName()
189 if (sel && !selection)
192 if (!sel && selection)
195 bool result = cursor->Prev();
197 if (!result && !mathstk.Empty()) {
198 cursor = mathstk.pop();
203 } else if (result && cursor->IsActive()) {
204 if (cursor->IsScript()) {
206 if (!cursor->IsScript())
213 MathParInset * p = cursor->GetActiveInset();
217 p->setArgumentIdx(p->getMaxArgumentIdx());
218 cursor = mathstk.push();
228 bool MathedCursor::Pop()
230 if (!mathstk.Empty()) {
231 cursor = mathstk.pop();
240 bool MathedCursor::Push()
242 if (cursor->IsActive()) {
243 MathParInset * p = cursor->GetActiveInset();
246 cursor = mathstk.push();
254 bool MathedCursor::Right(bool sel)
263 if (sel && !selection)
266 if (!sel && selection)
271 if (cursor->IsActive()) {
272 if (cursor->IsScript()) {
274 // A script may be followed by another script
275 if (cursor->IsScript())
281 MathParInset *p = cursor->GetActiveInset();
283 lyxerr << "Math error: Inset expected." << endl;
284 return cursor->Next();
286 p->setArgumentIdx(0);
287 cursor = mathstk.push();
291 result = cursor->Next();
294 if (cursor->GetChar()!= LM_TC_CR)
295 result = cursor->Next();
296 if (!result && !mathstk.Empty()) {
297 cursor = mathstk.pop();
309 void MathedCursor::SetPos(int x, int y)
316 lastcode = LM_TC_MIN;
318 cursor = mathstk.push();
319 cursor->SetData(par);
320 cursor->fitCoord(x, y);
322 while (cursor->GetX()<x && cursor->OK()) {
323 if (cursor->IsActive()) {
324 MathParInset * p = cursor->GetActiveInset();
325 if (p->Inside(x, y)) {
327 cursor = mathstk.push();
329 cursor->fitCoord(x, y);
335 if (!cursor->Next() && !Pop())
338 if (x - xp < cursor->GetX() - x)
344 void MathedCursor::Home()
350 cursor = mathstk.push();
355 void MathedCursor::End()
361 cursor = mathstk.push();
366 MathMatrixInset * create_multiline(short int type, int cols)
377 for (int i = 0; i < cols; ++i)
384 for (int i = 0; i < cols; ++i)
389 case LM_OT_MULTLINEN:
402 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
403 mt->SetAlign(' ', align);
408 void MathedCursor::Insert(byte c, MathedTextCodes t)
416 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
420 MathParInset * p = cursor->getPar();
421 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
422 short int type = LM_OT_MPAR;
424 if (c >= '1' && c <= '9') {
427 } else if (c >= 'A' && c <= 'I') {
428 type = LM_OT_ALIGNAT;
431 type = LM_OT_MULTLINE;
435 if (p->GetType() == LM_OT_PARN)
437 MathMatrixInset * mt = create_multiline(type, cols);
438 mt->SetStyle(LM_ST_DISPLAY);
440 mt->setData(p->GetData());
445 int pos = cursor->getPos();
446 cursor->SetData(par);
447 cursor->goPosAbs(pos);
449 if (p && p->Permit(LMPF_ALLOW_CR)) {
452 } else if (t == LM_TC_TAB) {
453 MathParInset * p = cursor->getPar();
454 if (p && p->Permit(LMPF_ALLOW_TAB)) {
456 cursor->insert(c, t);
459 cursor->goNextColumn();
460 } else // Navigate between arguments
461 if (p && p->GetType() == LM_OT_MACRO) {
462 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
463 p->setArgumentIdx(p->getArgumentIdx() + 1);
470 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
471 // was MacroModeInsert(c);
472 imacro->SetName(imacro->GetName() + static_cast<char>(c));
480 cursor->insert(c, t);
489 void MathedCursor::insertInset(MathedInset * p, int t)
495 if (MathIsActive(t)) {
497 static_cast<MathParInset*>(p)->setData(selarray);
502 if (mathstk.i < MAX_STACK_ITEMS - 1) {
503 if (accent && !MathIsActive(t)) {
506 cursor->insertInset(p, t);
507 if (MathIsActive(t)) {
513 lyxerr << "Math error: Full stack." << endl;
517 void MathedCursor::Delete()
527 if (cursor->Empty() && !mathstk.Empty())
528 cursor = mathstk.pop();
530 // if (cursor->GetChar()!= LM_TC_TAB)
536 void MathedCursor::DelLine()
546 MathParInset * p = cursor->getPar();
548 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
554 bool MathedCursor::Up(bool sel)
561 if (sel && !selection)
564 if (!sel && selection)
567 if (cursor->IsScript()) {
568 char cd = cursor->GetChar();
573 // A subscript may be followed by a superscript
576 if (MathIsUp(cursor->GetChar())) {
579 } else // return to the previous state
584 result = cursor->Up();
585 if (!result && cursor->getPar()) {
586 MathParInset * p = cursor->getPar();
588 if (p->GetType() == LM_OT_SCRIPT) {
589 MathedXIter * cx = mathstk.Item(1);
590 bool is_down = (cx->GetChar() == LM_TC_DOWN);
591 cursor = mathstk.pop();
593 result = (is_down) ? true: Up();
595 result = (p->getArgumentIdx() > 0);
597 p->setArgumentIdx(p->getArgumentIdx() - 1);
602 if (!result && !mathstk.Empty()) {
603 cursor = mathstk.pop();
611 bool MathedCursor::Down(bool sel)
618 if (sel && !selection)
621 if (!sel && selection)
624 if (cursor->IsScript()) {
625 char cd = cursor->GetChar();
626 if (MathIsDown(cd)) {
630 // A superscript may be followed by a subscript
633 if (MathIsDown(cursor->GetChar())) {
641 result = cursor->Down();
642 if (!result && cursor->getPar()) {
643 MathParInset * p= cursor->getPar();
644 if (p->GetType() == LM_OT_SCRIPT) {
645 MathedXIter * cx = mathstk.Item(1);
646 bool is_up = (cx->GetChar() == LM_TC_UP);
647 cursor = mathstk.pop();
649 result = (is_up) ? true : Down();
651 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
653 p->setArgumentIdx(p->getArgumentIdx() + 1);
657 if (!result && !mathstk.Empty()) {
658 cursor = mathstk.pop();
666 bool MathedCursor::Limits()
668 if (cursor->IsInset()) {
669 MathedInset * p = cursor->GetInset();
670 bool ol = p->GetLimits();
672 return (ol!= p->GetLimits());
678 void MathedCursor::SetSize(short size)
680 MathParInset * p = cursor->getPar();
681 p->UserSetSize(size);
686 void MathedCursor::setLabel(string const & label)
688 // ugly hack and possible bug
689 if (!cursor->setLabel(label))
690 lyxerr << "MathErr: Bad place to set labels." << endl;
694 void MathedCursor::setNumbered()
697 MathedRowContainer::iterator crow = cursor->currentRow();
699 crow->setNumbered(!crow->isNumbered());
703 void MathedCursor::Interpret(string const & s)
706 latexkeys const * l = 0;
707 MathedTextCodes tcode = LM_TC_INSET;
709 if (s[0] == '^' || s[0] == '_') {
710 char c = cursor->GetChar();
711 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
716 // A script may be followed by a script
717 if (MathIsUp(c) || MathIsDown(c)) {
720 c = cursor->GetChar();
721 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
727 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
728 insertInset(p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
731 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
732 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
733 p = new MathSpaceInset(sp);
734 insertInset(p, LM_TC_INSET);
740 p = MathMacroTable::mathMTable.createMacro(s);
742 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
744 p = new MathRootInset;
745 tcode = LM_TC_ACTIVE_INSET;
747 p = new MathFuncInset(s, LM_OT_UNDEF);
749 tcode = static_cast<MathMacro*>(p)->getTCode();
750 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
753 MathedInsetTypes fractype = LM_OT_FRAC;
756 p = new MathBigopInset(l->name, l->id);
761 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
762 LM_TC_BOPS: LM_TC_SYMB);
764 p = new MathFuncInset(l->name);
769 fractype = LM_OT_STACKREL;
770 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
773 p = new MathFracInset(fractype);
774 tcode = LM_TC_ACTIVE_INSET;
778 p = new MathSqrtInset;
779 tcode = LM_TC_ACTIVE_INSET;
783 p = new MathDecorationInset(l->id);
784 tcode = LM_TC_ACTIVE_INSET;
788 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
792 p = new MathSpaceInset(l->id);
796 p = new MathDotsInset(l->name, l->id);
804 p = MathMacroTable::mathMTable.createMacro(s);
805 tcode = static_cast<MathMacro*>(p)->getTCode();
806 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
810 p = new MathFuncInset(l->name);
816 insertInset(p, tcode);
822 bool MathedCursor::pullArg()
824 if (cursor->IsActive()) {
825 MathParInset * p = cursor->GetActiveInset();
829 MathedArray a = p->GetData();
843 void MathedCursor::MacroModeOpen()
846 imacro = new MathFuncInset("");
847 insertInset(imacro, LM_TC_INSET);
850 lyxerr << "Mathed Warning: Already in macro mode" << endl;
854 void MathedCursor::MacroModeClose()
858 latexkeys const * l = in_word_set(imacro->GetName());
859 if (!imacro->GetName().empty()
860 && (!l || (l && IsMacro(l->token, l->id))) &&
861 !MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
863 //imacro->SetName(macrobf);
864 // This guarantees that the string will be removed by destructor
865 imacro->SetType(LM_OT_UNDEF);
867 imacro->SetName(l->name);
870 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
872 static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
875 if (l || MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
876 Interpret(imacro->GetName());
885 void MathedCursor::SelCopy()
888 int const p1 = (cursor->getPos() < selpos) ?
889 cursor->getPos() : selpos;
890 int const p2 = (cursor->getPos() > selpos) ?
891 cursor->getPos() : selpos;
892 selarray = *(cursor->GetData());
893 selarray.shrink(p1, p2);
900 void MathedCursor::SelCut()
903 if (cursor->getPos() == selpos)
906 int const p1 = (cursor->getPos() < selpos) ?
907 cursor->getPos() : selpos;
908 int const p2 = (cursor->getPos() > selpos) ?
909 cursor->getPos() : selpos;
910 selarray = *(cursor->GetData());
911 selarray.shrink(p1, p2);
912 cursor->Clean(selpos);
919 void MathedCursor::SelDel()
921 // lyxerr << "Deleting sel "
923 if (cursor->getPos() == selpos)
925 cursor->Clean(selpos);
932 void MathedCursor::SelPaste()
934 // lyxerr << "paste " << selarray << " " << curor->pos;
938 if (!selarray.empty()) {
939 cursor->Merge(selarray);
945 void MathedCursor::SelStart()
947 lyxerr[Debug::MATHED] << "Starting sel " << endl;
949 selpos = cursor->getPos();
951 anchor = selstk.Item(-1);
952 anchor->SetData(cursor->getPar());
954 anchor->goPosAbs(selpos);
960 void MathedCursor::SelClear()
962 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
969 // Anchor position must be at the same level that stack.
970 void MathedCursor::SelBalance()
972 int d = mathstk.Level() - selstk.Level();
974 // If unbalanced, balance them
977 // lyxerr << "b[" << mathstk.Level() << " " << selstk.Level
978 // << " " << anchor->GetX() << " " << cursor->GetX() << "]";
979 anchor = selstk.pop();
980 if (anchor->GetX() >= cursor->GetX())
983 // lyxerr <<"a[" << mathstk.Level() << " " << selstk.Level() <<"]";
986 d = mathstk.Level() - selstk.Level();
989 // Once balanced the levels, check that they are at the same paragraph
990 selpos = anchor->getPos();
994 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
996 static int xpoint[10];
997 static int ypoint[10];
1008 // Balance anchor and cursor
1013 cursor->getPar()->GetXY(xo, yo);
1014 int w = cursor->getPar()->Width();
1017 cursor->GetPos(x1, y1);
1020 cursor->getAD(a1, d1);
1023 anchor->GetPos(x, y);
1026 anchor->getAD(a, d);
1028 // single row selection
1031 ypoint[i++] = y + d;
1033 ypoint[i++] = y - a;
1037 ypoint[i++] = y - a;
1041 ypoint[i++] = y1 - a;
1046 ypoint[i++] = y1 - a;
1048 ypoint[i++] = y1 + d;
1052 ypoint[i++] = y1 + d;
1055 ypoint[i++] = y + d;
1058 xpoint[i] = xpoint[0];
1059 ypoint[i++] = ypoint[0];
1064 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1065 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1066 // for (i = 0; i < np; ++i)
1067 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1071 void MathedCursor::setAccent(int ac)
1073 if (ac > 0 && accent < 8) {
1074 nestaccent[accent++] = ac;
1076 accent = 0; // consumed!
1080 int MathedCursor::getAccent() const
1082 return (accent > 0) ? nestaccent[accent - 1]: 0;
1086 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1088 MathedInset * ac = 0;
1090 for (int i = accent - 1; i >= 0; --i) {
1091 if (i == accent - 1)
1092 ac = new MathAccentInset(c, t, nestaccent[i]);
1094 ac = new MathAccentInset(ac, nestaccent[i]);
1098 cursor->insertInset(ac, LM_TC_INSET);
1100 accent = 0; // consumed!
1104 void MathedCursor::doAccent(MathedInset * p)
1106 MathedInset * ac = 0;
1108 for (int i = accent - 1; i >= 0; --i) {
1109 if (i == accent - 1)
1110 ac = new MathAccentInset(p, nestaccent[i]);
1112 ac = new MathAccentInset(ac, nestaccent[i]);
1116 cursor->insertInset(ac, LM_TC_INSET);
1118 accent = 0; // consumed!
1122 void MathedCursor::toggleLastCode(MathedTextCodes t)
1125 lastcode = LM_TC_VAR;
1131 void MathedCursor::GetPos(int & x, int & y)
1133 cursor->GetPos(x, y);
1137 short MathedCursor::GetFCode()
1139 return cursor->fcode();
1143 MathParInset * MathedCursor::GetPar()
1149 MathParInset * MathedCursor::getCurrentPar() const
1151 return cursor->getPar();
1155 string const & MathedCursor::getLabel() const
1157 return cursor->getLabel();
1161 bool MathedCursor::IsEnd() const
1163 return !cursor->OK();
1167 bool MathedCursor::InMacroMode()
1173 bool MathedCursor::Selection()
1179 void MathedCursor::clearLastCode()
1181 lastcode = LM_TC_MIN;
1185 void MathedCursor::setLastCode(MathedTextCodes t)
1191 MathedTextCodes MathedCursor::getLastCode() const