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 bool haveLTCaption() const;
431 // end longtable support
433 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
435 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
436 col_type column) const;
438 void setCellInset(row_type row, col_type column,
439 boost::shared_ptr<InsetTableCell>) const;
440 /// Search for \param inset in the tabular, with the
442 void validate(LaTeXFeatures &) const;
444 // FIXME Now that cells have an InsetTableCell as their insets, rather
445 // than an InsetText, it'd be possible to reverse the relationship here,
446 // so that cell_vector was a vector<InsetTableCell> rather than a
447 // vector<CellData>, and an InsetTableCell had a CellData as a member,
448 // or perhaps just had its members as members.
455 CellData(CellData const &);
457 CellData & operator=(CellData);
459 void swap(CellData & rhs);
467 LyXAlignment alignment;
469 VAlignment valignment;
483 docstring align_special;
485 Length p_width; // this is only set for multicolumn!!!
487 boost::shared_ptr<InsetTableCell> inset;
489 CellData & cellInfo(idx_type cell) const;
491 typedef std::vector<CellData> cell_vector;
493 typedef std::vector<cell_vector> cell_vvector;
504 /// Extra space between the top line and this row
506 /// Ignore top_space if true and use the default top space
507 bool top_space_default;
508 /// Extra space between this row and the bottom line
510 /// Ignore bottom_space if true and use the default bottom space
511 bool bottom_space_default;
512 /// Extra space between the bottom line and the next top line
513 Length interline_space;
514 /// Ignore interline_space if true and use the default interline space
515 bool interline_space_default;
516 /// This are for longtabulars only
519 /// a row of endfirsthead
523 /// row of endlastfoot
525 /// row for a newpage
531 typedef std::vector<RowData> row_vector;
539 LyXAlignment alignment;
541 VAlignment valignment;
547 docstring align_special;
550 typedef std::vector<ColumnData> column_vector;
553 idx_type numberofcells;
555 std::vector<row_type> rowofcell;
557 std::vector<col_type> columnofcell;
561 column_vector column_info;
563 mutable cell_vvector cell_info;
569 VAlignment tabular_valignment;
573 bool is_long_tabular;
576 /// endfirsthead data
584 void init(Buffer &, row_type rows_arg,
585 col_type columns_arg);
587 void updateIndexes();
589 bool setFixedWidth(row_type r, col_type c);
591 void updateContentAlignment(row_type r, col_type c);
592 /// return true of update is needed
593 bool updateColumnWidths();
595 idx_type columnSpan(idx_type cell) const;
597 BoxType useParbox(idx_type cell) const;
599 // helper function for Latex returns number of newlines
601 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
603 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
605 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
607 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
609 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
611 bool isValidRow(row_type const row) const;
613 int TeXRow(odocstream &, row_type const row,
614 OutputParams const &) const;
616 // helper functions for plain text
618 bool plaintextTopHLine(odocstream &, row_type row,
619 std::vector<unsigned int> const &) const;
621 bool plaintextBottomHLine(odocstream &, row_type row,
622 std::vector<unsigned int> const &) const;
624 void plaintextPrintCell(odocstream &,
625 OutputParams const &,
626 idx_type cell, row_type row, col_type column,
627 std::vector<unsigned int> const &,
628 bool onlydata) const;
629 /// auxiliary function for docbook
630 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
632 /// change associated Buffer
633 void setBuffer(Buffer & buffer);
634 /// retrieve associated Buffer
635 Buffer & buffer() const { return *buffer_; }
644 class InsetTableCell : public InsetText
648 InsetTableCell(Buffer & buf);
650 InsetCode lyxCode() const { return CELL_CODE; }
652 Inset * clone() { return new InsetTableCell(*this); }
654 bool getStatus(Cursor & cur, FuncRequest const & cmd,
655 FuncStatus & status) const;
657 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
659 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
660 /// writes the contents of the cell as a string, optionally
661 /// descending into insets
662 docstring asString(bool intoInsets = true);
667 void operator=(InsetTableCell const &);
669 // This boolean is supposed to track whether the cell has had its
670 // width explicitly set. We need to know this to determine whether
671 // layout changes and paragraph customization are allowed---that is,
672 // we need it in forcePlainLayout() and allowParagraphCustomization().
673 // Unfortunately, that information is not readily available in
674 // InsetTableCell. In the case of multicolumn cells, it is present
675 // in CellData, and so would be available here if CellData were to
676 // become a member of InsetTableCell. But in the other case, it isn't
677 // even available there, but is held in Tabular::ColumnData.
678 // So, the present solution uses this boolean to track the information
679 // we need to track, and tries to keep it updated. This is not ideal,
680 // but the other solutions are no better. These are:
681 // (i) Keep a pointer in InsetTableCell to the table;
682 // (ii) Find the table by iterating over the Buffer's insets.
683 // Solution (i) raises the problem of updating the pointer when an
684 // InsetTableCell is copied, and we'd therefore need a copy constructor
685 // in InsetTabular and then in Tabular, which seems messy, given how
686 // complicated those classes are. Solution (ii) involves a lot of
687 // iterating, since this information is needed quite often, and so may
689 // So, well, if someone can do better, please do!
693 // FIXME: Here the thoughts from the comment above also apply.
695 LyXAlignment contentAlign;
696 /// should paragraph indendation be omitted in any case?
697 bool neverIndent() const { return true; }
699 LyXAlignment contentAlignment() const { return contentAlign; }
701 virtual bool usePlainLayout() const { return true; }
703 virtual bool forcePlainLayout(idx_type = 0) const;
705 virtual bool allowParagraphCustomization(idx_type = 0) const;
706 /// Is the width forced to some value?
707 bool hasFixedWidth() const { return isFixedWidth; }
711 class InsetTabular : public Inset
715 InsetTabular(Buffer &, row_type rows = 1,
716 col_type columns = 1);
720 void setBuffer(Buffer & buffer);
723 static void string2params(std::string const &, InsetTabular &);
725 static std::string params2string(InsetTabular const &);
729 void write(std::ostream &) const;
731 void metrics(MetricsInfo &, Dimension &) const;
733 void draw(PainterInfo & pi, int x, int y) const;
735 void drawSelection(PainterInfo & pi, int x, int y) const;
737 docstring editMessage() const;
739 bool editable() const { return true; }
741 bool hasSettings() const { return true; }
743 bool insetAllowed(InsetCode code) const;
745 bool allowSpellCheck() const { return true; }
747 bool canTrackChanges() const { return true; }
748 /** returns true if, when outputing LaTeX, font changes should
749 be closed before generating this inset. This is needed for
750 insets that may contain several paragraphs */
751 bool noFontChange() const { return true; }
753 DisplayType display() const { return tabular.is_long_tabular ? AlignCenter : Inline; }
755 int latex(odocstream &, OutputParams const &) const;
757 int plaintext(odocstream &, OutputParams const &) const;
759 int docbook(odocstream &, OutputParams const &) const;
761 void validate(LaTeXFeatures & features) const;
763 InsetCode lyxCode() const { return TABULAR_CODE; }
765 docstring contextMenu(BufferView const & bv, int x, int y) const;
766 /// get offset of this cursor slice relative to our upper left corner
767 void cursorPos(BufferView const & bv, CursorSlice const & sl,
768 bool boundary, int & x, int & y) const;
770 bool tabularFeatures(Cursor & cur, std::string const & what);
772 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
773 std::string const & val = std::string());
775 void openLayoutDialog(BufferView *) const;
777 bool showInsetDialog(BufferView *) const;
779 size_t nargs() const { return tabular.numberofcells; }
781 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
783 boost::shared_ptr<InsetTableCell> cell(idx_type);
785 Text * getText(int) const;
787 /// set the change for the entire inset
788 void setChange(Change const & change);
789 /// accept the changes within the inset
790 void acceptChanges(BufferParams const & bparams);
791 /// reject the changes within the inset
792 void rejectChanges(BufferParams const & bparams);
794 // this should return true if we have a "normal" cell, otherwise false.
795 // "normal" means without width set!
796 /// should all paragraphs be output with "Standard" layout?
797 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
799 virtual bool forcePlainLayout(idx_type cell = 0) const;
801 virtual bool usePlainLayout() { return true; }
803 void addPreview(graphics::PreviewLoader &) const;
805 /// lock cell with given index
806 void edit(Cursor & cur, bool front, EntryDirection entry_from);
807 /// get table row from x coordinate
808 int rowFromY(Cursor & cur, int y) const;
809 /// get table column from y coordinate
810 int columnFromX(Cursor & cur, int x) const;
812 Inset * editXY(Cursor & cur, int x, int y);
813 /// can we go further down on mouse click?
814 bool descendable() const { return true; }
815 // Update the counters of this inset and of its contents
816 void updateLabels(ParIterator const &);
819 bool completionSupported(Cursor const &) const;
821 bool inlineCompletionSupported(Cursor const & cur) const;
823 bool automaticInlineCompletion() const;
825 bool automaticPopupCompletion() const;
827 bool showCompletionCursor() const;
829 CompletionList const * createCompletionList(Cursor const & cur) const;
831 docstring completionPrefix(Cursor const & cur) const;
833 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
835 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
837 virtual bool usePlainLayout() const { return true; }
840 virtual InsetTabular * asInsetTabular() { return this; }
842 virtual InsetTabular const * asInsetTabular() const { return this; }
844 bool isRightToLeft(Cursor & cur) const;
845 /// writes the cells between stidx and enidx as a string, optionally
846 /// descending into the insets
847 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
849 /// Returns whether the cell in the specified row and column is selected.
850 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
852 // Public structures and variables
854 mutable Tabular tabular;
858 InsetTabular(InsetTabular const &);
860 void doDispatch(Cursor & cur, FuncRequest & cmd);
862 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
864 int scroll() const { return scx_; }
866 Inset * clone() const { return new InsetTabular(*this); }
869 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
870 idx_type cell, Change const & change) const;
872 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
875 void moveNextCell(Cursor & cur,
876 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
878 void movePrevCell(Cursor & cur,
879 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
881 int cellXPos(idx_type cell) const;
883 void resetPos(Cursor & cur) const;
885 void removeTabularRow();
887 bool copySelection(Cursor & cur);
889 bool pasteClipboard(Cursor & cur);
891 void cutSelection(Cursor & cur);
893 void getSelection(Cursor & cur, row_type & rs, row_type & re,
894 col_type & cs, col_type & ce) const;
896 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
898 /// return the "Manhattan distance" to nearest corner
899 int dist(BufferView &, idx_type cell, int x, int y) const;
900 /// return the cell nearest to x, y
901 idx_type getNearestCell(BufferView &, int x, int y) const;
903 /// test the rotation state of the give cell range.
904 bool oneCellHasRotationState(bool rotated,
905 row_type row_start, row_type row_end,
906 col_type col_start, col_type col_end) const;
908 mutable idx_type first_visible_cell;
911 /// true when selecting rows with the mouse
913 /// true when selecting columns with the mouse
917 std::string const featureAsString(Tabular::Feature feature);
921 #endif // INSET_TABULAR_H