]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
first go at mathed file cleanup
[lyx.git] / src / mathed / math_fracinset.C
1 #include <config.h>
2
3 #include "math_fracinset.h"
4 #include "math_iter.h"
5 #include "LColor.h"
6 #include "Painter.h"
7 #include "mathed/support.h"
8
9
10 MathFracInset::MathFracInset(short ot)
11         : MathParInset(LM_ST_TEXT, "frac", ot)
12 {
13         
14         den = new MathParInset(LM_ST_TEXT); // this leaks
15         dh = 0;
16         idx = 0;
17         if (objtype == LM_OT_STACKREL) {
18                 flag |= LMPF_SCRIPT;
19                 SetName("stackrel");
20         }
21 }
22
23
24 MathFracInset::~MathFracInset()
25 {
26         delete den;
27 }
28
29
30 MathedInset * MathFracInset::Clone()
31 {   
32         MathFracInset * p = new MathFracInset(GetType());
33         MathedIter itn(array);
34         MathedIter itd(den->GetData());
35         p->SetData(itn.Copy(), itd.Copy());
36         p->idx = idx;
37         p->dh = dh;
38         return p;
39 }
40
41
42 bool MathFracInset::setArgumentIdx(int i)
43 {
44         if (i == 0 || i == 1) {
45                 idx = i;
46                 return true;
47         } else 
48                 return false;
49 }
50
51
52 void MathFracInset::SetStyle(short st)
53 {
54         MathParInset::SetStyle(st);
55         dh = 0;
56         den->SetStyle((size == LM_ST_DISPLAY) ?
57                       static_cast<short>(LM_ST_TEXT)
58                       : size);
59 }
60
61
62 void MathFracInset::SetData(MathedArray * n, MathedArray * d)
63 {
64         den->SetData(d);
65         MathParInset::SetData(n);
66 }
67
68
69 void MathFracInset::SetData(MathedArray * d)
70 {
71         if (idx == 0)
72                 MathParInset::SetData(d);
73         else {
74                 den->SetData(d);
75         }
76 }
77
78
79 void MathFracInset::GetXY(int & x, int & y) const
80 {  
81         if (idx == 0)
82                 MathParInset::GetXY(x, y);
83         else
84                 den->GetXY(x, y);
85 }
86
87
88 MathedArray * MathFracInset::GetData()
89 {
90         if (idx == 0)
91                 return array;
92         else
93                 return den->GetData();
94 }
95
96
97 bool MathFracInset::Inside(int x, int y) 
98 {
99         int xx = xo - (width - w0) / 2;
100         
101         return x >= xx && x <= xx + width && y <= yo + descent && y >= yo - ascent;
102 }
103
104
105 void MathFracInset::SetFocus(int /*x*/, int y)
106 {  
107 //    lyxerr << "y " << y << " " << yo << " " << den->yo << " ";
108         idx = (y > yo) ? 1: 0;
109 }
110
111
112 void
113 MathFracInset::draw(Painter & pain, int x, int y)
114
115         short idxp = idx;
116         short sizex = size;
117         
118         idx = 0;
119         if (size == LM_ST_DISPLAY) ++size;
120         MathParInset::draw(pain, x + (width - w0) / 2, y - des0);
121         den->draw(pain, x + (width - w1) / 2, y + den->Ascent() + 2 - dh);
122         size = sizex;
123         if (objtype == LM_OT_FRAC)
124                 pain.line(x + 2, y - dh, x + width - 4, y - dh, LColor::mathline);
125         idx = idxp;
126 }
127
128
129 void
130 MathFracInset::Metrics()
131 {
132         if (!dh) {
133                 int a, b;
134                 dh = mathed_char_height(LM_TC_CONST, size, 'I', a, b)/2;
135         }
136         short idxp = idx;
137         short sizex = size; 
138         idx = 0;
139         if (size == LM_ST_DISPLAY) ++size; 
140         MathParInset::Metrics();
141         size = sizex;
142         w0 = width;
143         int as = Height() + 2 + dh;
144         des0 = Descent() + 2 + dh;
145         den->Metrics();  
146         w1 = den->Width();   
147         width = ((w0 > w1) ? w0: w1) + 12;
148         ascent = as; 
149         descent = den->Height()+ 2 - dh;
150         idx = idxp;
151 }
152
153
154 void MathFracInset::Write(ostream & os, bool fragile)
155 {
156         os << '\\' << name << '{';
157         MathParInset::Write(os, fragile);
158         os << "}{";
159         den->Write(os, fragile);
160         os << '}';
161 }