]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
mathed65.diff
[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           idx_(0), den_(LM_ST_TEXT), dh_(0)
20 {
21         if (objtype == LM_OT_STACKREL) {
22                 flag |= LMPF_SCRIPT;
23                 SetName("stackrel");
24         }
25 }
26
27
28 MathedInset * MathFracInset::Clone()
29 {   
30         MathFracInset * p = new MathFracInset(*this);
31         return p;
32 }
33
34
35 bool MathFracInset::setArgumentIdx(int i)
36 {
37         if (i == 0 || i == 1) {
38                 idx_ = i;
39                 return true;
40         } else 
41                 return false;
42 }
43
44 int MathFracInset::getArgumentIdx() const
45 {
46   return idx_;
47 }
48
49
50 int MathFracInset::getMaxArgumentIdx() const
51 {
52   return 1;
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 const & n, MathedArray const & d)
66 {
67         den_.setData(d);
68         MathParInset::setData(n);
69 }
70
71
72 void MathFracInset::setData(MathedArray const & 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 MathedArray const & MathFracInset::GetData() const
101 {
102         if (idx_ == 0)
103                 return array;
104         else
105                 return den_.GetData();
106 }
107
108
109 bool MathFracInset::Inside(int x, int y) 
110 {
111         int const xx = xo() - (width - w0_) / 2;
112         
113         return x >= xx
114                 && x <= xx + width
115                 && y <= yo() + descent
116                 && y >= yo() - ascent;
117 }
118
119
120 void MathFracInset::SetFocus(int /*x*/, int y)
121 {  
122         // lyxerr << "y " << y << " " << yo << " " << den_->yo << " ";
123         idx_ = (y > yo()) ? 1 : 0;
124 }
125
126
127 void MathFracInset::draw(Painter & pain, int x, int y)
128
129         int const idxp = idx_;
130         int const sizex = size();
131         
132         idx_ = 0;
133         if (size() == LM_ST_DISPLAY) incSize();
134         MathParInset::draw(pain, x + (width - w0_) / 2, y - des0_);
135         den_.draw(pain, x + (width - w1_) / 2, y + den_.Ascent() + 2 - dh_);
136         size(sizex);
137         if (objtype == LM_OT_FRAC)
138                 pain.line(x + 2, y - dh_,
139                           x + width - 4, y - dh_, LColor::mathline);
140         idx_ = idxp;
141 }
142
143
144 void MathFracInset::Metrics()
145 {
146         if (!dh_) {
147                 int a;
148                 int b;
149                 dh_ = mathed_char_height(LM_TC_CONST, size(), 'I', a, b) / 2;
150         }
151         int const idxp = idx_;
152         int const sizex = size();
153         idx_ = 0;
154         if (size() == LM_ST_DISPLAY) incSize(); 
155         MathParInset::Metrics();
156         size(sizex);
157         w0_ = width;
158         int const as = Height() + 2 + dh_;
159         des0_ = Descent() + 2 + dh_;
160         den_.Metrics();  
161         w1_ = den_.Width();   
162         width = ((w0_ > w1_) ? w0_: w1_) + 12;
163         ascent = as; 
164         descent = den_.Height()+ 2 - dh_;
165         idx_ = idxp;
166 }
167
168
169 void MathFracInset::Write(ostream & os, bool fragile)
170 {
171         os << '\\' << name << '{';
172         MathParInset::Write(os, fragile);
173         os << "}{";
174         den_.Write(os, fragile);
175         os << '}';
176 }
177
178 void MathFracInset::WriteNormal(ostream & os)
179 {
180         os << '{' << name << ' ';
181         MathParInset::WriteNormal(os);
182         os << " ";
183         den_.WriteNormal(os);
184         os << "} ";
185 }