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->p;
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->p;
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::Insert(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->Insert(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->p;
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->p) {
602 MathParInset * p = cursor->p;
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->p) {
659 MathParInset * p= cursor->p;
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->p;
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->crow;
716 crow->setNumbered(!crow->isNumbered());
720 void MathedCursor::Interpret(string const & s)
724 MathedTextCodes tcode = LM_TC_INSET;
726 if (s[0] == '^' || s[0] == '_') {
727 char c = cursor->GetChar();
728 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
733 // A script may be followed by a script
734 if (MathIsUp(c) || MathIsDown(c)) {
737 c = cursor->GetChar();
738 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
744 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
745 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
748 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
749 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
750 p = new MathSpaceInset(sp);
757 p = MathMacroTable::mathMTable.getMacro(s);
759 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
761 p = new MathRootInset();
762 tcode = LM_TC_ACTIVE_INSET;
764 p = new MathFuncInset(s, LM_OT_UNDEF);
766 tcode = static_cast<MathMacro*>(p)->getTCode();
767 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
770 MathedInsetTypes fractype = LM_OT_FRAC;
773 p = new MathBigopInset(l->name, l->id);
778 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
779 LM_TC_BOPS: LM_TC_SYMB);
781 p = new MathFuncInset(l->name);
786 fractype = LM_OT_STACKREL;
787 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
790 p = new MathFracInset(fractype);
791 tcode = LM_TC_ACTIVE_INSET;
795 p = new MathSqrtInset;
796 tcode = LM_TC_ACTIVE_INSET;
800 p = new MathDecorationInset(l->id);
801 tcode = LM_TC_ACTIVE_INSET;
805 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
809 p = new MathSpaceInset(l->id);
813 p = new MathDotsInset(l->name, l->id);
821 p = MathMacroTable::mathMTable.getMacro(s);
822 tcode = static_cast<MathMacro*>(p)->getTCode();
823 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
827 p = new MathFuncInset(l->name);
839 bool MathedCursor::pullArg()
841 if (cursor->IsActive()) {
842 MathParInset * p = cursor->GetActiveInset();
846 MathedArray * a = p->GetData();
860 void MathedCursor::MacroModeOpen()
863 imacro = new MathFuncInset("");
867 lyxerr << "Mathed Warning: Already in macro mode" << endl;
871 void MathedCursor::MacroModeClose()
875 latexkeys * l = in_word_set(imacro->GetName());
876 if (!imacro->GetName().empty()
877 && (!l || (l && IsMacro(l->token, l->id))) &&
878 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
880 //imacro->SetName(macrobf);
881 // This guarantees that the string will be removed by destructor
882 imacro->SetType(LM_OT_UNDEF);
884 imacro->SetName(l->name);
887 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
889 static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
892 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
893 Interpret(imacro->GetName());
902 void MathedCursor::SelCopy()
905 int p1 = (cursor->pos < selpos) ? cursor->pos : selpos;
906 int p2 = (cursor->pos > selpos) ? cursor->pos : selpos;
907 selarray = cursor->Copy(p1, p2);
914 void MathedCursor::SelCut()
917 if (cursor->pos == selpos)
920 int p1 = (cursor->pos < selpos) ? cursor->pos : selpos;
921 int p2 = (cursor->pos > selpos) ? cursor->pos : selpos;
922 selarray = cursor->Copy(p1, p2);
923 cursor->Clean(selpos);
930 void MathedCursor::SelDel()
932 // lyxerr << "Deleting sel "
934 if (cursor->pos == 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->pos;
961 selstk = new MathStackXIter(mathstk);
962 anchor = selstk->Item(-1);
963 anchor->SetData(cursor->p);
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->pos;
1007 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
1009 static int xpoint[10];
1010 static int ypoint[10];
1021 // single row selection
1028 // Balance anchor and cursor
1033 cursor->p->GetXY(xo, yo);
1034 int w = cursor->p->Width();
1037 cursor->GetPos(x1, y1);
1038 cursor->getAD(a1, d1);
1041 anchor->GetPos(x, y);
1042 anchor->getAD(a, d);
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]);
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]);
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
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