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