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
231 TOGGLE_VARWIDTH_COLUMN,
235 UNSET_ROTATE_TABULAR,
237 TOGGLE_ROTATE_TABULAR,
270 SET_SPECIAL_MULTICOLUMN,
288 TABULAR_VALIGN_MIDDLE,
290 TABULAR_VALIGN_BOTTOM,
292 LONGTABULAR_ALIGN_LEFT,
294 LONGTABULAR_ALIGN_CENTER,
296 LONGTABULAR_ALIGN_RIGHT,
309 CELL_BEGIN_OF_MULTICOLUMN,
311 CELL_PART_OF_MULTICOLUMN,
313 CELL_BEGIN_OF_MULTIROW,
315 CELL_PART_OF_MULTIROW
323 LYX_VALIGN_MIDDLE = 1,
325 LYX_VALIGN_BOTTOM = 2
331 LYX_LONGTABULAR_ALIGN_LEFT = 0,
333 LYX_LONGTABULAR_ALIGN_CENTER = 1,
335 LYX_LONGTABULAR_ALIGN_RIGHT = 2
376 // we have this header type (is set in the getLT... functions)
378 // double borders on top
380 // double borders on bottom
382 // used for FirstHeader & LastFooter and if this is true
383 // all the rows marked as FirstHeader or LastFooter are
384 // ignored in the output and it is set to be empty!
388 /// type for row numbers
389 typedef size_t row_type;
390 /// type for column numbers
391 typedef size_t col_type;
392 /// type for cell indices
393 typedef size_t idx_type;
394 /// index indicating an invalid position
395 static const idx_type npos = static_cast<idx_type>(-1);
398 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
400 /// Returns true if there is a topline, returns false if not
401 bool topLine(idx_type cell) const;
402 /// Returns true if there is a topline, returns false if not
403 bool bottomLine(idx_type cell) const;
404 /// Returns true if there is a topline, returns false if not
405 bool leftLine(idx_type cell) const;
406 /// Returns true if there is a topline, returns false if not
407 bool rightLine(idx_type cell) const;
409 /// return space occupied by the second horizontal line and
410 /// interline space above row \p row in pixels
411 int interRowSpace(row_type row) const;
413 int interColumnSpace(idx_type cell) const;
415 /* returns the maximum over all rows */
417 int cellWidth(idx_type cell) const;
419 int cellHeight(idx_type cell) const;
425 row_type nrows() const {return row_info.size();}
427 col_type ncols() const {return column_info.size();}
429 int rowAscent(row_type row) const;
431 int rowDescent(row_type row) const;
433 void setRowAscent(row_type row, int height);
435 void setRowDescent(row_type row, int height);
437 void setTopLine(idx_type cell, bool line);
439 void setBottomLine(idx_type cell, bool line);
441 void setLeftLine(idx_type cell, bool line);
443 void setRightLine(idx_type cell, bool line);
445 bool rowTopLine(row_type row) const;
447 bool rowBottomLine(row_type row) const;
449 bool columnLeftLine(col_type column) const;
451 bool columnRightLine(col_type column) const;
453 void setAlignment(idx_type cell, LyXAlignment align,
454 bool onlycolumn = false);
456 void setVAlignment(idx_type cell, VAlignment align,
457 bool onlycolumn = false);
459 void setTabularWidth(Length const & l) { tabular_width = l; }
461 Length tabularWidth() const { return tabular_width; }
463 void setColumnPWidth(Cursor &, idx_type, Length const &);
465 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
467 bool toggleVarwidth(idx_type, bool const);
469 bool setMROffset(Cursor &, idx_type, Length const &);
471 void setAlignSpecial(idx_type cell, docstring const & special,
474 LyXAlignment getAlignment(idx_type cell,
475 bool onlycolumn = false) const;
477 VAlignment getVAlignment(idx_type cell,
478 bool onlycolumn = false) const;
479 /// The vertical offset of the table due to the vertical
480 /// alignment with respect to the baseline.
481 int offsetVAlignment() const;
483 Length const getPWidth(idx_type cell) const;
485 Length const getMROffset(idx_type cell) const;
487 int textHOffset(idx_type cell) const;
489 int textVOffset(idx_type cell) const;
491 void appendRow(row_type row);
493 void deleteRow(row_type row);
495 void copyRow(row_type row);
497 void insertRow(row_type row, bool copy);
499 void moveColumn(col_type col, ColDirection direction);
501 void moveRow(row_type row, RowDirection direction);
503 void appendColumn(col_type column);
505 void deleteColumn(col_type column);
507 void copyColumn(col_type column);
509 void insertColumn(col_type column, bool copy);
511 idx_type getFirstCellInRow(row_type row) const;
513 idx_type getLastCellInRow(row_type row) const;
515 idx_type numberOfCellsInRow(row_type row) const;
517 void write(std::ostream &) const;
521 void latex(otexstream &, OutputParams const &) const;
523 int docbook(odocstream & os, OutputParams const &) const;
525 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
527 void plaintext(odocstringstream &,
528 OutputParams const & runparams, int const depth,
529 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
531 bool isMultiColumn(idx_type cell) const;
533 bool hasMultiColumn(col_type cell) const;
535 bool hasVarwidthColumn() const;
537 idx_type setMultiColumn(idx_type cell, idx_type number,
538 bool const right_border);
540 void unsetMultiColumn(idx_type cell);
542 bool isPartOfMultiColumn(row_type row, col_type column) const;
544 bool isPartOfMultiRow(row_type row, col_type column) const;
546 bool isMultiRow(idx_type cell) const;
548 bool hasMultiRow(row_type r) const;
550 idx_type setMultiRow(idx_type cell, idx_type number,
551 bool const bottom_border,
552 LyXAlignment const halign);
554 void unsetMultiRow(idx_type cell);
556 row_type cellRow(idx_type cell) const;
558 col_type cellColumn(idx_type cell) const;
560 void setRotateCell(idx_type cell, int);
562 int getRotateCell(idx_type cell) const;
564 bool needRotating() const;
566 bool isLastCell(idx_type cell) const;
568 idx_type cellAbove(idx_type cell) const;
570 idx_type cellBelow(idx_type cell) const;
571 /// \return the index of the VISIBLE cell at row, column
572 /// this will be the same as the cell in the previous row,
573 /// e.g., if the cell is part of a multirow
574 idx_type cellIndex(row_type row, col_type column) const;
576 void setUsebox(idx_type cell, BoxType);
578 BoxType getUsebox(idx_type cell) const;
580 // Long Tabular Options support functions
582 void setLTHead(row_type row, bool flag, ltType const &, bool first);
584 bool getRowOfLTHead(row_type row, ltType &) const;
586 bool getRowOfLTFirstHead(row_type row, ltType &) const;
588 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
590 bool getRowOfLTFoot(row_type row, ltType &) const;
592 bool getRowOfLTLastFoot(row_type row, ltType &) const;
594 void setLTNewPage(row_type row, bool what);
596 bool getLTNewPage(row_type row) const;
598 idx_type setLTCaption(row_type row, bool what);
600 bool ltCaption(row_type row) const;
602 bool haveLTHead(bool withcaptions = true) const;
604 bool haveLTFirstHead(bool withcaptions = true) const;
606 bool haveLTFoot(bool withcaptions = true) const;
608 bool haveLTLastFoot(bool withcaptions = true) const;
610 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
612 // end longtable support
615 /// there is a subtle difference between these two methods.
618 /// cellInset(cellIndex(r,c));
619 /// can return different things. this is because cellIndex(r,c)
620 /// returns the VISIBLE cell at r,c, which may be the same as the
621 /// cell at the previous row or column, if we're dealing with some
622 /// multirow or multicell.
623 std::shared_ptr<InsetTableCell> cellInset(idx_type cell);
624 std::shared_ptr<InsetTableCell> cellInset(row_type row, col_type column);
625 InsetTableCell const * cellInset(idx_type cell) const;
628 void setCellInset(row_type row, col_type column,
629 std::shared_ptr<InsetTableCell>);
630 /// Search for \param inset in the tabular, with the
632 void validate(LaTeXFeatures &) const;
635 // FIXME Now that cells have an InsetTableCell as their insets, rather
636 // than an InsetText, it'd be possible to reverse the relationship here,
637 // so that cell_vector was a vector<InsetTableCell> rather than a
638 // vector<CellData>, and an InsetTableCell had a CellData as a member,
639 // or perhaps just had its members as members.
646 CellData(CellData const &);
648 CellData & operator=(CellData const &);
660 LyXAlignment alignment;
662 VAlignment valignment;
663 /// width of the part before the decimal
665 /// width of the decimal part
682 docstring align_special;
684 Length p_width; // this is only set for multicolumn!!!
686 std::shared_ptr<InsetTableCell> inset;
689 CellData const & cellInfo(idx_type cell) const;
691 CellData & cellInfo(idx_type cell);
693 typedef std::vector<CellData> cell_vector;
695 typedef std::vector<cell_vector> cell_vvector;
706 /// Extra space between the top line and this row
708 /// Ignore top_space if true and use the default top space
709 bool top_space_default;
710 /// Extra space between this row and the bottom line
712 /// Ignore bottom_space if true and use the default bottom space
713 bool bottom_space_default;
714 /// Extra space between the bottom line and the next top line
715 Length interline_space;
716 /// Ignore interline_space if true and use the default interline space
717 bool interline_space_default;
718 /// This are for longtabulars only
721 /// a row of endfirsthead
725 /// row of endlastfoot
727 /// row for a newpage
733 typedef std::vector<RowData> row_vector;
741 LyXAlignment alignment;
743 VAlignment valignment;
749 docstring align_special;
751 docstring decimal_point;
756 typedef std::vector<ColumnData> column_vector;
759 idx_type numberofcells;
761 std::vector<row_type> rowofcell;
763 std::vector<col_type> columnofcell;
767 column_vector column_info;
769 cell_vvector cell_info;
771 Length tabular_width;
777 VAlignment tabular_valignment;
781 HAlignment longtabular_alignment;
783 bool is_long_tabular;
786 /// endfirsthead data
794 void init(Buffer *, row_type rows_arg,
795 col_type columns_arg);
797 void updateIndexes();
799 bool setFixedWidth(row_type r, col_type c);
800 /// return true of update is needed
801 bool updateColumnWidths();
803 idx_type columnSpan(idx_type cell) const;
805 idx_type rowSpan(idx_type cell) const;
807 BoxType useParbox(idx_type cell) const;
809 // helper function for Latex
811 void TeXTopHLine(otexstream &, row_type row, std::string const & lang,
812 std::list<col_type>) const;
814 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang,
815 std::list<col_type>) const;
817 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow,
818 bool const bidi) const;
820 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
822 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &, std::list<col_type>) const;
824 bool isValidRow(row_type const row) const;
826 void TeXRow(otexstream &, row_type const row,
827 OutputParams const &, std::list<col_type>) const;
829 // helper functions for plain text
831 bool plaintextTopHLine(odocstringstream &, row_type row,
832 std::vector<unsigned int> const &) const;
834 bool plaintextBottomHLine(odocstringstream &, row_type row,
835 std::vector<unsigned int> const &) const;
837 void plaintextPrintCell(odocstringstream &,
838 OutputParams const &,
839 idx_type cell, row_type row, col_type column,
840 std::vector<unsigned int> const &,
841 bool onlydata, size_t max_length) const;
842 /// auxiliary function for docbook
843 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
845 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
846 bool header = false) const;
848 /// change associated Buffer
849 void setBuffer(Buffer & buffer);
850 /// retrieve associated Buffer
851 Buffer const & buffer() const { return *buffer_; }
852 /// retrieve associated Buffer
853 Buffer & buffer() { return *buffer_; }
861 class InsetTabular : public Inset
865 InsetTabular(Buffer *, row_type rows = 1,
866 col_type columns = 1);
870 void setBuffer(Buffer & buffer);
873 static void string2params(std::string const &, InsetTabular &);
875 static std::string params2string(InsetTabular const &);
879 void write(std::ostream &) const;
881 void metrics(MetricsInfo &, Dimension &) const;
883 void draw(PainterInfo & pi, int x, int y) const;
885 void drawSelection(PainterInfo & pi, int x, int y) const;
887 void drawBackground(PainterInfo & pi, int x, int y) const;
889 bool editable() const { return true; }
891 bool hasSettings() const { return true; }
893 bool insetAllowed(InsetCode code) const;
895 bool allowSpellCheck() const { return true; }
897 bool canTrackChanges() const { return true; }
899 bool canPaintChange(BufferView const &) const { return true; }
900 /** returns false if, when outputing LaTeX, font changes should
901 be closed before generating this inset. This is needed for
902 insets that may contain several paragraphs */
903 bool inheritFont() const { return false; }
905 bool allowsCaptionVariation(std::string const &) const;
907 bool isTable() const { return true; }
909 DisplayType display() const;
911 void latex(otexstream &, OutputParams const &) const;
913 int plaintext(odocstringstream & ods, OutputParams const & op,
914 size_t max_length = INT_MAX) const;
916 int docbook(odocstream &, OutputParams const &) const;
918 docstring xhtml(XHTMLStream &, OutputParams const &) const;
920 void validate(LaTeXFeatures & features) const;
922 InsetCode lyxCode() const { return TABULAR_CODE; }
924 std::string contextMenu(BufferView const &, int, int) const;
926 std::string contextMenuName() const;
927 /// get offset of this cursor slice relative to our upper left corner
928 void cursorPos(BufferView const & bv, CursorSlice const & sl,
929 bool boundary, int & x, int & y) const;
930 /// Executes a space-separated sequence of tabular-features requests
931 void tabularFeatures(Cursor & cur, std::string const & what);
932 /// Change a single tabular feature; does not handle undo.
933 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
934 std::string const & val = std::string());
936 size_t nargs() const { return tabular.numberofcells; }
938 std::shared_ptr<InsetTableCell const> cell(idx_type) const;
940 std::shared_ptr<InsetTableCell> cell(idx_type);
942 Text * getText(int) const;
944 /// set the change for the entire inset
945 void setChange(Change const & change);
946 /// accept the changes within the inset
947 void acceptChanges();
948 /// reject the changes within the inset
949 void rejectChanges();
951 // this should return true if we have a "normal" cell, otherwise false.
952 // "normal" means without width set!
953 /// should all paragraphs be output with "Standard" layout?
954 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
956 virtual bool forcePlainLayout(idx_type cell = 0) const;
958 void addPreview(DocIterator const & inset_pos,
959 graphics::PreviewLoader &) const;
961 /// lock cell with given index
962 void edit(Cursor & cur, bool front, EntryDirection entry_from);
963 /// get table row from x coordinate
964 int rowFromY(Cursor & cur, int y) const;
965 /// get table column from y coordinate
966 int columnFromX(Cursor & cur, int x) const;
968 Inset * editXY(Cursor & cur, int x, int y);
969 /// can we go further down on mouse click?
970 bool descendable(BufferView const &) const { return true; }
971 /// Update the counters of this inset and of its contents
972 void updateBuffer(ParIterator const &, UpdateType);
974 void addToToc(DocIterator const & di, bool output_active,
975 UpdateType utype, TocBackend & backend) const;
978 bool completionSupported(Cursor const &) const;
980 bool inlineCompletionSupported(Cursor const & cur) const;
982 bool automaticInlineCompletion() const;
984 bool automaticPopupCompletion() const;
986 bool showCompletionCursor() const;
988 CompletionList const * createCompletionList(Cursor const & cur) const;
990 docstring completionPrefix(Cursor const & cur) const;
992 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
994 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
996 virtual bool usePlainLayout() const { return true; }
998 docstring layoutName() const { return from_ascii("Tabular"); }
1002 InsetTabular * asInsetTabular() { return this; }
1004 InsetTabular const * asInsetTabular() const { return this; }
1006 bool isRightToLeft(Cursor & cur) const;
1007 /// writes the cells between stidx and enidx as a string, optionally
1008 /// descending into the insets
1009 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
1011 /// Returns whether the cell in the specified row and column is selected.
1012 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
1014 void setLayoutForHiddenCells(DocumentClass const & dc);
1016 // Public structures and variables
1018 mutable Tabular tabular;
1022 InsetTabular(InsetTabular const &);
1024 void doDispatch(Cursor & cur, FuncRequest & cmd);
1026 bool getFeatureStatus(Cursor & cur, std::string const & s,
1027 std::string const & argument, FuncStatus & status) const;
1029 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1031 Inset * clone() const { return new InsetTabular(*this); }
1034 bool hitSelectRow(BufferView const & bv, int x) const;
1036 bool hitSelectColumn(BufferView const & bv, int y) const;
1037 /// Returns true if coordinates are on row/column selection zones
1038 bool clickable(BufferView const &, int x, int y) const;
1041 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1042 idx_type cell) const;
1044 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1047 void moveNextCell(Cursor & cur,
1048 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1050 void movePrevCell(Cursor & cur,
1051 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1053 int cellXPos(idx_type cell) const;
1055 int cellYPos(idx_type cell) const;
1057 bool copySelection(Cursor & cur);
1059 bool pasteClipboard(Cursor & cur);
1061 void cutSelection(Cursor & cur);
1063 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1064 col_type & cs, col_type & ce) const;
1066 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1068 /// return the "Manhattan distance" to nearest corner
1069 int dist(BufferView &, idx_type cell, int x, int y) const;
1070 /// return the cell nearest to x, y
1071 idx_type getNearestCell(BufferView &, int x, int y) const;
1073 /// test the rotation state of the given cell range.
1074 bool oneCellHasRotationState(bool rotated,
1075 row_type row_start, row_type row_end,
1076 col_type col_start, col_type col_end) const;
1078 /// true when selecting rows with the mouse
1080 /// true when selecting columns with the mouse
1084 std::string const featureAsString(Tabular::Feature feature);
1086 /// Split cell on decimal symbol
1087 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1091 #endif // INSET_TABULAR_H