3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: (c) 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
22 #pragma implementation "formula.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
31 #include "minibuffer.h"
32 #include "BufferView.h"
33 #include "lyxscreen.h"
39 #include "LaTeXFeatures.h"
41 #include "lyx_gui_misc.h"
42 #include "support/LOstream.h"
46 extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
49 extern GC canvasGC, mathGC, mathLineGC, latexGC, cursorGC, mathFrameGC;
52 extern char * mathed_label;
55 extern int mono_video;
56 extern int fast_selection;
59 extern BufferView * current_view;
60 extern char const * latex_special_chars;
62 short greek_kb_flag = 0;
64 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
66 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
69 static int sel_x, sel_y;
71 MathedCursor * InsetFormula::mathcursor = 0;
74 int MathedInset::df_asc;
75 int MathedInset::df_des;
76 int MathedInset::df_width;
79 inline bool IsMacro(short token, int id)
81 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
82 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
87 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
90 LyXFont WhichFont(short type, int size)
120 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
130 f.setSize(lfont_size);
134 if (type == LM_TC_BSYM) {
144 case LM_ST_SCRIPTSCRIPT:
149 lyxerr << "Mathed Error: wrong font size: " << size << endl;
154 if (type != LM_TC_TEXTRM)
155 f.setColor(LColor::math);
157 if (type != LM_TC_TEXTRM)
158 f.setColor(LyXFont::MATH);
164 void mathed_init_fonts() //removed 'static' because DEC cxx does not
166 // Probably because this func is declared as a friend in math_defs.h
170 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
172 for (int i = 0 ; i < 8 ; ++i){
173 Math_Fonts[i] = LyXFont::ALL_SANE;
175 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
177 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
179 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
180 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
182 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
184 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
185 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
187 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
189 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
191 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
193 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
194 MathedInset::df_asc = f.maxAscent();
195 MathedInset::df_des = f.maxDescent();
196 MathedInset::df_width = f.width('I');
200 LyXFont mathed_get_font(short type, int size)
202 LyXFont f = WhichFont(type, size);
203 if (type == LM_TC_TEX) {
204 f.setLatex(LyXFont::ON);
209 void mathed_set_font(short type, int size)
212 cursorGC = getGC(gc_thin_on_off_line);
213 canvasGC = getGC(gc_lighted);
214 latexGC = getGC(gc_latex);
215 mathLineGC = getGC(gc_math);
216 mathFrameGC = getGC(gc_math_frame);
218 LyXFont f = WhichFont(type, size);
219 if (type == LM_TC_TEX) {
220 f.setLatex(LyXFont::ON);
228 int mathed_string_width(short type, int size, byte const * s, int ls)
230 LyXFont f = WhichFont(type, size);
233 if (MathIsBinary(type)) {
235 for (int i = 0; i < ls && i < 75; ++i) {
244 return f.textWidth(reinterpret_cast<char const *>(s), ls);
248 int mathed_char_width(short type, int size, byte c)
250 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
251 WhichFont(type, size).width(c);
256 int mathed_string_height(short type, int size, byte const * s,
257 int ls, int & asc, int & des)
259 LyXFont font = WhichFont(type, size);
261 for (int i = 0; i < ls; ++i) {
262 if (font.descent(s[i]) > des)
263 des = font.descent(s[i]);
264 if (font.ascent(s[i]) > asc)
265 asc = font.ascent(s[i]);
271 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
273 LyXFont font = WhichFont(type, size);
275 des = font.descent(c);
276 asc = font.ascent(c);
281 // In a near future maybe we use a better fonts renderer
283 void MathedInset::drawStr(Painter & pain, short type, int size,
284 int x, int y, byte * s, int ls)
287 if (MathIsBinary(type)) {
288 for (int i = 0; i < ls; ++i) {
289 #warning What conversion should be done for s[i] here?
290 st += string(" ") + char(s[i]) + ' ';
293 st = string(reinterpret_cast<char*>(s), ls);
295 LyXFont mf = mathed_get_font(type, size);
296 pain.text(x, y, st, mf);
299 void MathedInset::drawStr(short type, int siz, int x, int y, byte * s, int ls)
301 mathed_set_font(type, siz);
303 if (MathIsBinary(type)) {
305 for (int i = 0; i < ls && i < 75; ++i) {
314 GC gc = (type == LM_TC_TEX) ? latexGC: mathGC;
315 XDrawString(fl_display, pm, gc, x, y, reinterpret_cast<char*>(s), ls);
321 InsetFormula::InsetFormula(bool display)
323 par = new MathParInset; // this leaks
328 par->SetType(LM_OT_PAR);
329 par->SetStyle(LM_ST_DISPLAY);
334 InsetFormula::InsetFormula(MathParInset * p)
336 par = (p->GetType()>= LM_OT_MPAR) ?
337 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
341 disp_flag = (par->GetType()>0);
346 InsetFormula::~InsetFormula()
352 Inset * InsetFormula::Clone() const
354 InsetFormula * f = new InsetFormula(par);
360 void InsetFormula::Write(ostream & os)
367 int InsetFormula::Latex(ostream & os, signed char fragile)
370 //#warning Alejandro, the number of lines is not returned in this case
371 // This problem will disapear at 0.13.
373 InsetFormula::Latex(output, fragile);
379 int InsetFormula::Latex(string & file, signed char fragile)
382 //#warning Alejandro, the number of lines is not returned in this case
383 // This problem will disapear at 0.13.
387 mathed_write(par, file, &ret, fragile, label.c_str());
392 int InsetFormula::Linuxdoc(string &/*file*/)
398 int InsetFormula::DocBook(string &/*file*/)
404 // Check if uses AMS macros
405 void InsetFormula::Validate(LaTeXFeatures & features) const
407 // Validation only necesary if not using an AMS Style
408 if (!features.amsstyle)
409 mathedValidate(features, par);
413 void InsetFormula::Read(LyXLex & lex)
415 istream & is = lex.getStream();
417 mathed_parser_file(is, lex.GetLineNo());
419 // Silly hack to read labels.
421 mathed_parse(0, 0, &par);
423 disp_flag = (par->GetType() > 0);
425 // Update line number
426 lex.setLineNo(mathed_parser_lineno());
429 label = mathed_label;
440 int InsetFormula::ascent(Painter &, LyXFont const &) const
442 return par->Ascent() + ((disp_flag) ? 8 : 1);
445 int InsetFormula::Ascent(LyXFont const &) const
447 return par->Ascent() + ((disp_flag) ? 8 : 1);
453 int InsetFormula::descent(Painter &, LyXFont const &) const
455 return par->Descent() + ((disp_flag) ? 8 : 1);
458 int InsetFormula::Descent(LyXFont const &) const
460 return par->Descent() + ((disp_flag) ? 8 : 1);
466 int InsetFormula::width(Painter &, LyXFont const & f) const
468 lfont_size = f.size();
470 return par->Width(); //+2;
473 int InsetFormula::Width(LyXFont const & f) const
475 lfont_size = f.size();
477 return par->Width(); //+2;
483 void InsetFormula::draw(Painter & pain, LyXFont const &,
484 int baseline, float & x) const
486 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
488 lfont_size = font.size();
489 /// Let's try to wait a bit with this... (Lgb)
490 //UpdatableInset::draw(pain, font, baseline, x);
492 // otherwise a segfault could occur
493 // in some XDrawRectangles (i.e. matrix) (Matthias)
494 if (mathcursor && mathcursor->GetPar() == par) {
495 if (mathcursor->Selection()) {
499 mathcursor->SelGetArea(xp, yp, n);
500 pain.fillPolygon(xp, yp, n, LColor::selection);
502 mathcursor->draw(pain, int(x), baseline);
504 par->draw(pain, int(x), baseline);
506 x += float(width(pain, font));
508 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
509 LyXFont font = WhichFont(LM_TC_BF, par->size);
510 font.setLatex(LyXFont::OFF);
512 if (par->GetType() == LM_OT_PARN) {
515 str = string("(") + label + ")";
518 pain.text(int(x + 20), baseline, str, font);
519 } else if (par->GetType() == LM_OT_MPARN) {
520 MathMatrixInset * mt =
521 static_cast<MathMatrixInset*>(par);
523 MathedRowSt const * crow = mt->getRowSt();
525 y = baseline + crow->getBaseline();
526 if (crow->isNumbered()) {
528 if (crow->getLabel())
529 str = string("(") + crow->getLabel() + ")";
532 pain.text(int(x + 20), y, str, font);
534 crow = crow->getNext();
538 cursor_visible = false;
541 void InsetFormula::Draw(LyXFont f, LyXScreen & scr, int baseline, float & x)
543 // This is Alejandros domain so I'll use this
544 unsigned long pm = scr.getForeground();
546 lfont_size = f.size();
547 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
548 // in some XDrawRectangles (i.e. matrix) (Matthias)
549 if (mathcursor && mathcursor->GetPar() == par) {
550 if (mathcursor->Selection()) {
552 XPoint * p = mathcursor->SelGetArea(n);
553 XFillPolygon(fl_display, pm, getGC(gc_selection),
554 p, n, Nonconvex, CoordModeOrigin);
556 mathcursor->Draw(pm, int(x), baseline);
559 par->setDrawable(pm);
560 par->Draw(int(x), baseline);
562 x += float(Width(f));
564 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
566 LyXFont font = WhichFont(LM_TC_BF, par->size);
567 font.setLatex(LyXFont::OFF);
569 if (par->GetType() == LM_OT_PARN) {
571 sprintf(s, "(%s)", label.c_str());
574 font.drawString(s, pm, baseline, int(x+20));
576 if (par->GetType() == LM_OT_MPARN) {
577 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
579 MathedRowSt const* crow = mt->getRowSt();
581 y = baseline + crow->getBaseline();
582 if (crow->isNumbered()) {
583 if (crow->getLabel())
584 sprintf(s, "(%s)", crow->getLabel());
587 font.drawString(s, pm, y, int(x+20));
589 crow = crow->getNext();
593 cursor_visible = false;
598 void InsetFormula::Edit(int x, int y)
600 mathcursor = new MathedCursor(par);
601 current_view->lockInset(this);
603 UpdateInset(current_view, this, false);
606 mathcursor->SetPos(x, y);
612 void InsetFormula::InsetUnlock()
615 if (mathcursor->InMacroMode()) {
616 mathcursor->MacroModeClose();
622 UpdateInset(current_view, this, false);
626 // Now a symbol can be inserted only if the inset is locked
627 void InsetFormula::InsertSymbol(char const * s)
629 if (!s || !mathcursor) return;
630 mathcursor->Interpret(s);
635 void InsetFormula::GetCursorPos(int& x, int& y) const
637 mathcursor->GetPos(x, y);
642 void InsetFormula::ToggleInsetCursor()
648 mathcursor->GetPos(x, y);
651 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
652 asc = font.maxAscent();
653 desc = font.maxDescent();
656 current_view->hideLockedInsetCursor(x, y, asc, desc);
658 current_view->showLockedInsetCursor(x, y, asc, desc);
659 cursor_visible = !cursor_visible;
663 void InsetFormula::ShowInsetCursor()
665 if (!cursor_visible) {
668 mathcursor->GetPos(x, y);
671 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
672 asc = font.maxAscent();
673 desc = font.maxDescent();
674 current_view->fitLockedInsetCursor(x, y, asc, desc);
681 void InsetFormula::HideInsetCursor()
688 void InsetFormula::ToggleInsetSelection()
696 //mathcursor->SelGetArea(n);
697 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
701 UpdateInset(current_view, this, false);
706 void InsetFormula::display(bool dspf)
708 if (dspf != disp_flag) {
710 par->SetType(LM_OT_PAR);
711 par->SetStyle(LM_ST_DISPLAY);
713 if (par->GetType() >= LM_OT_MPAR) {
714 MathParInset * p = new MathParInset(par);
718 mathcursor->SetPar(par);
720 par->SetType(LM_OT_MIN);
721 par->SetStyle(LM_ST_TEXT);
722 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
731 int InsetFormula::GetNumberOfLabels() const
733 // This is dirty, I know. I'll clean it at 0.13
734 if (par->GetType() == LM_OT_MPARN) {
735 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
737 MathedRowSt const * crow = mt->getRowSt();
739 if (crow->getLabel()) ++nl;
740 crow = crow->getNext();
751 string InsetFormula::getLabel(int il) const
753 //#warning This is dirty, I know. Ill clean it at 0.11
754 // Correction, the only way to clean this is with a new kernel: 0.13.
755 if (par->GetType() == LM_OT_MPARN) {
757 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
759 MathedRowSt const * crow = mt->getRowSt();
761 if (crow->getLabel()) {
763 lab = crow->getLabel();
768 crow = crow->getNext();
776 void InsetFormula::UpdateLocal()
778 par->Metrics(); // To inform lyx kernel the exact size
779 // (there were problems with arrays).
780 UpdateInset(current_view, this);
784 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
789 mathcursor->SetPos(x, y);
794 UpdateInset(current_view, this, false);
799 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
802 sel_x = x; sel_y = y;
803 if (mathcursor->Selection()) {
804 mathcursor->SelClear();
805 UpdateInset(current_view, this, false);
810 void InsetFormula::InsetMotionNotify(int x, int y, int /*button*/)
812 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
815 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
816 mathcursor->SelStart();
818 mathcursor->GetPos(sel_x, sel_y);
824 mathcursor->SetPos(x, y);
826 mathcursor->GetPos(x, y);
827 if (sel_x!= x || sel_y!= y)
828 UpdateInset(current_view, this, false);
829 sel_x = x; sel_y = y;
834 void InsetFormula::InsetKeyPress(XKeyEvent *)
836 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
840 // Special Mathed functions
841 bool InsetFormula::SetNumber(bool numbf)
844 short type = par->GetType();
845 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
846 if (numbf && !oldf) ++type;
847 if (!numbf && oldf) --type;
855 bool InsetFormula::LocalDispatch(int action, char const * arg)
857 // extern char *dispatch_result;
858 MathedTextCodes varcode = LM_TC_MIN;
859 bool was_macro = mathcursor->InMacroMode();
861 bool space_on = false;
862 bool was_selection = mathcursor->Selection();
864 static MathSpaceInset * sp= 0;
868 if (mathcursor->Selection() && (fast_selection || mono_video))
869 ToggleInsetSelection();
872 if (mathcursor->getLastCode() == LM_TC_TEX) {
877 // --- Cursor Movements ---------------------------------------------
878 case LFUN_RIGHTSEL: sel = true;
881 result = mathcursor->Right(sel);
884 case LFUN_LEFTSEL: sel = true;
887 result = mathcursor->Left(sel);
890 case LFUN_UPSEL: sel = true;
892 result = mathcursor->Up(sel);
895 case LFUN_DOWNSEL: sel = true;
897 result = mathcursor->Down(sel);
905 case LFUN_DELETE_LINE_FORWARD:
906 //current_view->lockedInsetStoreUndo(Undo::INSERT);
907 current_view->lockedInsetStoreUndo(Undo::DELETE);
908 mathcursor->DelLine();
912 current_view->lockedInsetStoreUndo(Undo::INSERT);
913 mathcursor->Insert(' ', LM_TC_CR);
914 par = mathcursor->GetPar();
918 current_view->lockedInsetStoreUndo(Undo::INSERT);
919 mathcursor->Insert(0, LM_TC_TAB);
923 current_view->lockedInsetStoreUndo(Undo::INSERT);
924 mathcursor->Insert('T', LM_TC_TAB);
928 if (!mathcursor->Left())
931 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
932 UpdateInset(current_view, this);
937 //current_view->lockedInsetStoreUndo(Undo::INSERT);
938 current_view->lockedInsetStoreUndo(Undo::DELETE);
939 mathcursor->Delete();
940 UpdateInset(current_view, this);
943 // sprintf(dispatch_buffer, "%d %d",);
944 // dispatch_result = dispatch_buffer;
949 sscanf(arg, "%d %d", &x, &y);
951 mathcursor->SetPos(x1+x, y1+y);
955 /* cursor selection ---------------------------- */
959 mathcursor->MacroModeClose();
960 current_view->lockedInsetStoreUndo(Undo::INSERT);
961 mathcursor->SelPaste(); UpdateLocal(); break;
963 current_view->lockedInsetStoreUndo(Undo::DELETE);
964 mathcursor->SelCut(); UpdateLocal(); break;
965 case LFUN_COPY: mathcursor->SelCopy(); break;
968 case LFUN_WORDRIGHTSEL:
969 case LFUN_WORDLEFTSEL:
972 // --- accented characters ------------------------------
974 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
975 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
976 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
977 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
978 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
979 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
980 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
981 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
982 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
983 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
988 if (!greek_kb_flag) {
990 current_view->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
997 case LFUN_GREEK_TOGGLE:
999 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
1001 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
1003 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
1008 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
1009 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
1010 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
1011 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
1012 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
1013 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
1016 // varcode = LM_TC_TEX;
1017 mathcursor->setLastCode(LM_TC_TEX);
1018 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1022 case LFUN_MATH_NUMBER:
1024 current_view->lockedInsetStoreUndo(Undo::INSERT);
1026 short type = par->GetType();
1027 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
1030 if (!label.empty()) {
1033 current_view->owner()->getMiniBuffer()->Set(_("No number"));
1036 current_view->owner()->getMiniBuffer()->Set(_("Number"));
1044 case LFUN_MATH_NONUMBER:
1046 if (par->GetType() == LM_OT_MPARN) {
1047 // MathMatrixInset *mt = (MathMatrixInset*)par;
1049 // mt->SetNumbered(!mt->IsNumbered());
1051 mathcursor->setNumbered();
1057 case LFUN_MATH_LIMITS:
1059 current_view->lockedInsetStoreUndo(Undo::INSERT);
1060 if (mathcursor->Limits())
1064 case LFUN_MATH_SIZE:
1066 latexkeys * l = in_word_set (arg, strlen(arg));
1067 int sz = (l) ? l->id: -1;
1068 mathcursor->SetSize(sz);
1073 case LFUN_INSERT_MATH:
1075 current_view->lockedInsetStoreUndo(Undo::INSERT);
1080 case LFUN_INSERT_MATRIX:
1082 current_view->lockedInsetStoreUndo(Undo::INSERT);
1084 char s[80], arg2[80];
1085 // This is just so that too long args won't ooze out of s.
1086 strncpy(arg2, arg, 80); arg2[79]= '\0';
1087 k = sscanf(arg2, "%d %d %s", &m, &n, s);
1092 } else if (k == 1) {
1096 MathMatrixInset * p = new MathMatrixInset(m, n);
1097 if (mathcursor && p) {
1098 if (k > 2 && int(strlen(s)) > m)
1099 p->SetAlign(s[0], &s[1]);
1100 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1106 case LFUN_MATH_DELIM:
1108 current_view->lockedInsetStoreUndo(Undo::INSERT);
1109 char lf[40], rg[40], arg2[40];
1110 int ilf = '(', irg = '.';
1112 string vdelim("(){}[]./|");
1115 strncpy(arg2, arg, 40); arg2[39]= '\0';
1116 int n = sscanf(arg2, "%s %s", lf, rg);
1117 lf[39] = '\0'; rg[39] = '\0';
1124 l = in_word_set(lf, strlen(lf));
1125 // Long words will cause l == 0; so check.
1128 if (vdelim.find(lf[0]) != string::npos)
1136 l = in_word_set(rg, strlen(rg));
1139 if (vdelim.find(rg[0]) != string::npos)
1144 MathDelimInset * p = new MathDelimInset(ilf, irg);
1145 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1150 case LFUN_PROTECTEDSPACE:
1152 current_view->lockedInsetStoreUndo(Undo::INSERT);
1153 sp = new MathSpaceInset(1);
1154 mathcursor->Insert(sp);
1160 case LFUN_INSERT_LABEL:
1162 current_view->lockedInsetStoreUndo(Undo::INSERT);
1163 if (par->GetType() < LM_OT_PAR) break;
1167 result = askForText(_("Enter new label to insert:"));
1172 if (!lb.empty() && lb[0] > ' ') {
1174 if (par->GetType() == LM_OT_MPARN) {
1175 mathcursor->setLabel(lb.c_str());
1176 // MathMatrixInset *mt = (MathMatrixInset*)par;
1177 // mt->SetLabel(lb);
1179 //if (label.notEmpty()) delete label;
1188 case LFUN_MATH_DISPLAY:
1189 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1190 current_view->lockedInsetStoreUndo(Undo::EDIT);
1191 display(!disp_flag);
1195 // Invalid actions under math mode
1196 case LFUN_MATH_MODE:
1198 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1199 current_view->owner()->getMiniBuffer()->Set(_("math text mode"));
1200 varcode = LM_TC_TEXTRM;
1202 varcode = LM_TC_VAR;
1204 mathcursor->setLastCode(varcode);
1208 current_view->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1211 //------- dummy actions
1212 case LFUN_EXEC_COMMAND:
1213 current_view->owner()->getMiniBuffer()->ExecCommand();
1217 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1218 unsigned char c = arg[0];
1219 current_view->lockedInsetStoreUndo(Undo::INSERT);
1221 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1223 mathcursor->setAccent(0);
1225 if (c == 0) { // Dead key, do nothing
1226 //lyxerr << "deadkey" << endl;
1230 if (mathcursor->getLastCode() == LM_TC_TEX) {
1231 mathcursor->MacroModeOpen();
1232 mathcursor->clearLastCode();
1233 varcode = LM_TC_MIN;
1236 short f = (mathcursor->getLastCode()) ?
1237 mathcursor->getLastCode() :
1238 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1239 varcode = MathIsAlphaFont(f) ?
1240 static_cast<MathedTextCodes>(f) :
1244 // lyxerr << "Varcode << vardoce;
1245 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1246 varcode = LM_TC_MIN;
1247 if (greek_kb_flag<2) greek_kb_flag = 0;
1249 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1250 mathcursor->Insert(c, LM_TC_TEX);
1252 mathcursor->Insert('}', LM_TC_TEX);
1255 mathcursor->clearLastCode();
1256 // varcode = LM_TC_MIN;
1258 if (c == '_' && varcode == LM_TC_TEX) {
1259 mathcursor->Insert(c, LM_TC_SPECIAL);
1260 mathcursor->clearLastCode();
1261 // varcode = LM_TC_MIN;
1263 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1264 mathcursor->MacroModeOpen();
1265 mathcursor->clearLastCode();
1266 mathcursor->Insert(c, LM_TC_MIN);
1269 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1270 mathcursor->Insert(c, LM_TC_CONST);
1272 if (strchr("+/-*<>=", c))
1273 mathcursor->Insert(c, LM_TC_BOP);
1275 if (strchr(latex_special_chars, c) && c!= '_')
1276 mathcursor->Insert(c, LM_TC_SPECIAL);
1278 if (c == '_' || c == '^') {
1282 mathcursor->Interpret (s);
1286 short f = (mathcursor->getLastCode()) ?
1287 mathcursor->getLastCode() :
1288 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1289 varcode = MathIsAlphaFont(f) ?
1290 static_cast<MathedTextCodes>(f) :
1293 if (varcode == LM_TC_TEXTRM) {
1294 mathcursor->Insert(c, LM_TC_TEXTRM);
1297 mathcursor->MacroModeClose();
1300 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1304 if (!mathcursor->Pop() && mathcursor->IsEnd())
1309 mathcursor->Insert (c, LM_TC_VAR);
1313 mathcursor->MacroModeClose();
1314 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1315 mathcursor->setLastCode(LM_TC_TEX);
1319 // lyxerr << "Closed by action " << action << endl;
1323 if (was_macro != mathcursor->InMacroMode()
1325 && action != LFUN_BACKSPACE)
1327 if (sp && !space_on) sp = 0;
1328 if (mathcursor->Selection() || (was_selection
1330 && !(fast_selection || mono_video)
1333 ToggleInsetSelection();
1338 current_view->unlockInset(this);
1346 MathFuncInset::draw(Painter & pain, int x, int y)
1348 if (name && name[0] > ' ') {
1349 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1350 font.setLatex(LyXFont::ON);
1351 x += (font.textWidth("I", 1) + 3) / 4;
1354 int a = font.maxAscent();
1355 int d = font.maxDescent();
1356 pain.fillRectangle(x, y - a, font.textWidth(name, strlen(name)), a + d);
1359 pain.text(x, y, name, font);
1364 MathFuncInset::Draw(int x, int y)
1366 if (name && name[0] > ' ') {
1367 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1368 font.setLatex(LyXFont::ON);
1369 x += (font.textWidth("I", 1)+3)/4;
1371 int a = font.maxAscent(), d = font.maxDescent();
1372 XFillRectangle (fl_display, pm, getGC(gc_copy),
1374 font.textWidth(name, strlen(name)), a+d);
1377 font.drawString(name, pm, y, x);
1382 void MathFuncInset::Metrics()
1384 ln = (name) ? strlen(name): 0;
1385 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1386 font.setLatex(LyXFont::ON);
1387 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1388 mathed_string_height(LM_TC_TEXTRM, size,
1389 reinterpret_cast<unsigned char const *>(name),
1390 strlen(name), ascent, descent);
1395 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1397 MathedIter it(par->GetData());
1399 while (it.OK() && !(features.binom && features.boldsymbol)) {
1402 MathParInset * p = it.GetActiveInset();
1403 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1404 strcmp(p->GetName(), "binom") == 0) {
1405 features.binom = true;
1407 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1408 p->setArgumentIdx(i);
1409 mathedValidate(features, p);
1413 MathedInset* p = it.GetInset();
1414 if (!features.boldsymbol && p->GetName() &&
1415 strcmp(p->GetName(), "boldsymbol") == 0) {
1416 features.boldsymbol = true;