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,
188 CELL_BEGIN_OF_MULTICOLUMN,
190 CELL_PART_OF_MULTICOLUMN
198 LYX_VALIGN_BOTTOM = 1,
200 LYX_VALIGN_MIDDLE = 2
216 // we have this header type (is set in the getLT... functions)
218 // double borders on top
220 // double borders on bottom
222 // used for FirstHeader & LastFooter and if this is true
223 // all the rows marked as FirstHeader or LastFooter are
224 // ignored in the output and it is set to be empty!
228 /// type for row numbers
229 typedef size_t row_type;
230 /// type for column numbers
231 typedef size_t col_type;
232 /// type for cell indices
233 typedef size_t idx_type;
234 /// index indicating an invalid position
235 static const idx_type npos = static_cast<idx_type>(-1);
238 Tabular(Buffer &, col_type columns_arg, row_type rows_arg);
240 /// Returns true if there is a topline, returns false if not
241 bool topLine(idx_type cell) const;
242 /// Returns true if there is a topline, returns false if not
243 bool bottomLine(idx_type cell) const;
244 /// Returns true if there is a topline, returns false if not
245 bool leftLine(idx_type cell) const;
246 /// Returns true if there is a topline, returns false if not
247 bool rightLine(idx_type cell) const;
250 bool topAlreadyDrawn(idx_type cell) const;
252 bool leftAlreadyDrawn(idx_type cell) const;
254 bool isLastRow(idx_type cell) const;
256 /// return space occupied by the second horizontal line and
257 /// interline space above row \p row in pixels
258 int getAdditionalHeight(row_type row) const;
260 int getAdditionalWidth(idx_type cell) const;
262 /* returns the maximum over all rows */
264 int columnWidth(idx_type cell) const;
270 int rowAscent(row_type row) const;
272 int rowDescent(row_type row) const;
274 void setRowAscent(row_type row, int height);
276 void setRowDescent(row_type row, int height);
278 void setCellWidth(idx_type cell, int new_width);
280 void setAllLines(idx_type cell, bool line);
282 void setTopLine(idx_type cell, bool line);
284 void setBottomLine(idx_type cell, bool line);
286 void setLeftLine(idx_type cell, bool line);
288 void setRightLine(idx_type cell, bool line);
290 bool rowTopLine(row_type row) const;
292 bool rowBottomLine(row_type row) const;
294 bool columnLeftLine(col_type column) const;
296 bool columnRightLine(col_type column) const;
298 void setAlignment(idx_type cell, LyXAlignment align,
299 bool onlycolumn = false);
301 void setVAlignment(idx_type cell, VAlignment align,
302 bool onlycolumn = false);
304 void setColumnPWidth(Cursor &, idx_type, Length const &);
306 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
308 void setAlignSpecial(idx_type cell, docstring const & special,
311 LyXAlignment getAlignment(idx_type cell,
312 bool onlycolumn = false) const;
314 VAlignment getVAlignment(idx_type cell,
315 bool onlycolumn = false) const;
317 Length const getPWidth(idx_type cell) const;
319 int cellWidth(idx_type cell) const;
321 int getBeginningOfTextInCell(idx_type cell) const;
323 void appendRow(idx_type cell);
325 void deleteRow(row_type row);
327 void copyRow(row_type);
329 void appendColumn(idx_type cell);
331 void deleteColumn(col_type column);
333 void copyColumn(col_type);
335 bool isFirstCellInRow(idx_type cell) const;
337 idx_type getFirstCellInRow(row_type row) const;
339 bool isLastCellInRow(idx_type cell) const;
341 idx_type getLastCellInRow(row_type row) const;
343 idx_type numberOfCellsInRow(idx_type cell) const;
345 void write(std::ostream &) const;
349 int latex(odocstream &, OutputParams const &) const;
351 int docbook(odocstream & os, OutputParams const &) const;
353 void plaintext(odocstream &,
354 OutputParams const & runparams, int const depth,
355 bool onlydata, char_type delim) const;
357 bool isMultiColumn(idx_type cell) const;
359 bool isMultiColumnReal(idx_type cell) const;
361 void setMultiColumn(idx_type cell, idx_type number);
363 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
365 bool isPartOfMultiColumn(row_type row, col_type column) const;
367 row_type cellRow(idx_type cell) const;
369 col_type cellColumn(idx_type cell) const;
371 col_type cellRightColumn(idx_type cell) const;
373 void setRotateCell(idx_type cell, bool);
375 bool getRotateCell(idx_type cell) const;
377 bool needRotating() const;
379 bool isLastCell(idx_type cell) const;
381 idx_type cellAbove(idx_type cell) const;
383 idx_type cellBelow(idx_type cell) const;
385 idx_type cellIndex(row_type row, col_type column) const;
387 void setUsebox(idx_type cell, BoxType);
389 BoxType getUsebox(idx_type cell) const;
391 // Long Tabular Options support functions
393 bool checkLTType(row_type row, ltType const &) const;
395 void setLTHead(row_type row, bool flag, ltType const &, bool first);
397 bool getRowOfLTHead(row_type row, ltType &) const;
399 bool getRowOfLTFirstHead(row_type row, ltType &) const;
401 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
403 bool getRowOfLTFoot(row_type row, ltType &) const;
405 bool getRowOfLTLastFoot(row_type row, ltType &) const;
407 void setLTNewPage(row_type row, bool what);
409 bool getLTNewPage(row_type row) const;
411 idx_type setLTCaption(row_type row, bool what);
413 bool ltCaption(row_type row) const;
415 bool haveLTHead() const;
417 bool haveLTFirstHead() const;
419 bool haveLTFoot() const;
421 bool haveLTLastFoot() const;
423 // end longtable support
425 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
427 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
428 col_type column) const;
430 void setCellInset(row_type row, col_type column,
431 boost::shared_ptr<InsetTableCell>) const;
432 /// Search for \param inset in the tabular, with the
434 void validate(LaTeXFeatures &) const;
436 // FIXME Now that cells have an InsetTableCell as their insets, rather
437 // than an InsetText, it'd be possible to reverse the relationship here,
438 // so that cell_vector was a vector<InsetTableCell> rather than a
439 // vector<CellData>, and an InsetTableCell had a CellData as a member,
440 // or perhaps just had its members as members.
447 CellData(CellData const &);
449 CellData & operator=(CellData);
451 void swap(CellData & rhs);
459 LyXAlignment alignment;
461 VAlignment valignment;
475 docstring align_special;
477 Length p_width; // this is only set for multicolumn!!!
479 boost::shared_ptr<InsetTableCell> inset;
481 CellData & cellInfo(idx_type cell) const;
483 typedef std::vector<CellData> cell_vector;
485 typedef std::vector<cell_vector> cell_vvector;
496 /// Extra space between the top line and this row
498 /// Ignore top_space if true and use the default top space
499 bool top_space_default;
500 /// Extra space between this row and the bottom line
502 /// Ignore bottom_space if true and use the default bottom space
503 bool bottom_space_default;
504 /// Extra space between the bottom line and the next top line
505 Length interline_space;
506 /// Ignore interline_space if true and use the default interline space
507 bool interline_space_default;
508 /// This are for longtabulars only
511 /// a row of endfirsthead
515 /// row of endlastfoot
517 /// row for a newpage
523 typedef std::vector<RowData> row_vector;
531 LyXAlignment alignment;
533 VAlignment valignment;
539 docstring align_special;
542 typedef std::vector<ColumnData> column_vector;
545 idx_type numberofcells;
547 std::vector<row_type> rowofcell;
549 std::vector<col_type> columnofcell;
553 column_vector column_info;
555 mutable cell_vvector cell_info;
563 bool is_long_tabular;
566 /// endfirsthead data
574 void init(Buffer &, row_type rows_arg,
575 col_type columns_arg);
577 void updateIndexes();
579 bool setFixedWidth(row_type r, col_type c);
580 /// return true of update is needed
581 bool updateColumnWidths();
583 idx_type columnSpan(idx_type cell) const;
585 BoxType useParbox(idx_type cell) const;
587 // helper function for Latex returns number of newlines
589 int TeXTopHLine(odocstream &, row_type row) const;
591 int TeXBottomHLine(odocstream &, row_type row) const;
593 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
595 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
597 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
599 bool isValidRow(row_type const row) const;
601 int TeXRow(odocstream &, row_type const row,
602 OutputParams const &) const;
604 // helper functions for plain text
606 bool plaintextTopHLine(odocstream &, row_type row,
607 std::vector<unsigned int> const &) const;
609 bool plaintextBottomHLine(odocstream &, row_type row,
610 std::vector<unsigned int> const &) const;
612 void plaintextPrintCell(odocstream &,
613 OutputParams const &,
614 idx_type cell, row_type row, col_type column,
615 std::vector<unsigned int> const &,
616 bool onlydata) const;
617 /// auxiliary function for docbook
618 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
620 /// change associated Buffer
621 void setBuffer(Buffer & buffer);
622 /// retrieve associated Buffer
623 Buffer & buffer() const { return *buffer_; }
632 class InsetTableCell : public InsetText
636 InsetTableCell(Buffer & buf);
638 InsetCode lyxCode() const { return CELL_CODE; }
640 Inset * clone() { return new InsetTableCell(*this); }
642 bool getStatus(Cursor & cur, FuncRequest const & cmd,
643 FuncStatus & status) const;
645 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
646 /// writes the contents of the cell as a string, optionally
647 /// descending into insets
648 docstring asString(bool intoInsets = true);
653 void operator=(InsetTableCell const &);
655 // This boolean is supposed to track whether the cell has had its
656 // width explicitly set. We need to know this to determine whether
657 // layout changes and paragraph customization are allowed---that is,
658 // we need it in forcePlainLayout() and allowParagraphCustomization().
659 // Unfortunately, that information is not readily available in
660 // InsetTableCell. In the case of multicolumn cells, it is present
661 // in CellData, and so would be available here if CellData were to
662 // become a member of InsetTableCell. But in the other case, it isn't
663 // even available there, but is held in Tabular::ColumnData.
664 // So, the present solution uses this boolean to track the information
665 // we need to track, and tries to keep it updated. This is not ideal,
666 // but the other solutions are no better. These are:
667 // (i) Keep a pointer in InsetTableCell to the table;
668 // (ii) Find the table by iterating over the Buffer's insets.
669 // Solution (i) raises the problem of updating the pointer when an
670 // InsetTableCell is copied, and we'd therefore need a copy constructor
671 // in InsetTabular and then in Tabular, which seems messy, given how
672 // complicated those classes are. Solution (ii) involves a lot of
673 // iterating, since this information is needed quite often, and so may
675 // So, well, if someone can do better, please do!
679 /// should paragraph indendation be omitted in any case?
680 bool neverIndent() const { return true; }
682 virtual bool usePlainLayout() const { return true; }
684 virtual bool forcePlainLayout(idx_type = 0) const;
686 virtual bool allowParagraphCustomization(idx_type = 0) const;
687 /// Is the width forced to some value?
688 bool hasFixedWidth() const { return isFixedWidth; }
692 class InsetTabular : public Inset
696 InsetTabular(Buffer &, row_type rows = 1,
697 col_type columns = 1);
701 void setBuffer(Buffer & buffer);
704 static void string2params(std::string const &, InsetTabular &);
706 static std::string params2string(InsetTabular const &);
710 void write(std::ostream &) const;
712 void metrics(MetricsInfo &, Dimension &) const;
714 void draw(PainterInfo & pi, int x, int y) const;
716 void drawSelection(PainterInfo & pi, int x, int y) const;
718 docstring editMessage() const;
720 EDITABLE editable() const { return HIGHLY_EDITABLE; }
722 bool insetAllowed(InsetCode code) const;
724 bool allowSpellCheck() const { return true; }
726 bool canTrackChanges() const { return true; }
727 /** returns true if, when outputing LaTeX, font changes should
728 be closed before generating this inset. This is needed for
729 insets that may contain several paragraphs */
730 bool noFontChange() const { return true; }
732 DisplayType display() const { return tabular.is_long_tabular ? AlignCenter : Inline; }
734 int latex(odocstream &, OutputParams const &) const;
736 int plaintext(odocstream &, OutputParams const &) const;
738 int docbook(odocstream &, OutputParams const &) const;
740 void validate(LaTeXFeatures & features) const;
742 InsetCode lyxCode() const { return TABULAR_CODE; }
744 docstring contextMenu(BufferView const & bv, int x, int y) const;
745 /// get offset of this cursor slice relative to our upper left corner
746 void cursorPos(BufferView const & bv, CursorSlice const & sl,
747 bool boundary, int & x, int & y) const;
749 bool tabularFeatures(Cursor & cur, std::string const & what);
751 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
752 std::string const & val = std::string());
754 void openLayoutDialog(BufferView *) const;
756 bool showInsetDialog(BufferView *) const;
758 size_t nargs() const { return tabular.numberofcells; }
760 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
762 boost::shared_ptr<InsetTableCell> cell(idx_type);
764 Text * getText(int) const;
766 /// set the change for the entire inset
767 void setChange(Change const & change);
768 /// accept the changes within the inset
769 void acceptChanges(BufferParams const & bparams);
770 /// reject the changes within the inset
771 void rejectChanges(BufferParams const & bparams);
773 // this should return true if we have a "normal" cell, otherwise false.
774 // "normal" means without width set!
775 /// should all paragraphs be output with "Standard" layout?
776 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
778 virtual bool forcePlainLayout(idx_type cell = 0) const;
780 virtual bool usePlainLayout() { return true; }
782 void addPreview(graphics::PreviewLoader &) const;
784 /// lock cell with given index
785 void edit(Cursor & cur, bool front, EntryDirection entry_from);
786 /// get table row from x coordinate
787 int rowFromY(Cursor & cur, int y) const;
788 /// get table column from y coordinate
789 int columnFromX(Cursor & cur, int x) const;
791 Inset * editXY(Cursor & cur, int x, int y);
792 /// can we go further down on mouse click?
793 bool descendable() const { return true; }
794 // Update the counters of this inset and of its contents
795 void updateLabels(ParIterator const &);
798 bool completionSupported(Cursor const &) const;
800 bool inlineCompletionSupported(Cursor const & cur) const;
802 bool automaticInlineCompletion() const;
804 bool automaticPopupCompletion() const;
806 bool showCompletionCursor() const;
808 CompletionList const * createCompletionList(Cursor const & cur) const;
810 docstring completionPrefix(Cursor const & cur) const;
812 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
814 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
816 virtual bool usePlainLayout() const { return true; }
819 virtual InsetTabular * asInsetTabular() { return this; }
821 virtual InsetTabular const * asInsetTabular() const { return this; }
823 bool isRightToLeft(Cursor & cur) const;
824 /// writes the cells between stidx and enidx as a string, optionally
825 /// descending into the insets
826 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
829 // Public structures and variables
831 mutable Tabular tabular;
835 InsetTabular(InsetTabular const &);
837 void doDispatch(Cursor & cur, FuncRequest & cmd);
839 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
841 int scroll() const { return scx_; }
843 Inset * clone() const { return new InsetTabular(*this); }
846 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
847 idx_type cell, bool erased) const;
849 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
852 void moveNextCell(Cursor & cur,
853 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
855 void movePrevCell(Cursor & cur,
856 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
858 int cellXPos(idx_type cell) const;
860 void resetPos(Cursor & cur) const;
862 void removeTabularRow();
864 bool copySelection(Cursor & cur);
866 bool pasteClipboard(Cursor & cur);
868 void cutSelection(Cursor & cur);
870 void getSelection(Cursor & cur, row_type & rs, row_type & re,
871 col_type & cs, col_type & ce) const;
873 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
875 /// return the "Manhattan distance" to nearest corner
876 int dist(BufferView &, idx_type cell, int x, int y) const;
877 /// return the cell nearest to x, y
878 idx_type getNearestCell(BufferView &, int x, int y) const;
880 /// test the rotation state of the give cell range.
881 bool oneCellHasRotationState(bool rotated,
882 row_type row_start, row_type row_end,
883 col_type col_start, col_type col_end) const;
885 mutable idx_type first_visible_cell;
888 /// true when selecting rows with the mouse
890 /// true when selecting columns with the mouse
894 std::string const featureAsString(Tabular::Feature feature);
898 #endif // INSET_TABULAR_H