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