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
14 * Full author contact details are available in file CREDITS.
17 // Things to think of when designing the new tabular support:
18 // - color support (colortbl, color)
19 // - decimal alignment (dcloumn)
20 // - custom lines (hhline)
23 #ifndef INSET_TABULAR_H
24 #define INSET_TABULAR_H
27 #include "InsetText.h"
30 #include "support/shared_ptr.h"
50 class InsetTableCell : public InsetText
54 InsetTableCell(Buffer * buf);
56 InsetCode lyxCode() const { return CELL_CODE; }
58 Inset * clone() { return new InsetTableCell(*this); }
60 bool getStatus(Cursor & cur, FuncRequest const & cmd,
61 FuncStatus & status) const;
63 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
65 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
66 /// writes the contents of the cell as a string, optionally
67 /// descending into insets
68 docstring asString(bool intoInsets = true);
70 docstring xhtml(XHTMLStream &, OutputParams const &) const;
75 void operator=(InsetTableCell const &);
77 // This boolean is supposed to track whether the cell has had its
78 // width explicitly set. We need to know this to determine whether
79 // layout changes and paragraph customization are allowed---that is,
80 // we need it in forcePlainLayout() and allowParagraphCustomization().
81 // Unfortunately, that information is not readily available in
82 // InsetTableCell. In the case of multicolumn cells, it is present
83 // in CellData, and so would be available here if CellData were to
84 // become a member of InsetTableCell. But in the other case, it isn't
85 // even available there, but is held in Tabular::ColumnData.
86 // So, the present solution uses this boolean to track the information
87 // we need to track, and tries to keep it updated. This is not ideal,
88 // but the other solutions are no better. These are:
89 // (i) Keep a pointer in InsetTableCell to the table;
90 // (ii) Find the table by iterating over the Buffer's insets.
91 // Solution (i) raises the problem of updating the pointer when an
92 // InsetTableCell is copied, and we'd therefore need a copy constructor
93 // in InsetTabular and then in Tabular, which seems messy, given how
94 // complicated those classes are. Solution (ii) involves a lot of
95 // iterating, since this information is needed quite often, and so may
97 // So, well, if someone can do better, please do!
101 // FIXME: Here the thoughts from the comment above also apply.
103 LyXAlignment contentAlign;
104 /// should paragraph indendation be omitted in any case?
105 bool neverIndent() const { return true; }
107 LyXAlignment contentAlignment() const { return contentAlign; }
109 virtual bool usePlainLayout() const { return true; }
111 virtual bool forcePlainLayout(idx_type = 0) const;
113 virtual bool allowParagraphCustomization(idx_type = 0) const;
114 /// Is the width forced to some value?
115 bool hasFixedWidth() const { return isFixedWidth; }
120 // A helper struct for tables
211 UNSET_ROTATE_TABULAR,
213 TOGGLE_ROTATE_TABULAR,
245 SET_SPECIAL_MULTICOLUMN,
261 TABULAR_VALIGN_MIDDLE,
263 TABULAR_VALIGN_BOTTOM,
265 LONGTABULAR_ALIGN_LEFT,
267 LONGTABULAR_ALIGN_CENTER,
269 LONGTABULAR_ALIGN_RIGHT,
280 CELL_BEGIN_OF_MULTICOLUMN,
282 CELL_PART_OF_MULTICOLUMN,
284 CELL_BEGIN_OF_MULTIROW,
286 CELL_PART_OF_MULTIROW
294 LYX_VALIGN_MIDDLE = 1,
296 LYX_VALIGN_BOTTOM = 2
302 LYX_LONGTABULAR_ALIGN_LEFT = 0,
304 LYX_LONGTABULAR_ALIGN_CENTER = 1,
306 LYX_LONGTABULAR_ALIGN_RIGHT = 2
322 // we have this header type (is set in the getLT... functions)
324 // double borders on top
326 // double borders on bottom
328 // used for FirstHeader & LastFooter and if this is true
329 // all the rows marked as FirstHeader or LastFooter are
330 // ignored in the output and it is set to be empty!
334 /// type for row numbers
335 typedef size_t row_type;
336 /// type for column numbers
337 typedef size_t col_type;
338 /// type for cell indices
339 typedef size_t idx_type;
340 /// index indicating an invalid position
341 static const idx_type npos = static_cast<idx_type>(-1);
344 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
346 /// Returns true if there is a topline, returns false if not
347 bool topLine(idx_type cell) const;
348 /// Returns true if there is a topline, returns false if not
349 bool bottomLine(idx_type cell) const;
350 /// Returns true if there is a topline, returns false if not
351 bool leftLine(idx_type cell) const;
352 /// Returns true if there is a topline, returns false if not
353 bool rightLine(idx_type cell) const;
355 /// return space occupied by the second horizontal line and
356 /// interline space above row \p row in pixels
357 int interRowSpace(row_type row) const;
359 int interColumnSpace(idx_type cell) const;
361 /* returns the maximum over all rows */
363 int cellWidth(idx_type cell) const;
365 int cellHeight(idx_type cell) const;
371 row_type nrows() const {return row_info.size();}
373 col_type ncols() const {return column_info.size();}
375 int rowAscent(row_type row) const;
377 int rowDescent(row_type row) const;
379 void setRowAscent(row_type row, int height);
381 void setRowDescent(row_type row, int height);
383 void setTopLine(idx_type cell, bool line);
385 void setBottomLine(idx_type cell, bool line);
387 void setLeftLine(idx_type cell, bool line);
389 void setRightLine(idx_type cell, bool line);
391 bool rowTopLine(row_type row) const;
393 bool rowBottomLine(row_type row) const;
395 bool columnLeftLine(col_type column) const;
397 bool columnRightLine(col_type column) const;
399 void setAlignment(idx_type cell, LyXAlignment align,
400 bool onlycolumn = false);
402 void setVAlignment(idx_type cell, VAlignment align,
403 bool onlycolumn = false);
405 void setColumnPWidth(Cursor &, idx_type, Length const &);
407 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
409 bool setMROffset(Cursor &, idx_type, Length const &);
411 void setAlignSpecial(idx_type cell, docstring const & special,
414 LyXAlignment getAlignment(idx_type cell,
415 bool onlycolumn = false) const;
417 VAlignment getVAlignment(idx_type cell,
418 bool onlycolumn = false) const;
420 Length const getPWidth(idx_type cell) const;
422 Length const getMROffset(idx_type cell) const;
424 int textHOffset(idx_type cell) const;
426 int textVOffset(idx_type cell) const;
428 void appendRow(idx_type cell);
430 void deleteRow(row_type row);
432 void copyRow(row_type);
434 void appendColumn(idx_type cell);
436 void deleteColumn(col_type column);
438 void copyColumn(col_type);
440 idx_type getFirstCellInRow(row_type row) const;
442 idx_type getLastCellInRow(row_type row) const;
444 idx_type numberOfCellsInRow(row_type row) const;
446 void write(std::ostream &) const;
450 int latex(odocstream &, OutputParams const &) const;
452 int docbook(odocstream & os, OutputParams const &) const;
454 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
456 void plaintext(odocstream &,
457 OutputParams const & runparams, int const depth,
458 bool onlydata, char_type delim) const;
460 bool isMultiColumn(idx_type cell) const;
462 idx_type setMultiColumn(idx_type cell, idx_type number);
464 void unsetMultiColumn(idx_type cell);
466 bool isPartOfMultiColumn(row_type row, col_type column) const;
468 bool isPartOfMultiRow(row_type row, col_type column) const;
470 bool isMultiRow(idx_type cell) const;
472 idx_type setMultiRow(idx_type cell, idx_type number);
474 void unsetMultiRow(idx_type cell);
476 row_type cellRow(idx_type cell) const;
478 col_type cellColumn(idx_type cell) const;
480 void setRotateCell(idx_type cell, bool);
482 bool getRotateCell(idx_type cell) const;
484 bool needRotating() const;
486 bool isLastCell(idx_type cell) const;
488 idx_type cellAbove(idx_type cell) const;
490 idx_type cellBelow(idx_type cell) const;
492 idx_type cellIndex(row_type row, col_type column) const;
494 void setUsebox(idx_type cell, BoxType);
496 BoxType getUsebox(idx_type cell) const;
498 // Long Tabular Options support functions
500 bool checkLTType(row_type row, ltType const &) const;
502 void setLTHead(row_type row, bool flag, ltType const &, bool first);
504 bool getRowOfLTHead(row_type row, ltType &) const;
506 bool getRowOfLTFirstHead(row_type row, ltType &) const;
508 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
510 bool getRowOfLTFoot(row_type row, ltType &) const;
512 bool getRowOfLTLastFoot(row_type row, ltType &) const;
514 void setLTNewPage(row_type row, bool what);
516 bool getLTNewPage(row_type row) const;
518 idx_type setLTCaption(row_type row, bool what);
520 bool ltCaption(row_type row) const;
522 bool haveLTHead() const;
524 bool haveLTFirstHead() const;
526 bool haveLTFoot() const;
528 bool haveLTLastFoot() const;
530 bool haveLTCaption() const;
532 // end longtable support
534 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
536 shared_ptr<InsetTableCell> cellInset(row_type row,
537 col_type column) const;
539 void setCellInset(row_type row, col_type column,
540 shared_ptr<InsetTableCell>) const;
541 /// Search for \param inset in the tabular, with the
543 void validate(LaTeXFeatures &) const;
545 // FIXME Now that cells have an InsetTableCell as their insets, rather
546 // than an InsetText, it'd be possible to reverse the relationship here,
547 // so that cell_vector was a vector<InsetTableCell> rather than a
548 // vector<CellData>, and an InsetTableCell had a CellData as a member,
549 // or perhaps just had its members as members.
556 CellData(CellData const &);
558 CellData & operator=(CellData);
560 void swap(CellData & rhs);
572 LyXAlignment alignment;
574 VAlignment valignment;
575 /// width of the part before the decimal
577 /// width of the decimal part
594 docstring align_special;
596 Length p_width; // this is only set for multicolumn!!!
598 shared_ptr<InsetTableCell> inset;
600 CellData & cellInfo(idx_type cell) const;
602 typedef std::vector<CellData> cell_vector;
604 typedef std::vector<cell_vector> cell_vvector;
615 /// Extra space between the top line and this row
617 /// Ignore top_space if true and use the default top space
618 bool top_space_default;
619 /// Extra space between this row and the bottom line
621 /// Ignore bottom_space if true and use the default bottom space
622 bool bottom_space_default;
623 /// Extra space between the bottom line and the next top line
624 Length interline_space;
625 /// Ignore interline_space if true and use the default interline space
626 bool interline_space_default;
627 /// This are for longtabulars only
630 /// a row of endfirsthead
634 /// row of endlastfoot
636 /// row for a newpage
642 typedef std::vector<RowData> row_vector;
650 LyXAlignment alignment;
652 VAlignment valignment;
658 docstring align_special;
660 docstring decimal_point;
663 typedef std::vector<ColumnData> column_vector;
666 idx_type numberofcells;
668 std::vector<row_type> rowofcell;
670 std::vector<col_type> columnofcell;
674 column_vector column_info;
676 mutable cell_vvector cell_info;
682 VAlignment tabular_valignment;
686 HAlignment longtabular_alignment;
688 bool is_long_tabular;
691 /// endfirsthead data
699 void init(Buffer *, row_type rows_arg,
700 col_type columns_arg);
702 void updateIndexes();
704 bool setFixedWidth(row_type r, col_type c);
705 /// return true of update is needed
706 bool updateColumnWidths();
708 idx_type columnSpan(idx_type cell) const;
710 idx_type rowSpan(idx_type cell) const;
712 BoxType useParbox(idx_type cell) const;
714 // helper function for Latex returns number of newlines
716 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
718 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
720 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
722 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
724 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
726 bool isValidRow(row_type const row) const;
728 int TeXRow(odocstream &, row_type const row,
729 OutputParams const &) const;
731 // helper functions for plain text
733 bool plaintextTopHLine(odocstream &, row_type row,
734 std::vector<unsigned int> const &) const;
736 bool plaintextBottomHLine(odocstream &, row_type row,
737 std::vector<unsigned int> const &) const;
739 void plaintextPrintCell(odocstream &,
740 OutputParams const &,
741 idx_type cell, row_type row, col_type column,
742 std::vector<unsigned int> const &,
743 bool onlydata) const;
744 /// auxiliary function for docbook
745 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
747 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
748 bool header = false) const;
750 /// change associated Buffer
751 void setBuffer(Buffer & buffer);
752 /// retrieve associated Buffer
753 Buffer & buffer() const { return *buffer_; }
761 class InsetTabular : public Inset
765 InsetTabular(Buffer *, row_type rows = 1,
766 col_type columns = 1);
770 void setBuffer(Buffer & buffer);
773 static void string2params(std::string const &, InsetTabular &);
775 static std::string params2string(InsetTabular const &);
779 void write(std::ostream &) const;
781 void metrics(MetricsInfo &, Dimension &) const;
783 void draw(PainterInfo & pi, int x, int y) const;
785 void drawSelection(PainterInfo & pi, int x, int y) const;
787 void drawBackground(PainterInfo & pi, int x, int y) const;
789 bool editable() const { return true; }
791 bool hasSettings() const { return true; }
793 bool insetAllowed(InsetCode code) const;
795 bool allowSpellCheck() const { return true; }
797 bool canTrackChanges() const { return true; }
798 /** returns true if, when outputing LaTeX, font changes should
799 be closed before generating this inset. This is needed for
800 insets that may contain several paragraphs */
801 bool noFontChange() const { return true; }
803 DisplayType display() const;
805 int latex(odocstream &, OutputParams const &) const;
807 int plaintext(odocstream &, OutputParams const &) const;
809 int docbook(odocstream &, OutputParams const &) const;
811 docstring xhtml(XHTMLStream &, OutputParams const &) const;
813 void validate(LaTeXFeatures & features) const;
815 InsetCode lyxCode() const { return TABULAR_CODE; }
817 docstring contextMenu(BufferView const &, int, int) const;
819 docstring contextMenuName() const;
820 /// get offset of this cursor slice relative to our upper left corner
821 void cursorPos(BufferView const & bv, CursorSlice const & sl,
822 bool boundary, int & x, int & y) const;
824 bool tabularFeatures(Cursor & cur, std::string const & what);
826 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
827 std::string const & val = std::string());
829 size_t nargs() const { return tabular.numberofcells; }
831 shared_ptr<InsetTableCell const> cell(idx_type) const;
833 shared_ptr<InsetTableCell> cell(idx_type);
835 Text * getText(int) const;
837 /// set the change for the entire inset
838 void setChange(Change const & change);
839 /// accept the changes within the inset
840 void acceptChanges();
841 /// reject the changes within the inset
842 void rejectChanges();
844 // this should return true if we have a "normal" cell, otherwise false.
845 // "normal" means without width set!
846 /// should all paragraphs be output with "Standard" layout?
847 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
849 virtual bool forcePlainLayout(idx_type cell = 0) const;
851 void addPreview(DocIterator const & inset_pos,
852 graphics::PreviewLoader &) const;
854 /// lock cell with given index
855 void edit(Cursor & cur, bool front, EntryDirection entry_from);
856 /// get table row from x coordinate
857 int rowFromY(Cursor & cur, int y) const;
858 /// get table column from y coordinate
859 int columnFromX(Cursor & cur, int x) const;
861 Inset * editXY(Cursor & cur, int x, int y);
862 /// can we go further down on mouse click?
863 bool descendable(BufferView const &) const { return true; }
864 /// Update the counters of this inset and of its contents
865 void updateBuffer(ParIterator const &, UpdateType);
867 void addToToc(DocIterator const &) const;
870 bool completionSupported(Cursor const &) const;
872 bool inlineCompletionSupported(Cursor const & cur) const;
874 bool automaticInlineCompletion() const;
876 bool automaticPopupCompletion() const;
878 bool showCompletionCursor() const;
880 CompletionList const * createCompletionList(Cursor const & cur) const;
882 docstring completionPrefix(Cursor const & cur) const;
884 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
886 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
888 virtual bool usePlainLayout() const { return true; }
891 InsetTabular * asInsetTabular() { return this; }
893 InsetTabular const * asInsetTabular() const { return this; }
895 bool isRightToLeft(Cursor & cur) const;
896 /// writes the cells between stidx and enidx as a string, optionally
897 /// descending into the insets
898 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
900 /// Returns whether the cell in the specified row and column is selected.
901 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
903 // Public structures and variables
905 mutable Tabular tabular;
909 InsetTabular(InsetTabular const &);
911 void doDispatch(Cursor & cur, FuncRequest & cmd);
913 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
915 int scroll() const { return scx_; }
917 Inset * clone() const { return new InsetTabular(*this); }
920 void drawCellLines(PainterInfo &, int x, int y, row_type row,
921 idx_type cell) const;
923 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
926 void moveNextCell(Cursor & cur,
927 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
929 void movePrevCell(Cursor & cur,
930 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
932 int cellXPos(idx_type cell) const;
934 int cellYPos(idx_type cell) const;
936 void resetPos(Cursor & cur) const;
938 bool copySelection(Cursor & cur);
940 bool pasteClipboard(Cursor & cur);
942 void cutSelection(Cursor & cur);
944 void getSelection(Cursor & cur, row_type & rs, row_type & re,
945 col_type & cs, col_type & ce) const;
947 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
949 /// return the "Manhattan distance" to nearest corner
950 int dist(BufferView &, idx_type cell, int x, int y) const;
951 /// return the cell nearest to x, y
952 idx_type getNearestCell(BufferView &, int x, int y) const;
954 /// test the rotation state of the give cell range.
955 bool oneCellHasRotationState(bool rotated,
956 row_type row_start, row_type row_end,
957 col_type col_start, col_type col_end) const;
959 mutable idx_type first_visible_cell;
962 /// The vertical offset of the table due to the vertical
963 /// alignment with respect to the baseline.
964 mutable int offset_valign_;
965 /// true when selecting rows with the mouse
967 /// true when selecting columns with the mouse
971 std::string const featureAsString(Tabular::Feature feature);
973 /// Split cell on decimal symbol
974 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
978 #endif // INSET_TABULAR_H