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