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 <boost/shared_ptr.hpp>
48 namespace frontend { class Painter; }
56 // A helper struct for tables
127 UNSET_ROTATE_TABULAR,
129 TOGGLE_ROTATE_TABULAR,
157 SET_SPECIAL_MULTICOLUMN,
159 SET_SPECIAL_MULTIROW,
175 TABULAR_VALIGN_MIDDLE,
177 TABULAR_VALIGN_BOTTOM,
179 LONGTABULAR_ALIGN_LEFT,
181 LONGTABULAR_ALIGN_CENTER,
183 LONGTABULAR_ALIGN_RIGHT,
192 CELL_BEGIN_OF_MULTICOLUMN,
194 CELL_PART_OF_MULTICOLUMN,
196 CELL_BEGIN_OF_MULTIROW,
198 CELL_PART_OF_MULTIROW
206 LYX_VALIGN_MIDDLE = 1,
208 LYX_VALIGN_BOTTOM = 2
214 LYX_LONGTABULAR_ALIGN_LEFT = 0,
216 LYX_LONGTABULAR_ALIGN_CENTER = 1,
218 LYX_LONGTABULAR_ALIGN_RIGHT = 2
234 // we have this header type (is set in the getLT... functions)
236 // double borders on top
238 // double borders on bottom
240 // used for FirstHeader & LastFooter and if this is true
241 // all the rows marked as FirstHeader or LastFooter are
242 // ignored in the output and it is set to be empty!
246 /// type for row numbers
247 typedef size_t row_type;
248 /// type for column numbers
249 typedef size_t col_type;
250 /// type for cell indices
251 typedef size_t idx_type;
252 /// index indicating an invalid position
253 static const idx_type npos = static_cast<idx_type>(-1);
256 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
258 /// Returns true if there is a topline, returns false if not
259 bool topLine(idx_type cell) const;
260 /// Returns true if there is a topline, returns false if not
261 bool bottomLine(idx_type cell) const;
262 /// Returns true if there is a topline, returns false if not
263 bool leftLine(idx_type cell) const;
264 /// Returns true if there is a topline, returns false if not
265 bool rightLine(idx_type cell) const;
268 bool topAlreadyDrawn(idx_type cell) const;
270 bool leftAlreadyDrawn(idx_type cell) const;
272 bool isLastRow(idx_type cell) const;
274 /// return space occupied by the second horizontal line and
275 /// interline space above row \p row in pixels
276 int interRowSpace(row_type row) const;
278 int interColumnSpace(idx_type cell) const;
280 /* returns the maximum over all rows */
282 int columnWidth(idx_type cell) const;
284 int rowHeight(idx_type cell) const;
290 int rowAscent(row_type row) const;
292 int rowDescent(row_type row) const;
294 void setRowAscent(row_type row, int height);
296 void setRowDescent(row_type row, int height);
298 void setCellWidth(idx_type cell, int new_width);
300 void setAllLines(idx_type cell, bool line);
302 void setTopLine(idx_type cell, bool line);
304 void setBottomLine(idx_type cell, bool line);
306 void setLeftLine(idx_type cell, bool line);
308 void setRightLine(idx_type cell, bool line);
310 bool rowTopLine(row_type row) const;
312 bool rowBottomLine(row_type row) const;
314 bool columnLeftLine(col_type column) const;
316 bool columnRightLine(col_type column) const;
318 void setAlignment(idx_type cell, LyXAlignment align,
319 bool onlycolumn = false);
321 void setVAlignment(idx_type cell, VAlignment align,
322 bool onlycolumn = false);
324 void setColumnPWidth(Cursor &, idx_type, Length const &);
326 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
328 void setAlignSpecial(idx_type cell, docstring const & special,
331 LyXAlignment getAlignment(idx_type cell,
332 bool onlycolumn = false) const;
334 VAlignment getVAlignment(idx_type cell,
335 bool onlycolumn = false) const;
337 Length const getPWidth(idx_type cell) const;
339 int cellWidth(idx_type cell) const;
341 int textHOffset(idx_type cell) const;
343 int textVOffset(idx_type cell) const;
345 void appendRow(idx_type cell);
347 void deleteRow(row_type row);
349 void copyRow(row_type);
351 void appendColumn(idx_type cell);
353 void deleteColumn(col_type column);
355 void copyColumn(col_type);
357 idx_type getFirstCellInRow(row_type row) const;
359 idx_type getLastCellInRow(row_type row) const;
361 idx_type numberOfCellsInRow(row_type row) const;
363 void write(std::ostream &) const;
367 int latex(odocstream &, OutputParams const &) const;
369 int docbook(odocstream & os, OutputParams const &) const;
371 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
373 void plaintext(odocstream &,
374 OutputParams const & runparams, int const depth,
375 bool onlydata, char_type delim) const;
377 bool isMultiColumn(idx_type cell) const;
379 bool isMultiColumnReal(idx_type cell) const;
381 void setMultiColumn(idx_type cell, idx_type number);
383 void unsetMultiColumn(idx_type cell);
385 bool isPartOfMultiColumn(row_type row, col_type column) const;
387 bool isPartOfMultiRow(row_type row, col_type column) const;
389 bool isMultiRow(idx_type cell) const;
391 void setMultiRow(idx_type cell, idx_type number);
393 void unsetMultiRow(idx_type cell);
395 row_type cellRow(idx_type cell) const;
397 col_type cellColumn(idx_type cell) const;
399 col_type cellRightColumn(idx_type cell) const;
401 void setRotateCell(idx_type cell, bool);
403 bool getRotateCell(idx_type cell) const;
405 bool needRotating() const;
407 bool isLastCell(idx_type cell) const;
409 idx_type cellAbove(idx_type cell) const;
411 idx_type cellBelow(idx_type cell) const;
413 idx_type cellIndex(row_type row, col_type column) const;
415 void setUsebox(idx_type cell, BoxType);
417 BoxType getUsebox(idx_type cell) const;
419 // Long Tabular Options support functions
421 bool checkLTType(row_type row, ltType const &) const;
423 void setLTHead(row_type row, bool flag, ltType const &, bool first);
425 bool getRowOfLTHead(row_type row, ltType &) const;
427 bool getRowOfLTFirstHead(row_type row, ltType &) const;
429 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
431 bool getRowOfLTFoot(row_type row, ltType &) const;
433 bool getRowOfLTLastFoot(row_type row, ltType &) const;
435 void setLTNewPage(row_type row, bool what);
437 bool getLTNewPage(row_type row) const;
439 idx_type setLTCaption(row_type row, bool what);
441 bool ltCaption(row_type row) const;
443 bool haveLTHead() const;
445 bool haveLTFirstHead() const;
447 bool haveLTFoot() const;
449 bool haveLTLastFoot() const;
451 bool haveLTCaption() const;
453 // end longtable support
455 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
457 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
458 col_type column) const;
460 void setCellInset(row_type row, col_type column,
461 boost::shared_ptr<InsetTableCell>) const;
462 /// Search for \param inset in the tabular, with the
464 void validate(LaTeXFeatures &) const;
466 // FIXME Now that cells have an InsetTableCell as their insets, rather
467 // than an InsetText, it'd be possible to reverse the relationship here,
468 // so that cell_vector was a vector<InsetTableCell> rather than a
469 // vector<CellData>, and an InsetTableCell had a CellData as a member,
470 // or perhaps just had its members as members.
477 CellData(CellData const &);
479 CellData & operator=(CellData);
481 void swap(CellData & rhs);
491 LyXAlignment alignment;
493 VAlignment valignment;
507 docstring align_special;
509 Length p_width; // this is only set for multicolumn!!!
511 boost::shared_ptr<InsetTableCell> inset;
513 CellData & cellInfo(idx_type cell) const;
515 typedef std::vector<CellData> cell_vector;
517 typedef std::vector<cell_vector> cell_vvector;
528 /// Extra space between the top line and this row
530 /// Ignore top_space if true and use the default top space
531 bool top_space_default;
532 /// Extra space between this row and the bottom line
534 /// Ignore bottom_space if true and use the default bottom space
535 bool bottom_space_default;
536 /// Extra space between the bottom line and the next top line
537 Length interline_space;
538 /// Ignore interline_space if true and use the default interline space
539 bool interline_space_default;
540 /// This are for longtabulars only
543 /// a row of endfirsthead
547 /// row of endlastfoot
549 /// row for a newpage
555 typedef std::vector<RowData> row_vector;
563 LyXAlignment alignment;
565 VAlignment valignment;
571 docstring align_special;
574 typedef std::vector<ColumnData> column_vector;
577 idx_type numberofcells;
579 std::vector<row_type> rowofcell;
581 std::vector<col_type> columnofcell;
585 column_vector column_info;
587 mutable cell_vvector cell_info;
593 VAlignment tabular_valignment;
597 HAlignment longtabular_alignment;
599 bool is_long_tabular;
602 /// endfirsthead data
610 void init(Buffer *, row_type rows_arg,
611 col_type columns_arg);
613 void updateIndexes();
615 bool setFixedWidth(row_type r, col_type c);
617 void updateContentAlignment(row_type r, col_type c);
618 /// return true of update is needed
619 bool updateColumnWidths();
621 idx_type columnSpan(idx_type cell) const;
623 idx_type rowSpan(idx_type cell) const;
625 BoxType useParbox(idx_type cell) const;
627 // helper function for Latex returns number of newlines
629 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
631 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
633 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
635 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
637 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
639 bool isValidRow(row_type const row) const;
641 int TeXRow(odocstream &, row_type const row,
642 OutputParams const &) const;
644 // helper functions for plain text
646 bool plaintextTopHLine(odocstream &, row_type row,
647 std::vector<unsigned int> const &) const;
649 bool plaintextBottomHLine(odocstream &, row_type row,
650 std::vector<unsigned int> const &) const;
652 void plaintextPrintCell(odocstream &,
653 OutputParams const &,
654 idx_type cell, row_type row, col_type column,
655 std::vector<unsigned int> const &,
656 bool onlydata) const;
657 /// auxiliary function for docbook
658 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
660 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &) const;
662 /// change associated Buffer
663 void setBuffer(Buffer & buffer);
664 /// retrieve associated Buffer
665 Buffer & buffer() const { return *buffer_; }
674 class InsetTableCell : public InsetText
678 InsetTableCell(Buffer * buf);
680 InsetCode lyxCode() const { return CELL_CODE; }
682 Inset * clone() { return new InsetTableCell(*this); }
684 bool getStatus(Cursor & cur, FuncRequest const & cmd,
685 FuncStatus & status) const;
687 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
689 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
690 /// writes the contents of the cell as a string, optionally
691 /// descending into insets
692 docstring asString(bool intoInsets = true);
694 docstring xhtml(XHTMLStream &, OutputParams const &) const;
699 void operator=(InsetTableCell const &);
701 // This boolean is supposed to track whether the cell has had its
702 // width explicitly set. We need to know this to determine whether
703 // layout changes and paragraph customization are allowed---that is,
704 // we need it in forcePlainLayout() and allowParagraphCustomization().
705 // Unfortunately, that information is not readily available in
706 // InsetTableCell. In the case of multicolumn cells, it is present
707 // in CellData, and so would be available here if CellData were to
708 // become a member of InsetTableCell. But in the other case, it isn't
709 // even available there, but is held in Tabular::ColumnData.
710 // So, the present solution uses this boolean to track the information
711 // we need to track, and tries to keep it updated. This is not ideal,
712 // but the other solutions are no better. These are:
713 // (i) Keep a pointer in InsetTableCell to the table;
714 // (ii) Find the table by iterating over the Buffer's insets.
715 // Solution (i) raises the problem of updating the pointer when an
716 // InsetTableCell is copied, and we'd therefore need a copy constructor
717 // in InsetTabular and then in Tabular, which seems messy, given how
718 // complicated those classes are. Solution (ii) involves a lot of
719 // iterating, since this information is needed quite often, and so may
721 // So, well, if someone can do better, please do!
725 // FIXME: Here the thoughts from the comment above also apply.
727 LyXAlignment contentAlign;
728 /// should paragraph indendation be omitted in any case?
729 bool neverIndent() const { return true; }
731 LyXAlignment contentAlignment() const { return contentAlign; }
733 virtual bool usePlainLayout() const { return true; }
735 virtual bool forcePlainLayout(idx_type = 0) const;
737 virtual bool allowParagraphCustomization(idx_type = 0) const;
738 /// Is the width forced to some value?
739 bool hasFixedWidth() const { return isFixedWidth; }
743 class InsetTabular : public Inset
747 InsetTabular(Buffer *, row_type rows = 1,
748 col_type columns = 1);
752 void setBuffer(Buffer & buffer);
755 static void string2params(std::string const &, InsetTabular &);
757 static std::string params2string(InsetTabular const &);
761 void write(std::ostream &) const;
763 void metrics(MetricsInfo &, Dimension &) const;
765 void draw(PainterInfo & pi, int x, int y) const;
767 void drawSelection(PainterInfo & pi, int x, int y) const;
769 bool editable() const { return true; }
771 bool hasSettings() const { return true; }
773 bool insetAllowed(InsetCode code) const;
775 bool allowSpellCheck() const { return true; }
777 bool canTrackChanges() const { return true; }
778 /** returns true if, when outputing LaTeX, font changes should
779 be closed before generating this inset. This is needed for
780 insets that may contain several paragraphs */
781 bool noFontChange() const { return true; }
783 DisplayType display() const;
785 int latex(odocstream &, OutputParams const &) const;
787 int plaintext(odocstream &, OutputParams const &) const;
789 int docbook(odocstream &, OutputParams const &) const;
791 docstring xhtml(XHTMLStream &, OutputParams const &) const;
793 void validate(LaTeXFeatures & features) const;
795 InsetCode lyxCode() const { return TABULAR_CODE; }
797 docstring contextMenu(BufferView const & bv, int x, int y) const;
798 /// get offset of this cursor slice relative to our upper left corner
799 void cursorPos(BufferView const & bv, CursorSlice const & sl,
800 bool boundary, int & x, int & y) const;
802 bool tabularFeatures(Cursor & cur, std::string const & what);
804 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
805 std::string const & val = std::string());
807 size_t nargs() const { return tabular.numberofcells; }
809 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
811 boost::shared_ptr<InsetTableCell> cell(idx_type);
813 Text * getText(int) const;
815 /// set the change for the entire inset
816 void setChange(Change const & change);
817 /// accept the changes within the inset
818 void acceptChanges();
819 /// reject the changes within the inset
820 void rejectChanges();
822 // this should return true if we have a "normal" cell, otherwise false.
823 // "normal" means without width set!
824 /// should all paragraphs be output with "Standard" layout?
825 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
827 virtual bool forcePlainLayout(idx_type cell = 0) const;
829 void addPreview(DocIterator const & inset_pos,
830 graphics::PreviewLoader &) const;
832 /// lock cell with given index
833 void edit(Cursor & cur, bool front, EntryDirection entry_from);
834 /// get table row from x coordinate
835 int rowFromY(Cursor & cur, int y) const;
836 /// get table column from y coordinate
837 int columnFromX(Cursor & cur, int x) const;
839 Inset * editXY(Cursor & cur, int x, int y);
840 /// can we go further down on mouse click?
841 bool descendable(BufferView const &) const { return true; }
842 /// Update the counters of this inset and of its contents
843 void updateLabels(ParIterator const &, UpdateType);
845 void addToToc(DocIterator const &);
848 bool completionSupported(Cursor const &) const;
850 bool inlineCompletionSupported(Cursor const & cur) const;
852 bool automaticInlineCompletion() const;
854 bool automaticPopupCompletion() const;
856 bool showCompletionCursor() const;
858 CompletionList const * createCompletionList(Cursor const & cur) const;
860 docstring completionPrefix(Cursor const & cur) const;
862 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
864 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
866 virtual bool usePlainLayout() const { return true; }
869 virtual InsetTabular * asInsetTabular() { return this; }
871 virtual InsetTabular const * asInsetTabular() const { return this; }
873 bool isRightToLeft(Cursor & cur) const;
874 /// writes the cells between stidx and enidx as a string, optionally
875 /// descending into the insets
876 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
878 /// Returns whether the cell in the specified row and column is selected.
879 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
881 // Public structures and variables
883 mutable Tabular tabular;
887 InsetTabular(InsetTabular const &);
889 void doDispatch(Cursor & cur, FuncRequest & cmd);
891 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
893 int scroll() const { return scx_; }
895 Inset * clone() const { return new InsetTabular(*this); }
898 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
899 idx_type cell, Change const & change) const;
901 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
904 void moveNextCell(Cursor & cur,
905 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
907 void movePrevCell(Cursor & cur,
908 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
910 int cellXPos(idx_type cell) const;
912 void resetPos(Cursor & cur) const;
914 void removeTabularRow();
916 bool copySelection(Cursor & cur);
918 bool pasteClipboard(Cursor & cur);
920 void cutSelection(Cursor & cur);
922 void getSelection(Cursor & cur, row_type & rs, row_type & re,
923 col_type & cs, col_type & ce) const;
925 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
927 /// return the "Manhattan distance" to nearest corner
928 int dist(BufferView &, idx_type cell, int x, int y) const;
929 /// return the cell nearest to x, y
930 idx_type getNearestCell(BufferView &, int x, int y) const;
932 /// test the rotation state of the give cell range.
933 bool oneCellHasRotationState(bool rotated,
934 row_type row_start, row_type row_end,
935 col_type col_start, col_type col_end) const;
937 mutable idx_type first_visible_cell;
940 /// true when selecting rows with the mouse
942 /// true when selecting columns with the mouse
946 std::string const featureAsString(Tabular::Feature feature);
950 #endif // INSET_TABULAR_H