MathAccentInset rewritten

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2215 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-07-10 13:17:43 +00:00
parent 4124b05c9f
commit 88b5d739b5
8 changed files with 126 additions and 317 deletions

View File

@ -1,4 +1,12 @@
2001-07-04 André Pönitz <poenitz@htwm.de>
2001-07-10 André Pönitz <poenitz@htwm.de>
* math_accentinset.[hC]: rewrite
* math_parser.C:
math_cursor.C: subsequent changes
2001-07-10 André Pönitz <poenitz@htwm.de>
* math_grid.C: <Delete> in the first cell of a completely empty row
deletes that row, <C-Return> places the cursor in the first of the

View File

@ -39,7 +39,6 @@
#include "font.h"
#include "math_arrayinset.h"
#include "math_spaceinset.h"
#include "math_deliminset.h"
#include "support/lyxlib.h"
#include "mathed/support.h"
#include "undo_funcs.h"
@ -78,6 +77,18 @@ void handleFont(BufferView * bv, MathTextCodes t)
mathcursor->handleFont(t);
}
void handleAccent(BufferView * bv, int code)
{
bv->lockedInsetStoreUndo(Undo::EDIT);
mathcursor->handleAccent(code);
}
void handleDelim(BufferView * bv, int l, int r)
{
bv->lockedInsetStoreUndo(Undo::EDIT);
mathcursor->handleDelim(l, r);
}
bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
{
LyXText * lt = bv->getLyXText();
@ -92,6 +103,7 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
return true;
}
} // namespaces
@ -621,16 +633,16 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
// --- accented characters ------------------------------
case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
case LFUN_CARON: mathcursor->setAccent(LM_check); break;
case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
case LFUN_UMLAUT: handleAccent(bv, LM_ddot); break;
case LFUN_CIRCUMFLEX: handleAccent(bv, LM_hat); break;
case LFUN_GRAVE: handleAccent(bv, LM_grave); break;
case LFUN_ACUTE: handleAccent(bv, LM_acute); break;
case LFUN_TILDE: handleAccent(bv, LM_tilde); break;
case LFUN_MACRON: handleAccent(bv, LM_bar); break;
case LFUN_DOT: handleAccent(bv, LM_dot); break;
case LFUN_CARON: handleAccent(bv, LM_check); break;
case LFUN_BREVE: handleAccent(bv, LM_breve); break;
case LFUN_VECTOR: handleAccent(bv, LM_vec); break;
// Greek mode
case LFUN_GREEK:
@ -735,16 +747,7 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
} else if (vdelim.find(rt[0]) != string::npos)
irt = rt[0];
if (mathcursor->selection) {
MathDelimInset * p = new MathDelimInset(ilt, irt);
MathArray ar;
mathcursor->selArray(ar);
lyxerr << "selarray: " << ar << "\n";
p->cell(0) = ar;
mathcursor->insert(p);
} else {
mathcursor->insert(new MathDelimInset(ilt, irt));
}
handleDelim(bv, ilt, irt);
updateLocal(bv);
break;
}
@ -843,16 +846,11 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
default:
if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
unsigned char c = arg[0];
lyxerr << "char: '" << c << "' int: " << int(c) << endl;
//lyxerr << "char: '" << c << "' int: " << int(c) << endl;
//owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
//lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
bv->lockedInsetStoreUndo(Undo::INSERT);
if (c == ' ' && mathcursor->getAccent() == LM_hat) {
c = '^';
mathcursor->setAccent(0);
}
if (c == 0) { // Dead key, do nothing
//lyxerr << "deadkey" << endl;
break;

View File

@ -7,60 +7,24 @@
using std::ostream;
MathAccentInset::MathAccentInset(byte cx, MathTextCodes f, int cd)
: MathInset(1), c(cx), fn(f), code(cd), inset(0)
MathAccentInset::MathAccentInset(int f)
: MathInset(1), code(f)
{}
MathAccentInset::MathAccentInset(MathInset * ins, int cd)
: MathInset(0), c(0), fn(LM_TC_MIN), code(cd), inset(ins)
{}
MathAccentInset::~MathAccentInset()
{
delete inset;
}
MathInset * MathAccentInset::clone() const
{
MathAccentInset * p;
if (inset)
p = new MathAccentInset(inset->clone(), code);
else
p = new MathAccentInset(c, fn, code);
return p;
return new MathAccentInset(*this);
}
void MathAccentInset::draw(Painter & pain, int x, int y)
{
int const dw = width() - 2;
if (inset)
inset->draw(pain, x, y);
else
drawChar(pain, fn, size(), x, y, c);
x += (code == LM_not) ? (width() - dw) / 2 : 2;
mathed_draw_deco(pain, x, y - dy, dw, dh, code);
}
void MathAccentInset::Metrics(MathStyles st)
{
if (inset) {
inset->Metrics(st);
ascent_ = inset->ascent();
descent_ = inset->descent();
width_ = inset->width();
dh = ascent_;
} else {
mathed_char_dim(fn, size(), c, ascent_, descent_, width_);
dh = width() / 2 - 1;
}
xcell(0).Metrics(st);
ascent_ = xcell(0).ascent();
descent_ = xcell(0).descent();
width_ = xcell(0).width();
dh = 5;
if (code == LM_not) {
ascent_ += dh;
descent_ += dh;
@ -69,8 +33,14 @@ void MathAccentInset::Metrics(MathStyles st)
ascent_ += dh + 2;
dy = ascent_;
// if (MathIsBinary(fn))
// width += 2*mathed_char_width(fn, size, ' ');
}
void MathAccentInset::draw(Painter & pain, int x, int y)
{
int const dw = width() - 2;
xcell(0).draw(pain, x, y);
x += (code == LM_not) ? (width() - dw) / 2 : 2;
mathed_draw_deco(pain, x, y - dy, dw, dh, code);
}
@ -78,28 +48,14 @@ void MathAccentInset::Write(ostream & os, bool fragile) const
{
latexkeys const * l = lm_get_key_by_id(code, LM_TK_ACCENT);
os << '\\' << l->name;
if (code!= LM_not)
os << '{';
else
if (code == LM_not)
os << ' ';
else
os << '{';
if (inset)
inset->Write(os, fragile);
else {
if (fn>= LM_TC_RM && fn <= LM_TC_TEXTRM)
os << '\\' << math_font_name[fn - LM_TC_RM] << '{';
if (MathIsSymbol(fn)) {
latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
if (l)
os << '\\' << l->name << ' ';
} else
os << char(c);
if (fn>= LM_TC_RM && fn<= LM_TC_TEXTRM)
os << '}';
}
cell(0).Write(os, fragile);
if (code!= LM_not)
if (code != LM_not)
os << '}';
}
@ -109,23 +65,7 @@ void MathAccentInset::WriteNormal(ostream & os) const
latexkeys const * l = lm_get_key_by_id(code, LM_TK_ACCENT);
os << "[accent " << l->name << " ";
if (inset)
inset->WriteNormal(os);
else {
if (fn >= LM_TC_RM && fn <= LM_TC_TEXTRM)
os << "[font " << math_font_name[fn - LM_TC_RM] << "]";
if (MathIsSymbol(fn)) {
latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
if (l)
os << "[symbol " << l->name << "] ";
} else
os << "[char " << char(c) << "] ";
}
cell(0).WriteNormal(os);
os << "] ";
}
int MathAccentInset::getAccentCode() const
{
return code;
}

View File

@ -10,13 +10,9 @@
class MathAccentInset : public MathInset {
public:
///
MathAccentInset(byte, MathTextCodes, int);
explicit MathAccentInset(int);
///
MathAccentInset(MathInset *, int);
///
~MathAccentInset();
///
MathInset * clone() const;
MathInset * clone() const;
///
void draw(Painter &, int, int);
///
@ -29,17 +25,13 @@ public:
int getAccentCode() const;
///
bool isAccentInset() const { return true; }
protected:
///
byte c;
///
MathTextCodes fn;
private:
///
int code;
///
MathInset * inset;
int dh;
///
int dh, dy;
int dy;
};
#endif

View File

@ -42,6 +42,7 @@
#include "math_fracinset.h"
#include "math_decorationinset.h"
#include "math_dotsinset.h"
#include "math_deliminset.h"
#include "math_accentinset.h"
#include "math_macrotemplate.h"
#include "math_sqrtinset.h"
@ -79,7 +80,6 @@ bool IsMacro(short tok, int id)
MathCursor::MathCursor(InsetFormulaBase * formula)
: formula_(formula)
{
accent = 0;
lastcode = LM_TC_MIN;
macro_mode = false;
first();
@ -345,10 +345,9 @@ void MathCursor::End()
}
void MathCursor::insert(char c, MathTextCodes t)
{
lyxerr << "inserting '" << c << "'\n";
//lyxerr << "inserting '" << c << "'\n";
if (selection)
SelDel();
@ -366,15 +365,10 @@ void MathCursor::insert(char c, MathTextCodes t)
}
}
if (accent)
doAccent(c, t);
else {
array().insert(cursor_, c, t);
array().next(cursor_);
}
array().insert(cursor_, c, t);
array().next(cursor_);
lastcode = t;
return;
}
@ -389,15 +383,8 @@ void MathCursor::insert(MathInset * p)
SelDel();
}
if (accent && !p->nargs())
doAccent(p);
else {
array().insert(cursor_, p);
array().next(cursor_);
}
//if (p->nargs())
// push(p, true);
array().insert(cursor_, p);
array().next(cursor_);
}
@ -670,7 +657,7 @@ in_word_set(s) << " \n";
break;
case LM_TK_ACCENT:
setAccent(l->id);
p = new MathAccentInset(l->id);
break;
case LM_TK_MACRO:
@ -727,9 +714,6 @@ void MathCursor::MacroModeClose()
imacro->SetName(l->name);
} else {
Left();
if (nextInset()->isAccentInset())
setAccent(
static_cast<MathAccentInset*>(nextInset())->getAccentCode());
array().erase(cursor_);
if (l || MathMacroTable::hasTemplate(imacro->name()))
Interpret(imacro->name());
@ -753,16 +737,6 @@ void MathCursor::SelCopy()
}
}
void MathCursor::selArray(MathArray & ar) const
{
int const p1 = min(cursor_, anchor_);
int const p2 = max(cursor_, anchor_);
ar = array();
ar.erase(p2, ar.size());
ar.erase(0, p1);
}
void MathCursor::SelCut()
{
seldump("SelCut");
@ -892,57 +866,6 @@ void MathCursor::SelGetArea(int * xpoint, int * ypoint, int & n)
}
void MathCursor::setAccent(int ac)
{
if (ac > 0 && accent < 8)
nestaccent[accent++] = ac;
else
accent = 0; // consumed!
}
int MathCursor::getAccent() const
{
return accent > 0 ? nestaccent[accent - 1] : 0;
}
void MathCursor::doAccent(char c, MathTextCodes t)
{
MathInset * ac = 0;
for (int i = accent - 1; i >= 0; --i) {
if (i == accent - 1)
ac = new MathAccentInset(c, t, nestaccent[i]);
else
ac = new MathAccentInset(ac, nestaccent[i]);
}
if (ac)
insert(ac);
accent = 0; // consumed!
}
void MathCursor::doAccent(MathInset * p)
{
MathInset * ac = 0;
for (int i = accent - 1; i >= 0; --i) {
if (i == accent - 1)
ac = new MathAccentInset(p, nestaccent[i]);
else
ac = new MathAccentInset(ac, nestaccent[i]);
}
if (ac)
insert(ac);
accent = 0; // consumed!
}
void MathCursor::handleFont(MathTextCodes t)
{
if (selection) {
@ -963,6 +886,27 @@ void MathCursor::handleFont(MathTextCodes t)
}
void MathCursor::handleAccent(int code)
{
MathAccentInset * p = new MathAccentInset(code);
if (selection) {
SelCut();
p->cell(0) = selarray;
}
insert(p);
}
void MathCursor::handleDelim(int l, int r)
{
MathDelimInset * p = new MathDelimInset(l, r);
if (selection) {
SelCut();
p->cell(0) = selarray;
}
insert(p);
}
void MathCursor::GetPos(int & x, int & y)
{
x = xarray().xo() + xarray().pos2x(cursor_);

View File

@ -80,10 +80,6 @@ public:
void SetSize(MathStyles);
///
bool toggleLimits();
/// Set accent: if argument = 0 it's considered consumed
void setAccent(int ac = 0);
/// Returns last accent
int getAccent() const;
///
// Macro mode methods
void MacroModeOpen();
@ -117,6 +113,10 @@ public:
void setLastCode(MathTextCodes t);
///
void handleFont(MathTextCodes t);
///
void handleAccent(int code);
///
void handleDelim(int l, int r);
/// Splits cells and shifts right part to the next cell
void splitCell();
/// Splits line and insert new row of cell
@ -138,8 +138,6 @@ public:
///
MathTextCodes prevCode() const;
///
void selArray(MathArray &) const;
///
char valign() const;
///
char halign() const;
@ -166,14 +164,6 @@ public:
///
InsetFormulaBase * const formula_;
///
void doAccent(char c, MathTextCodes t);
///
void doAccent(MathInset * p);
///
int accent;
///
int nestaccent[8];
///
MathTextCodes lastcode;
///

View File

@ -94,7 +94,9 @@ void MathMacroTable::builtinMacros()
// This macro doesn't have arguments
{
MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
t->push_back(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not));
MathAccentInset * p = new MathAccentInset(LM_not);
p->cell(0).push_back(LM_in, LM_TC_BOPS);
t->push_back(p);
insertTemplate(t);
}
@ -111,7 +113,9 @@ void MathMacroTable::builtinMacros()
{
MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0);
t->push_back(new MathAccentInset('0', LM_TC_RM, LM_not));
MathAccentInset * p = new MathAccentInset(LM_not);
p->cell(0).push_back('0', LM_TC_VAR);
t->push_back(p);
insertTemplate(t);
}

View File

@ -227,9 +227,12 @@ unsigned char LexGetArg(unsigned char lf, bool accept_spaces = false)
int yylex()
{
static int init_done;
static bool init_done = false;
if (!init_done) LexInitCodes();
if (!init_done) {
LexInitCodes();
init_done = true;
}
while (yyis->good()) {
unsigned char c = getuchar(yyis);
@ -330,68 +333,6 @@ int yylex()
}
// Accent hacks only for 0.12. Stolen from Cursor.
int accent = 0;
int nestaccent[8];
void setAccent(int ac)
{
if (ac > 0 && accent < 8)
nestaccent[accent++] = ac;
else
accent = 0; // consumed!
}
MathInset * doAccent(unsigned char c, MathTextCodes t)
{
MathInset * ac = 0;
for (int i = accent - 1; i >= 0; --i) {
if (i == accent - 1)
ac = new MathAccentInset(c, t, nestaccent[i]);
else
ac = new MathAccentInset(ac, nestaccent[i]);
}
accent = 0; // consumed!
return ac;
}
MathInset * doAccent(MathInset * p)
{
MathInset * ac = 0;
for (int i = accent - 1; i >= 0; --i) {
if (i == accent - 1)
ac = new MathAccentInset(p, nestaccent[i]);
else
ac = new MathAccentInset(ac, nestaccent[i]);
}
accent = 0; // consumed!
return ac;
}
void do_insert(MathArray & dat, MathInset * m)
{
if (accent)
dat.push_back(doAccent(m));
else
dat.push_back(m);
}
void do_insert(MathArray & dat, unsigned char ch, MathTextCodes fcode)
{
if (accent)
dat.push_back(doAccent(ch, fcode));
else
dat.push_back(ch, fcode);
}
void handle_frac(MathArray & dat, string const & name)
{
MathFracInset * p = new MathFracInset(name);
@ -548,8 +489,6 @@ void mathed_parse(MathArray & array, unsigned flags)
yyvarcode = LM_TC_VAR;
int brace = 0;
int acc_brace = 0;
int acc_braces[8];
++plevel;
while (t) {
@ -569,7 +508,7 @@ void mathed_parse(MathArray & array, unsigned flags)
switch (t) {
case LM_TK_ALPHA:
do_insert(array, yylval.i, yyvarcode);
array.push_back(yylval.i, yyvarcode);
break;
case LM_TK_ARGUMENT:
@ -581,15 +520,11 @@ void mathed_parse(MathArray & array, unsigned flags)
break;
case LM_TK_STR:
do_insert(array, yylval.i, LM_TC_CONST);
array.push_back(yylval.i, LM_TC_CONST);
break;
case LM_TK_OPEN:
++brace;
if (accent && tprev == LM_TK_ACCENT) {
acc_braces[acc_brace++] = brace;
break;
}
if (flags & FLAG_BRACE_OPT) {
flags &= ~FLAG_BRACE_OPT;
flags |= FLAG_BRACE;
@ -608,10 +543,6 @@ void mathed_parse(MathArray & array, unsigned flags)
panic = true;
break;
}
if (acc_brace && brace == acc_braces[acc_brace - 1] - 1) {
--acc_brace;
break;
}
if (flags & FLAG_BRACE_FONT) {
yyvarcode = LM_TC_VAR;
flags &= ~FLAG_BRACE_FONT;
@ -704,13 +635,13 @@ void mathed_parse(MathArray & array, unsigned flags)
case LM_TK_SYM:
if (yylval.l->id < 256) {
MathTextCodes tc = MathIsBOPS(yylval.l->id) ? LM_TC_BOPS: LM_TC_SYMB;
do_insert(array, yylval.l->id, tc);
array.push_back(yylval.l->id, tc);
} else
do_insert(array, new MathFuncInset(yylval.l->name));
array.push_back(new MathFuncInset(yylval.l->name));
break;
case LM_TK_BOP:
do_insert(array, yylval.i, LM_TC_BOP);
array.push_back(yylval.i, LM_TC_BOP);
break;
case LM_TK_SPACE:
@ -801,15 +732,19 @@ void mathed_parse(MathArray & array, unsigned flags)
case LM_TK_WIDE:
{
MathDecorationInset * sq = new MathDecorationInset(yylval.l->id);
mathed_parse(sq->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
array.push_back(sq);
MathDecorationInset * p = new MathDecorationInset(yylval.l->id);
mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
array.push_back(p);
break;
}
case LM_TK_ACCENT:
setAccent(yylval.l->id);
{
MathAccentInset * p = new MathAccentInset(yylval.l->id);
mathed_parse(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST);
array.push_back(p);
break;
}
case LM_TK_NONUM:
curr_num = false;
@ -817,9 +752,9 @@ void mathed_parse(MathArray & array, unsigned flags)
case LM_TK_PMOD:
case LM_TK_FUNC:
if (accent)
array.push_back(t, LM_TC_CONST);
else
//if (accent)
// array.push_back(t, LM_TC_CONST);
//else
array.push_back(new MathFuncInset(yylval.l->name));
break;
@ -832,10 +767,10 @@ void mathed_parse(MathArray & array, unsigned flags)
MathMacro * m = MathMacroTable::cloneTemplate(yytext);
for (int i = 0; i < m->nargs(); ++i)
mathed_parse(m->cell(i), FLAG_BRACE_OPT | FLAG_BRACE_LAST);
do_insert(array, m);
array.push_back(m);
m->Metrics(LM_ST_TEXT);
} else
do_insert(array, new MathFuncInset(yytext, LM_OT_UNDEF));
array.push_back(new MathFuncInset(yytext, LM_OT_UNDEF));
break;
case LM_TK_END:
@ -862,7 +797,7 @@ void mathed_parse(MathArray & array, unsigned flags)
mm->halign(halign);
mathed_parse_lines(mm, halign.size(), latex_mathenv[i].numbered, false);
do_insert(array, mm);
array.push_back(mm);
//lyxerr << "read matrix " << *mm << "\n";
break;
} else
@ -871,7 +806,7 @@ void mathed_parse(MathArray & array, unsigned flags)
}
case LM_TK_MACRO:
do_insert(array, MathMacroTable::cloneTemplate(yylval.l->name));
array.push_back(MathMacroTable::cloneTemplate(yylval.l->name));
break;
case LM_TK_LABEL:
@ -944,5 +879,3 @@ MathInset * mathed_parse(LyXLex & lex)
return p;
}