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