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
337 // we have this header type (is set in the getLT... functions)
339 // double borders on top
341 // double borders on bottom
343 // used for FirstHeader & LastFooter and if this is true
344 // all the rows marked as FirstHeader or LastFooter are
345 // ignored in the output and it is set to be empty!
349 /// type for row numbers
350 typedef size_t row_type;
351 /// type for column numbers
352 typedef size_t col_type;
353 /// type for cell indices
354 typedef size_t idx_type;
355 /// index indicating an invalid position
356 static const idx_type npos = static_cast<idx_type>(-1);
359 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
361 /// Returns true if there is a topline, returns false if not
362 bool topLine(idx_type cell) const;
363 /// Returns true if there is a topline, returns false if not
364 bool bottomLine(idx_type cell) const;
365 /// Returns true if there is a topline, returns false if not
366 bool leftLine(idx_type cell) const;
367 /// Returns true if there is a topline, returns false if not
368 bool rightLine(idx_type cell) const;
370 /// return space occupied by the second horizontal line and
371 /// interline space above row \p row in pixels
372 int interRowSpace(row_type row) const;
374 int interColumnSpace(idx_type cell) const;
376 /* returns the maximum over all rows */
378 int cellWidth(idx_type cell) const;
380 int cellHeight(idx_type cell) const;
386 row_type nrows() const {return row_info.size();}
388 col_type ncols() const {return column_info.size();}
390 int rowAscent(row_type row) const;
392 int rowDescent(row_type row) const;
394 void setRowAscent(row_type row, int height);
396 void setRowDescent(row_type row, int height);
398 void setTopLine(idx_type cell, bool line);
400 void setBottomLine(idx_type cell, bool line);
402 void setLeftLine(idx_type cell, bool line);
404 void setRightLine(idx_type cell, bool line);
406 bool rowTopLine(row_type row) const;
408 bool rowBottomLine(row_type row) const;
410 bool columnLeftLine(col_type column) const;
412 bool columnRightLine(col_type column) const;
414 void setAlignment(idx_type cell, LyXAlignment align,
415 bool onlycolumn = false);
417 void setVAlignment(idx_type cell, VAlignment align,
418 bool onlycolumn = false);
420 void setTabularWidth(Length const & l) { tabular_width = l; }
422 Length tabularWidth() const { return tabular_width; }
424 void setColumnPWidth(Cursor &, idx_type, Length const &);
426 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
428 bool setMROffset(Cursor &, idx_type, Length const &);
430 void setAlignSpecial(idx_type cell, docstring const & special,
433 LyXAlignment getAlignment(idx_type cell,
434 bool onlycolumn = false) const;
436 VAlignment getVAlignment(idx_type cell,
437 bool onlycolumn = false) const;
439 Length const getPWidth(idx_type cell) const;
441 Length const getMROffset(idx_type cell) const;
443 int textHOffset(idx_type cell) const;
445 int textVOffset(idx_type cell) const;
447 void appendRow(row_type row);
449 void deleteRow(row_type row);
451 void copyRow(row_type row);
453 void insertRow(row_type row, bool copy);
455 void appendColumn(col_type column);
457 void deleteColumn(col_type column);
459 void copyColumn(col_type column);
461 void insertColumn(col_type column, bool copy);
463 idx_type getFirstCellInRow(row_type row) const;
465 idx_type getLastCellInRow(row_type row) const;
467 idx_type numberOfCellsInRow(row_type row) const;
469 void write(std::ostream &) const;
473 void latex(otexstream &, OutputParams const &) const;
475 int docbook(odocstream & os, OutputParams const &) const;
477 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
479 void plaintext(odocstream &,
480 OutputParams const & runparams, int const depth,
481 bool onlydata, char_type delim) const;
483 bool isMultiColumn(idx_type cell) const;
485 idx_type setMultiColumn(idx_type cell, idx_type number,
486 bool const right_border);
488 void unsetMultiColumn(idx_type cell);
490 bool isPartOfMultiColumn(row_type row, col_type column) const;
492 bool isPartOfMultiRow(row_type row, col_type column) const;
494 bool isMultiRow(idx_type cell) const;
496 idx_type setMultiRow(idx_type cell, idx_type number,
497 bool const bottom_border);
499 void unsetMultiRow(idx_type cell);
501 row_type cellRow(idx_type cell) const;
503 col_type cellColumn(idx_type cell) const;
505 void setRotateCell(idx_type cell, int);
507 int getRotateCell(idx_type cell) const;
509 bool needRotating() const;
511 bool isLastCell(idx_type cell) const;
513 idx_type cellAbove(idx_type cell) const;
515 idx_type cellBelow(idx_type cell) const;
517 idx_type cellIndex(row_type row, col_type column) const;
519 void setUsebox(idx_type cell, BoxType);
521 BoxType getUsebox(idx_type cell) const;
523 // Long Tabular Options support functions
525 void setLTHead(row_type row, bool flag, ltType const &, bool first);
527 bool getRowOfLTHead(row_type row, ltType &) const;
529 bool getRowOfLTFirstHead(row_type row, ltType &) const;
531 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
533 bool getRowOfLTFoot(row_type row, ltType &) const;
535 bool getRowOfLTLastFoot(row_type row, ltType &) const;
537 void setLTNewPage(row_type row, bool what);
539 bool getLTNewPage(row_type row) const;
541 idx_type setLTCaption(row_type row, bool what);
543 bool ltCaption(row_type row) const;
545 bool haveLTHead(bool withcaptions = true) const;
547 bool haveLTFirstHead(bool withcaptions = true) const;
549 bool haveLTFoot(bool withcaptions = true) const;
551 bool haveLTLastFoot(bool withcaptions = true) const;
553 bool haveLTCaption(CaptionType captiontype = CAPTION_ANY) const;
555 // end longtable support
557 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
559 shared_ptr<InsetTableCell> cellInset(row_type row,
560 col_type column) const;
562 void setCellInset(row_type row, col_type column,
563 shared_ptr<InsetTableCell>) const;
564 /// Search for \param inset in the tabular, with the
566 void validate(LaTeXFeatures &) const;
568 // FIXME Now that cells have an InsetTableCell as their insets, rather
569 // than an InsetText, it'd be possible to reverse the relationship here,
570 // so that cell_vector was a vector<InsetTableCell> rather than a
571 // vector<CellData>, and an InsetTableCell had a CellData as a member,
572 // or perhaps just had its members as members.
579 CellData(CellData const &);
581 CellData & operator=(CellData);
583 void swap(CellData & rhs);
595 LyXAlignment alignment;
597 VAlignment valignment;
598 /// width of the part before the decimal
600 /// width of the decimal part
617 docstring align_special;
619 Length p_width; // this is only set for multicolumn!!!
621 shared_ptr<InsetTableCell> inset;
623 CellData & cellInfo(idx_type cell) const;
625 typedef std::vector<CellData> cell_vector;
627 typedef std::vector<cell_vector> cell_vvector;
638 /// Extra space between the top line and this row
640 /// Ignore top_space if true and use the default top space
641 bool top_space_default;
642 /// Extra space between this row and the bottom line
644 /// Ignore bottom_space if true and use the default bottom space
645 bool bottom_space_default;
646 /// Extra space between the bottom line and the next top line
647 Length interline_space;
648 /// Ignore interline_space if true and use the default interline space
649 bool interline_space_default;
650 /// This are for longtabulars only
653 /// a row of endfirsthead
657 /// row of endlastfoot
659 /// row for a newpage
665 typedef std::vector<RowData> row_vector;
673 LyXAlignment alignment;
675 VAlignment valignment;
681 docstring align_special;
683 docstring decimal_point;
686 typedef std::vector<ColumnData> column_vector;
689 idx_type numberofcells;
691 std::vector<row_type> rowofcell;
693 std::vector<col_type> columnofcell;
697 column_vector column_info;
699 mutable cell_vvector cell_info;
701 Length tabular_width;
707 VAlignment tabular_valignment;
711 HAlignment longtabular_alignment;
713 bool is_long_tabular;
716 /// endfirsthead data
724 void init(Buffer *, row_type rows_arg,
725 col_type columns_arg);
727 void updateIndexes();
729 bool setFixedWidth(row_type r, col_type c);
730 /// return true of update is needed
731 bool updateColumnWidths();
733 idx_type columnSpan(idx_type cell) const;
735 idx_type rowSpan(idx_type cell) const;
737 BoxType useParbox(idx_type cell) const;
739 // helper function for Latex
741 void TeXTopHLine(otexstream &, row_type row, std::string const lang) const;
743 void TeXBottomHLine(otexstream &, row_type row, std::string const lang) const;
745 void TeXCellPreamble(otexstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
747 void TeXCellPostamble(otexstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
749 void TeXLongtableHeaderFooter(otexstream &, OutputParams const &) const;
751 bool isValidRow(row_type const row) const;
753 void TeXRow(otexstream &, row_type const row,
754 OutputParams const &) const;
756 // helper functions for plain text
758 bool plaintextTopHLine(odocstream &, row_type row,
759 std::vector<unsigned int> const &) const;
761 bool plaintextBottomHLine(odocstream &, row_type row,
762 std::vector<unsigned int> const &) const;
764 void plaintextPrintCell(odocstream &,
765 OutputParams const &,
766 idx_type cell, row_type row, col_type column,
767 std::vector<unsigned int> const &,
768 bool onlydata) const;
769 /// auxiliary function for docbook
770 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
772 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &,
773 bool header = false) const;
775 /// change associated Buffer
776 void setBuffer(Buffer & buffer);
777 /// retrieve associated Buffer
778 Buffer & buffer() const { return *buffer_; }
786 class InsetTabular : public Inset
790 InsetTabular(Buffer *, row_type rows = 1,
791 col_type columns = 1);
795 void setBuffer(Buffer & buffer);
798 static void string2params(std::string const &, InsetTabular &);
800 static std::string params2string(InsetTabular const &);
804 void write(std::ostream &) const;
806 void metrics(MetricsInfo &, Dimension &) const;
808 void draw(PainterInfo & pi, int x, int y) const;
810 void drawSelection(PainterInfo & pi, int x, int y) const;
812 void drawBackground(PainterInfo & pi, int x, int y) const;
814 bool editable() const { return true; }
816 bool hasSettings() const { return true; }
818 bool insetAllowed(InsetCode code) const;
820 bool allowSpellCheck() const { return true; }
822 bool canTrackChanges() const { return true; }
823 /** returns false if, when outputing LaTeX, font changes should
824 be closed before generating this inset. This is needed for
825 insets that may contain several paragraphs */
826 bool inheritFont() const { return false; }
828 DisplayType display() const;
830 void latex(otexstream &, OutputParams const &) const;
832 int plaintext(odocstream &, OutputParams const &) const;
834 int docbook(odocstream &, OutputParams const &) const;
836 docstring xhtml(XHTMLStream &, OutputParams const &) const;
838 void validate(LaTeXFeatures & features) const;
840 InsetCode lyxCode() const { return TABULAR_CODE; }
842 std::string contextMenu(BufferView const &, int, int) const;
844 std::string contextMenuName() const;
845 /// get offset of this cursor slice relative to our upper left corner
846 void cursorPos(BufferView const & bv, CursorSlice const & sl,
847 bool boundary, int & x, int & y) const;
849 bool tabularFeatures(Cursor & cur, std::string const & what);
851 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
852 std::string const & val = std::string());
854 size_t nargs() const { return tabular.numberofcells; }
856 shared_ptr<InsetTableCell const> cell(idx_type) const;
858 shared_ptr<InsetTableCell> cell(idx_type);
860 Text * getText(int) const;
862 /// set the change for the entire inset
863 void setChange(Change const & change);
864 /// accept the changes within the inset
865 void acceptChanges();
866 /// reject the changes within the inset
867 void rejectChanges();
869 // this should return true if we have a "normal" cell, otherwise false.
870 // "normal" means without width set!
871 /// should all paragraphs be output with "Standard" layout?
872 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
874 virtual bool forcePlainLayout(idx_type cell = 0) const;
876 void addPreview(DocIterator const & inset_pos,
877 graphics::PreviewLoader &) const;
879 /// lock cell with given index
880 void edit(Cursor & cur, bool front, EntryDirection entry_from);
881 /// get table row from x coordinate
882 int rowFromY(Cursor & cur, int y) const;
883 /// get table column from y coordinate
884 int columnFromX(Cursor & cur, int x) const;
886 Inset * editXY(Cursor & cur, int x, int y);
887 /// can we go further down on mouse click?
888 bool descendable(BufferView const &) const { return true; }
889 /// Update the counters of this inset and of its contents
890 void updateBuffer(ParIterator const &, UpdateType);
892 void addToToc(DocIterator const &) const;
895 bool completionSupported(Cursor const &) const;
897 bool inlineCompletionSupported(Cursor const & cur) const;
899 bool automaticInlineCompletion() const;
901 bool automaticPopupCompletion() const;
903 bool showCompletionCursor() const;
905 CompletionList const * createCompletionList(Cursor const & cur) const;
907 docstring completionPrefix(Cursor const & cur) const;
909 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
911 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
913 virtual bool usePlainLayout() const { return true; }
916 InsetTabular * asInsetTabular() { return this; }
918 InsetTabular const * asInsetTabular() const { return this; }
920 bool isRightToLeft(Cursor & cur) const;
921 /// writes the cells between stidx and enidx as a string, optionally
922 /// descending into the insets
923 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
925 /// Returns whether the cell in the specified row and column is selected.
926 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
928 // Public structures and variables
930 mutable Tabular tabular;
934 InsetTabular(InsetTabular const &);
936 void doDispatch(Cursor & cur, FuncRequest & cmd);
938 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
940 Inset * clone() const { return new InsetTabular(*this); }
943 void drawCellLines(PainterInfo &, int x, int y, row_type row,
944 idx_type cell) const;
946 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
949 void moveNextCell(Cursor & cur,
950 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
952 void movePrevCell(Cursor & cur,
953 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
955 int cellXPos(idx_type cell) const;
957 int cellYPos(idx_type cell) const;
959 void resetPos(Cursor & cur) const;
961 bool copySelection(Cursor & cur);
963 bool pasteClipboard(Cursor & cur);
965 void cutSelection(Cursor & cur);
967 void getSelection(Cursor & cur, row_type & rs, row_type & re,
968 col_type & cs, col_type & ce) const;
970 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
972 /// return the "Manhattan distance" to nearest corner
973 int dist(BufferView &, idx_type cell, int x, int y) const;
974 /// return the cell nearest to x, y
975 idx_type getNearestCell(BufferView &, int x, int y) const;
977 /// test the rotation state of the give cell range.
978 bool oneCellHasRotationState(bool rotated,
979 row_type row_start, row_type row_end,
980 col_type col_start, col_type col_end) const;
982 mutable idx_type first_visible_cell;
985 /// The vertical offset of the table due to the vertical
986 /// alignment with respect to the baseline.
987 mutable int offset_valign_;
988 /// true when selecting rows with the mouse
990 /// true when selecting columns with the mouse
994 std::string const featureAsString(Tabular::Feature feature);
996 /// Split cell on decimal symbol
997 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
1001 #endif // INSET_TABULAR_H