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: (c) 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"
32 extern void mathed_set_font(short type, int style);
34 extern GC canvasGC, mathGC, latexGC, cursorGC, mathFrameGC;
36 static LyxArrayBase *selarray=0;
38 inline bool IsAlpha(char c)
40 return ('A' <= c && c<='Z' || 'a' <= c && c<='z');
43 // This was very smaller, I'll change it later
44 inline bool IsMacro(short token, int id)
46 return (token!=LM_TK_STACK && token!=LM_TK_FRAC && token!=LM_TK_SQRT && token!=LM_TK_WIDE &&
47 token!=LM_TK_SPACE && token!=LM_TK_DOTS && token!=LM_TK_FUNCLIM &&
48 token!=LM_TK_BIGSYM && token!=LM_TK_ACCENT &&
49 !(token==LM_TK_SYM && id<255));
52 // Yes, mathed isn't using string yet.
53 inline char *strnew(char const* s)
55 char *s1 = new char[strlen(s)+1];
62 #define MAX_STACK_ITEMS 32
64 struct MathStackXIter {
69 MathStackXIter(int n=MAX_STACK_ITEMS): imax(n) {
70 item = new MathedXIter[imax];
74 MathStackXIter(MathStackXIter &stk);
80 void push(MathedXIter** a) {
89 MathedXIter* Item(int idx) {
90 return (idx+1 <= i) ? &item[i-idx-1]: (MathedXIter*)0;
98 return (i>=MAX_STACK_ITEMS);
105 int Level() { return i; }
107 } mathstk, *selstk=0;
110 MathStackXIter::MathStackXIter(MathStackXIter &stk) {
112 item = new MathedXIter[imax];
114 for (int k=0; k<i; k++) {
115 item[k].SetData(stk.item[k].getPar());
117 item[k].goPosAbs(stk.item[k].getPos());
122 /***---------------- Mathed Cursor ---------------------------***/
124 MathedCursor::MathedCursor(MathParInset *p) // : par(p)
128 lastcode = LM_TC_MIN;
131 if (!MathMacroTable::built)
132 MathMacroTable::mathMTable.builtinMacros();
136 void MathedCursor::SetPar(MathParInset *p)
141 selection = false; // not SelClear() ?
143 mathstk.push(&cursor);
145 cursor->SetData(par);
148 void MathedCursor::Draw(long unsigned pm, int x, int y)
150 // fprintf(stderr, "Cursor[%d %d] ", x, y);
151 win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
153 int w = par->Width()+2, a = par->Ascent()+1, h = par->Height()+1;
154 if (par->GetType()>LM_OT_PAR) { a += 4; h += 8; }
156 if (!canvasGC) mathed_set_font(LM_TC_VAR, 1);
157 // XFillRectangle(fl_display,pm, canvasGC, x, y-a, w, h);
158 XDrawRectangle(fl_display,pm, mathFrameGC, x-1, y-a, w, h);
160 MathParInset::pm = pm;
166 void MathedCursor::Redraw()
168 lyxerr.debug("Mathed: Redrawing!", Error::MATHED);
170 int w = par->Width(), h = par->Height();
173 mathed_set_font(LM_TC_VAR, 1);
174 XFillRectangle(fl_display, win,canvasGC,x, y-par->Ascent(), w, h);
176 MathParInset::pm = win;
180 bool MathedCursor::Left(bool sel)
187 if (sel && !selection) SelStart();
188 if (!sel && selection) SelClear();
189 bool result = cursor->Prev();
190 if (!result && !mathstk.Empty()) {
191 cursor = mathstk.pop();
194 if (selection) SelClear();
196 if (result && cursor->IsActive()) {
197 if (cursor->IsScript()) {
199 if (!cursor->IsScript())
205 MathParInset *p = cursor->GetActiveInset();
209 p->setArgumentIdx(p->getMaxArgumentIdx());
210 mathstk.push(&cursor);
219 bool MathedCursor::Pop()
221 if (!mathstk.Empty()) {
222 cursor = mathstk.pop();
230 bool MathedCursor::Push()
232 if (cursor->IsActive()) {
233 MathParInset *p = cursor->GetActiveInset();
234 if (!p) return false;
235 mathstk.push(&cursor);
242 bool MathedCursor::Right(bool sel)
249 if (sel && !selection) SelStart();
250 if (!sel && selection) SelClear();
253 if (cursor->IsActive()) {
254 if (cursor->IsScript()) {
256 // A script may be followed by another script
257 if (cursor->IsScript())
262 MathParInset *p = cursor->GetActiveInset();
264 fprintf(stderr, "Math error: Inset expected.\n");
265 return cursor->Next();
267 p->setArgumentIdx(0);
268 mathstk.push(&cursor);
272 result = cursor->Next();
274 if (cursor->GetChar()!=LM_TC_CR)
275 result = cursor->Next();
276 if (!result && !mathstk.Empty()) {
277 cursor = mathstk.pop();
281 if (selection) SelClear();
288 void MathedCursor::SetPos(int x, int y)
292 if (macro_mode) MacroModeClose();
293 lastcode = LM_TC_MIN;
295 mathstk.push(&cursor);
296 cursor->SetData(par);
297 cursor->fitCoord(x, y);
298 while (cursor->GetX()<x && cursor->OK()) {
299 if (cursor->IsActive()) {
300 MathParInset *p = cursor->GetActiveInset();
301 if (p->Inside(x, y)) {
303 mathstk.push(&cursor);
305 cursor->fitCoord(x, y);
311 if (!cursor->Next() && !Pop())
314 if (x-xp < cursor->GetX()-x) cursor->ipop();
319 void MathedCursor::Home()
321 if (macro_mode) MacroModeClose();
324 mathstk.push(&cursor);
328 void MathedCursor::End()
330 if (macro_mode) MacroModeClose();
333 mathstk.push(&cursor);
337 void MathedCursor::Insert(byte c, MathedTextCodes t)
339 if (selection) SelDel();
344 if (macro_mode && !(MathIsAlphaFont(t) || t==LM_TC_MIN))
348 MathParInset *p= cursor->p;
349 if (p==par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
350 MathMatrixInset* mt = new MathMatrixInset(3, 0);
351 mt->SetAlign(' ', "rcl");
352 mt->SetStyle(LM_ST_DISPLAY);
353 mt->SetType((p->GetType()==LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
354 mt->SetData(p->GetData());
355 p->SetData(0);//BUG duda
360 int pos = cursor->getPos();
361 cursor->SetData(par);
362 cursor->goPosAbs(pos);
364 if (p && p->Permit(LMPF_ALLOW_CR)) {
369 MathParInset *p = cursor->p;
370 if (p && p->Permit(LMPF_ALLOW_TAB)) {
372 cursor->Insert(c, t);
375 cursor->goNextColumn();
376 } else // Navigate between arguments
377 if (p && p->GetType()==LM_OT_MACRO) {
378 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
379 p->setArgumentIdx(p->getArgumentIdx()+1);
386 if (MathIsAlphaFont(t) || t==LM_TC_MIN) {
394 cursor->Insert(c, t);
402 void MathedCursor::Insert(MathedInset* p, int t)
404 if (macro_mode) MacroModeClose();
406 if (MathIsActive(t)) {
408 ((MathParInset*)p)->SetData(selarray);
413 if (mathstk.i<MAX_STACK_ITEMS-1) {
415 if (accent && !MathIsActive(t)) {
418 cursor->Insert(p, t);
420 if (MathIsActive(t)) {
427 fprintf(stderr, "Math error: Full stack.\n");
430 void MathedCursor::Delete()
432 if (macro_mode) return;
437 if (cursor->Empty() && !mathstk.Empty()) {
438 cursor = mathstk.pop();
440 // if (cursor->GetChar()!=LM_TC_TAB)
445 void MathedCursor::DelLine()
447 if (macro_mode) MacroModeClose();
452 MathParInset *p= cursor->p;
453 if (p && (p->GetType()<=LM_OT_MATRIX && p->GetType()>=LM_OT_MPAR)) {
459 bool MathedCursor::Up(bool sel)
463 if (macro_mode) MacroModeClose();
465 if (sel && !selection) SelStart();
466 if (!sel && selection) SelClear();
470 if (cursor->IsScript()) {
471 char cd = cursor->GetChar();
476 // A subscript may be followed by a superscript
479 if (MathIsUp(cursor->GetChar())) {
482 } else // return to the previous state
487 result = cursor->Up();
488 if (!result && cursor->p) {
491 if (p->GetType()==LM_OT_SCRIPT) {
492 MathedXIter *cx = mathstk.Item(1);
493 bool is_down = (cx->GetChar()==LM_TC_DOWN);
494 cursor = mathstk.pop();
496 result = (is_down) ? true: Up();
498 result = (p->getArgumentIdx() > 0);
500 p->setArgumentIdx(p->getArgumentIdx()-1);
504 if (!result && !mathstk.Empty()) {
505 cursor = mathstk.pop();
513 bool MathedCursor::Down(bool sel)
517 if (macro_mode) MacroModeClose();
519 if (sel && !selection) SelStart();
520 if (!sel && selection) SelClear();
521 // if (selection) SelClear();
525 if (cursor->IsScript()) {
526 char cd = cursor->GetChar();
527 if (MathIsDown(cd)) {
531 // A superscript may be followed by a subscript
534 if (MathIsDown(cursor->GetChar())) {
542 result = cursor->Down();
543 if (!result && cursor->p) {
545 if (p->GetType()==LM_OT_SCRIPT) {
546 MathedXIter *cx = mathstk.Item(1);
547 bool is_up = (cx->GetChar()==LM_TC_UP);
548 cursor = mathstk.pop();
550 result = (is_up) ? true: Down();
552 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
554 p->setArgumentIdx(p->getArgumentIdx()+1);
558 if (!result && !mathstk.Empty()) {
559 cursor = mathstk.pop();
566 bool MathedCursor::Limits()
568 if (cursor->IsInset()) {
569 MathedInset *p = cursor->GetInset();
570 bool ol = p->GetLimits();
572 return (ol!=p->GetLimits());
577 void MathedCursor::SetSize(short size)
579 MathParInset *p = cursor->p;
580 p->UserSetSize(size);
585 void MathedCursor::setLabel(char const* label)
586 { // ugly hack and possible bug
587 if (!cursor->setLabel(strnew(label)))
588 fprintf(stderr, "MathErr: Bad place to set labels.");
592 void MathedCursor::setNumbered()
593 { // another ugly hack
594 MathedRowSt *crow = cursor->crow;
596 crow->setNumbered(!crow->isNumbered());
600 void MathedCursor::Interpret(char const *s)
604 MathedTextCodes tcode = LM_TC_INSET;
606 if (s[0]=='^' || s[0]=='_') {
607 char c = cursor->GetChar();
608 if (MathIsUp(c) && s[0]=='^' || MathIsDown(c) && s[0]=='_') {
611 } else // A script may be followed by a script
612 if (MathIsUp(c) || MathIsDown(c)) {
615 c = cursor->GetChar();
616 if (MathIsUp(c) && s[0]=='^' || MathIsDown(c) && s[0]=='_') {
622 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
623 Insert (p, (s[0]=='_') ? LM_TC_DOWN: LM_TC_UP);
626 if (s[0]=='!' || s[0]==',' || s[0]==':' || s[0]==';') {
627 int sp = ((s[0]==',') ? 1:((s[0]==':') ? 2:((s[0]==';') ? 3: 0)));
628 p = new MathSpaceInset(sp);
632 l = in_word_set (s, strlen(s));
635 p = MathMacroTable::mathMTable.getMacro(s);
637 lyxerr.debug(string("Macro2 ") + s + ' '
638 + tostr(tcode), Error::MATHED);
639 if (strcmp("root", s)==0) {
640 p = new MathRootInset();
641 tcode = LM_TC_ACTIVE_INSET;
643 p = new MathFuncInset(s, LM_OT_UNDEF);
645 tcode = ((MathMacro*)p)->getTCode();
646 fprintf(stderr, "Macro2 %s %d ", s, tcode);
649 MathedInsetTypes fractype = LM_OT_FRAC;
653 p = new MathBigopInset(l->name, l->id);
659 Insert((byte)l->id, MathIsBOPS(l->id) ?
660 LM_TC_BOPS: LM_TC_SYMB);
662 p = new MathFuncInset(l->name);
667 fractype = LM_OT_STACKREL;
668 lyxerr.debug(" i:stackrel ", Error::MATHED);
671 p = new MathFracInset(fractype);
672 tcode = LM_TC_ACTIVE_INSET;
677 p = new MathSqrtInset;
678 tcode = LM_TC_ACTIVE_INSET;
683 p = new MathDecorationInset(l->id);
684 tcode = LM_TC_ACTIVE_INSET;
689 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
694 p = new MathSpaceInset(l->id);
699 p = new MathDotsInset(l->name, l->id);
706 p = MathMacroTable::mathMTable.getMacro(s);
707 tcode = ((MathMacro*)p)->getTCode();
708 lyxerr.debug(string("Macro ") + s + ' '
709 + tostr(tcode), Error::MATHED);
713 p = new MathFuncInset(l->name);
725 bool MathedCursor::pullArg()
727 if (cursor->IsActive()) {
728 MathParInset *p = cursor->GetActiveInset();
732 LyxArrayBase *a = p->GetData();
746 void MathedCursor::MacroModeOpen()
751 imacro = new MathFuncInset(¯obf[0]);
755 fprintf(stderr, "Mathed Warning: Already in macro mode\n");
758 void MathedCursor::MacroModeClose()
762 latexkeys *l = in_word_set(macrobf, macroln);
763 if (macroln>0 && (!l || (l && IsMacro(l->token, l->id))) &&
764 !MathMacroTable::mathMTable.getMacro(macrobf)) {
766 imacro->SetName(strnew(macrobf));
767 // This guarantees that the string will be removed by destructor
768 imacro->SetType(LM_OT_UNDEF);
770 imacro->SetName(l->name);
774 if (cursor->GetInset()->GetType()==LM_OT_ACCENT) {
775 setAccent(((MathAccentInset*)cursor->GetInset())->getAccentCode());
778 if (l || MathMacroTable::mathMTable.getMacro(macrobf)) {
786 void MathedCursor::MacroModeBack()
790 macrobf[--macroln] = '\0';
795 fprintf(stderr, "Mathed Warning: we are not in macro mode\n");
798 void MathedCursor::MacroModeInsert(char c)
801 macrobf[macroln+1] = macrobf[macroln];
802 macrobf[macroln++] = c;
805 fprintf(stderr, "Mathed Warning: we are not in macro mode\n");
808 void MathedCursor::SelCopy()
812 p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
813 p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
814 selarray = cursor->Copy(p1, p2);
820 void MathedCursor::SelCut()
823 if (cursor->pos==selpos) return;
826 p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
827 p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
828 selarray = cursor->Copy(p1, p2);
829 cursor->Clean(selpos);
835 void MathedCursor::SelDel()
837 // fprintf(stderr, "Deleting sel ");
839 if (cursor->pos==selpos) return;
840 cursor->Clean(selpos);
846 void MathedCursor::SelPaste()
848 // fprintf(stderr, "paste %p %d ", selarray, cursor->pos);
849 if (selection) SelDel();
851 cursor->Merge(selarray);
856 void MathedCursor::SelStart()
858 lyxerr.debug("Starting sel ",Error::MATHED);
860 selpos = cursor->pos;
861 selstk = new MathStackXIter(mathstk);
862 anchor = selstk->Item(-1);
863 anchor->SetData(cursor->p);
865 anchor->goPosAbs(selpos);
871 void MathedCursor::SelClear()
873 lyxerr.debug("Clearing sel ", Error::MATHED);
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 // fprintf(stderr, "b[%d %d %d %d]", mathstk.Level(), selstk->Level(), anchor->GetX(), cursor->GetX());
891 anchor = selstk->pop();
892 if (anchor->GetX() >= cursor->GetX())
895 // fprintf(stderr, "a[%d %d]", 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 XPoint *MathedCursor::SelGetArea(int& np)
913 static XPoint point[10];
915 // single row selection
916 int i = 0, x, y, a, d, w, xo, yo, x1, y1, a1, d1; //, p1, p2;
918 // Balance anchor and cursor
921 cursor->p->GetXY(xo, yo);
922 w = cursor->p->Width();
923 cursor->GetPos(x1, y1);
924 cursor->getAD(a1, d1);
925 anchor->GetPos(x, y);
955 point[i].x = point[0].x;
956 point[i++].y = point[0].y;
958 // fprintf(stderr, "AN[%d %d %d %d] ", x, y, x1, y1);
959 // fprintf(stderr, "MT[%d %d %d %d] ", a, d, a1, d1);
960 // for (i=0; i<np; i++)
961 // fprintf(stderr, "XY[%d %d] ", point[i].x, point[i].y);
968 void MathedCursor::setAccent(int ac)
970 if (ac > 0 && accent < 8) {
971 nestaccent[accent++] = ac;
973 accent = 0; // consumed!
977 int MathedCursor::getAccent() const
979 return (accent>0) ? nestaccent[accent-1]: 0;
983 void MathedCursor::doAccent(byte c, MathedTextCodes t)
987 for (int i=accent-1; i>=0; i--) {
989 ac = new MathAccentInset(c, t, nestaccent[i]);
991 ac = new MathAccentInset(ac, nestaccent[i]);
996 accent = 0; // consumed!
1000 void MathedCursor::doAccent(MathedInset *p)
1002 MathedInset *ac = 0;
1004 for (int i=accent-1; i>=0; i--) {
1006 ac = new MathAccentInset(p, nestaccent[i]);
1008 ac = new MathAccentInset(ac, nestaccent[i]);
1013 accent = 0; // consumed!