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
11 * \author Edwin Leuven
14 * Full author contact details are available in file CREDITS.
17 // Things to think of when designing the new tabular support:
18 // - color support (colortbl, color)
19 // - decimal alignment (dcloumn)
20 // - custom lines (hhline)
23 #ifndef INSET_TABULAR_H
24 #define INSET_TABULAR_H
27 #include "InsetText.h"
31 #include <boost/shared_ptr.hpp>
48 namespace frontend { class Painter; }
56 // A helper struct for tables
135 UNSET_ROTATE_TABULAR,
137 TOGGLE_ROTATE_TABULAR,
165 SET_SPECIAL_MULTICOLUMN,
167 SET_SPECIAL_MULTIROW,
183 TABULAR_VALIGN_MIDDLE,
185 TABULAR_VALIGN_BOTTOM,
187 LONGTABULAR_ALIGN_LEFT,
189 LONGTABULAR_ALIGN_CENTER,
191 LONGTABULAR_ALIGN_RIGHT,
200 CELL_BEGIN_OF_MULTICOLUMN,
202 CELL_PART_OF_MULTICOLUMN,
204 CELL_BEGIN_OF_MULTIROW,
206 CELL_PART_OF_MULTIROW
214 LYX_VALIGN_MIDDLE = 1,
216 LYX_VALIGN_BOTTOM = 2
222 LYX_LONGTABULAR_ALIGN_LEFT = 0,
224 LYX_LONGTABULAR_ALIGN_CENTER = 1,
226 LYX_LONGTABULAR_ALIGN_RIGHT = 2
242 // we have this header type (is set in the getLT... functions)
244 // double borders on top
246 // double borders on bottom
248 // used for FirstHeader & LastFooter and if this is true
249 // all the rows marked as FirstHeader or LastFooter are
250 // ignored in the output and it is set to be empty!
254 /// type for row numbers
255 typedef size_t row_type;
256 /// type for column numbers
257 typedef size_t col_type;
258 /// type for cell indices
259 typedef size_t idx_type;
260 /// index indicating an invalid position
261 static const idx_type npos = static_cast<idx_type>(-1);
264 Tabular(Buffer * buf, col_type columns_arg, row_type rows_arg);
266 /// Returns true if there is a topline, returns false if not
267 bool topLine(idx_type cell) const;
268 /// Returns true if there is a topline, returns false if not
269 bool bottomLine(idx_type cell) const;
270 /// Returns true if there is a topline, returns false if not
271 bool leftLine(idx_type cell) const;
272 /// Returns true if there is a topline, returns false if not
273 bool rightLine(idx_type cell) const;
276 bool topAlreadyDrawn(idx_type cell) const;
278 bool leftAlreadyDrawn(idx_type cell) const;
280 bool isLastRow(idx_type cell) const;
282 /// return space occupied by the second horizontal line and
283 /// interline space above row \p row in pixels
284 int interRowSpace(row_type row) const;
286 int interColumnSpace(idx_type cell) const;
288 /* returns the maximum over all rows */
290 int columnWidth(idx_type cell) const;
292 int rowHeight(idx_type cell) const;
298 int rowAscent(row_type row) const;
300 int rowDescent(row_type row) const;
302 void setRowAscent(row_type row, int height);
304 void setRowDescent(row_type row, int height);
306 void setCellWidth(idx_type cell, int new_width);
308 void setAllLines(idx_type cell, bool line);
310 void setTopLine(idx_type cell, bool line);
312 void setBottomLine(idx_type cell, bool line);
314 void setLeftLine(idx_type cell, bool line);
316 void setRightLine(idx_type cell, bool line);
318 bool rowTopLine(row_type row) const;
320 bool rowBottomLine(row_type row) const;
322 bool columnLeftLine(col_type column) const;
324 bool columnRightLine(col_type column) const;
326 void setAlignment(idx_type cell, LyXAlignment align,
327 bool onlycolumn = false);
329 void setVAlignment(idx_type cell, VAlignment align,
330 bool onlycolumn = false);
332 void setColumnPWidth(Cursor &, idx_type, Length const &);
334 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
336 void setAlignSpecial(idx_type cell, docstring const & special,
339 LyXAlignment getAlignment(idx_type cell,
340 bool onlycolumn = false) const;
342 VAlignment getVAlignment(idx_type cell,
343 bool onlycolumn = false) const;
345 Length const getPWidth(idx_type cell) const;
347 int cellWidth(idx_type cell) const;
349 int textHOffset(idx_type cell) const;
351 int textVOffset(idx_type cell) const;
353 void appendRow(idx_type cell);
355 void deleteRow(row_type row);
357 void copyRow(row_type);
359 void appendColumn(idx_type cell);
361 void deleteColumn(col_type column);
363 void copyColumn(col_type);
365 idx_type getFirstCellInRow(row_type row) const;
367 idx_type getLastCellInRow(row_type row) const;
369 idx_type numberOfCellsInRow(row_type row) const;
371 void write(std::ostream &) const;
375 int latex(odocstream &, OutputParams const &) const;
377 int docbook(odocstream & os, OutputParams const &) const;
379 docstring xhtml(XHTMLStream & os, OutputParams const &) const;
381 void plaintext(odocstream &,
382 OutputParams const & runparams, int const depth,
383 bool onlydata, char_type delim) const;
385 bool isMultiColumn(idx_type cell) const;
387 bool isMultiColumnReal(idx_type cell) const;
389 void setMultiColumn(idx_type cell, idx_type number);
391 void unsetMultiColumn(idx_type cell);
393 bool isPartOfMultiColumn(row_type row, col_type column) const;
395 bool isPartOfMultiRow(row_type row, col_type column) const;
397 bool isMultiRow(idx_type cell) const;
399 void setMultiRow(idx_type cell, idx_type number);
401 void unsetMultiRow(idx_type cell);
403 row_type cellRow(idx_type cell) const;
405 col_type cellColumn(idx_type cell) const;
407 col_type cellRightColumn(idx_type cell) const;
409 void setRotateCell(idx_type cell, bool);
411 bool getRotateCell(idx_type cell) const;
413 bool needRotating() const;
415 bool isLastCell(idx_type cell) const;
417 idx_type cellAbove(idx_type cell) const;
419 idx_type cellBelow(idx_type cell) const;
421 idx_type cellIndex(row_type row, col_type column) const;
423 void setUsebox(idx_type cell, BoxType);
425 BoxType getUsebox(idx_type cell) const;
427 // Long Tabular Options support functions
429 bool checkLTType(row_type row, ltType const &) const;
431 void setLTHead(row_type row, bool flag, ltType const &, bool first);
433 bool getRowOfLTHead(row_type row, ltType &) const;
435 bool getRowOfLTFirstHead(row_type row, ltType &) const;
437 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
439 bool getRowOfLTFoot(row_type row, ltType &) const;
441 bool getRowOfLTLastFoot(row_type row, ltType &) const;
443 void setLTNewPage(row_type row, bool what);
445 bool getLTNewPage(row_type row) const;
447 idx_type setLTCaption(row_type row, bool what);
449 bool ltCaption(row_type row) const;
451 bool haveLTHead() const;
453 bool haveLTFirstHead() const;
455 bool haveLTFoot() const;
457 bool haveLTLastFoot() const;
459 bool haveLTCaption() const;
461 // end longtable support
463 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
465 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
466 col_type column) const;
468 void setCellInset(row_type row, col_type column,
469 boost::shared_ptr<InsetTableCell>) const;
470 /// Search for \param inset in the tabular, with the
472 void validate(LaTeXFeatures &) const;
474 // FIXME Now that cells have an InsetTableCell as their insets, rather
475 // than an InsetText, it'd be possible to reverse the relationship here,
476 // so that cell_vector was a vector<InsetTableCell> rather than a
477 // vector<CellData>, and an InsetTableCell had a CellData as a member,
478 // or perhaps just had its members as members.
485 CellData(CellData const &);
487 CellData & operator=(CellData);
489 void swap(CellData & rhs);
499 LyXAlignment alignment;
501 VAlignment valignment;
515 docstring align_special;
517 Length p_width; // this is only set for multicolumn!!!
519 boost::shared_ptr<InsetTableCell> inset;
521 CellData & cellInfo(idx_type cell) const;
523 typedef std::vector<CellData> cell_vector;
525 typedef std::vector<cell_vector> cell_vvector;
536 /// Extra space between the top line and this row
538 /// Ignore top_space if true and use the default top space
539 bool top_space_default;
540 /// Extra space between this row and the bottom line
542 /// Ignore bottom_space if true and use the default bottom space
543 bool bottom_space_default;
544 /// Extra space between the bottom line and the next top line
545 Length interline_space;
546 /// Ignore interline_space if true and use the default interline space
547 bool interline_space_default;
548 /// This are for longtabulars only
551 /// a row of endfirsthead
555 /// row of endlastfoot
557 /// row for a newpage
563 typedef std::vector<RowData> row_vector;
571 LyXAlignment alignment;
573 VAlignment valignment;
579 docstring align_special;
582 typedef std::vector<ColumnData> column_vector;
585 idx_type numberofcells;
587 std::vector<row_type> rowofcell;
589 std::vector<col_type> columnofcell;
593 column_vector column_info;
595 mutable cell_vvector cell_info;
601 VAlignment tabular_valignment;
605 HAlignment longtabular_alignment;
607 bool is_long_tabular;
610 /// endfirsthead data
618 void init(Buffer *, row_type rows_arg,
619 col_type columns_arg);
621 void updateIndexes();
623 bool setFixedWidth(row_type r, col_type c);
625 void updateContentAlignment(row_type r, col_type c);
626 /// return true of update is needed
627 bool updateColumnWidths();
629 idx_type columnSpan(idx_type cell) const;
631 idx_type rowSpan(idx_type cell) const;
633 BoxType useParbox(idx_type cell) const;
635 // helper function for Latex returns number of newlines
637 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
639 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
641 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
643 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
645 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
647 bool isValidRow(row_type const row) const;
649 int TeXRow(odocstream &, row_type const row,
650 OutputParams const &) const;
652 // helper functions for plain text
654 bool plaintextTopHLine(odocstream &, row_type row,
655 std::vector<unsigned int> const &) const;
657 bool plaintextBottomHLine(odocstream &, row_type row,
658 std::vector<unsigned int> const &) const;
660 void plaintextPrintCell(odocstream &,
661 OutputParams const &,
662 idx_type cell, row_type row, col_type column,
663 std::vector<unsigned int> const &,
664 bool onlydata) const;
665 /// auxiliary function for docbook
666 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
668 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &) const;
670 /// change associated Buffer
671 void setBuffer(Buffer & buffer);
672 /// retrieve associated Buffer
673 Buffer & buffer() const { return *buffer_; }
682 class InsetTableCell : public InsetText
686 InsetTableCell(Buffer * buf);
688 InsetCode lyxCode() const { return CELL_CODE; }
690 Inset * clone() { return new InsetTableCell(*this); }
692 bool getStatus(Cursor & cur, FuncRequest const & cmd,
693 FuncStatus & status) const;
695 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
697 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
698 /// writes the contents of the cell as a string, optionally
699 /// descending into insets
700 docstring asString(bool intoInsets = true);
702 docstring xhtml(XHTMLStream &, OutputParams const &) const;
707 void operator=(InsetTableCell const &);
709 // This boolean is supposed to track whether the cell has had its
710 // width explicitly set. We need to know this to determine whether
711 // layout changes and paragraph customization are allowed---that is,
712 // we need it in forcePlainLayout() and allowParagraphCustomization().
713 // Unfortunately, that information is not readily available in
714 // InsetTableCell. In the case of multicolumn cells, it is present
715 // in CellData, and so would be available here if CellData were to
716 // become a member of InsetTableCell. But in the other case, it isn't
717 // even available there, but is held in Tabular::ColumnData.
718 // So, the present solution uses this boolean to track the information
719 // we need to track, and tries to keep it updated. This is not ideal,
720 // but the other solutions are no better. These are:
721 // (i) Keep a pointer in InsetTableCell to the table;
722 // (ii) Find the table by iterating over the Buffer's insets.
723 // Solution (i) raises the problem of updating the pointer when an
724 // InsetTableCell is copied, and we'd therefore need a copy constructor
725 // in InsetTabular and then in Tabular, which seems messy, given how
726 // complicated those classes are. Solution (ii) involves a lot of
727 // iterating, since this information is needed quite often, and so may
729 // So, well, if someone can do better, please do!
733 // FIXME: Here the thoughts from the comment above also apply.
735 LyXAlignment contentAlign;
736 /// should paragraph indendation be omitted in any case?
737 bool neverIndent() const { return true; }
739 LyXAlignment contentAlignment() const { return contentAlign; }
741 virtual bool usePlainLayout() const { return true; }
743 virtual bool forcePlainLayout(idx_type = 0) const;
745 virtual bool allowParagraphCustomization(idx_type = 0) const;
746 /// Is the width forced to some value?
747 bool hasFixedWidth() const { return isFixedWidth; }
751 class InsetTabular : public Inset
755 InsetTabular(Buffer *, row_type rows = 1,
756 col_type columns = 1);
760 void setBuffer(Buffer & buffer);
763 static void string2params(std::string const &, InsetTabular &);
765 static std::string params2string(InsetTabular const &);
769 void write(std::ostream &) const;
771 void metrics(MetricsInfo &, Dimension &) const;
773 void draw(PainterInfo & pi, int x, int y) const;
775 void drawSelection(PainterInfo & pi, int x, int y) const;
777 bool editable() const { return true; }
779 bool hasSettings() const { return true; }
781 bool insetAllowed(InsetCode code) const;
783 bool allowSpellCheck() const { return true; }
785 bool canTrackChanges() const { return true; }
786 /** returns true if, when outputing LaTeX, font changes should
787 be closed before generating this inset. This is needed for
788 insets that may contain several paragraphs */
789 bool noFontChange() const { return true; }
791 DisplayType display() const;
793 int latex(odocstream &, OutputParams const &) const;
795 int plaintext(odocstream &, OutputParams const &) const;
797 int docbook(odocstream &, OutputParams const &) const;
799 docstring xhtml(XHTMLStream &, OutputParams const &) const;
801 void validate(LaTeXFeatures & features) const;
803 InsetCode lyxCode() const { return TABULAR_CODE; }
805 docstring contextMenu(BufferView const & bv, int x, int y) const;
806 /// get offset of this cursor slice relative to our upper left corner
807 void cursorPos(BufferView const & bv, CursorSlice const & sl,
808 bool boundary, int & x, int & y) const;
810 bool tabularFeatures(Cursor & cur, std::string const & what);
812 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
813 std::string const & val = std::string());
815 size_t nargs() const { return tabular.numberofcells; }
817 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
819 boost::shared_ptr<InsetTableCell> cell(idx_type);
821 Text * getText(int) const;
823 /// set the change for the entire inset
824 void setChange(Change const & change);
825 /// accept the changes within the inset
826 void acceptChanges();
827 /// reject the changes within the inset
828 void rejectChanges();
830 // this should return true if we have a "normal" cell, otherwise false.
831 // "normal" means without width set!
832 /// should all paragraphs be output with "Standard" layout?
833 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
835 virtual bool forcePlainLayout(idx_type cell = 0) const;
837 void addPreview(DocIterator const & inset_pos,
838 graphics::PreviewLoader &) const;
840 /// lock cell with given index
841 void edit(Cursor & cur, bool front, EntryDirection entry_from);
842 /// get table row from x coordinate
843 int rowFromY(Cursor & cur, int y) const;
844 /// get table column from y coordinate
845 int columnFromX(Cursor & cur, int x) const;
847 Inset * editXY(Cursor & cur, int x, int y);
848 /// can we go further down on mouse click?
849 bool descendable(BufferView const &) const { return true; }
850 /// Update the counters of this inset and of its contents
851 void updateLabels(ParIterator const &, UpdateType);
853 void addToToc(DocIterator const &);
856 bool completionSupported(Cursor const &) const;
858 bool inlineCompletionSupported(Cursor const & cur) const;
860 bool automaticInlineCompletion() const;
862 bool automaticPopupCompletion() const;
864 bool showCompletionCursor() const;
866 CompletionList const * createCompletionList(Cursor const & cur) const;
868 docstring completionPrefix(Cursor const & cur) const;
870 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
872 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
874 virtual bool usePlainLayout() const { return true; }
877 virtual InsetTabular * asInsetTabular() { return this; }
879 virtual InsetTabular const * asInsetTabular() const { return this; }
881 bool isRightToLeft(Cursor & cur) const;
882 /// writes the cells between stidx and enidx as a string, optionally
883 /// descending into the insets
884 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
886 /// Returns whether the cell in the specified row and column is selected.
887 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
889 // Public structures and variables
891 mutable Tabular tabular;
895 InsetTabular(InsetTabular const &);
897 void doDispatch(Cursor & cur, FuncRequest & cmd);
899 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
901 int scroll() const { return scx_; }
903 Inset * clone() const { return new InsetTabular(*this); }
906 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
907 idx_type cell, Change const & change) const;
909 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
912 void moveNextCell(Cursor & cur,
913 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
915 void movePrevCell(Cursor & cur,
916 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
918 int cellXPos(idx_type cell) const;
920 void resetPos(Cursor & cur) const;
922 void removeTabularRow();
924 bool copySelection(Cursor & cur);
926 bool pasteClipboard(Cursor & cur);
928 void cutSelection(Cursor & cur);
930 void getSelection(Cursor & cur, row_type & rs, row_type & re,
931 col_type & cs, col_type & ce) const;
933 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
935 /// return the "Manhattan distance" to nearest corner
936 int dist(BufferView &, idx_type cell, int x, int y) const;
937 /// return the cell nearest to x, y
938 idx_type getNearestCell(BufferView &, int x, int y) const;
940 /// test the rotation state of the give cell range.
941 bool oneCellHasRotationState(bool rotated,
942 row_type row_start, row_type row_end,
943 col_type col_start, col_type col_end) const;
945 mutable idx_type first_visible_cell;
948 /// true when selecting rows with the mouse
950 /// true when selecting columns with the mouse
954 std::string const featureAsString(Tabular::Feature feature);
958 #endif // INSET_TABULAR_H