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
28 #include "InsetText.h"
31 #include "support/shared_ptr.h"
52 class InsetTableCell : public InsetText
56 InsetTableCell(Buffer * buf);
58 InsetCode lyxCode() const { return CELL_CODE; }
60 Inset * clone() const { return new InsetTableCell(*this); }
62 bool getStatus(Cursor & cur, FuncRequest const & cmd,
63 FuncStatus & status) const;
65 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
67 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
68 /// writes the contents of the cell as a string, optionally
69 /// descending into insets
70 docstring asString(bool intoInsets = true);
72 docstring xhtml(XHTMLStream &, OutputParams const &) const;
74 void addToToc(DocIterator const & di, bool output_active,
75 UpdateType utype) const;
80 void operator=(InsetTableCell const &);
82 // This boolean is supposed to track whether the cell has had its
83 // width explicitly set. We need to know this to determine whether
84 // layout changes and paragraph customization are allowed---that is,
85 // we need it in forcePlainLayout() and allowParagraphCustomization().
86 // Unfortunately, that information is not readily available in
87 // InsetTableCell. In the case of multicolumn cells, it is present
88 // in CellData, and so would be available here if CellData were to
89 // become a member of InsetTableCell. But in the other case, it isn't
90 // even available there, but is held in Tabular::ColumnData.
91 // So, the present solution uses this boolean to track the information
92 // we need to track, and tries to keep it updated. This is not ideal,
93 // but the other solutions are no better. These are:
94 // (i) Keep a pointer in InsetTableCell to the table;
95 // (ii) Find the table by iterating over the Buffer's insets.
96 // Solution (i) raises the problem of updating the pointer when an
97 // InsetTableCell is copied, and we'd therefore need a copy constructor
98 // in InsetTabular and then in Tabular, which seems messy, given how
99 // complicated those classes are. Solution (ii) involves a lot of
100 // iterating, since this information is needed quite often, and so may
102 // So, well, if someone can do better, please do!
106 // FIXME: Here the thoughts from the comment above also apply.
108 LyXAlignment contentAlign;
109 /// should paragraph indendation be omitted in any case?
110 bool neverIndent() const { return true; }
112 LyXAlignment contentAlignment() const { return contentAlign; }
114 virtual bool usePlainLayout() const { return true; }
116 virtual bool forcePlainLayout(idx_type = 0) const;
118 virtual bool allowParagraphCustomization(idx_type = 0) const;
120 virtual bool forceLocalFontSwitch() const;
121 /// Is the width forced to some value?
122 bool hasFixedWidth() const { return isFixedWidth; }
123 /// Can the cell contain several paragraphs?
124 /** FIXME this is wrong for multirows, that are limited to one
125 * paragraph. However, we cannot test for this (see the big
128 bool allowMultiPar() const { return isFixedWidth; }
133 // A helper struct for tables
232 UNSET_ROTATE_TABULAR,
234 TOGGLE_ROTATE_TABULAR,
267 SET_SPECIAL_MULTICOLUMN,
283 TABULAR_VALIGN_MIDDLE,
285 TABULAR_VALIGN_BOTTOM,
287 LONGTABULAR_ALIGN_LEFT,
289 LONGTABULAR_ALIGN_CENTER,
291 LONGTABULAR_ALIGN_RIGHT,
304 CELL_BEGIN_OF_MULTICOLUMN,
306 CELL_PART_OF_MULTICOLUMN,
308 CELL_BEGIN_OF_MULTIROW,
310 CELL_PART_OF_MULTIROW
318 LYX_VALIGN_MIDDLE = 1,
320 LYX_VALIGN_BOTTOM = 2
326 LYX_LONGTABULAR_ALIGN_LEFT = 0,
328 LYX_LONGTABULAR_ALIGN_CENTER = 1,
330 LYX_LONGTABULAR_ALIGN_RIGHT = 2
369 // we have this header type (is set in the getLT... functions)
371 // double borders on top
373 // double borders on bottom
375 // used for FirstHeader & LastFooter and if this is true
376 // all the rows marked as FirstHeader or LastFooter are
377 // ignored in the output and it is set to be empty!
381 /// type for row numbers
382 typedef size_t row_type;
383 /// type for column numbers
384 typedef size_t col_type;
385 /// type for cell indices
386 typedef size_t idx_type;
387 /// index indicating an invalid position
388 static const idx_type npos = static_cast<idx_type>(-1);
391 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
393 /// Returns true if there is a topline, returns false if not
394 bool topLine(idx_type cell) const;
395 /// Returns true if there is a topline, returns false if not
396 bool bottomLine(idx_type cell) const;
397 /// Returns true if there is a topline, returns false if not
398 bool leftLine(idx_type cell) const;
399 /// Returns true if there is a topline, returns false if not
400 bool rightLine(idx_type cell) const;
402 /// return space occupied by the second horizontal line and
403 /// interline space above row \p row in pixels
404 int interRowSpace(row_type row) const;
406 int interColumnSpace(idx_type cell) const;
408 /* returns the maximum over all rows */
410 int cellWidth(idx_type cell) const;
412 int cellHeight(idx_type cell) const;
418 row_type nrows() const {return row_info.size();}
420 col_type ncols() const {return column_info.size();}
422 int rowAscent(row_type row) const;
424 int rowDescent(row_type row) const;
426 void setRowAscent(row_type row, int height);
428 void setRowDescent(row_type row, int height);
430 void setTopLine(idx_type cell, bool line);
432 void setBottomLine(idx_type cell, bool line);
434 void setLeftLine(idx_type cell, bool line);
436 void setRightLine(idx_type cell, bool line);
438 bool rowTopLine(row_type row) const;
440 bool rowBottomLine(row_type row) const;
442 bool columnLeftLine(col_type column) const;
444 bool columnRightLine(col_type column) const;
446 void setAlignment(idx_type cell, LyXAlignment align,
447 bool onlycolumn = false);
449 void setVAlignment(idx_type cell, VAlignment align,
450 bool onlycolumn = false);
452 void setTabularWidth(Length const & l) { tabular_width = l; }
454 Length tabularWidth() const { return tabular_width; }
456 void setColumnPWidth(Cursor &, idx_type, Length const &);
458 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
460 bool setMROffset(Cursor &, idx_type, Length const &);
462 void setAlignSpecial(idx_type cell, docstring const & special,
465 LyXAlignment getAlignment(idx_type cell,
466 bool onlycolumn = false) const;
468 VAlignment getVAlignment(idx_type cell,
469 bool onlycolumn = false) const;
471 Length const getPWidth(idx_type cell) const;
473 Length const getMROffset(idx_type cell) const;
475 int textHOffset(idx_type cell) const;
477 int textVOffset(idx_type cell) const;
479 void appendRow(row_type row);
481 void deleteRow(row_type row);
483 void copyRow(row_type row);
485 void insertRow(row_type row, bool copy);
487 void moveColumn(col_type col, ColDirection direction);
489 void moveRow(row_type row, RowDirection direction);
491 void appendColumn(col_type column);
493 void deleteColumn(col_type column);
495 void copyColumn(col_type column);
497 void insertColumn(col_type column, bool copy);
499 idx_type getFirstCellInRow(row_type row) const;
501 idx_type getLastCellInRow(row_type row) const;
503 idx_type numberOfCellsInRow(row_type row) const;
505 void write(std::ostream &) const;
509 void latex(otexstream &, OutputParams const &) const;
511 int docbook(odocstream & os, OutputParams const &) const;
513 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
515 void plaintext(odocstringstream &,
516 OutputParams const & runparams, int const depth,
517 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
519 bool isMultiColumn(idx_type cell) const;
521 bool hasMultiColumn(col_type cell) const;
523 idx_type setMultiColumn(idx_type cell, idx_type number,
524 bool const right_border);
526 void unsetMultiColumn(idx_type cell);
528 bool isPartOfMultiColumn(row_type row, col_type column) const;
530 bool isPartOfMultiRow(row_type row, col_type column) const;
532 bool isMultiRow(idx_type cell) const;
534 bool hasMultiRow(row_type r) const;
536 idx_type setMultiRow(idx_type cell, idx_type number,
537 bool const bottom_border,
538 LyXAlignment const halign);
540 void unsetMultiRow(idx_type cell);
542 row_type cellRow(idx_type cell) const;
544 col_type cellColumn(idx_type cell) const;
546 void setRotateCell(idx_type cell, int);
548 int getRotateCell(idx_type cell) const;
550 bool needRotating() const;
552 bool isLastCell(idx_type cell) const;
554 idx_type cellAbove(idx_type cell) const;
556 idx_type cellBelow(idx_type cell) const;
557 /// \return the index of the VISIBLE cell at row, column
558 /// this will be the same as the cell in the previous row,
559 /// e.g., if the cell is part of a multirow
560 idx_type cellIndex(row_type row, col_type column) const;
562 void setUsebox(idx_type cell, BoxType);
564 BoxType getUsebox(idx_type cell) const;
566 // Long Tabular Options support functions
568 void setLTHead(row_type row, bool flag, ltType const &, bool first);
570 bool getRowOfLTHead(row_type row, ltType &) const;
572 bool getRowOfLTFirstHead(row_type row, ltType &) const;
574 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
576 bool getRowOfLTFoot(row_type row, ltType &) const;
578 bool getRowOfLTLastFoot(row_type row, ltType &) const;
580 void setLTNewPage(row_type row, bool what);
582 bool getLTNewPage(row_type row) const;
584 idx_type setLTCaption(row_type row, bool what);
586 bool ltCaption(row_type row) const;
588 bool haveLTHead(bool withcaptions = true) const;
590 bool haveLTFirstHead(bool withcaptions = true) const;
592 bool haveLTFoot(bool withcaptions = true) const;
594 bool haveLTLastFoot(bool withcaptions = true) const;
596 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
598 // end longtable support
601 /// there is a subtle difference between these two methods.
604 /// cellInset(cellIndex(r,c));
605 /// can return different things. this is because cellIndex(r,c)
606 /// returns the VISIBLE cell at r,c, which may be the same as the
607 /// cell at the previous row or column, if we're dealing with some
608 /// multirow or multicell.
609 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
610 shared_ptr<InsetTableCell> cellInset(row_type row,
611 col_type column) const;
614 void setCellInset(row_type row, col_type column,
615 shared_ptr<InsetTableCell>) const;
616 /// Search for \param inset in the tabular, with the
618 void validate(LaTeXFeatures &) const;
621 // FIXME Now that cells have an InsetTableCell as their insets, rather
622 // than an InsetText, it'd be possible to reverse the relationship here,
623 // so that cell_vector was a vector<InsetTableCell> rather than a
624 // vector<CellData>, and an InsetTableCell had a CellData as a member,
625 // or perhaps just had its members as members.
632 CellData(CellData const &);
634 CellData & operator=(CellData const &);
646 LyXAlignment alignment;
648 VAlignment valignment;
649 /// width of the part before the decimal
651 /// width of the decimal part
668 docstring align_special;
670 Length p_width; // this is only set for multicolumn!!!
672 shared_ptr<InsetTableCell> inset;
674 CellData & cellInfo(idx_type cell) const;
676 typedef std::vector<CellData> cell_vector;
678 typedef std::vector<cell_vector> cell_vvector;
689 /// Extra space between the top line and this row
691 /// Ignore top_space if true and use the default top space
692 bool top_space_default;
693 /// Extra space between this row and the bottom line
695 /// Ignore bottom_space if true and use the default bottom space
696 bool bottom_space_default;
697 /// Extra space between the bottom line and the next top line
698 Length interline_space;
699 /// Ignore interline_space if true and use the default interline space
700 bool interline_space_default;
701 /// This are for longtabulars only
704 /// a row of endfirsthead
708 /// row of endlastfoot
710 /// row for a newpage
716 typedef std::vector<RowData> row_vector;
724 LyXAlignment alignment;
726 VAlignment valignment;
732 docstring align_special;
734 docstring decimal_point;
737 typedef std::vector<ColumnData> column_vector;
740 idx_type numberofcells;
742 std::vector<row_type> rowofcell;
744 std::vector<col_type> columnofcell;
748 column_vector column_info;
750 mutable cell_vvector cell_info;
752 Length tabular_width;
758 VAlignment tabular_valignment;
762 HAlignment longtabular_alignment;
764 bool is_long_tabular;
767 /// endfirsthead data
775 void init(Buffer *, row_type rows_arg,
776 col_type columns_arg);
778 void updateIndexes();
780 bool setFixedWidth(row_type r, col_type c);
781 /// return true of update is needed
782 bool updateColumnWidths();
784 idx_type columnSpan(idx_type cell) const;
786 idx_type rowSpan(idx_type cell) const;
788 BoxType useParbox(idx_type cell) const;
790 // helper function for Latex
792 void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
794 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
796 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
798 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
800 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
802 bool isValidRow(row_type const row) const;
804 void TeXRow(otexstream &, row_type const row,
805 OutputParams const &) const;
807 // helper functions for plain text
809 bool plaintextTopHLine(odocstringstream &, row_type row,
810 std::vector<unsigned int> const &) const;
812 bool plaintextBottomHLine(odocstringstream &, row_type row,
813 std::vector<unsigned int> const &) const;
815 void plaintextPrintCell(odocstringstream &,
816 OutputParams const &,
817 idx_type cell, row_type row, col_type column,
818 std::vector<unsigned int> const &,
819 bool onlydata, size_t max_length) const;
820 /// auxiliary function for docbook
821 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
823 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
824 bool header = false) const;
826 /// change associated Buffer
827 void setBuffer(Buffer & buffer);
828 /// retrieve associated Buffer
829 Buffer & buffer() const { return *buffer_; }
837 class InsetTabular : public Inset
841 InsetTabular(Buffer *, row_type rows = 1,
842 col_type columns = 1);
846 void setBuffer(Buffer & buffer);
849 static void string2params(std::string const &, InsetTabular &);
851 static std::string params2string(InsetTabular const &);
855 void write(std::ostream &) const;
857 void metrics(MetricsInfo &, Dimension &) const;
859 void draw(PainterInfo & pi, int x, int y) const;
861 void drawSelection(PainterInfo & pi, int x, int y) const;
863 void drawBackground(PainterInfo & pi, int x, int y) const;
865 bool editable() const { return true; }
867 bool hasSettings() const { return true; }
869 bool insetAllowed(InsetCode code) const;
871 bool allowSpellCheck() const { return true; }
873 bool canTrackChanges() const { return true; }
874 /** returns false if, when outputing LaTeX, font changes should
875 be closed before generating this inset. This is needed for
876 insets that may contain several paragraphs */
877 bool inheritFont() const { return false; }
879 bool allowsCaptionVariation(std::string const &) const;
881 DisplayType display() const;
883 void latex(otexstream &, OutputParams const &) const;
885 int plaintext(odocstringstream & ods, OutputParams const & op,
886 size_t max_length = INT_MAX) const;
888 int docbook(odocstream &, OutputParams const &) const;
890 docstring xhtml(XHTMLStream &, OutputParams const &) const;
892 void validate(LaTeXFeatures & features) const;
894 InsetCode lyxCode() const { return TABULAR_CODE; }
896 std::string contextMenu(BufferView const &, int, int) const;
898 std::string contextMenuName() const;
899 /// get offset of this cursor slice relative to our upper left corner
900 void cursorPos(BufferView const & bv, CursorSlice const & sl,
901 bool boundary, int & x, int & y) const;
903 bool tabularFeatures(Cursor & cur, std::string const & what);
905 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
906 std::string const & val = std::string());
908 size_t nargs() const { return tabular.numberofcells; }
910 shared_ptr<InsetTableCell const> cell(idx_type) const;
912 shared_ptr<InsetTableCell> cell(idx_type);
914 Text * getText(int) const;
916 /// set the change for the entire inset
917 void setChange(Change const & change);
918 /// accept the changes within the inset
919 void acceptChanges();
920 /// reject the changes within the inset
921 void rejectChanges();
923 // this should return true if we have a "normal" cell, otherwise false.
924 // "normal" means without width set!
925 /// should all paragraphs be output with "Standard" layout?
926 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
928 virtual bool forcePlainLayout(idx_type cell = 0) const;
930 void addPreview(DocIterator const & inset_pos,
931 graphics::PreviewLoader &) const;
933 /// lock cell with given index
934 void edit(Cursor & cur, bool front, EntryDirection entry_from);
935 /// get table row from x coordinate
936 int rowFromY(Cursor & cur, int y) const;
937 /// get table column from y coordinate
938 int columnFromX(Cursor & cur, int x) const;
940 Inset * editXY(Cursor & cur, int x, int y);
941 /// can we go further down on mouse click?
942 bool descendable(BufferView const &) const { return true; }
943 /// Update the counters of this inset and of its contents
944 void updateBuffer(ParIterator const &, UpdateType);
946 void addToToc(DocIterator const & di, bool output_active,
947 UpdateType utype) const;
950 bool completionSupported(Cursor const &) const;
952 bool inlineCompletionSupported(Cursor const & cur) const;
954 bool automaticInlineCompletion() const;
956 bool automaticPopupCompletion() const;
958 bool showCompletionCursor() const;
960 CompletionList const * createCompletionList(Cursor const & cur) const;
962 docstring completionPrefix(Cursor const & cur) const;
964 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
966 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
968 virtual bool usePlainLayout() const { return true; }
971 InsetTabular * asInsetTabular() { return this; }
973 InsetTabular const * asInsetTabular() const { return this; }
975 bool isRightToLeft(Cursor & cur) const;
976 /// writes the cells between stidx and enidx as a string, optionally
977 /// descending into the insets
978 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
980 /// Returns whether the cell in the specified row and column is selected.
981 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
983 void setLayoutForHiddenCells(DocumentClass const & dc);
985 // Public structures and variables
987 mutable Tabular tabular;
991 InsetTabular(InsetTabular const &);
993 void doDispatch(Cursor & cur, FuncRequest & cmd);
995 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
997 Inset * clone() const { return new InsetTabular(*this); }
1000 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1001 idx_type cell) const;
1003 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1006 void moveNextCell(Cursor & cur,
1007 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1009 void movePrevCell(Cursor & cur,
1010 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1012 int cellXPos(idx_type cell) const;
1014 int cellYPos(idx_type cell) const;
1016 bool copySelection(Cursor & cur);
1018 bool pasteClipboard(Cursor & cur);
1020 void cutSelection(Cursor & cur);
1022 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1023 col_type & cs, col_type & ce) const;
1025 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1027 /// return the "Manhattan distance" to nearest corner
1028 int dist(BufferView &, idx_type cell, int x, int y) const;
1029 /// return the cell nearest to x, y
1030 idx_type getNearestCell(BufferView &, int x, int y) const;
1032 /// test the rotation state of the give cell range.
1033 bool oneCellHasRotationState(bool rotated,
1034 row_type row_start, row_type row_end,
1035 col_type col_start, col_type col_end) const;
1037 mutable idx_type first_visible_cell_;
1038 /// The vertical offset of the table due to the vertical
1039 /// alignment with respect to the baseline.
1040 mutable int offset_valign_;
1041 /// true when selecting rows with the mouse
1043 /// true when selecting columns with the mouse
1047 std::string const featureAsString(Tabular::Feature feature);
1049 /// Split cell on decimal symbol
1050 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1054 #endif // INSET_TABULAR_H