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,
282 CELL_BEGIN_OF_MULTICOLUMN,
284 CELL_PART_OF_MULTICOLUMN,
286 CELL_BEGIN_OF_MULTIROW,
288 CELL_PART_OF_MULTIROW
296 LYX_VALIGN_MIDDLE = 1,
298 LYX_VALIGN_BOTTOM = 2
304 LYX_LONGTABULAR_ALIGN_LEFT = 0,
306 LYX_LONGTABULAR_ALIGN_CENTER = 1,
308 LYX_LONGTABULAR_ALIGN_RIGHT = 2
324 // we have this header type (is set in the getLT... functions)
326 // double borders on top
328 // double borders on bottom
330 // used for FirstHeader & LastFooter and if this is true
331 // all the rows marked as FirstHeader or LastFooter are
332 // ignored in the output and it is set to be empty!
336 /// type for row numbers
337 typedef size_t row_type;
338 /// type for column numbers
339 typedef size_t col_type;
340 /// type for cell indices
341 typedef size_t idx_type;
342 /// index indicating an invalid position
343 static const idx_type npos = static_cast<idx_type>(-1);
346 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
348 /// Returns true if there is a topline, returns false if not
349 bool topLine(idx_type cell) const;
350 /// Returns true if there is a topline, returns false if not
351 bool bottomLine(idx_type cell) const;
352 /// Returns true if there is a topline, returns false if not
353 bool leftLine(idx_type cell) const;
354 /// Returns true if there is a topline, returns false if not
355 bool rightLine(idx_type cell) const;
357 /// return space occupied by the second horizontal line and
358 /// interline space above row \p row in pixels
359 int interRowSpace(row_type row) const;
361 int interColumnSpace(idx_type cell) const;
363 /* returns the maximum over all rows */
365 int cellWidth(idx_type cell) const;
367 int cellHeight(idx_type cell) const;
373 row_type nrows() const {return row_info.size();}
375 col_type ncols() const {return column_info.size();}
377 int rowAscent(row_type row) const;
379 int rowDescent(row_type row) const;
381 void setRowAscent(row_type row, int height);
383 void setRowDescent(row_type row, int height);
385 void setTopLine(idx_type cell, bool line);
387 void setBottomLine(idx_type cell, bool line);
389 void setLeftLine(idx_type cell, bool line);
391 void setRightLine(idx_type cell, bool line);
393 bool rowTopLine(row_type row) const;
395 bool rowBottomLine(row_type row) const;
397 bool columnLeftLine(col_type column) const;
399 bool columnRightLine(col_type column) const;
401 void setAlignment(idx_type cell, LyXAlignment align,
402 bool onlycolumn = false);
404 void setVAlignment(idx_type cell, VAlignment align,
405 bool onlycolumn = false);
407 void setTabularWidth(Length const & l) { tabular_width = l; }
409 Length tabularWidth() const { return tabular_width; }
411 void setColumnPWidth(Cursor &, idx_type, Length const &);
413 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
415 bool setMROffset(Cursor &, idx_type, Length const &);
417 void setAlignSpecial(idx_type cell, docstring const & special,
420 LyXAlignment getAlignment(idx_type cell,
421 bool onlycolumn = false) const;
423 VAlignment getVAlignment(idx_type cell,
424 bool onlycolumn = false) const;
426 Length const getPWidth(idx_type cell) const;
428 Length const getMROffset(idx_type cell) const;
430 int textHOffset(idx_type cell) const;
432 int textVOffset(idx_type cell) const;
434 void appendRow(idx_type cell);
436 void deleteRow(row_type row);
438 void copyRow(row_type);
440 void appendColumn(idx_type cell);
442 void deleteColumn(col_type column);
444 void copyColumn(col_type);
446 idx_type getFirstCellInRow(row_type row) const;
448 idx_type getLastCellInRow(row_type row) const;
450 idx_type numberOfCellsInRow(row_type row) const;
452 void write(std::ostream &) const;
456 void latex(otexstream &, OutputParams const &) const;
458 int docbook(odocstream & os, OutputParams const &) const;
460 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
462 void plaintext(odocstream &,
463 OutputParams const & runparams, int const depth,
464 bool onlydata, char_type delim) const;
466 bool isMultiColumn(idx_type cell) const;
468 idx_type setMultiColumn(idx_type cell, idx_type number);
470 void unsetMultiColumn(idx_type cell);
472 bool isPartOfMultiColumn(row_type row, col_type column) const;
474 bool isPartOfMultiRow(row_type row, col_type column) const;
476 bool isMultiRow(idx_type cell) const;
478 idx_type setMultiRow(idx_type cell, idx_type number);
480 void unsetMultiRow(idx_type cell);
482 row_type cellRow(idx_type cell) const;
484 col_type cellColumn(idx_type cell) const;
486 void setRotateCell(idx_type cell, bool);
488 bool getRotateCell(idx_type cell) const;
490 bool needRotating() const;
492 bool isLastCell(idx_type cell) const;
494 idx_type cellAbove(idx_type cell) const;
496 idx_type cellBelow(idx_type cell) const;
498 idx_type cellIndex(row_type row, col_type column) const;
500 void setUsebox(idx_type cell, BoxType);
502 BoxType getUsebox(idx_type cell) const;
504 // Long Tabular Options support functions
506 bool checkLTType(row_type row, ltType const &) const;
508 void setLTHead(row_type row, bool flag, ltType const &, bool first);
510 bool getRowOfLTHead(row_type row, ltType &) const;
512 bool getRowOfLTFirstHead(row_type row, ltType &) const;
514 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
516 bool getRowOfLTFoot(row_type row, ltType &) const;
518 bool getRowOfLTLastFoot(row_type row, ltType &) const;
520 void setLTNewPage(row_type row, bool what);
522 bool getLTNewPage(row_type row) const;
524 idx_type setLTCaption(row_type row, bool what);
526 bool ltCaption(row_type row) const;
528 bool haveLTHead() const;
530 bool haveLTFirstHead() const;
532 bool haveLTFoot() const;
534 bool haveLTLastFoot() const;
536 bool haveLTCaption() const;
538 // end longtable support
540 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
542 shared_ptr<InsetTableCell> cellInset(row_type row,
543 col_type column) const;
545 void setCellInset(row_type row, col_type column,
546 shared_ptr<InsetTableCell>) const;
547 /// Search for \param inset in the tabular, with the
549 void validate(LaTeXFeatures &) const;
551 // FIXME Now that cells have an InsetTableCell as their insets, rather
552 // than an InsetText, it'd be possible to reverse the relationship here,
553 // so that cell_vector was a vector<InsetTableCell> rather than a
554 // vector<CellData>, and an InsetTableCell had a CellData as a member,
555 // or perhaps just had its members as members.
562 CellData(CellData const &);
564 CellData & operator=(CellData);
566 void swap(CellData & rhs);
578 LyXAlignment alignment;
580 VAlignment valignment;
581 /// width of the part before the decimal
583 /// width of the decimal part
600 docstring align_special;
602 Length p_width; // this is only set for multicolumn!!!
604 shared_ptr<InsetTableCell> inset;
606 CellData & cellInfo(idx_type cell) const;
608 typedef std::vector<CellData> cell_vector;
610 typedef std::vector<cell_vector> cell_vvector;
621 /// Extra space between the top line and this row
623 /// Ignore top_space if true and use the default top space
624 bool top_space_default;
625 /// Extra space between this row and the bottom line
627 /// Ignore bottom_space if true and use the default bottom space
628 bool bottom_space_default;
629 /// Extra space between the bottom line and the next top line
630 Length interline_space;
631 /// Ignore interline_space if true and use the default interline space
632 bool interline_space_default;
633 /// This are for longtabulars only
636 /// a row of endfirsthead
640 /// row of endlastfoot
642 /// row for a newpage
648 typedef std::vector<RowData> row_vector;
656 LyXAlignment alignment;
658 VAlignment valignment;
664 docstring align_special;
666 docstring decimal_point;
669 typedef std::vector<ColumnData> column_vector;
672 idx_type numberofcells;
674 std::vector<row_type> rowofcell;
676 std::vector<col_type> columnofcell;
680 column_vector column_info;
682 mutable cell_vvector cell_info;
684 Length tabular_width;
690 VAlignment tabular_valignment;
694 HAlignment longtabular_alignment;
696 bool is_long_tabular;
699 /// endfirsthead data
707 void init(Buffer *, row_type rows_arg,
708 col_type columns_arg);
710 void updateIndexes();
712 bool setFixedWidth(row_type r, col_type c);
713 /// return true of update is needed
714 bool updateColumnWidths();
716 idx_type columnSpan(idx_type cell) const;
718 idx_type rowSpan(idx_type cell) const;
720 BoxType useParbox(idx_type cell) const;
722 // helper function for Latex
724 void TeXTopHLine(otexstream &, row_type row, std::string const lang) const;
726 void TeXBottomHLine(otexstream &, row_type row, std::string const lang) const;
728 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
730 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
732 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
734 bool isValidRow(row_type const row) const;
736 void TeXRow(otexstream &, row_type const row,
737 OutputParams const &) const;
739 // helper functions for plain text
741 bool plaintextTopHLine(odocstream &, row_type row,
742 std::vector<unsigned int> const &) const;
744 bool plaintextBottomHLine(odocstream &, row_type row,
745 std::vector<unsigned int> const &) const;
747 void plaintextPrintCell(odocstream &,
748 OutputParams const &,
749 idx_type cell, row_type row, col_type column,
750 std::vector<unsigned int> const &,
751 bool onlydata) const;
752 /// auxiliary function for docbook
753 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
755 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
756 bool header = false) const;
758 /// change associated Buffer
759 void setBuffer(Buffer & buffer);
760 /// retrieve associated Buffer
761 Buffer & buffer() const { return *buffer_; }
769 class InsetTabular : public Inset
773 InsetTabular(Buffer *, row_type rows = 1,
774 col_type columns = 1);
778 void setBuffer(Buffer & buffer);
781 static void string2params(std::string const &, InsetTabular &);
783 static std::string params2string(InsetTabular const &);
787 void write(std::ostream &) const;
789 void metrics(MetricsInfo &, Dimension &) const;
791 void draw(PainterInfo & pi, int x, int y) const;
793 void drawSelection(PainterInfo & pi, int x, int y) const;
795 void drawBackground(PainterInfo & pi, int x, int y) const;
797 bool editable() const { return true; }
799 bool hasSettings() const { return true; }
801 bool insetAllowed(InsetCode code) const;
803 bool allowSpellCheck() const { return true; }
805 bool canTrackChanges() const { return true; }
806 /** returns false if, when outputing LaTeX, font changes should
807 be closed before generating this inset. This is needed for
808 insets that may contain several paragraphs */
809 bool inheritFont() const { return false; }
811 DisplayType display() const;
813 void latex(otexstream &, OutputParams const &) const;
815 int plaintext(odocstream &, OutputParams const &) const;
817 int docbook(odocstream &, OutputParams const &) const;
819 docstring xhtml(XHTMLStream &, OutputParams const &) const;
821 void validate(LaTeXFeatures & features) const;
823 InsetCode lyxCode() const { return TABULAR_CODE; }
825 docstring contextMenu(BufferView const &, int, int) const;
827 docstring contextMenuName() const;
828 /// get offset of this cursor slice relative to our upper left corner
829 void cursorPos(BufferView const & bv, CursorSlice const & sl,
830 bool boundary, int & x, int & y) const;
832 bool tabularFeatures(Cursor & cur, std::string const & what);
834 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
835 std::string const & val = std::string());
837 size_t nargs() const { return tabular.numberofcells; }
839 shared_ptr<InsetTableCell const> cell(idx_type) const;
841 shared_ptr<InsetTableCell> cell(idx_type);
843 Text * getText(int) const;
845 /// set the change for the entire inset
846 void setChange(Change const & change);
847 /// accept the changes within the inset
848 void acceptChanges();
849 /// reject the changes within the inset
850 void rejectChanges();
852 // this should return true if we have a "normal" cell, otherwise false.
853 // "normal" means without width set!
854 /// should all paragraphs be output with "Standard" layout?
855 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
857 virtual bool forcePlainLayout(idx_type cell = 0) const;
859 void addPreview(DocIterator const & inset_pos,
860 graphics::PreviewLoader &) const;
862 /// lock cell with given index
863 void edit(Cursor & cur, bool front, EntryDirection entry_from);
864 /// get table row from x coordinate
865 int rowFromY(Cursor & cur, int y) const;
866 /// get table column from y coordinate
867 int columnFromX(Cursor & cur, int x) const;
869 Inset * editXY(Cursor & cur, int x, int y);
870 /// can we go further down on mouse click?
871 bool descendable(BufferView const &) const { return true; }
872 /// Update the counters of this inset and of its contents
873 void updateBuffer(ParIterator const &, UpdateType);
875 void addToToc(DocIterator const &) const;
878 bool completionSupported(Cursor const &) const;
880 bool inlineCompletionSupported(Cursor const & cur) const;
882 bool automaticInlineCompletion() const;
884 bool automaticPopupCompletion() const;
886 bool showCompletionCursor() const;
888 CompletionList const * createCompletionList(Cursor const & cur) const;
890 docstring completionPrefix(Cursor const & cur) const;
892 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
894 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
896 virtual bool usePlainLayout() const { return true; }
899 InsetTabular * asInsetTabular() { return this; }
901 InsetTabular const * asInsetTabular() const { return this; }
903 bool isRightToLeft(Cursor & cur) const;
904 /// writes the cells between stidx and enidx as a string, optionally
905 /// descending into the insets
906 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
908 /// Returns whether the cell in the specified row and column is selected.
909 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
911 // Public structures and variables
913 mutable Tabular tabular;
917 InsetTabular(InsetTabular const &);
919 void doDispatch(Cursor & cur, FuncRequest & cmd);
921 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
923 Inset * clone() const { return new InsetTabular(*this); }
926 void drawCellLines(PainterInfo &, int x, int y, row_type row,
927 idx_type cell) const;
929 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
932 void moveNextCell(Cursor & cur,
933 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
935 void movePrevCell(Cursor & cur,
936 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
938 int cellXPos(idx_type cell) const;
940 int cellYPos(idx_type cell) const;
942 void resetPos(Cursor & cur) const;
944 bool copySelection(Cursor & cur);
946 bool pasteClipboard(Cursor & cur);
948 void cutSelection(Cursor & cur);
950 void getSelection(Cursor & cur, row_type & rs, row_type & re,
951 col_type & cs, col_type & ce) const;
953 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
955 /// return the "Manhattan distance" to nearest corner
956 int dist(BufferView &, idx_type cell, int x, int y) const;
957 /// return the cell nearest to x, y
958 idx_type getNearestCell(BufferView &, int x, int y) const;
960 /// test the rotation state of the give cell range.
961 bool oneCellHasRotationState(bool rotated,
962 row_type row_start, row_type row_end,
963 col_type col_start, col_type col_end) const;
965 mutable idx_type first_visible_cell;
968 /// The vertical offset of the table due to the vertical
969 /// alignment with respect to the baseline.
970 mutable int offset_valign_;
971 /// true when selecting rows with the mouse
973 /// true when selecting columns with the mouse
977 std::string const featureAsString(Tabular::Feature feature);
979 /// Split cell on decimal symbol
980 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
984 #endif // INSET_TABULAR_H