]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
small cleanup, doxygen, formatting changes
[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         int const idxp = idx_;
122         int const 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_,
131                           x + width - 4, y - dh_, LColor::mathline);
132         idx_ = idxp;
133 }
134
135
136 void
137 MathFracInset::Metrics()
138 {
139         if (!dh_) {
140                 int a;
141                 int b;
142                 dh_ = mathed_char_height(LM_TC_CONST, size(), 'I', a, b) / 2;
143         }
144         int const idxp = idx_;
145         int const sizex = size();
146         idx_ = 0;
147         if (size() == LM_ST_DISPLAY) incSize(); 
148         MathParInset::Metrics();
149         size(sizex);
150         w0_ = width;
151         int const as = Height() + 2 + dh_;
152         des0_ = Descent() + 2 + dh_;
153         den_->Metrics();  
154         w1_ = den_->Width();   
155         width = ((w0_ > w1_) ? w0_: w1_) + 12;
156         ascent = as; 
157         descent = den_->Height()+ 2 - dh_;
158         idx_ = idxp;
159 }
160
161
162 void MathFracInset::Write(ostream & os, bool fragile)
163 {
164         os << '\\' << name << '{';
165         MathParInset::Write(os, fragile);
166         os << "}{";
167         den_->Write(os, fragile);
168         os << '}';
169 }