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);
236 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
238 /// Returns true if there is a topline, returns false if not
239 bool topLine(idx_type cell) const;
240 /// Returns true if there is a topline, returns false if not
241 bool bottomLine(idx_type cell) const;
242 /// Returns true if there is a topline, returns false if not
243 bool leftLine(idx_type cell) const;
244 /// Returns true if there is a topline, returns false if not
245 bool rightLine(idx_type cell) const;
248 bool topAlreadyDrawn(idx_type cell) const;
250 bool leftAlreadyDrawn(idx_type cell) const;
252 bool isLastRow(idx_type cell) const;
254 /// return space occupied by the second horizontal line and
255 /// interline space above row \p row in pixels
256 int getAdditionalHeight(row_type row) const;
258 int getAdditionalWidth(idx_type cell) const;
260 /* returns the maximum over all rows */
262 int columnWidth(idx_type cell) const;
268 int rowAscent(row_type row) const;
270 int rowDescent(row_type row) const;
272 void setRowAscent(row_type row, int height);
274 void setRowDescent(row_type row, int height);
276 void setCellWidth(idx_type cell, int new_width);
278 void setAllLines(idx_type cell, bool line);
280 void setTopLine(idx_type cell, bool line);
282 void setBottomLine(idx_type cell, bool line);
284 void setLeftLine(idx_type cell, bool line);
286 void setRightLine(idx_type cell, bool line);
288 bool rowTopLine(row_type row) const;
290 bool rowBottomLine(row_type row) const;
292 bool columnLeftLine(col_type column) const;
294 bool columnRightLine(col_type column) const;
296 void setAlignment(idx_type cell, LyXAlignment align,
297 bool onlycolumn = false);
299 void setVAlignment(idx_type cell, VAlignment align,
300 bool onlycolumn = false);
302 void setColumnPWidth(Cursor &, idx_type, Length const &);
304 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
306 void setAlignSpecial(idx_type cell, docstring const & special,
309 LyXAlignment getAlignment(idx_type cell,
310 bool onlycolumn = false) const;
312 VAlignment getVAlignment(idx_type cell,
313 bool onlycolumn = false) const;
315 Length const getPWidth(idx_type cell) const;
317 Length const getColumnPWidth(idx_type cell) const;
319 Length const getMColumnPWidth(idx_type cell) const;
321 docstring const getAlignSpecial(idx_type cell, int what) const;
323 int cellWidth(idx_type cell) const;
325 int getBeginningOfTextInCell(idx_type cell) const;
327 void appendRow(idx_type cell);
329 void deleteRow(row_type row);
331 void copyRow(row_type);
333 void appendColumn(idx_type cell);
335 void deleteColumn(col_type column);
337 void copyColumn(col_type);
339 bool isFirstCellInRow(idx_type cell) const;
341 idx_type getFirstCellInRow(row_type row) const;
343 bool isLastCellInRow(idx_type cell) const;
345 idx_type getLastCellInRow(row_type row) const;
347 idx_type numberOfCellsInRow(idx_type cell) const;
349 void write(std::ostream &) const;
353 int latex(odocstream &, OutputParams const &) const;
355 int docbook(odocstream & os, OutputParams const &) const;
357 void plaintext(odocstream &,
358 OutputParams const & runparams, int const depth,
359 bool onlydata, char_type delim) const;
361 bool isMultiColumn(idx_type cell) const;
363 bool isMultiColumnReal(idx_type cell) const;
365 void setMultiColumn(idx_type cell, idx_type number);
367 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
369 bool isPartOfMultiColumn(row_type row, col_type column) const;
371 row_type cellRow(idx_type cell) const;
373 col_type cellColumn(idx_type cell) const;
375 col_type cellRightColumn(idx_type cell) const;
377 void setRotateCell(idx_type cell, bool);
379 bool getRotateCell(idx_type cell) const;
381 bool needRotating() const;
383 bool isLastCell(idx_type cell) const;
385 idx_type cellAbove(idx_type cell) const;
387 idx_type cellBelow(idx_type cell) const;
389 idx_type cellIndex(row_type row, col_type column) const;
391 void setUsebox(idx_type cell, BoxType);
393 BoxType getUsebox(idx_type cell) const;
395 // Long Tabular Options support functions
397 bool checkLTType(row_type row, ltType const &) const;
399 void setLTHead(row_type row, bool flag, ltType const &, bool first);
401 bool getRowOfLTHead(row_type row, ltType &) const;
403 bool getRowOfLTFirstHead(row_type row, ltType &) const;
405 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
407 bool getRowOfLTFoot(row_type row, ltType &) const;
409 bool getRowOfLTLastFoot(row_type row, ltType &) const;
411 void setLTNewPage(row_type row, bool what);
413 bool getLTNewPage(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 idx_type cellFromInset(Inset const * inset) const;
436 void validate(LaTeXFeatures &) const;
443 CellData(Buffer const &, Tabular const &);
445 CellData(CellData const &);
447 CellData & operator=(CellData);
449 void swap(CellData & rhs);
457 LyXAlignment alignment;
459 VAlignment valignment;
473 docstring align_special;
475 Length p_width; // this is only set for multicolumn!!!
477 boost::shared_ptr<InsetTableCell> inset;
479 CellData & cellInfo(idx_type cell) const;
481 typedef std::vector<CellData> cell_vector;
483 typedef std::vector<cell_vector> cell_vvector;
498 /// Extra space between the top line and this row
500 /// Ignore top_space if true and use the default top space
501 bool top_space_default;
502 /// Extra space between this row and the bottom line
504 /// Ignore bottom_space if true and use the default bottom space
505 bool bottom_space_default;
506 /// Extra space between the bottom line and the next top line
507 Length interline_space;
508 /// Ignore interline_space if true and use the default interline space
509 bool interline_space_default;
510 /// This are for longtabulars only
513 /// a row of endfirsthead
517 /// row of endlastfoot
519 /// row for a newpage
523 typedef std::vector<RowData> row_vector;
531 LyXAlignment alignment;
533 VAlignment valignment;
543 docstring align_special;
546 typedef std::vector<ColumnData> column_vector;
549 idx_type numberofcells;
551 std::vector<row_type> rowofcell;
553 std::vector<col_type> columnofcell;
557 column_vector column_info;
559 mutable cell_vvector cell_info;
567 bool is_long_tabular;
570 /// endfirsthead data
578 void init(Buffer const &, row_type rows_arg,
579 col_type columns_arg);
581 void updateIndexes();
582 /// return true of update is needed
583 bool updateColumnWidths();
585 idx_type columnSpan(idx_type cell) const;
587 BoxType useParbox(idx_type cell) const;
589 // helper function for Latex returns number of newlines
591 int TeXTopHLine(odocstream &, row_type row) const;
593 int TeXBottomHLine(odocstream &, row_type row) const;
595 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
597 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
599 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
601 bool isValidRow(row_type const row) const;
603 int TeXRow(odocstream &, row_type const row,
604 OutputParams const &) const;
606 // helper functions for plain text
608 bool plaintextTopHLine(odocstream &, row_type row,
609 std::vector<unsigned int> const &) const;
611 bool plaintextBottomHLine(odocstream &, row_type row,
612 std::vector<unsigned int> const &) const;
614 void plaintextPrintCell(odocstream &,
615 OutputParams const &,
616 idx_type cell, row_type row, col_type column,
617 std::vector<unsigned int> const &,
618 bool onlydata) const;
619 /// auxiliary function for docbook
620 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
622 /// change associated Buffer
623 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
624 /// retrieve associated Buffer
625 Buffer const & buffer() const { return *buffer_; }
628 Buffer const * buffer_;
634 class InsetTableCell : public InsetText
638 InsetTableCell(Buffer const & buf,
639 Tabular::CellData const * cd, Tabular const * t);
641 InsetCode lyxCode() const { return CELL_CODE; }
643 Inset * clone() { return new InsetTableCell(*this); }
645 virtual bool useEmptyLayout() const { return true; }
647 virtual bool forceEmptyLayout(idx_type = 0) const;
649 virtual bool allowParagraphCustomization(idx_type = 0) const;
651 bool getStatus(Cursor & cur, FuncRequest const & cmd,
652 FuncStatus & status) const;
654 virtual bool neverIndent() { return true; }
656 void setCellData(Tabular::CellData const * cd) { cell_data_ = cd; }
658 void setTabular(Tabular const * t) { table_ = t; }
663 void operator=(InsetTableCell const &);
666 Tabular::CellData const * cell_data_;
668 Tabular const * table_;
672 class InsetTabular : public Inset
676 InsetTabular(Buffer const &, row_type rows = 1,
677 col_type columns = 1);
681 static void string2params(std::string const &, InsetTabular &);
683 static std::string params2string(InsetTabular const &);
687 void write(std::ostream &) const;
689 void metrics(MetricsInfo &, Dimension &) const;
691 void draw(PainterInfo & pi, int x, int y) const;
693 void drawSelection(PainterInfo & pi, int x, int y) const;
695 docstring editMessage() const;
697 EDITABLE editable() const { return HIGHLY_EDITABLE; }
699 bool insetAllowed(InsetCode code) const;
701 bool allowSpellCheck() const { return true; }
703 bool canTrackChanges() const { return true; }
704 /** returns true if, when outputing LaTeX, font changes should
705 be closed before generating this inset. This is needed for
706 insets that may contain several paragraphs */
707 bool noFontChange() const { return true; }
709 DisplayType display() const { return tabular.is_long_tabular ? AlignCenter : Inline; }
711 int latex(odocstream &, OutputParams const &) const;
713 int plaintext(odocstream &, OutputParams const &) const;
715 int docbook(odocstream &, OutputParams const &) const;
717 void validate(LaTeXFeatures & features) const;
719 InsetCode lyxCode() const { return TABULAR_CODE; }
721 docstring contextMenu(BufferView const & bv, int x, int y) const;
722 /// get offset of this cursor slice relative to our upper left corner
723 void cursorPos(BufferView const & bv, CursorSlice const & sl,
724 bool boundary, int & x, int & y) const;
726 bool tabularFeatures(Cursor & cur, std::string const & what);
728 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
729 std::string const & val = std::string());
731 void openLayoutDialog(BufferView *) const;
733 bool showInsetDialog(BufferView *) const;
735 size_t nargs() const { return tabular.numberofcells; }
737 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
739 boost::shared_ptr<InsetTableCell> cell(idx_type);
741 Text * getText(int) const;
743 /// set the change for the entire inset
744 void setChange(Change const & change);
745 /// accept the changes within the inset
746 void acceptChanges(BufferParams const & bparams);
747 /// reject the changes within the inset
748 void rejectChanges(BufferParams const & bparams);
750 // this should return true if we have a "normal" cell, otherwise false.
751 // "normal" means without width set!
752 /// should all paragraphs be output with "Standard" layout?
753 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
755 virtual bool forceEmptyLayout(idx_type cell = 0) const;
757 virtual bool useEmptyLayout() { return true; }
759 void addPreview(graphics::PreviewLoader &) const;
761 /// lock cell with given index
762 void edit(Cursor & cur, bool front, EntryDirection entry_from);
763 /// get table row from x coordinate
764 int rowFromY(Cursor & cur, int y) const;
765 /// get table column from y coordinate
766 int columnFromX(Cursor & cur, int x) const;
768 Inset * editXY(Cursor & cur, int x, int y);
769 /// can we go further down on mouse click?
770 bool descendable() const { return true; }
771 // Update the counters of this inset and of its contents
772 void updateLabels(ParIterator const &);
775 bool completionSupported(Cursor const &) const;
777 bool inlineCompletionSupported(Cursor const & cur) const;
779 bool automaticInlineCompletion() const;
781 bool automaticPopupCompletion() const;
783 bool showCompletionCursor() const;
785 CompletionList const * createCompletionList(Cursor const & cur) const;
787 docstring completionPrefix(Cursor const & cur) const;
789 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
791 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
794 virtual InsetTabular * asInsetTabular() { return this; }
796 virtual InsetTabular const * asInsetTabular() const { return this; }
798 bool isRightToLeft(Cursor & cur) const;
801 // Public structures and variables
803 mutable Tabular tabular;
807 InsetTabular(InsetTabular const &);
809 void doDispatch(Cursor & cur, FuncRequest & cmd);
811 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
813 int scroll() const { return scx_; }
815 Inset * clone() const { return new InsetTabular(*this); }
818 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
819 idx_type cell, bool erased) const;
821 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
824 void moveNextCell(Cursor & cur,
825 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
827 void movePrevCell(Cursor & cur,
828 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
830 int cellXPos(idx_type cell) const;
832 void resetPos(Cursor & cur) const;
834 void removeTabularRow();
836 bool copySelection(Cursor & cur);
838 bool pasteClipboard(Cursor & cur);
840 void cutSelection(Cursor & cur);
842 void getSelection(Cursor & cur, row_type & rs, row_type & re,
843 col_type & cs, col_type & ce) const;
845 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
846 /// are we operating on several cells?
847 bool tablemode(Cursor & cur) const;
849 /// return the "Manhattan distance" to nearest corner
850 int dist(BufferView &, idx_type cell, int x, int y) const;
851 /// return the cell nearest to x, y
852 idx_type getNearestCell(BufferView &, int x, int y) const;
854 /// test the rotation state of the give cell range.
855 bool oneCellHasRotationState(bool rotated,
856 row_type row_start, row_type row_end,
857 col_type col_start, col_type col_end) const;
859 mutable idx_type first_visible_cell;
862 /// true when selecting rows with the mouse
864 /// true when selecting columns with the mouse
868 std::string const featureAsString(Tabular::Feature feature);
872 #endif // INSET_TABULAR_H