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.
18 #include "lyxlength.h"
19 #include "insets/insettext.h"
21 #include <boost/shared_ptr.hpp>
30 /* The features the text class offers for tables */
108 UNSET_ROTATE_TABULAR,
151 CELL_BEGIN_OF_MULTICOLUMN,
153 CELL_PART_OF_MULTICOLUMN
161 LYX_VALIGN_BOTTOM = 1,
163 LYX_VALIGN_MIDDLE = 2
179 // we have this header type (is set in the getLT... functions)
181 // double borders on top
183 // double borders on bottom
185 // used for FirstHeader & LastFooter and if this is true
186 // all the rows marked as FirstHeader or LastFooter are
187 // ignored in the output and it is set to be empty!
191 /// type for row numbers
192 typedef size_t row_type;
193 /// type for column numbers
194 typedef size_t col_type;
195 /// type for cell indices
196 typedef size_t idx_type;
197 /// index indicating an invalid position
198 static const idx_type npos = static_cast<idx_type>(-1);
201 LyXTabular(BufferParams const &, col_type columns_arg,
204 /// Returns true if there is a topline, returns false if not
205 bool topLine(idx_type cell, bool onlycolumn = false) const;
206 /// Returns true if there is a topline, returns false if not
207 bool bottomLine(idx_type cell, bool onlycolumn = false) const;
208 /// Returns true if there is a topline, returns false if not
209 bool leftLine(idx_type cell, bool onlycolumn = false) const;
210 /// Returns true if there is a topline, returns false if not
211 bool rightLine(idx_type cell, bool onlycolumn = false) const;
214 bool topAlreadyDrawn(idx_type cell) const;
216 bool leftAlreadyDrawn(idx_type cell) const;
218 bool isLastRow(idx_type cell) const;
220 /// return space occupied by the second horizontal line and
221 /// interline space above row \p row in pixels
222 int getAdditionalHeight(row_type row) const;
224 int getAdditionalWidth(idx_type cell) const;
226 /* returns the maximum over all rows */
228 int getWidthOfColumn(idx_type cell) const;
230 int getWidthOfTabular() const;
232 int getAscentOfRow(row_type row) const;
234 int getDescentOfRow(row_type row) const;
236 int getHeightOfTabular() const;
238 void setAscentOfRow(row_type row, int height);
240 void setDescentOfRow(row_type row, int height);
242 void setWidthOfCell(idx_type cell, int new_width);
244 void setAllLines(idx_type cell, bool line);
246 void setTopLine(idx_type cell, bool line, bool onlycolumn = false);
248 void setBottomLine(idx_type cell, bool line, bool onlycolumn = false);
250 void setLeftLine(idx_type cell, bool line, bool onlycolumn = false);
252 void setRightLine(idx_type cell, bool line, bool onlycolumn = false);
254 void setAlignment(idx_type cell, LyXAlignment align,
255 bool onlycolumn = false);
257 void setVAlignment(idx_type cell, VAlignment align,
258 bool onlycolumn = false);
260 void setColumnPWidth(LCursor &, idx_type, LyXLength const &);
262 bool setMColumnPWidth(LCursor &, idx_type, LyXLength const &);
264 void setAlignSpecial(idx_type cell, std::string const & special,
267 LyXAlignment getAlignment(idx_type cell,
268 bool onlycolumn = false) const;
270 VAlignment getVAlignment(idx_type cell,
271 bool onlycolumn = false) const;
273 LyXLength const getPWidth(idx_type cell) const;
275 LyXLength const getColumnPWidth(idx_type cell) const;
277 LyXLength const getMColumnPWidth(idx_type cell) const;
279 std::string const getAlignSpecial(idx_type cell, int what) const;
281 int getWidthOfCell(idx_type cell) const;
283 int getBeginningOfTextInCell(idx_type cell) const;
285 void appendRow(BufferParams const &, idx_type cell);
287 void deleteRow(row_type row);
289 void copyRow(BufferParams const &, row_type);
291 void appendColumn(BufferParams const &, idx_type cell);
293 void deleteColumn(col_type column);
295 void copyColumn(BufferParams const &, col_type);
297 bool isFirstCellInRow(idx_type cell) const;
299 idx_type getFirstCellInRow(row_type row) const;
301 bool isLastCellInRow(idx_type cell) const;
303 idx_type getLastCellInRow(row_type row) const;
305 idx_type getNumberOfCells() const;
307 idx_type numberOfCellsInRow(idx_type cell) const;
309 void write(Buffer const &, std::ostream &) const;
311 void read(Buffer const &, LyXLex &);
313 int latex(Buffer const &, lyx::odocstream &,
314 OutputParams const &) const;
316 int docbook(Buffer const & buf, std::ostream & os,
317 OutputParams const &) const;
319 int plaintext(Buffer const &, lyx::odocstream &,
320 OutputParams const & runparams,
322 bool onlydata, unsigned char delim) const;
324 bool isMultiColumn(idx_type cell) const;
326 bool isMultiColumnReal(idx_type cell) const;
328 void setMultiColumn(Buffer *, idx_type cell, idx_type number);
330 idx_type unsetMultiColumn(idx_type cell); // returns number of new cells
332 bool isPartOfMultiColumn(row_type row, col_type column) const;
334 row_type row_of_cell(idx_type cell) const;
336 col_type column_of_cell(idx_type cell) const;
338 col_type right_column_of_cell(idx_type cell) const;
340 void setBookTabs(bool);
342 bool useBookTabs() const;
344 void setLongTabular(bool);
346 bool isLongTabular() const;
348 void setRotateTabular(bool);
350 bool getRotateTabular() const;
352 void setRotateCell(idx_type cell, bool);
354 bool getRotateCell(idx_type cell) const;
356 bool needRotating() const;
358 bool isLastCell(idx_type cell) const;
360 idx_type getCellAbove(idx_type cell) const;
362 idx_type getCellBelow(idx_type cell) const;
364 idx_type getLastCellAbove(idx_type cell) const;
366 idx_type getLastCellBelow(idx_type cell) const;
368 idx_type getCellNumber(row_type row, col_type column) const;
370 void setUsebox(idx_type cell, BoxType);
372 BoxType getUsebox(idx_type cell) const;
374 // Long Tabular Options support functions
376 bool checkLTType(row_type row, ltType const &) const;
378 void setLTHead(row_type row, bool flag, ltType const &, bool first);
380 bool getRowOfLTHead(row_type row, ltType &) const;
382 bool getRowOfLTFirstHead(row_type row, ltType &) const;
384 void setLTFoot(row_type row, bool flag, ltType const &, bool last);
386 bool getRowOfLTFoot(row_type row, ltType &) const;
388 bool getRowOfLTLastFoot(row_type row, ltType &) const;
390 void setLTNewPage(row_type row, bool what);
392 bool getLTNewPage(row_type row) const;
394 bool haveLTHead() const;
396 bool haveLTFirstHead() const;
398 bool haveLTFoot() const;
400 bool haveLTLastFoot() const;
402 // end longtable support
404 boost::shared_ptr<InsetText> getCellInset(idx_type cell) const;
406 boost::shared_ptr<InsetText> getCellInset(row_type row,
407 col_type column) const;
409 void setCellInset(row_type row, col_type column,
410 boost::shared_ptr<InsetText>) const;
411 /// Search for \param inset in the tabular, with the
413 idx_type getCellFromInset(InsetBase const * inset) const;
415 row_type rows() const { return rows_; }
417 col_type columns() const { return columns_;}
419 void validate(LaTeXFeatures &) const;
426 cellstruct(BufferParams const &);
428 cellstruct(cellstruct const &);
430 cellstruct & operator=(cellstruct);
432 void swap(cellstruct & rhs);
440 LyXAlignment alignment;
442 VAlignment valignment;
456 std::string align_special;
458 LyXLength p_width; // this is only set for multicolumn!!!
460 boost::shared_ptr<InsetText> inset;
462 cellstruct & cellinfo_of_cell(idx_type cell) const;
464 typedef std::vector<cellstruct> cell_vector;
466 typedef std::vector<cell_vector> cell_vvector;
481 /// Extra space between the top line and this row
483 /// Ignore top_space if true and use the default top space
484 bool top_space_default;
485 /// Extra space between this row and the bottom line
486 LyXLength bottom_space;
487 /// Ignore bottom_space if true and use the default bottom space
488 bool bottom_space_default;
489 /// Extra space between the bottom line and the next top line
490 LyXLength interline_space;
491 /// Ignore interline_space if true and use the default interline space
492 bool interline_space_default;
493 /// This are for longtabulars only
496 /// a row of endfirsthead
500 /// row of endlastfoot
502 /// row for a pagebreak
506 typedef std::vector<rowstruct> row_vector;
514 LyXAlignment alignment;
516 VAlignment valignment;
526 std::string align_special;
529 typedef std::vector<columnstruct> column_vector;
536 idx_type numberofcells;
538 std::vector<row_type> rowofcell;
540 std::vector<col_type> columnofcell;
544 column_vector column_info;
546 mutable cell_vvector cell_info;
548 int width_of_tabular;
556 bool is_long_tabular;
559 /// endfirsthead data
567 void init(BufferParams const &, row_type rows_arg,
568 col_type columns_arg);
570 void set_row_column_number_info();
571 /// Returns true if a complete update is necessary, otherwise false
572 bool setWidthOfMulticolCell(idx_type cell, int new_width);
574 void recalculateMulticolumnsOfColumn(col_type column);
575 /// Returns true if change
576 void calculate_width_of_column(col_type column);
578 bool calculate_width_of_column_NMC(col_type column); // no multi cells
580 void calculate_width_of_tabular();
582 void delete_column(col_type column);
584 idx_type cells_in_multicolumn(idx_type cell) const;
586 BoxType useParbox(idx_type cell) const;
588 // helper function for Latex returns number of newlines
590 int TeXTopHLine(lyx::odocstream &, row_type row) const;
592 int TeXBottomHLine(lyx::odocstream &, row_type row) const;
594 int TeXCellPreamble(lyx::odocstream &, idx_type cell) const;
596 int TeXCellPostamble(lyx::odocstream &, idx_type cell) const;
598 int TeXLongtableHeaderFooter(lyx::odocstream &, Buffer const & buf,
599 OutputParams const &) const;
601 bool isValidRow(row_type const row) const;
603 int TeXRow(lyx::odocstream &, row_type const row, Buffer const & buf,
604 OutputParams const &) const;
606 // helper function for ASCII returns number of newlines
608 int asciiTopHLine(lyx::odocstream &, row_type row,
609 std::vector<unsigned int> const &) const;
611 int asciiBottomHLine(lyx::odocstream &, row_type row,
612 std::vector<unsigned int> const &) const;
614 int asciiPrintCell(Buffer const &, lyx::odocstream &,
615 OutputParams const &,
616 idx_type cell, row_type row, col_type column,
617 std::vector<unsigned int> const &,
618 bool onlydata) const;
619 /// auxiliary function for docbook
620 int docbookRow(Buffer const & buf, std::ostream & os, row_type,
621 OutputParams const &) const;
624 /// renumber cells after structural changes