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
234 UNSET_ROTATE_TABULAR,
236 TOGGLE_ROTATE_TABULAR,
269 SET_SPECIAL_MULTICOLUMN,
287 TABULAR_VALIGN_MIDDLE,
289 TABULAR_VALIGN_BOTTOM,
291 LONGTABULAR_ALIGN_LEFT,
293 LONGTABULAR_ALIGN_CENTER,
295 LONGTABULAR_ALIGN_RIGHT,
308 CELL_BEGIN_OF_MULTICOLUMN,
310 CELL_PART_OF_MULTICOLUMN,
312 CELL_BEGIN_OF_MULTIROW,
314 CELL_PART_OF_MULTIROW
322 LYX_VALIGN_MIDDLE = 1,
324 LYX_VALIGN_BOTTOM = 2
330 LYX_LONGTABULAR_ALIGN_LEFT = 0,
332 LYX_LONGTABULAR_ALIGN_CENTER = 1,
334 LYX_LONGTABULAR_ALIGN_RIGHT = 2
373 // we have this header type (is set in the getLT... functions)
375 // double borders on top
377 // double borders on bottom
379 // used for FirstHeader & LastFooter and if this is true
380 // all the rows marked as FirstHeader or LastFooter are
381 // ignored in the output and it is set to be empty!
385 /// type for row numbers
386 typedef size_t row_type;
387 /// type for column numbers
388 typedef size_t col_type;
389 /// type for cell indices
390 typedef size_t idx_type;
391 /// index indicating an invalid position
392 static const idx_type npos = static_cast<idx_type>(-1);
395 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
397 /// Returns true if there is a topline, returns false if not
398 bool topLine(idx_type cell) const;
399 /// Returns true if there is a topline, returns false if not
400 bool bottomLine(idx_type cell) const;
401 /// Returns true if there is a topline, returns false if not
402 bool leftLine(idx_type cell) const;
403 /// Returns true if there is a topline, returns false if not
404 bool rightLine(idx_type cell) const;
406 /// return space occupied by the second horizontal line and
407 /// interline space above row \p row in pixels
408 int interRowSpace(row_type row) const;
410 int interColumnSpace(idx_type cell) const;
412 /* returns the maximum over all rows */
414 int cellWidth(idx_type cell) const;
416 int cellHeight(idx_type cell) const;
422 row_type nrows() const {return row_info.size();}
424 col_type ncols() const {return column_info.size();}
426 int rowAscent(row_type row) const;
428 int rowDescent(row_type row) const;
430 void setRowAscent(row_type row, int height);
432 void setRowDescent(row_type row, int height);
434 void setTopLine(idx_type cell, bool line);
436 void setBottomLine(idx_type cell, bool line);
438 void setLeftLine(idx_type cell, bool line);
440 void setRightLine(idx_type cell, bool line);
442 bool rowTopLine(row_type row) const;
444 bool rowBottomLine(row_type row) const;
446 bool columnLeftLine(col_type column) const;
448 bool columnRightLine(col_type column) const;
450 void setAlignment(idx_type cell, LyXAlignment align,
451 bool onlycolumn = false);
453 void setVAlignment(idx_type cell, VAlignment align,
454 bool onlycolumn = false);
456 void setTabularWidth(Length const & l) { tabular_width = l; }
458 Length tabularWidth() const { return tabular_width; }
460 void setColumnPWidth(Cursor &, idx_type, Length const &);
462 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
464 bool setMROffset(Cursor &, idx_type, Length const &);
466 void setAlignSpecial(idx_type cell, docstring const & special,
469 LyXAlignment getAlignment(idx_type cell,
470 bool onlycolumn = false) const;
472 VAlignment getVAlignment(idx_type cell,
473 bool onlycolumn = false) const;
475 Length const getPWidth(idx_type cell) const;
477 Length const getMROffset(idx_type cell) const;
479 int textHOffset(idx_type cell) const;
481 int textVOffset(idx_type cell) const;
483 void appendRow(row_type row);
485 void deleteRow(row_type row);
487 void copyRow(row_type row);
489 void insertRow(row_type row, bool copy);
491 void moveColumn(col_type col, ColDirection direction);
493 void moveRow(row_type row, RowDirection direction);
495 void appendColumn(col_type column);
497 void deleteColumn(col_type column);
499 void copyColumn(col_type column);
501 void insertColumn(col_type column, bool copy);
503 idx_type getFirstCellInRow(row_type row) const;
505 idx_type getLastCellInRow(row_type row) const;
507 idx_type numberOfCellsInRow(row_type row) const;
509 void write(std::ostream &) const;
513 void latex(otexstream &, OutputParams const &) const;
515 int docbook(odocstream & os, OutputParams const &) const;
517 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
519 void plaintext(odocstringstream &,
520 OutputParams const & runparams, int const depth,
521 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
523 bool isMultiColumn(idx_type cell) const;
525 bool hasMultiColumn(col_type cell) const;
527 idx_type setMultiColumn(idx_type cell, idx_type number,
528 bool const right_border);
530 void unsetMultiColumn(idx_type cell);
532 bool isPartOfMultiColumn(row_type row, col_type column) const;
534 bool isPartOfMultiRow(row_type row, col_type column) const;
536 bool isMultiRow(idx_type cell) const;
538 bool hasMultiRow(row_type r) const;
540 idx_type setMultiRow(idx_type cell, idx_type number,
541 bool const bottom_border,
542 LyXAlignment const halign);
544 void unsetMultiRow(idx_type cell);
546 row_type cellRow(idx_type cell) const;
548 col_type cellColumn(idx_type cell) const;
550 void setRotateCell(idx_type cell, int);
552 int getRotateCell(idx_type cell) const;
554 bool needRotating() const;
556 bool isLastCell(idx_type cell) const;
558 idx_type cellAbove(idx_type cell) const;
560 idx_type cellBelow(idx_type cell) const;
561 /// \return the index of the VISIBLE cell at row, column
562 /// this will be the same as the cell in the previous row,
563 /// e.g., if the cell is part of a multirow
564 idx_type cellIndex(row_type row, col_type column) const;
566 void setUsebox(idx_type cell, BoxType);
568 BoxType getUsebox(idx_type cell) const;
570 // Long Tabular Options support functions
572 void setLTHead(row_type row, bool flag, ltType const &, bool first);
574 bool getRowOfLTHead(row_type row, ltType &) const;
576 bool getRowOfLTFirstHead(row_type row, ltType &) const;
578 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
580 bool getRowOfLTFoot(row_type row, ltType &) const;
582 bool getRowOfLTLastFoot(row_type row, ltType &) const;
584 void setLTNewPage(row_type row, bool what);
586 bool getLTNewPage(row_type row) const;
588 idx_type setLTCaption(row_type row, bool what);
590 bool ltCaption(row_type row) const;
592 bool haveLTHead(bool withcaptions = true) const;
594 bool haveLTFirstHead(bool withcaptions = true) const;
596 bool haveLTFoot(bool withcaptions = true) const;
598 bool haveLTLastFoot(bool withcaptions = true) const;
600 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
602 // end longtable support
605 /// there is a subtle difference between these two methods.
608 /// cellInset(cellIndex(r,c));
609 /// can return different things. this is because cellIndex(r,c)
610 /// returns the VISIBLE cell at r,c, which may be the same as the
611 /// cell at the previous row or column, if we're dealing with some
612 /// multirow or multicell.
613 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
614 shared_ptr<InsetTableCell> cellInset(row_type row,
615 col_type column) const;
618 void setCellInset(row_type row, col_type column,
619 shared_ptr<InsetTableCell>) const;
620 /// Search for \param inset in the tabular, with the
622 void validate(LaTeXFeatures &) const;
625 // FIXME Now that cells have an InsetTableCell as their insets, rather
626 // than an InsetText, it'd be possible to reverse the relationship here,
627 // so that cell_vector was a vector<InsetTableCell> rather than a
628 // vector<CellData>, and an InsetTableCell had a CellData as a member,
629 // or perhaps just had its members as members.
636 CellData(CellData const &);
638 CellData & operator=(CellData const &);
650 LyXAlignment alignment;
652 VAlignment valignment;
653 /// width of the part before the decimal
655 /// width of the decimal part
672 docstring align_special;
674 Length p_width; // this is only set for multicolumn!!!
676 shared_ptr<InsetTableCell> inset;
678 CellData & cellInfo(idx_type cell) const;
680 typedef std::vector<CellData> cell_vector;
682 typedef std::vector<cell_vector> cell_vvector;
693 /// Extra space between the top line and this row
695 /// Ignore top_space if true and use the default top space
696 bool top_space_default;
697 /// Extra space between this row and the bottom line
699 /// Ignore bottom_space if true and use the default bottom space
700 bool bottom_space_default;
701 /// Extra space between the bottom line and the next top line
702 Length interline_space;
703 /// Ignore interline_space if true and use the default interline space
704 bool interline_space_default;
705 /// This are for longtabulars only
708 /// a row of endfirsthead
712 /// row of endlastfoot
714 /// row for a newpage
720 typedef std::vector<RowData> row_vector;
728 LyXAlignment alignment;
730 VAlignment valignment;
736 docstring align_special;
738 docstring decimal_point;
741 typedef std::vector<ColumnData> column_vector;
744 idx_type numberofcells;
746 std::vector<row_type> rowofcell;
748 std::vector<col_type> columnofcell;
752 column_vector column_info;
754 mutable cell_vvector cell_info;
756 Length tabular_width;
762 VAlignment tabular_valignment;
766 HAlignment longtabular_alignment;
768 bool is_long_tabular;
771 /// endfirsthead data
779 void init(Buffer *, row_type rows_arg,
780 col_type columns_arg);
782 void updateIndexes();
784 bool setFixedWidth(row_type r, col_type c);
785 /// return true of update is needed
786 bool updateColumnWidths();
788 idx_type columnSpan(idx_type cell) const;
790 idx_type rowSpan(idx_type cell) const;
792 BoxType useParbox(idx_type cell) const;
794 // helper function for Latex
796 void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
798 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
800 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
802 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
804 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
806 bool isValidRow(row_type const row) const;
808 void TeXRow(otexstream &, row_type const row,
809 OutputParams const &) const;
811 // helper functions for plain text
813 bool plaintextTopHLine(odocstringstream &, row_type row,
814 std::vector<unsigned int> const &) const;
816 bool plaintextBottomHLine(odocstringstream &, row_type row,
817 std::vector<unsigned int> const &) const;
819 void plaintextPrintCell(odocstringstream &,
820 OutputParams const &,
821 idx_type cell, row_type row, col_type column,
822 std::vector<unsigned int> const &,
823 bool onlydata, size_t max_length) const;
824 /// auxiliary function for docbook
825 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
827 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
828 bool header = false) const;
830 /// change associated Buffer
831 void setBuffer(Buffer & buffer);
832 /// retrieve associated Buffer
833 Buffer & buffer() const { return *buffer_; }
841 class InsetTabular : public Inset
845 InsetTabular(Buffer *, row_type rows = 1,
846 col_type columns = 1);
850 void setBuffer(Buffer & buffer);
853 static void string2params(std::string const &, InsetTabular &);
855 static std::string params2string(InsetTabular const &);
859 void write(std::ostream &) const;
861 void metrics(MetricsInfo &, Dimension &) const;
863 void draw(PainterInfo & pi, int x, int y) const;
865 void drawSelection(PainterInfo & pi, int x, int y) const;
867 void drawBackground(PainterInfo & pi, int x, int y) const;
869 bool editable() const { return true; }
871 bool hasSettings() const { return true; }
873 bool insetAllowed(InsetCode code) const;
875 bool allowSpellCheck() const { return true; }
877 bool canTrackChanges() const { return true; }
878 /** returns false if, when outputing LaTeX, font changes should
879 be closed before generating this inset. This is needed for
880 insets that may contain several paragraphs */
881 bool inheritFont() const { return false; }
883 bool allowsCaptionVariation(std::string const &) const;
885 DisplayType display() const;
887 void latex(otexstream &, OutputParams const &) const;
889 int plaintext(odocstringstream & ods, OutputParams const & op,
890 size_t max_length = INT_MAX) const;
892 int docbook(odocstream &, OutputParams const &) const;
894 docstring xhtml(XHTMLStream &, OutputParams const &) const;
896 void validate(LaTeXFeatures & features) const;
898 InsetCode lyxCode() const { return TABULAR_CODE; }
900 std::string contextMenu(BufferView const &, int, int) const;
902 std::string contextMenuName() const;
903 /// get offset of this cursor slice relative to our upper left corner
904 void cursorPos(BufferView const & bv, CursorSlice const & sl,
905 bool boundary, int & x, int & y) const;
907 bool tabularFeatures(Cursor & cur, std::string const & what);
909 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
910 std::string const & val = std::string());
912 size_t nargs() const { return tabular.numberofcells; }
914 shared_ptr<InsetTableCell const> cell(idx_type) const;
916 shared_ptr<InsetTableCell> cell(idx_type);
918 Text * getText(int) const;
920 /// set the change for the entire inset
921 void setChange(Change const & change);
922 /// accept the changes within the inset
923 void acceptChanges();
924 /// reject the changes within the inset
925 void rejectChanges();
927 // this should return true if we have a "normal" cell, otherwise false.
928 // "normal" means without width set!
929 /// should all paragraphs be output with "Standard" layout?
930 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
932 virtual bool forcePlainLayout(idx_type cell = 0) const;
934 void addPreview(DocIterator const & inset_pos,
935 graphics::PreviewLoader &) const;
937 /// lock cell with given index
938 void edit(Cursor & cur, bool front, EntryDirection entry_from);
939 /// get table row from x coordinate
940 int rowFromY(Cursor & cur, int y) const;
941 /// get table column from y coordinate
942 int columnFromX(Cursor & cur, int x) const;
944 Inset * editXY(Cursor & cur, int x, int y);
945 /// can we go further down on mouse click?
946 bool descendable(BufferView const &) const { return true; }
947 /// Update the counters of this inset and of its contents
948 void updateBuffer(ParIterator const &, UpdateType);
950 void addToToc(DocIterator const & di, bool output_active,
951 UpdateType utype) const;
954 bool completionSupported(Cursor const &) const;
956 bool inlineCompletionSupported(Cursor const & cur) const;
958 bool automaticInlineCompletion() const;
960 bool automaticPopupCompletion() const;
962 bool showCompletionCursor() const;
964 CompletionList const * createCompletionList(Cursor const & cur) const;
966 docstring completionPrefix(Cursor const & cur) const;
968 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
970 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
972 virtual bool usePlainLayout() const { return true; }
975 InsetTabular * asInsetTabular() { return this; }
977 InsetTabular const * asInsetTabular() const { return this; }
979 bool isRightToLeft(Cursor & cur) const;
980 /// writes the cells between stidx and enidx as a string, optionally
981 /// descending into the insets
982 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
984 /// Returns whether the cell in the specified row and column is selected.
985 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
987 void setLayoutForHiddenCells(DocumentClass const & dc);
989 // Public structures and variables
991 mutable Tabular tabular;
995 InsetTabular(InsetTabular const &);
997 void doDispatch(Cursor & cur, FuncRequest & cmd);
999 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
1001 Inset * clone() const { return new InsetTabular(*this); }
1004 void drawCellLines(PainterInfo &, int x, int y, row_type row,
1005 idx_type cell) const;
1007 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
1010 void moveNextCell(Cursor & cur,
1011 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1013 void movePrevCell(Cursor & cur,
1014 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
1016 int cellXPos(idx_type cell) const;
1018 int cellYPos(idx_type cell) const;
1020 bool copySelection(Cursor & cur);
1022 bool pasteClipboard(Cursor & cur);
1024 void cutSelection(Cursor & cur);
1026 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1027 col_type & cs, col_type & ce) const;
1029 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1031 /// return the "Manhattan distance" to nearest corner
1032 int dist(BufferView &, idx_type cell, int x, int y) const;
1033 /// return the cell nearest to x, y
1034 idx_type getNearestCell(BufferView &, int x, int y) const;
1036 /// test the rotation state of the give cell range.
1037 bool oneCellHasRotationState(bool rotated,
1038 row_type row_start, row_type row_end,
1039 col_type col_start, col_type col_end) const;
1041 mutable idx_type first_visible_cell_;
1042 /// The vertical offset of the table due to the vertical
1043 /// alignment with respect to the baseline.
1044 mutable int offset_valign_;
1045 /// true when selecting rows with the mouse
1047 /// true when selecting columns with the mouse
1051 std::string const featureAsString(Tabular::Feature feature);
1053 /// Split cell on decimal symbol
1054 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1058 #endif // INSET_TABULAR_H