]> git.lyx.org Git - lyx.git/blob - src/tabular.h
typos
[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         void plaintext(Buffer const &, odocstream &,
321                        OutputParams const & runparams, int const depth,
322                        bool onlydata, unsigned char delim) const;
323         ///
324         bool isMultiColumn(idx_type cell) const;
325         ///
326         bool isMultiColumnReal(idx_type cell) const;
327         ///
328         void setMultiColumn(Buffer *, idx_type cell, idx_type number);
329         ///
330         idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
331         ///
332         bool isPartOfMultiColumn(row_type row, col_type column) const;
333         ///
334         row_type row_of_cell(idx_type cell) const;
335         ///
336         col_type column_of_cell(idx_type cell) const;
337         ///
338         col_type right_column_of_cell(idx_type cell) const;
339         ///
340         void setBookTabs(bool);
341         ///
342         bool useBookTabs() const;
343         ///
344         void setLongTabular(bool);
345         ///
346         bool isLongTabular() const;
347         ///
348         void setRotateTabular(bool);
349         ///
350         bool getRotateTabular() const;
351         ///
352         void setRotateCell(idx_type cell, bool);
353         ///
354         bool getRotateCell(idx_type cell) const;
355         ///
356         bool needRotating() const;
357         ///
358         bool isLastCell(idx_type cell) const;
359         ///
360         idx_type getCellAbove(idx_type cell) const;
361         ///
362         idx_type getCellBelow(idx_type cell) const;
363         ///
364         idx_type getLastCellAbove(idx_type cell) const;
365         ///
366         idx_type getLastCellBelow(idx_type cell) const;
367         ///
368         idx_type getCellNumber(row_type row, col_type column) const;
369         ///
370         void setUsebox(idx_type cell, BoxType);
371         ///
372         BoxType getUsebox(idx_type cell) const;
373         //
374         // Long Tabular Options support functions
375         ///
376         bool checkLTType(row_type row, ltType const &) const;
377         ///
378         void setLTHead(row_type row, bool flag, ltType const &, bool first);
379         ///
380         bool getRowOfLTHead(row_type row, ltType &) const;
381         ///
382         bool getRowOfLTFirstHead(row_type row, ltType &) const;
383         ///
384         void setLTFoot(row_type row, bool flag, ltType const &, bool last);
385         ///
386         bool getRowOfLTFoot(row_type row, ltType &) const;
387         ///
388         bool getRowOfLTLastFoot(row_type row, ltType &) const;
389         ///
390         void setLTNewPage(row_type row, bool what);
391         ///
392         bool getLTNewPage(row_type row) const;
393         ///
394         bool haveLTHead() const;
395         ///
396         bool haveLTFirstHead() const;
397         ///
398         bool haveLTFoot() const;
399         ///
400         bool haveLTLastFoot() const;
401         ///
402         // end longtable support
403         ///
404         boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
405         ///
406         boost::shared_ptr<InsetText> getCellInset(row_type row,
407                                                   col_type column) const;
408         ///
409         void setCellInset(row_type row, col_type column,
410                           boost::shared_ptr<InsetText>) const;
411         /// Search for \param inset in the tabular, with the
412         ///
413         idx_type getCellFromInset(InsetBase const * inset) const;
414         ///
415         row_type rows() const { return rows_; }
416         ///
417         col_type columns() const { return columns_;}
418         ///
419         void validate(LaTeXFeatures &) const;
420         ///
421 //private:
422         ///
423         class cellstruct {
424         public:
425                 ///
426                 cellstruct(BufferParams const &);
427                 ///
428                 cellstruct(cellstruct const &);
429                 ///
430                 cellstruct & operator=(cellstruct);
431                 ///
432                 void swap(cellstruct & rhs);
433                 ///
434                 idx_type cellno;
435                 ///
436                 int width_of_cell;
437                 ///
438                 int multicolumn;
439                 ///
440                 LyXAlignment alignment;
441                 ///
442                 VAlignment valignment;
443                 ///
444                 bool top_line;
445                 ///
446                 bool bottom_line;
447                 ///
448                 bool left_line;
449                 ///
450                 bool right_line;
451                 ///
452                 BoxType usebox;
453                 ///
454                 bool rotate;
455                 ///
456                 docstring align_special;
457                 ///
458                 LyXLength p_width; // this is only set for multicolumn!!!
459                 ///
460                 boost::shared_ptr<InsetText> inset;
461         };
462         cellstruct & cellinfo_of_cell(idx_type cell) const;
463         ///
464         typedef std::vector<cellstruct> cell_vector;
465         ///
466         typedef std::vector<cell_vector> cell_vvector;
467
468         ///
469         class rowstruct {
470         public:
471                 ///
472                 rowstruct();
473                 ///
474                 int ascent_of_row;
475                 ///
476                 int descent_of_row;
477                 ///
478                 bool top_line;
479                 ///
480                 bool bottom_line;
481                 /// Extra space between the top line and this row
482                 LyXLength top_space;
483                 /// Ignore top_space if true and use the default top space
484                 bool top_space_default;
485                 /// Extra space between this row and the bottom line
486                 LyXLength bottom_space;
487                 /// Ignore bottom_space if true and use the default bottom space
488                 bool bottom_space_default;
489                 /// Extra space between the bottom line and the next top line
490                 LyXLength interline_space;
491                 /// Ignore interline_space if true and use the default interline space
492                 bool interline_space_default;
493                 /// This are for longtabulars only
494                 /// a row of endhead
495                 bool endhead;
496                 /// a row of endfirsthead
497                 bool endfirsthead;
498                 /// a row of endfoot
499                 bool endfoot;
500                 /// row of endlastfoot
501                 bool endlastfoot;
502                 /// row for a pagebreak
503                 bool newpage;
504         };
505         ///
506         typedef std::vector<rowstruct> row_vector;
507
508         ///
509         class columnstruct {
510                 public:
511                 ///
512                 columnstruct();
513                 ///
514                 LyXAlignment alignment;
515                 ///
516                 VAlignment valignment;
517                 ///
518                 bool left_line;
519                 ///
520                 bool right_line;
521                 ///
522                 int  width_of_column;
523                 ///
524                 LyXLength p_width;
525                 ///
526                 docstring align_special;
527         };
528         ///
529         typedef std::vector<columnstruct> column_vector;
530
531         ///
532         row_type rows_;
533         ///
534         col_type columns_;
535         ///
536         idx_type numberofcells;
537         ///
538         std::vector<row_type> rowofcell;
539         ///
540         std::vector<col_type> columnofcell;
541         ///
542         row_vector row_info;
543         ///
544         column_vector column_info;
545         ///
546         mutable cell_vvector cell_info;
547         ///
548         int width_of_tabular;
549         ///
550         bool use_booktabs;
551         ///
552         bool rotate;
553         //
554         // for long tabulars
555         //
556         bool is_long_tabular;
557         /// endhead data
558         ltType endhead;
559         /// endfirsthead data
560         ltType endfirsthead;
561         /// endfoot data
562         ltType endfoot;
563         /// endlastfoot data
564         ltType endlastfoot;
565
566         ///
567         void init(BufferParams const &, row_type rows_arg,
568                   col_type columns_arg);
569         ///
570         void set_row_column_number_info();
571         /// Returns true if a complete update is necessary, otherwise false
572         bool setWidthOfMulticolCell(idx_type cell, int new_width);
573         ///
574         void recalculateMulticolumnsOfColumn(col_type column);
575         /// Returns true if change
576         void calculate_width_of_column(col_type column);
577         ///
578         bool calculate_width_of_column_NMC(col_type column); // no multi cells
579         ///
580         void calculate_width_of_tabular();
581         ///
582         void delete_column(col_type column);
583         ///
584         idx_type cells_in_multicolumn(idx_type cell) const;
585         ///
586         BoxType useParbox(idx_type cell) const;
587         ///
588         // helper function for Latex returns number of newlines
589         ///
590         int TeXTopHLine(odocstream &, row_type row) const;
591         ///
592         int TeXBottomHLine(odocstream &, row_type row) const;
593         ///
594         int TeXCellPreamble(odocstream &, idx_type cell) const;
595         ///
596         int TeXCellPostamble(odocstream &, idx_type cell) const;
597         ///
598         int TeXLongtableHeaderFooter(odocstream &, Buffer const & buf,
599                                      OutputParams const &) const;
600         ///
601         bool isValidRow(row_type const row) const;
602         ///
603         int TeXRow(odocstream &, row_type const row, Buffer const & buf,
604                    OutputParams const &) const;
605         ///
606         // helper functions for plain text
607         ///
608         bool plaintextTopHLine(odocstream &, row_type row,
609                                std::vector<unsigned int> const &) const;
610         ///
611         bool plaintextBottomHLine(odocstream &, row_type row,
612                                   std::vector<unsigned int> const &) const;
613         ///
614         void plaintextPrintCell(Buffer const &, odocstream &,
615                                 OutputParams const &,
616                                 idx_type cell, row_type row, col_type column,
617                                 std::vector<unsigned int> const &,
618                                 bool onlydata) const;
619         /// auxiliary function for docbook
620         int docbookRow(Buffer const & buf, odocstream & os, row_type,
621                        OutputParams const &) const;
622
623 private:
624         /// renumber cells after structural changes
625         void fixCellNums();
626 };
627
628
629 } // namespace lyx
630
631 #endif