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;
79 int MathedInset::workWidth;
83 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
86 LyXFont WhichFont(short type, int size)
116 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
126 f.setSize(lfont_size);
130 if (type == LM_TC_BSYM) {
140 case LM_ST_SCRIPTSCRIPT:
145 lyxerr << "Mathed Error: wrong font size: " << size << endl;
149 if (type != LM_TC_TEXTRM)
150 f.setColor(LColor::math);
155 void mathed_init_fonts() //removed 'static' because DEC cxx does not
157 // Probably because this func is declared as a friend in math_defs.h
161 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
163 for (int i = 0 ; i < 8 ; ++i) {
164 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
166 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
170 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
171 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
175 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
176 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
178 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
180 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
182 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
184 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
185 MathedInset::df_asc = lyxfont::maxAscent(f);
186 MathedInset::df_des = lyxfont::maxDescent(f);
187 MathedInset::df_width = lyxfont::width('I', f);
191 LyXFont mathed_get_font(short type, int size)
193 LyXFont f = WhichFont(type, size);
194 if (type == LM_TC_TEX) {
195 f.setLatex(LyXFont::ON);
201 int mathed_string_width(short type, int size, string const & s)
204 if (MathIsBinary(type))
205 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
213 LyXFont const f = WhichFont(type, size);
214 return lyxfont::width(st, f);
217 int mathed_char_width(short type, int size, byte c)
219 if (MathIsBinary(type)) {
222 return mathed_string_width(type, size, s);
225 return lyxfont::width(c, WhichFont(type, size));
229 int mathed_string_height(short type, int size, string const & s,
230 int & asc, int & des)
232 LyXFont font = WhichFont(type, size);
234 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
235 des = max(des, lyxfont::descent(*it, font));
236 asc = max(asc, lyxfont::ascent(*it, font));
242 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
244 LyXFont font = WhichFont(type, size);
245 des = lyxfont::descent(c, font);
246 asc = lyxfont::ascent(c, font);
251 // In a near future maybe we use a better fonts renderer
252 void MathedInset::drawStr(Painter & pain, short type, int siz,
253 int x, int y, string const & s)
256 if (MathIsBinary(type))
257 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
265 LyXFont const mf = mathed_get_font(type, siz);
266 pain.text(x, y, st, mf);
270 InsetFormula::InsetFormula(bool display)
272 par = new MathParInset; // this leaks
277 par->SetType(LM_OT_PAR);
278 par->SetStyle(LM_ST_DISPLAY);
283 InsetFormula::InsetFormula(MathParInset * p)
285 if (is_matrix_type(p->GetType()))
286 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
287 par = is_multiline(p->GetType()) ?
288 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
292 disp_flag = (par->GetType()>0);
297 InsetFormula::~InsetFormula()
303 Inset * InsetFormula::Clone(Buffer const &) const
305 InsetFormula * f = new InsetFormula(par);
311 void InsetFormula::Write(Buffer const * buf, ostream & os) const
314 Latex(buf, os, false, false);
318 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
321 //#warning Alejandro, the number of lines is not returned in this case
322 // This problem will disapear at 0.13.
323 mathed_write(par, os, &ret, fragile, label);
328 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
330 par->Write(os, false);
335 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
337 return Ascii(buf, os, 0);
341 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
343 return Ascii(buf, os, 0);
347 // Check if uses AMS macros
348 void InsetFormula::Validate(LaTeXFeatures & features) const
350 if (is_ams(par->GetType()))
351 features.amsstyle = true;
353 // Validation is necessary only if not using AMS math.
354 // To be safe, we will always run mathedValidate.
355 //if (!features.amsstyle)
356 mathedValidate(features, par);
360 void InsetFormula::Read(Buffer const *, LyXLex & lex)
362 istream & is = lex.getStream();
364 mathed_parser_file(is, lex.GetLineNo());
366 // Silly hack to read labels.
368 mathed_label.erase();
370 mathed_parse(0, 0, &par);
372 disp_flag = (par->GetType() > 0);
374 // Update line number
375 lex.setLineNo(mathed_parser_lineno());
377 //if (mathed_label) {
378 if (!mathed_label.empty()) {
379 label = mathed_label;
381 mathed_label.erase();
384 // reading of end_inset in the inset!!!
387 if (lex.GetString() == "\\end_inset")
389 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
390 " or missing \\end_inset!" << endl;
399 int InsetFormula::ascent(BufferView *, LyXFont const &) const
401 return par->Ascent() + ((disp_flag) ? 8 : 1);
405 int InsetFormula::descent(BufferView *, LyXFont const &) const
407 return par->Descent() + ((disp_flag) ? 8 : 1);
411 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
413 MathedInset::workWidth = bv->workWidth();
414 lfont_size = f.size();
416 return par->Width(); //+2;
420 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
421 int baseline, float & x, bool) const
423 MathedInset::workWidth = bv->workWidth();
424 Painter & pain = bv->painter();
425 // Seems commenting out solves a problem.
426 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
427 font.setSize(f.size());
428 lfont_size = font.size();
429 /// Let's try to wait a bit with this... (Lgb)
430 //UpdatableInset::draw(pain, font, baseline, x);
432 // otherwise a segfault could occur
433 // in some XDrawRectangles (i.e. matrix) (Matthias)
434 if (mathcursor && mathcursor->GetPar() == par) {
435 if (mathcursor->Selection()) {
439 mathcursor->SelGetArea(&xp, &yp, n);
440 pain.fillPolygon(xp, yp, n, LColor::selection);
442 mathcursor->draw(pain, int(x), baseline);
444 par->draw(pain, int(x), baseline);
446 x += float(width(bv, font));
448 if (is_numbered(par->GetType())) {
449 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
450 wfont.setLatex(LyXFont::OFF);
452 if (is_singlely_numbered(par->GetType())) {
455 str = string("(") + label + ")";
458 pain.text(int(x + 20), baseline, str, wfont);
460 MathMatrixInset * mt =
461 static_cast<MathMatrixInset*>(par);
463 MathedRowSt const * crow = mt->getRowSt();
465 y = baseline + crow->getBaseline();
466 if (crow->isNumbered()) {
468 if (!crow->getLabel().empty())
469 str = string("(") + crow->getLabel() + ")";
472 pain.text(int(x + 20), y, str, wfont);
474 crow = crow->getNext();
478 cursor_visible = false;
482 string const InsetFormula::EditMessage() const
484 return _("Math editor mode");
488 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
490 mathcursor = new MathedCursor(par);
491 if (!bv->lockInset(this))
492 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
494 bv->updateInset(this, false);
497 mathcursor->SetPos(x, y);
503 void InsetFormula::InsetUnlock(BufferView * bv)
506 if (mathcursor->InMacroMode()) {
507 mathcursor->MacroModeClose();
513 bv->updateInset(this, false);
517 // Now a symbol can be inserted only if the inset is locked
518 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
520 if (s.empty() || !mathcursor) return;
521 mathcursor->Interpret(s);
526 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
528 mathcursor->GetPos(x, y);
534 void InsetFormula::ToggleInsetCursor(BufferView * bv)
541 mathcursor->GetPos(x, y);
544 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
545 int asc = lyxfont::maxAscent(font);
546 int desc = lyxfont::maxDescent(font);
549 bv->hideLockedInsetCursor();
551 bv->showLockedInsetCursor(x, y, asc, desc);
552 cursor_visible = !cursor_visible;
556 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
558 if (!cursor_visible) {
562 mathcursor->GetPos(x, y);
565 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
566 int asc = lyxfont::maxAscent(font);
567 int desc = lyxfont::maxDescent(font);
568 bv->fitLockedInsetCursor(x, y, asc, desc);
570 ToggleInsetCursor(bv);
575 void InsetFormula::HideInsetCursor(BufferView * bv)
578 ToggleInsetCursor(bv);
582 void InsetFormula::ToggleInsetSelection(BufferView * bv)
590 //mathcursor->SelGetArea(n);
591 // XFillPolygon(fl_get_display(), pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
595 bv->updateInset(this, false);
600 void InsetFormula::display(bool dspf)
602 if (dspf != disp_flag) {
604 par->SetType(LM_OT_PAR);
605 par->SetStyle(LM_ST_DISPLAY);
607 if (is_multiline(par->GetType())) {
608 MathParInset * p = new MathParInset(par);
612 mathcursor->SetPar(par);
614 par->SetType(LM_OT_MIN);
615 par->SetStyle(LM_ST_TEXT);
616 if (!label.empty()) {
625 vector<string> const InsetFormula::getLabelList() const
627 //#warning This is dirty, I know. Ill clean it at 0.11
628 // Correction, the only way to clean this is with a new kernel: 0.13.
630 vector<string> label_list;
632 if (is_multi_numbered(par->GetType())) {
633 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
634 MathedRowSt const * crow = mt->getRowSt();
636 if (!crow->getLabel().empty())
637 label_list.push_back(crow->getLabel());
638 crow = crow->getNext();
640 } else if (!label.empty())
641 label_list.push_back(label);
647 void InsetFormula::UpdateLocal(BufferView * bv)
649 par->Metrics(); // To inform lyx kernel the exact size
650 // (there were problems with arrays).
651 bv->updateInset(this, true);
655 void InsetFormula::InsetButtonRelease(BufferView * bv,
656 int x, int y, int /*button*/)
662 mathcursor->SetPos(x, y);
667 bv->updateInset(this, false);
673 void InsetFormula::InsetButtonPress(BufferView * bv,
674 int x, int y, int /*button*/)
677 sel_x = x; sel_y = y;
678 if (mathcursor && mathcursor->Selection()) {
679 mathcursor->SelClear();
680 bv->updateInset(this, false);
685 void InsetFormula::InsetMotionNotify(BufferView * bv,
686 int x, int y, int /*button*/)
688 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
691 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
692 mathcursor->SelStart();
694 mathcursor->GetPos(sel_x, sel_y);
695 } else if (sel_flag) {
699 mathcursor->SetPos(x, y);
701 mathcursor->GetPos(x, y);
702 if (sel_x!= x || sel_y!= y)
703 bv->updateInset(this, false);
704 sel_x = x; sel_y = y;
709 void InsetFormula::InsetKeyPress(XKeyEvent *)
711 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
715 // Special Mathed functions
716 bool InsetFormula::SetNumber(bool numbf)
719 short type = par->GetType();
720 bool oldf = is_numbered(type);
721 if (numbf && !oldf) ++type;
722 if (!numbf && oldf) --type;
730 UpdatableInset::RESULT
731 InsetFormula::LocalDispatch(BufferView * bv,
732 int action, string const & arg)
734 // extern char *dispatch_result;
735 MathedTextCodes varcode = LM_TC_MIN;
736 bool was_macro = mathcursor->InMacroMode();
738 bool space_on = false;
739 bool was_selection = mathcursor->Selection();
740 RESULT result = DISPATCHED;
741 static MathSpaceInset * sp= 0;
745 if (mathcursor->getLastCode() == LM_TC_TEX) {
750 // --- Cursor Movements ---------------------------------------------
751 case LFUN_RIGHTSEL: sel = true;
754 result = DISPATCH_RESULT(mathcursor->Right(sel));
755 if (!sel && (result == DISPATCHED))
756 result = DISPATCHED_NOUPDATE;
759 case LFUN_LEFTSEL: sel = true;
762 result = DISPATCH_RESULT(mathcursor->Left(sel));
763 if (!sel && (result == DISPATCHED))
764 result = DISPATCHED_NOUPDATE;
767 case LFUN_UPSEL: sel = true;
769 result = DISPATCH_RESULT(mathcursor->Up(sel));
770 if (!sel && (result == DISPATCHED))
771 result = DISPATCHED_NOUPDATE;
774 case LFUN_DOWNSEL: sel = true;
776 result = DISPATCH_RESULT(mathcursor->Down(sel));
777 if (!sel && (result == DISPATCHED))
778 result = DISPATCHED_NOUPDATE;
782 result = DISPATCHED_NOUPDATE;
786 result = DISPATCHED_NOUPDATE;
788 case LFUN_DELETE_LINE_FORWARD:
789 bv->lockedInsetStoreUndo(Undo::DELETE);
790 mathcursor->DelLine();
795 bv->lockedInsetStoreUndo(Undo::INSERT);
796 byte c = arg.empty() ? '1' : arg[0];
797 mathcursor->Insert(c, LM_TC_CR);
798 if (!label.empty()) {
799 mathcursor->setLabel(label);
802 par = mathcursor->GetPar();
807 bv->lockedInsetStoreUndo(Undo::INSERT);
808 mathcursor->Insert(0, LM_TC_TAB);
812 bv->lockedInsetStoreUndo(Undo::INSERT);
813 mathcursor->Insert('T', LM_TC_TAB);
817 if (!mathcursor->Left())
820 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
821 bv->updateInset(this, true);
826 bv->lockedInsetStoreUndo(Undo::DELETE);
827 mathcursor->Delete();
828 bv->updateInset(this, true);
831 // sprintf(dispatch_buffer, "%d %d",);
832 // dispatch_result = dispatch_buffer;
840 istringstream ist(arg.c_str());
843 mathcursor->SetPos(x1 + x, y1 + y);
847 /* cursor selection ---------------------------- */
851 mathcursor->MacroModeClose();
852 bv->lockedInsetStoreUndo(Undo::INSERT);
853 mathcursor->SelPaste(); UpdateLocal(bv); break;
855 bv->lockedInsetStoreUndo(Undo::DELETE);
856 mathcursor->SelCut(); UpdateLocal(bv); break;
857 case LFUN_COPY: mathcursor->SelCopy(); break;
860 case LFUN_WORDRIGHTSEL:
861 case LFUN_WORDLEFTSEL:
864 // --- accented characters ------------------------------
866 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
867 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
868 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
869 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
870 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
871 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
872 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
873 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
874 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
875 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
880 if (!greek_kb_flag) {
882 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
889 case LFUN_GREEK_TOGGLE:
891 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
893 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
895 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
900 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
901 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
902 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
903 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
904 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
905 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
908 // varcode = LM_TC_TEX;
909 mathcursor->setLastCode(LM_TC_TEX);
910 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
914 case LFUN_MATH_NUMBER:
916 bv->lockedInsetStoreUndo(Undo::INSERT);
918 short type = par->GetType();
919 if (is_numbered(type)) {
921 if (!label.empty()) {
924 bv->owner()->getMiniBuffer()->Set(_("No number"));
927 bv->owner()->getMiniBuffer()->Set(_("Number"));
935 case LFUN_MATH_NONUMBER:
937 if (is_multi_numbered(par->GetType())) {
938 // MathMatrixInset *mt = (MathMatrixInset*)par;
940 // mt->SetNumbered(!mt->IsNumbered());
942 #warning This is a terrible hack! We should find a better solution.
943 while (mathcursor->getLabel() == MathedXIter::error_label) {
944 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
947 mathcursor->setNumbered();
953 case LFUN_MATH_LIMITS:
955 bv->lockedInsetStoreUndo(Undo::INSERT);
956 if (mathcursor->Limits())
962 latexkeys * l = in_word_set (arg);
963 int sz = (l) ? l->id: -1;
964 mathcursor->SetSize(sz);
969 case LFUN_INSERT_MATH:
971 bv->lockedInsetStoreUndo(Undo::INSERT);
972 InsertSymbol(bv, arg);
976 case LFUN_INSERT_MATRIX:
978 bv->lockedInsetStoreUndo(Undo::INSERT);
980 char s[80], arg2[80];
981 // This is just so that too long args won't ooze out of s.
982 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
983 k = sscanf(arg2, "%d %d %s", &m, &n, s);
992 MathMatrixInset * p = new MathMatrixInset(m, n);
993 if (mathcursor && p) {
994 if (k > 2 && int(strlen(s)) > m)
995 p->SetAlign(s[0], &s[1]);
996 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1002 case LFUN_MATH_DELIM:
1004 bv->lockedInsetStoreUndo(Undo::INSERT);
1005 char lf[40], rg[40], arg2[40];
1006 int ilf = '(', irg = '.';
1008 string vdelim("(){}[]./|");
1012 ::strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1013 int n = sscanf(arg2, "%s %s", lf, rg);
1014 lf[39] = '\0'; rg[39] = '\0';
1018 ilf = lyx::atoi(lf);
1021 l = in_word_set(lf, strlen(lf));
1022 // Long words will cause l == 0; so check.
1025 if (vdelim.find(lf[0]) != string::npos)
1030 irg = lyx::atoi(rg);
1033 l = in_word_set(rg, strlen(rg));
1036 if (vdelim.find(rg[0]) != string::npos)
1041 MathDelimInset * p = new MathDelimInset(ilf, irg);
1042 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1047 case LFUN_PROTECTEDSPACE:
1049 bv->lockedInsetStoreUndo(Undo::INSERT);
1050 sp = new MathSpaceInset(1);
1051 mathcursor->Insert(sp);
1057 case LFUN_INSERT_LABEL:
1059 bv->lockedInsetStoreUndo(Undo::INSERT);
1060 if (par->GetType() < LM_OT_PAR)
1063 string old_label = is_multiline(par->GetType())
1064 ? mathcursor->getLabel() : label;
1066 #warning This is a terrible hack! We should find a better solution.
1067 /// This is needed becuase in some positions mathcursor->cursor->crow
1068 /// is equal to 0, and therefore the label cannot be inserted.
1069 /// So we move the cursor left until mathcursor->cursor->crow != 0.
1070 while (old_label == MathedXIter::error_label) {
1071 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1073 old_label = mathcursor->getLabel();
1076 string new_label = arg;
1077 if (new_label.empty()) {
1078 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1079 pair<bool, string> res = old_label.empty()
1080 ? askForText(_("Enter new label to insert:"), default_label)
1081 : askForText(_("Enter label:"), old_label);
1084 new_label = frontStrip(strip(res.second));
1087 if (new_label == old_label)
1088 break; // Nothing to do
1090 if (!new_label.empty())
1093 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1096 if (is_multi_numbered(par->GetType()))
1097 mathcursor->setLabel(new_label);
1098 // MathMatrixInset *mt = (MathMatrixInset*)par;
1099 // mt->SetLabel(new_label);
1106 case LFUN_MATH_DISPLAY:
1107 bv->lockedInsetStoreUndo(Undo::EDIT);
1108 display(!disp_flag);
1112 // Invalid actions under math mode
1113 case LFUN_MATH_MODE:
1115 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1116 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1117 varcode = LM_TC_TEXTRM;
1119 varcode = LM_TC_VAR;
1121 mathcursor->setLastCode(varcode);
1125 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1128 //------- dummy actions
1129 case LFUN_EXEC_COMMAND:
1130 bv->owner()->getMiniBuffer()->PrepareForCommand();
1134 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1135 unsigned char c = arg[0];
1136 bv->lockedInsetStoreUndo(Undo::INSERT);
1138 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1140 mathcursor->setAccent(0);
1142 if (c == 0) { // Dead key, do nothing
1143 //lyxerr << "deadkey" << endl;
1147 if (mathcursor->getLastCode() == LM_TC_TEX) {
1148 mathcursor->MacroModeOpen();
1149 mathcursor->clearLastCode();
1150 varcode = LM_TC_MIN;
1153 short f = (mathcursor->getLastCode()) ?
1154 mathcursor->getLastCode() :
1155 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1156 varcode = MathIsAlphaFont(f) ?
1157 static_cast<MathedTextCodes>(f) :
1161 // lyxerr << "Varcode << vardoce;
1162 MathedTextCodes char_code = varcode;
1163 if (greek_kb_flag) {
1165 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1166 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1167 'Y', 0, 0, 0, 0 , 'Z' };
1168 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1169 char_code = LM_TC_RM;
1172 char_code = LM_TC_SYMB;
1174 mathcursor->Insert(c, char_code);
1175 if (greek_kb_flag && char_code == LM_TC_RM )
1176 mathcursor->setLastCode(LM_TC_VAR);
1177 varcode = LM_TC_MIN;
1178 if (greek_kb_flag<2) greek_kb_flag = 0;
1180 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1181 mathcursor->Insert(c, LM_TC_TEX);
1183 mathcursor->Insert('}', LM_TC_TEX);
1186 mathcursor->clearLastCode();
1187 // varcode = LM_TC_MIN;
1189 if (c == '_' && varcode == LM_TC_TEX) {
1190 mathcursor->Insert(c, LM_TC_SPECIAL);
1191 mathcursor->clearLastCode();
1192 // varcode = LM_TC_MIN;
1194 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1195 mathcursor->MacroModeOpen();
1196 mathcursor->clearLastCode();
1197 mathcursor->Insert(c, LM_TC_MIN);
1200 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1201 mathcursor->Insert(c, LM_TC_CONST);
1203 if (strchr("+/-*<>=", c))
1204 mathcursor->Insert(c, LM_TC_BOP);
1206 if (strchr(latex_special_chars, c) && c!= '_')
1207 mathcursor->Insert(c, LM_TC_SPECIAL);
1209 if (c == '_' || c == '^') {
1213 mathcursor->Interpret (s);
1217 short f = (mathcursor->getLastCode()) ?
1218 mathcursor->getLastCode() :
1219 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1220 varcode = MathIsAlphaFont(f) ?
1221 static_cast<MathedTextCodes>(f) :
1224 if (varcode == LM_TC_TEXTRM) {
1225 mathcursor->Insert(c, LM_TC_TEXTRM);
1228 mathcursor->MacroModeClose();
1231 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1235 if (!mathcursor->Pop() && mathcursor->IsEnd())
1239 if (c == '\'' || c == '@') {
1240 mathcursor->Insert (c, LM_TC_VAR);
1244 mathcursor->MacroModeClose();
1245 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1246 mathcursor->setLastCode(LM_TC_TEX);
1249 } else if (action == LFUN_MATH_PANEL) {
1250 result = UNDISPATCHED;
1252 // lyxerr << "Closed by action " << action << endl;
1256 if (was_macro != mathcursor->InMacroMode()
1258 && action != LFUN_BACKSPACE)
1260 if (sp && !space_on) sp = 0;
1261 if (mathcursor->Selection() || was_selection)
1262 ToggleInsetSelection(bv);
1264 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1265 (result == UNDISPATCHED))
1266 ShowInsetCursor(bv);
1268 bv->unlockInset(this);
1275 MathFuncInset::draw(Painter & pain, int x, int y)
1277 if (!name.empty() && name[0] > ' ') {
1278 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1279 font.setLatex(LyXFont::ON);
1280 x += (lyxfont::width('I', font) + 3) / 4;
1281 pain.text(x, y, name, font);
1286 void MathFuncInset::Metrics()
1288 //ln = (name) ? strlen(name): 0;
1289 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1290 font.setLatex(LyXFont::ON);
1296 width = lyxfont::width(name, font)
1297 + lyxfont::width('I', font) / 2;
1298 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1304 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1306 MathedIter it(par->GetData());
1308 while (it.OK() && !(features.binom && features.boldsymbol)) {
1310 if (it.IsActive()) {
1311 MathParInset * p = it.GetActiveInset();
1312 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1313 p->GetName() == "binom") {
1314 features.binom = true;
1316 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1317 p->setArgumentIdx(i);
1318 mathedValidate(features, p);
1322 MathedInset* p = it.GetInset();
1323 if (!features.boldsymbol &&
1324 p->GetName() == "boldsymbol") {
1325 features.boldsymbol = true;