]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
fix the matrix reading other 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           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
45 void MathFracInset::SetStyle(short st)
46 {
47         MathParInset::SetStyle(st);
48         dh_ = 0;
49         den_.SetStyle((size() == LM_ST_DISPLAY) ?
50                       static_cast<short>(LM_ST_TEXT)
51                       : size());
52 }
53
54
55 void MathFracInset::SetData(MathedArray const & n, MathedArray const & d)
56 {
57         den_.setData(d);
58         MathParInset::setData(n);
59 }
60
61
62 void MathFracInset::setData(MathedArray const & d)
63 {
64         if (idx_ == 0)
65                 MathParInset::setData(d);
66         else {
67                 den_.setData(d);
68         }
69 }
70
71
72 void MathFracInset::GetXY(int & x, int & y) const
73 {  
74         if (idx_ == 0)
75                 MathParInset::GetXY(x, y);
76         else
77                 den_.GetXY(x, y);
78 }
79
80
81 MathedArray & MathFracInset::GetData()
82 {
83         if (idx_ == 0)
84                 return array;
85         else
86                 return den_.GetData();
87 }
88
89
90 MathedArray const & MathFracInset::GetData() const
91 {
92         if (idx_ == 0)
93                 return array;
94         else
95                 return den_.GetData();
96 }
97
98
99 bool MathFracInset::Inside(int x, int y) 
100 {
101         int const xx = xo() - (width - w0_) / 2;
102         
103         return x >= xx
104                 && x <= xx + width
105                 && y <= yo() + descent
106                 && y >= yo() - ascent;
107 }
108
109
110 void MathFracInset::SetFocus(int /*x*/, int y)
111 {  
112         // lyxerr << "y " << y << " " << yo << " " << den_->yo << " ";
113         idx_ = (y > yo()) ? 1 : 0;
114 }
115
116
117 void
118 MathFracInset::draw(Painter & pain, int x, int y)
119
120         int const idxp = idx_;
121         int const sizex = size();
122         
123         idx_ = 0;
124         if (size() == LM_ST_DISPLAY) incSize();
125         MathParInset::draw(pain, x + (width - w0_) / 2, y - des0_);
126         den_.draw(pain, x + (width - w1_) / 2, y + den_.Ascent() + 2 - dh_);
127         size(sizex);
128         if (objtype == LM_OT_FRAC)
129                 pain.line(x + 2, y - dh_,
130                           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;
140                 int b;
141                 dh_ = mathed_char_height(LM_TC_CONST, size(), 'I', a, b) / 2;
142         }
143         int const idxp = idx_;
144         int const sizex = size();
145         idx_ = 0;
146         if (size() == LM_ST_DISPLAY) incSize(); 
147         MathParInset::Metrics();
148         size(sizex);
149         w0_ = width;
150         int const as = Height() + 2 + dh_;
151         des0_ = Descent() + 2 + dh_;
152         den_.Metrics();  
153         w1_ = den_.Width();   
154         width = ((w0_ > w1_) ? w0_: w1_) + 12;
155         ascent = as; 
156         descent = den_.Height()+ 2 - dh_;
157         idx_ = idxp;
158 }
159
160
161 void MathFracInset::Write(ostream & os, bool fragile)
162 {
163         os << '\\' << name << '{';
164         MathParInset::Write(os, fragile);
165         os << "}{";
166         den_.Write(os, fragile);
167         os << '}';
168 }