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