]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
9a50ca15574a53f26dcf6fb1650125fa71164a5d
[lyx.git] / src / mathed / math_inset.h
1 // -*- C++ -*-
2 /*
3  *  File:        math_inset.h
4  *  Purpose:     Declaration of insets for mathed 
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
6  *  Created:     January 1996
7  *  Description: Math paragraph and objects for a WYSIWYG math editor.
8  *
9  *  Dependencies: Xlib, XForms
10  *
11  *  Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Mathed & Lyx project.
14  *
15  *   You are free to use and modify this code under the terms of
16  *   the GNU General Public Licence version 2 or later.
17  */
18
19 //  Note: These math insets are internal to Mathed and are not derived
20 //        from lyx inset.
21
22 #ifndef MATH_INSET
23 #define MATH_INSET
24
25 #ifdef __GNUG__
26 #pragma interface
27 #endif
28
29 #include <stdio.h>
30 #include "math_defs.h"
31 #include "symbol_def.h"
32 #include "LString.h"
33
34
35 /**
36  Functions or LaTeX names for objects that I don't know how to draw.
37  */
38 class MathFuncInset: public MathedInset  {
39 public:
40         ///
41    MathFuncInset(char const * nm,
42                  short ot = LM_OT_FUNC, short st = LM_ST_TEXT);
43         ///
44    ~MathFuncInset();
45         ///
46    MathedInset * Clone();
47         ///
48    void Draw(int, int);
49         ///
50    void Write(FILE * file);
51         ///
52    void Write(string & file);
53         ///
54    void Metrics();
55         ///
56    inline bool GetLimits() const;
57 protected:
58         ///
59    int ln;
60         ///
61    bool lims;
62         ///
63    char * fname;
64 };
65
66
67 /// Accents
68 class MathAccentInset: public MathedInset {
69  public:
70         ///
71    MathAccentInset(byte, MathedTextCodes, int, short st = LM_ST_TEXT);
72         ///
73    MathAccentInset(MathedInset *, int, short st = LM_ST_TEXT);
74         ///
75    ~MathAccentInset();
76         ///
77    MathedInset * Clone();
78         ///
79    void Draw(int, int);
80         ///
81    void Write(FILE * file);
82         ///
83    void Write(string & file);
84         ///
85    void Metrics();
86         ///
87    int getAccentCode() const { return code; }
88
89  protected:
90         ///
91    byte c;
92         ///
93    MathedTextCodes fn;
94         ///
95    int code;
96         ///
97    MathedInset * inset;
98         ///
99    int dh, dy;
100 };
101
102
103 ///
104 class MathDotsInset: public MathedInset {
105  public:
106         ///
107    MathDotsInset(char const *, int, short st = LM_ST_TEXT);
108         ///
109    ~MathDotsInset() {}
110         ///
111    MathedInset * Clone();
112         ///
113    void Draw(int, int);
114         ///
115    void Write(FILE * file);
116         ///
117    void Write(string & file);
118         ///
119    void Metrics();
120  protected:
121         ///
122    int dh, code;
123 };   
124
125
126 /// Smart spaces
127 class MathSpaceInset: public MathedInset  {
128  public:
129         ///
130    MathSpaceInset(int sp, short ot = LM_OT_SPACE, short st = LM_ST_TEXT);
131         ///
132    ~MathSpaceInset() {}
133         ///
134    MathedInset * Clone();
135         ///
136   void Draw(int, int);
137         ///
138    void Write(FILE * file);
139         ///
140    void Write(string & file);
141         ///
142    inline void Metrics();
143         ///
144    inline void SetSpace(int sp);
145         ///
146    int GetSpace() { return space; }
147  protected:
148         ///
149    int space;
150 };
151
152
153 /// big operators
154 class MathBigopInset: public MathedInset {
155  public:
156         ///
157    MathBigopInset(char const *, int, short st = LM_ST_TEXT);
158         ///
159    ~MathBigopInset() {}
160         ///
161    MathedInset * Clone();
162         ///
163    void Draw(int, int);
164         ///
165    void Write(FILE * file);
166         ///
167    void Write(string & file);
168         ///
169    void Metrics();
170         ///
171    inline bool GetLimits() const;
172         ///
173    inline void SetLimits(bool);
174  protected:
175         ///
176    int lims;
177         ///
178    int sym;   
179 };
180
181  
182 //------- All editable insets must be derived from MathParInset.
183
184 ///
185 class MathSqrtInset: public MathParInset {
186  public:
187     ///
188     MathSqrtInset(short st = LM_ST_TEXT);
189     ///
190     ~MathSqrtInset() {}
191     ///
192     MathedInset * Clone();
193     ///
194     void Draw(int x, int baseline);
195     ///
196     void Write(FILE * file);
197     ///
198     void Write(string & file);
199     ///
200     void Metrics();
201     ///
202     bool Inside(int, int);
203     
204  private:
205     ///
206    int hmax, wbody;
207 };
208
209
210 /// Fraction like objects (frac, stackrel, binom) 
211 class MathFracInset: public MathParInset {
212  public:
213         ///
214     MathFracInset(short ot = LM_OT_FRAC);
215         ///
216     ~MathFracInset();
217         ///
218     MathedInset * Clone();
219         ///
220     void Draw(int x, int baseline);
221         ///
222     void Write(FILE * file);
223         ///
224     void Write(string & file);
225         ///
226     void Metrics();
227  
228     /** This does the same that SetData(LyxArrayBase*) but for both
229          numerator and denominator at once.
230      */
231     void SetData(LyxArrayBase *, LyxArrayBase *);
232         ///
233     void SetData(LyxArrayBase *);
234         ///
235     void GetXY(int & x, int & y) const;
236         ///
237     void SetFocus(int, int);
238     ///
239     bool Inside(int, int);
240     ///
241     LyxArrayBase * GetData();
242     ///
243     bool setArgumentIdx(int i); // was bool Up/down(void);
244     ///
245     int  getArgumentIdx() { return int(idx); }
246     ///
247     int  getMaxArgumentIdx() { return 1; }
248         ///
249     void  SetStyle(short);
250  protected:
251         ///
252     short idx;
253         ///
254     MathParInset * den;
255         ///
256     int w0, w1, des0, dh;
257 };
258
259
260 /// A delimiter
261 class MathDelimInset: public MathParInset {
262  public:
263         ///
264    MathDelimInset(int, int, short st = LM_ST_TEXT);
265         ///
266    ~MathDelimInset() {}
267         ///
268    MathedInset * Clone();
269         ///
270    void Draw(int, int);
271         ///
272    void Write(FILE * file);
273         ///
274    void Write(string & file);
275         ///
276    void Metrics();
277  protected:
278         ///
279    int left, right;
280         ///
281    int dw, dh;
282 };
283
284
285 /// Decorations over (below) a math object
286 class MathDecorationInset: public MathParInset {
287  public:
288         ///
289    MathDecorationInset(int, short st = LM_ST_TEXT);
290         ///
291    ~MathDecorationInset() {}
292         ///
293    MathedInset * Clone();
294         ///
295    void Draw(int, int);
296         ///
297    void Write(FILE * file);
298         ///
299    void Write(string & file);
300         ///
301    void Metrics();
302         ///
303    inline bool GetLimits() const;
304  protected:
305         ///
306    int deco;
307         ///
308    bool upper;
309         ///
310    int dw, dh, dy;
311 };
312
313
314 // --------------------  Inline functions ---------------------
315
316  
317 inline
318 MathFuncInset::~MathFuncInset()
319 {
320     if (fname && GetType() == LM_OT_UNDEF) delete[] fname;
321 }
322
323
324 inline
325 bool MathFuncInset::GetLimits() const 
326 {  
327    return bool(lims && (GetStyle() == LM_ST_DISPLAY)); 
328
329
330
331 inline
332 void MathFuncInset::Write(FILE * file)
333 {
334    fprintf(file, "\\%s ", name);
335 }
336
337
338 inline
339 void MathFuncInset::Write(string & file)
340 {
341    file += '\\';
342    file += name;
343    file += ' ';
344 }
345
346
347 inline
348 void MathSpaceInset::Metrics()
349 {
350    width = (space) ? space * 2 : 2;
351    if (space>3) width *= 2;
352    if (space == 5) width *= 2;
353    width += 4;
354    ascent = 4; descent = 0;
355 }
356
357
358 inline
359 void MathSpaceInset::SetSpace(int sp)
360
361    space = sp;
362    Metrics();
363 }    
364
365
366 inline
367 bool MathBigopInset::GetLimits() const 
368 {  
369     // Default case
370     if (lims < 0) {
371         return sym != LM_int && sym != LM_oint && GetStyle() == LM_ST_DISPLAY;
372     } 
373     
374     // Custom 
375     return lims > 0;
376
377
378
379 inline
380 void MathBigopInset::SetLimits(bool ls) 
381 {  
382     lims = ls ? 1 : 0; 
383
384
385
386 inline
387 bool MathDecorationInset::GetLimits() const
388
389    return deco == LM_underbrace || deco == LM_overbrace;
390 }    
391
392 #endif