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,
246 SET_SPECIAL_MULTICOLUMN,
262 TABULAR_VALIGN_MIDDLE,
264 TABULAR_VALIGN_BOTTOM,
266 LONGTABULAR_ALIGN_LEFT,
268 LONGTABULAR_ALIGN_CENTER,
270 LONGTABULAR_ALIGN_RIGHT,
283 CELL_BEGIN_OF_MULTICOLUMN,
285 CELL_PART_OF_MULTICOLUMN,
287 CELL_BEGIN_OF_MULTIROW,
289 CELL_PART_OF_MULTIROW
297 LYX_VALIGN_MIDDLE = 1,
299 LYX_VALIGN_BOTTOM = 2
305 LYX_LONGTABULAR_ALIGN_LEFT = 0,
307 LYX_LONGTABULAR_ALIGN_CENTER = 1,
309 LYX_LONGTABULAR_ALIGN_RIGHT = 2
338 // we have this header type (is set in the getLT... functions)
340 // double borders on top
342 // double borders on bottom
344 // used for FirstHeader & LastFooter and if this is true
345 // all the rows marked as FirstHeader or LastFooter are
346 // ignored in the output and it is set to be empty!
350 /// type for row numbers
351 typedef size_t row_type;
352 /// type for column numbers
353 typedef size_t col_type;
354 /// type for cell indices
355 typedef size_t idx_type;
356 /// index indicating an invalid position
357 static const idx_type npos = static_cast<idx_type>(-1);
360 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
362 /// Returns true if there is a topline, returns false if not
363 bool topLine(idx_type cell) const;
364 /// Returns true if there is a topline, returns false if not
365 bool bottomLine(idx_type cell) const;
366 /// Returns true if there is a topline, returns false if not
367 bool leftLine(idx_type cell) const;
368 /// Returns true if there is a topline, returns false if not
369 bool rightLine(idx_type cell) const;
371 /// return space occupied by the second horizontal line and
372 /// interline space above row \p row in pixels
373 int interRowSpace(row_type row) const;
375 int interColumnSpace(idx_type cell) const;
377 /* returns the maximum over all rows */
379 int cellWidth(idx_type cell) const;
381 int cellHeight(idx_type cell) const;
387 row_type nrows() const {return row_info.size();}
389 col_type ncols() const {return column_info.size();}
391 int rowAscent(row_type row) const;
393 int rowDescent(row_type row) const;
395 void setRowAscent(row_type row, int height);
397 void setRowDescent(row_type row, int height);
399 void setTopLine(idx_type cell, bool line);
401 void setBottomLine(idx_type cell, bool line);
403 void setLeftLine(idx_type cell, bool line);
405 void setRightLine(idx_type cell, bool line);
407 bool rowTopLine(row_type row) const;
409 bool rowBottomLine(row_type row) const;
411 bool columnLeftLine(col_type column) const;
413 bool columnRightLine(col_type column) const;
415 void setAlignment(idx_type cell, LyXAlignment align,
416 bool onlycolumn = false);
418 void setVAlignment(idx_type cell, VAlignment align,
419 bool onlycolumn = false);
421 void setTabularWidth(Length const & l) { tabular_width = l; }
423 Length tabularWidth() const { return tabular_width; }
425 void setColumnPWidth(Cursor &, idx_type, Length const &);
427 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
429 bool setMROffset(Cursor &, idx_type, Length const &);
431 void setAlignSpecial(idx_type cell, docstring const & special,
434 LyXAlignment getAlignment(idx_type cell,
435 bool onlycolumn = false) const;
437 VAlignment getVAlignment(idx_type cell,
438 bool onlycolumn = false) const;
440 Length const getPWidth(idx_type cell) const;
442 Length const getMROffset(idx_type cell) const;
444 int textHOffset(idx_type cell) const;
446 int textVOffset(idx_type cell) const;
448 void appendRow(row_type row);
450 void deleteRow(row_type row);
452 void copyRow(row_type row);
454 void insertRow(row_type row, bool copy);
456 void appendColumn(col_type column);
458 void deleteColumn(col_type column);
460 void copyColumn(col_type column);
462 void insertColumn(col_type column, bool copy);
464 idx_type getFirstCellInRow(row_type row) const;
466 idx_type getLastCellInRow(row_type row) const;
468 idx_type numberOfCellsInRow(row_type row) const;
470 void write(std::ostream &) const;
474 void latex(otexstream &, OutputParams const &) const;
476 int docbook(odocstream & os, OutputParams const &) const;
478 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
480 void plaintext(odocstream &,
481 OutputParams const & runparams, int const depth,
482 bool onlydata, char_type delim) const;
484 bool isMultiColumn(idx_type cell) const;
486 idx_type setMultiColumn(idx_type cell, idx_type number,
487 bool const right_border);
489 void unsetMultiColumn(idx_type cell);
491 bool isPartOfMultiColumn(row_type row, col_type column) const;
493 bool isPartOfMultiRow(row_type row, col_type column) const;
495 bool isMultiRow(idx_type cell) const;
497 idx_type setMultiRow(idx_type cell, idx_type number,
498 bool const bottom_border);
500 void unsetMultiRow(idx_type cell);
502 row_type cellRow(idx_type cell) const;
504 col_type cellColumn(idx_type cell) const;
506 void setRotateCell(idx_type cell, int);
508 int getRotateCell(idx_type cell) const;
510 bool needRotating() const;
512 bool isLastCell(idx_type cell) const;
514 idx_type cellAbove(idx_type cell) const;
516 idx_type cellBelow(idx_type cell) const;
518 idx_type cellIndex(row_type row, col_type column) const;
520 void setUsebox(idx_type cell, BoxType);
522 BoxType getUsebox(idx_type cell) const;
524 // Long Tabular Options support functions
526 void setLTHead(row_type row, bool flag, ltType const &, bool first);
528 bool getRowOfLTHead(row_type row, ltType &) const;
530 bool getRowOfLTFirstHead(row_type row, ltType &) const;
532 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
534 bool getRowOfLTFoot(row_type row, ltType &) const;
536 bool getRowOfLTLastFoot(row_type row, ltType &) const;
538 void setLTNewPage(row_type row, bool what);
540 bool getLTNewPage(row_type row) const;
542 idx_type setLTCaption(row_type row, bool what);
544 bool ltCaption(row_type row) const;
546 bool haveLTHead(bool withcaptions = true) const;
548 bool haveLTFirstHead(bool withcaptions = true) const;
550 bool haveLTFoot(bool withcaptions = true) const;
552 bool haveLTLastFoot(bool withcaptions = true) const;
554 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
556 // end longtable support
558 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
560 shared_ptr<InsetTableCell> cellInset(row_type row,
561 col_type column) const;
563 void setCellInset(row_type row, col_type column,
564 shared_ptr<InsetTableCell>) const;
565 /// Search for \param inset in the tabular, with the
567 void validate(LaTeXFeatures &) const;
569 // FIXME Now that cells have an InsetTableCell as their insets, rather
570 // than an InsetText, it'd be possible to reverse the relationship here,
571 // so that cell_vector was a vector<InsetTableCell> rather than a
572 // vector<CellData>, and an InsetTableCell had a CellData as a member,
573 // or perhaps just had its members as members.
580 CellData(CellData const &);
582 CellData & operator=(CellData);
584 void swap(CellData & rhs);
596 LyXAlignment alignment;
598 VAlignment valignment;
599 /// width of the part before the decimal
601 /// width of the decimal part
618 docstring align_special;
620 Length p_width; // this is only set for multicolumn!!!
622 shared_ptr<InsetTableCell> inset;
624 CellData & cellInfo(idx_type cell) const;
626 typedef std::vector<CellData> cell_vector;
628 typedef std::vector<cell_vector> cell_vvector;
639 /// Extra space between the top line and this row
641 /// Ignore top_space if true and use the default top space
642 bool top_space_default;
643 /// Extra space between this row and the bottom line
645 /// Ignore bottom_space if true and use the default bottom space
646 bool bottom_space_default;
647 /// Extra space between the bottom line and the next top line
648 Length interline_space;
649 /// Ignore interline_space if true and use the default interline space
650 bool interline_space_default;
651 /// This are for longtabulars only
654 /// a row of endfirsthead
658 /// row of endlastfoot
660 /// row for a newpage
666 typedef std::vector<RowData> row_vector;
674 LyXAlignment alignment;
676 VAlignment valignment;
682 docstring align_special;
684 docstring decimal_point;
687 typedef std::vector<ColumnData> column_vector;
690 idx_type numberofcells;
692 std::vector<row_type> rowofcell;
694 std::vector<col_type> columnofcell;
698 column_vector column_info;
700 mutable cell_vvector cell_info;
702 Length tabular_width;
708 VAlignment tabular_valignment;
712 HAlignment longtabular_alignment;
714 bool is_long_tabular;
717 /// endfirsthead data
725 void init(Buffer *, row_type rows_arg,
726 col_type columns_arg);
728 void updateIndexes();
730 bool setFixedWidth(row_type r, col_type c);
731 /// return true of update is needed
732 bool updateColumnWidths();
734 idx_type columnSpan(idx_type cell) const;
736 idx_type rowSpan(idx_type cell) const;
738 BoxType useParbox(idx_type cell) const;
740 // helper function for Latex
742 void TeXTopHLine(otexstream &, row_type row, std::string const lang) const;
744 void TeXBottomHLine(otexstream &, row_type row, std::string const lang) const;
746 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
748 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
750 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
752 bool isValidRow(row_type const row) const;
754 void TeXRow(otexstream &, row_type const row,
755 OutputParams const &) const;
757 // helper functions for plain text
759 bool plaintextTopHLine(odocstream &, row_type row,
760 std::vector<unsigned int> const &) const;
762 bool plaintextBottomHLine(odocstream &, row_type row,
763 std::vector<unsigned int> const &) const;
765 void plaintextPrintCell(odocstream &,
766 OutputParams const &,
767 idx_type cell, row_type row, col_type column,
768 std::vector<unsigned int> const &,
769 bool onlydata) const;
770 /// auxiliary function for docbook
771 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
773 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
774 bool header = false) const;
776 /// change associated Buffer
777 void setBuffer(Buffer & buffer);
778 /// retrieve associated Buffer
779 Buffer & buffer() const { return *buffer_; }
787 class InsetTabular : public Inset
791 InsetTabular(Buffer *, row_type rows = 1,
792 col_type columns = 1);
796 void setBuffer(Buffer & buffer);
799 static void string2params(std::string const &, InsetTabular &);
801 static std::string params2string(InsetTabular const &);
805 void write(std::ostream &) const;
807 void metrics(MetricsInfo &, Dimension &) const;
809 void draw(PainterInfo & pi, int x, int y) const;
811 void drawSelection(PainterInfo & pi, int x, int y) const;
813 void drawBackground(PainterInfo & pi, int x, int y) const;
815 bool editable() const { return true; }
817 bool hasSettings() const { return true; }
819 bool insetAllowed(InsetCode code) const;
821 bool allowSpellCheck() const { return true; }
823 bool canTrackChanges() const { return true; }
824 /** returns false if, when outputing LaTeX, font changes should
825 be closed before generating this inset. This is needed for
826 insets that may contain several paragraphs */
827 bool inheritFont() const { return false; }
829 DisplayType display() const;
831 void latex(otexstream &, OutputParams const &) const;
833 int plaintext(odocstream &, OutputParams const &) const;
835 int docbook(odocstream &, OutputParams const &) const;
837 docstring xhtml(XHTMLStream &, OutputParams const &) const;
839 void validate(LaTeXFeatures & features) const;
841 InsetCode lyxCode() const { return TABULAR_CODE; }
843 std::string contextMenu(BufferView const &, int, int) const;
845 std::string contextMenuName() const;
846 /// get offset of this cursor slice relative to our upper left corner
847 void cursorPos(BufferView const & bv, CursorSlice const & sl,
848 bool boundary, int & x, int & y) const;
850 bool tabularFeatures(Cursor & cur, std::string const & what);
852 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
853 std::string const & val = std::string());
855 size_t nargs() const { return tabular.numberofcells; }
857 shared_ptr<InsetTableCell const> cell(idx_type) const;
859 shared_ptr<InsetTableCell> cell(idx_type);
861 Text * getText(int) const;
863 /// set the change for the entire inset
864 void setChange(Change const & change);
865 /// accept the changes within the inset
866 void acceptChanges();
867 /// reject the changes within the inset
868 void rejectChanges();
870 // this should return true if we have a "normal" cell, otherwise false.
871 // "normal" means without width set!
872 /// should all paragraphs be output with "Standard" layout?
873 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
875 virtual bool forcePlainLayout(idx_type cell = 0) const;
877 void addPreview(DocIterator const & inset_pos,
878 graphics::PreviewLoader &) const;
880 /// lock cell with given index
881 void edit(Cursor & cur, bool front, EntryDirection entry_from);
882 /// get table row from x coordinate
883 int rowFromY(Cursor & cur, int y) const;
884 /// get table column from y coordinate
885 int columnFromX(Cursor & cur, int x) const;
887 Inset * editXY(Cursor & cur, int x, int y);
888 /// can we go further down on mouse click?
889 bool descendable(BufferView const &) const { return true; }
890 /// Update the counters of this inset and of its contents
891 void updateBuffer(ParIterator const &, UpdateType);
893 void addToToc(DocIterator const &) const;
896 bool completionSupported(Cursor const &) const;
898 bool inlineCompletionSupported(Cursor const & cur) const;
900 bool automaticInlineCompletion() const;
902 bool automaticPopupCompletion() const;
904 bool showCompletionCursor() const;
906 CompletionList const * createCompletionList(Cursor const & cur) const;
908 docstring completionPrefix(Cursor const & cur) const;
910 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
912 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
914 virtual bool usePlainLayout() const { return true; }
917 InsetTabular * asInsetTabular() { return this; }
919 InsetTabular const * asInsetTabular() const { return this; }
921 bool isRightToLeft(Cursor & cur) const;
922 /// writes the cells between stidx and enidx as a string, optionally
923 /// descending into the insets
924 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
926 /// Returns whether the cell in the specified row and column is selected.
927 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
929 // Public structures and variables
931 mutable Tabular tabular;
935 InsetTabular(InsetTabular const &);
937 void doDispatch(Cursor & cur, FuncRequest & cmd);
939 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
941 Inset * clone() const { return new InsetTabular(*this); }
944 void drawCellLines(PainterInfo &, int x, int y, row_type row,
945 idx_type cell) const;
947 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
950 void moveNextCell(Cursor & cur,
951 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
953 void movePrevCell(Cursor & cur,
954 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
956 int cellXPos(idx_type cell) const;
958 int cellYPos(idx_type cell) const;
960 void resetPos(Cursor & cur) const;
962 bool copySelection(Cursor & cur);
964 bool pasteClipboard(Cursor & cur);
966 void cutSelection(Cursor & cur);
968 void getSelection(Cursor & cur, row_type & rs, row_type & re,
969 col_type & cs, col_type & ce) const;
971 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
973 /// return the "Manhattan distance" to nearest corner
974 int dist(BufferView &, idx_type cell, int x, int y) const;
975 /// return the cell nearest to x, y
976 idx_type getNearestCell(BufferView &, int x, int y) const;
978 /// test the rotation state of the give cell range.
979 bool oneCellHasRotationState(bool rotated,
980 row_type row_start, row_type row_end,
981 col_type col_start, col_type col_end) const;
983 mutable idx_type first_visible_cell;
986 /// The vertical offset of the table due to the vertical
987 /// alignment with respect to the baseline.
988 mutable int offset_valign_;
989 /// true when selecting rows with the mouse
991 /// true when selecting columns with the mouse
995 std::string const featureAsString(Tabular::Feature feature);
997 /// Split cell on decimal symbol
998 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
1002 #endif // INSET_TABULAR_H