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;
77 void operator=(InsetTableCell const &);
79 // This boolean is supposed to track whether the cell has had its
80 // width explicitly set. We need to know this to determine whether
81 // layout changes and paragraph customization are allowed---that is,
82 // we need it in forcePlainLayout() and allowParagraphCustomization().
83 // Unfortunately, that information is not readily available in
84 // InsetTableCell. In the case of multicolumn cells, it is present
85 // in CellData, and so would be available here if CellData were to
86 // become a member of InsetTableCell. But in the other case, it isn't
87 // even available there, but is held in Tabular::ColumnData.
88 // So, the present solution uses this boolean to track the information
89 // we need to track, and tries to keep it updated. This is not ideal,
90 // but the other solutions are no better. These are:
91 // (i) Keep a pointer in InsetTableCell to the table;
92 // (ii) Find the table by iterating over the Buffer's insets.
93 // Solution (i) raises the problem of updating the pointer when an
94 // InsetTableCell is copied, and we'd therefore need a copy constructor
95 // in InsetTabular and then in Tabular, which seems messy, given how
96 // complicated those classes are. Solution (ii) involves a lot of
97 // iterating, since this information is needed quite often, and so may
99 // So, well, if someone can do better, please do!
103 // FIXME: Here the thoughts from the comment above also apply.
105 LyXAlignment contentAlign;
106 /// should paragraph indendation be omitted in any case?
107 bool neverIndent() const { return true; }
109 LyXAlignment contentAlignment() const { return contentAlign; }
111 virtual bool usePlainLayout() const { return true; }
113 virtual bool forcePlainLayout(idx_type = 0) const;
115 virtual bool allowParagraphCustomization(idx_type = 0) const;
116 /// Is the width forced to some value?
117 bool hasFixedWidth() const { return isFixedWidth; }
122 // A helper struct for tables
221 UNSET_ROTATE_TABULAR,
223 TOGGLE_ROTATE_TABULAR,
256 SET_SPECIAL_MULTICOLUMN,
272 TABULAR_VALIGN_MIDDLE,
274 TABULAR_VALIGN_BOTTOM,
276 LONGTABULAR_ALIGN_LEFT,
278 LONGTABULAR_ALIGN_CENTER,
280 LONGTABULAR_ALIGN_RIGHT,
293 CELL_BEGIN_OF_MULTICOLUMN,
295 CELL_PART_OF_MULTICOLUMN,
297 CELL_BEGIN_OF_MULTIROW,
299 CELL_PART_OF_MULTIROW
307 LYX_VALIGN_MIDDLE = 1,
309 LYX_VALIGN_BOTTOM = 2
315 LYX_LONGTABULAR_ALIGN_LEFT = 0,
317 LYX_LONGTABULAR_ALIGN_CENTER = 1,
319 LYX_LONGTABULAR_ALIGN_RIGHT = 2
358 // we have this header type (is set in the getLT... functions)
360 // double borders on top
362 // double borders on bottom
364 // used for FirstHeader & LastFooter and if this is true
365 // all the rows marked as FirstHeader or LastFooter are
366 // ignored in the output and it is set to be empty!
370 /// type for row numbers
371 typedef size_t row_type;
372 /// type for column numbers
373 typedef size_t col_type;
374 /// type for cell indices
375 typedef size_t idx_type;
376 /// index indicating an invalid position
377 static const idx_type npos = static_cast<idx_type>(-1);
380 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
382 /// Returns true if there is a topline, returns false if not
383 bool topLine(idx_type cell) const;
384 /// Returns true if there is a topline, returns false if not
385 bool bottomLine(idx_type cell) const;
386 /// Returns true if there is a topline, returns false if not
387 bool leftLine(idx_type cell) const;
388 /// Returns true if there is a topline, returns false if not
389 bool rightLine(idx_type cell) const;
391 /// return space occupied by the second horizontal line and
392 /// interline space above row \p row in pixels
393 int interRowSpace(row_type row) const;
395 int interColumnSpace(idx_type cell) const;
397 /* returns the maximum over all rows */
399 int cellWidth(idx_type cell) const;
401 int cellHeight(idx_type cell) const;
407 row_type nrows() const {return row_info.size();}
409 col_type ncols() const {return column_info.size();}
411 int rowAscent(row_type row) const;
413 int rowDescent(row_type row) const;
415 void setRowAscent(row_type row, int height);
417 void setRowDescent(row_type row, int height);
419 void setTopLine(idx_type cell, bool line);
421 void setBottomLine(idx_type cell, bool line);
423 void setLeftLine(idx_type cell, bool line);
425 void setRightLine(idx_type cell, bool line);
427 bool rowTopLine(row_type row) const;
429 bool rowBottomLine(row_type row) const;
431 bool columnLeftLine(col_type column) const;
433 bool columnRightLine(col_type column) const;
435 void setAlignment(idx_type cell, LyXAlignment align,
436 bool onlycolumn = false);
438 void setVAlignment(idx_type cell, VAlignment align,
439 bool onlycolumn = false);
441 void setTabularWidth(Length const & l) { tabular_width = l; }
443 Length tabularWidth() const { return tabular_width; }
445 void setColumnPWidth(Cursor &, idx_type, Length const &);
447 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
449 bool setMROffset(Cursor &, idx_type, Length const &);
451 void setAlignSpecial(idx_type cell, docstring const & special,
454 LyXAlignment getAlignment(idx_type cell,
455 bool onlycolumn = false) const;
457 VAlignment getVAlignment(idx_type cell,
458 bool onlycolumn = false) const;
460 Length const getPWidth(idx_type cell) const;
462 Length const getMROffset(idx_type cell) const;
464 int textHOffset(idx_type cell) const;
466 int textVOffset(idx_type cell) const;
468 void appendRow(row_type row);
470 void deleteRow(row_type row);
472 void copyRow(row_type row);
474 void insertRow(row_type row, bool copy);
476 void moveColumn(col_type col, ColDirection direction);
478 void moveRow(row_type row, RowDirection direction);
480 void appendColumn(col_type column);
482 void deleteColumn(col_type column);
484 void copyColumn(col_type column);
486 void insertColumn(col_type column, bool copy);
488 idx_type getFirstCellInRow(row_type row) const;
490 idx_type getLastCellInRow(row_type row) const;
492 idx_type numberOfCellsInRow(row_type row) const;
494 void write(std::ostream &) const;
498 void latex(otexstream &, OutputParams const &) const;
500 int docbook(odocstream & os, OutputParams const &) const;
502 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
504 void plaintext(odocstringstream &,
505 OutputParams const & runparams, int const depth,
506 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
508 bool isMultiColumn(idx_type cell) const;
510 bool hasMultiColumn(col_type cell) const;
512 idx_type setMultiColumn(idx_type cell, idx_type number,
513 bool const right_border);
515 void unsetMultiColumn(idx_type cell);
517 bool isPartOfMultiColumn(row_type row, col_type column) const;
519 bool isPartOfMultiRow(row_type row, col_type column) const;
521 bool isMultiRow(idx_type cell) const;
523 bool hasMultiRow(row_type r) const;
525 idx_type setMultiRow(idx_type cell, idx_type number,
526 bool const bottom_border);
528 void unsetMultiRow(idx_type cell);
530 row_type cellRow(idx_type cell) const;
532 col_type cellColumn(idx_type cell) const;
534 void setRotateCell(idx_type cell, int);
536 int getRotateCell(idx_type cell) const;
538 bool needRotating() const;
540 bool isLastCell(idx_type cell) const;
542 idx_type cellAbove(idx_type cell) const;
544 idx_type cellBelow(idx_type cell) const;
546 idx_type cellIndex(row_type row, col_type column) const;
548 void setUsebox(idx_type cell, BoxType);
550 BoxType getUsebox(idx_type cell) const;
552 // Long Tabular Options support functions
554 void setLTHead(row_type row, bool flag, ltType const &, bool first);
556 bool getRowOfLTHead(row_type row, ltType &) const;
558 bool getRowOfLTFirstHead(row_type row, ltType &) const;
560 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
562 bool getRowOfLTFoot(row_type row, ltType &) const;
564 bool getRowOfLTLastFoot(row_type row, ltType &) const;
566 void setLTNewPage(row_type row, bool what);
568 bool getLTNewPage(row_type row) const;
570 idx_type setLTCaption(row_type row, bool what);
572 bool ltCaption(row_type row) const;
574 bool haveLTHead(bool withcaptions = true) const;
576 bool haveLTFirstHead(bool withcaptions = true) const;
578 bool haveLTFoot(bool withcaptions = true) const;
580 bool haveLTLastFoot(bool withcaptions = true) const;
582 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
584 // end longtable support
586 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
588 shared_ptr<InsetTableCell> cellInset(row_type row,
589 col_type column) const;
591 void setCellInset(row_type row, col_type column,
592 shared_ptr<InsetTableCell>) const;
593 /// Search for \param inset in the tabular, with the
595 void validate(LaTeXFeatures &) const;
597 // FIXME Now that cells have an InsetTableCell as their insets, rather
598 // than an InsetText, it'd be possible to reverse the relationship here,
599 // so that cell_vector was a vector<InsetTableCell> rather than a
600 // vector<CellData>, and an InsetTableCell had a CellData as a member,
601 // or perhaps just had its members as members.
608 CellData(CellData const &);
610 CellData & operator=(CellData);
612 void swap(CellData & rhs);
624 LyXAlignment alignment;
626 VAlignment valignment;
627 /// width of the part before the decimal
629 /// width of the decimal part
646 docstring align_special;
648 Length p_width; // this is only set for multicolumn!!!
650 shared_ptr<InsetTableCell> inset;
652 CellData & cellInfo(idx_type cell) const;
654 typedef std::vector<CellData> cell_vector;
656 typedef std::vector<cell_vector> cell_vvector;
667 /// Extra space between the top line and this row
669 /// Ignore top_space if true and use the default top space
670 bool top_space_default;
671 /// Extra space between this row and the bottom line
673 /// Ignore bottom_space if true and use the default bottom space
674 bool bottom_space_default;
675 /// Extra space between the bottom line and the next top line
676 Length interline_space;
677 /// Ignore interline_space if true and use the default interline space
678 bool interline_space_default;
679 /// This are for longtabulars only
682 /// a row of endfirsthead
686 /// row of endlastfoot
688 /// row for a newpage
694 typedef std::vector<RowData> row_vector;
702 LyXAlignment alignment;
704 VAlignment valignment;
710 docstring align_special;
712 docstring decimal_point;
715 typedef std::vector<ColumnData> column_vector;
718 idx_type numberofcells;
720 std::vector<row_type> rowofcell;
722 std::vector<col_type> columnofcell;
726 column_vector column_info;
728 mutable cell_vvector cell_info;
730 Length tabular_width;
736 VAlignment tabular_valignment;
740 HAlignment longtabular_alignment;
742 bool is_long_tabular;
745 /// endfirsthead data
753 void init(Buffer *, row_type rows_arg,
754 col_type columns_arg);
756 void updateIndexes();
758 bool setFixedWidth(row_type r, col_type c);
759 /// return true of update is needed
760 bool updateColumnWidths();
762 idx_type columnSpan(idx_type cell) const;
764 idx_type rowSpan(idx_type cell) const;
766 BoxType useParbox(idx_type cell) const;
768 // helper function for Latex
770 void TeXTopHLine(otexstream &, row_type row, std::string const lang) const;
772 void TeXBottomHLine(otexstream &, row_type row, std::string const lang) const;
774 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
776 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
778 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
780 bool isValidRow(row_type const row) const;
782 void TeXRow(otexstream &, row_type const row,
783 OutputParams const &) const;
785 // helper functions for plain text
787 bool plaintextTopHLine(odocstringstream &, row_type row,
788 std::vector<unsigned int> const &) const;
790 bool plaintextBottomHLine(odocstringstream &, row_type row,
791 std::vector<unsigned int> const &) const;
793 void plaintextPrintCell(odocstringstream &,
794 OutputParams const &,
795 idx_type cell, row_type row, col_type column,
796 std::vector<unsigned int> const &,
797 bool onlydata, size_t max_length) const;
798 /// auxiliary function for docbook
799 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
801 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
802 bool header = false) const;
804 /// change associated Buffer
805 void setBuffer(Buffer & buffer);
806 /// retrieve associated Buffer
807 Buffer & buffer() const { return *buffer_; }
815 class InsetTabular : public Inset
819 InsetTabular(Buffer *, row_type rows = 1,
820 col_type columns = 1);
824 void setBuffer(Buffer & buffer);
827 static void string2params(std::string const &, InsetTabular &);
829 static std::string params2string(InsetTabular const &);
833 void write(std::ostream &) const;
835 void metrics(MetricsInfo &, Dimension &) const;
837 void draw(PainterInfo & pi, int x, int y) const;
839 void drawSelection(PainterInfo & pi, int x, int y) const;
841 void drawBackground(PainterInfo & pi, int x, int y) const;
843 bool editable() const { return true; }
845 bool hasSettings() const { return true; }
847 bool insetAllowed(InsetCode code) const;
849 bool allowSpellCheck() const { return true; }
851 bool canTrackChanges() const { return true; }
852 /** returns false if, when outputing LaTeX, font changes should
853 be closed before generating this inset. This is needed for
854 insets that may contain several paragraphs */
855 bool inheritFont() const { return false; }
857 bool allowsCaptionVariation(std::string const &) const;
859 DisplayType display() const;
861 void latex(otexstream &, OutputParams const &) const;
863 int plaintext(odocstringstream & ods, OutputParams const & op,
864 size_t max_length = INT_MAX) const;
866 int docbook(odocstream &, OutputParams const &) const;
868 docstring xhtml(XHTMLStream &, OutputParams const &) const;
870 void validate(LaTeXFeatures & features) const;
872 InsetCode lyxCode() const { return TABULAR_CODE; }
874 std::string contextMenu(BufferView const &, int, int) const;
876 std::string contextMenuName() const;
877 /// get offset of this cursor slice relative to our upper left corner
878 void cursorPos(BufferView const & bv, CursorSlice const & sl,
879 bool boundary, int & x, int & y) const;
881 bool tabularFeatures(Cursor & cur, std::string const & what);
883 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
884 std::string const & val = std::string());
886 size_t nargs() const { return tabular.numberofcells; }
888 shared_ptr<InsetTableCell const> cell(idx_type) const;
890 shared_ptr<InsetTableCell> cell(idx_type);
892 Text * getText(int) const;
894 /// set the change for the entire inset
895 void setChange(Change const & change);
896 /// accept the changes within the inset
897 void acceptChanges();
898 /// reject the changes within the inset
899 void rejectChanges();
901 // this should return true if we have a "normal" cell, otherwise false.
902 // "normal" means without width set!
903 /// should all paragraphs be output with "Standard" layout?
904 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
906 virtual bool forcePlainLayout(idx_type cell = 0) const;
908 void addPreview(DocIterator const & inset_pos,
909 graphics::PreviewLoader &) const;
911 /// lock cell with given index
912 void edit(Cursor & cur, bool front, EntryDirection entry_from);
913 /// get table row from x coordinate
914 int rowFromY(Cursor & cur, int y) const;
915 /// get table column from y coordinate
916 int columnFromX(Cursor & cur, int x) const;
918 Inset * editXY(Cursor & cur, int x, int y);
919 /// can we go further down on mouse click?
920 bool descendable(BufferView const &) const { return true; }
921 /// Update the counters of this inset and of its contents
922 void updateBuffer(ParIterator const &, UpdateType);
924 void addToToc(DocIterator const & di, bool output_active) const;
927 bool completionSupported(Cursor const &) const;
929 bool inlineCompletionSupported(Cursor const & cur) const;
931 bool automaticInlineCompletion() const;
933 bool automaticPopupCompletion() const;
935 bool showCompletionCursor() const;
937 CompletionList const * createCompletionList(Cursor const & cur) const;
939 docstring completionPrefix(Cursor const & cur) const;
941 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
943 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
945 virtual bool usePlainLayout() const { return true; }
948 InsetTabular * asInsetTabular() { return this; }
950 InsetTabular const * asInsetTabular() const { return this; }
952 bool isRightToLeft(Cursor & cur) const;
953 /// writes the cells between stidx and enidx as a string, optionally
954 /// descending into the insets
955 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
957 /// Returns whether the cell in the specified row and column is selected.
958 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
960 // Public structures and variables
962 mutable Tabular tabular;
966 InsetTabular(InsetTabular const &);
968 void doDispatch(Cursor & cur, FuncRequest & cmd);
970 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
972 Inset * clone() const { return new InsetTabular(*this); }
975 void drawCellLines(PainterInfo &, int x, int y, row_type row,
976 idx_type cell) const;
978 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
981 void moveNextCell(Cursor & cur,
982 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
984 void movePrevCell(Cursor & cur,
985 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
987 int cellXPos(idx_type cell) const;
989 int cellYPos(idx_type cell) const;
991 void resetPos(Cursor & cur) const;
993 bool copySelection(Cursor & cur);
995 bool pasteClipboard(Cursor & cur);
997 void cutSelection(Cursor & cur);
999 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1000 col_type & cs, col_type & ce) const;
1002 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1004 /// return the "Manhattan distance" to nearest corner
1005 int dist(BufferView &, idx_type cell, int x, int y) const;
1006 /// return the cell nearest to x, y
1007 idx_type getNearestCell(BufferView &, int x, int y) const;
1009 /// test the rotation state of the give cell range.
1010 bool oneCellHasRotationState(bool rotated,
1011 row_type row_start, row_type row_end,
1012 col_type col_start, col_type col_end) const;
1014 mutable idx_type first_visible_cell;
1017 /// The vertical offset of the table due to the vertical
1018 /// alignment with respect to the baseline.
1019 mutable int offset_valign_;
1020 /// true when selecting rows with the mouse
1022 /// true when selecting columns with the mouse
1026 std::string const featureAsString(Tabular::Feature feature);
1028 /// Split cell on decimal symbol
1029 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
1033 #endif // INSET_TABULAR_H