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,
188 CELL_BEGIN_OF_MULTICOLUMN,
190 CELL_PART_OF_MULTICOLUMN
198 LYX_VALIGN_BOTTOM = 1,
200 LYX_VALIGN_MIDDLE = 2
216 // we have this header type (is set in the getLT... functions)
218 // double borders on top
220 // double borders on bottom
222 // used for FirstHeader & LastFooter and if this is true
223 // all the rows marked as FirstHeader or LastFooter are
224 // ignored in the output and it is set to be empty!
228 /// type for row numbers
229 typedef size_t row_type;
230 /// type for column numbers
231 typedef size_t col_type;
232 /// type for cell indices
233 typedef size_t idx_type;
234 /// index indicating an invalid position
235 static const idx_type npos = static_cast<idx_type>(-1);
238 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
240 /// Returns true if there is a topline, returns false if not
241 bool topLine(idx_type cell) const;
242 /// Returns true if there is a topline, returns false if not
243 bool bottomLine(idx_type cell) const;
244 /// Returns true if there is a topline, returns false if not
245 bool leftLine(idx_type cell) const;
246 /// Returns true if there is a topline, returns false if not
247 bool rightLine(idx_type cell) const;
250 bool topAlreadyDrawn(idx_type cell) const;
252 bool leftAlreadyDrawn(idx_type cell) const;
254 bool isLastRow(idx_type cell) const;
256 /// return space occupied by the second horizontal line and
257 /// interline space above row \p row in pixels
258 int getAdditionalHeight(row_type row) const;
260 int getAdditionalWidth(idx_type cell) const;
262 /* returns the maximum over all rows */
264 int columnWidth(idx_type cell) const;
270 int rowAscent(row_type row) const;
272 int rowDescent(row_type row) const;
274 void setRowAscent(row_type row, int height);
276 void setRowDescent(row_type row, int height);
278 void setCellWidth(idx_type cell, int new_width);
280 void setAllLines(idx_type cell, bool line);
282 void setTopLine(idx_type cell, bool line);
284 void setBottomLine(idx_type cell, bool line);
286 void setLeftLine(idx_type cell, bool line);
288 void setRightLine(idx_type cell, bool line);
290 bool rowTopLine(row_type row) const;
292 bool rowBottomLine(row_type row) const;
294 bool columnLeftLine(col_type column) const;
296 bool columnRightLine(col_type column) const;
298 void setAlignment(idx_type cell, LyXAlignment align,
299 bool onlycolumn = false);
301 void setVAlignment(idx_type cell, VAlignment align,
302 bool onlycolumn = false);
304 void setColumnPWidth(Cursor &, idx_type, Length const &);
306 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
308 void setAlignSpecial(idx_type cell, docstring const & special,
311 LyXAlignment getAlignment(idx_type cell,
312 bool onlycolumn = false) const;
314 VAlignment getVAlignment(idx_type cell,
315 bool onlycolumn = false) const;
317 Length const getPWidth(idx_type cell) const;
319 int cellWidth(idx_type cell) const;
321 int getBeginningOfTextInCell(idx_type cell) const;
323 void appendRow(idx_type cell);
325 void deleteRow(row_type row);
327 void copyRow(row_type);
329 void appendColumn(idx_type cell);
331 void deleteColumn(col_type column);
333 void copyColumn(col_type);
335 bool isFirstCellInRow(idx_type cell) const;
337 idx_type getFirstCellInRow(row_type row) const;
339 bool isLastCellInRow(idx_type cell) const;
341 idx_type getLastCellInRow(row_type row) const;
343 idx_type numberOfCellsInRow(idx_type cell) const;
345 void write(std::ostream &) const;
349 int latex(odocstream &, OutputParams const &) const;
351 int docbook(odocstream & os, OutputParams const &) const;
353 void plaintext(odocstream &,
354 OutputParams const & runparams, int const depth,
355 bool onlydata, char_type delim) const;
357 bool isMultiColumn(idx_type cell) const;
359 bool isMultiColumnReal(idx_type cell) const;
361 void setMultiColumn(idx_type cell, idx_type number);
363 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
365 bool isPartOfMultiColumn(row_type row, col_type column) const;
367 row_type cellRow(idx_type cell) const;
369 col_type cellColumn(idx_type cell) const;
371 col_type cellRightColumn(idx_type cell) const;
373 void setRotateCell(idx_type cell, bool);
375 bool getRotateCell(idx_type cell) const;
377 bool needRotating() const;
379 bool isLastCell(idx_type cell) const;
381 idx_type cellAbove(idx_type cell) const;
383 idx_type cellBelow(idx_type cell) const;
385 idx_type cellIndex(row_type row, col_type column) const;
387 void setUsebox(idx_type cell, BoxType);
389 BoxType getUsebox(idx_type cell) const;
391 // Long Tabular Options support functions
393 bool checkLTType(row_type row, ltType const &) const;
395 void setLTHead(row_type row, bool flag, ltType const &, bool first);
397 bool getRowOfLTHead(row_type row, ltType &) const;
399 bool getRowOfLTFirstHead(row_type row, ltType &) const;
401 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
403 bool getRowOfLTFoot(row_type row, ltType &) const;
405 bool getRowOfLTLastFoot(row_type row, ltType &) const;
407 void setLTNewPage(row_type row, bool what);
409 bool getLTNewPage(row_type row) const;
411 idx_type setLTCaption(row_type row, bool what);
413 bool ltCaption(row_type row) const;
415 bool haveLTHead() const;
417 bool haveLTFirstHead() const;
419 bool haveLTFoot() const;
421 bool haveLTLastFoot() const;
423 // end longtable support
425 boost::shared_ptr<InsetTableCell> cellInset(idx_type cell) const;
427 boost::shared_ptr<InsetTableCell> cellInset(row_type row,
428 col_type column) const;
430 void setCellInset(row_type row, col_type column,
431 boost::shared_ptr<InsetTableCell>) const;
432 /// Search for \param inset in the tabular, with the
434 void validate(LaTeXFeatures &) const;
441 CellData(Buffer const &);
443 CellData(CellData const &);
445 CellData & operator=(CellData);
447 void swap(CellData & rhs);
455 LyXAlignment alignment;
457 VAlignment valignment;
471 docstring align_special;
473 Length p_width; // this is only set for multicolumn!!!
475 boost::shared_ptr<InsetTableCell> inset;
477 CellData & cellInfo(idx_type cell) const;
479 typedef std::vector<CellData> cell_vector;
481 typedef std::vector<cell_vector> cell_vvector;
492 /// Extra space between the top line and this row
494 /// Ignore top_space if true and use the default top space
495 bool top_space_default;
496 /// Extra space between this row and the bottom line
498 /// Ignore bottom_space if true and use the default bottom space
499 bool bottom_space_default;
500 /// Extra space between the bottom line and the next top line
501 Length interline_space;
502 /// Ignore interline_space if true and use the default interline space
503 bool interline_space_default;
504 /// This are for longtabulars only
507 /// a row of endfirsthead
511 /// row of endlastfoot
513 /// row for a newpage
519 typedef std::vector<RowData> row_vector;
527 LyXAlignment alignment;
529 VAlignment valignment;
535 docstring align_special;
538 typedef std::vector<ColumnData> column_vector;
541 idx_type numberofcells;
543 std::vector<row_type> rowofcell;
545 std::vector<col_type> columnofcell;
549 column_vector column_info;
551 mutable cell_vvector cell_info;
559 bool is_long_tabular;
562 /// endfirsthead data
570 void init(Buffer const &, row_type rows_arg,
571 col_type columns_arg);
573 void updateIndexes();
574 /// return true of update is needed
575 bool updateColumnWidths();
577 idx_type columnSpan(idx_type cell) const;
579 BoxType useParbox(idx_type cell) const;
581 // helper function for Latex returns number of newlines
583 int TeXTopHLine(odocstream &, row_type row) const;
585 int TeXBottomHLine(odocstream &, row_type row) const;
587 int TeXCellPreamble(odocstream &, idx_type cell, bool & ismulticol) const;
589 int TeXCellPostamble(odocstream &, idx_type cell, bool ismulticol) const;
591 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
593 bool isValidRow(row_type const row) const;
595 int TeXRow(odocstream &, row_type const row,
596 OutputParams const &) const;
598 // helper functions for plain text
600 bool plaintextTopHLine(odocstream &, row_type row,
601 std::vector<unsigned int> const &) const;
603 bool plaintextBottomHLine(odocstream &, row_type row,
604 std::vector<unsigned int> const &) const;
606 void plaintextPrintCell(odocstream &,
607 OutputParams const &,
608 idx_type cell, row_type row, col_type column,
609 std::vector<unsigned int> const &,
610 bool onlydata) const;
611 /// auxiliary function for docbook
612 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
614 /// change associated Buffer
615 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
616 /// retrieve associated Buffer
617 Buffer const & buffer() const { return *buffer_; }
620 Buffer const * buffer_;
626 class InsetTableCell : public InsetText
630 InsetTableCell(Buffer const & buf);
632 InsetCode lyxCode() const { return CELL_CODE; }
634 Inset * clone() { return new InsetTableCell(*this); }
636 virtual bool usePlainLayout() const { return true; }
638 virtual bool forcePlainLayout(idx_type = 0) const;
640 virtual bool allowParagraphCustomization(idx_type = 0) const;
642 bool getStatus(Cursor & cur, FuncRequest const & cmd,
643 FuncStatus & status) const;
645 virtual bool neverIndent() { return true; }
647 void toggleFixedWidth(bool fw) { isFixedWidth = fw; }
652 void operator=(InsetTableCell const &);
658 class InsetTabular : public Inset
662 InsetTabular(Buffer const &, row_type rows = 1,
663 col_type columns = 1);
667 static void string2params(std::string const &, InsetTabular &);
669 static std::string params2string(InsetTabular const &);
673 void write(std::ostream &) const;
675 void metrics(MetricsInfo &, Dimension &) const;
677 void draw(PainterInfo & pi, int x, int y) const;
679 void drawSelection(PainterInfo & pi, int x, int y) const;
681 docstring editMessage() const;
683 EDITABLE editable() const { return HIGHLY_EDITABLE; }
685 bool insetAllowed(InsetCode code) const;
687 bool allowSpellCheck() const { return true; }
689 bool canTrackChanges() const { return true; }
690 /** returns true if, when outputing LaTeX, font changes should
691 be closed before generating this inset. This is needed for
692 insets that may contain several paragraphs */
693 bool noFontChange() const { return true; }
695 DisplayType display() const { return tabular.is_long_tabular ? AlignCenter : Inline; }
697 int latex(odocstream &, OutputParams const &) const;
699 int plaintext(odocstream &, OutputParams const &) const;
701 int docbook(odocstream &, OutputParams const &) const;
703 void validate(LaTeXFeatures & features) const;
705 InsetCode lyxCode() const { return TABULAR_CODE; }
707 docstring contextMenu(BufferView const & bv, int x, int y) const;
708 /// get offset of this cursor slice relative to our upper left corner
709 void cursorPos(BufferView const & bv, CursorSlice const & sl,
710 bool boundary, int & x, int & y) const;
712 bool tabularFeatures(Cursor & cur, std::string const & what);
714 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
715 std::string const & val = std::string());
717 void openLayoutDialog(BufferView *) const;
719 bool showInsetDialog(BufferView *) const;
721 size_t nargs() const { return tabular.numberofcells; }
723 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
725 boost::shared_ptr<InsetTableCell> cell(idx_type);
727 Text * getText(int) const;
729 /// set the change for the entire inset
730 void setChange(Change const & change);
731 /// accept the changes within the inset
732 void acceptChanges(BufferParams const & bparams);
733 /// reject the changes within the inset
734 void rejectChanges(BufferParams const & bparams);
736 // this should return true if we have a "normal" cell, otherwise false.
737 // "normal" means without width set!
738 /// should all paragraphs be output with "Standard" layout?
739 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
741 virtual bool forcePlainLayout(idx_type cell = 0) const;
743 virtual bool usePlainLayout() { return true; }
745 void addPreview(graphics::PreviewLoader &) const;
747 /// lock cell with given index
748 void edit(Cursor & cur, bool front, EntryDirection entry_from);
749 /// get table row from x coordinate
750 int rowFromY(Cursor & cur, int y) const;
751 /// get table column from y coordinate
752 int columnFromX(Cursor & cur, int x) const;
754 Inset * editXY(Cursor & cur, int x, int y);
755 /// can we go further down on mouse click?
756 bool descendable() const { return true; }
757 // Update the counters of this inset and of its contents
758 void updateLabels(ParIterator const &);
761 bool completionSupported(Cursor const &) const;
763 bool inlineCompletionSupported(Cursor const & cur) const;
765 bool automaticInlineCompletion() const;
767 bool automaticPopupCompletion() const;
769 bool showCompletionCursor() const;
771 CompletionList const * createCompletionList(Cursor const & cur) const;
773 docstring completionPrefix(Cursor const & cur) const;
775 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
777 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
780 virtual InsetTabular * asInsetTabular() { return this; }
782 virtual InsetTabular const * asInsetTabular() const { return this; }
784 bool isRightToLeft(Cursor & cur) const;
787 // Public structures and variables
789 mutable Tabular tabular;
793 InsetTabular(InsetTabular const &);
795 void doDispatch(Cursor & cur, FuncRequest & cmd);
797 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
799 int scroll() const { return scx_; }
801 Inset * clone() const { return new InsetTabular(*this); }
804 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
805 idx_type cell, bool erased) const;
807 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
810 void moveNextCell(Cursor & cur,
811 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
813 void movePrevCell(Cursor & cur,
814 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
816 int cellXPos(idx_type cell) const;
818 void resetPos(Cursor & cur) const;
820 void removeTabularRow();
822 bool copySelection(Cursor & cur);
824 bool pasteClipboard(Cursor & cur);
826 void cutSelection(Cursor & cur);
828 void getSelection(Cursor & cur, row_type & rs, row_type & re,
829 col_type & cs, col_type & ce) const;
831 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
832 /// are we operating on several cells?
833 bool tablemode(Cursor & cur) const;
835 /// return the "Manhattan distance" to nearest corner
836 int dist(BufferView &, idx_type cell, int x, int y) const;
837 /// return the cell nearest to x, y
838 idx_type getNearestCell(BufferView &, int x, int y) const;
840 /// test the rotation state of the give cell range.
841 bool oneCellHasRotationState(bool rotated,
842 row_type row_start, row_type row_end,
843 col_type col_start, col_type col_end) const;
845 mutable idx_type first_visible_cell;
848 /// true when selecting rows with the mouse
850 /// true when selecting columns with the mouse
854 std::string const featureAsString(Tabular::Feature feature);
858 #endif // INSET_TABULAR_H