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 INSETTABULAR_H
36 #define INSETTABULAR_H
39 #include "InsetText.h"
42 #include "MailInset.h"
44 #include <boost/shared_ptr.hpp>
61 namespace frontend { class Painter; }
69 // A helper struct for tables
138 UNSET_ROTATE_TABULAR,
140 TOGGLE_ROTATE_TABULAR,
187 CELL_BEGIN_OF_MULTICOLUMN,
189 CELL_PART_OF_MULTICOLUMN
197 LYX_VALIGN_BOTTOM = 1,
199 LYX_VALIGN_MIDDLE = 2
215 // we have this header type (is set in the getLT... functions)
217 // double borders on top
219 // double borders on bottom
221 // used for FirstHeader & LastFooter and if this is true
222 // all the rows marked as FirstHeader or LastFooter are
223 // ignored in the output and it is set to be empty!
227 /// type for row numbers
228 typedef size_t row_type;
229 /// type for column numbers
230 typedef size_t col_type;
231 /// type for cell indices
232 typedef size_t idx_type;
233 /// index indicating an invalid position
234 static const idx_type npos = static_cast<idx_type>(-1);
239 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
241 /// Returns true if there is a topline, returns false if not
242 bool topLine(idx_type cell) const;
243 /// Returns true if there is a topline, returns false if not
244 bool bottomLine(idx_type cell) const;
245 /// Returns true if there is a topline, returns false if not
246 bool leftLine(idx_type cell) const;
247 /// Returns true if there is a topline, returns false if not
248 bool rightLine(idx_type cell) const;
251 bool topAlreadyDrawn(idx_type cell) const;
253 bool leftAlreadyDrawn(idx_type cell) const;
255 bool isLastRow(idx_type cell) const;
257 /// return space occupied by the second horizontal line and
258 /// interline space above row \p row in pixels
259 int getAdditionalHeight(row_type row) const;
261 int getAdditionalWidth(idx_type cell) const;
263 /* returns the maximum over all rows */
265 int columnWidth(idx_type cell) const;
271 int rowAscent(row_type row) const;
273 int rowDescent(row_type row) const;
275 void setRowAscent(row_type row, int height);
277 void setRowDescent(row_type row, int height);
279 void setCellWidth(idx_type cell, int new_width);
281 void setAllLines(idx_type cell, bool line);
283 void setTopLine(idx_type cell, bool line);
285 void setBottomLine(idx_type cell, bool line);
287 void setLeftLine(idx_type cell, bool line);
289 void setRightLine(idx_type cell, bool line);
291 bool rowTopLine(row_type row) const;
293 bool rowBottomLine(row_type row) const;
295 bool columnLeftLine(col_type column) const;
297 bool columnRightLine(col_type column) const;
299 void setAlignment(idx_type cell, LyXAlignment align,
300 bool onlycolumn = false);
302 void setVAlignment(idx_type cell, VAlignment align,
303 bool onlycolumn = false);
305 void setColumnPWidth(Cursor &, idx_type, Length const &);
307 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
309 void setAlignSpecial(idx_type cell, docstring const & special,
312 LyXAlignment getAlignment(idx_type cell,
313 bool onlycolumn = false) const;
315 VAlignment getVAlignment(idx_type cell,
316 bool onlycolumn = false) const;
318 Length const getPWidth(idx_type cell) const;
320 Length const getColumnPWidth(idx_type cell) const;
322 Length const getMColumnPWidth(idx_type cell) const;
324 docstring const getAlignSpecial(idx_type cell, int what) const;
326 int cellWidth(idx_type cell) const;
328 int getBeginningOfTextInCell(idx_type cell) const;
330 void appendRow(idx_type cell);
332 void deleteRow(row_type row);
334 void copyRow(row_type);
336 void appendColumn(idx_type cell);
338 void deleteColumn(col_type column);
340 void copyColumn(col_type);
342 bool isFirstCellInRow(idx_type cell) const;
344 idx_type getFirstCellInRow(row_type row) const;
346 bool isLastCellInRow(idx_type cell) const;
348 idx_type getLastCellInRow(row_type row) const;
350 idx_type cellCount() const;
352 idx_type numberOfCellsInRow(idx_type cell) const;
354 void write(std::ostream &) const;
358 int latex(odocstream &, OutputParams const &) const;
360 int docbook(odocstream & os, OutputParams const &) const;
362 void plaintext(odocstream &,
363 OutputParams const & runparams, int const depth,
364 bool onlydata, char_type delim) const;
366 bool isMultiColumn(idx_type cell) const;
368 bool isMultiColumnReal(idx_type cell) const;
370 void setMultiColumn(idx_type cell, idx_type number);
372 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
374 bool isPartOfMultiColumn(row_type row, col_type column) const;
376 row_type cellRow(idx_type cell) const;
378 col_type cellColumn(idx_type cell) const;
380 col_type cellRightColumn(idx_type cell) const;
382 void setBookTabs(bool);
384 bool useBookTabs() const;
386 void setLongTabular(bool);
388 bool isLongTabular() const;
390 void setRotateTabular(bool);
392 bool getRotateTabular() const;
394 void setRotateCell(idx_type cell, bool);
396 bool getRotateCell(idx_type cell) const;
398 bool needRotating() const;
400 bool isLastCell(idx_type cell) const;
402 idx_type cellAbove(idx_type cell) const;
404 idx_type cellBelow(idx_type cell) const;
406 idx_type cellIndex(row_type row, col_type column) const;
408 void setUsebox(idx_type cell, BoxType);
410 BoxType getUsebox(idx_type cell) const;
412 // Long Tabular Options support functions
414 bool checkLTType(row_type row, ltType const &) const;
416 void setLTHead(row_type row, bool flag, ltType const &, bool first);
418 bool getRowOfLTHead(row_type row, ltType &) const;
420 bool getRowOfLTFirstHead(row_type row, ltType &) const;
422 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
424 bool getRowOfLTFoot(row_type row, ltType &) const;
426 bool getRowOfLTLastFoot(row_type row, ltType &) const;
428 void setLTNewPage(row_type row, bool what);
430 bool getLTNewPage(row_type row) const;
432 bool haveLTHead() const;
434 bool haveLTFirstHead() const;
436 bool haveLTFoot() const;
438 bool haveLTLastFoot() const;
440 // end longtable support
442 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
444 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
445 col_type column) const;
447 void setCellInset(row_type row, col_type column,
448 boost::shared_ptr<InsetTableCell>) const;
449 /// Search for \param inset in the tabular, with the
451 idx_type cellFromInset(Inset const * inset) const;
453 row_type rowCount() const { return row_info.size(); }
455 col_type columnCount() const { return column_info.size();}
457 void validate(LaTeXFeatures &) const;
464 CellData(Buffer const &, Tabular const &);
466 CellData(CellData const &);
468 CellData & operator=(CellData);
470 void swap(CellData & rhs);
478 LyXAlignment alignment;
480 VAlignment valignment;
494 docstring align_special;
496 Length p_width; // this is only set for multicolumn!!!
498 boost::shared_ptr<InsetTableCell> inset;
500 CellData & cellInfo(idx_type cell) const;
502 typedef std::vector<CellData> cell_vector;
504 typedef std::vector<cell_vector> cell_vvector;
519 /// Extra space between the top line and this row
521 /// Ignore top_space if true and use the default top space
522 bool top_space_default;
523 /// Extra space between this row and the bottom line
525 /// Ignore bottom_space if true and use the default bottom space
526 bool bottom_space_default;
527 /// Extra space between the bottom line and the next top line
528 Length interline_space;
529 /// Ignore interline_space if true and use the default interline space
530 bool interline_space_default;
531 /// This are for longtabulars only
534 /// a row of endfirsthead
538 /// row of endlastfoot
540 /// row for a newpage
544 typedef std::vector<RowData> row_vector;
552 LyXAlignment alignment;
554 VAlignment valignment;
564 docstring align_special;
567 typedef std::vector<ColumnData> column_vector;
570 idx_type numberofcells;
572 std::vector<row_type> rowofcell;
574 std::vector<col_type> columnofcell;
578 column_vector column_info;
580 mutable cell_vvector cell_info;
588 bool is_long_tabular;
591 /// endfirsthead data
599 void init(Buffer const &, row_type rows_arg,
600 col_type columns_arg);
602 void updateIndexes();
603 /// return true of update is needed
604 bool updateColumnWidths();
606 idx_type columnSpan(idx_type cell) const;
608 BoxType useParbox(idx_type cell) const;
610 // helper function for Latex returns number of newlines
612 int TeXTopHLine(odocstream &, row_type row) const;
614 int TeXBottomHLine(odocstream &, row_type row) const;
616 int TeXCellPreamble(odocstream &, idx_type cell) const;
618 int TeXCellPostamble(odocstream &, idx_type cell) const;
620 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
622 bool isValidRow(row_type const row) const;
624 int TeXRow(odocstream &, row_type const row,
625 OutputParams const &) const;
627 // helper functions for plain text
629 bool plaintextTopHLine(odocstream &, row_type row,
630 std::vector<unsigned int> const &) const;
632 bool plaintextBottomHLine(odocstream &, row_type row,
633 std::vector<unsigned int> const &) const;
635 void plaintextPrintCell(odocstream &,
636 OutputParams const &,
637 idx_type cell, row_type row, col_type column,
638 std::vector<unsigned int> const &,
639 bool onlydata) const;
640 /// auxiliary function for docbook
641 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
643 /// change associated Buffer
644 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
645 /// retrieve associated Buffer
646 Buffer const & buffer() const { return *buffer_; }
649 Buffer const * buffer_;
655 class InsetTableCell : public InsetText {
658 explicit InsetTableCell(Buffer const & buf,
659 Tabular::CellData const * cd, Tabular const * t);
661 virtual 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; }
681 Tabular::CellData const * cell_data_;
683 Tabular const * table_;
687 void operator=(InsetTableCell const &);
691 class InsetTabular : public Inset {
694 InsetTabular(Buffer const &, row_type rows = 1,
695 col_type columns = 1);
701 void write(std::ostream &) const;
703 void metrics(MetricsInfo &, Dimension &) const;
705 void draw(PainterInfo & pi, int x, int y) const;
707 void drawSelection(PainterInfo & pi, int x, int y) const;
709 docstring editMessage() const;
711 EDITABLE editable() const { return HIGHLY_EDITABLE; }
713 bool insetAllowed(InsetCode code) const;
715 bool allowSpellCheck() const { return true; }
717 bool canTrackChanges() const { return true; }
718 /** returns true if, when outputing LaTeX, font changes should
719 be closed before generating this inset. This is needed for
720 insets that may contain several paragraphs */
721 bool noFontChange() const { return true; }
723 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
725 int latex(odocstream &, OutputParams const &) const;
727 int plaintext(odocstream &, OutputParams const &) const;
729 int docbook(odocstream &, OutputParams const &) const;
731 void validate(LaTeXFeatures & features) const;
733 InsetCode lyxCode() const { return TABULAR_CODE; }
734 /// get offset of this cursor slice relative to our upper left corner
735 void cursorPos(BufferView const & bv, CursorSlice const & sl,
736 bool boundary, int & x, int & y) const;
738 bool tabularFeatures(Cursor & cur, std::string const & what);
740 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
741 std::string const & val = std::string());
743 void openLayoutDialog(BufferView *) const;
745 bool showInsetDialog(BufferView *) const;
747 size_t nargs() const { return tabular.cellCount(); }
749 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
751 boost::shared_ptr<InsetTableCell> cell(idx_type);
753 Text * getText(int) const;
755 /// set the change for the entire inset
756 void setChange(Change const & change);
757 /// accept the changes within the inset
758 void acceptChanges(BufferParams const & bparams);
759 /// reject the changes within the inset
760 void rejectChanges(BufferParams const & bparams);
762 // this should return true if we have a "normal" cell, otherwise false.
763 // "normal" means without width set!
764 /// should all paragraphs be output with "Standard" layout?
765 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
767 virtual bool forceEmptyLayout(idx_type cell = 0) const;
769 virtual bool useEmptyLayout() { return true; }
771 void addPreview(graphics::PreviewLoader &) const;
773 /// lock cell with given index
774 void edit(Cursor & cur, bool front, EntryDirection entry_from);
775 /// get table row from x coordinate
776 int rowFromY(Cursor & cur, int y) const;
777 /// get table column from y coordinate
778 int columnFromX(Cursor & cur, int x) const;
780 Inset * editXY(Cursor & cur, int x, int y);
781 /// can we go further down on mouse click?
782 bool descendable() const { return true; }
783 // Update the counters of this inset and of its contents
784 void updateLabels(ParIterator const &);
787 bool completionSupported(Cursor const &) const;
789 bool inlineCompletionSupported(Cursor const & cur) const;
791 bool automaticInlineCompletion() const;
793 bool automaticPopupCompletion() const;
795 bool showCompletionCursor() const;
797 CompletionList const * createCompletionList(Cursor const & cur) const;
799 docstring completionPrefix(Cursor const & cur) const;
801 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
803 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
806 // Public structures and variables
808 mutable Tabular tabular;
812 InsetTabular(InsetTabular const &);
814 void doDispatch(Cursor & cur, FuncRequest & cmd);
816 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
818 int scroll() const { return scx_; }
820 Inset * clone() const { return new InsetTabular(*this); }
823 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
824 idx_type cell, bool erased) const;
826 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
829 void moveNextCell(Cursor & cur);
831 void movePrevCell(Cursor & cur);
833 int cellXPos(idx_type cell) const;
835 void resetPos(Cursor & cur) const;
837 void removeTabularRow();
839 bool copySelection(Cursor & cur);
841 bool pasteClipboard(Cursor & cur);
843 void cutSelection(Cursor & cur);
845 bool isRightToLeft(Cursor & cur) const;
847 void getSelection(Cursor & cur, row_type & rs, row_type & re,
848 col_type & cs, col_type & ce) const;
850 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
851 /// are we operating on several cells?
852 bool tablemode(Cursor & cur) const;
854 /// return the "Manhattan distance" to nearest corner
855 int dist(BufferView &, idx_type cell, int x, int y) const;
856 /// return the cell nearest to x, y
857 idx_type getNearestCell(BufferView &, int x, int y) const;
859 /// test the rotation state of the give cell range.
860 bool oneCellHasRotationState(bool rotated,
861 row_type row_start, row_type row_end,
862 col_type col_start, col_type col_end) const;
864 mutable idx_type first_visible_cell;
870 class InsetTabularMailer : public MailInset {
873 InsetTabularMailer(InsetTabular const & inset);
875 virtual Inset & inset() const { return inset_; }
877 virtual std::string const & name() const { return name_; }
879 virtual std::string const inset2string(Buffer const &) const;
881 static void string2params(std::string const &, InsetTabular &);
883 static std::string const params2string(InsetTabular const &);
886 static std::string const name_;
888 InsetTabular & inset_;
891 std::string const featureAsString(Tabular::Feature feature);