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;
275 /// return space occupied by the second horizontal line and
276 /// interline space above row \p row in pixels
277 int interRowSpace(row_type row) const;
279 int interColumnSpace(idx_type cell) const;
281 /* returns the maximum over all rows */
283 int columnWidth(idx_type cell) const;
285 int rowHeight(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 textHOffset(idx_type cell) const;
344 int textVOffset(idx_type cell) const;
346 void appendRow(idx_type cell);
348 void deleteRow(row_type row);
350 void copyRow(row_type);
352 void appendColumn(idx_type cell);
354 void deleteColumn(col_type column);
356 void copyColumn(col_type);
358 idx_type getFirstCellInRow(row_type row) const;
360 idx_type getLastCellInRow(row_type row) const;
362 idx_type numberOfCellsInRow(row_type row) const;
364 void write(std::ostream &) const;
368 int latex(odocstream &, OutputParams const &) const;
370 int docbook(odocstream & os, OutputParams const &) const;
372 docstring xhtml(XHTMLStream & 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 void unsetMultiColumn(idx_type cell);
386 bool isPartOfMultiColumn(row_type row, col_type column) const;
388 bool isPartOfMultiRow(row_type row, col_type column) const;
390 bool isMultiRow(idx_type cell) const;
392 void setMultiRow(idx_type cell, idx_type number);
394 void unsetMultiRow(idx_type cell);
396 row_type cellRow(idx_type cell) const;
398 col_type cellColumn(idx_type cell) const;
400 col_type cellRightColumn(idx_type cell) const;
402 void setRotateCell(idx_type cell, bool);
404 bool getRotateCell(idx_type cell) const;
406 bool needRotating() const;
408 bool isLastCell(idx_type cell) const;
410 idx_type cellAbove(idx_type cell) const;
412 idx_type cellBelow(idx_type cell) const;
414 idx_type cellIndex(row_type row, col_type column) const;
416 void setUsebox(idx_type cell, BoxType);
418 BoxType getUsebox(idx_type cell) const;
420 // Long Tabular Options support functions
422 bool checkLTType(row_type row, ltType const &) const;
424 void setLTHead(row_type row, bool flag, ltType const &, bool first);
426 bool getRowOfLTHead(row_type row, ltType &) const;
428 bool getRowOfLTFirstHead(row_type row, ltType &) const;
430 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
432 bool getRowOfLTFoot(row_type row, ltType &) const;
434 bool getRowOfLTLastFoot(row_type row, ltType &) const;
436 void setLTNewPage(row_type row, bool what);
438 bool getLTNewPage(row_type row) const;
440 idx_type setLTCaption(row_type row, bool what);
442 bool ltCaption(row_type row) const;
444 bool haveLTHead() const;
446 bool haveLTFirstHead() const;
448 bool haveLTFoot() const;
450 bool haveLTLastFoot() const;
452 bool haveLTCaption() const;
454 // end longtable support
456 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
458 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
459 col_type column) const;
461 void setCellInset(row_type row, col_type column,
462 boost::shared_ptr<InsetTableCell>) const;
463 /// Search for \param inset in the tabular, with the
465 void validate(LaTeXFeatures &) const;
467 // FIXME Now that cells have an InsetTableCell as their insets, rather
468 // than an InsetText, it'd be possible to reverse the relationship here,
469 // so that cell_vector was a vector<InsetTableCell> rather than a
470 // vector<CellData>, and an InsetTableCell had a CellData as a member,
471 // or perhaps just had its members as members.
478 CellData(CellData const &);
480 CellData & operator=(CellData);
482 void swap(CellData & rhs);
492 LyXAlignment alignment;
494 VAlignment valignment;
508 docstring align_special;
510 Length p_width; // this is only set for multicolumn!!!
512 boost::shared_ptr<InsetTableCell> inset;
514 CellData & cellInfo(idx_type cell) const;
516 typedef std::vector<CellData> cell_vector;
518 typedef std::vector<cell_vector> cell_vvector;
529 /// Extra space between the top line and this row
531 /// Ignore top_space if true and use the default top space
532 bool top_space_default;
533 /// Extra space between this row and the bottom line
535 /// Ignore bottom_space if true and use the default bottom space
536 bool bottom_space_default;
537 /// Extra space between the bottom line and the next top line
538 Length interline_space;
539 /// Ignore interline_space if true and use the default interline space
540 bool interline_space_default;
541 /// This are for longtabulars only
544 /// a row of endfirsthead
548 /// row of endlastfoot
550 /// row for a newpage
556 typedef std::vector<RowData> row_vector;
564 LyXAlignment alignment;
566 VAlignment valignment;
572 docstring align_special;
575 typedef std::vector<ColumnData> column_vector;
578 idx_type numberofcells;
580 std::vector<row_type> rowofcell;
582 std::vector<col_type> columnofcell;
586 column_vector column_info;
588 mutable cell_vvector cell_info;
594 VAlignment tabular_valignment;
598 HAlignment longtabular_alignment;
600 bool is_long_tabular;
603 /// endfirsthead data
611 void init(Buffer *, row_type rows_arg,
612 col_type columns_arg);
614 void updateIndexes();
616 bool setFixedWidth(row_type r, col_type c);
618 void updateContentAlignment(row_type r, col_type c);
619 /// return true of update is needed
620 bool updateColumnWidths();
622 idx_type columnSpan(idx_type cell) const;
624 idx_type rowSpan(idx_type cell) const;
626 BoxType useParbox(idx_type cell) const;
628 // helper function for Latex returns number of newlines
630 int TeXTopHLine(odocstream &, row_type row, std::string const lang) const;
632 int TeXBottomHLine(odocstream &, row_type row, std::string const lang) const;
634 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol, bool & ismultirow) const;
636 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol, bool ismultirow) const;
638 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
640 bool isValidRow(row_type const row) const;
642 int TeXRow(odocstream &, row_type const row,
643 OutputParams const &) const;
645 // helper functions for plain text
647 bool plaintextTopHLine(odocstream &, row_type row,
648 std::vector<unsigned int> const &) const;
650 bool plaintextBottomHLine(odocstream &, row_type row,
651 std::vector<unsigned int> const &) const;
653 void plaintextPrintCell(odocstream &,
654 OutputParams const &,
655 idx_type cell, row_type row, col_type column,
656 std::vector<unsigned int> const &,
657 bool onlydata) const;
658 /// auxiliary function for docbook
659 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
661 docstring xhtmlRow(XHTMLStream & xs, row_type, OutputParams const &) const;
663 /// change associated Buffer
664 void setBuffer(Buffer & buffer);
665 /// retrieve associated Buffer
666 Buffer & buffer() const { return *buffer_; }
675 class InsetTableCell : public InsetText
679 InsetTableCell(Buffer * buf);
681 InsetCode lyxCode() const { return CELL_CODE; }
683 Inset * clone() { return new InsetTableCell(*this); }
685 bool getStatus(Cursor & cur, FuncRequest const & cmd,
686 FuncStatus & status) const;
688 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
690 void setContentAlignment(LyXAlignment al) {contentAlign = al; }
691 /// writes the contents of the cell as a string, optionally
692 /// descending into insets
693 docstring asString(bool intoInsets = true);
695 docstring xhtml(XHTMLStream &, OutputParams const &) const;
700 void operator=(InsetTableCell const &);
702 // This boolean is supposed to track whether the cell has had its
703 // width explicitly set. We need to know this to determine whether
704 // layout changes and paragraph customization are allowed---that is,
705 // we need it in forcePlainLayout() and allowParagraphCustomization().
706 // Unfortunately, that information is not readily available in
707 // InsetTableCell. In the case of multicolumn cells, it is present
708 // in CellData, and so would be available here if CellData were to
709 // become a member of InsetTableCell. But in the other case, it isn't
710 // even available there, but is held in Tabular::ColumnData.
711 // So, the present solution uses this boolean to track the information
712 // we need to track, and tries to keep it updated. This is not ideal,
713 // but the other solutions are no better. These are:
714 // (i) Keep a pointer in InsetTableCell to the table;
715 // (ii) Find the table by iterating over the Buffer's insets.
716 // Solution (i) raises the problem of updating the pointer when an
717 // InsetTableCell is copied, and we'd therefore need a copy constructor
718 // in InsetTabular and then in Tabular, which seems messy, given how
719 // complicated those classes are. Solution (ii) involves a lot of
720 // iterating, since this information is needed quite often, and so may
722 // So, well, if someone can do better, please do!
726 // FIXME: Here the thoughts from the comment above also apply.
728 LyXAlignment contentAlign;
729 /// should paragraph indendation be omitted in any case?
730 bool neverIndent() const { return true; }
732 LyXAlignment contentAlignment() const { return contentAlign; }
734 virtual bool usePlainLayout() const { return true; }
736 virtual bool forcePlainLayout(idx_type = 0) const;
738 virtual bool allowParagraphCustomization(idx_type = 0) const;
739 /// Is the width forced to some value?
740 bool hasFixedWidth() const { return isFixedWidth; }
744 class InsetTabular : public Inset
748 InsetTabular(Buffer *, row_type rows = 1,
749 col_type columns = 1);
753 void setBuffer(Buffer & buffer);
756 static void string2params(std::string const &, InsetTabular &);
758 static std::string params2string(InsetTabular const &);
762 void write(std::ostream &) const;
764 void metrics(MetricsInfo &, Dimension &) const;
766 void draw(PainterInfo & pi, int x, int y) const;
768 void drawSelection(PainterInfo & pi, int x, int y) const;
770 bool editable() const { return true; }
772 bool hasSettings() const { return true; }
774 bool insetAllowed(InsetCode code) const;
776 bool allowSpellCheck() const { return true; }
778 bool canTrackChanges() const { return true; }
779 /** returns true if, when outputing LaTeX, font changes should
780 be closed before generating this inset. This is needed for
781 insets that may contain several paragraphs */
782 bool noFontChange() const { return true; }
784 DisplayType display() const;
786 int latex(odocstream &, OutputParams const &) const;
788 int plaintext(odocstream &, OutputParams const &) const;
790 int docbook(odocstream &, OutputParams const &) const;
792 docstring xhtml(XHTMLStream &, OutputParams const &) const;
794 void validate(LaTeXFeatures & features) const;
796 InsetCode lyxCode() const { return TABULAR_CODE; }
798 docstring contextMenu(BufferView const & bv, int x, int y) const;
799 /// get offset of this cursor slice relative to our upper left corner
800 void cursorPos(BufferView const & bv, CursorSlice const & sl,
801 bool boundary, int & x, int & y) const;
803 bool tabularFeatures(Cursor & cur, std::string const & what);
805 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
806 std::string const & val = std::string());
808 size_t nargs() const { return tabular.numberofcells; }
810 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
812 boost::shared_ptr<InsetTableCell> cell(idx_type);
814 Text * getText(int) const;
816 /// set the change for the entire inset
817 void setChange(Change const & change);
818 /// accept the changes within the inset
819 void acceptChanges();
820 /// reject the changes within the inset
821 void rejectChanges();
823 // this should return true if we have a "normal" cell, otherwise false.
824 // "normal" means without width set!
825 /// should all paragraphs be output with "Standard" layout?
826 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
828 virtual bool forcePlainLayout(idx_type cell = 0) const;
830 void addPreview(DocIterator const & inset_pos,
831 graphics::PreviewLoader &) const;
833 /// lock cell with given index
834 void edit(Cursor & cur, bool front, EntryDirection entry_from);
835 /// get table row from x coordinate
836 int rowFromY(Cursor & cur, int y) const;
837 /// get table column from y coordinate
838 int columnFromX(Cursor & cur, int x) const;
840 Inset * editXY(Cursor & cur, int x, int y);
841 /// can we go further down on mouse click?
842 bool descendable(BufferView const &) const { return true; }
843 /// Update the counters of this inset and of its contents
844 void updateLabels(ParIterator const &, UpdateType);
846 void addToToc(DocIterator const &);
849 bool completionSupported(Cursor const &) const;
851 bool inlineCompletionSupported(Cursor const & cur) const;
853 bool automaticInlineCompletion() const;
855 bool automaticPopupCompletion() const;
857 bool showCompletionCursor() const;
859 CompletionList const * createCompletionList(Cursor const & cur) const;
861 docstring completionPrefix(Cursor const & cur) const;
863 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
865 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
867 virtual bool usePlainLayout() const { return true; }
870 virtual InsetTabular * asInsetTabular() { return this; }
872 virtual InsetTabular const * asInsetTabular() const { return this; }
874 bool isRightToLeft(Cursor & cur) const;
875 /// writes the cells between stidx and enidx as a string, optionally
876 /// descending into the insets
877 docstring asString(idx_type stidx, idx_type enidx, bool intoInsets = true);
879 /// Returns whether the cell in the specified row and column is selected.
880 bool isCellSelected(Cursor & cur, row_type row, col_type col) const;
882 // Public structures and variables
884 mutable Tabular tabular;
888 InsetTabular(InsetTabular const &);
890 void doDispatch(Cursor & cur, FuncRequest & cmd);
892 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
894 int scroll() const { return scx_; }
896 Inset * clone() const { return new InsetTabular(*this); }
899 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
900 idx_type cell, Change const & change) const;
902 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
905 void moveNextCell(Cursor & cur,
906 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
908 void movePrevCell(Cursor & cur,
909 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
911 int cellXPos(idx_type cell) const;
913 void resetPos(Cursor & cur) const;
915 void removeTabularRow();
917 bool copySelection(Cursor & cur);
919 bool pasteClipboard(Cursor & cur);
921 void cutSelection(Cursor & cur);
923 void getSelection(Cursor & cur, row_type & rs, row_type & re,
924 col_type & cs, col_type & ce) const;
926 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
928 /// return the "Manhattan distance" to nearest corner
929 int dist(BufferView &, idx_type cell, int x, int y) const;
930 /// return the cell nearest to x, y
931 idx_type getNearestCell(BufferView &, int x, int y) const;
933 /// test the rotation state of the give cell range.
934 bool oneCellHasRotationState(bool rotated,
935 row_type row_start, row_type row_end,
936 col_type col_start, col_type col_end) const;
938 mutable idx_type first_visible_cell;
941 /// true when selecting rows with the mouse
943 /// true when selecting columns with the mouse
947 std::string const featureAsString(Tabular::Feature feature);
951 #endif // INSET_TABULAR_H