]> git.lyx.org Git - lyx.git/blob - src/mathed/math_matrixinset.C
mathed50.diff
[lyx.git] / src / mathed / math_matrixinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_matrixinset.h"
8 #include "math_rowst.h"
9 #include "math_xiter.h"
10 #include "support/LOstream.h"
11
12 using std::ostream;
13
14 extern int number_of_newlines;
15
16 MathMatrixInset::MathMatrixInset(int m, int n, short st)
17         : MathParInset(st, "array", LM_OT_MATRIX), nc_(m), nr_(0), ws_(m),
18           v_align_(0), h_align_(nc_, 'c')
19 {
20         flag = 15;
21         if (n > 0) {
22                 row_.data_ = new MathedRowSt(nc_ + 1);
23                 MathedXIter it(this);
24                 for (int j = 1; j < n; ++j)
25                         it.addRow();
26                 nr_ = n;
27                 if (nr_ == 1 && nc_ > 1) {
28                         for (int j = 0; j < nc_ - 1; ++j) 
29                                 it.insert('T', LM_TC_TAB);
30                 }
31         } else if (n < 0) {
32                 row_.data_ = new MathedRowSt(nc_ + 1);
33                 nr_ = 1;
34         }
35 }
36
37
38 MathedInset * MathMatrixInset::Clone()
39 {
40         return new MathMatrixInset(*this);
41 }
42
43
44 void MathMatrixInset::SetAlign(char vv, string const & hh)
45 {
46         v_align_ = vv;
47         h_align_ = hh.substr(0, nc_); // usr just h_align = hh; perhaps
48 }
49
50
51 // Check the number of tabs and crs
52 void MathMatrixInset::setData(MathedArray const & a)
53 {
54         array = a;
55
56         MathedIter it(&array);
57         int nn = nc_ - 1;
58         nr_ = 1;
59         // count tabs per row
60         while (it.OK()) {
61                 if (it.IsTab()) {
62                         if (nn < 0) { 
63                                 it.Delete();
64                                 continue;
65                         } else {
66                                 // it.Next();
67                                 --nn;
68                         }
69                 }
70                 if (it.IsCR()) {
71                         while (nn > 0) {
72                                 it.insert(' ', LM_TC_TAB);
73                                 --nn;
74                         }
75                         nn = nc_ - 1;
76                         ++nr_;
77                 }
78                 it.Next();
79         }
80         it.Reset();
81         
82         // Automatically inserts tabs around bops
83         // DISABLED because it's very easy to insert tabs 
84 }
85
86
87 void MathMatrixInset::draw(Painter & pain, int x, int baseline)
88 {
89         MathParInset::draw(pain, x, baseline);
90 }
91
92
93
94 void MathMatrixInset::Metrics()
95 {
96         if (row_.empty()) {
97                 // lyxerr << " MIDA ";
98                 MathedXIter it(this);
99                 row_.data_ = it.adjustVerticalSt();
100         } 
101         
102         // Clean the arrays      
103         for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
104                 for (int i = 0; i <= nc_; ++i)
105                         it->setTab(i, 0);
106         
107         // Basic metrics
108         MathParInset::Metrics();
109
110         MathedRowContainer::iterator cxrow = row_.begin();
111         if (nc_ <= 1 && cxrow.is_last()) {
112                 cxrow->ascent(ascent);
113                 cxrow->descent(descent);
114         }
115         
116         // Vertical positions of each row
117         MathedRowContainer::iterator cprow = cxrow;
118         int h = 0;
119         for ( ; cxrow; ++cxrow) {
120                 for (int i = 0; i < nc_; ++i) {
121                         if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
122                                 ws_[i] = cxrow->getTab(i);
123                         if (cxrow.is_last() && ws_[i] == 0)
124                                 ws_[i] = df_width;
125                 }
126                 
127                 cxrow->setBaseline((cxrow == row_.begin()) ?
128                                    cxrow->ascent() :
129                                    cxrow->ascent() + cprow->descent()
130                                    + MATH_ROWSEP + cprow->getBaseline());
131                 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;  
132                 cprow = cxrow;
133         }
134         
135         int const hl = Descent();
136         h -= MATH_ROWSEP;
137         
138         //  Compute vertical align
139         switch (v_align_) {
140         case 't':
141                 ascent = row_.begin()->getBaseline();
142                 break;
143         case 'b':
144                 ascent = h - hl;
145                 break;
146         default:
147                 ascent = (row_.begin().is_last()) ? h / 2 : h - hl;
148                 break;
149         }
150         descent = h - ascent + 2;
151         
152         // Increase ws_[i] for 'R' columns (except the first one)
153         for (int i = 1; i < nc_; ++i)
154                 if (h_align_[i] == 'R')
155                         ws_[i] += 10 * df_width;
156         // Increase ws_[i] for 'C' column
157         if (h_align_[0] == 'C')
158                 if (ws_[0] < 7 * workWidth / 8)
159                         ws_[0] = 7 * workWidth / 8;
160         
161         // Adjust local tabs
162         width = MATH_COLSEP;
163         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
164                 int rg = MATH_COLSEP;
165                 int lf = 0;
166                 for (int i = 0; i < nc_; ++i) {
167                         bool isvoid = false;
168                         if (cxrow->getTab(i) <= 0) {
169                                 cxrow->setTab(i, df_width);
170                                 isvoid = true;
171                         }
172                         switch (h_align_[i]) {
173                         case 'l':
174                                 lf = 0;
175                                 break;
176                         case 'c':
177                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
178                                 break;
179                         case 'r':
180                         case 'R':
181                                 lf = ws_[i] - cxrow->getTab(i);
182                                 break;
183                         case 'C':
184                                 if (cxrow == row_.begin())
185                                         lf = 0;
186                                 else if (cxrow.is_last())
187                                         lf = ws_[i] - cxrow->getTab(i);
188                                 else
189                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
190                                 break;
191                         }
192                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
193                         cxrow->setTab(i, lf + rg);
194                         rg = ws_[i] - ww + MATH_COLSEP;
195                         if (cxrow == row_.begin())
196                                 width += ws_[i] + MATH_COLSEP;
197                 }
198                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
199         }
200 }
201
202
203 void MathMatrixInset::Write(ostream & os, bool fragile)
204 {
205         if (GetType() == LM_OT_MATRIX) {
206                 if (fragile)
207                         os << "\\protect";
208                 os << "\\begin{"
209                    << name
210                    << '}';
211                 if (v_align_ == 't' || v_align_ == 'b') {
212                         os << '['
213                            << char(v_align_)
214                            << ']';
215                 }
216                 os << '{'
217                    << h_align_
218                    << "}\n";
219                 ++number_of_newlines;
220         }
221         MathParInset::Write(os, fragile);
222         if (GetType() == LM_OT_MATRIX){
223                 os << "\n";
224                 if (fragile)
225                         os << "\\protect";
226                 os << "\\end{"
227                    << name
228                    << '}';
229                 ++number_of_newlines;
230         }
231 }