4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Lars Gullik Bjønnes
9 * \author Matthias Ettrich
10 * \author André Pönitz
11 * \author Jürgen Vigna
13 * Full author contact details are available in file CREDITS.
17 // This is Juergen's rewrite of the tabular (table) support.
19 // Things to think of when designing the new tabular support:
20 // - color support (colortbl, color)
21 // - decimal alignment (dcloumn)
22 // - custom lines (hhline)
28 // This is what I have written about tabular support in the LyX3-Tasks file:
30 // o rewrite of table code. Should probably be written as some
31 // kind of an inset. [Done]
32 // o enhance longtable support
36 #ifndef INSETTABULAR_H
37 #define INSETTABULAR_H
40 #include "InsetText.h"
43 #include "MailInset.h"
45 #include <boost/shared_ptr.hpp>
60 namespace frontend { class Painter; }
68 // A helper struct for tables
111 M_TOGGLE_LINE_BOTTOM,
145 UNSET_ROTATE_TABULAR,
147 TOGGLE_ROTATE_TABULAR,
192 CELL_BEGIN_OF_MULTICOLUMN,
194 CELL_PART_OF_MULTICOLUMN
202 LYX_VALIGN_BOTTOM = 1,
204 LYX_VALIGN_MIDDLE = 2
220 // we have this header type (is set in the getLT... functions)
222 // double borders on top
224 // double borders on bottom
226 // used for FirstHeader & LastFooter and if this is true
227 // all the rows marked as FirstHeader or LastFooter are
228 // ignored in the output and it is set to be empty!
232 /// type for row numbers
233 typedef size_t row_type;
234 /// type for column numbers
235 typedef size_t col_type;
236 /// type for cell indices
237 typedef size_t idx_type;
238 /// index indicating an invalid position
239 static const idx_type npos = static_cast<idx_type>(-1);
244 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
246 /// Returns true if there is a topline, returns false if not
247 bool topLine(idx_type cell, bool wholerow = false) const;
248 /// Returns true if there is a topline, returns false if not
249 bool bottomLine(idx_type cell, bool wholerow = false) const;
250 /// Returns true if there is a topline, returns false if not
251 bool leftLine(idx_type cell, bool wholecolumn = false) const;
252 /// Returns true if there is a topline, returns false if not
253 bool rightLine(idx_type cell, bool wholecolumn = false) const;
256 bool topAlreadyDrawn(idx_type cell) const;
258 bool leftAlreadyDrawn(idx_type cell) const;
260 bool isLastRow(idx_type cell) const;
262 /// return space occupied by the second horizontal line and
263 /// interline space above row \p row in pixels
264 int getAdditionalHeight(row_type row) const;
266 int getAdditionalWidth(idx_type cell) const;
268 /* returns the maximum over all rows */
270 int columnWidth(idx_type cell) const;
276 int rowAscent(row_type row) const;
278 int rowDescent(row_type row) const;
280 void setRowAscent(row_type row, int height);
282 void setRowDescent(row_type row, int height);
284 void setCellWidth(idx_type cell, int new_width);
286 void setAllLines(idx_type cell, bool line);
288 void setTopLine(idx_type cell, bool line, bool wholerow = false);
290 void setBottomLine(idx_type cell, bool line, bool wholerow = false);
292 void setLeftLine(idx_type cell, bool line, bool wholecolumn = false);
294 void setRightLine(idx_type cell, bool line, bool wholecolumn = false);
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 cellCount() const;
349 idx_type numberOfCellsInRow(idx_type cell) const;
351 void write(std::ostream &) const;
355 int latex(odocstream &, OutputParams const &) const;
357 int docbook(odocstream & os, OutputParams const &) const;
359 void plaintext(odocstream &,
360 OutputParams const & runparams, int const depth,
361 bool onlydata, char_type delim) const;
363 bool isMultiColumn(idx_type cell) const;
365 bool isMultiColumnReal(idx_type cell) const;
367 void setMultiColumn(idx_type cell, idx_type number);
369 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
371 bool isPartOfMultiColumn(row_type row, col_type column) const;
373 row_type cellRow(idx_type cell) const;
375 col_type cellColumn(idx_type cell) const;
377 col_type cellRightColumn(idx_type cell) const;
379 void setBookTabs(bool);
381 bool useBookTabs() const;
383 void setLongTabular(bool);
385 bool isLongTabular() const;
387 void setRotateTabular(bool);
389 bool getRotateTabular() const;
391 void setRotateCell(idx_type cell, bool);
393 bool getRotateCell(idx_type cell) const;
395 bool needRotating() const;
397 bool isLastCell(idx_type cell) const;
399 idx_type getCellAbove(idx_type cell) const;
401 idx_type getCellBelow(idx_type cell) const;
403 idx_type getLastCellAbove(idx_type cell) const;
405 idx_type getLastCellBelow(idx_type cell) const;
407 idx_type cellIndex(row_type row, col_type column) const;
409 void setUsebox(idx_type cell, BoxType);
411 BoxType getUsebox(idx_type cell) const;
413 // Long Tabular Options support functions
415 bool checkLTType(row_type row, ltType const &) const;
417 void setLTHead(row_type row, bool flag, ltType const &, bool first);
419 bool getRowOfLTHead(row_type row, ltType &) const;
421 bool getRowOfLTFirstHead(row_type row, ltType &) const;
423 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
425 bool getRowOfLTFoot(row_type row, ltType &) const;
427 bool getRowOfLTLastFoot(row_type row, ltType &) const;
429 void setLTNewPage(row_type row, bool what);
431 bool getLTNewPage(row_type row) const;
433 bool haveLTHead() const;
435 bool haveLTFirstHead() const;
437 bool haveLTFoot() const;
439 bool haveLTLastFoot() const;
441 // end longtable support
443 boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
445 boost::shared_ptr<InsetText> getCellInset(row_type row,
446 col_type column) const;
448 void setCellInset(row_type row, col_type column,
449 boost::shared_ptr<InsetText>) const;
450 /// Search for \param inset in the tabular, with the
452 idx_type getCellFromInset(Inset const * inset) const;
454 row_type rowCount() const { return row_info.size(); }
456 col_type columnCount() const { return column_info.size();}
458 void validate(LaTeXFeatures &) const;
465 CellData(Buffer const &);
467 CellData(CellData const &);
469 CellData & operator=(CellData);
471 void swap(CellData & rhs);
479 LyXAlignment alignment;
481 VAlignment valignment;
495 docstring align_special;
497 Length p_width; // this is only set for multicolumn!!!
499 boost::shared_ptr<InsetText> inset;
501 CellData & cellinfo_of_cell(idx_type cell) const;
503 typedef std::vector<CellData> cell_vector;
505 typedef std::vector<cell_vector> cell_vvector;
520 /// Extra space between the top line and this row
522 /// Ignore top_space if true and use the default top space
523 bool top_space_default;
524 /// Extra space between this row and the bottom line
526 /// Ignore bottom_space if true and use the default bottom space
527 bool bottom_space_default;
528 /// Extra space between the bottom line and the next top line
529 Length interline_space;
530 /// Ignore interline_space if true and use the default interline space
531 bool interline_space_default;
532 /// This are for longtabulars only
535 /// a row of endfirsthead
539 /// row of endlastfoot
541 /// row for a newpage
545 typedef std::vector<RowData> row_vector;
553 LyXAlignment alignment;
555 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;
589 bool is_long_tabular;
592 /// endfirsthead data
600 void init(Buffer const &, row_type rows_arg,
601 col_type columns_arg);
603 void updateIndexes();
604 /// Returns true if a complete update is necessary, otherwise false
605 bool setWidthOfMulticolCell(idx_type cell, int new_width);
607 void recalculateMulticolumnsOfColumn(col_type column);
608 /// Returns true if change
609 void calculate_width_of_column(col_type column);
611 bool calculate_width_of_column_NMC(col_type column); // no multi cells
613 idx_type cells_in_multicolumn(idx_type cell) const;
615 BoxType useParbox(idx_type cell) const;
617 // helper function for Latex returns number of newlines
619 int TeXTopHLine(odocstream &, row_type row) const;
621 int TeXBottomHLine(odocstream &, row_type row) const;
623 int TeXCellPreamble(odocstream &, idx_type cell) const;
625 int TeXCellPostamble(odocstream &, idx_type cell) const;
627 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
629 bool isValidRow(row_type const row) const;
631 int TeXRow(odocstream &, row_type const row,
632 OutputParams const &) const;
634 // helper functions for plain text
636 bool plaintextTopHLine(odocstream &, row_type row,
637 std::vector<unsigned int> const &) const;
639 bool plaintextBottomHLine(odocstream &, row_type row,
640 std::vector<unsigned int> const &) const;
642 void plaintextPrintCell(odocstream &,
643 OutputParams const &,
644 idx_type cell, row_type row, col_type column,
645 std::vector<unsigned int> const &,
646 bool onlydata) const;
647 /// auxiliary function for docbook
648 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
650 /// change associated Buffer
651 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
652 /// retrieve associated Buffer
653 Buffer const & buffer() const { return *buffer_; }
656 Buffer const * buffer_;
658 /// renumber cells after structural changes
664 class InsetTabular : public Inset {
667 InsetTabular(Buffer const &, row_type rows = 1,
668 col_type columns = 1);
674 void write(std::ostream &) const;
676 void metrics(MetricsInfo &, Dimension &) const;
678 void draw(PainterInfo & pi, int x, int y) const;
680 void drawSelection(PainterInfo & pi, int x, int y) const;
682 docstring editMessage() const;
684 EDITABLE editable() const { return HIGHLY_EDITABLE; }
686 bool insetAllowed(InsetCode code) const;
688 bool allowSpellCheck() const { return true; }
690 bool canTrackChanges() const { return true; }
691 /** returns true if, when outputing LaTeX, font changes should
692 be closed before generating this inset. This is needed for
693 insets that may contain several paragraphs */
694 bool noFontChange() const { return true; }
696 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
698 int latex(odocstream &, OutputParams const &) const;
700 int plaintext(odocstream &, OutputParams const &) const;
702 int docbook(odocstream &, OutputParams const &) const;
704 void validate(LaTeXFeatures & features) const;
706 InsetCode lyxCode() const { return TABULAR_CODE; }
707 /// get offset of this cursor slice relative to our upper left corner
708 void cursorPos(BufferView const & bv, CursorSlice const & sl,
709 bool boundary, int & x, int & y) const;
711 bool tabularFeatures(Cursor & cur, std::string const & what);
713 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
714 std::string const & val = std::string());
716 void openLayoutDialog(BufferView *) const;
718 bool showInsetDialog(BufferView *) const;
720 size_t nargs() const { return tabular.cellCount(); }
722 boost::shared_ptr<InsetText const> cell(idx_type) const;
724 boost::shared_ptr<InsetText> cell(idx_type);
726 Text * getText(int) const;
728 /// set the change for the entire inset
729 void setChange(Change const & change);
730 /// accept the changes within the inset
731 void acceptChanges(BufferParams const & bparams);
732 /// reject the changes within the inset
733 void rejectChanges(BufferParams const & bparams);
735 // this should return true if we have a "normal" cell, otherwise false.
736 // "normal" means without width set!
737 /// should all paragraphs be output with "Standard" layout?
738 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
740 virtual bool forceEmptyLayout() { return true; }
742 void addPreview(graphics::PreviewLoader &) const;
744 /// lock cell with given index
745 void edit(Cursor & cur, bool front, EntryDirection entry_from);
747 Inset * editXY(Cursor & cur, int x, int y);
748 /// can we go further down on mouse click?
749 bool descendable() const { return true; }
750 // Update the counters of this inset and of its contents
751 void updateLabels(ParIterator const &);
754 // Public structures and variables
756 mutable Tabular tabular;
760 InsetTabular(InsetTabular const &);
762 void doDispatch(Cursor & cur, FuncRequest & cmd);
764 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
766 int scroll() const { return scx_; }
768 Inset * clone() const { return new InsetTabular(*this); }
771 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
772 idx_type cell, bool erased) const;
774 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
777 void moveNextCell(Cursor & cur);
779 void movePrevCell(Cursor & cur);
781 int getCellXPos(idx_type cell) const;
783 void resetPos(Cursor & cur) const;
785 void removeTabularRow();
787 bool copySelection(Cursor & cur);
789 bool pasteClipboard(Cursor & cur);
791 void cutSelection(Cursor & cur);
793 bool isRightToLeft(Cursor & cur) const;
795 void getSelection(Cursor & cur, row_type & rs, row_type & re,
796 col_type & cs, col_type & ce) const;
798 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
799 /// are we operating on several cells?
800 bool tablemode(Cursor & cur) const;
802 /// return the "Manhattan distance" to nearest corner
803 int dist(BufferView &, idx_type cell, int x, int y) const;
804 /// return the cell nearest to x, y
805 idx_type getNearestCell(BufferView &, int x, int y) const;
807 /// test the rotation state of the give cell range.
808 bool oneCellHasRotationState(bool rotated,
809 row_type row_start, row_type row_end,
810 col_type col_start, col_type col_end) const;
812 mutable idx_type first_visible_cell;
818 class InsetTabularMailer : public MailInset {
821 InsetTabularMailer(InsetTabular const & inset);
823 virtual Inset & inset() const { return inset_; }
825 virtual std::string const & name() const { return name_; }
827 virtual std::string const inset2string(Buffer const &) const;
829 static void string2params(std::string const &, InsetTabular &);
831 static std::string const params2string(InsetTabular const &);
834 static std::string const name_;
836 InsetTabular & inset_;
839 std::string const featureAsString(Tabular::Feature feature);