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_root.h"
29 #include "support/lstrings.h"
33 #include "math_matrixinset.h"
34 #include "math_rowst.h"
35 #include "math_spaceinset.h"
36 #include "math_funcinset.h"
37 #include "math_bigopinset.h"
38 #include "math_fracinset.h"
39 #include "math_decorationinset.h"
40 #include "math_dotsinset.h"
41 #include "math_accentinset.h"
43 static MathedArray * selarray = 0;
47 // This was very smaller, I'll change it later
49 bool IsMacro(short tok, int id)
51 return (tok != LM_TK_STACK && tok != LM_TK_FRAC && tok != LM_TK_SQRT
53 && tok != LM_TK_SPACE && tok != LM_TK_DOTS
54 && tok != LM_TK_FUNCLIM
55 && tok != LM_TK_BIGSYM && tok != LM_TK_ACCENT &&
56 !(tok == LM_TK_SYM && id < 255));
59 static int const MAX_STACK_ITEMS = 32;
61 struct MathStackXIter {
65 MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
66 item = new MathedXIter[imax];
70 MathStackXIter(MathStackXIter & stk);
76 void push(MathedXIter ** a) {
85 MathedXIter * Item(int idx) {
86 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
94 return i >= MAX_STACK_ITEMS;
101 int Level() { return i; }
103 } mathstk, *selstk = 0;
106 MathStackXIter::MathStackXIter(MathStackXIter & stk)
109 item = new MathedXIter[imax];
111 for (int k = 0; k < i; ++k) {
112 item[k].SetData(stk.item[k].getPar());
114 item[k].goPosAbs(stk.item[k].getPos());
119 /***---------------- Mathed Cursor ---------------------------***/
121 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
125 lastcode = LM_TC_MIN;
128 if (!MathMacroTable::built)
129 MathMacroTable::mathMTable.builtinMacros();
133 void MathedCursor::SetPar(MathParInset * p)
138 selection = false; // not SelClear() ?
140 mathstk.push(&cursor);
142 cursor->SetData(par);
146 void MathedCursor::draw(Painter & pain, int x, int y)
148 // lyxerr << "Cursor[" << x << " " << y << "] ";
149 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
151 int w = par->Width() + 2;
152 int a = par->Ascent() + 1;
153 int h = par->Height() + 1;
154 if (par->GetType() > LM_OT_PAR) { a += 4; h += 8; }
156 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
158 par->draw(pain, x, y);
163 void MathedCursor::Redraw(Painter & pain)
165 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
167 int w = par->Width(), h = par->Height();
171 //mathed_set_font(LM_TC_VAR, 1);
172 pain.fillRectangle(x, y - par->Ascent(),
173 x + w, y - par->Ascent() + h,
175 par->draw(pain, x, y);
179 bool MathedCursor::Left(bool sel)
186 if (sel && !selection) SelStart();
187 if (!sel && selection) SelClear();
188 bool result = cursor->Prev();
189 if (!result && !mathstk.Empty()) {
190 cursor = mathstk.pop();
193 if (selection) SelClear();
195 if (result && cursor->IsActive()) {
196 if (cursor->IsScript()) {
198 if (!cursor->IsScript())
204 MathParInset * p = cursor->GetActiveInset();
208 p->setArgumentIdx(p->getMaxArgumentIdx());
209 mathstk.push(&cursor);
219 bool MathedCursor::Pop()
221 if (!mathstk.Empty()) {
222 cursor = mathstk.pop();
231 bool MathedCursor::Push()
233 if (cursor->IsActive()) {
234 MathParInset * p = cursor->GetActiveInset();
235 if (!p) return false;
236 mathstk.push(&cursor);
244 bool MathedCursor::Right(bool sel)
251 if (sel && !selection) SelStart();
252 if (!sel && selection) SelClear();
255 if (cursor->IsActive()) {
256 if (cursor->IsScript()) {
258 // A script may be followed by another script
259 if (cursor->IsScript())
264 MathParInset *p = cursor->GetActiveInset();
266 lyxerr << "Math error: Inset expected." << endl;
267 return cursor->Next();
269 p->setArgumentIdx(0);
270 mathstk.push(&cursor);
274 result = cursor->Next();
276 if (cursor->GetChar()!= LM_TC_CR)
277 result = cursor->Next();
278 if (!result && !mathstk.Empty()) {
279 cursor = mathstk.pop();
283 if (selection) SelClear();
290 void MathedCursor::SetPos(int x, int y)
294 if (macro_mode) MacroModeClose();
295 lastcode = LM_TC_MIN;
297 mathstk.push(&cursor);
298 cursor->SetData(par);
299 cursor->fitCoord(x, y);
300 while (cursor->GetX()<x && cursor->OK()) {
301 if (cursor->IsActive()) {
302 MathParInset * p = cursor->GetActiveInset();
303 if (p->Inside(x, y)) {
305 mathstk.push(&cursor);
307 cursor->fitCoord(x, y);
313 if (!cursor->Next() && !Pop())
316 if (x-xp < cursor->GetX()-x) cursor->ipop();
321 void MathedCursor::Home()
323 if (macro_mode) MacroModeClose();
326 mathstk.push(&cursor);
331 void MathedCursor::End()
333 if (macro_mode) MacroModeClose();
336 mathstk.push(&cursor);
341 MathMatrixInset * create_multiline(short int type, int cols)
352 for (int i = 0; i < cols; ++i)
358 for (int i = 0; i < cols; ++i)
362 case LM_OT_MULTLINEN:
374 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
375 mt->SetAlign(' ', align);
379 void MathedCursor::Insert(byte c, MathedTextCodes t)
381 if (selection) SelDel();
386 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
390 MathParInset * p = cursor->p;
391 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
392 short int type = LM_OT_MPAR;
394 if (c >= '1' && c <= '9') {
397 } else if (c >= 'A' && c <= 'I') {
398 type = LM_OT_ALIGNAT;
401 type = LM_OT_MULTLINE;
405 if (p->GetType() == LM_OT_PARN)
407 MathMatrixInset * mt = create_multiline(type, cols);
408 mt->SetStyle(LM_ST_DISPLAY);
410 mt->SetData(p->GetData());
411 p->SetData(0);//BUG duda
416 int pos = cursor->getPos();
417 cursor->SetData(par);
418 cursor->goPosAbs(pos);
420 if (p && p->Permit(LMPF_ALLOW_CR)) {
424 if (t == LM_TC_TAB) {
425 MathParInset * p = cursor->p;
426 if (p && p->Permit(LMPF_ALLOW_TAB)) {
428 cursor->Insert(c, t);
431 cursor->goNextColumn();
432 } else // Navigate between arguments
433 if (p && p->GetType() == LM_OT_MACRO) {
434 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
435 p->setArgumentIdx(p->getArgumentIdx() + 1);
442 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
450 cursor->Insert(c, t);
459 void MathedCursor::Insert(MathedInset * p, int t)
461 if (macro_mode) MacroModeClose();
463 if (MathIsActive(t)) {
465 static_cast<MathParInset*>(p)->SetData(selarray);
470 if (mathstk.i < MAX_STACK_ITEMS - 1) {
472 if (accent && !MathIsActive(t)) {
475 cursor->Insert(p, t);
477 if (MathIsActive(t)) {
484 lyxerr << "Math error: Full stack." << endl;
488 void MathedCursor::Delete()
490 if (macro_mode) return;
495 if (cursor->Empty() && !mathstk.Empty()) {
496 cursor = mathstk.pop();
498 // if (cursor->GetChar()!= LM_TC_TAB)
504 void MathedCursor::DelLine()
506 if (macro_mode) MacroModeClose();
511 MathParInset *p= cursor->p;
512 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
518 bool MathedCursor::Up(bool sel)
522 if (macro_mode) MacroModeClose();
524 if (sel && !selection) SelStart();
525 if (!sel && selection) SelClear();
528 if (cursor->IsScript()) {
529 char cd = cursor->GetChar();
534 // A subscript may be followed by a superscript
537 if (MathIsUp(cursor->GetChar())) {
540 } else // return to the previous state
545 result = cursor->Up();
546 if (!result && cursor->p) {
547 MathParInset * p = cursor->p;
549 if (p->GetType() == LM_OT_SCRIPT) {
550 MathedXIter * cx = mathstk.Item(1);
551 bool is_down = (cx->GetChar() == LM_TC_DOWN);
552 cursor = mathstk.pop();
554 result = (is_down) ? true: Up();
556 result = (p->getArgumentIdx() > 0);
558 p->setArgumentIdx(p->getArgumentIdx() - 1);
562 if (!result && !mathstk.Empty()) {
563 cursor = mathstk.pop();
571 bool MathedCursor::Down(bool sel)
575 if (macro_mode) MacroModeClose();
577 if (sel && !selection) SelStart();
578 if (!sel && selection) SelClear();
579 // if (selection) SelClear();
581 if (cursor->IsScript()) {
582 char cd = cursor->GetChar();
583 if (MathIsDown(cd)) {
587 // A superscript may be followed by a subscript
590 if (MathIsDown(cursor->GetChar())) {
598 result = cursor->Down();
599 if (!result && cursor->p) {
600 MathParInset * p= cursor->p;
601 if (p->GetType() == LM_OT_SCRIPT) {
602 MathedXIter * cx = mathstk.Item(1);
603 bool is_up = (cx->GetChar() == LM_TC_UP);
604 cursor = mathstk.pop();
606 result = (is_up) ? true: Down();
608 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
610 p->setArgumentIdx(p->getArgumentIdx() + 1);
614 if (!result && !mathstk.Empty()) {
615 cursor = mathstk.pop();
623 bool MathedCursor::Limits()
625 if (cursor->IsInset()) {
626 MathedInset * p = cursor->GetInset();
627 bool ol = p->GetLimits();
629 return (ol!= p->GetLimits());
635 void MathedCursor::SetSize(short size)
637 MathParInset * p = cursor->p;
638 p->UserSetSize(size);
643 void MathedCursor::setLabel(string const & label)
644 { // ugly hack and possible bug
645 if (!cursor->setLabel(label))
646 lyxerr << "MathErr: Bad place to set labels." << endl;
650 void MathedCursor::setNumbered()
651 { // another ugly hack
652 MathedRowSt * crow = cursor->crow;
654 crow->setNumbered(!crow->isNumbered());
658 void MathedCursor::Interpret(string const & s)
662 MathedTextCodes tcode = LM_TC_INSET;
664 if (s[0] == '^' || s[0] == '_') {
665 char c = cursor->GetChar();
666 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
669 } else // A script may be followed by a script
670 if (MathIsUp(c) || MathIsDown(c)) {
673 c = cursor->GetChar();
674 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
680 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
681 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
684 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
685 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
686 p = new MathSpaceInset(sp);
693 p = MathMacroTable::mathMTable.getMacro(s);
695 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
697 p = new MathRootInset();
698 tcode = LM_TC_ACTIVE_INSET;
700 p = new MathFuncInset(s, LM_OT_UNDEF);
702 tcode = static_cast<MathMacro*>(p)->getTCode();
703 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
706 MathedInsetTypes fractype = LM_OT_FRAC;
710 p = new MathBigopInset(l->name, l->id);
716 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
717 LM_TC_BOPS: LM_TC_SYMB);
719 p = new MathFuncInset(l->name);
724 fractype = LM_OT_STACKREL;
725 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
728 p = new MathFracInset(fractype);
729 tcode = LM_TC_ACTIVE_INSET;
734 p = new MathSqrtInset;
735 tcode = LM_TC_ACTIVE_INSET;
740 p = new MathDecorationInset(l->id);
741 tcode = LM_TC_ACTIVE_INSET;
746 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
751 p = new MathSpaceInset(l->id);
756 p = new MathDotsInset(l->name, l->id);
763 p = MathMacroTable::mathMTable.getMacro(s);
764 tcode = static_cast<MathMacro*>(p)->getTCode();
765 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
769 p = new MathFuncInset(l->name);
781 bool MathedCursor::pullArg()
783 if (cursor->IsActive()) {
784 MathParInset * p = cursor->GetActiveInset();
788 MathedArray * a = p->GetData();
802 void MathedCursor::MacroModeOpen()
805 imacro = new MathFuncInset("");
809 lyxerr << "Mathed Warning: Already in macro mode" << endl;
813 void MathedCursor::MacroModeClose()
817 latexkeys * l = in_word_set(imacro->GetName());
818 if (!imacro->GetName().empty()
819 && (!l || (l && IsMacro(l->token, l->id))) &&
820 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
822 //imacro->SetName(macrobf);
823 // This guarantees that the string will be removed by destructor
824 imacro->SetType(LM_OT_UNDEF);
826 imacro->SetName(l->name);
829 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
830 setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
833 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
834 Interpret(imacro->GetName());
843 void MathedCursor::MacroModeBack()
846 if (!imacro->GetName().empty()) {
847 imacro->SetName(imacro->GetName().substr(0, imacro->GetName().length() - 1));
852 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
856 void MathedCursor::MacroModeInsert(char c)
859 imacro->SetName(imacro->GetName() + c);
862 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
866 void MathedCursor::SelCopy()
869 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
870 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
871 selarray = cursor->Copy(p1, p2);
878 void MathedCursor::SelCut()
881 if (cursor->pos == selpos) return;
883 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
884 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
885 selarray = cursor->Copy(p1, p2);
886 cursor->Clean(selpos);
893 void MathedCursor::SelDel()
895 // lyxerr << "Deleting sel "
897 if (cursor->pos == selpos) return;
898 cursor->Clean(selpos);
905 void MathedCursor::SelPaste()
907 // lyxerr << "paste " << selarray << " " << curor->pos;
908 if (selection) SelDel();
910 cursor->Merge(selarray);
916 void MathedCursor::SelStart()
918 lyxerr[Debug::MATHED] << "Starting sel " << endl;
920 selpos = cursor->pos;
921 selstk = new MathStackXIter(mathstk);
922 anchor = selstk->Item(-1);
923 anchor->SetData(cursor->p);
925 anchor->goPosAbs(selpos);
932 void MathedCursor::SelClear()
934 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
943 // Anchor position must be at the same level that stack.
944 void MathedCursor::SelBalance()
946 int d = mathstk.Level() - selstk->Level();
948 // If unbalanced, balance them
951 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
952 anchor = selstk->pop();
953 if (anchor->GetX() >= cursor->GetX())
956 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
959 d = mathstk.Level() - selstk->Level();
962 // Once balanced the levels, check that they are at the same paragraph
963 selpos = anchor->pos;
967 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
969 static int xpoint[10];
970 static int ypoint[10];
981 // single row selection
988 // Balance anchor and cursor
993 cursor->p->GetXY(xo, yo);
994 int w = cursor->p->Width();
997 cursor->GetPos(x1, y1);
998 cursor->getAD(a1, d1);
1001 anchor->GetPos(x, y);
1002 anchor->getAD(a, d);
1005 ypoint[i++] = y + d;
1007 ypoint[i++] = y - a;
1011 ypoint[i++] = y - a;
1015 ypoint[i++] = y1 - a;
1020 ypoint[i++] = y1 - a;
1022 ypoint[i++] = y1 + d;
1026 ypoint[i++] = y1 + d;
1029 ypoint[i++] = y + d;
1032 xpoint[i] = xpoint[0];
1033 ypoint[i++] = ypoint[0];
1038 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1039 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1040 // for (i = 0; i < np; ++i)
1041 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1046 void MathedCursor::setAccent(int ac)
1048 if (ac > 0 && accent < 8) {
1049 nestaccent[accent++] = ac;
1051 accent = 0; // consumed!
1055 int MathedCursor::getAccent() const
1057 return (accent > 0) ? nestaccent[accent - 1]: 0;
1061 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1063 MathedInset * ac = 0;
1065 for (int i = accent - 1; i >= 0; --i) {
1066 if (i == accent - 1)
1067 ac = new MathAccentInset(c, t, nestaccent[i]);
1069 ac = new MathAccentInset(ac, nestaccent[i]);
1074 accent = 0; // consumed!
1078 void MathedCursor::doAccent(MathedInset * p)
1080 MathedInset * ac = 0;
1082 for (int i = accent - 1; i >= 0; --i) {
1083 if (i == accent - 1)
1084 ac = new MathAccentInset(p, nestaccent[i]);
1086 ac = new MathAccentInset(ac, nestaccent[i]);
1091 accent = 0; // consumed!
1095 void MathedCursor::toggleLastCode(MathedTextCodes t)
1098 lastcode = LM_TC_VAR;