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"
31 #include "support/shared_ptr.h"
51 class InsetTableCell : public InsetText
55 InsetTableCell(Buffer * buf);
57 InsetCode lyxCode() const { return CELL_CODE; }
59 Inset * clone() { return new InsetTableCell(*this); }
61 bool getStatus(Cursor & cur, FuncRequest const & cmd,
62 FuncStatus & status) const;
64 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
66 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
67 /// writes the contents of the cell as a string, optionally
68 /// descending into insets
69 docstring asString(bool intoInsets = true);
71 docstring xhtml(XHTMLStream &, OutputParams const &) const;
76 void operator=(InsetTableCell const &);
78 // This boolean is supposed to track whether the cell has had its
79 // width explicitly set. We need to know this to determine whether
80 // layout changes and paragraph customization are allowed---that is,
81 // we need it in forcePlainLayout() and allowParagraphCustomization().
82 // Unfortunately, that information is not readily available in
83 // InsetTableCell. In the case of multicolumn cells, it is present
84 // in CellData, and so would be available here if CellData were to
85 // become a member of InsetTableCell. But in the other case, it isn't
86 // even available there, but is held in Tabular::ColumnData.
87 // So, the present solution uses this boolean to track the information
88 // we need to track, and tries to keep it updated. This is not ideal,
89 // but the other solutions are no better. These are:
90 // (i) Keep a pointer in InsetTableCell to the table;
91 // (ii) Find the table by iterating over the Buffer's insets.
92 // Solution (i) raises the problem of updating the pointer when an
93 // InsetTableCell is copied, and we'd therefore need a copy constructor
94 // in InsetTabular and then in Tabular, which seems messy, given how
95 // complicated those classes are. Solution (ii) involves a lot of
96 // iterating, since this information is needed quite often, and so may
98 // So, well, if someone can do better, please do!
102 // FIXME: Here the thoughts from the comment above also apply.
104 LyXAlignment contentAlign;
105 /// should paragraph indendation be omitted in any case?
106 bool neverIndent() const { return true; }
108 LyXAlignment contentAlignment() const { return contentAlign; }
110 virtual bool usePlainLayout() const { return true; }
112 virtual bool forcePlainLayout(idx_type = 0) const;
114 virtual bool allowParagraphCustomization(idx_type = 0) const;
115 /// Is the width forced to some value?
116 bool hasFixedWidth() const { return isFixedWidth; }
121 // A helper struct for tables
210 UNSET_ROTATE_TABULAR,
212 TOGGLE_ROTATE_TABULAR,
244 SET_SPECIAL_MULTICOLUMN,
260 TABULAR_VALIGN_MIDDLE,
262 TABULAR_VALIGN_BOTTOM,
264 LONGTABULAR_ALIGN_LEFT,
266 LONGTABULAR_ALIGN_CENTER,
268 LONGTABULAR_ALIGN_RIGHT,
279 CELL_BEGIN_OF_MULTICOLUMN,
281 CELL_PART_OF_MULTICOLUMN,
283 CELL_BEGIN_OF_MULTIROW,
285 CELL_PART_OF_MULTIROW
293 LYX_VALIGN_MIDDLE = 1,
295 LYX_VALIGN_BOTTOM = 2
301 LYX_LONGTABULAR_ALIGN_LEFT = 0,
303 LYX_LONGTABULAR_ALIGN_CENTER = 1,
305 LYX_LONGTABULAR_ALIGN_RIGHT = 2
321 // we have this header type (is set in the getLT... functions)
323 // double borders on top
325 // double borders on bottom
327 // used for FirstHeader & LastFooter and if this is true
328 // all the rows marked as FirstHeader or LastFooter are
329 // ignored in the output and it is set to be empty!
333 /// type for row numbers
334 typedef size_t row_type;
335 /// type for column numbers
336 typedef size_t col_type;
337 /// type for cell indices
338 typedef size_t idx_type;
339 /// index indicating an invalid position
340 static const idx_type npos = static_cast<idx_type>(-1);
343 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
345 /// Returns true if there is a topline, returns false if not
346 bool topLine(idx_type cell) const;
347 /// Returns true if there is a topline, returns false if not
348 bool bottomLine(idx_type cell) const;
349 /// Returns true if there is a topline, returns false if not
350 bool leftLine(idx_type cell) const;
351 /// Returns true if there is a topline, returns false if not
352 bool rightLine(idx_type cell) const;
354 /// return space occupied by the second horizontal line and
355 /// interline space above row \p row in pixels
356 int interRowSpace(row_type row) const;
358 int interColumnSpace(idx_type cell) const;
360 /* returns the maximum over all rows */
362 int cellWidth(idx_type cell) const;
364 int cellHeight(idx_type cell) const;
370 row_type nrows() const {return row_info.size();}
372 col_type ncols() const {return column_info.size();}
374 int rowAscent(row_type row) const;
376 int rowDescent(row_type row) const;
378 void setRowAscent(row_type row, int height);
380 void setRowDescent(row_type row, int height);
382 void setTopLine(idx_type cell, bool line);
384 void setBottomLine(idx_type cell, bool line);
386 void setLeftLine(idx_type cell, bool line);
388 void setRightLine(idx_type cell, bool line);
390 bool rowTopLine(row_type row) const;
392 bool rowBottomLine(row_type row) const;
394 bool columnLeftLine(col_type column) const;
396 bool columnRightLine(col_type column) const;
398 void setAlignment(idx_type cell, LyXAlignment align,
399 bool onlycolumn = false);
401 void setVAlignment(idx_type cell, VAlignment align,
402 bool onlycolumn = false);
404 void setColumnPWidth(Cursor &, idx_type, Length const &);
406 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
408 void setAlignSpecial(idx_type cell, docstring const & special,
411 LyXAlignment getAlignment(idx_type cell,
412 bool onlycolumn = false) const;
414 VAlignment getVAlignment(idx_type cell,
415 bool onlycolumn = false) const;
417 Length const getPWidth(idx_type cell) const;
419 int textHOffset(idx_type cell) const;
421 int textVOffset(idx_type cell) const;
423 void appendRow(idx_type cell);
425 void deleteRow(row_type row);
427 void copyRow(row_type);
429 void appendColumn(idx_type cell);
431 void deleteColumn(col_type column);
433 void copyColumn(col_type);
435 idx_type getFirstCellInRow(row_type row) const;
437 idx_type getLastCellInRow(row_type row) const;
439 idx_type numberOfCellsInRow(row_type row) const;
441 void write(std::ostream &) const;
445 int latex(odocstream &, OutputParams const &) const;
447 int docbook(odocstream & os, OutputParams const &) const;
449 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
451 void plaintext(odocstream &,
452 OutputParams const & runparams, int const depth,
453 bool onlydata, char_type delim) const;
455 bool isMultiColumn(idx_type cell) const;
457 idx_type setMultiColumn(idx_type cell, idx_type number);
459 void unsetMultiColumn(idx_type cell);
461 bool isPartOfMultiColumn(row_type row, col_type column) const;
463 bool isPartOfMultiRow(row_type row, col_type column) const;
465 bool isMultiRow(idx_type cell) const;
467 idx_type setMultiRow(idx_type cell, idx_type number);
469 void unsetMultiRow(idx_type cell);
471 row_type cellRow(idx_type cell) const;
473 col_type cellColumn(idx_type cell) const;
475 void setRotateCell(idx_type cell, bool);
477 bool getRotateCell(idx_type cell) const;
479 bool needRotating() const;
481 bool isLastCell(idx_type cell) const;
483 idx_type cellAbove(idx_type cell) const;
485 idx_type cellBelow(idx_type cell) const;
487 idx_type cellIndex(row_type row, col_type column) const;
489 void setUsebox(idx_type cell, BoxType);
491 BoxType getUsebox(idx_type cell) const;
493 // Long Tabular Options support functions
495 bool checkLTType(row_type row, ltType const &) const;
497 void setLTHead(row_type row, bool flag, ltType const &, bool first);
499 bool getRowOfLTHead(row_type row, ltType &) const;
501 bool getRowOfLTFirstHead(row_type row, ltType &) const;
503 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
505 bool getRowOfLTFoot(row_type row, ltType &) const;
507 bool getRowOfLTLastFoot(row_type row, ltType &) const;
509 void setLTNewPage(row_type row, bool what);
511 bool getLTNewPage(row_type row) const;
513 idx_type setLTCaption(row_type row, bool what);
515 bool ltCaption(row_type row) const;
517 bool haveLTHead() const;
519 bool haveLTFirstHead() const;
521 bool haveLTFoot() const;
523 bool haveLTLastFoot() const;
525 bool haveLTCaption() const;
527 // end longtable support
529 shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
531 shared_ptr<InsetTableCell> cellInset(row_type row,
532 col_type column) const;
534 void setCellInset(row_type row, col_type column,
535 shared_ptr<InsetTableCell>) const;
536 /// Search for \param inset in the tabular, with the
538 void validate(LaTeXFeatures &) const;
540 // FIXME Now that cells have an InsetTableCell as their insets, rather
541 // than an InsetText, it'd be possible to reverse the relationship here,
542 // so that cell_vector was a vector<InsetTableCell> rather than a
543 // vector<CellData>, and an InsetTableCell had a CellData as a member,
544 // or perhaps just had its members as members.
551 CellData(CellData const &);
553 CellData & operator=(CellData);
555 void swap(CellData & rhs);
565 LyXAlignment alignment;
567 VAlignment valignment;
568 /// width of the part before the decimal
570 /// width of the decimal part
587 docstring align_special;
589 Length p_width; // this is only set for multicolumn!!!
591 shared_ptr<InsetTableCell> inset;
593 CellData & cellInfo(idx_type cell) const;
595 typedef std::vector<CellData> cell_vector;
597 typedef std::vector<cell_vector> cell_vvector;
608 /// Extra space between the top line and this row
610 /// Ignore top_space if true and use the default top space
611 bool top_space_default;
612 /// Extra space between this row and the bottom line
614 /// Ignore bottom_space if true and use the default bottom space
615 bool bottom_space_default;
616 /// Extra space between the bottom line and the next top line
617 Length interline_space;
618 /// Ignore interline_space if true and use the default interline space
619 bool interline_space_default;
620 /// This are for longtabulars only
623 /// a row of endfirsthead
627 /// row of endlastfoot
629 /// row for a newpage
635 typedef std::vector<RowData> row_vector;
643 LyXAlignment alignment;
645 VAlignment valignment;
651 docstring align_special;
653 docstring decimal_point;
656 typedef std::vector<ColumnData> column_vector;
659 idx_type numberofcells;
661 std::vector<row_type> rowofcell;
663 std::vector<col_type> columnofcell;
667 column_vector column_info;
669 mutable cell_vvector cell_info;
675 VAlignment tabular_valignment;
679 HAlignment longtabular_alignment;
681 bool is_long_tabular;
684 /// endfirsthead data
692 void init(Buffer *, row_type rows_arg,
693 col_type columns_arg);
695 void updateIndexes();
697 bool setFixedWidth(row_type r, col_type c);
698 /// return true of update is needed
699 bool updateColumnWidths();
701 idx_type columnSpan(idx_type cell) const;
703 idx_type rowSpan(idx_type cell) const;
705 BoxType useParbox(idx_type cell) const;
707 // helper function for Latex returns number of newlines
709 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
711 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
713 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
715 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
717 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
719 bool isValidRow(row_type const row) const;
721 int TeXRow(odocstream &, row_type const row,
722 OutputParams const &) const;
724 // helper functions for plain text
726 bool plaintextTopHLine(odocstream &, row_type row,
727 std::vector<unsigned int> const &) const;
729 bool plaintextBottomHLine(odocstream &, row_type row,
730 std::vector<unsigned int> const &) const;
732 void plaintextPrintCell(odocstream &,
733 OutputParams const &,
734 idx_type cell, row_type row, col_type column,
735 std::vector<unsigned int> const &,
736 bool onlydata) const;
737 /// auxiliary function for docbook
738 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
740 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &) const;
742 /// change associated Buffer
743 void setBuffer(Buffer & buffer);
744 /// retrieve associated Buffer
745 Buffer & buffer() const { return *buffer_; }
753 class InsetTabular : public Inset
757 InsetTabular(Buffer *, row_type rows = 1,
758 col_type columns = 1);
762 void setBuffer(Buffer & buffer);
765 static void string2params(std::string const &, InsetTabular &);
767 static std::string params2string(InsetTabular const &);
771 void write(std::ostream &) const;
773 void metrics(MetricsInfo &, Dimension &) const;
775 void draw(PainterInfo & pi, int x, int y) const;
777 void drawSelection(PainterInfo & pi, int x, int y) const;
779 bool editable() const { return true; }
781 bool hasSettings() const { return true; }
783 bool insetAllowed(InsetCode code) const;
785 bool allowSpellCheck() const { return true; }
787 bool canTrackChanges() const { return true; }
788 /** returns true if, when outputing LaTeX, font changes should
789 be closed before generating this inset. This is needed for
790 insets that may contain several paragraphs */
791 bool noFontChange() const { return true; }
793 DisplayType display() const;
795 int latex(odocstream &, OutputParams const &) const;
797 int plaintext(odocstream &, OutputParams const &) const;
799 int docbook(odocstream &, OutputParams const &) const;
801 docstring xhtml(XHTMLStream &, OutputParams const &) const;
803 void validate(LaTeXFeatures & features) const;
805 InsetCode lyxCode() const { return TABULAR_CODE; }
807 docstring contextMenu(BufferView const & bv, int x, int y) const;
808 /// get offset of this cursor slice relative to our upper left corner
809 void cursorPos(BufferView const & bv, CursorSlice const & sl,
810 bool boundary, int & x, int & y) const;
812 bool tabularFeatures(Cursor & cur, std::string const & what);
814 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
815 std::string const & val = std::string());
817 size_t nargs() const { return tabular.numberofcells; }
819 shared_ptr<InsetTableCell const> cell(idx_type) const;
821 shared_ptr<InsetTableCell> cell(idx_type);
823 Text * getText(int) const;
825 /// set the change for the entire inset
826 void setChange(Change const & change);
827 /// accept the changes within the inset
828 void acceptChanges();
829 /// reject the changes within the inset
830 void rejectChanges();
832 // this should return true if we have a "normal" cell, otherwise false.
833 // "normal" means without width set!
834 /// should all paragraphs be output with "Standard" layout?
835 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
837 virtual bool forcePlainLayout(idx_type cell = 0) const;
839 void addPreview(DocIterator const & inset_pos,
840 graphics::PreviewLoader &) const;
842 /// lock cell with given index
843 void edit(Cursor & cur, bool front, EntryDirection entry_from);
844 /// get table row from x coordinate
845 int rowFromY(Cursor & cur, int y) const;
846 /// get table column from y coordinate
847 int columnFromX(Cursor & cur, int x) const;
849 Inset * editXY(Cursor & cur, int x, int y);
850 /// can we go further down on mouse click?
851 bool descendable(BufferView const &) const { return true; }
852 /// Update the counters of this inset and of its contents
853 void updateBuffer(ParIterator const &, UpdateType);
855 void addToToc(DocIterator const &);
858 bool completionSupported(Cursor const &) const;
860 bool inlineCompletionSupported(Cursor const & cur) const;
862 bool automaticInlineCompletion() const;
864 bool automaticPopupCompletion() const;
866 bool showCompletionCursor() const;
868 CompletionList const * createCompletionList(Cursor const & cur) const;
870 docstring completionPrefix(Cursor const & cur) const;
872 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
874 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
876 virtual bool usePlainLayout() const { return true; }
879 InsetTabular * asInsetTabular() { return this; }
881 InsetTabular const * asInsetTabular() const { return this; }
883 bool isRightToLeft(Cursor & cur) const;
884 /// writes the cells between stidx and enidx as a string, optionally
885 /// descending into the insets
886 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
888 /// Returns whether the cell in the specified row and column is selected.
889 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
891 // Public structures and variables
893 mutable Tabular tabular;
897 InsetTabular(InsetTabular const &);
899 void doDispatch(Cursor & cur, FuncRequest & cmd);
901 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
903 int scroll() const { return scx_; }
905 Inset * clone() const { return new InsetTabular(*this); }
908 void drawCellLines(PainterInfo &, int x, int y, row_type row,
909 idx_type cell) const;
911 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
914 void moveNextCell(Cursor & cur,
915 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
917 void movePrevCell(Cursor & cur,
918 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
920 int cellXPos(idx_type cell) const;
922 void resetPos(Cursor & cur) const;
924 bool copySelection(Cursor & cur);
926 bool pasteClipboard(Cursor & cur);
928 void cutSelection(Cursor & cur);
930 void getSelection(Cursor & cur, row_type & rs, row_type & re,
931 col_type & cs, col_type & ce) const;
933 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
935 /// return the "Manhattan distance" to nearest corner
936 int dist(BufferView &, idx_type cell, int x, int y) const;
937 /// return the cell nearest to x, y
938 idx_type getNearestCell(BufferView &, int x, int y) const;
940 /// test the rotation state of the give cell range.
941 bool oneCellHasRotationState(bool rotated,
942 row_type row_start, row_type row_end,
943 col_type col_start, col_type col_end) const;
945 mutable idx_type first_visible_cell;
948 /// true when selecting rows with the mouse
950 /// true when selecting columns with the mouse
954 std::string const featureAsString(Tabular::Feature feature);
956 /// Split cell on decimal symbol
957 InsetTableCell splitCell(InsetTableCell & head, docstring const decimal_sym, bool & hassep);
961 #endif // INSET_TABULAR_H