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