]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTabular.h
Add support for CALS tables in DocBook.
[lyx.git] / src / insets / InsetTabular.h
1 // -*- C++ -*-
2 /**
3  * \file InsetTabular.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  * \author Edwin Leuven
12  * \author Uwe Stöhr
13  * \author Scott Kostyshak
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 // Things to think of when designing the new tabular support:
19 // - color support (colortbl, color)
20 // - decimal alignment (dcloumn)
21 // - custom lines (hhline)
22 // - column styles
23
24 #ifndef INSET_TABULAR_H
25 #define INSET_TABULAR_H
26
27 #include "InsetText.h"
28 #include "Length.h"
29
30 #include <climits>
31 #include <iosfwd>
32 #include <memory>
33 #include <vector>
34
35
36 namespace lyx {
37
38 class Buffer;
39 class BufferView;
40 class CompletionList;
41 class Cursor;
42 class CursorSlice;
43 class FuncStatus;
44 class Lexer;
45 class OutputParams;
46 class Paragraph;
47 class XMLStream;
48
49
50 ///
51 class InsetTableCell : public InsetText
52 {
53 public:
54         ///
55         explicit InsetTableCell(Buffer * buf);
56         ///
57         InsetCode lyxCode() const { return CELL_CODE; }
58         ///
59         Inset * clone() const { return new InsetTableCell(*this); }
60         ///
61         bool getStatus(Cursor & cur, FuncRequest const & cmd,
62                 FuncStatus & status) const;
63         ///
64         void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
65         ///
66         void toggleMultiCol(bool m) { isMultiColumn = m; }
67         ///
68         void toggleMultiRow(bool m) { isMultiRow = m; }
69         ///
70         void setContentAlignment(LyXAlignment al) {contentAlign = al; }
71         /// writes the contents of the cell as a string, optionally
72         /// descending into insets
73         docstring asString(bool intoInsets = true);
74         ///
75         docstring xhtml(XMLStream &, OutputParams const &) const;
76         ///
77         void docbook(XMLStream &, OutputParams const &) const;
78         ///
79         void addToToc(DocIterator const & di, bool output_active,
80                                   UpdateType utype, TocBackend & backend) const;
81         ///
82         void metrics(MetricsInfo &, Dimension &) const;
83 private:
84         /// unimplemented
85         InsetTableCell();
86         /// unimplemented
87         void operator=(InsetTableCell const &);
88         // FIXME
89         // These booleans are supposed to track whether the cell has had its
90         // width explicitly set and whether it is part of a multicolumn, respectively.
91         // We need to know this to determine whether
92         // layout changes and paragraph customization are allowed---that is,
93         // we need it in forcePlainLayout() and allowParagraphCustomization().
94         // Unfortunately, that information is not readily available in
95         // InsetTableCell. In the case of multicolumn cells, it is present
96         // in CellData, and so would be available here if CellData were to
97         // become a member of InsetTableCell. But in the other case, it isn't
98         // even available there, but is held in Tabular::ColumnData.
99         // So, the present solution uses this boolean to track the information
100         // we need to track, and tries to keep it updated. This is not ideal,
101         // but the other solutions are no better. These are:
102         // (i)  Keep a pointer in InsetTableCell to the table;
103         // (ii) Find the table by iterating over the Buffer's insets.
104         // Solution (i) raises the problem of updating the pointer when an
105         // InsetTableCell is copied, and we'd therefore need a copy constructor
106         // in InsetTabular and then in Tabular, which seems messy, given how
107         // complicated those classes are. Solution (ii) involves a lot of
108         // iterating, since this information is needed quite often, and so may
109         // be quite slow.
110         // So, well, if someone can do better, please do!
111         // --rgh
112         ///
113         bool isFixedWidth;
114         ///
115         bool isMultiColumn;
116         ///
117         bool isMultiRow;
118         // FIXME: Here the thoughts from the comment above also apply.
119         ///
120         LyXAlignment contentAlign;
121         /// should paragraph indentation be omitted in any case?
122         bool neverIndent() const { return true; }
123         ///
124         LyXAlignment contentAlignment() const { return contentAlign; }
125         ///
126         virtual bool usePlainLayout() const { return true; }
127         ///
128         virtual bool forcePlainLayout(idx_type = 0) const;
129         ///
130         virtual bool allowParagraphCustomization(idx_type = 0) const;
131         ///
132         virtual bool forceLocalFontSwitch() const;
133         /// Is the width forced to some value?
134         bool hasFixedWidth() const { return isFixedWidth; }
135         /// Can the cell contain several paragraphs?
136         bool allowMultiPar() const { return !isMultiRow && (!isMultiColumn || isFixedWidth); }
137 };
138
139
140 //
141 // A helper struct for tables
142 //
143 class Tabular {
144 public:
145         ///
146         enum Feature {
147                 ///
148                 APPEND_ROW = 0,
149                 ///
150                 APPEND_COLUMN,
151                 ///
152                 DELETE_ROW,
153                 ///
154                 DELETE_COLUMN,
155                 ///
156                 COPY_ROW,
157                 ///
158                 COPY_COLUMN,
159                 ///
160                 MOVE_COLUMN_RIGHT,
161                 ///
162                 MOVE_COLUMN_LEFT,
163                 ///
164                 MOVE_ROW_DOWN,
165                 ///
166                 MOVE_ROW_UP,
167                 ///
168                 SET_LINE_TOP,
169                 ///
170                 SET_LINE_BOTTOM,
171                 ///
172                 SET_LINE_LEFT,
173                 ///
174                 SET_LINE_RIGHT,
175                 ///FIXME: remove
176                 TOGGLE_LINE_TOP,
177                 ///FIXME: remove
178                 TOGGLE_LINE_BOTTOM,
179                 ///FIXME: remove
180                 TOGGLE_LINE_LEFT,
181                 ///FIXME: remove
182                 TOGGLE_LINE_RIGHT,
183                 ///
184                 SET_LTRIM_TOP,
185                 ///
186                 SET_RTRIM_TOP,
187                 ///
188                 SET_LTRIM_BOTTOM,
189                 ///
190                 SET_RTRIM_BOTTOM,
191                 ///
192                 TOGGLE_LTRIM_TOP,
193                 ///
194                 TOGGLE_RTRIM_TOP,
195                 ///
196                 TOGGLE_LTRIM_BOTTOM,
197                 ///
198                 TOGGLE_RTRIM_BOTTOM,
199                 ///
200                 ALIGN_LEFT,
201                 ///
202                 ALIGN_RIGHT,
203                 ///
204                 ALIGN_CENTER,
205                 ///
206                 ALIGN_BLOCK,
207                 ///
208                 ALIGN_DECIMAL,
209                 ///
210                 VALIGN_TOP,
211                 ///
212                 VALIGN_BOTTOM,
213                 ///
214                 VALIGN_MIDDLE,
215                 ///
216                 M_ALIGN_LEFT,
217                 ///
218                 M_ALIGN_RIGHT,
219                 ///
220                 M_ALIGN_CENTER,
221                 ///
222                 M_VALIGN_TOP,
223                 ///
224                 M_VALIGN_BOTTOM,
225                 ///
226                 M_VALIGN_MIDDLE,
227                 ///
228                 MULTICOLUMN,
229                 ///
230                 SET_MULTICOLUMN,
231                 ///
232                 UNSET_MULTICOLUMN,
233                 ///
234                 MULTIROW,
235                 ///
236                 SET_MULTIROW,
237                 ///
238                 UNSET_MULTIROW,
239                 ///
240                 SET_MROFFSET,
241                 ///
242                 SET_ALL_LINES,
243                 ///
244                 RESET_FORMAL_DEFAULT,
245                 ///
246                 UNSET_ALL_LINES,
247                 ///
248                 TOGGLE_LONGTABULAR,
249                 ///
250                 SET_LONGTABULAR,
251                 ///
252                 UNSET_LONGTABULAR,
253                 ///
254                 SET_PWIDTH,
255                 ///
256                 SET_MPWIDTH,
257                 ///
258                 TOGGLE_VARWIDTH_COLUMN,
259                 ///
260                 SET_ROTATE_TABULAR,
261                 ///
262                 UNSET_ROTATE_TABULAR,
263                 ///
264                 TOGGLE_ROTATE_TABULAR,
265                 ///
266                 SET_ROTATE_CELL,
267                 ///
268                 UNSET_ROTATE_CELL,
269                 ///
270                 TOGGLE_ROTATE_CELL,
271                 ///
272                 SET_USEBOX,
273                 ///
274                 SET_LTHEAD,
275                 UNSET_LTHEAD,
276                 ///
277                 SET_LTFIRSTHEAD,
278                 UNSET_LTFIRSTHEAD,
279                 ///
280                 SET_LTFOOT,
281                 UNSET_LTFOOT,
282                 ///
283                 SET_LTLASTFOOT,
284                 UNSET_LTLASTFOOT,
285                 ///
286                 SET_LTNEWPAGE,
287                 UNSET_LTNEWPAGE,
288                 ///
289                 TOGGLE_LTCAPTION,
290                 ///
291                 SET_LTCAPTION,
292                 ///
293                 UNSET_LTCAPTION,
294                 ///
295                 SET_SPECIAL_COLUMN,
296                 ///
297                 SET_SPECIAL_MULTICOLUMN,
298                 ///
299                 TOGGLE_BOOKTABS,
300                 ///
301                 SET_BOOKTABS,
302                 ///
303                 UNSET_BOOKTABS,
304                 ///
305                 SET_TOP_SPACE,
306                 ///
307                 SET_BOTTOM_SPACE,
308                 ///
309                 SET_INTERLINE_SPACE,
310                 ///
311                 SET_BORDER_LINES,
312                 ///
313                 TABULAR_VALIGN_TOP,
314                 ///
315                 TABULAR_VALIGN_MIDDLE,
316                 ///
317                 TABULAR_VALIGN_BOTTOM,
318                 ///
319                 LONGTABULAR_ALIGN_LEFT,
320                 ///
321                 LONGTABULAR_ALIGN_CENTER,
322                 ///
323                 LONGTABULAR_ALIGN_RIGHT,
324                 ///
325                 SET_DECIMAL_POINT,
326                 ///
327                 SET_TABULAR_WIDTH,
328                 ///
329                 SET_INNER_LINES,
330                 ///
331                 LAST_ACTION
332         };
333         ///
334         enum {
335                 ///
336                 CELL_NORMAL = 0,
337                 ///
338                 CELL_BEGIN_OF_MULTICOLUMN,
339                 ///
340                 CELL_PART_OF_MULTICOLUMN,
341                 ///
342                 CELL_BEGIN_OF_MULTIROW,
343                 ///
344                 CELL_PART_OF_MULTIROW
345         };
346
347         ///
348         enum VAlignment {
349                 ///
350                 LYX_VALIGN_TOP = 0,
351                 ///
352                 LYX_VALIGN_MIDDLE = 1,
353                 ///
354                 LYX_VALIGN_BOTTOM = 2
355
356         };
357         ///
358         enum HAlignment {
359                 ///
360                 LYX_LONGTABULAR_ALIGN_LEFT = 0,
361                 ///
362                 LYX_LONGTABULAR_ALIGN_CENTER = 1,
363                 ///
364                 LYX_LONGTABULAR_ALIGN_RIGHT = 2
365         };
366
367         enum BoxType {
368                 ///
369                 BOX_NONE = 0,
370                 ///
371                 BOX_PARBOX = 1,
372                 ///
373                 BOX_MINIPAGE = 2,
374                 ///
375                 BOX_VARWIDTH = 3
376         };
377
378         enum CaptionType {
379                 ///
380                 CAPTION_FIRSTHEAD,
381                 ///
382                 CAPTION_HEAD,
383                 ///
384                 CAPTION_FOOT,
385                 ///
386                 CAPTION_LASTFOOT,
387                 ///
388                 CAPTION_ANY
389         };
390
391         enum RowDirection {
392                 UP,
393                 DOWN
394         };
395
396         enum ColDirection {
397                 RIGHT,
398                 LEFT
399         };
400
401         class ltType {
402         public:
403                 // constructor
404                 ltType();
405                 // we have this header type (is set in the getLT... functions)
406                 bool set;
407                 // double borders on top
408                 bool topDL;
409                 // double borders on bottom
410                 bool bottomDL;
411                 // used for FirstHeader & LastFooter and if this is true
412                 // all the rows marked as FirstHeader or LastFooter are
413                 // ignored in the output and it is set to be empty!
414                 bool empty;
415         };
416
417         /// type for row numbers
418         typedef size_t row_type;
419         /// type for column numbers
420         typedef size_t col_type;
421         /// type for cell indices
422         typedef size_t idx_type;
423         /// index indicating an invalid position
424         static const idx_type npos = static_cast<idx_type>(-1);
425
426         /// constructor
427         Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
428
429         /// Returns true if there is a topline, returns false if not
430         bool topLine(idx_type cell) const;
431         /// Returns true if there is a topline, returns false if not
432         bool bottomLine(idx_type cell) const;
433         /// Returns true if there is a topline, returns false if not
434         /// If \p ignore_bt is true, we return the state as if booktabs was
435         /// not used
436         bool leftLine(idx_type cell, bool const ignore_bt = false) const;
437         /// Returns true if there is a topline, returns false if not
438         /// If \p ignore_bt is true, we return the state as if booktabs was
439         /// not used
440         bool rightLine(idx_type cell, bool const ignore_bt = false) const;
441         /// Returns whether the top line is trimmed left and/or right
442         std::pair<bool, bool> topLineTrim(idx_type const cell) const;
443         /// Returns whether the bottom line is trimmed left and/or right
444         std::pair<bool, bool> bottomLineTrim(idx_type const cell) const;
445
446         /// return space occupied by the second horizontal line and
447         /// interline space above row \p row in pixels
448         int interRowSpace(row_type row) const;
449         ///
450         int interColumnSpace(idx_type cell) const;
451
452         /* returns the maximum over all rows */
453         ///
454         int cellWidth(idx_type cell) const;
455         ///
456         int cellHeight(idx_type cell) const;
457         ///
458         int width() const;
459         ///
460         int height() const;
461         ///
462         row_type nrows() const {return row_info.size();}
463         ///
464         col_type ncols() const {return column_info.size();}
465         ///
466         int rowAscent(row_type row) const;
467         ///
468         int rowDescent(row_type row) const;
469         ///
470         void setRowAscent(row_type row, int height);
471         ///
472         void setRowDescent(row_type row, int height);
473         ///
474         void setTopLine(idx_type cell, bool line);
475         ///
476         void setBottomLine(idx_type cell, bool line);
477         ///
478         void setTopLineLTrim(idx_type cell, bool val);
479         ///
480         void setBottomLineLTrim(idx_type cell, bool val);
481         ///
482         void setTopLineRTrim(idx_type cell, bool val);
483         ///
484         void setBottomLineRTrim(idx_type cell, bool val);
485         ///
486         void setTopLineTrim(idx_type cell, std::pair<bool, bool>);
487         ///
488         void setBottomLineTrim(idx_type cell, std::pair<bool, bool>);
489         ///
490         void setLeftLine(idx_type cell, bool line);
491         ///
492         void setRightLine(idx_type cell, bool line);
493         ///
494         bool rowTopLine(row_type row) const;
495         ///
496         bool rowBottomLine(row_type row) const;
497         ///
498         bool columnLeftLine(col_type column) const;
499         ///
500         bool columnRightLine(col_type column) const;
501
502         void setAlignment(idx_type cell, LyXAlignment align,
503                           bool onlycolumn = false);
504         ///
505         void setVAlignment(idx_type cell, VAlignment align,
506                            bool onlycolumn = false);
507         ///
508         void setTabularWidth(Length const & l) { tabular_width = l; }
509         ///
510         Length tabularWidth() const { return tabular_width; }
511         ///
512         void setColumnPWidth(Cursor &, idx_type, Length const &);
513         ///
514         bool setMColumnPWidth(Cursor &, idx_type, Length const &);
515         ///
516         bool toggleVarwidth(idx_type, bool const);
517         ///
518         bool setMROffset(Cursor &, idx_type, Length const &);
519         ///
520         void setAlignSpecial(idx_type cell, docstring const & special,
521                              Feature what);
522         ///
523         LyXAlignment getAlignment(idx_type cell,
524                                   bool onlycolumn = false) const;
525         ///
526         VAlignment getVAlignment(idx_type cell,
527                                  bool onlycolumn = false) const;
528         /// The vertical offset of the table due to the vertical
529         /// alignment with respect to the baseline.
530         int offsetVAlignment() const;
531         ///
532         Length const getPWidth(idx_type cell) const;
533         ///
534         Length const getMROffset(idx_type cell) const;
535         ///
536         int textHOffset(idx_type cell) const;
537         ///
538         int textVOffset(idx_type cell) const;
539         ///
540         void appendRow(row_type row);
541         ///
542         void deleteRow(row_type row, bool const force = false);
543         ///
544         void copyRow(row_type row);
545         ///
546         void insertRow(row_type row, bool copy);
547         ///
548         void moveColumn(col_type col, ColDirection direction);
549         ///
550         void moveRow(row_type row, RowDirection direction);
551         ///
552         void appendColumn(col_type column);
553         ///
554         void deleteColumn(col_type column, bool const force = false);
555         ///
556         void copyColumn(col_type column);
557         ///
558         void insertColumn(col_type column, bool copy);
559         ///
560         idx_type getFirstCellInRow(row_type row, bool const ct = false) const;
561         ///
562         idx_type getLastCellInRow(row_type row, bool const ct = false) const;
563         ///
564         idx_type getFirstRow(bool const ct = false) const;
565         ///
566         idx_type getLastRow(bool const ct = false) const;
567         ///
568         idx_type numberOfCellsInRow(row_type row) const;
569         ///
570         void write(std::ostream &) const;
571         ///
572         void read(Lexer &);
573         ///
574         void latex(otexstream &, OutputParams const &) const;
575         /// serialise the table in DocBook, according to buffer parameters
576         void docbook(XMLStream &, OutputParams const &) const;
577         ///
578         docstring xhtml(XMLStream &, OutputParams const &) const;
579         ///
580         void plaintext(odocstringstream &,
581                        OutputParams const & runparams, int const depth,
582                        bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
583         ///
584         bool isMultiColumn(idx_type cell) const;
585         ///
586         bool hasMultiColumn(col_type cell) const;
587         ///
588         bool hasVarwidthColumn() const;
589         ///
590         bool isVTypeColumn(col_type cell) const;
591         ///
592         idx_type setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
593                              bool const right_border);
594         ///
595         void unsetMultiColumn(idx_type cell);
596         ///
597         bool isPartOfMultiColumn(row_type row, col_type column) const;
598         ///
599         bool isPartOfMultiRow(row_type row, col_type column) const;
600         ///
601         bool isMultiRow(idx_type cell) const;
602         ///
603         bool hasMultiRow(row_type r) const;
604         ///
605         idx_type setMultiRow(Cursor & cur, idx_type cell, idx_type number,
606                              bool const bottom_border,
607                              LyXAlignment const halign);
608         ///
609         void unsetMultiRow(idx_type cell);
610         ///
611         row_type cellRow(idx_type cell) const;
612         ///
613         col_type cellColumn(idx_type cell) const;
614         ///
615         void setRotateCell(idx_type cell, int);
616         ///
617         int getRotateCell(idx_type cell) const;
618         ///
619         bool needRotating() const;
620         ///
621         bool isLastCell(idx_type cell) const;
622         ///
623         idx_type cellAbove(idx_type cell) const;
624         ///
625         idx_type cellBelow(idx_type cell) const;
626         /// \return the index of the VISIBLE cell at row, column
627         /// this will be the same as the cell in the previous row,
628         /// e.g., if the cell is part of a multirow
629         idx_type cellIndex(row_type row, col_type column) const;
630         ///
631         void setUsebox(idx_type cell, BoxType);
632         ///
633         BoxType getUsebox(idx_type cell) const;
634         //
635         // Long Tabular Options support functions
636         ///
637         void setLTHead(row_type row, bool flag, ltType const &, bool first);
638         ///
639         bool getRowOfLTHead(row_type row, ltType &) const;
640         ///
641         bool getRowOfLTFirstHead(row_type row, ltType &) const;
642         ///
643         void setLTFoot(row_type row, bool flag, ltType const &, bool last);
644         ///
645         bool getRowOfLTFoot(row_type row, ltType &) const;
646         ///
647         bool getRowOfLTLastFoot(row_type row, ltType &) const;
648         ///
649         void setLTNewPage(row_type row, bool what);
650         ///
651         bool getLTNewPage(row_type row) const;
652         ///
653         idx_type setLTCaption(Cursor & cur, row_type row, bool what);
654         ///
655         bool ltCaption(row_type row) const;
656         ///
657         bool haveLTHead(bool withcaptions = true) const;
658         ///
659         bool haveLTFirstHead(bool withcaptions = true) const;
660         ///
661         bool haveLTFoot(bool withcaptions = true) const;
662         ///
663         bool haveLTLastFoot(bool withcaptions = true) const;
664         ///
665         bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
666         ///
667         // end longtable support
668
669         //@{
670         /// there is a subtle difference between these two methods.
671         ///   cellInset(r,c);
672         /// and
673         ///   cellInset(cellIndex(r,c));
674         /// can return different things. this is because cellIndex(r,c)
675         /// returns the VISIBLE cell at r,c, which may be the same as the
676         /// cell at the previous row or column, if we're dealing with some
677         /// multirow or multicell.
678         std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
679         std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
680         InsetTableCell const * cellInset(idx_type cell) const;
681         //@}
682         ///
683         void setCellInset(row_type row, col_type column,
684                           std::shared_ptr<InsetTableCell>);
685         /// Search for \param inset in the tabular, with the
686         ///
687         void validate(LaTeXFeatures &) const;
688
689         //private:
690         // FIXME Now that cells have an InsetTableCell as their insets, rather
691         // than an InsetText, it'd be possible to reverse the relationship here,
692         // so that cell_vector was a vector<InsetTableCell> rather than a
693         // vector<CellData>, and an InsetTableCell had a CellData as a member,
694         // or perhaps just had its members as members.
695         ///
696         class CellData {
697         public:
698                 ///
699                 explicit CellData(Buffer *);
700                 ///
701                 CellData(CellData const &);
702                 ///
703                 CellData & operator=(CellData const &);
704                 ///
705                 idx_type cellno;
706                 ///
707                 int width;
708                 ///
709                 int multicolumn;
710                 ///
711                 int multirow;
712                 ///
713                 Length mroffset;
714                 ///
715                 LyXAlignment alignment;
716                 ///
717                 VAlignment valignment;
718                 /// width of the part before the decimal
719                 int decimal_hoffset;
720                 /// width of the decimal part
721                 int decimal_width;
722                 ///
723                 int voffset;
724                 ///
725                 bool top_line;
726                 ///
727                 bool bottom_line;
728                 ///
729                 bool left_line;
730                 ///
731                 bool right_line;
732                 ///
733                 bool top_line_rtrimmed;
734                 ///
735                 bool top_line_ltrimmed;
736                 ///
737                 bool bottom_line_rtrimmed;
738                 ///
739                 bool bottom_line_ltrimmed;
740                 ///
741                 BoxType usebox;
742                 ///
743                 int rotate;
744                 ///
745                 docstring align_special;
746                 ///
747                 Length p_width; // this is only set for multicolumn!!!
748                 ///
749                 std::shared_ptr<InsetTableCell> inset;
750         };
751         ///
752         CellData const & cellInfo(idx_type cell) const;
753         ///
754         CellData & cellInfo(idx_type cell);
755         ///
756         typedef std::vector<CellData> cell_vector;
757         ///
758         typedef std::vector<cell_vector> cell_vvector;
759
760         ///
761         class RowData {
762         public:
763                 ///
764                 RowData();
765                 ///
766                 int ascent;
767                 ///
768                 int descent;
769                 /// Extra space between the top line and this row
770                 Length top_space;
771                 /// Ignore top_space if true and use the default top space
772                 bool top_space_default;
773                 /// Extra space between this row and the bottom line
774                 Length bottom_space;
775                 /// Ignore bottom_space if true and use the default bottom space
776                 bool bottom_space_default;
777                 /// Extra space between the bottom line and the next top line
778                 Length interline_space;
779                 /// Ignore interline_space if true and use the default interline space
780                 bool interline_space_default;
781                 /// This are for longtabulars only
782                 /// a row of endhead
783                 bool endhead;
784                 /// a row of endfirsthead
785                 bool endfirsthead;
786                 /// a row of endfoot
787                 bool endfoot;
788                 /// row of endlastfoot
789                 bool endlastfoot;
790                 /// row for a newpage
791                 bool newpage;
792                 /// caption
793                 bool caption;
794                 ///
795                 Change change;
796         };
797         ///
798         typedef std::vector<RowData> row_vector;
799
800         ///
801         class ColumnData {
802                 public:
803                 ///
804                 ColumnData();
805                 ///
806                 LyXAlignment alignment;
807                 ///
808                 VAlignment valignment;
809                 ///
810                 int width;
811                 ///
812                 Length p_width;
813                 ///
814                 docstring align_special;
815                 ///
816                 docstring decimal_point;
817                 ///
818                 bool varwidth;
819                 ///
820                 Change change;
821         };
822         ///
823         typedef std::vector<ColumnData> column_vector;
824
825         ///
826         idx_type numberofcells;
827         ///
828         std::vector<row_type> rowofcell;
829         ///
830         std::vector<col_type> columnofcell;
831         ///
832         row_vector row_info;
833         ///
834         column_vector column_info;
835         ///
836         cell_vvector cell_info;
837         ///
838         Length tabular_width;
839         ///
840         bool use_booktabs;
841         ///
842         int rotate;
843         ///
844         VAlignment tabular_valignment;
845         //
846         // for long tabulars
847         ///
848         HAlignment longtabular_alignment;
849         //
850         bool is_long_tabular;
851         /// endhead data
852         ltType endhead;
853         /// endfirsthead data
854         ltType endfirsthead;
855         /// endfoot data
856         ltType endfoot;
857         /// endlastfoot data
858         ltType endlastfoot;
859
860         ///
861         void init(Buffer *, row_type rows_arg,
862                   col_type columns_arg);
863         ///
864         void updateIndexes();
865         ///
866         bool setFixedWidth(row_type r, col_type c);
867         /// return true of update is needed
868         bool updateColumnWidths(MetricsInfo & mi);
869         ///
870         idx_type columnSpan(idx_type cell) const;
871         ///
872         idx_type rowSpan(idx_type cell) const;
873         ///
874         BoxType useBox(idx_type cell) const;
875         ///
876         // helper function for Latex
877         ///
878         void TeXTopHLine(otexstream &, row_type row, std::list<col_type>,
879                          std::list<col_type>) const;
880         ///
881         void TeXBottomHLine(otexstream &, row_type row, std::list<col_type>,
882                             std::list<col_type>) const;
883         ///
884         void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow,
885                              bool const bidi) const;
886         ///
887         void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
888         ///
889         void TeXLongtableHeaderFooter(otexstream &, OutputParams const &, std::list<col_type>,
890                                       std::list<col_type>) const;
891         ///
892         bool isValidRow(row_type const row) const;
893         ///
894         void TeXRow(otexstream &, row_type const row,
895                     OutputParams const &, std::list<col_type>, std::list<col_type>) const;
896         ///
897         // helper functions for plain text
898         ///
899         bool plaintextTopHLine(odocstringstream &, row_type row,
900                                std::vector<unsigned int> const &) const;
901         ///
902         bool plaintextBottomHLine(odocstringstream &, row_type row,
903                                   std::vector<unsigned int> const &) const;
904         ///
905         void plaintextPrintCell(odocstringstream &,
906                                 OutputParams const &,
907                                 idx_type cell, row_type row, col_type column,
908                                 std::vector<unsigned int> const &,
909                                 bool onlydata, size_t max_length) const;
910         /// auxiliary function for DocBook
911         void docbookRow(XMLStream &, row_type, OutputParams const &,
912                                         bool header = false) const;
913         /// auxiliary function for DocBook: export this row as HTML
914         void docbookRowAsHTML(XMLStream &, row_type, OutputParams const &,
915                                         bool header) const;
916         /// auxiliary function for DocBook: export this row as CALS
917         void docbookRowAsCALS(XMLStream &, row_type, OutputParams const &) const;
918         ///
919         docstring xhtmlRow(XMLStream & xs, row_type, OutputParams const &,
920                            bool header = false) const;
921
922         /// change associated Buffer
923         void setBuffer(Buffer & buffer);
924         /// retrieve associated Buffer
925         Buffer const & buffer() const { return *buffer_; }
926         /// retrieve associated Buffer
927         Buffer & buffer() { return *buffer_; }
928
929 private:
930         Buffer * buffer_;
931
932 }; // Tabular
933
934
935 class InsetTabular : public Inset
936 {
937 public:
938         ///
939         InsetTabular(Buffer *, row_type rows = 1,
940                      col_type columns = 1);
941         ///
942         ~InsetTabular();
943         ///
944         void setBuffer(Buffer & buffer);
945
946         ///
947         static void string2params(std::string const &, InsetTabular &);
948         ///
949         static std::string params2string(InsetTabular const &);
950         ///
951         void read(Lexer &);
952         ///
953         void write(std::ostream &) const;
954         ///
955         void metrics(MetricsInfo &, Dimension &) const;
956         ///
957         void draw(PainterInfo & pi, int x, int y) const;
958         ///
959         void drawSelection(PainterInfo & pi, int x, int y) const;
960         ///
961         void drawBackground(PainterInfo & pi, int x, int y) const;
962         ///
963         bool editable() const { return true; }
964         ///
965         bool hasSettings() const { return true; }
966         ///
967         bool insetAllowed(InsetCode code) const;
968         ///
969         bool allowSpellCheck() const { return true; }
970         ///
971         bool canTrackChanges() const { return true; }
972         ///
973         bool canPaintChange(BufferView const &) const { return true; }
974         /** returns false if, when outputting LaTeX, font changes should
975             be closed before generating this inset. This is needed for
976             insets that may contain several paragraphs */
977         bool inheritFont() const { return false; }
978         ///
979         bool allowsCaptionVariation(std::string const &) const;
980         //
981         bool isTable() const { return true; }
982         ///
983         RowFlags rowFlags() const;
984         ///
985         void latex(otexstream &, OutputParams const &) const;
986         ///
987         int plaintext(odocstringstream & ods, OutputParams const & op,
988                       size_t max_length = INT_MAX) const;
989         ///
990         void docbook(XMLStream &, OutputParams const &) const;
991         ///
992         docstring xhtml(XMLStream &, OutputParams const &) const;
993         ///
994         void validate(LaTeXFeatures & features) const;
995         ///
996         InsetCode lyxCode() const { return TABULAR_CODE; }
997         ///
998         std::string contextMenu(BufferView const &, int, int) const;
999         ///
1000         std::string contextMenuName() const;
1001         /// get offset of this cursor slice relative to our upper left corner
1002         void cursorPos(BufferView const & bv, CursorSlice const & sl,
1003                 bool boundary, int & x, int & y) const;
1004         /// Executes a space-separated sequence of tabular-features requests
1005         void tabularFeatures(Cursor & cur, std::string const & what);
1006         /// Change a single tabular feature; does not handle undo.
1007         void tabularFeatures(Cursor & cur, Tabular::Feature feature,
1008                              std::string const & val = std::string());
1009         /// number of cells
1010         size_t nargs() const { return tabular.numberofcells; }
1011         ///
1012         std::shared_ptr<InsetTableCell const> cell(idx_type) const;
1013         ///
1014         std::shared_ptr<InsetTableCell> cell(idx_type);
1015         ///
1016         Text * getText(int) const;
1017
1018         /// does the inset contain changes ?
1019         bool isChanged() const;
1020         /// set the change for the entire inset
1021         void setChange(Change const & change);
1022         /// accept the changes within the inset
1023         void acceptChanges();
1024         /// reject the changes within the inset
1025         void rejectChanges();
1026
1027         // this should return true if we have a "normal" cell, otherwise false.
1028         // "normal" means without width set!
1029         /// should all paragraphs be output with "Standard" layout?
1030         virtual bool allowParagraphCustomization(idx_type cell = 0) const;
1031         ///
1032         virtual bool forcePlainLayout(idx_type cell = 0) const;
1033         ///
1034         void addPreview(DocIterator const & inset_pos,
1035                 graphics::PreviewLoader &) const;
1036
1037         /// lock cell with given index
1038         void edit(Cursor & cur, bool front, EntryDirection entry_from);
1039         /// get table row from x coordinate
1040         int rowFromY(Cursor & cur, int y) const;
1041         /// get table column from y coordinate
1042         int columnFromX(Cursor & cur, int x) const;
1043         ///
1044         Inset * editXY(Cursor & cur, int x, int y);
1045         /// can we go further down on mouse click?
1046         bool descendable(BufferView const &) const { return true; }
1047         /// Update the counters of this inset and of its contents
1048         void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false);
1049         ///
1050         void addToToc(DocIterator const & di, bool output_active,
1051                                   UpdateType utype, TocBackend & backend) const;
1052
1053         ///
1054         bool completionSupported(Cursor const &) const;
1055         ///
1056         bool inlineCompletionSupported(Cursor const & cur) const;
1057         ///
1058         bool automaticInlineCompletion() const;
1059         ///
1060         bool automaticPopupCompletion() const;
1061         ///
1062         bool showCompletionCursor() const;
1063         ///
1064         CompletionList const * createCompletionList(Cursor const & cur) const;
1065         ///
1066         docstring completionPrefix(Cursor const & cur) const;
1067         ///
1068         bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
1069         ///
1070         void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
1071         ///
1072         virtual bool usePlainLayout() const { return true; }
1073         ///
1074         docstring layoutName() const { return from_ascii("Tabular"); }
1075
1076
1077         ///
1078         InsetTabular * asInsetTabular() { return this; }
1079         ///
1080         InsetTabular const * asInsetTabular() const { return this; }
1081         ///
1082         bool isRightToLeft(Cursor & cur) const;
1083         /// writes the cells between stidx and enidx as a string, optionally
1084         /// descending into the insets
1085         docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
1086         ///
1087         ParagraphList asParList(idx_type stidx, idx_type enidx);
1088
1089         /// Returns whether the cell in the specified row and column is selected.
1090         bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1091         ///
1092         void setLayoutForHiddenCells(DocumentClass const & dc);
1093         //
1094         // Public structures and variables
1095         ///
1096         mutable Tabular tabular;
1097
1098 private:
1099         ///
1100         InsetTabular(InsetTabular const &);
1101         ///
1102         void doDispatch(Cursor & cur, FuncRequest & cmd);
1103         ///
1104         bool getFeatureStatus(Cursor & cur, std::string const & s,
1105                          std::string const & argument, FuncStatus & status) const;
1106         ///
1107         bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1108         ///
1109         Inset * clone() const { return new InsetTabular(*this); }
1110
1111         ///
1112         bool hitSelectRow(BufferView const & bv, int x) const;
1113         ///
1114         bool hitSelectColumn(BufferView const & bv, int y) const;
1115         /// Returns true if coordinates are on row/column selection zones
1116         bool clickable(BufferView const &, int x, int y) const;
1117
1118         ///
1119         void drawCellLines(PainterInfo &, int x, int y, row_type row,
1120                            idx_type cell) const;
1121         ///
1122         void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1123
1124         ///
1125         void moveNextCell(Cursor & cur,
1126                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1127         ///
1128         void movePrevCell(Cursor & cur,
1129                                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1130         ///
1131         int cellXPos(idx_type cell) const;
1132         ///
1133         int cellYPos(idx_type cell) const;
1134         ///
1135         bool copySelection(Cursor & cur);
1136         ///
1137         bool pasteClipboard(Cursor & cur);
1138         ///
1139         void cutSelection(Cursor & cur);
1140         ///
1141         void getSelection(Cursor & cur, row_type & rs, row_type & re,
1142                           col_type & cs, col_type & ce) const;
1143         ///
1144         bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1145
1146         /// return the "Manhattan distance" to nearest corner
1147         int dist(BufferView &, idx_type cell, int x, int y) const;
1148         /// return the cell nearest to x, y
1149         idx_type getNearestCell(BufferView &, int x, int y) const;
1150
1151         /// test the rotation state of the given cell range.
1152         bool oneCellHasRotationState(bool rotated,
1153                                 row_type row_start, row_type row_end,
1154                                 col_type col_start, col_type col_end) const;
1155
1156         /// true when selecting rows with the mouse
1157         bool rowselect_;
1158         /// true when selecting columns with the mouse
1159         bool colselect_;
1160 };
1161
1162 std::string const featureAsString(Tabular::Feature feature);
1163
1164 /// Split cell on decimal symbol
1165 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1166
1167 } // namespace lyx
1168
1169 #endif // INSET_TABULAR_H