]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
b048f29c46db02b4a74f440326277d682dc46160
[lyx.git] / src / mathed / formulabase.C
1  /*
2 *  File:        formula.C
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.
7 *
8 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
9 *
10 *  Version: 0.4, Lyx project.
11 *
12 *   You are free to use and modify this code under the terms of
13 *   the GNU General Public Licence version 2 or later.
14 */
15
16 #include <config.h>
17 #include <fstream>
18
19 #include "Lsstream.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "formulamacro.h"
27 #include "commandtags.h"
28 #include "math_cursor.h"
29 #include "math_parser.h"
30 #include "BufferView.h"
31 #include "lyxtext.h"
32 #include "lyxfunc.h"
33 #include "gettext.h"
34 #include "LaTeXFeatures.h"
35 #include "debug.h"
36 #include "support/LOstream.h"
37 #include "LyXView.h"
38 #include "Painter.h"
39 #include "font.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "support/lyxlib.h"
43 #include "mathed/support.h"
44 #include "undo_funcs.h"
45
46 using std::endl;
47 using std::ostream;
48 using std::vector;
49
50 extern char const * latex_special_chars;
51
52 int greek_kb_flag = 0;
53 extern char const * latex_mathenv[];
54 LyXFont           * Math_Fonts = 0;
55 MathCursor        * mathcursor = 0;
56
57
58 namespace {
59
60
61 // local global
62 int sel_x;
63 int sel_y;
64 bool sel_flag;
65
66 void mathed_init_fonts();
67
68 string nicelabel(string const & label)
69 {
70         return "(" + (label.empty() ? "#" : label) + ")";
71 }
72
73 void handleFont(BufferView * bv, MathTextCodes t) 
74 {
75         if (mathcursor->Selection())
76                 bv->lockedInsetStoreUndo(Undo::EDIT);
77         mathcursor->handleFont(t);
78 }
79
80 void handleAccent(BufferView * bv, int code)
81 {
82         bv->lockedInsetStoreUndo(Undo::EDIT);
83         mathcursor->handleAccent(code);
84 }
85
86 void handleDelim(BufferView * bv, int l, int r)
87 {
88         bv->lockedInsetStoreUndo(Undo::EDIT);
89         mathcursor->handleDelim(l, r);
90 }
91
92 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
93 {
94         LyXText * lt = bv->getLyXText();
95         
96         bv->beforeChange(lt);
97         finishUndo();
98         if (!bv->insertInset(new_inset)) {
99                 delete new_inset;
100                 return false;
101         }
102         new_inset->edit(bv, 0, 0, 0);
103         return true;
104 }
105
106
107 } // namespaces
108
109
110
111 LyXFont WhichFont(short type, int size)
112 {
113         LyXFont f;
114         
115         if (!Math_Fonts)
116                 mathed_init_fonts();
117
118         switch (type) {
119         case LM_TC_SYMB:        
120                 f = Math_Fonts[2];
121                 break;
122
123         case LM_TC_BSYM:        
124                 f = Math_Fonts[2];
125                 break;
126
127         case LM_TC_VAR:
128         case LM_TC_IT:
129                 f = Math_Fonts[0];
130                 break;
131
132         case LM_TC_BF:
133                 f = Math_Fonts[3];
134                 break;
135
136         case LM_TC_SF:
137                 f = Math_Fonts[7];
138                 break;
139
140         case LM_TC_CAL:
141                 f = Math_Fonts[4];
142                 break;
143
144         case LM_TC_TT:
145                 f = Math_Fonts[5];
146                 break;
147
148         case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
149         case LM_TC_TEXTRM:
150         case LM_TC_RM:
151                 f = Math_Fonts[6];
152                 break;
153
154         default:
155                 f = Math_Fonts[1];
156                 break;
157         }
158
159         switch (size) {
160         case LM_ST_DISPLAY:
161                 if (type == LM_TC_BSYM) {
162                         f.incSize();
163                         f.incSize();
164                 }
165                 break;
166
167         case LM_ST_TEXT:
168                 break;
169
170         case LM_ST_SCRIPT:
171                 f.decSize();
172                 break;
173
174         case LM_ST_SCRIPTSCRIPT:
175                 f.decSize();
176                 f.decSize();
177                 break;
178
179         default:
180                 lyxerr << "Math Error: wrong font size: " << size << endl;
181                 break;
182         }
183
184         if (type != LM_TC_TEXTRM)
185                 f.setColor(LColor::math);
186
187         return f;
188 }
189
190
191 namespace {
192
193 void mathed_init_fonts()
194 {
195         Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
196         //at once (JMarc) rc
197
198         for (int i = 0 ; i < 8 ; ++i) {
199                 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
200         }
201
202         Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
203
204         Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
205
206         Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
207         Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
208
209         Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
210
211         Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
212         Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
213
214         Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
215
216         Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
217
218         Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
219 }
220
221
222 // returns the nearest enclosing matrix
223 MathArrayInset * matrixpar(int & idx)
224 {
225         idx = 0;
226         return
227                 static_cast<MathArrayInset *> 
228                         (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0); 
229 }
230
231
232 } // namespace anon
233
234
235 InsetFormulaBase::InsetFormulaBase(MathInset * par)
236         : par_(par)
237 {}
238
239
240 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
241         : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
242 {}
243
244
245 InsetFormulaBase::~InsetFormulaBase()
246 {
247 #ifdef WITH_WARNINGS
248 #warning leak this for a while...
249 #endif
250         //delete par_;
251 }
252
253
254 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
255 {
256         read(lex);
257 }
258
259 void InsetFormulaBase::write(Buffer const *, ostream & os) const
260 {
261         write(os);
262 }
263
264 int InsetFormulaBase::latex(Buffer const *, ostream & os,
265         bool fragile, bool spacing) const
266 {
267         return latex(os, fragile, spacing);
268 }
269
270 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
271 {
272         return ascii(os, spacing);
273 }
274
275 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
276 {
277         return linuxdoc(os);
278 }
279
280 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
281 {
282         return docBook(os);
283 }
284
285
286
287 // Check if uses AMS macros
288 void InsetFormulaBase::validate(LaTeXFeatures &) const
289 {}
290
291
292 string const InsetFormulaBase::editMessage() const
293 {
294         return _("Math editor mode");
295 }
296
297
298 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
299 {
300         mathcursor = new MathCursor(this);
301
302         if (!bv->lockInset(this))
303                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
304
305         par_->Metrics(LM_ST_TEXT);
306         bv->updateInset(this, false);
307         if (x == 0) {
308                 mathcursor->first();
309         } else {
310                 mathcursor->last();
311         }
312         sel_x = 0;
313         sel_y = 0;
314         sel_flag = false;
315 }
316
317
318 void InsetFormulaBase::insetUnlock(BufferView * bv)
319 {
320         if (mathcursor) {
321                 if (mathcursor->InMacroMode()) {
322                         mathcursor->MacroModeClose();
323                         updateLocal(bv);
324                 }
325                 delete mathcursor;
326         }
327         mathcursor = 0;
328         bv->updateInset(this, false);
329 }
330
331
332 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
333 {
334         mathcursor->GetPos(x, y);
335         x -= par_->xo();
336         y -= par_->yo();
337 }
338
339
340 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
341 {
342         if (!mathcursor)
343                 return;
344
345         if (isCursorVisible())
346                 bv->hideLockedInsetCursor();
347         else {
348                 int x;
349                 int y;
350                 mathcursor->GetPos(x, y);
351                 //x -= par_->xo();
352                 y -= par_->yo();
353
354                 LyXFont   font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
355                 int const asc  = lyxfont::maxAscent(font);
356                 int const desc = lyxfont::maxDescent(font);
357
358                 bv->showLockedInsetCursor(x, y, asc, desc);
359         }
360
361         toggleCursorVisible();
362 }
363
364
365 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
366 {
367         if (!isCursorVisible()) {
368                 if (mathcursor) {
369                         int x;
370                         int y;
371                         mathcursor->GetPos(x, y);
372                         x -= par_->xo();
373                         y -= par_->yo();
374                         LyXFont font   = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
375                         int const asc  = lyxfont::maxAscent(font);
376                         int const desc = lyxfont::maxDescent(font);
377                         bv->fitLockedInsetCursor(x, y, asc, desc);
378                 }
379                 toggleInsetCursor(bv);
380         }
381 }
382
383
384 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
385 {
386         if (isCursorVisible())
387                 toggleInsetCursor(bv);
388 }
389
390
391 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
392 {
393         if (!mathcursor)
394                 return;
395
396         bv->updateInset(this, false);
397 }
398
399
400 vector<string> const InsetFormulaBase::getLabelList() const
401 {
402   return std::vector<string>();
403 }
404
405
406 void InsetFormulaBase::updateLocal(BufferView * bv)
407 {
408         par_->Metrics(LM_ST_TEXT);
409         bv->updateInset(this, true);
410 }
411
412
413 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
414                                           int x, int y, int /*button*/)
415 {
416         if (mathcursor) {
417                 hideInsetCursor(bv);
418                 x += par_->xo();
419                 y += par_->yo();
420                 mathcursor->SetPos(x, y);
421                 showInsetCursor(bv);
422                 if (sel_flag) {
423                         sel_flag = false;
424                         sel_x = 0;
425                         sel_y = 0;
426                 }
427                 bv->updateInset(this, false);
428         }
429 }
430
431
432 void InsetFormulaBase::insetButtonPress(BufferView * bv,
433                                         int x, int y, int /*button*/)
434 {
435         sel_flag = false;
436         sel_x = x;
437         sel_y = y;
438         if (mathcursor && mathcursor->Selection()) {
439                 mathcursor->SelClear();
440                 bv->updateInset(this, false);
441         }
442 }
443
444
445 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
446                                          int x, int y, int /*button*/)
447 {
448         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
449                 sel_flag = true;
450                 hideInsetCursor(bv);
451                 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
452                 mathcursor->SelStart();
453                 showInsetCursor(bv);
454                 mathcursor->GetPos(sel_x, sel_y);
455         } else if (sel_flag) {
456                 hideInsetCursor(bv);
457                 x += par_->xo();
458                 y += par_->yo();
459                 mathcursor->SetPos(x, y);
460                 showInsetCursor(bv);
461                 mathcursor->GetPos(x, y);
462                 if (sel_x != x || sel_y != y)
463                         bv->updateInset(this, false);
464                 sel_x = x;
465                 sel_y = y;
466         }
467 }
468
469
470 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
471 {
472         lyxerr[Debug::MATHED]
473                 << "Used InsetFormulaBase::InsetKeyPress." << endl;
474 }
475
476
477
478 UpdatableInset::RESULT
479 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
480                             string const & arg)
481 {
482         //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
483         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
484
485         if (!mathcursor) 
486                 return UNDISPATCHED;
487
488         MathTextCodes varcode = LM_TC_MIN;
489         bool was_macro = mathcursor->InMacroMode();
490         bool sel = false;
491         bool was_selection = mathcursor->Selection();
492         RESULT result = DISPATCHED;
493
494         hideInsetCursor(bv);
495
496         if (mathcursor->getLastCode() == LM_TC_TEX)
497                 varcode = LM_TC_TEX;
498
499         mathcursor->normalize();
500
501         switch (action) {
502
503                 // --- Cursor Movements ---------------------------------------------
504
505         case LFUN_RIGHTSEL:
506                 sel = true; // fall through...
507
508         case LFUN_RIGHT:
509                 result = DISPATCH_RESULT(mathcursor->Right(sel));
510                 updateLocal(bv);
511                 break;
512
513
514         case LFUN_LEFTSEL:
515                 sel = true; // fall through
516
517         case LFUN_LEFT:
518                 result = DISPATCH_RESULT(mathcursor->Left(sel));
519                 updateLocal(bv);
520                 break;
521
522
523         case LFUN_UPSEL:
524                 sel = true;
525
526         case LFUN_UP:
527                 result = DISPATCH_RESULT(mathcursor->Up(sel));
528                 updateLocal(bv);
529                 break;
530
531
532         case LFUN_DOWNSEL:
533                 sel = true;
534
535         case LFUN_DOWN:
536                 result = DISPATCH_RESULT(mathcursor->Down(sel));
537                 updateLocal(bv);
538                 break;
539
540         case LFUN_HOME:
541                 mathcursor->Home();
542                 updateLocal(bv);
543                 break;
544
545         case LFUN_END:
546                 mathcursor->End();
547                 updateLocal(bv);
548                 break;
549
550         case LFUN_DELETE_LINE_FORWARD:
551                 bv->lockedInsetStoreUndo(Undo::DELETE);
552                 mathcursor->DelLine();
553                 updateLocal(bv);
554                 break;
555
556         case LFUN_TAB:
557                 mathcursor->idxNext();
558                 updateLocal(bv);
559                 break;
560
561         case LFUN_SHIFT_TAB:
562                 mathcursor->idxPrev();
563                 updateLocal(bv);
564                 break;
565
566         case LFUN_TABINSERT:
567                 bv->lockedInsetStoreUndo(Undo::EDIT);
568                 mathcursor->splitCell();
569                 updateLocal(bv);
570                 break;
571
572         case LFUN_BACKSPACE:
573                 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
574                         bv->lockedInsetStoreUndo(Undo::DELETE);
575                         mathcursor->pullArg();
576                         bv->updateInset(this, true);
577                         break;
578                 }
579                 if (!mathcursor->Left())
580                         break;
581                 // fall through...
582
583         case LFUN_DELETE:
584                 bv->lockedInsetStoreUndo(Undo::DELETE);
585                 mathcursor->Delete();
586                 bv->updateInset(this, true);
587                 break;
588
589                 //    case LFUN_GETXY:
590                 //      sprintf(dispatch_buffer, "%d %d",);
591                 //      dispatch_result = dispatch_buffer;
592                 //      break;
593         case LFUN_SETXY:
594         {
595                 lyxerr << "LFUN_SETXY broken!\n";
596                 int x;
597                 int y;
598                 int x1;
599                 int y1;
600                 istringstream is(arg.c_str());
601                 is >> x >> y;
602                 par_->GetXY(x1, y1);
603                 mathcursor->SetPos(x1 + x, y1 + y);
604                 updateLocal(bv);
605         }
606         break;
607
608                 // cursor selection ---------------------------- 
609
610         case LFUN_PASTE:
611                 if (was_macro)
612                         mathcursor->MacroModeClose();
613                 bv->lockedInsetStoreUndo(Undo::INSERT);
614                 mathcursor->SelPaste();
615                 updateLocal(bv);
616                 break;
617
618         case LFUN_CUT:
619                 bv->lockedInsetStoreUndo(Undo::DELETE);
620                 mathcursor->SelCut();
621                 updateLocal(bv);
622                 break;
623
624         case LFUN_COPY:
625                 mathcursor->SelCopy();
626                 break;
627
628         case LFUN_HOMESEL:
629         case LFUN_ENDSEL:
630         case LFUN_WORDRIGHTSEL:
631         case LFUN_WORDLEFTSEL:
632                 break;
633
634                 // --- accented characters ------------------------------
635
636         case LFUN_UMLAUT:     handleAccent(bv, LM_ddot); break;
637         case LFUN_CIRCUMFLEX: handleAccent(bv, LM_hat); break;
638         case LFUN_GRAVE:      handleAccent(bv, LM_grave); break;
639         case LFUN_ACUTE:      handleAccent(bv, LM_acute); break;
640         case LFUN_TILDE:      handleAccent(bv, LM_tilde); break;
641         case LFUN_MACRON:     handleAccent(bv, LM_bar); break;
642         case LFUN_DOT:        handleAccent(bv, LM_dot); break;
643         case LFUN_CARON:      handleAccent(bv, LM_check); break;
644         case LFUN_BREVE:      handleAccent(bv, LM_breve); break;
645         case LFUN_VECTOR:     handleAccent(bv, LM_vec); break;
646
647                 // Greek mode
648         case LFUN_GREEK:
649                 if (!greek_kb_flag) {
650                         greek_kb_flag = 1;
651                         bv->owner()->message(_("Math greek mode on"));
652                 } else
653                         greek_kb_flag = 0;
654                 break;
655
656                 // Greek keyboard
657         case LFUN_GREEK_TOGGLE:
658                 greek_kb_flag = greek_kb_flag ? 0 : 2;
659                 if (greek_kb_flag)
660                         bv->owner()->message(_("Math greek keyboard on"));
661                 else
662                         bv->owner()->message(_("Math greek keyboard off"));
663                 break;
664
665                 //  Math fonts
666         case LFUN_BOLD:    handleFont(bv, LM_TC_BF); break;
667         case LFUN_SANS:    handleFont(bv, LM_TC_SF); break;
668         case LFUN_EMPH:    handleFont(bv, LM_TC_CAL); break;
669         case LFUN_ROMAN:   handleFont(bv, LM_TC_RM); break;
670         case LFUN_CODE:    handleFont(bv, LM_TC_TT); break;
671         case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
672
673         case LFUN_MATH_MODE:
674                 handleFont(bv, LM_TC_TEXTRM);
675                 //bv->owner()->message(_("math text mode toggled"));
676                 break;
677
678 #ifndef NO_LATEX
679         case LFUN_TEX:
680                 if (!mathcursor->Selection()) {
681                         mathcursor->handleFont(LM_TC_TEX);
682                         //bv->owner()->message(_("TeX mode toggled"));
683                 }
684                 break;
685 #endif
686
687         case LFUN_MATH_LIMITS:
688                 bv->lockedInsetStoreUndo(Undo::INSERT);
689                 if (mathcursor->toggleLimits())
690                         updateLocal(bv);
691                 break;
692
693         case LFUN_MATH_SIZE:
694                 if (!arg.empty()) {
695                         bv->lockedInsetStoreUndo(Undo::INSERT);
696                         latexkeys const * l = in_word_set(arg);
697                         mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
698                         updateLocal(bv);
699                 }
700                 break;
701
702         case LFUN_INSERT_MATRIX:
703                 if (!arg.empty()) {
704                         bv->lockedInsetStoreUndo(Undo::INSERT);
705                         mathcursor->Interpret("matrix " + arg);
706                         updateLocal(bv);
707                 }
708                 break;
709
710         case LFUN_INSERT_MATH:
711                 if (!arg.empty()) {
712                         bv->lockedInsetStoreUndo(Undo::INSERT);
713                         mathcursor->Interpret(arg);
714                         updateLocal(bv);
715                 }
716                 break;
717
718         case LFUN_MATH_DELIM:
719         {
720                 bv->lockedInsetStoreUndo(Undo::INSERT);
721                 int ilt = '(';
722                 int irt = '.';
723                 static const string vdelim("(){}[]./|");
724                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
725
726                 if (arg.empty())
727                         break;
728
729                 istringstream is(arg.c_str());
730                 string lt;
731                 string rt;
732                 is >> lt >> rt;
733                 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
734                 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
735
736                 if (lt.size() > 1) {
737                         latexkeys const * l = in_word_set(lt);
738                         if (l)
739                                 ilt = l->id;
740                 } else if (vdelim.find(lt[0]) != string::npos)
741                                 ilt = lt[0];
742
743                 if (rt.size() > 1) {
744                         latexkeys const * l = in_word_set(rt);
745                         if (l)
746                                 irt = l->id;
747                 } else if (vdelim.find(rt[0]) != string::npos)
748                                 irt = rt[0];
749
750                 handleDelim(bv, ilt, irt);
751                 updateLocal(bv);
752                 break;
753         }
754
755         case LFUN_PROTECTEDSPACE:
756                 bv->lockedInsetStoreUndo(Undo::INSERT);
757                 mathcursor->insert(new MathSpaceInset(1));
758                 updateLocal(bv);
759                 break;
760
761         case LFUN_UNDO:
762                 bv->owner()->message(_("Invalid action in math mode!"));
763                 break;
764
765
766         case LFUN_MATH_HALIGN:
767         {
768                 bv->lockedInsetStoreUndo(Undo::INSERT);
769                 lyxerr << "handling halign '" << arg << "'\n";
770                 int idx;
771                 MathArrayInset * p = matrixpar(idx);
772                 if (!p)
773                         break; 
774                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
775                 updateLocal(bv);
776                 break;
777         }
778
779         case LFUN_MATH_VALIGN:
780         {
781                 bv->lockedInsetStoreUndo(Undo::INSERT);
782                 lyxerr << "handling valign '" << arg << "'\n";
783                 int idx;
784                 MathArrayInset * p = matrixpar(idx);
785                 if (!p)
786                         break; 
787                 p->valign(arg.size() ? arg[0] : 'c');
788                 updateLocal(bv);
789                 break;
790         }
791
792         case LFUN_MATH_ROW_INSERT:
793         {
794                 bv->lockedInsetStoreUndo(Undo::INSERT);
795                 int idx;
796                 MathArrayInset * p = matrixpar(idx);
797                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
798                 if (!p)
799                         break; 
800                 p->addRow(p->row(idx));
801                 updateLocal(bv);
802                 break;
803         }
804
805         case LFUN_MATH_ROW_DELETE:
806         {
807                 bv->lockedInsetStoreUndo(Undo::INSERT);
808                 int idx;
809                 MathArrayInset * p = matrixpar(idx);
810                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
811                 if (!p)
812                         break; 
813                 p->delRow(p->row(idx));
814                 updateLocal(bv);
815                 break;
816         }
817
818         case LFUN_MATH_COLUMN_INSERT:
819         {
820                 bv->lockedInsetStoreUndo(Undo::INSERT);
821                 int idx;
822                 MathArrayInset * p = matrixpar(idx);
823                 if (!p)
824                         break; 
825                 p->addCol(p->col(idx));
826                 updateLocal(bv);
827                 break;
828         }
829
830         case LFUN_MATH_COLUMN_DELETE:
831         {
832                 bv->lockedInsetStoreUndo(Undo::INSERT);
833                 int idx;
834                 MathArrayInset * p = matrixpar(idx);
835                 if (!p)
836                         break; 
837                 p->delCol(p->col(idx));
838                 updateLocal(bv);
839                 break;
840         }
841
842         case LFUN_EXEC_COMMAND:
843                 result = UNDISPATCHED;
844                 break;
845
846         default:
847                 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
848                         unsigned char c = arg[0];
849                         //lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
850                         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
851                         //lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
852                         bv->lockedInsetStoreUndo(Undo::INSERT);
853
854                         if (c == 0) {      // Dead key, do nothing
855                                 //lyxerr << "deadkey" << endl;
856                                 break;
857                         }
858
859                         if (isalpha(c)) {
860                                 if (mathcursor->getLastCode() == LM_TC_TEX) {
861                                         mathcursor->MacroModeOpen();
862                                         mathcursor->clearLastCode();
863                                         varcode = LM_TC_MIN;
864                                 } else if (!varcode) {          
865                                         short f = mathcursor->getLastCode() ?
866                                                 mathcursor->getLastCode() :
867                                                 mathcursor->nextCode();
868                                         varcode = MathIsAlphaFont(f) ?
869                                                 static_cast<MathTextCodes>(f) :
870                                                 LM_TC_VAR;
871                                 }
872                                 
873                                 //           lyxerr << "Varcode << vardoce;
874                                 MathTextCodes char_code = varcode;
875                                 if (greek_kb_flag) {
876                                         char greek[26] =
877                                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
878                                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
879                                          'Y',  0,   0,   0,   0 , 'Z' };
880                                         
881                                         if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
882                                                 char_code = LM_TC_RM;
883                                                 c = greek[c - 'A'];
884                                         } else
885                                                 char_code = LM_TC_SYMB;
886                                 }
887                                 
888                                 mathcursor->insert(c, char_code);
889                                 
890                                 if (greek_kb_flag && char_code == LM_TC_RM )
891                                         mathcursor->setLastCode(LM_TC_VAR);
892                                 
893                                 varcode = LM_TC_MIN;
894                                 
895                                 if (greek_kb_flag < 2)
896                                         greek_kb_flag = 0;
897                                 
898                         } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
899                                 mathcursor->insert(c, LM_TC_TEX);
900                                 if (c == '{') {
901                                         mathcursor->insert('}', LM_TC_TEX);
902                                         mathcursor->Left();
903                                 }
904                                 mathcursor->clearLastCode();
905                                 //             varcode = LM_TC_MIN;
906                         } else if (c == '_' && varcode == LM_TC_TEX) {
907                                 mathcursor->insert(c, LM_TC_SPECIAL);
908                                 mathcursor->clearLastCode();
909                                 //             varcode = LM_TC_MIN;
910                         } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
911                                 mathcursor->MacroModeOpen();
912                                 mathcursor->clearLastCode();
913                                 mathcursor->insert(c, LM_TC_MIN);
914                         } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
915                                 MathTextCodes code = mathcursor->getLastCode();
916                                 if (code != LM_TC_TEXTRM)
917                                         code = LM_TC_CONST;
918                                 mathcursor->insert(c, code);
919                         } else if (strchr("+/-*<>=", c)) {
920                                 MathTextCodes code = mathcursor->getLastCode();
921                                 if (code != LM_TC_TEXTRM)
922                                         code = LM_TC_BOP;
923                                 mathcursor->insert(c, code);
924                         } else if (strchr(latex_special_chars, c) && c != '_') {
925                                 MathTextCodes code = mathcursor->getLastCode();
926                                 if (code != LM_TC_TEXTRM)
927                                         code = LM_TC_SPECIAL;
928                                 mathcursor->insert(c, code);
929                         } else if (c == '_' || c == '^') {
930                                 char s[2];
931                                 s[0] = c;
932                                 s[1] = 0;
933                                 mathcursor->Interpret(s);
934                         } else if (c == ' ') {  
935                                 if (!varcode) { 
936                                         short f = (mathcursor->getLastCode()) ?
937                                                 mathcursor->getLastCode() :
938                                                 mathcursor->nextCode();
939                                         varcode = MathIsAlphaFont(f) ?
940                                                 static_cast<MathTextCodes>(f) :
941                                                 LM_TC_VAR;
942                                 }
943                                 
944                                 if (varcode == LM_TC_TEXTRM) {
945                                         mathcursor->insert(c, LM_TC_TEXTRM);
946                                 } else if (was_macro) {
947                                         mathcursor->MacroModeClose();
948                                 } else {
949                                         if (!mathcursor->pop())
950                                                 result = FINISHED;
951                                         mathcursor->plainRight();
952                                 }
953                         } else if (c == '\'' || c == '@') {
954                                 mathcursor->insert (c, LM_TC_VAR);
955                         } else if (c == '\\') {
956                                 if (was_macro)
957                                         mathcursor->MacroModeClose();
958                                 bv->owner()->message(_("TeX mode"));
959                                 mathcursor->setLastCode(LM_TC_TEX);
960                         }
961                         updateLocal(bv);
962                 } else if (action == LFUN_MATH_PANEL) {
963                         result = UNDISPATCHED;
964                 } else {
965                         lyxerr << "Closed by action " << action << endl;
966                         result =  FINISHED;
967                 }
968         }
969
970         mathcursor->normalize();
971
972         if (mathcursor && was_macro != mathcursor->InMacroMode()
973                                 && action >= 0
974                                 && action != LFUN_BACKSPACE) 
975                 updateLocal(bv);
976         
977         //if (mathcursor)
978         //              updateLocal(bv);
979
980         if (mathcursor && (mathcursor->Selection() || was_selection))
981                 toggleInsetSelection(bv);
982
983         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
984             result == UNDISPATCHED)
985                 showInsetCursor(bv);
986         else
987                 bv->unlockInset(this);
988
989         return result;  // original version
990 }
991
992
993
994 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
995  * on greek mode */
996 bool math_insert_greek(BufferView * bv, char c)
997 {
998         if (!bv->available())
999                 return false;
1000
1001         if (!isalpha(c))
1002                 return false;
1003
1004         string tmp;
1005         tmp = c;
1006         if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
1007                 int greek_kb_flag_save = greek_kb_flag;
1008                 InsetFormula * new_inset = new InsetFormula();
1009                 bv->beforeChange(bv->text);
1010                 if (!bv->insertInset(new_inset)) {
1011                         delete new_inset;
1012                         return false;
1013                 }
1014                 //Update(1);//BUG
1015                 new_inset->edit(bv, 0, 0, 0);
1016                 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
1017                 if (greek_kb_flag_save < 2) {
1018                         bv->unlockInset(new_inset); // bv->theLockingInset());
1019                         bv->text->cursorRight(bv, true);
1020                 }
1021         } else
1022                 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
1023                                 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
1024                         static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
1025                 else
1026                         lyxerr << "Math error: attempt to write on a wrong "
1027                                 "class of inset." << endl;
1028         return true;
1029 }
1030
1031
1032
1033 Inset::Code InsetFormulaBase::lyxCode() const
1034 {
1035         return Inset::MATH_CODE;
1036 }
1037
1038
1039 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
1040 {
1041         // We have already discussed what was here
1042         LyXFont font(f);
1043 #ifndef NO_LATEX
1044         font.setLatex(LyXFont::OFF);
1045 #endif
1046         return font;
1047 }
1048
1049 MathInset * InsetFormulaBase::par() const
1050 {
1051         return par_;
1052 }
1053
1054
1055 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
1056 {
1057         if (bv->available()) {
1058 // Feature "Read math inset from selection" disabled.
1059 //              // use selection if available..
1060 //              string sel;
1061 //              if (action == LFUN_MATH_IMPORT_SELECTION)
1062 //                      sel = "";
1063 //              else
1064 //                      sel = bv->getLyXText()->selectionAsString(bv->buffer());
1065
1066                         InsetFormula * f;
1067 //              if (sel.empty()) {
1068                                 f = new InsetFormula;
1069                                 if (openNewInset(bv, f)) {
1070                                         // don't do that also for LFUN_MATH_MODE unless you want end up with
1071                                         // always changing to mathrm when opening an inlined inset
1072                                         // -- I really hate "LyXfunc overloading"...
1073                                         if (display)
1074                                                 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
1075                                         f->localDispatch(bv, LFUN_INSERT_MATH, arg);
1076                                 }
1077 //              } else {
1078 //                      f = new InsetFormula(sel);
1079 //                      bv->getLyXText()->cutSelection(bv);
1080 //                      openNewInset(bv, f);
1081 //              }
1082         }
1083         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
1084 }
1085
1086 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1087 {
1088         mathDispatchCreation(bv, arg, true);
1089 }
1090         
1091 void mathDispatchMathMode(BufferView * bv, string const & arg)
1092 {
1093         mathDispatchCreation(bv, arg, false);
1094 }
1095
1096 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1097 {
1098         mathDispatchCreation(bv, arg, true);
1099 }
1100
1101 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1102 {
1103         if (bv->available()) {
1104                 string s(arg);
1105                 if (s.empty())
1106                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1107                 else {
1108                         string const s1 = token(s, ' ', 1);
1109                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
1110                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1111                 }
1112         }
1113 }
1114
1115 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1116 {          
1117         if (bv->available()) { 
1118                 if (openNewInset(bv, new InsetFormula))
1119                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1120         }
1121 }          
1122
1123
1124 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1125 {          
1126         if (bv->available()) { 
1127                 if (openNewInset(bv, new InsetFormula))
1128                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1129         }
1130 }          
1131
1132 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1133 {
1134         if (bv->available()) {
1135                 if (arg.size() && arg[0] == '\\') {
1136                         InsetFormula * f = new InsetFormula(arg);
1137                         openNewInset(bv, f);
1138                 } else {
1139                         mathDispatchMathMode(bv, arg);
1140                 }
1141         }
1142 }
1143