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 void MathedCursor::Insert(byte c, MathedTextCodes t)
334 if (selection) SelDel();
339 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
343 MathParInset * p = cursor->p;
344 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
345 MathMatrixInset * mt = new MathMatrixInset(3, 0);
346 mt->SetAlign(' ', "rcl");
347 mt->SetStyle(LM_ST_DISPLAY);
348 mt->SetType((p->GetType() == LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
349 mt->SetData(p->GetData());
350 p->SetData(0);//BUG duda
355 int pos = cursor->getPos();
356 cursor->SetData(par);
357 cursor->goPosAbs(pos);
359 if (p && p->Permit(LMPF_ALLOW_CR)) {
363 if (t == LM_TC_TAB) {
364 MathParInset * p = cursor->p;
365 if (p && p->Permit(LMPF_ALLOW_TAB)) {
367 cursor->Insert(c, t);
370 cursor->goNextColumn();
371 } else // Navigate between arguments
372 if (p && p->GetType() == LM_OT_MACRO) {
373 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
374 p->setArgumentIdx(p->getArgumentIdx() + 1);
381 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
389 cursor->Insert(c, t);
398 void MathedCursor::Insert(MathedInset * p, int t)
400 if (macro_mode) MacroModeClose();
402 if (MathIsActive(t)) {
404 static_cast<MathParInset*>(p)->SetData(selarray);
409 if (mathstk.i < MAX_STACK_ITEMS - 1) {
411 if (accent && !MathIsActive(t)) {
414 cursor->Insert(p, t);
416 if (MathIsActive(t)) {
423 lyxerr << "Math error: Full stack." << endl;
427 void MathedCursor::Delete()
429 if (macro_mode) return;
434 if (cursor->Empty() && !mathstk.Empty()) {
435 cursor = mathstk.pop();
437 // if (cursor->GetChar()!= LM_TC_TAB)
443 void MathedCursor::DelLine()
445 if (macro_mode) MacroModeClose();
450 MathParInset *p= cursor->p;
451 if (p && (p->GetType()<= LM_OT_MATRIX && p->GetType()>= LM_OT_MPAR)) {
457 bool MathedCursor::Up(bool sel)
461 if (macro_mode) MacroModeClose();
463 if (sel && !selection) SelStart();
464 if (!sel && selection) SelClear();
467 if (cursor->IsScript()) {
468 char cd = cursor->GetChar();
473 // A subscript may be followed by a superscript
476 if (MathIsUp(cursor->GetChar())) {
479 } else // return to the previous state
484 result = cursor->Up();
485 if (!result && cursor->p) {
486 MathParInset * p = cursor->p;
488 if (p->GetType() == LM_OT_SCRIPT) {
489 MathedXIter * cx = mathstk.Item(1);
490 bool is_down = (cx->GetChar() == LM_TC_DOWN);
491 cursor = mathstk.pop();
493 result = (is_down) ? true: Up();
495 result = (p->getArgumentIdx() > 0);
497 p->setArgumentIdx(p->getArgumentIdx() - 1);
501 if (!result && !mathstk.Empty()) {
502 cursor = mathstk.pop();
510 bool MathedCursor::Down(bool sel)
514 if (macro_mode) MacroModeClose();
516 if (sel && !selection) SelStart();
517 if (!sel && selection) SelClear();
518 // if (selection) SelClear();
520 if (cursor->IsScript()) {
521 char cd = cursor->GetChar();
522 if (MathIsDown(cd)) {
526 // A superscript may be followed by a subscript
529 if (MathIsDown(cursor->GetChar())) {
537 result = cursor->Down();
538 if (!result && cursor->p) {
539 MathParInset * p= cursor->p;
540 if (p->GetType() == LM_OT_SCRIPT) {
541 MathedXIter * cx = mathstk.Item(1);
542 bool is_up = (cx->GetChar() == LM_TC_UP);
543 cursor = mathstk.pop();
545 result = (is_up) ? true: Down();
547 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
549 p->setArgumentIdx(p->getArgumentIdx() + 1);
553 if (!result && !mathstk.Empty()) {
554 cursor = mathstk.pop();
562 bool MathedCursor::Limits()
564 if (cursor->IsInset()) {
565 MathedInset * p = cursor->GetInset();
566 bool ol = p->GetLimits();
568 return (ol!= p->GetLimits());
574 void MathedCursor::SetSize(short size)
576 MathParInset * p = cursor->p;
577 p->UserSetSize(size);
582 void MathedCursor::setLabel(string const & label)
583 { // ugly hack and possible bug
584 if (!cursor->setLabel(label))
585 lyxerr << "MathErr: Bad place to set labels." << endl;
589 void MathedCursor::setNumbered()
590 { // another ugly hack
591 MathedRowSt * crow = cursor->crow;
593 crow->setNumbered(!crow->isNumbered());
597 void MathedCursor::Interpret(string const & s)
601 MathedTextCodes tcode = LM_TC_INSET;
603 if (s[0] == '^' || s[0] == '_') {
604 char c = cursor->GetChar();
605 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
608 } else // A script may be followed by a script
609 if (MathIsUp(c) || MathIsDown(c)) {
612 c = cursor->GetChar();
613 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
619 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
620 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
623 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
624 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
625 p = new MathSpaceInset(sp);
632 p = MathMacroTable::mathMTable.getMacro(s);
634 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
636 p = new MathRootInset();
637 tcode = LM_TC_ACTIVE_INSET;
639 p = new MathFuncInset(s, LM_OT_UNDEF);
641 tcode = static_cast<MathMacro*>(p)->getTCode();
642 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
645 MathedInsetTypes fractype = LM_OT_FRAC;
649 p = new MathBigopInset(l->name, l->id);
655 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
656 LM_TC_BOPS: LM_TC_SYMB);
658 p = new MathFuncInset(l->name);
663 fractype = LM_OT_STACKREL;
664 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
667 p = new MathFracInset(fractype);
668 tcode = LM_TC_ACTIVE_INSET;
673 p = new MathSqrtInset;
674 tcode = LM_TC_ACTIVE_INSET;
679 p = new MathDecorationInset(l->id);
680 tcode = LM_TC_ACTIVE_INSET;
685 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
690 p = new MathSpaceInset(l->id);
695 p = new MathDotsInset(l->name, l->id);
702 p = MathMacroTable::mathMTable.getMacro(s);
703 tcode = static_cast<MathMacro*>(p)->getTCode();
704 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
708 p = new MathFuncInset(l->name);
720 bool MathedCursor::pullArg()
722 if (cursor->IsActive()) {
723 MathParInset * p = cursor->GetActiveInset();
727 LyxArrayBase * a = p->GetData();
741 void MathedCursor::MacroModeOpen()
744 imacro = new MathFuncInset("");
748 lyxerr << "Mathed Warning: Already in macro mode" << endl;
752 void MathedCursor::MacroModeClose()
756 latexkeys * l = in_word_set(imacro->GetName());
757 if (!imacro->GetName().empty()
758 && (!l || (l && IsMacro(l->token, l->id))) &&
759 !MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
761 //imacro->SetName(macrobf);
762 // This guarantees that the string will be removed by destructor
763 imacro->SetType(LM_OT_UNDEF);
765 imacro->SetName(l->name);
768 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
769 setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
772 if (l || MathMacroTable::mathMTable.getMacro(imacro->GetName())) {
773 Interpret(imacro->GetName());
782 void MathedCursor::MacroModeBack()
785 if (!imacro->GetName().empty()) {
786 imacro->SetName(imacro->GetName().substr(0, imacro->GetName().length() - 1));
791 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
795 void MathedCursor::MacroModeInsert(char c)
798 imacro->SetName(imacro->GetName() + c);
801 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
805 void MathedCursor::SelCopy()
808 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
809 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
810 selarray = cursor->Copy(p1, p2);
817 void MathedCursor::SelCut()
820 if (cursor->pos == selpos) return;
822 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
823 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
824 selarray = cursor->Copy(p1, p2);
825 cursor->Clean(selpos);
832 void MathedCursor::SelDel()
834 // lyxerr << "Deleting sel "
836 if (cursor->pos == selpos) return;
837 cursor->Clean(selpos);
844 void MathedCursor::SelPaste()
846 // lyxerr << "paste " << selarray << " " << curor->pos;
847 if (selection) SelDel();
849 cursor->Merge(selarray);
855 void MathedCursor::SelStart()
857 lyxerr[Debug::MATHED] << "Starting sel " << endl;
859 selpos = cursor->pos;
860 selstk = new MathStackXIter(mathstk);
861 anchor = selstk->Item(-1);
862 anchor->SetData(cursor->p);
864 anchor->goPosAbs(selpos);
871 void MathedCursor::SelClear()
873 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
882 // Anchor position must be at the same level that stack.
883 void MathedCursor::SelBalance()
885 int d = mathstk.Level() - selstk->Level();
887 // If unbalanced, balance them
890 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
891 anchor = selstk->pop();
892 if (anchor->GetX() >= cursor->GetX())
895 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
898 d = mathstk.Level() - selstk->Level();
901 // Once balanced the levels, check that they are at the same paragraph
902 selpos = anchor->pos;
906 void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
908 static int xpoint[10];
909 static int ypoint[10];
920 // single row selection
927 // Balance anchor and cursor
932 cursor->p->GetXY(xo, yo);
933 int w = cursor->p->Width();
936 cursor->GetPos(x1, y1);
937 cursor->getAD(a1, d1);
940 anchor->GetPos(x, y);
954 ypoint[i++] = y1 - a;
959 ypoint[i++] = y1 - a;
961 ypoint[i++] = y1 + d;
965 ypoint[i++] = y1 + d;
971 xpoint[i] = xpoint[0];
972 ypoint[i++] = ypoint[0];
977 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
978 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
979 // for (i = 0; i < np; ++i)
980 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
985 void MathedCursor::setAccent(int ac)
987 if (ac > 0 && accent < 8) {
988 nestaccent[accent++] = ac;
990 accent = 0; // consumed!
994 int MathedCursor::getAccent() const
996 return (accent > 0) ? nestaccent[accent - 1]: 0;
1000 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1002 MathedInset * ac = 0;
1004 for (int i = accent - 1; i >= 0; --i) {
1005 if (i == accent - 1)
1006 ac = new MathAccentInset(c, t, nestaccent[i]);
1008 ac = new MathAccentInset(ac, nestaccent[i]);
1013 accent = 0; // consumed!
1017 void MathedCursor::doAccent(MathedInset * p)
1019 MathedInset * ac = 0;
1021 for (int i = accent - 1; i >= 0; --i) {
1022 if (i == accent - 1)
1023 ac = new MathAccentInset(p, nestaccent[i]);
1025 ac = new MathAccentInset(ac, nestaccent[i]);
1030 accent = 0; // consumed!