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 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
226 RESET_FORMAL_DEFAULT,
240 TOGGLE_VARWIDTH_COLUMN,
244 UNSET_ROTATE_TABULAR,
246 TOGGLE_ROTATE_TABULAR,
279 SET_SPECIAL_MULTICOLUMN,
297 TABULAR_VALIGN_MIDDLE,
299 TABULAR_VALIGN_BOTTOM,
301 LONGTABULAR_ALIGN_LEFT,
303 LONGTABULAR_ALIGN_CENTER,
305 LONGTABULAR_ALIGN_RIGHT,
320 CELL_BEGIN_OF_MULTICOLUMN,
322 CELL_PART_OF_MULTICOLUMN,
324 CELL_BEGIN_OF_MULTIROW,
326 CELL_PART_OF_MULTIROW
334 LYX_VALIGN_MIDDLE = 1,
336 LYX_VALIGN_BOTTOM = 2
342 LYX_LONGTABULAR_ALIGN_LEFT = 0,
344 LYX_LONGTABULAR_ALIGN_CENTER = 1,
346 LYX_LONGTABULAR_ALIGN_RIGHT = 2
387 // we have this header type (is set in the getLT... functions)
389 // double borders on top
391 // double borders on bottom
393 // used for FirstHeader & LastFooter and if this is true
394 // all the rows marked as FirstHeader or LastFooter are
395 // ignored in the output and it is set to be empty!
399 /// type for row numbers
400 typedef size_t row_type;
401 /// type for column numbers
402 typedef size_t col_type;
403 /// type for cell indices
404 typedef size_t idx_type;
405 /// index indicating an invalid position
406 static const idx_type npos = static_cast<idx_type>(-1);
409 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
411 /// Returns true if there is a topline, returns false if not
412 bool topLine(idx_type cell) const;
413 /// Returns true if there is a topline, returns false if not
414 bool bottomLine(idx_type cell) const;
415 /// Returns true if there is a topline, returns false if not
416 bool leftLine(idx_type cell) const;
417 /// Returns true if there is a topline, returns false if not
418 bool rightLine(idx_type cell) const;
420 /// return space occupied by the second horizontal line and
421 /// interline space above row \p row in pixels
422 int interRowSpace(row_type row) const;
424 int interColumnSpace(idx_type cell) const;
426 /* returns the maximum over all rows */
428 int cellWidth(idx_type cell) const;
430 int cellHeight(idx_type cell) const;
436 row_type nrows() const {return row_info.size();}
438 col_type ncols() const {return column_info.size();}
440 int rowAscent(row_type row) const;
442 int rowDescent(row_type row) const;
444 void setRowAscent(row_type row, int height);
446 void setRowDescent(row_type row, int height);
448 void setTopLine(idx_type cell, bool line);
450 void setBottomLine(idx_type cell, bool line);
452 void setLeftLine(idx_type cell, bool line);
454 void setRightLine(idx_type cell, bool line);
456 bool rowTopLine(row_type row) const;
458 bool rowBottomLine(row_type row) const;
460 bool columnLeftLine(col_type column) const;
462 bool columnRightLine(col_type column) const;
464 void setAlignment(idx_type cell, LyXAlignment align,
465 bool onlycolumn = false);
467 void setVAlignment(idx_type cell, VAlignment align,
468 bool onlycolumn = false);
470 void setTabularWidth(Length const & l) { tabular_width = l; }
472 Length tabularWidth() const { return tabular_width; }
474 void setColumnPWidth(Cursor &, idx_type, Length const &);
476 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
478 bool toggleVarwidth(idx_type, bool const);
480 bool setMROffset(Cursor &, idx_type, Length const &);
482 void setAlignSpecial(idx_type cell, docstring const & special,
485 LyXAlignment getAlignment(idx_type cell,
486 bool onlycolumn = false) const;
488 VAlignment getVAlignment(idx_type cell,
489 bool onlycolumn = false) const;
490 /// The vertical offset of the table due to the vertical
491 /// alignment with respect to the baseline.
492 int offsetVAlignment() const;
494 Length const getPWidth(idx_type cell) const;
496 Length const getMROffset(idx_type cell) const;
498 int textHOffset(idx_type cell) const;
500 int textVOffset(idx_type cell) const;
502 void appendRow(row_type row);
504 void deleteRow(row_type row);
506 void copyRow(row_type row);
508 void insertRow(row_type row, bool copy);
510 void moveColumn(col_type col, ColDirection direction);
512 void moveRow(row_type row, RowDirection direction);
514 void appendColumn(col_type column);
516 void deleteColumn(col_type column);
518 void copyColumn(col_type column);
520 void insertColumn(col_type column, bool copy);
522 idx_type getFirstCellInRow(row_type row) const;
524 idx_type getLastCellInRow(row_type row) const;
526 idx_type numberOfCellsInRow(row_type row) const;
528 void write(std::ostream &) const;
532 void latex(otexstream &, OutputParams const &) const;
534 int docbook(odocstream & os, OutputParams const &) const;
536 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
538 void plaintext(odocstringstream &,
539 OutputParams const & runparams, int const depth,
540 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
542 bool isMultiColumn(idx_type cell) const;
544 bool hasMultiColumn(col_type cell) const;
546 bool hasVarwidthColumn() const;
548 bool isVTypeColumn(col_type cell) const;
550 idx_type setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
551 bool const right_border);
553 void unsetMultiColumn(idx_type cell);
555 bool isPartOfMultiColumn(row_type row, col_type column) const;
557 bool isPartOfMultiRow(row_type row, col_type column) const;
559 bool isMultiRow(idx_type cell) const;
561 bool hasMultiRow(row_type r) const;
563 idx_type setMultiRow(Cursor & cur, idx_type cell, idx_type number,
564 bool const bottom_border,
565 LyXAlignment const halign);
567 void unsetMultiRow(idx_type cell);
569 row_type cellRow(idx_type cell) const;
571 col_type cellColumn(idx_type cell) const;
573 void setRotateCell(idx_type cell, int);
575 int getRotateCell(idx_type cell) const;
577 bool needRotating() const;
579 bool isLastCell(idx_type cell) const;
581 idx_type cellAbove(idx_type cell) const;
583 idx_type cellBelow(idx_type cell) const;
584 /// \return the index of the VISIBLE cell at row, column
585 /// this will be the same as the cell in the previous row,
586 /// e.g., if the cell is part of a multirow
587 idx_type cellIndex(row_type row, col_type column) const;
589 void setUsebox(idx_type cell, BoxType);
591 BoxType getUsebox(idx_type cell) const;
593 // Long Tabular Options support functions
595 void setLTHead(row_type row, bool flag, ltType const &, bool first);
597 bool getRowOfLTHead(row_type row, ltType &) const;
599 bool getRowOfLTFirstHead(row_type row, ltType &) const;
601 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
603 bool getRowOfLTFoot(row_type row, ltType &) const;
605 bool getRowOfLTLastFoot(row_type row, ltType &) const;
607 void setLTNewPage(row_type row, bool what);
609 bool getLTNewPage(row_type row) const;
611 idx_type setLTCaption(Cursor & cur, row_type row, bool what);
613 bool ltCaption(row_type row) const;
615 bool haveLTHead(bool withcaptions = true) const;
617 bool haveLTFirstHead(bool withcaptions = true) const;
619 bool haveLTFoot(bool withcaptions = true) const;
621 bool haveLTLastFoot(bool withcaptions = true) const;
623 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
625 // end longtable support
628 /// there is a subtle difference between these two methods.
631 /// cellInset(cellIndex(r,c));
632 /// can return different things. this is because cellIndex(r,c)
633 /// returns the VISIBLE cell at r,c, which may be the same as the
634 /// cell at the previous row or column, if we're dealing with some
635 /// multirow or multicell.
636 std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
637 std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
638 InsetTableCell const * cellInset(idx_type cell) const;
641 void setCellInset(row_type row, col_type column,
642 std::shared_ptr<InsetTableCell>);
643 /// Search for \param inset in the tabular, with the
645 void validate(LaTeXFeatures &) const;
648 // FIXME Now that cells have an InsetTableCell as their insets, rather
649 // than an InsetText, it'd be possible to reverse the relationship here,
650 // so that cell_vector was a vector<InsetTableCell> rather than a
651 // vector<CellData>, and an InsetTableCell had a CellData as a member,
652 // or perhaps just had its members as members.
659 CellData(CellData const &);
661 CellData & operator=(CellData const &);
673 LyXAlignment alignment;
675 VAlignment valignment;
676 /// width of the part before the decimal
678 /// width of the decimal part
695 docstring align_special;
697 Length p_width; // this is only set for multicolumn!!!
699 std::shared_ptr<InsetTableCell> inset;
702 CellData const & cellInfo(idx_type cell) const;
704 CellData & cellInfo(idx_type cell);
706 typedef std::vector<CellData> cell_vector;
708 typedef std::vector<cell_vector> cell_vvector;
719 /// Extra space between the top line and this row
721 /// Ignore top_space if true and use the default top space
722 bool top_space_default;
723 /// Extra space between this row and the bottom line
725 /// Ignore bottom_space if true and use the default bottom space
726 bool bottom_space_default;
727 /// Extra space between the bottom line and the next top line
728 Length interline_space;
729 /// Ignore interline_space if true and use the default interline space
730 bool interline_space_default;
731 /// This are for longtabulars only
734 /// a row of endfirsthead
738 /// row of endlastfoot
740 /// row for a newpage
746 typedef std::vector<RowData> row_vector;
754 LyXAlignment alignment;
756 VAlignment valignment;
762 docstring align_special;
764 docstring decimal_point;
769 typedef std::vector<ColumnData> column_vector;
772 idx_type numberofcells;
774 std::vector<row_type> rowofcell;
776 std::vector<col_type> columnofcell;
780 column_vector column_info;
782 cell_vvector cell_info;
784 Length tabular_width;
790 VAlignment tabular_valignment;
794 HAlignment longtabular_alignment;
796 bool is_long_tabular;
799 /// endfirsthead data
807 void init(Buffer *, row_type rows_arg,
808 col_type columns_arg);
810 void updateIndexes();
812 bool setFixedWidth(row_type r, col_type c);
813 /// return true of update is needed
814 bool updateColumnWidths(MetricsInfo & mi);
816 idx_type columnSpan(idx_type cell) const;
818 idx_type rowSpan(idx_type cell) const;
820 BoxType useBox(idx_type cell) const;
822 // helper function for Latex
824 void TeXTopHLine(otexstream &, row_type row, std::string const & lang,
825 std::list<col_type>) const;
827 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang,
828 std::list<col_type>) const;
830 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow,
831 bool const bidi) const;
833 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
835 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &, std::list<col_type>) const;
837 bool isValidRow(row_type const row) const;
839 void TeXRow(otexstream &, row_type const row,
840 OutputParams const &, std::list<col_type>) const;
842 // helper functions for plain text
844 bool plaintextTopHLine(odocstringstream &, row_type row,
845 std::vector<unsigned int> const &) const;
847 bool plaintextBottomHLine(odocstringstream &, row_type row,
848 std::vector<unsigned int> const &) const;
850 void plaintextPrintCell(odocstringstream &,
851 OutputParams const &,
852 idx_type cell, row_type row, col_type column,
853 std::vector<unsigned int> const &,
854 bool onlydata, size_t max_length) const;
855 /// auxiliary function for docbook
856 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
858 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
859 bool header = false) const;
861 /// change associated Buffer
862 void setBuffer(Buffer & buffer);
863 /// retrieve associated Buffer
864 Buffer const & buffer() const { return *buffer_; }
865 /// retrieve associated Buffer
866 Buffer & buffer() { return *buffer_; }
874 class InsetTabular : public Inset
878 InsetTabular(Buffer *, row_type rows = 1,
879 col_type columns = 1);
883 void setBuffer(Buffer & buffer);
886 static void string2params(std::string const &, InsetTabular &);
888 static std::string params2string(InsetTabular const &);
892 void write(std::ostream &) const;
894 void metrics(MetricsInfo &, Dimension &) const;
896 void draw(PainterInfo & pi, int x, int y) const;
898 void drawSelection(PainterInfo & pi, int x, int y) const;
900 void drawBackground(PainterInfo & pi, int x, int y) const;
902 bool editable() const { return true; }
904 bool hasSettings() const { return true; }
906 bool insetAllowed(InsetCode code) const;
908 bool allowSpellCheck() const { return true; }
910 bool canTrackChanges() const { return true; }
912 bool canPaintChange(BufferView const &) const { return true; }
913 /** returns false if, when outputing LaTeX, font changes should
914 be closed before generating this inset. This is needed for
915 insets that may contain several paragraphs */
916 bool inheritFont() const { return false; }
918 bool allowsCaptionVariation(std::string const &) const;
920 bool isTable() const { return true; }
922 DisplayType display() const;
924 void latex(otexstream &, OutputParams const &) const;
926 int plaintext(odocstringstream & ods, OutputParams const & op,
927 size_t max_length = INT_MAX) const;
929 int docbook(odocstream &, OutputParams const &) const;
931 docstring xhtml(XHTMLStream &, OutputParams const &) const;
933 void validate(LaTeXFeatures & features) const;
935 InsetCode lyxCode() const { return TABULAR_CODE; }
937 std::string contextMenu(BufferView const &, int, int) const;
939 std::string contextMenuName() const;
940 /// get offset of this cursor slice relative to our upper left corner
941 void cursorPos(BufferView const & bv, CursorSlice const & sl,
942 bool boundary, int & x, int & y) const;
943 /// Executes a space-separated sequence of tabular-features requests
944 void tabularFeatures(Cursor & cur, std::string const & what);
945 /// Change a single tabular feature; does not handle undo.
946 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
947 std::string const & val = std::string());
949 size_t nargs() const { return tabular.numberofcells; }
951 std::shared_ptr<InsetTableCell const> cell(idx_type) const;
953 std::shared_ptr<InsetTableCell> cell(idx_type);
955 Text * getText(int) const;
957 /// set the change for the entire inset
958 void setChange(Change const & change);
959 /// accept the changes within the inset
960 void acceptChanges();
961 /// reject the changes within the inset
962 void rejectChanges();
964 // this should return true if we have a "normal" cell, otherwise false.
965 // "normal" means without width set!
966 /// should all paragraphs be output with "Standard" layout?
967 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
969 virtual bool forcePlainLayout(idx_type cell = 0) const;
971 void addPreview(DocIterator const & inset_pos,
972 graphics::PreviewLoader &) const;
974 /// lock cell with given index
975 void edit(Cursor & cur, bool front, EntryDirection entry_from);
976 /// get table row from x coordinate
977 int rowFromY(Cursor & cur, int y) const;
978 /// get table column from y coordinate
979 int columnFromX(Cursor & cur, int x) const;
981 Inset * editXY(Cursor & cur, int x, int y);
982 /// can we go further down on mouse click?
983 bool descendable(BufferView const &) const { return true; }
984 /// Update the counters of this inset and of its contents
985 void updateBuffer(ParIterator const &, UpdateType);
987 void addToToc(DocIterator const & di, bool output_active,
988 UpdateType utype, TocBackend & backend) const;
991 bool completionSupported(Cursor const &) const;
993 bool inlineCompletionSupported(Cursor const & cur) const;
995 bool automaticInlineCompletion() const;
997 bool automaticPopupCompletion() const;
999 bool showCompletionCursor() const;
1001 CompletionList const * createCompletionList(Cursor const & cur) const;
1003 docstring completionPrefix(Cursor const & cur) const;
1005 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
1007 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
1009 virtual bool usePlainLayout() const { return true; }
1011 docstring layoutName() const { return from_ascii("Tabular"); }
1015 InsetTabular * asInsetTabular() { return this; }
1017 InsetTabular const * asInsetTabular() const { return this; }
1019 bool isRightToLeft(Cursor & cur) const;
1020 /// writes the cells between stidx and enidx as a string, optionally
1021 /// descending into the insets
1022 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
1024 /// Returns whether the cell in the specified row and column is selected.
1025 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1027 void setLayoutForHiddenCells(DocumentClass const & dc);
1029 // Public structures and variables
1031 mutable Tabular tabular;
1035 InsetTabular(InsetTabular const &);
1037 void doDispatch(Cursor & cur, FuncRequest & cmd);
1039 bool getFeatureStatus(Cursor & cur, std::string const & s,
1040 std::string const & argument, FuncStatus & status) const;
1042 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1044 Inset * clone() const { return new InsetTabular(*this); }
1047 bool hitSelectRow(BufferView const & bv, int x) const;
1049 bool hitSelectColumn(BufferView const & bv, int y) const;
1050 /// Returns true if coordinates are on row/column selection zones
1051 bool clickable(BufferView const &, int x, int y) const;
1054 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1055 idx_type cell) const;
1057 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1060 void moveNextCell(Cursor & cur,
1061 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1063 void movePrevCell(Cursor & cur,
1064 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1066 int cellXPos(idx_type cell) const;
1068 int cellYPos(idx_type cell) const;
1070 bool copySelection(Cursor & cur);
1072 bool pasteClipboard(Cursor & cur);
1074 void cutSelection(Cursor & cur);
1076 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1077 col_type & cs, col_type & ce) const;
1079 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1081 /// return the "Manhattan distance" to nearest corner
1082 int dist(BufferView &, idx_type cell, int x, int y) const;
1083 /// return the cell nearest to x, y
1084 idx_type getNearestCell(BufferView &, int x, int y) const;
1086 /// test the rotation state of the given cell range.
1087 bool oneCellHasRotationState(bool rotated,
1088 row_type row_start, row_type row_end,
1089 col_type col_start, col_type col_end) const;
1091 /// true when selecting rows with the mouse
1093 /// true when selecting columns with the mouse
1097 std::string const featureAsString(Tabular::Feature feature);
1099 /// Split cell on decimal symbol
1100 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1104 #endif // INSET_TABULAR_H