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"
34 static LyxArrayBase * selarray = 0;
38 // This was very smaller, I'll change it later
40 bool IsMacro(short tok, int id)
42 return (tok != LM_TK_STACK && tok != LM_TK_FRAC && tok != LM_TK_SQRT
44 && tok != LM_TK_SPACE && tok != LM_TK_DOTS
45 && tok != LM_TK_FUNCLIM
46 && tok != LM_TK_BIGSYM && tok != LM_TK_ACCENT &&
47 !(tok == LM_TK_SYM && id < 255));
50 static int const MAX_STACK_ITEMS = 32;
52 struct MathStackXIter {
56 MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
57 item = new MathedXIter[imax];
61 MathStackXIter(MathStackXIter & stk);
67 void push(MathedXIter ** a) {
76 MathedXIter * Item(int idx) {
77 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
85 return i >= MAX_STACK_ITEMS;
92 int Level() { return i; }
94 } mathstk, *selstk = 0;
97 MathStackXIter::MathStackXIter(MathStackXIter & stk)
100 item = new MathedXIter[imax];
102 for (int k = 0; k < i; ++k) {
103 item[k].SetData(stk.item[k].getPar());
105 item[k].goPosAbs(stk.item[k].getPos());
110 /***---------------- Mathed Cursor ---------------------------***/
112 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
116 lastcode = LM_TC_MIN;
119 if (!MathMacroTable::built)
120 MathMacroTable::mathMTable.builtinMacros();
124 void MathedCursor::SetPar(MathParInset * p)
129 selection = false; // not SelClear() ?
131 mathstk.push(&cursor);
133 cursor->SetData(par);
137 void MathedCursor::draw(Painter & pain, int x, int y)
139 // lyxerr << "Cursor[" << x << " " << y << "] ";
140 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
142 int w = par->Width() + 2;
143 int a = par->Ascent() + 1;
144 int h = par->Height() + 1;
145 if (par->GetType() > LM_OT_PAR) { a += 4; h += 8; }
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(), h = par->Height();
162 //mathed_set_font(LM_TC_VAR, 1);
163 pain.fillRectangle(x, y - par->Ascent(),
164 x + w, y - par->Ascent() + h,
166 par->draw(pain, x, y);
170 bool MathedCursor::Left(bool sel)
177 if (sel && !selection) SelStart();
178 if (!sel && selection) SelClear();
179 bool result = cursor->Prev();
180 if (!result && !mathstk.Empty()) {
181 cursor = mathstk.pop();
184 if (selection) SelClear();
186 if (result && cursor->IsActive()) {
187 if (cursor->IsScript()) {
189 if (!cursor->IsScript())
195 MathParInset * p = cursor->GetActiveInset();
199 p->setArgumentIdx(p->getMaxArgumentIdx());
200 mathstk.push(&cursor);
210 bool MathedCursor::Pop()
212 if (!mathstk.Empty()) {
213 cursor = mathstk.pop();
222 bool MathedCursor::Push()
224 if (cursor->IsActive()) {
225 MathParInset * p = cursor->GetActiveInset();
226 if (!p) return false;
227 mathstk.push(&cursor);
235 bool MathedCursor::Right(bool sel)
242 if (sel && !selection) SelStart();
243 if (!sel && selection) SelClear();
246 if (cursor->IsActive()) {
247 if (cursor->IsScript()) {
249 // A script may be followed by another script
250 if (cursor->IsScript())
255 MathParInset *p = cursor->GetActiveInset();
257 lyxerr << "Math error: Inset expected." << endl;
258 return cursor->Next();
260 p->setArgumentIdx(0);
261 mathstk.push(&cursor);
265 result = cursor->Next();
267 if (cursor->GetChar()!= LM_TC_CR)
268 result = cursor->Next();
269 if (!result && !mathstk.Empty()) {
270 cursor = mathstk.pop();
274 if (selection) SelClear();
281 void MathedCursor::SetPos(int x, int y)
285 if (macro_mode) MacroModeClose();
286 lastcode = LM_TC_MIN;
288 mathstk.push(&cursor);
289 cursor->SetData(par);
290 cursor->fitCoord(x, y);
291 while (cursor->GetX()<x && cursor->OK()) {
292 if (cursor->IsActive()) {
293 MathParInset * p = cursor->GetActiveInset();
294 if (p->Inside(x, y)) {
296 mathstk.push(&cursor);
298 cursor->fitCoord(x, y);
304 if (!cursor->Next() && !Pop())
307 if (x-xp < cursor->GetX()-x) cursor->ipop();
312 void MathedCursor::Home()
314 if (macro_mode) MacroModeClose();
317 mathstk.push(&cursor);
322 void MathedCursor::End()
324 if (macro_mode) MacroModeClose();
327 mathstk.push(&cursor);
332 MathMatrixInset * create_multiline(short int type, int cols)
343 for (int i = 0; i < cols; ++i)
347 case LM_OT_MULTLINEN:
349 align = "c"; // This is incorrect!
359 MathMatrixInset * mt = new MathMatrixInset(columns, -1);
360 mt->SetAlign(' ', align);
364 void MathedCursor::Insert(byte c, MathedTextCodes t)
366 if (selection) SelDel();
371 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
375 MathParInset * p = cursor->p;
376 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
377 short int type = LM_OT_MPAR;
379 if (c >= '1' && c <= '9') {
380 type = LM_OT_ALIGNAT;
383 type = LM_OT_MULTLINE;
387 if (p->GetType() == LM_OT_PARN)
389 MathMatrixInset * mt = create_multiline(type, cols);
390 mt->SetStyle(LM_ST_DISPLAY);
392 mt->SetData(p->GetData());
393 p->SetData(0);//BUG duda
398 int pos = cursor->getPos();
399 cursor->SetData(par);
400 cursor->goPosAbs(pos);
402 if (p && p->Permit(LMPF_ALLOW_CR)) {
406 if (t == LM_TC_TAB) {
407 MathParInset * p = cursor->p;
408 if (p && p->Permit(LMPF_ALLOW_TAB)) {
410 cursor->Insert(c, t);
413 cursor->goNextColumn();
414 } else // Navigate between arguments
415 if (p && p->GetType() == LM_OT_MACRO) {
416 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
417 p->setArgumentIdx(p->getArgumentIdx() + 1);
424 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
432 cursor->Insert(c, t);
441 void MathedCursor::Insert(MathedInset * p, int t)
443 if (macro_mode) MacroModeClose();
445 if (MathIsActive(t)) {
447 static_cast<MathParInset*>(p)->SetData(selarray);
452 if (mathstk.i < MAX_STACK_ITEMS - 1) {
454 if (accent && !MathIsActive(t)) {
457 cursor->Insert(p, t);
459 if (MathIsActive(t)) {
466 lyxerr << "Math error: Full stack." << endl;
470 void MathedCursor::Delete()
472 if (macro_mode) return;
477 if (cursor->Empty() && !mathstk.Empty()) {
478 cursor = mathstk.pop();
480 // if (cursor->GetChar()!= LM_TC_TAB)
486 void MathedCursor::DelLine()
488 if (macro_mode) MacroModeClose();
493 MathParInset *p= cursor->p;
494 if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
500 bool MathedCursor::Up(bool sel)
504 if (macro_mode) MacroModeClose();
506 if (sel && !selection) SelStart();
507 if (!sel && selection) SelClear();
510 if (cursor->IsScript()) {
511 char cd = cursor->GetChar();
516 // A subscript may be followed by a superscript
519 if (MathIsUp(cursor->GetChar())) {
522 } else // return to the previous state
527 result = cursor->Up();
528 if (!result && cursor->p) {
529 MathParInset * p = cursor->p;
531 if (p->GetType() == LM_OT_SCRIPT) {
532 MathedXIter * cx = mathstk.Item(1);
533 bool is_down = (cx->GetChar() == LM_TC_DOWN);
534 cursor = mathstk.pop();
536 result = (is_down) ? true: Up();
538 result = (p->getArgumentIdx() > 0);
540 p->setArgumentIdx(p->getArgumentIdx() - 1);
544 if (!result && !mathstk.Empty()) {
545 cursor = mathstk.pop();
553 bool MathedCursor::Down(bool sel)
557 if (macro_mode) MacroModeClose();
559 if (sel && !selection) SelStart();
560 if (!sel && selection) SelClear();
561 // if (selection) SelClear();
563 if (cursor->IsScript()) {
564 char cd = cursor->GetChar();
565 if (MathIsDown(cd)) {
569 // A superscript may be followed by a subscript
572 if (MathIsDown(cursor->GetChar())) {
580 result = cursor->Down();
581 if (!result && cursor->p) {
582 MathParInset * p= cursor->p;
583 if (p->GetType() == LM_OT_SCRIPT) {
584 MathedXIter * cx = mathstk.Item(1);
585 bool is_up = (cx->GetChar() == LM_TC_UP);
586 cursor = mathstk.pop();
588 result = (is_up) ? true: Down();
590 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
592 p->setArgumentIdx(p->getArgumentIdx() + 1);
596 if (!result && !mathstk.Empty()) {
597 cursor = mathstk.pop();
605 bool MathedCursor::Limits()
607 if (cursor->IsInset()) {
608 MathedInset * p = cursor->GetInset();
609 bool ol = p->GetLimits();
611 return (ol!= p->GetLimits());
617 void MathedCursor::SetSize(short size)
619 MathParInset * p = cursor->p;
620 p->UserSetSize(size);
625 void MathedCursor::setLabel(string const & label)
626 { // ugly hack and possible bug
627 if (!cursor->setLabel(label))
628 lyxerr << "MathErr: Bad place to set labels." << endl;
632 void MathedCursor::setNumbered()
633 { // another ugly hack
634 MathedRowSt * crow = cursor->crow;
636 crow->setNumbered(!crow->isNumbered());
640 void MathedCursor::Interpret(string const & s)
644 MathedTextCodes tcode = LM_TC_INSET;
646 if (s[0] == '^' || s[0] == '_') {
647 char c = cursor->GetChar();
648 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
651 } else // A script may be followed by a script
652 if (MathIsUp(c) || MathIsDown(c)) {
655 c = cursor->GetChar();
656 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
662 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
663 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
666 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
667 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
668 p = new MathSpaceInset(sp);
675 p = MathMacroTable::mathMTable.getMacro(s);
677 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
679 p = new MathRootInset();
680 tcode = LM_TC_ACTIVE_INSET;
682 p = new MathFuncInset(s, LM_OT_UNDEF);
684 tcode = static_cast<MathMacro*>(p)->getTCode();
685 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
688 MathedInsetTypes fractype = LM_OT_FRAC;
692 p = new MathBigopInset(l->name, l->id);
698 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
699 LM_TC_BOPS: LM_TC_SYMB);
701 p = new MathFuncInset(l->name);
706 fractype = LM_OT_STACKREL;
707 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
710 p = new MathFracInset(fractype);
711 tcode = LM_TC_ACTIVE_INSET;
716 p = new MathSqrtInset;
717 tcode = LM_TC_ACTIVE_INSET;
722 p = new MathDecorationInset(l->id);
723 tcode = LM_TC_ACTIVE_INSET;
728 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
733 p = new MathSpaceInset(l->id);
738 p = new MathDotsInset(l->name, l->id);
745 p = MathMacroTable::mathMTable.getMacro(s);
746 tcode = static_cast<MathMacro*>(p)->getTCode();
747 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
751 p = new MathFuncInset(l->name);
763 bool MathedCursor::pullArg()
765 if (cursor->IsActive()) {
766 MathParInset * p = cursor->GetActiveInset();
770 LyxArrayBase * a = p->GetData();
784 void MathedCursor::MacroModeOpen()
787 imacro = new MathFuncInset("");
791 lyxerr << "Mathed Warning: Already in macro mode" << endl;
795 void MathedCursor::MacroModeClose()
799 latexkeys * l = in_word_set(imacro->GetName());
800 if (!imacro->GetName().empty()
801 && (!l || (l && IsMacro(l->token, l->id))) &&
802 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
804 //imacro->SetName(macrobf);
805 // This guarantees that the string will be removed by destructor
806 imacro->SetType(LM_OT_UNDEF);
808 imacro->SetName(l->name);
811 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
812 setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
815 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
816 Interpret(imacro->GetName());
825 void MathedCursor::MacroModeBack()
828 if (!imacro->GetName().empty()) {
829 imacro->SetName(imacro->GetName().substr(0, imacro->GetName().length() - 1));
834 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
838 void MathedCursor::MacroModeInsert(char c)
841 imacro->SetName(imacro->GetName() + c);
844 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
848 void MathedCursor::SelCopy()
851 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
852 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
853 selarray = cursor->Copy(p1, p2);
860 void MathedCursor::SelCut()
863 if (cursor->pos == selpos) return;
865 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
866 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
867 selarray = cursor->Copy(p1, p2);
868 cursor->Clean(selpos);
875 void MathedCursor::SelDel()
877 // lyxerr << "Deleting sel "
879 if (cursor->pos == selpos) return;
880 cursor->Clean(selpos);
887 void MathedCursor::SelPaste()
889 // lyxerr << "paste " << selarray << " " << curor->pos;
890 if (selection) SelDel();
892 cursor->Merge(selarray);
898 void MathedCursor::SelStart()
900 lyxerr[Debug::MATHED] << "Starting sel " << endl;
902 selpos = cursor->pos;
903 selstk = new MathStackXIter(mathstk);
904 anchor = selstk->Item(-1);
905 anchor->SetData(cursor->p);
907 anchor->goPosAbs(selpos);
914 void MathedCursor::SelClear()
916 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
925 // Anchor position must be at the same level that stack.
926 void MathedCursor::SelBalance()
928 int d = mathstk.Level() - selstk->Level();
930 // If unbalanced, balance them
933 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
934 anchor = selstk->pop();
935 if (anchor->GetX() >= cursor->GetX())
938 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
941 d = mathstk.Level() - selstk->Level();
944 // Once balanced the levels, check that they are at the same paragraph
945 selpos = anchor->pos;
949 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
951 static int xpoint[10];
952 static int ypoint[10];
963 // single row selection
970 // Balance anchor and cursor
975 cursor->p->GetXY(xo, yo);
976 int w = cursor->p->Width();
979 cursor->GetPos(x1, y1);
980 cursor->getAD(a1, d1);
983 anchor->GetPos(x, y);
997 ypoint[i++] = y1 - a;
1002 ypoint[i++] = y1 - a;
1004 ypoint[i++] = y1 + d;
1008 ypoint[i++] = y1 + d;
1011 ypoint[i++] = y + d;
1014 xpoint[i] = xpoint[0];
1015 ypoint[i++] = ypoint[0];
1020 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1021 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1022 // for (i = 0; i < np; ++i)
1023 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1028 void MathedCursor::setAccent(int ac)
1030 if (ac > 0 && accent < 8) {
1031 nestaccent[accent++] = ac;
1033 accent = 0; // consumed!
1037 int MathedCursor::getAccent() const
1039 return (accent > 0) ? nestaccent[accent - 1]: 0;
1043 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1045 MathedInset * ac = 0;
1047 for (int i = accent - 1; i >= 0; --i) {
1048 if (i == accent - 1)
1049 ac = new MathAccentInset(c, t, nestaccent[i]);
1051 ac = new MathAccentInset(ac, nestaccent[i]);
1056 accent = 0; // consumed!
1060 void MathedCursor::doAccent(MathedInset * p)
1062 MathedInset * ac = 0;
1064 for (int i = accent - 1; i >= 0; --i) {
1065 if (i == accent - 1)
1066 ac = new MathAccentInset(p, nestaccent[i]);
1068 ac = new MathAccentInset(ac, nestaccent[i]);
1073 accent = 0; // consumed!
1077 void MathedCursor::toggleLastCode(MathedTextCodes t)
1080 lastcode = LM_TC_VAR;