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; }
124 // A helper struct for tables
223 UNSET_ROTATE_TABULAR,
225 TOGGLE_ROTATE_TABULAR,
258 SET_SPECIAL_MULTICOLUMN,
274 TABULAR_VALIGN_MIDDLE,
276 TABULAR_VALIGN_BOTTOM,
278 LONGTABULAR_ALIGN_LEFT,
280 LONGTABULAR_ALIGN_CENTER,
282 LONGTABULAR_ALIGN_RIGHT,
295 CELL_BEGIN_OF_MULTICOLUMN,
297 CELL_PART_OF_MULTICOLUMN,
299 CELL_BEGIN_OF_MULTIROW,
301 CELL_PART_OF_MULTIROW
309 LYX_VALIGN_MIDDLE = 1,
311 LYX_VALIGN_BOTTOM = 2
317 LYX_LONGTABULAR_ALIGN_LEFT = 0,
319 LYX_LONGTABULAR_ALIGN_CENTER = 1,
321 LYX_LONGTABULAR_ALIGN_RIGHT = 2
360 // we have this header type (is set in the getLT... functions)
362 // double borders on top
364 // double borders on bottom
366 // used for FirstHeader & LastFooter and if this is true
367 // all the rows marked as FirstHeader or LastFooter are
368 // ignored in the output and it is set to be empty!
372 /// type for row numbers
373 typedef size_t row_type;
374 /// type for column numbers
375 typedef size_t col_type;
376 /// type for cell indices
377 typedef size_t idx_type;
378 /// index indicating an invalid position
379 static const idx_type npos = static_cast<idx_type>(-1);
382 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
384 /// Returns true if there is a topline, returns false if not
385 bool topLine(idx_type cell) const;
386 /// Returns true if there is a topline, returns false if not
387 bool bottomLine(idx_type cell) const;
388 /// Returns true if there is a topline, returns false if not
389 bool leftLine(idx_type cell) const;
390 /// Returns true if there is a topline, returns false if not
391 bool rightLine(idx_type cell) const;
393 /// return space occupied by the second horizontal line and
394 /// interline space above row \p row in pixels
395 int interRowSpace(row_type row) const;
397 int interColumnSpace(idx_type cell) const;
399 /* returns the maximum over all rows */
401 int cellWidth(idx_type cell) const;
403 int cellHeight(idx_type cell) const;
409 row_type nrows() const {return row_info.size();}
411 col_type ncols() const {return column_info.size();}
413 int rowAscent(row_type row) const;
415 int rowDescent(row_type row) const;
417 void setRowAscent(row_type row, int height);
419 void setRowDescent(row_type row, int height);
421 void setTopLine(idx_type cell, bool line);
423 void setBottomLine(idx_type cell, bool line);
425 void setLeftLine(idx_type cell, bool line);
427 void setRightLine(idx_type cell, bool line);
429 bool rowTopLine(row_type row) const;
431 bool rowBottomLine(row_type row) const;
433 bool columnLeftLine(col_type column) const;
435 bool columnRightLine(col_type column) const;
437 void setAlignment(idx_type cell, LyXAlignment align,
438 bool onlycolumn = false);
440 void setVAlignment(idx_type cell, VAlignment align,
441 bool onlycolumn = false);
443 void setTabularWidth(Length const & l) { tabular_width = l; }
445 Length tabularWidth() const { return tabular_width; }
447 void setColumnPWidth(Cursor &, idx_type, Length const &);
449 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
451 bool setMROffset(Cursor &, idx_type, Length const &);
453 void setAlignSpecial(idx_type cell, docstring const & special,
456 LyXAlignment getAlignment(idx_type cell,
457 bool onlycolumn = false) const;
459 VAlignment getVAlignment(idx_type cell,
460 bool onlycolumn = false) const;
462 Length const getPWidth(idx_type cell) const;
464 Length const getMROffset(idx_type cell) const;
466 int textHOffset(idx_type cell) const;
468 int textVOffset(idx_type cell) const;
470 void appendRow(row_type row);
472 void deleteRow(row_type row);
474 void copyRow(row_type row);
476 void insertRow(row_type row, bool copy);
478 void moveColumn(col_type col, ColDirection direction);
480 void moveRow(row_type row, RowDirection direction);
482 void appendColumn(col_type column);
484 void deleteColumn(col_type column);
486 void copyColumn(col_type column);
488 void insertColumn(col_type column, bool copy);
490 idx_type getFirstCellInRow(row_type row) const;
492 idx_type getLastCellInRow(row_type row) const;
494 idx_type numberOfCellsInRow(row_type row) const;
496 void write(std::ostream &) const;
500 void latex(otexstream &, OutputParams const &) const;
502 int docbook(odocstream & os, OutputParams const &) const;
504 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
506 void plaintext(odocstringstream &,
507 OutputParams const & runparams, int const depth,
508 bool onlydata, char_type delim, size_t max_length = INT_MAX) const;
510 bool isMultiColumn(idx_type cell) const;
512 bool hasMultiColumn(col_type cell) const;
514 idx_type setMultiColumn(idx_type cell, idx_type number,
515 bool const right_border);
517 void unsetMultiColumn(idx_type cell);
519 bool isPartOfMultiColumn(row_type row, col_type column) const;
521 bool isPartOfMultiRow(row_type row, col_type column) const;
523 bool isMultiRow(idx_type cell) const;
525 bool hasMultiRow(row_type r) const;
527 idx_type setMultiRow(idx_type cell, idx_type number,
528 bool const bottom_border,
529 LyXAlignment const halign);
531 void unsetMultiRow(idx_type cell);
533 row_type cellRow(idx_type cell) const;
535 col_type cellColumn(idx_type cell) const;
537 void setRotateCell(idx_type cell, int);
539 int getRotateCell(idx_type cell) const;
541 bool needRotating() const;
543 bool isLastCell(idx_type cell) const;
545 idx_type cellAbove(idx_type cell) const;
547 idx_type cellBelow(idx_type cell) const;
549 idx_type cellIndex(row_type row, col_type column) const;
551 void setUsebox(idx_type cell, BoxType);
553 BoxType getUsebox(idx_type cell) const;
555 // Long Tabular Options support functions
557 void setLTHead(row_type row, bool flag, ltType const &, bool first);
559 bool getRowOfLTHead(row_type row, ltType &) const;
561 bool getRowOfLTFirstHead(row_type row, ltType &) const;
563 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
565 bool getRowOfLTFoot(row_type row, ltType &) const;
567 bool getRowOfLTLastFoot(row_type row, ltType &) const;
569 void setLTNewPage(row_type row, bool what);
571 bool getLTNewPage(row_type row) const;
573 idx_type setLTCaption(row_type row, bool what);
575 bool ltCaption(row_type row) const;
577 bool haveLTHead(bool withcaptions = true) const;
579 bool haveLTFirstHead(bool withcaptions = true) const;
581 bool haveLTFoot(bool withcaptions = true) const;
583 bool haveLTLastFoot(bool withcaptions = true) const;
585 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
587 // end longtable support
589 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
591 shared_ptr<InsetTableCell> cellInset(row_type row,
592 col_type column) const;
594 void setCellInset(row_type row, col_type column,
595 shared_ptr<InsetTableCell>) const;
596 /// Search for \param inset in the tabular, with the
598 void validate(LaTeXFeatures &) const;
600 // FIXME Now that cells have an InsetTableCell as their insets, rather
601 // than an InsetText, it'd be possible to reverse the relationship here,
602 // so that cell_vector was a vector<InsetTableCell> rather than a
603 // vector<CellData>, and an InsetTableCell had a CellData as a member,
604 // or perhaps just had its members as members.
611 CellData(CellData const &);
613 CellData & operator=(CellData const &);
625 LyXAlignment alignment;
627 VAlignment valignment;
628 /// width of the part before the decimal
630 /// width of the decimal part
647 docstring align_special;
649 Length p_width; // this is only set for multicolumn!!!
651 shared_ptr<InsetTableCell> inset;
653 CellData & cellInfo(idx_type cell) const;
655 typedef std::vector<CellData> cell_vector;
657 typedef std::vector<cell_vector> cell_vvector;
668 /// Extra space between the top line and this row
670 /// Ignore top_space if true and use the default top space
671 bool top_space_default;
672 /// Extra space between this row and the bottom line
674 /// Ignore bottom_space if true and use the default bottom space
675 bool bottom_space_default;
676 /// Extra space between the bottom line and the next top line
677 Length interline_space;
678 /// Ignore interline_space if true and use the default interline space
679 bool interline_space_default;
680 /// This are for longtabulars only
683 /// a row of endfirsthead
687 /// row of endlastfoot
689 /// row for a newpage
695 typedef std::vector<RowData> row_vector;
703 LyXAlignment alignment;
705 VAlignment valignment;
711 docstring align_special;
713 docstring decimal_point;
716 typedef std::vector<ColumnData> column_vector;
719 idx_type numberofcells;
721 std::vector<row_type> rowofcell;
723 std::vector<col_type> columnofcell;
727 column_vector column_info;
729 mutable cell_vvector cell_info;
731 Length tabular_width;
737 VAlignment tabular_valignment;
741 HAlignment longtabular_alignment;
743 bool is_long_tabular;
746 /// endfirsthead data
754 void init(Buffer *, row_type rows_arg,
755 col_type columns_arg);
757 void updateIndexes();
759 bool setFixedWidth(row_type r, col_type c);
760 /// return true of update is needed
761 bool updateColumnWidths();
763 idx_type columnSpan(idx_type cell) const;
765 idx_type rowSpan(idx_type cell) const;
767 BoxType useParbox(idx_type cell) const;
769 // helper function for Latex
771 void TeXTopHLine(otexstream &, row_type row, std::string const & lang) const;
773 void TeXBottomHLine(otexstream &, row_type row, std::string const & lang) const;
775 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
777 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
779 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
781 bool isValidRow(row_type const row) const;
783 void TeXRow(otexstream &, row_type const row,
784 OutputParams const &) const;
786 // helper functions for plain text
788 bool plaintextTopHLine(odocstringstream &, row_type row,
789 std::vector<unsigned int> const &) const;
791 bool plaintextBottomHLine(odocstringstream &, row_type row,
792 std::vector<unsigned int> const &) const;
794 void plaintextPrintCell(odocstringstream &,
795 OutputParams const &,
796 idx_type cell, row_type row, col_type column,
797 std::vector<unsigned int> const &,
798 bool onlydata, size_t max_length) const;
799 /// auxiliary function for docbook
800 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
802 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
803 bool header = false) const;
805 /// change associated Buffer
806 void setBuffer(Buffer & buffer);
807 /// retrieve associated Buffer
808 Buffer & buffer() const { return *buffer_; }
816 class InsetTabular : public Inset
820 InsetTabular(Buffer *, row_type rows = 1,
821 col_type columns = 1);
825 void setBuffer(Buffer & buffer);
828 static void string2params(std::string const &, InsetTabular &);
830 static std::string params2string(InsetTabular const &);
834 void write(std::ostream &) const;
836 void metrics(MetricsInfo &, Dimension &) const;
838 void draw(PainterInfo & pi, int x, int y) const;
840 void drawSelection(PainterInfo & pi, int x, int y) const;
842 void drawBackground(PainterInfo & pi, int x, int y) const;
844 bool editable() const { return true; }
846 bool hasSettings() const { return true; }
848 bool insetAllowed(InsetCode code) const;
850 bool allowSpellCheck() const { return true; }
852 bool canTrackChanges() const { return true; }
853 /** returns false if, when outputing LaTeX, font changes should
854 be closed before generating this inset. This is needed for
855 insets that may contain several paragraphs */
856 bool inheritFont() const { return false; }
858 bool allowsCaptionVariation(std::string const &) const;
860 DisplayType display() const;
862 void latex(otexstream &, OutputParams const &) const;
864 int plaintext(odocstringstream & ods, OutputParams const & op,
865 size_t max_length = INT_MAX) const;
867 int docbook(odocstream &, OutputParams const &) const;
869 docstring xhtml(XHTMLStream &, OutputParams const &) const;
871 void validate(LaTeXFeatures & features) const;
873 InsetCode lyxCode() const { return TABULAR_CODE; }
875 std::string contextMenu(BufferView const &, int, int) const;
877 std::string contextMenuName() const;
878 /// get offset of this cursor slice relative to our upper left corner
879 void cursorPos(BufferView const & bv, CursorSlice const & sl,
880 bool boundary, int & x, int & y) const;
882 bool tabularFeatures(Cursor & cur, std::string const & what);
884 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
885 std::string const & val = std::string());
887 size_t nargs() const { return tabular.numberofcells; }
889 shared_ptr<InsetTableCell const> cell(idx_type) const;
891 shared_ptr<InsetTableCell> cell(idx_type);
893 Text * getText(int) const;
895 /// set the change for the entire inset
896 void setChange(Change const & change);
897 /// accept the changes within the inset
898 void acceptChanges();
899 /// reject the changes within the inset
900 void rejectChanges();
902 // this should return true if we have a "normal" cell, otherwise false.
903 // "normal" means without width set!
904 /// should all paragraphs be output with "Standard" layout?
905 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
907 virtual bool forcePlainLayout(idx_type cell = 0) const;
909 void addPreview(DocIterator const & inset_pos,
910 graphics::PreviewLoader &) const;
912 /// lock cell with given index
913 void edit(Cursor & cur, bool front, EntryDirection entry_from);
914 /// get table row from x coordinate
915 int rowFromY(Cursor & cur, int y) const;
916 /// get table column from y coordinate
917 int columnFromX(Cursor & cur, int x) const;
919 Inset * editXY(Cursor & cur, int x, int y);
920 /// can we go further down on mouse click?
921 bool descendable(BufferView const &) const { return true; }
922 /// Update the counters of this inset and of its contents
923 void updateBuffer(ParIterator const &, UpdateType);
925 void addToToc(DocIterator const & di, bool output_active) const;
928 bool completionSupported(Cursor const &) const;
930 bool inlineCompletionSupported(Cursor const & cur) const;
932 bool automaticInlineCompletion() const;
934 bool automaticPopupCompletion() const;
936 bool showCompletionCursor() const;
938 CompletionList const * createCompletionList(Cursor const & cur) const;
940 docstring completionPrefix(Cursor const & cur) const;
942 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
944 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
946 virtual bool usePlainLayout() const { return true; }
949 InsetTabular * asInsetTabular() { return this; }
951 InsetTabular const * asInsetTabular() const { return this; }
953 bool isRightToLeft(Cursor & cur) const;
954 /// writes the cells between stidx and enidx as a string, optionally
955 /// descending into the insets
956 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
958 /// Returns whether the cell in the specified row and column is selected.
959 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
961 // Public structures and variables
963 mutable Tabular tabular;
967 InsetTabular(InsetTabular const &);
969 void doDispatch(Cursor & cur, FuncRequest & cmd);
971 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
973 Inset * clone() const { return new InsetTabular(*this); }
976 void drawCellLines(PainterInfo &, int x, int y, row_type row,
977 idx_type cell) const;
979 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
982 void moveNextCell(Cursor & cur,
983 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
985 void movePrevCell(Cursor & cur,
986 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
988 int cellXPos(idx_type cell) const;
990 int cellYPos(idx_type cell) const;
992 void resetPos(Cursor & cur) const;
994 bool copySelection(Cursor & cur);
996 bool pasteClipboard(Cursor & cur);
998 void cutSelection(Cursor & cur);
1000 void getSelection(Cursor & cur, row_type & rs, row_type & re,
1001 col_type & cs, col_type & ce) const;
1003 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
1005 /// return the "Manhattan distance" to nearest corner
1006 int dist(BufferView &, idx_type cell, int x, int y) const;
1007 /// return the cell nearest to x, y
1008 idx_type getNearestCell(BufferView &, int x, int y) const;
1010 /// test the rotation state of the give cell range.
1011 bool oneCellHasRotationState(bool rotated,
1012 row_type row_start, row_type row_end,
1013 col_type col_start, col_type col_end) const;
1015 mutable idx_type first_visible_cell;
1018 /// The vertical offset of the table due to the vertical
1019 /// alignment with respect to the baseline.
1020 mutable int offset_valign_;
1021 /// true when selecting rows with the mouse
1023 /// true when selecting columns with the mouse
1027 std::string const featureAsString(Tabular::Feature feature);
1029 /// Split cell on decimal symbol
1030 InsetTableCell splitCell(InsetTableCell & head, docstring const & decimal_sym, bool & hassep);
1034 #endif // INSET_TABULAR_H