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; }
68 typedef InsetText InsetTableCell;
71 // A helper struct for tables
140 UNSET_ROTATE_TABULAR,
142 TOGGLE_ROTATE_TABULAR,
189 CELL_BEGIN_OF_MULTICOLUMN,
191 CELL_PART_OF_MULTICOLUMN
199 LYX_VALIGN_BOTTOM = 1,
201 LYX_VALIGN_MIDDLE = 2
217 // we have this header type (is set in the getLT... functions)
219 // double borders on top
221 // double borders on bottom
223 // used for FirstHeader & LastFooter and if this is true
224 // all the rows marked as FirstHeader or LastFooter are
225 // ignored in the output and it is set to be empty!
229 /// type for row numbers
230 typedef size_t row_type;
231 /// type for column numbers
232 typedef size_t col_type;
233 /// type for cell indices
234 typedef size_t idx_type;
235 /// index indicating an invalid position
236 static const idx_type npos = static_cast<idx_type>(-1);
241 Tabular(Buffer const &, col_type columns_arg, row_type rows_arg);
243 /// Returns true if there is a topline, returns false if not
244 bool topLine(idx_type cell) const;
245 /// Returns true if there is a topline, returns false if not
246 bool bottomLine(idx_type cell) const;
247 /// Returns true if there is a topline, returns false if not
248 bool leftLine(idx_type cell) const;
249 /// Returns true if there is a topline, returns false if not
250 bool rightLine(idx_type cell) const;
253 bool topAlreadyDrawn(idx_type cell) const;
255 bool leftAlreadyDrawn(idx_type cell) const;
257 bool isLastRow(idx_type cell) const;
259 /// return space occupied by the second horizontal line and
260 /// interline space above row \p row in pixels
261 int getAdditionalHeight(row_type row) const;
263 int getAdditionalWidth(idx_type cell) const;
265 /* returns the maximum over all rows */
267 int columnWidth(idx_type cell) const;
273 int rowAscent(row_type row) const;
275 int rowDescent(row_type row) const;
277 void setRowAscent(row_type row, int height);
279 void setRowDescent(row_type row, int height);
281 void setCellWidth(idx_type cell, int new_width);
283 void setAllLines(idx_type cell, bool line);
285 void setTopLine(idx_type cell, bool line);
287 void setBottomLine(idx_type cell, bool line);
289 void setLeftLine(idx_type cell, bool line);
291 void setRightLine(idx_type cell, bool line);
293 bool rowTopLine(row_type row) const;
295 bool rowBottomLine(row_type row) const;
297 bool columnLeftLine(col_type column) const;
299 bool columnRightLine(col_type column) const;
301 void setAlignment(idx_type cell, LyXAlignment align,
302 bool onlycolumn = false);
304 void setVAlignment(idx_type cell, VAlignment align,
305 bool onlycolumn = false);
307 void setColumnPWidth(Cursor &, idx_type, Length const &);
309 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
311 void setAlignSpecial(idx_type cell, docstring const & special,
314 LyXAlignment getAlignment(idx_type cell,
315 bool onlycolumn = false) const;
317 VAlignment getVAlignment(idx_type cell,
318 bool onlycolumn = false) const;
320 Length const getPWidth(idx_type cell) const;
322 Length const getColumnPWidth(idx_type cell) const;
324 Length const getMColumnPWidth(idx_type cell) const;
326 docstring const getAlignSpecial(idx_type cell, int what) const;
328 int cellWidth(idx_type cell) const;
330 int getBeginningOfTextInCell(idx_type cell) const;
332 void appendRow(idx_type cell);
334 void deleteRow(row_type row);
336 void copyRow(row_type);
338 void appendColumn(idx_type cell);
340 void deleteColumn(col_type column);
342 void copyColumn(col_type);
344 bool isFirstCellInRow(idx_type cell) const;
346 idx_type getFirstCellInRow(row_type row) const;
348 bool isLastCellInRow(idx_type cell) const;
350 idx_type getLastCellInRow(row_type row) const;
352 idx_type cellCount() const;
354 idx_type numberOfCellsInRow(idx_type cell) const;
356 void write(std::ostream &) const;
360 int latex(odocstream &, OutputParams const &) const;
362 int docbook(odocstream & os, OutputParams const &) const;
364 void plaintext(odocstream &,
365 OutputParams const & runparams, int const depth,
366 bool onlydata, char_type delim) const;
368 bool isMultiColumn(idx_type cell) const;
370 bool isMultiColumnReal(idx_type cell) const;
372 void setMultiColumn(idx_type cell, idx_type number);
374 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
376 bool isPartOfMultiColumn(row_type row, col_type column) const;
378 row_type cellRow(idx_type cell) const;
380 col_type cellColumn(idx_type cell) const;
382 col_type cellRightColumn(idx_type cell) const;
384 void setBookTabs(bool);
386 bool useBookTabs() const;
388 void setLongTabular(bool);
390 bool isLongTabular() const;
392 void setRotateTabular(bool);
394 bool getRotateTabular() const;
396 void setRotateCell(idx_type cell, bool);
398 bool getRotateCell(idx_type cell) const;
400 bool needRotating() const;
402 bool isLastCell(idx_type cell) const;
404 idx_type getCellAbove(idx_type cell) const;
406 idx_type getCellBelow(idx_type cell) const;
408 idx_type getLastCellAbove(idx_type cell) const;
410 idx_type getLastCellBelow(idx_type cell) const;
412 idx_type cellIndex(row_type row, col_type column) const;
414 void setUsebox(idx_type cell, BoxType);
416 BoxType getUsebox(idx_type cell) const;
418 // Long Tabular Options support functions
420 bool checkLTType(row_type row, ltType const &) const;
422 void setLTHead(row_type row, bool flag, ltType const &, bool first);
424 bool getRowOfLTHead(row_type row, ltType &) const;
426 bool getRowOfLTFirstHead(row_type row, ltType &) const;
428 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
430 bool getRowOfLTFoot(row_type row, ltType &) const;
432 bool getRowOfLTLastFoot(row_type row, ltType &) const;
434 void setLTNewPage(row_type row, bool what);
436 bool getLTNewPage(row_type row) const;
438 bool haveLTHead() const;
440 bool haveLTFirstHead() const;
442 bool haveLTFoot() const;
444 bool haveLTLastFoot() const;
446 // end longtable support
448 boost::shared_ptr<InsetTableCell> getCellInset(idx_type cell) const;
450 boost::shared_ptr<InsetTableCell> getCellInset(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 idx_type getCellFromInset(Inset const * inset) const;
459 row_type rowCount() const { return row_info.size(); }
461 col_type columnCount() const { return column_info.size();}
463 void validate(LaTeXFeatures &) const;
470 CellData(Buffer const &);
472 CellData(CellData const &);
474 CellData & operator=(CellData);
476 void swap(CellData & rhs);
484 LyXAlignment alignment;
486 VAlignment valignment;
500 docstring align_special;
502 Length p_width; // this is only set for multicolumn!!!
504 boost::shared_ptr<InsetTableCell> inset;
506 CellData & cellinfo_of_cell(idx_type cell) const;
508 typedef std::vector<CellData> cell_vector;
510 typedef std::vector<cell_vector> cell_vvector;
525 /// Extra space between the top line and this row
527 /// Ignore top_space if true and use the default top space
528 bool top_space_default;
529 /// Extra space between this row and the bottom line
531 /// Ignore bottom_space if true and use the default bottom space
532 bool bottom_space_default;
533 /// Extra space between the bottom line and the next top line
534 Length interline_space;
535 /// Ignore interline_space if true and use the default interline space
536 bool interline_space_default;
537 /// This are for longtabulars only
540 /// a row of endfirsthead
544 /// row of endlastfoot
546 /// row for a newpage
550 typedef std::vector<RowData> row_vector;
558 LyXAlignment alignment;
560 VAlignment valignment;
570 docstring align_special;
573 typedef std::vector<ColumnData> column_vector;
576 idx_type numberofcells;
578 std::vector<row_type> rowofcell;
580 std::vector<col_type> columnofcell;
584 column_vector column_info;
586 mutable cell_vvector cell_info;
594 bool is_long_tabular;
597 /// endfirsthead data
605 void init(Buffer const &, row_type rows_arg,
606 col_type columns_arg);
608 void updateIndexes();
609 /// Returns true if a complete update is necessary, otherwise false
610 bool setWidthOfMulticolCell(idx_type cell, int new_width);
612 void recalculateMulticolumnsOfColumn(col_type column);
613 /// Returns true if change
614 void calculate_width_of_column(col_type column);
616 bool calculate_width_of_column_NMC(col_type column); // no multi cells
618 idx_type columnSpan(idx_type cell) const;
620 BoxType useParbox(idx_type cell) const;
622 // helper function for Latex returns number of newlines
624 int TeXTopHLine(odocstream &, row_type row) const;
626 int TeXBottomHLine(odocstream &, row_type row) const;
628 int TeXCellPreamble(odocstream &, idx_type cell) const;
630 int TeXCellPostamble(odocstream &, idx_type cell) const;
632 int TeXLongtableHeaderFooter(odocstream &, OutputParams const &) const;
634 bool isValidRow(row_type const row) const;
636 int TeXRow(odocstream &, row_type const row,
637 OutputParams const &) const;
639 // helper functions for plain text
641 bool plaintextTopHLine(odocstream &, row_type row,
642 std::vector<unsigned int> const &) const;
644 bool plaintextBottomHLine(odocstream &, row_type row,
645 std::vector<unsigned int> const &) const;
647 void plaintextPrintCell(odocstream &,
648 OutputParams const &,
649 idx_type cell, row_type row, col_type column,
650 std::vector<unsigned int> const &,
651 bool onlydata) const;
652 /// auxiliary function for docbook
653 int docbookRow(odocstream & os, row_type, OutputParams const &) const;
655 /// change associated Buffer
656 void setBuffer(Buffer const & buffer) { buffer_ = &buffer; }
657 /// retrieve associated Buffer
658 Buffer const & buffer() const { return *buffer_; }
661 Buffer const * buffer_;
663 /// renumber cells after structural changes
669 class InsetTabular : public Inset {
672 InsetTabular(Buffer const &, row_type rows = 1,
673 col_type columns = 1);
679 void write(std::ostream &) const;
681 void metrics(MetricsInfo &, Dimension &) const;
683 void draw(PainterInfo & pi, int x, int y) const;
685 void drawSelection(PainterInfo & pi, int x, int y) const;
687 docstring editMessage() const;
689 EDITABLE editable() const { return HIGHLY_EDITABLE; }
691 bool insetAllowed(InsetCode code) const;
693 bool allowSpellCheck() const { return true; }
695 bool canTrackChanges() const { return true; }
696 /** returns true if, when outputing LaTeX, font changes should
697 be closed before generating this inset. This is needed for
698 insets that may contain several paragraphs */
699 bool noFontChange() const { return true; }
701 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
703 int latex(odocstream &, OutputParams const &) const;
705 int plaintext(odocstream &, OutputParams const &) const;
707 int docbook(odocstream &, OutputParams const &) const;
709 void validate(LaTeXFeatures & features) const;
711 InsetCode lyxCode() const { return TABULAR_CODE; }
712 /// get offset of this cursor slice relative to our upper left corner
713 void cursorPos(BufferView const & bv, CursorSlice const & sl,
714 bool boundary, int & x, int & y) const;
716 bool tabularFeatures(Cursor & cur, std::string const & what);
718 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
719 std::string const & val = std::string());
721 void openLayoutDialog(BufferView *) const;
723 bool showInsetDialog(BufferView *) const;
725 size_t nargs() const { return tabular.cellCount(); }
727 boost::shared_ptr<InsetTableCell const> cell(idx_type) const;
729 boost::shared_ptr<InsetTableCell> cell(idx_type);
731 Text * getText(int) const;
733 /// set the change for the entire inset
734 void setChange(Change const & change);
735 /// accept the changes within the inset
736 void acceptChanges(BufferParams const & bparams);
737 /// reject the changes within the inset
738 void rejectChanges(BufferParams const & bparams);
740 // this should return true if we have a "normal" cell, otherwise false.
741 // "normal" means without width set!
742 /// should all paragraphs be output with "Standard" layout?
743 virtual bool allowParagraphCustomization(idx_type cell = 0) const;
745 virtual bool forceEmptyLayout(idx_type cell = 0) const;
747 virtual bool useEmptyLayout() { return true; }
749 void addPreview(graphics::PreviewLoader &) const;
751 /// lock cell with given index
752 void edit(Cursor & cur, bool front, EntryDirection entry_from);
753 /// get table row from x coordinate
754 int rowFromY(Cursor & cur, int y) const;
755 /// get table column from y coordinate
756 int columnFromX(Cursor & cur, int x) const;
758 Inset * editXY(Cursor & cur, int x, int y);
759 /// can we go further down on mouse click?
760 bool descendable() const { return true; }
761 // Update the counters of this inset and of its contents
762 void updateLabels(ParIterator const &);
765 bool completionSupported(Cursor const &) const;
767 bool inlineCompletionSupported(Cursor const & cur) const;
769 bool automaticInlineCompletion() const;
771 bool automaticPopupCompletion() const;
773 bool showCompletionCursor() const;
775 CompletionList const * createCompletionList(Cursor const & cur) const;
777 docstring completionPrefix(Cursor const & cur) const;
779 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
781 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;
784 // Public structures and variables
786 mutable Tabular tabular;
790 InsetTabular(InsetTabular const &);
792 void doDispatch(Cursor & cur, FuncRequest & cmd);
794 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
796 int scroll() const { return scx_; }
798 Inset * clone() const { return new InsetTabular(*this); }
801 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
802 idx_type cell, bool erased) const;
804 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
807 void moveNextCell(Cursor & cur);
809 void movePrevCell(Cursor & cur);
811 int getCellXPos(idx_type cell) const;
813 void resetPos(Cursor & cur) const;
815 void removeTabularRow();
817 bool copySelection(Cursor & cur);
819 bool pasteClipboard(Cursor & cur);
821 void cutSelection(Cursor & cur);
823 bool isRightToLeft(Cursor & cur) const;
825 void getSelection(Cursor & cur, row_type & rs, row_type & re,
826 col_type & cs, col_type & ce) const;
828 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
829 /// are we operating on several cells?
830 bool tablemode(Cursor & cur) const;
832 /// return the "Manhattan distance" to nearest corner
833 int dist(BufferView &, idx_type cell, int x, int y) const;
834 /// return the cell nearest to x, y
835 idx_type getNearestCell(BufferView &, int x, int y) const;
837 /// test the rotation state of the give cell range.
838 bool oneCellHasRotationState(bool rotated,
839 row_type row_start, row_type row_end,
840 col_type col_start, col_type col_end) const;
842 mutable idx_type first_visible_cell;
848 class InsetTabularMailer : public MailInset {
851 InsetTabularMailer(InsetTabular const & inset);
853 virtual Inset & inset() const { return inset_; }
855 virtual std::string const & name() const { return name_; }
857 virtual std::string const inset2string(Buffer const &) const;
859 static void string2params(std::string const &, InsetTabular &);
861 static std::string const params2string(InsetTabular const &);
864 static std::string const name_;
866 InsetTabular & inset_;
869 std::string const featureAsString(Tabular::Feature feature);