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