4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Matthias Ettrich
10 * \author Jürgen Vigna
11 * \author Edwin Leuven
13 * \author Scott Kostyshak
15 * Full author contact details are available in file CREDITS.
18 // Things to think of when designing the new tabular support:
19 // - color support (colortbl, color)
20 // - decimal alignment (dcloumn)
21 // - custom lines (hhline)
24 #ifndef INSET_TABULAR_H
25 #define INSET_TABULAR_H
27 #include "InsetText.h"
51 class InsetTableCell : public InsetText
55 explicit InsetTableCell(Buffer * buf);
57 InsetCode lyxCode() const { return CELL_CODE; }
59 Inset * clone() const { return new InsetTableCell(*this); }
61 bool getStatus(Cursor & cur, FuncRequest const & cmd,
62 FuncStatus & status) const;
64 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
66 void toggleMultiCol(bool m) { isMultiColumn = m; }
68 void toggleMultiRow(bool m) { isMultiRow = m; }
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);
75 docstring xhtml(XHTMLStream &, OutputParams const &) const;
77 void addToToc(DocIterator const & di, bool output_active,
78 UpdateType utype, TocBackend & backend) const;
80 void metrics(MetricsInfo &, Dimension &) const;
85 void operator=(InsetTableCell const &);
87 // These booleans are supposed to track whether the cell has had its
88 // width explicitly set and whether it is part of a multicolumn, respectively.
89 // We need to know this to determine whether
90 // layout changes and paragraph customization are allowed---that is,
91 // we need it in forcePlainLayout() and allowParagraphCustomization().
92 // Unfortunately, that information is not readily available in
93 // InsetTableCell. In the case of multicolumn cells, it is present
94 // in CellData, and so would be available here if CellData were to
95 // become a member of InsetTableCell. But in the other case, it isn't
96 // even available there, but is held in Tabular::ColumnData.
97 // So, the present solution uses this boolean to track the information
98 // we need to track, and tries to keep it updated. This is not ideal,
99 // but the other solutions are no better. These are:
100 // (i) Keep a pointer in InsetTableCell to the table;
101 // (ii) Find the table by iterating over the Buffer's insets.
102 // Solution (i) raises the problem of updating the pointer when an
103 // InsetTableCell is copied, and we'd therefore need a copy constructor
104 // in InsetTabular and then in Tabular, which seems messy, given how
105 // complicated those classes are. Solution (ii) involves a lot of
106 // iterating, since this information is needed quite often, and so may
108 // So, well, if someone can do better, please do!
116 // FIXME: Here the thoughts from the comment above also apply.
118 LyXAlignment contentAlign;
119 /// should paragraph indendation be omitted in any case?
120 bool neverIndent() const { return true; }
122 LyXAlignment contentAlignment() const { return contentAlign; }
124 virtual bool usePlainLayout() const { return true; }
126 virtual bool forcePlainLayout(idx_type = 0) const;
128 virtual bool allowParagraphCustomization(idx_type = 0) const;
130 virtual bool forceLocalFontSwitch() const;
131 /// Is the width forced to some value?
132 bool hasFixedWidth() const { return isFixedWidth; }
133 /// Can the cell contain several paragraphs?
134 bool allowMultiPar() const { return !isMultiRow && (!isMultiColumn || isFixedWidth); }
139 // A helper struct for tables
242 RESET_FORMAL_DEFAULT,
256 TOGGLE_VARWIDTH_COLUMN,
260 UNSET_ROTATE_TABULAR,
262 TOGGLE_ROTATE_TABULAR,
295 SET_SPECIAL_MULTICOLUMN,
313 TABULAR_VALIGN_MIDDLE,
315 TABULAR_VALIGN_BOTTOM,
317 LONGTABULAR_ALIGN_LEFT,
319 LONGTABULAR_ALIGN_CENTER,
321 LONGTABULAR_ALIGN_RIGHT,
336 CELL_BEGIN_OF_MULTICOLUMN,
338 CELL_PART_OF_MULTICOLUMN,
340 CELL_BEGIN_OF_MULTIROW,
342 CELL_PART_OF_MULTIROW
350 LYX_VALIGN_MIDDLE = 1,
352 LYX_VALIGN_BOTTOM = 2
358 LYX_LONGTABULAR_ALIGN_LEFT = 0,
360 LYX_LONGTABULAR_ALIGN_CENTER = 1,
362 LYX_LONGTABULAR_ALIGN_RIGHT = 2
403 // we have this header type (is set in the getLT... functions)
405 // double borders on top
407 // double borders on bottom
409 // used for FirstHeader & LastFooter and if this is true
410 // all the rows marked as FirstHeader or LastFooter are
411 // ignored in the output and it is set to be empty!
415 /// type for row numbers
416 typedef size_t row_type;
417 /// type for column numbers
418 typedef size_t col_type;
419 /// type for cell indices
420 typedef size_t idx_type;
421 /// index indicating an invalid position
422 static const idx_type npos = static_cast<idx_type>(-1);
425 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
427 /// Returns true if there is a topline, returns false if not
428 bool topLine(idx_type cell) const;
429 /// Returns true if there is a topline, returns false if not
430 bool bottomLine(idx_type cell) const;
431 /// Returns true if there is a topline, returns false if not
432 /// If \p ignore_bt is true, we return the state as if booktabs was
434 bool leftLine(idx_type cell, bool const ignore_bt = false) const;
435 /// Returns true if there is a topline, returns false if not
436 /// If \p ignore_bt is true, we return the state as if booktabs was
438 bool rightLine(idx_type cell, bool const ignore_bt = false) const;
439 /// Returns whether the top line is trimmed left and/or right
440 std::pair<bool, bool> topLineTrim(idx_type const cell) const;
441 /// Returns whether the bottom line is trimmed left and/or right
442 std::pair<bool, bool> bottomLineTrim(idx_type const cell) const;
444 /// return space occupied by the second horizontal line and
445 /// interline space above row \p row in pixels
446 int interRowSpace(row_type row) const;
448 int interColumnSpace(idx_type cell) const;
450 /* returns the maximum over all rows */
452 int cellWidth(idx_type cell) const;
454 int cellHeight(idx_type cell) const;
460 row_type nrows() const {return row_info.size();}
462 col_type ncols() const {return column_info.size();}
464 int rowAscent(row_type row) const;
466 int rowDescent(row_type row) const;
468 void setRowAscent(row_type row, int height);
470 void setRowDescent(row_type row, int height);
472 void setTopLine(idx_type cell, bool line);
474 void setBottomLine(idx_type cell, bool line);
476 void setTopLineLTrim(idx_type cell, bool val);
478 void setBottomLineLTrim(idx_type cell, bool val);
480 void setTopLineRTrim(idx_type cell, bool val);
482 void setBottomLineRTrim(idx_type cell, bool val);
484 void setTopLineTrim(idx_type cell, std::pair<bool, bool>);
486 void setBottomLineTrim(idx_type cell, std::pair<bool, bool>);
488 void setLeftLine(idx_type cell, bool line);
490 void setRightLine(idx_type cell, bool line);
492 bool rowTopLine(row_type row) const;
494 bool rowBottomLine(row_type row) const;
496 bool columnLeftLine(col_type column) const;
498 bool columnRightLine(col_type column) const;
500 void setAlignment(idx_type cell, LyXAlignment align,
501 bool onlycolumn = false);
503 void setVAlignment(idx_type cell, VAlignment align,
504 bool onlycolumn = false);
506 void setTabularWidth(Length const & l) { tabular_width = l; }
508 Length tabularWidth() const { return tabular_width; }
510 void setColumnPWidth(Cursor &, idx_type, Length const &);
512 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
514 bool toggleVarwidth(idx_type, bool const);
516 bool setMROffset(Cursor &, idx_type, Length const &);
518 void setAlignSpecial(idx_type cell, docstring const & special,
521 LyXAlignment getAlignment(idx_type cell,
522 bool onlycolumn = false) const;
524 VAlignment getVAlignment(idx_type cell,
525 bool onlycolumn = false) const;
526 /// The vertical offset of the table due to the vertical
527 /// alignment with respect to the baseline.
528 int offsetVAlignment() const;
530 Length const getPWidth(idx_type cell) const;
532 Length const getMROffset(idx_type cell) const;
534 int textHOffset(idx_type cell) const;
536 int textVOffset(idx_type cell) const;
538 void appendRow(row_type row);
540 void deleteRow(row_type row, bool const force = false);
542 void copyRow(row_type row);
544 void insertRow(row_type row, bool copy);
546 void moveColumn(col_type col, ColDirection direction);
548 void moveRow(row_type row, RowDirection direction);
550 void appendColumn(col_type column);
552 void deleteColumn(col_type column, bool const force = false);
554 void copyColumn(col_type column);
556 void insertColumn(col_type column, bool copy);
558 idx_type getFirstCellInRow(row_type row) const;
560 idx_type getLastCellInRow(row_type row) const;
562 idx_type numberOfCellsInRow(row_type row) const;
564 void write(std::ostream &) const;
568 void latex(otexstream &, OutputParams const &) const;
570 int docbook(odocstream & os, OutputParams const &) const;
572 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
574 void plaintext(odocstringstream &,
575 OutputParams const & runparams, int const depth,
576 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
578 bool isMultiColumn(idx_type cell) const;
580 bool hasMultiColumn(col_type cell) const;
582 bool hasVarwidthColumn() const;
584 bool isVTypeColumn(col_type cell) const;
586 idx_type setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
587 bool const right_border);
589 void unsetMultiColumn(idx_type cell);
591 bool isPartOfMultiColumn(row_type row, col_type column) const;
593 bool isPartOfMultiRow(row_type row, col_type column) const;
595 bool isMultiRow(idx_type cell) const;
597 bool hasMultiRow(row_type r) const;
599 idx_type setMultiRow(Cursor & cur, idx_type cell, idx_type number,
600 bool const bottom_border,
601 LyXAlignment const halign);
603 void unsetMultiRow(idx_type cell);
605 row_type cellRow(idx_type cell) const;
607 col_type cellColumn(idx_type cell) const;
609 void setRotateCell(idx_type cell, int);
611 int getRotateCell(idx_type cell) const;
613 bool needRotating() const;
615 bool isLastCell(idx_type cell) const;
617 idx_type cellAbove(idx_type cell) const;
619 idx_type cellBelow(idx_type cell) const;
620 /// \return the index of the VISIBLE cell at row, column
621 /// this will be the same as the cell in the previous row,
622 /// e.g., if the cell is part of a multirow
623 idx_type cellIndex(row_type row, col_type column) const;
625 void setUsebox(idx_type cell, BoxType);
627 BoxType getUsebox(idx_type cell) const;
629 // Long Tabular Options support functions
631 void setLTHead(row_type row, bool flag, ltType const &, bool first);
633 bool getRowOfLTHead(row_type row, ltType &) const;
635 bool getRowOfLTFirstHead(row_type row, ltType &) const;
637 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
639 bool getRowOfLTFoot(row_type row, ltType &) const;
641 bool getRowOfLTLastFoot(row_type row, ltType &) const;
643 void setLTNewPage(row_type row, bool what);
645 bool getLTNewPage(row_type row) const;
647 idx_type setLTCaption(Cursor & cur, row_type row, bool what);
649 bool ltCaption(row_type row) const;
651 bool haveLTHead(bool withcaptions = true) const;
653 bool haveLTFirstHead(bool withcaptions = true) const;
655 bool haveLTFoot(bool withcaptions = true) const;
657 bool haveLTLastFoot(bool withcaptions = true) const;
659 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
661 // end longtable support
664 /// there is a subtle difference between these two methods.
667 /// cellInset(cellIndex(r,c));
668 /// can return different things. this is because cellIndex(r,c)
669 /// returns the VISIBLE cell at r,c, which may be the same as the
670 /// cell at the previous row or column, if we're dealing with some
671 /// multirow or multicell.
672 std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
673 std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
674 InsetTableCell const * cellInset(idx_type cell) const;
677 void setCellInset(row_type row, col_type column,
678 std::shared_ptr<InsetTableCell>);
679 /// Search for \param inset in the tabular, with the
681 void validate(LaTeXFeatures &) const;
684 // FIXME Now that cells have an InsetTableCell as their insets, rather
685 // than an InsetText, it'd be possible to reverse the relationship here,
686 // so that cell_vector was a vector<InsetTableCell> rather than a
687 // vector<CellData>, and an InsetTableCell had a CellData as a member,
688 // or perhaps just had its members as members.
693 explicit CellData(Buffer *);
695 CellData(CellData const &);
697 CellData & operator=(CellData const &);
709 LyXAlignment alignment;
711 VAlignment valignment;
712 /// width of the part before the decimal
714 /// width of the decimal part
727 bool top_line_rtrimmed;
729 bool top_line_ltrimmed;
731 bool bottom_line_rtrimmed;
733 bool bottom_line_ltrimmed;
739 docstring align_special;
741 Length p_width; // this is only set for multicolumn!!!
743 std::shared_ptr<InsetTableCell> inset;
746 CellData const & cellInfo(idx_type cell) const;
748 CellData & cellInfo(idx_type cell);
750 typedef std::vector<CellData> cell_vector;
752 typedef std::vector<cell_vector> cell_vvector;
763 /// Extra space between the top line and this row
765 /// Ignore top_space if true and use the default top space
766 bool top_space_default;
767 /// Extra space between this row and the bottom line
769 /// Ignore bottom_space if true and use the default bottom space
770 bool bottom_space_default;
771 /// Extra space between the bottom line and the next top line
772 Length interline_space;
773 /// Ignore interline_space if true and use the default interline space
774 bool interline_space_default;
775 /// This are for longtabulars only
778 /// a row of endfirsthead
782 /// row of endlastfoot
784 /// row for a newpage
792 typedef std::vector<RowData> row_vector;
800 LyXAlignment alignment;
802 VAlignment valignment;
808 docstring align_special;
810 docstring decimal_point;
817 typedef std::vector<ColumnData> column_vector;
820 idx_type numberofcells;
822 std::vector<row_type> rowofcell;
824 std::vector<col_type> columnofcell;
828 column_vector column_info;
830 cell_vvector cell_info;
832 Length tabular_width;
838 VAlignment tabular_valignment;
842 HAlignment longtabular_alignment;
844 bool is_long_tabular;
847 /// endfirsthead data
855 void init(Buffer *, row_type rows_arg,
856 col_type columns_arg);
858 void updateIndexes();
860 bool setFixedWidth(row_type r, col_type c);
861 /// return true of update is needed
862 bool updateColumnWidths(MetricsInfo & mi);
864 idx_type columnSpan(idx_type cell) const;
866 idx_type rowSpan(idx_type cell) const;
868 BoxType useBox(idx_type cell) const;
870 // helper function for Latex
872 void TeXTopHLine(otexstream &, row_type row, std::list<col_type>) const;
874 void TeXBottomHLine(otexstream &, row_type row, std::list<col_type>) const;
876 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow,
877 bool const bidi) const;
879 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
881 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &, std::list<col_type>) const;
883 bool isValidRow(row_type const row) const;
885 void TeXRow(otexstream &, row_type const row,
886 OutputParams const &, std::list<col_type>) const;
888 // helper functions for plain text
890 bool plaintextTopHLine(odocstringstream &, row_type row,
891 std::vector<unsigned int> const &) const;
893 bool plaintextBottomHLine(odocstringstream &, row_type row,
894 std::vector<unsigned int> const &) const;
896 void plaintextPrintCell(odocstringstream &,
897 OutputParams const &,
898 idx_type cell, row_type row, col_type column,
899 std::vector<unsigned int> const &,
900 bool onlydata, size_t max_length) const;
901 /// auxiliary function for docbook
902 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
904 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
905 bool header = false) const;
907 /// change associated Buffer
908 void setBuffer(Buffer & buffer);
909 /// retrieve associated Buffer
910 Buffer const & buffer() const { return *buffer_; }
911 /// retrieve associated Buffer
912 Buffer & buffer() { return *buffer_; }
920 class InsetTabular : public Inset
924 InsetTabular(Buffer *, row_type rows = 1,
925 col_type columns = 1);
929 void setBuffer(Buffer & buffer);
932 static void string2params(std::string const &, InsetTabular &);
934 static std::string params2string(InsetTabular const &);
938 void write(std::ostream &) const;
940 void metrics(MetricsInfo &, Dimension &) const;
942 void draw(PainterInfo & pi, int x, int y) const;
944 void drawSelection(PainterInfo & pi, int x, int y) const;
946 void drawBackground(PainterInfo & pi, int x, int y) const;
948 bool editable() const { return true; }
950 bool hasSettings() const { return true; }
952 bool insetAllowed(InsetCode code) const;
954 bool allowSpellCheck() const { return true; }
956 bool canTrackChanges() const { return true; }
958 bool canPaintChange(BufferView const &) const { return true; }
959 /** returns false if, when outputing LaTeX, font changes should
960 be closed before generating this inset. This is needed for
961 insets that may contain several paragraphs */
962 bool inheritFont() const { return false; }
964 bool allowsCaptionVariation(std::string const &) const;
966 bool isTable() const { return true; }
968 DisplayType display() const;
970 void latex(otexstream &, OutputParams const &) const;
972 int plaintext(odocstringstream & ods, OutputParams const & op,
973 size_t max_length = INT_MAX) const;
975 int docbook(odocstream &, OutputParams const &) const;
977 docstring xhtml(XHTMLStream &, OutputParams const &) const;
979 void validate(LaTeXFeatures & features) const;
981 InsetCode lyxCode() const { return TABULAR_CODE; }
983 std::string contextMenu(BufferView const &, int, int) const;
985 std::string contextMenuName() const;
986 /// get offset of this cursor slice relative to our upper left corner
987 void cursorPos(BufferView const & bv, CursorSlice const & sl,
988 bool boundary, int & x, int & y) const;
989 /// Executes a space-separated sequence of tabular-features requests
990 void tabularFeatures(Cursor & cur, std::string const & what);
991 /// Change a single tabular feature; does not handle undo.
992 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
993 std::string const & val = std::string());
995 size_t nargs() const { return tabular.numberofcells; }
997 std::shared_ptr<InsetTableCell const> cell(idx_type) const;
999 std::shared_ptr<InsetTableCell> cell(idx_type);
1001 Text * getText(int) const;
1003 /// does the inset contain changes ?
1004 bool isChanged() const;
1005 /// set the change for the entire inset
1006 void setChange(Change const & change);
1007 /// accept the changes within the inset
1008 void acceptChanges();
1009 /// reject the changes within the inset
1010 void rejectChanges();
1012 // this should return true if we have a "normal" cell, otherwise false.
1013 // "normal" means without width set!
1014 /// should all paragraphs be output with "Standard" layout?
1015 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
1017 virtual bool forcePlainLayout(idx_type cell = 0) const;
1019 void addPreview(DocIterator const & inset_pos,
1020 graphics::PreviewLoader &) const;
1022 /// lock cell with given index
1023 void edit(Cursor & cur, bool front, EntryDirection entry_from);
1024 /// get table row from x coordinate
1025 int rowFromY(Cursor & cur, int y) const;
1026 /// get table column from y coordinate
1027 int columnFromX(Cursor & cur, int x) const;
1029 Inset * editXY(Cursor & cur, int x, int y);
1030 /// can we go further down on mouse click?
1031 bool descendable(BufferView const &) const { return true; }
1032 /// Update the counters of this inset and of its contents
1033 void updateBuffer(ParIterator const &, UpdateType);
1035 void addToToc(DocIterator const & di, bool output_active,
1036 UpdateType utype, TocBackend & backend) const;
1039 bool completionSupported(Cursor const &) const;
1041 bool inlineCompletionSupported(Cursor const & cur) const;
1043 bool automaticInlineCompletion() const;
1045 bool automaticPopupCompletion() const;
1047 bool showCompletionCursor() const;
1049 CompletionList const * createCompletionList(Cursor const & cur) const;
1051 docstring completionPrefix(Cursor const & cur) const;
1053 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
1055 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
1057 virtual bool usePlainLayout() const { return true; }
1059 docstring layoutName() const { return from_ascii("Tabular"); }
1063 InsetTabular * asInsetTabular() { return this; }
1065 InsetTabular const * asInsetTabular() const { return this; }
1067 bool isRightToLeft(Cursor & cur) const;
1068 /// writes the cells between stidx and enidx as a string, optionally
1069 /// descending into the insets
1070 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
1072 /// Returns whether the cell in the specified row and column is selected.
1073 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1075 void setLayoutForHiddenCells(DocumentClass const & dc);
1077 // Public structures and variables
1079 mutable Tabular tabular;
1083 InsetTabular(InsetTabular const &);
1085 void doDispatch(Cursor & cur, FuncRequest & cmd);
1087 bool getFeatureStatus(Cursor & cur, std::string const & s,
1088 std::string const & argument, FuncStatus & status) const;
1090 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1092 Inset * clone() const { return new InsetTabular(*this); }
1095 bool hitSelectRow(BufferView const & bv, int x) const;
1097 bool hitSelectColumn(BufferView const & bv, int y) const;
1098 /// Returns true if coordinates are on row/column selection zones
1099 bool clickable(BufferView const &, int x, int y) const;
1102 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1103 idx_type cell) const;
1105 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1108 void moveNextCell(Cursor & cur,
1109 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1111 void movePrevCell(Cursor & cur,
1112 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1114 int cellXPos(idx_type cell) const;
1116 int cellYPos(idx_type cell) const;
1118 bool copySelection(Cursor & cur);
1120 bool pasteClipboard(Cursor & cur);
1122 void cutSelection(Cursor & cur);
1124 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1125 col_type & cs, col_type & ce) const;
1127 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1129 /// return the "Manhattan distance" to nearest corner
1130 int dist(BufferView &, idx_type cell, int x, int y) const;
1131 /// return the cell nearest to x, y
1132 idx_type getNearestCell(BufferView &, int x, int y) const;
1134 /// test the rotation state of the given cell range.
1135 bool oneCellHasRotationState(bool rotated,
1136 row_type row_start, row_type row_end,
1137 col_type col_start, col_type col_end) const;
1139 /// true when selecting rows with the mouse
1141 /// true when selecting columns with the mouse
1145 std::string const featureAsString(Tabular::Feature feature);
1147 /// Split cell on decimal symbol
1148 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1152 #endif // INSET_TABULAR_H