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>
59 namespace frontend { class Painter; }
67 // A helper struct for tables
136 UNSET_ROTATE_TABULAR,
138 TOGGLE_ROTATE_TABULAR,
182 TABULAR_VALIGN_MIDDLE,
184 TABULAR_VALIGN_BOTTOM,
186 LONGTABULAR_ALIGN_LEFT,
188 LONGTABULAR_ALIGN_CENTER,
190 LONGTABULAR_ALIGN_RIGHT,
199 CELL_BEGIN_OF_MULTICOLUMN,
201 CELL_PART_OF_MULTICOLUMN
209 LYX_VALIGN_MIDDLE = 1,
211 LYX_VALIGN_BOTTOM = 2
217 LYX_LONGTABULAR_ALIGN_LEFT = 0,
219 LYX_LONGTABULAR_ALIGN_CENTER = 1,
221 LYX_LONGTABULAR_ALIGN_RIGHT = 2
237 // we have this header type (is set in the getLT... functions)
239 // double borders on top
241 // double borders on bottom
243 // used for FirstHeader & LastFooter and if this is true
244 // all the rows marked as FirstHeader or LastFooter are
245 // ignored in the output and it is set to be empty!
249 /// type for row numbers
250 typedef size_t row_type;
251 /// type for column numbers
252 typedef size_t col_type;
253 /// type for cell indices
254 typedef size_t idx_type;
255 /// index indicating an invalid position
256 static const idx_type npos = static_cast<idx_type>(-1);
259 Tabular(Buffer &, col_type columns_arg, row_type rows_arg);
261 /// Returns true if there is a topline, returns false if not
262 bool topLine(idx_type cell) const;
263 /// Returns true if there is a topline, returns false if not
264 bool bottomLine(idx_type cell) const;
265 /// Returns true if there is a topline, returns false if not
266 bool leftLine(idx_type cell) const;
267 /// Returns true if there is a topline, returns false if not
268 bool rightLine(idx_type cell) const;
271 bool topAlreadyDrawn(idx_type cell) const;
273 bool leftAlreadyDrawn(idx_type cell) const;
275 bool isLastRow(idx_type cell) const;
277 /// return space occupied by the second horizontal line and
278 /// interline space above row \p row in pixels
279 int getAdditionalHeight(row_type row) const;
281 int getAdditionalWidth(idx_type cell) const;
283 /* returns the maximum over all rows */
285 int columnWidth(idx_type cell) const;
291 int rowAscent(row_type row) const;
293 int rowDescent(row_type row) const;
295 void setRowAscent(row_type row, int height);
297 void setRowDescent(row_type row, int height);
299 void setCellWidth(idx_type cell, int new_width);
301 void setAllLines(idx_type cell, bool line);
303 void setTopLine(idx_type cell, bool line);
305 void setBottomLine(idx_type cell, bool line);
307 void setLeftLine(idx_type cell, bool line);
309 void setRightLine(idx_type cell, bool line);
311 bool rowTopLine(row_type row) const;
313 bool rowBottomLine(row_type row) const;
315 bool columnLeftLine(col_type column) const;
317 bool columnRightLine(col_type column) const;
319 void setAlignment(idx_type cell, LyXAlignment align,
320 bool onlycolumn = false);
322 void setVAlignment(idx_type cell, VAlignment align,
323 bool onlycolumn = false);
325 void setColumnPWidth(Cursor &, idx_type, Length const &);
327 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
329 void setAlignSpecial(idx_type cell, docstring const & special,
332 LyXAlignment getAlignment(idx_type cell,
333 bool onlycolumn = false) const;
335 VAlignment getVAlignment(idx_type cell,
336 bool onlycolumn = false) const;
338 Length const getPWidth(idx_type cell) const;
340 int cellWidth(idx_type cell) const;
342 int getBeginningOfTextInCell(idx_type cell) const;
344 void appendRow(idx_type cell);
346 void deleteRow(row_type row);
348 void copyRow(row_type);
350 void appendColumn(idx_type cell);
352 void deleteColumn(col_type column);
354 void copyColumn(col_type);
356 bool isFirstCellInRow(idx_type cell) const;
358 idx_type getFirstCellInRow(row_type row) const;
360 bool isLastCellInRow(idx_type cell) const;
362 idx_type getLastCellInRow(row_type row) const;
364 idx_type numberOfCellsInRow(idx_type cell) const;
366 void write(std::ostream &) const;
370 int latex(odocstream &, OutputParams const &) const;
372 int docbook(odocstream & os, OutputParams const &) const;
374 void plaintext(odocstream &,
375 OutputParams const & runparams, int const depth,
376 bool onlydata, char_type delim) const;
378 bool isMultiColumn(idx_type cell) const;
380 bool isMultiColumnReal(idx_type cell) const;
382 void setMultiColumn(idx_type cell, idx_type number);
384 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
386 bool isPartOfMultiColumn(row_type row, col_type column) const;
388 row_type cellRow(idx_type cell) const;
390 col_type cellColumn(idx_type cell) const;
392 col_type cellRightColumn(idx_type cell) 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 idx_type setLTCaption(row_type row, bool what);
434 bool ltCaption(row_type row) const;
436 bool haveLTHead() const;
438 bool haveLTFirstHead() const;
440 bool haveLTFoot() const;
442 bool haveLTLastFoot() const;
444 bool haveLTCaption() const;
446 // end longtable support
448 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
450 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
451 col_type column) const;
453 void setCellInset(row_type row, col_type column,
454 boost::shared_ptr<InsetTableCell>) const;
455 /// Search for \param inset in the tabular, with the
457 void validate(LaTeXFeatures &) const;
459 // FIXME Now that cells have an InsetTableCell as their insets, rather
460 // than an InsetText, it'd be possible to reverse the relationship here,
461 // so that cell_vector was a vector<InsetTableCell> rather than a
462 // vector<CellData>, and an InsetTableCell had a CellData as a member,
463 // or perhaps just had its members as members.
470 CellData(CellData const &);
472 CellData & operator=(CellData);
474 void swap(CellData & rhs);
482 LyXAlignment alignment;
484 VAlignment valignment;
498 docstring align_special;
500 Length p_width; // this is only set for multicolumn!!!
502 boost::shared_ptr<InsetTableCell> inset;
504 CellData & cellInfo(idx_type cell) const;
506 typedef std::vector<CellData> cell_vector;
508 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
546 typedef std::vector<RowData> row_vector;
554 LyXAlignment alignment;
556 VAlignment valignment;
562 docstring align_special;
565 typedef std::vector<ColumnData> column_vector;
568 idx_type numberofcells;
570 std::vector<row_type> rowofcell;
572 std::vector<col_type> columnofcell;
576 column_vector column_info;
578 mutable cell_vvector cell_info;
584 VAlignment tabular_valignment;
588 HAlignment longtabular_alignment;
590 bool is_long_tabular;
593 /// endfirsthead data
601 void init(Buffer &, row_type rows_arg,
602 col_type columns_arg);
604 void updateIndexes();
606 bool setFixedWidth(row_type r, col_type c);
608 void updateContentAlignment(row_type r, col_type c);
609 /// return true of update is needed
610 bool updateColumnWidths();
612 idx_type columnSpan(idx_type cell) const;
614 BoxType useParbox(idx_type cell) const;
616 // helper function for Latex returns number of newlines
618 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
620 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
622 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
624 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
626 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
628 bool isValidRow(row_type const row) const;
630 int TeXRow(odocstream &, row_type const row,
631 OutputParams const &) const;
633 // helper functions for plain text
635 bool plaintextTopHLine(odocstream &, row_type row,
636 std::vector<unsigned int> const &) const;
638 bool plaintextBottomHLine(odocstream &, row_type row,
639 std::vector<unsigned int> const &) const;
641 void plaintextPrintCell(odocstream &,
642 OutputParams const &,
643 idx_type cell, row_type row, col_type column,
644 std::vector<unsigned int> const &,
645 bool onlydata) const;
646 /// auxiliary function for docbook
647 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
649 /// change associated Buffer
650 void setBuffer(Buffer & buffer);
651 /// retrieve associated Buffer
652 Buffer & buffer() const { return *buffer_; }
661 class InsetTableCell : public InsetText
665 InsetTableCell(Buffer & buf);
667 InsetCode lyxCode() const { return CELL_CODE; }
669 Inset * clone() { return new InsetTableCell(*this); }
671 bool getStatus(Cursor & cur, FuncRequest const & cmd,
672 FuncStatus & status) const;
674 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
676 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
677 /// writes the contents of the cell as a string, optionally
678 /// descending into insets
679 docstring asString(bool intoInsets = true);
684 void operator=(InsetTableCell const &);
686 // This boolean is supposed to track whether the cell has had its
687 // width explicitly set. We need to know this to determine whether
688 // layout changes and paragraph customization are allowed---that is,
689 // we need it in forcePlainLayout() and allowParagraphCustomization().
690 // Unfortunately, that information is not readily available in
691 // InsetTableCell. In the case of multicolumn cells, it is present
692 // in CellData, and so would be available here if CellData were to
693 // become a member of InsetTableCell. But in the other case, it isn't
694 // even available there, but is held in Tabular::ColumnData.
695 // So, the present solution uses this boolean to track the information
696 // we need to track, and tries to keep it updated. This is not ideal,
697 // but the other solutions are no better. These are:
698 // (i) Keep a pointer in InsetTableCell to the table;
699 // (ii) Find the table by iterating over the Buffer's insets.
700 // Solution (i) raises the problem of updating the pointer when an
701 // InsetTableCell is copied, and we'd therefore need a copy constructor
702 // in InsetTabular and then in Tabular, which seems messy, given how
703 // complicated those classes are. Solution (ii) involves a lot of
704 // iterating, since this information is needed quite often, and so may
706 // So, well, if someone can do better, please do!
710 // FIXME: Here the thoughts from the comment above also apply.
712 LyXAlignment contentAlign;
713 /// should paragraph indendation be omitted in any case?
714 bool neverIndent() const { return true; }
716 LyXAlignment contentAlignment() const { return contentAlign; }
718 virtual bool usePlainLayout() const { return true; }
720 virtual bool forcePlainLayout(idx_type = 0) const;
722 virtual bool allowParagraphCustomization(idx_type = 0) const;
723 /// Is the width forced to some value?
724 bool hasFixedWidth() const { return isFixedWidth; }
728 class InsetTabular : public Inset
732 InsetTabular(Buffer &, row_type rows = 1,
733 col_type columns = 1);
737 void setBuffer(Buffer & buffer);
740 static void string2params(std::string const &, InsetTabular &);
742 static std::string params2string(InsetTabular const &);
746 void write(std::ostream &) const;
748 void metrics(MetricsInfo &, Dimension &) const;
750 void draw(PainterInfo & pi, int x, int y) const;
752 void drawSelection(PainterInfo & pi, int x, int y) const;
754 bool editable() const { return true; }
756 bool hasSettings() const { return true; }
758 bool insetAllowed(InsetCode code) const;
760 bool allowSpellCheck() const { return true; }
762 bool canTrackChanges() const { return true; }
763 /** returns true if, when outputing LaTeX, font changes should
764 be closed before generating this inset. This is needed for
765 insets that may contain several paragraphs */
766 bool noFontChange() const { return true; }
768 DisplayType display() const;
770 int latex(odocstream &, OutputParams const &) const;
772 int plaintext(odocstream &, OutputParams const &) const;
774 int docbook(odocstream &, OutputParams const &) const;
776 void validate(LaTeXFeatures & features) const;
778 InsetCode lyxCode() const { return TABULAR_CODE; }
780 docstring contextMenu(BufferView const & bv, int x, int y) const;
781 /// get offset of this cursor slice relative to our upper left corner
782 void cursorPos(BufferView const & bv, CursorSlice const & sl,
783 bool boundary, int & x, int & y) const;
785 bool tabularFeatures(Cursor & cur, std::string const & what);
787 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
788 std::string const & val = std::string());
790 void openLayoutDialog(BufferView *) const;
792 bool showInsetDialog(BufferView *) const;
794 size_t nargs() const { return tabular.numberofcells; }
796 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
798 boost::shared_ptr<InsetTableCell> cell(idx_type);
800 Text * getText(int) const;
802 /// set the change for the entire inset
803 void setChange(Change const & change);
804 /// accept the changes within the inset
805 void acceptChanges();
806 /// reject the changes within the inset
807 void rejectChanges();
809 // this should return true if we have a "normal" cell, otherwise false.
810 // "normal" means without width set!
811 /// should all paragraphs be output with "Standard" layout?
812 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
814 virtual bool forcePlainLayout(idx_type cell = 0) const;
816 void addPreview(graphics::PreviewLoader &) const;
818 /// lock cell with given index
819 void edit(Cursor & cur, bool front, EntryDirection entry_from);
820 /// get table row from x coordinate
821 int rowFromY(Cursor & cur, int y) const;
822 /// get table column from y coordinate
823 int columnFromX(Cursor & cur, int x) const;
825 Inset * editXY(Cursor & cur, int x, int y);
826 /// can we go further down on mouse click?
827 bool descendable() const { return true; }
828 // Update the counters of this inset and of its contents
829 void updateLabels(ParIterator const &);
832 bool completionSupported(Cursor const &) const;
834 bool inlineCompletionSupported(Cursor const & cur) const;
836 bool automaticInlineCompletion() const;
838 bool automaticPopupCompletion() const;
840 bool showCompletionCursor() const;
842 CompletionList const * createCompletionList(Cursor const & cur) const;
844 docstring completionPrefix(Cursor const & cur) const;
846 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
848 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
850 virtual bool usePlainLayout() const { return true; }
853 virtual InsetTabular * asInsetTabular() { return this; }
855 virtual InsetTabular const * asInsetTabular() const { return this; }
857 bool isRightToLeft(Cursor & cur) const;
858 /// writes the cells between stidx and enidx as a string, optionally
859 /// descending into the insets
860 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
862 /// Returns whether the cell in the specified row and column is selected.
863 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
865 // Public structures and variables
867 mutable Tabular tabular;
871 InsetTabular(InsetTabular const &);
873 void doDispatch(Cursor & cur, FuncRequest & cmd);
875 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
877 int scroll() const { return scx_; }
879 Inset * clone() const { return new InsetTabular(*this); }
882 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
883 idx_type cell, Change const & change) const;
885 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
888 void moveNextCell(Cursor & cur,
889 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
891 void movePrevCell(Cursor & cur,
892 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
894 int cellXPos(idx_type cell) const;
896 void resetPos(Cursor & cur) const;
898 void removeTabularRow();
900 bool copySelection(Cursor & cur);
902 bool pasteClipboard(Cursor & cur);
904 void cutSelection(Cursor & cur);
906 void getSelection(Cursor & cur, row_type & rs, row_type & re,
907 col_type & cs, col_type & ce) const;
909 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
911 /// return the "Manhattan distance" to nearest corner
912 int dist(BufferView &, idx_type cell, int x, int y) const;
913 /// return the cell nearest to x, y
914 idx_type getNearestCell(BufferView &, int x, int y) const;
916 /// test the rotation state of the give cell range.
917 bool oneCellHasRotationState(bool rotated,
918 row_type row_start, row_type row_end,
919 col_type col_start, col_type col_end) const;
921 mutable idx_type first_visible_cell;
924 /// true when selecting rows with the mouse
926 /// true when selecting columns with the mouse
930 std::string const featureAsString(Tabular::Feature feature);
934 #endif // INSET_TABULAR_H