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