]> git.lyx.org Git - lyx.git/blob - src/tabular.h
ws changes only
[lyx.git] / src / tabular.h
1 // -*- C++ -*-
2 /**
3  * \file tabular.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Matthias Ettrich
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef TABULAR_H
16 #define TABULAR_H
17
18 #include "lyxlength.h"
19 #include "insets/insettext.h"
20
21 #include <iosfwd>
22 #include <vector>
23
24 class InsetTabular;
25 class LatexRunParams;
26
27 /* The features the text class offers for tables */
28
29 ///
30 class LyXTabular  {
31 public:
32         ///
33         enum Feature {
34                 ///
35                 APPEND_ROW = 0,
36                 ///
37                 APPEND_COLUMN,
38                 ///
39                 DELETE_ROW,
40                 ///
41                 DELETE_COLUMN,
42                 ///
43                 TOGGLE_LINE_TOP,
44                 ///
45                 TOGGLE_LINE_BOTTOM,
46                 ///
47                 TOGGLE_LINE_LEFT,
48                 ///
49                 TOGGLE_LINE_RIGHT,
50                 ///
51                 ALIGN_LEFT,
52                 ///
53                 ALIGN_RIGHT,
54                 ///
55                 ALIGN_CENTER,
56                 ///
57                 ALIGN_BLOCK,
58                 ///
59                 VALIGN_TOP,
60                 ///
61                 VALIGN_BOTTOM,
62                 ///
63                 VALIGN_MIDDLE,
64                 ///
65                 M_TOGGLE_LINE_TOP,
66                 ///
67                 M_TOGGLE_LINE_BOTTOM,
68                 ///
69                 M_TOGGLE_LINE_LEFT,
70                 ///
71                 M_TOGGLE_LINE_RIGHT,
72                 ///
73                 M_ALIGN_LEFT,
74                 ///
75                 M_ALIGN_RIGHT,
76                 ///
77                 M_ALIGN_CENTER,
78                 ///
79                 M_VALIGN_TOP,
80                 ///
81                 M_VALIGN_BOTTOM,
82                 ///
83                 M_VALIGN_MIDDLE,
84                 ///
85                 MULTICOLUMN,
86                 ///
87                 SET_ALL_LINES,
88                 ///
89                 UNSET_ALL_LINES,
90                 ///
91                 SET_LONGTABULAR,
92                 ///
93                 UNSET_LONGTABULAR,
94                 ///
95                 SET_PWIDTH,
96                 ///
97                 SET_MPWIDTH,
98                 ///
99                 SET_ROTATE_TABULAR,
100                 ///
101                 UNSET_ROTATE_TABULAR,
102                 ///
103                 SET_ROTATE_CELL,
104                 ///
105                 UNSET_ROTATE_CELL,
106                 ///
107                 SET_USEBOX,
108                 ///
109                 SET_LTHEAD,
110                 UNSET_LTHEAD,
111                 ///
112                 SET_LTFIRSTHEAD,
113                 UNSET_LTFIRSTHEAD,
114                 ///
115                 SET_LTFOOT,
116                 UNSET_LTFOOT,
117                 ///
118                 SET_LTLASTFOOT,
119                 UNSET_LTLASTFOOT,
120                 ///
121                 SET_LTNEWPAGE,
122                 ///
123                 SET_SPECIAL_COLUMN,
124                 ///
125                 SET_SPECIAL_MULTI,
126                 ///
127                 LAST_ACTION
128         };
129         ///
130         enum {
131                 ///
132                 CELL_NORMAL = 0,
133                 ///
134                 CELL_BEGIN_OF_MULTICOLUMN,
135                 ///
136                 CELL_PART_OF_MULTICOLUMN
137         };
138
139         ///
140         enum VAlignment {
141                 ///
142                 LYX_VALIGN_TOP = 0,
143                 ///
144                 LYX_VALIGN_BOTTOM = 1,
145                 ///
146                 LYX_VALIGN_MIDDLE = 2
147         };
148
149         enum BoxType {
150                 ///
151                 BOX_NONE = 0,
152                 ///
153                 BOX_PARBOX = 1,
154                 ///
155                 BOX_MINIPAGE = 2
156         };
157
158         struct ltType {
159                 // constructor
160                 ltType();
161                 // we have this header type (is set in the getLT... functions)
162                 bool set;
163                 // double borders on top
164                 bool topDL;
165                 // double borders on bottom
166                 bool bottomDL;
167                 // used for FirstHeader & LastFooter and if this is true
168                 // all the rows marked as FirstHeader or LastFooter are
169                 // ignored in the output and it is set to be empty!
170                 bool empty;
171         };
172
173         /// constructor
174         LyXTabular(BufferParams const &, int columns_arg, int rows_arg);
175         ///
176         void setOwner(InsetTabular * inset);
177
178         /// Returns true if there is a topline, returns false if not
179         bool topLine(int cell, bool onlycolumn = false) const;
180         /// Returns true if there is a topline, returns false if not
181         bool bottomLine(int cell, bool onlycolumn = false) const;
182         /// Returns true if there is a topline, returns false if not
183         bool leftLine(int cell, bool onlycolumn = false) const;
184         /// Returns true if there is a topline, returns false if not
185         bool rightLine(int cell, bool onlycolumn = false) const;
186
187         ///
188         bool topAlreadyDrawn(int cell) const;
189         ///
190         bool leftAlreadyDrawn(int cell) const;
191         ///
192         bool isLastRow(int cell) const;
193
194         ///
195         int getAdditionalHeight(int row) const;
196         ///
197         int getAdditionalWidth(int cell) const;
198
199         /* returns the maximum over all rows */
200         ///
201         int getWidthOfColumn(int cell) const;
202         ///
203         int getWidthOfTabular() const;
204         ///
205         int getAscentOfRow(int row) const;
206         ///
207         int getDescentOfRow(int row) const;
208         ///
209         int getHeightOfTabular() const;
210         /// Returns true if a complete update is necessary, otherwise false
211         void setAscentOfRow(int row, int height);
212         /// Returns true if a complete update is necessary, otherwise false
213         void setDescentOfRow(int row, int height);
214         /// Returns true if a complete update is necessary, otherwise false
215         void setWidthOfCell(int cell, int new_width);
216         ///
217         void setAllLines(int cell, bool line);
218         ///
219         void setTopLine(int cell, bool line, bool onlycolumn = false);
220         ///
221         void setBottomLine(int cell, bool line, bool onlycolumn = false);
222         ///
223         void setLeftLine(int cell, bool line, bool onlycolumn = false);
224         ///
225         void setRightLine(int cell, bool line, bool onlycolumn = false);
226         ///
227         void setAlignment(int cell, LyXAlignment align,
228                           bool onlycolumn = false);
229         ///
230         void setVAlignment(int cell, VAlignment align,
231                            bool onlycolumn = false);
232         ///
233         void setColumnPWidth(int cell, LyXLength const & width);
234         ///
235         bool setMColumnPWidth(int cell, LyXLength const & width);
236         ///
237         void setAlignSpecial(int cell, std::string const & special, Feature what);
238         ///
239         LyXAlignment getAlignment(int cell, bool onlycolumn = false) const;
240         ///
241         VAlignment getVAlignment(int cell, bool onlycolumn = false) const;
242         ///
243         LyXLength const getPWidth(int cell) const;
244         ///
245         LyXLength const getColumnPWidth(int cell) const;
246         ///
247         LyXLength const getMColumnPWidth(int cell) const;
248         ///
249         std::string const getAlignSpecial(int cell, int what) const;
250         ///
251         int getWidthOfCell(int cell) const;
252         ///
253         int getBeginningOfTextInCell(int cell) const;
254         ///
255         void appendRow(BufferParams const &, int cell);
256         ///
257         void deleteRow(int row);
258         ///
259         void appendColumn(BufferParams const &, int cell);
260         ///
261         void deleteColumn(int column);
262         ///
263         bool isFirstCellInRow(int cell) const;
264         ///
265         int getFirstCellInRow(int row) const;
266         ///
267         bool isLastCellInRow(int cell) const;
268         ///
269         int getLastCellInRow(int row) const;
270         ///
271         int getNumberOfCells() const;
272         ///
273         int numberOfCellsInRow(int cell) const;
274         ///
275         void write(Buffer const &, std::ostream &) const;
276         ///
277         void read(Buffer const &, LyXLex &);
278         ///
279         int latex(Buffer const &, std::ostream &,
280                   LatexRunParams const &) const;
281         //
282         int linuxdoc(Buffer const & buf, std::ostream & os) const;
283         ///
284         int docbook(Buffer const & buf, std::ostream & os, bool mixcont) const;
285         ///
286         int ascii(Buffer const &, std::ostream &, int const depth,
287                   bool onlydata, unsigned char delim) const;
288         ///
289         bool isMultiColumn(int cell) const;
290         ///
291         bool isMultiColumnReal(int cell) const;
292         ///
293         void setMultiColumn(Buffer *, int cell, int number);
294         ///
295         int unsetMultiColumn(int cell); // returns number of new cells
296         ///
297         bool isPartOfMultiColumn(int row, int column) const;
298         ///
299         int row_of_cell(int cell) const;
300         ///
301         int column_of_cell(int cell) const;
302         ///
303         int right_column_of_cell(int cell) const;
304         ///
305         void setLongTabular(bool);
306         ///
307         bool isLongTabular() const;
308         ///
309         void setRotateTabular(bool);
310         ///
311         bool getRotateTabular() const;
312         ///
313         void setRotateCell(int cell, bool);
314         ///
315         bool getRotateCell(int cell) const;
316         ///
317         bool needRotating() const;
318         ///
319         bool isLastCell(int cell) const;
320         ///
321         int getCellAbove(int cell) const;
322         ///
323         int getCellBelow(int cell) const;
324         ///
325         int getLastCellAbove(int cell) const;
326         ///
327         int getLastCellBelow(int cell) const;
328         ///
329         int getCellNumber(int row, int column) const;
330         ///
331         void setUsebox(int cell, BoxType);
332         ///
333         BoxType getUsebox(int cell) const;
334         //
335         // Long Tabular Options support functions
336         ///
337         bool checkLTType(int row, ltType const &) const;
338         ///
339         void setLTHead(int row, bool flag, ltType const &, bool first);
340         ///
341         bool getRowOfLTHead(int row, ltType &) const;
342         ///
343         bool getRowOfLTFirstHead(int row, ltType &) const;
344         ///
345         void setLTFoot(int row, bool flag, ltType const &, bool last);
346         ///
347         bool getRowOfLTFoot(int row, ltType &) const;
348         ///
349         bool getRowOfLTLastFoot(int row, ltType &) const;
350         ///
351         void setLTNewPage(int row, bool what);
352         ///
353         bool getLTNewPage(int row) const;
354         ///
355         bool haveLTHead() const;
356         ///
357         bool haveLTFirstHead() const;
358         ///
359         bool haveLTFoot() const;
360         ///
361         bool haveLTLastFoot() const;
362         ///
363         // end longtable support
364         ///
365         InsetText & getCellInset(int cell) const;
366         ///
367         InsetText & getCellInset(int row, int column) const;
368         /// Search for \param inset in the tabular, with the
369         /// 
370         int getCellFromInset(InsetOld const * inset) const;
371         ///
372         int rows() const { return rows_; }
373         ///
374         int columns() const { return columns_;}
375         ///
376         void validate(LaTeXFeatures &) const;
377         /// Appends \c list with all labels found within this inset.
378         void getLabelList(Buffer const &, std::vector<std::string> & list) const;
379         ///
380 //private:
381         ///
382         struct cellstruct {
383                 ///
384                 cellstruct(BufferParams const &);
385                 ///
386                 int cellno;
387                 ///
388                 int width_of_cell;
389                 ///
390                 int multicolumn;
391                 ///
392                 LyXAlignment alignment;
393                 ///
394                 VAlignment valignment;
395                 ///
396                 bool top_line;
397                 ///
398                 bool bottom_line;
399                 ///
400                 bool left_line;
401                 ///
402                 bool right_line;
403                 ///
404                 BoxType usebox;
405                 ///
406                 bool rotate;
407                 ///
408                 std::string align_special;
409                 ///
410                 LyXLength p_width; // this is only set for multicolumn!!!
411                 ///
412                 InsetText inset;
413         };
414         cellstruct & cellinfo_of_cell(int cell) const;
415         ///
416         typedef std::vector<cellstruct> cell_vector;
417         ///
418         typedef std::vector<cell_vector> cell_vvector;
419
420         ///
421         struct rowstruct {
422                 ///
423                 rowstruct();
424                 ///
425                 int ascent_of_row;
426                 ///
427                 int descent_of_row;
428                 ///
429                 bool top_line;
430                 ///
431                 bool bottom_line;
432                 /// This are for longtabulars only
433                 /// a row of endhead
434                 bool endhead;
435                 /// a row of endfirsthead
436                 bool endfirsthead;
437                 /// a row of endfoot
438                 bool endfoot;
439                 /// row of endlastfoot
440                 bool endlastfoot;
441                 /// row for a pagebreak
442                 bool newpage;
443         };
444         ///
445         typedef std::vector<rowstruct> row_vector;
446
447         ///
448         struct columnstruct {
449                 ///
450                 columnstruct();
451                 ///
452                 LyXAlignment alignment;
453                 ///
454                 VAlignment valignment;
455                 ///
456                 bool left_line;
457                 ///
458                 bool right_line;
459                 ///
460                 int  width_of_column;
461                 ///
462                 LyXLength p_width;
463                 ///
464                 std::string align_special;
465         };
466         ///
467         typedef std::vector<columnstruct> column_vector;
468
469         ///
470         int rows_;
471         ///
472         int columns_;
473         ///
474         int numberofcells;
475         ///
476         std::vector<int> rowofcell;
477         ///
478         std::vector<int> columnofcell;
479         ///
480         row_vector row_info;
481         ///
482         column_vector column_info;
483         ///
484         mutable cell_vvector cell_info;
485         ///
486         int width_of_tabular;
487         ///
488         bool rotate;
489         //
490         // for long tabulars
491         //
492         bool is_long_tabular;
493         /// endhead data
494         ltType endhead;
495         /// endfirsthead data
496         ltType endfirsthead;
497         /// endfoot data
498         ltType endfoot;
499         /// endlastfoot data
500         ltType endlastfoot;
501
502         ///
503         void init(BufferParams const &, int rows_arg, int columns_arg);
504         ///
505         void set_row_column_number_info();
506         /// Returns true if a complete update is necessary, otherwise false
507         bool setWidthOfMulticolCell(int cell, int new_width);
508         ///
509         void recalculateMulticolumnsOfColumn(int column);
510         /// Returns true if change
511         void calculate_width_of_column(int column);
512         ///
513         bool calculate_width_of_column_NMC(int column); // no multi cells
514         ///
515         void calculate_width_of_tabular();
516         ///
517         void delete_column(int column);
518         ///
519         int cells_in_multicolumn(int cell) const;
520         ///
521         BoxType useParbox(int cell) const;
522         ///
523         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
524         ///
525         // helper function for Latex returns number of newlines
526         ///
527         int TeXTopHLine(std::ostream &, int row) const;
528         ///
529         int TeXBottomHLine(std::ostream &, int row) const;
530         ///
531         int TeXCellPreamble(std::ostream &, int cell) const;
532         ///
533         int TeXCellPostamble(std::ostream &, int cell) const;
534         ///
535         int TeXLongtableHeaderFooter(std::ostream &, Buffer const & buf,
536                                      LatexRunParams const &) const;
537         ///
538         bool isValidRow(int const row) const;
539         ///
540         int TeXRow(std::ostream &, int const row, Buffer const & buf,
541                    LatexRunParams const &) const;
542         ///
543         // helper function for ASCII returns number of newlines
544         ///
545         int asciiTopHLine(std::ostream &, int row,
546                           std::vector<unsigned int> const &) const;
547         ///
548         int asciiBottomHLine(std::ostream &, int row,
549                              std::vector<unsigned int> const &) const;
550         ///
551         int asciiPrintCell(Buffer const &, std::ostream &,
552                            int cell, int row, int column,
553                            std::vector<unsigned int> const &,
554                                            bool onlydata) const;
555         /// auxiliary function for docbook
556         int docbookRow(Buffer const & buf, std::ostream & os, int row) const;
557
558 private:
559         /// renumber cells after structural changes
560         void fixCellNums();
561 };
562
563 #endif