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,
183 TABULAR_VALIGN_MIDDLE,
185 TABULAR_VALIGN_BOTTOM,
194 CELL_BEGIN_OF_MULTICOLUMN,
196 CELL_PART_OF_MULTICOLUMN
204 LYX_VALIGN_BOTTOM = 1,
206 LYX_VALIGN_MIDDLE = 2
222 // we have this header type (is set in the getLT... functions)
224 // double borders on top
226 // double borders on bottom
228 // used for FirstHeader & LastFooter and if this is true
229 // all the rows marked as FirstHeader or LastFooter are
230 // ignored in the output and it is set to be empty!
234 /// type for row numbers
235 typedef size_t row_type;
236 /// type for column numbers
237 typedef size_t col_type;
238 /// type for cell indices
239 typedef size_t idx_type;
240 /// index indicating an invalid position
241 static const idx_type npos = static_cast<idx_type>(-1);
244 Tabular(Buffer &, 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) const;
248 /// Returns true if there is a topline, returns false if not
249 bool bottomLine(idx_type cell) const;
250 /// Returns true if there is a topline, returns false if not
251 bool leftLine(idx_type cell) const;
252 /// Returns true if there is a topline, returns false if not
253 bool rightLine(idx_type cell) 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);
290 void setBottomLine(idx_type cell, bool line);
292 void setLeftLine(idx_type cell, bool line);
294 void setRightLine(idx_type cell, bool line);
296 bool rowTopLine(row_type row) const;
298 bool rowBottomLine(row_type row) const;
300 bool columnLeftLine(col_type column) const;
302 bool columnRightLine(col_type column) const;
304 void setAlignment(idx_type cell, LyXAlignment align,
305 bool onlycolumn = false);
307 void setVAlignment(idx_type cell, VAlignment align,
308 bool onlycolumn = false);
310 void setColumnPWidth(Cursor &, idx_type, Length const &);
312 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
314 void setAlignSpecial(idx_type cell, docstring const & special,
317 LyXAlignment getAlignment(idx_type cell,
318 bool onlycolumn = false) const;
320 VAlignment getVAlignment(idx_type cell,
321 bool onlycolumn = false) const;
323 Length const getPWidth(idx_type cell) 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 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 setRotateCell(idx_type cell, bool);
381 bool getRotateCell(idx_type cell) const;
383 bool needRotating() const;
385 bool isLastCell(idx_type cell) const;
387 idx_type cellAbove(idx_type cell) const;
389 idx_type cellBelow(idx_type cell) const;
391 idx_type cellIndex(row_type row, col_type column) const;
393 void setUsebox(idx_type cell, BoxType);
395 BoxType getUsebox(idx_type cell) const;
397 // Long Tabular Options support functions
399 bool checkLTType(row_type row, ltType const &) const;
401 void setLTHead(row_type row, bool flag, ltType const &, bool first);
403 bool getRowOfLTHead(row_type row, ltType &) const;
405 bool getRowOfLTFirstHead(row_type row, ltType &) const;
407 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
409 bool getRowOfLTFoot(row_type row, ltType &) const;
411 bool getRowOfLTLastFoot(row_type row, ltType &) const;
413 void setLTNewPage(row_type row, bool what);
415 bool getLTNewPage(row_type row) const;
417 idx_type setLTCaption(row_type row, bool what);
419 bool ltCaption(row_type row) const;
421 bool haveLTHead() const;
423 bool haveLTFirstHead() const;
425 bool haveLTFoot() const;
427 bool haveLTLastFoot() const;
429 // end longtable support
431 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
433 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
434 col_type column) const;
436 void setCellInset(row_type row, col_type column,
437 boost::shared_ptr<InsetTableCell>) const;
438 /// Search for \param inset in the tabular, with the
440 void validate(LaTeXFeatures &) const;
442 // FIXME Now that cells have an InsetTableCell as their insets, rather
443 // than an InsetText, it'd be possible to reverse the relationship here,
444 // so that cell_vector was a vector<InsetTableCell> rather than a
445 // vector<CellData>, and an InsetTableCell had a CellData as a member,
446 // or perhaps just had its members as members.
453 CellData(CellData const &);
455 CellData & operator=(CellData);
457 void swap(CellData & rhs);
465 LyXAlignment alignment;
467 VAlignment valignment;
481 docstring align_special;
483 Length p_width; // this is only set for multicolumn!!!
485 boost::shared_ptr<InsetTableCell> inset;
487 CellData & cellInfo(idx_type cell) const;
489 typedef std::vector<CellData> cell_vector;
491 typedef std::vector<cell_vector> cell_vvector;
502 /// Extra space between the top line and this row
504 /// Ignore top_space if true and use the default top space
505 bool top_space_default;
506 /// Extra space between this row and the bottom line
508 /// Ignore bottom_space if true and use the default bottom space
509 bool bottom_space_default;
510 /// Extra space between the bottom line and the next top line
511 Length interline_space;
512 /// Ignore interline_space if true and use the default interline space
513 bool interline_space_default;
514 /// This are for longtabulars only
517 /// a row of endfirsthead
521 /// row of endlastfoot
523 /// row for a newpage
529 typedef std::vector<RowData> row_vector;
537 LyXAlignment alignment;
539 VAlignment valignment;
545 docstring align_special;
548 typedef std::vector<ColumnData> column_vector;
551 idx_type numberofcells;
553 std::vector<row_type> rowofcell;
555 std::vector<col_type> columnofcell;
559 column_vector column_info;
561 mutable cell_vvector cell_info;
567 VAlignment tabular_valignment;
571 bool is_long_tabular;
574 /// endfirsthead data
582 void init(Buffer &, row_type rows_arg,
583 col_type columns_arg);
585 void updateIndexes();
587 bool setFixedWidth(row_type r, col_type c);
589 void updateContentAlignment(row_type r, col_type c);
590 /// return true of update is needed
591 bool updateColumnWidths();
593 idx_type columnSpan(idx_type cell) const;
595 BoxType useParbox(idx_type cell) const;
597 // helper function for Latex returns number of newlines
599 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
601 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
603 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
605 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
607 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
609 bool isValidRow(row_type const row) const;
611 int TeXRow(odocstream &, row_type const row,
612 OutputParams const &) const;
614 // helper functions for plain text
616 bool plaintextTopHLine(odocstream &, row_type row,
617 std::vector<unsigned int> const &) const;
619 bool plaintextBottomHLine(odocstream &, row_type row,
620 std::vector<unsigned int> const &) const;
622 void plaintextPrintCell(odocstream &,
623 OutputParams const &,
624 idx_type cell, row_type row, col_type column,
625 std::vector<unsigned int> const &,
626 bool onlydata) const;
627 /// auxiliary function for docbook
628 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
630 /// change associated Buffer
631 void setBuffer(Buffer & buffer);
632 /// retrieve associated Buffer
633 Buffer & buffer() const { return *buffer_; }
642 class InsetTableCell : public InsetText
646 InsetTableCell(Buffer & buf);
648 InsetCode lyxCode() const { return CELL_CODE; }
650 Inset * clone() { return new InsetTableCell(*this); }
652 bool getStatus(Cursor & cur, FuncRequest const & cmd,
653 FuncStatus & status) const;
655 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
657 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
658 /// writes the contents of the cell as a string, optionally
659 /// descending into insets
660 docstring asString(bool intoInsets = true);
665 void operator=(InsetTableCell const &);
667 // This boolean is supposed to track whether the cell has had its
668 // width explicitly set. We need to know this to determine whether
669 // layout changes and paragraph customization are allowed---that is,
670 // we need it in forcePlainLayout() and allowParagraphCustomization().
671 // Unfortunately, that information is not readily available in
672 // InsetTableCell. In the case of multicolumn cells, it is present
673 // in CellData, and so would be available here if CellData were to
674 // become a member of InsetTableCell. But in the other case, it isn't
675 // even available there, but is held in Tabular::ColumnData.
676 // So, the present solution uses this boolean to track the information
677 // we need to track, and tries to keep it updated. This is not ideal,
678 // but the other solutions are no better. These are:
679 // (i) Keep a pointer in InsetTableCell to the table;
680 // (ii) Find the table by iterating over the Buffer's insets.
681 // Solution (i) raises the problem of updating the pointer when an
682 // InsetTableCell is copied, and we'd therefore need a copy constructor
683 // in InsetTabular and then in Tabular, which seems messy, given how
684 // complicated those classes are. Solution (ii) involves a lot of
685 // iterating, since this information is needed quite often, and so may
687 // So, well, if someone can do better, please do!
691 // FIXME: Here the thoughts from the comment above also apply.
693 LyXAlignment contentAlign;
694 /// should paragraph indendation be omitted in any case?
695 bool neverIndent() const { return true; }
697 LyXAlignment contentAlignment() const { return contentAlign; }
699 virtual bool usePlainLayout() const { return true; }
701 virtual bool forcePlainLayout(idx_type = 0) const;
703 virtual bool allowParagraphCustomization(idx_type = 0) const;
704 /// Is the width forced to some value?
705 bool hasFixedWidth() const { return isFixedWidth; }
709 class InsetTabular : public Inset
713 InsetTabular(Buffer &, row_type rows = 1,
714 col_type columns = 1);
718 void setBuffer(Buffer & buffer);
721 static void string2params(std::string const &, InsetTabular &);
723 static std::string params2string(InsetTabular const &);
727 void write(std::ostream &) const;
729 void metrics(MetricsInfo &, Dimension &) const;
731 void draw(PainterInfo & pi, int x, int y) const;
733 void drawSelection(PainterInfo & pi, int x, int y) const;
735 docstring editMessage() const;
737 EDITABLE editable() const { return HIGHLY_EDITABLE; }
739 bool insetAllowed(InsetCode code) const;
741 bool allowSpellCheck() const { return true; }
743 bool canTrackChanges() const { return true; }
744 /** returns true if, when outputing LaTeX, font changes should
745 be closed before generating this inset. This is needed for
746 insets that may contain several paragraphs */
747 bool noFontChange() const { return true; }
749 DisplayType display() const { return tabular.is_long_tabular ? AlignCenter : Inline; }
751 int latex(odocstream &, OutputParams const &) const;
753 int plaintext(odocstream &, OutputParams const &) const;
755 int docbook(odocstream &, OutputParams const &) const;
757 void validate(LaTeXFeatures & features) const;
759 InsetCode lyxCode() const { return TABULAR_CODE; }
761 docstring contextMenu(BufferView const & bv, int x, int y) const;
762 /// get offset of this cursor slice relative to our upper left corner
763 void cursorPos(BufferView const & bv, CursorSlice const & sl,
764 bool boundary, int & x, int & y) const;
766 bool tabularFeatures(Cursor & cur, std::string const & what);
768 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
769 std::string const & val = std::string());
771 void openLayoutDialog(BufferView *) const;
773 bool showInsetDialog(BufferView *) const;
775 size_t nargs() const { return tabular.numberofcells; }
777 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
779 boost::shared_ptr<InsetTableCell> cell(idx_type);
781 Text * getText(int) const;
783 /// set the change for the entire inset
784 void setChange(Change const & change);
785 /// accept the changes within the inset
786 void acceptChanges(BufferParams const & bparams);
787 /// reject the changes within the inset
788 void rejectChanges(BufferParams const & bparams);
790 // this should return true if we have a "normal" cell, otherwise false.
791 // "normal" means without width set!
792 /// should all paragraphs be output with "Standard" layout?
793 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
795 virtual bool forcePlainLayout(idx_type cell = 0) const;
797 virtual bool usePlainLayout() { return true; }
799 void addPreview(graphics::PreviewLoader &) const;
801 /// lock cell with given index
802 void edit(Cursor & cur, bool front, EntryDirection entry_from);
803 /// get table row from x coordinate
804 int rowFromY(Cursor & cur, int y) const;
805 /// get table column from y coordinate
806 int columnFromX(Cursor & cur, int x) const;
808 Inset * editXY(Cursor & cur, int x, int y);
809 /// can we go further down on mouse click?
810 bool descendable() const { return true; }
811 // Update the counters of this inset and of its contents
812 void updateLabels(ParIterator const &);
815 bool completionSupported(Cursor const &) const;
817 bool inlineCompletionSupported(Cursor const & cur) const;
819 bool automaticInlineCompletion() const;
821 bool automaticPopupCompletion() const;
823 bool showCompletionCursor() const;
825 CompletionList const * createCompletionList(Cursor const & cur) const;
827 docstring completionPrefix(Cursor const & cur) const;
829 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
831 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
833 virtual bool usePlainLayout() const { return true; }
836 virtual InsetTabular * asInsetTabular() { return this; }
838 virtual InsetTabular const * asInsetTabular() const { return this; }
840 bool isRightToLeft(Cursor & cur) const;
841 /// writes the cells between stidx and enidx as a string, optionally
842 /// descending into the insets
843 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
845 /// Returns whether the cell in the specified row and column is selected.
846 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
848 // Public structures and variables
850 mutable Tabular tabular;
854 InsetTabular(InsetTabular const &);
856 void doDispatch(Cursor & cur, FuncRequest & cmd);
858 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
860 int scroll() const { return scx_; }
862 Inset * clone() const { return new InsetTabular(*this); }
865 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
866 idx_type cell, bool erased) const;
868 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
871 void moveNextCell(Cursor & cur,
872 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
874 void movePrevCell(Cursor & cur,
875 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
877 int cellXPos(idx_type cell) const;
879 void resetPos(Cursor & cur) const;
881 void removeTabularRow();
883 bool copySelection(Cursor & cur);
885 bool pasteClipboard(Cursor & cur);
887 void cutSelection(Cursor & cur);
889 void getSelection(Cursor & cur, row_type & rs, row_type & re,
890 col_type & cs, col_type & ce) const;
892 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
894 /// return the "Manhattan distance" to nearest corner
895 int dist(BufferView &, idx_type cell, int x, int y) const;
896 /// return the cell nearest to x, y
897 idx_type getNearestCell(BufferView &, int x, int y) const;
899 /// test the rotation state of the give cell range.
900 bool oneCellHasRotationState(bool rotated,
901 row_type row_start, row_type row_end,
902 col_type col_start, col_type col_end) const;
904 mutable idx_type first_visible_cell;
907 /// true when selecting rows with the mouse
909 /// true when selecting columns with the mouse
913 std::string const featureAsString(Tabular::Feature feature);
917 #endif // INSET_TABULAR_H