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 "MailInset.h"
42 #include "InsetText.h"
45 #include <boost/shared_ptr.hpp>
60 namespace frontend { class Painter; }
68 // A helper struct for tables
111 M_TOGGLE_LINE_BOTTOM,
145 UNSET_ROTATE_TABULAR,
147 TOGGLE_ROTATE_TABULAR,
192 CELL_BEGIN_OF_MULTICOLUMN,
194 CELL_PART_OF_MULTICOLUMN
202 LYX_VALIGN_BOTTOM = 1,
204 LYX_VALIGN_MIDDLE = 2
220 // we have this header type (is set in the getLT... functions)
222 // double borders on top
224 // double borders on bottom
226 // used for FirstHeader & LastFooter and if this is true
227 // all the rows marked as FirstHeader or LastFooter are
228 // ignored in the output and it is set to be empty!
232 /// type for row numbers
233 typedef size_t row_type;
234 /// type for column numbers
235 typedef size_t col_type;
236 /// type for cell indices
237 typedef size_t idx_type;
238 /// index indicating an invalid position
239 static const idx_type npos = static_cast<idx_type>(-1);
242 Tabular(BufferParams const &, col_type columns_arg,
245 /// Returns true if there is a topline, returns false if not
246 bool topLine(idx_type cell, bool wholerow = false) const;
247 /// Returns true if there is a topline, returns false if not
248 bool bottomLine(idx_type cell, bool wholerow = false) const;
249 /// Returns true if there is a topline, returns false if not
250 bool leftLine(idx_type cell, bool wholecolumn = false) const;
251 /// Returns true if there is a topline, returns false if not
252 bool rightLine(idx_type cell, bool wholecolumn = false) const;
255 bool topAlreadyDrawn(idx_type cell) const;
257 bool leftAlreadyDrawn(idx_type cell) const;
259 bool isLastRow(idx_type cell) const;
261 /// return space occupied by the second horizontal line and
262 /// interline space above row \p row in pixels
263 int getAdditionalHeight(row_type row) const;
265 int getAdditionalWidth(idx_type cell) const;
267 /* returns the maximum over all rows */
269 int getWidthOfColumn(idx_type cell) const;
271 int getWidthOfTabular() const;
273 int getAscentOfRow(row_type row) const;
275 int getDescentOfRow(row_type row) const;
277 int getHeightOfTabular() const;
279 void setAscentOfRow(row_type row, int height);
281 void setDescentOfRow(row_type row, int height);
283 void setWidthOfCell(idx_type cell, int new_width);
285 void setAllLines(idx_type cell, bool line);
287 void setTopLine(idx_type cell, bool line, bool wholerow = false);
289 void setBottomLine(idx_type cell, bool line, bool wholerow = false);
291 void setLeftLine(idx_type cell, bool line, bool wholecolumn = false);
293 void setRightLine(idx_type cell, bool line, bool wholecolumn = false);
295 void setAlignment(idx_type cell, LyXAlignment align,
296 bool onlycolumn = false);
298 void setVAlignment(idx_type cell, VAlignment align,
299 bool onlycolumn = false);
301 void setColumnPWidth(Cursor &, idx_type, Length const &);
303 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
305 void setAlignSpecial(idx_type cell, docstring const & special,
308 LyXAlignment getAlignment(idx_type cell,
309 bool onlycolumn = false) const;
311 VAlignment getVAlignment(idx_type cell,
312 bool onlycolumn = false) const;
314 Length const getPWidth(idx_type cell) const;
316 Length const getColumnPWidth(idx_type cell) const;
318 Length const getMColumnPWidth(idx_type cell) const;
320 docstring const getAlignSpecial(idx_type cell, int what) const;
322 int getWidthOfCell(idx_type cell) const;
324 int getBeginningOfTextInCell(idx_type cell) const;
326 void appendRow(BufferParams const &, idx_type cell);
328 void deleteRow(row_type row);
330 void copyRow(BufferParams const &, row_type);
332 void appendColumn(BufferParams const &, idx_type cell);
334 void deleteColumn(col_type column);
336 void copyColumn(BufferParams const &, col_type);
338 bool isFirstCellInRow(idx_type cell) const;
340 idx_type getFirstCellInRow(row_type row) const;
342 bool isLastCellInRow(idx_type cell) const;
344 idx_type getLastCellInRow(row_type row) const;
346 idx_type getNumberOfCells() const;
348 idx_type numberOfCellsInRow(idx_type cell) const;
350 void write(Buffer const &, std::ostream &) const;
352 void read(Buffer const &, Lexer &);
354 int latex(Buffer const &, odocstream &, OutputParams const &) const;
356 int docbook(Buffer const & buf, odocstream & os, OutputParams const &) const;
358 void plaintext(Buffer const &, odocstream &,
359 OutputParams const & runparams, int const depth,
360 bool onlydata, unsigned char delim) const;
362 bool isMultiColumn(idx_type cell) const;
364 bool isMultiColumnReal(idx_type cell) const;
366 void setMultiColumn(Buffer *, idx_type cell, idx_type number);
368 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
370 bool isPartOfMultiColumn(row_type row, col_type column) const;
372 row_type row_of_cell(idx_type cell) const;
374 col_type column_of_cell(idx_type cell) const;
376 col_type right_column_of_cell(idx_type cell) const;
378 void setBookTabs(bool);
380 bool useBookTabs() const;
382 void setLongTabular(bool);
384 bool isLongTabular() const;
386 void setRotateTabular(bool);
388 bool getRotateTabular() const;
390 void setRotateCell(idx_type cell, bool);
392 bool getRotateCell(idx_type cell) const;
394 bool needRotating() const;
396 bool isLastCell(idx_type cell) const;
398 idx_type getCellAbove(idx_type cell) const;
400 idx_type getCellBelow(idx_type cell) const;
402 idx_type getLastCellAbove(idx_type cell) const;
404 idx_type getLastCellBelow(idx_type cell) const;
406 idx_type getCellNumber(row_type row, col_type column) const;
408 void setUsebox(idx_type cell, BoxType);
410 BoxType getUsebox(idx_type cell) const;
412 // Long Tabular Options support functions
414 bool checkLTType(row_type row, ltType const &) const;
416 void setLTHead(row_type row, bool flag, ltType const &, bool first);
418 bool getRowOfLTHead(row_type row, ltType &) const;
420 bool getRowOfLTFirstHead(row_type row, ltType &) const;
422 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
424 bool getRowOfLTFoot(row_type row, ltType &) const;
426 bool getRowOfLTLastFoot(row_type row, ltType &) const;
428 void setLTNewPage(row_type row, bool what);
430 bool getLTNewPage(row_type row) const;
432 bool haveLTHead() const;
434 bool haveLTFirstHead() const;
436 bool haveLTFoot() const;
438 bool haveLTLastFoot() const;
440 // end longtable support
442 boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
444 boost::shared_ptr<InsetText> getCellInset(row_type row,
445 col_type column) const;
447 void setCellInset(row_type row, col_type column,
448 boost::shared_ptr<InsetText>) const;
449 /// Search for \param inset in the tabular, with the
451 idx_type getCellFromInset(Inset const * inset) const;
453 row_type rows() const { return rows_; }
455 col_type columns() const { return columns_;}
457 void validate(LaTeXFeatures &) const;
464 cellstruct(BufferParams const &);
466 cellstruct(cellstruct const &);
468 cellstruct & operator=(cellstruct);
470 void swap(cellstruct & rhs);
478 LyXAlignment alignment;
480 VAlignment valignment;
494 docstring align_special;
496 Length p_width; // this is only set for multicolumn!!!
498 boost::shared_ptr<InsetText> inset;
500 cellstruct & cellinfo_of_cell(idx_type cell) const;
502 typedef std::vector<cellstruct> cell_vector;
504 typedef std::vector<cell_vector> cell_vvector;
519 /// Extra space between the top line and this row
521 /// Ignore top_space if true and use the default top space
522 bool top_space_default;
523 /// Extra space between this row and the bottom line
525 /// Ignore bottom_space if true and use the default bottom space
526 bool bottom_space_default;
527 /// Extra space between the bottom line and the next top line
528 Length interline_space;
529 /// Ignore interline_space if true and use the default interline space
530 bool interline_space_default;
531 /// This are for longtabulars only
534 /// a row of endfirsthead
538 /// row of endlastfoot
540 /// row for a pagebreak
544 typedef std::vector<rowstruct> row_vector;
552 LyXAlignment alignment;
554 VAlignment valignment;
564 docstring align_special;
567 typedef std::vector<columnstruct> column_vector;
574 idx_type numberofcells;
576 std::vector<row_type> rowofcell;
578 std::vector<col_type> columnofcell;
582 column_vector column_info;
584 mutable cell_vvector cell_info;
586 int width_of_tabular;
594 bool is_long_tabular;
597 /// endfirsthead data
605 void init(BufferParams const &, row_type rows_arg,
606 col_type columns_arg);
608 void set_row_column_number_info();
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 void calculate_width_of_tabular();
620 void delete_column(col_type column);
622 idx_type cells_in_multicolumn(idx_type cell) const;
624 BoxType useParbox(idx_type cell) const;
626 // helper function for Latex returns number of newlines
628 int TeXTopHLine(odocstream &, row_type row) const;
630 int TeXBottomHLine(odocstream &, row_type row) const;
632 int TeXCellPreamble(odocstream &, idx_type cell) const;
634 int TeXCellPostamble(odocstream &, idx_type cell) const;
636 int TeXLongtableHeaderFooter(odocstream &, Buffer const & buf,
637 OutputParams const &) const;
639 bool isValidRow(row_type const row) const;
641 int TeXRow(odocstream &, row_type const row, Buffer const & buf,
642 OutputParams const &) const;
644 // helper functions for plain text
646 bool plaintextTopHLine(odocstream &, row_type row,
647 std::vector<unsigned int> const &) const;
649 bool plaintextBottomHLine(odocstream &, row_type row,
650 std::vector<unsigned int> const &) const;
652 void plaintextPrintCell(Buffer const &, odocstream &,
653 OutputParams const &,
654 idx_type cell, row_type row, col_type column,
655 std::vector<unsigned int> const &,
656 bool onlydata) const;
657 /// auxiliary function for docbook
658 int docbookRow(Buffer const & buf, odocstream & os, row_type,
659 OutputParams const &) const;
662 /// renumber cells after structural changes
668 class InsetTabular : public Inset {
671 InsetTabular(Buffer const &, row_type rows = 1,
672 col_type columns = 1);
676 void read(Buffer const &, Lexer &);
678 void write(Buffer const &, std::ostream &) const;
680 bool metrics(MetricsInfo &, Dimension &) const;
682 void draw(PainterInfo & pi, int x, int y) const;
684 void drawSelection(PainterInfo & pi, int x, int y) const;
686 virtual docstring const editMessage() const;
688 EDITABLE editable() const { return HIGHLY_EDITABLE; }
690 bool insetAllowed(Inset::Code) const { return true; }
692 bool allowSpellCheck() const { return true; }
694 bool canTrackChanges() const { return true; }
695 /** returns true if, when outputing LaTeX, font changes should
696 be closed before generating this inset. This is needed for
697 insets that may contain several paragraphs */
698 bool noFontChange() const { return true; }
700 DisplayType display() const { return tabular.isLongTabular() ? AlignCenter : Inline; }
702 int latex(Buffer const &, odocstream &,
703 OutputParams const &) const;
705 int plaintext(Buffer const &, odocstream &,
706 OutputParams const &) const;
708 int docbook(Buffer const &, odocstream &,
709 OutputParams const &) const;
711 void validate(LaTeXFeatures & features) const;
713 Code lyxCode() const { return Inset::TABULAR_CODE; }
714 /// get offset of this cursor slice relative to our upper left corner
715 void cursorPos(BufferView const & bv, CursorSlice const & sl,
716 bool boundary, int & x, int & y) const;
718 bool tabularFeatures(Cursor & cur, std::string const & what);
720 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
721 std::string const & val = std::string());
723 void openLayoutDialog(BufferView *) const;
725 bool showInsetDialog(BufferView *) const;
727 size_t nargs() const { return tabular.getNumberOfCells(); }
729 boost::shared_ptr<InsetText const> cell(idx_type) const;
731 boost::shared_ptr<InsetText> cell(idx_type);
733 Text * getText(int) const;
735 /// set the change for the entire inset
736 void setChange(Change const & change);
737 /// accept the changes within the inset
738 void acceptChanges(BufferParams const & bparams);
739 /// reject the changes within the inset
740 void rejectChanges(BufferParams const & bparams);
742 // this should return true if we have a "normal" cell, otherwise false.
743 // "normal" means without width set!
744 /// should all paragraphs be output with "Standard" layout?
745 bool forceDefaultParagraphs(idx_type cell = 0) const;
748 void addPreview(graphics::PreviewLoader &) const;
751 Buffer const & buffer() const;
753 /// set the owning buffer
754 void buffer(Buffer const * buf);
755 /// lock cell with given index
756 void edit(Cursor & cur, bool left);
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 virtual void updateLabels(Buffer const &, ParIterator const &);
765 // Public structures and variables
767 mutable Tabular tabular;
771 InsetTabular(InsetTabular const &);
773 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
775 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
777 int scroll() const { return scx_; }
780 virtual std::auto_ptr<Inset> doClone() const;
783 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
784 idx_type cell, bool erased) const;
786 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
789 void moveNextCell(Cursor & cur);
791 void movePrevCell(Cursor & cur);
793 int getCellXPos(idx_type cell) const;
795 void resetPos(Cursor & cur) const;
797 void removeTabularRow();
799 bool copySelection(Cursor & cur);
801 bool pasteClipboard(Cursor & cur);
803 void cutSelection(Cursor & cur);
805 bool isRightToLeft(Cursor & cur) const;
807 void getSelection(Cursor & cur, row_type & rs, row_type & re,
808 col_type & cs, col_type & ce) const;
810 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
811 /// are we operating on several cells?
812 bool tablemode(Cursor & cur) const;
814 /// return the "Manhattan distance" to nearest corner
815 int dist(BufferView &, idx_type cell, int x, int y) const;
816 /// return the cell nearest to x, y
817 idx_type getNearestCell(BufferView &, int x, int y) const;
819 /// test the rotation state of the give cell range.
820 bool oneCellHasRotationState(bool rotated,
821 row_type row_start, row_type row_end,
822 col_type col_start, col_type col_end) const;
824 Buffer const * buffer_;
826 mutable idx_type first_visible_cell;
832 class InsetTabularMailer : public MailInset {
835 InsetTabularMailer(InsetTabular const & inset);
837 virtual Inset & inset() const { return inset_; }
839 virtual std::string const & name() const { return name_; }
841 virtual std::string const inset2string(Buffer const &) const;
843 static void string2params(std::string const &, InsetTabular &);
845 static std::string const params2string(InsetTabular const &);
848 static std::string const name_;
850 InsetTabular & inset_;
853 std::string const featureAsString(Tabular::Feature feature);