]> git.lyx.org Git - features.git/blob - src/mathed/math_matrixinset.C
mathed57.diff
[features.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                 MathedXIter it(this);
23                 for (int j = 1; j < n; ++j)
24                         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                 MathedXIter it(this);
32                 it.addRow();
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         // Adjust row structure
97         MathedXIter it(this);
98         it.GoBegin();
99         int nrows = 1;
100         while (it.OK()) {
101                 if (it.IsCR()) {
102                         ++nrows;
103                         if (it.col >= it.ncols)
104                                 it.ncols = it.col + 1; 
105                 }   
106                 it.Next();      
107         }
108         row_.data_.resize(nrows);
109         
110         // Clean the arrays      
111         for (MathedRowContainer::iterator it = row_.begin(); it; ++it)
112                 for (int i = 0; i <= nc_; ++i)
113                         it->setTab(i, 0);
114         
115         // Basic metrics
116         MathParInset::Metrics();
117
118         MathedRowContainer::iterator cxrow = row_.begin();
119         if (nc_ <= 1 && cxrow.is_last()) {
120                 cxrow->ascent(ascent);
121                 cxrow->descent(descent);
122         }
123         
124         // Vertical positions of each row
125         MathedRowContainer::iterator cprow = cxrow;
126         int h = 0;
127         for ( ; cxrow; ++cxrow) {
128                 for (int i = 0; i < nc_; ++i) {
129                         if (cxrow == row_.begin() || ws_[i] < cxrow->getTab(i))
130                                 ws_[i] = cxrow->getTab(i);
131                         if (cxrow.is_last() && ws_[i] == 0)
132                                 ws_[i] = df_width;
133                 }
134                 
135                 cxrow->setBaseline((cxrow == row_.begin()) ?
136                                    cxrow->ascent() :
137                                    cxrow->ascent() + cprow->descent()
138                                    + MATH_ROWSEP + cprow->getBaseline());
139                 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;  
140                 cprow = cxrow;
141         }
142         
143         int const hl = Descent();
144         h -= MATH_ROWSEP;
145         
146         //  Compute vertical align
147         switch (v_align_) {
148         case 't':
149                 ascent = row_.begin()->getBaseline();
150                 break;
151         case 'b':
152                 ascent = h - hl;
153                 break;
154         default:
155                 ascent = (row_.begin().is_last()) ? h / 2 : h - hl;
156                 break;
157         }
158         descent = h - ascent + 2;
159         
160         // Increase ws_[i] for 'R' columns (except the first one)
161         for (int i = 1; i < nc_; ++i)
162                 if (h_align_[i] == 'R')
163                         ws_[i] += 10 * df_width;
164         // Increase ws_[i] for 'C' column
165         if (h_align_[0] == 'C')
166                 if (ws_[0] < 7 * workWidth / 8)
167                         ws_[0] = 7 * workWidth / 8;
168         
169         // Adjust local tabs
170         width = MATH_COLSEP;
171         for (cxrow = row_.begin(); cxrow; ++cxrow) {   
172                 int rg = MATH_COLSEP;
173                 int lf = 0;
174                 for (int i = 0; i < nc_; ++i) {
175                         bool isvoid = false;
176                         if (cxrow->getTab(i) <= 0) {
177                                 cxrow->setTab(i, df_width);
178                                 isvoid = true;
179                         }
180                         switch (h_align_[i]) {
181                         case 'l':
182                                 lf = 0;
183                                 break;
184                         case 'c':
185                                 lf = (ws_[i] - cxrow->getTab(i))/2; 
186                                 break;
187                         case 'r':
188                         case 'R':
189                                 lf = ws_[i] - cxrow->getTab(i);
190                                 break;
191                         case 'C':
192                                 if (cxrow == row_.begin())
193                                         lf = 0;
194                                 else if (cxrow.is_last())
195                                         lf = ws_[i] - cxrow->getTab(i);
196                                 else
197                                         lf = (ws_[i] - cxrow->getTab(i))/2; 
198                                 break;
199                         }
200                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
201                         cxrow->setTab(i, lf + rg);
202                         rg = ws_[i] - ww + MATH_COLSEP;
203                         if (cxrow == row_.begin())
204                                 width += ws_[i] + MATH_COLSEP;
205                 }
206                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
207         }
208 }
209
210
211 void MathMatrixInset::Write(ostream & os, bool fragile)
212 {
213         if (GetType() == LM_OT_MATRIX) {
214                 if (fragile)
215                         os << "\\protect";
216                 os << "\\begin{"
217                    << name
218                    << '}';
219                 if (v_align_ == 't' || v_align_ == 'b') {
220                         os << '['
221                            << char(v_align_)
222                            << ']';
223                 }
224                 os << '{'
225                    << h_align_
226                    << "}\n";
227                 ++number_of_newlines;
228         }
229         MathParInset::Write(os, fragile);
230         if (GetType() == LM_OT_MATRIX){
231                 os << "\n";
232                 if (fragile)
233                         os << "\\protect";
234                 os << "\\end{"
235                    << name
236                    << '}';
237                 ++number_of_newlines;
238         }
239 }