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
12 * Full author contact details are available in file CREDITS.
16 // This is Juergen's rewrite of the tabular (table) support.
18 // Things to think of when designing the new tabular support:
19 // - color support (colortbl, color)
20 // - decimal alignment (dcloumn)
21 // - custom lines (hhline)
27 // This is what I have written about tabular support in the LyX3-Tasks file:
29 // o rewrite of table code. Should probably be written as some
30 // kind of an inset. [Done]
31 // o enhance longtable support
35 #ifndef INSET_TABULAR_H
36 #define INSET_TABULAR_H
39 #include "InsetText.h"
43 #include <boost/shared_ptr.hpp>
60 namespace frontend { class Painter; }
68 // A helper struct for tables
137 UNSET_ROTATE_TABULAR,
139 TOGGLE_ROTATE_TABULAR,
183 TABULAR_VALIGN_MIDDLE,
185 TABULAR_VALIGN_BOTTOM,
187 LONGTABULAR_ALIGN_LEFT,
189 LONGTABULAR_ALIGN_CENTER,
191 LONGTABULAR_ALIGN_RIGHT,
200 CELL_BEGIN_OF_MULTICOLUMN,
202 CELL_PART_OF_MULTICOLUMN
210 LYX_VALIGN_MIDDLE = 1,
212 LYX_VALIGN_BOTTOM = 2
218 LYX_LONGTABULAR_ALIGN_LEFT = 0,
220 LYX_LONGTABULAR_ALIGN_CENTER = 1,
222 LYX_LONGTABULAR_ALIGN_RIGHT = 2
238 // we have this header type (is set in the getLT... functions)
240 // double borders on top
242 // double borders on bottom
244 // used for FirstHeader & LastFooter and if this is true
245 // all the rows marked as FirstHeader or LastFooter are
246 // ignored in the output and it is set to be empty!
250 /// type for row numbers
251 typedef size_t row_type;
252 /// type for column numbers
253 typedef size_t col_type;
254 /// type for cell indices
255 typedef size_t idx_type;
256 /// index indicating an invalid position
257 static const idx_type npos = static_cast<idx_type>(-1);
260 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
262 /// Returns true if there is a topline, returns false if not
263 bool topLine(idx_type cell) const;
264 /// Returns true if there is a topline, returns false if not
265 bool bottomLine(idx_type cell) const;
266 /// Returns true if there is a topline, returns false if not
267 bool leftLine(idx_type cell) const;
268 /// Returns true if there is a topline, returns false if not
269 bool rightLine(idx_type cell) const;
272 bool topAlreadyDrawn(idx_type cell) const;
274 bool leftAlreadyDrawn(idx_type cell) const;
276 bool isLastRow(idx_type cell) const;
278 /// return space occupied by the second horizontal line and
279 /// interline space above row \p row in pixels
280 int getAdditionalHeight(row_type row) const;
282 int getAdditionalWidth(idx_type cell) const;
284 /* returns the maximum over all rows */
286 int columnWidth(idx_type cell) const;
292 int rowAscent(row_type row) const;
294 int rowDescent(row_type row) const;
296 void setRowAscent(row_type row, int height);
298 void setRowDescent(row_type row, int height);
300 void setCellWidth(idx_type cell, int new_width);
302 void setAllLines(idx_type cell, bool line);
304 void setTopLine(idx_type cell, bool line);
306 void setBottomLine(idx_type cell, bool line);
308 void setLeftLine(idx_type cell, bool line);
310 void setRightLine(idx_type cell, bool line);
312 bool rowTopLine(row_type row) const;
314 bool rowBottomLine(row_type row) const;
316 bool columnLeftLine(col_type column) const;
318 bool columnRightLine(col_type column) const;
320 void setAlignment(idx_type cell, LyXAlignment align,
321 bool onlycolumn = false);
323 void setVAlignment(idx_type cell, VAlignment align,
324 bool onlycolumn = false);
326 void setColumnPWidth(Cursor &, idx_type, Length const &);
328 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
330 void setAlignSpecial(idx_type cell, docstring const & special,
333 LyXAlignment getAlignment(idx_type cell,
334 bool onlycolumn = false) const;
336 VAlignment getVAlignment(idx_type cell,
337 bool onlycolumn = false) const;
339 Length const getPWidth(idx_type cell) const;
341 int cellWidth(idx_type cell) const;
343 int getBeginningOfTextInCell(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 bool isFirstCellInRow(idx_type cell) const;
359 idx_type getFirstCellInRow(row_type row) const;
361 bool isLastCellInRow(idx_type cell) const;
363 idx_type getLastCellInRow(row_type row) const;
365 idx_type numberOfCellsInRow(idx_type cell) const;
367 void write(std::ostream &) const;
371 int latex(odocstream &, OutputParams const &) const;
373 int docbook(odocstream & os, OutputParams const &) const;
375 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
377 void plaintext(odocstream &,
378 OutputParams const & runparams, int const depth,
379 bool onlydata, char_type delim) const;
381 bool isMultiColumn(idx_type cell) const;
383 bool isMultiColumnReal(idx_type cell) const;
385 void setMultiColumn(idx_type cell, idx_type number);
387 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
389 bool isPartOfMultiColumn(row_type row, col_type column) const;
391 row_type cellRow(idx_type cell) const;
393 col_type cellColumn(idx_type cell) const;
395 col_type cellRightColumn(idx_type cell) const;
397 void setRotateCell(idx_type cell, bool);
399 bool getRotateCell(idx_type cell) const;
401 bool needRotating() const;
403 bool isLastCell(idx_type cell) const;
405 idx_type cellAbove(idx_type cell) const;
407 idx_type cellBelow(idx_type cell) const;
409 idx_type cellIndex(row_type row, col_type column) const;
411 void setUsebox(idx_type cell, BoxType);
413 BoxType getUsebox(idx_type cell) const;
415 // Long Tabular Options support functions
417 bool checkLTType(row_type row, ltType const &) const;
419 void setLTHead(row_type row, bool flag, ltType const &, bool first);
421 bool getRowOfLTHead(row_type row, ltType &) const;
423 bool getRowOfLTFirstHead(row_type row, ltType &) const;
425 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
427 bool getRowOfLTFoot(row_type row, ltType &) const;
429 bool getRowOfLTLastFoot(row_type row, ltType &) const;
431 void setLTNewPage(row_type row, bool what);
433 bool getLTNewPage(row_type row) const;
435 idx_type setLTCaption(row_type row, bool what);
437 bool ltCaption(row_type row) const;
439 bool haveLTHead() const;
441 bool haveLTFirstHead() const;
443 bool haveLTFoot() const;
445 bool haveLTLastFoot() const;
447 bool haveLTCaption() const;
449 // end longtable support
451 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
453 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
454 col_type column) const;
456 void setCellInset(row_type row, col_type column,
457 boost::shared_ptr<InsetTableCell>) const;
458 /// Search for \param inset in the tabular, with the
460 void validate(LaTeXFeatures &) const;
462 // FIXME Now that cells have an InsetTableCell as their insets, rather
463 // than an InsetText, it'd be possible to reverse the relationship here,
464 // so that cell_vector was a vector<InsetTableCell> rather than a
465 // vector<CellData>, and an InsetTableCell had a CellData as a member,
466 // or perhaps just had its members as members.
473 CellData(CellData const &);
475 CellData & operator=(CellData);
477 void swap(CellData & rhs);
485 LyXAlignment alignment;
487 VAlignment valignment;
501 docstring align_special;
503 Length p_width; // this is only set for multicolumn!!!
505 boost::shared_ptr<InsetTableCell> inset;
507 CellData & cellInfo(idx_type cell) const;
509 typedef std::vector<CellData> cell_vector;
511 typedef std::vector<cell_vector> cell_vvector;
522 /// Extra space between the top line and this row
524 /// Ignore top_space if true and use the default top space
525 bool top_space_default;
526 /// Extra space between this row and the bottom line
528 /// Ignore bottom_space if true and use the default bottom space
529 bool bottom_space_default;
530 /// Extra space between the bottom line and the next top line
531 Length interline_space;
532 /// Ignore interline_space if true and use the default interline space
533 bool interline_space_default;
534 /// This are for longtabulars only
537 /// a row of endfirsthead
541 /// row of endlastfoot
543 /// row for a newpage
549 typedef std::vector<RowData> row_vector;
557 LyXAlignment alignment;
559 VAlignment valignment;
565 docstring align_special;
568 typedef std::vector<ColumnData> column_vector;
571 idx_type numberofcells;
573 std::vector<row_type> rowofcell;
575 std::vector<col_type> columnofcell;
579 column_vector column_info;
581 mutable cell_vvector cell_info;
587 VAlignment tabular_valignment;
591 HAlignment longtabular_alignment;
593 bool is_long_tabular;
596 /// endfirsthead data
604 void init(Buffer *, row_type rows_arg,
605 col_type columns_arg);
607 void updateIndexes();
609 bool setFixedWidth(row_type r, col_type c);
611 void updateContentAlignment(row_type r, col_type c);
612 /// return true of update is needed
613 bool updateColumnWidths();
615 idx_type columnSpan(idx_type cell) const;
617 BoxType useParbox(idx_type cell) const;
619 // helper function for Latex returns number of newlines
621 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
623 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
625 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
627 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
629 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
631 bool isValidRow(row_type const row) const;
633 int TeXRow(odocstream &, row_type const row,
634 OutputParams const &) const;
636 // helper functions for plain text
638 bool plaintextTopHLine(odocstream &, row_type row,
639 std::vector<unsigned int> const &) const;
641 bool plaintextBottomHLine(odocstream &, row_type row,
642 std::vector<unsigned int> const &) const;
644 void plaintextPrintCell(odocstream &,
645 OutputParams const &,
646 idx_type cell, row_type row, col_type column,
647 std::vector<unsigned int> const &,
648 bool onlydata) const;
649 /// auxiliary function for docbook
650 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
652 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &) const;
654 /// change associated Buffer
655 void setBuffer(Buffer & buffer);
656 /// retrieve associated Buffer
657 Buffer & buffer() const { return *buffer_; }
666 class InsetTableCell : public InsetText
670 InsetTableCell(Buffer * buf);
672 InsetCode lyxCode() const { return CELL_CODE; }
674 Inset * clone() { return new InsetTableCell(*this); }
676 bool getStatus(Cursor & cur, FuncRequest const & cmd,
677 FuncStatus & status) const;
679 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
681 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
682 /// writes the contents of the cell as a string, optionally
683 /// descending into insets
684 docstring asString(bool intoInsets = true);
686 docstring xhtml(XHTMLStream &, OutputParams const &) const;
691 void operator=(InsetTableCell const &);
693 // This boolean is supposed to track whether the cell has had its
694 // width explicitly set. We need to know this to determine whether
695 // layout changes and paragraph customization are allowed---that is,
696 // we need it in forcePlainLayout() and allowParagraphCustomization().
697 // Unfortunately, that information is not readily available in
698 // InsetTableCell. In the case of multicolumn cells, it is present
699 // in CellData, and so would be available here if CellData were to
700 // become a member of InsetTableCell. But in the other case, it isn't
701 // even available there, but is held in Tabular::ColumnData.
702 // So, the present solution uses this boolean to track the information
703 // we need to track, and tries to keep it updated. This is not ideal,
704 // but the other solutions are no better. These are:
705 // (i) Keep a pointer in InsetTableCell to the table;
706 // (ii) Find the table by iterating over the Buffer's insets.
707 // Solution (i) raises the problem of updating the pointer when an
708 // InsetTableCell is copied, and we'd therefore need a copy constructor
709 // in InsetTabular and then in Tabular, which seems messy, given how
710 // complicated those classes are. Solution (ii) involves a lot of
711 // iterating, since this information is needed quite often, and so may
713 // So, well, if someone can do better, please do!
717 // FIXME: Here the thoughts from the comment above also apply.
719 LyXAlignment contentAlign;
720 /// should paragraph indendation be omitted in any case?
721 bool neverIndent() const { return true; }
723 LyXAlignment contentAlignment() const { return contentAlign; }
725 virtual bool usePlainLayout() const { return true; }
727 virtual bool forcePlainLayout(idx_type = 0) const;
729 virtual bool allowParagraphCustomization(idx_type = 0) const;
730 /// Is the width forced to some value?
731 bool hasFixedWidth() const { return isFixedWidth; }
735 class InsetTabular : public Inset
739 InsetTabular(Buffer *, row_type rows = 1,
740 col_type columns = 1);
744 void setBuffer(Buffer & buffer);
747 static void string2params(std::string const &, InsetTabular &);
749 static std::string params2string(InsetTabular const &);
753 void write(std::ostream &) const;
755 void metrics(MetricsInfo &, Dimension &) const;
757 void draw(PainterInfo & pi, int x, int y) const;
759 void drawSelection(PainterInfo & pi, int x, int y) const;
761 bool editable() const { return true; }
763 bool hasSettings() const { return true; }
765 bool insetAllowed(InsetCode code) const;
767 bool allowSpellCheck() const { return true; }
769 bool canTrackChanges() const { return true; }
770 /** returns true if, when outputing LaTeX, font changes should
771 be closed before generating this inset. This is needed for
772 insets that may contain several paragraphs */
773 bool noFontChange() const { return true; }
775 DisplayType display() const;
777 int latex(odocstream &, OutputParams const &) const;
779 int plaintext(odocstream &, OutputParams const &) const;
781 int docbook(odocstream &, OutputParams const &) const;
783 docstring xhtml(XHTMLStream &, OutputParams const &) const;
785 void validate(LaTeXFeatures & features) const;
787 InsetCode lyxCode() const { return TABULAR_CODE; }
789 docstring contextMenu(BufferView const & bv, int x, int y) const;
790 /// get offset of this cursor slice relative to our upper left corner
791 void cursorPos(BufferView const & bv, CursorSlice const & sl,
792 bool boundary, int & x, int & y) const;
794 bool tabularFeatures(Cursor & cur, std::string const & what);
796 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
797 std::string const & val = std::string());
799 void openLayoutDialog(BufferView *) const;
801 bool showInsetDialog(BufferView *) const;
803 size_t nargs() const { return tabular.numberofcells; }
805 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
807 boost::shared_ptr<InsetTableCell> cell(idx_type);
809 Text * getText(int) const;
811 /// set the change for the entire inset
812 void setChange(Change const & change);
813 /// accept the changes within the inset
814 void acceptChanges();
815 /// reject the changes within the inset
816 void rejectChanges();
818 // this should return true if we have a "normal" cell, otherwise false.
819 // "normal" means without width set!
820 /// should all paragraphs be output with "Standard" layout?
821 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
823 virtual bool forcePlainLayout(idx_type cell = 0) const;
825 void addPreview(DocIterator const & inset_pos,
826 graphics::PreviewLoader &) const;
828 /// lock cell with given index
829 void edit(Cursor & cur, bool front, EntryDirection entry_from);
830 /// get table row from x coordinate
831 int rowFromY(Cursor & cur, int y) const;
832 /// get table column from y coordinate
833 int columnFromX(Cursor & cur, int x) const;
835 Inset * editXY(Cursor & cur, int x, int y);
836 /// can we go further down on mouse click?
837 bool descendable(BufferView const &) const { return true; }
838 /// Update the counters of this inset and of its contents
839 void updateLabels(ParIterator const &, UpdateType);
841 void addToToc(DocIterator const &);
844 bool completionSupported(Cursor const &) const;
846 bool inlineCompletionSupported(Cursor const & cur) const;
848 bool automaticInlineCompletion() const;
850 bool automaticPopupCompletion() const;
852 bool showCompletionCursor() const;
854 CompletionList const * createCompletionList(Cursor const & cur) const;
856 docstring completionPrefix(Cursor const & cur) const;
858 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
860 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
862 virtual bool usePlainLayout() const { return true; }
865 virtual InsetTabular * asInsetTabular() { return this; }
867 virtual InsetTabular const * asInsetTabular() const { return this; }
869 bool isRightToLeft(Cursor & cur) const;
870 /// writes the cells between stidx and enidx as a string, optionally
871 /// descending into the insets
872 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
874 /// Returns whether the cell in the specified row and column is selected.
875 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
877 // Public structures and variables
879 mutable Tabular tabular;
883 InsetTabular(InsetTabular const &);
885 void doDispatch(Cursor & cur, FuncRequest & cmd);
887 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
889 int scroll() const { return scx_; }
891 Inset * clone() const { return new InsetTabular(*this); }
894 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
895 idx_type cell, Change const & change) const;
897 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
900 void moveNextCell(Cursor & cur,
901 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
903 void movePrevCell(Cursor & cur,
904 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
906 int cellXPos(idx_type cell) const;
908 void resetPos(Cursor & cur) const;
910 void removeTabularRow();
912 bool copySelection(Cursor & cur);
914 bool pasteClipboard(Cursor & cur);
916 void cutSelection(Cursor & cur);
918 void getSelection(Cursor & cur, row_type & rs, row_type & re,
919 col_type & cs, col_type & ce) const;
921 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
923 /// return the "Manhattan distance" to nearest corner
924 int dist(BufferView &, idx_type cell, int x, int y) const;
925 /// return the cell nearest to x, y
926 idx_type getNearestCell(BufferView &, int x, int y) const;
928 /// test the rotation state of the give cell range.
929 bool oneCellHasRotationState(bool rotated,
930 row_type row_start, row_type row_end,
931 col_type col_start, col_type col_end) const;
933 mutable idx_type first_visible_cell;
936 /// true when selecting rows with the mouse
938 /// true when selecting columns with the mouse
942 std::string const featureAsString(Tabular::Feature feature);
946 #endif // INSET_TABULAR_H