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"
34 extern void mathed_set_font(short type, int style);
36 extern GC canvasGC, mathGC, latexGC, cursorGC, mathFrameGC;
38 static LyxArrayBase * selarray = 0;
40 inline bool IsAlpha(char c)
42 return ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z');
45 // This was very smaller, I'll change it later
46 inline 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));
57 // Yes, mathed isn't using string yet.
58 inline char * strnew(char const * s)
60 char * s1 = new char[strlen(s)+1];
67 #define MAX_STACK_ITEMS 32
69 struct MathStackXIter {
74 MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
75 item = new MathedXIter[imax];
79 MathStackXIter(MathStackXIter & stk);
85 void push(MathedXIter ** a) {
94 MathedXIter * Item(int idx) {
95 return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
103 return i >= MAX_STACK_ITEMS;
110 int Level() { return i; }
112 } mathstk, *selstk = 0;
115 MathStackXIter::MathStackXIter(MathStackXIter & stk) {
117 item = new MathedXIter[imax];
119 for (int k = 0; k < i; ++k) {
120 item[k].SetData(stk.item[k].getPar());
122 item[k].goPosAbs(stk.item[k].getPos());
127 /***---------------- Mathed Cursor ---------------------------***/
129 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
133 lastcode = LM_TC_MIN;
136 if (!MathMacroTable::built)
137 MathMacroTable::mathMTable.builtinMacros();
141 void MathedCursor::SetPar(MathParInset * p)
146 selection = false; // not SelClear() ?
148 mathstk.push(&cursor);
150 cursor->SetData(par);
155 void MathedCursor::draw(Painter & pain, int x, int y)
157 // lyxerr << "Cursor[" << x << " " << y << "] ";
158 //win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
160 int w = par->Width() + 2;
161 int a = par->Ascent() + 1;
162 int h = par->Height() + 1;
163 if (par->GetType() > LM_OT_PAR) { a += 4; h += 8; }
165 pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
167 par->draw(pain, x, y);
171 void MathedCursor::Draw(long unsigned pm, int x, int y)
173 // lyxerr << "Cursor[" << x << " " << y << "] ";
174 win = pm; // win = (mathedCanvas) ? mathedCanvas: pm;
176 int w = par->Width()+2, a = par->Ascent()+1, h = par->Height()+1;
177 if (par->GetType() > LM_OT_PAR) { a += 4; h += 8; }
179 if (!canvasGC) mathed_set_font(LM_TC_VAR, 1);
180 // XFillRectangle(fl_display, pm, canvasGC, x, y-a, w, h);
181 XDrawRectangle(fl_display, pm, mathFrameGC, x - 1, y - a, w, h);
183 MathParInset::pm = pm;
191 void MathedCursor::Redraw(Painter & pain)
193 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
195 int w = par->Width(), h = par->Height();
198 //mathed_set_font(LM_TC_VAR, 1);
199 pain.fillRectangle(x, y - par->Ascent(),
200 x + w, y - par->Ascent() + h,
202 par->draw(pain, x, y);
205 void MathedCursor::Redraw()
207 lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
209 int w = par->Width(), h = par->Height();
212 mathed_set_font(LM_TC_VAR, 1);
213 XFillRectangle(fl_display, win, canvasGC, x, y-par->Ascent(), w, h);
215 MathParInset::pm = win;
221 bool MathedCursor::Left(bool sel)
228 if (sel && !selection) SelStart();
229 if (!sel && selection) SelClear();
230 bool result = cursor->Prev();
231 if (!result && !mathstk.Empty()) {
232 cursor = mathstk.pop();
235 if (selection) SelClear();
237 if (result && cursor->IsActive()) {
238 if (cursor->IsScript()) {
240 if (!cursor->IsScript())
246 MathParInset * p = cursor->GetActiveInset();
250 p->setArgumentIdx(p->getMaxArgumentIdx());
251 mathstk.push(&cursor);
261 bool MathedCursor::Pop()
263 if (!mathstk.Empty()) {
264 cursor = mathstk.pop();
273 bool MathedCursor::Push()
275 if (cursor->IsActive()) {
276 MathParInset * p = cursor->GetActiveInset();
277 if (!p) return false;
278 mathstk.push(&cursor);
286 bool MathedCursor::Right(bool sel)
293 if (sel && !selection) SelStart();
294 if (!sel && selection) SelClear();
297 if (cursor->IsActive()) {
298 if (cursor->IsScript()) {
300 // A script may be followed by another script
301 if (cursor->IsScript())
306 MathParInset *p = cursor->GetActiveInset();
308 lyxerr << "Math error: Inset expected." << endl;
309 return cursor->Next();
311 p->setArgumentIdx(0);
312 mathstk.push(&cursor);
316 result = cursor->Next();
318 if (cursor->GetChar()!= LM_TC_CR)
319 result = cursor->Next();
320 if (!result && !mathstk.Empty()) {
321 cursor = mathstk.pop();
325 if (selection) SelClear();
332 void MathedCursor::SetPos(int x, int y)
336 if (macro_mode) MacroModeClose();
337 lastcode = LM_TC_MIN;
339 mathstk.push(&cursor);
340 cursor->SetData(par);
341 cursor->fitCoord(x, y);
342 while (cursor->GetX()<x && cursor->OK()) {
343 if (cursor->IsActive()) {
344 MathParInset * p = cursor->GetActiveInset();
345 if (p->Inside(x, y)) {
347 mathstk.push(&cursor);
349 cursor->fitCoord(x, y);
355 if (!cursor->Next() && !Pop())
358 if (x-xp < cursor->GetX()-x) cursor->ipop();
363 void MathedCursor::Home()
365 if (macro_mode) MacroModeClose();
368 mathstk.push(&cursor);
373 void MathedCursor::End()
375 if (macro_mode) MacroModeClose();
378 mathstk.push(&cursor);
383 void MathedCursor::Insert(byte c, MathedTextCodes t)
385 if (selection) SelDel();
390 if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
394 MathParInset * p = cursor->p;
395 if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
396 MathMatrixInset * mt = new MathMatrixInset(3, 0);
397 mt->SetAlign(' ', "rcl");
398 mt->SetStyle(LM_ST_DISPLAY);
399 mt->SetType((p->GetType() == LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
400 mt->SetData(p->GetData());
401 p->SetData(0);//BUG duda
406 int pos = cursor->getPos();
407 cursor->SetData(par);
408 cursor->goPosAbs(pos);
410 if (p && p->Permit(LMPF_ALLOW_CR)) {
414 if (t == LM_TC_TAB) {
415 MathParInset * p = cursor->p;
416 if (p && p->Permit(LMPF_ALLOW_TAB)) {
418 cursor->Insert(c, t);
421 cursor->goNextColumn();
422 } else // Navigate between arguments
423 if (p && p->GetType() == LM_OT_MACRO) {
424 if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
425 p->setArgumentIdx(p->getArgumentIdx()+1);
432 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
440 cursor->Insert(c, t);
449 void MathedCursor::Insert(MathedInset * p, int t)
451 if (macro_mode) MacroModeClose();
453 if (MathIsActive(t)) {
455 static_cast<MathParInset*>(p)->SetData(selarray);
460 if (mathstk.i < MAX_STACK_ITEMS - 1) {
462 if (accent && !MathIsActive(t)) {
465 cursor->Insert(p, t);
467 if (MathIsActive(t)) {
474 lyxerr << "Math error: Full stack." << endl;
478 void MathedCursor::Delete()
480 if (macro_mode) return;
485 if (cursor->Empty() && !mathstk.Empty()) {
486 cursor = mathstk.pop();
488 // if (cursor->GetChar()!= LM_TC_TAB)
494 void MathedCursor::DelLine()
496 if (macro_mode) MacroModeClose();
501 MathParInset *p= cursor->p;
502 if (p && (p->GetType()<= LM_OT_MATRIX && p->GetType()>= LM_OT_MPAR)) {
508 bool MathedCursor::Up(bool sel)
512 if (macro_mode) MacroModeClose();
514 if (sel && !selection) SelStart();
515 if (!sel && selection) SelClear();
518 if (cursor->IsScript()) {
519 char cd = cursor->GetChar();
524 // A subscript may be followed by a superscript
527 if (MathIsUp(cursor->GetChar())) {
530 } else // return to the previous state
535 result = cursor->Up();
536 if (!result && cursor->p) {
537 MathParInset * p = cursor->p;
539 if (p->GetType() == LM_OT_SCRIPT) {
540 MathedXIter * cx = mathstk.Item(1);
541 bool is_down = (cx->GetChar() == LM_TC_DOWN);
542 cursor = mathstk.pop();
544 result = (is_down) ? true: Up();
546 result = (p->getArgumentIdx() > 0);
548 p->setArgumentIdx(p->getArgumentIdx()-1);
552 if (!result && !mathstk.Empty()) {
553 cursor = mathstk.pop();
561 bool MathedCursor::Down(bool sel)
565 if (macro_mode) MacroModeClose();
567 if (sel && !selection) SelStart();
568 if (!sel && selection) SelClear();
569 // if (selection) SelClear();
571 if (cursor->IsScript()) {
572 char cd = cursor->GetChar();
573 if (MathIsDown(cd)) {
577 // A superscript may be followed by a subscript
580 if (MathIsDown(cursor->GetChar())) {
588 result = cursor->Down();
589 if (!result && cursor->p) {
590 MathParInset * p= cursor->p;
591 if (p->GetType() == LM_OT_SCRIPT) {
592 MathedXIter * cx = mathstk.Item(1);
593 bool is_up = (cx->GetChar() == LM_TC_UP);
594 cursor = mathstk.pop();
596 result = (is_up) ? true: Down();
598 result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
600 p->setArgumentIdx(p->getArgumentIdx()+1);
604 if (!result && !mathstk.Empty()) {
605 cursor = mathstk.pop();
613 bool MathedCursor::Limits()
615 if (cursor->IsInset()) {
616 MathedInset * p = cursor->GetInset();
617 bool ol = p->GetLimits();
619 return (ol!= p->GetLimits());
625 void MathedCursor::SetSize(short size)
627 MathParInset * p = cursor->p;
628 p->UserSetSize(size);
633 void MathedCursor::setLabel(char const * label)
634 { // ugly hack and possible bug
635 if (!cursor->setLabel(strnew(label)))
636 lyxerr << "MathErr: Bad place to set labels." << endl;
640 void MathedCursor::setNumbered()
641 { // another ugly hack
642 MathedRowSt * crow = cursor->crow;
644 crow->setNumbered(!crow->isNumbered());
648 void MathedCursor::Interpret(char const * s)
652 MathedTextCodes tcode = LM_TC_INSET;
654 if (s[0] == '^' || s[0] == '_') {
655 char c = cursor->GetChar();
656 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
659 } else // A script may be followed by a script
660 if (MathIsUp(c) || MathIsDown(c)) {
663 c = cursor->GetChar();
664 if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
670 p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
671 Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP);
674 if (s[0] == '!' || s[0] == ',' || s[0] == ':' || s[0] == ';') {
675 int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0)));
676 p = new MathSpaceInset(sp);
680 l = in_word_set (s, strlen(s));
683 p = MathMacroTable::mathMTable.getMacro(s);
685 lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
686 if (strcmp("root", s) == 0) {
687 p = new MathRootInset();
688 tcode = LM_TC_ACTIVE_INSET;
690 p = new MathFuncInset(s, LM_OT_UNDEF);
692 tcode = static_cast<MathMacro*>(p)->getTCode();
693 lyxerr << "Macro2 " << s << ' ' << tcode << " " ;
696 MathedInsetTypes fractype = LM_OT_FRAC;
700 p = new MathBigopInset(l->name, l->id);
706 Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ?
707 LM_TC_BOPS: LM_TC_SYMB);
709 p = new MathFuncInset(l->name);
714 fractype = LM_OT_STACKREL;
715 lyxerr[Debug::MATHED] << " i:stackrel " << endl;
718 p = new MathFracInset(fractype);
719 tcode = LM_TC_ACTIVE_INSET;
724 p = new MathSqrtInset;
725 tcode = LM_TC_ACTIVE_INSET;
730 p = new MathDecorationInset(l->id);
731 tcode = LM_TC_ACTIVE_INSET;
736 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
741 p = new MathSpaceInset(l->id);
746 p = new MathDotsInset(l->name, l->id);
753 p = MathMacroTable::mathMTable.getMacro(s);
754 tcode = static_cast<MathMacro*>(p)->getTCode();
755 lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
759 p = new MathFuncInset(l->name);
771 bool MathedCursor::pullArg()
773 if (cursor->IsActive()) {
774 MathParInset * p = cursor->GetActiveInset();
778 LyxArrayBase * a = p->GetData();
792 void MathedCursor::MacroModeOpen()
797 imacro = new MathFuncInset(¯obf[0]);
801 lyxerr << "Mathed Warning: Already in macro mode" << endl;
805 void MathedCursor::MacroModeClose()
809 latexkeys * l = in_word_set(macrobf, macroln);
810 if (macroln > 0 && (!l || (l && IsMacro(l->token, l->id))) &&
811 !MathMacroTable::mathMTable.getMacro(macrobf)) {
813 imacro->SetName(strnew(macrobf));
814 // This guarantees that the string will be removed by destructor
815 imacro->SetType(LM_OT_UNDEF);
817 imacro->SetName(l->name);
821 if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
822 setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
825 if (l || MathMacroTable::mathMTable.getMacro(macrobf)) {
834 void MathedCursor::MacroModeBack()
838 macrobf[--macroln] = '\0';
843 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
847 void MathedCursor::MacroModeInsert(char c)
850 macrobf[macroln + 1] = macrobf[macroln];
851 macrobf[macroln++] = c;
854 lyxerr << "Mathed Warning: we are not in macro mode" << endl;
858 void MathedCursor::SelCopy()
861 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
862 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
863 selarray = cursor->Copy(p1, p2);
870 void MathedCursor::SelCut()
873 if (cursor->pos == selpos) return;
875 int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
876 int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
877 selarray = cursor->Copy(p1, p2);
878 cursor->Clean(selpos);
885 void MathedCursor::SelDel()
887 // lyxerr << "Deleting sel "
889 if (cursor->pos == selpos) return;
890 cursor->Clean(selpos);
897 void MathedCursor::SelPaste()
899 // lyxerr << "paste " << selarray << " " << curor->pos;
900 if (selection) SelDel();
902 cursor->Merge(selarray);
908 void MathedCursor::SelStart()
910 lyxerr[Debug::MATHED] << "Starting sel " << endl;
912 selpos = cursor->pos;
913 selstk = new MathStackXIter(mathstk);
914 anchor = selstk->Item(-1);
915 anchor->SetData(cursor->p);
917 anchor->goPosAbs(selpos);
924 void MathedCursor::SelClear()
926 lyxerr[Debug::MATHED] << "Clearing sel " << endl;
935 // Anchor position must be at the same level that stack.
936 void MathedCursor::SelBalance()
938 int d = mathstk.Level() - selstk->Level();
940 // If unbalanced, balance them
943 // lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
944 anchor = selstk->pop();
945 if (anchor->GetX() >= cursor->GetX())
948 // lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
951 d = mathstk.Level() - selstk->Level();
954 // Once balanced the levels, check that they are at the same paragraph
955 selpos = anchor->pos;
960 void MathedCursor::SelGetArea(int * xp, int * yp, int & np)
967 static int xpoint[10];
968 static int ypoint[10];
970 // single row selection
971 int i = 0, x, y, a, d, xo, yo, x1, y1, a1, d1;
973 // Balance anchor and cursor
976 cursor->p->GetXY(xo, yo);
977 int w = cursor->p->Width();
978 cursor->GetPos(x1, y1);
979 cursor->getAD(a1, d1);
980 anchor->GetPos(x, y);
994 ypoint[i++] = y1 - a;
999 ypoint[i++] = y1 - a;
1001 ypoint[i++] = y1 + d;
1005 ypoint[i++] = y1 + d;
1008 ypoint[i++] = y + d;
1011 xpoint[i] = xpoint[0];
1012 ypoint[i++] = ypoint[0];
1017 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1018 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1019 // for (i = 0; i < np; ++i)
1020 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1024 XPoint * MathedCursor::SelGetArea(int & np)
1031 static XPoint point[10];
1033 // single row selection
1034 int i = 0, x, y, a, d, xo, yo, x1, y1, a1, d1; //, p1, p2;
1036 // Balance anchor and cursor
1039 cursor->p->GetXY(xo, yo);
1040 int w = cursor->p->Width();
1041 cursor->GetPos(x1, y1);
1042 cursor->getAD(a1, d1);
1043 anchor->GetPos(x, y);
1044 anchor->getAD(a, d);
1052 point[i].x = xo + w;
1053 point[i++].y = y - a;
1055 point[i].x = xo + w;
1056 point[i++].y = y1 - a;
1061 point[i++].y = y1 - a;
1063 point[i++].y = y1 + d;
1067 point[i++].y = y1 + d;
1070 point[i++].y = y + d;
1073 point[i].x = point[0].x;
1074 point[i++].y = point[0].y;
1076 // lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
1077 // lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
1078 // for (i = 0; i < np; ++i)
1079 // lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
1086 void MathedCursor::setAccent(int ac)
1088 if (ac > 0 && accent < 8) {
1089 nestaccent[accent++] = ac;
1091 accent = 0; // consumed!
1095 int MathedCursor::getAccent() const
1097 return (accent > 0) ? nestaccent[accent - 1]: 0;
1101 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1103 MathedInset * ac = 0;
1105 for (int i = accent - 1; i >= 0; --i) {
1106 if (i == accent - 1)
1107 ac = new MathAccentInset(c, t, nestaccent[i]);
1109 ac = new MathAccentInset(ac, nestaccent[i]);
1114 accent = 0; // consumed!
1118 void MathedCursor::doAccent(MathedInset * p)
1120 MathedInset * ac = 0;
1122 for (int i = accent - 1; i >= 0; --i) {
1123 if (i == accent - 1)
1124 ac = new MathAccentInset(p, nestaccent[i]);
1126 ac = new MathAccentInset(ac, nestaccent[i]);
1131 accent = 0; // consumed!