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