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,
186 CELL_BEGIN_OF_MULTICOLUMN,
188 CELL_PART_OF_MULTICOLUMN
196 LYX_VALIGN_BOTTOM = 1,
198 LYX_VALIGN_MIDDLE = 2
214 // we have this header type (is set in the getLT... functions)
216 // double borders on top
218 // double borders on bottom
220 // used for FirstHeader & LastFooter and if this is true
221 // all the rows marked as FirstHeader or LastFooter are
222 // ignored in the output and it is set to be empty!
226 /// type for row numbers
227 typedef size_t row_type;
228 /// type for column numbers
229 typedef size_t col_type;
230 /// type for cell indices
231 typedef size_t idx_type;
232 /// index indicating an invalid position
233 static const idx_type npos = static_cast<idx_type>(-1);
238 Tabular(Buffer const &, 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 Length const getColumnPWidth(idx_type cell) const;
321 Length const getMColumnPWidth(idx_type cell) const;
323 docstring const getAlignSpecial(idx_type cell, int what) const;
325 int cellWidth(idx_type cell) const;
327 int getBeginningOfTextInCell(idx_type cell) const;
329 void appendRow(idx_type cell);
331 void deleteRow(row_type row);
333 void copyRow(row_type);
335 void appendColumn(idx_type cell);
337 void deleteColumn(col_type column);
339 void copyColumn(col_type);
341 bool isFirstCellInRow(idx_type cell) const;
343 idx_type getFirstCellInRow(row_type row) const;
345 bool isLastCellInRow(idx_type cell) const;
347 idx_type getLastCellInRow(row_type row) const;
349 idx_type cellCount() const;
351 idx_type numberOfCellsInRow(idx_type cell) const;
353 void write(std::ostream &) const;
357 int latex(odocstream &, OutputParams const &) const;
359 int docbook(odocstream & os, OutputParams const &) const;
361 void plaintext(odocstream &,
362 OutputParams const & runparams, int const depth,
363 bool onlydata, char_type delim) const;
365 bool isMultiColumn(idx_type cell) const;
367 bool isMultiColumnReal(idx_type cell) const;
369 void setMultiColumn(idx_type cell, idx_type number);
371 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
373 bool isPartOfMultiColumn(row_type row, col_type column) const;
375 row_type cellRow(idx_type cell) const;
377 col_type cellColumn(idx_type cell) const;
379 col_type cellRightColumn(idx_type cell) const;
381 void setBookTabs(bool);
383 bool useBookTabs() const;
385 void setLongTabular(bool);
387 bool isLongTabular() const;
389 void setRotateTabular(bool);
391 bool getRotateTabular() const;
393 void setRotateCell(idx_type cell, bool);
395 bool getRotateCell(idx_type cell) const;
397 bool needRotating() const;
399 bool isLastCell(idx_type cell) const;
401 idx_type cellAbove(idx_type cell) const;
403 idx_type cellBelow(idx_type cell) const;
405 idx_type cellIndex(row_type row, col_type column) const;
407 void setUsebox(idx_type cell, BoxType);
409 BoxType getUsebox(idx_type cell) const;
411 // Long Tabular Options support functions
413 bool checkLTType(row_type row, ltType const &) const;
415 void setLTHead(row_type row, bool flag, ltType const &, bool first);
417 bool getRowOfLTHead(row_type row, ltType &) const;
419 bool getRowOfLTFirstHead(row_type row, ltType &) const;
421 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
423 bool getRowOfLTFoot(row_type row, ltType &) const;
425 bool getRowOfLTLastFoot(row_type row, ltType &) const;
427 void setLTNewPage(row_type row, bool what);
429 bool getLTNewPage(row_type row) const;
431 bool haveLTHead() const;
433 bool haveLTFirstHead() const;
435 bool haveLTFoot() const;
437 bool haveLTLastFoot() const;
439 // end longtable support
441 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
443 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
444 col_type column) const;
446 void setCellInset(row_type row, col_type column,
447 boost::shared_ptr<InsetTableCell>) const;
448 /// Search for \param inset in the tabular, with the
450 idx_type cellFromInset(Inset const * inset) const;
452 row_type rowCount() const { return row_info.size(); }
454 col_type columnCount() const { return column_info.size();}
456 void validate(LaTeXFeatures &) const;
463 CellData(Buffer const &, Tabular const &);
465 CellData(CellData const &);
467 CellData & operator=(CellData);
469 void swap(CellData & rhs);
477 LyXAlignment alignment;
479 VAlignment valignment;
493 docstring align_special;
495 Length p_width; // this is only set for multicolumn!!!
497 boost::shared_ptr<InsetTableCell> inset;
499 CellData & cellInfo(idx_type cell) const;
501 typedef std::vector<CellData> cell_vector;
503 typedef std::vector<cell_vector> cell_vvector;
518 /// Extra space between the top line and this row
520 /// Ignore top_space if true and use the default top space
521 bool top_space_default;
522 /// Extra space between this row and the bottom line
524 /// Ignore bottom_space if true and use the default bottom space
525 bool bottom_space_default;
526 /// Extra space between the bottom line and the next top line
527 Length interline_space;
528 /// Ignore interline_space if true and use the default interline space
529 bool interline_space_default;
530 /// This are for longtabulars only
533 /// a row of endfirsthead
537 /// row of endlastfoot
539 /// row for a newpage
543 typedef std::vector<RowData> row_vector;
551 LyXAlignment alignment;
553 VAlignment valignment;
563 docstring align_special;
566 typedef std::vector<ColumnData> column_vector;
569 idx_type numberofcells;
571 std::vector<row_type> rowofcell;
573 std::vector<col_type> columnofcell;
577 column_vector column_info;
579 mutable cell_vvector cell_info;
587 bool is_long_tabular;
590 /// endfirsthead data
598 void init(Buffer const &, row_type rows_arg,
599 col_type columns_arg);
601 void updateIndexes();
602 /// return true of update is needed
603 bool updateColumnWidths();
605 idx_type columnSpan(idx_type cell) const;
607 BoxType useParbox(idx_type cell) const;
609 // helper function for Latex returns number of newlines
611 int TeXTopHLine(odocstream &, row_type row) const;
613 int TeXBottomHLine(odocstream &, row_type row) const;
615 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
617 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
619 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
621 bool isValidRow(row_type const row) const;
623 int TeXRow(odocstream &, row_type const row,
624 OutputParams const &) const;
626 // helper functions for plain text
628 bool plaintextTopHLine(odocstream &, row_type row,
629 std::vector<unsigned int> const &) const;
631 bool plaintextBottomHLine(odocstream &, row_type row,
632 std::vector<unsigned int> const &) const;
634 void plaintextPrintCell(odocstream &,
635 OutputParams const &,
636 idx_type cell, row_type row, col_type column,
637 std::vector<unsigned int> const &,
638 bool onlydata) const;
639 /// auxiliary function for docbook
640 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
642 /// change associated Buffer
643 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
644 /// retrieve associated Buffer
645 Buffer const & buffer() const { return *buffer_; }
648 Buffer const * buffer_;
654 class InsetTableCell : public InsetText
658 InsetTableCell(Buffer const & buf,
659 Tabular::CellData const * cd, Tabular const * t);
661 InsetCode lyxCode() const { return CELL_CODE; }
663 Inset * clone() { return new InsetTableCell(*this); }
665 virtual bool useEmptyLayout() const { return true; }
667 virtual bool forceEmptyLayout(idx_type = 0) const;
669 virtual bool allowParagraphCustomization(idx_type = 0) const;
671 bool getStatus(Cursor & cur, FuncRequest const & cmd,
672 FuncStatus & status) const;
674 virtual bool neverIndent() { return true; }
676 void setCellData(Tabular::CellData const * cd) { cell_data_ = cd; }
678 void setTabular(Tabular const * t) { table_ = t; }
683 void operator=(InsetTableCell const &);
686 Tabular::CellData const * cell_data_;
688 Tabular const * table_;
692 class InsetTabular : public Inset
696 InsetTabular(Buffer const &, row_type rows = 1,
697 col_type columns = 1);
701 static void string2params(std::string const &, InsetTabular &);
703 static std::string params2string(InsetTabular const &);
707 void write(std::ostream &) const;
709 void metrics(MetricsInfo &, Dimension &) const;
711 void draw(PainterInfo & pi, int x, int y) const;
713 void drawSelection(PainterInfo & pi, int x, int y) const;
715 docstring editMessage() const;
717 EDITABLE editable() const { return HIGHLY_EDITABLE; }
719 bool insetAllowed(InsetCode code) const;
721 bool allowSpellCheck() const { return true; }
723 bool canTrackChanges() const { return true; }
724 /** returns true if, when outputing LaTeX, font changes should
725 be closed before generating this inset. This is needed for
726 insets that may contain several paragraphs */
727 bool noFontChange() const { return true; }
729 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
731 int latex(odocstream &, OutputParams const &) const;
733 int plaintext(odocstream &, OutputParams const &) const;
735 int docbook(odocstream &, OutputParams const &) const;
737 void validate(LaTeXFeatures & features) const;
739 InsetCode lyxCode() const { return TABULAR_CODE; }
741 docstring contextMenu(BufferView const & bv, int x, int y) const;
742 /// get offset of this cursor slice relative to our upper left corner
743 void cursorPos(BufferView const & bv, CursorSlice const & sl,
744 bool boundary, int & x, int & y) const;
746 bool tabularFeatures(Cursor & cur, std::string const & what);
748 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
749 std::string const & val = std::string());
751 void openLayoutDialog(BufferView *) const;
753 bool showInsetDialog(BufferView *) const;
755 size_t nargs() const { return tabular.cellCount(); }
757 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
759 boost::shared_ptr<InsetTableCell> cell(idx_type);
761 Text * getText(int) const;
763 /// set the change for the entire inset
764 void setChange(Change const & change);
765 /// accept the changes within the inset
766 void acceptChanges(BufferParams const & bparams);
767 /// reject the changes within the inset
768 void rejectChanges(BufferParams const & bparams);
770 // this should return true if we have a "normal" cell, otherwise false.
771 // "normal" means without width set!
772 /// should all paragraphs be output with "Standard" layout?
773 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
775 virtual bool forceEmptyLayout(idx_type cell = 0) const;
777 virtual bool useEmptyLayout() { return true; }
779 void addPreview(graphics::PreviewLoader &) const;
781 /// lock cell with given index
782 void edit(Cursor & cur, bool front, EntryDirection entry_from);
783 /// get table row from x coordinate
784 int rowFromY(Cursor & cur, int y) const;
785 /// get table column from y coordinate
786 int columnFromX(Cursor & cur, int x) const;
788 Inset * editXY(Cursor & cur, int x, int y);
789 /// can we go further down on mouse click?
790 bool descendable() const { return true; }
791 // Update the counters of this inset and of its contents
792 void updateLabels(ParIterator const &);
795 bool completionSupported(Cursor const &) const;
797 bool inlineCompletionSupported(Cursor const & cur) const;
799 bool automaticInlineCompletion() const;
801 bool automaticPopupCompletion() const;
803 bool showCompletionCursor() const;
805 CompletionList const * createCompletionList(Cursor const & cur) const;
807 docstring completionPrefix(Cursor const & cur) const;
809 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
811 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
814 virtual InsetTabular * asInsetTabular() { return this; }
816 virtual InsetTabular const * asInsetTabular() const { return this; }
818 bool isRightToLeft(Cursor & cur) const;
821 // Public structures and variables
823 mutable Tabular tabular;
827 InsetTabular(InsetTabular const &);
829 void doDispatch(Cursor & cur, FuncRequest & cmd);
831 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
833 int scroll() const { return scx_; }
835 Inset * clone() const { return new InsetTabular(*this); }
838 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
839 idx_type cell, bool erased) const;
841 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
844 void moveNextCell(Cursor & cur,
845 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
847 void movePrevCell(Cursor & cur,
848 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
850 int cellXPos(idx_type cell) const;
852 void resetPos(Cursor & cur) const;
854 void removeTabularRow();
856 bool copySelection(Cursor & cur);
858 bool pasteClipboard(Cursor & cur);
860 void cutSelection(Cursor & cur);
862 void getSelection(Cursor & cur, row_type & rs, row_type & re,
863 col_type & cs, col_type & ce) const;
865 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
866 /// are we operating on several cells?
867 bool tablemode(Cursor & cur) const;
869 /// return the "Manhattan distance" to nearest corner
870 int dist(BufferView &, idx_type cell, int x, int y) const;
871 /// return the cell nearest to x, y
872 idx_type getNearestCell(BufferView &, int x, int y) const;
874 /// test the rotation state of the give cell range.
875 bool oneCellHasRotationState(bool rotated,
876 row_type row_start, row_type row_end,
877 col_type col_start, col_type col_end) const;
879 mutable idx_type first_visible_cell;
882 /// true when selecting rows with the mouse
884 /// true when selecting columns with the mouse
888 std::string const featureAsString(Tabular::Feature feature);
892 #endif // INSET_TABULAR_H