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