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