]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formula.C
further code uglification to make Jean-Marc's compiler happy
[lyx.git] / src / mathed / formula.C
index b3a648990301f0de03235280a3ce2f0ac943e6e8..b99d255eb971d075756d03cf9a910ba41929f9c7 100644 (file)
 *   the GNU General Public Licence version 2 or later.
 */
 
-#include <config.h>
-#include <fstream>
-
-#include "support/LOstream.h"
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
+#include <config.h>
+#include <fstream>
+
 #include "formula.h"
 #include "commandtags.h"
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "lyx_main.h"
 #include "BufferView.h"
-#include "lyxtext.h"
 #include "gettext.h"
 #include "debug.h"
 #include "lyx_gui_misc.h"
 #include "support/LOstream.h"
 #include "support/lyxlib.h"
 #include "support/syscall.h"
+#include "support/lstrings.h"
 #include "LyXView.h"
 #include "Painter.h"
-#include "font.h"
 #include "lyxrc.h"
 #include "math_matrixinset.h"
 #include "mathed/support.h"
 
-using std::ostringstream;
 using std::ostream;
 using std::ifstream;
 using std::istream;
@@ -50,29 +46,39 @@ using std::pair;
 using std::endl;
 using std::vector;
 
-extern char const * latex_mathenv[];
-extern MathCursor * mathcursor;
-
 
-// quite a hack i know. Should be done with return values...
-int number_of_newlines = 0;
+InsetFormula::InsetFormula()
+       : par_(new MathMatrixInset)
+{}
 
 
-InsetFormula::InsetFormula()
-       : InsetFormulaBase(new MathMatrixInset)
+InsetFormula::InsetFormula(const InsetFormula & f)
+       : InsetFormulaBase(f), par_(static_cast<MathMatrixInset *>(f.par_->clone()))
 {}
 
 
 InsetFormula::InsetFormula(MathInsetTypes t)
-       : InsetFormulaBase(new MathMatrixInset(t))
+       : par_(new MathMatrixInset(t))
 {}
 
 
-InsetFormula::InsetFormula(string const & s)
-       : InsetFormulaBase(0)
+InsetFormula::InsetFormula(string const & s) 
+       : par_(mathed_parse_normal(s))
 {
-       istringstream is(s.c_str());
-       par(mathed_parse(is));
+       if (!par_)
+               par_ = mathed_parse_normal("$" + s + "$");
+
+       if (!par_) {
+               lyxerr << "cannot interpret '" << s << "' as math\n";
+               par_ = new MathMatrixInset(LM_OT_SIMPLE);
+       }
+       metrics();
+}
+
+
+InsetFormula::~InsetFormula()
+{
+       delete par_;
 }
 
 
@@ -82,42 +88,43 @@ Inset * InsetFormula::clone(Buffer const &, bool) const
 }
 
 
-void InsetFormula::write(ostream & os) const
+void InsetFormula::write(Buffer const * buf, ostream & os) const
 {
        os << "Formula ";
-       latex(os, false, false);
+       latex(buf, os, false, false);
 }
 
 
-int InsetFormula::latex(ostream & os, bool fragile, bool) const
+int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
 {
-       par()->Write(os, fragile);
+       par_->write(os, fragile);
        return 1;
 }
 
 
-int InsetFormula::ascii(ostream & os, int) const
+int InsetFormula::ascii(Buffer const *, ostream & os, int) const
 {
-       par()->Write(os, false);
+       par_->write(os, false);
        return 1;
 }
 
 
-int InsetFormula::linuxdoc(ostream & os) const
+int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
 {
-       return ascii(os, 0);
+       return ascii(buf, os, 0);
 }
 
 
-int InsetFormula::docBook(ostream & os) const
+int InsetFormula::docbook(Buffer const * buf, ostream & os) const
 {
-       return ascii(os, 0);
+       return ascii(buf, os, 0);
 }
 
 
-void InsetFormula::read(LyXLex & lex)
+void InsetFormula::read(Buffer const *, LyXLex & lex)
 {
-       par(mathed_parse(lex));
+       par(mathed_parse_normal(lex));
+       metrics();
 }
 
 
@@ -126,39 +133,37 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &,
 {
        int x = int(xx) - 1;
        y -= 2;
+
        MathInset::workwidth = bv->workWidth();
        Painter & pain = bv->painter();
 
-       int w = par()->width();
-       int h = par()->height();
-       int a = par()->ascent();
-       pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
-
-       if (mathcursor) {
-               par()->Metrics(LM_ST_TEXT);
-
-               if (mathcursor->formula() == this) {
-                       if (mathcursor->Selection()) {
-                               int xp[10];
-                               int yp[10];
-                               int n;
-                               mathcursor->SelGetArea(xp, yp, n);
-                               pain.fillPolygon(xp, yp, n, LColor::selection);
-                       }
-                       pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
-               }
+       metrics();
+       int w = par_->width();
+       int h = par_->height();
+       int a = par_->ascent();
+       pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
+
+       if (mathcursor && mathcursor->formula() == this) {
+               mathcursor->drawSelection(pain);
+               pain.rectangle(x, y - a, w, h, LColor::mathframe);
        }
 
-       par()->draw(pain, int(x), y);
-       xx += par()->width();
+       par_->draw(pain, x, y);
+       xx += par_->width();
 
        setCursorVisible(false);
 }
 
 
+void InsetFormula::metrics() const 
+{
+       par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
+}
+
+
 vector<string> const InsetFormula::getLabelList() const
 {
-       return par()->getLabelList();
+       return par_->getLabelList();
 }
 
 
@@ -173,7 +178,8 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                case LFUN_BREAKLINE: 
                        bv->lockedInsetStoreUndo(Undo::INSERT);
                        mathcursor->breakLine();
-                       updateLocal(bv);
+                       mathcursor->normalize();
+                       updateLocal(bv, true);
                        break;
 
                case LFUN_MATH_NUMBER:
@@ -181,11 +187,11 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        //lyxerr << "toggling all numbers\n";
                        if (display()) {
                                bv->lockedInsetStoreUndo(Undo::INSERT);
-                               bool old = par()->numberedType();
-                               for (int row = 0; row < par()->nrows(); ++row)
-                                       par()->numbered(row, !old);
+                               bool old = par_->numberedType();
+                               for (unsigned int row = 0; row < par_->nrows(); ++row)
+                                       par_->numbered(row, !old);
                                bv->owner()->message(old ? _("No number") : _("Number"));
-                               updateLocal(bv);
+                               updateLocal(bv, true);
                        }
                        break;
                }
@@ -196,10 +202,10 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        if (display()) {
                                bv->lockedInsetStoreUndo(Undo::INSERT);
                                int row = mathcursor->row();
-                               bool old = par()->numbered(row);
+                               bool old = par_->numbered(row);
                                bv->owner()->message(old ? _("No number") : _("Number"));
-                               par()->numbered(row, !old);
-                               updateLocal(bv);
+                               par_->numbered(row, !old);
+                               updateLocal(bv, true);
                        }
                        break;
                }
@@ -209,7 +215,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        bv->lockedInsetStoreUndo(Undo::INSERT);
 
                        int row = mathcursor->row();
-                       string old_label = par()->label(row);
+                       string old_label = par_->label(row);
                        string new_label = arg;
 
                        if (new_label.empty()) {
@@ -230,22 +236,22 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
 
                        if (!new_label.empty()) {
                                lyxerr << "setting label to '" << new_label << "'\n";
-                               par()->numbered(row, true);
+                               par_->numbered(row, true);
                        }
 
                        if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
                                bv->redraw();
 
-                       par()->label(row, new_label);
+                       par_->label(row, new_label);
 
-                       updateLocal(bv);
+                       updateLocal(bv, true);
                        break;
                }
 
                case LFUN_MATH_EXTERN:
                        bv->lockedInsetStoreUndo(Undo::EDIT);
                        handleExtern(arg, bv);
-                       updateLocal(bv);
+                       updateLocal(bv, true);
                        break;
 
                case LFUN_MATH_MUTATE:
@@ -253,11 +259,11 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                        bv->lockedInsetStoreUndo(Undo::EDIT);
                        int x;
                        int y;
-                       mathcursor->GetPos(x, y);
-                       par()->mutate(arg);
-                       mathcursor->SetPos(x, y);
+                       mathcursor->getPos(x, y);
+                       par_->mutate(arg);
+                       mathcursor->setPos(x, y);
                        mathcursor->normalize();
-                       updateLocal(bv);
+                       updateLocal(bv, true);
                        break;
                }
 
@@ -265,18 +271,33 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action,
                {
                        int x;
                        int y;
-                       mathcursor->GetPos(x, y);
-                       if (par()->GetType() == LM_OT_SIMPLE) {
-                               par()->mutate(LM_OT_EQUATION);
-                               par()->numbered(0, false);
-                       }
+                       mathcursor->getPos(x, y);
+                       if (par_->getType() == LM_OT_SIMPLE)
+                               par_->mutate(LM_OT_EQUATION);
                        else
-                               par()->mutate(LM_OT_SIMPLE);
-                       mathcursor->SetPos(x, y);
+                               par_->mutate(LM_OT_SIMPLE);
+                       mathcursor->setPos(x, y);
                        mathcursor->normalize();
-                       updateLocal(bv);
+                       updateLocal(bv, true);
                        break;
                }
+               
+               case LFUN_PASTESELECTION:
+               {
+                       string const clip = bv->getClipboard();
+               if (!clip.empty())
+                               par(mathed_parse_normal(clip));
+                       break;
+               }
+
+               case LFUN_MATH_COLUMN_INSERT:
+               {
+                       if (par_->getType() == LM_OT_ALIGN)
+                               par_->mutate(LM_OT_ALIGNAT);
+                       par_->addCol(par_->ncols());
+                       mathcursor->normalize();
+                       updateLocal(bv, true);
+               }
 
                default:
                        result = InsetFormulaBase::localDispatch(bv, action, arg);
@@ -291,31 +312,33 @@ void InsetFormula::handleExtern(const string & arg, BufferView *)
        //string outfile = lyx::tempName("maple.out");
        string outfile = "/tmp/lyx2" + arg + ".out";
        ostringstream os;
-       par()->WriteNormal(os); 
+       par_->writeNormal(os); 
        string code = os.str().c_str();
        string script = "lyx2" + arg + " '" + code + "' " + outfile;
        lyxerr << "calling: " << script << endl;
        Systemcalls cmd(Systemcalls::System, script, 0);
 
        ifstream is(outfile.c_str());
-       par(mathed_parse(is));
+       par(mathed_parse_normal(is));
+       metrics();
 }
 
 bool InsetFormula::display() const
 {
-       return par_->GetType() != LM_OT_SIMPLE;
+       return par_->getType() != LM_OT_SIMPLE;
 }
 
 
-MathMatrixInset * InsetFormula::par() const
+MathInset const * InsetFormula::par() const
 {
-       return static_cast<MathMatrixInset *>(par_);
+       return par_;
 }
 
-void InsetFormula::par(MathInset * p)
+
+void InsetFormula::par(MathMatrixInset * p)
 { 
        delete par_;
-       par_ = p ? p : new MathMatrixInset;
+       par_ = p ? static_cast<MathMatrixInset *>(p) : new MathMatrixInset;
 }
 
 
@@ -327,24 +350,36 @@ Inset::Code InsetFormula::lyxCode() const
 
 void InsetFormula::validate(LaTeXFeatures & features) const
 {
-       par()->Validate(features);
+       par_->validate(features);
+}
+
+
+bool InsetFormula::insetAllowed(Inset::Code code) const
+{
+       return code == Inset::LABEL_CODE && display(); 
 }
 
 
 int InsetFormula::ascent(BufferView *, LyXFont const &) const
 {
-       return par()->ascent() + 4;
+       return par_->ascent() + 1;
 }
 
 
 int InsetFormula::descent(BufferView *, LyXFont const &) const
 {
-       return par()->descent();
+       return par_->descent() + 1;
 }
 
 
 int InsetFormula::width(BufferView *, LyXFont const &) const
 {
-       par()->Metrics(LM_ST_TEXT);
-       return par()->width();
+       metrics();
+       return par_->width();
+}
+
+
+MathInsetTypes InsetFormula::getType() const
+{
+       return par_->getType();
 }