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: 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.
21 #pragma implementation "formula.h"
25 #include "commandtags.h"
26 #include "math_cursor.h"
27 #include "math_parser.h"
29 #include "minibuffer.h"
30 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
36 #include "support/LOstream.h"
40 #include "support/lyxlib.h"
58 extern string mathed_label;
60 extern char const * latex_special_chars;
62 int 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;
73 MathedCursor * InsetFormula::mathcursor = 0;
76 int MathedInset::df_asc;
77 int MathedInset::df_des;
78 int MathedInset::df_width;
82 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
85 LyXFont WhichFont(short type, int size)
115 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
125 f.setSize(lfont_size);
129 if (type == LM_TC_BSYM) {
139 case LM_ST_SCRIPTSCRIPT:
144 lyxerr << "Mathed Error: wrong font size: " << size << endl;
148 if (type != LM_TC_TEXTRM)
149 f.setColor(LColor::math);
154 void mathed_init_fonts() //removed 'static' because DEC cxx does not
156 // Probably because this func is declared as a friend in math_defs.h
160 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
162 for (int i = 0 ; i < 8 ; ++i) {
163 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
165 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
167 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
169 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
170 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
172 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
174 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
175 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
177 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
179 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
181 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
183 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
184 MathedInset::df_asc = lyxfont::maxAscent(f);
185 MathedInset::df_des = lyxfont::maxDescent(f);
186 MathedInset::df_width = lyxfont::width('I', f);
190 LyXFont mathed_get_font(short type, int size)
192 LyXFont f = WhichFont(type, size);
193 if (type == LM_TC_TEX) {
194 f.setLatex(LyXFont::ON);
200 int mathed_string_width(short type, int size, string const & s)
203 if (MathIsBinary(type))
204 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
212 LyXFont const f = WhichFont(type, size);
213 return lyxfont::width(st, f);
216 int mathed_char_width(short type, int size, byte c)
218 if (MathIsBinary(type)) {
221 return mathed_string_width(type, size, s);
224 return lyxfont::width(c, WhichFont(type, size));
228 int mathed_string_height(short type, int size, string const & s,
229 int & asc, int & des)
231 LyXFont font = WhichFont(type, size);
233 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
234 des = max(des, lyxfont::descent(*it, font));
235 asc = max(asc, lyxfont::ascent(*it, font));
241 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
243 LyXFont font = WhichFont(type, size);
244 des = lyxfont::descent(c, font);
245 asc = lyxfont::ascent(c, font);
250 // In a near future maybe we use a better fonts renderer
251 void MathedInset::drawStr(Painter & pain, short type, int siz,
252 int x, int y, string const & s)
255 if (MathIsBinary(type))
256 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
264 LyXFont const mf = mathed_get_font(type, siz);
265 pain.text(x, y, st, mf);
269 InsetFormula::InsetFormula(bool display)
271 par = new MathParInset; // this leaks
276 par->SetType(LM_OT_PAR);
277 par->SetStyle(LM_ST_DISPLAY);
282 InsetFormula::InsetFormula(MathParInset * p)
284 if (is_matrix_type(p->GetType()))
285 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
286 par = is_multiline(p->GetType()) ?
287 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
291 disp_flag = (par->GetType()>0);
296 InsetFormula::~InsetFormula()
302 Inset * InsetFormula::Clone(Buffer const &) const
304 InsetFormula * f = new InsetFormula(par);
310 void InsetFormula::Write(Buffer const * buf, ostream & os) const
313 Latex(buf, os, false, false);
317 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
320 //#warning Alejandro, the number of lines is not returned in this case
321 // This problem will disapear at 0.13.
322 mathed_write(par, os, &ret, fragile, label);
327 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
329 par->Write(os, false);
334 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
336 return Ascii(buf, os, 0);
340 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
342 return Ascii(buf, os, 0);
346 // Check if uses AMS macros
347 void InsetFormula::Validate(LaTeXFeatures & features) const
349 if (is_ams(par->GetType()))
350 features.amsstyle = true;
352 // Validation is necessary only if not using AMS math.
353 // To be safe, we will always run mathedValidate.
354 //if (!features.amsstyle)
355 mathedValidate(features, par);
359 void InsetFormula::Read(Buffer const *, LyXLex & lex)
361 istream & is = lex.getStream();
363 mathed_parser_file(is, lex.GetLineNo());
365 // Silly hack to read labels.
367 mathed_label.erase();
369 mathed_parse(0, 0, &par);
371 disp_flag = (par->GetType() > 0);
373 // Update line number
374 lex.setLineNo(mathed_parser_lineno());
376 //if (mathed_label) {
377 if (!mathed_label.empty()) {
378 label = mathed_label;
380 mathed_label.erase();
383 // reading of end_inset in the inset!!!
386 if (lex.GetString() == "\\end_inset")
388 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
389 " or missing \\end_inset!" << endl;
398 int InsetFormula::ascent(BufferView *, LyXFont const &) const
400 return par->Ascent() + ((disp_flag) ? 8 : 1);
404 int InsetFormula::descent(BufferView *, LyXFont const &) const
406 return par->Descent() + ((disp_flag) ? 8 : 1);
410 int InsetFormula::width(BufferView *, LyXFont const & f) const
412 lfont_size = f.size();
414 return par->Width(); //+2;
418 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
419 int baseline, float & x, bool) const
421 Painter & pain = bv->painter();
422 // Seems commenting out solves a problem.
423 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
424 font.setSize(f.size());
425 lfont_size = font.size();
426 /// Let's try to wait a bit with this... (Lgb)
427 //UpdatableInset::draw(pain, font, baseline, x);
429 // otherwise a segfault could occur
430 // in some XDrawRectangles (i.e. matrix) (Matthias)
431 if (mathcursor && mathcursor->GetPar() == par) {
432 if (mathcursor->Selection()) {
436 mathcursor->SelGetArea(&xp, &yp, n);
437 pain.fillPolygon(xp, yp, n, LColor::selection);
439 mathcursor->draw(pain, int(x), baseline);
441 par->draw(pain, int(x), baseline);
443 x += float(width(bv, font));
445 if (is_numbered(par->GetType())) {
446 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
447 wfont.setLatex(LyXFont::OFF);
449 if (is_singlely_numbered(par->GetType())) {
452 str = string("(") + label + ")";
455 pain.text(int(x + 20), baseline, str, wfont);
457 MathMatrixInset * mt =
458 static_cast<MathMatrixInset*>(par);
460 MathedRowSt const * crow = mt->getRowSt();
462 y = baseline + crow->getBaseline();
463 if (crow->isNumbered()) {
465 if (!crow->getLabel().empty())
466 str = string("(") + crow->getLabel() + ")";
469 pain.text(int(x + 20), y, str, wfont);
471 crow = crow->getNext();
475 cursor_visible = false;
479 string const InsetFormula::EditMessage() const
481 return _("Math editor mode");
485 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
487 mathcursor = new MathedCursor(par);
488 if (!bv->lockInset(this))
489 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
491 bv->updateInset(this, false);
494 mathcursor->SetPos(x, y);
500 void InsetFormula::InsetUnlock(BufferView * bv)
503 if (mathcursor->InMacroMode()) {
504 mathcursor->MacroModeClose();
510 bv->updateInset(this, false);
514 // Now a symbol can be inserted only if the inset is locked
515 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
517 if (s.empty() || !mathcursor) return;
518 mathcursor->Interpret(s);
523 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
525 mathcursor->GetPos(x, y);
531 void InsetFormula::ToggleInsetCursor(BufferView * bv)
538 mathcursor->GetPos(x, y);
541 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
542 int asc = lyxfont::maxAscent(font);
543 int desc = lyxfont::maxDescent(font);
546 bv->hideLockedInsetCursor();
548 bv->showLockedInsetCursor(x, y, asc, desc);
549 cursor_visible = !cursor_visible;
553 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
555 if (!cursor_visible) {
559 mathcursor->GetPos(x, y);
562 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
563 int asc = lyxfont::maxAscent(font);
564 int desc = lyxfont::maxDescent(font);
565 bv->fitLockedInsetCursor(x, y, asc, desc);
567 ToggleInsetCursor(bv);
572 void InsetFormula::HideInsetCursor(BufferView * bv)
575 ToggleInsetCursor(bv);
579 void InsetFormula::ToggleInsetSelection(BufferView * bv)
587 //mathcursor->SelGetArea(n);
588 // XFillPolygon(fl_get_display(), pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
592 bv->updateInset(this, false);
597 void InsetFormula::display(bool dspf)
599 if (dspf != disp_flag) {
601 par->SetType(LM_OT_PAR);
602 par->SetStyle(LM_ST_DISPLAY);
604 if (is_multiline(par->GetType())) {
605 MathParInset * p = new MathParInset(par);
609 mathcursor->SetPar(par);
611 par->SetType(LM_OT_MIN);
612 par->SetStyle(LM_ST_TEXT);
613 if (!label.empty()) {
622 vector<string> const InsetFormula::getLabelList() const
624 //#warning This is dirty, I know. Ill clean it at 0.11
625 // Correction, the only way to clean this is with a new kernel: 0.13.
627 vector<string> label_list;
629 if (is_multi_numbered(par->GetType())) {
630 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
631 MathedRowSt const * crow = mt->getRowSt();
633 if (!crow->getLabel().empty())
634 label_list.push_back(crow->getLabel());
635 crow = crow->getNext();
637 } else if (!label.empty())
638 label_list.push_back(label);
644 void InsetFormula::UpdateLocal(BufferView * bv)
646 par->Metrics(); // To inform lyx kernel the exact size
647 // (there were problems with arrays).
648 bv->updateInset(this, true);
652 void InsetFormula::InsetButtonRelease(BufferView * bv,
653 int x, int y, int /*button*/)
659 mathcursor->SetPos(x, y);
664 bv->updateInset(this, false);
670 void InsetFormula::InsetButtonPress(BufferView * bv,
671 int x, int y, int /*button*/)
674 sel_x = x; sel_y = y;
675 if (mathcursor && mathcursor->Selection()) {
676 mathcursor->SelClear();
677 bv->updateInset(this, false);
682 void InsetFormula::InsetMotionNotify(BufferView * bv,
683 int x, int y, int /*button*/)
685 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
688 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
689 mathcursor->SelStart();
691 mathcursor->GetPos(sel_x, sel_y);
692 } else if (sel_flag) {
696 mathcursor->SetPos(x, y);
698 mathcursor->GetPos(x, y);
699 if (sel_x!= x || sel_y!= y)
700 bv->updateInset(this, false);
701 sel_x = x; sel_y = y;
706 void InsetFormula::InsetKeyPress(XKeyEvent *)
708 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
712 // Special Mathed functions
713 bool InsetFormula::SetNumber(bool numbf)
716 short type = par->GetType();
717 bool oldf = is_numbered(type);
718 if (numbf && !oldf) ++type;
719 if (!numbf && oldf) --type;
727 UpdatableInset::RESULT
728 InsetFormula::LocalDispatch(BufferView * bv,
729 int action, string const & arg)
731 // extern char *dispatch_result;
732 MathedTextCodes varcode = LM_TC_MIN;
733 bool was_macro = mathcursor->InMacroMode();
735 bool space_on = false;
736 bool was_selection = mathcursor->Selection();
737 RESULT result = DISPATCHED;
738 static MathSpaceInset * sp= 0;
742 if (mathcursor->getLastCode() == LM_TC_TEX) {
747 // --- Cursor Movements ---------------------------------------------
748 case LFUN_RIGHTSEL: sel = true;
751 result = DISPATCH_RESULT(mathcursor->Right(sel));
752 if (!sel && (result == DISPATCHED))
753 result = DISPATCHED_NOUPDATE;
756 case LFUN_LEFTSEL: sel = true;
759 result = DISPATCH_RESULT(mathcursor->Left(sel));
760 if (!sel && (result == DISPATCHED))
761 result = DISPATCHED_NOUPDATE;
764 case LFUN_UPSEL: sel = true;
766 result = DISPATCH_RESULT(mathcursor->Up(sel));
767 if (!sel && (result == DISPATCHED))
768 result = DISPATCHED_NOUPDATE;
771 case LFUN_DOWNSEL: sel = true;
773 result = DISPATCH_RESULT(mathcursor->Down(sel));
774 if (!sel && (result == DISPATCHED))
775 result = DISPATCHED_NOUPDATE;
779 result = DISPATCHED_NOUPDATE;
783 result = DISPATCHED_NOUPDATE;
785 case LFUN_DELETE_LINE_FORWARD:
786 bv->lockedInsetStoreUndo(Undo::DELETE);
787 mathcursor->DelLine();
792 bv->lockedInsetStoreUndo(Undo::INSERT);
793 byte c = arg.empty() ? 'e' : arg[0];
794 mathcursor->Insert(c, LM_TC_CR);
795 if (!label.empty()) {
796 mathcursor->setLabel(label);
799 par = mathcursor->GetPar();
804 bv->lockedInsetStoreUndo(Undo::INSERT);
805 mathcursor->Insert(0, LM_TC_TAB);
809 bv->lockedInsetStoreUndo(Undo::INSERT);
810 mathcursor->Insert('T', LM_TC_TAB);
814 if (!mathcursor->Left())
817 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
818 bv->updateInset(this, true);
823 bv->lockedInsetStoreUndo(Undo::DELETE);
824 mathcursor->Delete();
825 bv->updateInset(this, true);
828 // sprintf(dispatch_buffer, "%d %d",);
829 // dispatch_result = dispatch_buffer;
837 istringstream ist(arg.c_str());
840 mathcursor->SetPos(x1 + x, y1 + y);
844 /* cursor selection ---------------------------- */
848 mathcursor->MacroModeClose();
849 bv->lockedInsetStoreUndo(Undo::INSERT);
850 mathcursor->SelPaste(); UpdateLocal(bv); break;
852 bv->lockedInsetStoreUndo(Undo::DELETE);
853 mathcursor->SelCut(); UpdateLocal(bv); break;
854 case LFUN_COPY: mathcursor->SelCopy(); break;
857 case LFUN_WORDRIGHTSEL:
858 case LFUN_WORDLEFTSEL:
861 // --- accented characters ------------------------------
863 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
864 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
865 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
866 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
867 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
868 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
869 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
870 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
871 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
872 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
877 if (!greek_kb_flag) {
879 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
886 case LFUN_GREEK_TOGGLE:
888 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
890 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
892 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
897 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
898 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
899 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
900 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
901 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
902 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
905 // varcode = LM_TC_TEX;
906 mathcursor->setLastCode(LM_TC_TEX);
907 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
911 case LFUN_MATH_NUMBER:
913 bv->lockedInsetStoreUndo(Undo::INSERT);
915 short type = par->GetType();
916 if (is_numbered(type)) {
918 if (!label.empty()) {
921 bv->owner()->getMiniBuffer()->Set(_("No number"));
924 bv->owner()->getMiniBuffer()->Set(_("Number"));
932 case LFUN_MATH_NONUMBER:
934 if (is_multi_numbered(par->GetType())) {
935 // MathMatrixInset *mt = (MathMatrixInset*)par;
937 // mt->SetNumbered(!mt->IsNumbered());
939 #warning This is a terrible hack! We should find a better solution.
940 while (mathcursor->getLabel() == MathedXIter::error_label) {
941 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
944 mathcursor->setNumbered();
950 case LFUN_MATH_LIMITS:
952 bv->lockedInsetStoreUndo(Undo::INSERT);
953 if (mathcursor->Limits())
959 latexkeys * l = in_word_set (arg);
960 int sz = (l) ? l->id: -1;
961 mathcursor->SetSize(sz);
966 case LFUN_INSERT_MATH:
968 bv->lockedInsetStoreUndo(Undo::INSERT);
969 InsertSymbol(bv, arg);
973 case LFUN_INSERT_MATRIX:
975 bv->lockedInsetStoreUndo(Undo::INSERT);
977 char s[80], arg2[80];
978 // This is just so that too long args won't ooze out of s.
979 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
980 k = sscanf(arg2, "%d %d %s", &m, &n, s);
989 MathMatrixInset * p = new MathMatrixInset(m, n);
990 if (mathcursor && p) {
991 if (k > 2 && int(strlen(s)) > m)
992 p->SetAlign(s[0], &s[1]);
993 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
999 case LFUN_MATH_DELIM:
1001 bv->lockedInsetStoreUndo(Undo::INSERT);
1002 char lf[40], rg[40], arg2[40];
1003 int ilf = '(', irg = '.';
1005 string vdelim("(){}[]./|");
1009 ::strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1010 int n = sscanf(arg2, "%s %s", lf, rg);
1011 lf[39] = '\0'; rg[39] = '\0';
1015 ilf = lyx::atoi(lf);
1018 l = in_word_set(lf, strlen(lf));
1019 // Long words will cause l == 0; so check.
1022 if (vdelim.find(lf[0]) != string::npos)
1027 irg = lyx::atoi(rg);
1030 l = in_word_set(rg, strlen(rg));
1033 if (vdelim.find(rg[0]) != string::npos)
1038 MathDelimInset * p = new MathDelimInset(ilf, irg);
1039 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1044 case LFUN_PROTECTEDSPACE:
1046 bv->lockedInsetStoreUndo(Undo::INSERT);
1047 sp = new MathSpaceInset(1);
1048 mathcursor->Insert(sp);
1054 case LFUN_INSERT_LABEL:
1056 bv->lockedInsetStoreUndo(Undo::INSERT);
1057 if (par->GetType() < LM_OT_PAR)
1060 string old_label = is_multiline(par->GetType())
1061 ? mathcursor->getLabel() : label;
1063 #warning This is a terrible hack! We should find a better solution.
1064 /// This is needed becuase in some positions mathcursor->cursor->crow
1065 /// is equal to 0, and therefore the label cannot be inserted.
1066 /// So we move the cursor left until mathcursor->cursor->crow != 0.
1067 while (old_label == MathedXIter::error_label) {
1068 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1070 old_label = mathcursor->getLabel();
1073 string new_label = arg;
1074 if (new_label.empty()) {
1075 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1076 pair<bool, string> res = old_label.empty()
1077 ? askForText(_("Enter new label to insert:"), default_label)
1078 : askForText(_("Enter label:"), old_label);
1081 new_label = frontStrip(strip(res.second));
1084 if (new_label == old_label)
1085 break; // Nothing to do
1087 if (!new_label.empty())
1090 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1093 if (is_multi_numbered(par->GetType()))
1094 mathcursor->setLabel(new_label);
1095 // MathMatrixInset *mt = (MathMatrixInset*)par;
1096 // mt->SetLabel(new_label);
1103 case LFUN_MATH_DISPLAY:
1104 bv->lockedInsetStoreUndo(Undo::EDIT);
1105 display(!disp_flag);
1109 // Invalid actions under math mode
1110 case LFUN_MATH_MODE:
1112 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1113 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1114 varcode = LM_TC_TEXTRM;
1116 varcode = LM_TC_VAR;
1118 mathcursor->setLastCode(varcode);
1122 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1125 //------- dummy actions
1126 case LFUN_EXEC_COMMAND:
1127 bv->owner()->getMiniBuffer()->PrepareForCommand();
1131 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1132 unsigned char c = arg[0];
1133 bv->lockedInsetStoreUndo(Undo::INSERT);
1135 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1137 mathcursor->setAccent(0);
1139 if (c == 0) { // Dead key, do nothing
1140 //lyxerr << "deadkey" << endl;
1144 if (mathcursor->getLastCode() == LM_TC_TEX) {
1145 mathcursor->MacroModeOpen();
1146 mathcursor->clearLastCode();
1147 varcode = LM_TC_MIN;
1150 short f = (mathcursor->getLastCode()) ?
1151 mathcursor->getLastCode() :
1152 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1153 varcode = MathIsAlphaFont(f) ?
1154 static_cast<MathedTextCodes>(f) :
1158 // lyxerr << "Varcode << vardoce;
1159 MathedTextCodes char_code = varcode;
1160 if (greek_kb_flag) {
1162 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1163 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1164 'Y', 0, 0, 0, 0 , 'Z' };
1165 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1166 char_code = LM_TC_RM;
1169 char_code = LM_TC_SYMB;
1171 mathcursor->Insert(c, char_code);
1172 if (greek_kb_flag && char_code == LM_TC_RM )
1173 mathcursor->setLastCode(LM_TC_VAR);
1174 varcode = LM_TC_MIN;
1175 if (greek_kb_flag<2) greek_kb_flag = 0;
1177 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1178 mathcursor->Insert(c, LM_TC_TEX);
1180 mathcursor->Insert('}', LM_TC_TEX);
1183 mathcursor->clearLastCode();
1184 // varcode = LM_TC_MIN;
1186 if (c == '_' && varcode == LM_TC_TEX) {
1187 mathcursor->Insert(c, LM_TC_SPECIAL);
1188 mathcursor->clearLastCode();
1189 // varcode = LM_TC_MIN;
1191 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1192 mathcursor->MacroModeOpen();
1193 mathcursor->clearLastCode();
1194 mathcursor->Insert(c, LM_TC_MIN);
1197 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1198 mathcursor->Insert(c, LM_TC_CONST);
1200 if (strchr("+/-*<>=", c))
1201 mathcursor->Insert(c, LM_TC_BOP);
1203 if (strchr(latex_special_chars, c) && c!= '_')
1204 mathcursor->Insert(c, LM_TC_SPECIAL);
1206 if (c == '_' || c == '^') {
1210 mathcursor->Interpret (s);
1214 short f = (mathcursor->getLastCode()) ?
1215 mathcursor->getLastCode() :
1216 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1217 varcode = MathIsAlphaFont(f) ?
1218 static_cast<MathedTextCodes>(f) :
1221 if (varcode == LM_TC_TEXTRM) {
1222 mathcursor->Insert(c, LM_TC_TEXTRM);
1225 mathcursor->MacroModeClose();
1228 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1232 if (!mathcursor->Pop() && mathcursor->IsEnd())
1236 if (c == '\'' || c == '@') {
1237 mathcursor->Insert (c, LM_TC_VAR);
1241 mathcursor->MacroModeClose();
1242 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1243 mathcursor->setLastCode(LM_TC_TEX);
1246 } else if (action == LFUN_MATH_PANEL) {
1247 result = UNDISPATCHED;
1249 // lyxerr << "Closed by action " << action << endl;
1253 if (was_macro != mathcursor->InMacroMode()
1255 && action != LFUN_BACKSPACE)
1257 if (sp && !space_on) sp = 0;
1258 if (mathcursor->Selection() || was_selection)
1259 ToggleInsetSelection(bv);
1261 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1262 (result == UNDISPATCHED))
1263 ShowInsetCursor(bv);
1265 bv->unlockInset(this);
1272 MathFuncInset::draw(Painter & pain, int x, int y)
1274 if (!name.empty() && name[0] > ' ') {
1275 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1276 font.setLatex(LyXFont::ON);
1277 x += (lyxfont::width('I', font) + 3) / 4;
1278 pain.text(x, y, name, font);
1283 void MathFuncInset::Metrics()
1285 //ln = (name) ? strlen(name): 0;
1286 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1287 font.setLatex(LyXFont::ON);
1293 width = lyxfont::width(name, font)
1294 + lyxfont::width('I', font) / 2;
1295 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1301 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1303 MathedIter it(par->GetData());
1305 while (it.OK() && !(features.binom && features.boldsymbol)) {
1307 if (it.IsActive()) {
1308 MathParInset * p = it.GetActiveInset();
1309 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1310 p->GetName() == "binom") {
1311 features.binom = true;
1313 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1314 p->setArgumentIdx(i);
1315 mathedValidate(features, p);
1319 MathedInset* p = it.GetInset();
1320 if (!features.boldsymbol &&
1321 p->GetName() == "boldsymbol") {
1322 features.boldsymbol = true;