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"
37 #include "LaTeXFeatures.h"
39 #include "lyx_gui_misc.h"
40 #include "support/LOstream.h"
44 extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
46 extern GC canvasGC, mathGC, mathLineGC, latexGC, cursorGC, mathFrameGC;
47 extern char * mathed_label;
49 extern int mono_video;
50 extern int fast_selection;
52 extern BufferView * current_view;
53 extern char const * latex_special_chars;
55 short greek_kb_flag = 0;
57 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
59 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
62 static int sel_x, sel_y;
64 MathedCursor * InsetFormula::mathcursor = 0;
67 int MathedInset::df_asc;
68 int MathedInset::df_des;
69 int MathedInset::df_width;
72 inline bool IsMacro(short token, int id)
74 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
75 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
80 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
83 LyXFont WhichFont(short type, int size)
113 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
123 f.setSize(lfont_size);
127 if (type == LM_TC_BSYM) {
137 case LM_ST_SCRIPTSCRIPT:
142 lyxerr << "Mathed Error: wrong font size: " << size << endl;
147 if (type != LM_TC_TEXTRM)
148 f.setColor(LColor::math);
150 if (type != LM_TC_TEXTRM)
151 f.setColor(LyXFont::MATH);
157 void mathed_init_fonts() //removed 'static' because DEC cxx does not
159 // Probably because this func is declared as a friend in math_defs.h
163 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
165 for (int i = 0 ; i < 8 ; ++i){
166 Math_Fonts[i] = LyXFont::ALL_SANE;
168 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
170 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
172 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
173 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
175 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
177 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
178 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
180 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
182 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
184 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
186 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
187 MathedInset::df_asc = f.maxAscent();
188 MathedInset::df_des = f.maxDescent();
189 MathedInset::df_width = f.width('I');
193 LyXFont mathed_get_font(short type, int size)
195 LyXFont f = WhichFont(type, size);
196 if (type == LM_TC_TEX) {
197 f.setLatex(LyXFont::ON);
202 void mathed_set_font(short type, int size)
205 cursorGC = getGC(gc_thin_on_off_line);
206 canvasGC = getGC(gc_lighted);
207 latexGC = getGC(gc_latex);
208 mathLineGC = getGC(gc_math);
209 mathFrameGC = getGC(gc_math_frame);
211 LyXFont f = WhichFont(type, size);
212 if (type == LM_TC_TEX) {
213 f.setLatex(LyXFont::ON);
221 int mathed_string_width(short type, int size, byte const * s, int ls)
223 LyXFont f = WhichFont(type, size);
226 if (MathIsBinary(type)) {
228 for (int i = 0; i < ls && i < 75; ++i) {
237 return f.textWidth(reinterpret_cast<char const *>(s), ls);
241 int mathed_char_width(short type, int size, byte c)
243 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
244 WhichFont(type, size).width(c);
249 int mathed_string_height(short type, int size, byte const * s,
250 int ls, int & asc, int & des)
252 LyXFont font = WhichFont(type, size);
254 for (int i = 0; i < ls; ++i) {
255 if (font.descent(s[i]) > des)
256 des = font.descent(s[i]);
257 if (font.ascent(s[i]) > asc)
258 asc = font.ascent(s[i]);
264 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
266 LyXFont font = WhichFont(type, size);
268 des = font.descent(c);
269 asc = font.ascent(c);
274 // In a near future maybe we use a better fonts renderer
276 void MathedInset::drawStr(Painter & pain, short type, int size,
277 int x, int y, byte * s, int ls)
280 if (MathIsBinary(type)) {
281 for (int i = 0; i < ls; ++i) {
282 #warning What conversion should be done for s[i] here?
283 st += string(" ") + char(s[i]) + ' ';
286 st = string(reinterpret_cast<char*>(s), ls);
288 LyXFont mf = mathed_get_font(type, size);
289 pain.text(x, y, st, mf);
292 void MathedInset::drawStr(short type, int siz, int x, int y, byte * s, int ls)
294 mathed_set_font(type, siz);
296 if (MathIsBinary(type)) {
298 for (int i = 0; i < ls && i < 75; ++i) {
307 GC gc = (type == LM_TC_TEX) ? latexGC: mathGC;
308 XDrawString(fl_display, pm, gc, x, y, reinterpret_cast<char*>(s), ls);
314 InsetFormula::InsetFormula(bool display)
316 par = new MathParInset; // this leaks
321 par->SetType(LM_OT_PAR);
322 par->SetStyle(LM_ST_DISPLAY);
327 InsetFormula::InsetFormula(MathParInset * p)
329 par = (p->GetType()>= LM_OT_MPAR) ?
330 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
334 disp_flag = (par->GetType()>0);
339 InsetFormula::~InsetFormula()
345 Inset * InsetFormula::Clone() const
347 InsetFormula * f = new InsetFormula(par);
353 void InsetFormula::Write(ostream & os)
360 int InsetFormula::Latex(ostream & os, signed char fragile)
363 //#warning Alejandro, the number of lines is not returned in this case
364 // This problem will disapear at 0.13.
366 InsetFormula::Latex(output, fragile);
372 int InsetFormula::Latex(string & file, signed char fragile)
375 //#warning Alejandro, the number of lines is not returned in this case
376 // This problem will disapear at 0.13.
380 mathed_write(par, file, &ret, fragile, label.c_str());
385 int InsetFormula::Linuxdoc(string &/*file*/)
391 int InsetFormula::DocBook(string &/*file*/)
397 // Check if uses AMS macros
398 void InsetFormula::Validate(LaTeXFeatures & features) const
400 // Validation only necesary if not using an AMS Style
401 if (!features.amsstyle)
402 mathedValidate(features, par);
406 void InsetFormula::Read(LyXLex & lex)
408 istream & is = lex.getStream();
410 mathed_parser_file(is, lex.GetLineNo());
412 // Silly hack to read labels.
414 mathed_parse(0, 0, &par);
416 disp_flag = (par->GetType() > 0);
418 // Update line number
419 lex.setLineNo(mathed_parser_lineno());
422 label = mathed_label;
433 int InsetFormula::ascent(Painter &, LyXFont const &) const
435 return par->Ascent() + ((disp_flag) ? 8 : 1);
438 int InsetFormula::Ascent(LyXFont const &) const
440 return par->Ascent() + ((disp_flag) ? 8 : 1);
446 int InsetFormula::descent(Painter &, LyXFont const &) const
448 return par->Descent() + ((disp_flag) ? 8 : 1);
451 int InsetFormula::Descent(LyXFont const &) const
453 return par->Descent() + ((disp_flag) ? 8 : 1);
459 int InsetFormula::width(Painter &, LyXFont const & f) const
461 lfont_size = f.size();
463 return par->Width(); //+2;
466 int InsetFormula::Width(LyXFont const & f) const
468 lfont_size = f.size();
470 return par->Width(); //+2;
476 void InsetFormula::draw(Painter & pain, LyXFont const &,
477 int baseline, float & x) const
479 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
481 lfont_size = font.size();
482 /// Let's try to wait a bit with this... (Lgb)
483 //UpdatableInset::draw(pain, font, baseline, x);
485 // otherwise a segfault could occur
486 // in some XDrawRectangles (i.e. matrix) (Matthias)
487 if (mathcursor && mathcursor->GetPar() == par) {
488 if (mathcursor->Selection()) {
492 mathcursor->SelGetArea(xp, yp, n);
493 pain.fillPolygon(xp, yp, n, LColor::selection);
495 mathcursor->draw(pain, int(x), baseline);
497 par->draw(pain, int(x), baseline);
499 x += float(width(pain, font));
501 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
502 LyXFont font = WhichFont(LM_TC_BF, par->size);
503 font.setLatex(LyXFont::OFF);
505 if (par->GetType() == LM_OT_PARN) {
508 str = string("(") + label + ")";
511 pain.text(int(x + 20), baseline, str, font);
512 } else if (par->GetType() == LM_OT_MPARN) {
513 MathMatrixInset * mt =
514 static_cast<MathMatrixInset*>(par);
516 MathedRowSt const * crow = mt->getRowSt();
518 y = baseline + crow->getBaseline();
519 if (crow->isNumbered()) {
521 if (crow->getLabel())
522 str = string("(") + crow->getLabel() + ")";
525 pain.text(int(x + 20), y, str, font);
527 crow = crow->getNext();
531 cursor_visible = false;
534 void InsetFormula::Draw(LyXFont f, LyXScreen & scr, int baseline, float & x)
536 // This is Alejandros domain so I'll use this
537 unsigned long pm = scr.getForeground();
539 lfont_size = f.size();
540 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
541 // in some XDrawRectangles (i.e. matrix) (Matthias)
542 if (mathcursor && mathcursor->GetPar() == par) {
543 if (mathcursor->Selection()) {
545 XPoint * p = mathcursor->SelGetArea(n);
546 XFillPolygon(fl_display, pm, getGC(gc_selection),
547 p, n, Nonconvex, CoordModeOrigin);
549 mathcursor->Draw(pm, int(x), baseline);
552 par->setDrawable(pm);
553 par->Draw(int(x), baseline);
555 x += float(Width(f));
557 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
559 LyXFont font = WhichFont(LM_TC_BF, par->size);
560 font.setLatex(LyXFont::OFF);
562 if (par->GetType() == LM_OT_PARN) {
564 sprintf(s, "(%s)", label.c_str());
567 font.drawString(s, pm, baseline, int(x+20));
569 if (par->GetType() == LM_OT_MPARN) {
570 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
572 MathedRowSt const* crow = mt->getRowSt();
574 y = baseline + crow->getBaseline();
575 if (crow->isNumbered()) {
576 if (crow->getLabel())
577 sprintf(s, "(%s)", crow->getLabel());
580 font.drawString(s, pm, y, int(x+20));
582 crow = crow->getNext();
586 cursor_visible = false;
591 void InsetFormula::Edit(int x, int y)
593 mathcursor = new MathedCursor(par);
594 current_view->lockInset(this);
596 UpdateInset(current_view, this, false);
599 mathcursor->SetPos(x, y);
605 void InsetFormula::InsetUnlock()
608 if (mathcursor->InMacroMode()) {
609 mathcursor->MacroModeClose();
615 UpdateInset(current_view, this, false);
619 // Now a symbol can be inserted only if the inset is locked
620 void InsetFormula::InsertSymbol(char const * s)
622 if (!s || !mathcursor) return;
623 mathcursor->Interpret(s);
628 void InsetFormula::GetCursorPos(int& x, int& y) const
630 mathcursor->GetPos(x, y);
635 void InsetFormula::ToggleInsetCursor()
641 mathcursor->GetPos(x, y);
644 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
645 asc = font.maxAscent();
646 desc = font.maxDescent();
649 current_view->hideLockedInsetCursor(x, y, asc, desc);
651 current_view->showLockedInsetCursor(x, y, asc, desc);
652 cursor_visible = !cursor_visible;
656 void InsetFormula::ShowInsetCursor()
658 if (!cursor_visible) {
661 mathcursor->GetPos(x, y);
664 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
665 asc = font.maxAscent();
666 desc = font.maxDescent();
667 current_view->fitLockedInsetCursor(x, y, asc, desc);
674 void InsetFormula::HideInsetCursor()
681 void InsetFormula::ToggleInsetSelection()
689 //mathcursor->SelGetArea(n);
690 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
694 UpdateInset(current_view, this, false);
699 void InsetFormula::display(bool dspf)
701 if (dspf != disp_flag) {
703 par->SetType(LM_OT_PAR);
704 par->SetStyle(LM_ST_DISPLAY);
706 if (par->GetType() >= LM_OT_MPAR) {
707 MathParInset * p = new MathParInset(par);
711 mathcursor->SetPar(par);
713 par->SetType(LM_OT_MIN);
714 par->SetStyle(LM_ST_TEXT);
715 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
724 int InsetFormula::GetNumberOfLabels() const
726 // This is dirty, I know. I'll clean it at 0.13
727 if (par->GetType() == LM_OT_MPARN) {
728 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
730 MathedRowSt const * crow = mt->getRowSt();
732 if (crow->getLabel()) ++nl;
733 crow = crow->getNext();
744 string InsetFormula::getLabel(int il) const
746 //#warning This is dirty, I know. Ill clean it at 0.11
747 // Correction, the only way to clean this is with a new kernel: 0.13.
748 if (par->GetType() == LM_OT_MPARN) {
750 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
752 MathedRowSt const * crow = mt->getRowSt();
754 if (crow->getLabel()) {
756 lab = crow->getLabel();
761 crow = crow->getNext();
769 void InsetFormula::UpdateLocal()
771 par->Metrics(); // To inform lyx kernel the exact size
772 // (there were problems with arrays).
773 UpdateInset(current_view, this);
777 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
782 mathcursor->SetPos(x, y);
787 UpdateInset(current_view, this, false);
792 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
795 sel_x = x; sel_y = y;
796 if (mathcursor->Selection()) {
797 mathcursor->SelClear();
798 UpdateInset(current_view, this, false);
803 void InsetFormula::InsetMotionNotify(int x, int y, int /*button*/)
805 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
808 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
809 mathcursor->SelStart();
811 mathcursor->GetPos(sel_x, sel_y);
817 mathcursor->SetPos(x, y);
819 mathcursor->GetPos(x, y);
820 if (sel_x!= x || sel_y!= y)
821 UpdateInset(current_view, this, false);
822 sel_x = x; sel_y = y;
827 void InsetFormula::InsetKeyPress(XKeyEvent *)
829 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
833 // Special Mathed functions
834 bool InsetFormula::SetNumber(bool numbf)
837 short type = par->GetType();
838 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
839 if (numbf && !oldf) ++type;
840 if (!numbf && oldf) --type;
848 bool InsetFormula::LocalDispatch(int action, char const * arg)
850 // extern char *dispatch_result;
851 MathedTextCodes varcode = LM_TC_MIN;
852 bool was_macro = mathcursor->InMacroMode();
854 bool space_on = false;
855 bool was_selection = mathcursor->Selection();
857 static MathSpaceInset * sp= 0;
860 if (mathcursor->Selection() && (fast_selection || mono_video))
861 ToggleInsetSelection();
863 if (mathcursor->getLastCode() == LM_TC_TEX) {
868 // --- Cursor Movements ---------------------------------------------
869 case LFUN_RIGHTSEL: sel = true;
872 result = mathcursor->Right(sel);
875 case LFUN_LEFTSEL: sel = true;
878 result = mathcursor->Left(sel);
881 case LFUN_UPSEL: sel = true;
883 result = mathcursor->Up(sel);
886 case LFUN_DOWNSEL: sel = true;
888 result = mathcursor->Down(sel);
896 case LFUN_DELETE_LINE_FORWARD:
897 //current_view->lockedInsetStoreUndo(Undo::INSERT);
898 current_view->lockedInsetStoreUndo(Undo::DELETE);
899 mathcursor->DelLine();
903 current_view->lockedInsetStoreUndo(Undo::INSERT);
904 mathcursor->Insert(' ', LM_TC_CR);
905 par = mathcursor->GetPar();
909 current_view->lockedInsetStoreUndo(Undo::INSERT);
910 mathcursor->Insert(0, LM_TC_TAB);
914 current_view->lockedInsetStoreUndo(Undo::INSERT);
915 mathcursor->Insert('T', LM_TC_TAB);
919 if (!mathcursor->Left())
922 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
923 UpdateInset(current_view, this);
928 //current_view->lockedInsetStoreUndo(Undo::INSERT);
929 current_view->lockedInsetStoreUndo(Undo::DELETE);
930 mathcursor->Delete();
931 UpdateInset(current_view, this);
934 // sprintf(dispatch_buffer, "%d %d",);
935 // dispatch_result = dispatch_buffer;
940 sscanf(arg, "%d %d", &x, &y);
942 mathcursor->SetPos(x1+x, y1+y);
946 /* cursor selection ---------------------------- */
950 mathcursor->MacroModeClose();
951 current_view->lockedInsetStoreUndo(Undo::INSERT);
952 mathcursor->SelPaste(); UpdateLocal(); break;
954 current_view->lockedInsetStoreUndo(Undo::DELETE);
955 mathcursor->SelCut(); UpdateLocal(); break;
956 case LFUN_COPY: mathcursor->SelCopy(); break;
959 case LFUN_WORDRIGHTSEL:
960 case LFUN_WORDLEFTSEL:
963 // --- accented characters ------------------------------
965 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
966 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
967 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
968 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
969 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
970 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
971 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
972 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
973 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
974 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
979 if (!greek_kb_flag) {
981 current_view->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
988 case LFUN_GREEK_TOGGLE:
990 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
992 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
994 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
999 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
1000 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
1001 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
1002 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
1003 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
1004 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
1007 // varcode = LM_TC_TEX;
1008 mathcursor->setLastCode(LM_TC_TEX);
1009 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1013 case LFUN_MATH_NUMBER:
1015 current_view->lockedInsetStoreUndo(Undo::INSERT);
1017 short type = par->GetType();
1018 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
1021 if (!label.empty()) {
1024 current_view->owner()->getMiniBuffer()->Set(_("No number"));
1027 current_view->owner()->getMiniBuffer()->Set(_("Number"));
1035 case LFUN_MATH_NONUMBER:
1037 if (par->GetType() == LM_OT_MPARN) {
1038 // MathMatrixInset *mt = (MathMatrixInset*)par;
1040 // mt->SetNumbered(!mt->IsNumbered());
1042 mathcursor->setNumbered();
1048 case LFUN_MATH_LIMITS:
1050 current_view->lockedInsetStoreUndo(Undo::INSERT);
1051 if (mathcursor->Limits())
1055 case LFUN_MATH_SIZE:
1057 latexkeys * l = in_word_set (arg, strlen(arg));
1058 int sz = (l) ? l->id: -1;
1059 mathcursor->SetSize(sz);
1064 case LFUN_INSERT_MATH:
1066 current_view->lockedInsetStoreUndo(Undo::INSERT);
1071 case LFUN_INSERT_MATRIX:
1073 current_view->lockedInsetStoreUndo(Undo::INSERT);
1075 char s[80], arg2[80];
1076 // This is just so that too long args won't ooze out of s.
1077 strncpy(arg2, arg, 80); arg2[79]= '\0';
1078 k = sscanf(arg2, "%d %d %s", &m, &n, s);
1083 } else if (k == 1) {
1087 MathMatrixInset * p = new MathMatrixInset(m, n);
1088 if (mathcursor && p) {
1089 if (k > 2 && int(strlen(s)) > m)
1090 p->SetAlign(s[0], &s[1]);
1091 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1097 case LFUN_MATH_DELIM:
1099 current_view->lockedInsetStoreUndo(Undo::INSERT);
1100 char lf[40], rg[40], arg2[40];
1101 int ilf = '(', irg = '.';
1103 string vdelim("(){}[]./|");
1106 strncpy(arg2, arg, 40); arg2[39]= '\0';
1107 int n = sscanf(arg2, "%s %s", lf, rg);
1108 lf[39] = '\0'; rg[39] = '\0';
1115 l = in_word_set(lf, strlen(lf));
1116 // Long words will cause l == 0; so check.
1119 if (vdelim.find(lf[0]) != string::npos)
1127 l = in_word_set(rg, strlen(rg));
1130 if (vdelim.find(rg[0]) != string::npos)
1135 MathDelimInset * p = new MathDelimInset(ilf, irg);
1136 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1141 case LFUN_PROTECTEDSPACE:
1143 current_view->lockedInsetStoreUndo(Undo::INSERT);
1144 sp = new MathSpaceInset(1);
1145 mathcursor->Insert(sp);
1151 case LFUN_INSERT_LABEL:
1153 current_view->lockedInsetStoreUndo(Undo::INSERT);
1154 if (par->GetType() < LM_OT_PAR) break;
1158 result = askForText(_("Enter new label to insert:"));
1163 if (!lb.empty() && lb[0] > ' ') {
1165 if (par->GetType() == LM_OT_MPARN) {
1166 mathcursor->setLabel(lb.c_str());
1167 // MathMatrixInset *mt = (MathMatrixInset*)par;
1168 // mt->SetLabel(lb);
1170 //if (label.notEmpty()) delete label;
1179 case LFUN_MATH_DISPLAY:
1180 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1181 current_view->lockedInsetStoreUndo(Undo::EDIT);
1182 display(!disp_flag);
1186 // Invalid actions under math mode
1187 case LFUN_MATH_MODE:
1189 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1190 current_view->owner()->getMiniBuffer()->Set(_("math text mode"));
1191 varcode = LM_TC_TEXTRM;
1193 varcode = LM_TC_VAR;
1195 mathcursor->setLastCode(varcode);
1199 current_view->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1202 //------- dummy actions
1203 case LFUN_EXEC_COMMAND:
1204 current_view->owner()->getMiniBuffer()->ExecCommand();
1208 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1209 unsigned char c = arg[0];
1210 current_view->lockedInsetStoreUndo(Undo::INSERT);
1212 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1214 mathcursor->setAccent(0);
1216 if (c == 0) { // Dead key, do nothing
1217 //lyxerr << "deadkey" << endl;
1221 if (mathcursor->getLastCode() == LM_TC_TEX) {
1222 mathcursor->MacroModeOpen();
1223 mathcursor->clearLastCode();
1224 varcode = LM_TC_MIN;
1227 short f = (mathcursor->getLastCode()) ?
1228 mathcursor->getLastCode() :
1229 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1230 varcode = MathIsAlphaFont(f) ?
1231 static_cast<MathedTextCodes>(f) :
1235 // lyxerr << "Varcode << vardoce;
1236 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1237 varcode = LM_TC_MIN;
1238 if (greek_kb_flag<2) greek_kb_flag = 0;
1240 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1241 mathcursor->Insert(c, LM_TC_TEX);
1243 mathcursor->Insert('}', LM_TC_TEX);
1246 mathcursor->clearLastCode();
1247 // varcode = LM_TC_MIN;
1249 if (c == '_' && varcode == LM_TC_TEX) {
1250 mathcursor->Insert(c, LM_TC_SPECIAL);
1251 mathcursor->clearLastCode();
1252 // varcode = LM_TC_MIN;
1254 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1255 mathcursor->MacroModeOpen();
1256 mathcursor->clearLastCode();
1257 mathcursor->Insert(c, LM_TC_MIN);
1260 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1261 mathcursor->Insert(c, LM_TC_CONST);
1263 if (strchr("+/-*<>=", c))
1264 mathcursor->Insert(c, LM_TC_BOP);
1266 if (strchr(latex_special_chars, c) && c!= '_')
1267 mathcursor->Insert(c, LM_TC_SPECIAL);
1269 if (c == '_' || c == '^') {
1273 mathcursor->Interpret (s);
1277 short f = (mathcursor->getLastCode()) ?
1278 mathcursor->getLastCode() :
1279 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1280 varcode = MathIsAlphaFont(f) ?
1281 static_cast<MathedTextCodes>(f) :
1284 if (varcode == LM_TC_TEXTRM) {
1285 mathcursor->Insert(c, LM_TC_TEXTRM);
1288 mathcursor->MacroModeClose();
1291 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1295 if (!mathcursor->Pop() && mathcursor->IsEnd())
1300 mathcursor->Insert (c, LM_TC_VAR);
1304 mathcursor->MacroModeClose();
1305 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1306 mathcursor->setLastCode(LM_TC_TEX);
1310 // lyxerr << "Closed by action " << action << endl;
1314 if (was_macro != mathcursor->InMacroMode()
1316 && action != LFUN_BACKSPACE)
1318 if (sp && !space_on) sp = 0;
1319 if (mathcursor->Selection() || (was_selection && !(fast_selection || mono_video)))
1320 ToggleInsetSelection();
1325 current_view->unlockInset(this);
1333 MathFuncInset::draw(Painter & pain, int x, int y)
1335 if (name && name[0] > ' ') {
1336 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1337 font.setLatex(LyXFont::ON);
1338 x += (font.textWidth("I", 1) + 3) / 4;
1340 int a = font.maxAscent();
1341 int d = font.maxDescent();
1342 pain.fillRectangle(x, y - a, font.textWidth(name, strlen(name)), a + d);
1344 pain.text(x, y, name, font);
1349 MathFuncInset::Draw(int x, int y)
1351 if (name && name[0] > ' ') {
1352 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1353 font.setLatex(LyXFont::ON);
1354 x += (font.textWidth("I", 1)+3)/4;
1356 int a = font.maxAscent(), d = font.maxDescent();
1357 XFillRectangle (fl_display, pm, getGC(gc_copy),
1359 font.textWidth(name, strlen(name)), a+d);
1362 font.drawString(name, pm, y, x);
1367 void MathFuncInset::Metrics()
1369 ln = (name) ? strlen(name): 0;
1370 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1371 font.setLatex(LyXFont::ON);
1372 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1373 mathed_string_height(LM_TC_TEXTRM, size,
1374 reinterpret_cast<unsigned char const *>(name),
1375 strlen(name), ascent, descent);
1380 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1382 MathedIter it(par->GetData());
1384 while (it.OK() && !(features.binom && features.boldsymbol)) {
1387 MathParInset * p = it.GetActiveInset();
1388 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1389 strcmp(p->GetName(), "binom") == 0) {
1390 features.binom = true;
1392 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1393 p->setArgumentIdx(i);
1394 mathedValidate(features, p);
1398 MathedInset* p = it.GetInset();
1399 if (!features.boldsymbol && p->GetName() &&
1400 strcmp(p->GetName(), "boldsymbol") == 0) {
1401 features.boldsymbol = true;