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