4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Lars Gullik Bjønnes
9 * \author Matthias Ettrich
10 * \author André Pönitz
11 * \author Jürgen Vigna
13 * Full author contact details are available in file CREDITS.
17 // This is Juergen's rewrite of the tabular (table) support.
19 // Things to think of when designing the new tabular support:
20 // - color support (colortbl, color)
21 // - decimal alignment (dcloumn)
22 // - custom lines (hhline)
28 // This is what I have written about tabular support in the LyX3-Tasks file:
30 // o rewrite of table code. Should probably be written as some
31 // kind of an inset. [Done]
32 // o enhance longtable support
36 #ifndef INSETTABULAR_H
37 #define INSETTABULAR_H
40 #include "InsetText.h"
43 #include "MailInset.h"
45 #include <boost/shared_ptr.hpp>
61 namespace frontend { class Painter; }
69 // A helper struct for tables
138 UNSET_ROTATE_TABULAR,
140 TOGGLE_ROTATE_TABULAR,
185 CELL_BEGIN_OF_MULTICOLUMN,
187 CELL_PART_OF_MULTICOLUMN
195 LYX_VALIGN_BOTTOM = 1,
197 LYX_VALIGN_MIDDLE = 2
213 // we have this header type (is set in the getLT... functions)
215 // double borders on top
217 // double borders on bottom
219 // used for FirstHeader & LastFooter and if this is true
220 // all the rows marked as FirstHeader or LastFooter are
221 // ignored in the output and it is set to be empty!
225 /// type for row numbers
226 typedef size_t row_type;
227 /// type for column numbers
228 typedef size_t col_type;
229 /// type for cell indices
230 typedef size_t idx_type;
231 /// index indicating an invalid position
232 static const idx_type npos = static_cast<idx_type>(-1);
237 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
239 /// Returns true if there is a topline, returns false if not
240 bool topLine(idx_type cell) const;
241 /// Returns true if there is a topline, returns false if not
242 bool bottomLine(idx_type cell) const;
243 /// Returns true if there is a topline, returns false if not
244 bool leftLine(idx_type cell) const;
245 /// Returns true if there is a topline, returns false if not
246 bool rightLine(idx_type cell) const;
249 bool topAlreadyDrawn(idx_type cell) const;
251 bool leftAlreadyDrawn(idx_type cell) const;
253 bool isLastRow(idx_type cell) const;
255 /// return space occupied by the second horizontal line and
256 /// interline space above row \p row in pixels
257 int getAdditionalHeight(row_type row) const;
259 int getAdditionalWidth(idx_type cell) const;
261 /* returns the maximum over all rows */
263 int columnWidth(idx_type cell) const;
269 int rowAscent(row_type row) const;
271 int rowDescent(row_type row) const;
273 void setRowAscent(row_type row, int height);
275 void setRowDescent(row_type row, int height);
277 void setCellWidth(idx_type cell, int new_width);
279 void setAllLines(idx_type cell, bool line);
281 void setTopLine(idx_type cell, bool line);
283 void setBottomLine(idx_type cell, bool line);
285 void setLeftLine(idx_type cell, bool line);
287 void setRightLine(idx_type cell, bool line);
289 bool rowTopLine(row_type row) const;
291 bool rowBottomLine(row_type row) const;
293 bool columnLeftLine(col_type column) const;
295 bool columnRightLine(col_type column) const;
297 void setAlignment(idx_type cell, LyXAlignment align,
298 bool onlycolumn = false);
300 void setVAlignment(idx_type cell, VAlignment align,
301 bool onlycolumn = false);
303 void setColumnPWidth(Cursor &, idx_type, Length const &);
305 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
307 void setAlignSpecial(idx_type cell, docstring const & special,
310 LyXAlignment getAlignment(idx_type cell,
311 bool onlycolumn = false) const;
313 VAlignment getVAlignment(idx_type cell,
314 bool onlycolumn = false) const;
316 Length const getPWidth(idx_type cell) const;
318 Length const getColumnPWidth(idx_type cell) const;
320 Length const getMColumnPWidth(idx_type cell) const;
322 docstring const getAlignSpecial(idx_type cell, int what) const;
324 int cellWidth(idx_type cell) const;
326 int getBeginningOfTextInCell(idx_type cell) const;
328 void appendRow(idx_type cell);
330 void deleteRow(row_type row);
332 void copyRow(row_type);
334 void appendColumn(idx_type cell);
336 void deleteColumn(col_type column);
338 void copyColumn(col_type);
340 bool isFirstCellInRow(idx_type cell) const;
342 idx_type getFirstCellInRow(row_type row) const;
344 bool isLastCellInRow(idx_type cell) const;
346 idx_type getLastCellInRow(row_type row) const;
348 idx_type cellCount() const;
350 idx_type numberOfCellsInRow(idx_type cell) const;
352 void write(std::ostream &) const;
356 int latex(odocstream &, OutputParams const &) const;
358 int docbook(odocstream & os, OutputParams const &) const;
360 void plaintext(odocstream &,
361 OutputParams const & runparams, int const depth,
362 bool onlydata, char_type delim) const;
364 bool isMultiColumn(idx_type cell) const;
366 bool isMultiColumnReal(idx_type cell) const;
368 void setMultiColumn(idx_type cell, idx_type number);
370 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
372 bool isPartOfMultiColumn(row_type row, col_type column) const;
374 row_type cellRow(idx_type cell) const;
376 col_type cellColumn(idx_type cell) const;
378 col_type cellRightColumn(idx_type cell) const;
380 void setBookTabs(bool);
382 bool useBookTabs() const;
384 void setLongTabular(bool);
386 bool isLongTabular() const;
388 void setRotateTabular(bool);
390 bool getRotateTabular() const;
392 void setRotateCell(idx_type cell, bool);
394 bool getRotateCell(idx_type cell) const;
396 bool needRotating() const;
398 bool isLastCell(idx_type cell) const;
400 idx_type getCellAbove(idx_type cell) const;
402 idx_type getCellBelow(idx_type cell) const;
404 idx_type getLastCellAbove(idx_type cell) const;
406 idx_type getLastCellBelow(idx_type cell) const;
408 idx_type cellIndex(row_type row, col_type column) const;
410 void setUsebox(idx_type cell, BoxType);
412 BoxType getUsebox(idx_type cell) const;
414 // Long Tabular Options support functions
416 bool checkLTType(row_type row, ltType const &) const;
418 void setLTHead(row_type row, bool flag, ltType const &, bool first);
420 bool getRowOfLTHead(row_type row, ltType &) const;
422 bool getRowOfLTFirstHead(row_type row, ltType &) const;
424 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
426 bool getRowOfLTFoot(row_type row, ltType &) const;
428 bool getRowOfLTLastFoot(row_type row, ltType &) const;
430 void setLTNewPage(row_type row, bool what);
432 bool getLTNewPage(row_type row) const;
434 bool haveLTHead() const;
436 bool haveLTFirstHead() const;
438 bool haveLTFoot() const;
440 bool haveLTLastFoot() const;
442 // end longtable support
444 boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
446 boost::shared_ptr<InsetText> getCellInset(row_type row,
447 col_type column) const;
449 void setCellInset(row_type row, col_type column,
450 boost::shared_ptr<InsetText>) const;
451 /// Search for \param inset in the tabular, with the
453 idx_type getCellFromInset(Inset const * inset) const;
455 row_type rowCount() const { return row_info.size(); }
457 col_type columnCount() const { return column_info.size();}
459 void validate(LaTeXFeatures &) const;
466 CellData(Buffer const &);
468 CellData(CellData const &);
470 CellData & operator=(CellData);
472 void swap(CellData & rhs);
480 LyXAlignment alignment;
482 VAlignment valignment;
496 docstring align_special;
498 Length p_width; // this is only set for multicolumn!!!
500 boost::shared_ptr<InsetText> inset;
502 CellData & cellinfo_of_cell(idx_type cell) const;
504 typedef std::vector<CellData> cell_vector;
506 typedef std::vector<cell_vector> cell_vvector;
521 /// Extra space between the top line and this row
523 /// Ignore top_space if true and use the default top space
524 bool top_space_default;
525 /// Extra space between this row and the bottom line
527 /// Ignore bottom_space if true and use the default bottom space
528 bool bottom_space_default;
529 /// Extra space between the bottom line and the next top line
530 Length interline_space;
531 /// Ignore interline_space if true and use the default interline space
532 bool interline_space_default;
533 /// This are for longtabulars only
536 /// a row of endfirsthead
540 /// row of endlastfoot
542 /// row for a newpage
546 typedef std::vector<RowData> row_vector;
554 LyXAlignment alignment;
556 VAlignment valignment;
566 docstring align_special;
569 typedef std::vector<ColumnData> column_vector;
572 idx_type numberofcells;
574 std::vector<row_type> rowofcell;
576 std::vector<col_type> columnofcell;
580 column_vector column_info;
582 mutable cell_vvector cell_info;
590 bool is_long_tabular;
593 /// endfirsthead data
601 void init(Buffer const &, row_type rows_arg,
602 col_type columns_arg);
604 void updateIndexes();
605 /// Returns true if a complete update is necessary, otherwise false
606 bool setWidthOfMulticolCell(idx_type cell, int new_width);
608 void recalculateMulticolumnsOfColumn(col_type column);
609 /// Returns true if change
610 void calculate_width_of_column(col_type column);
612 bool calculate_width_of_column_NMC(col_type column); // no multi cells
614 idx_type columnSpan(idx_type cell) const;
616 BoxType useParbox(idx_type cell) const;
618 // helper function for Latex returns number of newlines
620 int TeXTopHLine(odocstream &, row_type row) const;
622 int TeXBottomHLine(odocstream &, row_type row) const;
624 int TeXCellPreamble(odocstream &, idx_type cell) const;
626 int TeXCellPostamble(odocstream &, idx_type cell) const;
628 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
630 bool isValidRow(row_type const row) const;
632 int TeXRow(odocstream &, row_type const row,
633 OutputParams const &) const;
635 // helper functions for plain text
637 bool plaintextTopHLine(odocstream &, row_type row,
638 std::vector<unsigned int> const &) const;
640 bool plaintextBottomHLine(odocstream &, row_type row,
641 std::vector<unsigned int> const &) const;
643 void plaintextPrintCell(odocstream &,
644 OutputParams const &,
645 idx_type cell, row_type row, col_type column,
646 std::vector<unsigned int> const &,
647 bool onlydata) const;
648 /// auxiliary function for docbook
649 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
651 /// change associated Buffer
652 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
653 /// retrieve associated Buffer
654 Buffer const & buffer() const { return *buffer_; }
657 Buffer const * buffer_;
659 /// renumber cells after structural changes
665 class InsetTabular : public Inset {
668 InsetTabular(Buffer const &, row_type rows = 1,
669 col_type columns = 1);
675 void write(std::ostream &) const;
677 void metrics(MetricsInfo &, Dimension &) const;
679 void draw(PainterInfo & pi, int x, int y) const;
681 void drawSelection(PainterInfo & pi, int x, int y) const;
683 docstring editMessage() const;
685 EDITABLE editable() const { return HIGHLY_EDITABLE; }
687 bool insetAllowed(InsetCode code) const;
689 bool allowSpellCheck() const { return true; }
691 bool canTrackChanges() const { return true; }
692 /** returns true if, when outputing LaTeX, font changes should
693 be closed before generating this inset. This is needed for
694 insets that may contain several paragraphs */
695 bool noFontChange() const { return true; }
697 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
699 int latex(odocstream &, OutputParams const &) const;
701 int plaintext(odocstream &, OutputParams const &) const;
703 int docbook(odocstream &, OutputParams const &) const;
705 void validate(LaTeXFeatures & features) const;
707 InsetCode lyxCode() const { return TABULAR_CODE; }
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.cellCount(); }
723 boost::shared_ptr<InsetText const> cell(idx_type) const;
725 boost::shared_ptr<InsetText> 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 forceEmptyLayout() { return true; }
743 void addPreview(graphics::PreviewLoader &) const;
745 /// lock cell with given index
746 void edit(Cursor & cur, bool front, EntryDirection entry_from);
747 /// get table row from x coordinate
748 int rowFromY(Cursor & cur, int y) const;
749 /// get table column from y coordinate
750 int columnFromX(Cursor & cur, int x) const;
752 Inset * editXY(Cursor & cur, int x, int y);
753 /// can we go further down on mouse click?
754 bool descendable() const { return true; }
755 // Update the counters of this inset and of its contents
756 void updateLabels(ParIterator const &);
759 bool completionSupported(Cursor const &) const;
761 bool inlineCompletionSupported(Cursor const & cur) const;
763 bool automaticInlineCompletion() const;
765 bool automaticPopupCompletion() const;
767 bool showCompletionCursor() const;
769 CompletionList const * createCompletionList(Cursor const & cur) const;
771 docstring completionPrefix(Cursor const & cur) const;
773 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
775 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
778 // Public structures and variables
780 mutable Tabular tabular;
784 InsetTabular(InsetTabular const &);
786 void doDispatch(Cursor & cur, FuncRequest & cmd);
788 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
790 int scroll() const { return scx_; }
792 Inset * clone() const { return new InsetTabular(*this); }
795 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
796 idx_type cell, bool erased) const;
798 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
801 void moveNextCell(Cursor & cur);
803 void movePrevCell(Cursor & cur);
805 int getCellXPos(idx_type cell) const;
807 void resetPos(Cursor & cur) const;
809 void removeTabularRow();
811 bool copySelection(Cursor & cur);
813 bool pasteClipboard(Cursor & cur);
815 void cutSelection(Cursor & cur);
817 bool isRightToLeft(Cursor & cur) const;
819 void getSelection(Cursor & cur, row_type & rs, row_type & re,
820 col_type & cs, col_type & ce) const;
822 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
823 /// are we operating on several cells?
824 bool tablemode(Cursor & cur) const;
826 /// return the "Manhattan distance" to nearest corner
827 int dist(BufferView &, idx_type cell, int x, int y) const;
828 /// return the cell nearest to x, y
829 idx_type getNearestCell(BufferView &, int x, int y) const;
831 /// test the rotation state of the give cell range.
832 bool oneCellHasRotationState(bool rotated,
833 row_type row_start, row_type row_end,
834 col_type col_start, col_type col_end) const;
836 mutable idx_type first_visible_cell;
842 class InsetTabularMailer : public MailInset {
845 InsetTabularMailer(InsetTabular const & inset);
847 virtual Inset & inset() const { return inset_; }
849 virtual std::string const & name() const { return name_; }
851 virtual std::string const inset2string(Buffer const &) const;
853 static void string2params(std::string const &, InsetTabular &);
855 static std::string const params2string(InsetTabular const &);
858 static std::string const name_;
860 InsetTabular & inset_;
863 std::string const featureAsString(Tabular::Feature feature);