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,
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(BufferParams const &, col_type columns_arg,
241 /// Returns true if there is a topline, returns false if not
242 bool topLine(idx_type cell, bool wholerow = false) const;
243 /// Returns true if there is a topline, returns false if not
244 bool bottomLine(idx_type cell, bool wholerow = false) const;
245 /// Returns true if there is a topline, returns false if not
246 bool leftLine(idx_type cell, bool wholecolumn = false) const;
247 /// Returns true if there is a topline, returns false if not
248 bool rightLine(idx_type cell, bool wholecolumn = false) const;
251 bool topAlreadyDrawn(idx_type cell) const;
253 bool leftAlreadyDrawn(idx_type cell) const;
255 bool isLastRow(idx_type cell) const;
257 /// return space occupied by the second horizontal line and
258 /// interline space above row \p row in pixels
259 int getAdditionalHeight(row_type row) const;
261 int getAdditionalWidth(idx_type cell) const;
263 /* returns the maximum over all rows */
265 int getWidthOfColumn(idx_type cell) const;
267 int getWidthOfTabular() const;
269 int getAscentOfRow(row_type row) const;
271 int getDescentOfRow(row_type row) const;
273 int getHeightOfTabular() const;
275 void setAscentOfRow(row_type row, int height);
277 void setDescentOfRow(row_type row, int height);
279 void setWidthOfCell(idx_type cell, int new_width);
281 void setAllLines(idx_type cell, bool line);
283 void setTopLine(idx_type cell, bool line, bool wholerow = false);
285 void setBottomLine(idx_type cell, bool line, bool wholerow = false);
287 void setLeftLine(idx_type cell, bool line, bool wholecolumn = false);
289 void setRightLine(idx_type cell, bool line, bool wholecolumn = false);
291 void setAlignment(idx_type cell, LyXAlignment align,
292 bool onlycolumn = false);
294 void setVAlignment(idx_type cell, VAlignment align,
295 bool onlycolumn = false);
297 void setColumnPWidth(Cursor &, idx_type, Length const &);
299 bool setMColumnPWidth(Cursor &, idx_type, Length const &);
301 void setAlignSpecial(idx_type cell, docstring const & special,
304 LyXAlignment getAlignment(idx_type cell,
305 bool onlycolumn = false) const;
307 VAlignment getVAlignment(idx_type cell,
308 bool onlycolumn = false) const;
310 Length const getPWidth(idx_type cell) const;
312 Length const getColumnPWidth(idx_type cell) const;
314 Length const getMColumnPWidth(idx_type cell) const;
316 docstring const getAlignSpecial(idx_type cell, int what) const;
318 int getWidthOfCell(idx_type cell) const;
320 int getBeginningOfTextInCell(idx_type cell) const;
322 void appendRow(BufferParams const &, idx_type cell);
324 void deleteRow(row_type row);
326 void copyRow(BufferParams const &, row_type);
328 void appendColumn(BufferParams const &, idx_type cell);
330 void deleteColumn(col_type column);
332 void copyColumn(BufferParams const &, col_type);
334 bool isFirstCellInRow(idx_type cell) const;
336 idx_type getFirstCellInRow(row_type row) const;
338 bool isLastCellInRow(idx_type cell) const;
340 idx_type getLastCellInRow(row_type row) const;
342 idx_type getNumberOfCells() const;
344 idx_type numberOfCellsInRow(idx_type cell) const;
346 void write(Buffer const &, std::ostream &) const;
348 void read(Buffer const &, Lexer &);
350 int latex(Buffer const &, odocstream &, OutputParams const &) const;
352 int docbook(Buffer const & buf, odocstream & os, OutputParams const &) const;
354 void plaintext(Buffer const &, odocstream &,
355 OutputParams const & runparams, int const depth,
356 bool onlydata, unsigned char delim) const;
358 bool isMultiColumn(idx_type cell) const;
360 bool isMultiColumnReal(idx_type cell) const;
362 void setMultiColumn(Buffer *, idx_type cell, idx_type number);
364 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
366 bool isPartOfMultiColumn(row_type row, col_type column) const;
368 row_type row_of_cell(idx_type cell) const;
370 col_type column_of_cell(idx_type cell) const;
372 col_type right_column_of_cell(idx_type cell) const;
374 void setBookTabs(bool);
376 bool useBookTabs() const;
378 void setLongTabular(bool);
380 bool isLongTabular() const;
382 void setRotateTabular(bool);
384 bool getRotateTabular() const;
386 void setRotateCell(idx_type cell, bool);
388 bool getRotateCell(idx_type cell) const;
390 bool needRotating() const;
392 bool isLastCell(idx_type cell) const;
394 idx_type getCellAbove(idx_type cell) const;
396 idx_type getCellBelow(idx_type cell) const;
398 idx_type getLastCellAbove(idx_type cell) const;
400 idx_type getLastCellBelow(idx_type cell) const;
402 idx_type getCellNumber(row_type row, col_type column) const;
404 void setUsebox(idx_type cell, BoxType);
406 BoxType getUsebox(idx_type cell) const;
408 // Long Tabular Options support functions
410 bool checkLTType(row_type row, ltType const &) const;
412 void setLTHead(row_type row, bool flag, ltType const &, bool first);
414 bool getRowOfLTHead(row_type row, ltType &) const;
416 bool getRowOfLTFirstHead(row_type row, ltType &) const;
418 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
420 bool getRowOfLTFoot(row_type row, ltType &) const;
422 bool getRowOfLTLastFoot(row_type row, ltType &) const;
424 void setLTNewPage(row_type row, bool what);
426 bool getLTNewPage(row_type row) const;
428 bool haveLTHead() const;
430 bool haveLTFirstHead() const;
432 bool haveLTFoot() const;
434 bool haveLTLastFoot() const;
436 // end longtable support
438 boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
440 boost::shared_ptr<InsetText> getCellInset(row_type row,
441 col_type column) const;
443 void setCellInset(row_type row, col_type column,
444 boost::shared_ptr<InsetText>) const;
445 /// Search for \param inset in the tabular, with the
447 idx_type getCellFromInset(Inset const * inset) const;
449 row_type rows() const { return rows_; }
451 col_type columns() const { return columns_;}
453 void validate(LaTeXFeatures &) const;
460 cellstruct(BufferParams const &);
462 cellstruct(cellstruct const &);
464 cellstruct & operator=(cellstruct);
466 void swap(cellstruct & rhs);
474 LyXAlignment alignment;
476 VAlignment valignment;
490 docstring align_special;
492 Length p_width; // this is only set for multicolumn!!!
494 boost::shared_ptr<InsetText> inset;
496 cellstruct & cellinfo_of_cell(idx_type cell) const;
498 typedef std::vector<cellstruct> cell_vector;
500 typedef std::vector<cell_vector> cell_vvector;
515 /// Extra space between the top line and this row
517 /// Ignore top_space if true and use the default top space
518 bool top_space_default;
519 /// Extra space between this row and the bottom line
521 /// Ignore bottom_space if true and use the default bottom space
522 bool bottom_space_default;
523 /// Extra space between the bottom line and the next top line
524 Length interline_space;
525 /// Ignore interline_space if true and use the default interline space
526 bool interline_space_default;
527 /// This are for longtabulars only
530 /// a row of endfirsthead
534 /// row of endlastfoot
536 /// row for a pagebreak
540 typedef std::vector<rowstruct> row_vector;
548 LyXAlignment alignment;
550 VAlignment valignment;
560 docstring align_special;
563 typedef std::vector<columnstruct> column_vector;
570 idx_type numberofcells;
572 std::vector<row_type> rowofcell;
574 std::vector<col_type> columnofcell;
578 column_vector column_info;
580 mutable cell_vvector cell_info;
582 int width_of_tabular;
590 bool is_long_tabular;
593 /// endfirsthead data
601 void init(BufferParams const &, row_type rows_arg,
602 col_type columns_arg);
604 void set_row_column_number_info();
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 void calculate_width_of_tabular();
616 void delete_column(col_type column);
618 idx_type cells_in_multicolumn(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 &, Buffer const & buf,
633 OutputParams const &) const;
635 bool isValidRow(row_type const row) const;
637 int TeXRow(odocstream &, row_type const row, Buffer const & buf,
638 OutputParams const &) const;
640 // helper functions for plain text
642 bool plaintextTopHLine(odocstream &, row_type row,
643 std::vector<unsigned int> const &) const;
645 bool plaintextBottomHLine(odocstream &, row_type row,
646 std::vector<unsigned int> const &) const;
648 void plaintextPrintCell(Buffer const &, odocstream &,
649 OutputParams const &,
650 idx_type cell, row_type row, col_type column,
651 std::vector<unsigned int> const &,
652 bool onlydata) const;
653 /// auxiliary function for docbook
654 int docbookRow(Buffer const & buf, odocstream & os, row_type,
655 OutputParams const &) const;
658 /// renumber cells after structural changes
664 class InsetTabular : public Inset {
667 InsetTabular(Buffer const &, row_type rows = 1,
668 col_type columns = 1);
672 void read(Buffer const &, Lexer &);
674 void write(Buffer const &, std::ostream &) const;
676 bool metrics(MetricsInfo &, Dimension &) const;
678 void draw(PainterInfo & pi, int x, int y) const;
680 void drawSelection(PainterInfo & pi, int x, int y) const;
682 virtual docstring const editMessage() const;
684 EDITABLE editable() const { return HIGHLY_EDITABLE; }
686 bool insetAllowed(Inset::Code) const { return true; }
688 bool allowSpellCheck() const { return true; }
690 bool canTrackChanges() const { return true; }
691 /** returns true if, when outputing LaTeX, font changes should
692 be closed before generating this inset. This is needed for
693 insets that may contain several paragraphs */
694 bool noFontChange() const { return true; }
696 bool display() const { return tabular.isLongTabular(); }
698 int latex(Buffer const &, odocstream &,
699 OutputParams const &) const;
701 int plaintext(Buffer const &, odocstream &,
702 OutputParams const &) const;
704 int docbook(Buffer const &, odocstream &,
705 OutputParams const &) const;
707 void validate(LaTeXFeatures & features) const;
709 Code lyxCode() const { return Inset::TABULAR_CODE; }
710 /// get offset of this cursor slice relative to our upper left corner
711 void cursorPos(BufferView const & bv, CursorSlice const & sl,
712 bool boundary, int & x, int & y) const;
714 bool tabularFeatures(Cursor & cur, std::string const & what);
716 void tabularFeatures(Cursor & cur, Tabular::Feature feature,
717 std::string const & val = std::string());
719 void openLayoutDialog(BufferView *) const;
721 bool showInsetDialog(BufferView *) const;
723 size_t nargs() const { return tabular.getNumberOfCells(); }
725 boost::shared_ptr<InsetText const> cell(idx_type) const;
727 boost::shared_ptr<InsetText> cell(idx_type);
729 LyXText * getText(int) const;
731 /// set the change for the entire inset
732 void setChange(Change const & change);
733 /// accept the changes within the inset
734 void acceptChanges(BufferParams const & bparams);
735 /// reject the changes within the inset
736 void rejectChanges(BufferParams const & bparams);
738 // this should return true if we have a "normal" cell, otherwise false.
739 // "normal" means without width set!
740 /// should all paragraphs be output with "Standard" layout?
741 bool forceDefaultParagraphs(idx_type cell = 0) const;
744 void addPreview(graphics::PreviewLoader &) const;
747 Buffer const & buffer() const;
749 /// set the owning buffer
750 void buffer(Buffer const * buf);
751 /// lock cell with given index
752 void edit(Cursor & cur, bool left);
754 Inset * editXY(Cursor & cur, int x, int y);
755 /// can we go further down on mouse click?
756 bool descendable() const { return true; }
759 // Public structures and variables
761 mutable Tabular tabular;
765 InsetTabular(InsetTabular const &);
767 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
769 bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus &) const;
771 int scroll() const { return scx_; }
774 virtual std::auto_ptr<Inset> doClone() const;
777 void drawCellLines(frontend::Painter &, int x, int y, row_type row,
778 idx_type cell, bool erased) const;
780 void setCursorFromCoordinates(Cursor & cur, int x, int y) const;
783 void moveNextCell(Cursor & cur);
785 void movePrevCell(Cursor & cur);
787 int getCellXPos(idx_type cell) const;
789 void resetPos(Cursor & cur) const;
791 void removeTabularRow();
793 bool copySelection(Cursor & cur);
795 bool pasteClipboard(Cursor & cur);
797 void cutSelection(Cursor & cur);
799 bool isRightToLeft(Cursor & cur) const;
801 void getSelection(Cursor & cur, row_type & rs, row_type & re,
802 col_type & cs, col_type & ce) const;
804 bool insertPlaintextString(BufferView &, docstring const & buf, bool usePaste);
805 /// are we operating on several cells?
806 bool tablemode(Cursor & cur) const;
808 /// return the "Manhattan distance" to nearest corner
809 int dist(BufferView &, idx_type cell, int x, int y) const;
810 /// return the cell nearest to x, y
811 idx_type getNearestCell(BufferView &, int x, int y) const;
814 Buffer const * buffer_;
816 mutable idx_type first_visible_cell;
819 /// Ugly boolean used when this inset is dissolved and
820 /// InsetTabularMailer should not be used.
825 class InsetTabularMailer : public MailInset {
828 InsetTabularMailer(InsetTabular const & inset);
830 virtual Inset & inset() const { return inset_; }
832 virtual std::string const & name() const { return name_; }
834 virtual std::string const inset2string(Buffer const &) const;
836 static void string2params(std::string const &, InsetTabular &);
838 static std::string const params2string(InsetTabular const &);
841 static std::string const name_;
843 InsetTabular & inset_;
846 std::string const featureAsString(Tabular::Feature feature);