4 * Purpose: Math editor definitions
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: January 1996
7 * Description: Math paragraph and objects for a WYSIWYG math editor.
11 * Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
13 * Version: 0.8beta, Mathed & Lyx project.
15 * You are free to use and modify this code under the terms of
16 * the GNU General Public Licence version 2 or later.
31 #define MATH_ALIGN_LEFT 1
33 #define MATH_ALIGN_RIGHT 2
35 #define MATH_ALIGN_BOTTOM 4
37 #define MATH_ALIGN_TOP 8
44 /// Standard Math Sizes (Math mode styles)
57 /// Standard LaTeX Math Environments
58 enum MathedEnvironment {
74 /** The restrictions of a standard LaTeX math paragraph
75 allows to get a small number of text codes (<30) */
76 enum MathedTextCodes {
79 /// Open and Close group
89 /// Super and sub scripts
93 /// Editable Math Inset
95 /// Editable Text Inset
99 /// Internal code for constants
101 /// Internal code for variables
117 /// Math mode TeX characters ",;:{}"
119 /// Special characters "{}&#_%"
121 /// Internal code for operators
123 /// Internal code for symbols
132 /// This must be < 32
137 #define LM_TC_NORMAL LM_TC_VAR
140 /// Types of lyx-math insets
141 enum MathedInsetTypes {
144 /// A simple paragraph
146 /// A simple numbered paragraph
148 /// A multiline paragraph
150 /// A multiline numbered paragraph
191 enum MathedBinaryTypes {
199 LMB_BOP = (LMB_RELATION | LMB_OPERATOR)
207 /** Abstract base class for all math objects.
208 A math insets is for use of the math editor only, it isn't a
209 general LyX inset. It's used to represent all the math objects.
210 The formulaInset (a LyX inset) encapsulates a math inset.
214 /// A math inset has a name (usually its LaTeX name), type and font-size
215 MathedInset(char const *nm, short ot, short st);
217 MathedInset(MathedInset*);
219 virtual ~MathedInset() { };
222 virtual void Draw(int x, int baseline)=0;
224 /// Write LaTeX and Lyx code
225 virtual void Write(FILE *file)=0;
226 /// Write LaTeX and Lyx code
227 virtual void Write(LString &file)=0;
229 /// Reproduces itself
230 virtual MathedInset *Clone()=0;
232 /// Compute the size of the object
233 virtual void Metrics()=0;
235 virtual int Ascent() const { return ascent; }
237 virtual int Descent() const { return descent; }
239 virtual int Width() const { return width; }
241 virtual int Height() const { return ascent + descent; }
244 virtual bool GetLimits() const { return false; }
246 virtual void SetLimits(bool) { }
249 char const *GetName() const { return name; }
251 short GetType() const { return objtype; }
253 short GetStyle() const { return size; }
255 //Man: Avoid to use these functions if it's not strictly necessary
257 virtual void SetType(short t) { objtype = t; }
259 virtual void SetStyle(short st) { size = st; } // Metrics();
261 virtual void SetName(char const* n) { name = n; }
263 void setDrawable(long unsigned int d) { pm = d; }
278 /// This works while only one process can draw unless
279 /// the process have their own data
280 static long unsigned int pm;
282 static int df_asc, df_des, df_width;
284 /// In a near future maybe we use a better fonts renderer than X
285 void drawStr(short, int, int, int, byte*, int);
287 friend class MathedCursor;
289 friend void mathed_init_fonts();
295 /// Paragraph permissions
298 /// If false can use a non-standard size
300 /// If true can insert newlines
302 /// If true can use tabs
304 /// If true can insert new columns
305 LMPF_ALLOW_NEW_COL = 8,
306 /// Smaller than current size (frac)
308 /// Script size (subscript, stackrel)
313 /** The math paragraph base class, base to all editable math objects */
314 class MathParInset: public MathedInset {
317 MathParInset(short st=LM_ST_TEXT, char const *nm=NULL, short ot=LM_OT_MIN);
319 MathParInset(MathParInset*);
321 virtual ~MathParInset();
323 virtual MathedInset *Clone();
325 /// Draw the object on a drawable
326 virtual void Draw(int x, int baseline);
329 virtual void Write(FILE *file);
331 virtual void Write(LString &file);
333 virtual void Metrics();
335 virtual void UserSetSize(short);
337 /// Data is stored in a LyXArray
338 virtual void SetData(LyxArrayBase *);
340 virtual LyxArrayBase * GetData() { return array; }
342 /// Paragraph position
343 virtual void GetXY(int&, int&) const;
345 virtual void setXY(int x, int y) { xo =x; yo = y; }
347 virtual void SetFocus(int,int) { };
349 virtual bool Inside(int, int);
351 // Tab stuff used by Matrix.
353 virtual void SetAlign(char, char const*) { };
355 // virtual int GetTabPos() { return 0; }
357 // virtual int GetTab(int) { return 0; }
359 virtual int GetColumns() { return 1; }
361 virtual int GetRows() { return 1; }
363 virtual bool isMatrix() { return false; }
364 // /// These functions should report an error
365 // virtual char const* GetLabel() { return 0; }
366 // virtual char const* GetLabel(int) { return 0; }
368 // Vertical switching
370 virtual bool setArgumentIdx(int i) { return (i==0); }
372 virtual bool setNextArgIdx() { return false; }
374 virtual int getArgumentIdx() { return 0; }
376 virtual int getMaxArgumentIdx() { return 0; }
378 // virtual void SetLabel(char const*) { }
380 virtual void SetStyle(short);
382 virtual MathedRowSt *getRowSt() const { return 0; }
384 virtual void setRowSt(MathedRowSt*) { }
386 virtual bool Permit(short f) { return (bool)(f & flag); }
389 /// Paragraph data is stored here
391 /// Cursor start position
398 virtual void setFlag(MathedParFlag f) { flag |= f; }
400 friend class InsetFormula;
402 friend class MathedXIter;
404 friend class MathedCursor;
406 friend LyxArrayBase *mathed_parse(unsigned flags = 0, LyxArrayBase*a=NULL, MathParInset**p=NULL);
410 /* The physical structure of a row and aditional information is stored here.
411 It allows to manage the extra info independently of the paragraph data.
412 Only used for multiline paragraphs.
417 w = new int[n+1]; // this leaks
425 if (label) delete[] label;
427 /// Should be const but...
428 MathedRowSt* getNext() const { return next; }
429 /// ...we couldn't use this.
430 void setNext(MathedRowSt* n) { next = n; }
432 char const* getLabel() const { return label; }
434 bool isNumbered() const { return numbered; }
436 int getBaseline() const { return y; }
438 int getTab(int i) { return w[i]; }
440 void setLabel(char* l) { label = l; }
442 void setNumbered(bool nf) { numbered = nf; }
444 void setTab(int i, int t) { w[i] = t; }
458 friend class MathMatrixInset;
460 friend class MathedXIter;
464 /** Multiline math paragraph base class.
465 This is the base to all multiline editable math objects
466 like array and eqnarray.
468 class MathMatrixInset: public MathParInset {
471 MathMatrixInset(int m=1, int n=1, short st=LM_ST_TEXT);
473 MathMatrixInset(MathMatrixInset*);
475 MathedInset *Clone();
477 virtual ~MathMatrixInset();
481 void Write(FILE *file);
483 void Write(LString &file);
487 void SetData(LyxArrayBase *);
489 void SetAlign(char, char const*);
491 char *GetAlign(char* vv) {
498 int GetColumns() { return nc; }
500 int GetRows() { return nr; }
502 virtual bool isMatrix() { return true; }
504 /// Use this to manage the extra information independently of paragraph
505 MathedRowSt *getRowSt() const { return row; }
507 void setRowSt(MathedRowSt* r) { row = r; }
510 /// Number of columns & rows
515 char v_align; // add approp. signedness
518 /// Vertical structure
525 /************************* Prototypes **********************************/
527 LyxArrayBase *mathed_parse(unsigned flags, LyxArrayBase *data, MathParInset **mt);
529 void mathed_write(MathParInset*, FILE *, int*, char fragile, char const* label=NULL);
531 void mathed_write(MathParInset*, LString&, int*, char fragile, char const* label=NULL);
533 void mathed_parser_file(FILE*, int);
535 int mathed_parser_lineno();
537 int MathedLookupBOP(short);
539 /************************ Inline functions ********************************/
542 #define MathIsInset(x) (LM_TC_INSET<=(x) && (x)<=LM_TC_ACTIVE_INSET)
544 #define MathIsFont(x) (LM_TC_CONST<=(x) && (x)<=LM_TC_BSYM)
546 #define MathIsAlphaFont(x) (LM_TC_VAR<=(x) && (x)<=LM_TC_TEXTRM)
548 #define MathIsActive(x) (LM_TC_INSET<(x) && (x)<=LM_TC_ACTIVE_INSET)
550 #define MathIsUp(x) ((x)==LM_TC_UP)
552 #define MathIsDown(x) ((x)==LM_TC_DOWN)
554 #define MathIsScript(x) ((x)==LM_TC_DOWN || (x)==LM_TC_UP)
556 #define MathIsBOPS(x) (MathedLookupBOP(x)>LMB_NONE)
560 inline bool MathIsBinary(short x)
562 return (x==LM_TC_BOP || x==LM_TC_BOPS);
566 inline bool MathIsSymbol(short x) {
567 return (LM_TC_SYMB<=x && x<=LM_TC_BSYM);
572 MathedInset::MathedInset(char const *nm, short ot, short st):
573 name(nm), objtype(ot), size(st)
575 width = ascent = descent = 0;
579 bool MathParInset::Inside(int x, int y)
581 return (x>=xo && x<=xo+width && y<=yo+descent && y>=yo-ascent);
586 void MathParInset::GetXY(int& x, int& y) const
593 void MathParInset::UserSetSize(short sz)
597 flag = flag & ~LMPF_FIXED_SIZE;
603 void MathParInset::SetStyle(short sz)
605 if (Permit(LMPF_FIXED_SIZE)) {
606 if (Permit(LMPF_SCRIPT))
607 sz = (sz<LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
608 if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
611 MathedInset::SetStyle(sz);