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) 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;
118 /// Is the width forced to some value?
119 bool hasFixedWidth() const { return isFixedWidth; }
120 /// Can the cell contain several paragraphs?
121 /** FIXME this is wrong for multirows, that are limited to one
122 * paragraph. However, we cannot test for this (see the big
125 bool allowMultiPar() const { return isFixedWidth; }
130 // A helper struct for tables
229 UNSET_ROTATE_TABULAR,
231 TOGGLE_ROTATE_TABULAR,
264 SET_SPECIAL_MULTICOLUMN,
280 TABULAR_VALIGN_MIDDLE,
282 TABULAR_VALIGN_BOTTOM,
284 LONGTABULAR_ALIGN_LEFT,
286 LONGTABULAR_ALIGN_CENTER,
288 LONGTABULAR_ALIGN_RIGHT,
301 CELL_BEGIN_OF_MULTICOLUMN,
303 CELL_PART_OF_MULTICOLUMN,
305 CELL_BEGIN_OF_MULTIROW,
307 CELL_PART_OF_MULTIROW
315 LYX_VALIGN_MIDDLE = 1,
317 LYX_VALIGN_BOTTOM = 2
323 LYX_LONGTABULAR_ALIGN_LEFT = 0,
325 LYX_LONGTABULAR_ALIGN_CENTER = 1,
327 LYX_LONGTABULAR_ALIGN_RIGHT = 2
366 // we have this header type (is set in the getLT... functions)
368 // double borders on top
370 // double borders on bottom
372 // used for FirstHeader & LastFooter and if this is true
373 // all the rows marked as FirstHeader or LastFooter are
374 // ignored in the output and it is set to be empty!
378 /// type for row numbers
379 typedef size_t row_type;
380 /// type for column numbers
381 typedef size_t col_type;
382 /// type for cell indices
383 typedef size_t idx_type;
384 /// index indicating an invalid position
385 static const idx_type npos = static_cast<idx_type>(-1);
388 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
390 /// Returns true if there is a topline, returns false if not
391 bool topLine(idx_type cell) const;
392 /// Returns true if there is a topline, returns false if not
393 bool bottomLine(idx_type cell) const;
394 /// Returns true if there is a topline, returns false if not
395 bool leftLine(idx_type cell) const;
396 /// Returns true if there is a topline, returns false if not
397 bool rightLine(idx_type cell) const;
399 /// return space occupied by the second horizontal line and
400 /// interline space above row \p row in pixels
401 int interRowSpace(row_type row) const;
403 int interColumnSpace(idx_type cell) const;
405 /* returns the maximum over all rows */
407 int cellWidth(idx_type cell) const;
409 int cellHeight(idx_type cell) const;
415 row_type nrows() const {return row_info.size();}
417 col_type ncols() const {return column_info.size();}
419 int rowAscent(row_type row) const;
421 int rowDescent(row_type row) const;
423 void setRowAscent(row_type row, int height);
425 void setRowDescent(row_type row, int height);
427 void setTopLine(idx_type cell, bool line);
429 void setBottomLine(idx_type cell, bool line);
431 void setLeftLine(idx_type cell, bool line);
433 void setRightLine(idx_type cell, bool line);
435 bool rowTopLine(row_type row) const;
437 bool rowBottomLine(row_type row) const;
439 bool columnLeftLine(col_type column) const;
441 bool columnRightLine(col_type column) const;
443 void setAlignment(idx_type cell, LyXAlignment align,
444 bool onlycolumn = false);
446 void setVAlignment(idx_type cell, VAlignment align,
447 bool onlycolumn = false);
449 void setTabularWidth(Length const & l) { tabular_width = l; }
451 Length tabularWidth() const { return tabular_width; }
453 void setColumnPWidth(Cursor &, idx_type, Length const &);
455 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
457 bool setMROffset(Cursor &, idx_type, Length const &);
459 void setAlignSpecial(idx_type cell, docstring const & special,
462 LyXAlignment getAlignment(idx_type cell,
463 bool onlycolumn = false) const;
465 VAlignment getVAlignment(idx_type cell,
466 bool onlycolumn = false) const;
468 Length const getPWidth(idx_type cell) const;
470 Length const getMROffset(idx_type cell) const;
472 int textHOffset(idx_type cell) const;
474 int textVOffset(idx_type cell) const;
476 void appendRow(row_type row);
478 void deleteRow(row_type row);
480 void copyRow(row_type row);
482 void insertRow(row_type row, bool copy);
484 void moveColumn(col_type col, ColDirection direction);
486 void moveRow(row_type row, RowDirection direction);
488 void appendColumn(col_type column);
490 void deleteColumn(col_type column);
492 void copyColumn(col_type column);
494 void insertColumn(col_type column, bool copy);
496 idx_type getFirstCellInRow(row_type row) const;
498 idx_type getLastCellInRow(row_type row) const;
500 idx_type numberOfCellsInRow(row_type row) const;
502 void write(std::ostream &) const;
506 void latex(otexstream &, OutputParams const &) const;
508 int docbook(odocstream & os, OutputParams const &) const;
510 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
512 void plaintext(odocstringstream &,
513 OutputParams const & runparams, int const depth,
514 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
516 bool isMultiColumn(idx_type cell) const;
518 bool hasMultiColumn(col_type cell) const;
520 idx_type setMultiColumn(idx_type cell, idx_type number,
521 bool const right_border);
523 void unsetMultiColumn(idx_type cell);
525 bool isPartOfMultiColumn(row_type row, col_type column) const;
527 bool isPartOfMultiRow(row_type row, col_type column) const;
529 bool isMultiRow(idx_type cell) const;
531 bool hasMultiRow(row_type r) const;
533 idx_type setMultiRow(idx_type cell, idx_type number,
534 bool const bottom_border,
535 LyXAlignment const halign);
537 void unsetMultiRow(idx_type cell);
539 row_type cellRow(idx_type cell) const;
541 col_type cellColumn(idx_type cell) const;
543 void setRotateCell(idx_type cell, int);
545 int getRotateCell(idx_type cell) const;
547 bool needRotating() const;
549 bool isLastCell(idx_type cell) const;
551 idx_type cellAbove(idx_type cell) const;
553 idx_type cellBelow(idx_type cell) const;
554 /// \return the index of the VISIBLE cell at row, column
555 /// this will be the same as the cell in the previous row,
556 /// e.g., if the cell is part of a multirow
557 idx_type cellIndex(row_type row, col_type column) const;
559 void setUsebox(idx_type cell, BoxType);
561 BoxType getUsebox(idx_type cell) const;
563 // Long Tabular Options support functions
565 void setLTHead(row_type row, bool flag, ltType const &, bool first);
567 bool getRowOfLTHead(row_type row, ltType &) const;
569 bool getRowOfLTFirstHead(row_type row, ltType &) const;
571 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
573 bool getRowOfLTFoot(row_type row, ltType &) const;
575 bool getRowOfLTLastFoot(row_type row, ltType &) const;
577 void setLTNewPage(row_type row, bool what);
579 bool getLTNewPage(row_type row) const;
581 idx_type setLTCaption(row_type row, bool what);
583 bool ltCaption(row_type row) const;
585 bool haveLTHead(bool withcaptions = true) const;
587 bool haveLTFirstHead(bool withcaptions = true) const;
589 bool haveLTFoot(bool withcaptions = true) const;
591 bool haveLTLastFoot(bool withcaptions = true) const;
593 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
595 // end longtable support
598 /// there is a subtle difference between these two methods.
601 /// cellInset(cellIndex(r,c));
602 /// can return different things. this is because cellIndex(r,c)
603 /// returns the VISIBLE cell at r,c, which may be the same as the
604 /// cell at the previous row or column, if we're dealing with some
605 /// multirow or multicell.
606 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
607 shared_ptr<InsetTableCell> cellInset(row_type row,
608 col_type column) const;
611 void setCellInset(row_type row, col_type column,
612 shared_ptr<InsetTableCell>) const;
613 /// Search for \param inset in the tabular, with the
615 void validate(LaTeXFeatures &) const;
618 // FIXME Now that cells have an InsetTableCell as their insets, rather
619 // than an InsetText, it'd be possible to reverse the relationship here,
620 // so that cell_vector was a vector<InsetTableCell> rather than a
621 // vector<CellData>, and an InsetTableCell had a CellData as a member,
622 // or perhaps just had its members as members.
629 CellData(CellData const &);
631 CellData & operator=(CellData const &);
643 LyXAlignment alignment;
645 VAlignment valignment;
646 /// width of the part before the decimal
648 /// width of the decimal part
665 docstring align_special;
667 Length p_width; // this is only set for multicolumn!!!
669 shared_ptr<InsetTableCell> inset;
671 CellData & cellInfo(idx_type cell) const;
673 typedef std::vector<CellData> cell_vector;
675 typedef std::vector<cell_vector> cell_vvector;
686 /// Extra space between the top line and this row
688 /// Ignore top_space if true and use the default top space
689 bool top_space_default;
690 /// Extra space between this row and the bottom line
692 /// Ignore bottom_space if true and use the default bottom space
693 bool bottom_space_default;
694 /// Extra space between the bottom line and the next top line
695 Length interline_space;
696 /// Ignore interline_space if true and use the default interline space
697 bool interline_space_default;
698 /// This are for longtabulars only
701 /// a row of endfirsthead
705 /// row of endlastfoot
707 /// row for a newpage
713 typedef std::vector<RowData> row_vector;
721 LyXAlignment alignment;
723 VAlignment valignment;
729 docstring align_special;
731 docstring decimal_point;
734 typedef std::vector<ColumnData> column_vector;
737 idx_type numberofcells;
739 std::vector<row_type> rowofcell;
741 std::vector<col_type> columnofcell;
745 column_vector column_info;
747 mutable cell_vvector cell_info;
749 Length tabular_width;
755 VAlignment tabular_valignment;
759 HAlignment longtabular_alignment;
761 bool is_long_tabular;
764 /// endfirsthead data
772 void init(Buffer *, row_type rows_arg,
773 col_type columns_arg);
775 void updateIndexes();
777 bool setFixedWidth(row_type r, col_type c);
778 /// return true of update is needed
779 bool updateColumnWidths();
781 idx_type columnSpan(idx_type cell) const;
783 idx_type rowSpan(idx_type cell) const;
785 BoxType useParbox(idx_type cell) const;
787 // helper function for Latex
789 void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
791 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
793 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
795 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
797 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
799 bool isValidRow(row_type const row) const;
801 void TeXRow(otexstream &, row_type const row,
802 OutputParams const &) const;
804 // helper functions for plain text
806 bool plaintextTopHLine(odocstringstream &, row_type row,
807 std::vector<unsigned int> const &) const;
809 bool plaintextBottomHLine(odocstringstream &, row_type row,
810 std::vector<unsigned int> const &) const;
812 void plaintextPrintCell(odocstringstream &,
813 OutputParams const &,
814 idx_type cell, row_type row, col_type column,
815 std::vector<unsigned int> const &,
816 bool onlydata, size_t max_length) const;
817 /// auxiliary function for docbook
818 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
820 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
821 bool header = false) const;
823 /// change associated Buffer
824 void setBuffer(Buffer & buffer);
825 /// retrieve associated Buffer
826 Buffer & buffer() const { return *buffer_; }
834 class InsetTabular : public Inset
838 InsetTabular(Buffer *, row_type rows = 1,
839 col_type columns = 1);
843 void setBuffer(Buffer & buffer);
846 static void string2params(std::string const &, InsetTabular &);
848 static std::string params2string(InsetTabular const &);
852 void write(std::ostream &) const;
854 void metrics(MetricsInfo &, Dimension &) const;
856 void draw(PainterInfo & pi, int x, int y) const;
858 void drawSelection(PainterInfo & pi, int x, int y) const;
860 void drawBackground(PainterInfo & pi, int x, int y) const;
862 bool editable() const { return true; }
864 bool hasSettings() const { return true; }
866 bool insetAllowed(InsetCode code) const;
868 bool allowSpellCheck() const { return true; }
870 bool canTrackChanges() const { return true; }
871 /** returns false if, when outputing LaTeX, font changes should
872 be closed before generating this inset. This is needed for
873 insets that may contain several paragraphs */
874 bool inheritFont() const { return false; }
876 bool allowsCaptionVariation(std::string const &) const;
878 DisplayType display() const;
880 void latex(otexstream &, OutputParams const &) const;
882 int plaintext(odocstringstream & ods, OutputParams const & op,
883 size_t max_length = INT_MAX) const;
885 int docbook(odocstream &, OutputParams const &) const;
887 docstring xhtml(XHTMLStream &, OutputParams const &) const;
889 void validate(LaTeXFeatures & features) const;
891 InsetCode lyxCode() const { return TABULAR_CODE; }
893 std::string contextMenu(BufferView const &, int, int) const;
895 std::string contextMenuName() const;
896 /// get offset of this cursor slice relative to our upper left corner
897 void cursorPos(BufferView const & bv, CursorSlice const & sl,
898 bool boundary, int & x, int & y) const;
900 bool tabularFeatures(Cursor & cur, std::string const & what);
902 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
903 std::string const & val = std::string());
905 size_t nargs() const { return tabular.numberofcells; }
907 shared_ptr<InsetTableCell const> cell(idx_type) const;
909 shared_ptr<InsetTableCell> cell(idx_type);
911 Text * getText(int) const;
913 /// set the change for the entire inset
914 void setChange(Change const & change);
915 /// accept the changes within the inset
916 void acceptChanges();
917 /// reject the changes within the inset
918 void rejectChanges();
920 // this should return true if we have a "normal" cell, otherwise false.
921 // "normal" means without width set!
922 /// should all paragraphs be output with "Standard" layout?
923 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
925 virtual bool forcePlainLayout(idx_type cell = 0) const;
927 void addPreview(DocIterator const & inset_pos,
928 graphics::PreviewLoader &) const;
930 /// lock cell with given index
931 void edit(Cursor & cur, bool front, EntryDirection entry_from);
932 /// get table row from x coordinate
933 int rowFromY(Cursor & cur, int y) const;
934 /// get table column from y coordinate
935 int columnFromX(Cursor & cur, int x) const;
937 Inset * editXY(Cursor & cur, int x, int y);
938 /// can we go further down on mouse click?
939 bool descendable(BufferView const &) const { return true; }
940 /// Update the counters of this inset and of its contents
941 void updateBuffer(ParIterator const &, UpdateType);
943 void addToToc(DocIterator const & di, bool output_active) const;
946 bool completionSupported(Cursor const &) const;
948 bool inlineCompletionSupported(Cursor const & cur) const;
950 bool automaticInlineCompletion() const;
952 bool automaticPopupCompletion() const;
954 bool showCompletionCursor() const;
956 CompletionList const * createCompletionList(Cursor const & cur) const;
958 docstring completionPrefix(Cursor const & cur) const;
960 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
962 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
964 virtual bool usePlainLayout() const { return true; }
967 InsetTabular * asInsetTabular() { return this; }
969 InsetTabular const * asInsetTabular() const { return this; }
971 bool isRightToLeft(Cursor & cur) const;
972 /// writes the cells between stidx and enidx as a string, optionally
973 /// descending into the insets
974 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
976 /// Returns whether the cell in the specified row and column is selected.
977 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
979 void setLayoutForHiddenCells(DocumentClass const & dc);
981 // Public structures and variables
983 mutable Tabular tabular;
987 InsetTabular(InsetTabular const &);
989 void doDispatch(Cursor & cur, FuncRequest & cmd);
991 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
993 Inset * clone() const { return new InsetTabular(*this); }
996 void drawCellLines(PainterInfo &, int x, int y, row_type row,
997 idx_type cell) const;
999 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1002 void moveNextCell(Cursor & cur,
1003 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1005 void movePrevCell(Cursor & cur,
1006 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1008 int cellXPos(idx_type cell) const;
1010 int cellYPos(idx_type cell) const;
1012 bool copySelection(Cursor & cur);
1014 bool pasteClipboard(Cursor & cur);
1016 void cutSelection(Cursor & cur);
1018 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1019 col_type & cs, col_type & ce) const;
1021 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1023 /// return the "Manhattan distance" to nearest corner
1024 int dist(BufferView &, idx_type cell, int x, int y) const;
1025 /// return the cell nearest to x, y
1026 idx_type getNearestCell(BufferView &, int x, int y) const;
1028 /// test the rotation state of the give cell range.
1029 bool oneCellHasRotationState(bool rotated,
1030 row_type row_start, row_type row_end,
1031 col_type col_start, col_type col_end) const;
1033 mutable idx_type first_visible_cell;
1034 /// The vertical offset of the table due to the vertical
1035 /// alignment with respect to the baseline.
1036 mutable int offset_valign_;
1037 /// true when selecting rows with the mouse
1039 /// true when selecting columns with the mouse
1043 std::string const featureAsString(Tabular::Feature feature);
1045 /// Split cell on decimal symbol
1046 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1050 #endif // INSET_TABULAR_H