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 setContentAlignment(LyXAlignment al) {contentAlign = al; }
67 /// writes the contents of the cell as a string, optionally
68 /// descending into insets
69 docstring asString(bool intoInsets = true);
71 docstring xhtml(XHTMLStream &, OutputParams const &) const;
73 void addToToc(DocIterator const & di, bool output_active,
74 UpdateType utype, TocBackend & backend) const;
79 void operator=(InsetTableCell const &);
81 // This boolean is supposed to track whether the cell has had its
82 // width explicitly set. We need to know this to determine whether
83 // layout changes and paragraph customization are allowed---that is,
84 // we need it in forcePlainLayout() and allowParagraphCustomization().
85 // Unfortunately, that information is not readily available in
86 // InsetTableCell. In the case of multicolumn cells, it is present
87 // in CellData, and so would be available here if CellData were to
88 // become a member of InsetTableCell. But in the other case, it isn't
89 // even available there, but is held in Tabular::ColumnData.
90 // So, the present solution uses this boolean to track the information
91 // we need to track, and tries to keep it updated. This is not ideal,
92 // but the other solutions are no better. These are:
93 // (i) Keep a pointer in InsetTableCell to the table;
94 // (ii) Find the table by iterating over the Buffer's insets.
95 // Solution (i) raises the problem of updating the pointer when an
96 // InsetTableCell is copied, and we'd therefore need a copy constructor
97 // in InsetTabular and then in Tabular, which seems messy, given how
98 // complicated those classes are. Solution (ii) involves a lot of
99 // iterating, since this information is needed quite often, and so may
101 // So, well, if someone can do better, please do!
105 // FIXME: Here the thoughts from the comment above also apply.
107 LyXAlignment contentAlign;
108 /// should paragraph indendation be omitted in any case?
109 bool neverIndent() const { return true; }
111 LyXAlignment contentAlignment() const { return contentAlign; }
113 virtual bool usePlainLayout() const { return true; }
115 virtual bool forcePlainLayout(idx_type = 0) const;
117 virtual bool allowParagraphCustomization(idx_type = 0) const;
119 virtual bool forceLocalFontSwitch() const;
120 /// Is the width forced to some value?
121 bool hasFixedWidth() const { return isFixedWidth; }
122 /// Can the cell contain several paragraphs?
123 /** FIXME this is wrong for multirows, that are limited to one
124 * paragraph. However, we cannot test for this (see the big
127 bool allowMultiPar() const { return isFixedWidth; }
132 // A helper struct for tables
233 UNSET_ROTATE_TABULAR,
235 TOGGLE_ROTATE_TABULAR,
268 SET_SPECIAL_MULTICOLUMN,
286 TABULAR_VALIGN_MIDDLE,
288 TABULAR_VALIGN_BOTTOM,
290 LONGTABULAR_ALIGN_LEFT,
292 LONGTABULAR_ALIGN_CENTER,
294 LONGTABULAR_ALIGN_RIGHT,
307 CELL_BEGIN_OF_MULTICOLUMN,
309 CELL_PART_OF_MULTICOLUMN,
311 CELL_BEGIN_OF_MULTIROW,
313 CELL_PART_OF_MULTIROW
321 LYX_VALIGN_MIDDLE = 1,
323 LYX_VALIGN_BOTTOM = 2
329 LYX_LONGTABULAR_ALIGN_LEFT = 0,
331 LYX_LONGTABULAR_ALIGN_CENTER = 1,
333 LYX_LONGTABULAR_ALIGN_RIGHT = 2
374 // we have this header type (is set in the getLT... functions)
376 // double borders on top
378 // double borders on bottom
380 // used for FirstHeader & LastFooter and if this is true
381 // all the rows marked as FirstHeader or LastFooter are
382 // ignored in the output and it is set to be empty!
386 /// type for row numbers
387 typedef size_t row_type;
388 /// type for column numbers
389 typedef size_t col_type;
390 /// type for cell indices
391 typedef size_t idx_type;
392 /// index indicating an invalid position
393 static const idx_type npos = static_cast<idx_type>(-1);
396 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
398 /// Returns true if there is a topline, returns false if not
399 bool topLine(idx_type cell) const;
400 /// Returns true if there is a topline, returns false if not
401 bool bottomLine(idx_type cell) const;
402 /// Returns true if there is a topline, returns false if not
403 bool leftLine(idx_type cell) const;
404 /// Returns true if there is a topline, returns false if not
405 bool rightLine(idx_type cell) const;
407 /// return space occupied by the second horizontal line and
408 /// interline space above row \p row in pixels
409 int interRowSpace(row_type row) const;
411 int interColumnSpace(idx_type cell) const;
413 /* returns the maximum over all rows */
415 int cellWidth(idx_type cell) const;
417 int cellHeight(idx_type cell) const;
423 row_type nrows() const {return row_info.size();}
425 col_type ncols() const {return column_info.size();}
427 int rowAscent(row_type row) const;
429 int rowDescent(row_type row) const;
431 void setRowAscent(row_type row, int height);
433 void setRowDescent(row_type row, int height);
435 void setTopLine(idx_type cell, bool line);
437 void setBottomLine(idx_type cell, bool line);
439 void setLeftLine(idx_type cell, bool line);
441 void setRightLine(idx_type cell, bool line);
443 bool rowTopLine(row_type row) const;
445 bool rowBottomLine(row_type row) const;
447 bool columnLeftLine(col_type column) const;
449 bool columnRightLine(col_type column) const;
451 void setAlignment(idx_type cell, LyXAlignment align,
452 bool onlycolumn = false);
454 void setVAlignment(idx_type cell, VAlignment align,
455 bool onlycolumn = false);
457 void setTabularWidth(Length const & l) { tabular_width = l; }
459 Length tabularWidth() const { return tabular_width; }
461 void setColumnPWidth(Cursor &, idx_type, Length const &);
463 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
465 bool setMROffset(Cursor &, idx_type, Length const &);
467 void setAlignSpecial(idx_type cell, docstring const & special,
470 LyXAlignment getAlignment(idx_type cell,
471 bool onlycolumn = false) const;
473 VAlignment getVAlignment(idx_type cell,
474 bool onlycolumn = false) const;
475 /// The vertical offset of the table due to the vertical
476 /// alignment with respect to the baseline.
477 int offsetVAlignment() const;
479 Length const getPWidth(idx_type cell) const;
481 Length const getMROffset(idx_type cell) const;
483 int textHOffset(idx_type cell) const;
485 int textVOffset(idx_type cell) const;
487 void appendRow(row_type row);
489 void deleteRow(row_type row);
491 void copyRow(row_type row);
493 void insertRow(row_type row, bool copy);
495 void moveColumn(col_type col, ColDirection direction);
497 void moveRow(row_type row, RowDirection direction);
499 void appendColumn(col_type column);
501 void deleteColumn(col_type column);
503 void copyColumn(col_type column);
505 void insertColumn(col_type column, bool copy);
507 idx_type getFirstCellInRow(row_type row) const;
509 idx_type getLastCellInRow(row_type row) const;
511 idx_type numberOfCellsInRow(row_type row) const;
513 void write(std::ostream &) const;
517 void latex(otexstream &, OutputParams const &) const;
519 int docbook(odocstream & os, OutputParams const &) const;
521 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
523 void plaintext(odocstringstream &,
524 OutputParams const & runparams, int const depth,
525 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
527 bool isMultiColumn(idx_type cell) const;
529 bool hasMultiColumn(col_type cell) const;
531 idx_type setMultiColumn(idx_type cell, idx_type number,
532 bool const right_border);
534 void unsetMultiColumn(idx_type cell);
536 bool isPartOfMultiColumn(row_type row, col_type column) const;
538 bool isPartOfMultiRow(row_type row, col_type column) const;
540 bool isMultiRow(idx_type cell) const;
542 bool hasMultiRow(row_type r) const;
544 idx_type setMultiRow(idx_type cell, idx_type number,
545 bool const bottom_border,
546 LyXAlignment const halign);
548 void unsetMultiRow(idx_type cell);
550 row_type cellRow(idx_type cell) const;
552 col_type cellColumn(idx_type cell) const;
554 void setRotateCell(idx_type cell, int);
556 int getRotateCell(idx_type cell) const;
558 bool needRotating() const;
560 bool isLastCell(idx_type cell) const;
562 idx_type cellAbove(idx_type cell) const;
564 idx_type cellBelow(idx_type cell) const;
565 /// \return the index of the VISIBLE cell at row, column
566 /// this will be the same as the cell in the previous row,
567 /// e.g., if the cell is part of a multirow
568 idx_type cellIndex(row_type row, col_type column) const;
570 void setUsebox(idx_type cell, BoxType);
572 BoxType getUsebox(idx_type cell) const;
574 // Long Tabular Options support functions
576 void setLTHead(row_type row, bool flag, ltType const &, bool first);
578 bool getRowOfLTHead(row_type row, ltType &) const;
580 bool getRowOfLTFirstHead(row_type row, ltType &) const;
582 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
584 bool getRowOfLTFoot(row_type row, ltType &) const;
586 bool getRowOfLTLastFoot(row_type row, ltType &) const;
588 void setLTNewPage(row_type row, bool what);
590 bool getLTNewPage(row_type row) const;
592 idx_type setLTCaption(row_type row, bool what);
594 bool ltCaption(row_type row) const;
596 bool haveLTHead(bool withcaptions = true) const;
598 bool haveLTFirstHead(bool withcaptions = true) const;
600 bool haveLTFoot(bool withcaptions = true) const;
602 bool haveLTLastFoot(bool withcaptions = true) const;
604 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
606 // end longtable support
609 /// there is a subtle difference between these two methods.
612 /// cellInset(cellIndex(r,c));
613 /// can return different things. this is because cellIndex(r,c)
614 /// returns the VISIBLE cell at r,c, which may be the same as the
615 /// cell at the previous row or column, if we're dealing with some
616 /// multirow or multicell.
617 std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
618 std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
619 InsetTableCell const * cellInset(idx_type cell) const;
622 void setCellInset(row_type row, col_type column,
623 std::shared_ptr<InsetTableCell>);
624 /// Search for \param inset in the tabular, with the
626 void validate(LaTeXFeatures &) const;
629 // FIXME Now that cells have an InsetTableCell as their insets, rather
630 // than an InsetText, it'd be possible to reverse the relationship here,
631 // so that cell_vector was a vector<InsetTableCell> rather than a
632 // vector<CellData>, and an InsetTableCell had a CellData as a member,
633 // or perhaps just had its members as members.
640 CellData(CellData const &);
642 CellData & operator=(CellData const &);
654 LyXAlignment alignment;
656 VAlignment valignment;
657 /// width of the part before the decimal
659 /// width of the decimal part
676 docstring align_special;
678 Length p_width; // this is only set for multicolumn!!!
680 std::shared_ptr<InsetTableCell> inset;
683 CellData const & cellInfo(idx_type cell) const;
685 CellData & cellInfo(idx_type cell);
687 typedef std::vector<CellData> cell_vector;
689 typedef std::vector<cell_vector> cell_vvector;
700 /// Extra space between the top line and this row
702 /// Ignore top_space if true and use the default top space
703 bool top_space_default;
704 /// Extra space between this row and the bottom line
706 /// Ignore bottom_space if true and use the default bottom space
707 bool bottom_space_default;
708 /// Extra space between the bottom line and the next top line
709 Length interline_space;
710 /// Ignore interline_space if true and use the default interline space
711 bool interline_space_default;
712 /// This are for longtabulars only
715 /// a row of endfirsthead
719 /// row of endlastfoot
721 /// row for a newpage
727 typedef std::vector<RowData> row_vector;
735 LyXAlignment alignment;
737 VAlignment valignment;
743 docstring align_special;
745 docstring decimal_point;
748 typedef std::vector<ColumnData> column_vector;
751 idx_type numberofcells;
753 std::vector<row_type> rowofcell;
755 std::vector<col_type> columnofcell;
759 column_vector column_info;
761 cell_vvector cell_info;
763 Length tabular_width;
769 VAlignment tabular_valignment;
773 HAlignment longtabular_alignment;
775 bool is_long_tabular;
778 /// endfirsthead data
786 void init(Buffer *, row_type rows_arg,
787 col_type columns_arg);
789 void updateIndexes();
791 bool setFixedWidth(row_type r, col_type c);
792 /// return true of update is needed
793 bool updateColumnWidths();
795 idx_type columnSpan(idx_type cell) const;
797 idx_type rowSpan(idx_type cell) const;
799 BoxType useParbox(idx_type cell) const;
801 // helper function for Latex
803 void TeXTopHLine(otexstream &, row_type row, std::string const & lang,
804 std::list<col_type>) const;
806 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang,
807 std::list<col_type>) const;
809 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow,
810 bool const bidi) const;
812 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
814 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &, std::list<col_type>) const;
816 bool isValidRow(row_type const row) const;
818 void TeXRow(otexstream &, row_type const row,
819 OutputParams const &, std::list<col_type>) const;
821 // helper functions for plain text
823 bool plaintextTopHLine(odocstringstream &, row_type row,
824 std::vector<unsigned int> const &) const;
826 bool plaintextBottomHLine(odocstringstream &, row_type row,
827 std::vector<unsigned int> const &) const;
829 void plaintextPrintCell(odocstringstream &,
830 OutputParams const &,
831 idx_type cell, row_type row, col_type column,
832 std::vector<unsigned int> const &,
833 bool onlydata, size_t max_length) const;
834 /// auxiliary function for docbook
835 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
837 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
838 bool header = false) const;
840 /// change associated Buffer
841 void setBuffer(Buffer & buffer);
842 /// retrieve associated Buffer
843 Buffer const & buffer() const { return *buffer_; }
844 /// retrieve associated Buffer
845 Buffer & buffer() { return *buffer_; }
853 class InsetTabular : public Inset
857 InsetTabular(Buffer *, row_type rows = 1,
858 col_type columns = 1);
862 void setBuffer(Buffer & buffer);
865 static void string2params(std::string const &, InsetTabular &);
867 static std::string params2string(InsetTabular const &);
871 void write(std::ostream &) const;
873 void metrics(MetricsInfo &, Dimension &) const;
875 void draw(PainterInfo & pi, int x, int y) const;
877 void drawSelection(PainterInfo & pi, int x, int y) const;
879 void drawBackground(PainterInfo & pi, int x, int y) const;
881 bool editable() const { return true; }
883 bool hasSettings() const { return true; }
885 bool insetAllowed(InsetCode code) const;
887 bool allowSpellCheck() const { return true; }
889 bool canTrackChanges() const { return true; }
891 bool canPaintChange(BufferView const &) const { return true; }
892 /** returns false if, when outputing LaTeX, font changes should
893 be closed before generating this inset. This is needed for
894 insets that may contain several paragraphs */
895 bool inheritFont() const { return false; }
897 bool allowsCaptionVariation(std::string const &) const;
899 bool isTable() const { return true; }
901 DisplayType display() const;
903 void latex(otexstream &, OutputParams const &) const;
905 int plaintext(odocstringstream & ods, OutputParams const & op,
906 size_t max_length = INT_MAX) const;
908 int docbook(odocstream &, OutputParams const &) const;
910 docstring xhtml(XHTMLStream &, OutputParams const &) const;
912 void validate(LaTeXFeatures & features) const;
914 InsetCode lyxCode() const { return TABULAR_CODE; }
916 std::string contextMenu(BufferView const &, int, int) const;
918 std::string contextMenuName() const;
919 /// get offset of this cursor slice relative to our upper left corner
920 void cursorPos(BufferView const & bv, CursorSlice const & sl,
921 bool boundary, int & x, int & y) const;
922 /// Executes a space-separated sequence of tabular-features requests
923 void tabularFeatures(Cursor & cur, std::string const & what);
924 /// Change a single tabular feature; does not handle undo.
925 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
926 std::string const & val = std::string());
928 size_t nargs() const { return tabular.numberofcells; }
930 std::shared_ptr<InsetTableCell const> cell(idx_type) const;
932 std::shared_ptr<InsetTableCell> cell(idx_type);
934 Text * getText(int) const;
936 /// set the change for the entire inset
937 void setChange(Change const & change);
938 /// accept the changes within the inset
939 void acceptChanges();
940 /// reject the changes within the inset
941 void rejectChanges();
943 // this should return true if we have a "normal" cell, otherwise false.
944 // "normal" means without width set!
945 /// should all paragraphs be output with "Standard" layout?
946 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
948 virtual bool forcePlainLayout(idx_type cell = 0) const;
950 void addPreview(DocIterator const & inset_pos,
951 graphics::PreviewLoader &) const;
953 /// lock cell with given index
954 void edit(Cursor & cur, bool front, EntryDirection entry_from);
955 /// get table row from x coordinate
956 int rowFromY(Cursor & cur, int y) const;
957 /// get table column from y coordinate
958 int columnFromX(Cursor & cur, int x) const;
960 Inset * editXY(Cursor & cur, int x, int y);
961 /// can we go further down on mouse click?
962 bool descendable(BufferView const &) const { return true; }
963 /// Update the counters of this inset and of its contents
964 void updateBuffer(ParIterator const &, UpdateType);
966 void addToToc(DocIterator const & di, bool output_active,
967 UpdateType utype, TocBackend & backend) const;
970 bool completionSupported(Cursor const &) const;
972 bool inlineCompletionSupported(Cursor const & cur) const;
974 bool automaticInlineCompletion() const;
976 bool automaticPopupCompletion() const;
978 bool showCompletionCursor() const;
980 CompletionList const * createCompletionList(Cursor const & cur) const;
982 docstring completionPrefix(Cursor const & cur) const;
984 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
986 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
988 virtual bool usePlainLayout() const { return true; }
990 docstring layoutName() const { return from_ascii("Tabular"); }
994 InsetTabular * asInsetTabular() { return this; }
996 InsetTabular const * asInsetTabular() const { return this; }
998 bool isRightToLeft(Cursor & cur) const;
999 /// writes the cells between stidx and enidx as a string, optionally
1000 /// descending into the insets
1001 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
1003 /// Returns whether the cell in the specified row and column is selected.
1004 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1006 void setLayoutForHiddenCells(DocumentClass const & dc);
1008 // Public structures and variables
1010 mutable Tabular tabular;
1014 InsetTabular(InsetTabular const &);
1016 void doDispatch(Cursor & cur, FuncRequest & cmd);
1018 bool getFeatureStatus(Cursor & cur, std::string const & s,
1019 std::string const & argument, FuncStatus & status) const;
1021 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1023 Inset * clone() const { return new InsetTabular(*this); }
1026 bool hitSelectRow(BufferView const & bv, int x) const;
1028 bool hitSelectColumn(BufferView const & bv, int y) const;
1029 /// Returns true if coordinates are on row/column selection zones
1030 bool clickable(BufferView const &, int x, int y) const;
1033 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1034 idx_type cell) const;
1036 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1039 void moveNextCell(Cursor & cur,
1040 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1042 void movePrevCell(Cursor & cur,
1043 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1045 int cellXPos(idx_type cell) const;
1047 int cellYPos(idx_type cell) const;
1049 bool copySelection(Cursor & cur);
1051 bool pasteClipboard(Cursor & cur);
1053 void cutSelection(Cursor & cur);
1055 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1056 col_type & cs, col_type & ce) const;
1058 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1060 /// return the "Manhattan distance" to nearest corner
1061 int dist(BufferView &, idx_type cell, int x, int y) const;
1062 /// return the cell nearest to x, y
1063 idx_type getNearestCell(BufferView &, int x, int y) const;
1065 /// test the rotation state of the given cell range.
1066 bool oneCellHasRotationState(bool rotated,
1067 row_type row_start, row_type row_end,
1068 col_type col_start, col_type col_end) const;
1070 /// true when selecting rows with the mouse
1072 /// true when selecting columns with the mouse
1076 std::string const featureAsString(Tabular::Feature feature);
1078 /// Split cell on decimal symbol
1079 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1083 #endif // INSET_TABULAR_H