From 9e6c3bd7c2afa65d85d2f8bbfb1f671827a5a082 Mon Sep 17 00:00:00 2001 From: Martin Vermeer Date: Thu, 6 Apr 2006 09:46:01 +0000 Subject: [PATCH] The nicefrac patch * LaTeXFeatures.C (simplefeatures[]: add nicefrac * mathed/math_factory.C (createMathInset): change frac semantics * mathed/math_fracinset.[Ch] (MathFracInset::metrics): (MathFracInset::draw): (MathFracInset::drawT): (MathFracInset::write): (MathFracInset::mathmlize): add nicefrac stuff * mathed/math_dfracinset.C: * mathed/math_tfracinset.C: adapt semantics * frontends/qt2/ui/QMathDialogBase.ui * frontends/qt2/QMathDialog.[Ch] (QMathDialog::symbol_clicked): provide GUI git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13566 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LaTeXFeatures.C | 1 + src/frontends/qt2/QMathDialog.C | 26 +++++++++- src/frontends/qt2/QMathDialog.h | 2 +- src/frontends/qt2/ui/QMathDialogBase.ui | 6 --- src/mathed/math_dfracinset.C | 2 +- src/mathed/math_factory.C | 4 +- src/mathed/math_fracinset.C | 69 ++++++++++++++++++++----- src/mathed/math_fracinset.h | 13 ++++- src/mathed/math_tfracinset.C | 2 +- 9 files changed, 97 insertions(+), 28 deletions(-) diff --git a/src/LaTeXFeatures.C b/src/LaTeXFeatures.C index 633c06143b..6dee6f82c8 100644 --- a/src/LaTeXFeatures.C +++ b/src/LaTeXFeatures.C @@ -242,6 +242,7 @@ char const * simplefeatures[] = { "dvipost", "fancybox", "calc", + "nicefrac", }; int const nb_simplefeatures = sizeof(simplefeatures) / sizeof(char const *); diff --git a/src/frontends/qt2/QMathDialog.C b/src/frontends/qt2/QMathDialog.C index 8448de3fa2..d47eec8ef5 100644 --- a/src/frontends/qt2/QMathDialog.C +++ b/src/frontends/qt2/QMathDialog.C @@ -124,6 +124,18 @@ QMathDialog::QMathDialog(QMath * form) connect(m, SIGNAL(activated(int)), this, SLOT(insertRoot(int))); sqrtPB->setPopup(m); + m = new QPopupMenu(fracPB); + m->setCaption(qt_("LyX: Fractions")); + m->insertTearOffHandle(); + m->insertItem(qt_("Standard \\frac"), 1); + m->insertItem(qt_("No hor. line \\atop"), 2); + m->insertItem(qt_("Nice \\nicefrac"), 3); + m->insertItem(qt_("Text frac (amsmath) \\tfrac"), 4); + m->insertItem(qt_("Display frac (amsmath) \\dfrac"), 5); + m->insertItem(qt_("Binomial \\choose"), 6); + connect(m, SIGNAL(activated(int)), this, SLOT(fracClicked(int))); + fracPB->setPopup(m); + m = new QPopupMenu(stylePB); m->setCaption(qt_("LyX: Math Styles")); m->insertTearOffHandle(); @@ -191,9 +203,19 @@ void QMathDialog::symbol_clicked(const string & str) } -void QMathDialog::fracClicked() +void QMathDialog::fracClicked(int id) { - form_->controller().dispatchInsert("frac"); + string str; + switch (id) { + case 1: str = "frac"; break; + case 2: str = "atop"; break; + case 3: str = "nicefrac"; break; + case 4: str = "tfrac"; break; + case 5: str = "dfrac"; break; + case 6: str = "choose"; break; + default: return; + } + form_->controller().dispatchInsert(str); } diff --git a/src/frontends/qt2/QMathDialog.h b/src/frontends/qt2/QMathDialog.h index 41874330bf..ea0f96edf8 100644 --- a/src/frontends/qt2/QMathDialog.h +++ b/src/frontends/qt2/QMathDialog.h @@ -30,7 +30,7 @@ public: public slots: virtual void delimiterClicked(); virtual void expandClicked(); - virtual void fracClicked(); + virtual void fracClicked(int); virtual void functionSelected(const QString &); virtual void matrixClicked(); virtual void subscriptClicked(); diff --git a/src/frontends/qt2/ui/QMathDialogBase.ui b/src/frontends/qt2/ui/QMathDialogBase.ui index 5e86440c35..5c078ceb50 100644 --- a/src/frontends/qt2/ui/QMathDialogBase.ui +++ b/src/frontends/qt2/ui/QMathDialogBase.ui @@ -813,12 +813,6 @@ QMathDialogBase accept() - - fracPB - clicked() - QMathDialogBase - fracClicked() - superscriptPB clicked() diff --git a/src/mathed/math_dfracinset.C b/src/mathed/math_dfracinset.C index 3b0c76d280..164b3c705b 100644 --- a/src/mathed/math_dfracinset.C +++ b/src/mathed/math_dfracinset.C @@ -24,7 +24,7 @@ using std::auto_ptr; MathDfracInset::MathDfracInset() - : MathFracInset(false) + : MathFracInset() {} diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index a959e50d08..36f3505dc0 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -317,10 +317,12 @@ MathAtom createMathInset(string const & s) return MathAtom(new MathBinomInset(s == "choose")); if (s == "over" || s == "frac") return MathAtom(new MathFracInset); + if (s == "nicefrac") + return MathAtom(new MathFracInset(MathFracInset::NICEFRAC)); //if (s == "infer") // return MathAtom(new MathInferInset); if (s == "atop") - return MathAtom(new MathFracInset(true)); + return MathAtom(new MathFracInset(MathFracInset::ATOP)); if (s == "lefteqn") return MathAtom(new MathLefteqnInset); if (s == "boldsymbol") diff --git a/src/mathed/math_fracinset.C b/src/mathed/math_fracinset.C index 829ddf5134..a84fff3f5a 100644 --- a/src/mathed/math_fracinset.C +++ b/src/mathed/math_fracinset.C @@ -15,6 +15,7 @@ #include "math_data.h" #include "math_mathmlstream.h" #include "textpainter.h" +#include "LaTeXFeatures.h" #include "LColor.h" #include "frontends/Painter.h" @@ -24,8 +25,8 @@ using std::max; using std::auto_ptr; -MathFracInset::MathFracInset(bool atop) - : atop_(atop) +MathFracInset::MathFracInset(Kind kind) + : kind_(kind) {} @@ -37,13 +38,13 @@ auto_ptr MathFracInset::doClone() const MathFracInset * MathFracInset::asFracInset() { - return atop_ ? 0 : this; + return kind_ == ATOP ? 0 : this; } MathFracInset const * MathFracInset::asFracInset() const { - return atop_ ? 0 : this; + return kind_ == ATOP ? 0 : this; } @@ -52,9 +53,15 @@ void MathFracInset::metrics(MetricsInfo & mi, Dimension & dim) const FracChanger dummy(mi.base); cell(0).metrics(mi); cell(1).metrics(mi); - dim.wid = max(cell(0).width(), cell(1).width()) + 2; - dim.asc = cell(0).height() + 2 + 5; - dim.des = cell(1).height() + 2 - 5; + if (kind_ == NICEFRAC) { + dim.wid = cell(0).width() + cell(1).width() + 5; + dim.asc = cell(0).height() + 5; + dim.des = cell(1).height() - 5; + } else { + dim.wid = max(cell(0).width(), cell(1).width()) + 2; + dim.asc = cell(0).height() + 2 + 5; + dim.des = cell(1).height() + 2 - 5; + } metricsMarkers(dim); dim_ = dim; } @@ -65,10 +72,25 @@ void MathFracInset::draw(PainterInfo & pi, int x, int y) const setPosCache(pi, x, y); int m = x + dim_.wid / 2; FracChanger dummy(pi.base); - cell(0).draw(pi, m - cell(0).width() / 2, y - cell(0).descent() - 2 - 5); - cell(1).draw(pi, m - cell(1).width() / 2, y + cell(1).ascent() + 2 - 5); - if (!atop_) - pi.pain.line(x + 1, y - 5, x + dim_.wid - 2, y - 5, LColor::math); + if (kind_ == NICEFRAC) { + cell(0).draw(pi, x + 2, + y - cell(0).descent() - 5); + cell(1).draw(pi, x + cell(0).width() + 5, + y + cell(1).ascent() / 2); + } else { + cell(0).draw(pi, m - cell(0).width() / 2, + y - cell(0).descent() - 2 - 5); + cell(1).draw(pi, m - cell(1).width() / 2, + y + cell(1).ascent() + 2 - 5); + } + if (kind_ == NICEFRAC) + pi.pain.line(x + cell(0).width(), + y + dim_.des - 2, + x + cell(0).width() + 5, + y - dim_.asc + 2, LColor::math); + if (kind_ == FRAC) + pi.pain.line(x + 1, y - 5, + x + dim_.wid - 2, y - 5, LColor::math); drawMarkers(pi, x, y); } @@ -89,14 +111,15 @@ void MathFracInset::drawT(TextPainter & pain, int x, int y) const int m = x + dim_.width() / 2; cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1); cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent()); - if (!atop_) + // ASCII art: ignore niceties + if (kind_ == FRAC || kind_ == NICEFRAC) pain.horizontalLine(x, y, dim_.width()); } void MathFracInset::write(WriteStream & os) const { - if (atop_) + if (kind_ == ATOP) os << '{' << cell(0) << "\\atop " << cell(1) << '}'; else // it's \\frac MathNestInset::write(os); @@ -105,7 +128,16 @@ void MathFracInset::write(WriteStream & os) const string MathFracInset::name() const { - return atop_ ? "atop" : "frac"; + switch (kind_) { + case FRAC: + return "frac"; + case NICEFRAC: + return "nicefrac"; + case ATOP: + return "atop"; + default: + return string(); + } } @@ -131,3 +163,12 @@ void MathFracInset::mathmlize(MathMLStream & os) const { os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac"); } + + +void MathFracInset::validate(LaTeXFeatures & features) const +{ + if (kind_ == NICEFRAC) + features.require("nicefrac"); + MathNestInset::validate(features); +} + diff --git a/src/mathed/math_fracinset.h b/src/mathed/math_fracinset.h index 2707fe8cc0..00c05753a0 100644 --- a/src/mathed/math_fracinset.h +++ b/src/mathed/math_fracinset.h @@ -20,7 +20,14 @@ class MathFracInset : public MathFracbaseInset { public: /// - explicit MathFracInset(bool atop = false); + enum Kind { + FRAC, + ATOP, + NICEFRAC + }; + + /// + explicit MathFracInset(Kind kind = FRAC); /// void metrics(MetricsInfo & mi, Dimension & dim) const; /// @@ -46,10 +53,12 @@ public: void octave(OctaveStream &) const; /// void mathmlize(MathMLStream &) const; + /// + void validate(LaTeXFeatures & features) const; public: virtual std::auto_ptr doClone() const; /// - bool const atop_; + Kind kind_; }; #endif diff --git a/src/mathed/math_tfracinset.C b/src/mathed/math_tfracinset.C index 9671363c2f..cd55d5f285 100644 --- a/src/mathed/math_tfracinset.C +++ b/src/mathed/math_tfracinset.C @@ -27,7 +27,7 @@ using std::auto_ptr; MathTfracInset::MathTfracInset() - : MathFracInset(false) + : MathFracInset() {} -- 2.39.2