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