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