]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
mathed32.diff
[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(*this);
36         // this cast will go again...
37         p->den_ = static_cast<MathParInset*>(p->den_->Clone());
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
102                 && x <= xx + width
103                 && y <= yo() + descent
104                 && y >= yo() - ascent;
105 }
106
107
108 void MathFracInset::SetFocus(int /*x*/, int y)
109 {  
110         // lyxerr << "y " << y << " " << yo << " " << den_->yo << " ";
111         idx_ = (y > yo()) ? 1 : 0;
112 }
113
114
115 void
116 MathFracInset::draw(Painter & pain, int x, int y)
117
118         int const idxp = idx_;
119         int const sizex = size();
120         
121         idx_ = 0;
122         if (size() == LM_ST_DISPLAY) incSize();
123         MathParInset::draw(pain, x + (width - w0_) / 2, y - des0_);
124         den_->draw(pain, x + (width - w1_) / 2, y + den_->Ascent() + 2 - dh_);
125         size(sizex);
126         if (objtype == LM_OT_FRAC)
127                 pain.line(x + 2, y - dh_,
128                           x + width - 4, y - dh_, LColor::mathline);
129         idx_ = idxp;
130 }
131
132
133 void
134 MathFracInset::Metrics()
135 {
136         if (!dh_) {
137                 int a;
138                 int b;
139                 dh_ = mathed_char_height(LM_TC_CONST, size(), 'I', a, b) / 2;
140         }
141         int const idxp = idx_;
142         int const sizex = size();
143         idx_ = 0;
144         if (size() == LM_ST_DISPLAY) incSize(); 
145         MathParInset::Metrics();
146         size(sizex);
147         w0_ = width;
148         int const as = Height() + 2 + dh_;
149         des0_ = Descent() + 2 + dh_;
150         den_->Metrics();  
151         w1_ = den_->Width();   
152         width = ((w0_ > w1_) ? w0_: w1_) + 12;
153         ascent = as; 
154         descent = den_->Height()+ 2 - dh_;
155         idx_ = idxp;
156 }
157
158
159 void MathFracInset::Write(ostream & os, bool fragile)
160 {
161         os << '\\' << name << '{';
162         MathParInset::Write(os, fragile);
163         os << "}{";
164         den_->Write(os, fragile);
165         os << '}';
166 }