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