]> git.lyx.org Git - lyx.git/blob - src/tabular.C
3d68635b218d858c59564110881b35e661ed2bd8
[lyx.git] / src / tabular.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000 The LyX Team.
7  *
8  *           @author: Jürgen Vigna
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <algorithm>
20 #include <cstdlib>
21
22 #include "tabular.h"
23 #include "debug.h"
24 #include "vspace.h"
25 #include "layout.h"
26 #include "lyx_gui_misc.h"
27 #include "buffer.h"
28 #include "BufferView.h"
29 #include "Painter.h"
30 #include "LaTeXFeatures.h"
31 #include "support/lstrings.h"
32 #include "support/lyxmanip.h"
33 #include "insets/insettabular.h"
34 #include "insets/insettext.h"
35 #include "gettext.h"
36
37 using std::ostream;
38 using std::istream;
39 using std::getline;
40 using std::max;
41 using std::endl;
42 using std::vector;
43
44 namespace {
45
46         int const WIDTH_OF_LINE = 5;
47
48 } // namespace
49
50 /// Define a few methods for the inner structs
51
52 LyXTabular::cellstruct::cellstruct() 
53 {
54         cellno = 0;
55         width_of_cell = 0;
56         multicolumn = LyXTabular::CELL_NORMAL;
57         alignment = LYX_ALIGN_CENTER;
58         valignment = LYX_VALIGN_TOP;
59         top_line = true;
60         bottom_line = false;
61         left_line = true;
62         right_line = false;
63         usebox = BOX_NONE;
64         rotate = false;
65 }
66
67
68 LyXTabular::rowstruct::rowstruct() 
69 {
70         top_line = true;
71         bottom_line = false;
72         ascent_of_row = 0;
73         descent_of_row = 0;
74         newpage = false;
75 }
76
77
78 LyXTabular::columnstruct::columnstruct() 
79 {
80         left_line = true;
81         right_line = false;
82         alignment = LYX_ALIGN_CENTER;
83         valignment = LYX_VALIGN_TOP;
84         width_of_column = 0;
85 }
86
87
88 /* konstruktor */
89 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
90 {
91         owner_ = inset;
92         Init(rows_arg, columns_arg);
93 }
94
95
96 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
97 {
98         owner_ = inset;
99         Init(lt.rows_, lt.columns_, &lt);
100 #if 0
101 #ifdef WITH_WARNINGS
102 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
103 #endif
104         operator=(lt);
105 #endif
106 }
107
108
109 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
110 {
111         owner_ = inset;
112         Read(buf, lex);
113 }
114
115
116 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
117 {
118         // If this and lt is not of the same size we have a serious bug
119         // So then it is ok to throw an exception, or for now
120         // call abort()
121         lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
122
123         cell_info = lt.cell_info;
124         row_info = lt.row_info;
125         column_info = lt.column_info;
126
127         // long tabular stuff
128         SetLongTabular(lt.is_long_tabular);
129         endhead = lt.endhead;
130         endfoot = lt.endfoot;
131         endfirsthead = lt.endfirsthead;
132         endlastfoot = lt.endlastfoot;
133
134         rotate = lt.rotate;
135
136         Reinit();
137         
138         return *this;
139 }
140
141
142 LyXTabular * LyXTabular::Clone(InsetTabular * inset)
143 {
144         LyXTabular * result = new LyXTabular(inset, *this);
145 #if 0
146         // don't know if this is good but I need to Clone also
147         // the text-insets here, this is for the Undo-facility!
148         for (int i = 0; i < rows_; ++i) {
149                 for (int j = 0; j < columns_; ++j) {
150                         result->cell_info[i][j].inset = cell_info[i][j].inset;
151                         result->cell_info[i][j].inset.setOwner(inset);
152                 }
153         }
154 #endif
155         return result;
156 }
157
158
159 /* activates all lines and sets all widths to 0 */ 
160 void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
161 {
162         rows_ = rows_arg;
163         columns_ = columns_arg;
164         row_info = row_vector(rows_, rowstruct());
165         column_info = column_vector(columns_, columnstruct());
166         cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
167
168         if (lt) {
169                 operator=(*lt);
170                 return;
171         }
172
173         int cellno = 0;
174         for (int i = 0; i < rows_; ++i) {
175                 for (int j = 0; j < columns_; ++j) {
176                         cell_info[i][j].inset.setOwner(owner_);
177                         cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
178                         cell_info[i][j].cellno = cellno++;
179                 }
180                 cell_info[i].back().right_line = true;
181         }
182         row_info.back().bottom_line = true;
183         row_info.front().bottom_line = true;
184
185         for (int i = 0; i < columns_; ++i) {
186                 calculate_width_of_column(i);
187         }
188         column_info.back().right_line = true;
189    
190         calculate_width_of_tabular();
191
192         rowofcell = vector<int>();
193         columnofcell = vector<int>();
194         set_row_column_number_info();
195         is_long_tabular = false;
196         rotate = false;
197         endhead = 0;
198         endfirsthead = 0;
199         endfoot = 0;
200         endlastfoot = 0;
201 }
202
203
204 void LyXTabular::AppendRow(int cell)
205 {
206         ++rows_;
207    
208         int row = row_of_cell(cell);
209
210         row_vector::iterator rit = row_info.begin() + row;
211         row_info.insert(rit, rowstruct());
212         // now set the values of the row before
213         row_info[row] = row_info[row+1];
214
215 #if 0
216         cell_vvector::iterator cit = cell_info.begin() + row;
217         cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
218 #else
219         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
220                                                                                                                   cellstruct()));
221
222         for (int i = 0; i <= row; ++i) {
223                 for (int j = 0; j < columns_; ++j) {
224                         c_info[i][j] = cell_info[i][j];
225                 }
226         }
227         for (int i = row + 1; i < rows_; ++i) {
228                 for (int j = 0; j < columns_; ++j) {
229                         c_info[i][j] = cell_info[i-1][j];
230                 }
231         }
232         cell_info = c_info;
233         ++row;
234         for (int j = 0; j < columns_; ++j) {
235                 cell_info[row][j].inset.clear();
236         }
237 #endif
238         Reinit();
239 }
240
241
242 void LyXTabular::DeleteRow(int row)
243 {
244         // Why make it so hard? (Lgb)
245         //if (!(rows_ - 1))
246         //return;
247         if (rows_ == 1) return; // Not allowed to delete last row
248         
249         row_info.erase(row_info.begin() + row); //&row_info[row]);
250         cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
251         --rows_;
252         Reinit();
253 }
254
255
256 void LyXTabular::AppendColumn(int cell)
257 {
258         ++columns_;
259    
260         cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
261                                                                                                                   cellstruct()));
262         int const column = column_of_cell(cell);
263         column_vector::iterator cit = column_info.begin() + column + 1;
264         column_info.insert(cit, columnstruct());
265         // set the column values of the column before
266         column_info[column+1] = column_info[column];
267
268         for (int i = 0; i < rows_; ++i) {
269                 for (int j = 0; j <= column; ++j) {
270                         c_info[i][j] = cell_info[i][j];
271                 }
272                 for (int j = column + 1; j < columns_; ++j) {
273                         c_info[i][j] = cell_info[i][j - 1];
274                 }
275                 // care about multicolumns
276                 if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
277                         cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
278                 }
279                 if ((column + 1) == columns_ ||
280                         cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
281                         cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
282                 }
283         }
284         cell_info = c_info;
285         //++column;
286         for (int i = 0; i < rows_; ++i) {
287                 //cell_info[i][column].inset.clear();
288                 cell_info[i][column + 1].inset.clear();
289         }
290         Reinit();
291 }
292
293
294 void LyXTabular::DeleteColumn(int column)
295 {
296         // Similar to DeleteRow
297         //if (!(columns_ - 1))
298         //return;
299         if (columns_ == 1) return; // Not allowed to delete last column
300          
301         column_info.erase(column_info.begin() + column);
302         for (int i = 0; i < rows_; ++i) {
303                 cell_info[i].erase(cell_info[i].begin() + column);
304         }
305         --columns_;
306         Reinit();
307 }
308
309
310 void LyXTabular::Reinit()
311 {   
312         for (int i = 0; i < rows_; ++i) {
313                 for (int j = 0; j < columns_; ++j) {
314                         cell_info[i][j].width_of_cell = 0;
315                         cell_info[i][j].inset.setOwner(owner_);
316                 }
317         }
318   
319         for (int i = 0; i < columns_; ++i) {
320                 calculate_width_of_column(i);
321         }
322         calculate_width_of_tabular();
323
324         set_row_column_number_info();
325 }
326
327
328 void LyXTabular::set_row_column_number_info(bool oldformat)
329 {
330         numberofcells = -1;
331         for (int row = 0; row < rows_; ++row) {
332                 for (int column = 0; column<columns_; ++column) {
333                         if (cell_info[row][column].multicolumn
334                                 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
335                                 ++numberofcells;
336                         cell_info[row][column].cellno = numberofcells;
337                 }
338         }
339         ++numberofcells; // because this is one more than as we start from 0
340
341         rowofcell.resize(numberofcells);
342         columnofcell.resize(numberofcells);
343
344         for (int row = 0, column = 0, c = 0;
345                  c < numberofcells && row < rows_ && column < columns_;) {
346                 rowofcell[c] = row;
347                 columnofcell[c] = column;
348                 ++c;
349                 do {
350                         ++column;
351                 } while (column < columns_ &&
352                                  cell_info[row][column].multicolumn
353                                  == LyXTabular::CELL_PART_OF_MULTICOLUMN);
354                 if (column == columns_) {
355                         column = 0;
356                         ++row;
357                 }
358         }
359
360         for (int row = 0; row < rows_; ++row) {
361                 for (int column = 0; column < columns_; ++column) {
362                         if (IsPartOfMultiColumn(row,column))
363                                 continue;
364                         // now set the right line of multicolumns right for oldformat read
365                         if (oldformat &&
366                                 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
367                         {
368                                 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
369                                 cell_info[row][column].right_line =
370                                         cell_info[row][column+cn-1].right_line;
371                         }
372                         cell_info[row][column].inset.SetAutoBreakRows(
373                                 !GetPWidth(GetCellNumber(row, column)).empty());
374                 }
375         }
376 }
377
378
379 int LyXTabular::GetNumberOfCells() const
380 {
381         return numberofcells;
382 }
383
384
385 int LyXTabular::NumberOfCellsInRow(int cell) const
386 {
387         int const row = row_of_cell(cell);
388         int result = 0;
389         for (int i = 0; i < columns_; ++i) {
390                 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
391                         ++result;
392         }
393         return result;
394 }
395
396
397 /* returns 1 if there is a topline, returns 0 if not */ 
398 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
399 {
400         int const row = row_of_cell(cell);
401         
402         if (!onlycolumn && IsMultiColumn(cell))
403                 return cellinfo_of_cell(cell)->top_line;
404         return row_info[row].top_line;
405 }
406
407
408 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
409 {
410         // no bottom line underneath non-existent cells if you please
411         // Isn't that a programming error? Is so this should
412         // be an Assert instead. (Lgb)
413         if (cell >= numberofcells)
414                 return false;
415
416         if (!onlycolumn && IsMultiColumn(cell))
417                 return cellinfo_of_cell(cell)->bottom_line;
418         return row_info[row_of_cell(cell)].bottom_line;
419 }
420
421
422 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
423 {
424         if (!onlycolumn && IsMultiColumn(cell))
425                 return cellinfo_of_cell(cell)->left_line;
426         return column_info[column_of_cell(cell)].left_line;
427 }
428
429
430 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
431 {
432         if (!onlycolumn && IsMultiColumn(cell))
433                 return cellinfo_of_cell(cell)->right_line;
434         return column_info[right_column_of_cell(cell)].right_line;
435 }
436
437
438 bool LyXTabular::TopAlreadyDrawed(int cell) const
439 {
440         int row = row_of_cell(cell);
441         if ((row > 0) && !GetAdditionalHeight(row)) {
442                 int column = column_of_cell(cell);
443                 --row;
444                 while (column
445                            && cell_info[row][column].multicolumn
446                            == LyXTabular::CELL_PART_OF_MULTICOLUMN)
447                         --column;
448                 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
449                         return row_info[row].bottom_line;
450                 else
451                         return cell_info[row][column].bottom_line;
452         }
453         return false;
454 }
455
456
457 bool LyXTabular::LeftAlreadyDrawed(int cell) const
458 {
459         int column = column_of_cell(cell);
460         if (column > 0) {
461                 int row = row_of_cell(cell);
462                 while (--column &&
463                            (cell_info[row][column].multicolumn ==
464                                 LyXTabular::CELL_PART_OF_MULTICOLUMN));
465                 if (GetAdditionalWidth(cell_info[row][column].cellno))
466                         return false;
467                 return column_info[column].right_line;
468         }
469         return false;
470 }
471
472
473 bool LyXTabular::IsLastRow(int cell) const
474 {
475         return (row_of_cell(cell) == rows_ - 1);
476 }
477
478
479 int LyXTabular::GetAdditionalHeight(int row) const
480 {
481         if (!row || row >= rows_)
482                 return 0;
483
484         bool top = true;
485         bool bottom = true;
486
487         for (int column = 0; column < columns_ - 1 && bottom; ++column) {
488                 switch (cell_info[row - 1][column].multicolumn) {
489                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
490                         bottom = cell_info[row - 1][column].bottom_line;
491                         break;
492                 case LyXTabular::CELL_NORMAL:
493                         bottom = row_info[row - 1].bottom_line;
494                 }
495         }
496         for (int column = 0; column < columns_ - 1 && top; ++column) {
497                 switch (cell_info[row][column].multicolumn){
498                 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
499                         top = cell_info[row][column].top_line;
500                         break;
501                 case LyXTabular::CELL_NORMAL:
502                         top = row_info[row].top_line;
503                 }
504         }
505         if (top && bottom)
506                 return WIDTH_OF_LINE;
507         return 0;
508 }
509
510
511 int LyXTabular::GetAdditionalWidth(int cell) const
512 {
513         // internally already set in SetWidthOfCell
514         // used to get it back in text.C
515         int const col = right_column_of_cell(cell);
516         if (col < columns_ - 1 && column_info[col].right_line &&
517                 column_info[col+1].left_line)
518                 return WIDTH_OF_LINE;
519         else
520                 return 0;
521 }
522
523
524 // returns the maximum over all rows 
525 int LyXTabular::GetWidthOfColumn(int cell) const
526 {
527         int const column1 = column_of_cell(cell);
528         int const column2 = right_column_of_cell(cell);
529         int result = 0;
530         for (int i = column1; i <= column2; ++i) {
531                 result += column_info[i].width_of_column;
532         }
533         return result;
534 }
535
536
537 int LyXTabular::GetWidthOfTabular() const
538 {
539         return width_of_tabular;
540 }
541
542
543 /* returns 1 if a complete update is necessary, otherwise 0 */ 
544 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
545 {
546         if (!IsMultiColumn(cell))
547                 return false;
548         
549         int const row = row_of_cell(cell);
550         int const column1 = column_of_cell(cell);
551         int const column2 = right_column_of_cell(cell);
552
553         // first set columns to 0 so we can calculate the right width
554         for (int i = column1; i <= column2; ++i) {
555                 cell_info[row][i].width_of_cell = 0;
556         }
557         // set the width to MAX_WIDTH until width > 0
558         int width = (new_width + 2 * WIDTH_OF_LINE);
559
560         int i = column1;
561         for (; i < column2 && width > column_info[i].width_of_column; ++i) {
562                 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
563                 width -= column_info[i].width_of_column;
564         }
565         if (width > 0) {
566                 cell_info[row][i].width_of_cell = width;
567         }
568         return true;
569 }
570
571
572 void LyXTabular::recalculateMulticolCells(int cell, int new_width)
573 {
574         int const row = row_of_cell(cell);
575         int const column1 = column_of_cell(cell);
576         int const column2 = right_column_of_cell(cell);
577
578         // first set columns to 0 so we can calculate the right width
579         int i = column1;
580         for (; i <= column2; ++i)
581                 cell_info[row][i].width_of_cell = 0;
582         for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
583                 ;
584         if (i < numberofcells)
585                 recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
586         SetWidthOfMulticolCell(cell, new_width);
587 }
588
589
590 /* returns 1 if a complete update is necessary, otherwise 0 */ 
591 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
592 {
593         int const row = row_of_cell(cell);
594         int const column1 = column_of_cell(cell);
595         bool tmp = false;
596         int width = 0;
597
598         if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE))
599                 return false;
600         if (IsMultiColumn(cell, true)) {
601                 tmp = SetWidthOfMulticolCell(cell, new_width);
602         } else {
603                 width = (new_width + 2*WIDTH_OF_LINE);
604                 cell_info[row][column1].width_of_cell = width;
605                 if (column_info[column1].right_line && (column1 < columns_-1) &&
606                         column_info[column1+1].left_line) // additional width
607                         cell_info[row][column1].width_of_cell += WIDTH_OF_LINE;
608                 tmp = calculate_width_of_column_NMC(column1);
609         }
610         if (tmp) {
611                 int i = 0;
612                 for (; i<columns_; ++i)
613                         calculate_width_of_column_NMC(i);
614                 for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
615                         ;
616                 if (i < numberofcells)
617                         recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
618                 for (i = 0; i < columns_; ++i)
619                         calculate_width_of_column(i);
620                 calculate_width_of_tabular();
621                 return true;
622         }
623         return false;
624 }
625
626
627 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
628 {
629         if (!IsMultiColumn(cell) || onlycolumn)
630                 column_info[column_of_cell(cell)].alignment = align;
631         if (!onlycolumn)
632                 cellinfo_of_cell(cell)->alignment = align;
633         return true;
634 }
635
636
637 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
638 {
639         if (!IsMultiColumn(cell) || onlycolumn)
640                 column_info[column_of_cell(cell)].valignment = align;
641         if (!onlycolumn)
642                 cellinfo_of_cell(cell)->valignment = align;
643         return true;
644 }
645
646
647 bool LyXTabular::SetColumnPWidth(int cell, string const & width)
648 {
649         bool flag = !width.empty();
650         int const j = column_of_cell(cell);
651
652         column_info[j].p_width = width;
653         if (flag) // do this only if there is a width
654                 SetAlignment(cell, LYX_ALIGN_LEFT);
655         for (int i = 0; i < rows_; ++i) {
656                 int c = GetCellNumber(i, j);
657                 flag = !GetPWidth(c).empty(); // because of multicolumns!
658                 GetCellInset(c)->SetAutoBreakRows(flag);
659         }
660         return true;
661 }
662
663
664 bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
665 {
666         bool const flag = !width.empty();
667
668         cellinfo_of_cell(cell)->p_width = width;
669         if (IsMultiColumn(cell)) {
670                 GetCellInset(cell)->SetAutoBreakRows(flag);
671                 return true;
672         }
673         return false;
674 }
675
676
677 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
678                                  LyXTabular::Feature what)
679 {
680         if (what == SET_SPECIAL_MULTI)
681                 cellinfo_of_cell(cell)->align_special = special;
682         else
683                 column_info[column_of_cell(cell)].align_special = special;
684         return true;
685 }
686
687
688 bool LyXTabular::SetAllLines(int cell, bool line)
689 {
690         SetTopLine(cell, line);
691         SetBottomLine(cell, line);
692         SetRightLine(cell, line);
693         SetLeftLine(cell, line);
694         return true;
695 }
696
697
698 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
699 {
700         int const row = row_of_cell(cell);
701
702         if (onlycolumn || !IsMultiColumn(cell))
703                 row_info[row].top_line = line;
704         else
705                 cellinfo_of_cell(cell)->top_line = line;
706         return true;
707 }
708
709
710 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
711 {
712         if (onlycolumn || !IsMultiColumn(cell))
713                 row_info[row_of_cell(cell)].bottom_line = line;
714         else
715                 cellinfo_of_cell(cell)->bottom_line = line;
716         return true;
717 }
718
719
720 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
721 {
722         if (onlycolumn || !IsMultiColumn(cell))
723                 column_info[column_of_cell(cell)].left_line = line;
724         else
725                 cellinfo_of_cell(cell)->left_line = line;
726         return true;
727 }
728
729
730 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
731 {
732         if (onlycolumn || !IsMultiColumn(cell))
733                 column_info[right_column_of_cell(cell)].right_line = line;
734         else
735                 cellinfo_of_cell(cell)->right_line = line;
736         return true;
737 }
738
739
740 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
741 {
742         if (!onlycolumn && IsMultiColumn(cell))
743                 return cellinfo_of_cell(cell)->alignment;
744         else
745                 return column_info[column_of_cell(cell)].alignment;
746 }
747
748
749 LyXTabular::VAlignment
750 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
751 {
752         if (!onlycolumn && IsMultiColumn(cell))
753                 return cellinfo_of_cell(cell)->valignment;
754         else
755                 return column_info[column_of_cell(cell)].valignment;
756 }
757
758
759 string const LyXTabular::GetPWidth(int cell) const
760 {
761         if (IsMultiColumn(cell))
762                 return cellinfo_of_cell(cell)->p_width;
763         return column_info[column_of_cell(cell)].p_width;
764 }
765
766
767 string const LyXTabular::GetColumnPWidth(int cell) const
768 {
769         return column_info[column_of_cell(cell)].p_width;
770 }
771
772
773 string const LyXTabular::GetMColumnPWidth(int cell) const
774 {
775         if (IsMultiColumn(cell))
776                 return cellinfo_of_cell(cell)->p_width;
777         return string();
778 }
779
780
781 string const LyXTabular::GetAlignSpecial(int cell, int what) const
782 {
783         if (what == SET_SPECIAL_MULTI)
784                 return cellinfo_of_cell(cell)->align_special;
785         return column_info[column_of_cell(cell)].align_special;
786 }
787
788
789 int LyXTabular::GetWidthOfCell(int cell) const
790 {
791         int const row = row_of_cell(cell);
792         int const column1 = column_of_cell(cell);
793         int const column2 = right_column_of_cell(cell);
794         int result = 0;
795         for (int i = column1; i <= column2; ++i) {
796                 result += cell_info[row][i].width_of_cell;
797         }
798         return result;
799 }
800
801
802 int LyXTabular::GetBeginningOfTextInCell(int cell) const
803 {
804         int x = 0;
805    
806         switch (GetAlignment(cell)){
807         case LYX_ALIGN_CENTER:
808                 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
809                 break;
810         case LYX_ALIGN_RIGHT:
811                 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
812                 // + GetAdditionalWidth(cell);
813                 break;
814         default: /* LYX_ALIGN_LEFT: nothing :-) */ 
815                 break;
816         }
817         
818         // the LaTeX Way :-(
819         x += WIDTH_OF_LINE;
820         return x;
821 }
822
823
824 bool LyXTabular::IsFirstCellInRow(int cell) const
825 {
826         return column_of_cell(cell) == 0;
827 }
828
829
830 int LyXTabular::GetFirstCellInRow(int row) const
831 {
832         if (row > (rows_-1))
833                 row = rows_ - 1;
834         return cell_info[row][0].cellno;
835 }
836
837 bool LyXTabular::IsLastCellInRow(int cell) const
838 {
839         return (right_column_of_cell(cell) == (columns_ - 1));
840 }
841
842
843 int LyXTabular::GetLastCellInRow(int row) const
844 {
845         if (row > (rows_-1))
846                 row = rows_ - 1;
847         return cell_info[row][columns_-1].cellno;
848 }
849
850
851 bool LyXTabular::calculate_width_of_column(int column)
852 {
853         int const old_column_width = column_info[column].width_of_column;
854         int maximum = 0;
855         
856         for (int i = 0; i < rows_; ++i) {
857                 maximum = max(cell_info[i][column].width_of_cell, maximum);
858         }
859         column_info[column].width_of_column = maximum;
860         return (column_info[column].width_of_column != old_column_width);
861 }
862
863
864 //
865 // calculate the with of the column without regarding REAL MultiColumn
866 // cells. This means MultiColumn-cells spanning more than 1 column.
867 //
868 bool LyXTabular::calculate_width_of_column_NMC(int column)
869 {
870         int const old_column_width = column_info[column].width_of_column;
871         int max = 0;
872         for (int i = 0; i < rows_; ++i) {
873                 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
874                         (cell_info[i][column].width_of_cell > max)) {
875                         max = cell_info[i][column].width_of_cell;
876                 }
877         }
878         column_info[column].width_of_column = max;
879         return (column_info[column].width_of_column != old_column_width);
880 }
881
882
883 void LyXTabular::calculate_width_of_tabular()
884 {
885         width_of_tabular = 0;
886         for (int i = 0; i < columns_; ++i) {
887                 width_of_tabular += column_info[i].width_of_column;
888         }
889 }
890
891
892 int LyXTabular::row_of_cell(int cell) const
893 {
894         if (cell >= numberofcells)
895                 return rows_ - 1;
896         else if (cell < 0)
897                 return 0;
898         return rowofcell[cell];
899 }
900
901
902 int LyXTabular::column_of_cell(int cell) const
903 {
904         if (cell >= numberofcells)
905                 return columns_ - 1;
906         else if (cell < 0)
907                 return 0;
908         return columnofcell[cell];
909 }
910
911
912 int LyXTabular::right_column_of_cell(int cell) const
913 {
914         int const row = row_of_cell(cell);
915         int column = column_of_cell(cell);
916         while (column < (columns_ - 1) &&
917                    cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
918                 ++column;
919         return column;
920 }
921
922
923 // Perfect case for a template... (Lgb)
924 #if 1
925 template<class T>
926 string const write_attribute(string const & name, T const & t)
927 {
928         string str = " " + name + "=\"" + tostr(t) + "\"";
929         return str;
930 }
931
932 template <>
933 string const write_attribute(string const & name, bool const & b)
934 {
935         return write_attribute(name, int(b));
936 }
937
938 #else
939
940 string const write_attribute(string const & name, int value)
941 {
942         string str = " " + name + "=\"" + tostr(value) + "\"";
943         return str;
944 }
945
946
947 string const write_attribute(string const & name, string const & value)
948 {
949         string str = " " + name + "=\"" + value + "\"";
950         return str;
951 }
952
953
954 string const write_attribute(string const & name, bool value)
955 {
956         string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
957         return str;
958 }
959 #endif
960
961
962 template<>
963 inline
964 string const tostr(LyXAlignment const & num)
965 {
966         switch(num) {
967         case LYX_ALIGN_NONE:
968                 return "none";
969         case LYX_ALIGN_BLOCK:
970                 return "block";
971         case LYX_ALIGN_LEFT:
972                 return "left";
973         case LYX_ALIGN_CENTER:
974                 return "center";
975         case LYX_ALIGN_RIGHT:
976                 return "right";
977         case LYX_ALIGN_LAYOUT:
978                 return "layout";
979         case LYX_ALIGN_SPECIAL:
980                 return "special";
981         }
982         return string();
983 }
984
985
986 template<>
987 inline
988 string const tostr(LyXTabular::VAlignment const & num)
989 {
990         switch(num) {
991         case LyXTabular::LYX_VALIGN_TOP:
992                 return "top";
993         case LyXTabular::LYX_VALIGN_CENTER:
994                 return "center";
995         case LyXTabular::LYX_VALIGN_BOTTOM:
996                 return "bottom";
997         }
998         return string();
999 }
1000
1001
1002 template<>
1003 inline
1004 string const tostr(LyXTabular::BoxType const & num)
1005 {
1006         switch(num) {
1007         case LyXTabular::BOX_NONE:
1008                 return "none";
1009         case LyXTabular::BOX_PARBOX:
1010                 return "parbox";
1011         case LyXTabular::BOX_MINIPAGE:
1012                 return "minipage";
1013         }
1014         return string();
1015 }
1016
1017
1018 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1019 {
1020         // header line
1021         os << "<lyxtabular"
1022            << write_attribute("version", 2)
1023            << write_attribute("rows", rows_)
1024            << write_attribute("columns", columns_)
1025            << ">\n";
1026         // global longtable options
1027         os << "<features"
1028            << write_attribute("rotate", tostr(rotate))
1029            << write_attribute("islongtable", tostr(is_long_tabular))
1030            << write_attribute("endhead", endhead)
1031            << write_attribute("endfirsthead", endfirsthead)
1032            << write_attribute("endfoot", endfoot)
1033            << write_attribute("endlastfoot", endlastfoot)
1034            << ">\n";
1035         for (int j = 0; j < columns_; ++j) {
1036                 os << "<column"
1037                    << write_attribute("alignment", tostr(column_info[j].alignment))
1038                    << write_attribute("valignment", tostr(column_info[j].valignment))
1039                    << write_attribute("leftline", tostr(column_info[j].left_line))
1040                    << write_attribute("rightline", tostr(column_info[j].right_line))
1041                    << write_attribute("width",
1042                                                           VSpace(column_info[j].p_width)
1043                                                           .asLyXCommand())
1044                    << write_attribute("special", column_info[j].align_special)
1045                    << ">\n";
1046         }
1047         for (int i = 0; i < rows_; ++i) {
1048                 os << "<row"
1049                    << write_attribute("topline", tostr(row_info[i].top_line))
1050                    << write_attribute("bottomline", tostr(row_info[i].bottom_line))
1051                    << write_attribute("newpage", tostr(row_info[i].newpage))
1052                    << ">\n";
1053                 for (int j = 0; j < columns_; ++j) {
1054                         os << "<cell"
1055                            << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1056                            << write_attribute("alignment", tostr(cell_info[i][j].alignment))
1057                            << write_attribute("valignment", tostr(cell_info[i][j].valignment))
1058                            << write_attribute("topline", tostr(cell_info[i][j].top_line))
1059                            << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
1060                            << write_attribute("leftline", tostr(cell_info[i][j].left_line))
1061                            << write_attribute("rightline", tostr(cell_info[i][j].right_line))
1062                            << write_attribute("rotate", tostr(cell_info[i][j].rotate))
1063                            << write_attribute("usebox", tostr(cell_info[i][j].usebox))
1064                            << write_attribute("width", cell_info[i][j].p_width)
1065                            << write_attribute("special", cell_info[i][j].align_special)
1066                            << ">\n";
1067                         os << "\\begin_inset ";
1068                         cell_info[i][j].inset.Write(buf, os);
1069                         os << "\n\\end_inset \n"
1070                            << "</cell>\n";
1071                 }
1072                 os << "</row>\n";
1073         }
1074         os << "</lyxtabular>\n";
1075 }
1076
1077
1078 namespace {
1079
1080 // I would have liked a fromstr template a lot better. (Lgb)
1081
1082 inline
1083 bool string2type(string const str, LyXAlignment & num)
1084 {
1085         if (str == "none")
1086                 num = LYX_ALIGN_NONE;
1087         else if (str == "block")
1088                 num = LYX_ALIGN_BLOCK;
1089         else if (str == "left")
1090                 num = LYX_ALIGN_LEFT;
1091         else if (str == "center")
1092                 num = LYX_ALIGN_CENTER;
1093         else if (str == "right")
1094                 num = LYX_ALIGN_RIGHT;
1095         else
1096                 return false;
1097         return true;
1098 }
1099
1100
1101 inline
1102 bool string2type(string const str, LyXTabular::VAlignment & num)
1103 {
1104         if (str == "top")
1105                 num = LyXTabular::LYX_VALIGN_TOP;
1106         else if (str == "center")
1107                 num = LyXTabular::LYX_VALIGN_CENTER;
1108         else if (str == "bottom")
1109                 num = LyXTabular::LYX_VALIGN_BOTTOM;
1110         else
1111                 return false;
1112         return true;
1113 }
1114
1115
1116 inline
1117 bool string2type(string const str, LyXTabular::BoxType & num)
1118 {
1119         if (str == "none")
1120                 num = LyXTabular::BOX_NONE;
1121         else if (str == "parbox")
1122                 num = LyXTabular::BOX_PARBOX;
1123         else if (str == "minipage")
1124                 num = LyXTabular::BOX_MINIPAGE;
1125         else
1126                 return false;
1127         return true;
1128 }
1129
1130
1131 inline
1132 bool string2type(string const str, bool & num)
1133 {
1134         if (str == "true")
1135                 num = true;
1136         else if (str == "false")
1137                 num = false;
1138         else
1139                 return false;
1140         return true;
1141 }
1142
1143
1144 bool getTokenValue(string const & str, const char * token, string & ret)
1145 {
1146         size_t token_length = strlen(token);
1147         string::size_type pos = str.find(token);
1148
1149         if (pos == string::npos || pos+token_length+1 >= str.length()
1150                 || str[pos+token_length] != '=')
1151                 return false;
1152         ret.erase();
1153         pos += token_length + 1;
1154         char ch = str[pos];
1155         if ((ch != '"') && (ch != '\'')) { // only read till next space
1156                 ret += ch;
1157                 ch = ' ';
1158         }
1159         while((pos < str.length() - 1) && (str[++pos] != ch))
1160                 ret += str[pos];
1161
1162         return true;
1163 }
1164
1165
1166 bool getTokenValue(string const & str, const char * token, int & num)
1167 {
1168         string tmp;
1169         if (!getTokenValue(str, token, tmp))
1170                 return false;
1171         num = strToInt(tmp);
1172         return true;
1173 }
1174
1175
1176 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
1177 {
1178         string tmp;
1179         if (!getTokenValue(str, token, tmp))
1180                 return false;
1181         return string2type(tmp, num);
1182 }
1183
1184
1185 bool getTokenValue(string const & str, const char * token,
1186                                    LyXTabular::VAlignment & num)
1187 {
1188         string tmp;
1189         if (!getTokenValue(str, token, tmp))
1190                 return false;
1191         return string2type(tmp, num);
1192 }
1193
1194
1195 bool getTokenValue(string const & str, const char * token,
1196                                    LyXTabular::BoxType & num)
1197 {
1198         string tmp;
1199         if (!getTokenValue(str, token, tmp))
1200                 return false;
1201         return string2type(tmp, num);
1202 }
1203
1204
1205 bool getTokenValue(string const & str, const char * token, bool & flag)
1206 {
1207         string tmp;
1208         if (!getTokenValue(str, token, tmp))
1209                 return false;
1210         return string2type(tmp, flag);
1211 }    
1212
1213
1214 inline
1215 void l_getline(istream & is, string & str)
1216 {
1217         str.erase();
1218         while (str.empty()) {
1219                 getline(is, str);
1220                 if (!str.empty() && str[str.length() - 1] == '\r')
1221                         str.erase(str.length() - 1);
1222         }
1223 }
1224
1225 } // namespace anon
1226
1227
1228 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1229 {
1230         string line;
1231         istream & is = lex.getStream();
1232
1233         l_getline(is, line);
1234         if (!prefixIs(line, "<lyxtabular ")
1235                 && !prefixIs(line, "<LyXTabular ")) {
1236                 OldFormatRead(lex, line);
1237                 return;
1238         }
1239
1240         int version;
1241         if (!getTokenValue(line, "version", version))
1242                 return;
1243         if (version == 1)
1244                 ReadOld(buf, is, lex, line);
1245         else if (version == 2)
1246                 ReadNew(buf, is, lex, line);
1247 }
1248
1249
1250 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1251                                                  LyXLex & lex, string const & l)
1252 {
1253         string line(l);
1254         int rows_arg;
1255         if (!getTokenValue(line, "rows", rows_arg))
1256                 return;
1257         int columns_arg;
1258         if (!getTokenValue(line, "columns", columns_arg))
1259                 return;
1260         Init(rows_arg, columns_arg);
1261         l_getline(is, line);
1262         if (!prefixIs(line, "<features")) {
1263                 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1264                         line << ")" << endl;
1265                 return;
1266         }
1267         getTokenValue(line, "rotate", rotate);
1268         getTokenValue(line, "islongtable", is_long_tabular);
1269         getTokenValue(line, "endhead", endhead);
1270         getTokenValue(line, "endfirsthead", endfirsthead);
1271         getTokenValue(line, "endfoot", endfoot);
1272         getTokenValue(line, "endlastfoot", endlastfoot);
1273
1274         for (int j = 0; j < columns_; ++j) {
1275                 l_getline(is,line);
1276                 if (!prefixIs(line,"<column")) {
1277                         lyxerr << "Wrong tabular format (expected <column ...> got" <<
1278                                 line << ")" << endl;
1279                         return;
1280                 }
1281                 getTokenValue(line, "alignment", column_info[j].alignment);
1282                 getTokenValue(line, "valignment", column_info[j].valignment);
1283                 getTokenValue(line, "leftline", column_info[j].left_line);
1284                 getTokenValue(line, "rightline", column_info[j].right_line);
1285                 getTokenValue(line, "width", column_info[j].p_width);
1286                 getTokenValue(line, "special", column_info[j].align_special);
1287         }
1288
1289         for (int i = 0; i < rows_; ++i) {
1290                 l_getline(is, line);
1291                 if (!prefixIs(line, "<row")) {
1292                         lyxerr << "Wrong tabular format (expected <row ...> got" <<
1293                                 line << ")" << endl;
1294                         return;
1295                 }
1296                 getTokenValue(line, "topline", row_info[i].top_line);
1297                 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1298                 getTokenValue(line, "newpage", row_info[i].newpage);
1299                 for (int j = 0; j < columns_; ++j) {
1300                         l_getline(is, line);
1301                         if (!prefixIs(line, "<cell")) {
1302                                 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1303                                         line << ")" << endl;
1304                                 return;
1305                         }
1306                         getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1307                         getTokenValue(line, "alignment", cell_info[i][j].alignment);
1308                         getTokenValue(line, "valignment", cell_info[i][j].valignment);
1309                         getTokenValue(line, "topline", cell_info[i][j].top_line);
1310                         getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1311                         getTokenValue(line, "leftline", cell_info[i][j].left_line);
1312                         getTokenValue(line, "rightline", cell_info[i][j].right_line);
1313                         getTokenValue(line, "rotate", cell_info[i][j].rotate);
1314                         getTokenValue(line, "usebox", cell_info[i][j].usebox);
1315                         getTokenValue(line, "width", cell_info[i][j].p_width);
1316                         getTokenValue(line, "special", cell_info[i][j].align_special);
1317                         l_getline(is, line);
1318                         if (prefixIs(line, "\\begin_inset")) {
1319                                 cell_info[i][j].inset.Read(buf, lex);
1320                                 l_getline(is, line);
1321                         }
1322                         if (!prefixIs(line, "</cell>")) {
1323                                 lyxerr << "Wrong tabular format (expected </cell> got" <<
1324                                         line << ")" << endl;
1325                                 return;
1326                         }
1327                 }
1328                 l_getline(is, line);
1329                 if (!prefixIs(line, "</row>")) {
1330                         lyxerr << "Wrong tabular format (expected </row> got" <<
1331                                 line << ")" << endl;
1332                         return;
1333                 }
1334         }
1335         while (!prefixIs(line, "</lyxtabular>")) {
1336                 l_getline(is, line);
1337         }
1338         set_row_column_number_info();
1339 }
1340
1341
1342 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
1343 {
1344         int version;
1345         int i;
1346         int j;
1347         int rows_arg = 0;
1348         int columns_arg = 0;
1349         int is_long_tabular_arg = false;
1350         int rotate_arg = false;
1351         int a = -1;
1352         int b = -1;
1353         int c = -1;
1354         int d = -1;
1355         int e = 0;
1356         int f = 0;
1357         int g = 0;
1358         
1359         istream & is = lex.getStream();
1360         string s(fl);
1361         if (s.length() > 8)
1362                 version = lyx::atoi(s.substr(8, string::npos));
1363         else
1364                 version = 1;
1365
1366         vector<int> cont_row_info;
1367
1368         if (version < 5) {
1369                 lyxerr << "Tabular format < 5 is not supported anymore\n"
1370                         "Get an older version of LyX (< 1.1.x) for conversion!"
1371                            << endl;
1372                 WriteAlert(_("Warning:"),
1373                                    _("Tabular format < 5 is not supported anymore\n"),
1374                                    _("Get an older version of LyX (< 1.1.x) for conversion!"));
1375                 if (version > 2) {
1376                         is >> rows_arg >> columns_arg >> is_long_tabular_arg
1377                            >> rotate_arg >> a >> b >> c >> d;
1378                 } else
1379                         is >> rows_arg >> columns_arg;
1380                 Init(rows_arg, columns_arg);
1381                 cont_row_info = vector<int>(rows_arg);
1382                 SetLongTabular(is_long_tabular_arg);
1383                 SetRotateTabular(rotate_arg);
1384                 string tmp;
1385                 for (i = 0; i < rows_; ++i) {
1386                         getline(is, tmp);
1387                         cont_row_info[i] = false;
1388                 }
1389                 for (i = 0; i < columns_; ++i) {
1390                         getline(is, tmp);
1391                 }
1392                 for (i = 0; i < rows_; ++i) {
1393                         for (j = 0; j < columns_; ++j) {
1394                                 getline(is, tmp);
1395                         }
1396                 }
1397         } else {
1398                 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1399                    >> rotate_arg >> a >> b >> c >> d;
1400                 Init(rows_arg, columns_arg);
1401                 cont_row_info = vector<int>(rows_arg);
1402                 SetLongTabular(is_long_tabular_arg);
1403                 SetRotateTabular(rotate_arg);
1404                 endhead = a + 1;
1405                 endfirsthead = b + 1;
1406                 endfoot = c + 1;
1407                 endlastfoot = d + 1;
1408                 for (i = 0; i < rows_; ++i) {
1409                         a = b = c = d = e = f = g = 0;
1410                         is >> a >> b >> c >> d;
1411                         row_info[i].top_line = a;
1412                         row_info[i].bottom_line = b;
1413                         cont_row_info[i] = c;
1414                         row_info[i].newpage = d;
1415                 }
1416                 for (i = 0; i < columns_; ++i) {
1417                         string s1;
1418                         string s2;
1419                         is >> a >> b >> c;
1420 #if 1
1421                         char ch; // skip '"'
1422                         is >> ch;
1423 #else
1424                         // ignore is buggy but we will use it later (Lgb)
1425                         is.ignore(); // skip '"'
1426 #endif    
1427                         getline(is, s1, '"');
1428 #if 1
1429                         is >> ch; // skip '"'
1430 #else
1431                         // ignore is buggy but we will use it later (Lgb)
1432                         is.ignore(); // skip '"'
1433 #endif
1434                         getline(is, s2, '"');
1435                         column_info[i].alignment = static_cast<LyXAlignment>(a);
1436                         column_info[i].left_line = b;
1437                         column_info[i].right_line = c;
1438                         column_info[i].p_width = s1;
1439                         column_info[i].align_special = s2;
1440                 }
1441                 for (i = 0; i < rows_; ++i) {
1442                         for (j = 0; j < columns_; ++j) {
1443                                 string s1;
1444                                 string s2;
1445                                 is >> a >> b >> c >> d >> e >> f >> g;
1446 #if 1
1447                                 char ch;
1448                                 is >> ch; // skip '"'
1449 #else
1450                                 // ignore is buggy but we will use it later (Lgb)
1451                                 is.ignore(); // skip '"'
1452 #endif
1453                                 getline(is, s1, '"');
1454 #if 1
1455                                 is >> ch; // skip '"'
1456 #else
1457                                 // ignore is buggy but we will use it later (Lgb)
1458                                 is.ignore(); // skip '"'
1459 #endif
1460                                 getline(is, s2, '"');
1461                                 cell_info[i][j].multicolumn = static_cast<char>(a);
1462                                 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1463                                 cell_info[i][j].top_line = static_cast<char>(c);
1464                                 cell_info[i][j].bottom_line = static_cast<char>(d);
1465                                 cell_info[i][j].left_line = column_info[j].left_line;
1466                                 cell_info[i][j].right_line = column_info[j].right_line;
1467                                 cell_info[i][j].rotate = static_cast<bool>(f);
1468                                 cell_info[i][j].usebox = static_cast<BoxType>(g);
1469                                 cell_info[i][j].align_special = s1;
1470                                 cell_info[i][j].p_width = s2;
1471                         }
1472                 }
1473         }
1474         set_row_column_number_info(true);
1475
1476         LyXParagraph * par = new LyXParagraph;
1477         LyXParagraph * return_par = 0;
1478
1479         string tmptok;
1480         int pos = 0;
1481         char depth = 0;
1482         LyXFont font(LyXFont::ALL_INHERIT);
1483         font.setLanguage(owner_->BufferOwner()->GetLanguage());
1484
1485         while (lex.IsOK()) {
1486                 lex.nextToken();
1487                 string const token = lex.GetString();
1488                 if (token.empty())
1489                         continue;
1490                 if (token == "\\layout"
1491                         || token == "\\end_float"
1492                         || token == "\\end_deeper") {
1493                         lex.pushToken(token);
1494                         break;
1495                 }
1496                 if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
1497                                                                                                                           return_par,
1498                                                                                                                           token, pos,
1499                                                                                                                           depth, font)) {
1500                         // the_end read
1501                         lex.pushToken(token);
1502                         break;
1503                 }
1504                 if (return_par) {
1505                         lex.printError("New Paragraph allocated! This should not happen!");
1506                         lex.pushToken(token);
1507                         delete par;
1508                         par = return_par;
1509                         break;
1510                 }
1511         }
1512         // now we have the par we should fill the insets with this!
1513         int cell = 0;
1514         InsetText * inset = GetCellInset(cell);
1515         int row;
1516
1517         for (int i = 0; i < par->size(); ++i) {
1518                 if (par->IsNewline(i)) {
1519                         ++cell;
1520                         if (cell > GetNumberOfCells()) {
1521                                 lyxerr << "Some error in reading old table format occured!" <<
1522                                         endl << "Terminating when reading cell[" << cell << "]!" <<
1523                                         endl;
1524                                 delete par;
1525                                 return;
1526                         }
1527                         row = row_of_cell(cell);
1528                         if (cont_row_info[row]) {
1529                                 DeleteRow(row);
1530                                 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1531                                 while(!IsFirstCellInRow(--cell));
1532                         } else {
1533                                 inset = GetCellInset(cell);
1534                                 continue;
1535                         }
1536                         inset = GetCellInset(cell);
1537                         row = row_of_cell(cell);
1538                         if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1539                         {
1540                                 // insert a space instead
1541                                 par->Erase(i);
1542                                 par->InsertChar(i, ' ');
1543                         }
1544                 }
1545                 par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
1546                 inset->par->InsertFromMinibuffer(inset->par->size());
1547         }
1548         delete par;
1549         Reinit();
1550 }
1551
1552
1553 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1554 {
1555         return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1556                         (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1557 }
1558
1559
1560 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1561 {
1562         int const row = row_of_cell(cell);
1563         int const column = column_of_cell(cell);
1564         return  &cell_info[row][column];
1565 }
1566
1567
1568 void LyXTabular::SetMultiColumn(int cell, int number)
1569 {
1570         cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1571         cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1572         cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1573         cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1574         for (number--; number > 0; --number) {
1575                 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1576         }
1577         set_row_column_number_info();
1578 }
1579
1580
1581 int LyXTabular::cells_in_multicolumn(int cell) const
1582 {
1583         int const row = row_of_cell(cell);
1584         int column = column_of_cell(cell);
1585         int result = 1;
1586         ++column;
1587         while ((column < columns_) &&
1588                    cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1589         {
1590                 ++result;
1591                 ++column;
1592         }
1593         return result;
1594 }
1595
1596
1597 int LyXTabular::UnsetMultiColumn(int cell)
1598 {
1599         int const row = row_of_cell(cell);
1600         int column = column_of_cell(cell);
1601         
1602         int result = 0;
1603         
1604         if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1605                 cell_info[row][column].multicolumn = CELL_NORMAL;
1606                 ++column;
1607                 while ((column < columns_) &&
1608                            (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1609                 {
1610                         cell_info[row][column].multicolumn = CELL_NORMAL;
1611                         ++column;
1612                         ++result;
1613                 }
1614         }
1615         set_row_column_number_info();
1616         return result;
1617 }
1618
1619
1620 void LyXTabular::SetLongTabular(bool what)
1621 {
1622         is_long_tabular = what;
1623 }
1624
1625
1626 bool LyXTabular::IsLongTabular() const
1627 {
1628         return is_long_tabular;
1629 }
1630
1631
1632 void LyXTabular::SetRotateTabular(bool flag)
1633 {
1634         rotate = flag;
1635 }
1636
1637
1638 bool LyXTabular::GetRotateTabular() const
1639 {
1640         return rotate;
1641 }
1642
1643
1644 void LyXTabular::SetRotateCell(int cell, bool flag)
1645 {
1646         cellinfo_of_cell(cell)->rotate = flag;
1647 }
1648
1649
1650 bool LyXTabular::GetRotateCell(int cell) const
1651 {
1652         return cellinfo_of_cell(cell)->rotate;
1653 }
1654
1655
1656 bool LyXTabular::NeedRotating() const
1657 {
1658         if (rotate)
1659                 return true;
1660         for (int i = 0; i < rows_; ++i) {
1661                 for (int j = 0; j < columns_; ++j) {
1662                         if (cell_info[i][j].rotate)
1663                                 return true;
1664                 }
1665         }
1666         return false;
1667 }
1668
1669
1670 bool LyXTabular::IsLastCell(int cell) const
1671 {
1672         if ((cell + 1) < GetNumberOfCells())
1673                 return false;
1674         return true;
1675 }
1676
1677
1678 int LyXTabular::GetCellAbove(int cell) const
1679 {
1680         if (row_of_cell(cell) > 0)
1681                 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1682         return cell;
1683 }
1684
1685
1686 int LyXTabular::GetCellBelow(int cell) const
1687 {
1688         if (row_of_cell(cell) + 1 < rows_)
1689                 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1690         return cell;
1691 }
1692
1693
1694 int LyXTabular::GetLastCellAbove(int cell) const
1695 {
1696         if (row_of_cell(cell) <= 0)
1697                 return cell;
1698         if (!IsMultiColumn(cell))
1699                 return GetCellAbove(cell);
1700         return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1701 }
1702
1703
1704 int LyXTabular::GetLastCellBelow(int cell) const
1705 {
1706         if (row_of_cell(cell) + 1 >= rows_)
1707                 return cell;
1708         if (!IsMultiColumn(cell))
1709                 return GetCellBelow(cell);
1710         return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1711 }
1712
1713
1714 int LyXTabular::GetCellNumber(int row, int column) const
1715 {
1716         if (column >= columns_)
1717                 column = columns_ - 1;
1718         else if (column < 0)
1719                 column = 0;
1720         if (row >= rows_)
1721                 row = rows_ - 1;
1722         else if (row < 0)
1723                 row = 0;
1724         
1725         return cell_info[row][column].cellno;
1726 }
1727
1728
1729 void LyXTabular::SetUsebox(int cell, BoxType type)
1730 {
1731         cellinfo_of_cell(cell)->usebox = type;
1732 }
1733
1734
1735 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1736 {
1737         if (column_info[column_of_cell(cell)].p_width.empty() &&
1738                 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
1739                 return BOX_NONE;
1740         if (cellinfo_of_cell(cell)->usebox > 1)
1741                 return cellinfo_of_cell(cell)->usebox;
1742         return UseParbox(cell);
1743 }
1744
1745
1746 void LyXTabular::SetLTHead(int cell, bool first)
1747 {
1748         int const row = row_of_cell(cell);
1749         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1750
1751         if (first) {
1752                 if (endfirsthead == val)
1753                         endfirsthead = 0;
1754                 else
1755                         endfirsthead = val;
1756         } else {
1757                 if (endhead == val)
1758                         endhead = 0;
1759                 else
1760                         endhead = val;
1761         }
1762 }
1763
1764
1765 bool LyXTabular::GetRowOfLTHead(int cell, int & row) const
1766 {
1767         row = endhead;
1768         if (abs(endhead) > rows_)
1769                 return false;
1770         return (row_of_cell(cell) == abs(endhead) - 1);
1771 }
1772
1773
1774 bool LyXTabular::GetRowOfLTFirstHead(int cell, int & row) const
1775 {
1776         row = endfirsthead;
1777         if (abs(endfirsthead) > rows_)
1778                 return false;
1779         return (row_of_cell(cell) == abs(endfirsthead) - 1);
1780 }
1781
1782
1783 void LyXTabular::SetLTFoot(int cell, bool last)
1784 {
1785         int const row = row_of_cell(cell);
1786         int const val = (row + 1) * (column_of_cell(cell) ? 1 : -1);
1787
1788         if (last) {
1789                 if (endlastfoot == val)
1790                         endlastfoot = 0;
1791                 else
1792                         endlastfoot = val;
1793         } else {
1794                 if (endfoot == val)
1795                         endfoot = 0;
1796                 else
1797                         endfoot = val;
1798         }
1799 }
1800
1801
1802 bool LyXTabular::GetRowOfLTFoot(int cell, int & row) const
1803 {
1804         row = endfoot;
1805         if ((endfoot + 1) > rows_)
1806                 return false;
1807         return (row_of_cell(cell) == abs(endfoot) - 1);
1808 }
1809
1810
1811 bool LyXTabular::GetRowOfLTLastFoot(int cell, int & row) const
1812 {
1813         row = endlastfoot;
1814         if (abs(endlastfoot) > rows_)
1815                 return false;
1816         return (row_of_cell(cell) == (abs(endlastfoot)-1));
1817 }
1818
1819
1820 void LyXTabular::SetLTNewPage(int cell, bool what)
1821 {
1822         row_info[row_of_cell(cell)].newpage = what;
1823 }
1824
1825
1826 bool LyXTabular::GetLTNewPage(int cell) const
1827 {
1828         return row_info[row_of_cell(cell)].newpage;
1829 }
1830
1831
1832 bool LyXTabular::SetAscentOfRow(int row, int height)
1833 {
1834         if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1835                 return false;
1836         row_info[row].ascent_of_row = height;
1837         return true;
1838 }
1839
1840
1841 bool LyXTabular::SetDescentOfRow(int row, int height)
1842 {
1843         if ((row >= rows_) || (row_info[row].descent_of_row == height))
1844                 return false;
1845         row_info[row].descent_of_row = height;
1846         return true;
1847 }
1848
1849
1850 int LyXTabular::GetAscentOfRow(int row) const
1851 {
1852         if (row >= rows_)
1853                 return 0;
1854         return row_info[row].ascent_of_row;
1855 }
1856
1857
1858 int LyXTabular::GetDescentOfRow(int row) const
1859 {
1860         if (row >= rows_)
1861                 return 0;
1862         return row_info[row].descent_of_row;
1863 }
1864
1865
1866 int LyXTabular::GetHeightOfTabular() const
1867 {
1868         int height = 0;
1869
1870         for (int row = 0; row < rows_; ++row)
1871                 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1872                         GetAdditionalHeight(row);
1873         return height;
1874 }
1875
1876
1877 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1878 {
1879         if ((row >= rows_) || (column >= columns_))
1880                 return false;
1881         return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1882 }
1883
1884
1885 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1886 {
1887         if ((row < 0) || (row >= rows_))
1888                 return 0;
1889
1890         int const fcell = GetFirstCellInRow(row);
1891         int const n = NumberOfCellsInRow(fcell) + fcell;
1892         int tmp = 0;
1893
1894         for (int i = fcell; i < n; ++i) {
1895                 if (TopLine(i))
1896                         ++tmp;
1897         }
1898         if (tmp == (n - fcell)){
1899                 os << "\\hline ";
1900         } else if (tmp) {
1901                 for (int i = fcell; i < n; ++i) {
1902                         if (TopLine(i)) {
1903                                 os << "\\cline{"
1904                                    << column_of_cell(i) + 1
1905                                    << '-'
1906                                    << right_column_of_cell(i) + 1
1907                                    << "} ";
1908                         }
1909                 }
1910         } else {
1911                 return 0;
1912         }
1913         os << "\n";
1914         return 1;
1915 }
1916
1917
1918 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1919 {
1920         if ((row < 0) || (row >= rows_))
1921                 return 0;
1922
1923         int const fcell = GetFirstCellInRow(row);
1924         int const n = NumberOfCellsInRow(fcell) + fcell;
1925         int tmp = 0;
1926
1927         for (int i = fcell; i < n; ++i) {
1928                 if (BottomLine(i))
1929                         ++tmp;
1930         }
1931         if (tmp == (n-fcell)){
1932                 os << "\\hline";
1933         } else if (tmp) {
1934                 for (int i = fcell; i < n; ++i) {
1935                         if (BottomLine(i)) {
1936                                 os << "\\cline{"
1937                                    << column_of_cell(i) + 1
1938                                    << '-'
1939                                    << right_column_of_cell(i) + 1
1940                                    << "} ";
1941                         }
1942                 }
1943         } else {
1944                 return 0;
1945         }
1946         os << "\n";
1947         return 1;
1948 }
1949
1950
1951 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1952 {
1953         int ret = 0;
1954
1955         if (GetRotateCell(cell)) {
1956                 os << "\\begin{sideways}\n";
1957                 ++ret;
1958         }
1959         if (IsMultiColumn(cell)) {
1960                 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1961                 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1962                         os << cellinfo_of_cell(cell)->align_special << "}{";
1963                 } else {
1964                         if (LeftLine(cell))
1965                                 os << '|';
1966                         if (!GetPWidth(cell).empty()) {
1967                                 switch (GetVAlignment(cell)) {
1968                                 case LYX_VALIGN_TOP:
1969                                         os << "p";
1970                                         break;
1971                                 case LYX_VALIGN_CENTER:
1972                                         os << "m";
1973                                         break;
1974                                 case LYX_VALIGN_BOTTOM:
1975                                         os << "b";
1976                                         break;
1977                                 }
1978                                 os << "{" << GetPWidth(cell) << '}';
1979                         } else {
1980                                 switch (GetAlignment(cell)) {
1981                                 case LYX_ALIGN_LEFT:
1982                                         os << 'l';
1983                                         break;
1984                                 case LYX_ALIGN_RIGHT:
1985                                         os << 'r';
1986                                         break;
1987                                 default:
1988                                         os << 'c';
1989                                         break;
1990                                 }
1991                         }
1992                         if (RightLine(cell))
1993                                 os << '|';
1994                         if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
1995                                 LeftLine(cell+1))
1996                                 os << '|';
1997                         os << "}{";
1998                 }
1999         }
2000         if (GetUsebox(cell) == BOX_PARBOX) {
2001                 os << "\\parbox[";
2002                 switch (GetVAlignment(cell)) {
2003                 case LYX_VALIGN_TOP:
2004                         os << "t";
2005                         break;
2006                 case LYX_VALIGN_CENTER:
2007                         os << "c";
2008                         break;
2009                 case LYX_VALIGN_BOTTOM:
2010                         os << "b";
2011                         break;
2012                 }
2013                 os << "]{" << GetPWidth(cell) << "}{";
2014         } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2015                 os << "\\begin{minipage}[";
2016                 switch (GetVAlignment(cell)) {
2017                 case LYX_VALIGN_TOP:
2018                         os << "t";
2019                         break;
2020                 case LYX_VALIGN_CENTER:
2021                         os << "m";
2022                         break;
2023                 case LYX_VALIGN_BOTTOM:
2024                         os << "b";
2025                         break;
2026                 }
2027                 os << "]{" << GetPWidth(cell) << "}\n";
2028                 ++ret;
2029         }
2030         return ret;
2031 }
2032
2033
2034 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2035 {
2036         int ret = 0;
2037
2038         // usual cells
2039         if (GetUsebox(cell) == BOX_PARBOX)
2040                 os << "}";
2041         else if (GetUsebox(cell) == BOX_MINIPAGE) {
2042                 os << "%\n\\end{minipage}";
2043                 ret += 2;
2044         }
2045         if (IsMultiColumn(cell)){
2046                 os << '}';
2047         }
2048         if (GetRotateCell(cell)) {
2049                 os << "%\n\\end{sideways}";
2050                 ++ret;
2051         }
2052         return ret;
2053 }
2054
2055
2056 int LyXTabular::Latex(Buffer const * buf,
2057                                           ostream & os, bool fragile, bool fp) const
2058 {
2059         int ret = 0;
2060         int cell = 0;
2061
2062         //+---------------------------------------------------------------------
2063         //+                      first the opening preamble                    +
2064         //+---------------------------------------------------------------------
2065
2066         if (rotate) {
2067                 os << "\\begin{sideways}\n";
2068                 ++ret;
2069         }
2070         if (is_long_tabular)
2071                 os << "\\begin{longtable}{";
2072         else
2073                 os << "\\begin{tabular}{";
2074         for (int i = 0; i < columns_; ++i) {
2075                 if (column_info[i].left_line)
2076                         os << '|';
2077                 if (!column_info[i].align_special.empty()) {
2078                         os << column_info[i].align_special;
2079                 } else if (!column_info[i].p_width.empty()) {
2080                         switch (column_info[i].valignment) {
2081                         case LYX_VALIGN_TOP:
2082                                 os << "p";
2083                                 break;
2084                         case LYX_VALIGN_CENTER:
2085                                 os << "m";
2086                                 break;
2087                         case LYX_VALIGN_BOTTOM:
2088                                 os << "b";
2089                                 break;
2090                         }
2091                         os << "{"
2092                            << column_info[i].p_width
2093                            << '}';
2094                 } else {
2095                         switch (column_info[i].alignment) {
2096                         case LYX_ALIGN_LEFT:
2097                                 os << 'l';
2098                                 break;
2099                         case LYX_ALIGN_RIGHT:
2100                                 os << 'r';
2101                                 break;
2102                         default:
2103                                 os << 'c';
2104                                 break;
2105                         }
2106                 }
2107                 if (column_info[i].right_line)
2108                         os << '|';
2109         }
2110         os << "}\n";
2111         ++ret;
2112
2113         //+---------------------------------------------------------------------
2114         //+                      the single row and columns (cells)            +
2115         //+---------------------------------------------------------------------
2116
2117         for (int i = 0; i < rows_; ++i) {
2118                 ret += TeXTopHLine(os, i);
2119                 int bret = ret;
2120                 if (IsLongTabular()) {
2121                         if ((endhead < 0) && (i == (abs(endhead)-1))) {
2122                                 os << "\\endhead\n";
2123                                 ++ret;
2124                         }
2125                         if ((endfirsthead < 0) && (i == (abs(endfirsthead)-1))) {
2126                                 os << "\\endfirsthead\n";
2127                                 ++ret;
2128                         }
2129                         if ((endfoot < 0) && (i == (abs(endfoot)-1))) {
2130                                 os << "\\endfoot\n";
2131                                 ++ret;
2132                         }
2133                         if ((endlastfoot < 0) && (i == (abs(endlastfoot)-1))) {
2134                                 os << "\\endlastfoot\n";
2135                                 ++ret;
2136                         }
2137                 }
2138                 if (ret > bret) {
2139                         ret += TeXBottomHLine(os, i-1);
2140                         ret += TeXTopHLine(os, i);
2141                 }
2142                 for (int j = 0; j < columns_; ++j) {
2143                         if (IsPartOfMultiColumn(i,j))
2144                                 continue;
2145                         ret += TeXCellPreamble(os, cell);
2146                         InsetText * inset = GetCellInset(cell);
2147
2148                         bool rtl = inset->par->isRightToLeftPar(buf->params) &&
2149                                         inset->par->size() > 0 && GetPWidth(cell).empty();
2150
2151                         if (rtl)
2152                                 os << "\\R{";
2153                         ret += inset->Latex(buf, os, fragile, fp);
2154                         if (rtl)
2155                                 os << "}";
2156
2157                         ret += TeXCellPostamble(os, cell);
2158                         if (!IsLastCellInRow(cell)) { // not last cell in row
2159                                 os << "&\n";
2160                                 ++ret;
2161                         }
2162                         ++cell;
2163                 }
2164                 os << "\\\\\n";
2165                 ret += TeXBottomHLine(os, i);
2166                 bret = ret;
2167                 if (IsLongTabular()) {
2168                         if ((endhead > 0) && (i == (endhead - 1))) {
2169                                 os << "\\endhead\n";
2170                                 ++ret;
2171                         }
2172                         if ((endfirsthead > 0) && (i == (endfirsthead - 1))) {
2173                                 os << "\\endfirsthead\n";
2174                                 ++ret;
2175                         }
2176                         if ((endfoot > 0) && (i == (endfoot - 1))) {
2177                                 os << "\\endfoot\n";
2178                                 ++ret;
2179                         }
2180                         if ((endlastfoot > 0) && (i == (endlastfoot - 1))) {
2181                                 os << "\\endlastfoot\n";
2182                                 ++ret;
2183                         }
2184 //          if (ret > bret)
2185 //              ret += TeXBottomHLine(os, i);
2186                         if (row_info[i].newpage) {
2187                                 os << "\\newpage\n";
2188                                 ++ret;
2189                         }
2190                 }
2191         }
2192
2193         //+---------------------------------------------------------------------
2194         //+                      the closing of the tabular                    +
2195         //+---------------------------------------------------------------------
2196
2197         if (is_long_tabular)
2198                 os << "\\end{longtable}";
2199         else
2200                 os << "\\end{tabular}";
2201         if (rotate) {
2202                 os << "\n\\end{sideways}";
2203                 ++ret;
2204         }
2205
2206         return ret;
2207 }
2208
2209
2210 int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
2211 {
2212         int ret = 0;
2213
2214         //+---------------------------------------------------------------------
2215         //+                      first the opening preamble                    +
2216         //+---------------------------------------------------------------------
2217
2218         os << "<tgroup cols=\"" << columns_
2219            << "\" colsep=\"1\" rowsep=\"1\">\n";
2220         
2221         for (int i = 0; i < columns_; ++i) {
2222                 os << "<colspec colname=\"col" << i << "\" align=\"";
2223                 switch (column_info[i].alignment) {
2224                 case LYX_ALIGN_LEFT:
2225                         os << "left";
2226                         break;
2227                 case LYX_ALIGN_RIGHT:
2228                         os << "right";
2229                         break;
2230                 default:
2231                         os << "center";
2232                         break;
2233                 }
2234                 os << "\"/>\n";
2235                 ++ret;
2236         }
2237
2238         //+---------------------------------------------------------------------
2239         //+                      the single row and columns (cells)            +
2240         //+---------------------------------------------------------------------
2241
2242         int cell = 0;
2243         os << "<tbody>\n";
2244         for (int i = 0; i < rows_; ++i) {
2245                 os << "<row>\n";
2246                 for (int j = 0; j < columns_; ++j) {
2247                         if (IsPartOfMultiColumn(i, j))
2248                                 continue;
2249                 
2250                         os << "<entry align=\"";
2251                         switch (GetAlignment(cell)) {
2252                         case LYX_ALIGN_LEFT:
2253                                 os << "left";
2254                                 break;
2255                         case LYX_ALIGN_RIGHT:
2256                                 os << "right";
2257                                 break;
2258                         default:
2259                                 os << "center";
2260                                 break;
2261                         }
2262                 
2263                         os << "\" valign=\"";
2264                         switch (GetVAlignment(cell)) {
2265                         case LYX_VALIGN_TOP:
2266                                 os << "top";
2267                                 break;
2268                         case LYX_VALIGN_BOTTOM:
2269                                 os << "bottom";
2270                                 break;
2271                         case LYX_VALIGN_CENTER:
2272                                 os << "middle";
2273                         }
2274                         os << "\"";
2275                 
2276                         if (IsMultiColumn(cell)) {
2277                                 os << " namest=\"col" << j << "\" ";
2278                                 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2279                         }
2280                 
2281                         os << ">";
2282                         ret += GetCellInset(cell)->DocBook(buf, os);
2283                         os << "</entry>";
2284                         ++cell;
2285                 }
2286                 os << "</row>\n";
2287         }
2288         os << "</tbody>\n";
2289         //+---------------------------------------------------------------------
2290         //+                      the closing of the tabular                    +
2291         //+---------------------------------------------------------------------
2292
2293         os << "</tgroup>";
2294         ++ret;
2295
2296         return ret;
2297 }
2298
2299
2300 namespace {
2301
2302         inline
2303         void print_n_chars(ostream & os, unsigned char ch, int n)
2304         {
2305                 os << string(n, ch);
2306         }
2307
2308 } // namespace anon
2309
2310
2311 int LyXTabular::AsciiTopHLine(ostream & os, int row,
2312                                                           vector<unsigned int> const & clen) const
2313 {
2314         int const fcell = GetFirstCellInRow(row);
2315         int const n = NumberOfCellsInRow(fcell) + fcell;
2316         int tmp = 0;
2317
2318         for (int i = fcell; i < n; ++i) {
2319                 if (TopLine(i)) {
2320                         ++tmp;
2321                         break;
2322                 }
2323         }
2324         if (!tmp)
2325                 return 0;
2326
2327         unsigned char ch;
2328         for (int i = fcell; i < n; ++i) {
2329                 if (TopLine(i)) {
2330                         if (LeftLine(i))
2331                                 os << "+-";
2332                         else
2333                                 os << "--";
2334                         ch = '-';
2335                 } else {
2336                         os << "  ";
2337                         ch = ' ';
2338                 }
2339                 int column = column_of_cell(i);
2340                 int len = clen[column];
2341                 while(IsPartOfMultiColumn(row, ++column))
2342                         len += clen[column] + 4;
2343                 print_n_chars(os, ch, len);
2344                 if (TopLine(i)) {
2345                         if (RightLine(i))
2346                                 os << "-+";
2347                         else
2348                                 os << "--";
2349                 } else {
2350                         os << "  ";
2351                 }
2352         }
2353         os << endl;
2354         return 1;
2355 }
2356
2357
2358 int LyXTabular::AsciiBottomHLine(ostream & os, int row,
2359                                                                  vector<unsigned int> const & clen) const
2360 {
2361         int const fcell = GetFirstCellInRow(row);
2362         int const n = NumberOfCellsInRow(fcell) + fcell;
2363         int tmp = 0;
2364
2365         for (int i = fcell; i < n; ++i) {
2366                 if (BottomLine(i)) {
2367                         ++tmp;
2368                         break;
2369                 }
2370         }
2371         if (!tmp)
2372                 return 0;
2373
2374         unsigned char ch;
2375         for (int i = fcell; i < n; ++i) {
2376                 if (BottomLine(i)) {
2377                         if (LeftLine(i))
2378                                 os << "+-";
2379                         else
2380                                 os << "--";
2381                         ch = '-';
2382                 } else {
2383                         os << "  ";
2384                         ch = ' ';
2385                 }
2386                 int column = column_of_cell(i);
2387                 int len = clen[column];
2388                 while(IsPartOfMultiColumn(row, ++column))
2389                         len += clen[column] + 4;
2390                 print_n_chars(os, ch, len);
2391                 if (BottomLine(i)) {
2392                         if (RightLine(i))
2393                                 os << "-+";
2394                         else
2395                                 os << "--";
2396                 } else {
2397                         os << "  ";
2398                 }
2399         }
2400         os << endl;
2401         return 1;
2402 }
2403
2404
2405 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
2406                                                            int cell, int row, int column,
2407                                                            vector<unsigned int> const & clen) const
2408 {
2409         ostringstream sstr;
2410         int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
2411
2412         if (LeftLine(cell))
2413                 os << "| ";
2414         else
2415                 os << "  ";
2416
2417         unsigned int len1 = sstr.str().length();
2418         unsigned int len2 = clen[column];
2419         while(IsPartOfMultiColumn(row, ++column))
2420                 len2 += clen[column] + 4;
2421         len2 -= len1;
2422
2423         switch (GetAlignment(cell)) {
2424         default:
2425         case LYX_ALIGN_LEFT:
2426                 len1 = 0;
2427                 break;
2428         case LYX_ALIGN_RIGHT:
2429                 len1 = len2;
2430                 len2 = 0;
2431                 break;
2432         case LYX_ALIGN_CENTER:
2433                 len1 = len2 / 2;
2434                 len2 -= len1;
2435                 break;
2436         }
2437
2438         for (unsigned int i = 0; i < len1; ++i)
2439                 os << " ";
2440         os << sstr.str();
2441         for (unsigned int i = 0; i < len2; ++i)
2442                 os << " ";
2443         if (RightLine(cell))
2444                 os << " |";
2445         else
2446                 os << "  ";
2447
2448         return ret;
2449 }
2450
2451
2452 int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
2453 {
2454         int ret = 0;
2455
2456         //+---------------------------------------------------------------------
2457         //+           first calculate the width of the single columns          +
2458         //+---------------------------------------------------------------------
2459         vector<unsigned int> clen(columns_);
2460
2461         // first all non (real) multicolumn cells!
2462         for (int j = 0; j < columns_; ++j) {
2463                 clen[j] = 0;
2464                 for (int i = 0; i < rows_; ++i) {
2465                         int cell = GetCellNumber(i, j);
2466                         if (IsMultiColumn(cell, true))
2467                                 continue;
2468                         ostringstream sstr;
2469                         GetCellInset(cell)->Ascii(buf, sstr, 0);
2470                         if (clen[j] < sstr.str().length())
2471                                 clen[j] = sstr.str().length();
2472                 }
2473         }
2474         // then all (real) multicolumn cells!
2475         for (int j = 0; j < columns_; ++j) {
2476                 for (int i = 0; i < rows_; ++i) {
2477                         int cell = GetCellNumber(i, j);
2478                         if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2479                                 continue;
2480                         ostringstream sstr;
2481                         GetCellInset(cell)->Ascii(buf, sstr, 0);
2482                         int len = int(sstr.str().length());
2483                         int const n = cells_in_multicolumn(cell);
2484                         for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2485                                 len -= clen[k];
2486                         if (len > int(clen[j + n - 1]))
2487                                 clen[j + n - 1] = len;
2488                 }
2489         }
2490         int cell = 0;
2491         for (int i = 0; i < rows_; ++i) {
2492                 AsciiTopHLine(os, i, clen);
2493                 for (int j = 0; j < columns_; ++j) {
2494                         if (IsPartOfMultiColumn(i,j))
2495                                 continue;
2496                         ret += AsciiPrintCell(buf, os, cell, i, j, clen);
2497                         ++cell;
2498                 }
2499                 os << endl;
2500                 AsciiBottomHLine(os, i, clen);
2501         }
2502         return ret;
2503 }
2504
2505
2506 InsetText * LyXTabular::GetCellInset(int cell) const
2507 {
2508         return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2509 }
2510
2511
2512 InsetText * LyXTabular::GetCellInset(int row, int column) const
2513 {
2514         return GetCellInset(GetCellNumber(row, column));
2515 }
2516
2517
2518 void LyXTabular::Validate(LaTeXFeatures & features) const
2519 {
2520         if (IsLongTabular())
2521                 features.longtable = true;
2522         if (NeedRotating())
2523                 features.rotating = true;
2524         for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
2525                 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2526                         features.array = true;
2527                 GetCellInset(cell)->Validate(features);
2528         }
2529 }
2530
2531
2532 std::vector<string> const LyXTabular::getLabelList() const
2533 {
2534         std::vector<string> label_list;
2535         for (int i = 0; i < rows_; ++i)
2536                 for (int j = 0; j < columns_; ++j) {
2537                         std::vector<string> const l =
2538                                 GetCellInset(i, j)->getLabelList();
2539                         label_list.insert(label_list.end(),
2540                                           l.begin(), l.end());
2541                 }
2542         return label_list;
2543 }
2544
2545                         
2546 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2547 {
2548         LyXParagraph * par = GetCellInset(cell)->par;
2549
2550         for (; par; par = par->next()) {
2551                 for (int i = 0; i < par->size(); ++i) {
2552                         if (par->GetChar(i)     == LyXParagraph::META_NEWLINE)
2553                                 return BOX_PARBOX;
2554                 }
2555         }
2556         return BOX_NONE;
2557 }
2558
2559 /* Emacs:
2560  * Local variables:
2561  * tab-width: 4
2562  * End:
2563  * vi:set tabstop=4:
2564  */