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