]> git.lyx.org Git - lyx.git/blob - src/tabular.h
Store and use QImage rather than QPixmap.
[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 OutputParams;
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         /// Returns true if there is a topline, returns false if not
177         bool topLine(int cell, bool onlycolumn = false) const;
178         /// Returns true if there is a topline, returns false if not
179         bool bottomLine(int cell, bool onlycolumn = false) const;
180         /// Returns true if there is a topline, returns false if not
181         bool leftLine(int cell, bool onlycolumn = false) const;
182         /// Returns true if there is a topline, returns false if not
183         bool rightLine(int cell, bool onlycolumn = false) const;
184
185         ///
186         bool topAlreadyDrawn(int cell) const;
187         ///
188         bool leftAlreadyDrawn(int cell) const;
189         ///
190         bool isLastRow(int cell) const;
191
192         ///
193         int getAdditionalHeight(int row) const;
194         ///
195         int getAdditionalWidth(int cell) const;
196
197         /* returns the maximum over all rows */
198         ///
199         int getWidthOfColumn(int cell) const;
200         ///
201         int getWidthOfTabular() const;
202         ///
203         int getAscentOfRow(int row) const;
204         ///
205         int getDescentOfRow(int row) const;
206         ///
207         int getHeightOfTabular() const;
208         /// Returns true if a complete update is necessary, otherwise false
209         void setAscentOfRow(int row, int height);
210         /// Returns true if a complete update is necessary, otherwise false
211         void setDescentOfRow(int row, int height);
212         /// Returns true if a complete update is necessary, otherwise false
213         void setWidthOfCell(int cell, int new_width);
214         ///
215         void setAllLines(int cell, bool line);
216         ///
217         void setTopLine(int cell, bool line, bool onlycolumn = false);
218         ///
219         void setBottomLine(int cell, bool line, bool onlycolumn = false);
220         ///
221         void setLeftLine(int cell, bool line, bool onlycolumn = false);
222         ///
223         void setRightLine(int cell, bool line, bool onlycolumn = false);
224         ///
225         void setAlignment(int cell, LyXAlignment align,
226                           bool onlycolumn = false);
227         ///
228         void setVAlignment(int cell, VAlignment align,
229                            bool onlycolumn = false);
230         ///
231         void setColumnPWidth(int cell, LyXLength const & width);
232         ///
233         bool setMColumnPWidth(int cell, LyXLength const & width);
234         ///
235         void setAlignSpecial(int cell, std::string const & special, Feature what);
236         ///
237         LyXAlignment getAlignment(int cell, bool onlycolumn = false) const;
238         ///
239         VAlignment getVAlignment(int cell, bool onlycolumn = false) const;
240         ///
241         LyXLength const getPWidth(int cell) const;
242         ///
243         LyXLength const getColumnPWidth(int cell) const;
244         ///
245         LyXLength const getMColumnPWidth(int cell) const;
246         ///
247         std::string const getAlignSpecial(int cell, int what) const;
248         ///
249         int getWidthOfCell(int cell) const;
250         ///
251         int getBeginningOfTextInCell(int cell) const;
252         ///
253         void appendRow(BufferParams const &, int cell);
254         ///
255         void deleteRow(int row);
256         ///
257         void appendColumn(BufferParams const &, int cell);
258         ///
259         void deleteColumn(int column);
260         ///
261         bool isFirstCellInRow(int cell) const;
262         ///
263         int getFirstCellInRow(int row) const;
264         ///
265         bool isLastCellInRow(int cell) const;
266         ///
267         int getLastCellInRow(int row) const;
268         ///
269         int getNumberOfCells() const;
270         ///
271         int numberOfCellsInRow(int cell) const;
272         ///
273         void write(Buffer const &, std::ostream &) const;
274         ///
275         void read(Buffer const &, LyXLex &);
276         ///
277         int latex(Buffer const &, std::ostream &,
278                   OutputParams const &) const;
279         //
280         int linuxdoc(Buffer const & buf, std::ostream & os,
281                      OutputParams const &) const;
282         ///
283         int docbook(Buffer const & buf, std::ostream & os,
284                     OutputParams const &) const;
285         ///
286         int plaintext(Buffer const &, std::ostream &,
287                   OutputParams const & runparams,
288                   int const depth,
289                   bool onlydata, unsigned char delim) const;
290         ///
291         bool isMultiColumn(int cell) const;
292         ///
293         bool isMultiColumnReal(int cell) const;
294         ///
295         void setMultiColumn(Buffer *, int cell, int number);
296         ///
297         int unsetMultiColumn(int cell); // returns number of new cells
298         ///
299         bool isPartOfMultiColumn(int row, int column) const;
300         ///
301         int row_of_cell(int cell) const;
302         ///
303         int column_of_cell(int cell) const;
304         ///
305         int right_column_of_cell(int cell) const;
306         ///
307         void setLongTabular(bool);
308         ///
309         bool isLongTabular() const;
310         ///
311         void setRotateTabular(bool);
312         ///
313         bool getRotateTabular() const;
314         ///
315         void setRotateCell(int cell, bool);
316         ///
317         bool getRotateCell(int cell) const;
318         ///
319         bool needRotating() const;
320         ///
321         bool isLastCell(int cell) const;
322         ///
323         int getCellAbove(int cell) const;
324         ///
325         int getCellBelow(int cell) const;
326         ///
327         int getLastCellAbove(int cell) const;
328         ///
329         int getLastCellBelow(int cell) const;
330         ///
331         int getCellNumber(int row, int column) const;
332         ///
333         void setUsebox(int cell, BoxType);
334         ///
335         BoxType getUsebox(int cell) const;
336         //
337         // Long Tabular Options support functions
338         ///
339         bool checkLTType(int row, ltType const &) const;
340         ///
341         void setLTHead(int row, bool flag, ltType const &, bool first);
342         ///
343         bool getRowOfLTHead(int row, ltType &) const;
344         ///
345         bool getRowOfLTFirstHead(int row, ltType &) const;
346         ///
347         void setLTFoot(int row, bool flag, ltType const &, bool last);
348         ///
349         bool getRowOfLTFoot(int row, ltType &) const;
350         ///
351         bool getRowOfLTLastFoot(int row, ltType &) const;
352         ///
353         void setLTNewPage(int row, bool what);
354         ///
355         bool getLTNewPage(int row) const;
356         ///
357         bool haveLTHead() const;
358         ///
359         bool haveLTFirstHead() const;
360         ///
361         bool haveLTFoot() const;
362         ///
363         bool haveLTLastFoot() const;
364         ///
365         // end longtable support
366         ///
367         InsetText & getCellInset(int cell) const;
368         ///
369         InsetText & getCellInset(int row, int column) const;
370         /// Search for \param inset in the tabular, with the
371         ///
372         int getCellFromInset(InsetBase const * inset) const;
373         ///
374         int rows() const { return rows_; }
375         ///
376         int columns() const { return columns_;}
377         ///
378         void validate(LaTeXFeatures &) const;
379         /// Appends \c list with all labels found within this inset.
380         void getLabelList(Buffer const &, std::vector<std::string> & list) const;
381         ///
382 //private:
383         ///
384         struct cellstruct {
385                 ///
386                 cellstruct(BufferParams const &);
387                 ///
388                 int cellno;
389                 ///
390                 int width_of_cell;
391                 ///
392                 int multicolumn;
393                 ///
394                 LyXAlignment alignment;
395                 ///
396                 VAlignment valignment;
397                 ///
398                 bool top_line;
399                 ///
400                 bool bottom_line;
401                 ///
402                 bool left_line;
403                 ///
404                 bool right_line;
405                 ///
406                 BoxType usebox;
407                 ///
408                 bool rotate;
409                 ///
410                 std::string align_special;
411                 ///
412                 LyXLength p_width; // this is only set for multicolumn!!!
413                 ///
414                 InsetText inset;
415         };
416         cellstruct & cellinfo_of_cell(int cell) const;
417         ///
418         typedef std::vector<cellstruct> cell_vector;
419         ///
420         typedef std::vector<cell_vector> cell_vvector;
421
422         ///
423         struct rowstruct {
424                 ///
425                 rowstruct();
426                 ///
427                 int ascent_of_row;
428                 ///
429                 int descent_of_row;
430                 ///
431                 bool top_line;
432                 ///
433                 bool bottom_line;
434                 /// This are for longtabulars only
435                 /// a row of endhead
436                 bool endhead;
437                 /// a row of endfirsthead
438                 bool endfirsthead;
439                 /// a row of endfoot
440                 bool endfoot;
441                 /// row of endlastfoot
442                 bool endlastfoot;
443                 /// row for a pagebreak
444                 bool newpage;
445         };
446         ///
447         typedef std::vector<rowstruct> row_vector;
448
449         ///
450         struct columnstruct {
451                 ///
452                 columnstruct();
453                 ///
454                 LyXAlignment alignment;
455                 ///
456                 VAlignment valignment;
457                 ///
458                 bool left_line;
459                 ///
460                 bool right_line;
461                 ///
462                 int  width_of_column;
463                 ///
464                 LyXLength p_width;
465                 ///
466                 std::string align_special;
467         };
468         ///
469         typedef std::vector<columnstruct> column_vector;
470
471         ///
472         int rows_;
473         ///
474         int columns_;
475         ///
476         int numberofcells;
477         ///
478         std::vector<int> rowofcell;
479         ///
480         std::vector<int> columnofcell;
481         ///
482         row_vector row_info;
483         ///
484         column_vector column_info;
485         ///
486         mutable cell_vvector cell_info;
487         ///
488         int width_of_tabular;
489         ///
490         bool rotate;
491         //
492         // for long tabulars
493         //
494         bool is_long_tabular;
495         /// endhead data
496         ltType endhead;
497         /// endfirsthead data
498         ltType endfirsthead;
499         /// endfoot data
500         ltType endfoot;
501         /// endlastfoot data
502         ltType endlastfoot;
503
504         ///
505         void init(BufferParams const &, int rows_arg, int columns_arg);
506         ///
507         void set_row_column_number_info();
508         /// Returns true if a complete update is necessary, otherwise false
509         bool setWidthOfMulticolCell(int cell, int new_width);
510         ///
511         void recalculateMulticolumnsOfColumn(int column);
512         /// Returns true if change
513         void calculate_width_of_column(int column);
514         ///
515         bool calculate_width_of_column_NMC(int column); // no multi cells
516         ///
517         void calculate_width_of_tabular();
518         ///
519         void delete_column(int column);
520         ///
521         int cells_in_multicolumn(int cell) const;
522         ///
523         BoxType useParbox(int cell) const;
524         ///
525         void setHeaderFooterRows(int header, int fheader, int footer, int lfooter);
526         ///
527         // helper function for Latex returns number of newlines
528         ///
529         int TeXTopHLine(std::ostream &, int row) const;
530         ///
531         int TeXBottomHLine(std::ostream &, int row) const;
532         ///
533         int TeXCellPreamble(std::ostream &, int cell) const;
534         ///
535         int TeXCellPostamble(std::ostream &, int cell) const;
536         ///
537         int TeXLongtableHeaderFooter(std::ostream &, Buffer const & buf,
538                                      OutputParams const &) const;
539         ///
540         bool isValidRow(int const row) const;
541         ///
542         int TeXRow(std::ostream &, int const row, Buffer const & buf,
543                    OutputParams const &) const;
544         ///
545         // helper function for ASCII returns number of newlines
546         ///
547         int asciiTopHLine(std::ostream &, int row,
548                           std::vector<unsigned int> const &) const;
549         ///
550         int asciiBottomHLine(std::ostream &, int row,
551                              std::vector<unsigned int> const &) const;
552         ///
553         int asciiPrintCell(Buffer const &, std::ostream &,
554                            OutputParams const &,
555                            int cell, int row, int column,
556                            std::vector<unsigned int> const &,
557                                            bool onlydata) const;
558         /// auxiliary function for docbook
559         int docbookRow(Buffer const & buf, std::ostream & os, int,
560                        OutputParams const &) const;
561
562 private:
563         /// renumber cells after structural changes
564         void fixCellNums();
565 };
566
567 #endif