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;
41 return ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z');
44 // This was very smaller, I'll change it later
46 bool IsMacro(short tok, int id)
48 return (tok != LM_TK_STACK && tok != LM_TK_FRAC && tok != LM_TK_SQRT
50 && tok != LM_TK_SPACE && tok != LM_TK_DOTS
51 && tok != LM_TK_FUNCLIM
52 && tok != LM_TK_BIGSYM && tok != LM_TK_ACCENT &&
53 !(tok == LM_TK_SYM && id < 255));
56 static int const MAX_STACK_ITEMS = 32;
58 struct MathStackXIter {
62 MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
63 item = new MathedXIter[imax];
67 MathStackXIter(MathStackXIter & stk);
73 void push(MathedXIter ** a) {
82 MathedXIter * Item(int idx) {
83 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
91 return i >= MAX_STACK_ITEMS;
98 int Level() { return i; }
100 } mathstk, *selstk = 0;
103 MathStackXIter::MathStackXIter(MathStackXIter & stk)
106 item = new MathedXIter[imax];
108 for (int k = 0; k < i; ++k) {
109 item[k].SetData(stk.item[k].getPar());
111 item[k].goPosAbs(stk.item[k].getPos());
116 /***---------------- Mathed Cursor ---------------------------***/
118 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
122 lastcode = LM_TC_MIN;
125 if (!MathMacroTable::built)
126 MathMacroTable::mathMTable.builtinMacros();
130 void MathedCursor::SetPar(MathParInset * p)
135 selection = false; // not SelClear() ?
137 mathstk.push(&cursor);
139 cursor->SetData(par);
143 void MathedCursor::draw(Painter & pain, int x, int y)
145 // lyxerr << "Cursor[" << x << " " << y << "] ";
146 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
148 int w = par->Width() + 2;
149 int a = par->Ascent() + 1;
150 int h = par->Height() + 1;
151 if (par->GetType() > LM_OT_PAR) { a += 4; h += 8; }
153 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
155 par->draw(pain, x, y);
160 void MathedCursor::Redraw(Painter & pain)
162 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
164 int w = par->Width(), h = par->Height();
168 //mathed_set_font(LM_TC_VAR, 1);
169 pain.fillRectangle(x, y - par->Ascent(),
170 x + w, y - par->Ascent() + h,
172 par->draw(pain, x, y);
176 bool MathedCursor::Left(bool sel)
183 if (sel && !selection) SelStart();
184 if (!sel && selection) SelClear();
185 bool result = cursor->Prev();
186 if (!result && !mathstk.Empty()) {
187 cursor = mathstk.pop();
190 if (selection) SelClear();
192 if (result && cursor->IsActive()) {
193 if (cursor->IsScript()) {
195 if (!cursor->IsScript())
201 MathParInset * p = cursor->GetActiveInset();
205 p->setArgumentIdx(p->getMaxArgumentIdx());
206 mathstk.push(&cursor);
216 bool MathedCursor::Pop()
218 if (!mathstk.Empty()) {
219 cursor = mathstk.pop();
228 bool MathedCursor::Push()
230 if (cursor->IsActive()) {
231 MathParInset * p = cursor->GetActiveInset();
232 if (!p) return false;
233 mathstk.push(&cursor);
241 bool MathedCursor::Right(bool sel)
248 if (sel && !selection) SelStart();
249 if (!sel && selection) SelClear();
252 if (cursor->IsActive()) {
253 if (cursor->IsScript()) {
255 // A script may be followed by another script
256 if (cursor->IsScript())
261 MathParInset *p = cursor->GetActiveInset();
263 lyxerr << "Math error: Inset expected." << endl;
264 return cursor->Next();
266 p->setArgumentIdx(0);
267 mathstk.push(&cursor);
271 result = cursor->Next();
273 if (cursor->GetChar()!= LM_TC_CR)
274 result = cursor->Next();
275 if (!result && !mathstk.Empty()) {
276 cursor = mathstk.pop();
280 if (selection) SelClear();
287 void MathedCursor::SetPos(int x, int y)
291 if (macro_mode) MacroModeClose();
292 lastcode = LM_TC_MIN;
294 mathstk.push(&cursor);
295 cursor->SetData(par);
296 cursor->fitCoord(x, y);
297 while (cursor->GetX()<x && cursor->OK()) {
298 if (cursor->IsActive()) {
299 MathParInset * p = cursor->GetActiveInset();
300 if (p->Inside(x, y)) {
302 mathstk.push(&cursor);
304 cursor->fitCoord(x, y);
310 if (!cursor->Next() && !Pop())
313 if (x-xp < cursor->GetX()-x) cursor->ipop();
318 void MathedCursor::Home()
320 if (macro_mode) MacroModeClose();
323 mathstk.push(&cursor);
328 void MathedCursor::End()
330 if (macro_mode) MacroModeClose();
333 mathstk.push(&cursor);
338 void MathedCursor::Insert(byte c, MathedTextCodes t)
340 if (selection) SelDel();
345 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
349 MathParInset * p = cursor->p;
350 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
351 MathMatrixInset * mt = new MathMatrixInset(3, 0);
352 mt->SetAlign(' ', "rcl");
353 mt->SetStyle(LM_ST_DISPLAY);
354 mt->SetType((p->GetType() == LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
355 mt->SetData(p->GetData());
356 p->SetData(0);//BUG duda
361 int pos = cursor->getPos();
362 cursor->SetData(par);
363 cursor->goPosAbs(pos);
365 if (p && p->Permit(LMPF_ALLOW_CR)) {
369 if (t == LM_TC_TAB) {
370 MathParInset * p = cursor->p;
371 if (p && p->Permit(LMPF_ALLOW_TAB)) {
373 cursor->Insert(c, t);
376 cursor->goNextColumn();
377 } else // Navigate between arguments
378 if (p && p->GetType() == LM_OT_MACRO) {
379 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
380 p->setArgumentIdx(p->getArgumentIdx() + 1);
387 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
395 cursor->Insert(c, t);
404 void MathedCursor::Insert(MathedInset * p, int t)
406 if (macro_mode) MacroModeClose();
408 if (MathIsActive(t)) {
410 static_cast<MathParInset*>(p)->SetData(selarray);
415 if (mathstk.i < MAX_STACK_ITEMS - 1) {
417 if (accent && !MathIsActive(t)) {
420 cursor->Insert(p, t);
422 if (MathIsActive(t)) {
429 lyxerr << "Math error: Full stack." << endl;
433 void MathedCursor::Delete()
435 if (macro_mode) return;
440 if (cursor->Empty() && !mathstk.Empty()) {
441 cursor = mathstk.pop();
443 // if (cursor->GetChar()!= LM_TC_TAB)
449 void MathedCursor::DelLine()
451 if (macro_mode) MacroModeClose();
456 MathParInset *p= cursor->p;
457 if (p && (p->GetType()<= LM_OT_MATRIX && p->GetType()>= LM_OT_MPAR)) {
463 bool MathedCursor::Up(bool sel)
467 if (macro_mode) MacroModeClose();
469 if (sel && !selection) SelStart();
470 if (!sel && selection) SelClear();
473 if (cursor->IsScript()) {
474 char cd = cursor->GetChar();
479 // A subscript may be followed by a superscript
482 if (MathIsUp(cursor->GetChar())) {
485 } else // return to the previous state
490 result = cursor->Up();
491 if (!result && cursor->p) {
492 MathParInset * p = cursor->p;
494 if (p->GetType() == LM_OT_SCRIPT) {
495 MathedXIter * cx = mathstk.Item(1);
496 bool is_down = (cx->GetChar() == LM_TC_DOWN);
497 cursor = mathstk.pop();
499 result = (is_down) ? true: Up();
501 result = (p->getArgumentIdx() > 0);
503 p->setArgumentIdx(p->getArgumentIdx() - 1);
507 if (!result && !mathstk.Empty()) {
508 cursor = mathstk.pop();
516 bool MathedCursor::Down(bool sel)
520 if (macro_mode) MacroModeClose();
522 if (sel && !selection) SelStart();
523 if (!sel && selection) SelClear();
524 // if (selection) SelClear();
526 if (cursor->IsScript()) {
527 char cd = cursor->GetChar();
528 if (MathIsDown(cd)) {
532 // A superscript may be followed by a subscript
535 if (MathIsDown(cursor->GetChar())) {
543 result = cursor->Down();
544 if (!result && cursor->p) {
545 MathParInset * p= cursor->p;
546 if (p->GetType() == LM_OT_SCRIPT) {
547 MathedXIter * cx = mathstk.Item(1);
548 bool is_up = (cx->GetChar() == LM_TC_UP);
549 cursor = mathstk.pop();
551 result = (is_up) ? true: Down();
553 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
555 p->setArgumentIdx(p->getArgumentIdx() + 1);
559 if (!result && !mathstk.Empty()) {
560 cursor = mathstk.pop();
568 bool MathedCursor::Limits()
570 if (cursor->IsInset()) {
571 MathedInset * p = cursor->GetInset();
572 bool ol = p->GetLimits();
574 return (ol!= p->GetLimits());
580 void MathedCursor::SetSize(short size)
582 MathParInset * p = cursor->p;
583 p->UserSetSize(size);
588 void MathedCursor::setLabel(string const & label)
589 { // ugly hack and possible bug
590 if (!cursor->setLabel(label))
591 lyxerr << "MathErr: Bad place to set labels." << endl;
595 void MathedCursor::setNumbered()
596 { // another ugly hack
597 MathedRowSt * crow = cursor->crow;
599 crow->setNumbered(!crow->isNumbered());
603 void MathedCursor::Interpret(string const & s)
607 MathedTextCodes tcode = LM_TC_INSET;
609 if (s[0] == '^' || s[0] == '_') {
610 char c = cursor->GetChar();
611 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
614 } else // A script may be followed by a script
615 if (MathIsUp(c) || MathIsDown(c)) {
618 c = cursor->GetChar();
619 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
625 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
626 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
629 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
630 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
631 p = new MathSpaceInset(sp);
638 p = MathMacroTable::mathMTable.getMacro(s);
640 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
642 p = new MathRootInset();
643 tcode = LM_TC_ACTIVE_INSET;
645 p = new MathFuncInset(s, LM_OT_UNDEF);
647 tcode = static_cast<MathMacro*>(p)->getTCode();
648 lyxerr << "Macro2 " << s << ' ' << tcode << " " ;
651 MathedInsetTypes fractype = LM_OT_FRAC;
655 p = new MathBigopInset(l->name, l->id);
661 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
662 LM_TC_BOPS: LM_TC_SYMB);
664 p = new MathFuncInset(l->name);
669 fractype = LM_OT_STACKREL;
670 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
673 p = new MathFracInset(fractype);
674 tcode = LM_TC_ACTIVE_INSET;
679 p = new MathSqrtInset;
680 tcode = LM_TC_ACTIVE_INSET;
685 p = new MathDecorationInset(l->id);
686 tcode = LM_TC_ACTIVE_INSET;
691 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
696 p = new MathSpaceInset(l->id);
701 p = new MathDotsInset(l->name, l->id);
708 p = MathMacroTable::mathMTable.getMacro(s);
709 tcode = static_cast<MathMacro*>(p)->getTCode();
710 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
714 p = new MathFuncInset(l->name);
726 bool MathedCursor::pullArg()
728 if (cursor->IsActive()) {
729 MathParInset * p = cursor->GetActiveInset();
733 LyxArrayBase * a = p->GetData();
747 void MathedCursor::MacroModeOpen()
750 imacro = new MathFuncInset("");
754 lyxerr << "Mathed Warning: Already in macro mode" << endl;
758 void MathedCursor::MacroModeClose()
762 latexkeys * l = in_word_set(imacro->GetName());
763 if (!imacro->GetName().empty()
764 && (!l || (l && IsMacro(l->token, l->id))) &&
765 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
767 //imacro->SetName(macrobf);
768 // This guarantees that the string will be removed by destructor
769 imacro->SetType(LM_OT_UNDEF);
771 imacro->SetName(l->name);
774 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
775 setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
778 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
779 Interpret(imacro->GetName());
788 void MathedCursor::MacroModeBack()
791 if (!imacro->GetName().empty()) {
792 imacro->SetName(imacro->GetName().substr(0, imacro->GetName().length() - 1));
797 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
801 void MathedCursor::MacroModeInsert(char c)
804 imacro->SetName(imacro->GetName() + c);
807 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
811 void MathedCursor::SelCopy()
814 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
815 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
816 selarray = cursor->Copy(p1, p2);
823 void MathedCursor::SelCut()
826 if (cursor->pos == selpos) return;
828 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
829 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
830 selarray = cursor->Copy(p1, p2);
831 cursor->Clean(selpos);
838 void MathedCursor::SelDel()
840 // lyxerr << "Deleting sel "
842 if (cursor->pos == selpos) return;
843 cursor->Clean(selpos);
850 void MathedCursor::SelPaste()
852 // lyxerr << "paste " << selarray << " " << curor->pos;
853 if (selection) SelDel();
855 cursor->Merge(selarray);
861 void MathedCursor::SelStart()
863 lyxerr[Debug::MATHED] << "Starting sel " << endl;
865 selpos = cursor->pos;
866 selstk = new MathStackXIter(mathstk);
867 anchor = selstk->Item(-1);
868 anchor->SetData(cursor->p);
870 anchor->goPosAbs(selpos);
877 void MathedCursor::SelClear()
879 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
888 // Anchor position must be at the same level that stack.
889 void MathedCursor::SelBalance()
891 int d = mathstk.Level() - selstk->Level();
893 // If unbalanced, balance them
896 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
897 anchor = selstk->pop();
898 if (anchor->GetX() >= cursor->GetX())
901 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
904 d = mathstk.Level() - selstk->Level();
907 // Once balanced the levels, check that they are at the same paragraph
908 selpos = anchor->pos;
912 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
914 static int xpoint[10];
915 static int ypoint[10];
926 // single row selection
933 // Balance anchor and cursor
938 cursor->p->GetXY(xo, yo);
939 int w = cursor->p->Width();
942 cursor->GetPos(x1, y1);
943 cursor->getAD(a1, d1);
946 anchor->GetPos(x, y);
960 ypoint[i++] = y1 - a;
965 ypoint[i++] = y1 - a;
967 ypoint[i++] = y1 + d;
971 ypoint[i++] = y1 + d;
977 xpoint[i] = xpoint[0];
978 ypoint[i++] = ypoint[0];
983 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
984 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
985 // for (i = 0; i < np; ++i)
986 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
991 void MathedCursor::setAccent(int ac)
993 if (ac > 0 && accent < 8) {
994 nestaccent[accent++] = ac;
996 accent = 0; // consumed!
1000 int MathedCursor::getAccent() const
1002 return (accent > 0) ? nestaccent[accent - 1]: 0;
1006 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1008 MathedInset * ac = 0;
1010 for (int i = accent - 1; i >= 0; --i) {
1011 if (i == accent - 1)
1012 ac = new MathAccentInset(c, t, nestaccent[i]);
1014 ac = new MathAccentInset(ac, nestaccent[i]);
1019 accent = 0; // consumed!
1023 void MathedCursor::doAccent(MathedInset * p)
1025 MathedInset * ac = 0;
1027 for (int i = accent - 1; i >= 0; --i) {
1028 if (i == accent - 1)
1029 ac = new MathAccentInset(p, nestaccent[i]);
1031 ac = new MathAccentInset(ac, nestaccent[i]);
1036 accent = 0; // consumed!